Skip to content

Commit 0606f92

Browse files
tniessendanielleadams
authored andcommittedOct 10, 2022
src: deduplicate setting RSA OAEP label
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>
1 parent 3c653cf commit 0606f92

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed
 

‎src/crypto/crypto_cipher.cc

+1-11
Original file line numberDiff line numberDiff line change
@@ -987,17 +987,7 @@ bool PublicKeyCipher::Cipher(
987987
return false;
988988
}
989989

990-
if (oaep_label.size() != 0) {
991-
// OpenSSL takes ownership of the label, so we need to create a copy.
992-
void* label = OPENSSL_memdup(oaep_label.data(), oaep_label.size());
993-
CHECK_NOT_NULL(label);
994-
if (0 >= EVP_PKEY_CTX_set0_rsa_oaep_label(ctx.get(),
995-
static_cast<unsigned char*>(label),
996-
oaep_label.size())) {
997-
OPENSSL_free(label);
998-
return false;
999-
}
1000-
}
990+
if (!SetRsaOaepLabel(ctx, oaep_label.ToByteSource())) return false;
1001991

1002992
size_t out_len = 0;
1003993
if (EVP_PKEY_cipher(

‎src/crypto/crypto_rsa.cc

+1-12
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,7 @@ WebCryptoCipherStatus RSA_Cipher(
221221
return WebCryptoCipherStatus::FAILED;
222222
}
223223

224-
size_t label_len = params.label.size();
225-
if (label_len > 0) {
226-
void* label = OPENSSL_memdup(params.label.data<char>(), label_len);
227-
CHECK_NOT_NULL(label);
228-
if (EVP_PKEY_CTX_set0_rsa_oaep_label(
229-
ctx.get(),
230-
static_cast<unsigned char*>(label),
231-
label_len) <= 0) {
232-
OPENSSL_free(label);
233-
return WebCryptoCipherStatus::FAILED;
234-
}
235-
}
224+
if (!SetRsaOaepLabel(ctx, params.label)) return WebCryptoCipherStatus::FAILED;
236225

237226
size_t out_len = 0;
238227
if (cipher(

‎src/crypto/crypto_util.cc

+15
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,21 @@ Maybe<bool> SetEncodedValue(
654654
return target->Set(env->context(), name, value);
655655
}
656656

657+
bool SetRsaOaepLabel(const EVPKeyCtxPointer& ctx, const ByteSource& label) {
658+
if (label.size() != 0) {
659+
// OpenSSL takes ownership of the label, so we need to create a copy.
660+
void* label_copy = OPENSSL_memdup(label.data(), label.size());
661+
CHECK_NOT_NULL(label_copy);
662+
int ret = EVP_PKEY_CTX_set0_rsa_oaep_label(
663+
ctx.get(), static_cast<unsigned char*>(label_copy), label.size());
664+
if (ret <= 0) {
665+
OPENSSL_free(label_copy);
666+
return false;
667+
}
668+
}
669+
return true;
670+
}
671+
657672
CryptoJobMode GetCryptoJobMode(v8::Local<v8::Value> args) {
658673
CHECK(args->IsUint32());
659674
uint32_t mode = args.As<v8::Uint32>()->Value();

‎src/crypto/crypto_util.h

+2
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ v8::Maybe<bool> SetEncodedValue(
791791
const BIGNUM* bn,
792792
int size = 0);
793793

794+
bool SetRsaOaepLabel(const EVPKeyCtxPointer& rsa, const ByteSource& label);
795+
794796
namespace Util {
795797
void Initialize(Environment* env, v8::Local<v8::Object> target);
796798
void RegisterExternalReferences(ExternalReferenceRegistry* registry);

0 commit comments

Comments
 (0)