forked from lestrrat-go/jwx
/
jws.go
581 lines (492 loc) · 17 KB
/
jws.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//go:generate go run internal/cmd/genheader/main.go
// Package jws implements the digital signature on JSON based data
// structures as described in https://tools.ietf.org/html/rfc7515
//
// If you do not care about the details, the only things that you
// would need to use are the following functions:
//
// jws.Sign(payload, algorithm, key)
// jws.Verify(encodedjws, algorithm, key)
//
// To sign, simply use `jws.Sign`. `payload` is a []byte buffer that
// contains whatever data you want to sign. `alg` is one of the
// jwa.SignatureAlgorithm constants from package jwa. For RSA and
// ECDSA family of algorithms, you will need to prepare a private key.
// For HMAC family, you just need a []byte value. The `jws.Sign`
// function will return the encoded JWS message on success.
//
// To verify, use `jws.Verify`. It will parse the `encodedjws` buffer
// and verify the result using `algorithm` and `key`. Upon successful
// verification, the original payload is returned, so you can work on it.
package jws
import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/json"
"io"
"strings"
"unicode"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jws/sign"
"github.com/lestrrat-go/jwx/jws/verify"
"github.com/pkg/errors"
)
// Sign is a short way to generate a JWS in compact serialization
// for a given payload. If you need more control over the signature
// generation process, you should manually create signers and tweak
// the message.
/*
func Sign(payload []byte, alg jwa.SignatureAlgorithm, key interface{}, options ...Option) ([]byte, error) {
signer, err := sign.New(alg)
if err != nil {
return nil, errors.Wrap(err, "failed to create signer")
}
msg, err := SignMulti(payload, WithSigner(signer, key))
if err != nil {
return nil, errors.Wrap(err, "failed to sign payload")
}
return msg, nil
}
*/
type payloadSigner struct {
signer sign.Signer
key interface{}
protected Headers
public Headers
}
func (s *payloadSigner) Sign(payload []byte) ([]byte, error) {
return s.signer.Sign(payload, s.key)
}
func (s *payloadSigner) Algorithm() jwa.SignatureAlgorithm {
return s.signer.Algorithm()
}
func (s *payloadSigner) ProtectedHeader() Headers {
return s.protected
}
func (s *payloadSigner) PublicHeader() Headers {
return s.public
}
// Sign generates a signature for the given payload, and serializes
// it in compact serialization format. In this format you may NOT use
// multiple signers.
//
// If you would like to pass custom headers, use the WithHeaders option.
func Sign(payload []byte, alg jwa.SignatureAlgorithm, key interface{}, options ...Option) ([]byte, error) {
var hdrs Headers = &StandardHeaders{}
for _, o := range options {
switch o.Name() {
case optkeyHeaders:
hdrs = o.Value().(Headers)
}
}
signer, err := sign.New(alg)
if err != nil {
return nil, errors.Wrap(err, `failed to create signer`)
}
hdrs.Set(AlgorithmKey, signer.Algorithm())
hdrbuf, err := json.Marshal(hdrs)
if err != nil {
return nil, errors.Wrap(err, `failed to marshal headers`)
}
var buf bytes.Buffer
enc := base64.NewEncoder(base64.RawURLEncoding, &buf)
if _, err := enc.Write(hdrbuf); err != nil {
return nil, errors.Wrap(err, `failed to write headers as base64`)
}
if err := enc.Close(); err != nil {
return nil, errors.Wrap(err, `failed to finalize writing headers as base64`)
}
buf.WriteByte('.')
enc = base64.NewEncoder(base64.RawURLEncoding, &buf)
if _, err := enc.Write(payload); err != nil {
return nil, errors.Wrap(err, `failed to write payload as base64`)
}
if err := enc.Close(); err != nil {
return nil, errors.Wrap(err, `failed to finalize writing payload as base64`)
}
signature, err := signer.Sign(buf.Bytes(), key)
if err != nil {
return nil, errors.Wrap(err, `failed to sign payload`)
}
buf.WriteByte('.')
enc = base64.NewEncoder(base64.RawURLEncoding, &buf)
if _, err := enc.Write(signature); err != nil {
return nil, errors.Wrap(err, `failed to write signature as base64`)
}
if err := enc.Close(); err != nil {
return nil, errors.Wrap(err, `failed to finalize writing signature as base64`)
}
return buf.Bytes(), nil
}
// SignLiteral generates a signature for the given payload and headers, and serializes
// it in compact serialization format. In this format you may NOT use
// multiple signers.
//
func SignLiteral(payload []byte, alg jwa.SignatureAlgorithm, key interface{}, headers []byte) ([]byte, error) {
signer, err := sign.New(alg)
if err != nil {
return nil, errors.Wrap(err, `failed to create signer`)
}
var buf bytes.Buffer
enc := base64.NewEncoder(base64.RawURLEncoding, &buf)
if _, err := enc.Write(headers); err != nil {
return nil, errors.Wrap(err, `failed to write headers as base64`)
}
if err := enc.Close(); err != nil {
return nil, errors.Wrap(err, `failed to finalize writing headers as base64`)
}
buf.WriteByte('.')
enc = base64.NewEncoder(base64.RawURLEncoding, &buf)
if _, err := enc.Write(payload); err != nil {
return nil, errors.Wrap(err, `failed to write payload as base64`)
}
if err := enc.Close(); err != nil {
return nil, errors.Wrap(err, `failed to finalize writing payload as base64`)
}
signature, err := signer.Sign(buf.Bytes(), key)
if err != nil {
return nil, errors.Wrap(err, `failed to sign payload`)
}
buf.WriteByte('.')
enc = base64.NewEncoder(base64.RawURLEncoding, &buf)
if _, err := enc.Write(signature); err != nil {
return nil, errors.Wrap(err, `failed to write signature as base64`)
}
if err := enc.Close(); err != nil {
return nil, errors.Wrap(err, `failed to finalize writing signature as base64`)
}
return buf.Bytes(), nil
}
// SignMulti accepts multiple signers via the options parameter,
// and creates a JWS in JSON serialization format that contains
// signatures from applying aforementioned signers.
func SignMulti(payload []byte, options ...Option) ([]byte, error) {
var signers []PayloadSigner
for _, o := range options {
switch o.Name() {
case optkeyPayloadSigner:
signers = append(signers, o.Value().(PayloadSigner))
}
}
if len(signers) == 0 {
return nil, errors.New(`no signers provided`)
}
var result EncodedMessage
result.Payload = base64.RawURLEncoding.EncodeToString(payload)
for _, signer := range signers {
protected := signer.ProtectedHeader()
if protected == nil {
protected = &StandardHeaders{}
}
protected.Set(AlgorithmKey, signer.Algorithm())
hdrbuf, err := json.Marshal(protected)
if err != nil {
return nil, errors.Wrap(err, `failed to marshal headers`)
}
encodedHeader := base64.RawURLEncoding.EncodeToString(hdrbuf)
var buf bytes.Buffer
buf.WriteString(encodedHeader)
buf.WriteByte('.')
buf.WriteString(result.Payload)
signature, err := signer.Sign(buf.Bytes())
if err != nil {
return nil, errors.Wrap(err, `failed to sign payload`)
}
result.Signatures = append(result.Signatures, &EncodedSignature{
Headers: signer.PublicHeader(),
Protected: encodedHeader,
Signature: base64.RawURLEncoding.EncodeToString(signature),
})
}
return json.Marshal(result)
}
// Verify checks if the given JWS message is verifiable using `alg` and `key`.
// If the verification is successful, `err` is nil, and the content of the
// payload that was signed is returned. If you need more fine-grained
// control of the verification process, manually call `Parse`, generate a
// verifier, and call `Verify` on the parsed JWS message object.
func Verify(buf []byte, alg jwa.SignatureAlgorithm, key interface{}) (ret []byte, err error) {
verifier, err := verify.New(alg)
if err != nil {
return nil, errors.Wrap(err, "failed to create verifier")
}
buf = bytes.TrimSpace(buf)
if len(buf) == 0 {
return nil, errors.New(`attempt to verify empty buffer`)
}
if buf[0] == '{' {
var v FullEncodedMessage
if err := json.Unmarshal(buf, &v); err != nil {
return nil, errors.Wrap(err, `failed to unmarshal JWS message`)
}
// There's something wrong if the Message part is not initialized
if v.EncodedMessage == nil {
return nil, errors.New(`invalid JWS message format`)
}
// if we're using the flattened serialization format, then m.Signature
// will be non-nil
msg := v.EncodedMessage
if v.EncodedSignature != nil {
msg.Signatures[0] = v.EncodedSignature
}
var buf bytes.Buffer
for _, sig := range msg.Signatures {
buf.Reset()
buf.WriteString(sig.Protected)
buf.WriteByte('.')
buf.WriteString(msg.Payload)
decodedSignature, err := base64.RawURLEncoding.DecodeString(sig.Signature)
if err != nil {
continue
}
if err := verifier.Verify(buf.Bytes(), decodedSignature, key); err == nil {
// verified!
decodedPayload, err := base64.RawURLEncoding.DecodeString(msg.Payload)
if err != nil {
return nil, errors.Wrap(err, `message verified, failed to decode payload`)
}
return decodedPayload, nil
}
}
return nil, errors.New(`could not verify with any of the signatures`)
}
protected, payload, signature, err := SplitCompact(bytes.NewReader(buf))
if err != nil {
return nil, errors.Wrap(err, `failed extract from compact serialization format`)
}
var verifyBuf bytes.Buffer
verifyBuf.Write(protected)
verifyBuf.WriteByte('.')
verifyBuf.Write(payload)
decodedSignature := make([]byte, base64.RawURLEncoding.DecodedLen(len(signature)))
if _, err := base64.RawURLEncoding.Decode(decodedSignature, signature); err != nil {
return nil, errors.Wrap(err, `failed to decode signature`)
}
if err := verifier.Verify(verifyBuf.Bytes(), decodedSignature, key); err != nil {
return nil, errors.Wrap(err, `failed to verify message`)
}
decodedPayload := make([]byte, base64.RawURLEncoding.DecodedLen(len(payload)))
if _, err := base64.RawURLEncoding.Decode(decodedPayload, payload); err != nil {
return nil, errors.Wrap(err, `message verified, failed to decode payload`)
}
return decodedPayload, nil
}
// VerifyWithJKU wraps VerifyWithJKUAndContext using the background context.
func VerifyWithJKU(buf []byte, jwkurl string, options ...Option) ([]byte, error) {
return VerifyWithJKUAndContext(context.Background(), buf, jwkurl, options...)
}
// VerifyWithJKUAndContext verifies the JWS message using a remote JWK
// file represented in the url.
func VerifyWithJKUAndContext(ctx context.Context, buf []byte, jwkurl string, options ...Option) ([]byte, error) {
key, err := jwk.FetchHTTPWithContext(ctx, jwkurl, options...)
if err != nil {
return nil, errors.Wrap(err, `failed to fetch jwk via HTTP`)
}
return VerifyWithJWKSet(buf, key, nil)
}
// VerifyWithJWK verifies the JWS message using the specified JWK
func VerifyWithJWK(buf []byte, key jwk.Key) (payload []byte, err error) {
keyval, err := key.Materialize()
if err != nil {
return nil, errors.Wrap(err, `failed to materialize jwk.Key`)
}
payload, err = Verify(buf, jwa.SignatureAlgorithm(key.Algorithm()), keyval)
if err != nil {
return nil, errors.Wrap(err, "failed to verify message")
}
return payload, nil
}
// VerifyWithJWKSet verifies the JWS message using JWK key set.
// By default it will only pick up keys that have the "use" key
// set to either "sig" or "enc", but you can override it by
// providing a keyaccept function.
func VerifyWithJWKSet(buf []byte, keyset *jwk.Set, keyaccept JWKAcceptFunc) ([]byte, error) {
if keyaccept == nil {
keyaccept = DefaultJWKAcceptor
}
for _, key := range keyset.Keys {
if !keyaccept(key) {
continue
}
payload, err := VerifyWithJWK(buf, key)
if err == nil {
return payload, nil
}
}
// refs #140, #141
//
// We should not be Wrap()'ing the error here, because of various
// reasons -- but the fundamental one is that the only value we can get
// here is the "last error" seen in the above loop, when the symptom
// that we want to report is that none of the keys worked.
//
// Here, we just return that fact, and we do not rely on the value of
// previous errors.
return nil, errors.New("failed to verify with any of the keys")
}
// Parse parses contents from the given source and creates a jws.Message
// struct. The input can be in either compact or full JSON serialization.
func Parse(src io.Reader) (m *Message, err error) {
rdr := bufio.NewReader(src)
var first rune
for {
r, _, err := rdr.ReadRune()
if err != nil {
return nil, errors.Wrap(err, `failed to read rune`)
}
if !unicode.IsSpace(r) {
first = r
rdr.UnreadRune()
break
}
}
var parser func(io.Reader) (*Message, error)
if first == '{' {
parser = parseJSON
} else {
parser = parseCompact
}
m, err = parser(rdr)
if err != nil {
return nil, errors.Wrap(err, `failed to parse jws message`)
}
return m, nil
}
// ParseString is the same as Parse, but take in a string
func ParseString(s string) (*Message, error) {
return Parse(strings.NewReader(s))
}
func parseJSON(src io.Reader) (result *Message, err error) {
var wrapper FullEncodedMessageUnmarshalProxy
if err := json.NewDecoder(src).Decode(&wrapper); err != nil {
return nil, errors.Wrap(err, `failed to unmarshal jws message`)
}
if wrapper.EncodedMessageUnmarshalProxy == nil {
return nil, errors.New(`invalid payload (probably empty)`)
}
// if the "signature" field exist, treat it as a flattened
if wrapper.EncodedSignatureUnmarshalProxy != nil {
if len(wrapper.Signatures) != 0 {
return nil, errors.New("invalid message: mixed flattened/full json serialization")
}
wrapper.Signatures = append(wrapper.Signatures, wrapper.EncodedSignatureUnmarshalProxy)
}
var plain Message
plain.payload, err = base64.RawURLEncoding.DecodeString(wrapper.Payload)
if err != nil {
return nil, errors.Wrap(err, `failed to decode payload`)
}
for i, sig := range wrapper.Signatures {
var plainSig Signature
plainSig.headers = sig.Headers
if l := len(sig.Protected); l > 0 {
plainSig.protected = new(StandardHeaders)
hdrbuf, err := base64.RawURLEncoding.DecodeString(sig.Protected)
if err != nil {
return nil, errors.Wrapf(err, `failed to base64 decode protected header for signature #%d`, i+1)
}
if err := json.Unmarshal(hdrbuf, &plainSig.protected); err != nil {
return nil, errors.Wrapf(err, `failed to unmarshal protected header for signature #%d`, i+1)
}
}
plainSig.signature, err = base64.RawURLEncoding.DecodeString(sig.Signature)
if err != nil {
return nil, errors.Wrapf(err, `failed to decode signature #%d`, i)
}
plain.signatures = append(plain.signatures, &plainSig)
}
return &plain, nil
}
// SplitCompact splits a JWT and returns its three parts
// separately: protected headers, payload and signature.
func SplitCompact(rdr io.Reader) ([]byte, []byte, []byte, error) {
var protected []byte
var payload []byte
var signature []byte
var periods int = 0
var state int = 0
buf := make([]byte, 4096)
var sofar []byte
for {
// read next bytes
n, err := rdr.Read(buf)
// return on unexpected read error
if err != nil && err != io.EOF {
return nil, nil, nil, err
}
// append to current buffer
sofar = append(sofar, buf[:n]...)
// loop to capture multiple '.' in current buffer
for loop := true; loop; {
var i = bytes.IndexByte(sofar, '.')
if i == -1 && err != io.EOF {
// no '.' found -> exit and read next bytes (outer loop)
loop = false
continue
} else if i == -1 && err == io.EOF {
// no '.' found -> process rest and exit
i = len(sofar)
loop = false
} else {
// '.' found
periods++
}
// Reaching this point means we have found a '.' or EOF and process the rest of the buffer
switch state {
case 0:
protected = sofar[:i]
state++
case 1:
payload = sofar[:i]
state++
case 2:
signature = sofar[:i]
}
// Shorten current buffer
if len(sofar) > i {
sofar = sofar[i+1:]
}
}
// Exit on EOF
if err == io.EOF {
break
}
}
if periods != 2 {
return nil, nil, nil, errors.New(`invalid number of segments`)
}
return protected, payload, signature, nil
}
// parseCompact parses a JWS value serialized via compact serialization.
func parseCompact(rdr io.Reader) (m *Message, err error) {
protected, payload, signature, err := SplitCompact(rdr)
if err != nil {
return nil, errors.Wrap(err, `invalid compact serialization format`)
}
decodedHeader := make([]byte, base64.RawURLEncoding.DecodedLen(len(protected)))
if _, err := base64.RawURLEncoding.Decode(decodedHeader, protected); err != nil {
return nil, errors.Wrap(err, `failed to decode headers`)
}
var hdr StandardHeaders
if err := json.Unmarshal(decodedHeader, &hdr); err != nil {
return nil, errors.Wrap(err, `failed to parse JOSE headers`)
}
decodedPayload := make([]byte, base64.RawURLEncoding.DecodedLen(len(payload)))
if _, err = base64.RawURLEncoding.Decode(decodedPayload, payload); err != nil {
return nil, errors.Wrap(err, `failed to decode payload`)
}
decodedSignature := make([]byte, base64.RawURLEncoding.DecodedLen(len(signature)))
if _, err := base64.RawURLEncoding.Decode(decodedSignature, signature); err != nil {
return nil, errors.Wrap(err, `failed to decode signature`)
}
var msg Message
msg.payload = decodedPayload
msg.signatures = append(msg.signatures, &Signature{
protected: &hdr,
signature: decodedSignature,
})
return &msg, nil
}