schannel_verify.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. ***************************************************************************/
  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. while(total_bytes_read < ca_file_bufsize) {
  265. DWORD bytes_to_read = (DWORD)(ca_file_bufsize - total_bytes_read);
  266. DWORD bytes_read = 0;
  267. if(!ReadFile(ca_file_handle, ca_file_buffer + total_bytes_read,
  268. bytes_to_read, &bytes_read, NULL)) {
  269. char buffer[STRERROR_LEN];
  270. failf(data,
  271. "schannel: failed to read from 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. if(bytes_read == 0) {
  278. /* Premature EOF -- adjust the bufsize to the new value */
  279. ca_file_bufsize = total_bytes_read;
  280. }
  281. else {
  282. total_bytes_read += bytes_read;
  283. }
  284. }
  285. /* Null terminate the buffer */
  286. ca_file_buffer[ca_file_bufsize] = '\0';
  287. result = add_certs_data_to_store(trust_store,
  288. ca_file_buffer, ca_file_bufsize,
  289. ca_file,
  290. data);
  291. cleanup:
  292. if(ca_file_handle != INVALID_HANDLE_VALUE) {
  293. CloseHandle(ca_file_handle);
  294. }
  295. Curl_safefree(ca_file_buffer);
  296. curlx_unicodefree(ca_file_tstr);
  297. return result;
  298. }
  299. /*
  300. * Returns the number of characters necessary to populate all the host_names.
  301. * If host_names is not NULL, populate it with all the host names. Each string
  302. * in the host_names is null-terminated and the last string is double
  303. * null-terminated. If no DNS names are found, a single null-terminated empty
  304. * string is returned.
  305. */
  306. static DWORD cert_get_name_string(struct Curl_easy *data,
  307. CERT_CONTEXT *cert_context,
  308. LPTSTR host_names,
  309. DWORD length)
  310. {
  311. DWORD actual_length = 0;
  312. BOOL compute_content = FALSE;
  313. CERT_INFO *cert_info = NULL;
  314. CERT_EXTENSION *extension = NULL;
  315. CRYPT_DECODE_PARA decode_para = {0, 0, 0};
  316. CERT_ALT_NAME_INFO *alt_name_info = NULL;
  317. DWORD alt_name_info_size = 0;
  318. BOOL ret_val = FALSE;
  319. LPTSTR current_pos = NULL;
  320. DWORD i;
  321. /* CERT_NAME_SEARCH_ALL_NAMES_FLAG is available from Windows 8 onwards. */
  322. if(curlx_verify_windows_version(6, 2, 0, PLATFORM_WINNT,
  323. VERSION_GREATER_THAN_EQUAL)) {
  324. #ifdef CERT_NAME_SEARCH_ALL_NAMES_FLAG
  325. /* CertGetNameString will provide the 8-bit character string without
  326. * any decoding */
  327. DWORD name_flags =
  328. CERT_NAME_DISABLE_IE4_UTF8_FLAG | CERT_NAME_SEARCH_ALL_NAMES_FLAG;
  329. actual_length = CertGetNameString(cert_context,
  330. CERT_NAME_DNS_TYPE,
  331. name_flags,
  332. NULL,
  333. host_names,
  334. length);
  335. return actual_length;
  336. #endif
  337. }
  338. compute_content = host_names != NULL && length != 0;
  339. /* Initialize default return values. */
  340. actual_length = 1;
  341. if(compute_content) {
  342. *host_names = '\0';
  343. }
  344. if(!cert_context) {
  345. failf(data, "schannel: Null certificate context.");
  346. return actual_length;
  347. }
  348. cert_info = cert_context->pCertInfo;
  349. if(!cert_info) {
  350. failf(data, "schannel: Null certificate info.");
  351. return actual_length;
  352. }
  353. extension = CertFindExtension(szOID_SUBJECT_ALT_NAME2,
  354. cert_info->cExtension,
  355. cert_info->rgExtension);
  356. if(!extension) {
  357. failf(data, "schannel: CertFindExtension() returned no extension.");
  358. return actual_length;
  359. }
  360. decode_para.cbSize = sizeof(CRYPT_DECODE_PARA);
  361. ret_val =
  362. CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  363. szOID_SUBJECT_ALT_NAME2,
  364. extension->Value.pbData,
  365. extension->Value.cbData,
  366. CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG,
  367. &decode_para,
  368. &alt_name_info,
  369. &alt_name_info_size);
  370. if(!ret_val) {
  371. failf(data,
  372. "schannel: CryptDecodeObjectEx() returned no alternate name "
  373. "information.");
  374. return actual_length;
  375. }
  376. current_pos = host_names;
  377. /* Iterate over the alternate names and populate host_names. */
  378. for(i = 0; i < alt_name_info->cAltEntry; i++) {
  379. const CERT_ALT_NAME_ENTRY *entry = &alt_name_info->rgAltEntry[i];
  380. wchar_t *dns_w = NULL;
  381. size_t current_length = 0;
  382. if(entry->dwAltNameChoice != CERT_ALT_NAME_DNS_NAME) {
  383. continue;
  384. }
  385. if(!entry->pwszDNSName) {
  386. infof(data, "schannel: Empty DNS name.");
  387. continue;
  388. }
  389. current_length = wcslen(entry->pwszDNSName) + 1;
  390. if(!compute_content) {
  391. actual_length += (DWORD)current_length;
  392. continue;
  393. }
  394. /* Sanity check to prevent buffer overrun. */
  395. if((actual_length + current_length) > length) {
  396. failf(data, "schannel: Not enough memory to list all host names.");
  397. break;
  398. }
  399. dns_w = entry->pwszDNSName;
  400. /* pwszDNSName is in ia5 string format and hence doesn't contain any
  401. * non-ascii characters. */
  402. while(*dns_w != '\0') {
  403. *current_pos++ = (char)(*dns_w++);
  404. }
  405. *current_pos++ = '\0';
  406. actual_length += (DWORD)current_length;
  407. }
  408. if(compute_content) {
  409. /* Last string has double null-terminator. */
  410. *current_pos = '\0';
  411. }
  412. return actual_length;
  413. }
  414. static CURLcode verify_host(struct Curl_easy *data,
  415. CERT_CONTEXT *pCertContextServer,
  416. const char * const conn_hostname)
  417. {
  418. CURLcode result = CURLE_PEER_FAILED_VERIFICATION;
  419. TCHAR *cert_hostname_buff = NULL;
  420. size_t cert_hostname_buff_index = 0;
  421. size_t hostlen = strlen(conn_hostname);
  422. DWORD len = 0;
  423. DWORD actual_len = 0;
  424. /* Determine the size of the string needed for the cert hostname */
  425. len = cert_get_name_string(data, pCertContextServer, NULL, 0);
  426. if(len == 0) {
  427. failf(data,
  428. "schannel: CertGetNameString() returned no "
  429. "certificate name information");
  430. result = CURLE_PEER_FAILED_VERIFICATION;
  431. goto cleanup;
  432. }
  433. /* CertGetNameString guarantees that the returned name will not contain
  434. * embedded null bytes. This appears to be undocumented behavior.
  435. */
  436. cert_hostname_buff = (LPTSTR)malloc(len * sizeof(TCHAR));
  437. if(!cert_hostname_buff) {
  438. result = CURLE_OUT_OF_MEMORY;
  439. goto cleanup;
  440. }
  441. actual_len = cert_get_name_string(
  442. data, pCertContextServer, (LPTSTR)cert_hostname_buff, len);
  443. /* Sanity check */
  444. if(actual_len != len) {
  445. failf(data,
  446. "schannel: CertGetNameString() returned certificate "
  447. "name information of unexpected size");
  448. result = CURLE_PEER_FAILED_VERIFICATION;
  449. goto cleanup;
  450. }
  451. /* If HAVE_CERT_NAME_SEARCH_ALL_NAMES is available, the output
  452. * will contain all DNS names, where each name is null-terminated
  453. * and the last DNS name is double null-terminated. Due to this
  454. * encoding, use the length of the buffer to iterate over all names.
  455. */
  456. result = CURLE_PEER_FAILED_VERIFICATION;
  457. while(cert_hostname_buff_index < len &&
  458. cert_hostname_buff[cert_hostname_buff_index] != TEXT('\0') &&
  459. result == CURLE_PEER_FAILED_VERIFICATION) {
  460. char *cert_hostname;
  461. /* Comparing the cert name and the connection hostname encoded as UTF-8
  462. * is acceptable since both values are assumed to use ASCII
  463. * (or some equivalent) encoding
  464. */
  465. cert_hostname = curlx_convert_tchar_to_UTF8(
  466. &cert_hostname_buff[cert_hostname_buff_index]);
  467. if(!cert_hostname) {
  468. result = CURLE_OUT_OF_MEMORY;
  469. }
  470. else {
  471. if(Curl_cert_hostcheck(cert_hostname, strlen(cert_hostname),
  472. conn_hostname, hostlen)) {
  473. infof(data,
  474. "schannel: connection hostname (%s) validated "
  475. "against certificate name (%s)",
  476. conn_hostname, cert_hostname);
  477. result = CURLE_OK;
  478. }
  479. else {
  480. size_t cert_hostname_len;
  481. infof(data,
  482. "schannel: connection hostname (%s) did not match "
  483. "against certificate name (%s)",
  484. conn_hostname, cert_hostname);
  485. cert_hostname_len =
  486. _tcslen(&cert_hostname_buff[cert_hostname_buff_index]);
  487. /* Move on to next cert name */
  488. cert_hostname_buff_index += cert_hostname_len + 1;
  489. result = CURLE_PEER_FAILED_VERIFICATION;
  490. }
  491. curlx_unicodefree(cert_hostname);
  492. }
  493. }
  494. if(result == CURLE_PEER_FAILED_VERIFICATION) {
  495. failf(data,
  496. "schannel: CertGetNameString() failed to match "
  497. "connection hostname (%s) against server certificate names",
  498. conn_hostname);
  499. }
  500. else if(result != CURLE_OK)
  501. failf(data, "schannel: server certificate name verification failed");
  502. cleanup:
  503. Curl_safefree(cert_hostname_buff);
  504. return result;
  505. }
  506. CURLcode Curl_verify_certificate(struct Curl_easy *data,
  507. struct connectdata *conn, int sockindex)
  508. {
  509. SECURITY_STATUS sspi_status;
  510. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  511. CURLcode result = CURLE_OK;
  512. CERT_CONTEXT *pCertContextServer = NULL;
  513. const CERT_CHAIN_CONTEXT *pChainContext = NULL;
  514. HCERTCHAINENGINE cert_chain_engine = NULL;
  515. HCERTSTORE trust_store = NULL;
  516. const char * const conn_hostname = SSL_HOST_NAME();
  517. DEBUGASSERT(BACKEND);
  518. sspi_status =
  519. s_pSecFn->QueryContextAttributes(&BACKEND->ctxt->ctxt_handle,
  520. SECPKG_ATTR_REMOTE_CERT_CONTEXT,
  521. &pCertContextServer);
  522. if((sspi_status != SEC_E_OK) || !pCertContextServer) {
  523. char buffer[STRERROR_LEN];
  524. failf(data, "schannel: Failed to read remote certificate context: %s",
  525. Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
  526. result = CURLE_PEER_FAILED_VERIFICATION;
  527. }
  528. if(result == CURLE_OK &&
  529. (SSL_CONN_CONFIG(CAfile) || SSL_CONN_CONFIG(ca_info_blob)) &&
  530. BACKEND->use_manual_cred_validation) {
  531. /*
  532. * Create a chain engine that uses the certificates in the CA file as
  533. * trusted certificates. This is only supported on Windows 7+.
  534. */
  535. if(curlx_verify_windows_version(6, 1, 0, PLATFORM_WINNT,
  536. VERSION_LESS_THAN)) {
  537. failf(data, "schannel: this version of Windows is too old to support "
  538. "certificate verification via CA bundle file.");
  539. result = CURLE_SSL_CACERT_BADFILE;
  540. }
  541. else {
  542. /* Open the certificate store */
  543. trust_store = CertOpenStore(CERT_STORE_PROV_MEMORY,
  544. 0,
  545. (HCRYPTPROV)NULL,
  546. CERT_STORE_CREATE_NEW_FLAG,
  547. NULL);
  548. if(!trust_store) {
  549. char buffer[STRERROR_LEN];
  550. failf(data, "schannel: failed to create certificate store: %s",
  551. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  552. result = CURLE_SSL_CACERT_BADFILE;
  553. }
  554. else {
  555. const struct curl_blob *ca_info_blob = SSL_CONN_CONFIG(ca_info_blob);
  556. if(ca_info_blob) {
  557. result = add_certs_data_to_store(trust_store,
  558. (const char *)ca_info_blob->data,
  559. ca_info_blob->len,
  560. "(memory blob)",
  561. data);
  562. }
  563. else {
  564. result = add_certs_file_to_store(trust_store,
  565. SSL_CONN_CONFIG(CAfile),
  566. data);
  567. }
  568. }
  569. }
  570. if(result == CURLE_OK) {
  571. struct cert_chain_engine_config_win7 engine_config;
  572. BOOL create_engine_result;
  573. memset(&engine_config, 0, sizeof(engine_config));
  574. engine_config.cbSize = sizeof(engine_config);
  575. engine_config.hExclusiveRoot = trust_store;
  576. /* CertCreateCertificateChainEngine will check the expected size of the
  577. * CERT_CHAIN_ENGINE_CONFIG structure and fail if the specified size
  578. * does not match the expected size. When this occurs, it indicates that
  579. * CAINFO is not supported on the version of Windows in use.
  580. */
  581. create_engine_result =
  582. CertCreateCertificateChainEngine(
  583. (CERT_CHAIN_ENGINE_CONFIG *)&engine_config, &cert_chain_engine);
  584. if(!create_engine_result) {
  585. char buffer[STRERROR_LEN];
  586. failf(data,
  587. "schannel: failed to create certificate chain engine: %s",
  588. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  589. result = CURLE_SSL_CACERT_BADFILE;
  590. }
  591. }
  592. }
  593. if(result == CURLE_OK) {
  594. CERT_CHAIN_PARA ChainPara;
  595. memset(&ChainPara, 0, sizeof(ChainPara));
  596. ChainPara.cbSize = sizeof(ChainPara);
  597. if(!CertGetCertificateChain(cert_chain_engine,
  598. pCertContextServer,
  599. NULL,
  600. pCertContextServer->hCertStore,
  601. &ChainPara,
  602. (SSL_SET_OPTION(no_revoke) ? 0 :
  603. CERT_CHAIN_REVOCATION_CHECK_CHAIN),
  604. NULL,
  605. &pChainContext)) {
  606. char buffer[STRERROR_LEN];
  607. failf(data, "schannel: CertGetCertificateChain failed: %s",
  608. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  609. pChainContext = NULL;
  610. result = CURLE_PEER_FAILED_VERIFICATION;
  611. }
  612. if(result == CURLE_OK) {
  613. CERT_SIMPLE_CHAIN *pSimpleChain = pChainContext->rgpChain[0];
  614. DWORD dwTrustErrorMask = ~(DWORD)(CERT_TRUST_IS_NOT_TIME_NESTED);
  615. dwTrustErrorMask &= pSimpleChain->TrustStatus.dwErrorStatus;
  616. if(data->set.ssl.revoke_best_effort) {
  617. /* Ignore errors when root certificates are missing the revocation
  618. * list URL, or when the list could not be downloaded because the
  619. * server is currently unreachable. */
  620. dwTrustErrorMask &= ~(DWORD)(CERT_TRUST_REVOCATION_STATUS_UNKNOWN |
  621. CERT_TRUST_IS_OFFLINE_REVOCATION);
  622. }
  623. if(dwTrustErrorMask) {
  624. if(dwTrustErrorMask & CERT_TRUST_IS_REVOKED)
  625. failf(data, "schannel: CertGetCertificateChain trust error"
  626. " CERT_TRUST_IS_REVOKED");
  627. else if(dwTrustErrorMask & CERT_TRUST_IS_PARTIAL_CHAIN)
  628. failf(data, "schannel: CertGetCertificateChain trust error"
  629. " CERT_TRUST_IS_PARTIAL_CHAIN");
  630. else if(dwTrustErrorMask & CERT_TRUST_IS_UNTRUSTED_ROOT)
  631. failf(data, "schannel: CertGetCertificateChain trust error"
  632. " CERT_TRUST_IS_UNTRUSTED_ROOT");
  633. else if(dwTrustErrorMask & CERT_TRUST_IS_NOT_TIME_VALID)
  634. failf(data, "schannel: CertGetCertificateChain trust error"
  635. " CERT_TRUST_IS_NOT_TIME_VALID");
  636. else if(dwTrustErrorMask & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
  637. failf(data, "schannel: CertGetCertificateChain trust error"
  638. " CERT_TRUST_REVOCATION_STATUS_UNKNOWN");
  639. else
  640. failf(data, "schannel: CertGetCertificateChain error mask: 0x%08x",
  641. dwTrustErrorMask);
  642. result = CURLE_PEER_FAILED_VERIFICATION;
  643. }
  644. }
  645. }
  646. if(result == CURLE_OK) {
  647. if(SSL_CONN_CONFIG(verifyhost)) {
  648. result = verify_host(data, pCertContextServer, conn_hostname);
  649. }
  650. }
  651. if(cert_chain_engine) {
  652. CertFreeCertificateChainEngine(cert_chain_engine);
  653. }
  654. if(trust_store) {
  655. CertCloseStore(trust_store, 0);
  656. }
  657. if(pChainContext)
  658. CertFreeCertificateChain(pChainContext);
  659. if(pCertContextServer)
  660. CertFreeCertificateContext(pCertContextServer);
  661. return result;
  662. }
  663. #endif /* HAS_MANUAL_VERIFY_API */
  664. #endif /* USE_SCHANNEL */