-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa.go
184 lines (159 loc) · 4.25 KB
/
rsa.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
package jwk
import (
"crypto/rsa"
"encoding/json"
"errors"
"math/big"
"github.com/ericyan/jwk/internal/base64url"
)
// RSAPublicKey represents an RSA public key, which contains
// algorithm-specific parameters defined in RFC7518, Section 6.3.1.
//
// RSAPublicKey implements the Key interface.
type RSAPublicKey struct {
*Params
N *base64url.Value `json:"n"`
E *base64url.Value `json:"e"`
pub *rsa.PublicKey
}
// NewRSAPublicKey creates a new RSAPublicKey.
func NewRSAPublicKey(pub *rsa.PublicKey, params *Params) (*RSAPublicKey, error) {
if params == nil {
params = &Params{KeyType: TypeRSA}
}
if params.KeyType == "" {
params.KeyType = TypeRSA
}
if params.KeyType != TypeRSA {
return nil, errors.New("jwk: invalid params, wrong key type")
}
// Sanity checks for the public key
if pub.N == nil || pub.E < 2 || pub.E > 1<<31-1 {
return nil, errors.New("jwk: invalid crypto key")
}
return &RSAPublicKey{
params,
base64url.NewBigInt(pub.N),
base64url.NewUint64(uint64(pub.E)),
pub,
}, nil
}
// ParseRSAPublicKey parses the JSON Web Key as an RSA public key.
func ParseRSAPublicKey(jwk []byte) (*RSAPublicKey, error) {
key := new(RSAPublicKey)
err := json.Unmarshal(jwk, key)
if err != nil {
return nil, err
}
if key.KeyType != TypeRSA {
return nil, errors.New("jwk: invalid JWT, wrong type")
}
if key.N == nil {
return nil, errors.New("jwk: invalid JWT, missing N")
}
if key.E == nil {
return nil, errors.New("jwk: invalid JWT, missing E")
}
key.pub = &rsa.PublicKey{
N: key.N.BigInt(),
E: int(key.E.Uint64()),
}
return key, nil
}
// CryptoKey returns the underlying cryptographic key.
func (key *RSAPublicKey) CryptoKey() CryptoKey {
return key.pub
}
// RSAPrivateKey represents an RSA private key, which contains
// algorithm-specific parameters defined in RFC7518, Section 6.3.2.
//
// RSAPrivate implements the Key interface.
type RSAPrivateKey struct {
*RSAPublicKey
D *base64url.Value `json:"d"`
P *base64url.Value `json:"p"`
Q *base64url.Value `json:"q"`
DP *base64url.Value `json:"dp,omitempty"`
DQ *base64url.Value `json:"dq,omitempty"`
QI *base64url.Value `json:"qi,omitempty"`
OTH *json.RawMessage `json:"oth,omitempty"` // multi-prime key not supported
priv *rsa.PrivateKey
}
// NewRSAPrivateKey creates a new RSAPrivate.
func NewRSAPrivateKey(priv *rsa.PrivateKey, params *Params) (*RSAPrivateKey, error) {
if priv == nil || priv.Validate() != nil {
return nil, errors.New("jwk: invalid crypto key")
}
if len(priv.Primes) > 2 {
return nil, errors.New("jwk: unsupported key: multi-prime RSA key")
}
pub, err := NewRSAPublicKey(&priv.PublicKey, params)
if err != nil {
return nil, err
}
key := &RSAPrivateKey{
RSAPublicKey: pub,
D: base64url.NewBigInt(priv.D),
P: base64url.NewBigInt(priv.Primes[0]),
Q: base64url.NewBigInt(priv.Primes[1]),
priv: priv,
}
if priv.Precomputed.Dp != nil {
key.DP = base64url.NewBigInt(priv.Precomputed.Dp)
}
if priv.Precomputed.Dq != nil {
key.DQ = base64url.NewBigInt(priv.Precomputed.Dq)
}
if priv.Precomputed.Qinv != nil {
key.QI = base64url.NewBigInt(priv.Precomputed.Qinv)
}
return key, nil
}
// ParseRSAPrivateKey parses the JSON Web Key as an RSA private key.
func ParseRSAPrivateKey(jwk []byte) (*RSAPrivateKey, error) {
key := new(RSAPrivateKey)
err := json.Unmarshal(jwk, key)
if err != nil {
return nil, err
}
if key.D == nil {
return nil, errors.New("jwk: invalid JWT, missing D")
}
if key.P == nil {
return nil, errors.New("jwk: invalid JWT, missing P")
}
if key.Q == nil {
return nil, errors.New("jwk: invalid JWT, missing Q")
}
pub, err := ParseRSAPublicKey(jwk)
if err != nil {
return nil, err
}
key.RSAPublicKey = pub
priv := &rsa.PrivateKey{
PublicKey: *key.RSAPublicKey.pub,
D: key.D.BigInt(),
Primes: []*big.Int{
key.P.BigInt(),
key.Q.BigInt(),
},
}
if key.DP != nil {
priv.Precomputed.Dp = key.DP.BigInt()
}
if key.DQ != nil {
priv.Precomputed.Dq = key.DQ.BigInt()
}
if key.QI != nil {
priv.Precomputed.Qinv = key.QI.BigInt()
}
if err := priv.Validate(); err != nil {
return nil, err
}
key.priv = priv
return key, nil
}
// CryptoKey returns the underlying cryptographic key.
func (key *RSAPrivateKey) CryptoKey() CryptoKey {
return key.priv
}