@@ -241,18 +241,28 @@ static int windows_load_system_certificates(struct tls_context *ctx)
241
241
{
242
242
int ret ;
243
243
HANDLE win_store ;
244
+ unsigned long err ;
244
245
PCCERT_CONTEXT win_cert = NULL ;
245
246
const unsigned char * win_cert_data ;
246
247
X509_STORE * ossl_store = SSL_CTX_get_cert_store (ctx -> ctx );
247
248
X509 * ossl_cert ;
248
249
250
+ /* Check if OpenSSL certificate store is available */
251
+ if (!ossl_store ) {
252
+ flb_error ("[tls] failed to retrieve openssl certificate store." );
253
+ return -1 ;
254
+ }
255
+
256
+ /* Open the Windows system certificate store */
249
257
win_store = CertOpenSystemStoreA (0 , "Root" );
250
258
if (win_store == NULL ) {
251
- flb_error ("[tls] Cannot open cert store: %i " , GetLastError ());
259
+ flb_error ("[tls] cannot open windows certificate store: %lu " , GetLastError ());
252
260
return -1 ;
253
261
}
254
262
255
- while (win_cert = CertEnumCertificatesInStore (win_store , win_cert )) {
263
+ /* Iterate over certificates in the store */
264
+ while ((win_cert = CertEnumCertificatesInStore (win_store , win_cert )) != NULL ) {
265
+ /* Check if the certificate is encoded in ASN.1 DER format */
256
266
if (win_cert -> dwCertEncodingType & X509_ASN_ENCODING ) {
257
267
/*
258
268
* Decode the certificate into X509 struct.
@@ -262,25 +272,42 @@ static int windows_load_system_certificates(struct tls_context *ctx)
262
272
*/
263
273
win_cert_data = win_cert -> pbCertEncoded ;
264
274
ossl_cert = d2i_X509 (NULL , & win_cert_data , win_cert -> cbCertEncoded );
275
+
265
276
if (!ossl_cert ) {
266
- flb_debug ("[tls] Cannot parse a certificate. skipping..." );
277
+ flb_debug ("[tls] cannot parse a certificate, error code: %lu, skipping..." , ERR_get_error () );
267
278
continue ;
268
279
}
269
280
270
281
/* Add X509 struct to the openssl cert store */
271
282
ret = X509_STORE_add_cert (ossl_store , ossl_cert );
272
283
if (!ret ) {
273
- flb_warn ("[tls] Failed to add a certificate to the store: %lu: %s" ,
274
- ERR_get_error (), ERR_error_string (ERR_get_error (), NULL ));
284
+ err = ERR_get_error ();
285
+ if (err == X509_R_CERT_ALREADY_IN_HASH_TABLE ) {
286
+ flb_debug ("[tls] certificate already exists in the store, skipping." );
287
+ }
288
+ else {
289
+ flb_warn ("[tls] failed to add certificate to openssl store. error code: %lu - %s" ,
290
+ err , ERR_error_string (err , NULL ));
291
+ }
275
292
}
276
293
X509_free (ossl_cert );
277
294
}
278
295
}
279
296
297
+ /* Check for errors during enumeration */
298
+ if (GetLastError () != CRYPT_E_NOT_FOUND ) {
299
+ flb_error ("[tls] error occurred while enumerating certificates: %lu" , GetLastError ());
300
+ CertCloseStore (win_store , 0 );
301
+ return -1 ;
302
+ }
303
+
304
+ /* Close the Windows system certificate store */
280
305
if (!CertCloseStore (win_store , 0 )) {
281
- flb_error ("[tls] Cannot close cert store: %i " , GetLastError ());
306
+ flb_error ("[tls] cannot close windows certificate store: %lu " , GetLastError ());
282
307
return -1 ;
283
308
}
309
+
310
+ flb_debug ("[tls] successfully loaded certificates from windows system store." );
284
311
return 0 ;
285
312
}
286
313
#endif
0 commit comments