curl_schannel.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 2014, Marc Hoersken, <info@marc-hoersken.de>
  9. * Copyright (C) 2012, Mark Salisbury, <mark.salisbury@hp.com>
  10. * Copyright (C) 2012 - 2014, 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 http://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 all SChannel-specific code for the TLS/SSL layer. No code
  26. * but vtls.c should ever call or use these functions.
  27. *
  28. */
  29. /*
  30. * Based upon the PolarSSL implementation in polarssl.c and polarssl.h:
  31. * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
  32. *
  33. * Based upon the CyaSSL implementation in cyassl.c and cyassl.h:
  34. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  35. *
  36. * Thanks for code and inspiration!
  37. */
  38. /*
  39. * TODO list for TLS/SSL implementation:
  40. * - implement client certificate authentication
  41. * - implement custom server certificate validation
  42. * - implement cipher/algorithm option
  43. *
  44. * Related articles on MSDN:
  45. * - Getting a Certificate for Schannel
  46. * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375447.aspx
  47. * - Specifying Schannel Ciphers and Cipher Strengths
  48. * http://msdn.microsoft.com/en-us/library/windows/desktop/aa380161.aspx
  49. */
  50. #include "curl_setup.h"
  51. #ifdef USE_SCHANNEL
  52. #ifndef USE_WINDOWS_SSPI
  53. # error "Can't compile SCHANNEL support without SSPI."
  54. #endif
  55. #include "curl_sspi.h"
  56. #include "curl_schannel.h"
  57. #include "vtls.h"
  58. #include "sendf.h"
  59. #include "connect.h" /* for the connect timeout */
  60. #include "strerror.h"
  61. #include "select.h" /* for the socket readyness */
  62. #include "inet_pton.h" /* for IP addr SNI check */
  63. #include "curl_multibyte.h"
  64. #include "warnless.h"
  65. #define _MPRINTF_REPLACE /* use our functions only */
  66. #include <curl/mprintf.h>
  67. #include "curl_memory.h"
  68. /* The last #include file should be: */
  69. #include "memdebug.h"
  70. /* Uncomment to force verbose output
  71. * #define infof(x, y, ...) printf(y, __VA_ARGS__)
  72. * #define failf(x, y, ...) printf(y, __VA_ARGS__)
  73. */
  74. static Curl_recv schannel_recv;
  75. static Curl_send schannel_send;
  76. #ifdef _WIN32_WCE
  77. static CURLcode verify_certificate(struct connectdata *conn, int sockindex);
  78. #endif
  79. static void InitSecBuffer(SecBuffer *buffer, unsigned long BufType,
  80. void *BufDataPtr, unsigned long BufByteSize)
  81. {
  82. buffer->cbBuffer = BufByteSize;
  83. buffer->BufferType = BufType;
  84. buffer->pvBuffer = BufDataPtr;
  85. }
  86. static void InitSecBufferDesc(SecBufferDesc *desc, SecBuffer *BufArr,
  87. unsigned long NumArrElem)
  88. {
  89. desc->ulVersion = SECBUFFER_VERSION;
  90. desc->pBuffers = BufArr;
  91. desc->cBuffers = NumArrElem;
  92. }
  93. static CURLcode
  94. schannel_connect_step1(struct connectdata *conn, int sockindex)
  95. {
  96. ssize_t written = -1;
  97. struct SessionHandle *data = conn->data;
  98. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  99. SecBuffer outbuf;
  100. SecBufferDesc outbuf_desc;
  101. SCHANNEL_CRED schannel_cred;
  102. SECURITY_STATUS sspi_status = SEC_E_OK;
  103. struct curl_schannel_cred *old_cred = NULL;
  104. struct in_addr addr;
  105. #ifdef ENABLE_IPV6
  106. struct in6_addr addr6;
  107. #endif
  108. TCHAR *host_name;
  109. CURLcode code;
  110. infof(data, "schannel: SSL/TLS connection with %s port %hu (step 1/3)\n",
  111. conn->host.name, conn->remote_port);
  112. /* check for an existing re-usable credential handle */
  113. if(!Curl_ssl_getsessionid(conn, (void**)&old_cred, NULL)) {
  114. connssl->cred = old_cred;
  115. infof(data, "schannel: re-using existing credential handle\n");
  116. }
  117. else {
  118. /* setup Schannel API options */
  119. memset(&schannel_cred, 0, sizeof(schannel_cred));
  120. schannel_cred.dwVersion = SCHANNEL_CRED_VERSION;
  121. if(data->set.ssl.verifypeer) {
  122. #ifdef _WIN32_WCE
  123. /* certificate validation on CE doesn't seem to work right; we'll
  124. do it following a more manual process. */
  125. schannel_cred.dwFlags = SCH_CRED_MANUAL_CRED_VALIDATION |
  126. SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
  127. SCH_CRED_IGNORE_REVOCATION_OFFLINE;
  128. #else
  129. schannel_cred.dwFlags = SCH_CRED_AUTO_CRED_VALIDATION |
  130. SCH_CRED_REVOCATION_CHECK_CHAIN;
  131. #endif
  132. infof(data, "schannel: checking server certificate revocation\n");
  133. }
  134. else {
  135. schannel_cred.dwFlags = SCH_CRED_MANUAL_CRED_VALIDATION |
  136. SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
  137. SCH_CRED_IGNORE_REVOCATION_OFFLINE;
  138. infof(data, "schannel: disable server certificate revocation checks\n");
  139. }
  140. if(!data->set.ssl.verifyhost) {
  141. schannel_cred.dwFlags |= SCH_CRED_NO_SERVERNAME_CHECK;
  142. infof(data, "schannel: verifyhost setting prevents Schannel from "
  143. "comparing the supplied target name with the subject "
  144. "names in server certificates. Also disables SNI.\n");
  145. }
  146. switch(data->set.ssl.version) {
  147. default:
  148. case CURL_SSLVERSION_DEFAULT:
  149. case CURL_SSLVERSION_TLSv1:
  150. schannel_cred.grbitEnabledProtocols = SP_PROT_TLS1_0_CLIENT |
  151. SP_PROT_TLS1_1_CLIENT |
  152. SP_PROT_TLS1_2_CLIENT;
  153. break;
  154. case CURL_SSLVERSION_TLSv1_0:
  155. schannel_cred.grbitEnabledProtocols = SP_PROT_TLS1_0_CLIENT;
  156. break;
  157. case CURL_SSLVERSION_TLSv1_1:
  158. schannel_cred.grbitEnabledProtocols = SP_PROT_TLS1_1_CLIENT;
  159. break;
  160. case CURL_SSLVERSION_TLSv1_2:
  161. schannel_cred.grbitEnabledProtocols = SP_PROT_TLS1_2_CLIENT;
  162. break;
  163. case CURL_SSLVERSION_SSLv3:
  164. schannel_cred.grbitEnabledProtocols = SP_PROT_SSL3_CLIENT;
  165. break;
  166. case CURL_SSLVERSION_SSLv2:
  167. schannel_cred.grbitEnabledProtocols = SP_PROT_SSL2_CLIENT;
  168. break;
  169. }
  170. /* allocate memory for the re-usable credential handle */
  171. connssl->cred = (struct curl_schannel_cred *)
  172. malloc(sizeof(struct curl_schannel_cred));
  173. if(!connssl->cred) {
  174. failf(data, "schannel: unable to allocate memory");
  175. return CURLE_OUT_OF_MEMORY;
  176. }
  177. memset(connssl->cred, 0, sizeof(struct curl_schannel_cred));
  178. /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa374716.aspx */
  179. sspi_status = s_pSecFn->AcquireCredentialsHandle(NULL, (TCHAR *)UNISP_NAME,
  180. SECPKG_CRED_OUTBOUND, NULL, &schannel_cred, NULL, NULL,
  181. &connssl->cred->cred_handle, &connssl->cred->time_stamp);
  182. if(sspi_status != SEC_E_OK) {
  183. if(sspi_status == SEC_E_WRONG_PRINCIPAL)
  184. failf(data, "schannel: SNI or certificate check failed: %s",
  185. Curl_sspi_strerror(conn, sspi_status));
  186. else
  187. failf(data, "schannel: AcquireCredentialsHandle failed: %s",
  188. Curl_sspi_strerror(conn, sspi_status));
  189. Curl_safefree(connssl->cred);
  190. return CURLE_SSL_CONNECT_ERROR;
  191. }
  192. }
  193. /* Warn if SNI is disabled due to use of an IP address */
  194. if(Curl_inet_pton(AF_INET, conn->host.name, &addr)
  195. #ifdef ENABLE_IPV6
  196. || Curl_inet_pton(AF_INET6, conn->host.name, &addr6)
  197. #endif
  198. ) {
  199. infof(data, "schannel: using IP address, SNI is not supported by OS.\n");
  200. }
  201. /* setup output buffer */
  202. InitSecBuffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
  203. InitSecBufferDesc(&outbuf_desc, &outbuf, 1);
  204. /* setup request flags */
  205. connssl->req_flags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT |
  206. ISC_REQ_CONFIDENTIALITY | ISC_REQ_ALLOCATE_MEMORY |
  207. ISC_REQ_STREAM;
  208. /* allocate memory for the security context handle */
  209. connssl->ctxt = (struct curl_schannel_ctxt *)
  210. malloc(sizeof(struct curl_schannel_ctxt));
  211. if(!connssl->ctxt) {
  212. failf(data, "schannel: unable to allocate memory");
  213. return CURLE_OUT_OF_MEMORY;
  214. }
  215. memset(connssl->ctxt, 0, sizeof(struct curl_schannel_ctxt));
  216. host_name = Curl_convert_UTF8_to_tchar(conn->host.name);
  217. if(!host_name)
  218. return CURLE_OUT_OF_MEMORY;
  219. /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375924.aspx */
  220. sspi_status = s_pSecFn->InitializeSecurityContext(
  221. &connssl->cred->cred_handle, NULL, host_name,
  222. connssl->req_flags, 0, 0, NULL, 0, &connssl->ctxt->ctxt_handle,
  223. &outbuf_desc, &connssl->ret_flags, &connssl->ctxt->time_stamp);
  224. Curl_unicodefree(host_name);
  225. if(sspi_status != SEC_I_CONTINUE_NEEDED) {
  226. if(sspi_status == SEC_E_WRONG_PRINCIPAL)
  227. failf(data, "schannel: SNI or certificate check failed: %s",
  228. Curl_sspi_strerror(conn, sspi_status));
  229. else
  230. failf(data, "schannel: initial InitializeSecurityContext failed: %s",
  231. Curl_sspi_strerror(conn, sspi_status));
  232. Curl_safefree(connssl->ctxt);
  233. return CURLE_SSL_CONNECT_ERROR;
  234. }
  235. infof(data, "schannel: sending initial handshake data: "
  236. "sending %lu bytes...\n", outbuf.cbBuffer);
  237. /* send initial handshake data which is now stored in output buffer */
  238. code = Curl_write_plain(conn, conn->sock[sockindex], outbuf.pvBuffer,
  239. outbuf.cbBuffer, &written);
  240. s_pSecFn->FreeContextBuffer(outbuf.pvBuffer);
  241. if((code != CURLE_OK) || (outbuf.cbBuffer != (size_t)written)) {
  242. failf(data, "schannel: failed to send initial handshake data: "
  243. "sent %zd of %lu bytes", written, outbuf.cbBuffer);
  244. return CURLE_SSL_CONNECT_ERROR;
  245. }
  246. infof(data, "schannel: sent initial handshake data: "
  247. "sent %zd bytes\n", written);
  248. /* continue to second handshake step */
  249. connssl->connecting_state = ssl_connect_2;
  250. return CURLE_OK;
  251. }
  252. static CURLcode
  253. schannel_connect_step2(struct connectdata *conn, int sockindex)
  254. {
  255. int i;
  256. ssize_t nread = -1, written = -1;
  257. struct SessionHandle *data = conn->data;
  258. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  259. SecBuffer outbuf[2];
  260. SecBufferDesc outbuf_desc;
  261. SecBuffer inbuf[2];
  262. SecBufferDesc inbuf_desc;
  263. SECURITY_STATUS sspi_status = SEC_E_OK;
  264. TCHAR *host_name;
  265. CURLcode code;
  266. bool doread;
  267. doread = (connssl->connecting_state != ssl_connect_2_writing) ? TRUE : FALSE;
  268. infof(data, "schannel: SSL/TLS connection with %s port %hu (step 2/3)\n",
  269. conn->host.name, conn->remote_port);
  270. if(!connssl->cred || !connssl->ctxt)
  271. return CURLE_SSL_CONNECT_ERROR;
  272. /* buffer to store previously received and encrypted data */
  273. if(connssl->encdata_buffer == NULL) {
  274. connssl->encdata_offset = 0;
  275. connssl->encdata_length = CURL_SCHANNEL_BUFFER_INIT_SIZE;
  276. connssl->encdata_buffer = malloc(connssl->encdata_length);
  277. if(connssl->encdata_buffer == NULL) {
  278. failf(data, "schannel: unable to allocate memory");
  279. return CURLE_OUT_OF_MEMORY;
  280. }
  281. }
  282. /* if we need a bigger buffer to read a full message, increase buffer now */
  283. if(connssl->encdata_length - connssl->encdata_offset <
  284. CURL_SCHANNEL_BUFFER_FREE_SIZE) {
  285. /* increase internal encrypted data buffer */
  286. connssl->encdata_length *= CURL_SCHANNEL_BUFFER_STEP_FACTOR;
  287. connssl->encdata_buffer = realloc(connssl->encdata_buffer,
  288. connssl->encdata_length);
  289. if(connssl->encdata_buffer == NULL) {
  290. failf(data, "schannel: unable to re-allocate memory");
  291. return CURLE_OUT_OF_MEMORY;
  292. }
  293. }
  294. for(;;) {
  295. if(doread) {
  296. /* read encrypted handshake data from socket */
  297. code = Curl_read_plain(conn->sock[sockindex],
  298. (char *) (connssl->encdata_buffer + connssl->encdata_offset),
  299. connssl->encdata_length - connssl->encdata_offset,
  300. &nread);
  301. if(code == CURLE_AGAIN) {
  302. if(connssl->connecting_state != ssl_connect_2_writing)
  303. connssl->connecting_state = ssl_connect_2_reading;
  304. infof(data, "schannel: failed to receive handshake, "
  305. "need more data\n");
  306. return CURLE_OK;
  307. }
  308. else if((code != CURLE_OK) || (nread == 0)) {
  309. failf(data, "schannel: failed to receive handshake, "
  310. "SSL/TLS connection failed");
  311. return CURLE_SSL_CONNECT_ERROR;
  312. }
  313. /* increase encrypted data buffer offset */
  314. connssl->encdata_offset += nread;
  315. }
  316. infof(data, "schannel: encrypted data buffer: offset %zu length %zu\n",
  317. connssl->encdata_offset, connssl->encdata_length);
  318. /* setup input buffers */
  319. InitSecBuffer(&inbuf[0], SECBUFFER_TOKEN, malloc(connssl->encdata_offset),
  320. curlx_uztoul(connssl->encdata_offset));
  321. InitSecBuffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
  322. InitSecBufferDesc(&inbuf_desc, inbuf, 2);
  323. /* setup output buffers */
  324. InitSecBuffer(&outbuf[0], SECBUFFER_TOKEN, NULL, 0);
  325. InitSecBuffer(&outbuf[1], SECBUFFER_ALERT, NULL, 0);
  326. InitSecBufferDesc(&outbuf_desc, outbuf, 2);
  327. if(inbuf[0].pvBuffer == NULL) {
  328. failf(data, "schannel: unable to allocate memory");
  329. return CURLE_OUT_OF_MEMORY;
  330. }
  331. /* copy received handshake data into input buffer */
  332. memcpy(inbuf[0].pvBuffer, connssl->encdata_buffer,
  333. connssl->encdata_offset);
  334. host_name = Curl_convert_UTF8_to_tchar(conn->host.name);
  335. if(!host_name)
  336. return CURLE_OUT_OF_MEMORY;
  337. /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375924.aspx */
  338. sspi_status = s_pSecFn->InitializeSecurityContext(
  339. &connssl->cred->cred_handle, &connssl->ctxt->ctxt_handle,
  340. host_name, connssl->req_flags, 0, 0, &inbuf_desc, 0, NULL,
  341. &outbuf_desc, &connssl->ret_flags, &connssl->ctxt->time_stamp);
  342. Curl_unicodefree(host_name);
  343. /* free buffer for received handshake data */
  344. Curl_safefree(inbuf[0].pvBuffer);
  345. /* check if the handshake was incomplete */
  346. if(sspi_status == SEC_E_INCOMPLETE_MESSAGE) {
  347. connssl->connecting_state = ssl_connect_2_reading;
  348. infof(data, "schannel: received incomplete message, need more data\n");
  349. return CURLE_OK;
  350. }
  351. /* check if the handshake needs to be continued */
  352. if(sspi_status == SEC_I_CONTINUE_NEEDED || sspi_status == SEC_E_OK) {
  353. for(i = 0; i < 2; i++) {
  354. /* search for handshake tokens that need to be send */
  355. if(outbuf[i].BufferType == SECBUFFER_TOKEN && outbuf[i].cbBuffer > 0) {
  356. infof(data, "schannel: sending next handshake data: "
  357. "sending %lu bytes...\n", outbuf[i].cbBuffer);
  358. /* send handshake token to server */
  359. code = Curl_write_plain(conn, conn->sock[sockindex],
  360. outbuf[i].pvBuffer, outbuf[i].cbBuffer,
  361. &written);
  362. if((code != CURLE_OK) || (outbuf[i].cbBuffer != (size_t)written)) {
  363. failf(data, "schannel: failed to send next handshake data: "
  364. "sent %zd of %lu bytes", written, outbuf[i].cbBuffer);
  365. return CURLE_SSL_CONNECT_ERROR;
  366. }
  367. }
  368. /* free obsolete buffer */
  369. if(outbuf[i].pvBuffer != NULL) {
  370. s_pSecFn->FreeContextBuffer(outbuf[i].pvBuffer);
  371. }
  372. }
  373. }
  374. else {
  375. if(sspi_status == SEC_E_WRONG_PRINCIPAL)
  376. failf(data, "schannel: SNI or certificate check failed: %s",
  377. Curl_sspi_strerror(conn, sspi_status));
  378. else
  379. failf(data, "schannel: next InitializeSecurityContext failed: %s",
  380. Curl_sspi_strerror(conn, sspi_status));
  381. return CURLE_SSL_CONNECT_ERROR;
  382. }
  383. /* check if there was additional remaining encrypted data */
  384. if(inbuf[1].BufferType == SECBUFFER_EXTRA && inbuf[1].cbBuffer > 0) {
  385. infof(data, "schannel: encrypted data length: %lu\n", inbuf[1].cbBuffer);
  386. /*
  387. There are two cases where we could be getting extra data here:
  388. 1) If we're renegotiating a connection and the handshake is already
  389. complete (from the server perspective), it can encrypted app data
  390. (not handshake data) in an extra buffer at this point.
  391. 2) (sspi_status == SEC_I_CONTINUE_NEEDED) We are negotiating a
  392. connection and this extra data is part of the handshake.
  393. We should process the data immediately; waiting for the socket to
  394. be ready may fail since the server is done sending handshake data.
  395. */
  396. /* check if the remaining data is less than the total amount
  397. and therefore begins after the already processed data */
  398. if(connssl->encdata_offset > inbuf[1].cbBuffer) {
  399. memmove(connssl->encdata_buffer,
  400. (connssl->encdata_buffer + connssl->encdata_offset) -
  401. inbuf[1].cbBuffer, inbuf[1].cbBuffer);
  402. connssl->encdata_offset = inbuf[1].cbBuffer;
  403. if(sspi_status == SEC_I_CONTINUE_NEEDED) {
  404. doread = FALSE;
  405. continue;
  406. }
  407. }
  408. }
  409. else {
  410. connssl->encdata_offset = 0;
  411. }
  412. break;
  413. }
  414. /* check if the handshake needs to be continued */
  415. if(sspi_status == SEC_I_CONTINUE_NEEDED) {
  416. connssl->connecting_state = ssl_connect_2_reading;
  417. return CURLE_OK;
  418. }
  419. /* check if the handshake is complete */
  420. if(sspi_status == SEC_E_OK) {
  421. connssl->connecting_state = ssl_connect_3;
  422. infof(data, "schannel: SSL/TLS handshake complete\n");
  423. }
  424. #ifdef _WIN32_WCE
  425. /* Windows CE doesn't do any server certificate validation.
  426. We have to do it manually. */
  427. if(data->set.ssl.verifypeer)
  428. return verify_certificate(conn, sockindex);
  429. #endif
  430. return CURLE_OK;
  431. }
  432. static CURLcode
  433. schannel_connect_step3(struct connectdata *conn, int sockindex)
  434. {
  435. CURLcode retcode = CURLE_OK;
  436. struct SessionHandle *data = conn->data;
  437. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  438. struct curl_schannel_cred *old_cred = NULL;
  439. int incache;
  440. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  441. infof(data, "schannel: SSL/TLS connection with %s port %hu (step 3/3)\n",
  442. conn->host.name, conn->remote_port);
  443. if(!connssl->cred)
  444. return CURLE_SSL_CONNECT_ERROR;
  445. /* check if the required context attributes are met */
  446. if(connssl->ret_flags != connssl->req_flags) {
  447. if(!(connssl->ret_flags & ISC_RET_SEQUENCE_DETECT))
  448. failf(data, "schannel: failed to setup sequence detection");
  449. if(!(connssl->ret_flags & ISC_RET_REPLAY_DETECT))
  450. failf(data, "schannel: failed to setup replay detection");
  451. if(!(connssl->ret_flags & ISC_RET_CONFIDENTIALITY))
  452. failf(data, "schannel: failed to setup confidentiality");
  453. if(!(connssl->ret_flags & ISC_RET_ALLOCATED_MEMORY))
  454. failf(data, "schannel: failed to setup memory allocation");
  455. if(!(connssl->ret_flags & ISC_RET_STREAM))
  456. failf(data, "schannel: failed to setup stream orientation");
  457. return CURLE_SSL_CONNECT_ERROR;
  458. }
  459. /* increment the reference counter of the credential/session handle */
  460. if(connssl->cred && connssl->ctxt) {
  461. connssl->cred->refcount++;
  462. infof(data, "schannel: incremented credential handle refcount = %d\n",
  463. connssl->cred->refcount);
  464. }
  465. /* save the current session data for possible re-use */
  466. incache = !(Curl_ssl_getsessionid(conn, (void**)&old_cred, NULL));
  467. if(incache) {
  468. if(old_cred != connssl->cred) {
  469. infof(data, "schannel: old credential handle is stale, removing\n");
  470. Curl_ssl_delsessionid(conn, (void*)old_cred);
  471. incache = FALSE;
  472. }
  473. }
  474. if(!incache) {
  475. retcode = Curl_ssl_addsessionid(conn, (void*)connssl->cred,
  476. sizeof(struct curl_schannel_cred));
  477. if(retcode) {
  478. failf(data, "schannel: failed to store credential handle");
  479. return retcode;
  480. }
  481. else {
  482. connssl->cred->cached = TRUE;
  483. infof(data, "schannel: stored credential handle in session cache\n");
  484. }
  485. }
  486. connssl->connecting_state = ssl_connect_done;
  487. return CURLE_OK;
  488. }
  489. static CURLcode
  490. schannel_connect_common(struct connectdata *conn, int sockindex,
  491. bool nonblocking, bool *done)
  492. {
  493. CURLcode retcode;
  494. struct SessionHandle *data = conn->data;
  495. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  496. curl_socket_t sockfd = conn->sock[sockindex];
  497. long timeout_ms;
  498. int what;
  499. /* check if the connection has already been established */
  500. if(ssl_connection_complete == connssl->state) {
  501. *done = TRUE;
  502. return CURLE_OK;
  503. }
  504. if(ssl_connect_1 == connssl->connecting_state) {
  505. /* check out how much more time we're allowed */
  506. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  507. if(timeout_ms < 0) {
  508. /* no need to continue if time already is up */
  509. failf(data, "SSL/TLS connection timeout");
  510. return CURLE_OPERATION_TIMEDOUT;
  511. }
  512. retcode = schannel_connect_step1(conn, sockindex);
  513. if(retcode)
  514. return retcode;
  515. }
  516. while(ssl_connect_2 == connssl->connecting_state ||
  517. ssl_connect_2_reading == connssl->connecting_state ||
  518. ssl_connect_2_writing == connssl->connecting_state) {
  519. /* check out how much more time we're allowed */
  520. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  521. if(timeout_ms < 0) {
  522. /* no need to continue if time already is up */
  523. failf(data, "SSL/TLS connection timeout");
  524. return CURLE_OPERATION_TIMEDOUT;
  525. }
  526. /* if ssl is expecting something, check if it's available. */
  527. if(connssl->connecting_state == ssl_connect_2_reading
  528. || connssl->connecting_state == ssl_connect_2_writing) {
  529. curl_socket_t writefd = ssl_connect_2_writing ==
  530. connssl->connecting_state ? sockfd : CURL_SOCKET_BAD;
  531. curl_socket_t readfd = ssl_connect_2_reading ==
  532. connssl->connecting_state ? sockfd : CURL_SOCKET_BAD;
  533. what = Curl_socket_ready(readfd, writefd, nonblocking ? 0 : timeout_ms);
  534. if(what < 0) {
  535. /* fatal error */
  536. failf(data, "select/poll on SSL/TLS socket, errno: %d", SOCKERRNO);
  537. return CURLE_SSL_CONNECT_ERROR;
  538. }
  539. else if(0 == what) {
  540. if(nonblocking) {
  541. *done = FALSE;
  542. return CURLE_OK;
  543. }
  544. else {
  545. /* timeout */
  546. failf(data, "SSL/TLS connection timeout");
  547. return CURLE_OPERATION_TIMEDOUT;
  548. }
  549. }
  550. /* socket is readable or writable */
  551. }
  552. /* Run transaction, and return to the caller if it failed or if
  553. * this connection is part of a multi handle and this loop would
  554. * execute again. This permits the owner of a multi handle to
  555. * abort a connection attempt before step2 has completed while
  556. * ensuring that a client using select() or epoll() will always
  557. * have a valid fdset to wait on.
  558. */
  559. retcode = schannel_connect_step2(conn, sockindex);
  560. if(retcode || (nonblocking &&
  561. (ssl_connect_2 == connssl->connecting_state ||
  562. ssl_connect_2_reading == connssl->connecting_state ||
  563. ssl_connect_2_writing == connssl->connecting_state)))
  564. return retcode;
  565. } /* repeat step2 until all transactions are done. */
  566. if(ssl_connect_3 == connssl->connecting_state) {
  567. retcode = schannel_connect_step3(conn, sockindex);
  568. if(retcode)
  569. return retcode;
  570. }
  571. if(ssl_connect_done == connssl->connecting_state) {
  572. connssl->state = ssl_connection_complete;
  573. conn->recv[sockindex] = schannel_recv;
  574. conn->send[sockindex] = schannel_send;
  575. *done = TRUE;
  576. }
  577. else
  578. *done = FALSE;
  579. /* reset our connection state machine */
  580. connssl->connecting_state = ssl_connect_1;
  581. return CURLE_OK;
  582. }
  583. static ssize_t
  584. schannel_send(struct connectdata *conn, int sockindex,
  585. const void *buf, size_t len, CURLcode *err)
  586. {
  587. ssize_t written = -1;
  588. size_t data_len = 0;
  589. unsigned char *data = NULL;
  590. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  591. SecBuffer outbuf[4];
  592. SecBufferDesc outbuf_desc;
  593. SECURITY_STATUS sspi_status = SEC_E_OK;
  594. CURLcode code;
  595. /* check if the maximum stream sizes were queried */
  596. if(connssl->stream_sizes.cbMaximumMessage == 0) {
  597. sspi_status = s_pSecFn->QueryContextAttributes(
  598. &connssl->ctxt->ctxt_handle,
  599. SECPKG_ATTR_STREAM_SIZES,
  600. &connssl->stream_sizes);
  601. if(sspi_status != SEC_E_OK) {
  602. *err = CURLE_SEND_ERROR;
  603. return -1;
  604. }
  605. }
  606. /* check if the buffer is longer than the maximum message length */
  607. if(len > connssl->stream_sizes.cbMaximumMessage) {
  608. *err = CURLE_SEND_ERROR;
  609. return -1;
  610. }
  611. /* calculate the complete message length and allocate a buffer for it */
  612. data_len = connssl->stream_sizes.cbHeader + len +
  613. connssl->stream_sizes.cbTrailer;
  614. data = (unsigned char*) malloc(data_len);
  615. if(data == NULL) {
  616. *err = CURLE_OUT_OF_MEMORY;
  617. return -1;
  618. }
  619. /* setup output buffers (header, data, trailer, empty) */
  620. InitSecBuffer(&outbuf[0], SECBUFFER_STREAM_HEADER,
  621. data, connssl->stream_sizes.cbHeader);
  622. InitSecBuffer(&outbuf[1], SECBUFFER_DATA,
  623. data + connssl->stream_sizes.cbHeader, curlx_uztoul(len));
  624. InitSecBuffer(&outbuf[2], SECBUFFER_STREAM_TRAILER,
  625. data + connssl->stream_sizes.cbHeader + len,
  626. connssl->stream_sizes.cbTrailer);
  627. InitSecBuffer(&outbuf[3], SECBUFFER_EMPTY, NULL, 0);
  628. InitSecBufferDesc(&outbuf_desc, outbuf, 4);
  629. /* copy data into output buffer */
  630. memcpy(outbuf[1].pvBuffer, buf, len);
  631. /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375390.aspx */
  632. sspi_status = s_pSecFn->EncryptMessage(&connssl->ctxt->ctxt_handle, 0,
  633. &outbuf_desc, 0);
  634. /* check if the message was encrypted */
  635. if(sspi_status == SEC_E_OK) {
  636. written = 0;
  637. /* send the encrypted message including header, data and trailer */
  638. len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
  639. /*
  640. It's important to send the full message which includes the header,
  641. encrypted payload, and trailer. Until the client receives all the
  642. data a coherent message has not been delivered and the client
  643. can't read any of it.
  644. If we wanted to buffer the unwritten encrypted bytes, we would
  645. tell the client that all data it has requested to be sent has been
  646. sent. The unwritten encrypted bytes would be the first bytes to
  647. send on the next invocation.
  648. Here's the catch with this - if we tell the client that all the
  649. bytes have been sent, will the client call this method again to
  650. send the buffered data? Looking at who calls this function, it
  651. seems the answer is NO.
  652. */
  653. /* send entire message or fail */
  654. while(len > (size_t)written) {
  655. ssize_t this_write;
  656. long timeleft;
  657. int what;
  658. this_write = 0;
  659. timeleft = Curl_timeleft(conn->data, NULL, FALSE);
  660. if(timeleft < 0) {
  661. /* we already got the timeout */
  662. failf(conn->data, "schannel: timed out sending data "
  663. "(bytes sent: %zd)", written);
  664. *err = CURLE_OPERATION_TIMEDOUT;
  665. written = -1;
  666. break;
  667. }
  668. what = Curl_socket_ready(CURL_SOCKET_BAD, conn->sock[sockindex],
  669. timeleft);
  670. if(what < 0) {
  671. /* fatal error */
  672. failf(conn->data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  673. *err = CURLE_SEND_ERROR;
  674. written = -1;
  675. break;
  676. }
  677. else if(0 == what) {
  678. failf(conn->data, "schannel: timed out sending data "
  679. "(bytes sent: %zd)", written);
  680. *err = CURLE_OPERATION_TIMEDOUT;
  681. written = -1;
  682. break;
  683. }
  684. /* socket is writable */
  685. code = Curl_write_plain(conn, conn->sock[sockindex], data + written,
  686. len - written, &this_write);
  687. if(code == CURLE_AGAIN)
  688. continue;
  689. else if(code != CURLE_OK) {
  690. *err = code;
  691. written = -1;
  692. break;
  693. }
  694. written += this_write;
  695. }
  696. }
  697. else if(sspi_status == SEC_E_INSUFFICIENT_MEMORY) {
  698. *err = CURLE_OUT_OF_MEMORY;
  699. }
  700. else{
  701. *err = CURLE_SEND_ERROR;
  702. }
  703. Curl_safefree(data);
  704. if(len == (size_t)written)
  705. /* Encrypted message including header, data and trailer entirely sent.
  706. The return value is the number of unencrypted bytes that were sent. */
  707. written = outbuf[1].cbBuffer;
  708. return written;
  709. }
  710. static ssize_t
  711. schannel_recv(struct connectdata *conn, int sockindex,
  712. char *buf, size_t len, CURLcode *err)
  713. {
  714. size_t size = 0;
  715. ssize_t nread = 0, ret = -1;
  716. CURLcode retcode;
  717. struct SessionHandle *data = conn->data;
  718. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  719. bool done = FALSE;
  720. SecBuffer inbuf[4];
  721. SecBufferDesc inbuf_desc;
  722. SECURITY_STATUS sspi_status = SEC_E_OK;
  723. infof(data, "schannel: client wants to read %zu bytes\n", len);
  724. *err = CURLE_OK;
  725. /* buffer to store previously received and decrypted data */
  726. if(connssl->decdata_buffer == NULL) {
  727. connssl->decdata_offset = 0;
  728. connssl->decdata_length = CURL_SCHANNEL_BUFFER_INIT_SIZE;
  729. connssl->decdata_buffer = malloc(connssl->decdata_length);
  730. if(connssl->decdata_buffer == NULL) {
  731. failf(data, "schannel: unable to allocate memory");
  732. *err = CURLE_OUT_OF_MEMORY;
  733. return -1;
  734. }
  735. }
  736. /* increase buffer in order to fit the requested amount of data */
  737. while(connssl->encdata_length - connssl->encdata_offset <
  738. CURL_SCHANNEL_BUFFER_FREE_SIZE || connssl->encdata_length < len) {
  739. /* increase internal encrypted data buffer */
  740. connssl->encdata_length *= CURL_SCHANNEL_BUFFER_STEP_FACTOR;
  741. connssl->encdata_buffer = realloc(connssl->encdata_buffer,
  742. connssl->encdata_length);
  743. if(connssl->encdata_buffer == NULL) {
  744. failf(data, "schannel: unable to re-allocate memory");
  745. *err = CURLE_OUT_OF_MEMORY;
  746. return -1;
  747. }
  748. }
  749. /* read encrypted data from socket */
  750. infof(data, "schannel: encrypted data buffer: offset %zu length %zu\n",
  751. connssl->encdata_offset, connssl->encdata_length);
  752. size = connssl->encdata_length - connssl->encdata_offset;
  753. if(size > 0) {
  754. *err = Curl_read_plain(conn->sock[sockindex],
  755. (char *) (connssl->encdata_buffer + connssl->encdata_offset),
  756. size, &nread);
  757. /* check for received data */
  758. if(*err != CURLE_OK)
  759. ret = -1;
  760. else {
  761. if(nread > 0)
  762. /* increase encrypted data buffer offset */
  763. connssl->encdata_offset += nread;
  764. ret = nread;
  765. }
  766. infof(data, "schannel: encrypted data got %zd\n", ret);
  767. }
  768. infof(data, "schannel: encrypted data buffer: offset %zu length %zu\n",
  769. connssl->encdata_offset, connssl->encdata_length);
  770. /* check if we still have some data in our buffers */
  771. while(connssl->encdata_offset > 0 && sspi_status == SEC_E_OK &&
  772. connssl->decdata_offset < len) {
  773. /* prepare data buffer for DecryptMessage call */
  774. InitSecBuffer(&inbuf[0], SECBUFFER_DATA, connssl->encdata_buffer,
  775. curlx_uztoul(connssl->encdata_offset));
  776. /* we need 3 more empty input buffers for possible output */
  777. InitSecBuffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
  778. InitSecBuffer(&inbuf[2], SECBUFFER_EMPTY, NULL, 0);
  779. InitSecBuffer(&inbuf[3], SECBUFFER_EMPTY, NULL, 0);
  780. InitSecBufferDesc(&inbuf_desc, inbuf, 4);
  781. /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375348.aspx */
  782. sspi_status = s_pSecFn->DecryptMessage(&connssl->ctxt->ctxt_handle,
  783. &inbuf_desc, 0, NULL);
  784. /* check if we need more data */
  785. if(sspi_status == SEC_E_INCOMPLETE_MESSAGE) {
  786. infof(data, "schannel: failed to decrypt data, need more data\n");
  787. *err = CURLE_AGAIN;
  788. return -1;
  789. }
  790. /* check if everything went fine (server may want to renegotiate
  791. context) */
  792. if(sspi_status == SEC_E_OK || sspi_status == SEC_I_RENEGOTIATE ||
  793. sspi_status == SEC_I_CONTEXT_EXPIRED) {
  794. /* check for successfully decrypted data */
  795. if(inbuf[1].BufferType == SECBUFFER_DATA) {
  796. infof(data, "schannel: decrypted data length: %lu\n",
  797. inbuf[1].cbBuffer);
  798. /* increase buffer in order to fit the received amount of data */
  799. size = inbuf[1].cbBuffer > CURL_SCHANNEL_BUFFER_FREE_SIZE ?
  800. inbuf[1].cbBuffer : CURL_SCHANNEL_BUFFER_FREE_SIZE;
  801. while(connssl->decdata_length - connssl->decdata_offset < size ||
  802. connssl->decdata_length < len) {
  803. /* increase internal decrypted data buffer */
  804. connssl->decdata_length *= CURL_SCHANNEL_BUFFER_STEP_FACTOR;
  805. connssl->decdata_buffer = realloc(connssl->decdata_buffer,
  806. connssl->decdata_length);
  807. if(connssl->decdata_buffer == NULL) {
  808. failf(data, "schannel: unable to re-allocate memory");
  809. *err = CURLE_OUT_OF_MEMORY;
  810. return -1;
  811. }
  812. }
  813. /* copy decrypted data to internal buffer */
  814. size = inbuf[1].cbBuffer;
  815. if(size > 0) {
  816. memcpy(connssl->decdata_buffer + connssl->decdata_offset,
  817. inbuf[1].pvBuffer, size);
  818. connssl->decdata_offset += size;
  819. }
  820. infof(data, "schannel: decrypted data added: %zu\n", size);
  821. infof(data, "schannel: decrypted data cached: offset %zu length %zu\n",
  822. connssl->decdata_offset, connssl->decdata_length);
  823. }
  824. /* check for remaining encrypted data */
  825. if(inbuf[3].BufferType == SECBUFFER_EXTRA && inbuf[3].cbBuffer > 0) {
  826. infof(data, "schannel: encrypted data length: %lu\n",
  827. inbuf[3].cbBuffer);
  828. /* check if the remaining data is less than the total amount
  829. * and therefore begins after the already processed data
  830. */
  831. if(connssl->encdata_offset > inbuf[3].cbBuffer) {
  832. /* move remaining encrypted data forward to the beginning of
  833. buffer */
  834. memmove(connssl->encdata_buffer,
  835. (connssl->encdata_buffer + connssl->encdata_offset) -
  836. inbuf[3].cbBuffer, inbuf[3].cbBuffer);
  837. connssl->encdata_offset = inbuf[3].cbBuffer;
  838. }
  839. infof(data, "schannel: encrypted data cached: offset %zu length %zu\n",
  840. connssl->encdata_offset, connssl->encdata_length);
  841. }
  842. else{
  843. /* reset encrypted buffer offset, because there is no data remaining */
  844. connssl->encdata_offset = 0;
  845. }
  846. }
  847. /* check if server wants to renegotiate the connection context */
  848. if(sspi_status == SEC_I_RENEGOTIATE) {
  849. infof(data, "schannel: remote party requests SSL/TLS renegotiation\n");
  850. /* begin renegotiation */
  851. infof(data, "schannel: renegotiating SSL/TLS connection\n");
  852. connssl->state = ssl_connection_negotiating;
  853. connssl->connecting_state = ssl_connect_2_writing;
  854. retcode = schannel_connect_common(conn, sockindex, FALSE, &done);
  855. if(retcode)
  856. *err = retcode;
  857. else {
  858. infof(data, "schannel: SSL/TLS connection renegotiated\n");
  859. /* now retry receiving data */
  860. return schannel_recv(conn, sockindex, buf, len, err);
  861. }
  862. }
  863. }
  864. infof(data, "schannel: decrypted data buffer: offset %zu length %zu\n",
  865. connssl->decdata_offset, connssl->decdata_length);
  866. /* copy requested decrypted data to supplied buffer */
  867. size = len < connssl->decdata_offset ? len : connssl->decdata_offset;
  868. if(size > 0) {
  869. memcpy(buf, connssl->decdata_buffer, size);
  870. ret = size;
  871. /* move remaining decrypted data forward to the beginning of buffer */
  872. memmove(connssl->decdata_buffer, connssl->decdata_buffer + size,
  873. connssl->decdata_offset - size);
  874. connssl->decdata_offset -= size;
  875. infof(data, "schannel: decrypted data returned %zd\n", size);
  876. infof(data, "schannel: decrypted data buffer: offset %zu length %zu\n",
  877. connssl->decdata_offset, connssl->decdata_length);
  878. }
  879. /* check if the server closed the connection */
  880. if(ret <= 0 && ( /* special check for Windows 2000 Professional */
  881. sspi_status == SEC_I_CONTEXT_EXPIRED || (sspi_status == SEC_E_OK &&
  882. connssl->encdata_offset > 0 && connssl->encdata_buffer[0] == 0x15))) {
  883. infof(data, "schannel: server closed the connection\n");
  884. *err = CURLE_OK;
  885. return 0;
  886. }
  887. /* check if something went wrong and we need to return an error */
  888. if(ret < 0 && sspi_status != SEC_E_OK) {
  889. infof(data, "schannel: failed to read data from server: %s\n",
  890. Curl_sspi_strerror(conn, sspi_status));
  891. *err = CURLE_RECV_ERROR;
  892. return -1;
  893. }
  894. return ret;
  895. }
  896. CURLcode
  897. Curl_schannel_connect_nonblocking(struct connectdata *conn, int sockindex,
  898. bool *done)
  899. {
  900. return schannel_connect_common(conn, sockindex, TRUE, done);
  901. }
  902. CURLcode
  903. Curl_schannel_connect(struct connectdata *conn, int sockindex)
  904. {
  905. CURLcode retcode;
  906. bool done = FALSE;
  907. retcode = schannel_connect_common(conn, sockindex, FALSE, &done);
  908. if(retcode)
  909. return retcode;
  910. DEBUGASSERT(done);
  911. return CURLE_OK;
  912. }
  913. bool Curl_schannel_data_pending(const struct connectdata *conn, int sockindex)
  914. {
  915. const struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  916. if(connssl->use) /* SSL/TLS is in use */
  917. return (connssl->encdata_offset > 0 ||
  918. connssl->decdata_offset > 0 ) ? TRUE : FALSE;
  919. else
  920. return FALSE;
  921. }
  922. void Curl_schannel_close(struct connectdata *conn, int sockindex)
  923. {
  924. if(conn->ssl[sockindex].use)
  925. /* if the SSL/TLS channel hasn't been shut down yet, do that now. */
  926. Curl_ssl_shutdown(conn, sockindex);
  927. }
  928. int Curl_schannel_shutdown(struct connectdata *conn, int sockindex)
  929. {
  930. /* See http://msdn.microsoft.com/en-us/library/windows/desktop/aa380138.aspx
  931. * Shutting Down an Schannel Connection
  932. */
  933. struct SessionHandle *data = conn->data;
  934. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  935. infof(data, "schannel: shutting down SSL/TLS connection with %s port %hu\n",
  936. conn->host.name, conn->remote_port);
  937. if(connssl->cred && connssl->ctxt) {
  938. SecBufferDesc BuffDesc;
  939. SecBuffer Buffer;
  940. SECURITY_STATUS sspi_status;
  941. SecBuffer outbuf;
  942. SecBufferDesc outbuf_desc;
  943. CURLcode code;
  944. TCHAR *host_name;
  945. DWORD dwshut = SCHANNEL_SHUTDOWN;
  946. InitSecBuffer(&Buffer, SECBUFFER_TOKEN, &dwshut, sizeof(dwshut));
  947. InitSecBufferDesc(&BuffDesc, &Buffer, 1);
  948. sspi_status = s_pSecFn->ApplyControlToken(&connssl->ctxt->ctxt_handle,
  949. &BuffDesc);
  950. if(sspi_status != SEC_E_OK)
  951. failf(data, "schannel: ApplyControlToken failure: %s",
  952. Curl_sspi_strerror(conn, sspi_status));
  953. host_name = Curl_convert_UTF8_to_tchar(conn->host.name);
  954. if(!host_name)
  955. return CURLE_OUT_OF_MEMORY;
  956. /* setup output buffer */
  957. InitSecBuffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
  958. InitSecBufferDesc(&outbuf_desc, &outbuf, 1);
  959. sspi_status = s_pSecFn->InitializeSecurityContext(
  960. &connssl->cred->cred_handle,
  961. &connssl->ctxt->ctxt_handle,
  962. host_name,
  963. connssl->req_flags,
  964. 0,
  965. 0,
  966. NULL,
  967. 0,
  968. &connssl->ctxt->ctxt_handle,
  969. &outbuf_desc,
  970. &connssl->ret_flags,
  971. &connssl->ctxt->time_stamp);
  972. Curl_unicodefree(host_name);
  973. if((sspi_status == SEC_E_OK) || (sspi_status == SEC_I_CONTEXT_EXPIRED)) {
  974. /* send close message which is in output buffer */
  975. ssize_t written;
  976. code = Curl_write_plain(conn, conn->sock[sockindex], outbuf.pvBuffer,
  977. outbuf.cbBuffer, &written);
  978. s_pSecFn->FreeContextBuffer(outbuf.pvBuffer);
  979. if((code != CURLE_OK) || (outbuf.cbBuffer != (size_t)written)) {
  980. infof(data, "schannel: failed to send close msg: %s"
  981. " (bytes written: %zd)\n", curl_easy_strerror(code), written);
  982. }
  983. }
  984. }
  985. /* free SSPI Schannel API security context handle */
  986. if(connssl->ctxt) {
  987. infof(data, "schannel: clear security context handle\n");
  988. s_pSecFn->DeleteSecurityContext(&connssl->ctxt->ctxt_handle);
  989. Curl_safefree(connssl->ctxt);
  990. }
  991. /* free SSPI Schannel API credential handle */
  992. if(connssl->cred) {
  993. /* decrement the reference counter of the credential/session handle */
  994. if(connssl->cred->refcount > 0) {
  995. connssl->cred->refcount--;
  996. infof(data, "schannel: decremented credential handle refcount = %d\n",
  997. connssl->cred->refcount);
  998. }
  999. /* if the handle was not cached and the refcount is zero */
  1000. if(!connssl->cred->cached && connssl->cred->refcount == 0) {
  1001. infof(data, "schannel: clear credential handle\n");
  1002. s_pSecFn->FreeCredentialsHandle(&connssl->cred->cred_handle);
  1003. Curl_safefree(connssl->cred);
  1004. }
  1005. }
  1006. /* free internal buffer for received encrypted data */
  1007. if(connssl->encdata_buffer != NULL) {
  1008. Curl_safefree(connssl->encdata_buffer);
  1009. connssl->encdata_length = 0;
  1010. connssl->encdata_offset = 0;
  1011. }
  1012. /* free internal buffer for received decrypted data */
  1013. if(connssl->decdata_buffer != NULL) {
  1014. Curl_safefree(connssl->decdata_buffer);
  1015. connssl->decdata_length = 0;
  1016. connssl->decdata_offset = 0;
  1017. }
  1018. return CURLE_OK;
  1019. }
  1020. void Curl_schannel_session_free(void *ptr)
  1021. {
  1022. struct curl_schannel_cred *cred = ptr;
  1023. if(cred && cred->cached && cred->refcount == 0) {
  1024. s_pSecFn->FreeCredentialsHandle(&cred->cred_handle);
  1025. Curl_safefree(cred);
  1026. }
  1027. }
  1028. int Curl_schannel_init(void)
  1029. {
  1030. return (Curl_sspi_global_init() == CURLE_OK ? 1 : 0);
  1031. }
  1032. void Curl_schannel_cleanup(void)
  1033. {
  1034. Curl_sspi_global_cleanup();
  1035. }
  1036. size_t Curl_schannel_version(char *buffer, size_t size)
  1037. {
  1038. size = snprintf(buffer, size, "WinSSL");
  1039. return size;
  1040. }
  1041. int Curl_schannel_random(unsigned char *entropy, size_t length)
  1042. {
  1043. HCRYPTPROV hCryptProv = 0;
  1044. if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
  1045. CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  1046. return 1;
  1047. if(!CryptGenRandom(hCryptProv, (DWORD)length, entropy)) {
  1048. CryptReleaseContext(hCryptProv, 0UL);
  1049. return 1;
  1050. }
  1051. CryptReleaseContext(hCryptProv, 0UL);
  1052. return 0;
  1053. }
  1054. #ifdef _WIN32_WCE
  1055. static CURLcode verify_certificate(struct connectdata *conn, int sockindex)
  1056. {
  1057. SECURITY_STATUS status;
  1058. struct SessionHandle *data = conn->data;
  1059. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1060. CURLcode result = CURLE_OK;
  1061. CERT_CONTEXT *pCertContextServer = NULL;
  1062. const CERT_CHAIN_CONTEXT *pChainContext = NULL;
  1063. status = s_pSecFn->QueryContextAttributes(&connssl->ctxt->ctxt_handle,
  1064. SECPKG_ATTR_REMOTE_CERT_CONTEXT,
  1065. &pCertContextServer);
  1066. if((status != SEC_E_OK) || (pCertContextServer == NULL)) {
  1067. failf(data, "schannel: Failed to read remote certificate context: %s",
  1068. Curl_sspi_strerror(conn, status));
  1069. result = CURLE_PEER_FAILED_VERIFICATION;
  1070. }
  1071. if(result == CURLE_OK) {
  1072. CERT_CHAIN_PARA ChainPara;
  1073. memset(&ChainPara, 0, sizeof(ChainPara));
  1074. ChainPara.cbSize = sizeof(ChainPara);
  1075. if(!CertGetCertificateChain(NULL,
  1076. pCertContextServer,
  1077. NULL,
  1078. pCertContextServer->hCertStore,
  1079. &ChainPara,
  1080. 0,
  1081. NULL,
  1082. &pChainContext)) {
  1083. failf(data, "schannel: CertGetCertificateChain failed: %s",
  1084. Curl_sspi_strerror(conn, GetLastError()));
  1085. pChainContext = NULL;
  1086. result = CURLE_PEER_FAILED_VERIFICATION;
  1087. }
  1088. if(result == CURLE_OK) {
  1089. CERT_SIMPLE_CHAIN *pSimpleChain = pChainContext->rgpChain[0];
  1090. DWORD dwTrustErrorMask = ~(DWORD)(CERT_TRUST_IS_NOT_TIME_NESTED|
  1091. CERT_TRUST_REVOCATION_STATUS_UNKNOWN);
  1092. dwTrustErrorMask &= pSimpleChain->TrustStatus.dwErrorStatus;
  1093. if(dwTrustErrorMask) {
  1094. if(dwTrustErrorMask & CERT_TRUST_IS_PARTIAL_CHAIN)
  1095. failf(data, "schannel: CertGetCertificateChain trust error"
  1096. " CERT_TRUST_IS_PARTIAL_CHAIN");
  1097. if(dwTrustErrorMask & CERT_TRUST_IS_UNTRUSTED_ROOT)
  1098. failf(data, "schannel: CertGetCertificateChain trust error"
  1099. " CERT_TRUST_IS_UNTRUSTED_ROOT");
  1100. if(dwTrustErrorMask & CERT_TRUST_IS_NOT_TIME_VALID)
  1101. failf(data, "schannel: CertGetCertificateChain trust error"
  1102. " CERT_TRUST_IS_NOT_TIME_VALID");
  1103. failf(data, "schannel: CertGetCertificateChain error mask: 0x%08x",
  1104. dwTrustErrorMask);
  1105. result = CURLE_PEER_FAILED_VERIFICATION;
  1106. }
  1107. }
  1108. }
  1109. if(result == CURLE_OK) {
  1110. if(data->set.ssl.verifyhost) {
  1111. TCHAR cert_hostname_buff[128];
  1112. xcharp_u hostname;
  1113. xcharp_u cert_hostname;
  1114. DWORD len;
  1115. cert_hostname.const_tchar_ptr = cert_hostname_buff;
  1116. hostname.tchar_ptr = Curl_convert_UTF8_to_tchar(conn->host.name);
  1117. len = CertGetNameString(pCertContextServer,
  1118. CERT_NAME_DNS_TYPE,
  1119. 0,
  1120. NULL,
  1121. cert_hostname.tchar_ptr,
  1122. 128);
  1123. if(len > 0 && *cert_hostname.tchar_ptr == '*') {
  1124. /* this is a wildcard cert. try matching the last len - 1 chars */
  1125. int hostname_len = strlen(conn->host.name);
  1126. cert_hostname.tchar_ptr++;
  1127. if(_tcsicmp(cert_hostname.const_tchar_ptr,
  1128. hostname.const_tchar_ptr + hostname_len - len + 2) != 0)
  1129. result = CURLE_PEER_FAILED_VERIFICATION;
  1130. }
  1131. else if(len == 0 || _tcsicmp(hostname.const_tchar_ptr,
  1132. cert_hostname.const_tchar_ptr) != 0) {
  1133. result = CURLE_PEER_FAILED_VERIFICATION;
  1134. }
  1135. if(result == CURLE_PEER_FAILED_VERIFICATION) {
  1136. char *_cert_hostname;
  1137. _cert_hostname = Curl_convert_tchar_to_UTF8(cert_hostname.tchar_ptr);
  1138. failf(data, "schannel: CertGetNameString() certificate hostname "
  1139. "(%s) did not match connection (%s)",
  1140. _cert_hostname, conn->host.name);
  1141. Curl_unicodefree(_cert_hostname);
  1142. }
  1143. Curl_unicodefree(hostname.tchar_ptr);
  1144. }
  1145. }
  1146. if(pChainContext)
  1147. CertFreeCertificateChain(pChainContext);
  1148. if(pCertContextServer)
  1149. CertFreeCertificateContext(pCertContextServer);
  1150. return result;
  1151. }
  1152. #endif /* _WIN32_WCE */
  1153. #endif /* USE_SCHANNEL */