Skip to content

Commit

Permalink
Merge pull request gchq#2 from artemisbot/features/add-pgp-kbpgp
Browse files Browse the repository at this point in the history
Promisified generation of key pair
  • Loading branch information
tlwr authored Dec 23, 2017
2 parents dcd8f98 + 670566b commit 5a5ce11
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 40 deletions.
141 changes: 135 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"crypto-api": "^0.7.5",
"crypto-js": "^3.1.9-1",
"diff": "^3.3.1",
"es6-promisify": "^5.0.0",
"escodegen": "^1.9.0",
"esmangle": "^1.0.1",
"esprima": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/core/config/OperationConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3848,6 +3848,7 @@ const OperationConfig = {
},
"Generate PGP Key Pair": {
module: "PGP",
manualBake: true,
description: "",
inputType: "string",
outputType: "string",
Expand Down
49 changes: 15 additions & 34 deletions src/core/operations/PGP.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as kbpgp from "kbpgp";
import promisify from "es6-promisify";

const ECC_SIZES = ["256", "384"];
const RSA_SIZES = ["1024", "2048", "4096"];
Expand Down Expand Up @@ -116,41 +117,21 @@ const PGP = {
expire_in: 86400 * 365 * 2 // eslint-disable-line camelcase
}],
};

return new Promise((resolve, reject) => {
kbpgp.KeyManager.generate(keyGenerationOptions, (genErr, unsignedKey) => {
if (genErr) {
return reject(`Error from kbpgp whilst generating key: ${genErr}`);
}

unsignedKey.sign({}, signErr => {
let signedKey = unsignedKey;
if (signErr) {
return reject(`Error from kbpgp whilst signing the generated key: ${signErr}`);
}

let privateKeyExportOptions = {};
if (password) privateKeyExportOptions.passphrase = password;

signedKey.export_pgp_private(privateKeyExportOptions, (privateExportErr, privateKey) => {
if (privateExportErr) {
return reject(`Error from kbpgp whilst exporting the private part of the signed key: ${privateExportErr}`);
}

signedKey.export_pgp_public({}, (publicExportErr, publicKey) => {
if (publicExportErr) {
return reject(`Error from kbpgp whilst exporting the public part of the signed key: ${publicExportErr}`);
}

return resolve(privateKey + "\n" + publicKey);
});
});

});
});
return new Promise(async (resolve, reject) => {
try {
const unsignedKey = await promisify(kbpgp.KeyManager.generate)(keyGenerationOptions);
await promisify(unsignedKey.sign, unsignedKey)({});
let signedKey = unsignedKey;
let privateKeyExportOptions = {};
if (password) privateKeyExportOptions.passphrase = password;
const privateKey = await promisify(signedKey.export_pgp_private, signedKey)(privateKeyExportOptions);
const publicKey = await promisify(signedKey.export_pgp_public, signedKey)({});
resolve(privateKey + "\n" + publicKey);
} catch (err) {
reject(`Error from kbpgp whilst generating key pair: ${err}`);
}
});
},

}
};

export default PGP;

0 comments on commit 5a5ce11

Please sign in to comment.