schannel_verify.c 23 KB

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