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

BIGNUM utility cleanups #2325

Merged
merged 1 commit into from
Jun 24, 2024
Merged

BIGNUM utility cleanups #2325

merged 1 commit into from
Jun 24, 2024

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Jun 24, 2024

Eliminates some duplication and condenses some of the handling of BIGNUM types... just one more of a number of PRs that'll be reworking and cleaning up the crypto impl a bit before coalescing with the node.js crypto impl details.

@jasnell jasnell requested review from mikea and fhanau June 24, 2024 20:56
@jasnell jasnell requested review from a team as code owners June 24, 2024 20:56
@@ -293,6 +294,14 @@ const SslDisposer<T, sslFree> SslDisposer<T, sslFree>::INSTANCE;
// Using BN_clear_free here ensures that any potentially sensitive information in the
// BIGNUM is also cleansed when it is freed.

using UniqueBignum = std::unique_ptr<BIGNUM, void(*)(BIGNUM*)>;
kj::Maybe<kj::Own<BIGNUM>> toBignum(kj::ArrayPtr<const kj::byte> data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stylistical suggestion: ArrayPtr/Array have .as() method that is designed to help with things like ptr.as<Bignum>(). I found it very helpful for Rust conversions and makes code much easier to read.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps if we introduce a Bignum helper that wraps BIGNUM at some point. Given that this is mostly just shifting around the way the existing code already worked, I'd prefer to keep it as is for now but I'm going to be continuing to iterate on this area.

src/workerd/api/crypto/impl.h Outdated Show resolved Hide resolved
src/workerd/api/crypto/impl.c++ Outdated Show resolved Hide resolved
src/workerd/api/crypto/impl.c++ Outdated Show resolved Hide resolved
@@ -1065,8 +1055,8 @@ kj::Own<EVP_PKEY> rsaJwkReader(SubtleCrypto::JsonWebKey&& keyDataJwk) {
// RSA_set0_*() transfers BIGNUM ownership to the RSA key, so we don't need to worry about
// calling BN_free().
OSSLCALL(RSA_set0_key(rsaKey.get(),
BN_bin2bn(modulus.begin(), modulus.size(), nullptr),
BN_bin2bn(publicExponent.begin(), publicExponent.size(), nullptr),
toBignumUnowned(modulus),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should add T* kj::Own<T>::disown() rather than create separate functions for such use-cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was considering the same thing. The ability to release ownership is the only reason why I didn't eliminate the std::unique_ptr use here entirely.

src/workerd/api/crypto/asymmetric.c++ Show resolved Hide resolved
@jasnell jasnell force-pushed the jsnell/crypto-rework-step2 branch from 69f78a3 to 8acd220 Compare June 24, 2024 22:30
@jasnell jasnell merged commit b6c01df into main Jun 24, 2024
10 checks passed
ns476 pushed a commit to ns476/workerd that referenced this pull request Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants