Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package acme |
| 6 | |
| 7 | import ( |
| 8 | "crypto" |
Anmol Sethi | e0d166c | 2016-08-04 00:47:19 -0400 | [diff] [blame] | 9 | "crypto/ecdsa" |
James Kasten | 9d13527 | 2020-11-11 15:47:14 -0800 | [diff] [blame] | 10 | "crypto/hmac" |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 11 | "crypto/rand" |
| 12 | "crypto/rsa" |
| 13 | "crypto/sha256" |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 14 | _ "crypto/sha512" // need for EC keys |
edef | e7c4368 | 2019-11-25 19:39:25 +0000 | [diff] [blame] | 15 | "encoding/asn1" |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 16 | "encoding/base64" |
| 17 | "encoding/json" |
James Kasten | 9d13527 | 2020-11-11 15:47:14 -0800 | [diff] [blame] | 18 | "errors" |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 19 | "fmt" |
| 20 | "math/big" |
| 21 | ) |
| 22 | |
Roland Shoemaker | 30dcbda | 2021-10-08 11:41:44 -0700 | [diff] [blame] | 23 | // KeyID is the account key identity provided by a CA during registration. |
| 24 | type KeyID string |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 25 | |
| 26 | // noKeyID indicates that jwsEncodeJSON should compute and use JWK instead of a KID. |
| 27 | // See jwsEncodeJSON for details. |
Roland Shoemaker | 30dcbda | 2021-10-08 11:41:44 -0700 | [diff] [blame] | 28 | const noKeyID = KeyID("") |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 29 | |
Alex Vaghin | 8834368 | 2019-08-31 21:44:59 +0200 | [diff] [blame] | 30 | // noPayload indicates jwsEncodeJSON will encode zero-length octet string |
| 31 | // in a JWS request. This is called POST-as-GET in RFC 8555 and is used to make |
| 32 | // authenticated GET requests via POSTing with an empty payload. |
| 33 | // See https://tools.ietf.org/html/rfc8555#section-6.3 for more details. |
| 34 | const noPayload = "" |
| 35 | |
Jason Baker | 403b017 | 2022-05-13 17:19:05 +0000 | [diff] [blame] | 36 | // noNonce indicates that the nonce should be omitted from the protected header. |
| 37 | // See jwsEncodeJSON for details. |
| 38 | const noNonce = "" |
| 39 | |
James Kasten | 9d13527 | 2020-11-11 15:47:14 -0800 | [diff] [blame] | 40 | // jsonWebSignature can be easily serialized into a JWS following |
| 41 | // https://tools.ietf.org/html/rfc7515#section-3.2. |
| 42 | type jsonWebSignature struct { |
| 43 | Protected string `json:"protected"` |
| 44 | Payload string `json:"payload"` |
| 45 | Sig string `json:"signature"` |
| 46 | } |
| 47 | |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 48 | // jwsEncodeJSON signs claimset using provided key and a nonce. |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 49 | // The result is serialized in JSON format containing either kid or jwk |
Roland Shoemaker | 30dcbda | 2021-10-08 11:41:44 -0700 | [diff] [blame] | 50 | // fields based on the provided KeyID value. |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 51 | // |
Jason Baker | 403b017 | 2022-05-13 17:19:05 +0000 | [diff] [blame] | 52 | // The claimset is marshalled using json.Marshal unless it is a string. |
| 53 | // In which case it is inserted directly into the message. |
| 54 | // |
| 55 | // If kid is non-empty, its quoted value is inserted in the protected header |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 56 | // as "kid" field value. Otherwise, JWK is computed using jwkEncode and inserted |
Alex Vaghin | a832865 | 2019-08-28 23:16:55 +0200 | [diff] [blame] | 57 | // as "jwk" field value. The "jwk" and "kid" fields are mutually exclusive. |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 58 | // |
Jason Baker | 403b017 | 2022-05-13 17:19:05 +0000 | [diff] [blame] | 59 | // If nonce is non-empty, its quoted value is inserted in the protected header. |
| 60 | // |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 61 | // See https://tools.ietf.org/html/rfc7515#section-7. |
Roland Shoemaker | 30dcbda | 2021-10-08 11:41:44 -0700 | [diff] [blame] | 62 | func jwsEncodeJSON(claimset interface{}, key crypto.Signer, kid KeyID, nonce, url string) ([]byte, error) { |
Ben Burkert | 65fa2f7 | 2022-01-22 13:59:02 -0500 | [diff] [blame] | 63 | if key == nil { |
| 64 | return nil, errors.New("nil key") |
| 65 | } |
Alex Vaghin | bfa7d42 | 2018-10-26 11:44:37 -0700 | [diff] [blame] | 66 | alg, sha := jwsHasher(key.Public()) |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 67 | if alg == "" || !sha.Available() { |
| 68 | return nil, ErrUnsupportedKey |
| 69 | } |
Jason Baker | 403b017 | 2022-05-13 17:19:05 +0000 | [diff] [blame] | 70 | headers := struct { |
| 71 | Alg string `json:"alg"` |
| 72 | KID string `json:"kid,omitempty"` |
| 73 | JWK json.RawMessage `json:"jwk,omitempty"` |
| 74 | Nonce string `json:"nonce,omitempty"` |
| 75 | URL string `json:"url"` |
| 76 | }{ |
| 77 | Alg: alg, |
| 78 | Nonce: nonce, |
| 79 | URL: url, |
| 80 | } |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 81 | switch kid { |
| 82 | case noKeyID: |
| 83 | jwk, err := jwkEncode(key.Public()) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
Jason Baker | 403b017 | 2022-05-13 17:19:05 +0000 | [diff] [blame] | 87 | headers.JWK = json.RawMessage(jwk) |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 88 | default: |
Jason Baker | 403b017 | 2022-05-13 17:19:05 +0000 | [diff] [blame] | 89 | headers.KID = string(kid) |
Alex Vaghin | fa1a291 | 2019-08-23 16:55:35 +0200 | [diff] [blame] | 90 | } |
Jason Baker | 403b017 | 2022-05-13 17:19:05 +0000 | [diff] [blame] | 91 | phJSON, err := json.Marshal(headers) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | phead := base64.RawURLEncoding.EncodeToString([]byte(phJSON)) |
Alex Vaghin | 8834368 | 2019-08-31 21:44:59 +0200 | [diff] [blame] | 96 | var payload string |
Jason Baker | 403b017 | 2022-05-13 17:19:05 +0000 | [diff] [blame] | 97 | if val, ok := claimset.(string); ok { |
| 98 | payload = val |
| 99 | } else { |
Alex Vaghin | 8834368 | 2019-08-31 21:44:59 +0200 | [diff] [blame] | 100 | cs, err := json.Marshal(claimset) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | payload = base64.RawURLEncoding.EncodeToString(cs) |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 105 | } |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 106 | hash := sha.New() |
| 107 | hash.Write([]byte(phead + "." + payload)) |
| 108 | sig, err := jwsSign(key, sha, hash.Sum(nil)) |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
James Kasten | 9d13527 | 2020-11-11 15:47:14 -0800 | [diff] [blame] | 112 | enc := jsonWebSignature{ |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 113 | Protected: phead, |
| 114 | Payload: payload, |
| 115 | Sig: base64.RawURLEncoding.EncodeToString(sig), |
| 116 | } |
| 117 | return json.Marshal(&enc) |
| 118 | } |
| 119 | |
Filippo Valsorda | eec23a3 | 2020-12-21 17:57:36 +0100 | [diff] [blame] | 120 | // jwsWithMAC creates and signs a JWS using the given key and the HS256 |
| 121 | // algorithm. kid and url are included in the protected header. rawPayload |
| 122 | // should not be base64-URL-encoded. |
| 123 | func jwsWithMAC(key []byte, kid, url string, rawPayload []byte) (*jsonWebSignature, error) { |
James Kasten | 9d13527 | 2020-11-11 15:47:14 -0800 | [diff] [blame] | 124 | if len(key) == 0 { |
| 125 | return nil, errors.New("acme: cannot sign JWS with an empty MAC key") |
| 126 | } |
Filippo Valsorda | eec23a3 | 2020-12-21 17:57:36 +0100 | [diff] [blame] | 127 | header := struct { |
| 128 | Algorithm string `json:"alg"` |
| 129 | KID string `json:"kid"` |
| 130 | URL string `json:"url,omitempty"` |
| 131 | }{ |
| 132 | // Only HMAC-SHA256 is supported. |
| 133 | Algorithm: "HS256", |
| 134 | KID: kid, |
| 135 | URL: url, |
| 136 | } |
| 137 | rawProtected, err := json.Marshal(header) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
James Kasten | 9d13527 | 2020-11-11 15:47:14 -0800 | [diff] [blame] | 141 | protected := base64.RawURLEncoding.EncodeToString(rawProtected) |
| 142 | payload := base64.RawURLEncoding.EncodeToString(rawPayload) |
| 143 | |
Filippo Valsorda | eec23a3 | 2020-12-21 17:57:36 +0100 | [diff] [blame] | 144 | h := hmac.New(sha256.New, key) |
| 145 | if _, err := h.Write([]byte(protected + "." + payload)); err != nil { |
James Kasten | 9d13527 | 2020-11-11 15:47:14 -0800 | [diff] [blame] | 146 | return nil, err |
| 147 | } |
Filippo Valsorda | eec23a3 | 2020-12-21 17:57:36 +0100 | [diff] [blame] | 148 | mac := h.Sum(nil) |
James Kasten | 9d13527 | 2020-11-11 15:47:14 -0800 | [diff] [blame] | 149 | |
| 150 | return &jsonWebSignature{ |
| 151 | Protected: protected, |
| 152 | Payload: payload, |
| 153 | Sig: base64.RawURLEncoding.EncodeToString(mac), |
| 154 | }, nil |
| 155 | } |
| 156 | |
Anmol Sethi | e0d166c | 2016-08-04 00:47:19 -0400 | [diff] [blame] | 157 | // jwkEncode encodes public part of an RSA or ECDSA key into a JWK. |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 158 | // The result is also suitable for creating a JWK thumbprint. |
Anmol Sethi | e0d166c | 2016-08-04 00:47:19 -0400 | [diff] [blame] | 159 | // https://tools.ietf.org/html/rfc7517 |
| 160 | func jwkEncode(pub crypto.PublicKey) (string, error) { |
| 161 | switch pub := pub.(type) { |
| 162 | case *rsa.PublicKey: |
| 163 | // https://tools.ietf.org/html/rfc7518#section-6.3.1 |
| 164 | n := pub.N |
| 165 | e := big.NewInt(int64(pub.E)) |
| 166 | // Field order is important. |
| 167 | // See https://tools.ietf.org/html/rfc7638#section-3.3 for details. |
| 168 | return fmt.Sprintf(`{"e":"%s","kty":"RSA","n":"%s"}`, |
| 169 | base64.RawURLEncoding.EncodeToString(e.Bytes()), |
| 170 | base64.RawURLEncoding.EncodeToString(n.Bytes()), |
| 171 | ), nil |
| 172 | case *ecdsa.PublicKey: |
| 173 | // https://tools.ietf.org/html/rfc7518#section-6.2.1 |
| 174 | p := pub.Curve.Params() |
| 175 | n := p.BitSize / 8 |
| 176 | if p.BitSize%8 != 0 { |
| 177 | n++ |
| 178 | } |
| 179 | x := pub.X.Bytes() |
| 180 | if n > len(x) { |
| 181 | x = append(make([]byte, n-len(x)), x...) |
| 182 | } |
| 183 | y := pub.Y.Bytes() |
| 184 | if n > len(y) { |
| 185 | y = append(make([]byte, n-len(y)), y...) |
| 186 | } |
| 187 | // Field order is important. |
| 188 | // See https://tools.ietf.org/html/rfc7638#section-3.3 for details. |
| 189 | return fmt.Sprintf(`{"crv":"%s","kty":"EC","x":"%s","y":"%s"}`, |
| 190 | p.Name, |
| 191 | base64.RawURLEncoding.EncodeToString(x), |
| 192 | base64.RawURLEncoding.EncodeToString(y), |
| 193 | ), nil |
| 194 | } |
| 195 | return "", ErrUnsupportedKey |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 198 | // jwsSign signs the digest using the given key. |
Alex Vaghin | bfa7d42 | 2018-10-26 11:44:37 -0700 | [diff] [blame] | 199 | // The hash is unused for ECDSA keys. |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 200 | func jwsSign(key crypto.Signer, hash crypto.Hash, digest []byte) ([]byte, error) { |
edef | e7c4368 | 2019-11-25 19:39:25 +0000 | [diff] [blame] | 201 | switch pub := key.Public().(type) { |
| 202 | case *rsa.PublicKey: |
| 203 | return key.Sign(rand.Reader, digest, hash) |
| 204 | case *ecdsa.PublicKey: |
| 205 | sigASN1, err := key.Sign(rand.Reader, digest, hash) |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 206 | if err != nil { |
| 207 | return nil, err |
| 208 | } |
edef | e7c4368 | 2019-11-25 19:39:25 +0000 | [diff] [blame] | 209 | |
| 210 | var rs struct{ R, S *big.Int } |
| 211 | if _, err := asn1.Unmarshal(sigASN1, &rs); err != nil { |
| 212 | return nil, err |
| 213 | } |
| 214 | |
| 215 | rb, sb := rs.R.Bytes(), rs.S.Bytes() |
| 216 | size := pub.Params().BitSize / 8 |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 217 | if size%8 > 0 { |
| 218 | size++ |
| 219 | } |
| 220 | sig := make([]byte, size*2) |
| 221 | copy(sig[size-len(rb):], rb) |
| 222 | copy(sig[size*2-len(sb):], sb) |
| 223 | return sig, nil |
| 224 | } |
edef | e7c4368 | 2019-11-25 19:39:25 +0000 | [diff] [blame] | 225 | return nil, ErrUnsupportedKey |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // jwsHasher indicates suitable JWS algorithm name and a hash function |
| 229 | // to use for signing a digest with the provided key. |
| 230 | // It returns ("", 0) if the key is not supported. |
Alex Vaghin | bfa7d42 | 2018-10-26 11:44:37 -0700 | [diff] [blame] | 231 | func jwsHasher(pub crypto.PublicKey) (string, crypto.Hash) { |
| 232 | switch pub := pub.(type) { |
| 233 | case *rsa.PublicKey: |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 234 | return "RS256", crypto.SHA256 |
Alex Vaghin | bfa7d42 | 2018-10-26 11:44:37 -0700 | [diff] [blame] | 235 | case *ecdsa.PublicKey: |
| 236 | switch pub.Params().Name { |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 237 | case "P-256": |
| 238 | return "ES256", crypto.SHA256 |
| 239 | case "P-384": |
| 240 | return "ES384", crypto.SHA384 |
Alex Vaghin | 2b0eeec | 2016-12-16 22:11:23 +0000 | [diff] [blame] | 241 | case "P-521": |
Alex Vaghin | 3461a68 | 2016-08-21 13:39:17 +0200 | [diff] [blame] | 242 | return "ES512", crypto.SHA512 |
| 243 | } |
| 244 | } |
| 245 | return "", 0 |
| 246 | } |
| 247 | |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 248 | // JWKThumbprint creates a JWK thumbprint out of pub |
| 249 | // as specified in https://tools.ietf.org/html/rfc7638. |
Anmol Sethi | e0d166c | 2016-08-04 00:47:19 -0400 | [diff] [blame] | 250 | func JWKThumbprint(pub crypto.PublicKey) (string, error) { |
| 251 | jwk, err := jwkEncode(pub) |
| 252 | if err != nil { |
| 253 | return "", err |
| 254 | } |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 255 | b := sha256.Sum256([]byte(jwk)) |
Anmol Sethi | e0d166c | 2016-08-04 00:47:19 -0400 | [diff] [blame] | 256 | return base64.RawURLEncoding.EncodeToString(b[:]), nil |
Alex Vaghin | 1777f3b | 2016-03-18 12:12:46 +0000 | [diff] [blame] | 257 | } |