/
jws.go
630 lines (546 loc) · 17.2 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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//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"
"io"
"io/ioutil"
"strings"
"unicode"
"unicode/utf8"
"github.com/lestrrat-go/jwx/internal/base64"
"github.com/lestrrat-go/jwx/internal/json"
"github.com/lestrrat-go/jwx/internal/pool"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/pkg/errors"
)
var registry = json.NewRegistry()
type payloadSigner struct {
signer 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.
//
// It accepts either a raw key (e.g. rsa.PrivateKey, ecdsa.PrivateKey, etc)
// or a jwk.Key, and the name of the algorithm that should be used to sign
// the token.
//
// If the key is a jwk.Key and the key contains a key ID (`kid` field),
// then it is added to the protected header generated by the signature
//
// The algorithm specified in the `alg` parameter must be able to support
// the type of key you provided, otherwise an error is returned.
//
// If you would like to pass custom headers, use the WithHeaders option.
//
// If the headers contain "b64" field, then the boolean value for the field
// is respected when creating the compact serialization form. That is,
// if you specify a header with `{"b64": false}`, then the payload is
// not base64 encoded.
func Sign(payload []byte, alg jwa.SignatureAlgorithm, key interface{}, options ...SignOption) ([]byte, error) {
var hdrs Headers
for _, o := range options {
//nolint:forcetypeassert
switch o.Ident() {
case identHeaders{}:
hdrs = o.Value().(Headers)
}
}
signer, err := NewSigner(alg)
if err != nil {
return nil, errors.Wrap(err, `failed to create signer`)
}
sig := &Signature{protected: hdrs}
_, signature, err := sig.Sign(payload, signer, key)
if err != nil {
return nil, errors.Wrap(err, `failed sign payload`)
}
return signature, nil
}
// SignMulti accepts multiple signers via the options parameter,
// and creates a JWS in JSON serialization format that contains
// signatures from applying aforementioned signers.
//
// Use `jws.WithSigner(...)` to specify values how to generate
// each signature in the `"signatures": [ ... ]` field.
func SignMulti(payload []byte, options ...Option) ([]byte, error) {
var signers []*payloadSigner
for _, o := range options {
switch o.Ident() {
case identPayloadSigner{}:
signers = append(signers, o.Value().(*payloadSigner))
}
}
if len(signers) == 0 {
return nil, errors.New(`no signers provided`)
}
var result Message
result.payload = payload
result.signatures = make([]*Signature, 0, len(signers))
for i, signer := range signers {
protected := signer.ProtectedHeader()
if protected == nil {
protected = NewHeaders()
}
if err := protected.Set(AlgorithmKey, signer.Algorithm()); err != nil {
return nil, errors.Wrap(err, `failed to set header`)
}
sig := &Signature{
headers: signer.PublicHeader(),
protected: protected,
}
_, _, err := sig.Sign(payload, signer.signer, signer.key)
if err != nil {
return nil, errors.Wrapf(err, `failed to generate signature for signer #%d (alg=%s)`, i, signer.Algorithm())
}
result.signatures = append(result.signatures, sig)
}
return json.Marshal(result)
}
// Verify checks if the given JWS message is verifiable using `alg` and `key`.
// `key` may be a "raw" key (e.g. rsa.PublicKey) or a jwk.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 generate a
// `Verifier` in `verify` subpackage, and call `Verify` method on it.
// If you need to access signatures and JOSE headers in a JWS message,
// use `Parse` function to get `Message` object.
func Verify(buf []byte, alg jwa.SignatureAlgorithm, key interface{}, options ...VerifyOption) ([]byte, error) {
var dst *Message
var detachedPayload []byte
//nolint:forcetypeassert
for _, option := range options {
switch option.Ident() {
case identMessage{}:
dst = option.Value().(*Message)
case identDetachedPayload{}:
detachedPayload = option.Value().([]byte)
}
}
buf = bytes.TrimSpace(buf)
if len(buf) == 0 {
return nil, errors.New(`attempt to verify empty buffer`)
}
if buf[0] == '{' {
return verifyJSON(buf, alg, key, dst, detachedPayload)
}
return verifyCompact(buf, alg, key, dst, detachedPayload)
}
// VerifySet uses keys store in a jwk.Set to verify the payload in `buf`.
//
// In order for `VerifySet()` to use a key in the given set, the
// `jwk.Key` object must have a valid "alg" field, and it also must
// have either an empty value or the value "sig" in the "use" field.
//
// Furthermore if the JWS signature asks for a spefici "kid", the
// `jwk.Key` must have the same "kid" as the signature.
func VerifySet(buf []byte, set jwk.Set) ([]byte, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
//nolint:forcetypeassert
for iter := set.Iterate(ctx); iter.Next(ctx); {
pair := iter.Pair()
key := pair.Value.(jwk.Key)
if key.Algorithm() == "" { // algorithm is not
continue
}
if usage := key.KeyUsage(); usage != "" && usage != jwk.ForSignature.String() {
continue
}
buf, err := Verify(buf, jwa.SignatureAlgorithm(key.Algorithm()), key)
if err != nil {
continue
}
return buf, nil
}
return nil, errors.New(`failed to verify message with any of the keys in the jwk.Set object`)
}
func verifyJSON(signed []byte, alg jwa.SignatureAlgorithm, key interface{}, dst *Message, detachedPayload []byte) ([]byte, error) {
verifier, err := NewVerifier(alg)
if err != nil {
return nil, errors.Wrap(err, "failed to create verifier")
}
var m Message
if err := json.Unmarshal(signed, &m); err != nil {
return nil, errors.Wrap(err, `failed to unmarshal JSON message`)
}
if len(m.payload) != 0 && detachedPayload != nil {
return nil, errors.New(`can't specify detached payload for JWS with payload`)
}
if detachedPayload != nil {
m.payload = detachedPayload
}
// Pre-compute the base64 encoded version of payload
var payload string
if m.b64 {
payload = base64.EncodeToString(m.payload)
} else {
payload = string(m.payload)
}
buf := pool.GetBytesBuffer()
defer pool.ReleaseBytesBuffer(buf)
for i, sig := range m.signatures {
buf.Reset()
if hdr := sig.headers; hdr != nil && hdr.KeyID() != "" {
if jwkKey, ok := key.(jwk.Key); ok {
if jwkKey.KeyID() != hdr.KeyID() {
continue
}
}
}
protected, err := json.Marshal(sig.protected)
if err != nil {
return nil, errors.Wrapf(err, `failed to marshal "protected" for signature #%d`, i+1)
}
buf.WriteString(base64.EncodeToString(protected))
buf.WriteByte('.')
buf.WriteString(payload)
if err := verifier.Verify(buf.Bytes(), sig.signature, key); err == nil {
if dst != nil {
*dst = m
}
return m.payload, nil
}
}
return nil, errors.New(`could not verify with any of the signatures`)
}
// get the value of b64 header field.
// If the field does not exist, returns true (default)
// Otherwise return the value specified by the header field.
func getB64Value(hdr Headers) bool {
b64raw, ok := hdr.Get("b64")
if !ok {
return true // default
}
b64, ok := b64raw.(bool) // default
if !ok {
return false
}
return b64
}
func verifyCompact(signed []byte, alg jwa.SignatureAlgorithm, key interface{}, dst *Message, detachedPayload []byte) ([]byte, error) {
protected, payload, signature, err := SplitCompact(signed)
if err != nil {
return nil, errors.Wrap(err, `failed extract from compact serialization format`)
}
verifier, err := NewVerifier(alg)
if err != nil {
return nil, errors.Wrap(err, "failed to create verifier")
}
verifyBuf := pool.GetBytesBuffer()
defer pool.ReleaseBytesBuffer(verifyBuf)
verifyBuf.Write(protected)
verifyBuf.WriteByte('.')
if len(payload) == 0 && detachedPayload != nil {
payload = detachedPayload
}
verifyBuf.Write(payload)
decodedSignature, err := base64.Decode(signature)
if err != nil {
return nil, errors.Wrap(err, `failed to decode signature`)
}
hdr := NewHeaders()
decodedProtected, err := base64.Decode(protected)
if err != nil {
return nil, errors.Wrap(err, `failed to decode headers`)
}
if err := json.Unmarshal(decodedProtected, hdr); err != nil {
return nil, errors.Wrap(err, `failed to decode headers`)
}
if hdr.KeyID() != "" {
if jwkKey, ok := key.(jwk.Key); ok {
if jwkKey.KeyID() != hdr.KeyID() {
return nil, errors.New(`"kid" fields do not match`)
}
}
}
if err := verifier.Verify(verifyBuf.Bytes(), decodedSignature, key); err != nil {
return nil, errors.Wrap(err, `failed to verify message`)
}
var decodedPayload []byte
if !getB64Value(hdr) { // it's not base64 encode
decodedPayload = payload
}
if decodedPayload == nil {
v, err := base64.Decode(payload)
if err != nil {
return nil, errors.Wrap(err, `message verified, failed to decode payload`)
}
decodedPayload = v
}
if dst != nil {
// Construct a new Message object
m := NewMessage()
m.SetPayload(decodedPayload)
sig := NewSignature()
sig.SetProtectedHeaders(hdr)
sig.SetSignature(decodedSignature)
m.AppendSignature(sig)
*dst = *m
}
return decodedPayload, nil
}
// This is an "optimized" ioutil.ReadAll(). It will attempt to read
// all of the contents from the reader IF the reader is of a certain
// concrete type.
func readAll(rdr io.Reader) ([]byte, bool) {
switch rdr.(type) {
case *bytes.Reader, *bytes.Buffer, *strings.Reader:
data, err := ioutil.ReadAll(rdr)
if err != nil {
return nil, false
}
return data, true
default:
return nil, false
}
}
// 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 []byte) (*Message, error) {
for i := 0; i < len(src); i++ {
r := rune(src[i])
if r >= utf8.RuneSelf {
r, _ = utf8.DecodeRune(src)
}
if !unicode.IsSpace(r) {
if r == '{' {
return parseJSON(src)
}
return parseCompact(src)
}
}
return nil, errors.New("invalid byte sequence")
}
// 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 ParseString(src string) (*Message, error) {
return Parse([]byte(src))
}
// 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 ParseReader(src io.Reader) (*Message, error) {
if data, ok := readAll(src); ok {
return Parse(data)
}
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
if err := rdr.UnreadRune(); err != nil {
return nil, errors.Wrap(err, `failed to unread rune`)
}
break
}
}
var parser func(io.Reader) (*Message, error)
if first == '{' {
parser = parseJSONReader
} else {
parser = parseCompactReader
}
m, err := parser(rdr)
if err != nil {
return nil, errors.Wrap(err, `failed to parse jws message`)
}
return m, nil
}
func parseJSONReader(src io.Reader) (result *Message, err error) {
var m Message
if err := json.NewDecoder(src).Decode(&m); err != nil {
return nil, errors.Wrap(err, `failed to unmarshal jws message`)
}
return &m, nil
}
func parseJSON(data []byte) (result *Message, err error) {
var m Message
if err := json.Unmarshal(data, &m); err != nil {
return nil, errors.Wrap(err, `failed to unmarshal jws message`)
}
return &m, nil
}
// SplitCompact splits a JWT and returns its three parts
// separately: protected headers, payload and signature.
func SplitCompact(src []byte) ([]byte, []byte, []byte, error) {
parts := bytes.Split(src, []byte("."))
if len(parts) < 3 {
return nil, nil, nil, errors.New(`invalid number of segments`)
}
return parts[0], parts[1], parts[2], nil
}
// SplitCompactString splits a JWT and returns its three parts
// separately: protected headers, payload and signature.
func SplitCompactString(src string) ([]byte, []byte, []byte, error) {
parts := strings.Split(src, ".")
if len(parts) < 3 {
return nil, nil, nil, errors.New(`invalid number of segments`)
}
return []byte(parts[0]), []byte(parts[1]), []byte(parts[2]), nil
}
// SplitCompactReader splits a JWT and returns its three parts
// separately: protected headers, payload and signature.
func SplitCompactReader(rdr io.Reader) ([]byte, []byte, []byte, error) {
if data, ok := readAll(rdr); ok {
return SplitCompact(data)
}
var protected []byte
var payload []byte
var signature []byte
var periods int
var state int
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, errors.Wrap(err, `unexpected end of input`)
}
// 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
}
// parseCompactReader parses a JWS value serialized via compact serialization.
func parseCompactReader(rdr io.Reader) (m *Message, err error) {
protected, payload, signature, err := SplitCompactReader(rdr)
if err != nil {
return nil, errors.Wrap(err, `invalid compact serialization format`)
}
return parse(protected, payload, signature)
}
func parseCompact(data []byte) (m *Message, err error) {
protected, payload, signature, err := SplitCompact(data)
if err != nil {
return nil, errors.Wrap(err, `invalid compact serialization format`)
}
return parse(protected, payload, signature)
}
func parse(protected, payload, signature []byte) (*Message, error) {
decodedHeader, err := base64.Decode(protected)
if err != nil {
return nil, errors.Wrap(err, `failed to decode protected headers`)
}
hdr := NewHeaders()
if err := json.Unmarshal(decodedHeader, hdr); err != nil {
return nil, errors.Wrap(err, `failed to parse JOSE headers`)
}
decodedPayload, err := base64.Decode(payload)
if err != nil {
return nil, errors.Wrap(err, `failed to decode payload`)
}
decodedSignature, err := base64.Decode(signature)
if 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
}
// RegisterCustomField allows users to specify that a private field
// be decoded as an instance of the specified type. This option has
// a global effect.
//
// For example, suppose you have a custom field `x-birthday`, which
// you want to represent as a string formatted in RFC3339 in JSON,
// but want it back as `time.Time`.
//
// In that case you would register a custom field as follows
//
// jwe.RegisterCustomField(`x-birthday`, timeT)
//
// Then `hdr.Get("x-birthday")` will still return an `interface{}`,
// but you can convert its type to `time.Time`
//
// bdayif, _ := hdr.Get(`x-birthday`)
// bday := bdayif.(time.Time)
//
func RegisterCustomField(name string, object interface{}) {
registry.Register(name, object)
}