36 Commits

Author SHA1 Message Date
Luigi Pinca
2cff98c853
build: fix missing files warning
This commit fixes the following warning:

    Warning: Missing input files:
    C:\Users\lpinca\node\tools\v8_gypfiles\..\..\deps\v8\third_party\abseil-cpp\absl\strings\internal\has_absl_stringify.h
    C:\Users\lpinca\node\deps\ncrypto\dh-primes.h

https://github.com/nodejs/node/commit/bd3c25cf31282f101196 removed
`'<(ABSEIL_ROOT)/absl/strings/has_absl_stringify.h'` instead of
`'<(ABSEIL_ROOT)/absl/strings/internal/has_absl_stringify.h'`

Refs: https://github.com/nodejs/node/commit/5ab3140dfbf0dfa33a66
Refs: https://github.com/nodejs/node/commit/b9b4cb7731168671d8f4
PR-URL: https://github.com/nodejs/node/pull/57870
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2025-04-19 15:22:06 +00:00
Tobias Nießen
0a1d5d353b
crypto: revert dangerous uses of std::string_view
An `std::string_view v` is a `const char* v.data()` along with an
`std::size_t v.size()` that guarantees that `v.size()` contiguous
elements of type `char` can be accessed relative to the pointer
`v.data()`.

One of the main reasons behind the existence of  `std::string_view` is
the ability to operate on `char` sequences without requiring null
termination, which otherwise often requires expensive copies of strings
to be made. As a consequence, it is generally incorrect to assume that
`v.data()` points to a null-terminated sequence of `char`, and the only
way to obtain a null-terminated string from an `std::string_view` is to
make a copy. It is not even possible to check if the sequence pointed to
by `v.data()` is null-terminated because the null character would be at
position `v.data() + v.size()`, which is outside of the range that `v`
guarantees safe access to. (A default-constructed `std::string_view`
even sets its own data pointer to a `nullptr`, which is fine because it
only needs to guarantee safe access to zero elements, i.e., to no
elements).

In `deps/ncrypto` and `src/crypto`, there are various APIs that consume
`std::string_view v` arguments but then ignore `v.size()` and treat
`v.data()` as a C-style string of type `const char*`. However, that is
not what call sites would expect from functions that explicitly ask for
`std::string_view` arguments, since it makes assumptions beyond the
guarantees provided by `std::string_view` and leads to undefined
behavior unless the given view either contains an embedded null
character or the `char` at address `v.data() + v.size()` is a null
character. This is not a reasonable assumption for `std::string_view` in
general, and it also defeats the purpose of `std::string_view` for the
most part since, when `v.size()` is being ignored, it is essentially
just a `const char*`.

Constructing an `std::string_view` from a `const char*` is also not
"free" but requires computing the length of the C-style string (unless
the length can be computed at compile time, e.g., because the value is
just a string literal). Repeated conversion between `const char*` as
used by OpenSSL and `std::string_view` as used by ncrypto thus incurs
the additional overhead of computing the length of the string whenever
an `std::string_view` is constructed from a `const char*`. (This seems
negligible compared to the safety argument though.)

Similarly, returning a `const char*` pointer to a C-style string as an
`std::string_view` has two downsides: the function must compute the
length of the string in order to construct the view, and the caller
can no longer assume that the return value is null-terminated and thus
cannot pass the returned view to functions that require their arguments
to be null terminated. (And, for the reasons explained above, the caller
also cannot check if the value is null-terminated without potentially
invoking undefined behavior.)

C++20 unfortunately does not have a type similar to Rust's `CStr` or
GSL `czstring`. Therefore, this commit changes many occurrences of
`std::string_view` back to `const char*`, which is conventional for
null-terminated C-style strings and does not require computing the
length of strings.

There are _a lot_ of occurrences of `std::string_view` in ncrypto and
for each one, we need to evaluate if it is safe and a good abstraction.
I tried to do so, but I might have changed too few or too many, so
please feel free to give feedback on individual occurrences.

PR-URL: https://github.com/nodejs/node/pull/57816
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
2025-04-15 12:09:22 +00:00
Tobias Nießen
195ed4a708 crypto: make auth tag size assumption explicit
The `CipherBase` class assumes that any authentication tag will fit into
`EVP_GCM_TLS_TAG_LEN` bytes, which is true because Node.js only supports
GCM with AES as the blocker cipher, and the block size of AES happens to
be 16 bytes, which coincidentally is also the output size of the
Poly1305 construction used by ChaCha20-Poly1305 as well as the maximum
size of authentication tags produced by AES in CCM or OCB mode.

