diff --git a/README.md b/README.md index 2fc511fe..23a45c79 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ Where Use `getModulus` to get the modulus for a certificate, a CSR or a private key. Modulus can be useful to check that a Private Key Matches a Certificate - pem.getModulus(certificate, callback) + pem.getModulus(certificate, [password,] callback) Where diff --git a/lib/pem.js b/lib/pem.js index 43143807..8a02aade 100644 --- a/lib/pem.js +++ b/lib/pem.js @@ -18,7 +18,6 @@ module.exports.readCertificateInfo = readCertificateInfo; module.exports.getPublicKey = getPublicKey; module.exports.getFingerprint = getFingerprint; module.exports.getModulus = getModulus; -module.exports.getModulusFromProtected = getModulusFromProtected; module.exports.getDhparamInfo = getDhparamInfo; module.exports.createPkcs12 = createPkcs12; module.exports.readPkcs12 = readPkcs12; @@ -436,9 +435,15 @@ function readCertificateInfo(certificate, callback) { * get the modulus from a certificate, a CSR or a private key * * @param {String} certificate PEM encoded, CSR PEM encoded, or private key + * @param {String} password password for the certificate * @param {Function} callback Callback function with an error object and {modulus} */ -function getModulus(certificate, callback) { +function getModulus(certificate, password, callback) { + if (!callback && typeof password === 'function') { + callback = password; + password = undefined; + } + certificate = Buffer.isBuffer(certificate) && certificate.toString() || certificate; var type = ''; @@ -455,45 +460,13 @@ function getModulus(certificate, callback) { '-in', '--TMPFILE--' ]; - spawnWrapper(params, certificate, function(err, code, stdout) { - if (err) { - return callback(err); - } - var match = stdout.match(/Modulus=([0-9a-fA-F]+)$/m); - if (match) { - return callback(null, { - modulus: match[1] - }); - } else { - return callback(new Error('No modulus')); - } - }); -} - -function getModulusFromProtected(key, password, callback){ - key = Buffer.isBuffer(key) && key.toString() || key; - - var type = ''; - if (key.match(/BEGIN(\sNEW)? CERTIFICATE REQUEST/)) { - type = 'req'; - } else if (key.match(/BEGIN RSA PRIVATE KEY/) || key.match(/BEGIN PRIVATE KEY/)) { - type = 'rsa'; - } else { - type = 'x509'; - } - var params = [type, - '-noout', - '-modulus', - '-in', - '--TMPFILE--' - ]; if (password) { params.push( '-passin'); params.push( 'pass:' + password); } - spawnWrapper(params, key, function(err, code, stdout) { + spawnWrapper(params, certificate, function(err, code, stdout) { if (err) { return callback(err); } diff --git a/test/pem.js b/test/pem.js index e5f4c97c..c5bf8690 100644 --- a/test/pem.js +++ b/test/pem.js @@ -453,7 +453,7 @@ exports['General Tests'] = { test.ok(certmodulus); test.ok(certmodulus.match(/^[0-9A-F]*$/)); test.ok(fs.readdirSync('./tmp').length === 0); - pem.getModulusFromProtected(key, 'password' ,function(error, data) { + pem.getModulus(key, 'password' ,function(error, data) { var keymodulus = (data && data.modulus || '').toString(); test.ifError(error); test.ok(keymodulus);