diff --git a/src/core/operations/ParseCSR.mjs b/src/core/operations/ParseCSR.mjs new file mode 100644 index 00000000..a96e7b3b --- /dev/null +++ b/src/core/operations/ParseCSR.mjs @@ -0,0 +1,56 @@ +/** + * @author arnydo [arnydo@protonmail.com] + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ + +import Operation from "../Operation"; +import forge from "node-forge/dist/forge.min.js"; + +/** + * ParseCSR operation + */ +class ParseCSR extends Operation { + + /** + * ParseCSR constructor + */ + constructor() { + super(); + + this.name = "Parse CSR"; + this.module = "PublicKey"; + this.description = "Parses a Base64 encoded Certificate Signing Request (CSR).

In public key infrastructure (PKI) systems, a certificate signing request (also CSR or certification request) is a message sent from an applicant to a certificate authority in order to apply for a digital identity certificate. It usually contains the public key for which the certificate should be issued, identifying information (such as a domain name) and integrity protection (e.g., a digital signature). The most common format for CSRs is the PKCS #10 specification and another is the Signed Public Key and Challenge SPKAC format generated by some web browsers.

This operation displays the contents of a certificate signing request in a human readable format, similar to the openssl command line tool."; + this.infoURL = "https://en.wikipedia.org/wiki/Certificate_signing_request"; + this.inputType = "string"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input) { + if (!input.length) { + return "No input"; + } + const csr = forge.pki.certificationRequestFromPem(input); + const extInfo = csr.getAttribute({ name: "extensionRequest" }).extensions; + const subjectAltNames = extInfo[0].altNames.map(function(name){ + return name.value; + }); + return `CommonName: ${csr.subject.getField("CN").value} +Locality: ${csr.subject.getField("L").value} +State: ${csr.subject.getField("ST").value} +Country: ${csr.subject.getField("C").value} +EmailAddress: ${csr.subject.getField("E").value} +Subject Alternative Names: ${subjectAltNames.map(function (name) { + return name; + })}`; + } + +} + +export default ParseCSR;