@@ -200,60 +200,59 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
200
200
void ReadSystemStoreCertificates (
201
201
std::vector<std::string>* system_root_certificates) {
202
202
#ifdef _WIN32
203
- const HCERTSTORE hStore = CertOpenSystemStoreW (0 , L" ROOT" );
204
- CHECK_NE (hStore, nullptr );
203
+ CertStorePointer system_store;
205
204
206
- auto cleanup =
207
- OnScopeLeave ([hStore]() { CHECK_EQ (CertCloseStore (hStore, 0 ), TRUE ); });
205
+ PCCERT_CONTEXT certificate_context_ptr = nullptr ;
208
206
209
- PCCERT_CONTEXT pCtx = nullptr ;
207
+ std::vector<X509*> system_root_certificates_X509 ;
210
208
211
- while ((pCtx = CertEnumCertificatesInStore (hStore, pCtx)) != nullptr ) {
212
- const DWORD cbSize = CertGetNameStringW (
213
- pCtx, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0 , nullptr , nullptr , 0 );
209
+ while ((certificate_context_ptr = CertEnumCertificatesInStore (
210
+ system_store.ref_ , certificate_context_ptr)) != nullptr ) {
211
+ const DWORD certificate_buffer_size =
212
+ CertGetNameStringW (certificate_context_ptr,
213
+ CERT_NAME_SIMPLE_DISPLAY_TYPE,
214
+ 0 ,
215
+ nullptr ,
216
+ nullptr ,
217
+ 0 );
214
218
215
- CHECK_GT (cbSize , 0 );
219
+ CHECK_GT (certificate_buffer_size , 0 );
216
220
217
- std::vector<wchar_t > pszName (cbSize );
221
+ std::vector<wchar_t > certificate_name (certificate_buffer_size );
218
222
219
- CHECK_GT (CertGetNameStringW (pCtx ,
223
+ CHECK_GT (CertGetNameStringW (certificate_context_ptr ,
220
224
CERT_NAME_SIMPLE_DISPLAY_TYPE,
221
225
0 ,
222
226
nullptr ,
223
- pszName .data (),
224
- cbSize ),
227
+ certificate_name .data (),
228
+ certificate_buffer_size ),
225
229
0 );
230
+ const unsigned char * certificate_src_ptr =
231
+ reinterpret_cast <const unsigned char *>(
232
+ certificate_context_ptr->pbCertEncoded );
233
+ const size_t certificate_src_length =
234
+ certificate_context_ptr->cbCertEncoded ;
226
235
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);
236
+ X509* cert =
237
+ d2i_X509 (nullptr , &certificate_src_ptr, certificate_src_length);
231
238
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); });
238
-
239
- const size_t written =
240
- base64_encode (certificate_src_ptr, slen, certificate_dst_ptr, dlen);
241
- CHECK_EQ (written, dlen);
239
+ system_root_certificates_X509.emplace_back (cert);
240
+ }
242
241
243
- std::string base64_string_output (certificate_dst_ptr, dlen);
242
+ for (size_t i = 0 ; i < system_root_certificates_X509.size (); i++) {
243
+ int result = 0 ;
244
244
245
- constexpr size_t distance = 72 ;
246
- size_t pos = distance ;
245
+ BIOPointer bio ( BIO_new ( BIO_s_mem ())) ;
246
+ CHECK (bio) ;
247
247
248
- while (pos < base64_string_output.size ()) {
249
- base64_string_output.insert (pos, " \n " );
250
- pos += distance + 1 ;
251
- }
248
+ BUF_MEM* mem = nullptr ;
249
+ result = PEM_write_bio_X509 (bio.get (), system_root_certificates_X509[i]);
252
250
253
- base64_string_output = " -----BEGIN CERTIFICATE-----\n " +
254
- base64_string_output + " \n -----END CERTIFICATE-----" ;
251
+ BIO_get_mem_ptr (bio.get (), &mem);
252
+ std::string certificate_string_pem (mem->data , mem->length );
253
+ system_root_certificates->emplace_back (certificate_string_pem);
255
254
256
- system_root_certificates-> emplace_back ( std::move (base64_string_output) );
255
+ bio. reset ( );
257
256
}
258
257
#endif
259
258
}
0 commit comments