| 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 httpsigscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ |
| 9 | |
| 10 | import ( |
| 11 | "cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/" |
| 12 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 13 | "net/httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/" |
| 14 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 15 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 16 | |
| 17 | "golang.org/x/crypto/sshscip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/" |
| 18 | ) |
| 19 | |
| 20 | // Algorithm specifies a cryptography secure algorithm for signing HTTP requests |
| 21 | // and responses. |
| 22 | type Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# string |
| 23 | |
| 24 | const ( |
| 25 | // MAC-based algoirthms. |
| 26 | HMAC_SHA224scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA224. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha224Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha224String. |
| 27 | HMAC_SHA256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha256String. |
| 28 | HMAC_SHA384scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA384. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha384Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha384String. |
| 29 | HMAC_SHA512scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA512. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha512Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha512String. |
| 30 | HMAC_SHA3_224scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA3_224. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha3_224Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha3_224String. |
| 31 | HMAC_SHA3_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA3_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha3_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha3_256String. |
| 32 | HMAC_SHA3_384scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA3_384. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha3_384Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha3_384String. |
| 33 | HMAC_SHA3_512scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA3_512. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha3_512Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha3_512String. |
| 34 | HMAC_SHA512_224scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA512_224. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha512_224Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha512_224String. |
| 35 | HMAC_SHA512_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_SHA512_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + sha512_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha512_256String. |
| 36 | HMAC_BLAKE2S_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_BLAKE2S_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + blake2s_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2s_256String. |
| 37 | HMAC_BLAKE2B_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_BLAKE2B_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + blake2b_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_256String. |
| 38 | HMAC_BLAKE2B_384scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_BLAKE2B_384. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + blake2b_384Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_384String. |
| 39 | HMAC_BLAKE2B_512scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/HMAC_BLAKE2B_512. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = hmacPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hmacPrefix. + "-" + blake2b_512Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_512String. |
| 40 | BLAKE2S_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/BLAKE2S_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = blake2s_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2s_256String. |
| 41 | BLAKE2B_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/BLAKE2B_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = blake2b_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_256String. |
| 42 | BLAKE2B_384scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/BLAKE2B_384. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = blake2b_384Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_384String. |
| 43 | BLAKE2B_512scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/BLAKE2B_512. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = blake2b_512Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_512String. |
| 44 | // RSA-based algorithms. |
| 45 | RSA_SHA224scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/RSA_SHA224. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha224Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha224String. |
| 46 | // RSA_SHA256 is the default algorithm. |
| 47 | RSA_SHA256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/RSA_SHA256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha256String. |
| 48 | RSA_SHA384scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/RSA_SHA384. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha384Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha384String. |
| 49 | RSA_SHA512scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/RSA_SHA512. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha512Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha512String. |
| 50 | // ECDSA algorithms |
| 51 | ECDSA_SHA224scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ECDSA_SHA224. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = ecdsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ecdsaPrefix. + "-" + sha224Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha224String. |
| 52 | ECDSA_SHA256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ECDSA_SHA256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = ecdsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ecdsaPrefix. + "-" + sha256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha256String. |
| 53 | ECDSA_SHA384scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ECDSA_SHA384. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = ecdsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ecdsaPrefix. + "-" + sha384Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha384String. |
| 54 | ECDSA_SHA512scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ECDSA_SHA512. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = ecdsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ecdsaPrefix. + "-" + sha512Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha512String. |
| 55 | // ED25519 algorithms |
| 56 | // can only be SHA512 |
| 57 | ED25519scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ED25519. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = ed25519Prefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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_224scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_SHA3_224. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha3_224Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha3_224String. |
| 62 | rsa_SHA3_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_SHA3_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha3_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha3_256String. |
| 63 | rsa_SHA3_384scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_SHA3_384. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha3_384Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha3_384String. |
| 64 | rsa_SHA3_512scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_SHA3_512. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha3_512Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha3_512String. |
| 65 | rsa_SHA512_224scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_SHA512_224. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha512_224Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha512_224String. |
| 66 | rsa_SHA512_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_SHA512_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + sha512_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sha512_256String. |
| 67 | rsa_BLAKE2S_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_BLAKE2S_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + blake2s_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2s_256String. |
| 68 | rsa_BLAKE2B_256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_BLAKE2B_256. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + blake2b_256Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_256String. |
| 69 | rsa_BLAKE2B_384scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_BLAKE2B_384. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + blake2b_384Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_384String. |
| 70 | rsa_BLAKE2B_512scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsa_BLAKE2B_512. Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# = rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix. + "-" + blake2b_512Stringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/blake2b_512String. |
| 71 | ) |
| 72 | |
| 73 | // HTTP Signatures can be applied to different HTTP headers, depending on the |
| 74 | // expected application behavior. |
| 75 | type SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme# string |
| 76 | |
| 77 | const ( |
| 78 | // Signature will place the HTTP Signature into the 'Signature' HTTP |
| 79 | // header. |
| 80 | Signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signature. SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme# = "Signature" |
| 81 | // Authorization will place the HTTP Signature into the 'Authorization' |
| 82 | // HTTP header. |
| 83 | Authorizationscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Authorization. SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 | signatureAuthSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#) authSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#authScheme().() string { |
| 97 | switch s { |
| 98 | case Authorizationscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Authorization.: |
| 99 | return signatureAuthSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 Signerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 | SignRequestscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signer#SignRequest.(pKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PrivateKey#, pubKeyId string, r *httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.0 `net/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 | SignResponsescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signer#SignResponse.(pKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PrivateKey#, pubKeyId string, r httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.0 `net/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 NewSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/NewSigner().(prefs []Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm#, dAlgo DigestAlgorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/DigestAlgorithm#, headers []string, scheme SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#, expiresIn int64) (Signerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signer#, Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm#, error) { |
| 162 | for _, pref := range prefs { |
| 163 | s, err := newSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/newSigner().(pref, dAlgo, headers, scheme, expiresIn) |
| 164 | if err != nil { |
| 165 | continue |
| 166 | } |
| 167 | return s, pref, err |
| 168 | } |
| 169 | s, err := newSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/newSigner().(defaultAlgorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/defaultAlgorithm., dAlgo, headers, scheme, expiresIn) |
| 170 | return s, defaultAlgorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 SSHSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 | SignRequestscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SSHSigner#SignRequest.(pubKeyId string, r *httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.0 `net/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 | SignResponsescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SSHSigner#SignResponse.(pubKeyId string, r httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.0 `net/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 NewSSHSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/NewSSHSigner().(s sshscip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/.Signerscip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/Signer#, dAlgo DigestAlgorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/DigestAlgorithm#, headers []string, scheme SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#, expiresIn int64) (SSHSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SSHSigner#, Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm#, error) { |
| 215 | sshAlgo := getSSHAlgorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/getSSHAlgorithm().(s.PublicKeyscip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/Signer#PublicKey().().Typescip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/PublicKey#Type().()) |
| 216 | if sshAlgo == "" { |
| 217 | return nil, "", fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("key type: %s not supported yet.", s.PublicKeyscip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/Signer#PublicKey().().Typescip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/PublicKey#Type().()) |
| 218 | } |
| 219 | |
| 220 | signer, err := newSSHSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 getSSHAlgorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/getSSHAlgorithm().(pkType string) Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm# { |
| 229 | switch { |
| 230 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(pkType, sshPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sshPrefix.+"-"+ed25519Prefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ed25519Prefix.): |
| 231 | return ED25519scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ED25519. |
| 232 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(pkType, sshPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/sshPrefix.+"-"+rsaPrefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/rsaPrefix.): |
| 233 | return RSA_SHA256scip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 Verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 | KeyIdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Verifier#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 | Verifyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Verifier#Verify.(pKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#, algo Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 | hostHeaderscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 NewVerifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/NewVerifier().(r *httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Request#) (Verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Verifier#, error) { |
| 277 | h := r.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Request#Header. |
| 278 | if _, hasHostHeader := h[hostHeaderscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hostHeader.]; len(r.Hostscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Request#Host.) > 0 && !hasHostHeader { |
| 279 | h[hostHeaderscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/hostHeader.] = []string{r.Hostscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Request#Host.} |
| 280 | } |
| 281 | return newVerifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/newVerifier().(h, func(h httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#, toInclude []string, created int64, expires int64) (string, error) { |
| 282 | return signatureStringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signatureString().(h, toInclude, addRequestTargetscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/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 NewResponseVerifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/NewResponseVerifier().(r *httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Response#) (Verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Verifier#, error) { |
| 289 | return newVerifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/newVerifier().(r.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Response#Header., func(h httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#, toInclude []string, created int64, expires int64) (string, error) { |
| 290 | return signatureStringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signatureString().(h, toInclude, requestTargetNotPermittedscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/requestTargetNotPermitted()., created, expires) |
| 291 | }) |
| 292 | } |
| 293 | |
| 294 | func newSSHSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/newSSHSigner().(sshSigner sshscip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/.Signerscip-go gomod golang.org/x/crypto v0.49.0 `golang.org/x/crypto/ssh`/Signer#, algo Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm#, dAlgo DigestAlgorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/DigestAlgorithm#, headers []string, scheme SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#, expiresIn int64) (SSHSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SSHSigner#, error) { |
| 295 | var expires, created int64 = 0, 0 |
| 296 | |
| 297 | if expiresIn != 0 { |
| 298 | created = timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Unixscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Unix().() |
| 299 | expires = created + expiresIn |
| 300 | } |
| 301 | |
| 302 | s, err := signerFromSSHSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signerFromSSHSigner().(sshSigner, string(algo)) |
| 303 | if err != nil { |
| 304 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no crypto implementation available for ssh algo %q: %s", algo, err) |
| 305 | } |
| 306 | |
| 307 | a := &asymmSSHSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSSHSigner#{ |
| 308 | asymmSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSSHSigner#asymmSigner.: &asymmSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#{ |
| 309 | sscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#s.: s, |
| 310 | dAlgoscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#dAlgo.: dAlgo, |
| 311 | headersscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#headers.: headers, |
| 312 | targetHeaderscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#targetHeader.: scheme, |
| 313 | prefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#prefix.: scheme.authSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#authScheme().(), |
| 314 | createdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#created.: created, |
| 315 | expiresscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#expires.: expires, |
| 316 | }, |
| 317 | } |
| 318 | |
| 319 | return a, nil |
| 320 | } |
| 321 | |
| 322 | func newSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/newSigner().(algo Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm#, dAlgo DigestAlgorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/DigestAlgorithm#, headers []string, scheme SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#, expiresIn int64) (Signerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signer#, error) { |
| 323 | var expires, created int64 = 0, 0 |
| 324 | if expiresIn != 0 { |
| 325 | created = timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Unixscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Unix().() |
| 326 | expires = created + expiresIn |
| 327 | } |
| 328 | |
| 329 | s, err := signerFromStringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signerFromString().(string(algo)) |
| 330 | if err == nil { |
| 331 | a := &asymmSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#{ |
| 332 | sscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#s.: s, |
| 333 | dAlgoscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#dAlgo.: dAlgo, |
| 334 | headersscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#headers.: headers, |
| 335 | targetHeaderscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#targetHeader.: scheme, |
| 336 | prefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#prefix.: scheme.authSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#authScheme().(), |
| 337 | createdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#created.: created, |
| 338 | expiresscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/asymmSigner#expires.: expires, |
| 339 | } |
| 340 | return a, nil |
| 341 | } |
| 342 | m, err := macerFromStringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macerFromString().(string(algo)) |
| 343 | if err != nil { |
| 344 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no crypto implementation available for %q: %s", algo, err) |
| 345 | } |
| 346 | c := &macSignerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macSigner#{ |
| 347 | mscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macSigner#m.: m, |
| 348 | dAlgoscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macSigner#dAlgo.: dAlgo, |
| 349 | headersscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macSigner#headers.: headers, |
| 350 | targetHeaderscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macSigner#targetHeader.: scheme, |
| 351 | prefixscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macSigner#prefix.: scheme.authSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#authScheme().(), |
| 352 | createdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macSigner#created.: created, |
| 353 | expiresscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macSigner#expires.: expires, |
| 354 | } |
| 355 | return c, nil |
| 356 | } |