Skip to content

Commit c3efe72

Browse files
committed
tls: support Uint8Arrays for protocol list buffers
PR-URL: #11984 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 2dc1053 commit c3efe72

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

doc/api/tls.md

+34-14
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,10 @@ decrease overall server throughput.
752752
<!-- YAML
753753
added: v0.11.3
754754
changes:
755+
- version: REPLACEME
756+
pr-url: https://github.com/nodejs/node/pull/11984
757+
description: The `ALPNProtocols` and `NPNProtocols` options can
758+
be `Uint8Array`s now.
755759
- version: v5.3.0, v4.7.0
756760
pr-url: https://github.com/nodejs/node/pull/4246
757761
description: The `secureContext` option is supported now.
@@ -776,16 +780,18 @@ changes:
776780
against the list of supplied CAs. An `'error'` event is emitted if
777781
verification fails; `err.code` contains the OpenSSL error code. Defaults to
778782
`true`.
779-
* `NPNProtocols` {string[]|Buffer[]} An array of strings or `Buffer`s
780-
containing supported NPN protocols. `Buffer`s should have the format
781-
`[len][name][len][name]...` e.g. `0x05hello0x05world`, where the first
782-
byte is the length of the next protocol name. Passing an array is usually
783-
much simpler, e.g. `['hello', 'world']`.
784-
* `ALPNProtocols`: {string[]|Buffer[]} An array of strings or `Buffer`s
785-
containing the supported ALPN protocols. `Buffer`s should have the format
786-
`[len][name][len][name]...` e.g. `0x05hello0x05world`, where the first byte
787-
is the length of the next protocol name. Passing an array is usually much
788-
simpler: `['hello', 'world']`.)
783+
* `NPNProtocols` {string[]|Buffer[]|Uint8Array[]|Buffer|Uint8Array}
784+
An array of strings, Buffer`s or `Uint8Array`s, or a single `Buffer` or
785+
`Uint8Array` containing supported NPN protocols. `Buffer`s should have the
786+
format `[len][name][len][name]...` e.g. `0x05hello0x05world`, where the
787+
first byte is the length of the next protocol name. Passing an array is
788+
usually much simpler, e.g. `['hello', 'world']`.
789+
* `ALPNProtocols`: {string[]|Buffer[]|Uint8Array[]|Buffer|Uint8Array}
790+
An array of strings, `Buffer`s or `Uint8Array`s, or a single `Buffer` or
791+
`Uint8Array` containing the supported ALPN protocols. `Buffer`s should have
792+
the format `[len][name][len][name]...` e.g. `0x05hello0x05world`, where the
793+
first byte is the length of the next protocol name. Passing an array is
794+
usually much simpler, e.g. `['hello', 'world']`.
789795
* `servername`: {string} Server name for the SNI (Server Name Indication) TLS
790796
extension.
791797
* `checkServerIdentity(servername, cert)` {Function} A callback function
@@ -1002,6 +1008,10 @@ publicly trusted list of CAs as given in
10021008
<!-- YAML
10031009
added: v0.3.2
10041010
changes:
1011+
- version: REPLACEME
1012+
pr-url: https://github.com/nodejs/node/pull/11984
1013+
description: The `ALPNProtocols` and `NPNProtocols` options can
1014+
be `Uint8Array`s now.
10051015
- version: v5.0.0
10061016
pr-url: https://github.com/nodejs/node/pull/2564
10071017
description: ALPN options are supported now.
@@ -1018,10 +1028,20 @@ changes:
10181028
* `rejectUnauthorized` {boolean} If not `false` the server will reject any
10191029
connection which is not authorized with the list of supplied CAs. This
10201030
option only has an effect if `requestCert` is `true`. Defaults to `true`.
1021-
* `NPNProtocols` {string[]|Buffer} An array of strings or a `Buffer` naming
1022-
possible NPN protocols. (Protocols should be ordered by their priority.)
1023-
* `ALPNProtocols` {string[]|Buffer} An array of strings or a `Buffer` naming
1024-
possible ALPN protocols. (Protocols should be ordered by their priority.)
1031+
* `NPNProtocols` {string[]|Buffer[]|Uint8Array[]|Buffer|Uint8Array}
1032+
An array of strings, Buffer`s or `Uint8Array`s, or a single `Buffer` or
1033+
`Uint8Array` containing supported NPN protocols. `Buffer`s should have the
1034+
format `[len][name][len][name]...` e.g. `0x05hello0x05world`, where the
1035+
first byte is the length of the next protocol name. Passing an array is
1036+
usually much simpler, e.g. `['hello', 'world']`.
1037+
(Protocols should be ordered by their priority.)
1038+
* `ALPNProtocols`: {string[]|Buffer[]|Uint8Array[]|Buffer|Uint8Array}
1039+
An array of strings, `Buffer`s or `Uint8Array`s, or a single `Buffer` or
1040+
`Uint8Array` containing the supported ALPN protocols. `Buffer`s should have
1041+
the format `[len][name][len][name]...` e.g. `0x05hello0x05world`, where the
1042+
first byte is the length of the next protocol name. Passing an array is
1043+
usually much simpler, e.g. `['hello', 'world']`.
1044+
(Protocols should be ordered by their priority.)
10251045
When the server receives both NPN and ALPN extensions from the client,
10261046
ALPN takes precedence over NPN and the server does not send an NPN
10271047
extension to the client.

lib/tls.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const net = require('net');
2828
const url = require('url');
2929
const binding = process.binding('crypto');
3030
const Buffer = require('buffer').Buffer;
31+
const { isUint8Array } = process.binding('util');
3132

3233
// Allow {CLIENT_RENEG_LIMIT} client-initiated session renegotiations
3334
// every {CLIENT_RENEG_WINDOW} seconds. An error event is emitted if more
@@ -71,7 +72,7 @@ exports.convertNPNProtocols = function(protocols, out) {
7172
// If protocols is Array - translate it into buffer
7273
if (Array.isArray(protocols)) {
7374
out.NPNProtocols = convertProtocols(protocols);
74-
} else if (protocols instanceof Buffer) {
75+
} else if (isUint8Array(protocols)) {
7576
// Copy new buffer not to be modified by user.
7677
out.NPNProtocols = Buffer.from(protocols);
7778
}
@@ -81,7 +82,7 @@ exports.convertALPNProtocols = function(protocols, out) {
8182
// If protocols is Array - translate it into buffer
8283
if (Array.isArray(protocols)) {
8384
out.ALPNProtocols = convertProtocols(protocols);
84-
} else if (protocols instanceof Buffer) {
85+
} else if (isUint8Array(protocols)) {
8586
// Copy new buffer not to be modified by user.
8687
out.ALPNProtocols = Buffer.from(protocols);
8788
}

test/parallel/test-tls-basic-validations.js

+14
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ assert.throws(() => tls.createSecurePair({}),
5656
assert(buffer.equals(Buffer.from('abcd')));
5757
assert(out.NPNProtocols.equals(Buffer.from('efgh')));
5858
}
59+
60+
{
61+
const buffer = new Uint8Array(Buffer.from('abcd'));
62+
const out = {};
63+
tls.convertALPNProtocols(buffer, out);
64+
assert(out.ALPNProtocols.equals(Buffer.from('abcd')));
65+
}
66+
67+
{
68+
const buffer = new Uint8Array(Buffer.from('abcd'));
69+
const out = {};
70+
tls.convertNPNProtocols(buffer, out);
71+
assert(out.NPNProtocols.equals(Buffer.from('abcd')));
72+
}

0 commit comments

Comments
 (0)