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.
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"
46 #define _MPRINTF_REPLACE /* use our functions only */
47 #include <curl/mprintf.h>
49 /* The last #include file should be: */
53 get_gss_name(struct connectdata *conn, gss_name_t *server)
55 struct negotiatedata *neg_ctx = &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(conn->host.name) + 1;
74 if (token.length + 1 > sizeof(name))
77 snprintf(name, sizeof(name), "%s@%s", service, conn->host.name);
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 snprintf(buf + len, sizeof(buf) - len,
108 ": %s", (char*) status_string.value);
109 len += status_string.length;
111 gss_release_buffer(&min_stat, &status_string);
112 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
114 infof(conn->data, buf);
117 int Curl_input_negotiate(struct connectdata *conn, char *header)
119 struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
120 OM_uint32 major_status, minor_status, minor_status2;
121 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
122 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
126 const char* protocol;
128 while(*header && isspace((int)*header))
130 if(checkprefix("GSS-Negotiate", header)) {
131 protocol = "GSS-Negotiate";
134 else if (checkprefix("Negotiate", header)) {
135 protocol = "Negotiate";
141 if (neg_ctx->context) {
142 if (neg_ctx->gss != gss) {
147 neg_ctx->protocol = protocol;
151 if (neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
152 /* We finished succesfully our part of authentication, but server
153 * rejected it (since we're again here). Exit with an error since we
154 * can't invent anything better */
155 Curl_cleanup_negotiate(conn->data);
159 if (neg_ctx->server_name == NULL &&
160 (ret = get_gss_name(conn, &neg_ctx->server_name)))
163 header += strlen(neg_ctx->protocol);
164 while(*header && isspace((int)*header))
167 len = strlen(header);
170 input_token.length = (len+3)/4 * 3;
171 input_token.value = malloc(input_token.length);
172 if (input_token.value == NULL)
174 rawlen = Curl_base64_decode(header, 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 succeded\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)
256 struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
257 OM_uint32 minor_status;
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 succeded\n");
299 len = Curl_base64_encode(neg_ctx->output_token.value,
300 neg_ctx->output_token.length,
304 return CURLE_OUT_OF_MEMORY;
306 conn->allocptr.userpwd =
307 aprintf("Authorization: %s %s\r\n", neg_ctx->protocol, encoded);
309 gss_release_buffer(&minor_status, &neg_ctx->output_token);
310 return (conn->allocptr.userpwd == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
313 void Curl_cleanup_negotiate(struct SessionHandle *data)
315 OM_uint32 minor_status;
316 struct negotiatedata *neg_ctx = &data->state.negotiate;
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));