-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwkutils.js
190 lines (172 loc) · 4.44 KB
/
jwkutils.js
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
// SEQUENCE(OBJECT IDENTIFIER = 1.2.840.113549.1.1.1, NULL) - rsaEncryption
const rsaPublicKeyOid = [
0x30,
0x0d,
0x06,
0x09,
0x2a,
0x86,
0x48,
0x86,
0xf7,
0x0d,
0x01,
0x01,
0x01,
0x05,
0x00
]
// OBJECT IDENTIFIER=1.2.840.10045.2.1 - ecPublicKey
const ecPublicKeyOid = [0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01]
const secp256k1Oid = [0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a] // OBJECT IDENTIFIER=1.3.132.0.10) - secp256k1
const prime256v1Oid = [
// OBJECT IDENTIFIER=1.2.840.10045.3.1.7 - prime256v1
0x06,
0x08,
0x2a,
0x86,
0x48,
0xce,
0x3d,
0x03,
0x01,
0x07
]
const secp384r1Oid = [0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22] // OBJECT IDENTIFIER=1.3.132.0.34 - secp384r1
const secp521r1Oid = [0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23] // OBJECT IDENTIFIER=1.3.132.0.35 - secp521r1
function jwkToPem(jwk) {
switch (jwk.kty) {
case 'RSA': {
return rsaPublicJwkToPem(jwk)
}
case 'EC': {
return ecPublicKeyJwkToPem(jwk)
}
default: {
throw new Error(`Unknown key type: ${jwk.kty}`)
}
}
}
function rsaPublicJwkToPem(rsaPublicKeyJwk) {
let modulusBytes = asn1PositiveInteger(
new Uint8Array(Buffer.from(rsaPublicKeyJwk.n, 'base64'))
)
let exponentBytes = asn1PositiveInteger(
new Uint8Array(Buffer.from(rsaPublicKeyJwk.e, 'base64'))
)
let integerSequenceBytes = encodeAsn1Bytes(0x30, [
...modulusBytes, // modulus
...exponentBytes // exponent
])
let bitStringBytes = encodeAsn1Bytes(
0x03,
// Sequence
[0x00, ...integerSequenceBytes]
)
let pemBytes = new Uint8Array(
encodeAsn1Bytes(0x30, [
// Header
...rsaPublicKeyOid,
// Bit string
...bitStringBytes
])
)
return formatPemPublicKey(pemBytes)
}
function ecPublicKeyJwkToPem(ecPublicKeyJwk) {
let keyOid
switch (ecPublicKeyJwk.crv) {
case 'K-256': {
// Not part of the JWK standard
keyOid = encodeAsn1Bytes(0x30, [...ecPublicKeyOid, ...secp256k1Oid])
break
}
case 'P-256': {
keyOid = encodeAsn1Bytes(0x30, [...ecPublicKeyOid, ...prime256v1Oid])
break
}
case 'P-384': {
keyOid = encodeAsn1Bytes(0x30, [...ecPublicKeyOid, ...secp384r1Oid])
break
}
case 'P-521': {
keyOid = encodeAsn1Bytes(0x30, [...ecPublicKeyOid, ...secp521r1Oid])
break
}
default: {
throw new Error(`Unknown curve ${ecPublicKeyJwk.crv}`)
}
}
let xBytes = new Uint8Array(Buffer.from(ecPublicKeyJwk.x, 'base64'))
let yBytes = new Uint8Array(Buffer.from(ecPublicKeyJwk.y, 'base64'))
let bitStringBytes = encodeAsn1Bytes(
0x03,
// Sequence
[0x00, 0x04, ...xBytes, ...yBytes]
)
let pemBytes = new Uint8Array(
encodeAsn1Bytes(0x30, [
// Header
...keyOid,
// Bit string
...bitStringBytes
])
)
return formatPemPublicKey(pemBytes)
}
function encodeAsn1Bytes(type, bytes) {
let lengthBytes
if (bytes.length === 0) {
lengthBytes = [0]
} else if (bytes.length < 0x80) {
lengthBytes = [bytes.length]
} else if (bytes.length <= 0xff) {
lengthBytes = [0x81, bytes.length & 0xff]
} else if (bytes.length <= 0xffff) {
lengthBytes = [0x82, bytes.length >> 8, bytes.length & 0xff]
} else if (bytes.length <= 0xffffff) {
lengthBytes = [0x83, 0xff0000 >> 16, bytes.length >> 8, bytes.length & 0xff]
} else {
lengthBytes = [
0x84,
0xff000000 >> 24,
0xff0000 >> 16,
bytes.length >> 8,
bytes.length & 0xff
]
}
return [type, ...lengthBytes, ...bytes]
}
function asn1PositiveInteger(bytes) {
if (bytes[0] > 0x7f) {
return encodeAsn1Bytes(0x02, [0x00, ...bytes])
}
return encodeAsn1Bytes(0x02, bytes)
}
function formatPemPublicKey(bytes) {
let pemBase64 = Buffer.from(bytes.buffer)
.toString('base64')
.match(/.{1,64}/g)
.join('\n')
return `-----BEGIN PUBLIC KEY-----\n${pemBase64}\n-----END PUBLIC KEY-----`
}
function hexDump(bytes) {
console.log(
Buffer.from(bytes)
.toString('hex')
.toUpperCase()
.match(/.{1,32}/g)
.join('\n')
.replace(/(\w\w)/g, '$1 ')
.replace(/\s$/, '')
)
}
module.exports = {
rsaPublicJwkToPem,
ecPublicKeyJwkToPem,
jwkToPem
}
// Links
// * https://github.com/jrnker/CSharp-easy-RSA-PEM/blob/48349cfc010d6c6acf9feb12282431d9d03fd28c/CSharp-easy-RSA-PEM/CSharp-easy-RSA-PEM/AsnKeyBuilder.cs
// * https://lapo.it/asn1js/
// * https://github.com/EternalDeiwos/keyto/tree/d8480710393bc9ed93be3758a30246cddedec771