socks_sspi.c 22 KB

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