@@ -18,6 +18,8 @@ module.exports.readCertificateInfo = readCertificateInfo;
18
18
module . exports . getPublicKey = getPublicKey ;
19
19
module . exports . getFingerprint = getFingerprint ;
20
20
module . exports . getModulus = getModulus ;
21
+ module . exports . getModulusOfSignedCert = getModulusOfSignedCert ;
22
+ module . exports . getModulusOfCertificate = getModulusOfCertificate ;
21
23
module . exports . config = config ;
22
24
23
25
// PUBLIC API
@@ -56,9 +58,9 @@ function createPrivateKey(keyBitsize, options, callback) {
56
58
params . push ( '-passout' ) ;
57
59
params . push ( 'file:' + clientKeyPassword ) ;
58
60
}
59
-
61
+
60
62
params . push ( keyBitsize ) ;
61
-
63
+
62
64
execOpenSSL ( params , 'RSA PRIVATE KEY' , function ( error , key ) {
63
65
if ( clientKeyPassword ) {
64
66
fs . unlink ( clientKeyPassword ) ;
@@ -426,6 +428,49 @@ function getModulus(certificate, callback) {
426
428
} ) ;
427
429
}
428
430
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 ( / B E G I N ( \s N E W ) ? C E R T I F I C A T E R E Q U E S T / ) ) {
445
+ type = 'req' ;
446
+ } else if ( certificate . match ( / B E G I N R S A P R I V A T E K E Y / ) ) {
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 ( / M o d u l u s = ( [ 0 - 9 a - f A - 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
+
429
474
/**
430
475
* config the pem module
431
476
* @param {Object } options
0 commit comments