Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: Add PSK support (v9.0.0-pre) #14978

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fc041a8
tls: Applied PR #6701 to master, and updated outdated code snippets, …
taylorzane Aug 22, 2017
9e188bc
tls: Updated TLS-PSK Circuit test to pass all assertions.
taylorzane Aug 22, 2017
51db277
tls: Updated TLS documentation to detail that pskIdentity must be enc…
taylorzane Aug 22, 2017
09b9a8e
tls: Updated TLS-PSK tests to use `method() {}` syntax instead of `me…
taylorzane Aug 22, 2017
e954c49
tls: Updated SetPskIdentity to not throw when provided with too many …
taylorzane Aug 22, 2017
caadeaf
tls: Updated createSecureContext TLS-PSK logic to use `internal/error…
taylorzane Aug 22, 2017
45c120b
tls: Updated formatting to conform with eslint rules.
taylorzane Aug 22, 2017
eef89b8
tls: Added #14978 PR to the `changes:` section. Improved wording of p…
taylorzane Aug 22, 2017
6f095f8
tls: Updated tests to use the `common.skip` method when crypto is not…
taylorzane Aug 22, 2017
ba22b97
tls: Updated `onpskexchange` callback to use `ArrayBuffer.isView`. Cl…
taylorzane Aug 22, 2017
ba0727e
tls: Added `HandleScope` to PSK callbacks. Updated calls to `onpskexc…
taylorzane Aug 22, 2017
361b5ed
tls: Updated a few `assert` calls to use the proper `CHECK` call.
taylorzane Aug 22, 2017
49a285f
tls: Updated PSK callbacks to use `MaybeLocal` overload of `NewFromUt…
taylorzane Aug 22, 2017
16921ab
tls: Updated docs and tests to reflect changes to the TLS-PSK API.
taylorzane Aug 23, 2017
959887e
tls: Updated TLS-PSK API to only accept strings for `psk` and `identi…
taylorzane Aug 23, 2017
65cb351
tls: Updated TLS-PSK tests to not use `common.PORT` when possible. Ad…
taylorzane Aug 25, 2017
7b43283
tls: Updated TLS to check that TLS-PSK callback is a function. Update…
taylorzane Aug 25, 2017
bbbb658
tls: Updated `PSKClientCallback` to use a better method for casting a…
taylorzane Aug 25, 2017
5136c93
tls: Fixed linter error in _tls_common.js.
taylorzane Aug 25, 2017
6ed9f92
tls: Updated `PSKServerCallback` to use a better method for casting a…
taylorzane Aug 25, 2017
97a71bd
tls: Renamed `pskIdentity` to `pskIdentityHint`. Updated errors to us…
taylorzane Apr 13, 2018
b810f97
tls: Updated PSK callbacks to use the new syntax for getting the SSL …
taylorzane Apr 13, 2018
92fc64e
tls: Updated PSK tests to use new error messages, improved test asser…
taylorzane Apr 13, 2018
4880511
tls: Resolved lint errors.
taylorzane Apr 13, 2018
40fae68
tls: Updated PSK code to latest best practices.
taylorzane Apr 25, 2018
001b3bd
tls: Removed an unused env string.
taylorzane Apr 25, 2018
34e54c9
tls: Updated PSK functions to use automatic memory management.
taylorzane May 24, 2018
31fff46
tls: Add PSK support
taylorzane Aug 22, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ SNI (Server Name Indication) are TLS handshake extensions:
* SNI - Allows the use of one TLS server for multiple hostnames with different
SSL certificates.

### Pre-shared keys

<!-- type=misc -->

TLS-PSK support is also available as an alternative to normal certificate-based
authentication. TLS-PSK uses a pre-shared key instead of certificates to
authenticate a TLS connection, providing mutual authentication.

TLS-PSK and public key infrastructure are not mutually exclusive; clients and
servers can accommodate both, with the variety determined by the normal cipher
negotiation step.

Note that TLS-PSK is only a good choice where means exist to securely share a
key with every connecting machine, so it does not replace PKI for the majority
of TLS uses.

The TLS-PSK implementation in OpenSSL has also seen many security flaws in
recent years, mostly because it is used only by a minority of applications.
Please consider all alternative solutions before switching to PSK ciphers.

### Client-initiated renegotiation attack mitigation

<!-- type=misc -->
Expand Down Expand Up @@ -851,6 +871,9 @@ similar to:
<!-- YAML
added: v0.11.3
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/14978
description: The `pskCallback` option is now supported.
- version: v8.0.0
pr-url: https://github.com/nodejs/node/pull/12839
description: The `lookup` option is supported now.
Expand Down Expand Up @@ -884,6 +907,17 @@ changes:
verified against the list of supplied CAs. An `'error'` event is emitted if
verification fails; `err.code` contains the OpenSSL error code. **Default:**
`true`.
* `pskCallback(hint)` {Function} When negotiating TLS-PSK, this optional
function is called with the identity hint provided by the server. If the
client should continue to negotiate PSK ciphers, the return value of this
function must be an object in the form `{psk: <string>, identity:
<string>}`.
Note that PSK ciphers are disabled by default, and using TLS-PSK thus
requires explicitly specifying a cipher suite with the `ciphers` option.
Additionally, it may be necessary to disable `rejectUnauthorized` if a
client attempts to specify a certificate for the session.
Note that `psk` must be a hexadecimal string, `identity` must use UTF-8
encoding.
* `ALPNProtocols`: {string[]|Buffer[]|Uint8Array[]|Buffer|Uint8Array}
An array of strings, `Buffer`s or `Uint8Array`s, or a single `Buffer` or
`Uint8Array` containing the supported ALPN protocols. `Buffer`s should have
Expand Down Expand Up @@ -1168,7 +1202,8 @@ changes:
a 16-byte HMAC key, and a 16-byte AES key. This can be used to accept TLS
session tickets on multiple instances of the TLS server.
* ...: Any [`tls.createSecureContext()`][] option can be provided. For
servers, the identity options (`pfx` or `key`/`cert`) are usually required.
servers, the identity options (`pfx`, `key`/`cert` or `pskCallback`)
are usually required.
* `secureConnectionListener` {Function}

Creates a new [`tls.Server`][]. The `secureConnectionListener`, if provided, is
Expand Down
101 changes: 101 additions & 0 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const { parseCertString } = require('internal/tls');
const { isArrayBufferView } = require('internal/util/types');
const tls = require('tls');
const {
ERR_ASSERTION,
ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED,
ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes;
Expand Down Expand Up @@ -68,6 +69,83 @@ function validateKeyCert(name, value) {

exports.SecureContext = SecureContext;

function onpskexchange(pskCallback) {
return (identity, maxPskLen, maxIdentLen) => {
let ret = pskCallback(identity);
if (typeof ret !== 'object' || ret == null) {
ret = undefined;
}

if (ret) {
ret = { psk: ret.psk, identity: ret.identity };

if (!typeof ret.psk === 'string') {
throw new ERR_INVALID_ARG_TYPE('psk', ['string']);
}
if (ret.psk.length / 2 > maxPskLen) {
throw new ERR_ASSERTION(`Pre-shared key exceeds ${maxPskLen} bytes`);
}

// Only set for the client callback, which must provide an identity.
if (maxIdentLen) {
if (typeof ret.identity !== 'string') {
throw new ERR_INVALID_ARG_TYPE('identity', 'string');
}
if (ret.identity.length > maxIdentLen) {
throw new ERR_ASSERTION(`PSK identity exceeds ${maxIdentLen} bytes`);
}
}
}

return ret;
};
}

function ecdhCurveWarning() {
if (ecdhCurveWarning.emitted) return;
process.emitWarning('{ ecdhCurve: false } is deprecated.',
'DeprecationWarning',
'DEP0083');
ecdhCurveWarning.emitted = true;
}
ecdhCurveWarning.emitted = false;

function onpskexchange(pskCallback) {
return (identity, maxPskLen, maxIdentLen) => {
let ret = pskCallback(identity);
if (typeof ret !== 'object' || ret == null) {
ret = undefined;
}

if (ret) {
ret = { psk: ret.psk, identity: ret.identity };

if (!typeof ret.psk === 'string') {
throw new ERR_INVALID_ARG_TYPE('psk', ['string']);
}
if (ret.psk.length / 2 > maxPskLen) {
throw new ERR_ASSERTION(
`Pre-shared key exceeds ${maxPskLen} bytes`
);
}

// Only set for the client callback, which must provide an identity.
if (maxIdentLen) {
if (typeof ret.identity !== 'string') {
throw new ERR_INVALID_ARG_TYPE('identity',
'string');
}
if (ret.identity.length > maxIdentLen) {
throw new ERR_ASSERTION(
`PSK identity exceeds ${maxIdentLen} bytes`
);
}
}
}

return ret;
};
}

exports.createSecureContext = function createSecureContext(options, context) {
if (!options) options = {};
Expand Down Expand Up @@ -161,6 +239,29 @@ exports.createSecureContext = function createSecureContext(options, context) {
}
}

if (options.pskCallback && c.context.enablePskCallback) {
if (typeof options.pskCallback !== 'function') {
throw new ERR_INVALID_ARG_TYPE('pskCallback',
'function',
options.pskCallback);
}

c.context.onpskexchange = onpskexchange(options.pskCallback);
c.context.enablePskCallback();

if (options.pskIdentityHint) {
if (typeof options.pskIdentityHint !== 'string') {
throw new ERR_INVALID_ARG_TYPE(
'options.pskIdentityHint',
'string',
options.pskIdentityHint
);
}

c.context.setPskIdentity(options.pskIdentityHint);
}
}

if (options.sessionIdContext) {
c.context.setSessionIdContext(options.sessionIdContext);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ TLSSocket.prototype.getCipher = function(err) {
return null;
};

// TODO: support anonymous (nocert) and PSK
// TODO: support anonymous (nocert)


function onSocketSecure() {
Expand Down Expand Up @@ -846,6 +846,8 @@ function Server(options, listener) {
dhparam: this.dhparam,
secureProtocol: this.secureProtocol,
secureOptions: this.secureOptions,
pskCallback: this.pskCallback,
pskIdentityHint: this.pskIdentityHint,
honorCipherOrder: this.honorCipherOrder,
crl: this.crl,
sessionIdContext: this.sessionIdContext
Expand Down Expand Up @@ -934,6 +936,8 @@ Server.prototype.setOptions = function(options) {
else
this.honorCipherOrder = true;
if (secureOptions) this.secureOptions = secureOptions;
if (options.pskIdentityHint) this.pskIdentityHint = options.pskIdentityHint;
if (options.pskCallback) this.pskCallback = options.pskCallback;
if (options.ALPNProtocols)
tls.convertALPNProtocols(options.ALPNProtocols, this);
if (options.sessionIdContext) {
Expand Down
4 changes: 4 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ struct PackageConfig {
V(duration_string, "duration") \
V(emit_warning_string, "emitWarning") \
V(exchange_string, "exchange") \
V(identity_string, "identity") \
V(enablepush_string, "enablePush") \
V(encoding_string, "encoding") \
V(entries_string, "entries") \
V(entry_type_string, "entryType") \
Expand Down Expand Up @@ -223,6 +225,7 @@ struct PackageConfig {
V(onmessage_string, "onmessage") \
V(onnewsession_string, "onnewsession") \
V(onocspresponse_string, "onocspresponse") \
V(onpskexchange_string, "onpskexchange") \
V(ongoawaydata_string, "ongoawaydata") \
V(onorigin_string, "onorigin") \
V(onpriority_string, "onpriority") \
Expand Down Expand Up @@ -257,6 +260,7 @@ struct PackageConfig {
V(promise_string, "promise") \
V(pubkey_string, "pubkey") \
V(query_string, "query") \
V(psk_string, "psk") \
V(raw_string, "raw") \
V(read_host_object_string, "_readHostObject") \
V(readable_string, "readable") \
Expand Down
Loading