diff --git a/README.md b/README.md index d45632c2..6185ee75 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ Use `readCertificateInfo` for reading subject data from a certificate or a CSR Where * **certificate** is a PEM encoded CSR or a certificate - * **callback** is a callback function with an error object and `{country, state, locality, organization, organizationUnit, commonName, emailAddress}` + * **callback** is a callback function with an error object and `{country, state, locality, organization, organizationUnit, commonName, emailAddress, validity{start, end} }` ### Get fingerprint diff --git a/lib/pem.js b/lib/pem.js index 2e62b3d4..774588bb 100644 --- a/lib/pem.js +++ b/lib/pem.js @@ -329,6 +329,7 @@ function fetchCertificateData(certData, callback){ certData = (certData || "").toString(); var subject, extra, tmp, certValues = {}; + var validity = {}; if((subject = certData.match(/Subject:([^\n]*)\n/)) && subject.length>1){ subject = subject[1]; @@ -358,6 +359,13 @@ function fetchCertificateData(certData, callback){ tmp = extra.match(/emailAddress=([^,\n\/].*?)[,\n\/]/); certValues.emailAddress = tmp && tmp[1] || ""; } + if ((tmp = certData.match(/Not Before\s?:\s?([^\n]*)\n/)) && tmp.length>1) + validity.start = Date.parse(tmp && tmp[1] || ""); + if ((tmp = certData.match(/Not After\s?:\s?([^\n]*)\n/)) && tmp.length>1) + validity.end = Date.parse(tmp && tmp[1] || ""); + if (validity.start && validity.end) + certValues.validity = validity; + callback(null, certValues); } diff --git a/test/pem.js b/test/pem.js index f1dc7e04..80a3689f 100644 --- a/test/pem.js +++ b/test/pem.js @@ -145,6 +145,8 @@ exports["General Tests"] = { pem.readCertificateInfo(certificate, function(error, data){ test.ifError(error); + if(data.validity) + delete data.validity; test.deepEqual(data,{ country: '', state: '', @@ -172,6 +174,8 @@ exports["General Tests"] = { pem.readCertificateInfo(certificate, function(error, data){ test.ifError(error); + if(data.validity) + delete data.validity; test.deepEqual(data, certInfo); test.done(); });