socks_sspi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 = s_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. s_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's 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 = s_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. s_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. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  175. s_pSecFn->DeleteSecurityContext(&sspi_context);
  176. if(sspi_recv_token.pvBuffer)
  177. s_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((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, &code);
  187. if(code || (4 != written)) {
  188. failf(data, "Failed to send SSPI authentication request.");
  189. free(service_name);
  190. if(sspi_send_token.pvBuffer)
  191. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  192. if(sspi_recv_token.pvBuffer)
  193. s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  194. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  195. s_pSecFn->DeleteSecurityContext(&sspi_context);
  196. return CURLE_COULDNT_CONNECT;
  197. }
  198. written = Curl_conn_cf_send(cf->next, data,
  199. (char *)sspi_send_token.pvBuffer,
  200. sspi_send_token.cbBuffer, &code);
  201. if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
  202. failf(data, "Failed to send SSPI authentication token.");
  203. free(service_name);
  204. if(sspi_send_token.pvBuffer)
  205. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  206. if(sspi_recv_token.pvBuffer)
  207. s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  208. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  209. s_pSecFn->DeleteSecurityContext(&sspi_context);
  210. return CURLE_COULDNT_CONNECT;
  211. }
  212. }
  213. if(sspi_send_token.pvBuffer) {
  214. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  215. sspi_send_token.pvBuffer = NULL;
  216. }
  217. sspi_send_token.cbBuffer = 0;
  218. if(sspi_recv_token.pvBuffer) {
  219. s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  220. sspi_recv_token.pvBuffer = NULL;
  221. }
  222. sspi_recv_token.cbBuffer = 0;
  223. if(status != SEC_I_CONTINUE_NEEDED)
  224. break;
  225. /* analyse response */
  226. /* GSS-API response looks like
  227. * +----+------+-----+----------------+
  228. * |VER | MTYP | LEN | TOKEN |
  229. * +----+------+----------------------+
  230. * | 1 | 1 | 2 | up to 2^16 - 1 |
  231. * +----+------+-----+----------------+
  232. */
  233. result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
  234. if(result || (actualread != 4)) {
  235. failf(data, "Failed to receive SSPI authentication response.");
  236. free(service_name);
  237. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  238. s_pSecFn->DeleteSecurityContext(&sspi_context);
  239. return CURLE_COULDNT_CONNECT;
  240. }
  241. /* ignore the first (VER) byte */
  242. if(socksreq[1] == 255) { /* status / message type */
  243. failf(data, "User was rejected by the SOCKS5 server (%u %u).",
  244. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  245. free(service_name);
  246. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  247. s_pSecFn->DeleteSecurityContext(&sspi_context);
  248. return CURLE_COULDNT_CONNECT;
  249. }
  250. if(socksreq[1] != 1) { /* status / message type */
  251. failf(data, "Invalid SSPI authentication response type (%u %u).",
  252. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  253. free(service_name);
  254. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  255. s_pSecFn->DeleteSecurityContext(&sspi_context);
  256. return CURLE_COULDNT_CONNECT;
  257. }
  258. memcpy(&us_length, socksreq + 2, sizeof(short));
  259. us_length = ntohs(us_length);
  260. sspi_recv_token.cbBuffer = us_length;
  261. sspi_recv_token.pvBuffer = malloc(us_length);
  262. if(!sspi_recv_token.pvBuffer) {
  263. free(service_name);
  264. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  265. s_pSecFn->DeleteSecurityContext(&sspi_context);
  266. return CURLE_OUT_OF_MEMORY;
  267. }
  268. result = Curl_blockread_all(cf, data, (char *)sspi_recv_token.pvBuffer,
  269. sspi_recv_token.cbBuffer, &actualread);
  270. if(result || (actualread != us_length)) {
  271. failf(data, "Failed to receive SSPI authentication token.");
  272. free(service_name);
  273. if(sspi_recv_token.pvBuffer)
  274. s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
  275. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  276. s_pSecFn->DeleteSecurityContext(&sspi_context);
  277. return CURLE_COULDNT_CONNECT;
  278. }
  279. context_handle = &sspi_context;
  280. }
  281. free(service_name);
  282. /* Everything is good so far, user was authenticated! */
  283. status = s_pSecFn->QueryCredentialsAttributes(&cred_handle,
  284. SECPKG_CRED_ATTR_NAMES,
  285. &names);
  286. s_pSecFn->FreeCredentialsHandle(&cred_handle);
  287. if(check_sspi_err(data, status, "QueryCredentialAttributes")) {
  288. s_pSecFn->DeleteSecurityContext(&sspi_context);
  289. s_pSecFn->FreeContextBuffer(names.sUserName);
  290. failf(data, "Failed to determine user name.");
  291. return CURLE_COULDNT_CONNECT;
  292. }
  293. else {
  294. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  295. char *user_utf8 = curlx_convert_tchar_to_UTF8(names.sUserName);
  296. infof(data, "SOCKS5 server authenticated user %s with GSS-API.",
  297. (user_utf8 ? user_utf8 : "(unknown)"));
  298. curlx_unicodefree(user_utf8);
  299. #endif
  300. s_pSecFn->FreeContextBuffer(names.sUserName);
  301. }
  302. /* Do encryption */
  303. socksreq[0] = 1; /* GSS-API subnegotiation version */
  304. socksreq[1] = 2; /* encryption message type */
  305. gss_enc = 0; /* no data protection */
  306. /* do confidentiality protection if supported */
  307. if(sspi_ret_flags & ISC_REQ_CONFIDENTIALITY)
  308. gss_enc = 2;
  309. /* else do integrity protection */
  310. else if(sspi_ret_flags & ISC_REQ_INTEGRITY)
  311. gss_enc = 1;
  312. infof(data, "SOCKS5 server supports GSS-API %s data protection.",
  313. (gss_enc == 0)?"no":((gss_enc == 1)?"integrity":"confidentiality") );
  314. /* force to no data protection, avoid encryption/decryption for now */
  315. gss_enc = 0;
  316. /*
  317. * Sending the encryption type in clear seems wrong. It should be
  318. * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
  319. * The NEC reference implementations on which this is based is
  320. * therefore at fault
  321. *
  322. * +------+------+------+.......................+
  323. * + ver | mtyp | len | token |
  324. * +------+------+------+.......................+
  325. * + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
  326. * +------+------+------+.......................+
  327. *
  328. * Where:
  329. *
  330. * - "ver" is the protocol version number, here 1 to represent the
  331. * first version of the SOCKS/GSS-API protocol
  332. *
  333. * - "mtyp" is the message type, here 2 to represent a protection
  334. * -level negotiation message
  335. *
  336. * - "len" is the length of the "token" field in octets
  337. *
  338. * - "token" is the GSS-API encapsulated protection level
  339. *
  340. * The token is produced by encapsulating an octet containing the
  341. * required protection level using gss_seal()/gss_wrap() with conf_req
  342. * set to FALSE. The token is verified using gss_unseal()/
  343. * gss_unwrap().
  344. *
  345. */
  346. if(data->set.socks5_gssapi_nec) {
  347. us_length = htons((short)1);
  348. memcpy(socksreq + 2, &us_length, sizeof(short));
  349. }
  350. else {
  351. status = s_pSecFn->QueryContextAttributes(&sspi_context,
  352. SECPKG_ATTR_SIZES,
  353. &sspi_sizes);
  354. if(check_sspi_err(data, status, "QueryContextAttributes")) {
  355. s_pSecFn->DeleteSecurityContext(&sspi_context);
  356. failf(data, "Failed to query security context attributes.");
  357. return CURLE_COULDNT_CONNECT;
  358. }
  359. sspi_w_token[0].cbBuffer = sspi_sizes.cbSecurityTrailer;
  360. sspi_w_token[0].BufferType = SECBUFFER_TOKEN;
  361. sspi_w_token[0].pvBuffer = malloc(sspi_sizes.cbSecurityTrailer);
  362. if(!sspi_w_token[0].pvBuffer) {
  363. s_pSecFn->DeleteSecurityContext(&sspi_context);
  364. return CURLE_OUT_OF_MEMORY;
  365. }
  366. sspi_w_token[1].cbBuffer = 1;
  367. sspi_w_token[1].pvBuffer = malloc(1);
  368. if(!sspi_w_token[1].pvBuffer) {
  369. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  370. s_pSecFn->DeleteSecurityContext(&sspi_context);
  371. return CURLE_OUT_OF_MEMORY;
  372. }
  373. memcpy(sspi_w_token[1].pvBuffer, &gss_enc, 1);
  374. sspi_w_token[2].BufferType = SECBUFFER_PADDING;
  375. sspi_w_token[2].cbBuffer = sspi_sizes.cbBlockSize;
  376. sspi_w_token[2].pvBuffer = malloc(sspi_sizes.cbBlockSize);
  377. if(!sspi_w_token[2].pvBuffer) {
  378. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  379. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  380. s_pSecFn->DeleteSecurityContext(&sspi_context);
  381. return CURLE_OUT_OF_MEMORY;
  382. }
  383. status = s_pSecFn->EncryptMessage(&sspi_context,
  384. KERB_WRAP_NO_ENCRYPT,
  385. &wrap_desc,
  386. 0);
  387. if(check_sspi_err(data, status, "EncryptMessage")) {
  388. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  389. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  390. s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  391. s_pSecFn->DeleteSecurityContext(&sspi_context);
  392. failf(data, "Failed to query security context attributes.");
  393. return CURLE_COULDNT_CONNECT;
  394. }
  395. sspi_send_token.cbBuffer = sspi_w_token[0].cbBuffer
  396. + sspi_w_token[1].cbBuffer
  397. + sspi_w_token[2].cbBuffer;
  398. sspi_send_token.pvBuffer = malloc(sspi_send_token.cbBuffer);
  399. if(!sspi_send_token.pvBuffer) {
  400. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  401. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  402. s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  403. s_pSecFn->DeleteSecurityContext(&sspi_context);
  404. return CURLE_OUT_OF_MEMORY;
  405. }
  406. memcpy(sspi_send_token.pvBuffer, sspi_w_token[0].pvBuffer,
  407. sspi_w_token[0].cbBuffer);
  408. memcpy((PUCHAR) sspi_send_token.pvBuffer +(int)sspi_w_token[0].cbBuffer,
  409. sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
  410. memcpy((PUCHAR) sspi_send_token.pvBuffer
  411. + sspi_w_token[0].cbBuffer
  412. + sspi_w_token[1].cbBuffer,
  413. sspi_w_token[2].pvBuffer, sspi_w_token[2].cbBuffer);
  414. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  415. sspi_w_token[0].pvBuffer = NULL;
  416. sspi_w_token[0].cbBuffer = 0;
  417. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  418. sspi_w_token[1].pvBuffer = NULL;
  419. sspi_w_token[1].cbBuffer = 0;
  420. s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  421. sspi_w_token[2].pvBuffer = NULL;
  422. sspi_w_token[2].cbBuffer = 0;
  423. us_length = htons((short)sspi_send_token.cbBuffer);
  424. memcpy(socksreq + 2, &us_length, sizeof(short));
  425. }
  426. written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 4, &code);
  427. if(code || (4 != written)) {
  428. failf(data, "Failed to send SSPI encryption request.");
  429. if(sspi_send_token.pvBuffer)
  430. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  431. s_pSecFn->DeleteSecurityContext(&sspi_context);
  432. return CURLE_COULDNT_CONNECT;
  433. }
  434. if(data->set.socks5_gssapi_nec) {
  435. memcpy(socksreq, &gss_enc, 1);
  436. written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 1, &code);
  437. if(code || (1 != written)) {
  438. failf(data, "Failed to send SSPI encryption type.");
  439. s_pSecFn->DeleteSecurityContext(&sspi_context);
  440. return CURLE_COULDNT_CONNECT;
  441. }
  442. }
  443. else {
  444. written = Curl_conn_cf_send(cf->next, data,
  445. (char *)sspi_send_token.pvBuffer,
  446. sspi_send_token.cbBuffer, &code);
  447. if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
  448. failf(data, "Failed to send SSPI encryption type.");
  449. if(sspi_send_token.pvBuffer)
  450. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  451. s_pSecFn->DeleteSecurityContext(&sspi_context);
  452. return CURLE_COULDNT_CONNECT;
  453. }
  454. if(sspi_send_token.pvBuffer)
  455. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  456. }
  457. result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
  458. if(result || (actualread != 4)) {
  459. failf(data, "Failed to receive SSPI encryption response.");
  460. s_pSecFn->DeleteSecurityContext(&sspi_context);
  461. return CURLE_COULDNT_CONNECT;
  462. }
  463. /* ignore the first (VER) byte */
  464. if(socksreq[1] == 255) { /* status / message type */
  465. failf(data, "User was rejected by the SOCKS5 server (%u %u).",
  466. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  467. s_pSecFn->DeleteSecurityContext(&sspi_context);
  468. return CURLE_COULDNT_CONNECT;
  469. }
  470. if(socksreq[1] != 2) { /* status / message type */
  471. failf(data, "Invalid SSPI encryption response type (%u %u).",
  472. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  473. s_pSecFn->DeleteSecurityContext(&sspi_context);
  474. return CURLE_COULDNT_CONNECT;
  475. }
  476. memcpy(&us_length, socksreq + 2, sizeof(short));
  477. us_length = ntohs(us_length);
  478. sspi_w_token[0].cbBuffer = us_length;
  479. sspi_w_token[0].pvBuffer = malloc(us_length);
  480. if(!sspi_w_token[0].pvBuffer) {
  481. s_pSecFn->DeleteSecurityContext(&sspi_context);
  482. return CURLE_OUT_OF_MEMORY;
  483. }
  484. result = Curl_blockread_all(cf, data, (char *)sspi_w_token[0].pvBuffer,
  485. sspi_w_token[0].cbBuffer, &actualread);
  486. if(result || (actualread != us_length)) {
  487. failf(data, "Failed to receive SSPI encryption type.");
  488. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  489. s_pSecFn->DeleteSecurityContext(&sspi_context);
  490. return CURLE_COULDNT_CONNECT;
  491. }
  492. if(!data->set.socks5_gssapi_nec) {
  493. wrap_desc.cBuffers = 2;
  494. sspi_w_token[0].BufferType = SECBUFFER_STREAM;
  495. sspi_w_token[1].BufferType = SECBUFFER_DATA;
  496. sspi_w_token[1].cbBuffer = 0;
  497. sspi_w_token[1].pvBuffer = NULL;
  498. status = s_pSecFn->DecryptMessage(&sspi_context,
  499. &wrap_desc,
  500. 0,
  501. &qop);
  502. if(check_sspi_err(data, status, "DecryptMessage")) {
  503. if(sspi_w_token[0].pvBuffer)
  504. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  505. if(sspi_w_token[1].pvBuffer)
  506. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  507. s_pSecFn->DeleteSecurityContext(&sspi_context);
  508. failf(data, "Failed to query security context attributes.");
  509. return CURLE_COULDNT_CONNECT;
  510. }
  511. if(sspi_w_token[1].cbBuffer != 1) {
  512. failf(data, "Invalid SSPI encryption response length (%lu).",
  513. (unsigned long)sspi_w_token[1].cbBuffer);
  514. if(sspi_w_token[0].pvBuffer)
  515. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  516. if(sspi_w_token[1].pvBuffer)
  517. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  518. s_pSecFn->DeleteSecurityContext(&sspi_context);
  519. return CURLE_COULDNT_CONNECT;
  520. }
  521. memcpy(socksreq, sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
  522. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  523. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  524. }
  525. else {
  526. if(sspi_w_token[0].cbBuffer != 1) {
  527. failf(data, "Invalid SSPI encryption response length (%lu).",
  528. (unsigned long)sspi_w_token[0].cbBuffer);
  529. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  530. s_pSecFn->DeleteSecurityContext(&sspi_context);
  531. return CURLE_COULDNT_CONNECT;
  532. }
  533. memcpy(socksreq, sspi_w_token[0].pvBuffer, sspi_w_token[0].cbBuffer);
  534. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  535. }
  536. (void)curlx_nonblock(sock, TRUE);
  537. infof(data, "SOCKS5 access with%s protection granted.",
  538. (socksreq[0] == 0)?"out GSS-API data":
  539. ((socksreq[0] == 1)?" GSS-API integrity":" GSS-API confidentiality"));
  540. /* For later use if encryption is required
  541. conn->socks5_gssapi_enctype = socksreq[0];
  542. if(socksreq[0] != 0)
  543. conn->socks5_sspi_context = sspi_context;
  544. else {
  545. s_pSecFn->DeleteSecurityContext(&sspi_context);
  546. conn->socks5_sspi_context = sspi_context;
  547. }
  548. */
  549. return CURLE_OK;
  550. }
  551. #endif