Skip to content

Commit 1ed0697

Browse files
tniessenruyadorno
authored andcommitted
src: deduplicate X509 getter implementations
Reorder arguments of internal helper functions such that their order is consistent across X509 property getters. Add ReturnPropertyThroughBIO() and ReturnProperty(). Use these new helpers to deduplicate code across various X509 property getters. PR-URL: #48563 Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Alba Mendez <[email protected]>
1 parent 83fe6b1 commit 1ed0697

File tree

3 files changed

+53
-100
lines changed

3 files changed

+53
-100
lines changed

src/crypto/crypto_common.cc

+19-27
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ MaybeLocal<Value> GetModulusString(
492492
}
493493
} // namespace
494494

495-
MaybeLocal<Object> GetRawDERCertificate(Environment* env, X509* cert) {
495+
MaybeLocal<Value> GetRawDERCertificate(Environment* env, X509* cert) {
496496
int size = i2d_X509(cert, nullptr);
497497

498498
std::unique_ptr<BackingStore> bs;
@@ -872,10 +872,9 @@ bool SafeX509InfoAccessPrint(const BIOPointer& out, X509_EXTENSION* ext) {
872872
return ok;
873873
}
874874

875-
v8::MaybeLocal<v8::Value> GetSubjectAltNameString(
876-
Environment* env,
877-
const BIOPointer& bio,
878-
X509* cert) {
875+
v8::MaybeLocal<v8::Value> GetSubjectAltNameString(Environment* env,
876+
X509* cert,
877+
const BIOPointer& bio) {
879878
int index = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1);
880879
if (index < 0)
881880
return Undefined(env->isolate());
@@ -891,10 +890,9 @@ v8::MaybeLocal<v8::Value> GetSubjectAltNameString(
891890
return ToV8Value(env, bio);
892891
}
893892

894-
v8::MaybeLocal<v8::Value> GetInfoAccessString(
895-
Environment* env,
896-
const BIOPointer& bio,
897-
X509* cert) {
893+
v8::MaybeLocal<v8::Value> GetInfoAccessString(Environment* env,
894+
X509* cert,
895+
const BIOPointer& bio) {
898896
int index = X509_get_ext_by_NID(cert, NID_info_access, -1);
899897
if (index < 0)
900898
return Undefined(env->isolate());
@@ -910,10 +908,9 @@ v8::MaybeLocal<v8::Value> GetInfoAccessString(
910908
return ToV8Value(env, bio);
911909
}
912910

913-
MaybeLocal<Value> GetIssuerString(
914-
Environment* env,
915-
const BIOPointer& bio,
916-
X509* cert) {
911+
MaybeLocal<Value> GetIssuerString(Environment* env,
912+
X509* cert,
913+
const BIOPointer& bio) {
917914
X509_NAME* issuer_name = X509_get_issuer_name(cert);
918915
if (X509_NAME_print_ex(
919916
bio.get(),
@@ -927,10 +924,9 @@ MaybeLocal<Value> GetIssuerString(
927924
return ToV8Value(env, bio);
928925
}
929926

930-
MaybeLocal<Value> GetSubject(
931-
Environment* env,
932-
const BIOPointer& bio,
933-
X509* cert) {
927+
MaybeLocal<Value> GetSubject(Environment* env,
928+
X509* cert,
929+
const BIOPointer& bio) {
934930
if (X509_NAME_print_ex(
935931
bio.get(),
936932
X509_get_subject_name(cert),
@@ -1283,11 +1279,11 @@ MaybeLocal<Object> X509ToObject(
12831279
!Set<Value>(context,
12841280
info,
12851281
env->subjectaltname_string(),
1286-
GetSubjectAltNameString(env, bio, cert)) ||
1282+
GetSubjectAltNameString(env, cert, bio)) ||
12871283
!Set<Value>(context,
12881284
info,
12891285
env->infoaccess_string(),
1290-
GetInfoAccessString(env, bio, cert)) ||
1286+
GetInfoAccessString(env, cert, bio)) ||
12911287
!Set<Boolean>(context, info, env->ca_string(), is_ca)) {
12921288
return MaybeLocal<Object>();
12931289
}
@@ -1390,18 +1386,14 @@ MaybeLocal<Object> X509ToObject(
13901386
info,
13911387
env->fingerprint512_string(),
13921388
GetFingerprintDigest(env, EVP_sha512(), cert)) ||
1393-
!Set<Value>(context,
1394-
info,
1395-
env->ext_key_usage_string(),
1396-
GetKeyUsage(env, cert)) ||
1389+
!Set<Value>(
1390+
context, info, env->ext_key_usage_string(), GetKeyUsage(env, cert)) ||
13971391
!Set<Value>(context,
13981392
info,
13991393
env->serial_number_string(),
14001394
GetSerialNumber(env, cert)) ||
1401-
!Set<Object>(context,
1402-
info,
1403-
env->raw_string(),
1404-
GetRawDERCertificate(env, cert))) {
1395+
!Set<Value>(
1396+
context, info, env->raw_string(), GetRawDERCertificate(env, cert))) {
14051397
return MaybeLocal<Object>();
14061398
}
14071399

src/crypto/crypto_common.h

+13-17
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,26 @@ v8::MaybeLocal<v8::Value> GetCurrentCipherVersion(Environment* env,
118118

119119
v8::MaybeLocal<v8::Value> GetSerialNumber(Environment* env, X509* cert);
120120

121-
v8::MaybeLocal<v8::Object> GetRawDERCertificate(Environment* env, X509* cert);
121+
v8::MaybeLocal<v8::Value> GetRawDERCertificate(Environment* env, X509* cert);
122122

123123
v8::Local<v8::Value> ToV8Value(Environment* env, const BIOPointer& bio);
124124
bool SafeX509SubjectAltNamePrint(const BIOPointer& out, X509_EXTENSION* ext);
125125

126-
v8::MaybeLocal<v8::Value> GetSubject(
127-
Environment* env,
128-
const BIOPointer& bio,
129-
X509* cert);
126+
v8::MaybeLocal<v8::Value> GetSubject(Environment* env,
127+
X509* cert,
128+
const BIOPointer& bio);
130129

131-
v8::MaybeLocal<v8::Value> GetIssuerString(
132-
Environment* env,
133-
const BIOPointer& bio,
134-
X509* cert);
130+
v8::MaybeLocal<v8::Value> GetIssuerString(Environment* env,
131+
X509* cert,
132+
const BIOPointer& bio);
135133

136-
v8::MaybeLocal<v8::Value> GetSubjectAltNameString(
137-
Environment* env,
138-
const BIOPointer& bio,
139-
X509* cert);
134+
v8::MaybeLocal<v8::Value> GetSubjectAltNameString(Environment* env,
135+
X509* cert,
136+
const BIOPointer& bio);
140137

141-
v8::MaybeLocal<v8::Value> GetInfoAccessString(
142-
Environment* env,
143-
const BIOPointer& bio,
144-
X509* cert);
138+
v8::MaybeLocal<v8::Value> GetInfoAccessString(Environment* env,
139+
X509* cert,
140+
const BIOPointer& bio);
145141

146142
} // namespace crypto
147143
} // namespace node

src/crypto/crypto_x509.cc

+21-56
Original file line numberDiff line numberDiff line change
@@ -204,97 +204,62 @@ void X509Certificate::Parse(const FunctionCallbackInfo<Value>& args) {
204204
args.GetReturnValue().Set(cert);
205205
}
206206

207-
void X509Certificate::Subject(const FunctionCallbackInfo<Value>& args) {
207+
template <MaybeLocal<Value> Property(
208+
Environment* env, X509* cert, const BIOPointer& bio)>
209+
static void ReturnPropertyThroughBIO(const FunctionCallbackInfo<Value>& args) {
208210
Environment* env = Environment::GetCurrent(args);
209211
X509Certificate* cert;
210212
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
211213
BIOPointer bio(BIO_new(BIO_s_mem()));
212214
CHECK(bio);
213215
Local<Value> ret;
214-
if (GetSubject(env, bio, cert->get()).ToLocal(&ret))
216+
if (Property(env, cert->get(), bio).ToLocal(&ret))
215217
args.GetReturnValue().Set(ret);
216218
}
217219

220+
void X509Certificate::Subject(const FunctionCallbackInfo<Value>& args) {
221+
ReturnPropertyThroughBIO<GetSubject>(args);
222+
}
223+
218224
void X509Certificate::Issuer(const FunctionCallbackInfo<Value>& args) {
219-
Environment* env = Environment::GetCurrent(args);
220-
X509Certificate* cert;
221-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
222-
BIOPointer bio(BIO_new(BIO_s_mem()));
223-
CHECK(bio);
224-
Local<Value> ret;
225-
if (GetIssuerString(env, bio, cert->get()).ToLocal(&ret))
226-
args.GetReturnValue().Set(ret);
225+
ReturnPropertyThroughBIO<GetIssuerString>(args);
227226
}
228227

229228
void X509Certificate::SubjectAltName(const FunctionCallbackInfo<Value>& args) {
230-
Environment* env = Environment::GetCurrent(args);
231-
X509Certificate* cert;
232-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
233-
BIOPointer bio(BIO_new(BIO_s_mem()));
234-
CHECK(bio);
235-
Local<Value> ret;
236-
if (GetSubjectAltNameString(env, bio, cert->get()).ToLocal(&ret))
237-
args.GetReturnValue().Set(ret);
229+
ReturnPropertyThroughBIO<GetSubjectAltNameString>(args);
238230
}
239231

240232
void X509Certificate::InfoAccess(const FunctionCallbackInfo<Value>& args) {
241-
Environment* env = Environment::GetCurrent(args);
242-
X509Certificate* cert;
243-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
244-
BIOPointer bio(BIO_new(BIO_s_mem()));
245-
CHECK(bio);
246-
Local<Value> ret;
247-
if (GetInfoAccessString(env, bio, cert->get()).ToLocal(&ret))
248-
args.GetReturnValue().Set(ret);
233+
ReturnPropertyThroughBIO<GetInfoAccessString>(args);
249234
}
250235

251236
void X509Certificate::ValidFrom(const FunctionCallbackInfo<Value>& args) {
252-
Environment* env = Environment::GetCurrent(args);
253-
X509Certificate* cert;
254-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
255-
BIOPointer bio(BIO_new(BIO_s_mem()));
256-
CHECK(bio);
257-
Local<Value> ret;
258-
if (GetValidFrom(env, cert->get(), bio).ToLocal(&ret))
259-
args.GetReturnValue().Set(ret);
237+
ReturnPropertyThroughBIO<GetValidFrom>(args);
260238
}
261239

262240
void X509Certificate::ValidTo(const FunctionCallbackInfo<Value>& args) {
241+
ReturnPropertyThroughBIO<GetValidTo>(args);
242+
}
243+
244+
template <MaybeLocal<Value> Property(Environment* env, X509* cert)>
245+
static void ReturnProperty(const FunctionCallbackInfo<Value>& args) {
263246
Environment* env = Environment::GetCurrent(args);
264247
X509Certificate* cert;
265248
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
266-
BIOPointer bio(BIO_new(BIO_s_mem()));
267-
CHECK(bio);
268249
Local<Value> ret;
269-
if (GetValidTo(env, cert->get(), bio).ToLocal(&ret))
270-
args.GetReturnValue().Set(ret);
250+
if (Property(env, cert->get()).ToLocal(&ret)) args.GetReturnValue().Set(ret);
271251
}
272252

273253
void X509Certificate::KeyUsage(const FunctionCallbackInfo<Value>& args) {
274-
Environment* env = Environment::GetCurrent(args);
275-
X509Certificate* cert;
276-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
277-
Local<Value> ret;
278-
if (GetKeyUsage(env, cert->get()).ToLocal(&ret))
279-
args.GetReturnValue().Set(ret);
254+
ReturnProperty<GetKeyUsage>(args);
280255
}
281256

282257
void X509Certificate::SerialNumber(const FunctionCallbackInfo<Value>& args) {
283-
Environment* env = Environment::GetCurrent(args);
284-
X509Certificate* cert;
285-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
286-
Local<Value> ret;
287-
if (GetSerialNumber(env, cert->get()).ToLocal(&ret))
288-
args.GetReturnValue().Set(ret);
258+
ReturnProperty<GetSerialNumber>(args);
289259
}
290260

291261
void X509Certificate::Raw(const FunctionCallbackInfo<Value>& args) {
292-
Environment* env = Environment::GetCurrent(args);
293-
X509Certificate* cert;
294-
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
295-
Local<Value> ret;
296-
if (GetRawDERCertificate(env, cert->get()).ToLocal(&ret))
297-
args.GetReturnValue().Set(ret);
262+
ReturnProperty<GetRawDERCertificate>(args);
298263
}
299264

300265
void X509Certificate::PublicKey(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)