From b217b33fff34c56d48b6a3f233a0525aad2a4b09 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 11 Aug 2016 10:04:30 -0600 Subject: [PATCH] move cert-info.js to own module certpem --- lib/cert-info.js | 100 ----------------------------------------------- lib/utils.js | 2 +- package.json | 1 + 3 files changed, 2 insertions(+), 101 deletions(-) delete mode 100644 lib/cert-info.js diff --git a/lib/cert-info.js b/lib/cert-info.js deleted file mode 100644 index fb90195..0000000 --- a/lib/cert-info.js +++ /dev/null @@ -1,100 +0,0 @@ -'use strict'; - -var certInfo = module.exports; - -// this is really memory expensive to do -// (about half of a megabyte of loaded code) -certInfo._pemToBinAb = function (pem) { - var b64 = pem.replace(/(-----(BEGIN|END) CERTIFICATE-----|[\n\r])/g, ''); - var buf = Buffer(b64, 'base64'); - var ab = new Uint8Array(buf).buffer; // WORKS - //var ab = buf.buffer // Doesn't work - - return ab; -}; -certInfo.getCertInfo = function (pem) { - var ab = module.exports._pemToBinAb(pem); - var merge = require("node.extend"); - - var common = require("asn1js/org/pkijs/common"); - var _asn1js = require("asn1js"); - var _pkijs = require("pkijs"); - var _x509schema = require("pkijs/org/pkijs/x509_schema"); - - // #region Merging function/object declarations for ASN1js and PKIjs - var asn1js = merge(true, _asn1js, common); - - var x509schema = merge(true, _x509schema, asn1js); - - var pkijs_1 = merge(true, _pkijs, asn1js); - var pkijs = merge(true, pkijs_1, x509schema); - - var asn1 = pkijs.org.pkijs.fromBER(ab); - var certSimpl = new pkijs.org.pkijs.simpl.CERT({ schema: asn1.result }); - - return certSimpl; -}; - -certInfo.getBasicInfo = function (pem) { - var c = certInfo.getCertInfo(pem); - var domains = []; - var sub; - - c.extensions.forEach(function (ext) { - if (ext.parsedValue && ext.parsedValue.altNames) { - ext.parsedValue.altNames.forEach(function (alt) { - domains.push(alt.Name); - }); - } - }); - - sub = c.subject.types_and_values[0].value.value_block.value || null; - - return { - subject: sub - , altnames: domains - // for debugging during console.log - // do not expect these values to be here - , _issuedAt: c.notBefore.value - , _expiresAt: c.notAfter.value - , issuedAt: new Date(c.notBefore.value).valueOf() - , expiresAt: new Date(c.notAfter.value).valueOf() - }; -}; - -certInfo.getCertInfoFromFile = function (pemFile) { - return require('fs').readFileSync(pemFile, 'ascii'); -}; - -certInfo.testGetCertInfo = function (pathname) { - var path = require('path'); - var pemFile = pathname || path.join(__dirname, '..', 'tests', 'example.cert.pem'); - return certInfo.getCertInfo(certInfo.getCertInfoFromFile(pemFile)); -}; - -certInfo.testBasicCertInfo = function (pathname) { - var path = require('path'); - var pemFile = pathname || path.join(__dirname, '..', 'tests', 'example.cert.pem'); - return certInfo.getBasicInfo(certInfo.getCertInfoFromFile(pemFile)); -}; - -if (require.main === module) { - var c = certInfo.testGetCertInfo(process.argv[2]); - - console.info(''); - - console.info(c.notBefore.value); - console.info(new Date(c.notBefore.value).valueOf()); - - console.info(''); - - console.info(c.notAfter.value); - console.info(new Date(c.notAfter.value).valueOf()); - - console.info(''); - - var b = certInfo.testBasicCertInfo(process.argv[2]); - console.info(''); - console.info(JSON.stringify(b, null, ' ')); - console.info(''); -} diff --git a/lib/utils.js b/lib/utils.js index bf183ee..78b8d49 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,8 +8,8 @@ var PromiseA = require('bluebird'); var dns = PromiseA.promisifyAll(require('dns')); module.exports.attachCertInfo = function (results) { - var getCertInfo = require('./cert-info').getBasicInfo; // XXX Note: Parsing the certificate info comes at a great cost (~500kb) + var getCertInfo = require('certpem').info; var certInfo = getCertInfo(results.cert); // subject, altnames, issuedAt, expiresAt diff --git a/package.json b/package.json index 06a66db..e83b3ee 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "dependencies": { "asn1js": "^1.2.12", "bluebird": "^3.0.6", + "certpem": "^1.0.0", "homedir": "^0.6.0", "le-acme-core": "^2.0.5", "le-challenge-fs": "^2.0.2",