Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Startdate Option to CreateCertificate + helper function #44

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function createCSR(options, callback) {
* @param {String} [options.hash] Hash function to use (either md5 sha1 or sha256, defaults to sha256)
* @param {String} [options.csr] CSR for the certificate, if not defined a new one is generated
* @param {Number} [options.days] Certificate expire time in days
* @param {String} [options.startdate] Certificate start date [valide from] (YYMMDDHHMMSSZ e.g. 150215000000Z)
* @param {Function} callback Callback function with an error object and {certificate, csr, clientKey, serviceKey}
*/
function createCertificate(options, callback) {
Expand Down Expand Up @@ -201,6 +202,8 @@ function createCertificate(options, callback) {
var params = ['x509',
'-req',
'-' + (options.hash || 'sha256'),
'-startdate',
(options.startdate || DateYYMMDDHHMMSSZ()),
'-days',
Number(options.days) || '365',
'-in',
Expand Down Expand Up @@ -689,4 +692,25 @@ function testOpenSSLPath(pathBin, callback) {

callback();
});
}
}

/**
* Helper for YYMMDDHHMMSSZ Dates
*/
function DateYYMMDDHHMMSSZ(){
// GET CURRENT DATE
var date = new Date();

// GET YYYY, MM AND DD FROM THE DATE OBJECT
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth()+1).toString();
var dd = date.getDate().toString();

// CONVERT mm AND dd INTO chars
var mmChars = mm.split('');
var ddChars = dd.split('');

// CONCAT THE STRINGS IN YYYY-MM-DD FORMAT
var datestring = yyyy.slice(-2) + (mmChars[1]?mm:"0"+mmChars[0]) + (ddChars[1]?dd:"0"+ddChars[0]) + '000000Z';
return datestring;
}