From 75b0d9e60d02fc96e7ac7ff0022f767f8ff2efde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Fri, 12 May 2023 16:06:45 +0000 Subject: [PATCH] src: deduplicate X509Certificate::Fingerprint* All three functions do the same, except using different cryptographic hash functions. Move the common logic into a new template and use it directly. --- src/crypto/crypto_x509.cc | 51 ++++++++++++++------------------------- src/crypto/crypto_x509.h | 3 --- 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/src/crypto/crypto_x509.cc b/src/crypto/crypto_x509.cc index 48ec4b2587342d..8542eae8e8b665 100644 --- a/src/crypto/crypto_x509.cc +++ b/src/crypto/crypto_x509.cc @@ -51,6 +51,18 @@ void ManagedX509::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackFieldWithSize("cert", size); } +namespace { +template +void Fingerprint(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + X509Certificate* cert; + ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder()); + Local ret; + if (GetFingerprintDigest(env, algo(), cert->get()).ToLocal(&ret)) + args.GetReturnValue().Set(ret); +} +} // namespace + Local X509Certificate::GetConstructorTemplate( Environment* env) { Local tmpl = env->x509_constructor_template(); @@ -67,9 +79,9 @@ Local X509Certificate::GetConstructorTemplate( SetProtoMethod(isolate, tmpl, "issuer", Issuer); SetProtoMethod(isolate, tmpl, "validTo", ValidTo); SetProtoMethod(isolate, tmpl, "validFrom", ValidFrom); - SetProtoMethod(isolate, tmpl, "fingerprint", Fingerprint); - SetProtoMethod(isolate, tmpl, "fingerprint256", Fingerprint256); - SetProtoMethod(isolate, tmpl, "fingerprint512", Fingerprint512); + SetProtoMethod(isolate, tmpl, "fingerprint", Fingerprint); + SetProtoMethod(isolate, tmpl, "fingerprint256", Fingerprint); + SetProtoMethod(isolate, tmpl, "fingerprint512", Fingerprint); SetProtoMethod(isolate, tmpl, "keyUsage", KeyUsage); SetProtoMethod(isolate, tmpl, "serialNumber", SerialNumber); SetProtoMethod(isolate, tmpl, "pem", Pem); @@ -257,33 +269,6 @@ void X509Certificate::ValidTo(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(ret); } -void X509Certificate::Fingerprint(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - X509Certificate* cert; - ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder()); - Local ret; - if (GetFingerprintDigest(env, EVP_sha1(), cert->get()).ToLocal(&ret)) - args.GetReturnValue().Set(ret); -} - -void X509Certificate::Fingerprint256(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - X509Certificate* cert; - ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder()); - Local ret; - if (GetFingerprintDigest(env, EVP_sha256(), cert->get()).ToLocal(&ret)) - args.GetReturnValue().Set(ret); -} - -void X509Certificate::Fingerprint512(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - X509Certificate* cert; - ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder()); - Local ret; - if (GetFingerprintDigest(env, EVP_sha512(), cert->get()).ToLocal(&ret)) - args.GetReturnValue().Set(ret); -} - void X509Certificate::KeyUsage(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); X509Certificate* cert; @@ -570,9 +555,9 @@ void X509Certificate::RegisterExternalReferences( registry->Register(Issuer); registry->Register(ValidTo); registry->Register(ValidFrom); - registry->Register(Fingerprint); - registry->Register(Fingerprint256); - registry->Register(Fingerprint512); + registry->Register(Fingerprint); + registry->Register(Fingerprint); + registry->Register(Fingerprint); registry->Register(KeyUsage); registry->Register(SerialNumber); registry->Register(Pem); diff --git a/src/crypto/crypto_x509.h b/src/crypto/crypto_x509.h index 751e6e8cf4ba48..00b7689a194a32 100644 --- a/src/crypto/crypto_x509.h +++ b/src/crypto/crypto_x509.h @@ -79,9 +79,6 @@ class X509Certificate : public BaseObject { static void InfoAccess(const v8::FunctionCallbackInfo& args); static void ValidFrom(const v8::FunctionCallbackInfo& args); static void ValidTo(const v8::FunctionCallbackInfo& args); - static void Fingerprint(const v8::FunctionCallbackInfo& args); - static void Fingerprint256(const v8::FunctionCallbackInfo& args); - static void Fingerprint512(const v8::FunctionCallbackInfo& args); static void KeyUsage(const v8::FunctionCallbackInfo& args); static void SerialNumber(const v8::FunctionCallbackInfo& args); static void Raw(const v8::FunctionCallbackInfo& args);