-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate_proof.go
280 lines (233 loc) · 9.59 KB
/
validate_proof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package dpop
import (
"crypto"
"crypto/subtle"
"encoding/base64"
"errors"
"net/http"
"net/url"
"strings"
"time"
"github.com/ScaleFT/xjwt"
"golang.org/x/xerrors"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
// ProofValidator validates DPoP proof headers
type Validator struct {
xjwt.VerifyConfig
}
var (
ErrProofMissing = errors.New("dpop: HTTP Header '" + httpHeader + "' not present in request")
ErrProofMalformedHeader = errors.New(`dpop: HTTP Header '` + httpHeader + `' contained a malformed JWT header`)
ErrProofMalformedClaim = errors.New(`dpop: HTTP Header '` + httpHeader + `' contained a malformed JWT claim`)
ErrProofInvalidSignature = errors.New(`dpop: HTTP Header '` + httpHeader + `' contained an invalid JWT`)
)
// ProofClaims are common claims in the DPoP proof JWT.
type ProofClaims struct {
jwt.Claims
HTTPMethod string `json:"htm,omitempty"`
HTTPUri string `json:"htu,omitempty"`
}
// ValidateTokenRequest parses and performs a PARTIAL validation of the DPoP proof JWT.
//
// It returns common proof claims, raw claims, and the public JWK used to sign the proof.
//
// Callers MUST do additional validation for their use case of:
//
// ProofClaims.JTI: For replay protection, confirm this JTI has not been used before:
// Within a reasonable consideration of accuracy and resource
// utilization, a JWT with the same "jti" value has not been
// received previously (see Section 9.1).
//
// JSONWebKey: The caller must calculate the JWK SHA-256 Thumbprint, encoding it using base64url, and
// embed it in any Access Tokens issued or make it available in the introspection request:
// tb, err := key.Thumbprint(crypto.SHA256)
// cnfThumbprint := base64.URLEncoding.EncodeToString(tb)
//
// JSONWebKey: Algorithm and key type are acceptable.
//
// If this Client has previously used a DPoP binding at Token request, you may also wish to restrict the
// JSONWebKey to a previously used value.
//
func (pv *Validator) ValidateTokenRequest(req *http.Request) (*ProofClaims, []byte, *jose.JSONWebKey, error) {
pc, raw, k, err := pv.validate(req)
if err != nil {
return nil, nil, nil, err
}
return pc, raw, k, nil
}
// ValidateResourceAccess parses and performs a PARTIAL validation of the DPoP proof JWT.
//
// It returns common proof claims, raw claims, and the public JWK used to sign the proof.
//
// keyFingerprint is the "jkt#S256" cnf claim from a JWT based Access Token or from an introspection response.
//
// Callers MUST do additional validation for their use case of:
//
// ProofClaims.JTI: For replay protection, confirm this JTI has not been used before:
// Within a reasonable consideration of accuracy and resource
// utilization, a JWT with the same "jti" value has not been
// received previously (see Section 9.1).
//
func (pv *Validator) ValidateResourceAccess(req *http.Request, keyFingerprint string) (*ProofClaims, []byte, *jose.JSONWebKey, error) {
pc, raw, k, err := pv.validate(req)
if err != nil {
return nil, nil, nil, err
}
jwkHash, err := k.Thumbprint(crypto.SHA256)
if err != nil {
return nil, nil, nil, xerrors.Errorf("dpop: can't thumbprint jwk: %v", err)
}
atHash, err := base64.URLEncoding.DecodeString(keyFingerprint)
if err != nil {
return nil, nil, nil, xerrors.Errorf("dpop: access token's key hash failed to base64 decode: %v", err)
}
if subtle.ConstantTimeCompare(jwkHash, atHash) == 0 {
d := base64.URLEncoding.EncodeToString(jwkHash)
return nil, nil, nil, xerrors.Errorf("dpop: key mismatch: expected='%s' access_token_hash='%s'", d, keyFingerprint)
}
return pc, raw, k, nil
}
func (pv *Validator) validate(req *http.Request) (*ProofClaims, []byte, *jose.JSONWebKey, error) {
/*
4.2. Checking DPoP Proofs
To check if a string that was received as part of an HTTP Request is
a valid DPoP proof, the receiving server MUST ensure that
1. the string value is a well-formed JWT,
2. all required claims are contained in the JWT,
3. the "typ" field in the header has the value "dpop+jwt",
4. the algorithm in the header of the JWT indicates an asymmetric
digital signature algorithm, is not "none", is supported by the
application, and is deemed secure,
5. that the JWT is signed using the public key contained in the
"jwk" header of the JWT,
6. the "htm" claim matches the respective value for the HTTP
request in which the JWT was received (case-insensitive),
7. the "htu" claims matches the respective value for the HTTP
request in which the JWT was received, ignoring any query and
fragment parts,
8. the token was issued within an acceptable timeframe (see
Section 9.1), and
9. that, within a reasonable consideration of accuracy and resource
utilization, a JWT with the same "jti" value has not been
received previously (see Section 9.1).
Servers SHOULD employ Syntax-Based Normalization and Scheme-Based
Normalization in accordance with Section 6.2.2. and Section 6.2.3. of
[RFC3986] before comparing the "http_uri" claim.
*/
phdr := req.Header.Get(httpHeader)
if phdr == "" {
return nil, nil, nil, ErrProofMissing
}
pjwt, err := jwt.ParseSigned(phdr)
if err != nil {
return nil, nil, nil, xerrors.Errorf("dpop: "+httpHeader+" did not contain a valid JWT: %v", err)
}
if len(pjwt.Headers) != 1 {
return nil, nil, nil, xerrors.Errorf("dpop: JWT did not contain one header: %v", ErrProofMalformedHeader)
}
pjwtTyp, ok := pjwt.Headers[0].ExtraHeaders["typ"]
if !ok {
return nil, nil, nil, xerrors.Errorf("dpop: JWT missing typ field in header: %v", ErrProofMalformedHeader)
}
if pjwtTyp != typDPOP {
return nil, nil, nil, xerrors.Errorf("dpop: JWT typ mismatch in header: '%s': %v", pjwtTyp, ErrProofMalformedHeader)
}
pjwk := pjwt.Headers[0].JSONWebKey
if pjwk == nil {
return nil, nil, nil, xerrors.Errorf("dpop: JWT missing jwk field in header: %v", ErrProofMalformedHeader)
}
if !pjwk.IsPublic() {
return nil, nil, nil, xerrors.Errorf("dpop: JWT jwk field in header must be public key: %v", ErrProofMalformedHeader)
}
algo := jose.SignatureAlgorithm(pjwt.Headers[0].Algorithm)
if !isAllowedAlgo(algo) {
return nil, nil, nil, xerrors.Errorf("dpop: JWT alg not allowed: '%s': %v", algo, ErrProofInvalidSignature)
}
claims := &ProofClaims{}
err = pjwt.UnsafeClaimsWithoutVerification(claims)
if err != nil {
return nil, nil, nil, xerrors.Errorf("dpop: "+httpHeader+" did not contain JWT claims: %v", err)
}
if claims.HTTPMethod == "" {
return nil, nil, nil, xerrors.Errorf("dpop: JWT http_method claim missing: %v", ErrProofMalformedClaim)
}
if claims.HTTPUri == "" {
return nil, nil, nil, xerrors.Errorf("dpop: JWT http_uri claim missing: %v", ErrProofMalformedClaim)
}
if strings.ToUpper(claims.HTTPMethod) != strings.ToUpper(req.Method) {
return nil, nil, nil, xerrors.Errorf("dpop: JWT http_method claim mismatch: expected='%s' received='%s': %v",
req.Method, claims.HTTPMethod, ErrProofMalformedClaim)
}
claimUrl, err := url.Parse(claims.HTTPUri)
if err != nil {
return nil, nil, nil, xerrors.Errorf("dpop: JWT http_uri claim invalid: %v: %v", ErrProofMalformedClaim, err)
}
// From the req.URL docs:
//
// For server requests, the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI. For
// most requests, fields other than Path and RawQuery will be
// empty. (See RFC 7230, Section 5.3)
murl := mungedURL(req.URL)
if murl.Path != claimUrl.Path {
return nil, nil, nil, xerrors.Errorf("dpop: JWT http_uri claim mismatch in path: expected='%s' received='%s': %v", murl.String(), claimUrl.String(), ErrProofMalformedClaim)
}
if req.Host != claimUrl.Host {
return nil, nil, nil, xerrors.Errorf("dpop: JWT http_uri claim mismatch in hostname: expected='%s' received='%s': %v", req.Host, claimUrl.Host, ErrProofMalformedClaim)
}
// if claimUrl.Scheme != "https" {
// return nil, nil, nil, xerrors.Errorf("dpop: JWT http_uri claim mismatch in scheme: expected='https' received='%s': %v", claimUrl.Scheme, ErrProofMalformedClaim)
//}
var now time.Time
if pv.Now == nil {
now = time.Now()
} else {
now = pv.Now()
}
if claims.IssuedAt == nil {
return nil, nil, nil, xerrors.Errorf("dpop: JWT iat claim missing: %v", ErrProofMalformedClaim)
}
iat := claims.IssuedAt.Time()
d := absDuration(now.Sub(iat))
if d > time.Minute*5 {
return nil, nil, nil, xerrors.Errorf("dpop: JWT iat claim is more than 5 minutes from now: now='%s' iat='%s' %v", now.String(), iat.String(), ErrProofMalformedClaim)
}
vjwks := &jose.JSONWebKeySet{
Keys: []jose.JSONWebKey{
pjwk.Public(),
},
}
vc := pv.VerifyConfig
vc.KeySet = vjwks
rawClaims, err := xjwt.VerifyRaw([]byte(phdr), vc)
if err != nil {
return nil, nil, nil, xerrors.Errorf("dpop: JWT validation failed: %v", err)
}
return claims, rawClaims, pjwk, nil
}
func absDuration(n time.Duration) time.Duration {
y := n >> 63
return (n ^ y) - y
}
var validSignatureAlgorithm = []jose.SignatureAlgorithm{
jose.RS256, // RSASSA-PKCS-v1.5 using SHA-256
jose.RS384, // RSASSA-PKCS-v1.5 using SHA-384
jose.RS512, // RSASSA-PKCS-v1.5 using SHA-512
jose.ES256, // ECDSA using P-256 and SHA-256
jose.ES384, // ECDSA using P-384 and SHA-384
jose.ES512, // ECDSA using P-521 and SHA-512
jose.PS256, // RSASSA-PSS using SHA256 and MGF1-SHA256
jose.PS384, // RSASSA-PSS using SHA384 and MGF1-SHA384
jose.PS512, // RSASSA-PSS using SHA512 and MGF1-SHA512
jose.EdDSA, // EdDSA using Ed25519
}
func isAllowedAlgo(in jose.SignatureAlgorithm) bool {
for _, validAlgo := range validSignatureAlgorithm {
if in == validAlgo {
return true
}
}
return false
}