socks_sspi.c 25 KB

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