Compare commits
	
		
			2 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| a7722dea19 | |||
| 3668d337d7 | 
							
								
								
									
										40
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								README.md
									
									
									
									
									
								
							@ -1,17 +1,32 @@
 | 
			
		||||
# [certpem.js](https://git.coolaj86.com/coolaj86/cert-info.js)
 | 
			
		||||
# [cert-info.js](https://git.coolaj86.com/coolaj86/cert-info.js)
 | 
			
		||||
 | 
			
		||||
Read basic info from a cert.pem / x509 certificate.
 | 
			
		||||
 | 
			
		||||
Used for [Greenlock.js](https://git.coolaj86.com/coolaj86/greenlock-express.js)
 | 
			
		||||
 | 
			
		||||
# Features
 | 
			
		||||
 | 
			
		||||
| <175 lines of code | 1.7k gzipped | 4.4k minified | 8.8k with comments |
 | 
			
		||||
 | 
			
		||||
* [x] Parses x.509 certificate schemas
 | 
			
		||||
  * [x] DER/ASN.1
 | 
			
		||||
  * [x] PEM (base64-encoded DER)
 | 
			
		||||
  * [x] Subject
 | 
			
		||||
  * [x] SAN extension (altNames)
 | 
			
		||||
  * [x] Issuance Date (notBefore)
 | 
			
		||||
  * [x] Expiry Date (notAfter)
 | 
			
		||||
* [x] VanillaJS, **Zero Dependencies**
 | 
			
		||||
  * [x] Node.js
 | 
			
		||||
  * [ ] Browsers (built, publishing soon)
 | 
			
		||||
 | 
			
		||||
# Install
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
# bin
 | 
			
		||||
npm install --global certpem
 | 
			
		||||
npm install --global cert-info
 | 
			
		||||
 | 
			
		||||
# node.js library
 | 
			
		||||
npm install --save certpem
 | 
			
		||||
npm install --save cert-info
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
# Usage
 | 
			
		||||
@ -21,26 +36,22 @@ npm install --save certpem
 | 
			
		||||
For basic info (subject, altnames, issuedAt, expiresAt):
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
certpem /path/to/cert.pem
 | 
			
		||||
cert-info /path/to/cert.pem
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Output all info by passing `--debug` or use `--json` to see the basic info pretty-printed.
 | 
			
		||||
 | 
			
		||||
## node.js
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
'use strict';
 | 
			
		||||
 | 
			
		||||
var certpem = require('certpem').certpem
 | 
			
		||||
var certinfo = require('cert-info');
 | 
			
		||||
var cert = fs.readFile('cert.pem', 'ascii', function (err, certstr) {
 | 
			
		||||
 | 
			
		||||
  // basic info
 | 
			
		||||
  console.info(certpem.info(certstr));
 | 
			
		||||
 | 
			
		||||
  // way too much info
 | 
			
		||||
  // (requires npm install --save node.extend@1)
 | 
			
		||||
  console.info(certpem.debug(certstr));
 | 
			
		||||
  console.info(certinfo.info(certstr));
 | 
			
		||||
 | 
			
		||||
  // if you need to submit a bug report
 | 
			
		||||
  console.info(certinfo.debug(certstr));
 | 
			
		||||
});
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
@ -57,11 +68,12 @@ Example output:
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
With a few small changes this could also work in the browser (that's how its dependencies are designed).
 | 
			
		||||
With a few small changes this could also work in the browser
 | 
			
		||||
(it has no dependencies and all of the non-browser things are on the Enc object).
 | 
			
		||||
 | 
			
		||||
# Legal
 | 
			
		||||
 | 
			
		||||
[certpem.js](https://git.coolaj86.com/coolaj86/certpem.js) |
 | 
			
		||||
[cert-info.js](https://git.coolaj86.com/coolaj86/cert-info.js) |
 | 
			
		||||
MPL-2.0 |
 | 
			
		||||
[Terms of Use](https://therootcompany.com/legal/#terms) |
 | 
			
		||||
[Privacy Policy](https://therootcompany.com/legal/#privacy)
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | 
			
		||||
'use strict';
 | 
			
		||||
 | 
			
		||||
var certpem = require('../').certpem;
 | 
			
		||||
var certinfo = require('../');
 | 
			
		||||
var path = require('path');
 | 
			
		||||
var filepath = (process.cwd() + path.sep + 'cert.pem');
 | 
			
		||||
var debug = false;
 | 
			
		||||
@ -33,9 +33,9 @@ else {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if (debug) {
 | 
			
		||||
  console.info(JSON.stringify(certpem.debug(cert), null, '  '));
 | 
			
		||||
  console.info(JSON.stringify(certinfo.debug(cert), null, '  '));
 | 
			
		||||
} else {
 | 
			
		||||
  var c = certpem.info(cert);
 | 
			
		||||
  var c = certinfo.info(cert);
 | 
			
		||||
 | 
			
		||||
  if (json) {
 | 
			
		||||
    console.info(JSON.stringify(c, null, '  '));
 | 
			
		||||
							
								
								
									
										119
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										119
									
								
								index.js
									
									
									
									
									
								
							@ -4,121 +4,4 @@
 | 
			
		||||
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | 
			
		||||
'use strict';
 | 
			
		||||
 | 
			
		||||
var certInfo = module.exports;
 | 
			
		||||
module.exports.certpem = certInfo;
 | 
			
		||||
 | 
			
		||||
// ES5 version of mergeDeep
 | 
			
		||||
// https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge
 | 
			
		||||
/**
 | 
			
		||||
 * Simple object check.
 | 
			
		||||
 * @param item
 | 
			
		||||
 * @returns {boolean}
 | 
			
		||||
 */
 | 
			
		||||
function isObject(item) {
 | 
			
		||||
  return (item && typeof item === 'object' && !Array.isArray(item));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Deep merge two objects.
 | 
			
		||||
 * @param target
 | 
			
		||||
 * @param ...sources
 | 
			
		||||
 */
 | 
			
		||||
function merge(target) {
 | 
			
		||||
  var sources = Array.prototype.slice.call(arguments);
 | 
			
		||||
  sources.shift();
 | 
			
		||||
  if (!sources.length) { return target; }
 | 
			
		||||
  var source = sources.shift();
 | 
			
		||||
  var obj;
 | 
			
		||||
 | 
			
		||||
  if (isObject(target) && isObject(source)) {
 | 
			
		||||
    Object.keys(source).forEach(function (key) {
 | 
			
		||||
      if (isObject(source[key])) {
 | 
			
		||||
        if (!target[key]) { obj = {}; obj[key] = {}; Object.assign(target, obj); }
 | 
			
		||||
        merge(target[key], source[key]);
 | 
			
		||||
      } else {
 | 
			
		||||
        obj = {}; obj[key] = source[key];
 | 
			
		||||
        Object.assign(target, obj);
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  sources.unshift(target);
 | 
			
		||||
  return merge.apply(null, sources);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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(_asn1js, common);
 | 
			
		||||
 | 
			
		||||
var x509schema = merge(_x509schema, asn1js);
 | 
			
		||||
 | 
			
		||||
var pkijs_1 = merge(_pkijs, asn1js);
 | 
			
		||||
var pkijs = merge(pkijs_1, x509schema);
 | 
			
		||||
 | 
			
		||||
// 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.from(b64, 'base64');
 | 
			
		||||
  var ab = new Uint8Array(buf).buffer;          // WORKS
 | 
			
		||||
  //var ab = buf.buffer                         // Doesn't work
 | 
			
		||||
 | 
			
		||||
  return ab;
 | 
			
		||||
};
 | 
			
		||||
certInfo.debug = certInfo.getCertInfo = function (pem) {
 | 
			
		||||
  var ab = module.exports._pemToBinAb(pem);
 | 
			
		||||
 | 
			
		||||
  var asn1 = pkijs.org.pkijs.fromBER(ab);
 | 
			
		||||
  var certSimpl = new pkijs.org.pkijs.simpl.CERT({ schema: asn1.result });
 | 
			
		||||
 | 
			
		||||
  return certSimpl;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
certInfo.info = 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));
 | 
			
		||||
};
 | 
			
		||||
*/
 | 
			
		||||
module.exports = require('./lib/cert-info.js');
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										150
									
								
								lib/asn1-parser.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										150
									
								
								lib/asn1-parser.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,150 @@
 | 
			
		||||
// Copyright 2016-2018 AJ ONeal. All rights reserved
 | 
			
		||||
// https://git.coolaj86.com/coolaj86/asn1-parser.js
 | 
			
		||||
/* This Source Code Form is subject to the terms of the Mozilla Public
 | 
			
		||||
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 | 
			
		||||
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | 
			
		||||
;(function (exports) {
 | 
			
		||||
'use strict';
 | 
			
		||||
 | 
			
		||||
if (!exports.ASN1) { exports.ASN1 = {}; }
 | 
			
		||||
if (!exports.Enc) { exports.Enc = {}; }
 | 
			
		||||
if (!exports.PEM) { exports.PEM = {}; }
 | 
			
		||||
 | 
			
		||||
var ASN1 = exports.ASN1;
 | 
			
		||||
var Enc = exports.Enc;
 | 
			
		||||
var PEM = exports.PEM;
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// Parser
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
// Although I've only seen 9 max in https certificates themselves,
 | 
			
		||||
// but each domain list could have up to 100
 | 
			
		||||
ASN1.ELOOPN = 102;
 | 
			
		||||
ASN1.ELOOP = "uASN1.js Error: iterated over " + ASN1.ELOOPN + "+ elements (probably a malformed file)";
 | 
			
		||||
// I've seen https certificates go 29 deep
 | 
			
		||||
ASN1.EDEEPN = 60;
 | 
			
		||||
ASN1.EDEEP = "uASN1.js Error: element nested " + ASN1.EDEEPN + "+ layers deep (probably a malformed file)";
 | 
			
		||||
// Container Types are Sequence 0x30, Container Array? (0xA0, 0xA1)
 | 
			
		||||
// Value Types are Boolean 0x01, Integer 0x02, Null 0x05, Object ID 0x06, String 0x0C, 0x16, 0x13, 0x1e Value Array? (0x82)
 | 
			
		||||
// Bit String (0x03) and Octet String (0x04) may be values or containers
 | 
			
		||||
// Sometimes Bit String is used as a container (RSA Pub Spki)
 | 
			
		||||
ASN1.CTYPES = [ 0x30, 0x31, 0xa0, 0xa1 ];
 | 
			
		||||
ASN1.VTYPES = [ 0x01, 0x02, 0x05, 0x06, 0x0c, 0x82 ];
 | 
			
		||||
ASN1.parse = function parseAsn1Helper(buf) {
 | 
			
		||||
  //var ws = '  ';
 | 
			
		||||
  function parseAsn1(buf, depth, eager) {
 | 
			
		||||
    if (depth.length >= ASN1.EDEEPN) { throw new Error(ASN1.EDEEP); }
 | 
			
		||||
 | 
			
		||||
    var index = 2; // we know, at minimum, data starts after type (0) and lengthSize (1)
 | 
			
		||||
    var asn1 = { type: buf[0], lengthSize: 0, length: buf[1] };
 | 
			
		||||
    var child;
 | 
			
		||||
    var iters = 0;
 | 
			
		||||
    var adjust = 0;
 | 
			
		||||
    var adjustedLen;
 | 
			
		||||
 | 
			
		||||
    // Determine how many bytes the length uses, and what it is
 | 
			
		||||
    if (0x80 & asn1.length) {
 | 
			
		||||
      asn1.lengthSize = 0x7f & asn1.length;
 | 
			
		||||
      // I think that buf->hex->int solves the problem of Endianness... not sure
 | 
			
		||||
      asn1.length = parseInt(Enc.bufToHex(buf.slice(index, index + asn1.lengthSize)), 16);
 | 
			
		||||
      index += asn1.lengthSize;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // High-order bit Integers have a leading 0x00 to signify that they are positive.
 | 
			
		||||
    // Bit Streams use the first byte to signify padding, which x.509 doesn't use.
 | 
			
		||||
    if (0x00 === buf[index] && (0x02 === asn1.type || 0x03 === asn1.type)) {
 | 
			
		||||
      // However, 0x00 on its own is a valid number
 | 
			
		||||
      if (asn1.length > 1) {
 | 
			
		||||
        index += 1;
 | 
			
		||||
        adjust = -1;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    adjustedLen = asn1.length + adjust;
 | 
			
		||||
 | 
			
		||||
    //console.warn(depth.join(ws) + '0x' + Enc.numToHex(asn1.type), index, 'len:', asn1.length, asn1);
 | 
			
		||||
 | 
			
		||||
    function parseChildren(eager) {
 | 
			
		||||
      asn1.children = [];
 | 
			
		||||
      //console.warn('1 len:', (2 + asn1.lengthSize + asn1.length), 'idx:', index, 'clen:', 0);
 | 
			
		||||
      while (iters < ASN1.ELOOPN && index < (2 + asn1.length + asn1.lengthSize)) {
 | 
			
		||||
        iters += 1;
 | 
			
		||||
        depth.length += 1;
 | 
			
		||||
        child = parseAsn1(buf.slice(index, index + adjustedLen), depth, eager);
 | 
			
		||||
        depth.length -= 1;
 | 
			
		||||
        // The numbers don't match up exactly and I don't remember why...
 | 
			
		||||
        // probably something with adjustedLen or some such, but the tests pass
 | 
			
		||||
        index += (2 + child.lengthSize + child.length);
 | 
			
		||||
        //console.warn('2 len:', (2 + asn1.lengthSize + asn1.length), 'idx:', index, 'clen:', (2 + child.lengthSize + child.length));
 | 
			
		||||
        if (index > (2 + asn1.lengthSize + asn1.length)) {
 | 
			
		||||
          if (!eager) { console.error(JSON.stringify(asn1, ASN1._replacer, 2)); }
 | 
			
		||||
          throw new Error("Parse error: child value length (" + child.length
 | 
			
		||||
            + ") is greater than remaining parent length (" + (asn1.length - index)
 | 
			
		||||
            + " = " + asn1.length + " - " + index + ")");
 | 
			
		||||
        }
 | 
			
		||||
        asn1.children.push(child);
 | 
			
		||||
        //console.warn(depth.join(ws) + '0x' + Enc.numToHex(asn1.type), index, 'len:', asn1.length, asn1);
 | 
			
		||||
      }
 | 
			
		||||
      if (index !== (2 + asn1.lengthSize + asn1.length)) {
 | 
			
		||||
        //console.warn('index:', index, 'length:', (2 + asn1.lengthSize + asn1.length));
 | 
			
		||||
        throw new Error("premature end-of-file");
 | 
			
		||||
      }
 | 
			
		||||
      if (iters >= ASN1.ELOOPN) { throw new Error(ASN1.ELOOP); }
 | 
			
		||||
 | 
			
		||||
      delete asn1.value;
 | 
			
		||||
      return asn1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Recurse into types that are _always_ containers
 | 
			
		||||
    if (-1 !== ASN1.CTYPES.indexOf(asn1.type)) { return parseChildren(eager); }
 | 
			
		||||
 | 
			
		||||
    // Return types that are _always_ values
 | 
			
		||||
    asn1.value = buf.slice(index, index + adjustedLen);
 | 
			
		||||
    if (-1 !== ASN1.VTYPES.indexOf(asn1.type)) { return asn1; }
 | 
			
		||||
 | 
			
		||||
    // For ambigious / unknown types, recurse and return on failure
 | 
			
		||||
    // (and return child array size to zero)
 | 
			
		||||
    try { return parseChildren(true); }
 | 
			
		||||
    catch(e) { asn1.children.length = 0; return asn1; }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  var asn1 = parseAsn1(buf, []);
 | 
			
		||||
  var len = buf.byteLength || buf.length;
 | 
			
		||||
  if (len !== 2 + asn1.lengthSize + asn1.length) {
 | 
			
		||||
    throw new Error("Length of buffer does not match length of ASN.1 sequence.");
 | 
			
		||||
  }
 | 
			
		||||
  return asn1;
 | 
			
		||||
};
 | 
			
		||||
ASN1._replacer = function (k, v) {
 | 
			
		||||
  if ('type' === k) { return '0x' + Enc.numToHex(v); }
 | 
			
		||||
  if (v && 'value' === k) { return '0x' + Enc.bufToHex(v.data || v); }
 | 
			
		||||
  return v;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// don't replace the full parseBlock, if it exists
 | 
			
		||||
PEM.parseBlock = PEM.parseBlock || function (str) {
 | 
			
		||||
  var b64 = str.split(/\n/).filter(function (line) {
 | 
			
		||||
    return !/-----/.test(line);
 | 
			
		||||
  }).join('');
 | 
			
		||||
  var der = Enc.base64ToBuf(b64);
 | 
			
		||||
  return { bytes: der };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Enc.base64ToBuf = function (b64) {
 | 
			
		||||
  return Buffer.from(b64, 'base64');
 | 
			
		||||
};
 | 
			
		||||
Enc.binToBuf = function (bin) {
 | 
			
		||||
  return Buffer.from(bin, 'binary');
 | 
			
		||||
};
 | 
			
		||||
Enc.bufToHex = function (u8) {
 | 
			
		||||
  return Buffer.from(u8).toString('hex');
 | 
			
		||||
};
 | 
			
		||||
Enc.numToHex = function (d) {
 | 
			
		||||
  d = d.toString(16);
 | 
			
		||||
  if (d.length % 2) {
 | 
			
		||||
    return '0' + d;
 | 
			
		||||
  }
 | 
			
		||||
  return d;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}('undefined' !== typeof window ? window : module.exports));
 | 
			
		||||
							
								
								
									
										106
									
								
								lib/cert-info.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								lib/cert-info.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,106 @@
 | 
			
		||||
// Copyright 2016-2018 AJ ONeal. All rights reserved
 | 
			
		||||
/* This Source Code Form is subject to the terms of the Mozilla Public
 | 
			
		||||
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 | 
			
		||||
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | 
			
		||||
'use strict';
 | 
			
		||||
 | 
			
		||||
var certInfo = module.exports;
 | 
			
		||||
 | 
			
		||||
var ASN1 = require("./asn1-parser.js").ASN1;
 | 
			
		||||
var PEM = require("./asn1-parser.js").PEM;
 | 
			
		||||
var Enc = require("./asn1-parser.js").Enc;
 | 
			
		||||
 | 
			
		||||
Enc.hexToBuf = function (hex) {
 | 
			
		||||
  return Buffer.from(hex, 'hex');
 | 
			
		||||
};
 | 
			
		||||
Enc.bufToUtf8 = function (u8) {
 | 
			
		||||
  return Buffer.from(u8).toString('utf8');
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
certInfo.debug = certInfo.getCertInfo = function (pem) {
 | 
			
		||||
  var bytes = PEM.parseBlock(pem).bytes;
 | 
			
		||||
  return ASN1.parse(bytes);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
certInfo.info = certInfo.getBasicInfo = function (pem) {
 | 
			
		||||
  var c = certInfo.getCertInfo(pem);
 | 
			
		||||
  // A cert has 3 parts: cert, signature meta, signature
 | 
			
		||||
  if (c.children.length !== 3) {
 | 
			
		||||
    throw new Error("doesn't look like a certificate: expected 3 parts of header");
 | 
			
		||||
  }
 | 
			
		||||
  c = c.children[0];
 | 
			
		||||
  if (8 !== c.children.length) {
 | 
			
		||||
    throw new Error("doesn't look like a certificate: expected 8 parts to certificate");
 | 
			
		||||
  }
 | 
			
		||||
  // 0:0 value 2
 | 
			
		||||
  // 1 variable-length value
 | 
			
		||||
  // 2:0 sha256 identifier 2:1 null
 | 
			
		||||
  // 3 certificate of issuer C/O/OU/CN
 | 
			
		||||
  // 4:0 notBefore 4:1 notAfter
 | 
			
		||||
  var nbf = Enc.hexToBuf(c.children[4].children[0].value);
 | 
			
		||||
  var exp = Enc.hexToBuf(c.children[4].children[1].value);
 | 
			
		||||
  nbf = new Date(Date.UTC(
 | 
			
		||||
    '20' + nbf.slice(0, 2)
 | 
			
		||||
  , nbf.slice(2, 4) - 1
 | 
			
		||||
  , nbf.slice(4, 6)
 | 
			
		||||
  , nbf.slice(6, 8)
 | 
			
		||||
  , nbf.slice(8, 10)
 | 
			
		||||
  , nbf.slice(10, 12)
 | 
			
		||||
  ));
 | 
			
		||||
  exp = new Date(Date.UTC(
 | 
			
		||||
    '20' + exp.slice(0, 2)
 | 
			
		||||
  , exp.slice(2, 4) - 1
 | 
			
		||||
  , exp.slice(4, 6)
 | 
			
		||||
  , exp.slice(6, 8)
 | 
			
		||||
  , exp.slice(8, 10)
 | 
			
		||||
  , exp.slice(10, 12)
 | 
			
		||||
  ));
 | 
			
		||||
  // 5 the client C/O/OU/CN, etc
 | 
			
		||||
  var sub = c.children[5].children.filter(function (set) {
 | 
			
		||||
    if ('550403' === Enc.bufToHex(set.children[0].children[0].value)) {
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
  }).map(function (set) {
 | 
			
		||||
    return Enc.bufToUtf8(set.children[0].children[1].value);
 | 
			
		||||
  })[0];
 | 
			
		||||
  // 6 public key
 | 
			
		||||
  // 7 extensions
 | 
			
		||||
  var domains = c.children[7].children[0].children.filter(function (seq) {
 | 
			
		||||
    if ('551d11' === Enc.bufToHex(seq.children[0].value)) {
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
  }).map(function (seq) {
 | 
			
		||||
    return seq.children[1].children[0].children.map(function (name) {
 | 
			
		||||
      return Enc.bufToUtf8(name.value);
 | 
			
		||||
    });
 | 
			
		||||
  })[0];
 | 
			
		||||
 | 
			
		||||
  return {
 | 
			
		||||
    subject: sub
 | 
			
		||||
  , altnames: domains
 | 
			
		||||
    // for debugging during console.log
 | 
			
		||||
    // do not expect these values to be here
 | 
			
		||||
  , _issuedAt: nbf
 | 
			
		||||
  , _expiresAt: exp
 | 
			
		||||
  , issuedAt: nbf.valueOf()
 | 
			
		||||
  , expiresAt: exp.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));
 | 
			
		||||
};
 | 
			
		||||
*/
 | 
			
		||||
							
								
								
									
										31
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										31
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							@ -1,31 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "certpem",
 | 
			
		||||
  "version": "1.1.1",
 | 
			
		||||
  "lockfileVersion": 1,
 | 
			
		||||
  "requires": true,
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "asn1js": {
 | 
			
		||||
      "version": "1.2.12",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/asn1js/-/asn1js-1.2.12.tgz",
 | 
			
		||||
      "integrity": "sha1-h9XueXWWri0qPLAkciDcQv/D8hE="
 | 
			
		||||
    },
 | 
			
		||||
    "is": {
 | 
			
		||||
      "version": "3.2.1",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/is/-/is-3.2.1.tgz",
 | 
			
		||||
      "integrity": "sha1-0Kwq1V63sL7JJqUmb2xmKqqD3KU="
 | 
			
		||||
    },
 | 
			
		||||
    "node.extend": {
 | 
			
		||||
      "version": "1.1.6",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/node.extend/-/node.extend-1.1.6.tgz",
 | 
			
		||||
      "integrity": "sha1-p7iCyC1sk6SGOlUEvV3o7IYli5Y=",
 | 
			
		||||
      "requires": {
 | 
			
		||||
        "is": "^3.1.0"
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "pkijs": {
 | 
			
		||||
      "version": "1.3.33",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/pkijs/-/pkijs-1.3.33.tgz",
 | 
			
		||||
      "integrity": "sha1-ponvYhE7fDSOH/wJll0iOeW7TJI="
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								package.json
									
									
									
									
									
								
							@ -1,10 +1,13 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "certpem",
 | 
			
		||||
  "version": "1.1.3",
 | 
			
		||||
  "name": "cert-info",
 | 
			
		||||
  "version": "1.5.1",
 | 
			
		||||
  "description": "Read basic info (subject, altnames, expiresAt, issuedAt) from a cert.pem / x509 certificate (tls / ssl / https) ",
 | 
			
		||||
  "main": "index.js",
 | 
			
		||||
  "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
 | 
			
		||||
  "license": "MPL-2.0",
 | 
			
		||||
  "homepage": "https://git.coolaj86.com/coolaj86/cert-info.js",
 | 
			
		||||
  "bin": {
 | 
			
		||||
    "certpem": "bin/certpem.js"
 | 
			
		||||
    "cert-info": "bin/cert-info.js"
 | 
			
		||||
  },
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "test": "node test.js"
 | 
			
		||||
@ -29,14 +32,7 @@
 | 
			
		||||
    "issued",
 | 
			
		||||
    "asn1"
 | 
			
		||||
  ],
 | 
			
		||||
  "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
 | 
			
		||||
  "license": "MPL-2.0",
 | 
			
		||||
  "bugs": {
 | 
			
		||||
    "url": "https://git.coolaj86.com/coolaj86/cert-info.js/issues"
 | 
			
		||||
  },
 | 
			
		||||
  "homepage": "https://git.coolaj86.com/coolaj86/cert-info.js",
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "asn1js": "^1.2.12",
 | 
			
		||||
    "pkijs": "^1.3.27"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user