From 845633a7c62ad9ffd33880a43e05a49382bb76bb Mon Sep 17 00:00:00 2001 From: "Jose M. Palacios Diaz" Date: Mon, 6 Nov 2017 17:22:42 -0500 Subject: [PATCH] crypto: better docs for cases where peer's public key is invalid changes in c++ are in the computeSecret function, but the thrown exception that was moved to JS land was in BufferToPoint function, here i let the allocation error be thrown so the only value returned is the nullptr that i use later to catch the error in computeSecret, to then construct the exception in JS land. an ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY error was added to errors.js and with that, subsequent changes to docs and tests were made. PR-URL: https://github.com/nodejs/node/pull/16849 Refs: https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf Fixes: https://github.com/nodejs/node/issues/16625 Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis --- doc/api/crypto.md | 12 +++++++++++- doc/api/errors.md | 7 +++++++ lib/internal/crypto/diffiehellman.js | 2 ++ lib/internal/errors.js | 2 ++ src/node_crypto.cc | 7 +++++-- test/parallel/test-crypto-dh.js | 20 ++++++++++++++------ 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 9d6e344667520e..a5619b92df36fa 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -651,7 +651,11 @@ added: v0.11.14 changes: - version: v6.0.0 pr-url: https://github.com/nodejs/node/pull/5522 - description: The default `inputEncoding` changed from `binary` to `utf8`. + description: The default `inputEncoding` changed from `binary` to `utf8` + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/16849 + description: Changed error format to better support invalid public key + error --> - `otherPublicKey` {string | Buffer | TypedArray | DataView} - `inputEncoding` {string} @@ -668,6 +672,12 @@ provided, `otherPublicKey` is expected to be a [`Buffer`][], `TypedArray`, or If `outputEncoding` is given a string will be returned; otherwise a [`Buffer`][] is returned. +`ecdh.computeSecret` will throw an +`ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY` error when `otherPublicKey` +lies outside of the elliptic curve. Since `otherPublicKey` is +usually supplied from a remote user over an insecure network, +its recommended for developers to handle this exception accordingly. + ### ecdh.generateKeys([encoding[, format]])