@@ -21,6 +21,7 @@ using ncrypto::ClearErrorOnReturn;
21
21
using ncrypto::DataPointer;
22
22
using ncrypto::ECKeyPointer;
23
23
using ncrypto::SSLPointer;
24
+ using ncrypto::X509Name;
24
25
using ncrypto::X509Pointer;
25
26
using ncrypto::X509View;
26
27
using v8::Array;
@@ -92,6 +93,11 @@ void Fingerprint(const FunctionCallbackInfo<Value>& args) {
92
93
}
93
94
}
94
95
96
+ MaybeLocal<String> ToV8Value (Environment* env, std::string_view val) {
97
+ return String::NewFromUtf8 (
98
+ env->isolate (), val.data (), NewStringType::kNormal , val.size ());
99
+ }
100
+
95
101
MaybeLocal<Value> ToV8Value (Local<Context> context, BIOPointer&& bio) {
96
102
if (!bio) [[unlikely]]
97
103
return {};
@@ -106,51 +112,6 @@ MaybeLocal<Value> ToV8Value(Local<Context> context, BIOPointer&& bio) {
106
112
return ret;
107
113
}
108
114
109
- MaybeLocal<Value> ToV8Value (Local<Context> context, const ASN1_OBJECT* obj) {
110
- // If OpenSSL knows the type, use the short name of the type as the key, and
111
- // the numeric representation of the type's OID otherwise.
112
- int nid = OBJ_obj2nid (obj);
113
- char buf[80 ];
114
- const char * str;
115
- if (nid != NID_undef) {
116
- str = OBJ_nid2sn (nid);
117
- CHECK_NOT_NULL (str);
118
- } else {
119
- OBJ_obj2txt (buf, sizeof (buf), obj, true );
120
- str = buf;
121
- }
122
-
123
- Local<Value> result;
124
- if (!String::NewFromUtf8 (context->GetIsolate (), str).ToLocal (&result)) {
125
- return {};
126
- }
127
- return result;
128
- }
129
-
130
- MaybeLocal<Value> ToV8Value (Local<Context> context, const ASN1_STRING* str) {
131
- // The previous implementation used X509_NAME_print_ex, which escapes some
132
- // characters in the value. The old implementation did not decode/unescape
133
- // values correctly though, leading to ambiguous and incorrect
134
- // representations. The new implementation only converts to Unicode and does
135
- // not escape anything.
136
- unsigned char * value_str;
137
- int value_str_size = ASN1_STRING_to_UTF8 (&value_str, str);
138
- if (value_str_size < 0 ) [[unlikely]] {
139
- return Undefined (context->GetIsolate ());
140
- }
141
- DataPointer free_value_str (value_str, value_str_size);
142
-
143
- Local<Value> result;
144
- if (!String::NewFromUtf8 (context->GetIsolate (),
145
- reinterpret_cast <const char *>(value_str),
146
- NewStringType::kNormal ,
147
- value_str_size)
148
- .ToLocal (&result)) {
149
- return {};
150
- }
151
- return result;
152
- }
153
-
154
115
MaybeLocal<Value> ToV8Value (Local<Context> context, const BIOPointer& bio) {
155
116
if (!bio) [[unlikely]]
156
117
return {};
@@ -594,14 +555,9 @@ bool Set(Environment* env,
594
555
// Convert an X509_NAME* into a JavaScript object.
595
556
// Each entry of the name is converted into a property of the object.
596
557
// The property value may be a single string or an array of strings.
597
- template <X509_NAME* get_name (const X509*)>
598
558
static MaybeLocal<Value> GetX509NameObject (Environment* env,
599
- const X509View& cert) {
600
- X509_NAME* name = get_name (cert.get ());
601
- CHECK_NOT_NULL (name);
602
-
603
- int cnt = X509_NAME_entry_count (name);
604
- CHECK_GE (cnt, 0 );
559
+ const X509Name& name) {
560
+ if (!name) return {};
605
561
606
562
Local<Value> v8_name;
607
563
Local<Value> v8_value;
@@ -610,14 +566,9 @@ static MaybeLocal<Value> GetX509NameObject(Environment* env,
610
566
Object::New (env->isolate (), Null (env->isolate ()), nullptr , nullptr , 0 );
611
567
if (result.IsEmpty ()) return {};
612
568
613
- for (int i = 0 ; i < cnt; i++) {
614
- X509_NAME_ENTRY* entry = X509_NAME_get_entry (name, i);
615
- CHECK_NOT_NULL (entry);
616
-
617
- if (!ToV8Value (env->context (), X509_NAME_ENTRY_get_object (entry))
618
- .ToLocal (&v8_name) ||
619
- !ToV8Value (env->context (), X509_NAME_ENTRY_get_data (entry))
620
- .ToLocal (&v8_value)) {
569
+ for (auto i : name) {
570
+ if (!ToV8Value (env, i.first ).ToLocal (&v8_name) ||
571
+ !ToV8Value (env, i.second ).ToLocal (&v8_value)) {
621
572
return {};
622
573
}
623
574
@@ -727,11 +678,11 @@ MaybeLocal<Object> X509ToObject(Environment* env, const X509View& cert) {
727
678
if (!Set<Value>(env,
728
679
info,
729
680
env->subject_string (),
730
- GetX509NameObject<X509_get_subject_name> (env, cert)) ||
681
+ GetX509NameObject (env, cert. getSubjectName () )) ||
731
682
!Set<Value>(env,
732
683
info,
733
684
env->issuer_string (),
734
- GetX509NameObject<X509_get_issuer_name> (env, cert)) ||
685
+ GetX509NameObject (env, cert. getIssuerName () )) ||
735
686
!Set<Value>(env,
736
687
info,
737
688
env->subjectaltname_string (),
0 commit comments