1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014 - 2019, Steve Holme, <steve_holme@hotmail.com>.
9 * Copyright (C) 2015 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
24 ***************************************************************************/
26 #include "curl_setup.h"
28 #if defined(HAVE_GSSAPI) && defined(USE_KERBEROS5)
30 #include <curl/curl.h>
32 #include "vauth/vauth.h"
33 #include "curl_sasl.h"
35 #include "curl_gssapi.h"
37 #include "curl_printf.h"
39 /* The last #include files should be: */
40 #include "curl_memory.h"
44 * Curl_auth_is_gssapi_supported()
46 * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
50 * Returns TRUE if Kerberos V5 is supported by the GSS-API library.
52 bool Curl_auth_is_gssapi_supported(void)
58 * Curl_auth_create_gssapi_user_message()
60 * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
61 * message ready for sending to the recipient.
65 * data [in] - The session handle.
66 * userp [in] - The user name.
67 * passwdp [in] - The user's password.
68 * service [in] - The service type such as http, smtp, pop or imap.
69 * host [in[ - The host name.
70 * mutual_auth [in] - Flag specifying whether or not mutual authentication
72 * chlg [in] - Optional challenge message.
73 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
74 * out [out] - The result storage.
76 * Returns CURLE_OK on success.
78 CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
83 const bool mutual_auth,
84 const struct bufref *chlg,
85 struct kerberos5data *krb5,
88 CURLcode result = CURLE_OK;
89 OM_uint32 major_status;
90 OM_uint32 minor_status;
91 OM_uint32 unused_status;
92 gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
93 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
94 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
100 /* Generate our SPN */
101 char *spn = Curl_auth_build_spn(service, NULL, host);
103 return CURLE_OUT_OF_MEMORY;
105 /* Populate the SPN structure */
106 spn_token.value = spn;
107 spn_token.length = strlen(spn);
110 major_status = gss_import_name(&minor_status, &spn_token,
111 GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
112 if(GSS_ERROR(major_status)) {
113 Curl_gss_log_error(data, "gss_import_name() failed: ",
114 major_status, minor_status);
118 return CURLE_AUTH_ERROR;
125 if(!Curl_bufref_len(chlg)) {
126 infof(data, "GSSAPI handshake failure (empty challenge message)");
127 return CURLE_BAD_CONTENT_ENCODING;
129 input_token.value = (void *) Curl_bufref_ptr(chlg);
130 input_token.length = Curl_bufref_len(chlg);
133 major_status = Curl_gss_init_sec_context(data,
138 GSS_C_NO_CHANNEL_BINDINGS,
144 if(GSS_ERROR(major_status)) {
145 if(output_token.value)
146 gss_release_buffer(&unused_status, &output_token);
148 Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
149 major_status, minor_status);
151 return CURLE_AUTH_ERROR;
154 if(output_token.value && output_token.length) {
155 result = Curl_bufref_memdup(out, output_token.value, output_token.length);
156 gss_release_buffer(&unused_status, &output_token);
159 Curl_bufref_set(out, mutual_auth? "": NULL, 0, NULL);
165 * Curl_auth_create_gssapi_security_message()
167 * This is used to generate an already encoded GSSAPI (Kerberos V5) security
168 * token message ready for sending to the recipient.
172 * data [in] - The session handle.
173 * authzid [in] - The authorization identity if some.
174 * chlg [in] - Optional challenge message.
175 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
176 * out [out] - The result storage.
178 * Returns CURLE_OK on success.
180 CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
182 const struct bufref *chlg,
183 struct kerberos5data *krb5,
186 CURLcode result = CURLE_OK;
187 size_t messagelen = 0;
188 unsigned char *message = NULL;
189 OM_uint32 major_status;
190 OM_uint32 minor_status;
191 OM_uint32 unused_status;
192 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
193 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
194 unsigned char *indata;
195 gss_qop_t qop = GSS_C_QOP_DEFAULT;
196 unsigned int sec_layer = 0;
197 unsigned int max_size = 0;
199 /* Ensure we have a valid challenge message */
200 if(!Curl_bufref_len(chlg)) {
201 infof(data, "GSSAPI handshake failure (empty security message)");
202 return CURLE_BAD_CONTENT_ENCODING;
205 /* Setup the challenge "input" security buffer */
206 input_token.value = (void *) Curl_bufref_ptr(chlg);
207 input_token.length = Curl_bufref_len(chlg);
209 /* Decrypt the inbound challenge and obtain the qop */
210 major_status = gss_unwrap(&minor_status, krb5->context, &input_token,
211 &output_token, NULL, &qop);
212 if(GSS_ERROR(major_status)) {
213 Curl_gss_log_error(data, "gss_unwrap() failed: ",
214 major_status, minor_status);
215 return CURLE_BAD_CONTENT_ENCODING;
218 /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
219 if(output_token.length != 4) {
220 infof(data, "GSSAPI handshake failure (invalid security data)");
221 return CURLE_BAD_CONTENT_ENCODING;
224 /* Extract the security layer and the maximum message size */
225 indata = output_token.value;
226 sec_layer = indata[0];
227 max_size = (indata[1] << 16) | (indata[2] << 8) | indata[3];
229 /* Free the challenge as it is not required anymore */
230 gss_release_buffer(&unused_status, &output_token);
232 /* Process the security layer */
233 if(!(sec_layer & GSSAUTH_P_NONE)) {
234 infof(data, "GSSAPI handshake failure (invalid security layer)");
236 return CURLE_BAD_CONTENT_ENCODING;
238 sec_layer &= GSSAUTH_P_NONE; /* We do not support a security layer */
240 /* Process the maximum message size the server can receive */
242 /* The server has told us it supports a maximum receive buffer, however, as
243 we don't require one unless we are encrypting data, we tell the server
244 our receive buffer is zero. */
248 /* Allocate our message */
251 messagelen += strlen(authzid);
252 message = malloc(messagelen);
254 return CURLE_OUT_OF_MEMORY;
256 /* Populate the message with the security layer and client supported receive
258 message[0] = sec_layer & 0xFF;
259 message[1] = (max_size >> 16) & 0xFF;
260 message[2] = (max_size >> 8) & 0xFF;
261 message[3] = max_size & 0xFF;
263 /* If given, append the authorization identity. */
265 if(authzid && *authzid)
266 memcpy(message + 4, authzid, messagelen - 4);
268 /* Setup the "authentication data" security buffer */
269 input_token.value = message;
270 input_token.length = messagelen;
272 /* Encrypt the data */
273 major_status = gss_wrap(&minor_status, krb5->context, 0,
274 GSS_C_QOP_DEFAULT, &input_token, NULL,
276 if(GSS_ERROR(major_status)) {
277 Curl_gss_log_error(data, "gss_wrap() failed: ",
278 major_status, minor_status);
280 return CURLE_AUTH_ERROR;
283 /* Return the response. */
284 result = Curl_bufref_memdup(out, output_token.value, output_token.length);
285 /* Free the output buffer */
286 gss_release_buffer(&unused_status, &output_token);
288 /* Free the message buffer */
295 * Curl_auth_cleanup_gssapi()
297 * This is used to clean up the GSSAPI (Kerberos V5) specific data.
301 * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
304 void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
306 OM_uint32 minor_status;
308 /* Free our security context */
309 if(krb5->context != GSS_C_NO_CONTEXT) {
310 gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
311 krb5->context = GSS_C_NO_CONTEXT;
315 if(krb5->spn != GSS_C_NO_NAME) {
316 gss_release_name(&minor_status, &krb5->spn);
317 krb5->spn = GSS_C_NO_NAME;
321 #endif /* HAVE_GSSAPI && USE_KERBEROS5 */