schannel_verify.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 - 2018, 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.haxx.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 "system_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. typedef struct {
  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. } CERT_CHAIN_ENGINE_CONFIG_WIN7, *PCERT_CHAIN_ENGINE_CONFIG_WIN7;
  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 connectdata *conn)
  71. {
  72. CURLcode result;
  73. struct Curl_easy *data = conn->data;
  74. HANDLE ca_file_handle = INVALID_HANDLE_VALUE;
  75. LARGE_INTEGER file_size;
  76. char *ca_file_buffer = NULL;
  77. char *current_ca_file_ptr = NULL;
  78. const TCHAR *ca_file_tstr = NULL;
  79. size_t ca_file_bufsize = 0;
  80. DWORD total_bytes_read = 0;
  81. bool more_certs = 0;
  82. int num_certs = 0;
  83. size_t END_CERT_LEN;
  84. ca_file_tstr = Curl_convert_UTF8_to_tchar(ca_file);
  85. if(!ca_file_tstr) {
  86. failf(data,
  87. "schannel: invalid path name for CA file '%s': %s",
  88. ca_file, Curl_strerror(conn, GetLastError()));
  89. result = CURLE_SSL_CACERT_BADFILE;
  90. goto cleanup;
  91. }
  92. /*
  93. * Read the CA file completely into memory before parsing it. This
  94. * optimizes for the common case where the CA file will be relatively
  95. * small ( < 1 MiB ).
  96. */
  97. ca_file_handle = CreateFile(ca_file_tstr,
  98. GENERIC_READ,
  99. 0,
  100. NULL,
  101. OPEN_EXISTING,
  102. FILE_ATTRIBUTE_NORMAL,
  103. NULL);
  104. if(ca_file_handle == INVALID_HANDLE_VALUE) {
  105. failf(data,
  106. "schannel: failed to open CA file '%s': %s",
  107. ca_file, Curl_strerror(conn, GetLastError()));
  108. result = CURLE_SSL_CACERT_BADFILE;
  109. goto cleanup;
  110. }
  111. if(!GetFileSizeEx(ca_file_handle, &file_size)) {
  112. failf(data,
  113. "schannel: failed to determine size of CA file '%s': %s",
  114. ca_file, Curl_strerror(conn, GetLastError()));
  115. result = CURLE_SSL_CACERT_BADFILE;
  116. goto cleanup;
  117. }
  118. if(file_size.QuadPart > MAX_CAFILE_SIZE) {
  119. failf(data,
  120. "schannel: CA file exceeds max size of %u bytes",
  121. MAX_CAFILE_SIZE);
  122. result = CURLE_SSL_CACERT_BADFILE;
  123. goto cleanup;
  124. }
  125. ca_file_bufsize = (size_t)file_size.QuadPart;
  126. ca_file_buffer = (char *)malloc(ca_file_bufsize + 1);
  127. if(!ca_file_buffer) {
  128. result = CURLE_OUT_OF_MEMORY;
  129. goto cleanup;
  130. }
  131. result = CURLE_OK;
  132. while(total_bytes_read < ca_file_bufsize) {
  133. DWORD bytes_to_read = (DWORD)(ca_file_bufsize - total_bytes_read);
  134. DWORD bytes_read = 0;
  135. if(!ReadFile(ca_file_handle, ca_file_buffer + total_bytes_read,
  136. bytes_to_read, &bytes_read, NULL)) {
  137. failf(data,
  138. "schannel: failed to read from CA file '%s': %s",
  139. ca_file, Curl_strerror(conn, GetLastError()));
  140. result = CURLE_SSL_CACERT_BADFILE;
  141. goto cleanup;
  142. }
  143. if(bytes_read == 0) {
  144. /* Premature EOF -- adjust the bufsize to the new value */
  145. ca_file_bufsize = total_bytes_read;
  146. }
  147. else {
  148. total_bytes_read += bytes_read;
  149. }
  150. }
  151. /* Null terminate the buffer */
  152. ca_file_buffer[ca_file_bufsize] = '\0';
  153. if(result != CURLE_OK) {
  154. goto cleanup;
  155. }
  156. END_CERT_LEN = strlen(END_CERT);
  157. more_certs = 1;
  158. current_ca_file_ptr = ca_file_buffer;
  159. while(more_certs && *current_ca_file_ptr != '\0') {
  160. char *begin_cert_ptr = strstr(current_ca_file_ptr, BEGIN_CERT);
  161. if(!begin_cert_ptr || !is_cr_or_lf(begin_cert_ptr[strlen(BEGIN_CERT)])) {
  162. more_certs = 0;
  163. }
  164. else {
  165. char *end_cert_ptr = strstr(begin_cert_ptr, END_CERT);
  166. if(!end_cert_ptr) {
  167. failf(data,
  168. "schannel: CA file '%s' is not correctly formatted",
  169. ca_file);
  170. result = CURLE_SSL_CACERT_BADFILE;
  171. more_certs = 0;
  172. }
  173. else {
  174. CERT_BLOB cert_blob;
  175. CERT_CONTEXT *cert_context = NULL;
  176. BOOL add_cert_result = FALSE;
  177. DWORD actual_content_type = 0;
  178. DWORD cert_size = (DWORD)
  179. ((end_cert_ptr + END_CERT_LEN) - begin_cert_ptr);
  180. cert_blob.pbData = (BYTE *)begin_cert_ptr;
  181. cert_blob.cbData = cert_size;
  182. if(!CryptQueryObject(CERT_QUERY_OBJECT_BLOB,
  183. &cert_blob,
  184. CERT_QUERY_CONTENT_FLAG_CERT,
  185. CERT_QUERY_FORMAT_FLAG_ALL,
  186. 0,
  187. NULL,
  188. &actual_content_type,
  189. NULL,
  190. NULL,
  191. NULL,
  192. (const void **)&cert_context)) {
  193. failf(data,
  194. "schannel: failed to extract certificate from CA file "
  195. "'%s': %s",
  196. ca_file, Curl_strerror(conn, GetLastError()));
  197. result = CURLE_SSL_CACERT_BADFILE;
  198. more_certs = 0;
  199. }
  200. else {
  201. current_ca_file_ptr = begin_cert_ptr + cert_size;
  202. /* Sanity check that the cert_context object is the right type */
  203. if(CERT_QUERY_CONTENT_CERT != actual_content_type) {
  204. failf(data,
  205. "schannel: unexpected content type '%d' when extracting "
  206. "certificate from CA file '%s'",
  207. actual_content_type, ca_file);
  208. result = CURLE_SSL_CACERT_BADFILE;
  209. more_certs = 0;
  210. }
  211. else {
  212. add_cert_result =
  213. CertAddCertificateContextToStore(trust_store,
  214. cert_context,
  215. CERT_STORE_ADD_ALWAYS,
  216. NULL);
  217. CertFreeCertificateContext(cert_context);
  218. if(!add_cert_result) {
  219. failf(data,
  220. "schannel: failed to add certificate from CA file '%s' "
  221. "to certificate store: %s",
  222. ca_file, Curl_strerror(conn, GetLastError()));
  223. result = CURLE_SSL_CACERT_BADFILE;
  224. more_certs = 0;
  225. }
  226. else {
  227. num_certs++;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. if(result == CURLE_OK) {
  235. if(!num_certs) {
  236. infof(data,
  237. "schannel: did not add any certificates from CA file '%s'\n",
  238. ca_file);
  239. }
  240. else {
  241. infof(data,
  242. "schannel: added %d certificate(s) from CA file '%s'\n",
  243. num_certs, ca_file);
  244. }
  245. }
  246. cleanup:
  247. if(ca_file_handle != INVALID_HANDLE_VALUE) {
  248. CloseHandle(ca_file_handle);
  249. }
  250. Curl_safefree(ca_file_buffer);
  251. Curl_unicodefree(ca_file_tstr);
  252. return result;
  253. }
  254. static CURLcode verify_host(struct Curl_easy *data,
  255. CERT_CONTEXT *pCertContextServer,
  256. const char * const conn_hostname)
  257. {
  258. CURLcode result = CURLE_PEER_FAILED_VERIFICATION;
  259. TCHAR *cert_hostname_buff = NULL;
  260. size_t cert_hostname_buff_index = 0;
  261. DWORD len = 0;
  262. DWORD actual_len = 0;
  263. /* CertGetNameString will provide the 8-bit character string without
  264. * any decoding */
  265. DWORD name_flags = CERT_NAME_DISABLE_IE4_UTF8_FLAG;
  266. #ifdef CERT_NAME_SEARCH_ALL_NAMES_FLAG
  267. name_flags |= CERT_NAME_SEARCH_ALL_NAMES_FLAG;
  268. #endif
  269. /* Determine the size of the string needed for the cert hostname */
  270. len = CertGetNameString(pCertContextServer,
  271. CERT_NAME_DNS_TYPE,
  272. name_flags,
  273. NULL,
  274. NULL,
  275. 0);
  276. if(len == 0) {
  277. failf(data,
  278. "schannel: CertGetNameString() returned no "
  279. "certificate name information");
  280. result = CURLE_PEER_FAILED_VERIFICATION;
  281. goto cleanup;
  282. }
  283. /* CertGetNameString guarantees that the returned name will not contain
  284. * embedded null bytes. This appears to be undocumented behavior.
  285. */
  286. cert_hostname_buff = (LPTSTR)malloc(len * sizeof(TCHAR));
  287. if(!cert_hostname_buff) {
  288. result = CURLE_OUT_OF_MEMORY;
  289. goto cleanup;
  290. }
  291. actual_len = CertGetNameString(pCertContextServer,
  292. CERT_NAME_DNS_TYPE,
  293. name_flags,
  294. NULL,
  295. (LPTSTR) cert_hostname_buff,
  296. len);
  297. /* Sanity check */
  298. if(actual_len != len) {
  299. failf(data,
  300. "schannel: CertGetNameString() returned certificate "
  301. "name information of unexpected size");
  302. result = CURLE_PEER_FAILED_VERIFICATION;
  303. goto cleanup;
  304. }
  305. /* If HAVE_CERT_NAME_SEARCH_ALL_NAMES is available, the output
  306. * will contain all DNS names, where each name is null-terminated
  307. * and the last DNS name is double null-terminated. Due to this
  308. * encoding, use the length of the buffer to iterate over all names.
  309. */
  310. result = CURLE_PEER_FAILED_VERIFICATION;
  311. while(cert_hostname_buff_index < len &&
  312. cert_hostname_buff[cert_hostname_buff_index] != TEXT('\0') &&
  313. result == CURLE_PEER_FAILED_VERIFICATION) {
  314. char *cert_hostname;
  315. /* Comparing the cert name and the connection hostname encoded as UTF-8
  316. * is acceptable since both values are assumed to use ASCII
  317. * (or some equivalent) encoding
  318. */
  319. cert_hostname = Curl_convert_tchar_to_UTF8(
  320. &cert_hostname_buff[cert_hostname_buff_index]);
  321. if(!cert_hostname) {
  322. result = CURLE_OUT_OF_MEMORY;
  323. }
  324. else {
  325. int match_result;
  326. match_result = Curl_cert_hostcheck(cert_hostname, conn_hostname);
  327. if(match_result == CURL_HOST_MATCH) {
  328. infof(data,
  329. "schannel: connection hostname (%s) validated "
  330. "against certificate name (%s)\n",
  331. conn_hostname, cert_hostname);
  332. result = CURLE_OK;
  333. }
  334. else {
  335. size_t cert_hostname_len;
  336. infof(data,
  337. "schannel: connection hostname (%s) did not match "
  338. "against certificate name (%s)\n",
  339. conn_hostname, cert_hostname);
  340. cert_hostname_len = _tcslen(
  341. &cert_hostname_buff[cert_hostname_buff_index]);
  342. /* Move on to next cert name */
  343. cert_hostname_buff_index += cert_hostname_len + 1;
  344. result = CURLE_PEER_FAILED_VERIFICATION;
  345. }
  346. Curl_unicodefree(cert_hostname);
  347. }
  348. }
  349. if(result == CURLE_PEER_FAILED_VERIFICATION) {
  350. failf(data,
  351. "schannel: CertGetNameString() failed to match "
  352. "connection hostname (%s) against server certificate names",
  353. conn_hostname);
  354. }
  355. else if(result != CURLE_OK)
  356. failf(data, "schannel: server certificate name verification failed");
  357. cleanup:
  358. Curl_unicodefree(cert_hostname_buff);
  359. return result;
  360. }
  361. CURLcode verify_certificate(struct connectdata *conn, int sockindex)
  362. {
  363. SECURITY_STATUS status;
  364. struct Curl_easy *data = conn->data;
  365. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  366. CURLcode result = CURLE_OK;
  367. CERT_CONTEXT *pCertContextServer = NULL;
  368. const CERT_CHAIN_CONTEXT *pChainContext = NULL;
  369. HCERTCHAINENGINE cert_chain_engine = NULL;
  370. HCERTSTORE trust_store = NULL;
  371. const char * const conn_hostname = SSL_IS_PROXY() ?
  372. conn->http_proxy.host.name :
  373. conn->host.name;
  374. status = s_pSecFn->QueryContextAttributes(&BACKEND->ctxt->ctxt_handle,
  375. SECPKG_ATTR_REMOTE_CERT_CONTEXT,
  376. &pCertContextServer);
  377. if((status != SEC_E_OK) || (pCertContextServer == NULL)) {
  378. failf(data, "schannel: Failed to read remote certificate context: %s",
  379. Curl_sspi_strerror(conn, status));
  380. result = CURLE_PEER_FAILED_VERIFICATION;
  381. }
  382. if(result == CURLE_OK && SSL_CONN_CONFIG(CAfile) &&
  383. BACKEND->use_manual_cred_validation) {
  384. /*
  385. * Create a chain engine that uses the certificates in the CA file as
  386. * trusted certificates. This is only supported on Windows 7+.
  387. */
  388. if(Curl_verify_windows_version(6, 1, PLATFORM_WINNT, VERSION_LESS_THAN)) {
  389. failf(data, "schannel: this version of Windows is too old to support "
  390. "certificate verification via CA bundle file.");
  391. result = CURLE_SSL_CACERT_BADFILE;
  392. }
  393. else {
  394. /* Open the certificate store */
  395. trust_store = CertOpenStore(CERT_STORE_PROV_MEMORY,
  396. 0,
  397. (HCRYPTPROV)NULL,
  398. CERT_STORE_CREATE_NEW_FLAG,
  399. NULL);
  400. if(!trust_store) {
  401. failf(data, "schannel: failed to create certificate store: %s",
  402. Curl_strerror(conn, GetLastError()));
  403. result = CURLE_SSL_CACERT_BADFILE;
  404. }
  405. else {
  406. result = add_certs_to_store(trust_store, SSL_CONN_CONFIG(CAfile),
  407. conn);
  408. }
  409. }
  410. if(result == CURLE_OK) {
  411. CERT_CHAIN_ENGINE_CONFIG_WIN7 engine_config;
  412. BOOL create_engine_result;
  413. memset(&engine_config, 0, sizeof(engine_config));
  414. engine_config.cbSize = sizeof(engine_config);
  415. engine_config.hExclusiveRoot = trust_store;
  416. /* CertCreateCertificateChainEngine will check the expected size of the
  417. * CERT_CHAIN_ENGINE_CONFIG structure and fail if the specified size
  418. * does not match the expected size. When this occurs, it indicates that
  419. * CAINFO is not supported on the version of Windows in use.
  420. */
  421. create_engine_result =
  422. CertCreateCertificateChainEngine(
  423. (CERT_CHAIN_ENGINE_CONFIG *)&engine_config, &cert_chain_engine);
  424. if(!create_engine_result) {
  425. failf(data,
  426. "schannel: failed to create certificate chain engine: %s",
  427. Curl_strerror(conn, GetLastError()));
  428. result = CURLE_SSL_CACERT_BADFILE;
  429. }
  430. }
  431. }
  432. if(result == CURLE_OK) {
  433. CERT_CHAIN_PARA ChainPara;
  434. memset(&ChainPara, 0, sizeof(ChainPara));
  435. ChainPara.cbSize = sizeof(ChainPara);
  436. if(!CertGetCertificateChain(cert_chain_engine,
  437. pCertContextServer,
  438. NULL,
  439. pCertContextServer->hCertStore,
  440. &ChainPara,
  441. (data->set.ssl.no_revoke ? 0 :
  442. CERT_CHAIN_REVOCATION_CHECK_CHAIN),
  443. NULL,
  444. &pChainContext)) {
  445. failf(data, "schannel: CertGetCertificateChain failed: %s",
  446. Curl_sspi_strerror(conn, GetLastError()));
  447. pChainContext = NULL;
  448. result = CURLE_PEER_FAILED_VERIFICATION;
  449. }
  450. if(result == CURLE_OK) {
  451. CERT_SIMPLE_CHAIN *pSimpleChain = pChainContext->rgpChain[0];
  452. DWORD dwTrustErrorMask = ~(DWORD)(CERT_TRUST_IS_NOT_TIME_NESTED);
  453. dwTrustErrorMask &= pSimpleChain->TrustStatus.dwErrorStatus;
  454. if(dwTrustErrorMask) {
  455. if(dwTrustErrorMask & CERT_TRUST_IS_REVOKED)
  456. failf(data, "schannel: CertGetCertificateChain trust error"
  457. " CERT_TRUST_IS_REVOKED");
  458. else if(dwTrustErrorMask & CERT_TRUST_IS_PARTIAL_CHAIN)
  459. failf(data, "schannel: CertGetCertificateChain trust error"
  460. " CERT_TRUST_IS_PARTIAL_CHAIN");
  461. else if(dwTrustErrorMask & CERT_TRUST_IS_UNTRUSTED_ROOT)
  462. failf(data, "schannel: CertGetCertificateChain trust error"
  463. " CERT_TRUST_IS_UNTRUSTED_ROOT");
  464. else if(dwTrustErrorMask & CERT_TRUST_IS_NOT_TIME_VALID)
  465. failf(data, "schannel: CertGetCertificateChain trust error"
  466. " CERT_TRUST_IS_NOT_TIME_VALID");
  467. else if(dwTrustErrorMask & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
  468. failf(data, "schannel: CertGetCertificateChain trust error"
  469. " CERT_TRUST_REVOCATION_STATUS_UNKNOWN");
  470. else
  471. failf(data, "schannel: CertGetCertificateChain error mask: 0x%08x",
  472. dwTrustErrorMask);
  473. result = CURLE_PEER_FAILED_VERIFICATION;
  474. }
  475. }
  476. }
  477. if(result == CURLE_OK) {
  478. if(SSL_CONN_CONFIG(verifyhost)) {
  479. result = verify_host(conn->data, pCertContextServer, conn_hostname);
  480. }
  481. }
  482. if(cert_chain_engine) {
  483. CertFreeCertificateChainEngine(cert_chain_engine);
  484. }
  485. if(trust_store) {
  486. CertCloseStore(trust_store, 0);
  487. }
  488. if(pChainContext)
  489. CertFreeCertificateChain(pChainContext);
  490. if(pCertContextServer)
  491. CertFreeCertificateContext(pCertContextServer);
  492. return result;
  493. }
  494. #endif /* HAS_MANUAL_VERIFY_API */
  495. #endif /* USE_SCHANNEL */