krb5_gssapi.c (10852B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) Steve Holme, <steve_holme@hotmail.com>. 9 * Copyright (C) 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 https://curl.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 * SPDX-License-Identifier: curl 23 * 24 * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism 25 * 26 ***************************************************************************/ 27 28 #include "../curl_setup.h" 29 30 #if defined(HAVE_GSSAPI) && defined(USE_KERBEROS5) 31 32 #include <curl/curl.h> 33 34 #include "vauth.h" 35 #include "../curl_sasl.h" 36 #include "../urldata.h" 37 #include "../curl_gssapi.h" 38 #include "../sendf.h" 39 #include "../curl_printf.h" 40 41 /* The last #include files should be: */ 42 #include "../curl_memory.h" 43 #include "../memdebug.h" 44 45 #if defined(__GNUC__) && defined(__APPLE__) 46 #pragma GCC diagnostic push 47 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 48 #endif 49 50 /* 51 * Curl_auth_is_gssapi_supported() 52 * 53 * This is used to evaluate if GSSAPI (Kerberos V5) is supported. 54 * 55 * Parameters: None 56 * 57 * Returns TRUE if Kerberos V5 is supported by the GSS-API library. 58 */ 59 bool Curl_auth_is_gssapi_supported(void) 60 { 61 return TRUE; 62 } 63 64 /* 65 * Curl_auth_create_gssapi_user_message() 66 * 67 * This is used to generate an already encoded GSSAPI (Kerberos V5) user token 68 * message ready for sending to the recipient. 69 * 70 * Parameters: 71 * 72 * data [in] - The session handle. 73 * userp [in] - The username. 74 * passwdp [in] - The user's password. 75 * service [in] - The service type such as http, smtp, pop or imap. 76 * host [in[ - The hostname. 77 * mutual_auth [in] - Flag specifying whether or not mutual authentication 78 * is enabled. 79 * chlg [in] - Optional challenge message. 80 * krb5 [in/out] - The Kerberos 5 data struct being used and modified. 81 * out [out] - The result storage. 82 * 83 * Returns CURLE_OK on success. 84 */ 85 CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data, 86 const char *userp, 87 const char *passwdp, 88 const char *service, 89 const char *host, 90 const bool mutual_auth, 91 const struct bufref *chlg, 92 struct kerberos5data *krb5, 93 struct bufref *out) 94 { 95 CURLcode result = CURLE_OK; 96 OM_uint32 major_status; 97 OM_uint32 minor_status; 98 OM_uint32 unused_status; 99 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER; 100 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER; 101 102 (void) userp; 103 (void) passwdp; 104 105 if(!krb5->spn) { 106 gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER; 107 108 /* Generate our SPN */ 109 char *spn = Curl_auth_build_spn(service, NULL, host); 110 if(!spn) 111 return CURLE_OUT_OF_MEMORY; 112 113 /* Populate the SPN structure */ 114 spn_token.value = spn; 115 spn_token.length = strlen(spn); 116 117 /* Import the SPN */ 118 major_status = gss_import_name(&minor_status, &spn_token, 119 GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn); 120 if(GSS_ERROR(major_status)) { 121 Curl_gss_log_error(data, "gss_import_name() failed: ", 122 major_status, minor_status); 123 124 free(spn); 125 126 return CURLE_AUTH_ERROR; 127 } 128 129 free(spn); 130 } 131 132 if(chlg) { 133 if(!Curl_bufref_len(chlg)) { 134 infof(data, "GSSAPI handshake failure (empty challenge message)"); 135 return CURLE_BAD_CONTENT_ENCODING; 136 } 137 input_token.value = CURL_UNCONST(Curl_bufref_ptr(chlg)); 138 input_token.length = Curl_bufref_len(chlg); 139 } 140 141 major_status = Curl_gss_init_sec_context(data, 142 &minor_status, 143 &krb5->context, 144 krb5->spn, 145 &Curl_krb5_mech_oid, 146 GSS_C_NO_CHANNEL_BINDINGS, 147 &input_token, 148 &output_token, 149 mutual_auth, 150 NULL); 151 152 if(GSS_ERROR(major_status)) { 153 if(output_token.value) 154 gss_release_buffer(&unused_status, &output_token); 155 156 Curl_gss_log_error(data, "gss_init_sec_context() failed: ", 157 major_status, minor_status); 158 159 return CURLE_AUTH_ERROR; 160 } 161 162 if(output_token.value && output_token.length) { 163 result = Curl_bufref_memdup(out, output_token.value, output_token.length); 164 gss_release_buffer(&unused_status, &output_token); 165 } 166 else 167 Curl_bufref_set(out, mutual_auth ? "": NULL, 0, NULL); 168 169 return result; 170 } 171 172 /* 173 * Curl_auth_create_gssapi_security_message() 174 * 175 * This is used to generate an already encoded GSSAPI (Kerberos V5) security 176 * token message ready for sending to the recipient. 177 * 178 * Parameters: 179 * 180 * data [in] - The session handle. 181 * authzid [in] - The authorization identity if some. 182 * chlg [in] - Optional challenge message. 183 * krb5 [in/out] - The Kerberos 5 data struct being used and modified. 184 * out [out] - The result storage. 185 * 186 * Returns CURLE_OK on success. 187 */ 188 CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data, 189 const char *authzid, 190 const struct bufref *chlg, 191 struct kerberos5data *krb5, 192 struct bufref *out) 193 { 194 CURLcode result = CURLE_OK; 195 size_t messagelen = 0; 196 unsigned char *message = NULL; 197 OM_uint32 major_status; 198 OM_uint32 minor_status; 199 OM_uint32 unused_status; 200 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER; 201 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER; 202 unsigned char *indata; 203 gss_qop_t qop = GSS_C_QOP_DEFAULT; 204 unsigned int sec_layer = 0; 205 unsigned int max_size = 0; 206 207 /* Ensure we have a valid challenge message */ 208 if(!Curl_bufref_len(chlg)) { 209 infof(data, "GSSAPI handshake failure (empty security message)"); 210 return CURLE_BAD_CONTENT_ENCODING; 211 } 212 213 /* Setup the challenge "input" security buffer */ 214 input_token.value = CURL_UNCONST(Curl_bufref_ptr(chlg)); 215 input_token.length = Curl_bufref_len(chlg); 216 217 /* Decrypt the inbound challenge and obtain the qop */ 218 major_status = gss_unwrap(&minor_status, krb5->context, &input_token, 219 &output_token, NULL, &qop); 220 if(GSS_ERROR(major_status)) { 221 Curl_gss_log_error(data, "gss_unwrap() failed: ", 222 major_status, minor_status); 223 return CURLE_BAD_CONTENT_ENCODING; 224 } 225 226 /* Not 4 octets long so fail as per RFC4752 Section 3.1 */ 227 if(output_token.length != 4) { 228 infof(data, "GSSAPI handshake failure (invalid security data)"); 229 return CURLE_BAD_CONTENT_ENCODING; 230 } 231 232 /* Extract the security layer and the maximum message size */ 233 indata = output_token.value; 234 sec_layer = indata[0]; 235 max_size = ((unsigned int)indata[1] << 16) | 236 ((unsigned int)indata[2] << 8) | indata[3]; 237 238 /* Free the challenge as it is not required anymore */ 239 gss_release_buffer(&unused_status, &output_token); 240 241 /* Process the security layer */ 242 if(!(sec_layer & GSSAUTH_P_NONE)) { 243 infof(data, "GSSAPI handshake failure (invalid security layer)"); 244 245 return CURLE_BAD_CONTENT_ENCODING; 246 } 247 sec_layer &= GSSAUTH_P_NONE; /* We do not support a security layer */ 248 249 /* Process the maximum message size the server can receive */ 250 if(max_size > 0) { 251 /* The server has told us it supports a maximum receive buffer, however, as 252 we do not require one unless we are encrypting data, we tell the server 253 our receive buffer is zero. */ 254 max_size = 0; 255 } 256 257 /* Allocate our message */ 258 messagelen = 4; 259 if(authzid) 260 messagelen += strlen(authzid); 261 message = malloc(messagelen); 262 if(!message) 263 return CURLE_OUT_OF_MEMORY; 264 265 /* Populate the message with the security layer and client supported receive 266 message size. */ 267 message[0] = sec_layer & 0xFF; 268 message[1] = (max_size >> 16) & 0xFF; 269 message[2] = (max_size >> 8) & 0xFF; 270 message[3] = max_size & 0xFF; 271 272 /* If given, append the authorization identity. */ 273 274 if(authzid && *authzid) 275 memcpy(message + 4, authzid, messagelen - 4); 276 277 /* Setup the "authentication data" security buffer */ 278 input_token.value = message; 279 input_token.length = messagelen; 280 281 /* Encrypt the data */ 282 major_status = gss_wrap(&minor_status, krb5->context, 0, 283 GSS_C_QOP_DEFAULT, &input_token, NULL, 284 &output_token); 285 if(GSS_ERROR(major_status)) { 286 Curl_gss_log_error(data, "gss_wrap() failed: ", 287 major_status, minor_status); 288 free(message); 289 return CURLE_AUTH_ERROR; 290 } 291 292 /* Return the response. */ 293 result = Curl_bufref_memdup(out, output_token.value, output_token.length); 294 /* Free the output buffer */ 295 gss_release_buffer(&unused_status, &output_token); 296 297 /* Free the message buffer */ 298 free(message); 299 300 return result; 301 } 302 303 /* 304 * Curl_auth_cleanup_gssapi() 305 * 306 * This is used to clean up the GSSAPI (Kerberos V5) specific data. 307 * 308 * Parameters: 309 * 310 * krb5 [in/out] - The Kerberos 5 data struct being cleaned up. 311 * 312 */ 313 void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5) 314 { 315 OM_uint32 minor_status; 316 317 /* Free our security context */ 318 if(krb5->context != GSS_C_NO_CONTEXT) { 319 Curl_gss_delete_sec_context(&minor_status, &krb5->context, 320 GSS_C_NO_BUFFER); 321 krb5->context = GSS_C_NO_CONTEXT; 322 } 323 324 /* Free the SPN */ 325 if(krb5->spn != GSS_C_NO_NAME) { 326 gss_release_name(&minor_status, &krb5->spn); 327 krb5->spn = GSS_C_NO_NAME; 328 } 329 } 330 331 #if defined(__GNUC__) && defined(__APPLE__) 332 #pragma GCC diagnostic pop 333 #endif 334 335 #endif /* HAVE_GSSAPI && USE_KERBEROS5 */