forked from byteball/ocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.js
357 lines (331 loc) · 9.9 KB
/
signature.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
/*jslint node: true */
"use strict";
var ecdsa = require('secp256k1');
var ValidationUtils = require('./validation_utils.js');
var crypto = require('crypto');
function sign(hash, priv_key){
var res = ecdsa.ecdsaSign(hash, priv_key);
return Buffer.from(res.signature).toString("base64");
};
function verify(hash, b64_sig, b64_pub_key){
try{
var signature = Buffer.from(b64_sig, "base64"); // 64 bytes (32+32)
return ecdsa.ecdsaVerify(signature, hash, Buffer.from(b64_pub_key, "base64"));
}
catch(e){
console.log('signature verification exception: '+e.toString());
return false;
}
};
function verifyMessageWithPemPubKey(message, signature, pem_key) {
var verify = crypto.createVerify('SHA256');
verify.update(message);
verify.end();
var encoding = ValidationUtils.isValidHexadecimal(signature) ? 'hex' : 'base64';
try {
return verify.verify(pem_key, signature, encoding);
} catch(e1) {
try {
if (e1 instanceof TypeError)
return verify.verify({key: pem_key}, signature, encoding); // from Node v11, the key has to be included in an object
else{
console.log("exception when verifying with pem key: " + e1);
return false;
}
} catch(e2) {
console.log("exception when verifying with pem key: " + e1 + " " + e2);
return false;
}
}
}
function signMessageWithEcPemPrivKey(message, encoding, pem_key) {
//we fix pem key formatting
var contentAloneB64 = pem_key.replace("-----BEGIN EC PRIVATE KEY-----", "").replace("-----END EC PRIVATE KEY-----", "");
contentAloneB64 = contentAloneB64.replace(/\s/g, "");
pem_key = "-----BEGIN EC PRIVATE KEY-----" + "\n";
pem_key += contentAloneB64+"\n";
pem_key += "-----END EC PRIVATE KEY-----";
return signMessage(message, encoding, pem_key);
}
function vrfGenerate(seed, privkey){
return signMessageWithRsaPemPrivKey(seed, 'hex', privkey);
}
function signMessageWithRsaPemPrivKey(message, encoding, pem_key) {
//we fix pem key formatting
var contentAloneB64 = pem_key.replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "");
contentAloneB64 = contentAloneB64.replace(/\s/g, "");
pem_key = "-----BEGIN RSA PRIVATE KEY-----" + "\n";
pem_key += contentAloneB64+"\n";
pem_key += "-----END RSA PRIVATE KEY-----";
return signMessage(message, encoding, pem_key);
}
function signMessage(message, encoding, pem_key){
if (!encoding)
encoding = 'base64'
var sign = crypto.createSign('SHA256');
sign.update(message);
sign.end();
try {
return sign.sign(pem_key, encoding);
} catch(e1) {
try {
return sign.sign({key: pem_key}, encoding);
} catch(e2) {
console.log("exception when signing with pem key: " + e1 + " " + e2);
return null;
}
}
}
function validateAndFormatPemPubKey(pem_key, algo, handle) {
if (!ValidationUtils.isNonemptyString(pem_key))
return handle("pem key should be a non empty string");
//we remove header and footer if present
var contentAloneB64 = pem_key.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "");
//we remove space, tab space or carriage returns
contentAloneB64 = contentAloneB64.replace(/\s/g, "");
if (contentAloneB64.length > 736) // largest is RSA 4096 bits
return handle("pem content is too large");
if (!ValidationUtils.isValidBase64(contentAloneB64))
return handle("not valid base64 encoding" + contentAloneB64);
var contentAloneBuffer = Buffer.from(contentAloneB64, 'base64');
if (contentAloneBuffer[0] != 0x30)
return handle("pem key doesn't start with a sequence");
//we determine start and length of algo/curve identifiers
if (contentAloneBuffer[1] <= 0x7F){
if (contentAloneBuffer[2] != 0x30)
return handle("pem key doesn't have a second sequence");
var identifiersStart = 4;
var identifiersLength = contentAloneBuffer[3];
} else if (contentAloneBuffer[1] == 0x81){
if (contentAloneBuffer[3] != 0x30)
return handle("pem key doesn't have a second sequence");
var identifiersStart = 5;
var identifiersLength = contentAloneBuffer[4];
} else if (contentAloneBuffer[1] == 0x82){
if (contentAloneBuffer[4] != 0x30)
return handle("pem key doesn't have a second sequence");
var identifiersStart = 6;
var identifiersLength = contentAloneBuffer[5];
} else {
return handle("wrong length tag");
}
//we decode the length of identifiers
if (identifiersLength != 13 && identifiersLength != 16 && identifiersLength != 19 && identifiersLength != 20)
return handle("wrong identifiers length" + identifiersLength);
//we isolate the identifiers
var contentAloneHex = contentAloneBuffer.toString('hex')
var typeIdentifiersHex = contentAloneHex.slice(identifiersStart * 2, identifiersStart *2 + identifiersLength *2);
if (!objSupportedPemTypes[typeIdentifiersHex])
return handle("unsupported algo or curve in pem key");
if (algo != "any"){
if (algo == "ECDSA" && objSupportedPemTypes[typeIdentifiersHex].algo != "ECDSA")
return handle("PEM key is not ECDSA type");
if (algo == "RSA" && objSupportedPemTypes[typeIdentifiersHex].algo != "RSA")
return handle("PEM key is not RSA type");
}
if (objSupportedPemTypes[typeIdentifiersHex].algo == "ECDSA" && objSupportedPemTypes[typeIdentifiersHex].hex_pub_key_length != (contentAloneHex.length - identifiersStart * 2 - identifiersLength *2 - 8))
return handle("wrong key length");
//we add back header and footer
pem_key = "-----BEGIN PUBLIC KEY-----" + "\n";
pem_key += contentAloneB64+"\n";
pem_key += "-----END PUBLIC KEY-----";
return handle(null,pem_key);
}
var objSupportedPemTypes = {
'06072a8648ce3d020106092b2403030208010101': {
name: 'brainpoolP160r1',
hex_pub_key_length: 80,
algo: 'ECDSA'
},
'06072a8648ce3d020106092b2403030208010102': {
name: 'brainpoolP160t1',
hex_pub_key_length: 80,
algo: 'ECDSA'
},
'06072a8648ce3d020106092b2403030208010103': {
name: 'brainpoolP192r1',
hex_pub_key_length: 96,
algo: 'ECDSA'
},
'06072a8648ce3d020106092b2403030208010104': {
name: 'brainpoolP192t1',
hex_pub_key_length: 96,
algo: 'ECDSA'
},
'06072a8648ce3d020106092b2403030208010105': {
name: 'brainpoolP224r1',
hex_pub_key_length: 112,
algo: 'ECDSA'
},
'06072a8648ce3d020106092b2403030208010106': {
name: 'brainpoolP224t1',
hex_pub_key_length: 112,
algo: 'ECDSA'
},
'06072a8648ce3d020106092b2403030208010107': {
name: 'brainpoolP256r1',
hex_pub_key_length: 128,
algo: 'ECDSA'
},
'06072a8648ce3d020106092b2403030208010108': {
name: 'brainpoolP256t1',
hex_pub_key_length: 128,
algo: 'ECDSA'
},
'06072a8648ce3d020106082a8648ce3d030101': {
name: 'prime192v1',
hex_pub_key_length: 96,
algo: 'ECDSA'
},
'06072a8648ce3d020106082a8648ce3d030102': {
name: 'prime192v2',
hex_pub_key_length: 96,
algo: 'ECDSA'
},
'06072a8648ce3d020106082a8648ce3d030103': {
name: 'prime192v3',
hex_pub_key_length: 96,
algo: 'ECDSA'
},
'06072a8648ce3d020106082a8648ce3d030104': {
name: 'prime239v1',
hex_pub_key_length: 120,
algo: 'ECDSA'
},
'06072a8648ce3d020106082a8648ce3d030105': {
name: 'prime239v2',
hex_pub_key_length: 120,
algo: 'ECDSA'
},
'06072a8648ce3d020106082a8648ce3d030106': {
name: 'prime239v3',
hex_pub_key_length: 120,
algo: 'ECDSA'
},
'06072a8648ce3d020106082a8648ce3d030107': {
name: 'prime256v1',
hex_pub_key_length: 128,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040006': {
name: 'secp112r1',
hex_pub_key_length: 56,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040007': {
name: 'secp112r2',
hex_pub_key_length: 56,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b8104001c': {
name: 'secp128r1',
hex_pub_key_length: 64,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b8104001d': {
name: 'secp128r2',
hex_pub_key_length: 64,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040009': {
name: 'secp160k1',
hex_pub_key_length: 80,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040008': {
name: 'secp160r1',
hex_pub_key_length: 80,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b8104001e': {
name: 'secp160r2',
hex_pub_key_length: 80,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b8104001f': {
name: 'secp192k1',
hex_pub_key_length: 96,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040020': {
name: 'secp224k1',
hex_pub_key_length: 112,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040021': {
name: 'secp224r1',
hex_pub_key_length: 112,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b8104000a': {
name: 'secp256k1',
hex_pub_key_length: 128,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040022': {
name: 'secp384r1',
hex_pub_key_length: 192,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040004': {
name: 'sect113r1',
hex_pub_key_length: 60,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040005': {
name: 'sect113r2',
hex_pub_key_length: 60,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040016': {
name: 'sect131r1',
hex_pub_key_length: 68,
algo: 'ECDSA'
},
'06072a8648ce3d020106052b81040017': {
name: 'sect131r2',
hex_pub_key_length: 68,
algo: 'ECDSA'
},
'06072a8648ce3d02010605672b010401': {
name: 'wap-wsg-idm-ecid-wtls1',
hex_pub_key_length: 60,
algo: 'ECDSA'
},
'06072a8648ce3d02010605672b010404': {
name: 'wap-wsg-idm-ecid-wtls4',
hex_pub_key_length: 60,
algo: 'ECDSA'
},
'06072a8648ce3d02010605672b010406': {
name: 'wap-wsg-idm-ecid-wtls6',
hex_pub_key_length: 56,
algo: 'ECDSA'
},
'06072a8648ce3d02010605672b010407': {
name: 'wap-wsg-idm-ecid-wtls7',
hex_pub_key_length: 80,
algo: 'ECDSA'
},
'06072a8648ce3d02010605672b010408': {
name: 'wap-wsg-idm-ecid-wtls8',
hex_pub_key_length: 56,
algo: 'ECDSA'
},
'06072a8648ce3d02010605672b010409': {
name: 'wap-wsg-idm-ecid-wtls9',
hex_pub_key_length: 80,
algo: 'ECDSA'
},
'06092a864886f70d0101010500':{
name: 'PKCS #1',
algo: 'RSA'
}
};
exports.signMessageWithRsaPemPrivKey = signMessageWithRsaPemPrivKey;
exports.sign = sign;
exports.verify = verify;
exports.verifyMessageWithPemPubKey = verifyMessageWithPemPubKey;
exports.signMessageWithEcPemPrivKey = signMessageWithEcPemPrivKey;
exports.vrfGenerate = vrfGenerate;
exports.validateAndFormatPemPubKey = validateAndFormatPemPubKey;