From baa5d0005ad26c053a17b56a11a0c6912a2c9500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 24 May 2022 13:59:55 +0200 Subject: src: refactor GetCipherValue and related functions Modernize and simplify GetCipherValue and its call sites. PR-URL: https://github.com/nodejs/node/pull/43171 Reviewed-By: Ben Noordhuis Reviewed-By: Minwoo Jung --- src/crypto/crypto_common.cc | 43 ++++++++++++------------------------------- src/crypto/crypto_common.h | 12 ------------ 2 files changed, 12 insertions(+), 43 deletions(-) (limited to 'src/crypto') diff --git a/src/crypto/crypto_common.cc b/src/crypto/crypto_common.cc index 0aaf0043666..0b172e40ee7 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 7843f8acd1c..792760e9707 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); -- cgit v1.2.3