socks_sspi.c 22 KB

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