socks_sspi.c 22 KB

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