krb5_sspi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014 - 2021, 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.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 "warnless.h"
  30. #include "curl_multibyte.h"
  31. #include "sendf.h"
  32. /* The last #include files should be: */
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. /*
  36. * Curl_auth_is_gssapi_supported()
  37. *
  38. * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
  39. *
  40. * Parameters: None
  41. *
  42. * Returns TRUE if Kerberos V5 is supported by Windows SSPI.
  43. */
  44. bool Curl_auth_is_gssapi_supported(void)
  45. {
  46. PSecPkgInfo SecurityPackage;
  47. SECURITY_STATUS status;
  48. /* Query the security package for Kerberos */
  49. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
  50. TEXT(SP_NAME_KERBEROS),
  51. &SecurityPackage);
  52. /* Release the package buffer as it is not required anymore */
  53. if(status == SEC_E_OK) {
  54. s_pSecFn->FreeContextBuffer(SecurityPackage);
  55. }
  56. return (status == SEC_E_OK ? TRUE : FALSE);
  57. }
  58. /*
  59. * Curl_auth_create_gssapi_user_message()
  60. *
  61. * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
  62. * message ready for sending to the recipient.
  63. *
  64. * Parameters:
  65. *
  66. * data [in] - The session handle.
  67. * userp [in] - The user name in the format User or Domain\User.
  68. * passwdp [in] - The user's password.
  69. * service [in] - The service type such as http, smtp, pop or imap.
  70. * host [in] - The host name.
  71. * mutual_auth [in] - Flag specifying whether or not mutual authentication
  72. * is enabled.
  73. * chlg [in] - Optional challenge message.
  74. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  75. * out [out] - The result storage.
  76. *
  77. * Returns CURLE_OK on success.
  78. */
  79. CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
  80. const char *userp,
  81. const char *passwdp,
  82. const char *service,
  83. const char *host,
  84. const bool mutual_auth,
  85. const struct bufref *chlg,
  86. struct kerberos5data *krb5,
  87. struct bufref *out)
  88. {
  89. CURLcode result = CURLE_OK;
  90. CtxtHandle context;
  91. PSecPkgInfo SecurityPackage;
  92. SecBuffer chlg_buf;
  93. SecBuffer resp_buf;
  94. SecBufferDesc chlg_desc;
  95. SecBufferDesc resp_desc;
  96. SECURITY_STATUS status;
  97. unsigned long attrs;
  98. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  99. if(!krb5->spn) {
  100. /* Generate our SPN */
  101. krb5->spn = Curl_auth_build_spn(service, host, NULL);
  102. if(!krb5->spn)
  103. return CURLE_OUT_OF_MEMORY;
  104. }
  105. if(!krb5->output_token) {
  106. /* Query the security package for Kerberos */
  107. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
  108. TEXT(SP_NAME_KERBEROS),
  109. &SecurityPackage);
  110. if(status != SEC_E_OK) {
  111. failf(data, "SSPI: couldn't get auth info");
  112. return CURLE_AUTH_ERROR;
  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(chlg) {
  154. if(!Curl_bufref_len(chlg)) {
  155. infof(data, "GSSAPI handshake failure (empty challenge message)");
  156. return CURLE_BAD_CONTENT_ENCODING;
  157. }
  158. /* Setup the challenge "input" security buffer */
  159. chlg_desc.ulVersion = SECBUFFER_VERSION;
  160. chlg_desc.cBuffers = 1;
  161. chlg_desc.pBuffers = &chlg_buf;
  162. chlg_buf.BufferType = SECBUFFER_TOKEN;
  163. chlg_buf.pvBuffer = (void *) Curl_bufref_ptr(chlg);
  164. chlg_buf.cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
  165. }
  166. /* Setup the response "output" security buffer */
  167. resp_desc.ulVersion = SECBUFFER_VERSION;
  168. resp_desc.cBuffers = 1;
  169. resp_desc.pBuffers = &resp_buf;
  170. resp_buf.BufferType = SECBUFFER_TOKEN;
  171. resp_buf.pvBuffer = krb5->output_token;
  172. resp_buf.cbBuffer = curlx_uztoul(krb5->token_max);
  173. /* Generate our challenge-response message */
  174. status = s_pSecFn->InitializeSecurityContext(krb5->credentials,
  175. chlg ? krb5->context : NULL,
  176. krb5->spn,
  177. (mutual_auth ?
  178. ISC_REQ_MUTUAL_AUTH : 0),
  179. 0, SECURITY_NATIVE_DREP,
  180. chlg ? &chlg_desc : NULL, 0,
  181. &context,
  182. &resp_desc, &attrs,
  183. &expiry);
  184. if(status == SEC_E_INSUFFICIENT_MEMORY)
  185. return CURLE_OUT_OF_MEMORY;
  186. if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED)
  187. return CURLE_AUTH_ERROR;
  188. if(memcmp(&context, krb5->context, sizeof(context))) {
  189. s_pSecFn->DeleteSecurityContext(krb5->context);
  190. memcpy(krb5->context, &context, sizeof(context));
  191. }
  192. if(resp_buf.cbBuffer) {
  193. result = Curl_bufref_memdup(out, resp_buf.pvBuffer, resp_buf.cbBuffer);
  194. }
  195. else if(mutual_auth)
  196. Curl_bufref_set(out, "", 0, NULL);
  197. else
  198. Curl_bufref_set(out, NULL, 0, NULL);
  199. return result;
  200. }
  201. /*
  202. * Curl_auth_create_gssapi_security_message()
  203. *
  204. * This is used to generate an already encoded GSSAPI (Kerberos V5) security
  205. * token message ready for sending to the recipient.
  206. *
  207. * Parameters:
  208. *
  209. * data [in] - The session handle.
  210. * authzid [in] - The authorization identity if some.
  211. * chlg [in] - The optional challenge message.
  212. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  213. * out [out] - The result storage.
  214. *
  215. * Returns CURLE_OK on success.
  216. */
  217. CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
  218. const char *authzid,
  219. const struct bufref *chlg,
  220. struct kerberos5data *krb5,
  221. struct bufref *out)
  222. {
  223. size_t offset = 0;
  224. size_t messagelen = 0;
  225. size_t appdatalen = 0;
  226. unsigned char *trailer = NULL;
  227. unsigned char *message = NULL;
  228. unsigned char *padding = NULL;
  229. unsigned char *appdata = NULL;
  230. SecBuffer input_buf[2];
  231. SecBuffer wrap_buf[3];
  232. SecBufferDesc input_desc;
  233. SecBufferDesc wrap_desc;
  234. unsigned char *indata;
  235. unsigned long qop = 0;
  236. unsigned long sec_layer = 0;
  237. unsigned long max_size = 0;
  238. SecPkgContext_Sizes sizes;
  239. SECURITY_STATUS status;
  240. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  241. (void) data;
  242. #endif
  243. /* Ensure we have a valid challenge message */
  244. if(!Curl_bufref_len(chlg)) {
  245. infof(data, "GSSAPI handshake failure (empty security message)");
  246. return CURLE_BAD_CONTENT_ENCODING;
  247. }
  248. /* Get our response size information */
  249. status = s_pSecFn->QueryContextAttributes(krb5->context,
  250. SECPKG_ATTR_SIZES,
  251. &sizes);
  252. if(status == SEC_E_INSUFFICIENT_MEMORY)
  253. return CURLE_OUT_OF_MEMORY;
  254. if(status != SEC_E_OK)
  255. return CURLE_AUTH_ERROR;
  256. /* Setup the "input" security buffer */
  257. input_desc.ulVersion = SECBUFFER_VERSION;
  258. input_desc.cBuffers = 2;
  259. input_desc.pBuffers = input_buf;
  260. input_buf[0].BufferType = SECBUFFER_STREAM;
  261. input_buf[0].pvBuffer = (void *) Curl_bufref_ptr(chlg);
  262. input_buf[0].cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
  263. input_buf[1].BufferType = SECBUFFER_DATA;
  264. input_buf[1].pvBuffer = NULL;
  265. input_buf[1].cbBuffer = 0;
  266. /* Decrypt the inbound challenge and obtain the qop */
  267. status = s_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop);
  268. if(status != SEC_E_OK) {
  269. infof(data, "GSSAPI handshake failure (empty security message)");
  270. return CURLE_BAD_CONTENT_ENCODING;
  271. }
  272. /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
  273. if(input_buf[1].cbBuffer != 4) {
  274. infof(data, "GSSAPI handshake failure (invalid security data)");
  275. return CURLE_BAD_CONTENT_ENCODING;
  276. }
  277. /* Extract the security layer and the maximum message size */
  278. indata = input_buf[1].pvBuffer;
  279. sec_layer = indata[0];
  280. max_size = (indata[1] << 16) | (indata[2] << 8) | indata[3];
  281. /* Free the challenge as it is not required anymore */
  282. s_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer);
  283. /* Process the security layer */
  284. if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) {
  285. infof(data, "GSSAPI handshake failure (invalid security layer)");
  286. return CURLE_BAD_CONTENT_ENCODING;
  287. }
  288. sec_layer &= KERB_WRAP_NO_ENCRYPT; /* We do not support a security layer */
  289. /* Process the maximum message size the server can receive */
  290. if(max_size > 0) {
  291. /* The server has told us it supports a maximum receive buffer, however, as
  292. we don't require one unless we are encrypting data, we tell the server
  293. our receive buffer is zero. */
  294. max_size = 0;
  295. }
  296. /* Allocate the trailer */
  297. trailer = malloc(sizes.cbSecurityTrailer);
  298. if(!trailer)
  299. return CURLE_OUT_OF_MEMORY;
  300. /* Allocate our message */
  301. messagelen = 4;
  302. if(authzid)
  303. messagelen += strlen(authzid);
  304. message = malloc(messagelen);
  305. if(!message) {
  306. free(trailer);
  307. return CURLE_OUT_OF_MEMORY;
  308. }
  309. /* Populate the message with the security layer and client supported receive
  310. message size. */
  311. message[0] = sec_layer & 0xFF;
  312. message[1] = (max_size >> 16) & 0xFF;
  313. message[2] = (max_size >> 8) & 0xFF;
  314. message[3] = max_size & 0xFF;
  315. /* If given, append the authorization identity. */
  316. if(authzid && *authzid)
  317. memcpy(message + 4, authzid, messagelen - 4);
  318. /* Allocate the padding */
  319. padding = malloc(sizes.cbBlockSize);
  320. if(!padding) {
  321. free(message);
  322. free(trailer);
  323. return CURLE_OUT_OF_MEMORY;
  324. }
  325. /* Setup the "authentication data" security buffer */
  326. wrap_desc.ulVersion = SECBUFFER_VERSION;
  327. wrap_desc.cBuffers = 3;
  328. wrap_desc.pBuffers = wrap_buf;
  329. wrap_buf[0].BufferType = SECBUFFER_TOKEN;
  330. wrap_buf[0].pvBuffer = trailer;
  331. wrap_buf[0].cbBuffer = sizes.cbSecurityTrailer;
  332. wrap_buf[1].BufferType = SECBUFFER_DATA;
  333. wrap_buf[1].pvBuffer = message;
  334. wrap_buf[1].cbBuffer = curlx_uztoul(messagelen);
  335. wrap_buf[2].BufferType = SECBUFFER_PADDING;
  336. wrap_buf[2].pvBuffer = padding;
  337. wrap_buf[2].cbBuffer = sizes.cbBlockSize;
  338. /* Encrypt the data */
  339. status = s_pSecFn->EncryptMessage(krb5->context, KERB_WRAP_NO_ENCRYPT,
  340. &wrap_desc, 0);
  341. if(status != SEC_E_OK) {
  342. free(padding);
  343. free(message);
  344. free(trailer);
  345. if(status == SEC_E_INSUFFICIENT_MEMORY)
  346. return CURLE_OUT_OF_MEMORY;
  347. return CURLE_AUTH_ERROR;
  348. }
  349. /* Allocate the encryption (wrap) buffer */
  350. appdatalen = wrap_buf[0].cbBuffer + wrap_buf[1].cbBuffer +
  351. wrap_buf[2].cbBuffer;
  352. appdata = malloc(appdatalen);
  353. if(!appdata) {
  354. free(padding);
  355. free(message);
  356. free(trailer);
  357. return CURLE_OUT_OF_MEMORY;
  358. }
  359. /* Populate the encryption buffer */
  360. memcpy(appdata, wrap_buf[0].pvBuffer, wrap_buf[0].cbBuffer);
  361. offset += wrap_buf[0].cbBuffer;
  362. memcpy(appdata + offset, wrap_buf[1].pvBuffer, wrap_buf[1].cbBuffer);
  363. offset += wrap_buf[1].cbBuffer;
  364. memcpy(appdata + offset, wrap_buf[2].pvBuffer, wrap_buf[2].cbBuffer);
  365. /* Free all of our local buffers */
  366. free(padding);
  367. free(message);
  368. free(trailer);
  369. /* Return the response. */
  370. Curl_bufref_set(out, appdata, appdatalen, curl_free);
  371. return CURLE_OK;
  372. }
  373. /*
  374. * Curl_auth_cleanup_gssapi()
  375. *
  376. * This is used to clean up the GSSAPI (Kerberos V5) specific data.
  377. *
  378. * Parameters:
  379. *
  380. * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
  381. *
  382. */
  383. void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
  384. {
  385. /* Free our security context */
  386. if(krb5->context) {
  387. s_pSecFn->DeleteSecurityContext(krb5->context);
  388. free(krb5->context);
  389. krb5->context = NULL;
  390. }
  391. /* Free our credentials handle */
  392. if(krb5->credentials) {
  393. s_pSecFn->FreeCredentialsHandle(krb5->credentials);
  394. free(krb5->credentials);
  395. krb5->credentials = NULL;
  396. }
  397. /* Free our identity */
  398. Curl_sspi_free_identity(krb5->p_identity);
  399. krb5->p_identity = NULL;
  400. /* Free the SPN and output token */
  401. Curl_safefree(krb5->spn);
  402. Curl_safefree(krb5->output_token);
  403. /* Reset any variables */
  404. krb5->token_max = 0;
  405. }
  406. #endif /* USE_WINDOWS_SSPI && USE_KERBEROS5*/