Skip to content

Commit

Permalink
add node which to check if the user has openssl on their system
Browse files Browse the repository at this point in the history
  • Loading branch information
noamokman committed Jan 18, 2015
1 parent a308e55 commit b44c619
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 44 deletions.
106 changes: 64 additions & 42 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var spawn = require('child_process').spawn,
fs = require('fs'),
net = require('net'),
crypto = require('crypto'),
which = require('which'),
pathOpenSSL,
tempDir = process.env.PEMJS_TMPDIR || (os.tmpdir || os.tmpDir) && (os.tmpdir || os.tmpDir)() || '/tmp';

Expand Down Expand Up @@ -70,8 +71,6 @@ function createPrivateKey(keyBitsize, callback) {
* @param {Array} [options.altNames] is a list of subjectAltNames in the subjectAltName field
* @param {Function} callback Callback function with an error object and {csr, clientKey}
*/


function createCSR(options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
Expand Down Expand Up @@ -536,55 +535,62 @@ function generateCSRSubject(options) {
*/
function spawnOpenSSL(params, callback) {
var pathBin = pathOpenSSL || process.env.OPENSSL_BIN || 'openssl';
var openssl = spawn(pathBin, params),
stdout = '',
stderr = '';

openssl.stdout.on('data', function(data) {
stdout += (data || '').toString('binary');
});
testOpenSSLPath(pathBin, function(err) {
if(err) {
return callback(err);
}

openssl.stderr.on('data', function(data) {
stderr += (data || '').toString('binary');
});
var openssl = spawn(pathBin, params),
stdout = '',
stderr = '';

// We need both the return code and access to all of stdout. Stdout isn't
// *really* available until the close event fires; the timing nuance was
// making this fail periodically.
var needed = 2; // wait for both exit and close.
var code = -1;
var finished = false;
var done = function(err) {
if (finished) {
return;
}
openssl.stdout.on('data', function(data) {
stdout += (data || '').toString('binary');
});

if (err) {
finished = true;
return callback(err);
}
openssl.stderr.on('data', function(data) {
stderr += (data || '').toString('binary');
});

if (--needed < 1) {
finished = true;
if (code) {
callback(new Error('Invalid openssl exit code: ' + code + '\n% openssl ' + params.join(' ') + '\n' + stderr), code);
} else {
callback(null, code, stdout, stderr);
// We need both the return code and access to all of stdout. Stdout isn't
// *really* available until the close event fires; the timing nuance was
// making this fail periodically.
var needed = 2; // wait for both exit and close.
var code = -1;
var finished = false;
var done = function(err) {
if (finished) {
return;
}
}
};

openssl.on('error', done);
if (err) {
finished = true;
return callback(err);
}

if (--needed < 1) {
finished = true;
if (code) {
callback(new Error('Invalid openssl exit code: ' + code + '\n% openssl ' + params.join(' ') + '\n' + stderr), code);
} else {
callback(null, code, stdout, stderr);
}
}
};

openssl.on('error', done);

openssl.on('exit', function(ret) {
code = ret;
done();
});
openssl.on('exit', function(ret) {
code = ret;
done();
});

openssl.on('close', function() {
stdout = new Buffer(stdout, 'binary').toString('utf-8');
stderr = new Buffer(stderr, 'binary').toString('utf-8');
done();
openssl.on('close', function() {
stdout = new Buffer(stdout, 'binary').toString('utf-8');
stderr = new Buffer(stderr, 'binary').toString('utf-8');
done();
});
});
}

Expand Down Expand Up @@ -667,4 +673,20 @@ function execOpenSSL(params, searchStr, tmpfiles, callback) {
return callback(new Error(searchStr + ' not found from openssl output:\n---stdout---\n' + stdout + '\n---stderr---\n' + stderr + '\ncode: ' + code));
}
});
}

/**
* Validates the pathBin for the openssl command
*
* @param {String} pathBin The path to OpenSSL Bin
* @param {Function} callback Callback function with an error object
*/
function testOpenSSLPath(pathBin, callback){
which(pathBin, function(error){
if(error){
return callback(new Error('Could not find openssl on your system on this path: ' + pathBin));
}

callback();
});
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
"scripts": {
"test": "nodeunit test"
},
"dependencies": {},
"dependencies": {
"which": "^1.0.8"
},
"devDependencies": {
"nodeunit": "*"
},
"optionalDependencies": {},
"engines": {
"node": "*"
}
}
}

0 comments on commit b44c619

Please sign in to comment.