-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
233 lines (206 loc) · 5.22 KB
/
index.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
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
var asn = require('asn1.js')
var factor = require('./factor')
var one = new asn.bignum(1)
function urlize(base64) {
return base64.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '')
}
function hex2b64url(str) {
return urlize(Buffer.from(str, 'hex').toString('base64'))
}
function fromPEM(data) {
var text = data.toString().split(/(\r\n|\r|\n)+/g);
text = text.filter(function(line) {
return line.trim().length !== 0;
});
text = text.slice(1, -1).join('');
return Buffer.from(text.replace(/[^\w\d\+\/=]+/g, ''), 'base64');
}
var RSAPublicKey = asn.define('RSAPublicKey', function () {
this.seq().obj(
this.key('n').int(),
this.key('e').int()
)
})
var AlgorithmIdentifier = asn.define('AlgorithmIdentifier', function () {
this.seq().obj(
this.key('algorithm').objid(),
this.key('parameters').optional().any()
)
})
var PublicKeyInfo = asn.define('PublicKeyInfo', function () {
this.seq().obj(
this.key('algorithm').use(AlgorithmIdentifier),
this.key('publicKey').bitstr()
)
})
var Version = asn.define('Version', function () {
this.int({
0: 'two-prime',
1: 'multi'
})
})
var OtherPrimeInfos = asn.define('OtherPrimeInfos', function () {
this.seq().obj(
this.key('ri').int(),
this.key('di').int(),
this.key('ti').int()
)
})
var RSAPrivateKey = asn.define('RSAPrivateKey', function () {
this.seq().obj(
this.key('version').use(Version),
this.key('n').int(),
this.key('e').int(),
this.key('d').int(),
this.key('p').int(),
this.key('q').int(),
this.key('dp').int(),
this.key('dq').int(),
this.key('qi').int(),
this.key('other').optional().use(OtherPrimeInfos)
)
})
var PrivateKeyInfo = asn.define('PrivateKeyInfo', function () {
this.seq().obj(
this.key('version').use(Version),
this.key('algorithm').use(AlgorithmIdentifier),
this.key('privateKey').bitstr()
)
})
const RSA_OID = '1.2.840.113549.1.1.1'
function addExtras(obj, extras) {
extras = extras || {}
Object.keys(extras).forEach(
function (key) {
obj[key] = extras[key]
}
)
return obj
}
function pad(hex) {
return (hex.length % 2 === 1) ? '0' + hex : hex
}
function decodeRsaPublic(buffer, extras) {
var key = RSAPublicKey.decode(buffer, 'der')
var e = pad(key.e.toString(16))
var jwk = {
kty: 'RSA',
n: bn2base64url(key.n),
e: hex2b64url(e)
}
return addExtras(jwk, extras)
}
function decodeRsaPrivate(buffer, extras) {
var key = RSAPrivateKey.decode(buffer, 'der')
var e = pad(key.e.toString(16))
var jwk = {
kty: 'RSA',
n: bn2base64url(key.n),
e: hex2b64url(e),
d: bn2base64url(key.d),
p: bn2base64url(key.p),
q: bn2base64url(key.q),
dp: bn2base64url(key.dp),
dq: bn2base64url(key.dq),
qi: bn2base64url(key.qi)
}
return addExtras(jwk, extras)
}
function decodePublic(buffer, extras) {
var info = PublicKeyInfo.decode(buffer, 'der')
return decodeRsaPublic(info.publicKey.data, extras)
}
function decodePrivate(buffer, extras) {
var info = PrivateKeyInfo.decode(buffer, 'der')
return decodeRsaPrivate(info.privateKey.data, extras)
}
function getDecoder(header) {
var match = /^-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----$/.exec(header)
if (!match) { return null }
var isRSA = !!(match[1])
var isPrivate = (match[2] === 'PRIVATE')
if (isPrivate) {
return isRSA ? decodeRsaPrivate : decodePrivate
}
else {
return isRSA ? decodeRsaPublic : decodePublic
}
}
function pem2jwk(pem, extras) {
var text = pem.toString().split(/(\r\n|\r|\n)+/g)
text = text.filter(function(line) {
return line.trim().length !== 0
});
var decoder = getDecoder(text[0])
text = text.slice(1, -1).join('')
return decoder(Buffer.from(text.replace(/[^\w\d\+\/=]+/g, ''), 'base64'), extras)
}
function recomputePrimes(jwk) {
var pq = factor(jwk.e, jwk.d, jwk.n)
var p = pq.p
var q = pq.q
var dp = jwk.d.mod(p.sub(one))
var dq = jwk.d.mod(q.sub(one))
var qi = q.invm(p)
return {
n: jwk.n,
e: jwk.e,
d: jwk.d,
p: p,
q: q,
dp: dp,
dq: dq,
qi: qi
}
}
function parse(jwk) {
return {
n: string2bn(jwk.n),
e: string2bn(jwk.e),
d: jwk.d && string2bn(jwk.d),
p: jwk.p && string2bn(jwk.p),
q: jwk.q && string2bn(jwk.q),
dp: jwk.dp && string2bn(jwk.dp),
dq: jwk.dq && string2bn(jwk.dq),
qi: jwk.qi && string2bn(jwk.qi)
}
}
function jwk2pem(json) {
var jwk = parse(json)
var isPrivate = !!(jwk.d)
var t = isPrivate ? 'PRIVATE' : 'PUBLIC'
var header = '-----BEGIN RSA ' + t + ' KEY-----\n'
var footer = '\n-----END RSA ' + t + ' KEY-----\n'
var data = null
if (isPrivate) {
if (!jwk.p) {
jwk = recomputePrimes(jwk)
}
jwk.version = 'two-prime'
data = RSAPrivateKey.encode(jwk, 'der')
}
else {
data = RSAPublicKey.encode(jwk, 'der')
}
var body = data.toString('base64').match(/.{1,64}/g).join('\n')
return header + body + footer
}
function bn2base64url(bn) {
return hex2b64url(pad(bn.toString(16)))
}
function base64url2bn(str) {
return new asn.bignum(Buffer.from(str, 'base64'))
}
function string2bn(str) {
if (/^[0-9]+$/.test(str)) {
return new asn.bignum(str, 10)
}
return base64url2bn(str)
}
module.exports = {
pem2jwk: pem2jwk,
jwk2pem: jwk2pem,
BN: asn.bignum
}