diff options
Diffstat (limited to 'src/helpers')
-rw-r--r-- | src/helpers/getCertificateInfo.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/helpers/getCertificateInfo.ts b/src/helpers/getCertificateInfo.ts new file mode 100644 index 0000000..b7a0ed0 --- /dev/null +++ b/src/helpers/getCertificateInfo.ts @@ -0,0 +1,31 @@ +import jsrsasign from 'jsrsasign'; + +import { CertificateInfo } from '@types'; + +/** + * Extract PEM certificate info + * + * @param pemCertificate Result from call to `convertASN1toPEM(x5c[0])` + */ +export default function getCertificateInfo(pemCertificate: string): CertificateInfo { + const subjectCert = new jsrsasign.X509(); + subjectCert.readCertPEM(pemCertificate); + + const subjectString = subjectCert.getSubjectString(); + const subjectParts = subjectString.slice(1).split('/'); + + const subject: { [key: string]: string } = {}; + subjectParts.forEach((field) => { + const [key, val] = field.split('='); + subject[key] = val; + }); + + const { getVersion } = subjectCert; + const basicConstraintsCA = !!subjectCert.getExtBasicConstraints().cA; + + return { + subject, + version: getVersion(), + basicConstraintsCA, + }; +} |