-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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-----"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use template strings to tidy this up a little. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals |
||
} | ||
|
||
while ((m = regex2.exec(input)) !== null) { | ||
// This is necessary to avoid infinite loops with zero-width matches | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
There was a problem hiding this comment.
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.