socks_sspi.c 22 KB

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