socks_sspi.c 25 KB

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