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

src: deduplicate setting RSA OAEP label #44849

Merged
merged 1 commit into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/crypto/crypto_cipher.cc
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
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
Expand Up @@ -653,6 +653,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
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