UNPKG

@peculiar/fortify-webcomponents

Version:

Web-components for creating CSR or Certificate and viewing certificates list using Fortify

158 lines (154 loc) 6.23 kB
/*! * © Peculiar Ventures https://peculiarventures.com/ - BSD 3-Clause License */ 'use strict'; const certification_request = require('./certification_request-89396ee6.js'); const utils = require('./utils-10d69476.js'); /*! * © Peculiar Ventures https://peculiarventures.com/ - MIT License */ class X509Certificate extends certification_request.AsnData { constructor(raw) { super(utils.certificateRawToBuffer(raw), certification_request.Certificate); this.thumbprints = {}; this.type = 'X.509 Certificate'; this.tag = certification_request.PemConverter.CertificateTag; const { tbsCertificate } = this.asn; this.serialNumber = certification_request.Convert_1.ToHex(tbsCertificate.serialNumber); this.subject = new certification_request.Name(tbsCertificate.subject).toJSON(); this.issuer = new certification_request.Name(tbsCertificate.issuer).toJSON(); this.version = tbsCertificate.version + 1; const notBefore = tbsCertificate.validity.notBefore.utcTime || tbsCertificate.validity.notBefore.generalTime; if (!notBefore) { throw new Error("Cannot get 'notBefore' value"); } this.notBefore = notBefore; const notAfter = tbsCertificate.validity.notAfter.utcTime || tbsCertificate.validity.notAfter.generalTime; if (!notAfter) { throw new Error("Cannot get 'notAfter' value"); } this.notAfter = notAfter; this.validity = certification_request.dateDiff(this.notBefore, this.notAfter); } parseExtensions() { const { tbsCertificate } = this.asn; if (tbsCertificate.extensions) { this.extensions = tbsCertificate.extensions .map((e) => new utils.Extension(certification_request.AsnConvert.serialize(e))); } } getPublicKeyInfo(publicKeyInfo) { const { subjectPublicKey, algorithm } = publicKeyInfo; let params; if (algorithm.algorithm === certification_request.id_ecPublicKey && algorithm.parameters) { params = certification_request.AsnConvert.parse(algorithm.parameters, certification_request.ECParameters); } if (algorithm.algorithm === certification_request.id_rsaEncryption) { params = certification_request.AsnConvert.parse(subjectPublicKey, certification_request.RSAPublicKey); } if (algorithm.algorithm === certification_request.id_composite_key) { params = certification_request.AsnConvert.parse(subjectPublicKey, certification_request.CompositePublicKey); params = params.map((param) => this.getPublicKeyInfo(param)); } const spki = certification_request.AsnConvert.serialize(publicKeyInfo); return { params, value: spki, algorithm: algorithm.algorithm, }; } get publicKey() { return this.getPublicKeyInfo(this.asn.tbsCertificate.subjectPublicKeyInfo); } get signature() { const { signatureValue, signatureAlgorithm } = this.asn; let params; if (signatureAlgorithm.algorithm === certification_request.id_alg_composite) { const compositeSignatureValues = certification_request.AsnConvert.parse(signatureValue, certification_request.CompositeSignatureValue); const compositeParams = certification_request.AsnConvert.parse(signatureAlgorithm.parameters, certification_request.CompositeParams); params = compositeParams.map((param, index) => (Object.assign(Object.assign({}, param), { value: compositeSignatureValues[index] }))); } return { params, value: signatureValue, algorithm: signatureAlgorithm.algorithm, }; } async getThumbprint(algorithm = 'SHA-1') { try { const thumbprint = await utils.getCertificateThumbprint(algorithm, this.raw); if (thumbprint) { this.thumbprints[algorithm] = certification_request.Convert_1.ToHex(thumbprint); } } catch (error) { console.error('Error thumbprint get:', error); } } get commonName() { if (!this.subject) { return ''; } for (let i = 0; i < this.subject.length; i += 1) { const name = this.subject[i]; if (name.shortName === 'CN' || name.shortName === 'E' || name.shortName === 'O') { return name.value; } } return ''; } get issuerCommonName() { if (!this.issuer) { return ''; } for (let i = 0; i < this.issuer.length; i += 1) { const name = this.issuer[i]; if (name.shortName === 'CN') { return name.value; } if (name.shortName === 'E') { return name.value; } } return ''; } get isRoot() { return JSON.stringify(this.issuer) === JSON.stringify(this.subject); } subjectToString() { if (!this.subject) { return ''; } return this.subject .map((name) => (`${name.shortName}=${name.value}`)) .join(', '); } issuerToString() { if (!this.issuer) { return ''; } return this.issuer .map((name) => (`${name.shortName}=${name.value}`)) .join(', '); } toString(format = 'pem') { switch (format) { case 'pem': return certification_request.PemConverter.encode(this.raw, this.tag); case 'base64url': return certification_request.Convert_1.ToBase64Url(this.raw); default: return certification_request.Convert_1.ToBase64(this.raw); } } downloadAsPEM(name) { certification_request.Download.cert.asPEM(this.toString('pem'), name || this.commonName); } downloadAsDER(name) { certification_request.Download.cert.asDER(this.raw, name || this.commonName); } } exports.X509Certificate = X509Certificate; //# sourceMappingURL=x509_certificate-197d7aaf.js.map