1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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"
27 #if !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
29 #ifdef HAVE_OLD_GSSMIT
30 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
36 #include "curl_gssapi.h"
38 #include "curl_base64.h"
39 #include "http_negotiate.h"
40 #include "curl_memory.h"
43 #define _MPRINTF_REPLACE /* use our functions only */
44 #include <curl/mprintf.h>
46 /* The last #include file should be: */
50 get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
52 OM_uint32 major_status, minor_status;
53 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
55 const char* service = "HTTP";
57 token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
59 if(token.length + 1 > sizeof(name))
62 snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
65 token.value = (void *) name;
66 major_status = gss_import_name(&minor_status,
68 GSS_C_NT_HOSTBASED_SERVICE,
71 return GSS_ERROR(major_status) ? -1 : 0;
74 /* returning zero (0) means success, everything else is treated as "failure"
75 with no care exactly what the failure was */
76 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
79 struct SessionHandle *data = conn->data;
80 struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
81 &data->state.negotiate;
82 OM_uint32 major_status, minor_status, discard_st;
83 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
84 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
90 if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
91 /* We finished successfully our part of authentication, but server
92 * rejected it (since we're again here). Exit with an error since we
93 * can't invent anything better */
94 Curl_cleanup_negotiate(data);
98 if(neg_ctx->server_name == NULL &&
99 (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
102 header += strlen("Negotiate");
103 while(*header && ISSPACE(*header))
106 len = strlen(header);
108 result = Curl_base64_decode(header, (unsigned char **)&input_token.value,
110 if(result || rawlen == 0)
112 input_token.length = rawlen;
114 DEBUGASSERT(input_token.value != NULL);
117 major_status = Curl_gss_init_sec_context(data,
120 neg_ctx->server_name,
121 &Curl_spnego_mech_oid,
122 GSS_C_NO_CHANNEL_BINDINGS,
127 Curl_safefree(input_token.value);
129 neg_ctx->status = major_status;
130 if(GSS_ERROR(major_status)) {
131 if(output_token.value)
132 gss_release_buffer(&discard_st, &output_token);
133 Curl_gss_log_error(conn->data, minor_status,
134 "gss_init_sec_context() failed: ");
138 if(!output_token.value || !output_token.length) {
139 if(output_token.value)
140 gss_release_buffer(&discard_st, &output_token);
144 neg_ctx->output_token = output_token;
149 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
151 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
152 &conn->data->state.negotiate;
153 char *encoded = NULL;
157 OM_uint32 discard_st;
159 result = Curl_base64_encode(conn->data,
160 neg_ctx->output_token.value,
161 neg_ctx->output_token.length,
164 gss_release_buffer(&discard_st, &neg_ctx->output_token);
165 neg_ctx->output_token.value = NULL;
166 neg_ctx->output_token.length = 0;
170 if(!encoded || !len) {
171 gss_release_buffer(&discard_st, &neg_ctx->output_token);
172 neg_ctx->output_token.value = NULL;
173 neg_ctx->output_token.length = 0;
174 return CURLE_REMOTE_ACCESS_DENIED;
177 userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
180 Curl_safefree(conn->allocptr.proxyuserpwd);
181 conn->allocptr.proxyuserpwd = userp;
184 Curl_safefree(conn->allocptr.userpwd);
185 conn->allocptr.userpwd = userp;
188 Curl_safefree(encoded);
190 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
193 static void cleanup(struct negotiatedata *neg_ctx)
195 OM_uint32 minor_status;
196 if(neg_ctx->context != GSS_C_NO_CONTEXT)
197 gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
199 if(neg_ctx->output_token.value)
200 gss_release_buffer(&minor_status, &neg_ctx->output_token);
202 if(neg_ctx->server_name != GSS_C_NO_NAME)
203 gss_release_name(&minor_status, &neg_ctx->server_name);
205 memset(neg_ctx, 0, sizeof(*neg_ctx));
208 void Curl_cleanup_negotiate(struct SessionHandle *data)
210 cleanup(&data->state.negotiate);
211 cleanup(&data->state.proxyneg);
214 #endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */
216 #endif /* HAVE_GSSAPI */