krb5_sspi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014 - 2019, Steve Holme, <[email protected]>.
  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 https://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. * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(USE_WINDOWS_SSPI) && defined(USE_KERBEROS5)
  26. #include <curl/curl.h>
  27. #include "vauth/vauth.h"
  28. #include "urldata.h"
  29. #include "curl_base64.h"
  30. #include "warnless.h"
  31. #include "curl_multibyte.h"
  32. #include "sendf.h"
  33. /* The last #include files should be: */
  34. #include "curl_memory.h"
  35. #include "memdebug.h"
  36. /*
  37. * Curl_auth_is_gssapi_supported()
  38. *
  39. * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
  40. *
  41. * Parameters: None
  42. *
  43. * Returns TRUE if Kerberos V5 is supported by Windows SSPI.
  44. */
  45. bool Curl_auth_is_gssapi_supported(void)
  46. {
  47. PSecPkgInfo SecurityPackage;
  48. SECURITY_STATUS status;
  49. /* Query the security package for Kerberos */
  50. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
  51. TEXT(SP_NAME_KERBEROS),
  52. &SecurityPackage);
  53. /* Release the package buffer as it is not required anymore */
  54. if(status == SEC_E_OK) {
  55. s_pSecFn->FreeContextBuffer(SecurityPackage);
  56. }
  57. return (status == SEC_E_OK ? TRUE : FALSE);
  58. }
  59. /*
  60. * Curl_auth_create_gssapi_user_message()
  61. *
  62. * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
  63. * message ready for sending to the recipient.
  64. *
  65. * Parameters:
  66. *
  67. * data [in] - The session handle.
  68. * userp [in] - The user name in the format User or Domain\User.
  69. * passwdp [in] - The user's password.
  70. * service [in] - The service type such as http, smtp, pop or imap.
  71. * host [in] - The host name.
  72. * mutual_auth [in] - Flag specifying whether or not mutual authentication
  73. * is enabled.
  74. * chlg64 [in] - The optional base64 encoded challenge message.
  75. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  76. * outptr [in/out] - The address where a pointer to newly allocated memory
  77. * holding the result will be stored upon completion.
  78. * outlen [out] - The length of the output message.
  79. *
  80. * Returns CURLE_OK on success.
  81. */
  82. CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
  83. const char *userp,
  84. const char *passwdp,
  85. const char *service,
  86. const char *host,
  87. const bool mutual_auth,
  88. const char *chlg64,
  89. struct kerberos5data *krb5,
  90. char **outptr, size_t *outlen)
  91. {
  92. CURLcode result = CURLE_OK;
  93. size_t chlglen = 0;
  94. unsigned char *chlg = NULL;
  95. CtxtHandle context;
  96. PSecPkgInfo SecurityPackage;
  97. SecBuffer chlg_buf;
  98. SecBuffer resp_buf;
  99. SecBufferDesc chlg_desc;
  100. SecBufferDesc resp_desc;
  101. SECURITY_STATUS status;
  102. unsigned long attrs;
  103. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  104. if(!krb5->spn) {
  105. /* Generate our SPN */
  106. krb5->spn = Curl_auth_build_spn(service, host, NULL);
  107. if(!krb5->spn)
  108. return CURLE_OUT_OF_MEMORY;
  109. }
  110. if(!krb5->output_token) {
  111. /* Query the security package for Kerberos */
  112. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
  113. TEXT(SP_NAME_KERBEROS),
  114. &SecurityPackage);
  115. if(status != SEC_E_OK) {
  116. return CURLE_NOT_BUILT_IN;
  117. }
  118. krb5->token_max = SecurityPackage->cbMaxToken;
  119. /* Release the package buffer as it is not required anymore */
  120. s_pSecFn->FreeContextBuffer(SecurityPackage);
  121. /* Allocate our response buffer */
  122. krb5->output_token = malloc(krb5->token_max);
  123. if(!krb5->output_token)
  124. return CURLE_OUT_OF_MEMORY;
  125. }
  126. if(!krb5->credentials) {
  127. /* Do we have credentials to use or are we using single sign-on? */
  128. if(userp && *userp) {
  129. /* Populate our identity structure */
  130. result = Curl_create_sspi_identity(userp, passwdp, &krb5->identity);
  131. if(result)
  132. return result;
  133. /* Allow proper cleanup of the identity structure */
  134. krb5->p_identity = &krb5->identity;
  135. }
  136. else
  137. /* Use the current Windows user */
  138. krb5->p_identity = NULL;
  139. /* Allocate our credentials handle */
  140. krb5->credentials = calloc(1, sizeof(CredHandle));
  141. if(!krb5->credentials)
  142. return CURLE_OUT_OF_MEMORY;
  143. /* Acquire our credentials handle */
  144. status = s_pSecFn->AcquireCredentialsHandle(NULL,
  145. (TCHAR *)
  146. TEXT(SP_NAME_KERBEROS),
  147. SECPKG_CRED_OUTBOUND, NULL,
  148. krb5->p_identity, NULL, NULL,
  149. krb5->credentials, &expiry);
  150. if(status != SEC_E_OK)
  151. return CURLE_LOGIN_DENIED;
  152. /* Allocate our new context handle */
  153. krb5->context = calloc(1, sizeof(CtxtHandle));
  154. if(!krb5->context)
  155. return CURLE_OUT_OF_MEMORY;
  156. }
  157. if(chlg64 && *chlg64) {
  158. /* Decode the base-64 encoded challenge message */
  159. if(*chlg64 != '=') {
  160. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  161. if(result)
  162. return result;
  163. }
  164. /* Ensure we have a valid challenge message */
  165. if(!chlg) {
  166. infof(data, "GSSAPI handshake failure (empty challenge message)\n");
  167. return CURLE_BAD_CONTENT_ENCODING;
  168. }
  169. /* Setup the challenge "input" security buffer */
  170. chlg_desc.ulVersion = SECBUFFER_VERSION;
  171. chlg_desc.cBuffers = 1;
  172. chlg_desc.pBuffers = &chlg_buf;
  173. chlg_buf.BufferType = SECBUFFER_TOKEN;
  174. chlg_buf.pvBuffer = chlg;
  175. chlg_buf.cbBuffer = curlx_uztoul(chlglen);
  176. }
  177. /* Setup the response "output" security buffer */
  178. resp_desc.ulVersion = SECBUFFER_VERSION;
  179. resp_desc.cBuffers = 1;
  180. resp_desc.pBuffers = &resp_buf;
  181. resp_buf.BufferType = SECBUFFER_TOKEN;
  182. resp_buf.pvBuffer = krb5->output_token;
  183. resp_buf.cbBuffer = curlx_uztoul(krb5->token_max);
  184. /* Generate our challenge-response message */
  185. status = s_pSecFn->InitializeSecurityContext(krb5->credentials,
  186. chlg ? krb5->context : NULL,
  187. krb5->spn,
  188. (mutual_auth ?
  189. ISC_REQ_MUTUAL_AUTH : 0),
  190. 0, SECURITY_NATIVE_DREP,
  191. chlg ? &chlg_desc : NULL, 0,
  192. &context,
  193. &resp_desc, &attrs,
  194. &expiry);
  195. /* Free the decoded challenge as it is not required anymore */
  196. free(chlg);
  197. if(status == SEC_E_INSUFFICIENT_MEMORY) {
  198. return CURLE_OUT_OF_MEMORY;
  199. }
  200. if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  201. return CURLE_AUTH_ERROR;
  202. }
  203. if(memcmp(&context, krb5->context, sizeof(context))) {
  204. s_pSecFn->DeleteSecurityContext(krb5->context);
  205. memcpy(krb5->context, &context, sizeof(context));
  206. }
  207. if(resp_buf.cbBuffer) {
  208. /* Base64 encode the response */
  209. result = Curl_base64_encode(data, (char *) resp_buf.pvBuffer,
  210. resp_buf.cbBuffer, outptr, outlen);
  211. }
  212. else if(mutual_auth) {
  213. *outptr = strdup("");
  214. if(!*outptr)
  215. result = CURLE_OUT_OF_MEMORY;
  216. }
  217. return result;
  218. }
  219. /*
  220. * Curl_auth_create_gssapi_security_message()
  221. *
  222. * This is used to generate an already encoded GSSAPI (Kerberos V5) security
  223. * token message ready for sending to the recipient.
  224. *
  225. * Parameters:
  226. *
  227. * data [in] - The session handle.
  228. * chlg64 [in] - The optional base64 encoded challenge message.
  229. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  230. * outptr [in/out] - The address where a pointer to newly allocated memory
  231. * holding the result will be stored upon completion.
  232. * outlen [out] - The length of the output message.
  233. *
  234. * Returns CURLE_OK on success.
  235. */
  236. CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
  237. const char *chlg64,
  238. struct kerberos5data *krb5,
  239. char **outptr,
  240. size_t *outlen)
  241. {
  242. CURLcode result = CURLE_OK;
  243. size_t offset = 0;
  244. size_t chlglen = 0;
  245. size_t messagelen = 0;
  246. size_t appdatalen = 0;
  247. unsigned char *chlg = NULL;
  248. unsigned char *trailer = NULL;
  249. unsigned char *message = NULL;
  250. unsigned char *padding = NULL;
  251. unsigned char *appdata = NULL;
  252. SecBuffer input_buf[2];
  253. SecBuffer wrap_buf[3];
  254. SecBufferDesc input_desc;
  255. SecBufferDesc wrap_desc;
  256. unsigned long indata = 0;
  257. unsigned long outdata = 0;
  258. unsigned long qop = 0;
  259. unsigned long sec_layer = 0;
  260. unsigned long max_size = 0;
  261. SecPkgContext_Sizes sizes;
  262. SecPkgCredentials_Names names;
  263. SECURITY_STATUS status;
  264. char *user_name;
  265. /* Decode the base-64 encoded input message */
  266. if(strlen(chlg64) && *chlg64 != '=') {
  267. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  268. if(result)
  269. return result;
  270. }
  271. /* Ensure we have a valid challenge message */
  272. if(!chlg) {
  273. infof(data, "GSSAPI handshake failure (empty security message)\n");
  274. return CURLE_BAD_CONTENT_ENCODING;
  275. }
  276. /* Get our response size information */
  277. status = s_pSecFn->QueryContextAttributes(krb5->context,
  278. SECPKG_ATTR_SIZES,
  279. &sizes);
  280. if(status != SEC_E_OK) {
  281. free(chlg);
  282. if(status == SEC_E_INSUFFICIENT_MEMORY)
  283. return CURLE_OUT_OF_MEMORY;
  284. return CURLE_AUTH_ERROR;
  285. }
  286. /* Get the fully qualified username back from the context */
  287. status = s_pSecFn->QueryCredentialsAttributes(krb5->credentials,
  288. SECPKG_CRED_ATTR_NAMES,
  289. &names);
  290. if(status != SEC_E_OK) {
  291. free(chlg);
  292. if(status == SEC_E_INSUFFICIENT_MEMORY)
  293. return CURLE_OUT_OF_MEMORY;
  294. return CURLE_AUTH_ERROR;
  295. }
  296. /* Setup the "input" security buffer */
  297. input_desc.ulVersion = SECBUFFER_VERSION;
  298. input_desc.cBuffers = 2;
  299. input_desc.pBuffers = input_buf;
  300. input_buf[0].BufferType = SECBUFFER_STREAM;
  301. input_buf[0].pvBuffer = chlg;
  302. input_buf[0].cbBuffer = curlx_uztoul(chlglen);
  303. input_buf[1].BufferType = SECBUFFER_DATA;
  304. input_buf[1].pvBuffer = NULL;
  305. input_buf[1].cbBuffer = 0;
  306. /* Decrypt the inbound challenge and obtain the qop */
  307. status = s_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop);
  308. if(status != SEC_E_OK) {
  309. infof(data, "GSSAPI handshake failure (empty security message)\n");
  310. free(chlg);
  311. return CURLE_BAD_CONTENT_ENCODING;
  312. }
  313. /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
  314. if(input_buf[1].cbBuffer != 4) {
  315. infof(data, "GSSAPI handshake failure (invalid security data)\n");
  316. free(chlg);
  317. return CURLE_BAD_CONTENT_ENCODING;
  318. }
  319. /* Copy the data out and free the challenge as it is not required anymore */
  320. memcpy(&indata, input_buf[1].pvBuffer, 4);
  321. s_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer);
  322. free(chlg);
  323. /* Extract the security layer */
  324. sec_layer = indata & 0x000000FF;
  325. if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) {
  326. infof(data, "GSSAPI handshake failure (invalid security layer)\n");
  327. return CURLE_BAD_CONTENT_ENCODING;
  328. }
  329. /* Extract the maximum message size the server can receive */
  330. max_size = ntohl(indata & 0xFFFFFF00);
  331. if(max_size > 0) {
  332. /* The server has told us it supports a maximum receive buffer, however, as
  333. we don't require one unless we are encrypting data, we tell the server
  334. our receive buffer is zero. */
  335. max_size = 0;
  336. }
  337. /* Allocate the trailer */
  338. trailer = malloc(sizes.cbSecurityTrailer);
  339. if(!trailer)
  340. return CURLE_OUT_OF_MEMORY;
  341. /* Convert the user name to UTF8 when operating with Unicode */
  342. user_name = Curl_convert_tchar_to_UTF8(names.sUserName);
  343. if(!user_name) {
  344. free(trailer);
  345. return CURLE_OUT_OF_MEMORY;
  346. }
  347. /* Allocate our message */
  348. messagelen = sizeof(outdata) + strlen(user_name) + 1;
  349. message = malloc(messagelen);
  350. if(!message) {
  351. free(trailer);
  352. Curl_unicodefree(user_name);
  353. return CURLE_OUT_OF_MEMORY;
  354. }
  355. /* Populate the message with the security layer, client supported receive
  356. message size and authorization identity including the 0x00 based
  357. terminator. Note: Despite RFC4752 Section 3.1 stating "The authorization
  358. identity is not terminated with the zero-valued (%x00) octet." it seems
  359. necessary to include it. */
  360. outdata = htonl(max_size) | sec_layer;
  361. memcpy(message, &outdata, sizeof(outdata));
  362. strcpy((char *) message + sizeof(outdata), user_name);
  363. Curl_unicodefree(user_name);
  364. /* Allocate the padding */
  365. padding = malloc(sizes.cbBlockSize);
  366. if(!padding) {
  367. free(message);
  368. free(trailer);
  369. return CURLE_OUT_OF_MEMORY;
  370. }
  371. /* Setup the "authentication data" security buffer */
  372. wrap_desc.ulVersion = SECBUFFER_VERSION;
  373. wrap_desc.cBuffers = 3;
  374. wrap_desc.pBuffers = wrap_buf;
  375. wrap_buf[0].BufferType = SECBUFFER_TOKEN;
  376. wrap_buf[0].pvBuffer = trailer;
  377. wrap_buf[0].cbBuffer = sizes.cbSecurityTrailer;
  378. wrap_buf[1].BufferType = SECBUFFER_DATA;
  379. wrap_buf[1].pvBuffer = message;
  380. wrap_buf[1].cbBuffer = curlx_uztoul(messagelen);
  381. wrap_buf[2].BufferType = SECBUFFER_PADDING;
  382. wrap_buf[2].pvBuffer = padding;
  383. wrap_buf[2].cbBuffer = sizes.cbBlockSize;
  384. /* Encrypt the data */
  385. status = s_pSecFn->EncryptMessage(krb5->context, KERB_WRAP_NO_ENCRYPT,
  386. &wrap_desc, 0);
  387. if(status != SEC_E_OK) {
  388. free(padding);
  389. free(message);
  390. free(trailer);
  391. if(status == SEC_E_INSUFFICIENT_MEMORY)
  392. return CURLE_OUT_OF_MEMORY;
  393. return CURLE_AUTH_ERROR;
  394. }
  395. /* Allocate the encryption (wrap) buffer */
  396. appdatalen = wrap_buf[0].cbBuffer + wrap_buf[1].cbBuffer +
  397. wrap_buf[2].cbBuffer;
  398. appdata = malloc(appdatalen);
  399. if(!appdata) {
  400. free(padding);
  401. free(message);
  402. free(trailer);
  403. return CURLE_OUT_OF_MEMORY;
  404. }
  405. /* Populate the encryption buffer */
  406. memcpy(appdata, wrap_buf[0].pvBuffer, wrap_buf[0].cbBuffer);
  407. offset += wrap_buf[0].cbBuffer;
  408. memcpy(appdata + offset, wrap_buf[1].pvBuffer, wrap_buf[1].cbBuffer);
  409. offset += wrap_buf[1].cbBuffer;
  410. memcpy(appdata + offset, wrap_buf[2].pvBuffer, wrap_buf[2].cbBuffer);
  411. /* Base64 encode the response */
  412. result = Curl_base64_encode(data, (char *) appdata, appdatalen, outptr,
  413. outlen);
  414. /* Free all of our local buffers */
  415. free(appdata);
  416. free(padding);
  417. free(message);
  418. free(trailer);
  419. return result;
  420. }
  421. /*
  422. * Curl_auth_cleanup_gssapi()
  423. *
  424. * This is used to clean up the GSSAPI (Kerberos V5) specific data.
  425. *
  426. * Parameters:
  427. *
  428. * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
  429. *
  430. */
  431. void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
  432. {
  433. /* Free our security context */
  434. if(krb5->context) {
  435. s_pSecFn->DeleteSecurityContext(krb5->context);
  436. free(krb5->context);
  437. krb5->context = NULL;
  438. }
  439. /* Free our credentials handle */
  440. if(krb5->credentials) {
  441. s_pSecFn->FreeCredentialsHandle(krb5->credentials);
  442. free(krb5->credentials);
  443. krb5->credentials = NULL;
  444. }
  445. /* Free our identity */
  446. Curl_sspi_free_identity(krb5->p_identity);
  447. krb5->p_identity = NULL;
  448. /* Free the SPN and output token */
  449. Curl_safefree(krb5->spn);
  450. Curl_safefree(krb5->output_token);
  451. /* Reset any variables */
  452. krb5->token_max = 0;
  453. }
  454. #endif /* USE_WINDOWS_SSPI && USE_KERBEROS5*/