...
1 package jwt
2
3 import (
4 "crypto"
5 "crypto/ecdsa"
6 "crypto/rand"
7 "errors"
8 "math/big"
9 )
10
11 var (
12
13 ErrECDSAVerification = errors.New("crypto/ecdsa: verification error")
14 )
15
16
17
18 type SigningMethodECDSA struct {
19 Name string
20 Hash crypto.Hash
21 KeySize int
22 CurveBits int
23 }
24
25
26 var (
27 SigningMethodES256 *SigningMethodECDSA
28 SigningMethodES384 *SigningMethodECDSA
29 SigningMethodES512 *SigningMethodECDSA
30 )
31
32 func init() {
33
34 SigningMethodES256 = &SigningMethodECDSA{"ES256", crypto.SHA256, 32, 256}
35 RegisterSigningMethod(SigningMethodES256.Alg(), func() SigningMethod {
36 return SigningMethodES256
37 })
38
39
40 SigningMethodES384 = &SigningMethodECDSA{"ES384", crypto.SHA384, 48, 384}
41 RegisterSigningMethod(SigningMethodES384.Alg(), func() SigningMethod {
42 return SigningMethodES384
43 })
44
45
46 SigningMethodES512 = &SigningMethodECDSA{"ES512", crypto.SHA512, 66, 521}
47 RegisterSigningMethod(SigningMethodES512.Alg(), func() SigningMethod {
48 return SigningMethodES512
49 })
50 }
51
52 func (m *SigningMethodECDSA) Alg() string {
53 return m.Name
54 }
55
56
57
58 func (m *SigningMethodECDSA) Verify(signingString string, sig []byte, key interface{}) error {
59
60 var ecdsaKey *ecdsa.PublicKey
61 switch k := key.(type) {
62 case *ecdsa.PublicKey:
63 ecdsaKey = k
64 default:
65 return newError("ECDSA verify expects *ecdsa.PublicKey", ErrInvalidKeyType)
66 }
67
68 if len(sig) != 2*m.KeySize {
69 return ErrECDSAVerification
70 }
71
72 r := big.NewInt(0).SetBytes(sig[:m.KeySize])
73 s := big.NewInt(0).SetBytes(sig[m.KeySize:])
74
75
76 if !m.Hash.Available() {
77 return ErrHashUnavailable
78 }
79 hasher := m.Hash.New()
80 hasher.Write([]byte(signingString))
81
82
83 if verifystatus := ecdsa.Verify(ecdsaKey, hasher.Sum(nil), r, s); verifystatus {
84 return nil
85 }
86
87 return ErrECDSAVerification
88 }
89
90
91
92 func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) ([]byte, error) {
93
94 var ecdsaKey *ecdsa.PrivateKey
95 switch k := key.(type) {
96 case *ecdsa.PrivateKey:
97 ecdsaKey = k
98 default:
99 return nil, newError("ECDSA sign expects *ecdsa.PrivateKey", ErrInvalidKeyType)
100 }
101
102
103 if !m.Hash.Available() {
104 return nil, ErrHashUnavailable
105 }
106
107 hasher := m.Hash.New()
108 hasher.Write([]byte(signingString))
109
110
111 if r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, hasher.Sum(nil)); err == nil {
112 curveBits := ecdsaKey.Curve.Params().BitSize
113
114 if m.CurveBits != curveBits {
115 return nil, ErrInvalidKey
116 }
117
118 keyBytes := curveBits / 8
119 if curveBits%8 > 0 {
120 keyBytes += 1
121 }
122
123
124
125
126 out := make([]byte, 2*keyBytes)
127 r.FillBytes(out[0:keyBytes])
128 s.FillBytes(out[keyBytes:])
129
130 return out, nil
131 } else {
132 return nil, err
133 }
134 }
135
View as plain text