@@ -21,6 +21,7 @@ using ncrypto::ClearErrorOnReturn;
2121using ncrypto::DataPointer;
2222using ncrypto::ECKeyPointer;
2323using ncrypto::SSLPointer;
24+ using ncrypto::X509Name;
2425using ncrypto::X509Pointer;
2526using ncrypto::X509View;
2627using v8::Array;
@@ -92,6 +93,11 @@ void Fingerprint(const FunctionCallbackInfo<Value>& args) {
9293 }
9394}
9495
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+
95101MaybeLocal<Value> ToV8Value (Local<Context> context, BIOPointer&& bio) {
96102 if (!bio) [[unlikely]]
97103 return {};
@@ -106,51 +112,6 @@ MaybeLocal<Value> ToV8Value(Local<Context> context, BIOPointer&& bio) {
106112 return ret;
107113}
108114
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-
154115MaybeLocal<Value> ToV8Value (Local<Context> context, const BIOPointer& bio) {
155116 if (!bio) [[unlikely]]
156117 return {};
@@ -594,14 +555,9 @@ bool Set(Environment* env,
594555// Convert an X509_NAME* into a JavaScript object.
595556// Each entry of the name is converted into a property of the object.
596557// The property value may be a single string or an array of strings.
597- template <X509_NAME* get_name (const X509*)>
598558static 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 {};
605561
606562 Local<Value> v8_name;
607563 Local<Value> v8_value;
@@ -610,14 +566,9 @@ static MaybeLocal<Value> GetX509NameObject(Environment* env,
610566 Object::New (env->isolate (), Null (env->isolate ()), nullptr , nullptr , 0 );
611567 if (result.IsEmpty ()) return {};
612568
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)) {
621572 return {};
622573 }
623574
@@ -727,11 +678,11 @@ MaybeLocal<Object> X509ToObject(Environment* env, const X509View& cert) {
727678 if (!Set<Value>(env,
728679 info,
729680 env->subject_string (),
730- GetX509NameObject<X509_get_subject_name> (env, cert)) ||
681+ GetX509NameObject (env, cert. getSubjectName () )) ||
731682 !Set<Value>(env,
732683 info,
733684 env->issuer_string (),
734- GetX509NameObject<X509_get_issuer_name> (env, cert)) ||
685+ GetX509NameObject (env, cert. getIssuerName () )) ||
735686 !Set<Value>(env,
736687 info,
737688 env->subjectaltname_string (),
0 commit comments