socks_sspi.c 23 KB

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