This commit is contained in:
Erik Hansson 2024-05-03 11:00:48 +01:00 committed by GitHub
commit 7ca80f0d68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 142 additions and 96 deletions

View File

@ -5,8 +5,8 @@
*/
import r from "jsrsasign";
import { fromBase64 } from "../lib/Base64.mjs";
import { toHex } from "../lib/Hex.mjs";
import { fromBase64, toBase64 } from "../lib/Base64.mjs";
import { fromHex, toHex } from "../lib/Hex.mjs";
import { formatByteStr, formatDnObj } from "../lib/PublicKey.mjs";
import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
@ -33,6 +33,11 @@ class ParseX509Certificate extends Operation {
"name": "Input format",
"type": "option",
"value": ["PEM", "DER Hex", "Base64", "Raw"]
},
{
"name": "Output format",
"type": "option",
"value": ["Text", "JSON", "PEM", "DER Hex", "Base64", "Raw"]
}
];
this.checks = [
@ -55,9 +60,13 @@ class ParseX509Certificate extends Operation {
}
const cert = new r.X509(),
inputFormat = args[0];
inputFormat = args[0],
outputFormat = args[1];
let undefinedInputFormat = false,
undefinedOuputFormat = false,
output = "";
let undefinedInputFormat = false;
try {
switch (inputFormat) {
case "DER Hex":
@ -81,6 +90,45 @@ class ParseX509Certificate extends Operation {
}
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(),
issuer = cert.getIssuer(),
subject = cert.getSubject(),
@ -201,8 +249,6 @@ Extensions
${extensions}`;
}
}
/**
* Formats dates.
*