Skip to content

Commit c8d96ec

Browse files
committed
improve errors when searching windows cert store
- Replace an AWS_FATAL_ASSERT() with actual error handling: User-inputted strings should not crash a program. - Improved error logging: Quotes around paths. Describes how to fix problems with paths.
1 parent d4a2a8b commit c8d96ec

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

source/windows/windows_pki_utils.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
int aws_load_cert_from_system_cert_store(const char *cert_path, HCERTSTORE *cert_store, PCCERT_CONTEXT *certs) {
2525

26-
AWS_LOGF_INFO(AWS_LS_IO_PKI, "static: loading certificate at windows cert manager path %s.", cert_path);
26+
AWS_LOGF_INFO(AWS_LS_IO_PKI, "static: loading certificate at windows cert manager path '%s'.", cert_path);
2727
char *location_of_next_segment = strchr(cert_path, '\\');
2828

2929
if (!location_of_next_segment) {
30-
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: invalid certificate path %s.", cert_path);
30+
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: invalid certificate path '%s'. Must use '\\' as separator.", cert_path);
3131
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
3232
}
3333

@@ -52,7 +52,13 @@ int aws_load_cert_from_system_cert_store(const char *cert_path, HCERTSTORE *cert
5252
store_val = CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE;
5353
} else {
5454
AWS_LOGF_ERROR(
55-
AWS_LS_IO_PKI, "static: certificate path %s does not contain a valid cert store identifier.", cert_path);
55+
AWS_LS_IO_PKI,
56+
"static: invalid certificate path '%s'. System store location '%.*s' not recognized."
57+
" Expected something like 'CurrentUser'.",
58+
cert_path,
59+
(int)store_name_len,
60+
cert_path);
61+
5662
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
5763
}
5864

@@ -62,22 +68,26 @@ int aws_load_cert_from_system_cert_store(const char *cert_path, HCERTSTORE *cert
6268
location_of_next_segment = strchr(location_of_next_segment, '\\');
6369

6470
if (!location_of_next_segment) {
65-
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: invalid certificate path %s.", cert_path);
71+
AWS_LOGF_ERROR(
72+
AWS_LS_IO_PKI, "static: invalid certificate path '%s'. Expected additional '\\' separator.", cert_path);
6673
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
6774
}
6875

6976
/* The store_val value has to be only the path segment related to the physical store. Looking
7077
at the docs, 128 bytes should be plenty to store that segment.
7178
https://docs.microsoft.com/en-us/windows/desktop/SecCrypto/system-store-locations */
7279
char store_path[128] = {0};
73-
AWS_FATAL_ASSERT(location_of_next_segment - store_path_start < sizeof(store_path));
80+
if (location_of_next_segment - store_path_start >= sizeof(store_path)) {
81+
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: invalid certificate path '%s'. Store name is too long.", cert_path);
82+
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
83+
}
7484
memcpy(store_path, store_path_start, location_of_next_segment - store_path_start);
7585

7686
location_of_next_segment += 1;
7787
if (strlen(location_of_next_segment) != CERT_HASH_STR_LEN) {
7888
AWS_LOGF_ERROR(
7989
AWS_LS_IO_PKI,
80-
"static: invalid certificate path %s. %s should have been"
90+
"static: invalid certificate path '%s'. '%s' should have been"
8191
" 40 bytes of hex encoded data",
8292
cert_path,
8393
location_of_next_segment);
@@ -90,7 +100,7 @@ int aws_load_cert_from_system_cert_store(const char *cert_path, HCERTSTORE *cert
90100
if (!*cert_store) {
91101
AWS_LOGF_ERROR(
92102
AWS_LS_IO_PKI,
93-
"static: invalid certificate path %s. Failed to load cert store with error code %d",
103+
"static: invalid certificate path '%s'. Failed to load cert store with error code %d",
94104
cert_path,
95105
(int)GetLastError());
96106
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
@@ -112,7 +122,7 @@ int aws_load_cert_from_system_cert_store(const char *cert_path, HCERTSTORE *cert
112122
NULL)) {
113123
AWS_LOGF_ERROR(
114124
AWS_LS_IO_PKI,
115-
"static: invalid certificate path %s. %s should have been a hex encoded string",
125+
"static: invalid certificate path '%s'. '%s' should have been a hex encoded string",
116126
cert_path,
117127
location_of_next_segment);
118128
aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
@@ -125,7 +135,7 @@ int aws_load_cert_from_system_cert_store(const char *cert_path, HCERTSTORE *cert
125135
if (!*certs) {
126136
AWS_LOGF_ERROR(
127137
AWS_LS_IO_PKI,
128-
"static: invalid certificate path %s. "
138+
"static: invalid certificate path '%s'. "
129139
"The referenced certificate was not found in the certificate store, error code %d",
130140
cert_path,
131141
(int)GetLastError());

0 commit comments

Comments
 (0)