krb5_sspi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. * passwdp [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 credentials 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 = calloc(1, sizeof(CredHandle));
  137. if(!krb5->credentials)
  138. return CURLE_OUT_OF_MEMORY;
  139. /* Acquire our credentials handle */
  140. status = s_pSecFn->AcquireCredentialsHandle(NULL,
  141. (TCHAR *)
  142. TEXT(SP_NAME_KERBEROS),
  143. SECPKG_CRED_OUTBOUND, NULL,
  144. krb5->p_identity, NULL, NULL,
  145. krb5->credentials, &expiry);
  146. if(status != SEC_E_OK)
  147. return CURLE_LOGIN_DENIED;
  148. /* Allocate our new context handle */
  149. krb5->context = calloc(1, sizeof(CtxtHandle));
  150. if(!krb5->context)
  151. return CURLE_OUT_OF_MEMORY;
  152. }
  153. if(chlg64 && *chlg64) {
  154. /* Decode the base-64 encoded challenge message */
  155. if(*chlg64 != '=') {
  156. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  157. if(result)
  158. return result;
  159. }
  160. /* Ensure we have a valid challenge message */
  161. if(!chlg) {
  162. infof(data, "GSSAPI handshake failure (empty challenge message)\n");
  163. return CURLE_BAD_CONTENT_ENCODING;
  164. }
  165. /* Setup the challenge "input" security buffer */
  166. chlg_desc.ulVersion = SECBUFFER_VERSION;
  167. chlg_desc.cBuffers = 1;
  168. chlg_desc.pBuffers = &chlg_buf;
  169. chlg_buf.BufferType = SECBUFFER_TOKEN;
  170. chlg_buf.pvBuffer = chlg;
  171. chlg_buf.cbBuffer = curlx_uztoul(chlglen);
  172. }
  173. /* Setup the response "output" security buffer */
  174. resp_desc.ulVersion = SECBUFFER_VERSION;
  175. resp_desc.cBuffers = 1;
  176. resp_desc.pBuffers = &resp_buf;
  177. resp_buf.BufferType = SECBUFFER_TOKEN;
  178. resp_buf.pvBuffer = krb5->output_token;
  179. resp_buf.cbBuffer = curlx_uztoul(krb5->token_max);
  180. /* Generate our challenge-response message */
  181. status = s_pSecFn->InitializeSecurityContext(krb5->credentials,
  182. chlg ? krb5->context : NULL,
  183. krb5->spn,
  184. (mutual_auth ?
  185. ISC_REQ_MUTUAL_AUTH : 0),
  186. 0, SECURITY_NATIVE_DREP,
  187. chlg ? &chlg_desc : NULL, 0,
  188. &context,
  189. &resp_desc, &attrs,
  190. &expiry);
  191. /* Free the decoded challenge as it is not required anymore */
  192. free(chlg);
  193. if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  194. return CURLE_RECV_ERROR;
  195. }
  196. if(memcmp(&context, krb5->context, sizeof(context))) {
  197. s_pSecFn->DeleteSecurityContext(krb5->context);
  198. memcpy(krb5->context, &context, sizeof(context));
  199. }
  200. if(resp_buf.cbBuffer) {
  201. /* Base64 encode the response */
  202. result = Curl_base64_encode(data, (char *) resp_buf.pvBuffer,
  203. resp_buf.cbBuffer, outptr, outlen);
  204. }
  205. else if(mutual_auth) {
  206. *outptr = strdup("");
  207. if(!*outptr)
  208. result = CURLE_OUT_OF_MEMORY;
  209. }
  210. return result;
  211. }
  212. /*
  213. * Curl_auth_create_gssapi_security_message()
  214. *
  215. * This is used to generate an already encoded GSSAPI (Kerberos V5) security
  216. * token message ready for sending to the recipient.
  217. *
  218. * Parameters:
  219. *
  220. * data [in] - The session handle.
  221. * chlg64 [in] - The optional base64 encoded challenge message.
  222. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  223. * outptr [in/out] - The address where a pointer to newly allocated memory
  224. * holding the result will be stored upon completion.
  225. * outlen [out] - The length of the output message.
  226. *
  227. * Returns CURLE_OK on success.
  228. */
  229. CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
  230. const char *chlg64,
  231. struct kerberos5data *krb5,
  232. char **outptr,
  233. size_t *outlen)
  234. {
  235. CURLcode result = CURLE_OK;
  236. size_t offset = 0;
  237. size_t chlglen = 0;
  238. size_t messagelen = 0;
  239. size_t appdatalen = 0;
  240. unsigned char *chlg = NULL;
  241. unsigned char *trailer = NULL;
  242. unsigned char *message = NULL;
  243. unsigned char *padding = NULL;
  244. unsigned char *appdata = NULL;
  245. SecBuffer input_buf[2];
  246. SecBuffer wrap_buf[3];
  247. SecBufferDesc input_desc;
  248. SecBufferDesc wrap_desc;
  249. unsigned long indata = 0;
  250. unsigned long outdata = 0;
  251. unsigned long qop = 0;
  252. unsigned long sec_layer = 0;
  253. unsigned long max_size = 0;
  254. SecPkgContext_Sizes sizes;
  255. SecPkgCredentials_Names names;
  256. SECURITY_STATUS status;
  257. char *user_name;
  258. /* Decode the base-64 encoded input message */
  259. if(strlen(chlg64) && *chlg64 != '=') {
  260. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  261. if(result)
  262. return result;
  263. }
  264. /* Ensure we have a valid challenge message */
  265. if(!chlg) {
  266. infof(data, "GSSAPI handshake failure (empty security message)\n");
  267. return CURLE_BAD_CONTENT_ENCODING;
  268. }
  269. /* Get our response size information */
  270. status = s_pSecFn->QueryContextAttributes(krb5->context,
  271. SECPKG_ATTR_SIZES,
  272. &sizes);
  273. if(status != SEC_E_OK) {
  274. free(chlg);
  275. return CURLE_OUT_OF_MEMORY;
  276. }
  277. /* Get the fully qualified username back from the context */
  278. status = s_pSecFn->QueryCredentialsAttributes(krb5->credentials,
  279. SECPKG_CRED_ATTR_NAMES,
  280. &names);
  281. if(status != SEC_E_OK) {
  282. free(chlg);
  283. return CURLE_RECV_ERROR;
  284. }
  285. /* Setup the "input" security buffer */
  286. input_desc.ulVersion = SECBUFFER_VERSION;
  287. input_desc.cBuffers = 2;
  288. input_desc.pBuffers = input_buf;
  289. input_buf[0].BufferType = SECBUFFER_STREAM;
  290. input_buf[0].pvBuffer = chlg;
  291. input_buf[0].cbBuffer = curlx_uztoul(chlglen);
  292. input_buf[1].BufferType = SECBUFFER_DATA;
  293. input_buf[1].pvBuffer = NULL;
  294. input_buf[1].cbBuffer = 0;
  295. /* Decrypt the inbound challenge and obtain the qop */
  296. status = s_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop);
  297. if(status != SEC_E_OK) {
  298. infof(data, "GSSAPI handshake failure (empty security message)\n");
  299. free(chlg);
  300. return CURLE_BAD_CONTENT_ENCODING;
  301. }
  302. /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
  303. if(input_buf[1].cbBuffer != 4) {
  304. infof(data, "GSSAPI handshake failure (invalid security data)\n");
  305. free(chlg);
  306. return CURLE_BAD_CONTENT_ENCODING;
  307. }
  308. /* Copy the data out and free the challenge as it is not required anymore */
  309. memcpy(&indata, input_buf[1].pvBuffer, 4);
  310. s_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer);
  311. free(chlg);
  312. /* Extract the security layer */
  313. sec_layer = indata & 0x000000FF;
  314. if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) {
  315. infof(data, "GSSAPI handshake failure (invalid security layer)\n");
  316. return CURLE_BAD_CONTENT_ENCODING;
  317. }
  318. /* Extract the maximum message size the server can receive */
  319. max_size = ntohl(indata & 0xFFFFFF00);
  320. if(max_size > 0) {
  321. /* The server has told us it supports a maximum receive buffer, however, as
  322. we don't require one unless we are encrypting data, we tell the server
  323. our receive buffer is zero. */
  324. max_size = 0;
  325. }
  326. /* Allocate the trailer */
  327. trailer = malloc(sizes.cbSecurityTrailer);
  328. if(!trailer)
  329. return CURLE_OUT_OF_MEMORY;
  330. /* Convert the user name to UTF8 when operating with Unicode */
  331. user_name = Curl_convert_tchar_to_UTF8(names.sUserName);
  332. if(!user_name) {
  333. free(trailer);
  334. return CURLE_OUT_OF_MEMORY;
  335. }
  336. /* Allocate our message */
  337. messagelen = sizeof(outdata) + strlen(user_name) + 1;
  338. message = malloc(messagelen);
  339. if(!message) {
  340. free(trailer);
  341. Curl_unicodefree(user_name);
  342. return CURLE_OUT_OF_MEMORY;
  343. }
  344. /* Populate the message with the security layer, client supported receive
  345. message size and authorization identity including the 0x00 based
  346. terminator. Note: Despite RFC4752 Section 3.1 stating "The authorization
  347. identity is not terminated with the zero-valued (%x00) octet." it seems
  348. necessary to include it. */
  349. outdata = htonl(max_size) | sec_layer;
  350. memcpy(message, &outdata, sizeof(outdata));
  351. strcpy((char *) message + sizeof(outdata), user_name);
  352. Curl_unicodefree(user_name);
  353. /* Allocate the padding */
  354. padding = malloc(sizes.cbBlockSize);
  355. if(!padding) {
  356. free(message);
  357. free(trailer);
  358. return CURLE_OUT_OF_MEMORY;
  359. }
  360. /* Setup the "authentication data" security buffer */
  361. wrap_desc.ulVersion = SECBUFFER_VERSION;
  362. wrap_desc.cBuffers = 3;
  363. wrap_desc.pBuffers = wrap_buf;
  364. wrap_buf[0].BufferType = SECBUFFER_TOKEN;
  365. wrap_buf[0].pvBuffer = trailer;
  366. wrap_buf[0].cbBuffer = sizes.cbSecurityTrailer;
  367. wrap_buf[1].BufferType = SECBUFFER_DATA;
  368. wrap_buf[1].pvBuffer = message;
  369. wrap_buf[1].cbBuffer = curlx_uztoul(messagelen);
  370. wrap_buf[2].BufferType = SECBUFFER_PADDING;
  371. wrap_buf[2].pvBuffer = padding;
  372. wrap_buf[2].cbBuffer = sizes.cbBlockSize;
  373. /* Encrypt the data */
  374. status = s_pSecFn->EncryptMessage(krb5->context, KERB_WRAP_NO_ENCRYPT,
  375. &wrap_desc, 0);
  376. if(status != SEC_E_OK) {
  377. free(padding);
  378. free(message);
  379. free(trailer);
  380. return CURLE_OUT_OF_MEMORY;
  381. }
  382. /* Allocate the encryption (wrap) buffer */
  383. appdatalen = wrap_buf[0].cbBuffer + wrap_buf[1].cbBuffer +
  384. wrap_buf[2].cbBuffer;
  385. appdata = malloc(appdatalen);
  386. if(!appdata) {
  387. free(padding);
  388. free(message);
  389. free(trailer);
  390. return CURLE_OUT_OF_MEMORY;
  391. }
  392. /* Populate the encryption buffer */
  393. memcpy(appdata, wrap_buf[0].pvBuffer, wrap_buf[0].cbBuffer);
  394. offset += wrap_buf[0].cbBuffer;
  395. memcpy(appdata + offset, wrap_buf[1].pvBuffer, wrap_buf[1].cbBuffer);
  396. offset += wrap_buf[1].cbBuffer;
  397. memcpy(appdata + offset, wrap_buf[2].pvBuffer, wrap_buf[2].cbBuffer);
  398. /* Base64 encode the response */
  399. result = Curl_base64_encode(data, (char *) appdata, appdatalen, outptr,
  400. outlen);
  401. /* Free all of our local buffers */
  402. free(appdata);
  403. free(padding);
  404. free(message);
  405. free(trailer);
  406. return result;
  407. }
  408. /*
  409. * Curl_auth_gssapi_cleanup()
  410. *
  411. * This is used to clean up the GSSAPI (Kerberos V5) specific data.
  412. *
  413. * Parameters:
  414. *
  415. * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
  416. *
  417. */
  418. void Curl_auth_gssapi_cleanup(struct kerberos5data *krb5)
  419. {
  420. /* Free our security context */
  421. if(krb5->context) {
  422. s_pSecFn->DeleteSecurityContext(krb5->context);
  423. free(krb5->context);
  424. krb5->context = NULL;
  425. }
  426. /* Free our credentials handle */
  427. if(krb5->credentials) {
  428. s_pSecFn->FreeCredentialsHandle(krb5->credentials);
  429. free(krb5->credentials);
  430. krb5->credentials = NULL;
  431. }
  432. /* Free our identity */
  433. Curl_sspi_free_identity(krb5->p_identity);
  434. krb5->p_identity = NULL;
  435. /* Free the SPN and output token */
  436. Curl_safefree(krb5->spn);
  437. Curl_safefree(krb5->output_token);
  438. /* Reset any variables */
  439. krb5->token_max = 0;
  440. }
  441. #endif /* USE_WINDOWS_SSPI && USE_KERBEROS5*/