@@ -206,54 +206,57 @@ void ReadSystemStoreCertificates(
206206 auto cleanup =
207207 OnScopeLeave ([hStore]() { CHECK_EQ (CertCloseStore (hStore, 0 ), TRUE ); });
208208
209- PCCERT_CONTEXT pCtx = nullptr ;
209+ PCCERT_CONTEXT certificate_context_ptr = nullptr ;
210210
211- while ((pCtx = CertEnumCertificatesInStore (hStore, pCtx)) != nullptr ) {
212- const DWORD cbSize = CertGetNameStringW (
213- pCtx, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0 , nullptr , nullptr , 0 );
211+ std::vector<X509*> system_root_certificates_X509;
214212
215- CHECK_GT (cbSize, 0 );
213+ while ((certificate_context_ptr = CertEnumCertificatesInStore (
214+ hStore, certificate_context_ptr)) != nullptr ) {
215+ const DWORD certificate_buffer_size =
216+ CertGetNameStringW (certificate_context_ptr,
217+ CERT_NAME_SIMPLE_DISPLAY_TYPE,
218+ 0 ,
219+ nullptr ,
220+ nullptr ,
221+ 0 );
216222
217- std::vector< wchar_t > pszName (cbSize );
223+ CHECK_GT (certificate_buffer_size, 0 );
218224
219- CHECK_GT (CertGetNameStringW (pCtx,
225+ std::vector<wchar_t > certificate_name (certificate_buffer_size);
226+
227+ CHECK_GT (CertGetNameStringW (certificate_context_ptr,
220228 CERT_NAME_SIMPLE_DISPLAY_TYPE,
221229 0 ,
222230 nullptr ,
223- pszName .data (),
224- cbSize ),
231+ certificate_name .data (),
232+ certificate_buffer_size ),
225233 0 );
234+ const unsigned char * certificate_src_ptr =
235+ reinterpret_cast <const unsigned char *>(
236+ certificate_context_ptr->pbCertEncoded );
237+ const size_t certificate_src_length =
238+ certificate_context_ptr->cbCertEncoded ;
226239
227- const char * certificate_src_ptr =
228- reinterpret_cast <const char *>(pCtx->pbCertEncoded );
229- const size_t slen = pCtx->cbCertEncoded ;
230- const size_t dlen = base64_encoded_size (slen);
231-
232- char * certificate_dst_ptr = UncheckedMalloc (dlen);
233-
234- CHECK_NOT_NULL (certificate_dst_ptr);
235-
236- auto cleanup =
237- OnScopeLeave ([certificate_dst_ptr]() { free (certificate_dst_ptr); });
240+ X509* cert =
241+ d2i_X509 (nullptr , &certificate_src_ptr, certificate_src_length);
238242
239- const size_t written =
240- base64_encode (certificate_src_ptr, slen, certificate_dst_ptr, dlen);
241- CHECK_EQ (written, dlen);
242-
243- std::string base64_string_output (certificate_dst_ptr, dlen) ;
243+ system_root_certificates_X509. emplace_back (cert);
244+ }
245+
246+ for ( size_t i = 0 ; i < system_root_certificates_X509. size (); i++) {
247+ int result = 0 ;
244248
245- constexpr size_t distance = 72 ;
246- size_t pos = distance ;
249+ BIOPointer bio ( BIO_new ( BIO_s_mem ())) ;
250+ CHECK (bio) ;
247251
248- while (pos < base64_string_output.size ()) {
249- base64_string_output.insert (pos, " \n " );
250- pos += distance + 1 ;
251- }
252+ BUF_MEM* mem = nullptr ;
253+ result = PEM_write_bio_X509 (bio.get (), system_root_certificates_X509[i]);
252254
253- base64_string_output = " -----BEGIN CERTIFICATE-----\n " +
254- base64_string_output + " \n -----END CERTIFICATE-----" ;
255+ BIO_get_mem_ptr (bio.get (), &mem);
256+ std::string certificate_string_pem (mem->data , mem->length );
257+ system_root_certificates->emplace_back (certificate_string_pem);
255258
256- system_root_certificates-> emplace_back ( std::move (base64_string_output) );
259+ bio. reset ( );
257260 }
258261#endif
259262}
0 commit comments