1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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 * $Id: http_negotiate.c,v 1.7 2004/01/07 09:19:35 bagder Exp $
22 ***************************************************************************/
27 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
30 #ifndef CURL_DISABLE_HTTP
31 /* -- WIN32 approved -- */
43 #include "http_negotiate.h"
45 #define _MPRINTF_REPLACE /* use our functions only */
46 #include <curl/mprintf.h>
48 /* The last #include file should be: */
54 get_gss_name(struct connectdata *conn, gss_name_t *server)
56 struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
57 OM_uint32 major_status, minor_status;
58 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
62 /* GSSAPI implementation by Globus (known as GSI) requires the name to be
63 of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
64 of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
65 Change following lines if you want to use GSI */
67 /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
74 token.length = strlen(service) + 1 + strlen(conn->hostname) + 1;
75 if (token.length + 1 > sizeof(name))
77 sprintf(name, "%s@%s", service, conn->hostname);
79 token.value = (void *) name;
80 major_status = gss_import_name(&minor_status,
82 GSS_C_NT_HOSTBASED_SERVICE,
85 return GSS_ERROR(major_status) ? -1 : 0;
89 log_gss_error(struct connectdata *conn, OM_uint32 error_status, char *prefix)
91 OM_uint32 maj_stat, min_stat;
92 OM_uint32 msg_ctx = 0;
93 gss_buffer_desc status_string;
97 snprintf(buf, sizeof(buf), "%s", prefix);
100 maj_stat = gss_display_status (&min_stat,
106 if (sizeof(buf) > len + status_string.length + 1) {
107 sprintf(buf + len, ": %s", (char*) status_string.value);
108 len += status_string.length;
110 gss_release_buffer(&min_stat, &status_string);
111 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
113 infof(conn->data, buf);
116 int Curl_input_negotiate(struct connectdata *conn, char *header)
118 struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
119 OM_uint32 major_status, minor_status, minor_status2;
120 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
121 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
125 const char* protocol;
127 while(*header && isspace((int)*header))
129 if(checkprefix("GSS-Negotiate", header)) {
130 protocol = "GSS-Negotiate";
133 else if (checkprefix("Negotiate", header)) {
134 protocol = "Negotiate";
140 if (neg_ctx->context) {
141 if (neg_ctx->gss != gss) {
146 neg_ctx->protocol = protocol;
150 if (neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
151 /* We finished succesfully our part of authentication, but server
152 * rejected it (since we're again here). Exit with an error since we
153 * can't invent anything better */
154 Curl_cleanup_negotiate(conn->data);
158 if (neg_ctx->server_name == NULL &&
159 (ret = get_gss_name(conn, &neg_ctx->server_name)))
162 header += strlen(neg_ctx->protocol);
163 while(*header && isspace((int)*header))
166 len = strlen(header);
169 input_token.length = (len+3)/4 * 3;
170 input_token.value = malloc(input_token.length);
171 if (input_token.value == NULL)
173 rawlen = Curl_base64_decode(header, input_token.value);
176 input_token.length = rawlen;
178 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
179 if (checkprefix("Negotiate", header)) {
180 ASN1_OBJECT * object = NULL;
182 unsigned char * spnegoToken = NULL;
183 size_t spnegoTokenLength = 0;
184 unsigned char * mechToken = NULL;
185 size_t mechTokenLength = 0;
187 spnegoToken = malloc(input_token.length);
188 if (input_token.value == NULL)
190 spnegoTokenLength = input_token.length;
192 object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
193 if (!parseSpnegoTargetToken(spnegoToken,
203 infof(conn->data, "Parse SPNEGO Target Token failed\n");
206 free(input_token.value);
207 input_token.value = NULL;
208 input_token.value = malloc(mechTokenLength);
209 memcpy(input_token.value, mechToken,mechTokenLength);
210 input_token.length = mechTokenLength;
213 infof(conn->data, "Parse SPNEGO Target Token succeded\n");
219 major_status = gss_init_sec_context(&minor_status,
222 neg_ctx->server_name,
226 GSS_C_NO_CHANNEL_BINDINGS,
232 if (input_token.length > 0)
233 gss_release_buffer(&minor_status2, &input_token);
234 neg_ctx->status = major_status;
235 if (GSS_ERROR(major_status)) {
236 /* Curl_cleanup_negotiate(conn->data) ??? */
237 log_gss_error(conn, minor_status,
238 (char *)"gss_init_sec_context() failed: ");
242 if (output_token.length == 0) {
246 neg_ctx->output_token = output_token;
247 /* conn->bits.close = FALSE; */
253 CURLcode Curl_output_negotiate(struct connectdata *conn)
255 struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
256 OM_uint32 minor_status;
257 char *encoded = NULL;
260 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
261 if (checkprefix("Negotiate",neg_ctx->protocol)) {
262 ASN1_OBJECT * object = NULL;
264 unsigned char * spnegoToken = NULL;
265 size_t spnegoTokenLength = 0;
266 unsigned char * responseToken = NULL;
267 size_t responseTokenLength = 0;
269 responseToken = malloc(neg_ctx->output_token.length);
270 if ( responseToken == NULL)
271 return CURLE_OUT_OF_MEMORY;
272 memcpy(responseToken, neg_ctx->output_token.value,
273 neg_ctx->output_token.length);
274 responseTokenLength = neg_ctx->output_token.length;
276 object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
277 if (!makeSpnegoInitialToken (object,
281 &spnegoTokenLength)) {
283 responseToken = NULL;
284 infof(conn->data, "Make SPNEGO Initial Token failed\n");
287 free(neg_ctx->output_token.value);
288 responseToken = NULL;
289 neg_ctx->output_token.value = malloc(spnegoTokenLength);
290 memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
291 neg_ctx->output_token.length = spnegoTokenLength;
294 infof(conn->data, "Make SPNEGO Initial Token succeded\n");
298 len = Curl_base64_encode(neg_ctx->output_token.value,
299 neg_ctx->output_token.length,
303 return CURLE_OUT_OF_MEMORY;
305 conn->allocptr.userpwd =
306 aprintf("Authorization: %s %s\r\n", neg_ctx->protocol, encoded);
308 gss_release_buffer(&minor_status, &neg_ctx->output_token);
309 return (conn->allocptr.userpwd == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
312 void Curl_cleanup_negotiate(struct SessionHandle *data)
314 OM_uint32 minor_status;
315 struct negotiatedata *neg_ctx = &data->state.negotiate;
317 if (neg_ctx->context != GSS_C_NO_CONTEXT)
318 gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
320 if (neg_ctx->output_token.length != 0)
321 gss_release_buffer(&minor_status, &neg_ctx->output_token);
323 if (neg_ctx->server_name != GSS_C_NO_NAME)
324 gss_release_name(&minor_status, &neg_ctx->server_name);
326 memset(neg_ctx, 0, sizeof(*neg_ctx));