socks_sspi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. infof(data, "SOCKS5 server authenticated user %s with GSS-API.",
  294. names.sUserName);
  295. s_pSecFn->FreeContextBuffer(names.sUserName);
  296. /* Do encryption */
  297. socksreq[0] = 1; /* GSS-API subnegotiation version */
  298. socksreq[1] = 2; /* encryption message type */
  299. gss_enc = 0; /* no data protection */
  300. /* do confidentiality protection if supported */
  301. if(sspi_ret_flags & ISC_REQ_CONFIDENTIALITY)
  302. gss_enc = 2;
  303. /* else do integrity protection */
  304. else if(sspi_ret_flags & ISC_REQ_INTEGRITY)
  305. gss_enc = 1;
  306. infof(data, "SOCKS5 server supports GSS-API %s data protection.",
  307. (gss_enc == 0)?"no":((gss_enc == 1)?"integrity":"confidentiality") );
  308. /* force to no data protection, avoid encryption/decryption for now */
  309. gss_enc = 0;
  310. /*
  311. * Sending the encryption type in clear seems wrong. It should be
  312. * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
  313. * The NEC reference implementations on which this is based is
  314. * therefore at fault
  315. *
  316. * +------+------+------+.......................+
  317. * + ver | mtyp | len | token |
  318. * +------+------+------+.......................+
  319. * + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
  320. * +------+------+------+.......................+
  321. *
  322. * Where:
  323. *
  324. * - "ver" is the protocol version number, here 1 to represent the
  325. * first version of the SOCKS/GSS-API protocol
  326. *
  327. * - "mtyp" is the message type, here 2 to represent a protection
  328. * -level negotiation message
  329. *
  330. * - "len" is the length of the "token" field in octets
  331. *
  332. * - "token" is the GSS-API encapsulated protection level
  333. *
  334. * The token is produced by encapsulating an octet containing the
  335. * required protection level using gss_seal()/gss_wrap() with conf_req
  336. * set to FALSE. The token is verified using gss_unseal()/
  337. * gss_unwrap().
  338. *
  339. */
  340. if(data->set.socks5_gssapi_nec) {
  341. us_length = htons((short)1);
  342. memcpy(socksreq + 2, &us_length, sizeof(short));
  343. }
  344. else {
  345. status = s_pSecFn->QueryContextAttributes(&sspi_context,
  346. SECPKG_ATTR_SIZES,
  347. &sspi_sizes);
  348. if(check_sspi_err(data, status, "QueryContextAttributes")) {
  349. s_pSecFn->DeleteSecurityContext(&sspi_context);
  350. failf(data, "Failed to query security context attributes.");
  351. return CURLE_COULDNT_CONNECT;
  352. }
  353. sspi_w_token[0].cbBuffer = sspi_sizes.cbSecurityTrailer;
  354. sspi_w_token[0].BufferType = SECBUFFER_TOKEN;
  355. sspi_w_token[0].pvBuffer = malloc(sspi_sizes.cbSecurityTrailer);
  356. if(!sspi_w_token[0].pvBuffer) {
  357. s_pSecFn->DeleteSecurityContext(&sspi_context);
  358. return CURLE_OUT_OF_MEMORY;
  359. }
  360. sspi_w_token[1].cbBuffer = 1;
  361. sspi_w_token[1].pvBuffer = malloc(1);
  362. if(!sspi_w_token[1].pvBuffer) {
  363. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  364. s_pSecFn->DeleteSecurityContext(&sspi_context);
  365. return CURLE_OUT_OF_MEMORY;
  366. }
  367. memcpy(sspi_w_token[1].pvBuffer, &gss_enc, 1);
  368. sspi_w_token[2].BufferType = SECBUFFER_PADDING;
  369. sspi_w_token[2].cbBuffer = sspi_sizes.cbBlockSize;
  370. sspi_w_token[2].pvBuffer = malloc(sspi_sizes.cbBlockSize);
  371. if(!sspi_w_token[2].pvBuffer) {
  372. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  373. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  374. s_pSecFn->DeleteSecurityContext(&sspi_context);
  375. return CURLE_OUT_OF_MEMORY;
  376. }
  377. status = s_pSecFn->EncryptMessage(&sspi_context,
  378. KERB_WRAP_NO_ENCRYPT,
  379. &wrap_desc,
  380. 0);
  381. if(check_sspi_err(data, status, "EncryptMessage")) {
  382. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  383. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  384. s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  385. s_pSecFn->DeleteSecurityContext(&sspi_context);
  386. failf(data, "Failed to query security context attributes.");
  387. return CURLE_COULDNT_CONNECT;
  388. }
  389. sspi_send_token.cbBuffer = sspi_w_token[0].cbBuffer
  390. + sspi_w_token[1].cbBuffer
  391. + sspi_w_token[2].cbBuffer;
  392. sspi_send_token.pvBuffer = malloc(sspi_send_token.cbBuffer);
  393. if(!sspi_send_token.pvBuffer) {
  394. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  395. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  396. s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  397. s_pSecFn->DeleteSecurityContext(&sspi_context);
  398. return CURLE_OUT_OF_MEMORY;
  399. }
  400. memcpy(sspi_send_token.pvBuffer, sspi_w_token[0].pvBuffer,
  401. sspi_w_token[0].cbBuffer);
  402. memcpy((PUCHAR) sspi_send_token.pvBuffer +(int)sspi_w_token[0].cbBuffer,
  403. sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
  404. memcpy((PUCHAR) sspi_send_token.pvBuffer
  405. + sspi_w_token[0].cbBuffer
  406. + sspi_w_token[1].cbBuffer,
  407. sspi_w_token[2].pvBuffer, sspi_w_token[2].cbBuffer);
  408. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  409. sspi_w_token[0].pvBuffer = NULL;
  410. sspi_w_token[0].cbBuffer = 0;
  411. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  412. sspi_w_token[1].pvBuffer = NULL;
  413. sspi_w_token[1].cbBuffer = 0;
  414. s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
  415. sspi_w_token[2].pvBuffer = NULL;
  416. sspi_w_token[2].cbBuffer = 0;
  417. us_length = htons((short)sspi_send_token.cbBuffer);
  418. memcpy(socksreq + 2, &us_length, sizeof(short));
  419. }
  420. written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 4, &code);
  421. if(code || (4 != written)) {
  422. failf(data, "Failed to send SSPI encryption request.");
  423. if(sspi_send_token.pvBuffer)
  424. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  425. s_pSecFn->DeleteSecurityContext(&sspi_context);
  426. return CURLE_COULDNT_CONNECT;
  427. }
  428. if(data->set.socks5_gssapi_nec) {
  429. memcpy(socksreq, &gss_enc, 1);
  430. written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 1, &code);
  431. if(code || (1 != written)) {
  432. failf(data, "Failed to send SSPI encryption type.");
  433. s_pSecFn->DeleteSecurityContext(&sspi_context);
  434. return CURLE_COULDNT_CONNECT;
  435. }
  436. }
  437. else {
  438. written = Curl_conn_cf_send(cf->next, data,
  439. (char *)sspi_send_token.pvBuffer,
  440. sspi_send_token.cbBuffer, &code);
  441. if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
  442. failf(data, "Failed to send SSPI encryption type.");
  443. if(sspi_send_token.pvBuffer)
  444. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  445. s_pSecFn->DeleteSecurityContext(&sspi_context);
  446. return CURLE_COULDNT_CONNECT;
  447. }
  448. if(sspi_send_token.pvBuffer)
  449. s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
  450. }
  451. result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
  452. if(result || (actualread != 4)) {
  453. failf(data, "Failed to receive SSPI encryption response.");
  454. s_pSecFn->DeleteSecurityContext(&sspi_context);
  455. return CURLE_COULDNT_CONNECT;
  456. }
  457. /* ignore the first (VER) byte */
  458. if(socksreq[1] == 255) { /* status / message type */
  459. failf(data, "User was rejected by the SOCKS5 server (%u %u).",
  460. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  461. s_pSecFn->DeleteSecurityContext(&sspi_context);
  462. return CURLE_COULDNT_CONNECT;
  463. }
  464. if(socksreq[1] != 2) { /* status / message type */
  465. failf(data, "Invalid SSPI encryption response type (%u %u).",
  466. (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
  467. s_pSecFn->DeleteSecurityContext(&sspi_context);
  468. return CURLE_COULDNT_CONNECT;
  469. }
  470. memcpy(&us_length, socksreq + 2, sizeof(short));
  471. us_length = ntohs(us_length);
  472. sspi_w_token[0].cbBuffer = us_length;
  473. sspi_w_token[0].pvBuffer = malloc(us_length);
  474. if(!sspi_w_token[0].pvBuffer) {
  475. s_pSecFn->DeleteSecurityContext(&sspi_context);
  476. return CURLE_OUT_OF_MEMORY;
  477. }
  478. result = Curl_blockread_all(cf, data, (char *)sspi_w_token[0].pvBuffer,
  479. sspi_w_token[0].cbBuffer, &actualread);
  480. if(result || (actualread != us_length)) {
  481. failf(data, "Failed to receive SSPI encryption type.");
  482. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  483. s_pSecFn->DeleteSecurityContext(&sspi_context);
  484. return CURLE_COULDNT_CONNECT;
  485. }
  486. if(!data->set.socks5_gssapi_nec) {
  487. wrap_desc.cBuffers = 2;
  488. sspi_w_token[0].BufferType = SECBUFFER_STREAM;
  489. sspi_w_token[1].BufferType = SECBUFFER_DATA;
  490. sspi_w_token[1].cbBuffer = 0;
  491. sspi_w_token[1].pvBuffer = NULL;
  492. status = s_pSecFn->DecryptMessage(&sspi_context,
  493. &wrap_desc,
  494. 0,
  495. &qop);
  496. if(check_sspi_err(data, status, "DecryptMessage")) {
  497. if(sspi_w_token[0].pvBuffer)
  498. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  499. if(sspi_w_token[1].pvBuffer)
  500. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  501. s_pSecFn->DeleteSecurityContext(&sspi_context);
  502. failf(data, "Failed to query security context attributes.");
  503. return CURLE_COULDNT_CONNECT;
  504. }
  505. if(sspi_w_token[1].cbBuffer != 1) {
  506. failf(data, "Invalid SSPI encryption response length (%lu).",
  507. (unsigned long)sspi_w_token[1].cbBuffer);
  508. if(sspi_w_token[0].pvBuffer)
  509. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  510. if(sspi_w_token[1].pvBuffer)
  511. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  512. s_pSecFn->DeleteSecurityContext(&sspi_context);
  513. return CURLE_COULDNT_CONNECT;
  514. }
  515. memcpy(socksreq, sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
  516. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  517. s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
  518. }
  519. else {
  520. if(sspi_w_token[0].cbBuffer != 1) {
  521. failf(data, "Invalid SSPI encryption response length (%lu).",
  522. (unsigned long)sspi_w_token[0].cbBuffer);
  523. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  524. s_pSecFn->DeleteSecurityContext(&sspi_context);
  525. return CURLE_COULDNT_CONNECT;
  526. }
  527. memcpy(socksreq, sspi_w_token[0].pvBuffer, sspi_w_token[0].cbBuffer);
  528. s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
  529. }
  530. (void)curlx_nonblock(sock, TRUE);
  531. infof(data, "SOCKS5 access with%s protection granted.",
  532. (socksreq[0] == 0)?"out GSS-API data":
  533. ((socksreq[0] == 1)?" GSS-API integrity":" GSS-API confidentiality"));
  534. /* For later use if encryption is required
  535. conn->socks5_gssapi_enctype = socksreq[0];
  536. if(socksreq[0] != 0)
  537. conn->socks5_sspi_context = sspi_context;
  538. else {
  539. s_pSecFn->DeleteSecurityContext(&sspi_context);
  540. conn->socks5_sspi_context = sspi_context;
  541. }
  542. */
  543. return CURLE_OK;
  544. }
  545. #endif