socks_sspi.c 22 KB

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