summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Nießen <tniessen@tnie.de>2023-05-15 14:49:00 +0200
committerMichaël Zasso <targos@protonmail.com>2023-05-15 16:44:21 +0200
commit786a1c5398db7a7a605b11ab0dd3317a03cc0f89 (patch)
tree7271a08c695d8e51b786aa54b681f822c9b2cf34
parent010d2ecf9405be03dac22eeb6e3bc3afb5ffa3ee (diff)
downloadnode-new-786a1c5398db7a7a605b11ab0dd3317a03cc0f89.tar.gz
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. PR-URL: https://github.com/nodejs/node/pull/47978 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
-rw-r--r--src/crypto/crypto_x509.cc51
-rw-r--r--src/crypto/crypto_x509.h3
2 files changed, 18 insertions, 36 deletions
diff --git a/src/crypto/crypto_x509.cc b/src/crypto/crypto_x509.cc
index 48ec4b2587..8542eae8e8 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 <const EVP_MD* (*algo)()>
+void Fingerprint(const FunctionCallbackInfo<Value>& args) {
+ Environment* env = Environment::GetCurrent(args);
+ X509Certificate* cert;
+ ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
+ Local<Value> ret;
+ if (GetFingerprintDigest(env, algo(), cert->get()).ToLocal(&ret))
+ args.GetReturnValue().Set(ret);
+}
+} // namespace
+
Local<FunctionTemplate> X509Certificate::GetConstructorTemplate(
Environment* env) {
Local<FunctionTemplate> tmpl = env->x509_constructor_template();
@@ -67,9 +79,9 @@ Local<FunctionTemplate> 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<EVP_sha1>);
+ SetProtoMethod(isolate, tmpl, "fingerprint256", Fingerprint<EVP_sha256>);
+ SetProtoMethod(isolate, tmpl, "fingerprint512", Fingerprint<EVP_sha512>);
SetProtoMethod(isolate, tmpl, "keyUsage", KeyUsage);
SetProtoMethod(isolate, tmpl, "serialNumber", SerialNumber);
SetProtoMethod(isolate, tmpl, "pem", Pem);
@@ -257,33 +269,6 @@ void X509Certificate::ValidTo(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(ret);
}
-void X509Certificate::Fingerprint(const FunctionCallbackInfo<Value>& args) {
- Environment* env = Environment::GetCurrent(args);
- X509Certificate* cert;
- ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
- Local<Value> ret;
- if (GetFingerprintDigest(env, EVP_sha1(), cert->get()).ToLocal(&ret))
- args.GetReturnValue().Set(ret);
-}
-
-void X509Certificate::Fingerprint256(const FunctionCallbackInfo<Value>& args) {
- Environment* env = Environment::GetCurrent(args);
- X509Certificate* cert;
- ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
- Local<Value> ret;
- if (GetFingerprintDigest(env, EVP_sha256(), cert->get()).ToLocal(&ret))
- args.GetReturnValue().Set(ret);
-}
-
-void X509Certificate::Fingerprint512(const FunctionCallbackInfo<Value>& args) {
- Environment* env = Environment::GetCurrent(args);
- X509Certificate* cert;
- ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
- Local<Value> ret;
- if (GetFingerprintDigest(env, EVP_sha512(), cert->get()).ToLocal(&ret))
- args.GetReturnValue().Set(ret);
-}
-
void X509Certificate::KeyUsage(const FunctionCallbackInfo<Value>& 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<EVP_sha1>);
+ registry->Register(Fingerprint<EVP_sha256>);
+ registry->Register(Fingerprint<EVP_sha512>);
registry->Register(KeyUsage);
registry->Register(SerialNumber);
registry->Register(Pem);
diff --git a/src/crypto/crypto_x509.h b/src/crypto/crypto_x509.h
index 751e6e8cf4..00b7689a19 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<v8::Value>& args);
static void ValidFrom(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ValidTo(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void Fingerprint(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void Fingerprint256(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void Fingerprint512(const v8::FunctionCallbackInfo<v8::Value>& args);
static void KeyUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SerialNumber(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Raw(const v8::FunctionCallbackInfo<v8::Value>& args);