crypto: allow to restrict valid GCM tag length#20039
crypto: allow to restrict valid GCM tag length#20039tniessen wants to merge 2 commits intonodejs:masterfrom
Conversation
This change allows users to restrict accepted GCM authentication tag lengths to a single value. Fixes: nodejs#17523
|
@nodejs/crypto |
src/node_crypto.cc
Outdated
| char msg[50]; | ||
| snprintf(msg, sizeof(msg), | ||
| "Invalid GCM authentication tag length: %u", auth_tag_len); | ||
| env()->ThrowError(msg); |
There was a problem hiding this comment.
Would be ideal if the error can be thrown in JS but that's not necessary for landing.
There was a problem hiding this comment.
I hope to have some time between the release of node 10 and node 11 to refactor the whole cipher API to use the new error system, this PR aims for consistency with the rest :)
| @@ -2911,7 +2930,8 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) { | |||
| unsigned int tag_len = Buffer::Length(args[0]); | |||
| const int mode = EVP_CIPHER_CTX_mode(cipher->ctx_); | |||
| if (mode == EVP_CIPH_GCM_MODE) { | |||
There was a problem hiding this comment.
@tniessen I know your change is for GCM. I just think what's the minimal check we should do for CCM? Something like:
(cipher->auth_tag_len_ >= 0 && cipher->auth_tag_len_ != tag_len) || tag_len > EVP_GCM_TLS_TAG_LEN
|
It would be great if @bnoordhuis could take a look at this before landing. |
src/node_crypto.cc
Outdated
|
|
||
|
|
||
| static bool IsValidGCMTagLength(int tag_len) { | ||
| return tag_len == 4 || tag_len == 8 || (tag_len >= 12 && tag_len <= 16); |
There was a problem hiding this comment.
Micro-nit: superfluous parens.
src/node_crypto.cc
Outdated
| const int mode = EVP_CIPHER_CTX_mode(cipher->ctx_); | ||
| if (mode == EVP_CIPH_GCM_MODE) { | ||
| if (tag_len > 16 || (tag_len < 12 && tag_len != 8 && tag_len != 4)) { | ||
| if ((cipher->auth_tag_len_ >= 0 && cipher->auth_tag_len_ != tag_len) || |
src/node_crypto.h
Outdated
| const CipherKind kind_; | ||
| bool auth_tag_set_; | ||
| unsigned int auth_tag_len_; | ||
| int auth_tag_len_; |
There was a problem hiding this comment.
I'd prefer using some unsigned sentinel value, e.g.:
static const unsigned kNoAuthTag = static_cast<unsigned>(-1);|
Thanks @bnoordhuis, hope it is okay now! :) |
bnoordhuis
left a comment
There was a problem hiding this comment.
LGTM. It would be slightly nicer still to convert to unsigned / the sentinel immediately when converting from v8::Int32 to int.
|
Landed in 358d8ff. I will address your suggestion in another PR, @bnoordhuis. |
|
This does not land cleanly in v10.x-staging because it appears to depend on one of the semver-major's that landed last week that will not be making it in to 10.0.0. A backport PR may allow it to land in 10.x but it won't make it in time for 10.0.0 |
Add parentheses, like my compiler suggests. In the `IsValidGCMTagLength()` case I’m pretty sure that this grouping is the correct one, because it matches the earlier code; in the other case it would be good to have explicit confirmation. Refs: nodejs#20039
Notable Changes: * **addons**: - Fixed a memory leak for users of `AsyncResource` and N-API. (Michael Dawson) [#20668](#20668) * **assert**: - The `error` parameter of `assert.throws()` can be an object containing regular expressions now. (Ruben Bridgewater) [#20485](#20485) * **crypto**: - The `authTagLength` option has been made more flexible. (Tobias Nießen) [#20235](#20235), [#20039](#20039) * **http**: - Handling of `close` and `aborted` events has been made more consistent. (Robert Nagy) [#20075](#20075), [#20611](#20611) * Embedder support: - Functions for creating V8 `Isolate` and `Context` objects with Node.js-specific behaviour have been added to the API. (helloshuangzi) [#20639](#20639) - Node.js `Environment`s clean up resources before exiting now. (Anna Henningsen) [#19377](#19377) - Support for multi-threaded embedding has been improved. (Anna Henningsen) [#20542](#20542), [#20539](#20539), [#20541](#20541) * **timers**: - `timeout.refresh()` has been added to the public API. (Jeremiah Senkpiel) [#20298](#20298) PR-URL: #20724
Notable Changes: * **addons**: - Fixed a memory leak for users of `AsyncResource` and N-API. (Michael Dawson) [#20668](#20668) * **assert**: - The `error` parameter of `assert.throws()` can be an object containing regular expressions now. (Ruben Bridgewater) [#20485](#20485) * **crypto**: - The `authTagLength` option has been made more flexible. (Tobias Nießen) [#20235](#20235), [#20039](#20039) * **http**: - Handling of `close` and `aborted` events has been made more consistent. (Robert Nagy) [#20075](#20075), [#20611](#20611) * Embedder support: - Functions for creating V8 `Isolate` and `Context` objects with Node.js-specific behaviour have been added to the API. (helloshuangzi) [#20639](#20639) - Node.js `Environment`s clean up resources before exiting now. (Anna Henningsen) [#19377](#19377) - Support for multi-threaded embedding has been improved. (Anna Henningsen) [#20542](#20542), [#20539](#20539), [#20541](#20541) * **esm**: - Builtin modules (e.g. `fs`) now provide named exports in ES6 modules. (Gus Caplan) [#20403](#20403) * **timers**: - `timeout.refresh()` has been added to the public API. (Jeremiah Senkpiel) [#20298](#20298) PR-URL: #20724
Notable Changes: * **addons**: - Fixed a memory leak for users of `AsyncResource` and N-API. (Michael Dawson) [#20668](#20668) * **assert**: - The `error` parameter of `assert.throws()` can be an object containing regular expressions now. (Ruben Bridgewater) [#20485](#20485) * **crypto**: - The `authTagLength` option has been made more flexible. (Tobias Nießen) [#20235](#20235), [#20039](#20039) * **http**: - Handling of `close` and `aborted` events has been made more consistent. (Robert Nagy) [#20075](#20075), [#20611](#20611) * Embedder support: - Functions for creating V8 `Isolate` and `Context` objects with Node.js-specific behaviour have been added to the API. (Allen Yonghuang Wang) [#20639](#20639) - Node.js `Environment`s clean up resources before exiting now. (Anna Henningsen) [#19377](#19377) - Support for multi-threaded embedding has been improved. (Anna Henningsen) [#20542](#20542), [#20539](#20539), [#20541](#20541) * **esm**: - Builtin modules (e.g. `fs`) now provide named exports in ES6 modules. (Gus Caplan) [#20403](#20403) * **timers**: - `timeout.refresh()` has been added to the public API. (Jeremiah Senkpiel) [#20298](#20298) PR-URL: #20724
* addons:
- Fixed a memory leak for users of `AsyncResource` and N-API.
(Michael Dawson)
#20668
* assert:
- The `error` parameter of `assert.throws()` can be an object containing
regular expressions now. (Ruben Bridgewater)
#20485
* crypto:
- The `authTagLength` option has been made more flexible (Tobias Nießen)
#20235)
#20039
* esm:
- Builtin modules (e.g. `fs`) now provide named exports in ES6 modules.
(Gus Caplan)
#20403
* http:
- Handling of `close` and `aborted` events has been made more consistent.
(Robert Nagy)
#20075
#20611
* module:
- add --preserve-symlinks-main (David Goldstein)
#19911
* timers:
- `timeout.refresh()` has been added to the public API.
(Jeremiah Senkpiel)
#20298
* Embedder support:
- Functions for creating V8 `Isolate` and `Context` objects with
Node.js-specific behaviour have been added to the API.
(Allen Yonghuang Wang)
#20639
- Node.js `Environment`s clean up resources before exiting now.
(Anna Henningsen)
#19377
- Support for multi-threaded embedding has been improved.
(Anna Henningsen)
#20542
#20539
#20541
PR-URL: #20724
* addons:
- Fixed a memory leak for users of `AsyncResource` and N-API.
(Michael Dawson)
#20668
* assert:
- The `error` parameter of `assert.throws()` can be an object containing
regular expressions now. (Ruben Bridgewater)
#20485
* crypto:
- The `authTagLength` option has been made more flexible (Tobias Nießen)
#20235)
#20039
* esm:
- Builtin modules (e.g. `fs`) now provide named exports in ES6 modules.
(Gus Caplan)
#20403
* http:
- Handling of `close` and `aborted` events has been made more consistent.
(Robert Nagy)
#20075
#20611
* module:
- add --preserve-symlinks-main (David Goldstein)
#19911
* timers:
- `timeout.refresh()` has been added to the public API.
(Jeremiah Senkpiel)
#20298
* Embedder support:
- Functions for creating V8 `Isolate` and `Context` objects with
Node.js-specific behaviour have been added to the API.
(Allen Yonghuang Wang)
#20639
- Node.js `Environment`s clean up resources before exiting now.
(Anna Henningsen)
#19377
- Support for multi-threaded embedding has been improved.
(Anna Henningsen)
#20542
#20539
#20541
PR-URL: #20724
* addons:
- Fixed a memory leak for users of `AsyncResource` and N-API.
(Michael Dawson)
#20668
* assert:
- The `error` parameter of `assert.throws()` can be an object containing
regular expressions now. (Ruben Bridgewater)
#20485
* crypto:
- The `authTagLength` option has been made more flexible (Tobias Nießen)
#20235)
#20039
* esm:
- Builtin modules (e.g. `fs`) now provide named exports in ES6 modules.
(Gus Caplan)
#20403
* http:
- Handling of `close` and `aborted` events has been made more consistent.
(Robert Nagy)
#20075
#20611
* module:
- add --preserve-symlinks-main (David Goldstein)
#19911
* timers:
- `timeout.refresh()` has been added to the public API.
(Jeremiah Senkpiel)
#20298
* Embedder support:
- Functions for creating V8 `Isolate` and `Context` objects with
Node.js-specific behaviour have been added to the API.
(Allen Yonghuang Wang)
#20639
- Node.js `Environment`s clean up resources before exiting now.
(Anna Henningsen)
#19377
- Support for multi-threaded embedding has been improved.
(Anna Henningsen)
#20542
#20539
#20541
PR-URL: #20724
* addons:
- Fixed a memory leak for users of `AsyncResource` and N-API.
(Michael Dawson)
#20668
* assert:
- The `error` parameter of `assert.throws()` can be an object containing
regular expressions now. (Ruben Bridgewater)
#20485
* crypto:
- The `authTagLength` option has been made more flexible (Tobias Nießen)
#20235)
#20039
* esm:
- Builtin modules (e.g. `fs`) now provide named exports in ES6 modules.
(Gus Caplan)
#20403
* http:
- Handling of `close` and `aborted` events has been made more consistent.
(Robert Nagy)
#20075
#20611
* module:
- add --preserve-symlinks-main (David Goldstein)
#19911
* timers:
- `timeout.refresh()` has been added to the public API.
(Jeremiah Senkpiel)
#20298
* Embedder support:
- Functions for creating V8 `Isolate` and `Context` objects with
Node.js-specific behaviour have been added to the API.
(Allen Yonghuang Wang)
#20639
- Node.js `Environment`s clean up resources before exiting now.
(Anna Henningsen)
#19377
- Support for multi-threaded embedding has been improved.
(Anna Henningsen)
#20542
#20539
#20541
PR-URL: #20724
This change allows users to restrict accepted GCM authentication tag lengths to a single value as proposed in #17523 (comment). This relies on the API changes introduced in #18138.
Fixes: #17523
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes