2
0

schannel_verify.c 25 KB

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