schannel_verify.c 18 KB

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