Skip to content

Commit df363d0

Browse files
tniessenjuanarbol
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 08c6287 commit df363d0

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
@@ -203,97 +203,62 @@ void X509Certificate::Parse(const FunctionCallbackInfo<Value>& args) {
203203
args.GetReturnValue().Set(cert);
204204
}
205205

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

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

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

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

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

261239
void X509Certificate::ValidTo(const FunctionCallbackInfo<Value>& args) {
240+
ReturnPropertyThroughBIO<GetValidTo>(args);
241+
}
242+
243+
template <MaybeLocal<Value> Property(Environment* env, X509* cert)>
244+
static void ReturnProperty(const FunctionCallbackInfo<Value>& args) {
262245
Environment* env = Environment::GetCurrent(args);
263246
X509Certificate* cert;
264247
ASSIGN_OR_RETURN_UNWRAP(&cert, args.Holder());
265-
BIOPointer bio(BIO_new(BIO_s_mem()));
266-
CHECK(bio);
267248
Local<Value> ret;
268-
if (GetValidTo(env, cert->get(), bio).ToLocal(&ret))
269-
args.GetReturnValue().Set(ret);
249+
if (Property(env, cert->get()).ToLocal(&ret)) args.GetReturnValue().Set(ret);
270250
}
271251

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

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

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

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

0 commit comments

Comments
 (0)