Skip to content

Commit 5e41c02

Browse files
committed
crypto: clear errors from verify failure
OpenSSL will push errors onto the stack when a verify fails, which can disrupt TLS and other routines if we don't clear the error stack Fixes #6304
1 parent 8fc48bc commit 5e41c02

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/node_crypto.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,6 +3186,9 @@ class Verify : public ObjectWrap {
31863186
int VerifyFinal(char* key_pem, int key_pemLen, unsigned char* sig, int siglen) {
31873187
if (!initialised_) return 0;
31883188

3189+
ClearErrorOnReturn clear_error_on_return;
3190+
(void) &clear_error_on_return; // Silence compiler warning.
3191+
31893192
EVP_PKEY* pkey = NULL;
31903193
BIO *bp = NULL;
31913194
X509 *x509 = NULL;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
23+
24+
25+
var common = require('../common');
26+
var assert = require('assert');
27+
28+
try {
29+
var crypto = require('crypto');
30+
var tls = require('tls');
31+
} catch (e) {
32+
console.log('Not compiled with OPENSSL support.');
33+
process.exit();
34+
}
35+
36+
crypto.DEFAULT_ENCODING = 'buffer';
37+
38+
var fs = require('fs');
39+
40+
var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
41+
42+
var options = {
43+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
44+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
45+
};
46+
47+
var canSend = true;
48+
49+
var server = tls.Server(options, function(socket) {
50+
process.nextTick(function() {
51+
console.log('sending');
52+
socket.destroy();
53+
verify();
54+
});
55+
});
56+
57+
var client;
58+
59+
function verify() {
60+
console.log('verify');
61+
var verified = crypto.createVerify('RSA-SHA1')
62+
.update('Test')
63+
.verify(certPem, 'asdfasdfas', 'base64');
64+
}
65+
66+
server.listen(common.PORT, function() {
67+
client = tls.connect({
68+
port: common.PORT,
69+
rejectUnauthorized: false
70+
}, function() {
71+
verify();
72+
}).on('data', function(data) {
73+
console.log(data);
74+
}).on('error', function(err) {
75+
throw err;
76+
}).on('close', function() {
77+
server.close();
78+
}).resume();
79+
});
80+
81+
server.unref();

0 commit comments

Comments
 (0)