2021-11-13 01:46:31 +01:00
|
|
|
/**
|
|
|
|
* @author cplussharp
|
|
|
|
* @copyright Crown Copyright 2021
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import r from "jsrsasign";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ECDSA Sign operation
|
|
|
|
*/
|
|
|
|
class ECDSASignatureConversion extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ECDSASignatureConversion constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "ECDSA Signature Conversion";
|
|
|
|
this.module = "Ciphers";
|
|
|
|
this.description = "Convert an ECDSA signature between hex, asn1 and json.";
|
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm";
|
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "Input Format",
|
|
|
|
type: "option",
|
|
|
|
value: [
|
|
|
|
"Auto",
|
|
|
|
"ASN.1 HEX",
|
2024-04-14 11:34:59 +02:00
|
|
|
"P1363 HEX",
|
2021-11-13 01:46:31 +01:00
|
|
|
"JSON"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Output Format",
|
|
|
|
type: "option",
|
|
|
|
value: [
|
|
|
|
"ASN.1 HEX",
|
2024-04-14 11:34:59 +02:00
|
|
|
"P1363 HEX",
|
2021-11-13 01:46:31 +01:00
|
|
|
"JSON"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
let inputFormat = args[0];
|
|
|
|
const outputFormat = args[1];
|
|
|
|
|
|
|
|
// detect input format
|
|
|
|
if (inputFormat === "Auto") {
|
|
|
|
if (input.substr(0, 2) === "30" && r.ASN1HEX.isASN1HEX(input)) {
|
|
|
|
inputFormat = "ASN.1 HEX";
|
|
|
|
} else if (input.indexOf("{") !== -1) {
|
|
|
|
inputFormat = "JSON";
|
|
|
|
} else {
|
2024-04-14 11:34:59 +02:00
|
|
|
inputFormat = "P1363 HEX";
|
2021-11-13 01:46:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert input to ASN.1 hex
|
|
|
|
let signatureASN1Hex;
|
|
|
|
switch (inputFormat) {
|
|
|
|
case "ASN.1 HEX":
|
|
|
|
signatureASN1Hex = input;
|
|
|
|
break;
|
2024-04-14 11:34:59 +02:00
|
|
|
case "P1363 HEX":
|
2021-11-13 01:46:31 +01:00
|
|
|
signatureASN1Hex = r.KJUR.crypto.ECDSA.concatSigToASN1Sig(input);
|
|
|
|
break;
|
|
|
|
case "JSON": {
|
|
|
|
const inputJson = JSON.parse(input);
|
|
|
|
signatureASN1Hex = r.KJUR.crypto.ECDSA.hexRSSigToASN1Sig(inputJson.r, inputJson.s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert ASN.1 hex to output format
|
|
|
|
let result;
|
|
|
|
switch (outputFormat) {
|
|
|
|
case "ASN.1 HEX":
|
|
|
|
result = signatureASN1Hex;
|
|
|
|
break;
|
2024-04-14 11:34:59 +02:00
|
|
|
case "P1363 HEX":
|
2021-11-13 01:46:31 +01:00
|
|
|
result = r.KJUR.crypto.ECDSA.asn1SigToConcatSig(signatureASN1Hex);
|
|
|
|
break;
|
|
|
|
case "JSON": {
|
|
|
|
const signatureRS = r.KJUR.crypto.ECDSA.parseSigHexInHexRS(signatureASN1Hex);
|
|
|
|
result = JSON.stringify(signatureRS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ECDSASignatureConversion;
|