Skip to content

Commit

Permalink
src: deduplicate X509Certificate::Fingerprint*
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
tniessen authored and MoLow committed Jul 6, 2023
1 parent a3f0504 commit 2a35462
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
51 changes: 18 additions & 33 deletions src/crypto/crypto_x509.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -68,9 +80,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);
Expand Down Expand Up @@ -258,33 +270,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;
Expand Down Expand Up @@ -575,9 +560,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);
Expand Down
3 changes: 0 additions & 3 deletions src/crypto/crypto_x509.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2a35462

Please sign in to comment.