2
0

socks_sspi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) Markus Moeller, <markus_moeller@compuserve.com>
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #if defined(USE_WINDOWS_SSPI) && !defined(CURL_DISABLE_PROXY)
  27. #include "urldata.h"
  28. #include "sendf.h"
  29. #include "cfilters.h"
  30. #include "connect.h"
  31. #include "strerror.h"
  32. #include "timeval.h"
  33. #include "socks.h"
  34. #include "curl_sspi.h"
  35. #include "curl_multibyte.h"
  36. #include "warnless.h"
  37. #include "strdup.h"
  38. /* The last 3 #include files should be in this order */
  39. #include "curl_printf.h"
  40. #include "curl_memory.h"
  41. #include "memdebug.h"
  42. /*
  43. * Helper sspi error functions.
  44. */
  45. static int check_sspi_err(struct Curl_easy *data,
  46. SECURITY_STATUS status,
  47. const char *function)
  48. {
  49. if(status != SEC_E_OK &&
  50. status != SEC_I_COMPLETE_AND_CONTINUE &&
  51. status != SEC_I_COMPLETE_NEEDED &&
  52. status != SEC_I_CONTINUE_NEEDED) {
  53. char buffer[STRERROR_LEN];
  54. failf(data, "SSPI error: %s failed: %s", function,
  55. Curl_sspi_strerror(status, buffer, sizeof(buffer)));
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. /* This is the SSPI-using version of this function */
  61. CURLcode Curl_SOCKS5_gssapi_negotiate(struct Curl_cfilter *cf,
  62. struct Curl_easy *data)
  63. {
  64. struct connectdata *conn = cf->conn;
  65. curl_socket_t sock = conn->sock[cf->sockindex];
  66. CURLcode code;
  67. ssize_t actualread;
  68. ssize_t written;
  69. int result;
  70. /* Needs GSS-API authentication */
  71. SECURITY_STATUS status;
  72. unsigned long sspi_ret_flags = 0;
  73. unsigned char gss_enc;
  74. SecBuffer sspi_send_token, sspi_recv_token, sspi_w_token[3];
  75. SecBufferDesc input_desc, output_desc, wrap_desc;
  76. SecPkgContext_Sizes sspi_sizes;
  77. CredHandle cred_handle;
  78. CtxtHandle sspi_context;
  79. PCtxtHandle context_handle = NULL;
  80. SecPkgCredentials_Names names;
  81. TimeStamp expiry;
  82. char *service_name = NULL;
  83. unsigned short us_length;
  84. unsigned long qop;
  85. unsigned char socksreq[4]; /* room for GSS-API exchange header only */
  86. const char *service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
  87. data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd";
  88. const size_t service_length = strlen(service);
  89. /* GSS-API request looks like
  90. * +----+------+-----+----------------+
  91. * |VER | MTYP | LEN | TOKEN |
  92. * +----+------+----------------------+
  93. * | 1 | 1 | 2 | up to 2^16 - 1 |
  94. * +----+------+-----+----------------+
  95. */
  96. /* prepare service name */
  97. if(strchr(service, '/')) {
  98. service_name = strdup(service);
  99. if(!service_name)
  100. return CURLE_OUT_OF_MEMORY;
  101. }
  102. else {
  103. service_name = malloc(service_length +
  104. strlen(conn->socks_proxy.host.name) + 2);
  105. if(!service_name)
  106. return CURLE_OUT_OF_MEMORY;
  107. msnprintf(service_name, service_length +
  108. strlen(conn->socks_proxy.host.name) + 2, "%s/%s",
  109. service, conn->socks_proxy.host.name);
  110. }
  111. input_desc.cBuffers = 1;
  112. input_desc.pBuffers = &sspi_recv_token;
  113. input_desc.ulVersion = SECBUFFER_VERSION;
  114. sspi_recv_token.BufferType = SECBUFFER_TOKEN;
  115. sspi_recv_token.cbBuffer = 0;
  116. sspi_recv_token.pvBuffer = NULL;
  117. output_desc.cBuffers = 1;
  118. output_desc.pBuffers = &sspi_send_token;
  119. output_desc.ulVersion = SECBUFFER_VERSION;
  120. sspi_send_token.BufferType = SECBUFFER_TOKEN;
  121. sspi_send_token.cbBuffer = 0;
  122. sspi_send_token.pvBuffer = NULL;
  123. wrap_desc.cBuffers = 3;
  124. wrap_desc.pBuffers = sspi_w_token;
  125. wrap_desc.ulVersion = SECBUFFER_VERSION;
  126. cred_handle.dwLower = 0;
  127. cred_handle.dwUpper = 0;
  128. status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
  129. (TCHAR *) TEXT("Kerberos"),
  130. SECPKG_CRED_OUTBOUND,
  131. NULL,
  132. NULL,
  133. NULL,
  134. NULL,
  135. &cred_handle,
  136. &expiry);
  137. if(check_sspi_err(data, status, "AcquireCredentialsHandle")) {
  138. failf(data, "Failed to acquire credentials.");
  139. free(service_name);
  140. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  141. return CURLE_COULDNT_CONNECT;
  142. }
  143. (void)curlx_nonblock(sock, FALSE);
  144. /* As long as we need to keep sending some context info, and there is no */
  145. /* errors, keep sending it... */
  146. for(;;) {
  147. TCHAR *sname;
  148. sname = curlx_convert_UTF8_to_tchar(service_name);
  149. if(!sname)
  150. return CURLE_OUT_OF_MEMORY;
  151. status = Curl_pSecFn->InitializeSecurityContext(&cred_handle,
  152. context_handle,
  153. sname,
  154. ISC_REQ_MUTUAL_AUTH |
  155. ISC_REQ_ALLOCATE_MEMORY |
  156. ISC_REQ_CONFIDENTIALITY |
  157. ISC_REQ_REPLAY_DETECT,
  158. 0,
  159. SECURITY_NATIVE_DREP,
  160. &input_desc,
  161. 0,
  162. &sspi_context,
  163. &output_desc,
  164. &sspi_ret_flags,
  165. &expiry);
  166. curlx_unicodefree(sname);
  167. if(sspi_recv_token.pvBuffer) {
  168. Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  169. sspi_recv_token.pvBuffer = NULL;
  170. sspi_recv_token.cbBuffer = 0;
  171. }
  172. if(check_sspi_err(data, status, "InitializeSecurityContext")) {
  173. free(service_name);
  174. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  175. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  176. if(sspi_recv_token.pvBuffer)
  177. Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  178. failf(data, "Failed to initialise security context.");
  179. return CURLE_COULDNT_CONNECT;
  180. }
  181. if(sspi_send_token.cbBuffer) {
  182. socksreq[0] = 1; /* GSS-API subnegotiation version */
  183. socksreq[1] = 1; /* authentication message type */
  184. us_length = htons((unsigned short)sspi_send_token.cbBuffer);
  185. memcpy(socksreq + 2, &us_length, sizeof(short));
  186. written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 4, FALSE,
  187. &code);
  188. if(code || (4 != written)) {
  189. failf(data, "Failed to send SSPI authentication request.");
  190. free(service_name);
  191. if(sspi_send_token.pvBuffer)
  192. Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  193. if(sspi_recv_token.pvBuffer)
  194. Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  195. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  196. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  197. return CURLE_COULDNT_CONNECT;
  198. }
  199. written = Curl_conn_cf_send(cf->next, data,
  200. (char *)sspi_send_token.pvBuffer,
  201. sspi_send_token.cbBuffer, FALSE, &code);
  202. if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
  203. failf(data, "Failed to send SSPI authentication token.");
  204. free(service_name);
  205. if(sspi_send_token.pvBuffer)
  206. Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  207. if(sspi_recv_token.pvBuffer)
  208. Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  209. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  210. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  211. return CURLE_COULDNT_CONNECT;
  212. }
  213. }
  214. if(sspi_send_token.pvBuffer) {
  215. Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  216. sspi_send_token.pvBuffer = NULL;
  217. }
  218. sspi_send_token.cbBuffer = 0;
  219. if(sspi_recv_token.pvBuffer) {
  220. Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  221. sspi_recv_token.pvBuffer = NULL;
  222. }
  223. sspi_recv_token.cbBuffer = 0;
  224. if(status != SEC_I_CONTINUE_NEEDED)
  225. break;
  226. /* analyse response */
  227. /* GSS-API response looks like
  228. * +----+------+-----+----------------+
  229. * |VER | MTYP | LEN | TOKEN |
  230. * +----+------+----------------------+
  231. * | 1 | 1 | 2 | up to 2^16 - 1 |
  232. * +----+------+-----+----------------+
  233. */
  234. result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
  235. if(result || (actualread != 4)) {
  236. failf(data, "Failed to receive SSPI authentication response.");
  237. free(service_name);
  238. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  239. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  240. return CURLE_COULDNT_CONNECT;
  241. }
  242. /* ignore the first (VER) byte */
  243. if(socksreq[1] == 255) { /* status / message type */
  244. failf(data, "User was rejected by the SOCKS5 server (%u %u).",
  245. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  246. free(service_name);
  247. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  248. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  249. return CURLE_COULDNT_CONNECT;
  250. }
  251. if(socksreq[1] != 1) { /* status / message type */
  252. failf(data, "Invalid SSPI authentication response type (%u %u).",
  253. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  254. free(service_name);
  255. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  256. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  257. return CURLE_COULDNT_CONNECT;
  258. }
  259. memcpy(&us_length, socksreq + 2, sizeof(short));
  260. us_length = ntohs(us_length);
  261. sspi_recv_token.cbBuffer = us_length;
  262. sspi_recv_token.pvBuffer = malloc(us_length);
  263. if(!sspi_recv_token.pvBuffer) {
  264. free(service_name);
  265. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  266. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  267. return CURLE_OUT_OF_MEMORY;
  268. }
  269. result = Curl_blockread_all(cf, data, (char *)sspi_recv_token.pvBuffer,
  270. sspi_recv_token.cbBuffer, &actualread);
  271. if(result || (actualread != us_length)) {
  272. failf(data, "Failed to receive SSPI authentication token.");
  273. free(service_name);
  274. if(sspi_recv_token.pvBuffer)
  275. Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  276. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  277. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  278. return CURLE_COULDNT_CONNECT;
  279. }
  280. context_handle = &sspi_context;
  281. }
  282. free(service_name);
  283. /* Everything is good so far, user was authenticated! */
  284. status = Curl_pSecFn->QueryCredentialsAttributes(&cred_handle,
  285. SECPKG_CRED_ATTR_NAMES,
  286. &names);
  287. Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
  288. if(check_sspi_err(data, status, "QueryCredentialAttributes")) {
  289. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  290. Curl_pSecFn->FreeContextBuffer(names.sUserName);
  291. failf(data, "Failed to determine username.");
  292. return CURLE_COULDNT_CONNECT;
  293. }
  294. else {
  295. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  296. char *user_utf8 = curlx_convert_tchar_to_UTF8(names.sUserName);
  297. infof(data, "SOCKS5 server authenticated user %s with GSS-API.",
  298. (user_utf8 ? user_utf8 : "(unknown)"));
  299. curlx_unicodefree(user_utf8);
  300. #endif
  301. Curl_pSecFn->FreeContextBuffer(names.sUserName);
  302. }
  303. /* Do encryption */
  304. socksreq[0] = 1; /* GSS-API subnegotiation version */
  305. socksreq[1] = 2; /* encryption message type */
  306. gss_enc = 0; /* no data protection */
  307. /* do confidentiality protection if supported */
  308. if(sspi_ret_flags & ISC_REQ_CONFIDENTIALITY)
  309. gss_enc = 2;
  310. /* else do integrity protection */
  311. else if(sspi_ret_flags & ISC_REQ_INTEGRITY)
  312. gss_enc = 1;
  313. infof(data, "SOCKS5 server supports GSS-API %s data protection.",
  314. (gss_enc == 0) ? "no" :
  315. ((gss_enc == 1) ? "integrity":"confidentiality") );
  316. /* force to no data protection, avoid encryption/decryption for now */
  317. gss_enc = 0;
  318. /*
  319. * Sending the encryption type in clear seems wrong. It should be
  320. * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
  321. * The NEC reference implementations on which this is based is
  322. * therefore at fault
  323. *
  324. * +------+------+------+.......................+
  325. * + ver | mtyp | len | token |
  326. * +------+------+------+.......................+
  327. * + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
  328. * +------+------+------+.......................+
  329. *
  330. * Where:
  331. *
  332. * - "ver" is the protocol version number, here 1 to represent the
  333. * first version of the SOCKS/GSS-API protocol
  334. *
  335. * - "mtyp" is the message type, here 2 to represent a protection
  336. * -level negotiation message
  337. *
  338. * - "len" is the length of the "token" field in octets
  339. *
  340. * - "token" is the GSS-API encapsulated protection level
  341. *
  342. * The token is produced by encapsulating an octet containing the
  343. * required protection level using gss_seal()/gss_wrap() with conf_req
  344. * set to FALSE. The token is verified using gss_unseal()/
  345. * gss_unwrap().
  346. *
  347. */
  348. if(data->set.socks5_gssapi_nec) {
  349. us_length = htons((unsigned short)1);
  350. memcpy(socksreq + 2, &us_length, sizeof(short));
  351. }
  352. else {
  353. status = Curl_pSecFn->QueryContextAttributes(&sspi_context,
  354. SECPKG_ATTR_SIZES,
  355. &sspi_sizes);
  356. if(check_sspi_err(data, status, "QueryContextAttributes")) {
  357. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  358. failf(data, "Failed to query security context attributes.");
  359. return CURLE_COULDNT_CONNECT;
  360. }
  361. sspi_w_token[0].cbBuffer = sspi_sizes.cbSecurityTrailer;
  362. sspi_w_token[0].BufferType = SECBUFFER_TOKEN;
  363. sspi_w_token[0].pvBuffer = malloc(sspi_sizes.cbSecurityTrailer);
  364. if(!sspi_w_token[0].pvBuffer) {
  365. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  366. return CURLE_OUT_OF_MEMORY;
  367. }
  368. sspi_w_token[1].cbBuffer = 1;
  369. sspi_w_token[1].pvBuffer = malloc(1);
  370. if(!sspi_w_token[1].pvBuffer) {
  371. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  372. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  373. return CURLE_OUT_OF_MEMORY;
  374. }
  375. memcpy(sspi_w_token[1].pvBuffer, &gss_enc, 1);
  376. sspi_w_token[2].BufferType = SECBUFFER_PADDING;
  377. sspi_w_token[2].cbBuffer = sspi_sizes.cbBlockSize;
  378. sspi_w_token[2].pvBuffer = malloc(sspi_sizes.cbBlockSize);
  379. if(!sspi_w_token[2].pvBuffer) {
  380. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  381. Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  382. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  383. return CURLE_OUT_OF_MEMORY;
  384. }
  385. status = Curl_pSecFn->EncryptMessage(&sspi_context,
  386. KERB_WRAP_NO_ENCRYPT,
  387. &wrap_desc,
  388. 0);
  389. if(check_sspi_err(data, status, "EncryptMessage")) {
  390. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  391. Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  392. Curl_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  393. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  394. failf(data, "Failed to query security context attributes.");
  395. return CURLE_COULDNT_CONNECT;
  396. }
  397. sspi_send_token.cbBuffer = sspi_w_token[0].cbBuffer
  398. + sspi_w_token[1].cbBuffer
  399. + sspi_w_token[2].cbBuffer;
  400. sspi_send_token.pvBuffer = malloc(sspi_send_token.cbBuffer);
  401. if(!sspi_send_token.pvBuffer) {
  402. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  403. Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  404. Curl_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  405. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  406. return CURLE_OUT_OF_MEMORY;
  407. }
  408. memcpy(sspi_send_token.pvBuffer, sspi_w_token[0].pvBuffer,
  409. sspi_w_token[0].cbBuffer);
  410. memcpy((PUCHAR) sspi_send_token.pvBuffer +(int)sspi_w_token[0].cbBuffer,
  411. sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
  412. memcpy((PUCHAR) sspi_send_token.pvBuffer
  413. + sspi_w_token[0].cbBuffer
  414. + sspi_w_token[1].cbBuffer,
  415. sspi_w_token[2].pvBuffer, sspi_w_token[2].cbBuffer);
  416. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  417. sspi_w_token[0].pvBuffer = NULL;
  418. sspi_w_token[0].cbBuffer = 0;
  419. Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  420. sspi_w_token[1].pvBuffer = NULL;
  421. sspi_w_token[1].cbBuffer = 0;
  422. Curl_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  423. sspi_w_token[2].pvBuffer = NULL;
  424. sspi_w_token[2].cbBuffer = 0;
  425. us_length = htons((unsigned short)sspi_send_token.cbBuffer);
  426. memcpy(socksreq + 2, &us_length, sizeof(short));
  427. }
  428. written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 4, FALSE,
  429. &code);
  430. if(code || (4 != written)) {
  431. failf(data, "Failed to send SSPI encryption request.");
  432. if(sspi_send_token.pvBuffer)
  433. Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  434. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  435. return CURLE_COULDNT_CONNECT;
  436. }
  437. if(data->set.socks5_gssapi_nec) {
  438. memcpy(socksreq, &gss_enc, 1);
  439. written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 1, FALSE,
  440. &code);
  441. if(code || (1 != written)) {
  442. failf(data, "Failed to send SSPI encryption type.");
  443. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  444. return CURLE_COULDNT_CONNECT;
  445. }
  446. }
  447. else {
  448. written = Curl_conn_cf_send(cf->next, data,
  449. (char *)sspi_send_token.pvBuffer,
  450. sspi_send_token.cbBuffer, FALSE, &code);
  451. if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
  452. failf(data, "Failed to send SSPI encryption type.");
  453. if(sspi_send_token.pvBuffer)
  454. Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  455. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  456. return CURLE_COULDNT_CONNECT;
  457. }
  458. if(sspi_send_token.pvBuffer)
  459. Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  460. }
  461. result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
  462. if(result || (actualread != 4)) {
  463. failf(data, "Failed to receive SSPI encryption response.");
  464. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  465. return CURLE_COULDNT_CONNECT;
  466. }
  467. /* ignore the first (VER) byte */
  468. if(socksreq[1] == 255) { /* status / message type */
  469. failf(data, "User was rejected by the SOCKS5 server (%u %u).",
  470. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  471. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  472. return CURLE_COULDNT_CONNECT;
  473. }
  474. if(socksreq[1] != 2) { /* status / message type */
  475. failf(data, "Invalid SSPI encryption response type (%u %u).",
  476. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  477. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  478. return CURLE_COULDNT_CONNECT;
  479. }
  480. memcpy(&us_length, socksreq + 2, sizeof(short));
  481. us_length = ntohs(us_length);
  482. sspi_w_token[0].cbBuffer = us_length;
  483. sspi_w_token[0].pvBuffer = malloc(us_length);
  484. if(!sspi_w_token[0].pvBuffer) {
  485. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  486. return CURLE_OUT_OF_MEMORY;
  487. }
  488. result = Curl_blockread_all(cf, data, (char *)sspi_w_token[0].pvBuffer,
  489. sspi_w_token[0].cbBuffer, &actualread);
  490. if(result || (actualread != us_length)) {
  491. failf(data, "Failed to receive SSPI encryption type.");
  492. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  493. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  494. return CURLE_COULDNT_CONNECT;
  495. }
  496. if(!data->set.socks5_gssapi_nec) {
  497. wrap_desc.cBuffers = 2;
  498. sspi_w_token[0].BufferType = SECBUFFER_STREAM;
  499. sspi_w_token[1].BufferType = SECBUFFER_DATA;
  500. sspi_w_token[1].cbBuffer = 0;
  501. sspi_w_token[1].pvBuffer = NULL;
  502. status = Curl_pSecFn->DecryptMessage(&sspi_context,
  503. &wrap_desc,
  504. 0,
  505. &qop);
  506. if(check_sspi_err(data, status, "DecryptMessage")) {
  507. if(sspi_w_token[0].pvBuffer)
  508. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  509. if(sspi_w_token[1].pvBuffer)
  510. Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  511. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  512. failf(data, "Failed to query security context attributes.");
  513. return CURLE_COULDNT_CONNECT;
  514. }
  515. if(sspi_w_token[1].cbBuffer != 1) {
  516. failf(data, "Invalid SSPI encryption response length (%lu).",
  517. (unsigned long)sspi_w_token[1].cbBuffer);
  518. if(sspi_w_token[0].pvBuffer)
  519. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  520. if(sspi_w_token[1].pvBuffer)
  521. Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  522. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  523. return CURLE_COULDNT_CONNECT;
  524. }
  525. memcpy(socksreq, sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
  526. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  527. Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  528. }
  529. else {
  530. if(sspi_w_token[0].cbBuffer != 1) {
  531. failf(data, "Invalid SSPI encryption response length (%lu).",
  532. (unsigned long)sspi_w_token[0].cbBuffer);
  533. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  534. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  535. return CURLE_COULDNT_CONNECT;
  536. }
  537. memcpy(socksreq, sspi_w_token[0].pvBuffer, sspi_w_token[0].cbBuffer);
  538. Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  539. }
  540. (void)curlx_nonblock(sock, TRUE);
  541. infof(data, "SOCKS5 access with%s protection granted.",
  542. (socksreq[0] == 0) ? "out GSS-API data":
  543. ((socksreq[0] == 1) ? " GSS-API integrity" :
  544. " GSS-API confidentiality"));
  545. /* For later use if encryption is required
  546. conn->socks5_gssapi_enctype = socksreq[0];
  547. if(socksreq[0] != 0)
  548. conn->socks5_sspi_context = sspi_context;
  549. else {
  550. Curl_pSecFn->DeleteSecurityContext(&sspi_context);
  551. conn->socks5_sspi_context = sspi_context;
  552. }
  553. */
  554. return CURLE_OK;
  555. }
  556. #endif