Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit f71a6bb

Browse files
committed
Revert "feat: adds support for ed25199 and secp256k1 (#31)"
This reverts commit 9eb11f4.
1 parent e30330e commit f71a6bb

File tree

3 files changed

+98
-168
lines changed

3 files changed

+98
-168
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ Managing a key
5757
- `createKey (name, type, size, callback)`
5858
- `renameKey (oldName, newName, callback)`
5959
- `removeKey (name, callback)`
60-
- `exportKey (name, password, callback)` // Omit _password_ for `ed25199` or `secp256k1` keys
61-
- `importKey (name, encKey, password, callback)` // Omit _password_ for `ed25199` or `secp256k1` keys
60+
- `exportKey (name, password, callback)`
61+
- `importKey (name, pem, password, callback)`
6262
- `importPeer (name, peer, callback)`
6363

6464
A naming service for a key
@@ -67,7 +67,7 @@ A naming service for a key
6767
- `findKeyById (id, callback)`
6868
- `findKeyByName (name, callback)`
6969

70-
Cryptographically protected messages (Only supported with RSA keys)
70+
Cryptographically protected messages
7171

7272
- `cms.encrypt (name, plain, callback)`
7373
- `cms.decrypt (cmsData, callback)`

src/keychain.js

Lines changed: 59 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const NIST = {
2020
}
2121

