Skip to content

Commit cf85f89

Browse files
niedbalskiJorge Niedbalski
and
Jorge Niedbalski
authored
tls: improve windows system certificates load debug information (#9533)
gives detailed information on the cause of the failure when loading system certificates. Signed-off-by: Jorge Niedbalski <[email protected]> Co-authored-by: Jorge Niedbalski <[email protected]>
1 parent 642716a commit cf85f89

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/tls/openssl.c

+33-6
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,28 @@ static int windows_load_system_certificates(struct tls_context *ctx)
241241
{
242242
int ret;
243243
HANDLE win_store;
244+
unsigned long err;
244245
PCCERT_CONTEXT win_cert = NULL;
245246
const unsigned char *win_cert_data;
246247
X509_STORE *ossl_store = SSL_CTX_get_cert_store(ctx->ctx);
247248
X509 *ossl_cert;
248249

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 */
249257
win_store = CertOpenSystemStoreA(0, "Root");
250258
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());
252260
return -1;
253261
}
254262

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 */
256266
if (win_cert->dwCertEncodingType & X509_ASN_ENCODING) {
257267
/*
258268
* Decode the certificate into X509 struct.
@@ -262,25 +272,42 @@ static int windows_load_system_certificates(struct tls_context *ctx)
262272
*/
263273
win_cert_data = win_cert->pbCertEncoded;
264274
ossl_cert = d2i_X509(NULL, &win_cert_data, win_cert->cbCertEncoded);
275+
265276
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());
267278
continue;
268279
}
269280

270281
/* Add X509 struct to the openssl cert store */
271282
ret = X509_STORE_add_cert(ossl_store, ossl_cert);
272283
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+
}
275292
}
276293
X509_free(ossl_cert);
277294
}
278295
}
279296

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 */
280305
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());
282307
return -1;
283308
}
309+
310+
flb_debug("[tls] successfully loaded certificates from windows system store.");
284311
return 0;
285312
}
286313
#endif

0 commit comments

Comments
 (0)