Skip to content

Commit 2f8c1d1

Browse files
committed
added signed cert methods
1 parent 7fbfa90 commit 2f8c1d1

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

lib/pem.js

+47-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module.exports.readCertificateInfo = readCertificateInfo;
1818
module.exports.getPublicKey = getPublicKey;
1919
module.exports.getFingerprint = getFingerprint;
2020
module.exports.getModulus = getModulus;
21+
module.exports.getModulusOfSignedCert = getModulusOfSignedCert;
22+
module.exports.getModulusOfCertificate = getModulusOfCertificate;
2123
module.exports.config = config;
2224

2325
// PUBLIC API
@@ -56,9 +58,9 @@ function createPrivateKey(keyBitsize, options, callback) {
5658
params.push( '-passout' );
5759
params.push( 'file:' + clientKeyPassword );
5860
}
59-
61+
6062
params.push(keyBitsize);
61-
63+
6264
execOpenSSL(params, 'RSA PRIVATE KEY', function(error, key) {
6365
if(clientKeyPassword) {
6466
fs.unlink(clientKeyPassword);
@@ -426,6 +428,49 @@ function getModulus(certificate, callback) {
426428
});
427429
}
428430

431+
function getModulusOfCertificate(certificate, password, callback)
432+
{
433+
if (password && password !== '' && password !== null) {
434+
getModulusOfSignedCert(certificate,password,callback);
435+
} else {
436+
getModulus(certificate,callback);
437+
}
438+
}
439+
440+
function getModulusOfSignedCert(certificate, password, callback){
441+
certificate = Buffer.isBuffer(certificate) && certificate.toString() || certificate;
442+
443+
var type = '';
444+
if (certificate.match(/BEGIN(\sNEW)? CERTIFICATE REQUEST/)) {
445+
type = 'req';
446+
} else if (certificate.match(/BEGIN RSA PRIVATE KEY/)) {
447+
type = 'rsa';
448+
} else {
449+
type = 'x509';
450+
}
451+
var params = [type,
452+
'-noout',
453+
'-modulus',
454+
'-in',
455+
'--TMPFILE--',
456+
'-passin',
457+
'pass:' + password
458+
];
459+
spawnWrapper(params, certificate, function(err, code, stdout) {
460+
if (err) {
461+
return callback(err);
462+
}
463+
var match = stdout.match(/Modulus=([0-9a-fA-F]+)$/m);
464+
if (match) {
465+
return callback(null, {
466+
modulus: match[1]
467+
});
468+
} else {
469+
return callback(new Error('No modulus'));
470+
}
471+
});
472+
}
473+
429474
/**
430475
* config the pem module
431476
* @param {Object} options

0 commit comments

Comments
 (0)