krb5_sspi.c 16 KB

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