2018-08-30 23:38:01 +02:00
|
|
|
/**
|
2020-04-06 14:35:14 +02:00
|
|
|
* @author Matt C [me@mitt.dev]
|
2018-08-30 23:38:01 +02:00
|
|
|
* @author gchq77703 []
|
2020-04-06 14:35:14 +02:00
|
|
|
* @copyright Crown Copyright 2020
|
2018-08-30 23:38:01 +02:00
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
2020-04-06 16:24:06 +02:00
|
|
|
import OperationError from "../errors/OperationError";
|
2018-08-30 23:38:01 +02:00
|
|
|
import forge from "node-forge/dist/forge.min.js";
|
2020-04-07 12:45:54 +02:00
|
|
|
import Utils from "../Utils.mjs";
|
2020-04-06 14:35:14 +02:00
|
|
|
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
|
2018-08-30 23:38:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RSA Sign operation
|
|
|
|
*/
|
|
|
|
class RSASign extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSASign constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "RSA Sign";
|
|
|
|
this.module = "Ciphers";
|
|
|
|
this.description = "Sign a plaintext message with a PEM encoded RSA key.";
|
|
|
|
this.infoURL = "https://wikipedia.org/wiki/RSA_(cryptosystem)";
|
|
|
|
this.inputType = "string";
|
2020-04-07 12:45:54 +02:00
|
|
|
this.outputType = "string";
|
2018-08-30 23:38:01 +02:00
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "RSA Private Key (PEM)",
|
|
|
|
type: "text",
|
|
|
|
value: "-----BEGIN RSA PRIVATE KEY-----"
|
|
|
|
},
|
|
|
|
{
|
2020-04-06 14:35:14 +02:00
|
|
|
name: "Key Password",
|
2018-08-30 23:38:01 +02:00
|
|
|
type: "text",
|
|
|
|
value: ""
|
2020-04-06 14:35:14 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Message Digest Algorithm",
|
|
|
|
type: "option",
|
|
|
|
value: Object.keys(MD_ALGORITHMS)
|
2018-08-30 23:38:01 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
2020-04-06 14:35:14 +02:00
|
|
|
const [key, password, mdAlgo] = args;
|
2020-04-06 16:24:06 +02:00
|
|
|
if (key.replace("-----BEGIN RSA PRIVATE KEY-----", "").length === 0) {
|
|
|
|
throw new OperationError("Please enter a private key.");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const privateKey = forge.pki.decryptRsaPrivateKey(key, password);
|
|
|
|
// Generate message hash
|
|
|
|
const md = MD_ALGORITHMS[mdAlgo].create();
|
|
|
|
md.update(input, "utf8");
|
2020-04-07 12:45:54 +02:00
|
|
|
// Sign message hash
|
|
|
|
const sig = privateKey.sign(md);
|
|
|
|
return sig;
|
2020-04-06 16:24:06 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new OperationError(err);
|
|
|
|
}
|
2018-08-30 23:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RSASign;
|