diff --git a/lib/pem.js b/lib/pem.js index 2ce8e33a..7d66e630 100644 --- a/lib/pem.js +++ b/lib/pem.js @@ -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'; @@ -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; @@ -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(); + }); }); } @@ -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(); + }); } \ No newline at end of file diff --git a/package.json b/package.json index a52f6001..89f9a8c1 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "scripts": { "test": "nodeunit test" }, - "dependencies": {}, + "dependencies": { + "which": "^1.0.8" + }, "devDependencies": { "nodeunit": "*" }, @@ -19,4 +21,4 @@ "engines": { "node": "*" } -} \ No newline at end of file +}