socks_sspi.c 22 KB

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