| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) Marc Hoersken, <[email protected]> |
| 9 |
* Copyright (C) Mark Salisbury, <[email protected]> |
| 10 |
* Copyright (C) Daniel Stenberg, <[email protected]>, et al. |
| 11 |
* |
| 12 |
* This software is licensed as described in the file COPYING, which |
| 13 |
* you should have received as part of this distribution. The terms |
| 14 |
* are also available at https://curl.se/docs/copyright.html. |
| 15 |
* |
| 16 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 17 |
* copies of the Software, and permit persons to whom the Software is |
| 18 |
* furnished to do so, under the terms of the COPYING file. |
| 19 |
* |
| 20 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 21 |
* KIND, either express or implied. |
| 22 |
* |
| 23 |
* SPDX-License-Identifier: curl |
| 24 |
* |
| 25 |
***************************************************************************/ |
| 26 |
|
| 27 |
/* |
| 28 |
* Source file for Schannel-specific certificate verification. This code should |
| 29 |
* only be invoked by code in schannel.c. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include "../curl_setup.h" |
| 33 |
|
| 34 |
#ifdef USE_SCHANNEL |
| 35 |
#ifndef USE_WINDOWS_SSPI |
| 36 |
# error "cannot compile SCHANNEL support without SSPI." |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "schannel.h" |
| 40 |
#include "schannel_int.h" |
| 41 |
|
| 42 |
#include "../curlx/inet_pton.h" |
| 43 |
#include "vtls.h" |
| 44 |
#include "vtls_int.h" |
| 45 |
#include "../sendf.h" |
| 46 |
#include "../strerror.h" |
| 47 |
#include "../curlx/winapi.h" |
| 48 |
#include "../curlx/multibyte.h" |
| 49 |
#include "../curl_printf.h" |
| 50 |
#include "hostcheck.h" |
| 51 |
#include "../curlx/version_win32.h" |
| 52 |
|
| 53 |
/* The last #include file should be: */ |
| 54 |
#include "../curl_memory.h" |
| 55 |
#include "../memdebug.h" |
| 56 |
|
| 57 |
#define BACKEND ((struct schannel_ssl_backend_data *)connssl->backend) |
| 58 |
|
| 59 |
#ifdef HAS_MANUAL_VERIFY_API |
| 60 |
|
| 61 |
#ifdef __MINGW32CE__ |
| 62 |
#define CERT_QUERY_OBJECT_BLOB 0x00000002 |
| 63 |
#define CERT_QUERY_CONTENT_CERT 1 |
| 64 |
#define CERT_QUERY_CONTENT_FLAG_CERT (1 << CERT_QUERY_CONTENT_CERT) |
| 65 |
#define CERT_QUERY_FORMAT_BINARY 1 |
| 66 |
#define CERT_QUERY_FORMAT_BASE64_ENCODED 2 |
| 67 |
#define CERT_QUERY_FORMAT_ASN_ASCII_HEX_ENCODED 3 |
| 68 |
#define CERT_QUERY_FORMAT_FLAG_ALL \ |
| 69 |
(1 << CERT_QUERY_FORMAT_BINARY) | \ |
| 70 |
(1 << CERT_QUERY_FORMAT_BASE64_ENCODED) | \ |
| 71 |
(1 << CERT_QUERY_FORMAT_ASN_ASCII_HEX_ENCODED) |
| 72 |
#define CERT_CHAIN_REVOCATION_CHECK_CHAIN 0x20000000 |
| 73 |
#define CERT_NAME_DISABLE_IE4_UTF8_FLAG 0x00010000 |
| 74 |
#define CERT_TRUST_IS_OFFLINE_REVOCATION 0x01000000 |
| 75 |
#endif /* __MINGW32CE__ */ |
| 76 |
|
| 77 |
#define MAX_CAFILE_SIZE 1048576 /* 1 MiB */ |
| 78 |
#define BEGIN_CERT "-----BEGIN CERTIFICATE-----" |
| 79 |
#define END_CERT "\n-----END CERTIFICATE-----" |
| 80 |
|
| 81 |
struct cert_chain_engine_config_win7 { |
| 82 |
DWORD cbSize; |
| 83 |
HCERTSTORE hRestrictedRoot; |
| 84 |
HCERTSTORE hRestrictedTrust; |
| 85 |
HCERTSTORE hRestrictedOther; |
| 86 |
DWORD cAdditionalStore; |
| 87 |
HCERTSTORE *rghAdditionalStore; |
| 88 |
DWORD dwFlags; |
| 89 |
DWORD dwUrlRetrievalTimeout; |
| 90 |
DWORD MaximumCachedCertificates; |
| 91 |
DWORD CycleDetectionModulus; |
| 92 |
HCERTSTORE hExclusiveRoot; |
| 93 |
HCERTSTORE hExclusiveTrustedPeople; |
| 94 |
}; |
| 95 |
|
| 96 |
#ifndef UNDER_CE |
| 97 |
static int is_cr_or_lf(char c) |
| 98 |
{ |
| 99 |
return c == '\r' || c == '\n'; |
| 100 |
} |
| 101 |
|
| 102 |
/* Search the substring needle,needlelen into string haystack,haystacklen |
| 103 |
* Strings do not need to be terminated by a '\0'. |
| 104 |
* Similar of macOS/Linux memmem (not available on Visual Studio). |
| 105 |
* Return position of beginning of first occurrence or NULL if not found |
| 106 |
*/ |
| 107 |
static const char *c_memmem(const void *haystack, size_t haystacklen, |
| 108 |
const void *needle, size_t needlelen) |
| 109 |
{ |
| 110 |
const char *p; |
| 111 |
char first; |
| 112 |
const char *str_limit = (const char *)haystack + haystacklen; |
| 113 |
if(!needlelen || needlelen > haystacklen) |
| 114 |
return NULL; |
| 115 |
first = *(const char *)needle; |
| 116 |
for(p = (const char *)haystack; p <= (str_limit - needlelen); p++) |
| 117 |
if(((*p) == first) && (memcmp(p, needle, needlelen) == 0)) |
| 118 |
return p; |
| 119 |
|
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
static CURLcode add_certs_data_to_store(HCERTSTORE trust_store, |
| 124 |
const char *ca_buffer, |
| 125 |
size_t ca_buffer_size, |
| 126 |
const char *ca_file_text, |
| 127 |
struct Curl_easy *data) |
| 128 |
{ |
| 129 |
const size_t begin_cert_len = strlen(BEGIN_CERT); |
| 130 |
const size_t end_cert_len = strlen(END_CERT); |
| 131 |
CURLcode result = CURLE_OK; |
| 132 |
int num_certs = 0; |
| 133 |
bool more_certs = 1; |
| 134 |
const char *current_ca_file_ptr = ca_buffer; |
| 135 |
const char *ca_buffer_limit = ca_buffer + ca_buffer_size; |
| 136 |
|
| 137 |
while(more_certs && (current_ca_file_ptr < ca_buffer_limit)) { |
| 138 |
const char *begin_cert_ptr = c_memmem(current_ca_file_ptr, |
| 139 |
ca_buffer_limit-current_ca_file_ptr, |
| 140 |
BEGIN_CERT, |
| 141 |
begin_cert_len); |
| 142 |
if(!begin_cert_ptr || !is_cr_or_lf(begin_cert_ptr[begin_cert_len])) { |
| 143 |
more_certs = 0; |
| 144 |
} |
| 145 |
else { |
| 146 |
const char *end_cert_ptr = c_memmem(begin_cert_ptr, |
| 147 |
ca_buffer_limit-begin_cert_ptr, |
| 148 |
END_CERT, |
| 149 |
end_cert_len); |
| 150 |
if(!end_cert_ptr) { |
| 151 |
failf(data, |
| 152 |
"schannel: CA file '%s' is not correctly formatted", |
| 153 |
ca_file_text); |
| 154 |
result = CURLE_SSL_CACERT_BADFILE; |
| 155 |
more_certs = 0; |
| 156 |
} |
| 157 |
else { |
| 158 |
CERT_BLOB cert_blob; |
| 159 |
const CERT_CONTEXT *cert_context = NULL; |
| 160 |
BOOL add_cert_result = FALSE; |
| 161 |
DWORD actual_content_type = 0; |
| 162 |
DWORD cert_size = (DWORD) |
| 163 |
((end_cert_ptr + end_cert_len) - begin_cert_ptr); |
| 164 |
|
| 165 |
cert_blob.pbData = (BYTE *)CURL_UNCONST(begin_cert_ptr); |
| 166 |
cert_blob.cbData = cert_size; |
| 167 |
if(!CryptQueryObject(CERT_QUERY_OBJECT_BLOB, |
| 168 |
&cert_blob, |
| 169 |
CERT_QUERY_CONTENT_FLAG_CERT, |
| 170 |
CERT_QUERY_FORMAT_FLAG_ALL, |
| 171 |
0, |
| 172 |
NULL, |
| 173 |
&actual_content_type, |
| 174 |
NULL, |
| 175 |
NULL, |
| 176 |
NULL, |
| 177 |
(const void **)&cert_context)) { |
| 178 |
char buffer[WINAPI_ERROR_LEN]; |
| 179 |
failf(data, |
| 180 |
"schannel: failed to extract certificate from CA file " |
| 181 |
"'%s': %s", |
| 182 |
ca_file_text, |
| 183 |
curlx_winapi_strerror(GetLastError(), buffer, sizeof(buffer))); |
| 184 |
result = CURLE_SSL_CACERT_BADFILE; |
| 185 |
more_certs = 0; |
| 186 |
} |
| 187 |
else { |
| 188 |
current_ca_file_ptr = begin_cert_ptr + cert_size; |
| 189 |
|
| 190 |
/* Sanity check that the cert_context object is the right type */ |
| 191 |
if(CERT_QUERY_CONTENT_CERT != actual_content_type) { |
| 192 |
failf(data, |
| 193 |
"schannel: unexpected content type '%lu' when extracting " |
| 194 |
"certificate from CA file '%s'", |
| 195 |
actual_content_type, ca_file_text); |
| 196 |
result = CURLE_SSL_CACERT_BADFILE; |
| 197 |
more_certs = 0; |
| 198 |
} |
| 199 |
else { |
| 200 |
add_cert_result = |
| 201 |
CertAddCertificateContextToStore(trust_store, |
| 202 |
cert_context, |
| 203 |
CERT_STORE_ADD_ALWAYS, |
| 204 |
NULL); |
| 205 |
CertFreeCertificateContext(cert_context); |
| 206 |
if(!add_cert_result) { |
| 207 |
char buffer[WINAPI_ERROR_LEN]; |
| 208 |
failf(data, |
| 209 |
"schannel: failed to add certificate from CA file '%s' " |
| 210 |
"to certificate store: %s", |
| 211 |
ca_file_text, |
| 212 |
curlx_winapi_strerror(GetLastError(), buffer, |
| 213 |
sizeof(buffer))); |
| 214 |
result = CURLE_SSL_CACERT_BADFILE; |
| 215 |
more_certs = 0; |
| 216 |
} |
| 217 |
else { |
| 218 |
num_certs++; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if(result == CURLE_OK) { |
| 227 |
if(!num_certs) { |
| 228 |
infof(data, |
| 229 |
"schannel: did not add any certificates from CA file '%s'", |
| 230 |
ca_file_text); |
| 231 |
} |
| 232 |
else { |
| 233 |
infof(data, |
| 234 |
"schannel: added %d certificate(s) from CA file '%s'", |
| 235 |
num_certs, ca_file_text); |
| 236 |
} |
| 237 |
} |
| 238 |
return result; |
| 239 |
} |
| 240 |
|
| 241 |
static CURLcode add_certs_file_to_store(HCERTSTORE trust_store, |
| 242 |
const char *ca_file, |
| 243 |
struct Curl_easy *data) |
| 244 |
{ |
| 245 |
CURLcode result; |
| 246 |
HANDLE ca_file_handle = INVALID_HANDLE_VALUE; |
| 247 |
LARGE_INTEGER file_size; |
| 248 |
char *ca_file_buffer = NULL; |
| 249 |
TCHAR *ca_file_tstr = NULL; |
| 250 |
size_t ca_file_bufsize = 0; |
| 251 |
DWORD total_bytes_read = 0; |
| 252 |
|
| 253 |
ca_file_tstr = curlx_convert_UTF8_to_tchar(ca_file); |
| 254 |
if(!ca_file_tstr) { |
| 255 |
char buffer[WINAPI_ERROR_LEN]; |
| 256 |
failf(data, |
| 257 |
"schannel: invalid path name for CA file '%s': %s", |
| 258 |
ca_file, |
| 259 |
curlx_winapi_strerror(GetLastError(), buffer, sizeof(buffer))); |
| 260 |
result = CURLE_SSL_CACERT_BADFILE; |
| 261 |
goto cleanup; |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* Read the CA file completely into memory before parsing it. This |
| 266 |
* optimizes for the common case where the CA file will be relatively |
| 267 |
* small ( < 1 MiB ). |
| 268 |
*/ |
| 269 |
ca_file_handle = CreateFile(ca_file_tstr, |
| 270 |
GENERIC_READ, |
| 271 |
FILE_SHARE_READ, |
| 272 |
NULL, |
| 273 |
OPEN_EXISTING, |
| 274 |
FILE_ATTRIBUTE_NORMAL, |
| 275 |
NULL); |
| 276 |
if(ca_file_handle == INVALID_HANDLE_VALUE) { |
| 277 |
char buffer[WINAPI_ERROR_LEN]; |
| 278 |
failf(data, |
| 279 |
"schannel: failed to open CA file '%s': %s", |
| 280 |
ca_file, |
| 281 |
curlx_winapi_strerror(GetLastError(), buffer, sizeof(buffer))); |
| 282 |
result = CURLE_SSL_CACERT_BADFILE; |
| 283 |
goto cleanup; |
| 284 |
} |
| 285 |
|
| 286 |
if(!GetFileSizeEx(ca_file_handle, &file_size)) { |
| 287 |
char buffer[WINAPI_ERROR_LEN]; |
| 288 |
failf(data, |
| 289 |
"schannel: failed to determine size of CA file '%s': %s", |
| 290 |
ca_file, |
| 291 |
curlx_winapi_strerror(GetLastError(), buffer, sizeof(buffer))); |
| 292 |
result = CURLE_SSL_CACERT_BADFILE; |
| 293 |
goto cleanup; |
| 294 |
} |
| 295 |
|
| 296 |
if(file_size.QuadPart > MAX_CAFILE_SIZE) { |
| 297 |
failf(data, |
| 298 |
"schannel: CA file exceeds max size of %u bytes", |
| 299 |
MAX_CAFILE_SIZE); |
| 300 |
result = CURLE_SSL_CACERT_BADFILE; |
| 301 |
goto cleanup; |
| 302 |
} |
| 303 |
|
| 304 |
ca_file_bufsize = (size_t)file_size.QuadPart; |
| 305 |
ca_file_buffer = (char *)malloc(ca_file_bufsize + 1); |
| 306 |
if(!ca_file_buffer) { |
| 307 |
result = CURLE_OUT_OF_MEMORY; |
| 308 |
goto cleanup; |
| 309 |
} |
| 310 |
|
| 311 |
while(total_bytes_read < ca_file_bufsize) { |
| 312 |
DWORD bytes_to_read = (DWORD)(ca_file_bufsize - total_bytes_read); |
| 313 |
DWORD bytes_read = 0; |
| 314 |
|
| 315 |
if(!ReadFile(ca_file_handle, ca_file_buffer + total_bytes_read, |
| 316 |
bytes_to_read, &bytes_read, NULL)) { |
| 317 |
char buffer[WINAPI_ERROR_LEN]; |
| 318 |
failf(data, |
| 319 |
"schannel: failed to read from CA file '%s': %s", |
| 320 |
ca_file, |
| 321 |
curlx_winapi_strerror(GetLastError(), buffer, sizeof(buffer))); |
| 322 |
result = CURLE_SSL_CACERT_BADFILE; |
| 323 |
goto cleanup; |
| 324 |
} |
| 325 |
if(bytes_read == 0) { |
| 326 |
/* Premature EOF -- adjust the bufsize to the new value */ |
| 327 |
ca_file_bufsize = total_bytes_read; |
| 328 |
} |
| 329 |
else { |
| 330 |
total_bytes_read += bytes_read; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
/* null-terminate the buffer */ |
| 335 |
ca_file_buffer[ca_file_bufsize] = '\0'; |
| 336 |
|
| 337 |
result = add_certs_data_to_store(trust_store, |
| 338 |
ca_file_buffer, ca_file_bufsize, |
| 339 |
ca_file, |
| 340 |
data); |
| 341 |
|
| 342 |
cleanup: |
| 343 |
if(ca_file_handle != INVALID_HANDLE_VALUE) { |
| 344 |
CloseHandle(ca_file_handle); |
| 345 |
} |
| 346 |
Curl_safefree(ca_file_buffer); |
| 347 |
curlx_unicodefree(ca_file_tstr); |
| 348 |
|
| 349 |
return result; |
| 350 |
} |
| 351 |
#endif |
| 352 |
|
| 353 |
#endif /* HAS_MANUAL_VERIFY_API */ |
| 354 |
|
| 355 |
#ifndef UNDER_CE |
| 356 |
/* |
| 357 |
* Returns the number of characters necessary to populate all the host_names. |
| 358 |
* If host_names is not NULL, populate it with all the hostnames. Each string |
| 359 |
* in the host_names is null-terminated and the last string is double |
| 360 |
* null-terminated. If no DNS names are found, a single null-terminated empty |
| 361 |
* string is returned. |
| 362 |
*/ |
| 363 |
static DWORD cert_get_name_string(struct Curl_easy *data, |
| 364 |
CERT_CONTEXT *cert_context, |
| 365 |
LPTSTR host_names, |
| 366 |
DWORD length, |
| 367 |
PCERT_ALT_NAME_INFO alt_name_info, |
| 368 |
BOOL Win8_compat) |
| 369 |
{ |
| 370 |
DWORD actual_length = 0; |
| 371 |
#if defined(CURL_WINDOWS_UWP) |
| 372 |
(void)data; |
| 373 |
(void)cert_context; |
| 374 |
(void)host_names; |
| 375 |
(void)length; |
| 376 |
(void)alt_name_info; |
| 377 |
(void)Win8_compat; |
| 378 |
#else |
| 379 |
BOOL compute_content = FALSE; |
| 380 |
LPTSTR current_pos = NULL; |
| 381 |
DWORD i; |
| 382 |
|
| 383 |
#ifdef CERT_NAME_SEARCH_ALL_NAMES_FLAG |
| 384 |
/* CERT_NAME_SEARCH_ALL_NAMES_FLAG is available from Windows 8 onwards. */ |
| 385 |
if(Win8_compat) { |
| 386 |
/* CertGetNameString will provide the 8-bit character string without |
| 387 |
* any decoding */ |
| 388 |
DWORD name_flags = |
| 389 |
CERT_NAME_DISABLE_IE4_UTF8_FLAG | CERT_NAME_SEARCH_ALL_NAMES_FLAG; |
| 390 |
actual_length = CertGetNameString(cert_context, |
| 391 |
CERT_NAME_DNS_TYPE, |
| 392 |
name_flags, |
| 393 |
NULL, |
| 394 |
host_names, |
| 395 |
length); |
| 396 |
return actual_length; |
| 397 |
} |
| 398 |
#else |
| 399 |
(void)cert_context; |
| 400 |
(void)Win8_compat; |
| 401 |
#endif |
| 402 |
|
| 403 |
if(!alt_name_info) |
| 404 |
return 0; |
| 405 |
|
| 406 |
compute_content = host_names != NULL && length != 0; |
| 407 |
|
| 408 |
/* Initialize default return values. */ |
| 409 |
actual_length = 1; |
| 410 |
if(compute_content) { |
| 411 |
*host_names = '\0'; |
| 412 |
} |
| 413 |
|
| 414 |
current_pos = host_names; |
| 415 |
|
| 416 |
/* Iterate over the alternate names and populate host_names. */ |
| 417 |
for(i = 0; i < alt_name_info->cAltEntry; i++) { |
| 418 |
const CERT_ALT_NAME_ENTRY *entry = &alt_name_info->rgAltEntry[i]; |
| 419 |
wchar_t *dns_w = NULL; |
| 420 |
size_t current_length = 0; |
| 421 |
|
| 422 |
if(entry->dwAltNameChoice != CERT_ALT_NAME_DNS_NAME) { |
| 423 |
continue; |
| 424 |
} |
| 425 |
if(!entry->pwszDNSName) { |
| 426 |
infof(data, "schannel: Empty DNS name."); |
| 427 |
continue; |
| 428 |
} |
| 429 |
current_length = wcslen(entry->pwszDNSName) + 1; |
| 430 |
if(!compute_content) { |
| 431 |
actual_length += (DWORD)current_length; |
| 432 |
continue; |
| 433 |
} |
| 434 |
/* Sanity check to prevent buffer overrun. */ |
| 435 |
if((actual_length + current_length) > length) { |
| 436 |
failf(data, "schannel: Not enough memory to list all hostnames."); |
| 437 |
break; |
| 438 |
} |
| 439 |
dns_w = entry->pwszDNSName; |
| 440 |
/* pwszDNSName is in ia5 string format and hence does not contain any |
| 441 |
* non-ASCII characters. */ |
| 442 |
while(*dns_w != '\0') { |
| 443 |
*current_pos++ = (TCHAR)(*dns_w++); |
| 444 |
} |
| 445 |
*current_pos++ = '\0'; |
| 446 |
actual_length += (DWORD)current_length; |
| 447 |
} |
| 448 |
if(compute_content) { |
| 449 |
/* Last string has double null-terminator. */ |
| 450 |
*current_pos = '\0'; |
| 451 |
} |
| 452 |
#endif |
| 453 |
return actual_length; |
| 454 |
} |
| 455 |
|
| 456 |
/* |
| 457 |
* Returns TRUE if the hostname is a numeric IPv4/IPv6 Address, |
| 458 |
* and populates the buffer with IPv4/IPv6 info. |
| 459 |
*/ |
| 460 |
|
| 461 |
static bool get_num_host_info(struct num_ip_data *ip_blob, |
| 462 |
LPCSTR hostname) |
| 463 |
{ |
| 464 |
struct in_addr ia; |
| 465 |
struct in6_addr ia6; |
| 466 |
bool result = FALSE; |
| 467 |
|
| 468 |
int res = curlx_inet_pton(AF_INET, hostname, &ia); |
| 469 |
if(res) { |
| 470 |
ip_blob->size = sizeof(struct in_addr); |
| 471 |
memcpy(&ip_blob->bData.ia, &ia, sizeof(struct in_addr)); |
| 472 |
result = TRUE; |
| 473 |
} |
| 474 |
else { |
| 475 |
res = curlx_inet_pton(AF_INET6, hostname, &ia6); |
| 476 |
if(res) { |
| 477 |
ip_blob->size = sizeof(struct in6_addr); |
| 478 |
memcpy(&ip_blob->bData.ia6, &ia6, sizeof(struct in6_addr)); |
| 479 |
result = TRUE; |
| 480 |
} |
| 481 |
} |
| 482 |
return result; |
| 483 |
} |
| 484 |
|
| 485 |
static bool get_alt_name_info(struct Curl_easy *data, |
| 486 |
PCCERT_CONTEXT ctx, |
| 487 |
PCERT_ALT_NAME_INFO *alt_name_info, |
| 488 |
LPDWORD alt_name_info_size) |
| 489 |
{ |
| 490 |
bool result = FALSE; |
| 491 |
#if defined(CURL_WINDOWS_UWP) |
| 492 |
(void)data; |
| 493 |
(void)ctx; |
| 494 |
(void)alt_name_info; |
| 495 |
(void)alt_name_info_size; |
| 496 |
#else |
| 497 |
PCERT_INFO cert_info = NULL; |
| 498 |
PCERT_EXTENSION extension = NULL; |
| 499 |
CRYPT_DECODE_PARA decode_para = { sizeof(CRYPT_DECODE_PARA), NULL, NULL }; |
| 500 |
|
| 501 |
if(!ctx) { |
| 502 |
failf(data, "schannel: Null certificate context."); |
| 503 |
return result; |
| 504 |
} |
| 505 |
|
| 506 |
cert_info = ctx->pCertInfo; |
| 507 |
if(!cert_info) { |
| 508 |
failf(data, "schannel: Null certificate info."); |
| 509 |
return result; |
| 510 |
} |
| 511 |
|
| 512 |
extension = CertFindExtension(szOID_SUBJECT_ALT_NAME2, |
| 513 |
cert_info->cExtension, |
| 514 |
cert_info->rgExtension); |
| 515 |
if(!extension) { |
| 516 |
failf(data, "schannel: CertFindExtension() returned no extension."); |
| 517 |
return result; |
| 518 |
} |
| 519 |
|
| 520 |
if(!CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 521 |
szOID_SUBJECT_ALT_NAME2, |
| 522 |
extension->Value.pbData, |
| 523 |
extension->Value.cbData, |
| 524 |
CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, |
| 525 |
&decode_para, |
| 526 |
alt_name_info, |
| 527 |
alt_name_info_size)) { |
| 528 |
failf(data, |
| 529 |
"schannel: CryptDecodeObjectEx() returned no alternate name " |
| 530 |
"information."); |
| 531 |
return result; |
| 532 |
} |
| 533 |
result = TRUE; |
| 534 |
#endif |
| 535 |
return result; |
| 536 |
} |
| 537 |
#endif /* !UNDER_CE */ |
| 538 |
|
| 539 |
/* Verify the server's hostname */ |
| 540 |
CURLcode Curl_verify_host(struct Curl_cfilter *cf, |
| 541 |
struct Curl_easy *data) |
| 542 |
{ |
| 543 |
CURLcode result = CURLE_PEER_FAILED_VERIFICATION; |
| 544 |
struct ssl_connect_data *connssl = cf->ctx; |
| 545 |
CERT_CONTEXT *pCertContextServer = NULL; |
| 546 |
#ifdef UNDER_CE |
| 547 |
TCHAR cert_hostname_buff[256]; |
| 548 |
DWORD len; |
| 549 |
|
| 550 |
/* This code does not support certificates with multiple alternative names. |
| 551 |
* Right now we are only asking for the first preferred alternative name. |
| 552 |
* Instead we would need to do all via CERT_NAME_SEARCH_ALL_NAMES_FLAG |
| 553 |
* (If Windows CE supports that?) and run this section in a loop for each. |
| 554 |
* https://msdn.microsoft.com/en-us/library/windows/desktop/aa376086.aspx |
| 555 |
* curl: (51) schannel: CertGetNameString() certificate hostname |
| 556 |
* (.google.com) did not match connection (google.com) |
| 557 |
*/ |
| 558 |
len = CertGetNameString(pCertContextServer, |
| 559 |
CERT_NAME_DNS_TYPE, |
| 560 |
CERT_NAME_DISABLE_IE4_UTF8_FLAG, |
| 561 |
NULL, |
| 562 |
cert_hostname_buff, |
| 563 |
256); |
| 564 |
if(len > 0) { |
| 565 |
/* Comparing the cert name and the connection hostname encoded as UTF-8 |
| 566 |
* is acceptable since both values are assumed to use ASCII |
| 567 |
* (or some equivalent) encoding |
| 568 |
*/ |
| 569 |
char *cert_hostname = curlx_convert_tchar_to_UTF8(cert_hostname_buff); |
| 570 |
if(!cert_hostname) { |
| 571 |
result = CURLE_OUT_OF_MEMORY; |
| 572 |
} |
| 573 |
else{ |
| 574 |
const char *conn_hostname = connssl->peer.hostname; |
| 575 |
if(Curl_cert_hostcheck(cert_hostname, strlen(cert_hostname), |
| 576 |
conn_hostname, strlen(conn_hostname))) { |
| 577 |
infof(data, |
| 578 |
"schannel: connection hostname (%s) validated " |
| 579 |
"against certificate name (%s)\n", |
| 580 |
conn_hostname, cert_hostname); |
| 581 |
result = CURLE_OK; |
| 582 |
} |
| 583 |
else{ |
| 584 |
failf(data, |
| 585 |
"schannel: connection hostname (%s) " |
| 586 |
"does not match certificate name (%s)", |
| 587 |
conn_hostname, cert_hostname); |
| 588 |
} |
| 589 |
Curl_safefree(cert_hostname); |
| 590 |
} |
| 591 |
} |
| 592 |
else { |
| 593 |
failf(data, |
| 594 |
"schannel: CertGetNameString did not provide any " |
| 595 |
"certificate name information"); |
| 596 |
} |
| 597 |
#else |
| 598 |
SECURITY_STATUS sspi_status; |
| 599 |
TCHAR *cert_hostname_buff = NULL; |
| 600 |
size_t cert_hostname_buff_index = 0; |
| 601 |
const char *conn_hostname = connssl->peer.hostname; |
| 602 |
size_t hostlen = strlen(conn_hostname); |
| 603 |
DWORD len = 0; |
| 604 |
DWORD actual_len = 0; |
| 605 |
PCERT_ALT_NAME_INFO alt_name_info = NULL; |
| 606 |
DWORD alt_name_info_size = 0; |
| 607 |
struct num_ip_data ip_blob = { 0 }; |
| 608 |
bool Win8_compat; |
| 609 |
struct num_ip_data *p = &ip_blob; |
| 610 |
DWORD i; |
| 611 |
|
| 612 |
sspi_status = |
| 613 |
Curl_pSecFn->QueryContextAttributes(&BACKEND->ctxt->ctxt_handle, |
| 614 |
SECPKG_ATTR_REMOTE_CERT_CONTEXT, |
| 615 |
&pCertContextServer); |
| 616 |
|
| 617 |
if((sspi_status != SEC_E_OK) || !pCertContextServer) { |
| 618 |
char buffer[WINAPI_ERROR_LEN]; |
| 619 |
failf(data, "schannel: Failed to read remote certificate context: %s", |
| 620 |
Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer))); |
| 621 |
goto cleanup; |
| 622 |
} |
| 623 |
|
| 624 |
Win8_compat = curlx_verify_windows_version(6, 2, 0, PLATFORM_WINNT, |
| 625 |
VERSION_GREATER_THAN_EQUAL); |
| 626 |
if(get_num_host_info(p, conn_hostname) || !Win8_compat) { |
| 627 |
if(!get_alt_name_info(data, pCertContextServer, |
| 628 |
&alt_name_info, &alt_name_info_size)) { |
| 629 |
goto cleanup; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
if(p->size && alt_name_info) { |
| 634 |
for(i = 0; i < alt_name_info->cAltEntry; ++i) { |
| 635 |
PCERT_ALT_NAME_ENTRY entry = &alt_name_info->rgAltEntry[i]; |
| 636 |
if(entry->dwAltNameChoice == CERT_ALT_NAME_IP_ADDRESS) { |
| 637 |
if(entry->IPAddress.cbData == p->size) { |
| 638 |
if(!memcmp(entry->IPAddress.pbData, &p->bData, |
| 639 |
entry->IPAddress.cbData)) { |
| 640 |
result = CURLE_OK; |
| 641 |
infof(data, |
| 642 |
"schannel: connection hostname (%s) matched cert's IP address!", |
| 643 |
conn_hostname); |
| 644 |
break; |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
else { |
| 651 |
/* Determine the size of the string needed for the cert hostname */ |
| 652 |
len = cert_get_name_string(data, pCertContextServer, |
| 653 |
NULL, 0, alt_name_info, Win8_compat); |
| 654 |
if(len == 0) { |
| 655 |
failf(data, |
| 656 |
"schannel: CertGetNameString() returned no " |
| 657 |
"certificate name information"); |
| 658 |
goto cleanup; |
| 659 |
} |
| 660 |
|
| 661 |
/* CertGetNameString guarantees that the returned name will not contain |
| 662 |
* embedded null bytes. This appears to be undocumented behavior. |
| 663 |
*/ |
| 664 |
cert_hostname_buff = (LPTSTR)malloc(len * sizeof(TCHAR)); |
| 665 |
if(!cert_hostname_buff) { |
| 666 |
result = CURLE_OUT_OF_MEMORY; |
| 667 |
goto cleanup; |
| 668 |
} |
| 669 |
actual_len = cert_get_name_string(data, pCertContextServer, |
| 670 |
(LPTSTR)cert_hostname_buff, len, alt_name_info, Win8_compat); |
| 671 |
|
| 672 |
/* Sanity check */ |
| 673 |
if(actual_len != len) { |
| 674 |
failf(data, |
| 675 |
"schannel: CertGetNameString() returned certificate " |
| 676 |
"name information of unexpected size"); |
| 677 |
goto cleanup; |
| 678 |
} |
| 679 |
|
| 680 |
/* cert_hostname_buff contains all DNS names, where each name is |
| 681 |
* null-terminated and the last DNS name is double null-terminated. Due to |
| 682 |
* this encoding, use the length of the buffer to iterate over all names. |
| 683 |
*/ |
| 684 |
while(cert_hostname_buff_index < len && |
| 685 |
cert_hostname_buff[cert_hostname_buff_index] != TEXT('\0') && |
| 686 |
result == CURLE_PEER_FAILED_VERIFICATION) { |
| 687 |
|
| 688 |
char *cert_hostname; |
| 689 |
|
| 690 |
/* Comparing the cert name and the connection hostname encoded as UTF-8 |
| 691 |
* is acceptable since both values are assumed to use ASCII |
| 692 |
* (or some equivalent) encoding |
| 693 |
*/ |
| 694 |
cert_hostname = curlx_convert_tchar_to_UTF8( |
| 695 |
&cert_hostname_buff[cert_hostname_buff_index]); |
| 696 |
if(!cert_hostname) { |
| 697 |
result = CURLE_OUT_OF_MEMORY; |
| 698 |
} |
| 699 |
else { |
| 700 |
if(Curl_cert_hostcheck(cert_hostname, strlen(cert_hostname), |
| 701 |
conn_hostname, hostlen)) { |
| 702 |
infof(data, |
| 703 |
"schannel: connection hostname (%s) validated " |
| 704 |
"against certificate name (%s)", |
| 705 |
conn_hostname, cert_hostname); |
| 706 |
result = CURLE_OK; |
| 707 |
} |
| 708 |
else { |
| 709 |
size_t cert_hostname_len; |
| 710 |
|
| 711 |
infof(data, |
| 712 |
"schannel: connection hostname (%s) did not match " |
| 713 |
"against certificate name (%s)", |
| 714 |
conn_hostname, cert_hostname); |
| 715 |
|
| 716 |
cert_hostname_len = |
| 717 |
_tcslen(&cert_hostname_buff[cert_hostname_buff_index]); |
| 718 |
|
| 719 |
/* Move on to next cert name */ |
| 720 |
cert_hostname_buff_index += cert_hostname_len + 1; |
| 721 |
|
| 722 |
result = CURLE_PEER_FAILED_VERIFICATION; |
| 723 |
} |
| 724 |
curlx_unicodefree(cert_hostname); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
if(result == CURLE_PEER_FAILED_VERIFICATION) { |
| 729 |
failf(data, |
| 730 |
"schannel: CertGetNameString() failed to match " |
| 731 |
"connection hostname (%s) against server certificate names", |
| 732 |
conn_hostname); |
| 733 |
} |
| 734 |
else if(result != CURLE_OK) |
| 735 |
failf(data, "schannel: server certificate name verification failed"); |
| 736 |
} |
| 737 |
|
| 738 |
cleanup: |
| 739 |
Curl_safefree(cert_hostname_buff); |
| 740 |
|
| 741 |
if(pCertContextServer) |
| 742 |
CertFreeCertificateContext(pCertContextServer); |
| 743 |
#endif /* !UNDER_CE */ |
| 744 |
|
| 745 |
return result; |
| 746 |
} |
| 747 |
|
| 748 |
#ifdef HAS_MANUAL_VERIFY_API |
| 749 |
/* Verify the server's certificate and hostname */ |
| 750 |
CURLcode Curl_verify_certificate(struct Curl_cfilter *cf, |
| 751 |
struct Curl_easy *data) |
| 752 |
{ |
| 753 |
struct ssl_connect_data *connssl = cf->ctx; |
| 754 |
struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf); |
| 755 |
struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data); |
| 756 |
SECURITY_STATUS sspi_status; |
| 757 |
CURLcode result = CURLE_OK; |
| 758 |
CERT_CONTEXT *pCertContextServer = NULL; |
| 759 |
const CERT_CHAIN_CONTEXT *pChainContext = NULL; |
| 760 |
HCERTCHAINENGINE cert_chain_engine = NULL; |
| 761 |
#ifndef UNDER_CE |
| 762 |
HCERTSTORE trust_store = NULL; |
| 763 |
HCERTSTORE own_trust_store = NULL; |
| 764 |
#endif /* !UNDER_CE */ |
| 765 |
|
| 766 |
DEBUGASSERT(BACKEND); |
| 767 |
|
| 768 |
sspi_status = |
| 769 |
Curl_pSecFn->QueryContextAttributes(&BACKEND->ctxt->ctxt_handle, |
| 770 |
SECPKG_ATTR_REMOTE_CERT_CONTEXT, |
| 771 |
&pCertContextServer); |
| 772 |
|
| 773 |
if((sspi_status != SEC_E_OK) || !pCertContextServer) { |
| 774 |
char buffer[WINAPI_ERROR_LEN]; |
| 775 |
failf(data, "schannel: Failed to read remote certificate context: %s", |
| 776 |
Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer))); |
| 777 |
result = CURLE_PEER_FAILED_VERIFICATION; |
| 778 |
} |
| 779 |
|
| 780 |
#ifndef UNDER_CE |
| 781 |
if(result == CURLE_OK && |
| 782 |
(conn_config->CAfile || conn_config->ca_info_blob) && |
| 783 |
BACKEND->use_manual_cred_validation) { |
| 784 |
/* |
| 785 |
* Create a chain engine that uses the certificates in the CA file as |
| 786 |
* trusted certificates. This is only supported on Windows 7+. |
| 787 |
*/ |
| 788 |
|
| 789 |
if(curlx_verify_windows_version(6, 1, 0, PLATFORM_WINNT, |
| 790 |
VERSION_LESS_THAN)) { |
| 791 |
failf(data, "schannel: this version of Windows is too old to support " |
| 792 |
"certificate verification via CA bundle file."); |
| 793 |
result = CURLE_SSL_CACERT_BADFILE; |
| 794 |
} |
| 795 |
else { |
| 796 |
/* try cache */ |
| 797 |
trust_store = Curl_schannel_get_cached_cert_store(cf, data); |
| 798 |
|
| 799 |
if(trust_store) { |
| 800 |
infof(data, "schannel: reusing certificate store from cache"); |
| 801 |
} |
| 802 |
else { |
| 803 |
/* Open the certificate store */ |
| 804 |
trust_store = CertOpenStore(CERT_STORE_PROV_MEMORY, |
| 805 |
0, |
| 806 |
(HCRYPTPROV)NULL, |
| 807 |
CERT_STORE_CREATE_NEW_FLAG, |
| 808 |
NULL); |
| 809 |
if(!trust_store) { |
| 810 |
char buffer[WINAPI_ERROR_LEN]; |
| 811 |
failf(data, "schannel: failed to create certificate store: %s", |
| 812 |
curlx_winapi_strerror(GetLastError(), buffer, sizeof(buffer))); |
| 813 |
result = CURLE_SSL_CACERT_BADFILE; |
| 814 |
} |
| 815 |
else { |
| 816 |
const struct curl_blob *ca_info_blob = conn_config->ca_info_blob; |
| 817 |
own_trust_store = trust_store; |
| 818 |
|
| 819 |
if(ca_info_blob) { |
| 820 |
result = add_certs_data_to_store(trust_store, |
| 821 |
(const char *)ca_info_blob->data, |
| 822 |
ca_info_blob->len, |
| 823 |
"(memory blob)", |
| 824 |
data); |
| 825 |
} |
| 826 |
else { |
| 827 |
result = add_certs_file_to_store(trust_store, |
| 828 |
conn_config->CAfile, |
| 829 |
data); |
| 830 |
} |
| 831 |
if(result == CURLE_OK) { |
| 832 |
if(Curl_schannel_set_cached_cert_store(cf, data, trust_store)) { |
| 833 |
own_trust_store = NULL; |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
if(result == CURLE_OK) { |
| 841 |
struct cert_chain_engine_config_win7 engine_config; |
| 842 |
BOOL create_engine_result; |
| 843 |
|
| 844 |
memset(&engine_config, 0, sizeof(engine_config)); |
| 845 |
engine_config.cbSize = sizeof(engine_config); |
| 846 |
engine_config.hExclusiveRoot = trust_store; |
| 847 |
|
| 848 |
/* CertCreateCertificateChainEngine will check the expected size of the |
| 849 |
* CERT_CHAIN_ENGINE_CONFIG structure and fail if the specified size |
| 850 |
* does not match the expected size. When this occurs, it indicates that |
| 851 |
* CAINFO is not supported on the version of Windows in use. |
| 852 |
*/ |
| 853 |
create_engine_result = |
| 854 |
CertCreateCertificateChainEngine( |
| 855 |
(CERT_CHAIN_ENGINE_CONFIG *)&engine_config, &cert_chain_engine); |
| 856 |
if(!create_engine_result) { |
| 857 |
char buffer[WINAPI_ERROR_LEN]; |
| 858 |
failf(data, |
| 859 |
"schannel: failed to create certificate chain engine: %s", |
| 860 |
curlx_winapi_strerror(GetLastError(), buffer, sizeof(buffer))); |
| 861 |
result = CURLE_SSL_CACERT_BADFILE; |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
#endif /* !UNDER_CE */ |
| 866 |
|
| 867 |
if(result == CURLE_OK) { |
| 868 |
CERT_CHAIN_PARA ChainPara; |
| 869 |
|
| 870 |
memset(&ChainPara, 0, sizeof(ChainPara)); |
| 871 |
ChainPara.cbSize = sizeof(ChainPara); |
| 872 |
|
| 873 |
if(!CertGetCertificateChain(cert_chain_engine, |
| 874 |
pCertContextServer, |
| 875 |
NULL, |
| 876 |
pCertContextServer->hCertStore, |
| 877 |
&ChainPara, |
| 878 |
(ssl_config->no_revoke ? 0 : |
| 879 |
CERT_CHAIN_REVOCATION_CHECK_CHAIN), |
| 880 |
NULL, |
| 881 |
&pChainContext)) { |
| 882 |
char buffer[WINAPI_ERROR_LEN]; |
| 883 |
failf(data, "schannel: CertGetCertificateChain failed: %s", |
| 884 |
curlx_winapi_strerror(GetLastError(), buffer, sizeof(buffer))); |
| 885 |
pChainContext = NULL; |
| 886 |
result = CURLE_PEER_FAILED_VERIFICATION; |
| 887 |
} |
| 888 |
|
| 889 |
if(result == CURLE_OK) { |
| 890 |
CERT_SIMPLE_CHAIN *pSimpleChain = pChainContext->rgpChain[0]; |
| 891 |
DWORD dwTrustErrorMask = ~(DWORD)(CERT_TRUST_IS_NOT_TIME_NESTED); |
| 892 |
dwTrustErrorMask &= pSimpleChain->TrustStatus.dwErrorStatus; |
| 893 |
|
| 894 |
if(data->set.ssl.revoke_best_effort) { |
| 895 |
/* Ignore errors when root certificates are missing the revocation |
| 896 |
* list URL, or when the list could not be downloaded because the |
| 897 |
* server is currently unreachable. */ |
| 898 |
dwTrustErrorMask &= ~(DWORD)(CERT_TRUST_REVOCATION_STATUS_UNKNOWN | |
| 899 |
CERT_TRUST_IS_OFFLINE_REVOCATION); |
| 900 |
} |
| 901 |
|
| 902 |
if(dwTrustErrorMask) { |
| 903 |
if(dwTrustErrorMask & CERT_TRUST_IS_REVOKED) |
| 904 |
failf(data, "schannel: CertGetCertificateChain trust error" |
| 905 |
" CERT_TRUST_IS_REVOKED"); |
| 906 |
else if(dwTrustErrorMask & CERT_TRUST_IS_PARTIAL_CHAIN) |
| 907 |
failf(data, "schannel: CertGetCertificateChain trust error" |
| 908 |
" CERT_TRUST_IS_PARTIAL_CHAIN"); |
| 909 |
else if(dwTrustErrorMask & CERT_TRUST_IS_UNTRUSTED_ROOT) |
| 910 |
failf(data, "schannel: CertGetCertificateChain trust error" |
| 911 |
" CERT_TRUST_IS_UNTRUSTED_ROOT"); |
| 912 |
else if(dwTrustErrorMask & CERT_TRUST_IS_NOT_TIME_VALID) |
| 913 |
failf(data, "schannel: CertGetCertificateChain trust error" |
| 914 |
" CERT_TRUST_IS_NOT_TIME_VALID"); |
| 915 |
else if(dwTrustErrorMask & CERT_TRUST_REVOCATION_STATUS_UNKNOWN) |
| 916 |
failf(data, "schannel: CertGetCertificateChain trust error" |
| 917 |
" CERT_TRUST_REVOCATION_STATUS_UNKNOWN"); |
| 918 |
else |
| 919 |
failf(data, "schannel: CertGetCertificateChain error mask: 0x%08lx", |
| 920 |
dwTrustErrorMask); |
| 921 |
result = CURLE_PEER_FAILED_VERIFICATION; |
| 922 |
} |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
if(result == CURLE_OK) { |
| 927 |
if(conn_config->verifyhost) { |
| 928 |
result = Curl_verify_host(cf, data); |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
#ifndef UNDER_CE |
| 933 |
if(cert_chain_engine) { |
| 934 |
CertFreeCertificateChainEngine(cert_chain_engine); |
| 935 |
} |
| 936 |
|
| 937 |
if(own_trust_store) { |
| 938 |
CertCloseStore(own_trust_store, 0); |
| 939 |
} |
| 940 |
#endif /* !UNDER_CE */ |
| 941 |
|
| 942 |
if(pChainContext) |
| 943 |
CertFreeCertificateChain(pChainContext); |
| 944 |
|
| 945 |
if(pCertContextServer) |
| 946 |
CertFreeCertificateContext(pCertContextServer); |
| 947 |
|
| 948 |
return result; |
| 949 |
} |
| 950 |
|
| 951 |
#endif /* HAS_MANUAL_VERIFY_API */ |
| 952 |
#endif /* USE_SCHANNEL */ |