Skip to content

Commit cdde9a3

Browse files
committed
crypto: add newline to cert and key if not present
After one of OpenSSL updates we have stopped accepting PEM private keys and certificates that doesn't end with a newline (`\n`) character. Handle this regression in `crypto.js` to make less trouble to our users. fix #6892
1 parent 661190a commit cdde9a3

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

lib/crypto.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ function Credentials(secureProtocol, flags, context) {
7878

7979
exports.Credentials = Credentials;
8080

81+
function addNewline(buf) {
82+
var last = buf[buf.length - 1];
83+
var isBuf = Buffer.isBuffer(buf);
84+
85+
if (!isBuf && !util.isString(buf))
86+
throw new Error('Certificate should be of type Buffer or string');
87+
88+
if (isBuf ? last !== 10 : last !== '\n')
89+
return buf.toString().trim() + '\n';
90+
else
91+
return buf;
92+
}
8193

8294
exports.createCredentials = function(options, context) {
8395
if (!options) options = {};
@@ -89,14 +101,15 @@ exports.createCredentials = function(options, context) {
89101
if (context) return c;
90102

91103
if (options.key) {
104+
var key = addNewline(options.key);
92105
if (options.passphrase) {
93-
c.context.setKey(options.key, options.passphrase);
106+
c.context.setKey(key, options.passphrase);
94107
} else {
95-
c.context.setKey(options.key);
108+
c.context.setKey(key);
96109
}
97110
}
98111

99-
if (options.cert) c.context.setCert(options.cert);
112+
if (options.cert) c.context.setCert(addNewline(options.cert));
100113

101114
if (options.ciphers) c.context.setCiphers(options.ciphers);
102115

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
if (!process.versions.openssl) {
23+
console.error('Skipping because node compiled without OpenSSL.');
24+
process.exit(0);
25+
}
26+
27+
var tls = require('tls');
28+
29+
var assert = require('assert');
30+
var common = require('../common');
31+
32+
var cert = '-----BEGIN CERTIFICATE-----\n' +
33+
'MIIBfjCCASgCCQDmmNjAojbDQjANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB\n' +
34+
'VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0\n' +
35+
'cyBQdHkgTHRkMCAXDTE0MDExNjE3NTMxM1oYDzIyODcxMDMxMTc1MzEzWjBFMQsw\n' +
36+
'CQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJu\n' +
37+
'ZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAPKwlfMX\n' +
38+
'6HGZIt1xm7fna72eWcOYfUfSxSugghvqYgJt2Oi3lH+wsU1O9FzRIVmpeIjDXhbp\n' +
39+
'Mjsa1HtzSiccPXsCAwEAATANBgkqhkiG9w0BAQUFAANBAHOoKy0NkyfiYH7Ne5ka\n' +
40+
'uvCyndyeB4d24FlfqEUlkfaWCZlNKRaV9YhLDiEg3BcIreFo4brtKQfZzTRs0GVm\n' +
41+
'KHg=\n' +
42+
'-----END CERTIFICATE-----';
43+
var key = '-----BEGIN RSA PRIVATE KEY-----\n' +
44+
'MIIBPQIBAAJBAPKwlfMX6HGZIt1xm7fna72eWcOYfUfSxSugghvqYgJt2Oi3lH+w\n' +
45+
'sU1O9FzRIVmpeIjDXhbpMjsa1HtzSiccPXsCAwEAAQJBAM4uU9aJE0OfdE1p/X+K\n' +
46+
'LrCT3XMdFCJ24GgmHyOURtwDy18upQJecDVdcZp16fjtOPmaW95GoYRyifB3R4I5\n' +
47+
'RxECIQD7jRM9slCSVV8xp9kOJQNpHjhRQYVGBn+pyllS2sb+RQIhAPb7Y+BIccri\n' +
48+
'NWnuhwCW8hA7Fkj/kaBdAwyW7L3Tvui/AiEAiqLCovMecre4Yi6GcsQ1b/6mvSmm\n' +
49+
'IOS+AT6zIfXPTB0CIQCJKGR3ymN/Qw5crL1GQ41cHCQtF9ickOq/lBUW+j976wIh\n' +
50+
'AOaJnkQrmurlRdePX6LvN/LgGAQoxwovfjcOYNnZsIVY\n' +
51+
'-----END RSA PRIVATE KEY-----';
52+
53+
function test(cert, key, cb) {
54+
var server = tls.createServer({
55+
cert: cert,
56+
key: key
57+
}).listen(common.PORT, function() {
58+
server.close(cb);
59+
});
60+
}
61+
62+
var completed = false;
63+
test(cert, key, function() {
64+
test(new Buffer(cert), new Buffer(key), function() {
65+
completed = true;
66+
});
67+
});
68+
69+
process.on('exit', function() {
70+
assert(completed);
71+
});

0 commit comments

Comments
 (0)