1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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, minor_status2;
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 while(*header && ISSPACE(*header))
150 if(checkprefix("GSS-Negotiate", header)) {
151 protocol = "GSS-Negotiate";
154 else if(checkprefix("Negotiate", header)) {
155 protocol = "Negotiate";
161 if(neg_ctx->context) {
162 if(neg_ctx->gss != gss) {
167 neg_ctx->protocol = protocol;
171 if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
172 /* We finished successfully our part of authentication, but server
173 * rejected it (since we're again here). Exit with an error since we
174 * can't invent anything better */
175 Curl_cleanup_negotiate(data);
179 if(neg_ctx->server_name == NULL &&
180 (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
183 header += strlen(neg_ctx->protocol);
184 while(*header && ISSPACE(*header))
187 len = strlen(header);
189 error = Curl_base64_decode(header,
190 (unsigned char **)&input_token.value, &rawlen);
191 if(error || rawlen == 0)
193 input_token.length = rawlen;
195 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
196 if(checkprefix("Negotiate", header)) {
197 ASN1_OBJECT * object = NULL;
198 unsigned char * spnegoToken = NULL;
199 size_t spnegoTokenLength = 0;
200 unsigned char * mechToken = NULL;
201 size_t mechTokenLength = 0;
203 if(input_token.value == NULL)
204 return CURLE_OUT_OF_MEMORY;
206 spnegoToken = malloc(input_token.length);
207 if(spnegoToken == NULL)
208 return CURLE_OUT_OF_MEMORY;
210 spnegoTokenLength = input_token.length;
212 object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
213 if(!parseSpnegoTargetToken(spnegoToken,
223 infof(data, "Parse SPNEGO Target Token failed\n");
226 free(input_token.value);
227 input_token.value = malloc(mechTokenLength);
228 if(input_token.value == NULL)
229 return CURLE_OUT_OF_MEMORY;
231 memcpy(input_token.value, mechToken,mechTokenLength);
232 input_token.length = mechTokenLength;
235 infof(data, "Parse SPNEGO Target Token succeeded\n");
241 major_status = Curl_gss_init_sec_context(data,
244 neg_ctx->server_name,
245 GSS_C_NO_CHANNEL_BINDINGS,
249 if(input_token.length > 0)
250 gss_release_buffer(&minor_status2, &input_token);
251 neg_ctx->status = major_status;
252 if(GSS_ERROR(major_status)) {
253 /* Curl_cleanup_negotiate(data) ??? */
254 log_gss_error(conn, minor_status,
255 "gss_init_sec_context() failed: ");
259 if(output_token.length == 0) {
263 neg_ctx->output_token = output_token;
264 /* conn->bits.close = FALSE; */
270 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
272 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
273 &conn->data->state.negotiate;
274 char *encoded = NULL;
279 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
280 if(checkprefix("Negotiate", neg_ctx->protocol)) {
281 ASN1_OBJECT * object = NULL;
282 unsigned char * spnegoToken = NULL;
283 size_t spnegoTokenLength = 0;
284 unsigned char * responseToken = NULL;
285 size_t responseTokenLength = 0;
287 responseToken = malloc(neg_ctx->output_token.length);
288 if(responseToken == NULL)
289 return CURLE_OUT_OF_MEMORY;
290 memcpy(responseToken, neg_ctx->output_token.value,
291 neg_ctx->output_token.length);
292 responseTokenLength = neg_ctx->output_token.length;
294 object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
295 if(!makeSpnegoInitialToken (object,
299 &spnegoTokenLength)) {
301 responseToken = NULL;
302 infof(conn->data, "Make SPNEGO Initial Token failed\n");
306 responseToken = NULL;
307 free(neg_ctx->output_token.value);
308 neg_ctx->output_token.value = malloc(spnegoTokenLength);
309 if(neg_ctx->output_token.value == NULL) {
312 return CURLE_OUT_OF_MEMORY;
314 memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
315 neg_ctx->output_token.length = spnegoTokenLength;
318 infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
322 error = Curl_base64_encode(conn->data,
323 neg_ctx->output_token.value,
324 neg_ctx->output_token.length,
327 Curl_safefree(neg_ctx->output_token.value);
328 neg_ctx->output_token.value = NULL;
333 Curl_safefree(neg_ctx->output_token.value);
334 neg_ctx->output_token.value = NULL;
335 return CURLE_REMOTE_ACCESS_DENIED;
338 userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
339 neg_ctx->protocol, encoded);
342 conn->allocptr.proxyuserpwd = userp;
344 conn->allocptr.userpwd = userp;
346 Curl_cleanup_negotiate (conn->data);
347 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
350 static void cleanup(struct negotiatedata *neg_ctx)
352 OM_uint32 minor_status;
353 if(neg_ctx->context != GSS_C_NO_CONTEXT)
354 gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
356 if(neg_ctx->output_token.length != 0)
357 gss_release_buffer(&minor_status, &neg_ctx->output_token);
359 if(neg_ctx->server_name != GSS_C_NO_NAME)
360 gss_release_name(&minor_status, &neg_ctx->server_name);
362 memset(neg_ctx, 0, sizeof(*neg_ctx));
365 void Curl_cleanup_negotiate(struct SessionHandle *data)
367 cleanup(&data->state.negotiate);
368 cleanup(&data->state.proxyneg);