greenlock.js-ARCHIVED/lib/cert-info.js

101 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-08-04 16:06:31 +00:00
'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;
};
2016-08-09 18:43:13 +00:00
certInfo.getBasicInfo = function (pem) {
var c = certInfo.getCertInfo(pem);
var domains = [];
2016-08-09 18:48:50 +00:00
var sub;
2016-08-09 18:43:13 +00:00
c.extensions.forEach(function (ext) {
if (ext.parsedValue && ext.parsedValue.altNames) {
ext.parsedValue.altNames.forEach(function (alt) {
domains.push(alt.Name);
});
}
});
2016-08-09 18:48:50 +00:00
sub = c.subject.types_and_values[0].value.value_block.value || null;
2016-08-09 18:43:13 +00:00
return {
2016-08-09 19:19:29 +00:00
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()
2016-08-09 18:43:13 +00:00
};
};
2016-08-04 16:06:31 +00:00
certInfo.getCertInfoFromFile = function (pemFile) {
return require('fs').readFileSync(pemFile, 'ascii');
};
2016-08-09 19:10:53 +00:00
certInfo.testGetCertInfo = function (pathname) {
2016-08-04 16:06:31 +00:00
var path = require('path');
2016-08-09 19:10:53 +00:00
var pemFile = pathname || path.join(__dirname, '..', 'tests', 'example.cert.pem');
2016-08-04 16:06:31 +00:00
return certInfo.getCertInfo(certInfo.getCertInfoFromFile(pemFile));
};
2016-08-09 19:10:53 +00:00
certInfo.testBasicCertInfo = function (pathname) {
2016-08-09 18:43:13 +00:00
var path = require('path');
2016-08-09 19:10:53 +00:00
var pemFile = pathname || path.join(__dirname, '..', 'tests', 'example.cert.pem');
2016-08-09 18:43:13 +00:00
return certInfo.getBasicInfo(certInfo.getCertInfoFromFile(pemFile));
};
2016-08-04 16:06:31 +00:00
if (require.main === module) {
2016-08-09 19:10:53 +00:00
var c = certInfo.testGetCertInfo(process.argv[2]);
2016-08-04 16:06:31 +00:00
2016-08-08 23:43:31 +00:00
console.info('');
2016-08-04 16:06:31 +00:00
2016-08-08 23:43:31 +00:00
console.info(c.notBefore.value);
2016-08-09 00:01:22 +00:00
console.info(new Date(c.notBefore.value).valueOf());
2016-08-04 16:06:31 +00:00
2016-08-08 23:43:31 +00:00
console.info('');
2016-08-04 16:06:31 +00:00
2016-08-08 23:43:31 +00:00
console.info(c.notAfter.value);
2016-08-09 00:01:22 +00:00
console.info(new Date(c.notAfter.value).valueOf());
2016-08-04 16:06:31 +00:00
2016-08-08 23:43:31 +00:00
console.info('');
2016-08-09 19:10:53 +00:00
var b = certInfo.testBasicCertInfo(process.argv[2]);
console.info('');
console.info(JSON.stringify(b, null, ' '));
console.info('');
2016-08-04 16:06:31 +00:00
}