mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-02 14:11:02 +01:00
'Parse X.509 Certificate' Issuer and Subject name parsing improved. Closes #1365
This commit is contained in:
parent
7197a434c2
commit
2bf1ac6b9c
@ -9,35 +9,25 @@
|
|||||||
import { toHex, fromHex } from "./Hex.mjs";
|
import { toHex, fromHex } from "./Hex.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats Distinguished Name (DN) strings.
|
* Formats Distinguished Name (DN) objects to strings.
|
||||||
*
|
*
|
||||||
* @param {string} dnStr
|
* @param {Object} dnObj
|
||||||
* @param {number} indent
|
* @param {number} indent
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function formatDnStr(dnStr, indent) {
|
export function formatDnObj(dnObj, indent) {
|
||||||
const fields = dnStr.substr(1).replace(/([^\\])\//g, "$1$1/").split(/[^\\]\//);
|
let output = "";
|
||||||
let output = "",
|
|
||||||
maxKeyLen = 0,
|
|
||||||
key,
|
|
||||||
value,
|
|
||||||
i,
|
|
||||||
str;
|
|
||||||
|
|
||||||
for (i = 0; i < fields.length; i++) {
|
const maxKeyLen = dnObj.array.reduce((max, item) => {
|
||||||
if (!fields[i].length) continue;
|
return item[0].type.length > max ? item[0].type.length : max;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
key = fields[i].split("=")[0];
|
for (let i = 0; i < dnObj.array.length; i++) {
|
||||||
|
if (!dnObj.array[i].length) continue;
|
||||||
|
|
||||||
maxKeyLen = key.length > maxKeyLen ? key.length : maxKeyLen;
|
const key = dnObj.array[i][0].type;
|
||||||
}
|
const value = dnObj.array[i][0].value;
|
||||||
|
const str = `${key.padEnd(maxKeyLen, " ")} = ${value}\n`;
|
||||||
for (i = 0; i < fields.length; i++) {
|
|
||||||
if (!fields[i].length) continue;
|
|
||||||
|
|
||||||
key = fields[i].split("=")[0];
|
|
||||||
value = fields[i].split("=")[1];
|
|
||||||
str = key.padEnd(maxKeyLen, " ") + " = " + value + "\n";
|
|
||||||
|
|
||||||
output += str.padStart(indent + str.length, " ");
|
output += str.padStart(indent + str.length, " ");
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
import r from "jsrsasign";
|
import r from "jsrsasign";
|
||||||
import { fromBase64 } from "../lib/Base64.mjs";
|
import { fromBase64 } from "../lib/Base64.mjs";
|
||||||
import { toHex } from "../lib/Hex.mjs";
|
import { toHex } from "../lib/Hex.mjs";
|
||||||
import { formatByteStr, formatDnStr } 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";
|
||||||
|
|
||||||
@ -76,8 +76,8 @@ class ParseX509Certificate extends Operation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const sn = cert.getSerialNumberHex(),
|
const sn = cert.getSerialNumberHex(),
|
||||||
issuer = cert.getIssuerString(),
|
issuer = cert.getIssuer(),
|
||||||
subject = cert.getSubjectString(),
|
subject = cert.getSubject(),
|
||||||
pk = cert.getPublicKey(),
|
pk = cert.getPublicKey(),
|
||||||
pkFields = [],
|
pkFields = [],
|
||||||
sig = cert.getSignatureValueHex();
|
sig = cert.getSignatureValueHex();
|
||||||
@ -170,10 +170,10 @@ class ParseX509Certificate extends Operation {
|
|||||||
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 = formatDnStr(issuer, 2),
|
const issuerStr = formatDnObj(issuer, 2),
|
||||||
nbDate = formatDate(cert.getNotBefore()),
|
nbDate = formatDate(cert.getNotBefore()),
|
||||||
naDate = formatDate(cert.getNotAfter()),
|
naDate = formatDate(cert.getNotAfter()),
|
||||||
subjectStr = formatDnStr(subject, 2);
|
subjectStr = formatDnObj(subject, 2);
|
||||||
|
|
||||||
return `Version: ${cert.version} (0x${Utils.hex(cert.version - 1)})
|
return `Version: ${cert.version} (0x${Utils.hex(cert.version - 1)})
|
||||||
Serial number: ${new r.BigInteger(sn, 16).toString()} (0x${sn})
|
Serial number: ${new r.BigInteger(sn, 16).toString()} (0x${sn})
|
||||||
|
Loading…
Reference in New Issue
Block a user