2018-05-15 11:15:31 +02:00
|
|
|
/**
|
|
|
|
* @author tlwr [toby@toby.codes]
|
|
|
|
* @copyright Crown Copyright 2017
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
2018-05-15 17:04:57 +02:00
|
|
|
import kbpgp from "kbpgp";
|
2018-05-15 11:15:31 +02:00
|
|
|
import { ASP, importPrivateKey, importPublicKey } from "../lib/PGP";
|
2018-05-15 19:01:04 +02:00
|
|
|
import OperationError from "../errors/OperationError";
|
2018-05-15 17:04:57 +02:00
|
|
|
import promisifyDefault from "es6-promisify";
|
|
|
|
const promisify = promisifyDefault.promisify;
|
2018-05-15 11:15:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PGP Encrypt and Sign operation
|
|
|
|
*/
|
|
|
|
class PGPEncryptAndSign extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PGPEncryptAndSign constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "PGP Encrypt and Sign";
|
|
|
|
this.module = "PGP";
|
2018-05-16 11:17:49 +02:00
|
|
|
this.description = [
|
|
|
|
"Input: the cleartext you want to sign.",
|
|
|
|
"<br><br>",
|
|
|
|
"Arguments: the ASCII-armoured private key of the signer (plus the private key password if necessary)",
|
|
|
|
"and the ASCII-armoured PGP public key of the recipient.",
|
|
|
|
"<br><br>",
|
|
|
|
"This operation uses PGP to produce an encrypted digital signature.",
|
|
|
|
"<br><br>",
|
|
|
|
"Pretty Good Privacy is an encryption standard (OpenPGP) used for encrypting, decrypting, and signing messages.",
|
|
|
|
"<br><br>",
|
|
|
|
"This function uses the Keybase implementation of PGP.",
|
|
|
|
].join("\n");
|
2018-05-15 11:15:31 +02:00
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Private key of signer",
|
|
|
|
"type": "text",
|
|
|
|
"value": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Private key passphrase",
|
|
|
|
"type": "string",
|
|
|
|
"value": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Public key of recipient",
|
|
|
|
"type": "text",
|
|
|
|
"value": ""
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
2018-05-15 19:01:04 +02:00
|
|
|
*
|
|
|
|
* @throws {OperationError} if failure to sign message
|
2018-05-15 11:15:31 +02:00
|
|
|
*/
|
|
|
|
async run(input, args) {
|
|
|
|
const message = input,
|
2018-05-16 11:17:49 +02:00
|
|
|
[privateKey, passphrase, publicKey] = args;
|
2018-05-15 11:15:31 +02:00
|
|
|
let signedMessage;
|
|
|
|
|
2018-05-15 19:01:04 +02:00
|
|
|
if (!privateKey) throw new OperationError("Enter the private key of the signer.");
|
|
|
|
if (!publicKey) throw new OperationError("Enter the public key of the recipient.");
|
2018-05-15 11:15:31 +02:00
|
|
|
const privKey = await importPrivateKey(privateKey, passphrase);
|
|
|
|
const pubKey = await importPublicKey(publicKey);
|
|
|
|
|
|
|
|
try {
|
|
|
|
signedMessage = await promisify(kbpgp.box)({
|
|
|
|
"msg": message,
|
|
|
|
"encrypt_for": pubKey,
|
|
|
|
"sign_with": privKey,
|
|
|
|
"asp": ASP
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2018-05-15 19:01:04 +02:00
|
|
|
throw new OperationError(`Couldn't sign message: ${err}`);
|
2018-05-15 11:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return signedMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PGPEncryptAndSign;
|