2222
const defaultOptions = {
23-
// See https://cryptosense.com/blog/parameter-choice-for-pbkdf2/
23+
// See https://cryptosense.com/parametesr-choice-for-pbkdf2/
2424
dek: {
2525
keyLength: 512 / 8,
2626
iterationCount: 10000,
@@ -197,8 +197,7 @@ class Keychain {
197197
if (err) return _error(callback, err)
198198
if (exists) return _error(callback, `Key '${name}' already exists`)
199199

200-
type = type.toLowerCase()
201-
switch (type) {
200+
switch (type.toLowerCase()) {
202201
case 'rsa':
203202
if (size < 2048) {
204203
return _error(callback, `Invalid RSA key size ${size}`)
@@ -212,16 +211,21 @@ class Keychain {
212211
if (err) return _error(callback, err)
213212
keypair.id((err, kid) => {
214213
if (err) return _error(callback, err)
215-
216-
if (type === 'ed25519' || type === 'secp256k1') {
217-
const keypairMarshal = keypair.bytes
218-
self._storeKey(name, kid, keypairMarshal, dsname, callback)
219-
} else {
220-
keypair.export(this._(), (err, pem) => {
214+
keypair.export(this._(), (err, pem) => {
215+
if (err) return _error(callback, err)
216+
const keyInfo = {
217+
name: name,
218+
id: kid
219+
}
220+
const batch = self.store.batch()
221+
batch.put(dsname, pem)
222+
batch.put(DsInfoName(name), JSON.stringify(keyInfo))
223+
batch.commit((err) => {
221224
if (err) return _error(callback, err)
222-
self._storeKey(name, kid, pem, dsname, callback)
225+
226+
callback(null, keyInfo)
223227
})
224-
}
228+
})
225229
})
226230
})
227231
})
@@ -361,85 +365,76 @@ class Keychain {
361365
}
362366

363367
/**
364-
* Export an existing key.
365-
* If it's as an RSA key, include a password to export as a PEM encrypted PKCS #8 string
368+
* Export an existing key as a PEM encrypted PKCS #8 string
366369
*
367370
* @param {string} name - The local key name; must already exist.
368-
* @param {string} password - The password, for RSA keys (optional)
371+
* @param {string} password - The password
369372
* @param {function(Error, string)} callback
370373
* @returns {undefined}
371374
*/
372375
exportKey (name, password, callback) {
373-
if (typeof password === 'function' && typeof callback === 'undefined') {
374-
callback = password
375-
password = undefined
376-
}
377376
if (!validateKeyName(name)) {
378377
return _error(callback, `Invalid key name '${name}'`)
379378
}
379+
if (!password) {
380+
return _error(callback, 'Password is required')
381+
}
380382

381383
const dsname = DsName(name)
382384
this.store.get(dsname, (err, res) => {
383385
if (err) {
384386
return _error(callback, `Key '${name}' does not exist. ${err.message}`)
385387
}
386-
if (password) {
387-
const encKey = res.toString()
388-
crypto.keys.import(encKey, this._(), (err, privateKey) => {
389-
if (err) return _error(callback, err)
390-
privateKey.export(password, callback)
391-
})
392-
} else {
393-
crypto.keys.unmarshalPrivateKey(res, callback)
394-
}
388+
const pem = res.toString()
389+
crypto.keys.import(pem, this._(), (err, privateKey) => {
390+
if (err) return _error(callback, err)
391+
privateKey.export(password, callback)
392+
})
395393
})
396394
}
397395

398396
/**
399-
* Import a new key
400-
* If it's as an RSA key, include a password to import from a PEM encrypted PKCS #8 string
397+
* Import a new key from a PEM encoded PKCS #8 string
401398
*
402399
* @param {string} name - The local key name; must not already exist.
403-
* @param {string} encKey - The encoded key. If it's an RSA key, it needs to be a PEM encoded PKCS #8 string
404-
* @param {string} password - The password for RSA keys. (optional)
400+
* @param {string} pem - The PEM encoded PKCS #8 string
401+
* @param {string} password - The password.
405402
* @param {function(Error, KeyInfo)} callback
406403
* @returns {undefined}
407404
*/
408-
importKey (name, encKey, password, callback) {
405+
importKey (name, pem, password, callback) {
409406
const self = this
410-
if (typeof password === 'function' && typeof callback === 'undefined') {
411-
callback = password
412-
password = undefined
413-
}
414407
if (!validateKeyName(name) || name === 'self') {
415408
return _error(callback, `Invalid key name '${name}'`)
416409
}
417-
if (!encKey) {
418-
return _error(callback, 'The encoded key is required')
410+
if (!pem) {
411+
return _error(callback, 'PEM encoded key is required')
419412
}
420-
421413
const dsname = DsName(name)
422414
self.store.has(dsname, (err, exists) => {
423415
if (err) return _error(callback, err)
424416
if (exists) return _error(callback, `Key '${name}' already exists`)
425-
426-
if (password) {
427-
crypto.keys.import(encKey, password, (err, privateKey) => {
428-
if (err) return _error(callback, 'Cannot read the key, most likely the password is wrong')
429-
privateKey.id((err, kid) => {
417+
crypto.keys.import(pem, password, (err, privateKey) => {
418+
if (err) return _error(callback, 'Cannot read the key, most likely the password is wrong')
419+
privateKey.id((err, kid) => {
420+
if (err) return _error(callback, err)
421+
privateKey.export(this._(), (err, pem) => {
430422
if (err) return _error(callback, err)
431-
privateKey.export(this._(), (err, pem) => {
423+
const keyInfo = {
424+
name: name,
425+
id: kid
426+
}
427+
const batch = self.store.batch()
428+
batch.put(dsname, pem)
429+
batch.put(DsInfoName(name), JSON.stringify(keyInfo))
430+
batch.commit((err) => {
432431
if (err) return _error(callback, err)
433-
self._storeKey(name, kid, pem, dsname, callback)
432+
433+
callback(null, keyInfo)
434434
})
435435
})
436436
})
437-
} else {
438-
encKey.id((err, kid) => {
439-
if (err) return _error(callback, err)
440-
self._storeKey(name, kid, encKey.bytes, dsname, callback)
441-
})
442-
}
437+
})
443438
})
444439
}
445440

@@ -462,28 +457,23 @@ class Keychain {
462457
if (err) return _error(callback, err)
463458
privateKey.export(this._(), (err, pem) => {
464459
if (err) return _error(callback, err)
465-
self._storeKey(name, kid, pem, dsname, callback)
460+
const keyInfo = {
461+
name: name,
462+
id: kid
463+
}
464+
const batch = self.store.batch()
465+
batch.put(dsname, pem)
466+
batch.put(DsInfoName(name), JSON.stringify(keyInfo))
467+
batch.commit((err) => {
468+
if (err) return _error(callback, err)
469+
470+
callback(null, keyInfo)
471+
})
466472
})
467473
})
468474
})
469475
}
470476

471-
_storeKey (name, kid, encKey, dsname, callback) {
472-
const self = this
473-
const keyInfo = {
474-
name: name,
475-
id: kid
476-
}
477-
const batch = self.store.batch()
478-
batch.put(dsname, encKey)
479-
batch.put(DsInfoName(name), JSON.stringify(keyInfo))
480-
batch.commit((err) => {
481-
if (err) return _error(callback, err)
482-
483-
callback(null, keyInfo)
484-
})
485-
}
486-
487477
/**
488478
* Gets the private key as PEM encoded PKCS #8 string.
489479
*

0 commit comments

Comments
 (0)