| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/io/pem.h> |
| 7 |
#include <aws/io/private/pki_utils.h> |
| 8 |
|
| 9 |
#include <aws/common/uuid.h> |
| 10 |
|
| 11 |
#include <aws/io/logging.h> |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#include <windows.h> |
| 16 |
|
| 17 |
#ifdef _MSC_VER |
| 18 |
# pragma warning(disable : 4221) |
| 19 |
# pragma warning(disable : 4204) |
| 20 |
#endif |
| 21 |
|
| 22 |
#define CERT_HASH_STR_LEN 40 |
| 23 |
#define CERT_HASH_LEN 20 |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
static int s_split_system_cert_path(const char *cert_path, struct aws_byte_cursor out_splits[3]) { |
| 31 |
|
| 32 |
struct aws_byte_cursor cert_path_cursor = aws_byte_cursor_from_c_str(cert_path); |
| 33 |
|
| 34 |
struct aws_byte_cursor segment; |
| 35 |
AWS_ZERO_STRUCT(segment); |
| 36 |
|
| 37 |
for (size_t i = 0; i < 3; ++i) { |
| 38 |
if (!aws_byte_cursor_next_split(&cert_path_cursor, '\\', &segment)) { |
| 39 |
AWS_LOGF_ERROR( |
| 40 |
AWS_LS_IO_PKI, "static: invalid certificate path '%s'. Expected additional '\\' separator.", cert_path); |
| 41 |
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 42 |
} |
| 43 |
|
| 44 |
out_splits[i] = segment; |
| 45 |
} |
| 46 |
|
| 47 |
if (aws_byte_cursor_next_split(&cert_path_cursor, '\\', &segment)) { |
| 48 |
AWS_LOGF_ERROR( |
| 49 |
AWS_LS_IO_PKI, "static: invalid certificate path '%s'. Too many '\\' separators found.", cert_path); |
| 50 |
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 51 |
} |
| 52 |
|
| 53 |
return AWS_OP_SUCCESS; |
| 54 |
} |
| 55 |
|
| 56 |
int aws_load_cert_from_system_cert_store(const char *cert_path, HCERTSTORE *cert_store, PCCERT_CONTEXT *certs) { |
| 57 |
|
| 58 |
AWS_LOGF_INFO(AWS_LS_IO_PKI, "static: loading certificate at windows cert manager path '%s'.", cert_path); |
| 59 |
|
| 60 |
struct aws_byte_cursor segments[3]; |
| 61 |
if (s_split_system_cert_path(cert_path, segments)) { |
| 62 |
return AWS_OP_ERR; |
| 63 |
} |
| 64 |
const struct aws_byte_cursor store_location = segments[0]; |
| 65 |
const struct aws_byte_cursor store_path_cursor = segments[1]; |
| 66 |
const struct aws_byte_cursor cert_hash_cursor = segments[2]; |
| 67 |
|
| 68 |
DWORD store_val = 0; |
| 69 |
if (aws_byte_cursor_eq_c_str_ignore_case(&store_location, "CurrentUser")) { |
| 70 |
store_val = CERT_SYSTEM_STORE_CURRENT_USER; |
| 71 |
} else if (aws_byte_cursor_eq_c_str_ignore_case(&store_location, "LocalMachine")) { |
| 72 |
store_val = CERT_SYSTEM_STORE_LOCAL_MACHINE; |
| 73 |
} else if (aws_byte_cursor_eq_c_str_ignore_case(&store_location, "CurrentService")) { |
| 74 |
store_val = CERT_SYSTEM_STORE_CURRENT_SERVICE; |
| 75 |
} else if (aws_byte_cursor_eq_c_str_ignore_case(&store_location, "Services")) { |
| 76 |
store_val = CERT_SYSTEM_STORE_SERVICES; |
| 77 |
} else if (aws_byte_cursor_eq_c_str_ignore_case(&store_location, "Users")) { |
| 78 |
store_val = CERT_SYSTEM_STORE_USERS; |
| 79 |
} else if (aws_byte_cursor_eq_c_str_ignore_case(&store_location, "CurrentUserGroupPolicy")) { |
| 80 |
store_val = CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY; |
| 81 |
} else if (aws_byte_cursor_eq_c_str_ignore_case(&store_location, "LocalMachineGroupPolicy")) { |
| 82 |
store_val = CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY; |
| 83 |
} else if (aws_byte_cursor_eq_c_str_ignore_case(&store_location, "LocalMachineEnterprise")) { |
| 84 |
store_val = CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE; |
| 85 |
} else { |
| 86 |
AWS_LOGF_ERROR( |
| 87 |
AWS_LS_IO_PKI, |
| 88 |
"static: invalid certificate path '%s'. System store location '" PRInSTR "' not recognized." |
| 89 |
" Expected something like 'CurrentUser'.", |
| 90 |
cert_path, |
| 91 |
AWS_BYTE_CURSOR_PRI(store_location)); |
| 92 |
|
| 93 |
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 94 |
} |
| 95 |
|
| 96 |
AWS_LOGF_DEBUG(AWS_LS_IO_PKI, "static: determined registry value for lookup as %d.", (int)store_val); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
char store_path[128] = {0}; |
| 102 |
if (store_path_cursor.len >= sizeof(store_path)) { |
| 103 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: invalid certificate path '%s'. Store name is too long.", cert_path); |
| 104 |
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 105 |
} |
| 106 |
memcpy(store_path, store_path_cursor.ptr, store_path_cursor.len); |
| 107 |
|
| 108 |
if (cert_hash_cursor.len != CERT_HASH_STR_LEN) { |
| 109 |
AWS_LOGF_ERROR( |
| 110 |
AWS_LS_IO_PKI, |
| 111 |
"static: invalid certificate path '%s'. '" PRInSTR "' should have been" |
| 112 |
" 40 bytes of hex encoded data", |
| 113 |
cert_path, |
| 114 |
AWS_BYTE_CURSOR_PRI(cert_hash_cursor)); |
| 115 |
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 116 |
} |
| 117 |
|
| 118 |
*cert_store = CertOpenStore( |
| 119 |
CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, CERT_STORE_OPEN_EXISTING_FLAG | store_val, store_path); |
| 120 |
|
| 121 |
if (!*cert_store) { |
| 122 |
AWS_LOGF_ERROR( |
| 123 |
AWS_LS_IO_PKI, |
| 124 |
"static: invalid certificate path '%s'. Failed to load cert store with error code %d", |
| 125 |
cert_path, |
| 126 |
(int)GetLastError()); |
| 127 |
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 128 |
} |
| 129 |
|
| 130 |
BYTE cert_hash_data[CERT_HASH_LEN]; |
| 131 |
CRYPT_HASH_BLOB cert_hash = { |
| 132 |
.pbData = cert_hash_data, |
| 133 |
.cbData = CERT_HASH_LEN, |
| 134 |
}; |
| 135 |
|
| 136 |
if (!CryptStringToBinaryA( |
| 137 |
(LPCSTR)cert_hash_cursor.ptr, |
| 138 |
CERT_HASH_STR_LEN, |
| 139 |
CRYPT_STRING_HEX, |
| 140 |
cert_hash.pbData, |
| 141 |
&cert_hash.cbData, |
| 142 |
NULL, |
| 143 |
NULL)) { |
| 144 |
AWS_LOGF_ERROR( |
| 145 |
AWS_LS_IO_PKI, |
| 146 |
"static: invalid certificate path '%s'. '" PRInSTR "' should have been a hex encoded string", |
| 147 |
cert_path, |
| 148 |
AWS_BYTE_CURSOR_PRI(cert_hash_cursor)); |
| 149 |
aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 150 |
goto on_error; |
| 151 |
} |
| 152 |
|
| 153 |
*certs = CertFindCertificateInStore( |
| 154 |
*cert_store, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_HASH, &cert_hash, NULL); |
| 155 |
|
| 156 |
if (!*certs) { |
| 157 |
AWS_LOGF_ERROR( |
| 158 |
AWS_LS_IO_PKI, |
| 159 |
"static: invalid certificate path '%s'. " |
| 160 |
"The referenced certificate was not found in the certificate store, error code %d", |
| 161 |
cert_path, |
| 162 |
(int)GetLastError()); |
| 163 |
aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 164 |
goto on_error; |
| 165 |
} |
| 166 |
|
| 167 |
return AWS_OP_SUCCESS; |
| 168 |
|
| 169 |
on_error: |
| 170 |
|
| 171 |
if (*cert_store != NULL) { |
| 172 |
aws_close_cert_store(*cert_store); |
| 173 |
*cert_store = NULL; |
| 174 |
} |
| 175 |
|
| 176 |
return AWS_OP_ERR; |
| 177 |
} |
| 178 |
|
| 179 |
int aws_import_trusted_certificates( |
| 180 |
struct aws_allocator *alloc, |
| 181 |
const struct aws_byte_cursor *certificates_blob, |
| 182 |
HCERTSTORE *cert_store) { |
| 183 |
struct aws_array_list certificates; |
| 184 |
*cert_store = NULL; |
| 185 |
int result = AWS_OP_ERR; |
| 186 |
|
| 187 |
if (aws_pem_objects_init_from_file_contents(&certificates, alloc, *certificates_blob)) { |
| 188 |
goto clean_up; |
| 189 |
} |
| 190 |
|
| 191 |
size_t cert_count = aws_array_list_length(&certificates); |
| 192 |
if (cert_count == 0) { |
| 193 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 194 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: no certificates found, error %s", aws_error_name(aws_last_error())); |
| 195 |
goto clean_up; |
| 196 |
} |
| 197 |
|
| 198 |
HCERTSTORE tmp_cert_store = |
| 199 |
CertOpenStore(CERT_STORE_PROV_MEMORY, 0, (ULONG_PTR)NULL, CERT_STORE_CREATE_NEW_FLAG, NULL); |
| 200 |
*cert_store = tmp_cert_store; |
| 201 |
if (!*cert_store) { |
| 202 |
AWS_LOGF_ERROR( |
| 203 |
AWS_LS_IO_PKI, "static: failed to create temporary cert store, error code %d", (int)GetLastError()); |
| 204 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 205 |
goto clean_up; |
| 206 |
} |
| 207 |
|
| 208 |
AWS_LOGF_INFO(AWS_LS_IO_PKI, "static: loading %d certificates in cert chain for use as a CA", (int)cert_count); |
| 209 |
for (size_t i = 0; i < cert_count; ++i) { |
| 210 |
struct aws_pem_object *pem_object_ptr = NULL; |
| 211 |
aws_array_list_get_at_ptr(&certificates, (void **)&pem_object_ptr, i); |
| 212 |
|
| 213 |
CERT_BLOB cert_blob; |
| 214 |
CERT_CONTEXT *cert_context = NULL; |
| 215 |
|
| 216 |
cert_blob.pbData = pem_object_ptr->data.buffer; |
| 217 |
cert_blob.cbData = (DWORD)pem_object_ptr->data.len; |
| 218 |
|
| 219 |
DWORD content_type = 0; |
| 220 |
BOOL query_res = CryptQueryObject( |
| 221 |
CERT_QUERY_OBJECT_BLOB, |
| 222 |
&cert_blob, |
| 223 |
CERT_QUERY_CONTENT_FLAG_CERT, |
| 224 |
CERT_QUERY_FORMAT_FLAG_ALL, |
| 225 |
0, |
| 226 |
NULL, |
| 227 |
&content_type, |
| 228 |
NULL, |
| 229 |
NULL, |
| 230 |
NULL, |
| 231 |
(const void **)&cert_context); |
| 232 |
|
| 233 |
if (!query_res || cert_context == NULL) { |
| 234 |
AWS_LOGF_ERROR( |
| 235 |
AWS_LS_IO_PKI, "static: failed to parse certificate blob, error code %d", (int)GetLastError()); |
| 236 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 237 |
goto clean_up; |
| 238 |
} |
| 239 |
|
| 240 |
BOOL add_result = CertAddCertificateContextToStore(*cert_store, cert_context, CERT_STORE_ADD_ALWAYS, NULL); |
| 241 |
if (!add_result) { |
| 242 |
AWS_LOGF_ERROR( |
| 243 |
AWS_LS_IO_PKI, "static: failed to add certificate to store, error code %d", (int)GetLastError()); |
| 244 |
} |
| 245 |
|
| 246 |
CertFreeCertificateContext(cert_context); |
| 247 |
|
| 248 |
if (!add_result) { |
| 249 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 250 |
goto clean_up; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
result = AWS_OP_SUCCESS; |
| 255 |
|
| 256 |
clean_up: |
| 257 |
|
| 258 |
aws_pem_objects_clean_up(&certificates); |
| 259 |
|
| 260 |
if (result == AWS_OP_ERR && *cert_store) { |
| 261 |
aws_close_cert_store(*cert_store); |
| 262 |
*cert_store = NULL; |
| 263 |
} |
| 264 |
|
| 265 |
return result; |
| 266 |
} |
| 267 |
|
| 268 |
void aws_close_cert_store(HCERTSTORE cert_store) { |
| 269 |
CertCloseStore(cert_store, 0); |
| 270 |
} |
| 271 |
|
| 272 |
enum aws_rsa_private_key_container_type { |
| 273 |
AWS_RPKCT_PERSIST_TO_USER_PROFILE, |
| 274 |
AWS_RPKCT_PERSIST_TO_GLOBAL, |
| 275 |
AWS_RPKCT_EPHEMERAL, |
| 276 |
}; |
| 277 |
|
| 278 |
static int s_cert_context_import_rsa_private_key_to_key_container( |
| 279 |
PCCERT_CONTEXT certs, |
| 280 |
const BYTE *key, |
| 281 |
DWORD decoded_len, |
| 282 |
wchar_t uuid_wstr[AWS_UUID_STR_LEN], |
| 283 |
enum aws_rsa_private_key_container_type key_container_type, |
| 284 |
HCRYPTPROV *out_crypto_provider, |
| 285 |
HCRYPTKEY *out_private_key_handle, |
| 286 |
bool *tls13_disabled) { |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
HCRYPTPROV crypto_prov = 0; |
| 291 |
HCRYPTKEY h_key = 0; |
| 292 |
|
| 293 |
const wchar_t *container_name = NULL; |
| 294 |
DWORD acquire_context_flags = 0; |
| 295 |
|
| 296 |
switch (key_container_type) { |
| 297 |
case AWS_RPKCT_PERSIST_TO_USER_PROFILE: |
| 298 |
container_name = uuid_wstr; |
| 299 |
acquire_context_flags = CRYPT_NEWKEYSET; |
| 300 |
break; |
| 301 |
case AWS_RPKCT_PERSIST_TO_GLOBAL: |
| 302 |
container_name = uuid_wstr; |
| 303 |
acquire_context_flags = CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET; |
| 304 |
break; |
| 305 |
case AWS_RPKCT_EPHEMERAL: |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
if (!CryptAcquireContextW(&crypto_prov, container_name, NULL, PROV_RSA_FULL, acquire_context_flags)) { |
| 310 |
AWS_LOGF_WARN( |
| 311 |
AWS_LS_IO_PKI, |
| 312 |
"static: error creating a new rsa crypto context for key: key container type %d; error code %d", |
| 313 |
(int)key_container_type, |
| 314 |
(int)GetLastError()); |
| 315 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 316 |
goto on_error; |
| 317 |
} |
| 318 |
|
| 319 |
if (!CryptImportKey(crypto_prov, key, decoded_len, 0, 0, &h_key)) { |
| 320 |
AWS_LOGF_ERROR( |
| 321 |
AWS_LS_IO_PKI, |
| 322 |
"static: failed to import rsa key into crypto provider: key container type %d; error code %d", |
| 323 |
(int)key_container_type, |
| 324 |
GetLastError()); |
| 325 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 326 |
goto on_error; |
| 327 |
} |
| 328 |
|
| 329 |
if (key_container_type == AWS_RPKCT_EPHEMERAL) { |
| 330 |
if (!CertSetCertificateContextProperty(certs, CERT_KEY_PROV_HANDLE_PROP_ID, 0, (void *)crypto_prov)) { |
| 331 |
AWS_LOGF_ERROR( |
| 332 |
AWS_LS_IO_PKI, |
| 333 |
"static: error setting a certificate context property for rsa key: key container type %d; error code " |
| 334 |
"%d", |
| 335 |
(int)key_container_type, |
| 336 |
(int)GetLastError()); |
| 337 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 338 |
goto on_error; |
| 339 |
} |
| 340 |
|
| 341 |
AWS_LOGF_INFO(AWS_LS_IO_PKI, "static: TLS 1.3 does not support ephemeral keys, disabling TLS 1.3"); |
| 342 |
*tls13_disabled = true; |
| 343 |
} else { |
| 344 |
CRYPT_KEY_PROV_INFO key_prov_info; |
| 345 |
AWS_ZERO_STRUCT(key_prov_info); |
| 346 |
key_prov_info.pwszContainerName = uuid_wstr; |
| 347 |
key_prov_info.dwProvType = PROV_RSA_FULL; |
| 348 |
if (key_container_type == AWS_RPKCT_PERSIST_TO_GLOBAL) { |
| 349 |
key_prov_info.dwFlags = CRYPT_MACHINE_KEYSET; |
| 350 |
} |
| 351 |
key_prov_info.dwKeySpec = AT_KEYEXCHANGE; |
| 352 |
|
| 353 |
if (!CertSetCertificateContextProperty(certs, CERT_KEY_PROV_INFO_PROP_ID, 0, &key_prov_info)) { |
| 354 |
AWS_LOGF_ERROR( |
| 355 |
AWS_LS_IO_PKI, |
| 356 |
"static: error setting a certificate context property: key container type %d; error code %d", |
| 357 |
(int)key_container_type, |
| 358 |
(int)GetLastError()); |
| 359 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 360 |
goto on_error; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
AWS_LOGF_DEBUG( |
| 365 |
AWS_LS_IO_PKI, "static: successfully imported rsa private key, key container type %d", (int)key_container_type); |
| 366 |
|
| 367 |
*out_crypto_provider = crypto_prov; |
| 368 |
*out_private_key_handle = h_key; |
| 369 |
return AWS_OP_SUCCESS; |
| 370 |
|
| 371 |
on_error: |
| 372 |
if (h_key != 0) { |
| 373 |
CryptDestroyKey(h_key); |
| 374 |
} |
| 375 |
|
| 376 |
if (crypto_prov != 0) { |
| 377 |
CryptReleaseContext(crypto_prov, 0); |
| 378 |
} |
| 379 |
|
| 380 |
return AWS_OP_ERR; |
| 381 |
} |
| 382 |
|
| 383 |
static int s_cert_context_import_rsa_private_key( |
| 384 |
PCCERT_CONTEXT certs, |
| 385 |
const BYTE *key, |
| 386 |
DWORD decoded_len, |
| 387 |
bool is_client_mode, |
| 388 |
wchar_t uuid_wstr[AWS_UUID_STR_LEN], |
| 389 |
HCRYPTPROV *out_crypto_provider, |
| 390 |
HCRYPTKEY *out_private_key_handle, |
| 391 |
bool *tls13_disabled) { |
| 392 |
|
| 393 |
const enum aws_rsa_private_key_container_type client_available_key_container_types[] = { |
| 394 |
AWS_RPKCT_PERSIST_TO_USER_PROFILE, |
| 395 |
AWS_RPKCT_PERSIST_TO_GLOBAL, |
| 396 |
AWS_RPKCT_EPHEMERAL, |
| 397 |
}; |
| 398 |
|
| 399 |
|
| 400 |
const enum aws_rsa_private_key_container_type server_available_key_container_types[] = { |
| 401 |
AWS_RPKCT_PERSIST_TO_USER_PROFILE, |
| 402 |
AWS_RPKCT_PERSIST_TO_GLOBAL, |
| 403 |
}; |
| 404 |
|
| 405 |
size_t key_container_types_num = is_client_mode ? AWS_ARRAY_SIZE(client_available_key_container_types) |
| 406 |
: AWS_ARRAY_SIZE(server_available_key_container_types); |
| 407 |
const enum aws_rsa_private_key_container_type *available_key_container_types = |
| 408 |
is_client_mode ? client_available_key_container_types : server_available_key_container_types; |
| 409 |
|
| 410 |
|
| 411 |
for (size_t i = 0; i < key_container_types_num; ++i) { |
| 412 |
if (s_cert_context_import_rsa_private_key_to_key_container( |
| 413 |
certs, |
| 414 |
key, |
| 415 |
decoded_len, |
| 416 |
uuid_wstr, |
| 417 |
available_key_container_types[i], |
| 418 |
out_crypto_provider, |
| 419 |
out_private_key_handle, |
| 420 |
tls13_disabled) == AWS_OP_SUCCESS) { |
| 421 |
return AWS_OP_SUCCESS; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
return AWS_OP_ERR; |
| 426 |
} |
| 427 |
|
| 428 |
#define ECC_256_MAGIC_NUMBER 0x20 |
| 429 |
#define ECC_384_MAGIC_NUMBER 0x30 |
| 430 |
|
| 431 |
static ULONG s_compute_ecc_key_type_from_private_key_size(size_t private_key_len) { |
| 432 |
switch (private_key_len) { |
| 433 |
case ECC_256_MAGIC_NUMBER: |
| 434 |
return BCRYPT_ECDSA_PRIVATE_P256_MAGIC; |
| 435 |
case ECC_384_MAGIC_NUMBER: |
| 436 |
return BCRYPT_ECDSA_PRIVATE_P384_MAGIC; |
| 437 |
default: |
| 438 |
return BCRYPT_ECDSA_PRIVATE_P521_MAGIC; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
#ifndef AWS_SUPPORT_WIN7 |
| 443 |
|
| 444 |
enum aws_ecc_public_key_compression_type { |
| 445 |
AWS_EPKCT_COMPRESSED_EVEN = 0x02, |
| 446 |
AWS_EPKCT_COMPRESSED_ODD = 0x03, |
| 447 |
AWS_EPKCT_UNCOMPRESSED = 0x04, |
| 448 |
}; |
| 449 |
|
| 450 |
|
| 451 |
static int s_cert_context_import_ecc_private_key( |
| 452 |
PCCERT_CONTEXT cert_context, |
| 453 |
struct aws_allocator *allocator, |
| 454 |
const BYTE *key, |
| 455 |
DWORD decoded_len, |
| 456 |
wchar_t uuid_wstr[AWS_UUID_STR_LEN]) { |
| 457 |
|
| 458 |
(void)decoded_len; |
| 459 |
|
| 460 |
AWS_FATAL_ASSERT(cert_context != NULL); |
| 461 |
|
| 462 |
NCRYPT_PROV_HANDLE crypto_prov = 0; |
| 463 |
NCRYPT_KEY_HANDLE h_key = 0; |
| 464 |
BCRYPT_ECCKEY_BLOB *key_blob = NULL; |
| 465 |
int result = AWS_OP_ERR; |
| 466 |
SECURITY_STATUS status; |
| 467 |
|
| 468 |
CRYPT_BIT_BLOB *public_key_blob = &cert_context->pCertInfo->SubjectPublicKeyInfo.PublicKey; |
| 469 |
DWORD public_key_blob_length = public_key_blob->cbData; |
| 470 |
if (public_key_blob_length == 0) { |
| 471 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: invalid zero-length ecc key data"); |
| 472 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 473 |
goto done; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
if (*public_key_blob->pbData != AWS_EPKCT_UNCOMPRESSED) { |
| 490 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: compressed ecc public keys not yet supported."); |
| 491 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 492 |
goto done; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
public_key_blob_length--; |
| 501 |
struct aws_byte_cursor public_blob_cursor = { |
| 502 |
.ptr = public_key_blob->pbData + 1, |
| 503 |
.len = public_key_blob_length, |
| 504 |
}; |
| 505 |
|
| 506 |
CRYPT_ECC_PRIVATE_KEY_INFO *private_key_info = (CRYPT_ECC_PRIVATE_KEY_INFO *)key; |
| 507 |
ULONG private_key_length = private_key_info->PrivateKey.cbData; |
| 508 |
struct aws_byte_cursor private_key_cursor = { |
| 509 |
.ptr = private_key_info->PrivateKey.pbData, |
| 510 |
.len = private_key_length, |
| 511 |
}; |
| 512 |
|
| 513 |
DWORD key_blob_size = sizeof(BCRYPT_ECCKEY_BLOB) + public_key_blob_length + private_key_length; |
| 514 |
key_blob = (BCRYPT_ECCKEY_BLOB *)aws_mem_calloc(allocator, 1, key_blob_size); |
| 515 |
if (key_blob == NULL) { |
| 516 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: could not allocate ecc key blob memory"); |
| 517 |
goto done; |
| 518 |
} |
| 519 |
|
| 520 |
key_blob->dwMagic = s_compute_ecc_key_type_from_private_key_size(private_key_cursor.len); |
| 521 |
key_blob->cbKey = private_key_length; |
| 522 |
|
| 523 |
struct aws_byte_buf key_blob_buffer = { |
| 524 |
.buffer = (uint8_t *)key_blob, |
| 525 |
.len = sizeof(BCRYPT_ECCKEY_BLOB), |
| 526 |
.capacity = key_blob_size, |
| 527 |
}; |
| 528 |
|
| 529 |
if (aws_byte_buf_append(&key_blob_buffer, &public_blob_cursor)) { |
| 530 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: insufficient space to build ecc key blob"); |
| 531 |
goto done; |
| 532 |
} |
| 533 |
|
| 534 |
if (aws_byte_buf_append(&key_blob_buffer, &private_key_cursor)) { |
| 535 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: insufficient space to build ecc key blob"); |
| 536 |
goto done; |
| 537 |
} |
| 538 |
|
| 539 |
status = NCryptOpenStorageProvider(&crypto_prov, MS_KEY_STORAGE_PROVIDER, 0); |
| 540 |
if (status != ERROR_SUCCESS) { |
| 541 |
AWS_LOGF_ERROR( |
| 542 |
AWS_LS_IO_PKI, "static: could not open ncrypt key storage provider, error %d", (int)GetLastError()); |
| 543 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 544 |
goto done; |
| 545 |
} |
| 546 |
|
| 547 |
NCryptBuffer ncBuf = {AWS_UUID_STR_LEN * sizeof(wchar_t), NCRYPTBUFFER_PKCS_KEY_NAME, uuid_wstr}; |
| 548 |
NCryptBufferDesc ncBufDesc; |
| 549 |
ncBufDesc.ulVersion = 0; |
| 550 |
ncBufDesc.cBuffers = 1; |
| 551 |
ncBufDesc.pBuffers = &ncBuf; |
| 552 |
|
| 553 |
status = NCryptImportKey( |
| 554 |
crypto_prov, |
| 555 |
0, |
| 556 |
BCRYPT_ECCPRIVATE_BLOB, |
| 557 |
&ncBufDesc, |
| 558 |
&h_key, |
| 559 |
(BYTE *)key_blob, |
| 560 |
key_blob_size, |
| 561 |
NCRYPT_OVERWRITE_KEY_FLAG); |
| 562 |
|
| 563 |
if (status != ERROR_SUCCESS) { |
| 564 |
AWS_LOGF_ERROR( |
| 565 |
AWS_LS_IO_PKI, |
| 566 |
"static: failed to import ecc key with status %d, last error %d", |
| 567 |
status, |
| 568 |
(int)GetLastError()); |
| 569 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 570 |
goto done; |
| 571 |
} |
| 572 |
|
| 573 |
CRYPT_KEY_PROV_INFO key_prov_info = {uuid_wstr, MS_KEY_STORAGE_PROVIDER, 0, 0, 0, NULL, 0}; |
| 574 |
|
| 575 |
if (!CertSetCertificateContextProperty(cert_context, CERT_KEY_PROV_INFO_PROP_ID, 0, &key_prov_info)) { |
| 576 |
AWS_LOGF_ERROR( |
| 577 |
AWS_LS_IO_PKI, "static: failed to set cert context key provider, with last error %d", (int)GetLastError()); |
| 578 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 579 |
goto done; |
| 580 |
} |
| 581 |
|
| 582 |
result = AWS_OP_SUCCESS; |
| 583 |
|
| 584 |
done: |
| 585 |
|
| 586 |
if (h_key != 0) { |
| 587 |
NCryptFreeObject(h_key); |
| 588 |
} |
| 589 |
|
| 590 |
if (crypto_prov != 0) { |
| 591 |
NCryptFreeObject(crypto_prov); |
| 592 |
} |
| 593 |
|
| 594 |
if (key_blob != NULL) { |
| 595 |
aws_mem_release(allocator, key_blob); |
| 596 |
} |
| 597 |
|
| 598 |
return result; |
| 599 |
} |
| 600 |
|
| 601 |
#endif |
| 602 |
|
| 603 |
enum aws_certificate_type { |
| 604 |
AWS_CT_X509_UNKNOWN, |
| 605 |
AWS_CT_X509_RSA, |
| 606 |
AWS_CT_X509_ECC, |
| 607 |
}; |
| 608 |
|
| 609 |
int aws_import_key_pair_to_cert_context( |
| 610 |
struct aws_allocator *alloc, |
| 611 |
const struct aws_byte_cursor *public_cert_chain, |
| 612 |
const struct aws_byte_cursor *private_key, |
| 613 |
bool is_client_mode, |
| 614 |
HCERTSTORE *store, |
| 615 |
PCCERT_CONTEXT *certs, |
| 616 |
HCRYPTPROV *crypto_provider, |
| 617 |
HCRYPTKEY *private_key_handle, |
| 618 |
bool *tls13_disabled) { |
| 619 |
|
| 620 |
struct aws_array_list certificates, private_keys; |
| 621 |
AWS_ZERO_STRUCT(certificates); |
| 622 |
AWS_ZERO_STRUCT(private_keys); |
| 623 |
|
| 624 |
*certs = NULL; |
| 625 |
*store = NULL; |
| 626 |
*crypto_provider = 0; |
| 627 |
*private_key_handle = 0; |
| 628 |
|
| 629 |
int result = AWS_OP_ERR; |
| 630 |
BYTE *key = NULL; |
| 631 |
BYTE *key_wrapper = NULL; |
| 632 |
|
| 633 |
if (aws_pem_objects_init_from_file_contents(&certificates, alloc, *public_cert_chain)) { |
| 634 |
AWS_LOGF_ERROR( |
| 635 |
AWS_LS_IO_PKI, "static: failed to decode cert pem to buffer list with error %d", (int)aws_last_error()); |
| 636 |
goto clean_up; |
| 637 |
} |
| 638 |
|
| 639 |
if (aws_pem_objects_init_from_file_contents(&private_keys, alloc, *private_key)) { |
| 640 |
AWS_LOGF_ERROR( |
| 641 |
AWS_LS_IO_PKI, "static: failed to decode key pem to buffer list with error %d", (int)aws_last_error()); |
| 642 |
goto clean_up; |
| 643 |
} |
| 644 |
|
| 645 |
size_t cert_count = aws_array_list_length(&certificates); |
| 646 |
AWS_LOGF_INFO(AWS_LS_IO_PKI, "static: loading certificate chain with %d certificates.", (int)cert_count); |
| 647 |
*store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, (ULONG_PTR)NULL, CERT_STORE_CREATE_NEW_FLAG, NULL); |
| 648 |
|
| 649 |
if (!*store) { |
| 650 |
AWS_LOGF_ERROR( |
| 651 |
AWS_LS_IO_PKI, |
| 652 |
"static: failed to load in-memory/ephemeral certificate store, error code %d", |
| 653 |
GetLastError()); |
| 654 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 655 |
goto clean_up; |
| 656 |
} |
| 657 |
|
| 658 |
for (size_t i = 0; i < cert_count; ++i) { |
| 659 |
struct aws_pem_object *pem_object_ptr = NULL; |
| 660 |
aws_array_list_get_at_ptr(&certificates, (void **)&pem_object_ptr, i); |
| 661 |
|
| 662 |
CERT_BLOB cert_blob; |
| 663 |
|
| 664 |
cert_blob.pbData = pem_object_ptr->data.buffer; |
| 665 |
cert_blob.cbData = (DWORD)pem_object_ptr->data.len; |
| 666 |
|
| 667 |
DWORD content_type = 0; |
| 668 |
PCERT_CONTEXT cert_context = NULL; |
| 669 |
BOOL query_res = CryptQueryObject( |
| 670 |
CERT_QUERY_OBJECT_BLOB, |
| 671 |
&cert_blob, |
| 672 |
CERT_QUERY_CONTENT_FLAG_CERT, |
| 673 |
CERT_QUERY_FORMAT_FLAG_ALL, |
| 674 |
0, |
| 675 |
NULL, |
| 676 |
&content_type, |
| 677 |
NULL, |
| 678 |
NULL, |
| 679 |
NULL, |
| 680 |
(const void **)&cert_context); |
| 681 |
|
| 682 |
if (!query_res || cert_context == NULL) { |
| 683 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: invalid certificate blob, error code %d.", GetLastError()); |
| 684 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 685 |
goto clean_up; |
| 686 |
} |
| 687 |
|
| 688 |
BOOL add_result = CertAddCertificateContextToStore(*store, cert_context, CERT_STORE_ADD_ALWAYS, NULL); |
| 689 |
if (!add_result) { |
| 690 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: unable to add , error code %d.", GetLastError()); |
| 691 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 692 |
} |
| 693 |
|
| 694 |
if (i != 0 || !add_result) { |
| 695 |
CertFreeCertificateContext(cert_context); |
| 696 |
} else { |
| 697 |
*certs = cert_context; |
| 698 |
} |
| 699 |
|
| 700 |
if (!add_result) { |
| 701 |
goto clean_up; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
if (*certs == NULL) { |
| 706 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 707 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: no certificates found, error %s", aws_error_name(aws_last_error())); |
| 708 |
goto clean_up; |
| 709 |
} |
| 710 |
|
| 711 |
struct aws_pem_object *private_key_ptr = NULL; |
| 712 |
DWORD decoded_len = 0; |
| 713 |
DWORD decoded_wrapper_len = 0; |
| 714 |
enum aws_certificate_type cert_type = AWS_CT_X509_UNKNOWN; |
| 715 |
size_t private_key_count = aws_array_list_length(&private_keys); |
| 716 |
for (size_t i = 0; i < private_key_count; ++i) { |
| 717 |
aws_array_list_get_at_ptr(&private_keys, (void **)&private_key_ptr, i); |
| 718 |
|
| 719 |
if (CryptDecodeObjectEx( |
| 720 |
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 721 |
PKCS_RSA_PRIVATE_KEY, |
| 722 |
private_key_ptr->data.buffer, |
| 723 |
(DWORD)private_key_ptr->data.len, |
| 724 |
CRYPT_DECODE_ALLOC_FLAG, |
| 725 |
0, |
| 726 |
&key, |
| 727 |
&decoded_len)) { |
| 728 |
cert_type = AWS_CT_X509_RSA; |
| 729 |
} else if (CryptDecodeObjectEx( |
| 730 |
X509_ASN_ENCODING, |
| 731 |
PKCS_PRIVATE_KEY_INFO, |
| 732 |
private_key_ptr->data.buffer, |
| 733 |
(DWORD)private_key_ptr->data.len, |
| 734 |
CRYPT_DECODE_ALLOC_FLAG, |
| 735 |
0, |
| 736 |
&key_wrapper, |
| 737 |
&decoded_wrapper_len)) { |
| 738 |
CRYPT_PRIVATE_KEY_INFO *pPrivateKeyInfoStruct = (CRYPT_PRIVATE_KEY_INFO *)key_wrapper; |
| 739 |
if (CryptDecodeObjectEx( |
| 740 |
X509_ASN_ENCODING, |
| 741 |
PKCS_RSA_PRIVATE_KEY, |
| 742 |
pPrivateKeyInfoStruct->PrivateKey.pbData, |
| 743 |
pPrivateKeyInfoStruct->PrivateKey.cbData, |
| 744 |
CRYPT_DECODE_ALLOC_FLAG, |
| 745 |
0, |
| 746 |
&key, |
| 747 |
&decoded_len)) { |
| 748 |
cert_type = AWS_CT_X509_RSA; |
| 749 |
} |
| 750 |
} |
| 751 |
#ifndef AWS_SUPPORT_WIN7 |
| 752 |
else if (CryptDecodeObjectEx( |
| 753 |
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 754 |
X509_ECC_PRIVATE_KEY, |
| 755 |
private_key_ptr->data.buffer, |
| 756 |
(DWORD)private_key_ptr->data.len, |
| 757 |
CRYPT_DECODE_ALLOC_FLAG, |
| 758 |
NULL, |
| 759 |
&key, |
| 760 |
&decoded_len)) { |
| 761 |
cert_type = AWS_CT_X509_ECC; |
| 762 |
} |
| 763 |
#endif |
| 764 |
|
| 765 |
if (cert_type != AWS_CT_X509_UNKNOWN) { |
| 766 |
break; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
if (cert_type == AWS_CT_X509_UNKNOWN) { |
| 771 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 772 |
AWS_LOGF_ERROR( |
| 773 |
AWS_LS_IO_PKI, "static: no acceptable private key found, error %s", aws_error_name(aws_last_error())); |
| 774 |
goto clean_up; |
| 775 |
} |
| 776 |
|
| 777 |
struct aws_uuid uuid; |
| 778 |
if (aws_uuid_init(&uuid)) { |
| 779 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: failed to create a uuid."); |
| 780 |
goto clean_up; |
| 781 |
} |
| 782 |
|
| 783 |
char uuid_str[AWS_UUID_STR_LEN] = {0}; |
| 784 |
struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_str, sizeof(uuid_str)); |
| 785 |
uuid_buf.len = 0; |
| 786 |
aws_uuid_to_str(&uuid, &uuid_buf); |
| 787 |
|
| 788 |
wchar_t uuid_wstr[AWS_UUID_STR_LEN] = {0}; |
| 789 |
size_t converted_chars = 0; |
| 790 |
mbstowcs_s(&converted_chars, uuid_wstr, AWS_UUID_STR_LEN, uuid_str, sizeof(uuid_str)); |
| 791 |
(void)converted_chars; |
| 792 |
|
| 793 |
switch (cert_type) { |
| 794 |
case AWS_CT_X509_RSA: |
| 795 |
result = s_cert_context_import_rsa_private_key( |
| 796 |
*certs, |
| 797 |
key, |
| 798 |
decoded_len, |
| 799 |
is_client_mode, |
| 800 |
uuid_wstr, |
| 801 |
crypto_provider, |
| 802 |
private_key_handle, |
| 803 |
tls13_disabled); |
| 804 |
break; |
| 805 |
|
| 806 |
#ifndef AWS_SUPPORT_WIN7 |
| 807 |
case AWS_CT_X509_ECC: |
| 808 |
result = s_cert_context_import_ecc_private_key(*certs, alloc, key, decoded_len, uuid_wstr); |
| 809 |
break; |
| 810 |
#endif |
| 811 |
|
| 812 |
default: |
| 813 |
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: failed to decode private key"); |
| 814 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 815 |
goto clean_up; |
| 816 |
} |
| 817 |
|
| 818 |
clean_up: |
| 819 |
aws_pem_objects_clean_up(&certificates); |
| 820 |
aws_pem_objects_clean_up(&private_keys); |
| 821 |
|
| 822 |
LocalFree(key); |
| 823 |
LocalFree(key_wrapper); |
| 824 |
|
| 825 |
if (result == AWS_OP_ERR) { |
| 826 |
if (*store != NULL) { |
| 827 |
aws_close_cert_store(*store); |
| 828 |
*store = NULL; |
| 829 |
} |
| 830 |
|
| 831 |
if (*certs) { |
| 832 |
CertFreeCertificateContext(*certs); |
| 833 |
*certs = NULL; |
| 834 |
} |
| 835 |
|
| 836 |
if (*crypto_provider != 0) { |
| 837 |
CryptReleaseContext(*crypto_provider, 0); |
| 838 |
*crypto_provider = 0; |
| 839 |
} |
| 840 |
|
| 841 |
if (*private_key_handle != 0) { |
| 842 |
CryptDestroyKey(*private_key_handle); |
| 843 |
*private_key_handle = 0; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
return result; |
| 848 |
} |