Skip to content

Instantly share code, notes, and snippets.

@sthawali
Last active January 19, 2016 12:57
Show Gist options
  • Save sthawali/3968b74b2747ae906224 to your computer and use it in GitHub Desktop.
Save sthawali/3968b74b2747ae906224 to your computer and use it in GitHub Desktop.
Check push certificate expiration
var forge = require('node-forge');
var fs = require('fs');
var moment = require('moment');
var filename = 'P12_FILE_PATH';
var password = 'PASSWORD';
var p12b64 = fs.readFileSync(filename, { encoding: 'base64' });
var p12Der = forge.util.decode64(p12b64);
var p12Asn1 = forge.asn1.fromDer(p12Der);
var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
var bags = p12.getBags({ bagType: forge.pki.oids.certBag });
var cert = bags[forge.pki.oids.certBag][0].cert;
if (cert == null) return console.error('Error while parsing Certificate');
var expDate = moment(cert.validity.notAfter);
var now = moment();
var difference = now.diff(expDate, 'seconds');
if (difference > 0) return console.log('!!EXPIRED!!');
return console.log('All good!! valid Certificate');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment