1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014 - 2015, Steve Holme, <steve_holme@hotmail.com>.
9 * Copyright (C) 2015, 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 http://curl.haxx.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 "curl_sasl.h"
34 #include "curl_base64.h"
35 #include "curl_gssapi.h"
36 #include "curl_memory.h"
39 #define _MPRINTF_REPLACE /* use our functions only */
40 #include <curl/mprintf.h>
42 /* The last #include file should be: */
46 * Curl_sasl_build_gssapi_spn()
48 * This is used to build a SPN string in the format service@host.
52 * serivce [in] - The service type such as www, smtp, pop or imap.
53 * host [in] - The host name or realm.
55 * Returns a pointer to the newly allocated SPN.
57 static char *Curl_sasl_build_gssapi_spn(const char *service, const char *host)
59 /* Generate and return our SPN */
60 return aprintf("%s@%s", service, host);
64 * Curl_sasl_create_gssapi_user_message()
66 * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
67 * message ready for sending to the recipient.
71 * data [in] - The session handle.
72 * userp [in] - The user name.
73 * passdwp [in] - The user's password.
74 * service [in] - The service type such as www, smtp, pop or imap.
75 * mutual_auth [in] - Flag specifing whether or not mutual authentication
77 * chlg64 [in] - Pointer to the optional base64 encoded challenge
79 * krb5 [in/out] - The gssapi data struct being used and modified.
80 * outptr [in/out] - The address where a pointer to newly allocated memory
81 * holding the result will be stored upon completion.
82 * outlen [out] - The length of the output message.
84 * Returns CURLE_OK on success.
86 CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
90 const bool mutual_auth,
92 struct kerberos5data *krb5,
93 char **outptr, size_t *outlen)
95 CURLcode result = CURLE_OK;
97 unsigned char *chlg = NULL;
99 OM_uint32 gss_major_status;
100 OM_uint32 gss_minor_status;
101 gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
102 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
103 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
108 if(krb5->context == GSS_C_NO_CONTEXT) {
109 /* Generate our SPN */
110 char *spn = Curl_sasl_build_gssapi_spn(service,
111 data->easy_conn->host.name);
113 return CURLE_OUT_OF_MEMORY;
115 /* Populate the SPN structure */
116 spn_token.value = spn;
117 spn_token.length = strlen(spn);
120 gss_major_status = gss_import_name(&gss_minor_status, &spn_token,
121 GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
122 if(GSS_ERROR(gss_major_status)) {
123 Curl_gss_log_error(data, gss_minor_status, "gss_import_name() failed: ");
125 return CURLE_OUT_OF_MEMORY;
129 /* Decode the base-64 encoded challenge message */
130 if(strlen(chlg64) && *chlg64 != '=') {
131 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
136 /* Ensure we have a valid challenge message */
138 infof(data, "GSSAPI handshake failure (empty challenge message)\n");
140 return CURLE_BAD_CONTENT_ENCODING;
143 /* Setup the challenge "input" security buffer */
144 input_token.value = chlg;
145 input_token.length = chlglen;
148 gss_major_status = Curl_gss_init_sec_context(data,
153 GSS_C_NO_CHANNEL_BINDINGS,
159 Curl_safefree(input_token.value);
161 if(GSS_ERROR(gss_major_status)) {
162 if(output_token.value)
163 gss_release_buffer(&gss_status, &output_token);
165 Curl_gss_log_error(data, gss_minor_status,
166 "gss_init_sec_context() failed: ");
168 return CURLE_RECV_ERROR;
171 if(output_token.value && output_token.length) {
172 /* Base64 encode the response */
173 result = Curl_base64_encode(data, (char *) output_token.value,
174 output_token.length, outptr, outlen);
176 gss_release_buffer(&gss_status, &output_token);
183 * Curl_sasl_create_gssapi_security_message()
185 * This is used to generate an already encoded GSSAPI (Kerberos V5) security
186 * token message ready for sending to the recipient.
190 * data [in] - The session handle.
191 * chlg64 [in] - Pointer to the optional base64 encoded challenge message.
192 * krb5 [in/out] - The gssapi data struct being used and modified.
193 * outptr [in/out] - The address where a pointer to newly allocated memory
194 * holding the result will be stored upon completion.
195 * outlen [out] - The length of the output message.
197 * Returns CURLE_OK on success.
199 CURLcode Curl_sasl_create_gssapi_security_message(struct SessionHandle *data,
201 struct kerberos5data *krb5,
205 CURLcode result = CURLE_OK;
207 size_t messagelen = 0;
208 unsigned char *chlg = NULL;
209 unsigned char *message = NULL;
210 OM_uint32 gss_status;
211 OM_uint32 gss_major_status;
212 OM_uint32 gss_minor_status;
213 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
214 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
215 unsigned int indata = 0;
216 unsigned int outdata = 0;
217 gss_qop_t qop = GSS_C_QOP_DEFAULT;
218 unsigned int sec_layer = 0;
219 unsigned int max_size = 0;
220 gss_name_t username = GSS_C_NO_NAME;
221 gss_buffer_desc username_token;
223 /* Decode the base-64 encoded input message */
224 if(strlen(chlg64) && *chlg64 != '=') {
225 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
230 /* Ensure we have a valid challenge message */
232 infof(data, "GSSAPI handshake failure (empty security message)\n");
234 return CURLE_BAD_CONTENT_ENCODING;
237 /* Get the fully qualified username back from the context */
238 gss_major_status = gss_inquire_context(&gss_minor_status, krb5->context,
239 &username, NULL, NULL, NULL, NULL,
241 if(GSS_ERROR(gss_major_status)) {
242 Curl_gss_log_error(data, gss_minor_status,
243 "gss_inquire_context() failed: ");
247 return CURLE_OUT_OF_MEMORY;
250 /* Convert the username from internal format to a displayable token */
251 gss_major_status = gss_display_name(&gss_minor_status, username,
252 &username_token, NULL);
253 if(GSS_ERROR(gss_major_status)) {
254 Curl_gss_log_error(data, gss_minor_status, "gss_display_name() failed: ");
258 return CURLE_OUT_OF_MEMORY;
261 /* Setup the challenge "input" security buffer */
262 input_token.value = chlg;
263 input_token.length = chlglen;
265 /* Decrypt the inbound challenge and obtain the qop */
266 gss_major_status = gss_unwrap(&gss_minor_status, krb5->context, &input_token,
267 &output_token, NULL, &qop);
268 if(GSS_ERROR(gss_major_status)) {
269 Curl_gss_log_error(data, gss_minor_status, "gss_unwrap() failed: ");
271 gss_release_buffer(&gss_status, &username_token);
274 return CURLE_BAD_CONTENT_ENCODING;
277 /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
278 if(output_token.length != 4) {
279 infof(data, "GSSAPI handshake failure (invalid security data)\n");
281 gss_release_buffer(&gss_status, &username_token);
284 return CURLE_BAD_CONTENT_ENCODING;
287 /* Copy the data out and free the challenge as it is not required anymore */
288 memcpy(&indata, output_token.value, 4);
289 gss_release_buffer(&gss_status, &output_token);
292 /* Extract the security layer */
293 sec_layer = indata & 0x000000FF;
294 if(!(sec_layer & GSSAUTH_P_NONE)) {
295 infof(data, "GSSAPI handshake failure (invalid security layer)\n");
297 gss_release_buffer(&gss_status, &username_token);
299 return CURLE_BAD_CONTENT_ENCODING;
302 /* Extract the maximum message size the server can receive */
303 max_size = ntohl(indata & 0xFFFFFF00);
305 /* The server has told us it supports a maximum receive buffer, however, as
306 we don't require one unless we are encrypting data, we tell the server
307 our receive buffer is zero. */
311 /* Allocate our message */
312 messagelen = sizeof(outdata) + username_token.length + 1;
313 message = malloc(messagelen);
315 gss_release_buffer(&gss_status, &username_token);
317 return CURLE_OUT_OF_MEMORY;
320 /* Populate the message with the security layer, client supported receive
321 message size and authorization identity including the 0x00 based
322 terminator. Note: Dispite RFC4752 Section 3.1 stating "The authorization
323 identity is not terminated with the zero-valued (%x00) octet." it seems
324 necessary to include it. */
325 outdata = htonl(max_size) | sec_layer;
326 memcpy(message, &outdata, sizeof(outdata));
327 memcpy(message + sizeof(outdata), username_token.value,
328 username_token.length);
329 message[messagelen - 1] = '\0';
331 /* Free the username token as it is not required anymore */
332 gss_release_buffer(&gss_status, &username_token);
334 /* Setup the "authentication data" security buffer */
335 input_token.value = message;
336 input_token.length = messagelen;
338 /* Encrypt the data */
339 gss_major_status = gss_wrap(&gss_minor_status, krb5->context, 0,
340 GSS_C_QOP_DEFAULT, &input_token, NULL,
342 if(GSS_ERROR(gss_major_status)) {
343 Curl_gss_log_error(data, gss_minor_status, "gss_wrap() failed: ");
345 Curl_safefree(message);
347 return CURLE_OUT_OF_MEMORY;
350 /* Base64 encode the response */
351 result = Curl_base64_encode(data, (char *) output_token.value,
352 output_token.length, outptr, outlen);
354 /* Free the output buffer */
355 gss_release_buffer(&gss_status, &output_token);
357 /* Free the message buffer */
358 Curl_safefree(message);
364 * Curl_sasl_gssapi_cleanup()
366 * This is used to clean up the gssapi specific data.
370 * krb5 [in/out] - The kerberos 5 data struct being cleaned up.
373 void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5)
375 OM_uint32 minor_status;
377 /* Free our security context */
378 if(krb5->context != GSS_C_NO_CONTEXT) {
379 gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
380 krb5->context = GSS_C_NO_CONTEXT;
384 if(krb5->spn != GSS_C_NO_NAME) {
385 gss_release_name(&minor_status, &krb5->spn);
386 krb5->spn = GSS_C_NO_NAME;
390 #endif /* HAVE_GSSAPI && USE_KERBEROS5 */