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/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/*
46 * Curl_auth_is_gssapi_supported()
47 *
48 * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
49 *
50 * Parameters: None
51 *
52 * Returns TRUE if Kerberos V5 is supported by the GSS-API library.
53 */
54bool Curl_auth_is_gssapi_supported(void)
55{
56 return TRUE;
57}
58
59/*
60 * Curl_auth_create_gssapi_user_message()
61 *
62 * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
63 * message ready for sending to the recipient.
64 *
65 * Parameters:
66 *
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
73 * is enabled.
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.
77 *
78 * Returns CURLE_OK on success.
79 */
80CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
81 const char *userp,
82 const char *passwdp,
83 const char *service,
84 const char *host,
85 const bool mutual_auth,
86 const struct bufref *chlg,
87 struct kerberos5data *krb5,
88 struct bufref *out)
89{
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;
97
98 (void) userp;
99 (void) passwdp;
100
101 if(!krb5->spn) {
102 /* Generate our SPN */
103 char *spn = Curl_auth_build_spn(service, NULL, host);
104 if(!spn)
105 return CURLE_OUT_OF_MEMORY;
106
107 /* Populate the SPN structure */
108 spn_token.value = spn;
109 spn_token.length = strlen(spn);
110
111 /* Import the 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);
117
118 free(spn);
119
120 return CURLE_AUTH_ERROR;
121 }
122
123 free(spn);
124 }
125
126 if(chlg) {
127 if(!Curl_bufref_len(chlg)) {
128 infof(data, "GSSAPI handshake failure (empty challenge message)");
129 return CURLE_BAD_CONTENT_ENCODING;
130 }
131 input_token.value = (void *) Curl_bufref_ptr(chlg);
132 input_token.length = Curl_bufref_len(chlg);
133 }
134
135 major_status = Curl_gss_init_sec_context(data,
136 &minor_status,
137 &krb5->context,
138 krb5->spn,
139 &Curl_krb5_mech_oid,
140 GSS_C_NO_CHANNEL_BINDINGS,
141 &input_token,
142 &output_token,
143 mutual_auth,
144 NULL);
145
146 if(GSS_ERROR(major_status)) {
147 if(output_token.value)
148 gss_release_buffer(&unused_status, &output_token);
149
150 Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
151 major_status, minor_status);
152
153 return CURLE_AUTH_ERROR;
154 }
155
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);
159 }
160 else
161 Curl_bufref_set(out, mutual_auth? "": NULL, 0, NULL);
162
163 return result;
164}
165
166/*
167 * Curl_auth_create_gssapi_security_message()
168 *
169 * This is used to generate an already encoded GSSAPI (Kerberos V5) security
170 * token message ready for sending to the recipient.
171 *
172 * Parameters:
173 *
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.
179 *
180 * Returns CURLE_OK on success.
181 */
182CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
183 const char *authzid,
184 const struct bufref *chlg,
185 struct kerberos5data *krb5,
186 struct bufref *out)
187{
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;
200
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;
205 }
206
207 /* Setup the challenge "input" security buffer */
208 input_token.value = (void *) Curl_bufref_ptr(chlg);
209 input_token.length = Curl_bufref_len(chlg);
210
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;
218 }
219
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;
224 }
225
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];
230
231 /* Free the challenge as it is not required anymore */
232 gss_release_buffer(&unused_status, &output_token);
233
234 /* Process the security layer */
235 if(!(sec_layer & GSSAUTH_P_NONE)) {
236 infof(data, "GSSAPI handshake failure (invalid security layer)");
237
238 return CURLE_BAD_CONTENT_ENCODING;
239 }
240 sec_layer &= GSSAUTH_P_NONE; /* We do not support a security layer */
241
242 /* Process the maximum message size the server can receive */
243 if(max_size > 0) {
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. */
247 max_size = 0;
248 }
249
250 /* Allocate our message */
251 messagelen = 4;
252 if(authzid)
253 messagelen += strlen(authzid);
254 message = malloc(messagelen);
255 if(!message)
256 return CURLE_OUT_OF_MEMORY;
257
258 /* Populate the message with the security layer and client supported receive
259 message size. */
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;
264
265 /* If given, append the authorization identity. */
266
267 if(authzid && *authzid)
268 memcpy(message + 4, authzid, messagelen - 4);
269
270 /* Setup the "authentication data" security buffer */
271 input_token.value = message;
272 input_token.length = messagelen;
273
274 /* Encrypt the data */
275 major_status = gss_wrap(&minor_status, krb5->context, 0,
276 GSS_C_QOP_DEFAULT, &input_token, NULL,
277 &output_token);
278 if(GSS_ERROR(major_status)) {
279 Curl_gss_log_error(data, "gss_wrap() failed: ",
280 major_status, minor_status);
281 free(message);
282 return CURLE_AUTH_ERROR;
283 }
284
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);
289
290 /* Free the message buffer */
291 free(message);
292
293 return result;
294}
295
296/*
297 * Curl_auth_cleanup_gssapi()
298 *
299 * This is used to clean up the GSSAPI (Kerberos V5) specific data.
300 *
301 * Parameters:
302 *
303 * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
304 *
305 */
306void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
307{
308 OM_uint32 minor_status;
309
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;
314 }
315
316 /* Free the SPN */
317 if(krb5->spn != GSS_C_NO_NAME) {
318 gss_release_name(&minor_status, &krb5->spn);
319 krb5->spn = GSS_C_NO_NAME;
320 }
321}
322
323#endif /* HAVE_GSSAPI && USE_KERBEROS5 */
324