Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/key/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { openpgp } from '../openpgp';
import { serverTime } from '../serverTime';
import { DEFAULT_OFFSET } from '../constants';
import { decryptPrivateKey } from './decrypt';
import { encryptPrivateKey } from './encrypt';

// returns promise for generated RSA public and encrypted private keys
export function generateKey({ passphrase, date = serverTime(), offset = DEFAULT_OFFSET, ...rest }) {
Expand Down Expand Up @@ -165,3 +167,30 @@ export async function genPrivateEphemeralKey({ Curve, d, V, Fingerprint }) {
false
);
}

/**
* Add subkeys to an existing private key
*
* @param{Object<Options>} Private key, passphrase, and additional options
* @param {String} The armored private key
* @param {String} The key passphrase if it is encrypted
* @param {Object<Array>} Options for subkey generation
* @retruns {String} The updated key in armored format
* @async
*/
export async function addSubkey({ armoredKey, passphrase = null, options}) {
var key;
if (passphrase !== null) {
key = await decryptPrivateKey(armoredKey, passphrase);
} else {
const read = await openpgp.key.readArmored(armoredKey);
key = read.keys[0];
}

const newKey = await key.addSubkey(options);

if (passphrase !== null) {
return await encryptPrivateKey(newKey, passphrase);
}
return newKey.armor();
}
3 changes: 2 additions & 1 deletion lib/pmcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export {
getMatchingKey,
cloneKey,
genPublicEphemeralKey,
genPrivateEphemeralKey
genPrivateEphemeralKey,
addSubkey
} from './key/utils';

export { decryptPrivateKey, decryptSessionKey } from './key/decrypt';
Expand Down