This commit adds a new constant `ncrypto::Cipher::MAX_AUTH_TAG_LENGTH`
which is the maximum length of authentication tags produced by
algorithms that Node.js supports and replaces some constants in
`CipherBase` with semantically more meaningful named constants.

The OpenSSL team is debating whether a constant like
`MAX_AUTH_TAG_LENGTH` (`EVP_MAX_AEAD_TAG_LENGTH`) should exist at all
since its value necessarily depends on the set of AEAD algorithms
supported, but I do believe that, for Node.js, this is a step in the
right direction. It certainly makes more sense than to use the AES-GCM
tag size as defined by TLS.

PR-URL: https://github.com/nodejs/node/pull/57803
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2025-04-12 11:25:36 -07:00
Shelley Vohr
b9b4cb7731
crypto: remove BoringSSL dh-primes addition
PR-URL: https://github.com/nodejs/node/pull/57023
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2025-04-07 23:27:14 +02:00
Filip Skokan
12b81dfc93
crypto: fix output of privateDecrypt with zero-length data
closes #57553
closes #57572
closes #57558

PR-URL: https://github.com/nodejs/node/pull/57575
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
2025-04-01 06:29:29 +00:00
James M Snell
b71267e20c src: cleanup crypto more
* Use ncrypto APIs where appropriate
* Remove obsolete no-longer used functions
* Improve error handling a bit
* move secure heap handling to ncrypto
  To simplify handling of boringssl/openssl, move secure
  heap impl to ncrypto. Overall the reduces the complexity
  of the code in crypto_util by eliminating additional
  ifdef branches.
* simplify CryptoErrorStore::ToException a bit
* simplify error handling in crypto_common
* move curve utility methods to ncrypto
* verify that released DataPointers aren't on secure heap
  The ByteSource does not currently know how to free a DataPointer
  allocated on the secure heap, so just verify.
  DataPointers on the secure heap are not something that users can
  allocate on their own. Their use is rare. Eventually ByteSource
  is going to be refactored around ncrypto APIs so these additional
  checks should be temporary.
* simplify some ifdefs that are covered by ncrypto
* cleanup some obsolete includes in crypto_util

PR-URL: https://github.com/nodejs/node/pull/57323
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-03-14 07:09:49 -07:00
James M Snell
3329efecb1 src: refine ncrypto more
An eventual goal for ncrypto is to completely abstract away
details of working directly with openssl in order to make it
easier to work with multiple different openssl/boringssl versions.
As part of that we want to move away from direct reliance on
specific openssl APIs in the runtime and instead go through
the ncrypto abstractions. Not only does this help other
runtimes trying to be compatible with Node.js, but it helps
Node.js also by reducing the complexity of the crypto code
in Node.js itself.

PR-URL: https://github.com/nodejs/node/pull/57300
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-03-13 08:40:12 -07:00
James M Snell
206ebeb447 src, deps: port electron's boringssl workarounds
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/56812
Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-02-03 17:12:10 -08:00
James M Snell
c21d576579 src: clean up some obsolete crypto methods
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/56792
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-02-03 08:58:24 -08:00
James M Snell
e0a71517fe src: move more crypto to ncrypto
PR-URL: https://github.com/nodejs/node/pull/56653
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-01-26 05:47:52 -08:00
James M Snell
a5ed762d82
deps: fixup some minor coverity warnings
Fixes: https://github.com/nodejs/node/issues/56611
PR-URL: https://github.com/nodejs/node/pull/56612
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
2025-01-19 16:55:54 +00:00
James M Snell
294abc2ffc src: update ECKeyPointer in ncrypto
PR-URL: https://github.com/nodejs/node/pull/56526
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2025-01-14 23:54:40 +00:00
James M Snell
08fa9edcaf src: update ECPointPointer in ncrypto
PR-URL: https://github.com/nodejs/node/pull/56526
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2025-01-14 23:54:40 +00:00
James M Snell
d3cb7c0b96 src: update ECGroupPointer in ncrypto
PR-URL: https://github.com/nodejs/node/pull/56526
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2025-01-14 23:54:39 +00:00
James M Snell
6e23885d18 src: update ECDASSigPointer implementation in ncrypto
PR-URL: https://github.com/nodejs/node/pull/56526
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2025-01-14 23:54:39 +00:00
James M Snell
6879ab9b89 src: cleaning up more crypto internals for ncrypto
PR-URL: https://github.com/nodejs/node/pull/56526
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2025-01-14 23:54:38 +00:00
Cheng
8ca2956235
crypto: fix warning of ignoring return value
PR-URL: https://github.com/nodejs/node/pull/56527
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2025-01-11 14:55:30 +00:00
James M Snell
c0ad1937d0 src: move more crypto impl detail to ncrypto dep
* remove obsolete LogSecret function
* move StackOfX509 decl to ncrypto
* colocate GetSSLOCSPResponse with callsite
* move x509 error code and reason to ncrypto
* simplify X509Pointer/X509View pointer derefs a bit
* move prime gen and checks to ncrypto
* move BN_* methods into ncrypto
* move SSLPointer impl to ncrypto
* move SSLCtxPointer impl to ncrypto
* move EVP_CIPHER methods to ncrypto
* move CipherCtx methods to ncrypto

