Skip to content

Commit b05b330

Browse files
sam-githubdanbev
authored andcommitted
tls: add code for ERR_TLS_INVALID_PROTOCOL_METHOD
Add an error code property to invalid `secureProtocol` method exceptions. PR-URL: #24729 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 3513b0c commit b05b330

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

doc/api/errors.md

+6
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,12 @@ recommended to use 2048 bits or larger for stronger security.
16551655
A TLS/SSL handshake timed out. In this case, the server must also abort the
16561656
connection.
16571657

1658+
<a id="ERR_TLS_INVALID_PROTOCOL_METHOD"></a>
1659+
### ERR_TLS_INVALID_PROTOCOL_METHOD
1660+
1661+
The specified `secureProtocol` method is invalid. It is either unknown, or
1662+
disabled because it is insecure.
1663+
16581664
<a id="ERR_TLS_INVALID_PROTOCOL_VERSION"></a>
16591665
### ERR_TLS_INVALID_PROTOCOL_VERSION
16601666

src/node_crypto.cc

+16-7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ v8::MaybeLocal<v8::Object> New(Environment* env, unsigned char* udata,
6363

6464
namespace crypto {
6565

66+
using node::THROW_ERR_TLS_INVALID_PROTOCOL_METHOD;
67+
6668
using v8::Array;
6769
using v8::Boolean;
6870
using v8::ConstructorBehavior;
@@ -421,17 +423,23 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
421423
// protocols are supported unless explicitly disabled (which we do below
422424
// for SSLv2 and SSLv3.)
423425
if (strcmp(*sslmethod, "SSLv2_method") == 0) {
424-
return env->ThrowError("SSLv2 methods disabled");
426+
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
427+
return;
425428
} else if (strcmp(*sslmethod, "SSLv2_server_method") == 0) {
426-
return env->ThrowError("SSLv2 methods disabled");
429+
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
430+
return;
427431
} else if (strcmp(*sslmethod, "SSLv2_client_method") == 0) {
428-
return env->ThrowError("SSLv2 methods disabled");
432+
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
433+
return;
429434
} else if (strcmp(*sslmethod, "SSLv3_method") == 0) {
430-
return env->ThrowError("SSLv3 methods disabled");
435+
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
436+
return;
431437
} else if (strcmp(*sslmethod, "SSLv3_server_method") == 0) {
432-
return env->ThrowError("SSLv3 methods disabled");
438+
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
439+
return;
433440
} else if (strcmp(*sslmethod, "SSLv3_client_method") == 0) {
434-
return env->ThrowError("SSLv3 methods disabled");
441+
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
442+
return;
435443
} else if (strcmp(*sslmethod, "SSLv23_method") == 0) {
436444
// noop
437445
} else if (strcmp(*sslmethod, "SSLv23_server_method") == 0) {
@@ -483,7 +491,8 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
483491
max_version = TLS1_2_VERSION;
484492
method = TLS_client_method();
485493
} else {
486-
return env->ThrowError("Unknown method");
494+
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "Unknown method");
495+
return;
487496
}
488497
}
489498

src/node_errors.h

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ void FatalException(const v8::FunctionCallbackInfo<v8::Value>& args);
7171
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \
7272
V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error) \
7373
V(ERR_STRING_TOO_LONG, Error) \
74+
V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError) \
7475
V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, TypeError) \
7576

7677
#define V(code, type) \

test/parallel/test-tls-min-max-version.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function test(cmin, cmax, cprot, smin, smax, sprot, expect) {
2626
secureProtocol: sprot,
2727
},
2828
}, common.mustCall((err, pair, cleanup) => {
29-
if (expect && !expect.match(/^TLS/)) {
30-
assert(err.message.match(expect));
29+
if (expect && expect.match(/^ERR/)) {
30+
assert.strictEqual(err.code, expect);
3131
return cleanup();
3232
}
3333

@@ -53,18 +53,22 @@ const U = undefined;
5353
test(U, U, U, U, U, U, 'TLSv1.2');
5454

5555
// Insecure or invalid protocols cannot be enabled.
56-
test(U, U, U, U, U, 'SSLv2_method', 'SSLv2 methods disabled');
57-
test(U, U, U, U, U, 'SSLv3_method', 'SSLv3 methods disabled');
58-
test(U, U, 'SSLv2_method', U, U, U, 'SSLv2 methods disabled');
59-
test(U, U, 'SSLv3_method', U, U, U, 'SSLv3 methods disabled');
60-
test(U, U, 'hokey-pokey', U, U, U, 'Unknown method');
61-
test(U, U, U, U, U, 'hokey-pokey', 'Unknown method');
56+
test(U, U, U, U, U, 'SSLv2_method', 'ERR_TLS_INVALID_PROTOCOL_METHOD');
57+
test(U, U, U, U, U, 'SSLv3_method', 'ERR_TLS_INVALID_PROTOCOL_METHOD');
58+
test(U, U, 'SSLv2_method', U, U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD');
59+
test(U, U, 'SSLv3_method', U, U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD');
60+
test(U, U, 'hokey-pokey', U, U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD');
61+
test(U, U, U, U, U, 'hokey-pokey', 'ERR_TLS_INVALID_PROTOCOL_METHOD');
6262

6363
// Cannot use secureProtocol and min/max versions simultaneously.
64-
test(U, U, U, U, 'TLSv1.2', 'TLS1_2_method', 'conflicts with secureProtocol');
65-
test(U, U, U, 'TLSv1.2', U, 'TLS1_2_method', 'conflicts with secureProtocol');
66-
test(U, 'TLSv1.2', 'TLS1_2_method', U, U, U, 'conflicts with secureProtocol');
67-
test('TLSv1.2', U, 'TLS1_2_method', U, U, U, 'conflicts with secureProtocol');
64+
test(U, U, U, U, 'TLSv1.2', 'TLS1_2_method',
65+
'ERR_TLS_PROTOCOL_VERSION_CONFLICT');
66+
test(U, U, U, 'TLSv1.2', U, 'TLS1_2_method',
67+
'ERR_TLS_PROTOCOL_VERSION_CONFLICT');
68+
test(U, 'TLSv1.2', 'TLS1_2_method', U, U, U,
69+
'ERR_TLS_PROTOCOL_VERSION_CONFLICT');
70+
test('TLSv1.2', U, 'TLS1_2_method', U, U, U,
71+
'ERR_TLS_PROTOCOL_VERSION_CONFLICT');
6872

6973
// TLS_method means "any supported protocol".
7074
test(U, U, 'TLSv1_2_method', U, U, 'TLS_method', 'TLSv1.2');

0 commit comments

Comments
 (0)