@peculiar/fortify-webcomponents
Version:
Web-components for creating CSR or Certificate and viewing certificates list using Fortify
156 lines (153 loc) • 5.93 kB
JavaScript
/*!
* © Peculiar Ventures https://peculiarventures.com/ - BSD 3-Clause License
*/
import { e as AsnData, c as Certificate, P as PemConverter, C as Convert_1, N as Name, f as dateDiff, A as AsnConvert, i as id_ecPublicKey, g as ECParameters, h as id_rsaEncryption, R as RSAPublicKey, j as id_composite_key, k as CompositePublicKey, l as id_alg_composite, m as CompositeSignatureValue, n as CompositeParams, D as Download } from './certification_request-0b43f1e5.js';
import { c as certificateRawToBuffer, E as Extension, g as getCertificateThumbprint } from './utils-75993c76.js';
/*!
* © Peculiar Ventures https://peculiarventures.com/ - MIT License
*/
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 = Convert_1.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] = 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 PemConverter.encode(this.raw, this.tag);
case 'base64url':
return Convert_1.ToBase64Url(this.raw);
default:
return Convert_1.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-9d51de71.js.map