1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
26 #ifdef HAVE_OLD_GSSMIT
27 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
31 #ifndef CURL_DISABLE_HTTP
35 #include "curl_gssapi.h"
37 #include "curl_base64.h"
38 #include "http_negotiate.h"
39 #include "curl_memory.h"
43 # include <spnegohelp.h>
46 # include <openssl/objects.h>
51 # error "Can't compile SPNEGO support without OpenSSL."
55 #define _MPRINTF_REPLACE /* use our functions only */
56 #include <curl/mprintf.h>
58 /* The last #include file should be: */
62 get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
64 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
65 &conn->data->state.negotiate;
66 OM_uint32 major_status, minor_status;
67 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
71 /* GSSAPI implementation by Globus (known as GSI) requires the name to be
72 of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
73 of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
74 Change following lines if you want to use GSI */
76 /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
83 token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
85 if(token.length + 1 > sizeof(name))
88 snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
91 token.value = (void *) name;
92 major_status = gss_import_name(&minor_status,
94 GSS_C_NT_HOSTBASED_SERVICE,
97 return GSS_ERROR(major_status) ? -1 : 0;
101 log_gss_error(struct connectdata *conn, OM_uint32 error_status,
104 OM_uint32 maj_stat, min_stat;
105 OM_uint32 msg_ctx = 0;
106 gss_buffer_desc status_string;
110 snprintf(buf, sizeof(buf), "%s", prefix);
113 maj_stat = gss_display_status(&min_stat,
119 if(sizeof(buf) > len + status_string.length + 1) {
120 snprintf(buf + len, sizeof(buf) - len,
121 ": %s", (char*) status_string.value);
122 len += status_string.length;
124 gss_release_buffer(&min_stat, &status_string);
125 } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
127 infof(conn->data, "%s\n", buf);
130 /* returning zero (0) means success, everything else is treated as "failure"
131 with no care exactly what the failure was */
132 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
135 struct SessionHandle *data = conn->data;
136 struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
137 &data->state.negotiate;
138 OM_uint32 major_status, minor_status, discard_st;
139 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
140 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
145 const char* protocol;
148 if(checkprefix("GSS-Negotiate", header)) {
149 protocol = "GSS-Negotiate";
152 else if(checkprefix("Negotiate", header)) {
153 protocol = "Negotiate";
159 if(neg_ctx->context) {
160 if(neg_ctx->gss != gss) {
165 neg_ctx->protocol = protocol;
169 if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
170 /* We finished successfully our part of authentication, but server
171 * rejected it (since we're again here). Exit with an error since we
172 * can't invent anything better */
173 Curl_cleanup_negotiate(data);
177 if(neg_ctx->server_name == NULL &&
178 (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
181 header += strlen(neg_ctx->protocol);
182 while(*header && ISSPACE(*header))
185 len = strlen(header);
187 error = Curl_base64_decode(header,
188 (unsigned char **)&input_token.value, &rawlen);
189 if(error || rawlen == 0)
191 input_token.length = rawlen;
193 DEBUGASSERT(input_token.value != NULL);
195 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
196 if(checkprefix("Negotiate", header)) {
197 unsigned char *spnegoToken = NULL;
198 size_t spnegoTokenLength = 0;
199 gss_buffer_desc mechToken = GSS_C_EMPTY_BUFFER;
201 spnegoToken = malloc(input_token.length);
202 if(spnegoToken == NULL) {
203 Curl_safefree(input_token.value);
204 return CURLE_OUT_OF_MEMORY;
206 memcpy(spnegoToken, input_token.value, input_token.length);
207 spnegoTokenLength = input_token.length;
209 if(!parseSpnegoTargetToken(spnegoToken,
213 (unsigned char**)&mechToken.value,
217 Curl_safefree(spnegoToken);
218 infof(data, "Parse SPNEGO Target Token failed\n");
220 else if(!mechToken.value || !mechToken.length) {
221 Curl_safefree(spnegoToken);
223 gss_release_buffer(&discard_st, &mechToken);
224 infof(data, "Parse SPNEGO Target Token succeeded (NULL token)\n");
227 Curl_safefree(spnegoToken);
228 Curl_safefree(input_token.value);
229 input_token.value = malloc(mechToken.length);
230 if(input_token.value == NULL) {
231 gss_release_buffer(&discard_st, &mechToken);
232 return CURLE_OUT_OF_MEMORY;
234 memcpy(input_token.value, mechToken.value, mechToken.length);
235 input_token.length = mechToken.length;
236 gss_release_buffer(&discard_st, &mechToken);
237 infof(data, "Parse SPNEGO Target Token succeeded\n");
243 major_status = Curl_gss_init_sec_context(data,
246 neg_ctx->server_name,
247 GSS_C_NO_CHANNEL_BINDINGS,
251 Curl_safefree(input_token.value);
253 neg_ctx->status = major_status;
254 if(GSS_ERROR(major_status)) {
255 if(output_token.value)
256 gss_release_buffer(&discard_st, &output_token);
257 log_gss_error(conn, minor_status, "gss_init_sec_context() failed: ");
261 if(!output_token.value || !output_token.length) {
262 if(output_token.value)
263 gss_release_buffer(&discard_st, &output_token);
267 neg_ctx->output_token = output_token;
272 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
274 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
275 &conn->data->state.negotiate;
276 char *encoded = NULL;
280 OM_uint32 discard_st;
282 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
283 if(checkprefix("Negotiate", neg_ctx->protocol)) {
284 ASN1_OBJECT *object = NULL;
285 unsigned char *responseToken = NULL;
286 size_t responseTokenLength = 0;
287 gss_buffer_desc spnegoToken = GSS_C_EMPTY_BUFFER;
289 responseToken = malloc(neg_ctx->output_token.length);
290 if(responseToken == NULL)
291 return CURLE_OUT_OF_MEMORY;
292 memcpy(responseToken, neg_ctx->output_token.value,
293 neg_ctx->output_token.length);
294 responseTokenLength = neg_ctx->output_token.length;
296 object = OBJ_txt2obj("1.2.840.113554.1.2.2", 1);
298 Curl_safefree(responseToken);
299 return CURLE_OUT_OF_MEMORY;
302 if(!makeSpnegoInitialToken(object,
305 (unsigned char**)&spnegoToken.value,
306 &spnegoToken.length)) {
307 Curl_safefree(responseToken);
308 ASN1_OBJECT_free(object);
309 infof(conn->data, "Make SPNEGO Initial Token failed\n");
311 else if(!spnegoToken.value || !spnegoToken.length) {
312 Curl_safefree(responseToken);
313 ASN1_OBJECT_free(object);
314 if(spnegoToken.value)
315 gss_release_buffer(&discard_st, &spnegoToken);
316 infof(conn->data, "Make SPNEGO Initial Token succeeded (NULL token)\n");
319 Curl_safefree(responseToken);
320 ASN1_OBJECT_free(object);
321 gss_release_buffer(&discard_st, &neg_ctx->output_token);
322 neg_ctx->output_token.value = spnegoToken.value;
323 neg_ctx->output_token.length = spnegoToken.length;
324 infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
328 error = Curl_base64_encode(conn->data,
329 neg_ctx->output_token.value,
330 neg_ctx->output_token.length,
333 gss_release_buffer(&discard_st, &neg_ctx->output_token);
334 neg_ctx->output_token.value = NULL;
335 neg_ctx->output_token.length = 0;
339 if(!encoded || !len) {
340 gss_release_buffer(&discard_st, &neg_ctx->output_token);
341 neg_ctx->output_token.value = NULL;
342 neg_ctx->output_token.length = 0;
343 return CURLE_REMOTE_ACCESS_DENIED;
346 userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
347 neg_ctx->protocol, encoded);
349 Curl_safefree(conn->allocptr.proxyuserpwd);
350 conn->allocptr.proxyuserpwd = userp;
353 Curl_safefree(conn->allocptr.userpwd);
354 conn->allocptr.userpwd = userp;
357 Curl_safefree(encoded);
358 Curl_cleanup_negotiate(conn->data);
360 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
363 static void cleanup(struct negotiatedata *neg_ctx)
365 OM_uint32 minor_status;
366 if(neg_ctx->context != GSS_C_NO_CONTEXT)
367 gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
369 if(neg_ctx->output_token.value)
370 gss_release_buffer(&minor_status, &neg_ctx->output_token);
372 if(neg_ctx->server_name != GSS_C_NO_NAME)
373 gss_release_name(&minor_status, &neg_ctx->server_name);
375 memset(neg_ctx, 0, sizeof(*neg_ctx));
378 void Curl_cleanup_negotiate(struct SessionHandle *data)
380 cleanup(&data->state.negotiate);
381 cleanup(&data->state.proxyneg);