Skip to content

Commit 17a697c

Browse files
codebytereryzokuken
authored andcommitted
crypto: don't expose openssl internals
PR-URL: #29325 Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 8675152 commit 17a697c

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/node_crypto.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5203,7 +5203,7 @@ template <PublicKeyCipher::Operation operation,
52035203
bool PublicKeyCipher::Cipher(Environment* env,
52045204
const ManagedEVPPKey& pkey,
52055205
int padding,
5206-
const char* oaep_hash,
5206+
const EVP_MD* digest,
52075207
const unsigned char* data,
52085208
int len,
52095209
AllocatedBuffer* out) {
@@ -5215,9 +5215,8 @@ bool PublicKeyCipher::Cipher(Environment* env,
52155215
if (EVP_PKEY_CTX_set_rsa_padding(ctx.get(), padding) <= 0)
52165216
return false;
52175217

5218-
if (oaep_hash != nullptr) {
5219-
if (!EVP_PKEY_CTX_md(ctx.get(), EVP_PKEY_OP_TYPE_CRYPT,
5220-
EVP_PKEY_CTRL_RSA_OAEP_MD, oaep_hash))
5218+
if (digest != nullptr) {
5219+
if (!EVP_PKEY_CTX_set_rsa_oaep_md(ctx.get(), digest))
52215220
return false;
52225221
}
52235222

@@ -5259,6 +5258,12 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
52595258

52605259
const node::Utf8Value oaep_str(env->isolate(), args[offset + 2]);
52615260
const char* oaep_hash = args[offset + 2]->IsString() ? *oaep_str : nullptr;
5261+
const EVP_MD* digest = nullptr;
5262+
if (oaep_hash != nullptr) {
5263+
digest = EVP_get_digestbyname(oaep_hash);
5264+
if (digest == nullptr)
5265+
return THROW_ERR_OSSL_EVP_INVALID_DIGEST(env);
5266+
}
52625267

52635268
AllocatedBuffer out;
52645269

@@ -5268,7 +5273,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
52685273
env,
52695274
pkey,
52705275
padding,
5271-
oaep_hash,
5276+
digest,
52725277
buf.data(),
52735278
buf.length(),
52745279
&out);

src/node_crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ class PublicKeyCipher {
713713
static bool Cipher(Environment* env,
714714
const ManagedEVPPKey& pkey,
715715
int padding,
716-
const char* oaep_hash,
716+
const EVP_MD* digest,
717717
const unsigned char* data,
718718
int len,
719719
AllocatedBuffer* out);

src/node_errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void PrintErrorString(const char* format, ...);
4242
V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \
4343
V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \
4444
V(ERR_INVALID_ARG_VALUE, TypeError) \
45+
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \
4546
V(ERR_INVALID_ARG_TYPE, TypeError) \
4647
V(ERR_INVALID_MODULE_SPECIFIER, TypeError) \
4748
V(ERR_INVALID_PACKAGE_CONFIG, SyntaxError) \
@@ -89,6 +90,7 @@ void PrintErrorString(const char* format, ...);
8990
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
9091
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
9192
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
93+
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
9294
V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, \
9395
"MessagePort was found in message but not listed in transferList") \
9496
V(ERR_MISSING_PLATFORM_FOR_WORKER, \

0 commit comments

Comments
 (0)