2
0

schannel_verify.c 19 KB

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