Skip to content

Commit

Permalink
Creating a CSR for an encrypted key
Browse files Browse the repository at this point in the history
Added the ability to specify the password for a private key when creating a CSR.
  • Loading branch information
jpage-godaddy committed Feb 25, 2015
1 parent dbae78a commit e46481a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ results
node_modules
npm-debug.log
.DS_Store
tmp
tmp
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Where
Possible options are the following

* **clientKey** is an optional client key to use
* **clientKeyPassword** the optional password for `clientKey`
* **keyBitsize** - if `clientKey` is undefined, bit size to use for generating a new key (defaults to 2048)
* **hash** is a hash function to use (either `md5`, `sha1` or `sha256`, defaults to `sha256`)
* **country** is a CSR country field
Expand Down
5 changes: 5 additions & 0 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ function createCSR(options, callback) {
].join('\n'));
}

if (options.clientKeyPassword) {
params.push('-passin');
params.push('pass:' + options.clientKeyPassword);
}

execOpenSSL(params, 'CERTIFICATE REQUEST', tmpfiles, function(error, data) {
if (error) {
return callback(error);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-nodeunit": "^0.4.1"
"grunt-contrib-nodeunit": "^0.4.1",
"nodeunit": "^0.9.0"
},
"optionalDependencies": {},
"engines": {
Expand Down
24 changes: 24 additions & 0 deletions test/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,31 @@ exports['General Tests'] = {
});
});
},

'Create CSR with own encrypted key': function(test) {
var password = 'Some pass-phrase';
pem.createPrivateKey(2048, { cipher: 'des3', password: password }, function(error, data) {
var key = (data && data.key || '').toString();

pem.createCSR({
clientKey: key,
clientKeyPassword: password
}, function(error, data) {
var csr = (data && data.csr || '').toString();
test.ifError(error);
test.ok(csr);
test.ok(csr.match(/^\n*\-\-\-\-\-BEGIN CERTIFICATE REQUEST\-\-\-\-\-\n/));
test.ok(csr.match(/\n\-\-\-\-\-END CERTIFICATE REQUEST\-\-\-\-\-\n*$/));

test.equal(data && data.clientKey, key);

test.ok(data && data.clientKey);
test.ok(fs.readdirSync('./tmp').length === 0);
test.done();
});
});
},

'Create default certificate': function(test) {
pem.createCertificate(function(error, data) {
var certificate = (data && data.certificate || '').toString();
Expand Down

0 comments on commit e46481a

Please sign in to comment.