schannel_verify.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. /* Search the substring needle,needlelen into string haystack,haystacklen
  69. * Strings don't need to be terminated by a '\0'.
  70. * Similar of OSX/Linux memmem (not available on Visual Studio).
  71. * Return position of beginning of first occurrence or NULL if not found
  72. */
  73. static const char *c_memmem(const void *haystack, size_t haystacklen,
  74. const void *needle, size_t needlelen)
  75. {
  76. const char *p;
  77. char first;
  78. const char *str_limit = (const char *)haystack + haystacklen;
  79. if(!needlelen || needlelen > haystacklen)
  80. return NULL;
  81. first = *(const char *)needle;
  82. for(p = (const char *)haystack; p <= (str_limit - needlelen); p++)
  83. if(((*p) == first) && (memcmp(p, needle, needlelen) == 0))
  84. return p;
  85. return NULL;
  86. }
  87. static CURLcode add_certs_data_to_store(HCERTSTORE trust_store,
  88. const char *ca_buffer,
  89. size_t ca_buffer_size,
  90. const char *ca_file_text,
  91. struct Curl_easy *data)
  92. {
  93. const size_t begin_cert_len = strlen(BEGIN_CERT);
  94. const size_t end_cert_len = strlen(END_CERT);
  95. CURLcode result = CURLE_OK;
  96. int num_certs = 0;
  97. bool more_certs = 1;
  98. const char *current_ca_file_ptr = ca_buffer;
  99. const char *ca_buffer_limit = ca_buffer + ca_buffer_size;
  100. while(more_certs && (current_ca_file_ptr<ca_buffer_limit)) {
  101. const char *begin_cert_ptr = c_memmem(current_ca_file_ptr,
  102. ca_buffer_limit-current_ca_file_ptr,
  103. BEGIN_CERT,
  104. begin_cert_len);
  105. if(!begin_cert_ptr || !is_cr_or_lf(begin_cert_ptr[begin_cert_len])) {
  106. more_certs = 0;
  107. }
  108. else {
  109. const char *end_cert_ptr = c_memmem(begin_cert_ptr,
  110. ca_buffer_limit-begin_cert_ptr,
  111. END_CERT,
  112. end_cert_len);
  113. if(!end_cert_ptr) {
  114. failf(data,
  115. "schannel: CA file '%s' is not correctly formatted",
  116. ca_file_text);
  117. result = CURLE_SSL_CACERT_BADFILE;
  118. more_certs = 0;
  119. }
  120. else {
  121. CERT_BLOB cert_blob;
  122. CERT_CONTEXT *cert_context = NULL;
  123. BOOL add_cert_result = FALSE;
  124. DWORD actual_content_type = 0;
  125. DWORD cert_size = (DWORD)
  126. ((end_cert_ptr + end_cert_len) - begin_cert_ptr);
  127. cert_blob.pbData = (BYTE *)begin_cert_ptr;
  128. cert_blob.cbData = cert_size;
  129. if(!CryptQueryObject(CERT_QUERY_OBJECT_BLOB,
  130. &cert_blob,
  131. CERT_QUERY_CONTENT_FLAG_CERT,
  132. CERT_QUERY_FORMAT_FLAG_ALL,
  133. 0,
  134. NULL,
  135. &actual_content_type,
  136. NULL,
  137. NULL,
  138. NULL,
  139. (const void **)&cert_context)) {
  140. char buffer[STRERROR_LEN];
  141. failf(data,
  142. "schannel: failed to extract certificate from CA file "
  143. "'%s': %s",
  144. ca_file_text,
  145. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  146. result = CURLE_SSL_CACERT_BADFILE;
  147. more_certs = 0;
  148. }
  149. else {
  150. current_ca_file_ptr = begin_cert_ptr + cert_size;
  151. /* Sanity check that the cert_context object is the right type */
  152. if(CERT_QUERY_CONTENT_CERT != actual_content_type) {
  153. failf(data,
  154. "schannel: unexpected content type '%d' when extracting "
  155. "certificate from CA file '%s'",
  156. actual_content_type, ca_file_text);
  157. result = CURLE_SSL_CACERT_BADFILE;
  158. more_certs = 0;
  159. }
  160. else {
  161. add_cert_result =
  162. CertAddCertificateContextToStore(trust_store,
  163. cert_context,
  164. CERT_STORE_ADD_ALWAYS,
  165. NULL);
  166. CertFreeCertificateContext(cert_context);
  167. if(!add_cert_result) {
  168. char buffer[STRERROR_LEN];
  169. failf(data,
  170. "schannel: failed to add certificate from CA file '%s' "
  171. "to certificate store: %s",
  172. ca_file_text,
  173. Curl_winapi_strerror(GetLastError(), buffer,
  174. sizeof(buffer)));
  175. result = CURLE_SSL_CACERT_BADFILE;
  176. more_certs = 0;
  177. }
  178. else {
  179. num_certs++;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. if(result == CURLE_OK) {
  187. if(!num_certs) {
  188. infof(data,
  189. "schannel: did not add any certificates from CA file '%s'",
  190. ca_file_text);
  191. }
  192. else {
  193. infof(data,
  194. "schannel: added %d certificate(s) from CA file '%s'",
  195. num_certs, ca_file_text);
  196. }
  197. }
  198. return result;
  199. }
  200. static CURLcode add_certs_file_to_store(HCERTSTORE trust_store,
  201. const char *ca_file,
  202. struct Curl_easy *data)
  203. {
  204. CURLcode result;
  205. HANDLE ca_file_handle = INVALID_HANDLE_VALUE;
  206. LARGE_INTEGER file_size;
  207. char *ca_file_buffer = NULL;
  208. TCHAR *ca_file_tstr = NULL;
  209. size_t ca_file_bufsize = 0;
  210. DWORD total_bytes_read = 0;
  211. ca_file_tstr = curlx_convert_UTF8_to_tchar((char *)ca_file);
  212. if(!ca_file_tstr) {
  213. char buffer[STRERROR_LEN];
  214. failf(data,
  215. "schannel: invalid path name for CA file '%s': %s",
  216. ca_file,
  217. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  218. result = CURLE_SSL_CACERT_BADFILE;
  219. goto cleanup;
  220. }
  221. /*
  222. * Read the CA file completely into memory before parsing it. This
  223. * optimizes for the common case where the CA file will be relatively
  224. * small ( < 1 MiB ).
  225. */
  226. ca_file_handle = CreateFile(ca_file_tstr,
  227. GENERIC_READ,
  228. FILE_SHARE_READ,
  229. NULL,
  230. OPEN_EXISTING,
  231. FILE_ATTRIBUTE_NORMAL,
  232. NULL);
  233. if(ca_file_handle == INVALID_HANDLE_VALUE) {
  234. char buffer[STRERROR_LEN];
  235. failf(data,
  236. "schannel: failed to open CA file '%s': %s",
  237. ca_file,
  238. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  239. result = CURLE_SSL_CACERT_BADFILE;
  240. goto cleanup;
  241. }
  242. if(!GetFileSizeEx(ca_file_handle, &file_size)) {
  243. char buffer[STRERROR_LEN];
  244. failf(data,
  245. "schannel: failed to determine size of CA file '%s': %s",
  246. ca_file,
  247. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  248. result = CURLE_SSL_CACERT_BADFILE;
  249. goto cleanup;
  250. }
  251. if(file_size.QuadPart > MAX_CAFILE_SIZE) {
  252. failf(data,
  253. "schannel: CA file exceeds max size of %u bytes",
  254. MAX_CAFILE_SIZE);
  255. result = CURLE_SSL_CACERT_BADFILE;
  256. goto cleanup;
  257. }
  258. ca_file_bufsize = (size_t)file_size.QuadPart;
  259. ca_file_buffer = (char *)malloc(ca_file_bufsize + 1);
  260. if(!ca_file_buffer) {
  261. result = CURLE_OUT_OF_MEMORY;
  262. goto cleanup;
  263. }
  264. result = CURLE_OK;
  265. while(total_bytes_read < ca_file_bufsize) {
  266. DWORD bytes_to_read = (DWORD)(ca_file_bufsize - total_bytes_read);
  267. DWORD bytes_read = 0;
  268. if(!ReadFile(ca_file_handle, ca_file_buffer + total_bytes_read,
  269. bytes_to_read, &bytes_read, NULL)) {
  270. char buffer[STRERROR_LEN];
  271. failf(data,
  272. "schannel: failed to read from CA file '%s': %s",
  273. ca_file,
  274. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  275. result = CURLE_SSL_CACERT_BADFILE;
  276. goto cleanup;
  277. }
  278. if(bytes_read == 0) {
  279. /* Premature EOF -- adjust the bufsize to the new value */
  280. ca_file_bufsize = total_bytes_read;
  281. }
  282. else {
  283. total_bytes_read += bytes_read;
  284. }
  285. }
  286. /* Null terminate the buffer */
  287. ca_file_buffer[ca_file_bufsize] = '\0';
  288. if(result != CURLE_OK) {
  289. goto cleanup;
  290. }
  291. result = add_certs_data_to_store(trust_store,
  292. ca_file_buffer, ca_file_bufsize,
  293. ca_file,
  294. data);
  295. cleanup:
  296. if(ca_file_handle != INVALID_HANDLE_VALUE) {
  297. CloseHandle(ca_file_handle);
  298. }
  299. Curl_safefree(ca_file_buffer);
  300. curlx_unicodefree(ca_file_tstr);
  301. return result;
  302. }
  303. /*
  304. * Returns the number of characters necessary to populate all the host_names.
  305. * If host_names is not NULL, populate it with all the host names. Each string
  306. * in the host_names is null-terminated and the last string is double
  307. * null-terminated. If no DNS names are found, a single null-terminated empty
  308. * string is returned.
  309. */
  310. static DWORD cert_get_name_string(struct Curl_easy *data,
  311. CERT_CONTEXT *cert_context,
  312. LPTSTR host_names,
  313. DWORD length)
  314. {
  315. DWORD actual_length = 0;
  316. BOOL compute_content = FALSE;
  317. CERT_INFO *cert_info = NULL;
  318. CERT_EXTENSION *extension = NULL;
  319. CRYPT_DECODE_PARA decode_para = {0, 0, 0};
  320. CERT_ALT_NAME_INFO *alt_name_info = NULL;
  321. DWORD alt_name_info_size = 0;
  322. BOOL ret_val = FALSE;
  323. LPTSTR current_pos = NULL;
  324. DWORD i;
  325. /* CERT_NAME_SEARCH_ALL_NAMES_FLAG is available from Windows 8 onwards. */
  326. if(curlx_verify_windows_version(6, 2, PLATFORM_WINNT,
  327. VERSION_GREATER_THAN_EQUAL)) {
  328. #ifdef CERT_NAME_SEARCH_ALL_NAMES_FLAG
  329. /* CertGetNameString will provide the 8-bit character string without
  330. * any decoding */
  331. DWORD name_flags =
  332. CERT_NAME_DISABLE_IE4_UTF8_FLAG | CERT_NAME_SEARCH_ALL_NAMES_FLAG;
  333. actual_length = CertGetNameString(cert_context,
  334. CERT_NAME_DNS_TYPE,
  335. name_flags,
  336. NULL,
  337. host_names,
  338. length);
  339. return actual_length;
  340. #endif
  341. }
  342. compute_content = host_names != NULL && length != 0;
  343. /* Initialize default return values. */
  344. actual_length = 1;
  345. if(compute_content) {
  346. *host_names = '\0';
  347. }
  348. if(!cert_context) {
  349. failf(data, "schannel: Null certificate context.");
  350. return actual_length;
  351. }
  352. cert_info = cert_context->pCertInfo;
  353. if(!cert_info) {
  354. failf(data, "schannel: Null certificate info.");
  355. return actual_length;
  356. }
  357. extension = CertFindExtension(szOID_SUBJECT_ALT_NAME2,
  358. cert_info->cExtension,
  359. cert_info->rgExtension);
  360. if(!extension) {
  361. failf(data, "schannel: CertFindExtension() returned no extension.");
  362. return actual_length;
  363. }
  364. decode_para.cbSize = sizeof(CRYPT_DECODE_PARA);
  365. ret_val =
  366. CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  367. szOID_SUBJECT_ALT_NAME2,
  368. extension->Value.pbData,
  369. extension->Value.cbData,
  370. CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG,
  371. &decode_para,
  372. &alt_name_info,
  373. &alt_name_info_size);
  374. if(!ret_val) {
  375. failf(data,
  376. "schannel: CryptDecodeObjectEx() returned no alternate name "
  377. "information.");
  378. return actual_length;
  379. }
  380. current_pos = host_names;
  381. /* Iterate over the alternate names and populate host_names. */
  382. for(i = 0; i < alt_name_info->cAltEntry; i++) {
  383. const CERT_ALT_NAME_ENTRY *entry = &alt_name_info->rgAltEntry[i];
  384. wchar_t *dns_w = NULL;
  385. size_t current_length = 0;
  386. if(entry->dwAltNameChoice != CERT_ALT_NAME_DNS_NAME) {
  387. continue;
  388. }
  389. if(!entry->pwszDNSName) {
  390. infof(data, "schannel: Empty DNS name.");
  391. continue;
  392. }
  393. current_length = wcslen(entry->pwszDNSName) + 1;
  394. if(!compute_content) {
  395. actual_length += (DWORD)current_length;
  396. continue;
  397. }
  398. /* Sanity check to prevent buffer overrun. */
  399. if((actual_length + current_length) > length) {
  400. failf(data, "schannel: Not enough memory to list all host names.");
  401. break;
  402. }
  403. dns_w = entry->pwszDNSName;
  404. /* pwszDNSName is in ia5 string format and hence doesn't contain any
  405. * non-ascii characters. */
  406. while(*dns_w != '\0') {
  407. *current_pos++ = (char)(*dns_w++);
  408. }
  409. *current_pos++ = '\0';
  410. actual_length += (DWORD)current_length;
  411. }
  412. if(compute_content) {
  413. /* Last string has double null-terminator. */
  414. *current_pos = '\0';
  415. }
  416. return actual_length;
  417. }
  418. static CURLcode verify_host(struct Curl_easy *data,
  419. CERT_CONTEXT *pCertContextServer,
  420. const char * const conn_hostname)
  421. {
  422. CURLcode result = CURLE_PEER_FAILED_VERIFICATION;
  423. TCHAR *cert_hostname_buff = NULL;
  424. size_t cert_hostname_buff_index = 0;
  425. DWORD len = 0;
  426. DWORD actual_len = 0;
  427. /* Determine the size of the string needed for the cert hostname */
  428. len = cert_get_name_string(data, pCertContextServer, NULL, 0);
  429. if(len == 0) {
  430. failf(data,
  431. "schannel: CertGetNameString() returned no "
  432. "certificate name information");
  433. result = CURLE_PEER_FAILED_VERIFICATION;
  434. goto cleanup;
  435. }
  436. /* CertGetNameString guarantees that the returned name will not contain
  437. * embedded null bytes. This appears to be undocumented behavior.
  438. */
  439. cert_hostname_buff = (LPTSTR)malloc(len * sizeof(TCHAR));
  440. if(!cert_hostname_buff) {
  441. result = CURLE_OUT_OF_MEMORY;
  442. goto cleanup;
  443. }
  444. actual_len = cert_get_name_string(
  445. data, pCertContextServer, (LPTSTR)cert_hostname_buff, len);
  446. /* Sanity check */
  447. if(actual_len != len) {
  448. failf(data,
  449. "schannel: CertGetNameString() returned certificate "
  450. "name information of unexpected size");
  451. result = CURLE_PEER_FAILED_VERIFICATION;
  452. goto cleanup;
  453. }
  454. /* If HAVE_CERT_NAME_SEARCH_ALL_NAMES is available, the output
  455. * will contain all DNS names, where each name is null-terminated
  456. * and the last DNS name is double null-terminated. Due to this
  457. * encoding, use the length of the buffer to iterate over all names.
  458. */
  459. result = CURLE_PEER_FAILED_VERIFICATION;
  460. while(cert_hostname_buff_index < len &&
  461. cert_hostname_buff[cert_hostname_buff_index] != TEXT('\0') &&
  462. result == CURLE_PEER_FAILED_VERIFICATION) {
  463. char *cert_hostname;
  464. /* Comparing the cert name and the connection hostname encoded as UTF-8
  465. * is acceptable since both values are assumed to use ASCII
  466. * (or some equivalent) encoding
  467. */
  468. cert_hostname = curlx_convert_tchar_to_UTF8(
  469. &cert_hostname_buff[cert_hostname_buff_index]);
  470. if(!cert_hostname) {
  471. result = CURLE_OUT_OF_MEMORY;
  472. }
  473. else {
  474. int match_result;
  475. match_result = Curl_cert_hostcheck(cert_hostname, conn_hostname);
  476. if(match_result == CURL_HOST_MATCH) {
  477. infof(data,
  478. "schannel: connection hostname (%s) validated "
  479. "against certificate name (%s)",
  480. conn_hostname, cert_hostname);
  481. result = CURLE_OK;
  482. }
  483. else {
  484. size_t cert_hostname_len;
  485. infof(data,
  486. "schannel: connection hostname (%s) did not match "
  487. "against certificate name (%s)",
  488. conn_hostname, cert_hostname);
  489. cert_hostname_len =
  490. _tcslen(&cert_hostname_buff[cert_hostname_buff_index]);
  491. /* Move on to next cert name */
  492. cert_hostname_buff_index += cert_hostname_len + 1;
  493. result = CURLE_PEER_FAILED_VERIFICATION;
  494. }
  495. curlx_unicodefree(cert_hostname);
  496. }
  497. }
  498. if(result == CURLE_PEER_FAILED_VERIFICATION) {
  499. failf(data,
  500. "schannel: CertGetNameString() failed to match "
  501. "connection hostname (%s) against server certificate names",
  502. conn_hostname);
  503. }
  504. else if(result != CURLE_OK)
  505. failf(data, "schannel: server certificate name verification failed");
  506. cleanup:
  507. Curl_safefree(cert_hostname_buff);
  508. return result;
  509. }
  510. CURLcode Curl_verify_certificate(struct Curl_easy *data,
  511. struct connectdata *conn, int sockindex)
  512. {
  513. SECURITY_STATUS sspi_status;
  514. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  515. CURLcode result = CURLE_OK;
  516. CERT_CONTEXT *pCertContextServer = NULL;
  517. const CERT_CHAIN_CONTEXT *pChainContext = NULL;
  518. HCERTCHAINENGINE cert_chain_engine = NULL;
  519. HCERTSTORE trust_store = NULL;
  520. const char * const conn_hostname = SSL_HOST_NAME();
  521. sspi_status =
  522. s_pSecFn->QueryContextAttributes(&BACKEND->ctxt->ctxt_handle,
  523. SECPKG_ATTR_REMOTE_CERT_CONTEXT,
  524. &pCertContextServer);
  525. if((sspi_status != SEC_E_OK) || !pCertContextServer) {
  526. char buffer[STRERROR_LEN];
  527. failf(data, "schannel: Failed to read remote certificate context: %s",
  528. Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
  529. result = CURLE_PEER_FAILED_VERIFICATION;
  530. }
  531. if(result == CURLE_OK &&
  532. (SSL_CONN_CONFIG(CAfile) || SSL_CONN_CONFIG(ca_info_blob)) &&
  533. BACKEND->use_manual_cred_validation) {
  534. /*
  535. * Create a chain engine that uses the certificates in the CA file as
  536. * trusted certificates. This is only supported on Windows 7+.
  537. */
  538. if(curlx_verify_windows_version(6, 1, PLATFORM_WINNT, 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 */