UNPKG

@peculiar/fortify-webcomponents

Version:

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

161 lines (158 loc) 6.18 kB
/*! * © Peculiar Ventures https://peculiarventures.com/ - BSD 3-Clause License */ import { A as AsnData, C as Certificate, P as PemConverter, b as buildExports, N as Name, d as dateDiff, a as AsnConvert, i as id_ecPublicKey, E as ECParameters, c as id_rsaEncryption, R as RSAPublicKey, e as id_composite_key, f as CompositePublicKey, g as id_alg_composite, h as CompositeSignatureValue, j as CompositeParams, D as Download } from './ssh_certificate-CDwMJyak.js'; import { c as certificateRawToBuffer, E as Extension, g as getCertificateThumbprint } from './utils-Bx6Iov6q.js'; /*! * © Peculiar Ventures https://peculiarventures.com/ - MIT License */ /** * @license * Copyright (c) Peculiar Ventures, LLC. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ class X509Certificate extends AsnData { constructor(raw) { super(certificateRawToBuffer(raw), Certificate); this.thumbprints = {}; this.type = 'X.509 Certificate'; this.tag = PemConverter.CertificateTag; const { tbsCertificate } = this.asn; this.serialNumber = buildExports.Convert.ToHex(tbsCertificate.serialNumber); this.subject = new Name(tbsCertificate.subject).toJSON(); this.issuer = new 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 = dateDiff(this.notBefore, this.notAfter); } parseExtensions() { const { tbsCertificate } = this.asn; if (tbsCertificate.extensions) { this.extensions = tbsCertificate.extensions .map((e) => new Extension(AsnConvert.serialize(e))); } } getPublicKeyInfo(publicKeyInfo) { const { subjectPublicKey, algorithm } = publicKeyInfo; let params; if (algorithm.algorithm === id_ecPublicKey && algorithm.parameters) { params = AsnConvert.parse(algorithm.parameters, ECParameters); } if (algorithm.algorithm === id_rsaEncryption) { params = AsnConvert.parse(subjectPublicKey, RSAPublicKey); } if (algorithm.algorithm === id_composite_key) { params = AsnConvert.parse(subjectPublicKey, CompositePublicKey); params = params.map((param) => this.getPublicKeyInfo(param)); } const spki = 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 === id_alg_composite) { const compositeSignatureValues = AsnConvert.parse(signatureValue, CompositeSignatureValue); const compositeParams = AsnConvert.parse(signatureAlgorithm.parameters, 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 getCertificateThumbprint(algorithm, this.raw); if (thumbprint) { this.thumbprints[algorithm] = buildExports.Convert.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' || name.shortName === 'E' || name.shortName === 'O') { 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 PemConverter.encode(this.raw, this.tag); case 'base64url': return buildExports.Convert.ToBase64Url(this.raw); default: return buildExports.Convert.ToBase64(this.raw); } } downloadAsPEM(name) { Download.cert.asPEM(this.toString('pem'), name || this.commonName); } downloadAsDER(name) { Download.cert.asDER(this.raw, name || this.commonName); } } export { X509Certificate as X }; //# sourceMappingURL=x509_certificate-pTmrnePc.js.map //# sourceMappingURL=x509_certificate-pTmrnePc.js.map