@@ -986,24 +986,6 @@ static X509_STORE* NewRootCertStore() {
986
986
}
987
987
988
988
989
- void GetRootCertificates (const FunctionCallbackInfo<Value>& args) {
990
- Environment* env = Environment::GetCurrent (args);
991
- Local<Value> result[arraysize (root_certs)];
992
-
993
- for (size_t i = 0 ; i < arraysize (root_certs); i++) {
994
- if (!String::NewFromOneByte (
995
- env->isolate (),
996
- reinterpret_cast <const uint8_t *>(root_certs[i]),
997
- NewStringType::kNormal ).ToLocal (&result[i])) {
998
- return ;
999
- }
1000
- }
1001
-
1002
- args.GetReturnValue ().Set (
1003
- Array::New (env->isolate (), result, arraysize (root_certs)));
1004
- }
1005
-
1006
-
1007
989
void SecureContext::AddCACert (const FunctionCallbackInfo<Value>& args) {
1008
990
Environment* env = Environment::GetCurrent (args);
1009
991
@@ -2680,6 +2662,21 @@ static inline Local<Value> BIOToStringOrBuffer(Environment* env,
2680
2662
}
2681
2663
}
2682
2664
2665
+ static MaybeLocal<Value> X509ToPEM (Environment* env, X509* cert) {
2666
+ BIOPointer bio (BIO_new (BIO_s_mem ()));
2667
+ if (!bio) {
2668
+ ThrowCryptoError (env, ERR_get_error (), " BIO_new" );
2669
+ return MaybeLocal<Value>();
2670
+ }
2671
+
2672
+ if (PEM_write_bio_X509 (bio.get (), cert) == 0 ) {
2673
+ ThrowCryptoError (env, ERR_get_error (), " PEM_write_bio_X509" );
2674
+ return MaybeLocal<Value>();
2675
+ }
2676
+
2677
+ return BIOToStringOrBuffer (env, bio.get (), kKeyFormatPEM );
2678
+ }
2679
+
2683
2680
static bool WritePublicKeyInner (EVP_PKEY* pkey,
2684
2681
const BIOPointer& bio,
2685
2682
const PublicKeyEncodingConfig& config) {
@@ -6660,6 +6657,36 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
6660
6657
}
6661
6658
6662
6659
6660
+ void GetRootCertificates (const FunctionCallbackInfo<Value>& args) {
6661
+ Environment* env = Environment::GetCurrent (args);
6662
+
6663
+ if (root_cert_store == nullptr )
6664
+ root_cert_store = NewRootCertStore ();
6665
+
6666
+ stack_st_X509_OBJECT* objs = X509_STORE_get0_objects (root_cert_store);
6667
+ int num_objs = sk_X509_OBJECT_num (objs);
6668
+
6669
+ std::vector<Local<Value>> result;
6670
+ result.reserve (num_objs);
6671
+
6672
+ for (int i = 0 ; i < num_objs; i++) {
6673
+ X509_OBJECT* obj = sk_X509_OBJECT_value (objs, i);
6674
+ if (X509_OBJECT_get_type (obj) == X509_LU_X509) {
6675
+ X509* cert = X509_OBJECT_get0_X509 (obj);
6676
+
6677
+ Local<Value> value;
6678
+ if (!X509ToPEM (env, cert).ToLocal (&value))
6679
+ return ;
6680
+
6681
+ result.push_back (value);
6682
+ }
6683
+ }
6684
+
6685
+ args.GetReturnValue ().Set (
6686
+ Array::New (env->isolate (), result.data (), result.size ()));
6687
+ }
6688
+
6689
+
6663
6690
// Convert the input public key to compressed, uncompressed, or hybrid formats.
6664
6691
void ConvertKey (const FunctionCallbackInfo<Value>& args) {
6665
6692
MarkPopErrorOnReturn mark_pop_error_on_return;
0 commit comments