diff --git a/lib/pem.js b/lib/pem.js index e3ca2544..a5a610e8 100644 --- a/lib/pem.js +++ b/lib/pem.js @@ -63,7 +63,7 @@ function createCSR(options, callback){ var response = { csr: data, clientKey: options.clientKey - } + }; return callback(null, response); }); @@ -124,15 +124,15 @@ function createCertificate(options, callback){ clientKey: options.clientKey, certificate: data, serviceKey: options.serviceKey - } + }; return callback(null, response); }); } function getPublicKey(certificate, callback){ - if(!callback && typeof options == "function"){ - callback = options; - options = undefined; + if(!callback && typeof certificate == "function"){ + callback = certificate; + certificate = undefined; } certificate = (certificate || "").toString(); @@ -147,15 +147,15 @@ function getPublicKey(certificate, callback){ "-noout"]; }else if(certificate.match(/BEGIN RSA PRIVATE KEY/)){ params = ["rsa", - "-in", - "/dev/stdin", - "-pubout"]; + "-in", + "/dev/stdin", + "-pubout"]; }else{ params = ["x509", - "-in", - "/dev/stdin", - "-pubkey", - "-noout"]; + "-in", + "/dev/stdin", + "-pubkey", + "-noout"]; } execOpenSSL(params, "PUBLIC KEY", certificate, function(error, key){ @@ -167,9 +167,9 @@ function getPublicKey(certificate, callback){ } function readCertificateInfo(certificate, callback){ - if(!callback && typeof options == "function"){ - callback = options; - options = undefined; + if(!callback && typeof certificate == "function"){ + callback = certificate; + certificate = undefined; } certificate = (certificate || "").toString(); @@ -271,7 +271,17 @@ function generateCSRSubject(options){ function execOpenSSL(params, searchStr, stdin, callback){ var openssl = spawn("openssl", params), stdout = "", - stderr = ""; + stderr = "", + pushToStdin = function(){ + var data = stdin.shift(); + + if(data){ + openssl.stdin.write(data + "\n"); + if(!stdin.length){ + openssl.stdin.end(); + } + } + }; if(!callback && typeof stdin == "function"){ callback = stdin; @@ -301,12 +311,13 @@ function execOpenSSL(params, searchStr, stdin, callback){ openssl.on('exit', function (code) { var start, end; + stdout = new Buffer(stdout, "binary").toString("utf-8"); + stderr = new Buffer(stderr, "binary").toString("utf-8"); + if(code){ - return callback(new Error("Invalid openssl exit code "+code)); + return callback(new Error("Invalid openssl exit code "+code+"\nSTDOUT:\n"+stdout+"\nSTDERR:\n"+stderr)); } - stdout = new Buffer(stdout, "binary").toString("utf-8"); - if((start = stdout.match(new RegExp("\\-+BEGIN "+searchStr+"\\-+$", "m")))){ start = start.index; }else{ @@ -326,14 +337,4 @@ function execOpenSSL(params, searchStr, stdin, callback){ } }); - function pushToStdin(){ - var data = stdin.shift(); - - if(data){ - openssl.stdin.write(data + "\n"); - if(!stdin.length){ - openssl.stdin.end(); - } - } - } } \ No newline at end of file diff --git a/test/pem.js b/test/pem.js index d45ba388..9fe387ea 100644 --- a/test/pem.js +++ b/test/pem.js @@ -112,7 +112,7 @@ exports["General Tests"] = { organization: '', organizationUnit: '', commonName: 'localhost', - emailAddress: '' }) + emailAddress: '' }); test.done(); }); }); @@ -132,7 +132,7 @@ exports["General Tests"] = { pem.readCertificateInfo(csr, function(error, data){ test.ifError(error); - test.deepEqual(data, certInfo) + test.deepEqual(data, certInfo); test.done(); }); }); @@ -152,7 +152,7 @@ exports["General Tests"] = { organization: '', organizationUnit: '', commonName: 'localhost', - emailAddress: '' }) + emailAddress: '' }); test.done(); }); }); @@ -172,7 +172,7 @@ exports["General Tests"] = { pem.readCertificateInfo(certificate, function(error, data){ test.ifError(error); - test.deepEqual(data, certInfo) + test.deepEqual(data, certInfo); test.done(); }); }); @@ -237,4 +237,4 @@ exports["General Tests"] = { }); } -} \ No newline at end of file +}; \ No newline at end of file