This repository has been archived by the owner on Aug 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwk.go
254 lines (241 loc) · 9.03 KB
/
jwk.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
package jwc
// https://tools.ietf.org/html/rfc7517
import (
"bytes"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/binary"
"errors"
"math/big"
"time"
)
const rsaType = "RSA"
var (
// ErrJWKValueOutOfRangeParsingBigInt -
ErrJWKValueOutOfRangeParsingBigInt = errors.New("ErrJWKValueOutOfRangeParsingBigInt")
)
// JWK - JSON Web Key
type JWK struct {
Type string `json:"kty"`
ID JWKID `json:"kid"`
Usage Usage `json:"use"`
Algorithm Algorithm `json:"alg"`
CertificateChainBase64 []string `json:"x5c,omitempty"`
ThumbprintBase64 string `json:"x5t,omitempty"`
ExpirationTime *time.Time `json:"exp,omitempty"`
}
// JWKID - identify a specific jwk in a set
type JWKID string
// RSAPublicJWK - rsa public JSON web key
type RSAPublicJWK struct {
JWK
ModulusBase64 string `json:"n"`
PublicExponentBase64 string `json:"e"`
}
// RSAToPublicJWK - takes rsa public key and returns it as JWK
func RSAToPublicJWK(publicKey *rsa.PublicKey, jwkID JWKID, algo Algorithm, expirationTime *time.Time) (*RSAPublicJWK, error) {
publicX509DER, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return nil, err
}
publicX509DERBase64 := base64.RawStdEncoding.EncodeToString(publicX509DER)
publicThumbprint := sha1.Sum(publicX509DER)
publicThumbprintBase64 := base64.RawURLEncoding.EncodeToString(publicThumbprint[:])
modulusBase64 := base64.RawURLEncoding.EncodeToString(publicKey.N.Bytes())
expBuf := new(bytes.Buffer)
binary.Write(expBuf, binary.LittleEndian, uint64(publicKey.E))
expBytes := bytes.TrimRight(expBuf.Bytes(), "\x00")
publicExponentBase64 := base64.RawURLEncoding.EncodeToString(expBytes)
var usage Usage
switch algo {
case RS256, PS256:
usage = Signing
break
case ROAEP, RSA15:
usage = Encryption
}
publicJWK := RSAPublicJWK{
JWK: JWK{
CertificateChainBase64: []string{publicX509DERBase64},
ThumbprintBase64: publicThumbprintBase64,
ExpirationTime: expirationTime,
ID: jwkID,
Type: rsaType,
Algorithm: algo,
Usage: usage,
},
ModulusBase64: modulusBase64,
PublicExponentBase64: publicExponentBase64,
}
return &publicJWK, nil
}
// PublicRSA - takes JWK and return it as rsa public key
func (jwk *RSAPublicJWK) PublicRSA() (*rsa.PublicKey, error) {
modulusBytes, err := base64.RawURLEncoding.DecodeString(jwk.ModulusBase64)
if err != nil {
return nil, err
}
modulus := new(big.Int)
modulus = modulus.SetBytes(modulusBytes)
publicExponentBytes, err := base64.RawURLEncoding.DecodeString(jwk.PublicExponentBase64)
if err != nil {
return nil, err
}
for len(publicExponentBytes) < 8 {
publicExponentBytes = append(publicExponentBytes, 0)
}
publicExponent := int(binary.LittleEndian.Uint64(publicExponentBytes))
rsaPublicKey := rsa.PublicKey{
N: modulus,
E: publicExponent,
}
return &rsaPublicKey, nil
}
// RSAPrivateJWK - rsa private JSON web key
type RSAPrivateJWK struct {
JWK
ModulusBase64 string `json:"n"`
PublicExponentBase64 string `json:"e"`
PrivateExponentBase64 string `json:"d"`
FirstPrimeFactorBase64 string `json:"p"`
SecondPrimeFactorBase64 string `json:"q"`
// precomputed fields
PrivateExpModFirstPrimeMinusOneBase64 string `json:"dp"` // d mod(p-1)
PrivateExpModSecondPrimeMinusOneBase64 string `json:"dq"` // d mod(q-1)
SecondPrimeInverseModFirstPrimeBase64 string `json:"qi"` // q^-1 mod p
}
// PublicKey -
func (jwk *RSAPrivateJWK) PublicKey() *RSAPublicJWK {
return &RSAPublicJWK{
JWK: JWK{
Type: jwk.Type,
ID: jwk.ID,
Usage: jwk.Usage,
Algorithm: jwk.Algorithm,
CertificateChainBase64: jwk.CertificateChainBase64,
ThumbprintBase64: jwk.ThumbprintBase64,
ExpirationTime: jwk.ExpirationTime,
},
ModulusBase64: jwk.ModulusBase64,
PublicExponentBase64: jwk.PublicExponentBase64,
}
}
// RSAToPrivateJWK - takes rsa private key and returns it as JWK
func RSAToPrivateJWK(privateKey *rsa.PrivateKey, jwkID JWKID, algo Algorithm, expirationTime *time.Time) (*RSAPrivateJWK, error) {
privateX509DER := x509.MarshalPKCS1PrivateKey(privateKey)
privateX509DERBase64 := base64.RawStdEncoding.EncodeToString(privateX509DER)
privateThumbprint := sha1.Sum(privateX509DER)
privateThumbprintBase64 := base64.RawURLEncoding.EncodeToString(privateThumbprint[:])
modulusBase64 := base64.RawURLEncoding.EncodeToString(privateKey.PublicKey.N.Bytes())
expBuf := new(bytes.Buffer)
binary.Write(expBuf, binary.LittleEndian, uint64(privateKey.PublicKey.E))
expBytes := bytes.TrimRight(expBuf.Bytes(), "\x00")
publicExponentBase64 := base64.RawURLEncoding.EncodeToString(expBytes)
privateExponentBase64 := base64.RawURLEncoding.EncodeToString(privateKey.D.Bytes())
firstPrimeFactor := privateKey.Primes[0]
firstPrimeFactorBase64 := base64.RawURLEncoding.EncodeToString(firstPrimeFactor.Bytes())
secondPrimeFactor := privateKey.Primes[1]
secondPrimeFactorBase64 := base64.RawURLEncoding.EncodeToString(secondPrimeFactor.Bytes())
// precomputed
privateExpModFirstPrimeMinusOneBase64 := base64.RawURLEncoding.EncodeToString(privateKey.Precomputed.Dp.Bytes())
privateExpModSecondPrimeMinusOneBase64 := base64.RawURLEncoding.EncodeToString(privateKey.Precomputed.Dq.Bytes())
secondPrimeInverseModFirstPrimeBase64 := base64.RawURLEncoding.EncodeToString(privateKey.Precomputed.Qinv.Bytes())
var usage Usage
switch algo {
case RS256, PS256:
usage = Signing
break
case ROAEP, RSA15:
usage = Encryption
}
privateJWK := RSAPrivateJWK{
JWK: JWK{
CertificateChainBase64: []string{privateX509DERBase64},
ThumbprintBase64: privateThumbprintBase64,
ExpirationTime: expirationTime,
ID: jwkID,
Type: rsaType,
Algorithm: algo,
Usage: usage,
},
ModulusBase64: modulusBase64,
PublicExponentBase64: publicExponentBase64,
PrivateExponentBase64: privateExponentBase64,
FirstPrimeFactorBase64: firstPrimeFactorBase64,
SecondPrimeFactorBase64: secondPrimeFactorBase64,
// precomputed
PrivateExpModFirstPrimeMinusOneBase64: privateExpModFirstPrimeMinusOneBase64,
PrivateExpModSecondPrimeMinusOneBase64: privateExpModSecondPrimeMinusOneBase64,
SecondPrimeInverseModFirstPrimeBase64: secondPrimeInverseModFirstPrimeBase64,
}
return &privateJWK, nil
}
// PrivateRSA - takes JWK and return it as rsa private key
func (jwk *RSAPrivateJWK) PrivateRSA() (*rsa.PrivateKey, error) {
modulusBytes, err := base64.RawURLEncoding.DecodeString(jwk.ModulusBase64)
if err != nil {
return nil, err
}
modulus := new(big.Int)
modulus = modulus.SetBytes(modulusBytes)
publicExponentBytes, err := base64.RawURLEncoding.DecodeString(jwk.PublicExponentBase64)
if err != nil {
return nil, err
}
for len(publicExponentBytes) < 8 {
publicExponentBytes = append(publicExponentBytes, 0)
}
publicExponent := int(binary.LittleEndian.Uint64(publicExponentBytes))
privateExponentBytes, err := base64.RawURLEncoding.DecodeString(jwk.PrivateExponentBase64)
if err != nil {
return nil, err
}
privateExponent := new(big.Int)
privateExponent = privateExponent.SetBytes(privateExponentBytes)
firstPrimeFactorBytes, err := base64.RawURLEncoding.DecodeString(jwk.FirstPrimeFactorBase64)
if err != nil {
return nil, err
}
firstPrimeFactor := new(big.Int)
firstPrimeFactor = firstPrimeFactor.SetBytes(firstPrimeFactorBytes)
secondPrimeFactorBytes, err := base64.RawURLEncoding.DecodeString(jwk.SecondPrimeFactorBase64)
if err != nil {
return nil, err
}
secondPrimeFactor := new(big.Int)
secondPrimeFactor = secondPrimeFactor.SetBytes(secondPrimeFactorBytes)
privateExpModFirstPrimeMinusOneBytes, err := base64.RawURLEncoding.DecodeString(jwk.PrivateExpModFirstPrimeMinusOneBase64)
if err != nil {
return nil, err
}
privateExpModFirstPrimeMinusOne := new(big.Int)
privateExpModFirstPrimeMinusOne = privateExpModFirstPrimeMinusOne.SetBytes(privateExpModFirstPrimeMinusOneBytes)
privateExpModSecondPrimeMinusOneBytes, err := base64.RawURLEncoding.DecodeString(jwk.PrivateExpModSecondPrimeMinusOneBase64)
if err != nil {
return nil, err
}
privateExpModSecondPrimeMinusOne := new(big.Int)
privateExpModSecondPrimeMinusOne = privateExpModSecondPrimeMinusOne.SetBytes(privateExpModSecondPrimeMinusOneBytes)
secondPrimeInverseModFirstPrimeBytes, err := base64.RawURLEncoding.DecodeString(jwk.SecondPrimeInverseModFirstPrimeBase64)
if err != nil {
return nil, err
}
secondPrimeInverseModFirstPrime := new(big.Int)
secondPrimeInverseModFirstPrime = secondPrimeInverseModFirstPrime.SetBytes(secondPrimeInverseModFirstPrimeBytes)
rsaPrivateKey := rsa.PrivateKey{
PublicKey: rsa.PublicKey{
N: modulus,
E: publicExponent,
},
D: privateExponent,
Primes: []*big.Int{firstPrimeFactor, secondPrimeFactor},
Precomputed: rsa.PrecomputedValues{
Dp: privateExpModFirstPrimeMinusOne,
Dq: privateExpModSecondPrimeMinusOne,
Qinv: secondPrimeInverseModFirstPrime,
},
}
return &rsaPrivateKey, nil
}