acme: hardcode and remove ExternalAccountBinding.Algorithm

HMAC-SHA256 is a perfectly fine MAC algorithm, and there is no need to
ask the user to choose one.

This does break compatibility with the previous API, but it had been
live only for a weekend, so hopefully still in a window in which we can
make changes with a limited blast radius.

Updates golang/go#41430

Change-Id: I03741a545b25b9fcc147760cd20e9d7029844a6c
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/279453
Trust: Filippo Valsorda <filippo@golang.org>
Run-TryBot: Filippo Valsorda <filippo@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: James Kasten <jdkasten@google.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
This commit is contained in:
Filippo Valsorda
2020-12-21 18:15:55 +00:00
parent 9d13527586
commit eec23a3978
6 changed files with 35 additions and 103 deletions
+21 -36
View File
@@ -11,27 +11,15 @@ import (
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/sha256" "crypto/sha256"
"crypto/sha512"
_ "crypto/sha512" // need for EC keys _ "crypto/sha512" // need for EC keys
"encoding/asn1" "encoding/asn1"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"hash"
"math/big" "math/big"
) )
// MACAlgorithm represents a JWS MAC signature algorithm.
// See https://tools.ietf.org/html/rfc7518#section-3.1 for more details.
type MACAlgorithm string
const (
MACAlgorithmHS256 = MACAlgorithm("HS256")
MACAlgorithmHS384 = MACAlgorithm("HS384")
MACAlgorithmHS512 = MACAlgorithm("HS512")
)
// keyID is the account identity provided by a CA during registration. // keyID is the account identity provided by a CA during registration.
type keyID string type keyID string
@@ -101,24 +89,35 @@ func jwsEncodeJSON(claimset interface{}, key crypto.Signer, kid keyID, nonce, ur
return json.Marshal(&enc) return json.Marshal(&enc)
} }
// jwsWithMAC creates and signs a JWS using the given key and algorithm. // jwsWithMAC creates and signs a JWS using the given key and the HS256
// "rawProtected" and "rawPayload" should not be base64-URL-encoded. // algorithm. kid and url are included in the protected header. rawPayload
func jwsWithMAC(key []byte, alg MACAlgorithm, rawProtected, rawPayload []byte) (*jsonWebSignature, error) { // should not be base64-URL-encoded.
func jwsWithMAC(key []byte, kid, url string, rawPayload []byte) (*jsonWebSignature, error) {
if len(key) == 0 { if len(key) == 0 {
return nil, errors.New("acme: cannot sign JWS with an empty MAC key") return nil, errors.New("acme: cannot sign JWS with an empty MAC key")
} }
header := struct {
Algorithm string `json:"alg"`
KID string `json:"kid"`
URL string `json:"url,omitempty"`
}{
// Only HMAC-SHA256 is supported.
Algorithm: "HS256",
KID: kid,
URL: url,
}
rawProtected, err := json.Marshal(header)
if err != nil {
return nil, err
}
protected := base64.RawURLEncoding.EncodeToString(rawProtected) protected := base64.RawURLEncoding.EncodeToString(rawProtected)
payload := base64.RawURLEncoding.EncodeToString(rawPayload) payload := base64.RawURLEncoding.EncodeToString(rawPayload)
// Only HMACs are currently supported. h := hmac.New(sha256.New, key)
hmac, err := newHMAC(key, alg) if _, err := h.Write([]byte(protected + "." + payload)); err != nil {
if err != nil {
return nil, err return nil, err
} }
if _, err := hmac.Write([]byte(protected + "." + payload)); err != nil { mac := h.Sum(nil)
return nil, err
}
mac := hmac.Sum(nil)
return &jsonWebSignature{ return &jsonWebSignature{
Protected: protected, Protected: protected,
@@ -218,20 +217,6 @@ func jwsHasher(pub crypto.PublicKey) (string, crypto.Hash) {
return "", 0 return "", 0
} }
// newHMAC returns an appropriate HMAC for the given MACAlgorithm.
func newHMAC(key []byte, alg MACAlgorithm) (hash.Hash, error) {
switch alg {
case MACAlgorithmHS256:
return hmac.New(sha256.New, key), nil
case MACAlgorithmHS384:
return hmac.New(sha512.New384, key), nil
case MACAlgorithmHS512:
return hmac.New(sha512.New, key), nil
default:
return nil, fmt.Errorf("acme: unsupported MAC algorithm: %v", alg)
}
}
// JWKThumbprint creates a JWK thumbprint out of pub // JWKThumbprint creates a JWK thumbprint out of pub
// as specified in https://tools.ietf.org/html/rfc7638. // as specified in https://tools.ietf.org/html/rfc7638.
func JWKThumbprint(pub crypto.PublicKey) (string, error) { func JWKThumbprint(pub crypto.PublicKey) (string, error) {
+4 -49
View File
@@ -397,8 +397,6 @@ func TestJWSWithMAC(t *testing.T) {
// Example from RFC 7520 Section 4.4.3. // Example from RFC 7520 Section 4.4.3.
// https://tools.ietf.org/html/rfc7520#section-4.4.3 // https://tools.ietf.org/html/rfc7520#section-4.4.3
b64Key := "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg" b64Key := "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg"
alg := MACAlgorithmHS256
rawProtected := []byte(`{"alg":"HS256","kid":"018c0ae5-4d9b-471b-bfd6-eef314bc7037"}`)
rawPayload := []byte("It\xe2\x80\x99s a dangerous business, Frodo, going out your " + rawPayload := []byte("It\xe2\x80\x99s a dangerous business, Frodo, going out your " +
"door. You step onto the road, and if you don't keep your feet, " + "door. You step onto the road, and if you don't keep your feet, " +
"there\xe2\x80\x99s no knowing where you might be swept off " + "there\xe2\x80\x99s no knowing where you might be swept off " +
@@ -416,7 +414,7 @@ func TestJWSWithMAC(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to decode key: %q", b64Key) t.Fatalf("unable to decode key: %q", b64Key)
} }
got, err := jwsWithMAC(key, alg, rawProtected, rawPayload) got, err := jwsWithMAC(key, "018c0ae5-4d9b-471b-bfd6-eef314bc7037", "", rawPayload)
if err != nil { if err != nil {
t.Fatalf("jwsWithMAC() = %q", err) t.Fatalf("jwsWithMAC() = %q", err)
} }
@@ -432,22 +430,9 @@ func TestJWSWithMAC(t *testing.T) {
} }
func TestJWSWithMACError(t *testing.T) { func TestJWSWithMACError(t *testing.T) {
tt := []struct { p := "{}"
desc string if _, err := jwsWithMAC(nil, "", "", []byte(p)); err == nil {
alg MACAlgorithm t.Errorf("jwsWithMAC(nil, ...) = success; want err")
key []byte
}{
{"Unknown Algorithm", MACAlgorithm("UNKNOWN-ALG"), []byte("hmac-key")},
{"Empty Key", MACAlgorithmHS256, nil},
}
for _, tc := range tt {
tc := tc
t.Run(string(tc.desc), func(t *testing.T) {
p := "{}"
if _, err := jwsWithMAC(tc.key, tc.alg, []byte(p), []byte(p)); err == nil {
t.Errorf("jwsWithMAC(%v, %v, %s, %s) = success; want err", tc.key, tc.alg, p, p)
}
})
} }
} }
@@ -525,33 +510,3 @@ func TestJWKThumbprintErrUnsupportedKey(t *testing.T) {
t.Errorf("err = %q; want %q", err, ErrUnsupportedKey) t.Errorf("err = %q; want %q", err, ErrUnsupportedKey)
} }
} }
func TestNewHMAC(t *testing.T) {
tt := []struct {
alg MACAlgorithm
wantSize int
}{
{MACAlgorithmHS256, 32},
{MACAlgorithmHS384, 48},
{MACAlgorithmHS512, 64},
}
for _, tc := range tt {
tc := tc
t.Run(string(tc.alg), func(t *testing.T) {
h, err := newHMAC([]byte("key"), tc.alg)
if err != nil {
t.Fatalf("newHMAC(%v) = %q", tc.alg, err)
}
gotSize := len(h.Sum(nil))
if gotSize != tc.wantSize {
t.Errorf("HMAC produced signature with unexpected length; got %d want %d", gotSize, tc.wantSize)
}
})
}
}
func TestNewHMACError(t *testing.T) {
if h, err := newHMAC([]byte("key"), MACAlgorithm("UNKNOWN-ALG")); err == nil {
t.Errorf("newHMAC(UNKNOWN-ALG) = %T, nil; want error", h)
}
}
+1 -4
View File
@@ -5,7 +5,6 @@
package acme package acme
import ( import (
"bytes"
"context" "context"
"crypto" "crypto"
"encoding/base64" "encoding/base64"
@@ -93,9 +92,7 @@ func (c *Client) encodeExternalAccountBinding(eab *ExternalAccountBinding) (*jso
if err != nil { if err != nil {
return nil, err return nil, err
} }
var rProtected bytes.Buffer return jwsWithMAC(eab.Key, eab.KID, c.dir.RegURL, []byte(jwk))
fmt.Fprintf(&rProtected, `{"alg":%q,"kid":%q,"url":%q}`, eab.Algorithm, eab.KID, c.dir.RegURL)
return jwsWithMAC(eab.Key, eab.Algorithm, rProtected.Bytes(), []byte(jwk))
} }
// updateRegRFC is equivalent to c.UpdateReg but for CAs implementing RFC 8555. // updateRegRFC is equivalent to c.UpdateReg but for CAs implementing RFC 8555.
+5 -6
View File
@@ -351,9 +351,8 @@ func TestRFC_Register(t *testing.T) {
func TestRFC_RegisterExternalAccountBinding(t *testing.T) { func TestRFC_RegisterExternalAccountBinding(t *testing.T) {
eab := &ExternalAccountBinding{ eab := &ExternalAccountBinding{
KID: "kid-1", KID: "kid-1",
Key: []byte("secret"), Key: []byte("secret"),
Algorithm: MACAlgorithmHS256,
} }
type protected struct { type protected struct {
@@ -403,10 +402,10 @@ func TestRFC_RegisterExternalAccountBinding(t *testing.T) {
if prot.KID != eab.KID { if prot.KID != eab.KID {
t.Errorf("j.ExternalAccountBinding.KID = %s; want %s", prot.KID, eab.KID) t.Errorf("j.ExternalAccountBinding.KID = %s; want %s", prot.KID, eab.KID)
} }
// Ensure same Algorithm. // Ensure expected Algorithm.
if prot.Algorithm != string(eab.Algorithm) { if prot.Algorithm != "HS256" {
t.Errorf("j.ExternalAccountBinding.Alg = %s; want %s", t.Errorf("j.ExternalAccountBinding.Alg = %s; want %s",
prot.Algorithm, eab.Algorithm) prot.Algorithm, "HS256")
} }
// Ensure same URL as outer JWS. // Ensure same URL as outer JWS.
+1 -4
View File
@@ -217,13 +217,10 @@ type ExternalAccountBinding struct {
// Key is the bytes of the symmetric key that the CA provides to identify // Key is the bytes of the symmetric key that the CA provides to identify
// the account. Key must correspond to the KID. // the account. Key must correspond to the KID.
Key []byte Key []byte
// Algorithm used to sign the JWS.
Algorithm MACAlgorithm
} }
func (e *ExternalAccountBinding) String() string { func (e *ExternalAccountBinding) String() string {
return fmt.Sprintf("&{KID: %q, Key: redacted, Algorithm: %v}", e.KID, e.Algorithm) return fmt.Sprintf("&{KID: %q, Key: redacted}", e.KID)
} }
// Directory is ACME server discovery data. // Directory is ACME server discovery data.
+3 -4
View File
@@ -13,12 +13,11 @@ import (
func TestExternalAccountBindingString(t *testing.T) { func TestExternalAccountBindingString(t *testing.T) {
eab := ExternalAccountBinding{ eab := ExternalAccountBinding{
KID: "kid", KID: "kid",
Key: []byte("key"), Key: []byte("key"),
Algorithm: MACAlgorithmHS256,
} }
got := eab.String() got := eab.String()
want := `&{KID: "kid", Key: redacted, Algorithm: HS256}` want := `&{KID: "kid", Key: redacted}`
if got != want { if got != want {
t.Errorf("eab.String() = %q, want: %q", got, want) t.Errorf("eab.String() = %q, want: %q", got, want)
} }