Skip to content

Commit 923dec2

Browse files
cosmo0920edsiper
authored andcommitted
tls: openssl: Handle location prefix more strictly
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent ef3f075 commit 923dec2

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/tls/openssl.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,20 @@ int tls_context_alpn_set(void *ctx_backend, const char *alpn)
317317
* If no known prefix is found, *store_name_out is left as-is and *location_flags
318318
* is not modified (so legacy behavior is preserved).
319319
*/
320-
static void windows_resolve_certstore_location(const char *configured_name,
321-
DWORD *location_flags,
322-
const char **store_name_out)
320+
static int windows_resolve_certstore_location(const char *configured_name,
321+
DWORD *location_flags,
322+
const char **store_name_out)
323323
{
324324
const char *name;
325325
const char *sep;
326326
size_t prefix_len;
327327
char prefix_buf[32];
328328
size_t i;
329329
size_t len = 0;
330+
char c;
330331

331332
if (!configured_name || !*configured_name) {
332-
return;
333+
return FLB_FALSE;
333334
}
334335

335336
name = configured_name;
@@ -353,7 +354,8 @@ static void windows_resolve_certstore_location(const char *configured_name,
353354
* -> keep legacy behavior (location_flags unchanged).
354355
*/
355356
*store_name_out = name;
356-
return;
357+
358+
return FLB_FALSE;
357359
}
358360

359361
/* Copy and lowercase prefix into buffer */
@@ -363,7 +365,7 @@ static void windows_resolve_certstore_location(const char *configured_name,
363365
}
364366

365367
for (i = 0; i < prefix_len; i++) {
366-
char c = (char) name[i];
368+
c = (char) name[i];
367369

368370
if (c >= 'A' && c <= 'Z') {
369371
c = (char) (c - 'A' + 'a');
@@ -373,27 +375,29 @@ static void windows_resolve_certstore_location(const char *configured_name,
373375
prefix_buf[prefix_len] = '\0';
374376

375377
/* Default: keep *location_flags as-is */
376-
if (strncasecmp(prefix_buf, "currentuser", 11) == 0 ||
377-
strncasecmp(prefix_buf, "hkcu", 4) == 0) {
378+
if (strcmp(prefix_buf, "currentuser") == 0 ||
379+
strcmp(prefix_buf, "hkcu") == 0) {
378380
*location_flags = CERT_SYSTEM_STORE_CURRENT_USER;
379381
}
380-
else if (strncasecmp(prefix_buf, "localmachine", 12) == 0 ||
381-
strncasecmp(prefix_buf, "hklm", 4) == 0) {
382+
else if (strcmp(prefix_buf, "localmachine") == 0 ||
383+
strcmp(prefix_buf, "hklm") == 0) {
382384
*location_flags = CERT_SYSTEM_STORE_LOCAL_MACHINE;
383385
}
384-
else if (strncasecmp(prefix_buf, "localmachineenterprise", 22) == 0 ||
385-
strncasecmp(prefix_buf, "hklme", 5) == 0) {
386+
else if (strcmp(prefix_buf, "localmachineenterprise") == 0 ||
387+
strcmp(prefix_buf, "hklme") == 0) {
386388
*location_flags = CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE;
387389
}
388390
else {
389391
/* Unknown prefix -> treat entire string as store name */
390392
*store_name_out = configured_name;
391393

392-
return;
394+
return FLB_FALSE;
393395
}
394396

395397
/* Store name part after the separator "\" or "/" */
396398
*store_name_out = sep + 1;
399+
400+
return FLB_TRUE;
397401
}
398402

399403
static int windows_load_system_certificates(struct tls_context *ctx)
@@ -408,6 +412,7 @@ static int windows_load_system_certificates(struct tls_context *ctx)
408412
char *configured_name = "Root";
409413
const char *store_name = "Root";
410414
DWORD store_location = CERT_SYSTEM_STORE_CURRENT_USER;
415+
int has_location_prefix = FLB_FALSE;
411416

412417
/* Check if OpenSSL certificate store is available */
413418
if (!ossl_store) {
@@ -421,15 +426,15 @@ static int windows_load_system_certificates(struct tls_context *ctx)
421426
}
422427

423428
/* First, resolve explicit prefix if present */
424-
windows_resolve_certstore_location(configured_name,
425-
&store_location,
426-
&store_name);
429+
has_location_prefix = windows_resolve_certstore_location(configured_name,
430+
&store_location,
431+
&store_name);
427432

428433
/* Backward compatibility:
429434
* If no prefix was given (store_name == configured_name) and
430435
* use_enterprise_store is set, override location accordingly.
431436
*/
432-
if (store_name == configured_name && ctx->use_enterprise_store) {
437+
if (has_location_prefix == FLB_FALSE && ctx->use_enterprise_store) {
433438
store_location = CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE;
434439
}
435440

0 commit comments

Comments
 (0)