Skip to content

Commit 786a1c5

Browse files
tniessentargos
authored andcommitted
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: #47978 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 010d2ec commit 786a1c5

File tree

2 files changed

+18
-36
lines changed

2 files changed

+18
-36
lines changed

src/crypto/crypto_x509.cc

+18-33
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ void ManagedX509::MemoryInfo(MemoryTracker* tracker) const {
5151
tracker->TrackFieldWithSize("cert", size);
5252
}
5353

54+
namespace {
55+
template <const EVP_MD* (*algo)()>
56+
void Fingerprint(const FunctionCallbackInfo<Value>& args) {
57+
Environment* env = Environment::GetCurrent(args);
58+
X509Certificate* cert;
59+
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
60+
Local<Value> ret;
61+
if (GetFingerprintDigest(env, algo(), cert->get()).ToLocal(&ret))
62+
args.GetReturnValue().Set(ret);
63+
}
64+
} // namespace
65+
5466
Local<FunctionTemplate> X509Certificate::GetConstructorTemplate(
5567
Environment* env) {
5668
Local<FunctionTemplate> tmpl = env->x509_constructor_template();
@@ -67,9 +79,9 @@ Local<FunctionTemplate> X509Certificate::GetConstructorTemplate(
6779
SetProtoMethod(isolate, tmpl, "issuer", Issuer);
6880
SetProtoMethod(isolate, tmpl, "validTo", ValidTo);
6981
SetProtoMethod(isolate, tmpl, "validFrom", ValidFrom);
70-
SetProtoMethod(isolate, tmpl, "fingerprint", Fingerprint);
71-
SetProtoMethod(isolate, tmpl, "fingerprint256", Fingerprint256);
72-
SetProtoMethod(isolate, tmpl, "fingerprint512", Fingerprint512);
82+
SetProtoMethod(isolate, tmpl, "fingerprint", Fingerprint<EVP_sha1>);
83+
SetProtoMethod(isolate, tmpl, "fingerprint256", Fingerprint<EVP_sha256>);
84+
SetProtoMethod(isolate, tmpl, "fingerprint512", Fingerprint<EVP_sha512>);
7385
SetProtoMethod(isolate, tmpl, "keyUsage", KeyUsage);
7486
SetProtoMethod(isolate, tmpl, "serialNumber", SerialNumber);
7587
SetProtoMethod(isolate, tmpl, "pem", Pem);
@@ -257,33 +269,6 @@ void X509Certificate::ValidTo(const FunctionCallbackInfo<Value>& args) {
257269
args.GetReturnValue().Set(ret);
258270
}
259271

260-
void X509Certificate::Fingerprint(const FunctionCallbackInfo<Value>& args) {
261-
Environment* env = Environment::GetCurrent(args);
262-
X509Certificate* cert;
263-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
264-
Local<Value> ret;
265-
if (GetFingerprintDigest(env, EVP_sha1(), cert->get()).ToLocal(&ret))
266-
args.GetReturnValue().Set(ret);
267-
}
268-
269-
void X509Certificate::Fingerprint256(const FunctionCallbackInfo<Value>& args) {
270-
Environment* env = Environment::GetCurrent(args);
271-
X509Certificate* cert;
272-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
273-
Local<Value> ret;
274-
if (GetFingerprintDigest(env, EVP_sha256(), cert->get()).ToLocal(&ret))
275-
args.GetReturnValue().Set(ret);
276-
}
277-
278-
void X509Certificate::Fingerprint512(const FunctionCallbackInfo<Value>& args) {
279-
Environment* env = Environment::GetCurrent(args);
280-
X509Certificate* cert;
281-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
282-
Local<Value> ret;
283-
if (GetFingerprintDigest(env, EVP_sha512(), cert->get()).ToLocal(&ret))
284-
args.GetReturnValue().Set(ret);
285-
}
286-
287272
void X509Certificate::KeyUsage(const FunctionCallbackInfo<Value>& args) {
288273
Environment* env = Environment::GetCurrent(args);
289274
X509Certificate* cert;
@@ -570,9 +555,9 @@ void X509Certificate::RegisterExternalReferences(
570555
registry->Register(Issuer);
571556
registry->Register(ValidTo);
572557
registry->Register(ValidFrom);
573-
registry->Register(Fingerprint);
574-
registry->Register(Fingerprint256);
575-
registry->Register(Fingerprint512);
558+
registry->Register(Fingerprint<EVP_sha1>);
559+
registry->Register(Fingerprint<EVP_sha256>);
560+
registry->Register(Fingerprint<EVP_sha512>);
576561
registry->Register(KeyUsage);
577562
registry->Register(SerialNumber);
578563
registry->Register(Pem);

src/crypto/crypto_x509.h

-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ class X509Certificate : public BaseObject {
7979
static void InfoAccess(const v8::FunctionCallbackInfo<v8::Value>& args);
8080
static void ValidFrom(const v8::FunctionCallbackInfo<v8::Value>& args);
8181
static void ValidTo(const v8::FunctionCallbackInfo<v8::Value>& args);
82-
static void Fingerprint(const v8::FunctionCallbackInfo<v8::Value>& args);
83-
static void Fingerprint256(const v8::FunctionCallbackInfo<v8::Value>& args);
84-
static void Fingerprint512(const v8::FunctionCallbackInfo<v8::Value>& args);
8582
static void KeyUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
8683
static void SerialNumber(const v8::FunctionCallbackInfo<v8::Value>& args);
8784
static void Raw(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)