Skip to content

Commit d11ee19

Browse files
codebytereBridgeAR
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 c75813a commit d11ee19

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/node_crypto.cc

+10-5
Original file line numberDiff line numberDiff line change
@@ -5200,7 +5200,7 @@ template <PublicKeyCipher::Operation operation,
52005200
bool PublicKeyCipher::Cipher(Environment* env,
52015201
const ManagedEVPPKey& pkey,
52025202
int padding,
5203-
const char* oaep_hash,
5203+
const EVP_MD* digest,
52045204
const unsigned char* data,
52055205
int len,
52065206
AllocatedBuffer* out) {
@@ -5212,9 +5212,8 @@ bool PublicKeyCipher::Cipher(Environment* env,
52125212
if (EVP_PKEY_CTX_set_rsa_padding(ctx.get(), padding) <= 0)
52135213
return false;
52145214

5215-
if (oaep_hash != nullptr) {
5216-
if (!EVP_PKEY_CTX_md(ctx.get(), EVP_PKEY_OP_TYPE_CRYPT,
5217-
EVP_PKEY_CTRL_RSA_OAEP_MD, oaep_hash))
5215+
if (digest != nullptr) {
5216+
if (!EVP_PKEY_CTX_set_rsa_oaep_md(ctx.get(), digest))
52185217
return false;
52195218
}
52205219

@@ -5256,6 +5255,12 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
52565255

52575256
const node::Utf8Value oaep_str(env->isolate(), args[offset + 2]);
52585257
const char* oaep_hash = args[offset + 2]->IsString() ? *oaep_str : nullptr;
5258+
const EVP_MD* digest = nullptr;
5259+
if (oaep_hash != nullptr) {
5260+
digest = EVP_get_digestbyname(oaep_hash);
5261+
if (digest == nullptr)
5262+
return THROW_ERR_OSSL_EVP_INVALID_DIGEST(env);
5263+
}
52595264

52605265
AllocatedBuffer out;
52615266

@@ -5265,7 +5270,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
52655270
env,
52665271
pkey,
52675272
padding,
5268-
oaep_hash,
5273+
digest,
52695274
buf.data(),
52705275
buf.length(),
52715276
&out);

src/node_crypto.h

+1-1
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

+2
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)