From 685c69214343311ae63f92642a8e3bf08df8cff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sat, 21 May 2022 21:08:20 +0000 Subject: [PATCH] src: refactor GetCipherValue and related functions Modernize and simplify GetCipherValue and its call sites. --- src/crypto/crypto_common.cc | 43 +++++++++++-------------------------- src/crypto/crypto_common.h | 12 ----------- 2 files changed, 12 insertions(+), 43 deletions(-) diff --git a/src/crypto/crypto_common.cc b/src/crypto/crypto_common.cc index 09b93d40c277ed..f5e3941382d66f 100644 --- a/src/crypto/crypto_common.cc +++ b/src/crypto/crypto_common.cc @@ -314,28 +314,17 @@ bool Set( return !target->Set(context, name, value).IsNothing(); } -MaybeLocal GetCipherValue(Environment* env, - const SSL_CIPHER* cipher, - const char* (*getstr)(const SSL_CIPHER* cipher)) { +template +MaybeLocal GetCipherValue(Environment* env, const SSL_CIPHER* cipher) { if (cipher == nullptr) return Undefined(env->isolate()); return OneByteString(env->isolate(), getstr(cipher)); } -MaybeLocal GetCipherName(Environment* env, const SSL_CIPHER* cipher) { - return GetCipherValue(env, cipher, SSL_CIPHER_get_name); -} - -MaybeLocal GetCipherStandardName( - Environment* env, - const SSL_CIPHER* cipher) { - return GetCipherValue(env, cipher, SSL_CIPHER_standard_name); -} - -MaybeLocal GetCipherVersion(Environment* env, const SSL_CIPHER* cipher) { - return GetCipherValue(env, cipher, SSL_CIPHER_get_version); -} +constexpr auto GetCipherName = GetCipherValue; +constexpr auto GetCipherStandardName = GetCipherValue; +constexpr auto GetCipherVersion = GetCipherValue; StackOfX509 CloneSSLCerts(X509Pointer&& cert, const STACK_OF(X509)* const ssl_certs) { @@ -1052,18 +1041,10 @@ static MaybeLocal GetX509NameObject(Environment* env, X509* cert) { return result; } -MaybeLocal GetCipherName(Environment* env, const SSLPointer& ssl) { - return GetCipherName(env, SSL_get_current_cipher(ssl.get())); -} - -MaybeLocal GetCipherStandardName( - Environment* env, - const SSLPointer& ssl) { - return GetCipherStandardName(env, SSL_get_current_cipher(ssl.get())); -} - -MaybeLocal GetCipherVersion(Environment* env, const SSLPointer& ssl) { - return GetCipherVersion(env, SSL_get_current_cipher(ssl.get())); +template (*Get)(Environment* env, const SSL_CIPHER* cipher)> +MaybeLocal GetCurrentCipherValue(Environment* env, + const SSLPointer& ssl) { + return Get(env, SSL_get_current_cipher(ssl.get())); } MaybeLocal GetClientHelloCiphers( @@ -1109,15 +1090,15 @@ MaybeLocal GetCipherInfo(Environment* env, const SSLPointer& ssl) { if (!Set(env->context(), info, env->name_string(), - GetCipherName(env, ssl)) || + GetCurrentCipherValue(env, ssl)) || !Set(env->context(), info, env->standard_name_string(), - GetCipherStandardName(env, ssl)) || + GetCurrentCipherValue(env, ssl)) || !Set(env->context(), info, env->version_string(), - GetCipherVersion(env, ssl))) { + GetCurrentCipherValue(env, ssl))) { return MaybeLocal(); } diff --git a/src/crypto/crypto_common.h b/src/crypto/crypto_common.h index 7843f8acd1c5d5..792760e9707328 100644 --- a/src/crypto/crypto_common.h +++ b/src/crypto/crypto_common.h @@ -74,18 +74,6 @@ v8::MaybeLocal GetValidationErrorCode(Environment* env, int err); v8::MaybeLocal GetCert(Environment* env, const SSLPointer& ssl); -v8::MaybeLocal GetCipherName( - Environment* env, - const SSLPointer& ssl); - -v8::MaybeLocal GetCipherStandardName( - Environment* env, - const SSLPointer& ssl); - -v8::MaybeLocal GetCipherVersion( - Environment* env, - const SSLPointer& ssl); - v8::MaybeLocal GetCipherInfo( Environment* env, const SSLPointer& ssl);