Skip to content

Commit 2a35462

Browse files
tniessenMoLow
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 a3f0504 commit 2a35462

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();
@@ -68,9 +80,9 @@ Local<FunctionTemplate> X509Certificate::GetConstructorTemplate(
6880
SetProtoMethod(isolate, tmpl, "issuer", Issuer);
6981
SetProtoMethod(isolate, tmpl, "validTo", ValidTo);
7082
SetProtoMethod(isolate, tmpl, "validFrom", ValidFrom);
71-
SetProtoMethod(isolate, tmpl, "fingerprint", Fingerprint);
72-
SetProtoMethod(isolate, tmpl, "fingerprint256", Fingerprint256);
73-
SetProtoMethod(isolate, tmpl, "fingerprint512", Fingerprint512);
83+
SetProtoMethod(isolate, tmpl, "fingerprint", Fingerprint<EVP_sha1>);
84+
SetProtoMethod(isolate, tmpl, "fingerprint256", Fingerprint<EVP_sha256>);
85+
SetProtoMethod(isolate, tmpl, "fingerprint512", Fingerprint<EVP_sha512>);
7486
SetProtoMethod(isolate, tmpl, "keyUsage", KeyUsage);
7587
SetProtoMethod(isolate, tmpl, "serialNumber", SerialNumber);
7688
SetProtoMethod(isolate, tmpl, "pem", Pem);
@@ -258,33 +270,6 @@ void X509Certificate::ValidTo(const FunctionCallbackInfo<Value>& args) {
258270
args.GetReturnValue().Set(ret);
259271
}
260272

261-
void X509Certificate::Fingerprint(const FunctionCallbackInfo<Value>& args) {
262-
Environment* env = Environment::GetCurrent(args);
263-
X509Certificate* cert;
264-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
265-
Local<Value> ret;
266-
if (GetFingerprintDigest(env, EVP_sha1(), cert->get()).ToLocal(&ret))
267-
args.GetReturnValue().Set(ret);
268-
}
269-
270-
void X509Certificate::Fingerprint256(const FunctionCallbackInfo<Value>& args) {
271-
Environment* env = Environment::GetCurrent(args);
272-
X509Certificate* cert;
273-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
274-
Local<Value> ret;
275-
if (GetFingerprintDigest(env, EVP_sha256(), cert->get()).ToLocal(&ret))
276-
args.GetReturnValue().Set(ret);
277-
}
278-
279-
void X509Certificate::Fingerprint512(const FunctionCallbackInfo<Value>& args) {
280-
Environment* env = Environment::GetCurrent(args);
281-
X509Certificate* cert;
282-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
283-
Local<Value> ret;
284-
if (GetFingerprintDigest(env, EVP_sha512(), cert->get()).ToLocal(&ret))
285-
args.GetReturnValue().Set(ret);
286-
}
287-
288273
void X509Certificate::KeyUsage(const FunctionCallbackInfo<Value>& args) {
289274
Environment* env = Environment::GetCurrent(args);
290275
X509Certificate* cert;
@@ -575,9 +560,9 @@ void X509Certificate::RegisterExternalReferences(
575560
registry->Register(Issuer);
576561
registry->Register(ValidTo);
577562
registry->Register(ValidFrom);
578-
registry->Register(Fingerprint);
579-
registry->Register(Fingerprint256);
580-
registry->Register(Fingerprint512);
563+
registry->Register(Fingerprint<EVP_sha1>);
564+
registry->Register(Fingerprint<EVP_sha256>);
565+
registry->Register(Fingerprint<EVP_sha512>);
581566
registry->Register(KeyUsage);
582567
registry->Register(SerialNumber);
583568
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)