Skip to content

Commit

Permalink
src: deduplicate setting RSA OAEP label
Browse files Browse the repository at this point in the history
This bit unfortunately involves manual memory management, so it is best
to only implement it once.

PR-URL: #44849
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
tniessen authored and danielleadams committed Oct 10, 2022
1 parent 3c653cf commit 0606f92
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
12 changes: 1 addition & 11 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -987,17 +987,7 @@ bool PublicKeyCipher::Cipher(
return false;
}

if (oaep_label.size() != 0) {
// OpenSSL takes ownership of the label, so we need to create a copy.
void* label = OPENSSL_memdup(oaep_label.data(), oaep_label.size());
CHECK_NOT_NULL(label);
if (0 >= EVP_PKEY_CTX_set0_rsa_oaep_label(ctx.get(),
static_cast<unsigned char*>(label),
oaep_label.size())) {
OPENSSL_free(label);
return false;
}
}
if (!SetRsaOaepLabel(ctx, oaep_label.ToByteSource())) return false;

size_t out_len = 0;
if (EVP_PKEY_cipher(
Expand Down
13 changes: 1 addition & 12 deletions src/crypto/crypto_rsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,7 @@ WebCryptoCipherStatus RSA_Cipher(
return WebCryptoCipherStatus::FAILED;
}

size_t label_len = params.label.size();
if (label_len > 0) {
void* label = OPENSSL_memdup(params.label.data<char>(), label_len);
CHECK_NOT_NULL(label);
if (EVP_PKEY_CTX_set0_rsa_oaep_label(
ctx.get(),
static_cast<unsigned char*>(label),
label_len) <= 0) {
OPENSSL_free(label);
return WebCryptoCipherStatus::FAILED;
}
}
if (!SetRsaOaepLabel(ctx, params.label)) return WebCryptoCipherStatus::FAILED;

size_t out_len = 0;
if (cipher(
Expand Down
15 changes: 15 additions & 0 deletions src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,21 @@ Maybe<bool> SetEncodedValue(
return target->Set(env->context(), name, value);
}

bool SetRsaOaepLabel(const EVPKeyCtxPointer& ctx, const ByteSource& label) {
if (label.size() != 0) {
// OpenSSL takes ownership of the label, so we need to create a copy.
void* label_copy = OPENSSL_memdup(label.data(), label.size());
CHECK_NOT_NULL(label_copy);
int ret = EVP_PKEY_CTX_set0_rsa_oaep_label(
ctx.get(), static_cast<unsigned char*>(label_copy), label.size());
if (ret <= 0) {
OPENSSL_free(label_copy);
return false;
}
}
return true;
}

CryptoJobMode GetCryptoJobMode(v8::Local<v8::Value> args) {
CHECK(args->IsUint32());
uint32_t mode = args.As<v8::Uint32>()->Value();
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ v8::Maybe<bool> SetEncodedValue(
const BIGNUM* bn,
int size = 0);

bool SetRsaOaepLabel(const EVPKeyCtxPointer& rsa, const ByteSource& label);

namespace Util {
void Initialize(Environment* env, v8::Local<v8::Object> target);
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
Expand Down

0 comments on commit 0606f92

Please sign in to comment.