PR-URL: https://github.com/nodejs/node/pull/56421
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-01-08 06:52:34 -08:00
Shelley Vohr
d3bcb97e5a
build: use variable for crypto dep path
PR-URL: https://github.com/nodejs/node/pull/55928
Reviewed-By: Cheng Zhao <zcbenz@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2024-11-23 11:06:02 +00:00
Aviv Keller
e7991e8da6
build: apply cpp linting and formatting to ncrypto
PR-URL: https://github.com/nodejs/node/pull/55362
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-11-10 17:40:16 +00:00
James M Snell
5b9bf39b47
src: move more key related stuff to ncrypto
PR-URL: https://github.com/nodejs/node/pull/55368
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-11-02 15:11:28 +00:00
Shelley Vohr
99c6e4e6e4
crypto: include openssl/rand.h explicitly
PR-URL: https://github.com/nodejs/node/pull/55425
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2024-10-19 14:42:29 +00:00
James M Snell
ac53a5b29d
src: move more key handling to ncrypto
PR-URL: https://github.com/nodejs/node/pull/55108
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
2024-09-28 17:56:05 +00:00
James M Snell
c4681d55ae src: move evp stuff to ncrypto
PR-URL: https://github.com/nodejs/node/pull/54911
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-09-23 04:20:59 -07:00
Andrew Moon
29f31c6a76
crypto: add Date fields for validTo and validFrom
Added equivalent fields to `X509Certificate` in Date form.

PR-URL: https://github.com/nodejs/node/pull/54159
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
2024-09-18 00:56:24 +00:00
James M Snell
dcc2ed944f src: move hkdf, scrypto, pbkdf2 impl to ncrypto
PR-URL: https://github.com/nodejs/node/pull/54651
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
2024-09-06 19:44:26 -07:00
Cheng
d6f523480b
deps: fix sign-compare warning in ncrypto
PR-URL: https://github.com/nodejs/node/pull/54624
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
2024-08-31 02:10:36 +00:00
James M Snell
6bf7b6e342 src: move more crypto_dh.cc code to ncrypto
Update deps/ncrypto/ncrypto.cc

PR-URL: https://github.com/nodejs/node/pull/54459
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-08-27 07:27:12 -07:00
James M Snell
561bc87c76 src: move more crypto code to ncrypto
PR-URL: https://github.com/nodejs/node/pull/54320
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-08-19 07:05:13 -07:00
James M Snell
62611874d2 src: shift even moar x509 to ncrypto
PR-URL: https://github.com/nodejs/node/pull/54340
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2024-08-14 14:01:30 -07:00
James M Snell
5f230d2cf4 src: move some X509Certificate stuff to ncrypto
PR-URL: https://github.com/nodejs/node/pull/54241
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2024-08-12 11:39:45 -07:00
Cheng
4dae01ba2c
deps: fix GN build warning in ncrypto
PR-URL: https://github.com/nodejs/node/pull/54222
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2024-08-11 17:54:31 +00:00
James M Snell
926503b669 src: shift more crypto impl details to ncrypto
PR-URL: https://github.com/nodejs/node/pull/54028
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-08-05 14:01:21 -07:00
James M Snell
cf21220893 src: move spkac methods to ncrypto
PR-URL: https://github.com/nodejs/node/pull/53985
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-08-05 07:37:31 -07:00
Cheng
3de7a4c374
deps: add gn build files for ncrypto
PR-URL: https://github.com/nodejs/node/pull/53940
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-07-25 02:39:01 +00:00
James M Snell
efe5b81df9 deps: start working on ncrypto dep
Start moving src/crypto functionality out to a separate dep that
can be shared with other projects that need to emulate Node.js
crypto behavior.

PR-URL: https://github.com/nodejs/node/pull/53803
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-07-18 06:56:53 -07:00