| 1 | // Implements HTTP request and response signing and verification. Supports the |
| 2 | // major MAC and asymmetric key signature algorithms. It has several safety |
| 3 | // restrictions: One, none of the widely known non-cryptographically safe |
| 4 | // algorithms are permitted; Two, the RSA SHA256 algorithms must be available in |
| 5 | // the binary (and it should, barring export restrictions); Finally, the library |
| 6 | // assumes either the 'Authorizationn' or 'Signature' headers are to be set (but |
| 7 | // not both). |
| 8 | package httpsig |
| 9 | |
| 10 | import ( |
| 11 | "crypto" |
| 12 | "fmt" |
| 13 | "net/http" |
| 14 | "strings" |
| 15 | "time" |
| 16 | |
| 17 | "golang.org/x/crypto/ssh" |
| 18 | ) |
| 19 | |
| 20 | // Algorithm specifies a cryptography secure algorithm for signing HTTP requests |
| 21 | // and responses. |
| 22 | type Algorithm string |
| 23 | |
| 24 | const ( |
| 25 | // MAC-based algoirthms. |
| 26 | HMAC_SHA224 Algorithm = hmacPrefix + "-" + sha224String |
| 27 | HMAC_SHA256 Algorithm = hmacPrefix + "-" + sha256String |
| 28 | HMAC_SHA384 Algorithm = hmacPrefix + "-" + sha384String |
| 29 | HMAC_SHA512 Algorithm = hmacPrefix + "-" + sha512String |
| 30 | HMAC_SHA3_224 Algorithm = hmacPrefix + "-" + sha3_224String |
| 31 | HMAC_SHA3_256 Algorithm = hmacPrefix + "-" + sha3_256String |
| 32 | HMAC_SHA3_384 Algorithm = hmacPrefix + "-" + sha3_384String |
| 33 | HMAC_SHA3_512 Algorithm = hmacPrefix + "-" + sha3_512String |
| 34 | HMAC_SHA512_224 Algorithm = hmacPrefix + "-" + sha512_224String |
| 35 | HMAC_SHA512_256 Algorithm = hmacPrefix + "-" + sha512_256String |
| 36 | HMAC_BLAKE2S_256 Algorithm = hmacPrefix + "-" + blake2s_256String |
| 37 | HMAC_BLAKE2B_256 Algorithm = hmacPrefix + "-" + blake2b_256String |
| 38 | HMAC_BLAKE2B_384 Algorithm = hmacPrefix + "-" + blake2b_384String |
| 39 | HMAC_BLAKE2B_512 Algorithm = hmacPrefix + "-" + blake2b_512String |
| 40 | BLAKE2S_256 Algorithm = blake2s_256String |
| 41 | BLAKE2B_256 Algorithm = blake2b_256String |
| 42 | BLAKE2B_384 Algorithm = blake2b_384String |
| 43 | BLAKE2B_512 Algorithm = blake2b_512String |
| 44 | // RSA-based algorithms. |
| 45 | RSA_SHA224 Algorithm = rsaPrefix + "-" + sha224String |
| 46 | // RSA_SHA256 is the default algorithm. |
| 47 | RSA_SHA256 Algorithm = rsaPrefix + "-" + sha256String |
| 48 | RSA_SHA384 Algorithm = rsaPrefix + "-" + sha384String |
| 49 | RSA_SHA512 Algorithm = rsaPrefix + "-" + sha512String |
| 50 | // ECDSA algorithms |
| 51 | ECDSA_SHA224 Algorithm = ecdsaPrefix + "-" + sha224String |
| 52 | ECDSA_SHA256 Algorithm = ecdsaPrefix + "-" + sha256String |
| 53 | ECDSA_SHA384 Algorithm = ecdsaPrefix + "-" + sha384String |
| 54 | ECDSA_SHA512 Algorithm = ecdsaPrefix + "-" + sha512String |
| 55 | // ED25519 algorithms |
| 56 | // can only be SHA512 |
| 57 | ED25519 Algorithm = ed25519Prefix |
| 58 | |
| 59 | // Just because you can glue things together, doesn't mean they will |
| 60 | // work. The following options are not supported. |
| 61 | rsa_SHA3_224 Algorithm = rsaPrefix + "-" + sha3_224String |
| 62 | rsa_SHA3_256 Algorithm = rsaPrefix + "-" + sha3_256String |
| 63 | rsa_SHA3_384 Algorithm = rsaPrefix + "-" + sha3_384String |
| 64 | rsa_SHA3_512 Algorithm = rsaPrefix + "-" + sha3_512String |
| 65 | rsa_SHA512_224 Algorithm = rsaPrefix + "-" + sha512_224String |
| 66 | rsa_SHA512_256 Algorithm = rsaPrefix + "-" + sha512_256String |
| 67 | rsa_BLAKE2S_256 Algorithm = rsaPrefix + "-" + blake2s_256String |
| 68 | rsa_BLAKE2B_256 Algorithm = rsaPrefix + "-" + blake2b_256String |
| 69 | rsa_BLAKE2B_384 Algorithm = rsaPrefix + "-" + blake2b_384String |
| 70 | rsa_BLAKE2B_512 Algorithm = rsaPrefix + "-" + blake2b_512String |
| 71 | ) |
| 72 | |
| 73 | // HTTP Signatures can be applied to different HTTP headers, depending on the |
| 74 | // expected application behavior. |
| 75 | type SignatureScheme string |
| 76 | |
| 77 | const ( |
| 78 | // Signature will place the HTTP Signature into the 'Signature' HTTP |
| 79 | // header. |
| 80 | Signature SignatureScheme = "Signature" |
| 81 | // Authorization will place the HTTP Signature into the 'Authorization' |
| 82 | // HTTP header. |
| 83 | Authorization SignatureScheme = "Authorization" |
| 84 | ) |
| 85 | |
| 86 | const ( |
| 87 | // The HTTP Signatures specification uses the "Signature" auth-scheme |
| 88 | // for the Authorization header. This is coincidentally named, but not |
| 89 | // semantically the same, as the "Signature" HTTP header value. |
| 90 | signatureAuthScheme = "Signature" |
| 91 | ) |
| 92 | |
| 93 | // There are subtle differences to the values in the header. The Authorization |
| 94 | // header has an 'auth-scheme' value that must be prefixed to the rest of the |
| 95 | // key and values. |
| 96 | func (s SignatureScheme) authScheme() string { |
| 97 | switch s { |
| 98 | case Authorization: |
| 99 | return signatureAuthScheme |
| 100 | default: |
| 101 | return "" |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Signers will sign HTTP requests or responses based on the algorithms and |
| 106 | // headers selected at creation time. |
| 107 | // |
| 108 | // Signers are not safe to use between multiple goroutines. |
| 109 | // |
| 110 | // Note that signatures do set the deprecated 'algorithm' parameter for |
| 111 | // backwards compatibility. |
| 112 | type Signer interface { |
| 113 | // SignRequest signs the request using a private key. The public key id |
| 114 | // is used by the HTTP server to identify which key to use to verify the |
| 115 | // signature. |
| 116 | // |
| 117 | // If the Signer was created using a MAC based algorithm, then the key |
| 118 | // is expected to be of type []byte. If the Signer was created using an |
| 119 | // RSA based algorithm, then the private key is expected to be of type |
| 120 | // *rsa.PrivateKey. |
| 121 | // |
| 122 | // A Digest (RFC 3230) will be added to the request. The body provided |
| 123 | // must match the body used in the request, and is allowed to be nil. |
| 124 | // The Digest ensures the request body is not tampered with in flight, |
| 125 | // and if the signer is created to also sign the "Digest" header, the |
| 126 | // HTTP Signature will then ensure both the Digest and body are not both |
| 127 | // modified to maliciously represent different content. |
| 128 | SignRequest(pKey crypto.PrivateKey, pubKeyId string, r *http.Request, body []byte) error |
| 129 | // SignResponse signs the response using a private key. The public key |
| 130 | // id is used by the HTTP client to identify which key to use to verify |
| 131 | // the signature. |
| 132 | // |
| 133 | // If the Signer was created using a MAC based algorithm, then the key |
| 134 | // is expected to be of type []byte. If the Signer was created using an |
| 135 | // RSA based algorithm, then the private key is expected to be of type |
| 136 | // *rsa.PrivateKey. |
| 137 | // |
| 138 | // A Digest (RFC 3230) will be added to the response. The body provided |
| 139 | // must match the body written in the response, and is allowed to be |
| 140 | // nil. The Digest ensures the response body is not tampered with in |
| 141 | // flight, and if the signer is created to also sign the "Digest" |
| 142 | // header, the HTTP Signature will then ensure both the Digest and body |
| 143 | // are not both modified to maliciously represent different content. |
| 144 | SignResponse(pKey crypto.PrivateKey, pubKeyId string, r http.ResponseWriter, body []byte) error |
| 145 | } |
| 146 | |
| 147 | // NewSigner creates a new Signer with the provided algorithm preferences to |
| 148 | // make HTTP signatures. Only the first available algorithm will be used, which |
| 149 | // is returned by this function along with the Signer. If none of the preferred |
| 150 | // algorithms were available, then the default algorithm is used. The headers |
| 151 | // specified will be included into the HTTP signatures. |
| 152 | // |
| 153 | // The Digest will also be calculated on a request's body using the provided |
| 154 | // digest algorithm, if "Digest" is one of the headers listed. |
| 155 | // |
| 156 | // The provided scheme determines which header is populated with the HTTP |
| 157 | // Signature. |
| 158 | // |
| 159 | // An error is returned if an unknown or a known cryptographically insecure |
| 160 | // Algorithm is provided. |
| 161 | func NewSigner(prefs []Algorithm, dAlgo DigestAlgorithm, headers []string, scheme SignatureScheme, expiresIn int64) (Signer, Algorithm, error) { |
| 162 | for _, pref := range prefs { |
| 163 | s, err := newSigner(pref, dAlgo, headers, scheme, expiresIn) |
| 164 | if err != nil { |
| 165 | continue |
| 166 | } |
| 167 | return s, pref, err |
| 168 | } |
| 169 | s, err := newSigner(defaultAlgorithm, dAlgo, headers, scheme, expiresIn) |
| 170 | return s, defaultAlgorithm, err |
| 171 | } |
| 172 | |
| 173 | // Signers will sign HTTP requests or responses based on the algorithms and |
| 174 | // headers selected at creation time. |
| 175 | // |
| 176 | // Signers are not safe to use between multiple goroutines. |
| 177 | // |
| 178 | // Note that signatures do set the deprecated 'algorithm' parameter for |
| 179 | // backwards compatibility. |
| 180 | type SSHSigner interface { |
| 181 | // SignRequest signs the request using ssh.Signer. |
| 182 | // The public key id is used by the HTTP server to identify which key to use |
| 183 | // to verify the signature. |
| 184 | // |
| 185 | // A Digest (RFC 3230) will be added to the request. The body provided |
| 186 | // must match the body used in the request, and is allowed to be nil. |
| 187 | // The Digest ensures the request body is not tampered with in flight, |
| 188 | // and if the signer is created to also sign the "Digest" header, the |
| 189 | // HTTP Signature will then ensure both the Digest and body are not both |
| 190 | // modified to maliciously represent different content. |
| 191 | SignRequest(pubKeyId string, r *http.Request, body []byte) error |
| 192 | // SignResponse signs the response using ssh.Signer. The public key |
| 193 | // id is used by the HTTP client to identify which key to use to verify |
| 194 | // the signature. |
| 195 | // |
| 196 | // A Digest (RFC 3230) will be added to the response. The body provided |
| 197 | // must match the body written in the response, and is allowed to be |
| 198 | // nil. The Digest ensures the response body is not tampered with in |
| 199 | // flight, and if the signer is created to also sign the "Digest" |
| 200 | // header, the HTTP Signature will then ensure both the Digest and body |
| 201 | // are not both modified to maliciously represent different content. |
| 202 | SignResponse(pubKeyId string, r http.ResponseWriter, body []byte) error |
| 203 | } |
| 204 | |
| 205 | // NewwSSHSigner creates a new Signer using the specified ssh.Signer |
| 206 | // At the moment only ed25519 ssh keys are supported. |
| 207 | // The headers specified will be included into the HTTP signatures. |
| 208 | // |
| 209 | // The Digest will also be calculated on a request's body using the provided |
| 210 | // digest algorithm, if "Digest" is one of the headers listed. |
| 211 | // |
| 212 | // The provided scheme determines which header is populated with the HTTP |
| 213 | // Signature. |
| 214 | func NewSSHSigner(s ssh.Signer, dAlgo DigestAlgorithm, headers []string, scheme SignatureScheme, expiresIn int64) (SSHSigner, Algorithm, error) { |
| 215 | sshAlgo := getSSHAlgorithm(s.PublicKey().Type()) |
| 216 | if sshAlgo == "" { |
| 217 | return nil, "", fmt.Errorf("key type: %s not supported yet.", s.PublicKey().Type()) |
| 218 | } |
| 219 | |
| 220 | signer, err := newSSHSigner(s, sshAlgo, dAlgo, headers, scheme, expiresIn) |
| 221 | if err != nil { |
| 222 | return nil, "", err |
| 223 | } |
| 224 | |
| 225 | return signer, sshAlgo, nil |
| 226 | } |
| 227 | |
| 228 | func getSSHAlgorithm(pkType string) Algorithm { |
| 229 | switch { |
| 230 | case strings.HasPrefix(pkType, sshPrefix+"-"+ed25519Prefix): |
| 231 | return ED25519 |
| 232 | case strings.HasPrefix(pkType, sshPrefix+"-"+rsaPrefix): |
| 233 | return RSA_SHA256 |
| 234 | } |
| 235 | |
| 236 | return "" |
| 237 | } |
| 238 | |
| 239 | // Verifier verifies HTTP Signatures. |
| 240 | // |
| 241 | // It will determine which of the supported headers has the parameters |
| 242 | // that define the signature. |
| 243 | // |
| 244 | // Verifiers are not safe to use between multiple goroutines. |
| 245 | // |
| 246 | // Note that verification ignores the deprecated 'algorithm' parameter. |
| 247 | type Verifier interface { |
| 248 | // KeyId gets the public key id that the signature is signed with. |
| 249 | // |
| 250 | // Note that the application is expected to determine the algorithm |
| 251 | // used based on metadata or out-of-band information for this key id. |
| 252 | KeyId() string |
| 253 | // Verify accepts the public key specified by KeyId and returns an |
| 254 | // error if verification fails or if the signature is malformed. The |
| 255 | // algorithm must be the one used to create the signature in order to |
| 256 | // pass verification. The algorithm is determined based on metadata or |
| 257 | // out-of-band information for the key id. |
| 258 | // |
| 259 | // If the signature was created using a MAC based algorithm, then the |
| 260 | // key is expected to be of type []byte. If the signature was created |
| 261 | // using an RSA based algorithm, then the public key is expected to be |
| 262 | // of type *rsa.PublicKey. |
| 263 | Verify(pKey crypto.PublicKey, algo Algorithm) error |
| 264 | } |
| 265 | |
| 266 | const ( |
| 267 | // host is treated specially because golang may not include it in the |
| 268 | // request header map on the server side of a request. |
| 269 | hostHeader = "Host" |
| 270 | ) |
| 271 | |
| 272 | // NewVerifier verifies the given request. It returns an error if the HTTP |
| 273 | // Signature parameters are not present in any headers, are present in more than |
| 274 | // one header, are malformed, or are missing required parameters. It ignores |
| 275 | // unknown HTTP Signature parameters. |
| 276 | func NewVerifier(r *http.Request) (Verifier, error) { |
| 277 | h := r.Header |
| 278 | if _, hasHostHeader := h[hostHeader]; len(r.Host) > 0 && !hasHostHeader { |
| 279 | h[hostHeader] = []string{r.Host} |
| 280 | } |
| 281 | return newVerifier(h, func(h http.Header, toInclude []string, created int64, expires int64) (string, error) { |
| 282 | return signatureString(h, toInclude, addRequestTarget(r), created, expires) |
| 283 | }) |
| 284 | } |
| 285 | |
| 286 | // NewResponseVerifier verifies the given response. It returns errors under the |
| 287 | // same conditions as NewVerifier. |
| 288 | func NewResponseVerifier(r *http.Response) (Verifier, error) { |
| 289 | return newVerifier(r.Header, func(h http.Header, toInclude []string, created int64, expires int64) (string, error) { |
| 290 | return signatureString(h, toInclude, requestTargetNotPermitted, created, expires) |
| 291 | }) |
| 292 | } |
| 293 | |
| 294 | func newSSHSigner(sshSigner ssh.Signer, algo Algorithm, dAlgo DigestAlgorithm, headers []string, scheme SignatureScheme, expiresIn int64) (SSHSigner, error) { |
| 295 | var expires, created int64 = 0, 0 |
| 296 | |
| 297 | if expiresIn != 0 { |
| 298 | created = time.Now().Unix() |
| 299 | expires = created + expiresIn |
| 300 | } |
| 301 | |
| 302 | s, err := signerFromSSHSigner(sshSigner, string(algo)) |
| 303 | if err != nil { |
| 304 | return nil, fmt.Errorf("no crypto implementation available for ssh algo %q: %s", algo, err) |
| 305 | } |
| 306 | |
| 307 | a := &asymmSSHSigner{ |
| 308 | asymmSigner: &asymmSigner{ |
| 309 | s: s, |
| 310 | dAlgo: dAlgo, |
| 311 | headers: headers, |
| 312 | targetHeader: scheme, |
| 313 | prefix: scheme.authScheme(), |
| 314 | created: created, |
| 315 | expires: expires, |
| 316 | }, |
| 317 | } |
| 318 | |
| 319 | return a, nil |
| 320 | } |
| 321 | |
| 322 | func newSigner(algo Algorithm, dAlgo DigestAlgorithm, headers []string, scheme SignatureScheme, expiresIn int64) (Signer, error) { |
| 323 | var expires, created int64 = 0, 0 |
| 324 | if expiresIn != 0 { |
| 325 | created = time.Now().Unix() |
| 326 | expires = created + expiresIn |
| 327 | } |
| 328 | |
| 329 | s, err := signerFromString(string(algo)) |
| 330 | if err == nil { |
| 331 | a := &asymmSigner{ |
| 332 | s: s, |
| 333 | dAlgo: dAlgo, |
| 334 | headers: headers, |
| 335 | targetHeader: scheme, |
| 336 | prefix: scheme.authScheme(), |
| 337 | created: created, |
| 338 | expires: expires, |
| 339 | } |
| 340 | return a, nil |
| 341 | } |
| 342 | m, err := macerFromString(string(algo)) |
| 343 | if err != nil { |
| 344 | return nil, fmt.Errorf("no crypto implementation available for %q: %s", algo, err) |
| 345 | } |
| 346 | c := &macSigner{ |
| 347 | m: m, |
| 348 | dAlgo: dAlgo, |
| 349 | headers: headers, |
| 350 | targetHeader: scheme, |
| 351 | prefix: scheme.authScheme(), |
| 352 | created: created, |
| 353 | expires: expires, |
| 354 | } |
| 355 | return c, nil |
| 356 | } |