@@ -24,7 +24,7 @@ const (
2424func init () {
2525 // This should guarantee that at runtime the defaultAlgorithm will not
2626 // result in errors when fetching a macer or signer (see algorithms.go)
27- if ok , err := isAvailable (defaultAlgorithm ); err != nil {
27+ if ok , err := isAvailable (string ( defaultAlgorithm ) ); err != nil {
2828 panic (err )
2929 } else if ! ok {
3030 panic (fmt .Sprintf ("the default httpsig algorithm is unavailable: %q" , defaultAlgorithm ))
@@ -33,55 +33,78 @@ func init() {
3333
3434const (
3535 HMAC_SHA224 Algorithm = hmacPrefix + "-" + sha224String
36- HMAC_SHA256 = hmacPrefix + "-" + sha256String
37- HMAC_SHA384 = hmacPrefix + "-" + sha384String
38- HMAC_SHA512 = hmacPrefix + "-" + sha512String
39- HMAC_RIPEMD160 = hmacPrefix + "-" + ripemd160String
40- HMAC_SHA3_224 = hmacPrefix + "-" + sha3_224String
41- HMAC_SHA3_256 = hmacPrefix + "-" + sha3_256String
42- HMAC_SHA3_384 = hmacPrefix + "-" + sha3_384String
43- HMAC_SHA3_512 = hmacPrefix + "-" + sha3_512String
44- HMAC_SHA512_224 = hmacPrefix + "-" + sha512_224String
45- HMAC_SHA512_256 = hmacPrefix + "-" + sha512_256String
46- HMAC_BLAKE2S_256 = hmacPrefix + "-" + blake2s_256String
47- HMAC_BLAKE2B_256 = hmacPrefix + "-" + blake2b_256String
48- HMAC_BLAKE2B_384 = hmacPrefix + "-" + blake2b_384String
49- HMAC_BLAKE2B_512 = hmacPrefix + "-" + blake2b_512String
50- BLAKE2S_256 = blake2s_256String
51- BLAKE2B_256 = blake2b_256String
52- BLAKE2B_384 = blake2b_384String
53- BLAKE2B_512 = blake2b_512String
54- RSA_SHA224 = rsaPrefix + "-" + sha224String
36+ HMAC_SHA256 Algorithm = hmacPrefix + "-" + sha256String
37+ HMAC_SHA384 Algorithm = hmacPrefix + "-" + sha384String
38+ HMAC_SHA512 Algorithm = hmacPrefix + "-" + sha512String
39+ HMAC_RIPEMD160 Algorithm = hmacPrefix + "-" + ripemd160String
40+ HMAC_SHA3_224 Algorithm = hmacPrefix + "-" + sha3_224String
41+ HMAC_SHA3_256 Algorithm = hmacPrefix + "-" + sha3_256String
42+ HMAC_SHA3_384 Algorithm = hmacPrefix + "-" + sha3_384String
43+ HMAC_SHA3_512 Algorithm = hmacPrefix + "-" + sha3_512String
44+ HMAC_SHA512_224 Algorithm = hmacPrefix + "-" + sha512_224String
45+ HMAC_SHA512_256 Algorithm = hmacPrefix + "-" + sha512_256String
46+ HMAC_BLAKE2S_256 Algorithm = hmacPrefix + "-" + blake2s_256String
47+ HMAC_BLAKE2B_256 Algorithm = hmacPrefix + "-" + blake2b_256String
48+ HMAC_BLAKE2B_384 Algorithm = hmacPrefix + "-" + blake2b_384String
49+ HMAC_BLAKE2B_512 Algorithm = hmacPrefix + "-" + blake2b_512String
50+ BLAKE2S_256 Algorithm = blake2s_256String
51+ BLAKE2B_256 Algorithm = blake2b_256String
52+ BLAKE2B_384 Algorithm = blake2b_384String
53+ BLAKE2B_512 Algorithm = blake2b_512String
54+ RSA_SHA224 Algorithm = rsaPrefix + "-" + sha224String
5555 // RSA_SHA256 is the default algorithm.
56- RSA_SHA256 = rsaPrefix + "-" + sha256String
57- RSA_SHA384 = rsaPrefix + "-" + sha384String
58- RSA_SHA512 = rsaPrefix + "-" + sha512String
59- RSA_RIPEMD160 = rsaPrefix + "-" + ripemd160String
60- RSA_SHA3_224 = rsaPrefix + "-" + sha3_224String
61- RSA_SHA3_256 = rsaPrefix + "-" + sha3_256String
62- RSA_SHA3_384 = rsaPrefix + "-" + sha3_384String
63- RSA_SHA3_512 = rsaPrefix + "-" + sha3_512String
64- RSA_SHA512_224 = rsaPrefix + "-" + sha512_224String
65- RSA_SHA512_256 = rsaPrefix + "-" + sha512_256String
66- RSA_BLAKE2S_256 = rsaPrefix + "-" + blake2s_256String
67- RSA_BLAKE2B_256 = rsaPrefix + "-" + blake2b_256String
68- RSA_BLAKE2B_384 = rsaPrefix + "-" + blake2b_384String
69- RSA_BLAKE2B_512 = rsaPrefix + "-" + blake2b_512String
56+ RSA_SHA256 Algorithm = rsaPrefix + "-" + sha256String
57+ RSA_SHA384 Algorithm = rsaPrefix + "-" + sha384String
58+ RSA_SHA512 Algorithm = rsaPrefix + "-" + sha512String
59+ RSA_RIPEMD160 Algorithm = rsaPrefix + "-" + ripemd160String
60+ RSA_SHA3_224 Algorithm = rsaPrefix + "-" + sha3_224String
61+ RSA_SHA3_256 Algorithm = rsaPrefix + "-" + sha3_256String
62+ RSA_SHA3_384 Algorithm = rsaPrefix + "-" + sha3_384String
63+ RSA_SHA3_512 Algorithm = rsaPrefix + "-" + sha3_512String
64+ RSA_SHA512_224 Algorithm = rsaPrefix + "-" + sha512_224String
65+ RSA_SHA512_256 Algorithm = rsaPrefix + "-" + sha512_256String
66+ RSA_BLAKE2S_256 Algorithm = rsaPrefix + "-" + blake2s_256String
67+ RSA_BLAKE2B_256 Algorithm = rsaPrefix + "-" + blake2b_256String
68+ RSA_BLAKE2B_384 Algorithm = rsaPrefix + "-" + blake2b_384String
69+ RSA_BLAKE2B_512 Algorithm = rsaPrefix + "-" + blake2b_512String
7070)
7171
72- // HTTP Signatures can be applied to either the "Authorization" or "Signature"
73- // HTTP header
72+ // HTTP Signatures can be applied to different HTTP headers, depending on the
73+ // expected application behavior.
7474type SignatureScheme string
7575
7676const (
77- Signature SignatureScheme = "Signature"
78- Authorization = "Authorization"
77+ // Signature will place the HTTP Signature into the 'Signature' HTTP
78+ // header.
79+ Signature SignatureScheme = "Signature"
80+ // Authorization will place the HTTP Signature into the 'Authorization'
81+ // HTTP header.
82+ Authorization SignatureScheme = "Authorization"
7983)
8084
8185// Signers will sign HTTP requests or responses based on the algorithms and
8286// headers selected at creation time.
87+ //
88+ // Note that signatures do set the deprecated 'algorithm' parameter for
89+ // backwards compatibility.
8390type Signer interface {
91+ // SignRequest signs the request using a private key. The public key id
92+ // is used by the HTTP server to identify which key to use to verify the
93+ // signature.
94+ //
95+ // If the Signer was created using a MAC based algorithm, then the
96+ // private key is expected to be of type []byte. If the Signer was
97+ // created using an RSA based algorithm, then the private key is
98+ // expected to be of type *rsa.PrivateKey.
8499 SignRequest (pKey crypto.PrivateKey , pubKeyId string , r * http.Request ) error
100+ // SignResponse signs the response using a private key. The public key
101+ // id is used by the HTTP client to identify which key to use to verify
102+ // the signature.
103+ //
104+ // If the Signer was created using a MAC based algorithm, then the
105+ // private key is expected to be of type []byte. If the Signer was
106+ // created using an RSA based algorithm, then the private key is
107+ // expected to be of type *rsa.PrivateKey.
85108 SignResponse (pKey crypto.PrivateKey , pubKeyId string , r http.ResponseWriter ) error
86109}
87110
@@ -110,7 +133,42 @@ func NewSigner(prefs []Algorithm, headers []string, scheme SignatureScheme) (Sig
110133 return s , defaultAlgorithm , err
111134}
112135
113- // TODO: Verification
136+ // Verifier verifies HTTP Signatures.
137+ //
138+ // It will determine which of the supported headers has the parameters
139+ // that define the signature.
140+ //
141+ // Note that verification ignores the deprecated 'algorithm' parameter.
142+ type Verifier interface {
143+ // KeyId gets the public key id that the signature is signed with.
144+ //
145+ // Note that the application is expected to determine the algorithm
146+ // used based on metadata or out-of-band information for this key id.
147+ KeyId () string
148+ // Verify accepts the public key specified by KeyId and returns an
149+ // error if verification fails or if the signature is malformed. The
150+ // algorithm must be the one used to create the signature in order to
151+ // pass verification. The algorithm is determined based on metadata or
152+ // out-of-band information for the key id.
153+ //
154+ // If the signature was created using a MAC based algorithm, then the
155+ // public key is expected to be of type []byte. If the signature was
156+ // created using an RSA based algorithm, then the public key is
157+ // expected to be of type *rsa.PublicKey.
158+ Verify (pKey crypto.PublicKey , algo Algorithm ) error
159+ }
160+
161+ func NewVerifier (r * http.Request ) (Verifier , error ) {
162+ return newVerifier (r .Header , func (h http.Header , toInclude []string ) (string , error ) {
163+ return signatureString (h , toInclude , addRequestTarget (r ))
164+ })
165+ }
166+
167+ func NewResponseVerifier (r * http.Response ) (Verifier , error ) {
168+ return newVerifier (r .Header , func (h http.Header , toInclude []string ) (string , error ) {
169+ return signatureString (h , toInclude , requestTargetNotPermitted )
170+ })
171+ }
114172
115173func newSigner (algo Algorithm , headers []string , scheme SignatureScheme ) (Signer , error ) {
116174 s , err := signerFromString (string (algo ))
0 commit comments