1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014 - 2019, Steve Holme, <steve_holme@hotmail.com>.
9 * Copyright (C) 2015 - 2022, 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 * SPDX-License-Identifier: curl
24 * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
26 ***************************************************************************/
28 #include "curl_setup.h"
30 #if defined(HAVE_GSSAPI) && defined(USE_KERBEROS5)
32 #include <curl/curl.h>
34 #include "vauth/vauth.h"
35 #include "curl_sasl.h"
37 #include "curl_gssapi.h"
39 #include "curl_printf.h"
41 /* The last #include files should be: */
42 #include "curl_memory.h"
46 * Curl_auth_is_gssapi_supported()
48 * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
52 * Returns TRUE if Kerberos V5 is supported by the GSS-API library.
54 bool Curl_auth_is_gssapi_supported(void)
60 * Curl_auth_create_gssapi_user_message()
62 * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
63 * message ready for sending to the recipient.
67 * data [in] - The session handle.
68 * userp [in] - The user name.
69 * passwdp [in] - The user's password.
70 * service [in] - The service type such as http, smtp, pop or imap.
71 * host [in[ - The host name.
72 * mutual_auth [in] - Flag specifying whether or not mutual authentication
74 * chlg [in] - Optional challenge message.
75 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
76 * out [out] - The result storage.
78 * Returns CURLE_OK on success.
80 CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
85 const bool mutual_auth,
86 const struct bufref *chlg,
87 struct kerberos5data *krb5,
90 CURLcode result = CURLE_OK;
91 OM_uint32 major_status;
92 OM_uint32 minor_status;
93 OM_uint32 unused_status;
94 gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
95 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
96 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
102 /* Generate our SPN */
103 char *spn = Curl_auth_build_spn(service, NULL, host);
105 return CURLE_OUT_OF_MEMORY;
107 /* Populate the SPN structure */
108 spn_token.value = spn;
109 spn_token.length = strlen(spn);
112 major_status = gss_import_name(&minor_status, &spn_token,
113 GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
114 if(GSS_ERROR(major_status)) {
115 Curl_gss_log_error(data, "gss_import_name() failed: ",
116 major_status, minor_status);
120 return CURLE_AUTH_ERROR;
127 if(!Curl_bufref_len(chlg)) {
128 infof(data, "GSSAPI handshake failure (empty challenge message)");
129 return CURLE_BAD_CONTENT_ENCODING;
131 input_token.value = (void *) Curl_bufref_ptr(chlg);
132 input_token.length = Curl_bufref_len(chlg);
135 major_status = Curl_gss_init_sec_context(data,
140 GSS_C_NO_CHANNEL_BINDINGS,
146 if(GSS_ERROR(major_status)) {
147 if(output_token.value)
148 gss_release_buffer(&unused_status, &output_token);
150 Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
151 major_status, minor_status);
153 return CURLE_AUTH_ERROR;
156 if(output_token.value && output_token.length) {
157 result = Curl_bufref_memdup(out, output_token.value, output_token.length);
158 gss_release_buffer(&unused_status, &output_token);
161 Curl_bufref_set(out, mutual_auth? "": NULL, 0, NULL);
167 * Curl_auth_create_gssapi_security_message()
169 * This is used to generate an already encoded GSSAPI (Kerberos V5) security
170 * token message ready for sending to the recipient.
174 * data [in] - The session handle.
175 * authzid [in] - The authorization identity if some.
176 * chlg [in] - Optional challenge message.
177 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
178 * out [out] - The result storage.
180 * Returns CURLE_OK on success.
182 CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
184 const struct bufref *chlg,
185 struct kerberos5data *krb5,
188 CURLcode result = CURLE_OK;
189 size_t messagelen = 0;
190 unsigned char *message = NULL;
191 OM_uint32 major_status;
192 OM_uint32 minor_status;
193 OM_uint32 unused_status;
194 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
195 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
196 unsigned char *indata;
197 gss_qop_t qop = GSS_C_QOP_DEFAULT;
198 unsigned int sec_layer = 0;
199 unsigned int max_size = 0;
201 /* Ensure we have a valid challenge message */
202 if(!Curl_bufref_len(chlg)) {
203 infof(data, "GSSAPI handshake failure (empty security message)");
204 return CURLE_BAD_CONTENT_ENCODING;
207 /* Setup the challenge "input" security buffer */
208 input_token.value = (void *) Curl_bufref_ptr(chlg);
209 input_token.length = Curl_bufref_len(chlg);
211 /* Decrypt the inbound challenge and obtain the qop */
212 major_status = gss_unwrap(&minor_status, krb5->context, &input_token,
213 &output_token, NULL, &qop);
214 if(GSS_ERROR(major_status)) {
215 Curl_gss_log_error(data, "gss_unwrap() failed: ",
216 major_status, minor_status);
217 return CURLE_BAD_CONTENT_ENCODING;
220 /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
221 if(output_token.length != 4) {
222 infof(data, "GSSAPI handshake failure (invalid security data)");
223 return CURLE_BAD_CONTENT_ENCODING;
226 /* Extract the security layer and the maximum message size */
227 indata = output_token.value;
228 sec_layer = indata[0];
229 max_size = (indata[1] << 16) | (indata[2] << 8) | indata[3];
231 /* Free the challenge as it is not required anymore */
232 gss_release_buffer(&unused_status, &output_token);
234 /* Process the security layer */
235 if(!(sec_layer & GSSAUTH_P_NONE)) {
236 infof(data, "GSSAPI handshake failure (invalid security layer)");
238 return CURLE_BAD_CONTENT_ENCODING;
240 sec_layer &= GSSAUTH_P_NONE; /* We do not support a security layer */
242 /* Process the maximum message size the server can receive */
244 /* The server has told us it supports a maximum receive buffer, however, as
245 we don't require one unless we are encrypting data, we tell the server
246 our receive buffer is zero. */
250 /* Allocate our message */
253 messagelen += strlen(authzid);
254 message = malloc(messagelen);
256 return CURLE_OUT_OF_MEMORY;
258 /* Populate the message with the security layer and client supported receive
260 message[0] = sec_layer & 0xFF;
261 message[1] = (max_size >> 16) & 0xFF;
262 message[2] = (max_size >> 8) & 0xFF;
263 message[3] = max_size & 0xFF;
265 /* If given, append the authorization identity. */
267 if(authzid && *authzid)
268 memcpy(message + 4, authzid, messagelen - 4);
270 /* Setup the "authentication data" security buffer */
271 input_token.value = message;
272 input_token.length = messagelen;
274 /* Encrypt the data */
275 major_status = gss_wrap(&minor_status, krb5->context, 0,
276 GSS_C_QOP_DEFAULT, &input_token, NULL,
278 if(GSS_ERROR(major_status)) {
279 Curl_gss_log_error(data, "gss_wrap() failed: ",
280 major_status, minor_status);
282 return CURLE_AUTH_ERROR;
285 /* Return the response. */
286 result = Curl_bufref_memdup(out, output_token.value, output_token.length);
287 /* Free the output buffer */
288 gss_release_buffer(&unused_status, &output_token);
290 /* Free the message buffer */
297 * Curl_auth_cleanup_gssapi()
299 * This is used to clean up the GSSAPI (Kerberos V5) specific data.
303 * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
306 void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
308 OM_uint32 minor_status;
310 /* Free our security context */
311 if(krb5->context != GSS_C_NO_CONTEXT) {
312 gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
313 krb5->context = GSS_C_NO_CONTEXT;
317 if(krb5->spn != GSS_C_NO_NAME) {
318 gss_release_name(&minor_status, &krb5->spn);
319 krb5->spn = GSS_C_NO_NAME;
323 #endif /* HAVE_GSSAPI && USE_KERBEROS5 */