This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
tls: show human-readable error messages #6897
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ using v8::FunctionTemplate; | |
using v8::Handle; | ||
using v8::HandleScope; | ||
using v8::Integer; | ||
using v8::Isolate; | ||
using v8::Local; | ||
using v8::Null; | ||
using v8::Object; | ||
|
@@ -1248,32 +1249,32 @@ void SSLWrap<Base>::IsInitFinished(const FunctionCallbackInfo<Value>& args) { | |
} | ||
|
||
|
||
#define CASE_X509_ERR(CODE) case X509_V_ERR_##CODE: reason = #CODE; break; | ||
template <class Base> | ||
void SSLWrap<Base>::VerifyError(const FunctionCallbackInfo<Value>& args) { | ||
HandleScope scope(node_isolate); | ||
Isolate* isolate = args.GetIsolate(); | ||
HandleScope scope(isolate); | ||
|
||
Base* w = Unwrap<Base>(args.This()); | ||
|
||
// XXX(indutny) Do this check in JS land? | ||
X509* peer_cert = SSL_get_peer_certificate(w->ssl_); | ||
if (peer_cert == NULL) { | ||
// We requested a certificate and they did not send us one. | ||
// Definitely an error. | ||
// XXX(indutny) is this the right error message? | ||
Local<String> s = | ||
FIXED_ONE_BYTE_STRING(node_isolate, "UNABLE_TO_GET_ISSUER_CERT"); | ||
return args.GetReturnValue().Set(Exception::Error(s)); | ||
// XXX(bnoordhuis) The UNABLE_TO_GET_ISSUER_CERT error when there is no | ||
// peer certificate is questionable but it's compatible with what was | ||
// here before. | ||
long x509_verify_error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; | ||
if (X509* peer_cert = SSL_get_peer_certificate(w->ssl_)) { | ||
X509_free(peer_cert); | ||
x509_verify_error = SSL_get_verify_result(w->ssl_); | ||
} | ||
X509_free(peer_cert); | ||
|
||
long x509_verify_error = SSL_get_verify_result(w->ssl_); | ||
if (x509_verify_error == X509_V_OK) | ||
return args.GetReturnValue().SetNull(); | ||
|
||
const char* reason = NULL; | ||
Local<String> s; | ||
// XXX(bnoordhuis) X509_verify_cert_error_string() is not actually thread-safe | ||
// in the presence of invalid error codes. Probably academical but something | ||
// to keep in mind if/when node ever grows multi-isolate capabilities. | ||
const char* reason = X509_verify_cert_error_string(x509_verify_error); | ||
const char* code = reason; | ||
#define CASE_X509_ERR(CODE) case X509_V_ERR_##CODE: code = #CODE; break; | ||
switch (x509_verify_error) { | ||
case X509_V_OK: | ||
return args.GetReturnValue().SetNull(); | ||
CASE_X509_ERR(UNABLE_TO_GET_ISSUER_CERT) | ||
CASE_X509_ERR(UNABLE_TO_GET_CRL) | ||
CASE_X509_ERR(UNABLE_TO_DECRYPT_CERT_SIGNATURE) | ||
|
@@ -1301,18 +1302,16 @@ void SSLWrap<Base>::VerifyError(const FunctionCallbackInfo<Value>& args) { | |
CASE_X509_ERR(INVALID_PURPOSE) | ||
CASE_X509_ERR(CERT_UNTRUSTED) | ||
CASE_X509_ERR(CERT_REJECTED) | ||
default: | ||
s = OneByteString(node_isolate, | ||
X509_verify_cert_error_string(x509_verify_error)); | ||
break; | ||
} | ||
#undef CASE_X509_ERR | ||
|
||
if (s.IsEmpty()) | ||
s = OneByteString(node_isolate, reason); | ||
|
||
args.GetReturnValue().Set(Exception::Error(s)); | ||
Local<String> reason_string = OneByteString(isolate, reason); | ||
Local<Value> exception_value = Exception::Error(reason_string); | ||
Local<Object> exception_object = exception_value->ToObject(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Future-proofing. It's always an object now but if that ever changes, then |
||
exception_object->Set(FIXED_ONE_BYTE_STRING(isolate, "code"), | ||
OneByteString(isolate, code)); | ||
args.GetReturnValue().Set(exception_object); | ||
} | ||
#undef CASE_X509_ERR | ||
|
||
|
||
template <class Base> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
if (!process.versions.openssl) { | ||
console.error('Skipping because node compiled without OpenSSL.'); | ||
process.exit(0); | ||
} | ||
|
||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var fs = require('fs'); | ||
var tls = require('tls'); | ||
|
||
var key = fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'); | ||
var cert = fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem'); | ||
|
||
tls.createServer({ key: key, cert: cert }, function(conn) { | ||
conn.end(); | ||
this.close(); | ||
}).listen(0, function() { | ||
var options = { port: this.address().port, rejectUnauthorized: true }; | ||
tls.connect(options).on('error', common.mustCall(function(err) { | ||
assert.equal(err.code, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'); | ||
assert.equal(err.message, 'unable to verify the first certificate'); | ||
this.destroy(); | ||
})); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks a bit unrelated, right? Mind opening issue for changing all HandleScopes in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's related in the sense that we need the isolate further down below. I thought I'd fix up the HandleScope while I'm here, saves work if/when node grows multi-isolate support. I can back it out if you want.
I'm not going to fix up all the HandleScopes in the file however, I'm strictly working on a scratch-my-own-itch basis. (Or scratch-a-user-itch as is the case here.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you got me wrong, I'm going to fix it myself. Just would be cool if this patch won't contain partial fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, 'opening issue' - yes, I misread that. :-) Will do and I'll back out this change.