socks_sspi.c 24 KB

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