1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Marc Hoersken, <info@marc-hoersken.de>
9 * Copyright (C) Mark Salisbury, <mark.salisbury@hp.com>
10 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 "vtls.h"
43 #include "vtls_int.h"
44 #include "sendf.h"
45 #include "strerror.h"
46 #include "curl_multibyte.h"
47 #include "curl_printf.h"
48 #include "hostcheck.h"
49 #include "version_win32.h"
50
51 /* The last #include file should be: */
52 #include "curl_memory.h"
53 #include "memdebug.h"
54
55 #define BACKEND ((struct schannel_ssl_backend_data *)connssl->backend)
56
57
58 #ifdef HAS_MANUAL_VERIFY_API
59
60 #define MAX_CAFILE_SIZE 1048576 /* 1 MiB */
61 #define BEGIN_CERT "-----BEGIN CERTIFICATE-----"
62 #define END_CERT "\n-----END CERTIFICATE-----"
63
64 struct cert_chain_engine_config_win7 {
65 DWORD cbSize;
66 HCERTSTORE hRestrictedRoot;
67 HCERTSTORE hRestrictedTrust;
68 HCERTSTORE hRestrictedOther;
69 DWORD cAdditionalStore;
70 HCERTSTORE *rghAdditionalStore;
71 DWORD dwFlags;
72 DWORD dwUrlRetrievalTimeout;
73 DWORD MaximumCachedCertificates;
74 DWORD CycleDetectionModulus;
75 HCERTSTORE hExclusiveRoot;
76 HCERTSTORE hExclusiveTrustedPeople;
77 };
78
is_cr_or_lf(char c)79 static int is_cr_or_lf(char c)
80 {
81 return c == '\r' || c == '\n';
82 }
83
84 /* Search the substring needle,needlelen into string haystack,haystacklen
85 * Strings do not need to be terminated by a '\0'.
86 * Similar of macOS/Linux memmem (not available on Visual Studio).
87 * Return position of beginning of first occurrence or NULL if not found
88 */
c_memmem(const void * haystack,size_t haystacklen,const void * needle,size_t needlelen)89 static const char *c_memmem(const void *haystack, size_t haystacklen,
90 const void *needle, size_t needlelen)
91 {
92 const char *p;
93 char first;
94 const char *str_limit = (const char *)haystack + haystacklen;
95 if(!needlelen || needlelen > haystacklen)
96 return NULL;
97 first = *(const char *)needle;
98 for(p = (const char *)haystack; p <= (str_limit - needlelen); p++)
99 if(((*p) == first) && (memcmp(p, needle, needlelen) == 0))
100 return p;
101
102 return NULL;
103 }
104
add_certs_data_to_store(HCERTSTORE trust_store,const char * ca_buffer,size_t ca_buffer_size,const char * ca_file_text,struct Curl_easy * data)105 static CURLcode add_certs_data_to_store(HCERTSTORE trust_store,
106 const char *ca_buffer,
107 size_t ca_buffer_size,
108 const char *ca_file_text,
109 struct Curl_easy *data)
110 {
111 const size_t begin_cert_len = strlen(BEGIN_CERT);
112 const size_t end_cert_len = strlen(END_CERT);
113 CURLcode result = CURLE_OK;
114 int num_certs = 0;
115 bool more_certs = 1;
116 const char *current_ca_file_ptr = ca_buffer;
117 const char *ca_buffer_limit = ca_buffer + ca_buffer_size;
118
119 while(more_certs && (current_ca_file_ptr < ca_buffer_limit)) {
120 const char *begin_cert_ptr = c_memmem(current_ca_file_ptr,
121 ca_buffer_limit-current_ca_file_ptr,
122 BEGIN_CERT,
123 begin_cert_len);
124 if(!begin_cert_ptr || !is_cr_or_lf(begin_cert_ptr[begin_cert_len])) {
125 more_certs = 0;
126 }
127 else {
128 const char *end_cert_ptr = c_memmem(begin_cert_ptr,
129 ca_buffer_limit-begin_cert_ptr,
130 END_CERT,
131 end_cert_len);
132 if(!end_cert_ptr) {
133 failf(data,
134 "schannel: CA file '%s' is not correctly formatted",
135 ca_file_text);
136 result = CURLE_SSL_CACERT_BADFILE;
137 more_certs = 0;
138 }
139 else {
140 CERT_BLOB cert_blob;
141 CERT_CONTEXT *cert_context = NULL;
142 BOOL add_cert_result = FALSE;
143 DWORD actual_content_type = 0;
144 DWORD cert_size = (DWORD)
145 ((end_cert_ptr + end_cert_len) - begin_cert_ptr);
146
147 cert_blob.pbData = (BYTE *)begin_cert_ptr;
148 cert_blob.cbData = cert_size;
149 if(!CryptQueryObject(CERT_QUERY_OBJECT_BLOB,
150 &cert_blob,
151 CERT_QUERY_CONTENT_FLAG_CERT,
152 CERT_QUERY_FORMAT_FLAG_ALL,
153 0,
154 NULL,
155 &actual_content_type,
156 NULL,
157 NULL,
158 NULL,
159 (const void **)&cert_context)) {
160 char buffer[STRERROR_LEN];
161 failf(data,
162 "schannel: failed to extract certificate from CA file "
163 "'%s': %s",
164 ca_file_text,
165 Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
166 result = CURLE_SSL_CACERT_BADFILE;
167 more_certs = 0;
168 }
169 else {
170 current_ca_file_ptr = begin_cert_ptr + cert_size;
171
172 /* Sanity check that the cert_context object is the right type */
173 if(CERT_QUERY_CONTENT_CERT != actual_content_type) {
174 failf(data,
175 "schannel: unexpected content type '%lu' when extracting "
176 "certificate from CA file '%s'",
177 actual_content_type, ca_file_text);
178 result = CURLE_SSL_CACERT_BADFILE;
179 more_certs = 0;
180 }
181 else {
182 add_cert_result =
183 CertAddCertificateContextToStore(trust_store,
184 cert_context,
185 CERT_STORE_ADD_ALWAYS,
186 NULL);
187 CertFreeCertificateContext(cert_context);
188 if(!add_cert_result) {
189 char buffer[STRERROR_LEN];
190 failf(data,
191 "schannel: failed to add certificate from CA file '%s' "
192 "to certificate store: %s",
193 ca_file_text,
194 Curl_winapi_strerror(GetLastError(), buffer,
195 sizeof(buffer)));
196 result = CURLE_SSL_CACERT_BADFILE;
197 more_certs = 0;
198 }
199 else {
200 num_certs++;
201 }
202 }
203 }
204 }
205 }
206 }
207
208 if(result == CURLE_OK) {
209 if(!num_certs) {
210 infof(data,
211 "schannel: did not add any certificates from CA file '%s'",
212 ca_file_text);
213 }
214 else {
215 infof(data,
216 "schannel: added %d certificate(s) from CA file '%s'",
217 num_certs, ca_file_text);
218 }
219 }
220 return result;
221 }
222
add_certs_file_to_store(HCERTSTORE trust_store,const char * ca_file,struct Curl_easy * data)223 static CURLcode add_certs_file_to_store(HCERTSTORE trust_store,
224 const char *ca_file,
225 struct Curl_easy *data)
226 {
227 CURLcode result;
228 HANDLE ca_file_handle = INVALID_HANDLE_VALUE;
229 LARGE_INTEGER file_size;
230 char *ca_file_buffer = NULL;
231 TCHAR *ca_file_tstr = NULL;
232 size_t ca_file_bufsize = 0;
233 DWORD total_bytes_read = 0;
234
235 ca_file_tstr = curlx_convert_UTF8_to_tchar((char *)ca_file);
236 if(!ca_file_tstr) {
237 char buffer[STRERROR_LEN];
238 failf(data,
239 "schannel: invalid path name for CA file '%s': %s",
240 ca_file,
241 Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
242 result = CURLE_SSL_CACERT_BADFILE;
243 goto cleanup;
244 }
245
246 /*
247 * Read the CA file completely into memory before parsing it. This
248 * optimizes for the common case where the CA file will be relatively
249 * small ( < 1 MiB ).
250 */
251 ca_file_handle = CreateFile(ca_file_tstr,
252 GENERIC_READ,
253 FILE_SHARE_READ,
254 NULL,
255 OPEN_EXISTING,
256 FILE_ATTRIBUTE_NORMAL,
257 NULL);
258 if(ca_file_handle == INVALID_HANDLE_VALUE) {
259 char buffer[STRERROR_LEN];
260 failf(data,
261 "schannel: failed to open CA file '%s': %s",
262 ca_file,
263 Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
264 result = CURLE_SSL_CACERT_BADFILE;
265 goto cleanup;
266 }
267
268 if(!GetFileSizeEx(ca_file_handle, &file_size)) {
269 char buffer[STRERROR_LEN];
270 failf(data,
271 "schannel: failed to determine size of CA file '%s': %s",
272 ca_file,
273 Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
274 result = CURLE_SSL_CACERT_BADFILE;
275 goto cleanup;
276 }
277
278 if(file_size.QuadPart > MAX_CAFILE_SIZE) {
279 failf(data,
280 "schannel: CA file exceeds max size of %u bytes",
281 MAX_CAFILE_SIZE);
282 result = CURLE_SSL_CACERT_BADFILE;
283 goto cleanup;
284 }
285
286 ca_file_bufsize = (size_t)file_size.QuadPart;
287 ca_file_buffer = (char *)malloc(ca_file_bufsize + 1);
288 if(!ca_file_buffer) {
289 result = CURLE_OUT_OF_MEMORY;
290 goto cleanup;
291 }
292
293 while(total_bytes_read < ca_file_bufsize) {
294 DWORD bytes_to_read = (DWORD)(ca_file_bufsize - total_bytes_read);
295 DWORD bytes_read = 0;
296
297 if(!ReadFile(ca_file_handle, ca_file_buffer + total_bytes_read,
298 bytes_to_read, &bytes_read, NULL)) {
299 char buffer[STRERROR_LEN];
300 failf(data,
301 "schannel: failed to read from CA file '%s': %s",
302 ca_file,
303 Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
304 result = CURLE_SSL_CACERT_BADFILE;
305 goto cleanup;
306 }
307 if(bytes_read == 0) {
308 /* Premature EOF -- adjust the bufsize to the new value */
309 ca_file_bufsize = total_bytes_read;
310 }
311 else {
312 total_bytes_read += bytes_read;
313 }
314 }
315
316 /* Null terminate the buffer */
317 ca_file_buffer[ca_file_bufsize] = '\0';
318
319 result = add_certs_data_to_store(trust_store,
320 ca_file_buffer, ca_file_bufsize,
321 ca_file,
322 data);
323
324 cleanup:
325 if(ca_file_handle != INVALID_HANDLE_VALUE) {
326 CloseHandle(ca_file_handle);
327 }
328 Curl_safefree(ca_file_buffer);
329 curlx_unicodefree(ca_file_tstr);
330
331 return result;
332 }
333
334 #endif /* HAS_MANUAL_VERIFY_API */
335
336 /*
337 * Returns the number of characters necessary to populate all the host_names.
338 * If host_names is not NULL, populate it with all the hostnames. Each string
339 * in the host_names is null-terminated and the last string is double
340 * null-terminated. If no DNS names are found, a single null-terminated empty
341 * string is returned.
342 */
cert_get_name_string(struct Curl_easy * data,CERT_CONTEXT * cert_context,LPTSTR host_names,DWORD length)343 static DWORD cert_get_name_string(struct Curl_easy *data,
344 CERT_CONTEXT *cert_context,
345 LPTSTR host_names,
346 DWORD length)
347 {
348 DWORD actual_length = 0;
349 #if defined(CURL_WINDOWS_UWP)
350 (void)data;
351 (void)cert_context;
352 (void)host_names;
353 (void)length;
354 #else
355 BOOL compute_content = FALSE;
356 CERT_INFO *cert_info = NULL;
357 CERT_EXTENSION *extension = NULL;
358 CRYPT_DECODE_PARA decode_para = {0, 0, 0};
359 CERT_ALT_NAME_INFO *alt_name_info = NULL;
360 DWORD alt_name_info_size = 0;
361 BOOL ret_val = FALSE;
362 LPTSTR current_pos = NULL;
363 DWORD i;
364
365 #ifdef CERT_NAME_SEARCH_ALL_NAMES_FLAG
366 /* CERT_NAME_SEARCH_ALL_NAMES_FLAG is available from Windows 8 onwards. */
367 if(curlx_verify_windows_version(6, 2, 0, PLATFORM_WINNT,
368 VERSION_GREATER_THAN_EQUAL)) {
369 /* CertGetNameString will provide the 8-bit character string without
370 * any decoding */
371 DWORD name_flags =
372 CERT_NAME_DISABLE_IE4_UTF8_FLAG | CERT_NAME_SEARCH_ALL_NAMES_FLAG;
373 actual_length = CertGetNameString(cert_context,
374 CERT_NAME_DNS_TYPE,
375 name_flags,
376 NULL,
377 host_names,
378 length);
379 return actual_length;
380 }
381 #endif
382
383 compute_content = host_names != NULL && length != 0;
384
385 /* Initialize default return values. */
386 actual_length = 1;
387 if(compute_content) {
388 *host_names = '\0';
389 }
390
391 if(!cert_context) {
392 failf(data, "schannel: Null certificate context.");
393 return actual_length;
394 }
395
396 cert_info = cert_context->pCertInfo;
397 if(!cert_info) {
398 failf(data, "schannel: Null certificate info.");
399 return actual_length;
400 }
401
402 extension = CertFindExtension(szOID_SUBJECT_ALT_NAME2,
403 cert_info->cExtension,
404 cert_info->rgExtension);
405 if(!extension) {
406 failf(data, "schannel: CertFindExtension() returned no extension.");
407 return actual_length;
408 }
409
410 decode_para.cbSize = sizeof(CRYPT_DECODE_PARA);
411
412 ret_val =
413 CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
414 szOID_SUBJECT_ALT_NAME2,
415 extension->Value.pbData,
416 extension->Value.cbData,
417 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG,
418 &decode_para,
419 &alt_name_info,
420 &alt_name_info_size);
421 if(!ret_val) {
422 failf(data,
423 "schannel: CryptDecodeObjectEx() returned no alternate name "
424 "information.");
425 return actual_length;
426 }
427
428 current_pos = host_names;
429
430 /* Iterate over the alternate names and populate host_names. */
431 for(i = 0; i < alt_name_info->cAltEntry; i++) {
432 const CERT_ALT_NAME_ENTRY *entry = &alt_name_info->rgAltEntry[i];
433 wchar_t *dns_w = NULL;
434 size_t current_length = 0;
435
436 if(entry->dwAltNameChoice != CERT_ALT_NAME_DNS_NAME) {
437 continue;
438 }
439 if(!entry->pwszDNSName) {
440 infof(data, "schannel: Empty DNS name.");
441 continue;
442 }
443 current_length = wcslen(entry->pwszDNSName) + 1;
444 if(!compute_content) {
445 actual_length += (DWORD)current_length;
446 continue;
447 }
448 /* Sanity check to prevent buffer overrun. */
449 if((actual_length + current_length) > length) {
450 failf(data, "schannel: Not enough memory to list all hostnames.");
451 break;
452 }
453 dns_w = entry->pwszDNSName;
454 /* pwszDNSName is in ia5 string format and hence does not contain any
455 * non-ASCII characters. */
456 while(*dns_w != '\0') {
457 *current_pos++ = (TCHAR)(*dns_w++);
458 }
459 *current_pos++ = '\0';
460 actual_length += (DWORD)current_length;
461 }
462 if(compute_content) {
463 /* Last string has double null-terminator. */
464 *current_pos = '\0';
465 }
466 #endif
467 return actual_length;
468 }
469
470 /* Verify the server's hostname */
Curl_verify_host(struct Curl_cfilter * cf,struct Curl_easy * data)471 CURLcode Curl_verify_host(struct Curl_cfilter *cf,
472 struct Curl_easy *data)
473 {
474 struct ssl_connect_data *connssl = cf->ctx;
475 SECURITY_STATUS sspi_status;
476 CURLcode result = CURLE_PEER_FAILED_VERIFICATION;
477 CERT_CONTEXT *pCertContextServer = NULL;
478 TCHAR *cert_hostname_buff = NULL;
479 size_t cert_hostname_buff_index = 0;
480 const char *conn_hostname = connssl->peer.hostname;
481 size_t hostlen = strlen(conn_hostname);
482 DWORD len = 0;
483 DWORD actual_len = 0;
484
485 sspi_status =
486 Curl_pSecFn->QueryContextAttributes(&BACKEND->ctxt->ctxt_handle,
487 SECPKG_ATTR_REMOTE_CERT_CONTEXT,
488 &pCertContextServer);
489
490 if((sspi_status != SEC_E_OK) || !pCertContextServer) {
491 char buffer[STRERROR_LEN];
492 failf(data, "schannel: Failed to read remote certificate context: %s",
493 Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
494 result = CURLE_PEER_FAILED_VERIFICATION;
495 goto cleanup;
496 }
497
498 /* Determine the size of the string needed for the cert hostname */
499 len = cert_get_name_string(data, pCertContextServer, NULL, 0);
500 if(len == 0) {
501 failf(data,
502 "schannel: CertGetNameString() returned no "
503 "certificate name information");
504 result = CURLE_PEER_FAILED_VERIFICATION;
505 goto cleanup;
506 }
507
508 /* CertGetNameString guarantees that the returned name will not contain
509 * embedded null bytes. This appears to be undocumented behavior.
510 */
511 cert_hostname_buff = (LPTSTR)malloc(len * sizeof(TCHAR));
512 if(!cert_hostname_buff) {
513 result = CURLE_OUT_OF_MEMORY;
514 goto cleanup;
515 }
516 actual_len = cert_get_name_string(
517 data, pCertContextServer, (LPTSTR)cert_hostname_buff, len);
518
519 /* Sanity check */
520 if(actual_len != len) {
521 failf(data,
522 "schannel: CertGetNameString() returned certificate "
523 "name information of unexpected size");
524 result = CURLE_PEER_FAILED_VERIFICATION;
525 goto cleanup;
526 }
527
528 /* cert_hostname_buff contains all DNS names, where each name is
529 * null-terminated and the last DNS name is double null-terminated. Due to
530 * this encoding, use the length of the buffer to iterate over all names.
531 */
532 result = CURLE_PEER_FAILED_VERIFICATION;
533 while(cert_hostname_buff_index < len &&
534 cert_hostname_buff[cert_hostname_buff_index] != TEXT('\0') &&
535 result == CURLE_PEER_FAILED_VERIFICATION) {
536
537 char *cert_hostname;
538
539 /* Comparing the cert name and the connection hostname encoded as UTF-8
540 * is acceptable since both values are assumed to use ASCII
541 * (or some equivalent) encoding
542 */
543 cert_hostname = curlx_convert_tchar_to_UTF8(
544 &cert_hostname_buff[cert_hostname_buff_index]);
545 if(!cert_hostname) {
546 result = CURLE_OUT_OF_MEMORY;
547 }
548 else {
549 if(Curl_cert_hostcheck(cert_hostname, strlen(cert_hostname),
550 conn_hostname, hostlen)) {
551 infof(data,
552 "schannel: connection hostname (%s) validated "
553 "against certificate name (%s)",
554 conn_hostname, cert_hostname);
555 result = CURLE_OK;
556 }
557 else {
558 size_t cert_hostname_len;
559
560 infof(data,
561 "schannel: connection hostname (%s) did not match "
562 "against certificate name (%s)",
563 conn_hostname, cert_hostname);
564
565 cert_hostname_len =
566 _tcslen(&cert_hostname_buff[cert_hostname_buff_index]);
567
568 /* Move on to next cert name */
569 cert_hostname_buff_index += cert_hostname_len + 1;
570
571 result = CURLE_PEER_FAILED_VERIFICATION;
572 }
573 curlx_unicodefree(cert_hostname);
574 }
575 }
576
577 if(result == CURLE_PEER_FAILED_VERIFICATION) {
578 failf(data,
579 "schannel: CertGetNameString() failed to match "
580 "connection hostname (%s) against server certificate names",
581 conn_hostname);
582 }
583 else if(result != CURLE_OK)
584 failf(data, "schannel: server certificate name verification failed");
585
586 cleanup:
587 Curl_safefree(cert_hostname_buff);
588
589 if(pCertContextServer)
590 CertFreeCertificateContext(pCertContextServer);
591
592 return result;
593 }
594
595
596 #ifdef HAS_MANUAL_VERIFY_API
597 /* Verify the server's certificate and hostname */
Curl_verify_certificate(struct Curl_cfilter * cf,struct Curl_easy * data)598 CURLcode Curl_verify_certificate(struct Curl_cfilter *cf,
599 struct Curl_easy *data)
600 {
601 struct ssl_connect_data *connssl = cf->ctx;
602 struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
603 struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
604 SECURITY_STATUS sspi_status;
605 CURLcode result = CURLE_OK;
606 CERT_CONTEXT *pCertContextServer = NULL;
607 const CERT_CHAIN_CONTEXT *pChainContext = NULL;
608 HCERTCHAINENGINE cert_chain_engine = NULL;
609 HCERTSTORE trust_store = NULL;
610 HCERTSTORE own_trust_store = NULL;
611
612 DEBUGASSERT(BACKEND);
613
614 sspi_status =
615 Curl_pSecFn->QueryContextAttributes(&BACKEND->ctxt->ctxt_handle,
616 SECPKG_ATTR_REMOTE_CERT_CONTEXT,
617 &pCertContextServer);
618
619 if((sspi_status != SEC_E_OK) || !pCertContextServer) {
620 char buffer[STRERROR_LEN];
621 failf(data, "schannel: Failed to read remote certificate context: %s",
622 Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
623 result = CURLE_PEER_FAILED_VERIFICATION;
624 }
625
626 if(result == CURLE_OK &&
627 (conn_config->CAfile || conn_config->ca_info_blob) &&
628 BACKEND->use_manual_cred_validation) {
629 /*
630 * Create a chain engine that uses the certificates in the CA file as
631 * trusted certificates. This is only supported on Windows 7+.
632 */
633
634 if(curlx_verify_windows_version(6, 1, 0, PLATFORM_WINNT,
635 VERSION_LESS_THAN)) {
636 failf(data, "schannel: this version of Windows is too old to support "
637 "certificate verification via CA bundle file.");
638 result = CURLE_SSL_CACERT_BADFILE;
639 }
640 else {
641 /* try cache */
642 trust_store = Curl_schannel_get_cached_cert_store(cf, data);
643
644 if(trust_store) {
645 infof(data, "schannel: reusing certificate store from cache");
646 }
647 else {
648 /* Open the certificate store */
649 trust_store = CertOpenStore(CERT_STORE_PROV_MEMORY,
650 0,
651 (HCRYPTPROV)NULL,
652 CERT_STORE_CREATE_NEW_FLAG,
653 NULL);
654 if(!trust_store) {
655 char buffer[STRERROR_LEN];
656 failf(data, "schannel: failed to create certificate store: %s",
657 Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
658 result = CURLE_SSL_CACERT_BADFILE;
659 }
660 else {
661 const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
662 own_trust_store = trust_store;
663
664 if(ca_info_blob) {
665 result = add_certs_data_to_store(trust_store,
666 (const char *)ca_info_blob->data,
667 ca_info_blob->len,
668 "(memory blob)",
669 data);
670 }
671 else {
672 result = add_certs_file_to_store(trust_store,
673 conn_config->CAfile,
674 data);
675 }
676 if(result == CURLE_OK) {
677 if(Curl_schannel_set_cached_cert_store(cf, data, trust_store)) {
678 own_trust_store = NULL;
679 }
680 }
681 }
682 }
683 }
684
685 if(result == CURLE_OK) {
686 struct cert_chain_engine_config_win7 engine_config;
687 BOOL create_engine_result;
688
689 memset(&engine_config, 0, sizeof(engine_config));
690 engine_config.cbSize = sizeof(engine_config);
691 engine_config.hExclusiveRoot = trust_store;
692
693 /* CertCreateCertificateChainEngine will check the expected size of the
694 * CERT_CHAIN_ENGINE_CONFIG structure and fail if the specified size
695 * does not match the expected size. When this occurs, it indicates that
696 * CAINFO is not supported on the version of Windows in use.
697 */
698 create_engine_result =
699 CertCreateCertificateChainEngine(
700 (CERT_CHAIN_ENGINE_CONFIG *)&engine_config, &cert_chain_engine);
701 if(!create_engine_result) {
702 char buffer[STRERROR_LEN];
703 failf(data,
704 "schannel: failed to create certificate chain engine: %s",
705 Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
706 result = CURLE_SSL_CACERT_BADFILE;
707 }
708 }
709 }
710
711 if(result == CURLE_OK) {
712 CERT_CHAIN_PARA ChainPara;
713
714 memset(&ChainPara, 0, sizeof(ChainPara));
715 ChainPara.cbSize = sizeof(ChainPara);
716
717 if(!CertGetCertificateChain(cert_chain_engine,
718 pCertContextServer,
719 NULL,
720 pCertContextServer->hCertStore,
721 &ChainPara,
722 (ssl_config->no_revoke ? 0 :
723 CERT_CHAIN_REVOCATION_CHECK_CHAIN),
724 NULL,
725 &pChainContext)) {
726 char buffer[STRERROR_LEN];
727 failf(data, "schannel: CertGetCertificateChain failed: %s",
728 Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
729 pChainContext = NULL;
730 result = CURLE_PEER_FAILED_VERIFICATION;
731 }
732
733 if(result == CURLE_OK) {
734 CERT_SIMPLE_CHAIN *pSimpleChain = pChainContext->rgpChain[0];
735 DWORD dwTrustErrorMask = ~(DWORD)(CERT_TRUST_IS_NOT_TIME_NESTED);
736 dwTrustErrorMask &= pSimpleChain->TrustStatus.dwErrorStatus;
737
738 if(data->set.ssl.revoke_best_effort) {
739 /* Ignore errors when root certificates are missing the revocation
740 * list URL, or when the list could not be downloaded because the
741 * server is currently unreachable. */
742 dwTrustErrorMask &= ~(DWORD)(CERT_TRUST_REVOCATION_STATUS_UNKNOWN |
743 CERT_TRUST_IS_OFFLINE_REVOCATION);
744 }
745
746 if(dwTrustErrorMask) {
747 if(dwTrustErrorMask & CERT_TRUST_IS_REVOKED)
748 failf(data, "schannel: CertGetCertificateChain trust error"
749 " CERT_TRUST_IS_REVOKED");
750 else if(dwTrustErrorMask & CERT_TRUST_IS_PARTIAL_CHAIN)
751 failf(data, "schannel: CertGetCertificateChain trust error"
752 " CERT_TRUST_IS_PARTIAL_CHAIN");
753 else if(dwTrustErrorMask & CERT_TRUST_IS_UNTRUSTED_ROOT)
754 failf(data, "schannel: CertGetCertificateChain trust error"
755 " CERT_TRUST_IS_UNTRUSTED_ROOT");
756 else if(dwTrustErrorMask & CERT_TRUST_IS_NOT_TIME_VALID)
757 failf(data, "schannel: CertGetCertificateChain trust error"
758 " CERT_TRUST_IS_NOT_TIME_VALID");
759 else if(dwTrustErrorMask & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
760 failf(data, "schannel: CertGetCertificateChain trust error"
761 " CERT_TRUST_REVOCATION_STATUS_UNKNOWN");
762 else
763 failf(data, "schannel: CertGetCertificateChain error mask: 0x%08lx",
764 dwTrustErrorMask);
765 result = CURLE_PEER_FAILED_VERIFICATION;
766 }
767 }
768 }
769
770 if(result == CURLE_OK) {
771 if(conn_config->verifyhost) {
772 result = Curl_verify_host(cf, data);
773 }
774 }
775
776 if(cert_chain_engine) {
777 CertFreeCertificateChainEngine(cert_chain_engine);
778 }
779
780 if(own_trust_store) {
781 CertCloseStore(own_trust_store, 0);
782 }
783
784 if(pChainContext)
785 CertFreeCertificateChain(pChainContext);
786
787 if(pCertContextServer)
788 CertFreeCertificateContext(pCertContextServer);
789
790 return result;
791 }
792
793 #endif /* HAS_MANUAL_VERIFY_API */
794 #endif /* USE_SCHANNEL */
795