Skip to content

Commit d7e7008

Browse files
committed
crypto: throw if the key doesn't match cert
fix nodejs/node-v0.x-archive#8770 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> PR-URL: node-forward/node#66
1 parent b594e59 commit d7e7008

3 files changed

Lines changed: 73 additions & 20 deletions

File tree

lib/_tls_common.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,6 @@ exports.createSecureContext = function createSecureContext(options, context) {
6565

6666
if (context) return c;
6767

68-
if (options.key) {
69-
if (Array.isArray(options.key)) {
70-
for (var i = 0; i < options.key.length; i++) {
71-
var key = options.key[i];
72-
73-
if (key.passphrase)
74-
c.context.setKey(key.pem, key.passphrase);
75-
else
76-
c.context.setKey(key);
77-
}
78-
} else {
79-
if (options.passphrase) {
80-
c.context.setKey(options.key, options.passphrase);
81-
} else {
82-
c.context.setKey(options.key);
83-
}
84-
}
85-
}
86-
8768
// NOTE: It's important to add CA before the cert to be able to load
8869
// cert's issuer in C++ code.
8970
if (options.ca) {
@@ -107,6 +88,29 @@ exports.createSecureContext = function createSecureContext(options, context) {
10788
}
10889
}
10990

91+
// NOTE: It is important to set the key after the cert.
92+
// `ssl_set_pkey` returns `0` when the key does not much the cert, but
93+
// `ssl_set_cert` returns `1` and nullifies the key in the SSL structure
94+
// which leads to the crash later on.
95+
if (options.key) {
96+
if (Array.isArray(options.key)) {
97+
for (var i = 0; i < options.key.length; i++) {
98+
var key = options.key[i];
99+
100+
if (key.passphrase)
101+
c.context.setKey(key.pem, key.passphrase);
102+
else
103+
c.context.setKey(key);
104+
}
105+
} else {
106+
if (options.passphrase) {
107+
c.context.setKey(options.key, options.passphrase);
108+
} else {
109+
c.context.setKey(options.key);
110+
}
111+
}
112+
}
113+
110114
if (options.ciphers)
111115
c.context.setCiphers(options.ciphers);
112116
else

src/node_crypto.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,16 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
456456
return ThrowCryptoError(env, err);
457457
}
458458

459-
SSL_CTX_use_PrivateKey(sc->ctx_, key);
459+
int rv = SSL_CTX_use_PrivateKey(sc->ctx_, key);
460460
EVP_PKEY_free(key);
461461
BIO_free_all(bio);
462+
463+
if (!rv) {
464+
unsigned long err = ERR_get_error();
465+
if (!err)
466+
return env->ThrowError("SSL_CTX_use_PrivateKey");
467+
return ThrowCryptoError(env, err);
468+
}
462469
}
463470

464471

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 common = require('../common');
28+
var assert = require('assert');
29+
var tls = require('tls');
30+
var fs = require('fs');
31+
var fs = require('fs');
32+
33+
var options = {
34+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
35+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem')
36+
};
37+
38+
var cert = null;
39+
40+
assert.throws(function() {
41+
tls.createSecureContext(options);
42+
});

0 commit comments

Comments
 (0)