-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
rsa.js
387 lines (387 loc) · 14.5 KB
/
rsa.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var graphene_pk11_1 = require("graphene-pk11");
var error = require("../error");
var base64url = require("base64url");
var utils = require("../utils");
var alg_1 = require("./alg");
var key_1 = require("../key");
var aes = require("./aes");
exports.ALG_NAME_RSA_PKCS1 = "RSASSA-PKCS1-v1_5";
var ALG_NAME_RSA_PSS = "RSA-PSS";
exports.ALG_NAME_RSA_OAEP = "RSA-OAEP";
function create_template(session, alg, extractable, keyUsages) {
var label = "RSA-" + alg.modulusLength;
var id_pk = new Buffer(utils.GUID(session));
var id_pubk = new Buffer(utils.GUID(session));
return {
privateKey: {
token: !!process.env["WEBCRYPTO_PKCS11_TOKEN"],
sensitive: !!process.env["WEBCRYPTO_PKCS11_SENSITIVE"],
class: graphene_pk11_1.ObjectClass.PRIVATE_KEY,
keyType: graphene_pk11_1.KeyType.RSA,
private: true,
label: label,
id: id_pk,
extractable: extractable,
derive: false,
sign: keyUsages.indexOf(key_1.KU_SIGN) !== -1,
decrypt: keyUsages.indexOf(key_1.KU_DECRYPT) !== -1,
unwrap: keyUsages.indexOf(key_1.KU_UNWRAP) !== -1
},
publicKey: {
token: !!process.env["WEBCRYPTO_PKCS11_TOKEN"],
class: graphene_pk11_1.ObjectClass.PUBLIC_KEY,
keyType: graphene_pk11_1.KeyType.RSA,
label: label,
id: id_pubk,
verify: keyUsages.indexOf(key_1.KU_VERIFY) !== -1,
encrypt: keyUsages.indexOf(key_1.KU_ENCRYPT) !== -1,
wrap: keyUsages.indexOf(key_1.KU_WRAP) !== -1,
}
};
}
var Rsa = (function (_super) {
__extends(Rsa, _super);
function Rsa() {
_super.apply(this, arguments);
}
Rsa.generateKey = function (session, alg, extractable, keyUsages, callback) {
try {
var _alg_1 = alg;
this.checkAlgorithmIdentifier(alg);
this.checkAlgorithmHashedParams(alg);
this.checkKeyGenAlgorithm(_alg_1);
var template = create_template(session, _alg_1, extractable, keyUsages);
template.publicKey.publicExponent = new Buffer(_alg_1.publicExponent),
template.publicKey.modulusBits = _alg_1.modulusLength;
session.generateKeyPair(graphene_pk11_1.KeyGenMechanism.RSA, template.publicKey, template.privateKey, function (err, keys) {
try {
if (err)
callback(err, null);
else {
var wcKeyPair = {
privateKey: new key_1.P11CryptoKey(keys.privateKey, _alg_1),
publicKey: new key_1.P11CryptoKey(keys.publicKey, _alg_1)
};
callback(null, wcKeyPair);
}
}
catch (e) {
callback(e, null);
}
});
}
catch (e) {
callback(e, null);
}
};
Rsa.checkKeyGenAlgorithm = function (alg) {
if (!alg.modulusLength)
throw new TypeError("RsaKeyGenParams: modulusLength: Missing required property");
if (alg.modulusLength < 256 || alg.modulusLength > 16384)
throw new TypeError("RsaKeyGenParams: The modulus length must be a multiple of 8 bits and >= 256 and <= 16384");
if (!(alg.publicExponent && alg.publicExponent instanceof Uint8Array))
throw new TypeError("RsaKeyGenParams: publicExponent: Missing or not a Uint8Array");
};
Rsa.checkAlgorithmHashedParams = function (alg) {
_super.checkAlgorithmHashedParams.call(this, alg);
var _alg = alg.hash;
_alg.name = _alg.name.toUpperCase();
if (alg_1.RSA_HASH_ALGS.indexOf(_alg.name) === -1)
throw new error.AlgorithmError(error.ERROR_WRONG_ALGORITHM, _alg.name);
};
Rsa.jwkAlgName = function (alg) {
throw new Error("Not implemented");
};
Rsa.exportJwkPublicKey = function (session, key, callback) {
try {
this.checkPublicKey(key);
var pkey = key.key.getAttribute({
publicExponent: null,
modulus: null
});
var alg = this.jwkAlgName(key.algorithm);
var jwk = {
kty: "RSA",
alg: alg,
ext: true,
key_ops: key.usages,
e: base64url(pkey.publicExponent),
n: base64url(pkey.modulus)
};
callback(null, jwk);
}
catch (e) {
callback(e, null);
}
};
Rsa.exportJwkPrivateKey = function (session, key, callback) {
try {
this.checkPrivateKey(key);
var pkey = key.key.getAttribute({
publicExponent: null,
modulus: null,
privateExponent: null,
prime1: null,
prime2: null,
exp1: null,
exp2: null,
coefficient: null
});
var alg = this.jwkAlgName(key.algorithm);
var jwk = {
kty: "RSA",
alg: alg,
ext: true,
key_ops: key.usages,
e: base64url(pkey.publicExponent),
n: base64url(pkey.modulus),
d: base64url(pkey.privateExponent),
p: base64url(pkey.prime1),
q: base64url(pkey.prime2),
dp: base64url(pkey.exp1),
dq: base64url(pkey.exp2),
qi: base64url(pkey.coefficient)
};
callback(null, jwk);
}
catch (e) {
callback(e, null);
}
};
Rsa.exportKey = function (session, format, key, callback) {
try {
switch (format.toLowerCase()) {
case "jwk":
if (key.type === "private")
this.exportJwkPrivateKey(session, key, callback);
else
this.exportJwkPublicKey(session, key, callback);
default:
throw new Error("Not supported format '" + format + "'");
}
}
catch (e) {
callback(e, null);
}
};
Rsa.importJwkPrivateKey = function (session, jwk, algorithm, extractable, keyUsages, callback) {
try {
var template = create_template(session, algorithm, extractable, keyUsages).privateKey;
template.publicExponent = base64url.toBuffer(jwk.e);
template.modulus = base64url.toBuffer(jwk.n);
template.privateExponent = base64url.toBuffer(jwk.d);
template.prime1 = base64url.toBuffer(jwk.p);
template.prime2 = base64url.toBuffer(jwk.q);
template.exp1 = base64url.toBuffer(jwk.dp);
template.exp2 = base64url.toBuffer(jwk.dq);
template.coefficient = base64url.toBuffer(jwk.qi);
var p11key = session.create(template);
callback(null, new key_1.P11CryptoKey(p11key, algorithm));
}
catch (e) {
callback(e, null);
}
};
Rsa.importJwkPublicKey = function (session, jwk, algorithm, extractable, keyUsages, callback) {
try {
var template = create_template(session, algorithm, extractable, keyUsages).publicKey;
template.publicExponent = base64url.toBuffer(jwk.e);
template.modulus = base64url.toBuffer(jwk.n);
var p11key = session.create(template);
callback(null, new key_1.P11CryptoKey(p11key, algorithm));
}
catch (e) {
callback(e, null);
}
};
Rsa.importKey = function (session, format, keyData, algorithm, extractable, keyUsages, callback) {
try {
switch (format.toLowerCase()) {
case "jwk":
var jwk = keyData;
if (jwk.d)
this.importJwkPrivateKey(session, jwk, algorithm, extractable, keyUsages, callback);
else
this.importJwkPublicKey(session, jwk, algorithm, extractable, keyUsages, callback);
default:
throw new Error("Not supported format '" + format + "'");
}
}
catch (e) {
callback(e, null);
}
};
return Rsa;
}(alg_1.AlgorithmBase));
var RsaPKCS1 = (function (_super) {
__extends(RsaPKCS1, _super);
function RsaPKCS1() {
_super.apply(this, arguments);
}
RsaPKCS1.wc2pk11 = function (alg, key) {
var res = null;
switch (key.algorithm.hash.name.toUpperCase()) {
case "SHA-1":
res = "SHA1_RSA_PKCS";
break;
case "SHA-224":
res = "SHA224_RSA_PKCS";
break;
case "SHA-256":
res = "SHA256_RSA_PKCS";
break;
case "SHA-384":
res = "SHA384_RSA_PKCS";
break;
case "SHA-512":
res = "SHA512_RSA_PKCS";
break;
default:
throw new error.AlgorithmError(error.ERROR_WRONG_ALGORITHM, alg.hash.name);
}
return { name: res, params: null };
};
RsaPKCS1.jwkAlgName = function (alg) {
var algName = /(\d+)$/.exec(alg.hash.name)[1];
return "RS" + (algName === "1" ? "" : algName);
};
RsaPKCS1.onCheck = function (method, paramName, paramValue) {
switch (method) {
case "sign":
switch (paramName) {
case "alg":
break;
case "key":
this.checkPrivateKey(paramValue);
break;
case "data":
break;
}
break;
case "verify":
switch (paramName) {
case "alg":
break;
case "key":
this.checkPublicKey(paramValue);
break;
case "data":
break;
}
break;
}
};
RsaPKCS1.ALGORITHM_NAME = exports.ALG_NAME_RSA_PKCS1;
return RsaPKCS1;
}(Rsa));
exports.RsaPKCS1 = RsaPKCS1;
var RsaOAEP = (function (_super) {
__extends(RsaOAEP, _super);
function RsaOAEP() {
_super.apply(this, arguments);
}
RsaOAEP.jwkAlgName = function (alg) {
var algName = /(\d+)$/.exec(alg.hash.name)[1];
return "RSA-OAEP" + (algName === "1" ? "" : ("-" + algName));
};
RsaOAEP.onCheck = function (method, paramName, paramValue) {
switch (method) {
case "encrypt":
switch (paramName) {
case "alg":
break;
case "key":
this.checkPublicKey(paramValue);
break;
case "data":
break;
}
break;
case "decrypt":
switch (paramName) {
case "alg":
break;
case "key":
this.checkPrivateKey(paramValue);
break;
case "data":
break;
}
break;
}
};
RsaOAEP.wrapKey = function (session, format, key, wrappingKey, alg, callback) {
try {
if (format === "raw") {
var _alg = this.wc2pk11(alg, wrappingKey);
session.wrapKey(_alg, wrappingKey.key, key.key, callback);
}
else
_super.wrapKey.apply(this, arguments);
}
catch (e) {
callback(e, null);
}
};
RsaOAEP.unwrapKey = function (session, format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedAlgorithm, extractable, keyUsages, callback) {
try {
if (format === "raw") {
var _alg = this.wc2pk11(unwrapAlgorithm, unwrappingKey);
var template = aes.create_template(session, unwrappedAlgorithm, extractable, keyUsages);
session.unwrapKey(_alg, unwrappingKey.key, wrappedKey, template, function (err, p11key) {
if (err)
callback(err, null);
else
callback(null, new key_1.P11CryptoKey(p11key, unwrappedAlgorithm));
});
}
else
_super.unwrapKey.apply(this, arguments);
}
catch (e) {
callback(e, null);
}
};
RsaOAEP.wc2pk11 = function (alg, key) {
var params = null;
switch (key.algorithm.hash.name.toUpperCase()) {
case "SHA-1":
params = new graphene_pk11_1.RsaOaepParams(graphene_pk11_1.MechanismEnum.SHA1, graphene_pk11_1.RsaMgf.MGF1_SHA1);
break;
case "SHA-224":
params = new graphene_pk11_1.RsaOaepParams(graphene_pk11_1.MechanismEnum.SHA224, graphene_pk11_1.RsaMgf.MGF1_SHA224);
break;
case "SHA-256":
params = new graphene_pk11_1.RsaOaepParams(graphene_pk11_1.MechanismEnum.SHA256, graphene_pk11_1.RsaMgf.MGF1_SHA256);
break;
case "SHA-384":
params = new graphene_pk11_1.RsaOaepParams(graphene_pk11_1.MechanismEnum.SHA384, graphene_pk11_1.RsaMgf.MGF1_SHA384);
break;
case "SHA-512":
params = new graphene_pk11_1.RsaOaepParams(graphene_pk11_1.MechanismEnum.SHA512, graphene_pk11_1.RsaMgf.MGF1_SHA512);
break;
default:
throw new error.AlgorithmError(error.ERROR_WRONG_ALGORITHM, key.algorithm.hash.name);
}
var res = { name: "RSA_PKCS_OAEP", params: params };
return res;
};
RsaOAEP.ALGORITHM_NAME = exports.ALG_NAME_RSA_OAEP;
return RsaOAEP;
}(Rsa));
exports.RsaOAEP = RsaOAEP;
var RsaPSS = (function (_super) {
__extends(RsaPSS, _super);
function RsaPSS() {
_super.apply(this, arguments);
}
RsaPSS.ALGORITHM_NAME = ALG_NAME_RSA_PSS;
return RsaPSS;
}(Rsa));
exports.RsaPSS = RsaPSS;