1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
26 #ifdef HAVE_OLD_GSSMIT
27 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
30 #ifndef CURL_DISABLE_HTTP
34 #include "curl_gssapi.h"
36 #include "curl_base64.h"
37 #include "http_negotiate.h"
38 #include "curl_memory.h"
41 # include <spnegohelp.h>
44 # include <openssl/objects.h>
49 # error "Can't compile SPNEGO support without OpenSSL."
53 #define _MPRINTF_REPLACE /* use our functions only */
54 #include <curl/mprintf.h>
56 /* The last #include file should be: */
60 get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
62 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
63 &conn->data->state.negotiate;
64 OM_uint32 major_status, minor_status;
65 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
69 /* GSSAPI implementation by Globus (known as GSI) requires the name to be
70 of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
71 of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
72 Change following lines if you want to use GSI */
74 /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
81 token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
83 if(token.length + 1 > sizeof(name))
86 snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
89 token.value = (void *) name;
90 major_status = gss_import_name(&minor_status,
92 GSS_C_NT_HOSTBASED_SERVICE,
95 return GSS_ERROR(major_status) ? -1 : 0;
99 log_gss_error(struct connectdata *conn, OM_uint32 error_status,
102 OM_uint32 maj_stat, min_stat;
103 OM_uint32 msg_ctx = 0;
104 gss_buffer_desc status_string;
108 snprintf(buf, sizeof(buf), "%s", prefix);
111 maj_stat = gss_display_status(&min_stat,
117 if(sizeof(buf) > len + status_string.length + 1) {
118 snprintf(buf + len, sizeof(buf) - len,
119 ": %s", (char*) status_string.value);
120 len += status_string.length;
122 gss_release_buffer(&min_stat, &status_string);
123 } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
125 infof(conn->data, "%s", buf);
128 /* returning zero (0) means success, everything else is treated as "failure"
129 with no care exactly what the failure was */
130 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
133 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
134 &conn->data->state.negotiate;
135 OM_uint32 major_status, minor_status, minor_status2;
136 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
137 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
141 const char* protocol;
143 while(*header && ISSPACE(*header))
145 if(checkprefix("GSS-Negotiate", header)) {
146 protocol = "GSS-Negotiate";
149 else if(checkprefix("Negotiate", header)) {
150 protocol = "Negotiate";
156 if(neg_ctx->context) {
157 if(neg_ctx->gss != gss) {
162 neg_ctx->protocol = protocol;
166 if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
167 /* We finished successfully our part of authentication, but server
168 * rejected it (since we're again here). Exit with an error since we
169 * can't invent anything better */
170 Curl_cleanup_negotiate(conn->data);
174 if(neg_ctx->server_name == NULL &&
175 (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
178 header += strlen(neg_ctx->protocol);
179 while(*header && ISSPACE(*header))
182 len = strlen(header);
184 rawlen = Curl_base64_decode(header,
185 (unsigned char **)&input_token.value);
188 input_token.length = rawlen;
190 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
191 if(checkprefix("Negotiate", header)) {
192 ASN1_OBJECT * object = NULL;
194 unsigned char * spnegoToken = NULL;
195 size_t spnegoTokenLength = 0;
196 unsigned char * mechToken = NULL;
197 size_t mechTokenLength = 0;
199 if(input_token.value == NULL)
200 return CURLE_OUT_OF_MEMORY;
202 spnegoToken = malloc(input_token.length);
203 if(spnegoToken == NULL)
204 return CURLE_OUT_OF_MEMORY;
206 spnegoTokenLength = input_token.length;
208 object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
209 if(!parseSpnegoTargetToken(spnegoToken,
219 infof(conn->data, "Parse SPNEGO Target Token failed\n");
222 free(input_token.value);
223 input_token.value = malloc(mechTokenLength);
224 if(input_token.value == NULL)
225 return CURLE_OUT_OF_MEMORY;
227 memcpy(input_token.value, mechToken,mechTokenLength);
228 input_token.length = mechTokenLength;
231 infof(conn->data, "Parse SPNEGO Target Token succeeded\n");
237 major_status = Curl_gss_init_sec_context(&minor_status,
239 neg_ctx->server_name,
240 GSS_C_NO_CHANNEL_BINDINGS,
244 if(input_token.length > 0)
245 gss_release_buffer(&minor_status2, &input_token);
246 neg_ctx->status = major_status;
247 if(GSS_ERROR(major_status)) {
248 /* Curl_cleanup_negotiate(conn->data) ??? */
249 log_gss_error(conn, minor_status,
250 "gss_init_sec_context() failed: ");
254 if(output_token.length == 0) {
258 neg_ctx->output_token = output_token;
259 /* conn->bits.close = FALSE; */
265 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
267 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
268 &conn->data->state.negotiate;
269 char *encoded = NULL;
273 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
274 if(checkprefix("Negotiate", neg_ctx->protocol)) {
275 ASN1_OBJECT * object = NULL;
277 unsigned char * spnegoToken = NULL;
278 size_t spnegoTokenLength = 0;
279 unsigned char * responseToken = NULL;
280 size_t responseTokenLength = 0;
282 responseToken = malloc(neg_ctx->output_token.length);
283 if(responseToken == NULL)
284 return CURLE_OUT_OF_MEMORY;
285 memcpy(responseToken, neg_ctx->output_token.value,
286 neg_ctx->output_token.length);
287 responseTokenLength = neg_ctx->output_token.length;
289 object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
290 if(!makeSpnegoInitialToken (object,
294 &spnegoTokenLength)) {
296 responseToken = NULL;
297 infof(conn->data, "Make SPNEGO Initial Token failed\n");
301 responseToken = NULL;
302 free(neg_ctx->output_token.value);
303 neg_ctx->output_token.value = malloc(spnegoTokenLength);
304 if(neg_ctx->output_token.value == NULL) {
307 return CURLE_OUT_OF_MEMORY;
309 memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
310 neg_ctx->output_token.length = spnegoTokenLength;
313 infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
317 len = Curl_base64_encode(conn->data,
318 neg_ctx->output_token.value,
319 neg_ctx->output_token.length,
323 return CURLE_OUT_OF_MEMORY;
325 userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
326 neg_ctx->protocol, encoded);
329 conn->allocptr.proxyuserpwd = userp;
331 conn->allocptr.userpwd = userp;
333 Curl_cleanup_negotiate (conn->data);
334 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
337 static void cleanup(struct negotiatedata *neg_ctx)
339 OM_uint32 minor_status;
340 if(neg_ctx->context != GSS_C_NO_CONTEXT)
341 gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
343 if(neg_ctx->output_token.length != 0)
344 gss_release_buffer(&minor_status, &neg_ctx->output_token);
346 if(neg_ctx->server_name != GSS_C_NO_NAME)
347 gss_release_name(&minor_status, &neg_ctx->server_name);
349 memset(neg_ctx, 0, sizeof(*neg_ctx));
352 void Curl_cleanup_negotiate(struct SessionHandle *data)
354 cleanup(&data->state.negotiate);
355 cleanup(&data->state.proxyneg);