-
Notifications
You must be signed in to change notification settings - Fork 8
/
ld-key-pair.js
293 lines (254 loc) · 7.47 KB
/
ld-key-pair.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
/*!
* Copyright (c) 2018 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';
const forge = require('node-forge');
const Injector = require('./Injector');
const didUtil = require('./util');
const DEFAULT_RSA_KEY_BITS = 2048;
const DEFAULT_RSA_EXPONENT = 0x10001;
class LDKeyPair {
constructor(options) {
this.injector = options.injector || new Injector();
this.keyType = options.keyType;
this.publicKey = options.publicKey;
this.secretKey = options.secretKey;
this.passphrase = options.passphrase || null;
this.id = options.id;
}
/**
* @param [options]
* @param [options.keyType]
* @param [options.injector]
* @param [options.passphrase]
*
* @returns {Promise<LDKeyPair>}
*/
static async generate(options) {
switch(options.keyType) {
case 'Ed25519VerificationKey2018':
return Ed25519KeyPair.generate(options);
case 'RsaVerificationKey2018':
default:
return RSAKeyPair.generate(options);
}
}
/**
* @param data {object} Serialized LD key object
* @param data.keyType {string}
*
* @param [options]
* @param [options.injector]
* @param [options.passphrase]
*
* @returns {Promise<LDKeyPair>}
*/
static async from(data, options) {
switch(data.keyType) {
case 'Ed25519VerificationKey2018':
return Ed25519KeyPair.from(data, options);
case 'RsaVerificationKey2018':
default:
return RSAKeyPair.from(data, options);
}
}
static async pbkdf2(password, salt, iterations, keySize) {
return new Promise((resolve, reject) => {
forge.pkcs5.pbkdf2(password, salt, iterations, keySize, (err, key) =>
err ? reject(err) : resolve(key));
});
}
/**
* @param owner {string} DID of key owner
*/
publicNode({owner}) {
const publicNode = {
id: this.id,
type: this.keyType,
owner
};
this.addEncodedPublicKey(publicNode);
return publicNode;
}
}
class Ed25519KeyPair extends LDKeyPair {
static async generate(options) {
// currently node only
if(!options.injector.env.nodejs) {
throw new Error(
'"Ed25519VerificationKey2018" is not supported on this platform yet.');
}
const bs58 = require('bs58');
const chloride = require('chloride');
const keyPair = chloride.crypto_sign_keypair();
const keys = new Ed25519KeyPair({
publicKey: bs58.encode(keyPair.publicKey),
secretKey: bs58.encode(keyPair.secretKey),
...options
});
return keys;
}
static async from(data, options) {
const keyPair = new Ed25519KeyPair({
publicKey: data.publicKeyBase58,
secretKey: data.secretKeyBase58,
id: data.id,
keyType: data.keyType,
...options
});
return keyPair;
}
// publicKeyPem, publicKeyJwk, publicKeyHex, publicKeyBase64, publicKeyBase58
async export() {
let keyNode = {
id: this.id,
keyType: this.keyType,
publicKeyBase58: this.publicKey
};
return this.addEncryptedSecretKey(keyNode);
}
addEncodedPublicKey(publicKeyNode) {
publicKeyNode.publicKeyBase58 = this.publicKey;
return publicKeyNode;
}
async addEncryptedSecretKey(keyNode) {
if(this.passphrase !== null) {
keyNode.secretKeyJwe = await this.encrypt({secretKeyBase58: this.secretKey},
this.passphrase);
} else {
// no passphrase, do not encrypt private key
keyNode.secretKeyBase58 = this.secretKey;
}
return keyNode;
}
/**
* @param secretKey
* @param password
*
* @returns {Promise<JWE>}
*/
async encrypt(secretKey, password) {
const keySize = 32;
const salt = forge.random.getBytesSync(32);
const iterations = 4096;
const key = await LDKeyPair.pbkdf2(password, salt, iterations, keySize);
const jweHeader = {
alg: 'PBES2-A128GCMKW',
enc: 'A128GCMKW',
jwk: {
kty: 'PBKDF2',
s: didUtil.encodeBase64Url(salt, {forge}),
c: iterations
}
};
// FIXME: this probably needs to be cleaned up/made more standard
const iv = forge.random.getBytesSync(12);
const cipher = forge.cipher.createCipher('AES-GCM', key);
cipher.start({iv});
cipher.update(forge.util.createBuffer(JSON.stringify(secretKey)));
cipher.finish();
const encrypted = cipher.output.getBytes();
const tag = cipher.mode.tag.getBytes();
const jwe = {
unprotected: jweHeader,
iv: didUtil.encodeBase64Url(iv, {forge}),
ciphertext: didUtil.encodeBase64Url(encrypted, {forge}),
tag: didUtil.encodeBase64Url(tag, {forge})
};
return jwe;
}
async decrypt(jwe, password) {
// FIXME: check header, implement according to JWE standard
const keySize = 32;
let {s: salt, c: iterations} = jwe.unprotected.jwk;
salt = didUtil.decodeBase64Url(salt, {forge});
const key = await LDKeyPair.pbkdf2(password, salt, iterations, keySize);
const iv = didUtil.decodeBase64Url(jwe.iv, {forge});
const tag = didUtil.decodeBase64Url(jwe.tag, {forge});
const decipher = forge.cipher.createDecipher('AES-GCM', key);
decipher.start({iv, tag});
decipher.update(didUtil.decodeBase64Url(jwe.ciphertext, {forge}));
const pass = decipher.finish();
if(!pass) {
throw new Error('Invalid password.');
}
const privateKey = JSON.parse(decipher.output.getBytes());
return privateKey;
}
}
class RSAKeyPair extends LDKeyPair {
/**
* @param options
* @param [options.keyBits]
* @param [options.exponent]
* @param [options.injector]
*
* @returns {Promise<RSAKeyPair>}
*/
static async generate(options) {
const keyBits = options.keyBits || DEFAULT_RSA_KEY_BITS;
const exponent = options.exponent || DEFAULT_RSA_EXPONENT;
if(options.injector.env.nodejs) {
const ursa = require('ursa');
const keyPair = ursa.generatePrivateKey(keyBits, exponent);
return new RSAKeyPair({
secretKey: forge.pki.privateKeyFromPem(keyPair.toPrivatePem()),
publicKey: forge.pki.publicKeyFromPem(keyPair.toPublicPem()),
...options
});
}
// Generate for browser
return new Promise((resolve, reject) => {
forge.pki.rsa.generateKeyPair(
{bits: keyBits, e: exponent},
(err, keyPair) => {
if(err) {
return reject(err);
}
resolve(new RSAKeyPair({
secretKey: keyPair.privateKey,
publicKey: keyPair.publicKey,
...options
}));
});
});
}
static async from(data, options) {
const keys = new RSAKeyPair({
publicKey: forge.pki.publicKeyFromPem(data.publicKeyPem),
secretKey: forge.pki.privateKeyFromPem(data.secretKeyPem),
id: data.id,
keyType: data.keyType,
...options
});
return keys;
}
async export() {
const keyNode = {
id: this.id,
keyType: this.keyType
};
this.addEncodedPublicKey(keyNode);
this.addEncryptedSecretKey(keyNode);
return keyNode;
}
addEncodedPublicKey(publicKeyNode) {
publicKeyNode.publicKeyPem = forge.pki.publicKeyToPem(this.publicKey);
return publicKeyNode;
}
async addEncryptedSecretKey(keyNode) {
if(this.passphrase !== null) {
keyNode.secretKeyPem = forge.pki.encryptRsaPrivateKey(
this.secretKey, this.passphrase, {algorithm: 'aes256'});
} else {
// no passphrase, do not encrypt private key
keyNode.secretKeyPem = forge.pki.privateKeyToPem(this.secretKey);
}
return keyNode;
}
}
module.exports = {
LDKeyPair,
Ed25519KeyPair,
RSAKeyPair
};