1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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.
22 ***************************************************************************/
26 #ifdef HAVE_OLD_GSSMIT
27 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
30 #ifndef CURL_DISABLE_HTTP
31 /* -- WIN32 approved -- */
42 #include "http_negotiate.h"
45 #define _MPRINTF_REPLACE /* use our functions only */
46 #include <curl/mprintf.h>
48 /* The last #include file should be: */
52 get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
54 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
55 &conn->data->state.negotiate;
56 OM_uint32 major_status, minor_status;
57 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
61 /* GSSAPI implementation by Globus (known as GSI) requires the name to be
62 of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
63 of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
64 Change following lines if you want to use GSI */
66 /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
73 token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
75 if(token.length + 1 > sizeof(name))
78 snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
81 token.value = (void *) name;
82 major_status = gss_import_name(&minor_status,
84 GSS_C_NT_HOSTBASED_SERVICE,
87 return GSS_ERROR(major_status) ? -1 : 0;
91 log_gss_error(struct connectdata *conn, OM_uint32 error_status, char *prefix)
93 OM_uint32 maj_stat, min_stat;
94 OM_uint32 msg_ctx = 0;
95 gss_buffer_desc status_string;
99 snprintf(buf, sizeof(buf), "%s", prefix);
102 maj_stat = gss_display_status(&min_stat,
108 if(sizeof(buf) > len + status_string.length + 1) {
109 snprintf(buf + len, sizeof(buf) - len,
110 ": %s", (char*) status_string.value);
111 len += status_string.length;
113 gss_release_buffer(&min_stat, &status_string);
114 } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
116 infof(conn->data, "%s", buf);
119 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
122 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
123 &conn->data->state.negotiate;
124 OM_uint32 major_status, minor_status, minor_status2;
125 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
126 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
130 const char* protocol;
132 while(*header && ISSPACE(*header))
134 if(checkprefix("GSS-Negotiate", header)) {
135 protocol = "GSS-Negotiate";
138 else if(checkprefix("Negotiate", header)) {
139 protocol = "Negotiate";
145 if(neg_ctx->context) {
146 if(neg_ctx->gss != gss) {
151 neg_ctx->protocol = protocol;
155 if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
156 /* We finished succesfully our part of authentication, but server
157 * rejected it (since we're again here). Exit with an error since we
158 * can't invent anything better */
159 Curl_cleanup_negotiate(conn->data);
163 if(neg_ctx->server_name == NULL &&
164 (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
167 header += strlen(neg_ctx->protocol);
168 while(*header && ISSPACE(*header))
171 len = strlen(header);
173 int rawlen = Curl_base64_decode(header,
174 (unsigned char **)&input_token.value);
177 input_token.length = rawlen;
179 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
180 if(checkprefix("Negotiate", header)) {
181 ASN1_OBJECT * object = NULL;
183 unsigned char * spnegoToken = NULL;
184 size_t spnegoTokenLength = 0;
185 unsigned char * mechToken = NULL;
186 size_t mechTokenLength = 0;
188 spnegoToken = malloc(input_token.length);
189 if(input_token.value == NULL)
191 spnegoTokenLength = input_token.length;
193 object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
194 if(!parseSpnegoTargetToken(spnegoToken,
204 infof(conn->data, "Parse SPNEGO Target Token failed\n");
207 free(input_token.value);
208 input_token.value = NULL;
209 input_token.value = malloc(mechTokenLength);
210 memcpy(input_token.value, mechToken,mechTokenLength);
211 input_token.length = mechTokenLength;
214 infof(conn->data, "Parse SPNEGO Target Token succeeded\n");
220 major_status = gss_init_sec_context(&minor_status,
223 neg_ctx->server_name,
227 GSS_C_NO_CHANNEL_BINDINGS,
233 if(input_token.length > 0)
234 gss_release_buffer(&minor_status2, &input_token);
235 neg_ctx->status = major_status;
236 if(GSS_ERROR(major_status)) {
237 /* Curl_cleanup_negotiate(conn->data) ??? */
238 log_gss_error(conn, minor_status,
239 (char *)"gss_init_sec_context() failed: ");
243 if(output_token.length == 0) {
247 neg_ctx->output_token = output_token;
248 /* conn->bits.close = FALSE; */
254 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
256 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
257 &conn->data->state.negotiate;
258 char *encoded = NULL;
261 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
262 if(checkprefix("Negotiate", neg_ctx->protocol)) {
263 ASN1_OBJECT * object = NULL;
265 unsigned char * spnegoToken = NULL;
266 size_t spnegoTokenLength = 0;
267 unsigned char * responseToken = NULL;
268 size_t responseTokenLength = 0;
270 responseToken = malloc(neg_ctx->output_token.length);
271 if( responseToken == NULL)
272 return CURLE_OUT_OF_MEMORY;
273 memcpy(responseToken, neg_ctx->output_token.value,
274 neg_ctx->output_token.length);
275 responseTokenLength = neg_ctx->output_token.length;
277 object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
278 if(!makeSpnegoInitialToken (object,
282 &spnegoTokenLength)) {
284 responseToken = NULL;
285 infof(conn->data, "Make SPNEGO Initial Token failed\n");
288 free(neg_ctx->output_token.value);
289 responseToken = NULL;
290 neg_ctx->output_token.value = malloc(spnegoTokenLength);
291 memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
292 neg_ctx->output_token.length = spnegoTokenLength;
295 infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
299 len = Curl_base64_encode(conn->data,
300 neg_ctx->output_token.value,
301 neg_ctx->output_token.length,
305 return CURLE_OUT_OF_MEMORY;
307 conn->allocptr.userpwd =
308 aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
309 neg_ctx->protocol, encoded);
311 Curl_cleanup_negotiate (conn->data);
312 return (conn->allocptr.userpwd == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
315 static void cleanup(struct negotiatedata *neg_ctx)
317 OM_uint32 minor_status;
318 if(neg_ctx->context != GSS_C_NO_CONTEXT)
319 gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
321 if(neg_ctx->output_token.length != 0)
322 gss_release_buffer(&minor_status, &neg_ctx->output_token);
324 if(neg_ctx->server_name != GSS_C_NO_NAME)
325 gss_release_name(&minor_status, &neg_ctx->server_name);
327 memset(neg_ctx, 0, sizeof(*neg_ctx));
330 void Curl_cleanup_negotiate(struct SessionHandle *data)
332 cleanup(&data->state.negotiate);
333 cleanup(&data->state.proxyneg);