2
0

curl_sasl_sspi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014, Steve Holme, <steve_holme@hotmail.com>.
  9. * Copyright (C) 2014, 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. * RFC2831 DIGEST-MD5 authentication
  23. * RFC4422 Simple Authentication and Security Layer (SASL)
  24. * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
  25. *
  26. ***************************************************************************/
  27. #include "curl_setup.h"
  28. #if defined(USE_WINDOWS_SSPI)
  29. #include <curl/curl.h>
  30. #include "curl_sasl.h"
  31. #include "urldata.h"
  32. #include "curl_base64.h"
  33. #include "warnless.h"
  34. #include "curl_memory.h"
  35. #include "curl_multibyte.h"
  36. #define _MPRINTF_REPLACE /* use our functions only */
  37. #include <curl/mprintf.h>
  38. /* The last #include file should be: */
  39. #include "memdebug.h"
  40. #if defined(USE_KRB5)
  41. void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5);
  42. #endif
  43. /*
  44. * Curl_sasl_build_spn()
  45. *
  46. * This is used to build a SPN string in the format service/host.
  47. *
  48. * Parameters:
  49. *
  50. * serivce [in] - The service type such as www, smtp, pop or imap.
  51. * instance [in] - The instance name such as the host nme or realm.
  52. *
  53. * Returns a pointer to the newly allocated SPN.
  54. */
  55. TCHAR *Curl_sasl_build_spn(const char *service, const char *host)
  56. {
  57. char *utf8_spn = NULL;
  58. TCHAR *tchar_spn = NULL;
  59. /* Note: We could use DsMakeSPN() or DsClientMakeSpnForTargetServer() rather
  60. than doing this ourselves but the first is only available in Windows XP
  61. and Windows Server 2003 and the latter is only available in Windows 2000
  62. but not Windows95/98/ME or Windows NT4.0 unless the Active Directory
  63. Client Extensions are installed. As such it is far simpler for us to
  64. formulate the SPN instead. */
  65. /* Allocate our UTF8 based SPN */
  66. utf8_spn = aprintf("%s/%s", service, host);
  67. if(!utf8_spn) {
  68. return NULL;
  69. }
  70. /* Allocate our TCHAR based SPN */
  71. tchar_spn = Curl_convert_UTF8_to_tchar(utf8_spn);
  72. if(!tchar_spn) {
  73. Curl_safefree(utf8_spn);
  74. return NULL;
  75. }
  76. /* Release the UTF8 variant when operating with Unicode */
  77. Curl_unicodefree(utf8_spn);
  78. /* Return our newly allocated SPN */
  79. return tchar_spn;
  80. }
  81. #if !defined(CURL_DISABLE_CRYPTO_AUTH)
  82. /*
  83. * Curl_sasl_create_digest_md5_message()
  84. *
  85. * This is used to generate an already encoded DIGEST-MD5 response message
  86. * ready for sending to the recipient.
  87. *
  88. * Parameters:
  89. *
  90. * data [in] - The session handle.
  91. * chlg64 [in] - Pointer to the base64 encoded challenge message.
  92. * userp [in] - The user name.
  93. * passdwp [in] - The user's password.
  94. * service [in] - The service type such as www, smtp, pop or imap.
  95. * outptr [in/out] - The address where a pointer to newly allocated memory
  96. * holding the result will be stored upon completion.
  97. * outlen [out] - The length of the output message.
  98. *
  99. * Returns CURLE_OK on success.
  100. */
  101. CURLcode Curl_sasl_create_digest_md5_message(struct SessionHandle *data,
  102. const char *chlg64,
  103. const char *userp,
  104. const char *passwdp,
  105. const char *service,
  106. char **outptr, size_t *outlen)
  107. {
  108. CURLcode result = CURLE_OK;
  109. TCHAR *spn = NULL;
  110. size_t chlglen = 0;
  111. size_t resp_max = 0;
  112. unsigned char *chlg = NULL;
  113. unsigned char *resp = NULL;
  114. CredHandle handle;
  115. CtxtHandle ctx;
  116. PSecPkgInfo SecurityPackage;
  117. SEC_WINNT_AUTH_IDENTITY identity;
  118. SEC_WINNT_AUTH_IDENTITY *p_identity;
  119. SecBuffer chlg_buf;
  120. SecBuffer resp_buf;
  121. SecBufferDesc chlg_desc;
  122. SecBufferDesc resp_desc;
  123. SECURITY_STATUS status;
  124. unsigned long attrs;
  125. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  126. /* Decode the base-64 encoded challenge message */
  127. if(strlen(chlg64) && *chlg64 != '=') {
  128. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  129. if(result)
  130. return result;
  131. }
  132. /* Ensure we have a valid challenge message */
  133. if(!chlg)
  134. return CURLE_BAD_CONTENT_ENCODING;
  135. /* Query the security package for DigestSSP */
  136. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
  137. &SecurityPackage);
  138. if(status != SEC_E_OK) {
  139. Curl_safefree(chlg);
  140. return CURLE_NOT_BUILT_IN;
  141. }
  142. resp_max = SecurityPackage->cbMaxToken;
  143. /* Release the package buffer as it is not required anymore */
  144. s_pSecFn->FreeContextBuffer(SecurityPackage);
  145. /* Allocate our response buffer */
  146. resp = malloc(resp_max);
  147. if(!resp) {
  148. Curl_safefree(chlg);
  149. return CURLE_OUT_OF_MEMORY;
  150. }
  151. /* Generate our SPN */
  152. spn = Curl_sasl_build_spn(service, data->easy_conn->host.name);
  153. if(!spn) {
  154. Curl_safefree(resp);
  155. Curl_safefree(chlg);
  156. return CURLE_OUT_OF_MEMORY;
  157. }
  158. if(userp && *userp) {
  159. /* Populate our identity structure */
  160. result = Curl_create_sspi_identity(userp, passwdp, &identity);
  161. if(result) {
  162. Curl_safefree(spn);
  163. Curl_safefree(resp);
  164. Curl_safefree(chlg);
  165. return result;
  166. }
  167. /* Allow proper cleanup of the identity structure */
  168. p_identity = &identity;
  169. }
  170. else
  171. /* Use the current Windows user */
  172. p_identity = NULL;
  173. /* Acquire our credentials handle */
  174. status = s_pSecFn->AcquireCredentialsHandle(NULL,
  175. (TCHAR *) TEXT(SP_NAME_DIGEST),
  176. SECPKG_CRED_OUTBOUND, NULL,
  177. p_identity, NULL, NULL,
  178. &handle, &expiry);
  179. if(status != SEC_E_OK) {
  180. Curl_sspi_free_identity(p_identity);
  181. Curl_safefree(spn);
  182. Curl_safefree(resp);
  183. Curl_safefree(chlg);
  184. return CURLE_LOGIN_DENIED;
  185. }
  186. /* Setup the challenge "input" security buffer */
  187. chlg_desc.ulVersion = SECBUFFER_VERSION;
  188. chlg_desc.cBuffers = 1;
  189. chlg_desc.pBuffers = &chlg_buf;
  190. chlg_buf.BufferType = SECBUFFER_TOKEN;
  191. chlg_buf.pvBuffer = chlg;
  192. chlg_buf.cbBuffer = curlx_uztoul(chlglen);
  193. /* Setup the response "output" security buffer */
  194. resp_desc.ulVersion = SECBUFFER_VERSION;
  195. resp_desc.cBuffers = 1;
  196. resp_desc.pBuffers = &resp_buf;
  197. resp_buf.BufferType = SECBUFFER_TOKEN;
  198. resp_buf.pvBuffer = resp;
  199. resp_buf.cbBuffer = curlx_uztoul(resp_max);
  200. /* Generate our challenge-response message */
  201. status = s_pSecFn->InitializeSecurityContext(&handle, NULL, spn, 0, 0, 0,
  202. &chlg_desc, 0, &ctx,
  203. &resp_desc, &attrs, &expiry);
  204. if(status == SEC_I_COMPLETE_NEEDED ||
  205. status == SEC_I_COMPLETE_AND_CONTINUE)
  206. s_pSecFn->CompleteAuthToken(&handle, &resp_desc);
  207. else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  208. s_pSecFn->FreeCredentialsHandle(&handle);
  209. Curl_sspi_free_identity(p_identity);
  210. Curl_safefree(spn);
  211. Curl_safefree(resp);
  212. Curl_safefree(chlg);
  213. return CURLE_RECV_ERROR;
  214. }
  215. /* Base64 encode the response */
  216. result = Curl_base64_encode(data, (char *)resp, resp_buf.cbBuffer, outptr,
  217. outlen);
  218. /* Free our handles */
  219. s_pSecFn->DeleteSecurityContext(&ctx);
  220. s_pSecFn->FreeCredentialsHandle(&handle);
  221. /* Free the identity structure */
  222. Curl_sspi_free_identity(p_identity);
  223. /* Free the SPN */
  224. Curl_safefree(spn);
  225. /* Free the response buffer */
  226. Curl_safefree(resp);
  227. /* Free the decoded challenge message */
  228. Curl_safefree(chlg);
  229. return result;
  230. }
  231. #endif /* !CURL_DISABLE_CRYPTO_AUTH */
  232. #if defined(USE_KRB5)
  233. /*
  234. * Curl_sasl_create_gssapi_user_message()
  235. *
  236. * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
  237. * message ready for sending to the recipient.
  238. *
  239. * Parameters:
  240. *
  241. * data [in] - The session handle.
  242. * userp [in] - The user name.
  243. * passdwp [in] - The user's password.
  244. * service [in] - The service type such as www, smtp, pop or imap.
  245. * mutual_auth [in] - Flag specifing whether or not mutual authentication
  246. * is enabled.
  247. * chlg64 [in] - Pointer to the optional base64 encoded challenge
  248. * message.
  249. * krb5 [in/out] - The gssapi data struct being used and modified.
  250. * outptr [in/out] - The address where a pointer to newly allocated memory
  251. * holding the result will be stored upon completion.
  252. * outlen [out] - The length of the output message.
  253. *
  254. * Returns CURLE_OK on success.
  255. */
  256. CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
  257. const char *userp,
  258. const char *passwdp,
  259. const char *service,
  260. const bool mutual_auth,
  261. const char *chlg64,
  262. struct kerberos5data *krb5,
  263. char **outptr, size_t *outlen)
  264. {
  265. CURLcode result = CURLE_OK;
  266. size_t chlglen = 0;
  267. unsigned char *chlg = NULL;
  268. CtxtHandle context;
  269. PSecPkgInfo SecurityPackage;
  270. SecBuffer chlg_buf;
  271. SecBuffer resp_buf;
  272. SecBufferDesc chlg_desc;
  273. SecBufferDesc resp_desc;
  274. SECURITY_STATUS status;
  275. unsigned long attrs;
  276. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  277. if(!krb5->credentials) {
  278. /* Query the security package for Kerberos */
  279. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
  280. TEXT(SP_NAME_KERBEROS),
  281. &SecurityPackage);
  282. if(status != SEC_E_OK) {
  283. return CURLE_NOT_BUILT_IN;
  284. }
  285. krb5->token_max = SecurityPackage->cbMaxToken;
  286. /* Release the package buffer as it is not required anymore */
  287. s_pSecFn->FreeContextBuffer(SecurityPackage);
  288. /* Allocate our response buffer */
  289. krb5->output_token = malloc(krb5->token_max);
  290. if(!krb5->output_token)
  291. return CURLE_OUT_OF_MEMORY;
  292. /* Generate our SPN */
  293. krb5->spn = Curl_sasl_build_spn(service, data->easy_conn->host.name);
  294. if(!krb5->spn)
  295. return CURLE_OUT_OF_MEMORY;
  296. if(userp && *userp) {
  297. /* Populate our identity structure */
  298. result = Curl_create_sspi_identity(userp, passwdp, &krb5->identity);
  299. if(result)
  300. return result;
  301. /* Allow proper cleanup of the identity structure */
  302. krb5->p_identity = &krb5->identity;
  303. }
  304. else
  305. /* Use the current Windows user */
  306. krb5->p_identity = NULL;
  307. /* Allocate our credentials handle */
  308. krb5->credentials = malloc(sizeof(CredHandle));
  309. if(!krb5->credentials)
  310. return CURLE_OUT_OF_MEMORY;
  311. memset(krb5->credentials, 0, sizeof(CredHandle));
  312. /* Acquire our credentials handle */
  313. status = s_pSecFn->AcquireCredentialsHandle(NULL,
  314. (TCHAR *)
  315. TEXT(SP_NAME_KERBEROS),
  316. SECPKG_CRED_OUTBOUND, NULL,
  317. krb5->p_identity, NULL, NULL,
  318. krb5->credentials, &expiry);
  319. if(status != SEC_E_OK)
  320. return CURLE_LOGIN_DENIED;
  321. /* Allocate our new context handle */
  322. krb5->context = malloc(sizeof(CtxtHandle));
  323. if(!krb5->context)
  324. return CURLE_OUT_OF_MEMORY;
  325. memset(krb5->context, 0, sizeof(CtxtHandle));
  326. }
  327. else {
  328. /* Decode the base-64 encoded challenge message */
  329. if(strlen(chlg64) && *chlg64 != '=') {
  330. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  331. if(result)
  332. return result;
  333. }
  334. /* Ensure we have a valid challenge message */
  335. if(!chlg)
  336. return CURLE_BAD_CONTENT_ENCODING;
  337. /* Setup the challenge "input" security buffer */
  338. chlg_desc.ulVersion = SECBUFFER_VERSION;
  339. chlg_desc.cBuffers = 1;
  340. chlg_desc.pBuffers = &chlg_buf;
  341. chlg_buf.BufferType = SECBUFFER_TOKEN;
  342. chlg_buf.pvBuffer = chlg;
  343. chlg_buf.cbBuffer = curlx_uztoul(chlglen);
  344. }
  345. /* Setup the response "output" security buffer */
  346. resp_desc.ulVersion = SECBUFFER_VERSION;
  347. resp_desc.cBuffers = 1;
  348. resp_desc.pBuffers = &resp_buf;
  349. resp_buf.BufferType = SECBUFFER_TOKEN;
  350. resp_buf.pvBuffer = krb5->output_token;
  351. resp_buf.cbBuffer = curlx_uztoul(krb5->token_max);
  352. /* Generate our challenge-response message */
  353. status = s_pSecFn->InitializeSecurityContext(krb5->credentials,
  354. chlg ? krb5->context : NULL,
  355. krb5->spn,
  356. (mutual_auth ?
  357. ISC_REQ_MUTUAL_AUTH : 0),
  358. 0, SECURITY_NATIVE_DREP,
  359. chlg ? &chlg_desc : NULL, 0,
  360. &context,
  361. &resp_desc, &attrs,
  362. &expiry);
  363. if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  364. Curl_safefree(chlg);
  365. return CURLE_RECV_ERROR;
  366. }
  367. if(memcmp(&context, krb5->context, sizeof(context))) {
  368. s_pSecFn->DeleteSecurityContext(krb5->context);
  369. memcpy(krb5->context, &context, sizeof(context));
  370. }
  371. if(resp_buf.cbBuffer) {
  372. /* Base64 encode the response */
  373. result = Curl_base64_encode(data, (char *)resp_buf.pvBuffer,
  374. resp_buf.cbBuffer, outptr, outlen);
  375. }
  376. /* Free the decoded challenge */
  377. Curl_safefree(chlg);
  378. return result;
  379. }
  380. /*
  381. * Curl_sasl_create_gssapi_security_message()
  382. *
  383. * This is used to generate an already encoded GSSAPI (Kerberos V5) security
  384. * token message ready for sending to the recipient.
  385. *
  386. * Parameters:
  387. *
  388. * data [in] - The session handle.
  389. * chlg64 [in] - Pointer to the optional base64 encoded challenge message.
  390. * krb5 [in/out] - The gssapi data struct being used and modified.
  391. * outptr [in/out] - The address where a pointer to newly allocated memory
  392. * holding the result will be stored upon completion.
  393. * outlen [out] - The length of the output message.
  394. *
  395. * Returns CURLE_OK on success.
  396. */
  397. CURLcode Curl_sasl_create_gssapi_security_message(struct SessionHandle *data,
  398. const char *chlg64,
  399. struct kerberos5data *krb5,
  400. char **outptr,
  401. size_t *outlen)
  402. {
  403. CURLcode result = CURLE_OK;
  404. size_t offset = 0;
  405. size_t chlglen = 0;
  406. size_t messagelen = 0;
  407. size_t appdatalen = 0;
  408. unsigned char *chlg = NULL;
  409. unsigned char *trailer = NULL;
  410. unsigned char *message = NULL;
  411. unsigned char *padding = NULL;
  412. unsigned char *appdata = NULL;
  413. SecBuffer input_buf[2];
  414. SecBuffer wrap_buf[3];
  415. SecBufferDesc input_desc;
  416. SecBufferDesc wrap_desc;
  417. unsigned long indata = 0;
  418. unsigned long outdata = 0;
  419. unsigned long qop = 0;
  420. unsigned long sec_layer = 0;
  421. unsigned long max_size = 0;
  422. SecPkgContext_Sizes sizes;
  423. SecPkgCredentials_Names names;
  424. SECURITY_STATUS status;
  425. char *user_name;
  426. /* Decode the base-64 encoded input message */
  427. if(strlen(chlg64) && *chlg64 != '=') {
  428. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  429. if(result)
  430. return result;
  431. }
  432. /* Ensure we have a valid challenge message */
  433. if(!chlg)
  434. return CURLE_BAD_CONTENT_ENCODING;
  435. /* Get our response size information */
  436. status = s_pSecFn->QueryContextAttributes(krb5->context,
  437. SECPKG_ATTR_SIZES,
  438. &sizes);
  439. if(status != SEC_E_OK) {
  440. Curl_safefree(chlg);
  441. return CURLE_OUT_OF_MEMORY;
  442. }
  443. /* Get the fully qualified username back from the context */
  444. status = s_pSecFn->QueryCredentialsAttributes(krb5->credentials,
  445. SECPKG_CRED_ATTR_NAMES,
  446. &names);
  447. if(status != SEC_E_OK) {
  448. Curl_safefree(chlg);
  449. return CURLE_RECV_ERROR;
  450. }
  451. /* Setup the "input" security buffer */
  452. input_desc.ulVersion = SECBUFFER_VERSION;
  453. input_desc.cBuffers = 2;
  454. input_desc.pBuffers = input_buf;
  455. input_buf[0].BufferType = SECBUFFER_STREAM;
  456. input_buf[0].pvBuffer = chlg;
  457. input_buf[0].cbBuffer = curlx_uztoul(chlglen);
  458. input_buf[1].BufferType = SECBUFFER_DATA;
  459. input_buf[1].pvBuffer = NULL;
  460. input_buf[1].cbBuffer = 0;
  461. /* Decrypt in the inbound challenge obtaining the qop */
  462. status = s_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop);
  463. if(status != SEC_E_OK) {
  464. Curl_safefree(chlg);
  465. return CURLE_BAD_CONTENT_ENCODING;
  466. }
  467. /* Not 4 octets long to fail as per RFC4752 Section 3.1 */
  468. if(input_buf[1].cbBuffer != 4) {
  469. Curl_safefree(chlg);
  470. return CURLE_BAD_CONTENT_ENCODING;
  471. }
  472. /* Copy the data out into a coinput_bufnvenient variable and free the SSPI
  473. allocated buffer as it is not required anymore */
  474. memcpy(&indata, input_buf[1].pvBuffer, 4);
  475. s_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer);
  476. /* Extract the security layer */
  477. sec_layer = indata & 0x000000FF;
  478. if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) {
  479. Curl_safefree(chlg);
  480. return CURLE_BAD_CONTENT_ENCODING;
  481. }
  482. /* Extract the maximum message size the server can receive */
  483. max_size = ntohl(indata & 0xFFFFFF00);
  484. if(max_size > 0) {
  485. /* The server has told us it supports a maximum receive buffer, however, as
  486. we don't require one unless we are encrypting data we, tell the server
  487. our receive buffer is zero. */
  488. max_size = 0;
  489. }
  490. outdata = htonl(max_size) | sec_layer;
  491. /* Allocate the trailer */
  492. trailer = malloc(sizes.cbSecurityTrailer);
  493. if(!trailer) {
  494. Curl_safefree(chlg);
  495. return CURLE_OUT_OF_MEMORY;
  496. }
  497. /* Convert the user name to UTF8 when operating with Unicode */
  498. user_name = Curl_convert_tchar_to_UTF8(names.sUserName);
  499. if(!user_name) {
  500. Curl_safefree(trailer);
  501. Curl_safefree(chlg);
  502. return CURLE_OUT_OF_MEMORY;
  503. }
  504. /* Allocate our message */
  505. messagelen = 4 + strlen(user_name) + 1;
  506. message = malloc(messagelen);
  507. if(!message) {
  508. Curl_safefree(trailer);
  509. Curl_safefree(chlg);
  510. Curl_unicodefree(user_name);
  511. return CURLE_OUT_OF_MEMORY;
  512. }
  513. /* Populate the message with the security layer, client supported receive
  514. message size and authorization identity including the 0x00 based
  515. terminator. Note: Dispite RFC4752 Section 3.1 stating "The authorization
  516. identity is not terminated with the zero-valued (%x00) octet." it seems
  517. necessary to include it. */
  518. memcpy(message, &outdata, 4);
  519. strcpy((char *)message + 4, user_name);
  520. Curl_unicodefree(user_name);
  521. /* Allocate the padding */
  522. padding = malloc(sizes.cbBlockSize);
  523. if(!padding) {
  524. Curl_safefree(message);
  525. Curl_safefree(trailer);
  526. Curl_safefree(chlg);
  527. return CURLE_OUT_OF_MEMORY;
  528. }
  529. /* Setup the "authentication data" security buffer */
  530. wrap_desc.ulVersion = SECBUFFER_VERSION;
  531. wrap_desc.cBuffers = 3;
  532. wrap_desc.pBuffers = wrap_buf;
  533. wrap_buf[0].BufferType = SECBUFFER_TOKEN;
  534. wrap_buf[0].pvBuffer = trailer;
  535. wrap_buf[0].cbBuffer = sizes.cbSecurityTrailer;
  536. wrap_buf[1].BufferType = SECBUFFER_DATA;
  537. wrap_buf[1].pvBuffer = message;
  538. wrap_buf[1].cbBuffer = curlx_uztoul(messagelen);
  539. wrap_buf[2].BufferType = SECBUFFER_PADDING;
  540. wrap_buf[2].pvBuffer = padding;
  541. wrap_buf[2].cbBuffer = sizes.cbBlockSize;
  542. /* Encrypt the data */
  543. status = s_pSecFn->EncryptMessage(krb5->context, KERB_WRAP_NO_ENCRYPT,
  544. &wrap_desc, 0);
  545. if(status != SEC_E_OK) {
  546. Curl_safefree(padding);
  547. Curl_safefree(message);
  548. Curl_safefree(trailer);
  549. Curl_safefree(chlg);
  550. return CURLE_OUT_OF_MEMORY;
  551. }
  552. /* Allocate the encryption (wrap) buffer */
  553. appdatalen = wrap_buf[0].cbBuffer + wrap_buf[1].cbBuffer +
  554. wrap_buf[2].cbBuffer;
  555. appdata = malloc(appdatalen);
  556. if(!appdata) {
  557. Curl_safefree(padding);
  558. Curl_safefree(message);
  559. Curl_safefree(trailer);
  560. Curl_safefree(chlg);
  561. return CURLE_OUT_OF_MEMORY;
  562. }
  563. /* Populate the encryption buffer */
  564. memcpy(appdata, wrap_buf[0].pvBuffer, wrap_buf[0].cbBuffer);
  565. offset += wrap_buf[0].cbBuffer;
  566. memcpy(appdata + offset, wrap_buf[1].pvBuffer, wrap_buf[1].cbBuffer);
  567. offset += wrap_buf[1].cbBuffer;
  568. memcpy(appdata + offset, wrap_buf[2].pvBuffer, wrap_buf[2].cbBuffer);
  569. /* Base64 encode the response */
  570. result = Curl_base64_encode(data, (char *)appdata, appdatalen, outptr,
  571. outlen);
  572. /* Free all of our local buffers */
  573. Curl_safefree(appdata);
  574. Curl_safefree(padding);
  575. Curl_safefree(message);
  576. Curl_safefree(trailer);
  577. Curl_safefree(chlg);
  578. return result;
  579. }
  580. void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5)
  581. {
  582. /* Free our security context */
  583. if(krb5->context) {
  584. s_pSecFn->DeleteSecurityContext(krb5->context);
  585. free(krb5->context);
  586. krb5->context = NULL;
  587. }
  588. /* Free our credentials handle */
  589. if(krb5->credentials) {
  590. s_pSecFn->FreeCredentialsHandle(krb5->credentials);
  591. free(krb5->credentials);
  592. krb5->credentials = NULL;
  593. }
  594. /* Free our identity */
  595. Curl_sspi_free_identity(krb5->p_identity);
  596. krb5->p_identity = NULL;
  597. /* Free the SPN and output token */
  598. Curl_safefree(krb5->spn);
  599. Curl_safefree(krb5->output_token);
  600. /* Reset any variables */
  601. krb5->token_max = 0;
  602. }
  603. #endif /* USE_KRB5 */
  604. #endif /* USE_WINDOWS_SSPI */