Skip to content

Commit 7c6f4be

Browse files
committed
crypto: do not advertise unsupported algorithms
Fixes: #41857
1 parent 92b85e7 commit 7c6f4be

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/crypto/crypto_cipher.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,15 @@ void CipherBase::GetSSLCiphers(const FunctionCallbackInfo<Value>& args) {
235235

236236
void CipherBase::GetCiphers(const FunctionCallbackInfo<Value>& args) {
237237
Environment* env = Environment::GetCurrent(args);
238+
MarkPopErrorOnReturn mark_pop_error_on_return;
238239
CipherPushContext ctx(env);
239-
EVP_CIPHER_do_all_sorted(array_push_back<EVP_CIPHER>, &ctx);
240+
EVP_CIPHER_do_all_sorted(
241+
array_push_back<EVP_CIPHER,
242+
EVP_CIPHER_fetch,
243+
EVP_CIPHER_free,
244+
EVP_get_cipherbyname,
245+
EVP_CIPHER_get0_name>,
246+
&ctx);
240247
args.GetReturnValue().Set(ctx.ToJSArray());
241248
}
242249

src/crypto/crypto_hash.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ void Hash::MemoryInfo(MemoryTracker* tracker) const {
3535

3636
void Hash::GetHashes(const FunctionCallbackInfo<Value>& args) {
3737
Environment* env = Environment::GetCurrent(args);
38+
MarkPopErrorOnReturn mark_pop_error_on_return;
3839
CipherPushContext ctx(env);
39-
EVP_MD_do_all_sorted(array_push_back<EVP_MD>, &ctx);
40+
EVP_MD_do_all_sorted(
41+
array_push_back<EVP_MD,
42+
EVP_MD_fetch,
43+
EVP_MD_free,
44+
EVP_get_digestbyname,
45+
EVP_MD_get0_name>,
46+
&ctx);
4047
args.GetReturnValue().Set(ctx.ToJSArray());
4148
}
4249

src/crypto/crypto_util.h

+28-2
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,37 @@ class CipherPushContext {
616616
Environment* env_;
617617
};
618618

619-
template <class TypeName>
620-
void array_push_back(const TypeName* md,
619+
template <class TypeName,
620+
TypeName* fetch_type(OSSL_LIB_CTX*, const char*, const char*),
621+
void free_type(TypeName*),
622+
const TypeName* getbyname(const char *),
623+
const char* getname(const TypeName*)>
624+
void array_push_back(const TypeName* evp_ref,
621625
const char* from,
622626
const char* to,
623627
void* arg) {
628+
if (!from)
629+
return;
630+
631+
const TypeName* real_instance = getbyname(from);
632+
if (!real_instance)
633+
return;
634+
635+
const char* real_name = getname(real_instance);
636+
if (!real_name)
637+
return;
638+
639+
// EVP_*_fetch() does not support alias names, so we need to pass it the
640+
// real/original algorithm name
641+
// We use EVP_*_fetch() as a filter here because it will only return an
642+
// instance if the algorithm is supported by the public OpenSSL APIs (some
643+
// algorithms are used internally by OpenSSL and are also passed to this
644+
// callback)
645+
TypeName* fetched = fetch_type(nullptr, real_name, nullptr);
646+
if (!fetched)
647+
return;
648+
649+
free_type(fetched);
624650
static_cast<CipherPushContext*>(arg)->push_back(from);
625651
}
626652

0 commit comments

Comments
 (0)