Skip to content

Added operation Parse X509 Certificate Bundles #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .idea/CyberChef.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

448 changes: 448 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

72 changes: 23 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -137,6 +137,7 @@
"nodom": "^2.4.0",
"notepack.io": "^2.2.0",
"nwmatcher": "^1.4.4",
"openssl-nodejs": "^1.0.5",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is imported but never used.

"otp": "^0.1.3",
"popper.js": "^1.16.0",
"qr-image": "^3.2.0",
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
@@ -117,6 +117,7 @@
{
"name": "Public Key",
"ops": [
"Parse X.509 certificate bundles",
"Parse X.509 certificate",
"Parse ASN.1 hex string",
"PEM to Hex",
2 changes: 1 addition & 1 deletion src/core/lib/PublicKey.mjs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ export function formatDnStr (dnStr, indent) {

key = fields[i].split("=")[0];
value = fields[i].split("=")[1];
str = key.padEnd(maxKeyLen, " ") + " = " + value + "\n";
str = key.padEnd(maxKeyLen, " ") + " = " + value + ",";

output += str.padStart(indent + str.length, " ");
}
150 changes: 150 additions & 0 deletions src/core/operations/ParseX509CertificateBundles.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* @author nehagopinath [nehagowri94@gmail.com]
* @copyright Crown Copyright 2020
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import r from "jsrsasign";
import {formatDnStr} from "../lib/PublicKey";

/**
* ParseX509CertificateBundles operation
*/
class ParseX509CertificateBundles extends Operation {

/**
* ParseX509CertificateBundles constructor
*/
constructor() {
super();

this.name = "ParseX509CertificateBundles";
this.module = "PublicKey";
this.description = "X.509 is an ITU-T standard for a public key infrastructure (PKI) and Privilege Management Infrastructure (PMI). It is commonly involved with SSL/TLS security.<br><br>This operation displays the required contents of different certificates bundled in the input file in a human readable format, similar to the openssl command line tool.<br><br>Tags: X509, server hello, handshake";
this.infoURL = "https://wikipedia.org/wiki/X.509";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Input format",
"type": "option",
"value": ["PEM"]
}
];
this.patterns = [
{
"match": "^-+BEGIN CERTIFICATE-+\\r?\\n[\\da-z+/\\n\\r]+-+END CERTIFICATE-+\\r?\\n?$",
"flags": "i",
"args": [
"PEM"
]
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {

if (!input.length) {
return "No input";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use throw new OperationError(msg) instead of simply returning a string. This has repercussions for the NodeAPI.

}

const regex1 = /^-----BEGIN CERTIFICATE-----\r?\n((?:(?!-----).*\r?\n)*)-----END CERTIFICATE-----/gm;
const regex2 = /^-----BEGIN TRUSTED CERTIFICATE-----\r?\n((?:(?!-----).*\r?\n)*)-----END TRUSTED CERTIFICATE-----/gm;

let m;
let res = "";
let count = 0;

while ((m = regex1.exec(input)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These regexes can't hit on zero-width matches, so this check is unnecessary.

if (m.index === regex1.lastIndex) {
regex1.lastIndex++;

}

count++;
res += "\nCertificate:\n" + parseCert("-----BEGIN CERTIFICATE-----" + "\n" + m[1] + "\n" + "-----END CERTIFICATE-----");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

while ((m = regex2.exec(input)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These regexes can't hit on zero-width matches, so this check is unnecessary.

if (m.index === regex2.lastIndex) {
regex2.lastIndex++;

}

count++;
res += "\nCertificate:\n" + parseCert("-----BEGIN TRUSTED CERTIFICATE-----" + "\n" + m[1] + "\n" + "-----END TRUSTED CERTIFICATE-----");
}


return "Parsed Certificates =" + count + ":\n" + res;

}
}

/**
* parses individual certificates.
*
* @param {string} input
* @returns {string}
*/
function parseCert(input) {

const cert = new r.X509();
cert.readCertPEM(input);

const issuer = cert.getIssuerString(),
subject = cert.getSubjectString();

let extensions = "";

// Extensions
try {
// extensions =cert.getInfo();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line

extensions = cert.getInfo().split("X509v3 Extensions:\n")[1].split("signature")[0];
} catch (err) {
}

const issuerStr = formatDnStr(issuer, 2),
nbDate = formatDate(cert.getNotBefore()),
naDate = formatDate(cert.getNotAfter()),
subjectStr = formatDnStr(subject, 2);

return `Validity
Not Before: ${nbDate} (dd-mm-yyyy hh:mm:ss) (${cert.getNotBefore()})
Not After: ${naDate} (dd-mm-yyyy hh:mm:ss) (${cert.getNotAfter()})
Issuer
${issuerStr}
Subject
${subjectStr}
Extensions/basicConstraints
${extensions}`;

}

/**
* Formats dates.
*
* @param {string} dateStr
* @returns {string}
*/
function formatDate(dateStr) {
if (dateStr.length === 13) { // UTC Time
dateStr = (dateStr[0] < "5" ? "20" : "19") + dateStr;
}
return dateStr[6] + dateStr[7] + "/" +
dateStr[4] + dateStr[5] + "/" +
dateStr[0] + dateStr[1] + dateStr[2] + dateStr[3] + " " +
dateStr[8] + dateStr[9] + ":" +
dateStr[10] + dateStr[11] + ":" +
dateStr[12] + dateStr[13];
}

export default ParseX509CertificateBundles;