mirror of
https://github.com/gchq/CyberChef
synced 2025-01-12 04:28:53 +00:00
Add X.509 output formats
This commit is contained in:
parent
2efd075803
commit
daef663727
1 changed files with 142 additions and 96 deletions
|
@ -5,8 +5,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import r from "jsrsasign";
|
import r from "jsrsasign";
|
||||||
import { fromBase64 } from "../lib/Base64.mjs";
|
import { fromBase64, toBase64 } from "../lib/Base64.mjs";
|
||||||
import { toHex } from "../lib/Hex.mjs";
|
import { fromHex, toHex } from "../lib/Hex.mjs";
|
||||||
import { formatByteStr, formatDnObj } from "../lib/PublicKey.mjs";
|
import { formatByteStr, formatDnObj } from "../lib/PublicKey.mjs";
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import Utils from "../Utils.mjs";
|
import Utils from "../Utils.mjs";
|
||||||
|
@ -33,6 +33,11 @@ class ParseX509Certificate extends Operation {
|
||||||
"name": "Input format",
|
"name": "Input format",
|
||||||
"type": "option",
|
"type": "option",
|
||||||
"value": ["PEM", "DER Hex", "Base64", "Raw"]
|
"value": ["PEM", "DER Hex", "Base64", "Raw"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Output format",
|
||||||
|
"type": "option",
|
||||||
|
"value": ["Text", "JSON", "PEM", "DER Hex", "Base64", "Raw"]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
this.checks = [
|
this.checks = [
|
||||||
|
@ -55,9 +60,13 @@ class ParseX509Certificate extends Operation {
|
||||||
}
|
}
|
||||||
|
|
||||||
const cert = new r.X509(),
|
const cert = new r.X509(),
|
||||||
inputFormat = args[0];
|
inputFormat = args[0],
|
||||||
|
outputFormat = args[1];
|
||||||
|
|
||||||
|
let undefinedInputFormat = false,
|
||||||
|
undefinedOuputFormat = false,
|
||||||
|
output = "";
|
||||||
|
|
||||||
let undefinedInputFormat = false;
|
|
||||||
try {
|
try {
|
||||||
switch (inputFormat) {
|
switch (inputFormat) {
|
||||||
case "DER Hex":
|
case "DER Hex":
|
||||||
|
@ -81,6 +90,45 @@ class ParseX509Certificate extends Operation {
|
||||||
}
|
}
|
||||||
if (undefinedInputFormat) throw "Undefined input format";
|
if (undefinedInputFormat) throw "Undefined input format";
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (outputFormat) {
|
||||||
|
case "Text":
|
||||||
|
output = formatText(cert);
|
||||||
|
break;
|
||||||
|
case "JSON":
|
||||||
|
output = JSON.stringify(cert.getParam());
|
||||||
|
break;
|
||||||
|
case "DER Hex":
|
||||||
|
output = cert.hex;
|
||||||
|
break;
|
||||||
|
case "PEM":
|
||||||
|
output = r.hextopem(cert.hex, "CERTIFICATE");
|
||||||
|
break;
|
||||||
|
case "Base64":
|
||||||
|
output = toBase64(fromHex(cert.hex));
|
||||||
|
break;
|
||||||
|
case "Raw":
|
||||||
|
output = Utils.byteArrayToChars(fromHex(cert.hex));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
undefinedOuputFormat = true;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw "Certificate encoding error (what even hapened?)";
|
||||||
|
}
|
||||||
|
if (undefinedOuputFormat) throw "Undefined output format";
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Format X.509 certificate.
|
||||||
|
*
|
||||||
|
* @param {r.X509} cert
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function formatText(cert) {
|
||||||
const sn = cert.getSerialNumberHex(),
|
const sn = cert.getSerialNumberHex(),
|
||||||
issuer = cert.getIssuer(),
|
issuer = cert.getIssuer(),
|
||||||
subject = cert.getSubject(),
|
subject = cert.getSubject(),
|
||||||
|
@ -105,7 +153,7 @@ class ParseX509Certificate extends Operation {
|
||||||
});
|
});
|
||||||
pkFields.push({
|
pkFields.push({
|
||||||
key: "Length",
|
key: "Length",
|
||||||
value: (((new r.BigInteger(pk.pubKeyHex, 16)).bitLength()-3) /2) + " bits"
|
value: (((new r.BigInteger(pk.pubKeyHex, 16)).bitLength() - 3) / 2) + " bits"
|
||||||
});
|
});
|
||||||
pkFields.push({
|
pkFields.push({
|
||||||
key: "pub",
|
key: "pub",
|
||||||
|
@ -174,7 +222,7 @@ class ParseX509Certificate extends Operation {
|
||||||
// Extensions
|
// Extensions
|
||||||
try {
|
try {
|
||||||
extensions = cert.getInfo().split("X509v3 Extensions:\n")[1].split("signature")[0];
|
extensions = cert.getInfo().split("X509v3 Extensions:\n")[1].split("signature")[0];
|
||||||
} catch (err) {}
|
} catch (err) { }
|
||||||
|
|
||||||
const issuerStr = formatDnObj(issuer, 2),
|
const issuerStr = formatDnObj(issuer, 2),
|
||||||
nbDate = formatDate(cert.getNotBefore()),
|
nbDate = formatDate(cert.getNotBefore()),
|
||||||
|
@ -199,8 +247,6 @@ ${sigStr}
|
||||||
|
|
||||||
Extensions
|
Extensions
|
||||||
${extensions}`;
|
${extensions}`;
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue