Skip to content

Commit

Permalink
Add getDhparamInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed May 31, 2015
1 parent 9e23b92 commit eead162
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ Where
* **certificate** is a PEM encoded certificate, CSR or private key
* **callback** is a callback function with an error object and `{modulus}`

### Get DH parameter information

Use `getDhparamInfo` to get the size and prime of DH parameters.

pem.getDhparamInfo(dhparam, callback)

Where

* **dhparam** is a PEM encoded DH parameters string
* **callback** is a callback function with an error object and `{size, prime}`

### Setting openssl location

In some systems the `openssl` executable might not be available by the default name or it is not included in $PATH. In this case you can define the location of the executable yourself as a one time action after you have loaded the pem module:
Expand Down
48 changes: 48 additions & 0 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports.getPublicKey = getPublicKey;
module.exports.getFingerprint = getFingerprint;
module.exports.getModulus = getModulus;
module.exports.getModulusFromProtected = getModulusFromProtected;
module.exports.getDhparamInfo = getDhparamInfo;
module.exports.config = config;

// PUBLIC API
Expand Down Expand Up @@ -465,6 +466,53 @@ function getModulusFromProtected(key, password, callback){
});
}

/**
* get the size and prime of DH parameters
*
* @param {String} DH parameters, PEM encoded
* @param {Function} callback Callback function with an error object and {size, prime}
*/
function getDhparamInfo(dh, callback) {
dh = Buffer.isBuffer(dh) && dh.toString() || dh;

var params = [
'dh',
'-text',
'-in',
'--TMPFILE--'
];

spawnWrapper(params, dh, function(err, code, stdout) {
if (err) {
return callback(err);
}

var result = {};
var match = stdout.match(/Parameters: \((\d+) bit\)/);

if (match) {
result.size = Number(match[1]);
}

var prime = '';
stdout.split('\n').forEach(function (line) {
if (/\s+([0-9a-f][0-9a-f]:)+[0-9a-f]?[0-9a-f]?/g.test(line)) {
prime += line.trim();
}
});

if (prime) {
result.prime = prime;
}

if (!match && !prime) {
return callback(new Error('No DH info found'));
}

return callback(null, result);
});
}

/**
* config the pem module
* @param {Object} options
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/test.dh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN DH PARAMETERS-----
MIGHAoGBAMYXwgiuPY6TqxODWXbRRWx6eWoJuGkjKN8RjhBiLxFJzwgpdfONv5iG
IHHGI8/IfhHI78Mqq+5z3z8L16fuOYnpbaDa2BSUdHZQQmFiCV748lOv9he08UJ5
qgrFgdgi56V4FdRs2EHJnezvYmviAbIsi8imn+9TVed4DnOmuE1rAgEC
-----END DH PARAMETERS-----
17 changes: 17 additions & 0 deletions test/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ exports['General Tests'] = {
});
});
},

'Get modulus from a protected key': function(test) {
var certificate = fs.readFileSync('./test/fixtures/test.crt').toString();
var key = fs.readFileSync('./test/fixtures/test.key').toString();
Expand All @@ -425,8 +426,24 @@ exports['General Tests'] = {
test.done();
});
});
},

'Get DH param info': function(test) {
var dh = fs.readFileSync('./test/fixtures/test.dh').toString();

pem.getDhparamInfo(dh, function(error, data) {
var size = data && data.size || 0;
var prime = (data && data.prime || '').toString();
test.ifError(error);
test.ok(size);
test.ok(prime);
test.ok(fs.readdirSync('./tmp').length === 0);
test.equal(typeof size, 'number');
test.ok(/([0-9a-f][0-9a-f]:)+[0-9a-f][0-9a-f]$/g.test(prime));
test.done();
});
},

'Create and verify wildcard certificate': function(test) {
var certInfo = {
commonName: '*.node.ee'
Expand Down

0 comments on commit eead162

Please sign in to comment.