Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / http_negotiate.c
blob769ed130db5f9745d92eb8881c3935965eabc5d3
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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.
21 * $Id: http_negotiate.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
23 #include "setup.h"
25 #ifdef HAVE_GSSAPI
26 #ifdef HAVE_OLD_GSSMIT
27 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
28 #endif
30 #ifndef CURL_DISABLE_HTTP
31 /* -- WIN32 approved -- */
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <ctype.h>
38 #include "urldata.h"
39 #include "sendf.h"
40 #include "strequal.h"
41 #include "curl_base64.h"
42 #include "http_negotiate.h"
43 #include "memory.h"
45 #ifdef HAVE_SPNEGO
46 # include <spnegohelp.h>
47 # ifdef USE_SSLEAY
48 # ifdef USE_OPENSSL
49 # include <openssl/objects.h>
50 # else
51 # include <objects.h>
52 # endif
53 # else
54 # error "Can't compile SPNEGO support without OpenSSL."
55 # endif
56 #endif
58 #define _MPRINTF_REPLACE /* use our functions only */
59 #include <curl/mprintf.h>
61 /* The last #include file should be: */
62 #include "memdebug.h"
64 static int
65 get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
67 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
68 &conn->data->state.negotiate;
69 OM_uint32 major_status, minor_status;
70 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
71 char name[2048];
72 const char* service;
74 /* GSSAPI implementation by Globus (known as GSI) requires the name to be
75 of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
76 of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
77 Change following lines if you want to use GSI */
79 /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
81 if(neg_ctx->gss)
82 service = "KHTTP";
83 else
84 service = "HTTP";
86 token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
87 conn->host.name) + 1;
88 if(token.length + 1 > sizeof(name))
89 return EMSGSIZE;
91 snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
92 conn->host.name);
94 token.value = (void *) name;
95 major_status = gss_import_name(&minor_status,
96 &token,
97 GSS_C_NT_HOSTBASED_SERVICE,
98 server);
100 return GSS_ERROR(major_status) ? -1 : 0;
103 static void
104 log_gss_error(struct connectdata *conn, OM_uint32 error_status, char *prefix)
106 OM_uint32 maj_stat, min_stat;
107 OM_uint32 msg_ctx = 0;
108 gss_buffer_desc status_string;
109 char buf[1024];
110 size_t len;
112 snprintf(buf, sizeof(buf), "%s", prefix);
113 len = strlen(buf);
114 do {
115 maj_stat = gss_display_status(&min_stat,
116 error_status,
117 GSS_C_MECH_CODE,
118 GSS_C_NO_OID,
119 &msg_ctx,
120 &status_string);
121 if(sizeof(buf) > len + status_string.length + 1) {
122 snprintf(buf + len, sizeof(buf) - len,
123 ": %s", (char*) status_string.value);
124 len += status_string.length;
126 gss_release_buffer(&min_stat, &status_string);
127 } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
129 infof(conn->data, "%s", buf);
132 /* returning zero (0) means success, everything else is treated as "failure"
133 with no care exactly what the failure was */
134 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
135 const char *header)
137 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
138 &conn->data->state.negotiate;
139 OM_uint32 major_status, minor_status, minor_status2;
140 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
141 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
142 int ret;
143 size_t len;
144 bool gss;
145 const char* protocol;
147 while(*header && ISSPACE(*header))
148 header++;
149 if(checkprefix("GSS-Negotiate", header)) {
150 protocol = "GSS-Negotiate";
151 gss = TRUE;
153 else if(checkprefix("Negotiate", header)) {
154 protocol = "Negotiate";
155 gss = FALSE;
157 else
158 return -1;
160 if(neg_ctx->context) {
161 if(neg_ctx->gss != gss) {
162 return -1;
165 else {
166 neg_ctx->protocol = protocol;
167 neg_ctx->gss = gss;
170 if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
171 /* We finished succesfully our part of authentication, but server
172 * rejected it (since we're again here). Exit with an error since we
173 * can't invent anything better */
174 Curl_cleanup_negotiate(conn->data);
175 return -1;
178 if(neg_ctx->server_name == NULL &&
179 (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
180 return ret;
182 header += strlen(neg_ctx->protocol);
183 while(*header && ISSPACE(*header))
184 header++;
186 len = strlen(header);
187 if(len > 0) {
188 int rawlen = Curl_base64_decode(header,
189 (unsigned char **)&input_token.value);
190 if(rawlen < 0)
191 return -1;
192 input_token.length = rawlen;
194 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
195 if(checkprefix("Negotiate", header)) {
196 ASN1_OBJECT * object = NULL;
197 int rc = 1;
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,
214 spnegoTokenLength,
215 NULL,
216 NULL,
217 &mechToken,
218 &mechTokenLength,
219 NULL,
220 NULL)) {
221 free(spnegoToken);
222 spnegoToken = NULL;
223 infof(conn->data, "Parse SPNEGO Target Token failed\n");
225 else {
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;
233 free(mechToken);
234 mechToken = NULL;
235 infof(conn->data, "Parse SPNEGO Target Token succeeded\n");
238 #endif
241 major_status = gss_init_sec_context(&minor_status,
242 GSS_C_NO_CREDENTIAL,
243 &neg_ctx->context,
244 neg_ctx->server_name,
245 GSS_C_NO_OID,
246 GSS_C_DELEG_FLAG,
248 GSS_C_NO_CHANNEL_BINDINGS,
249 &input_token,
250 NULL,
251 &output_token,
252 NULL,
253 NULL);
254 if(input_token.length > 0)
255 gss_release_buffer(&minor_status2, &input_token);
256 neg_ctx->status = major_status;
257 if(GSS_ERROR(major_status)) {
258 /* Curl_cleanup_negotiate(conn->data) ??? */
259 log_gss_error(conn, minor_status,
260 (char *)"gss_init_sec_context() failed: ");
261 return -1;
264 if(output_token.length == 0) {
265 return -1;
268 neg_ctx->output_token = output_token;
269 /* conn->bits.close = FALSE; */
271 return 0;
275 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
277 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
278 &conn->data->state.negotiate;
279 char *encoded = NULL;
280 int len;
282 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
283 if(checkprefix("Negotiate", neg_ctx->protocol)) {
284 ASN1_OBJECT * object = NULL;
285 int rc = 1;
286 unsigned char * spnegoToken = NULL;
287 size_t spnegoTokenLength = 0;
288 unsigned char * responseToken = NULL;
289 size_t responseTokenLength = 0;
291 responseToken = malloc(neg_ctx->output_token.length);
292 if( responseToken == NULL)
293 return CURLE_OUT_OF_MEMORY;
294 memcpy(responseToken, neg_ctx->output_token.value,
295 neg_ctx->output_token.length);
296 responseTokenLength = neg_ctx->output_token.length;
298 object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
299 if(!makeSpnegoInitialToken (object,
300 responseToken,
301 responseTokenLength,
302 &spnegoToken,
303 &spnegoTokenLength)) {
304 free(responseToken);
305 responseToken = NULL;
306 infof(conn->data, "Make SPNEGO Initial Token failed\n");
308 else {
309 free(neg_ctx->output_token.value);
310 responseToken = NULL;
311 neg_ctx->output_token.value = malloc(spnegoTokenLength);
312 memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
313 neg_ctx->output_token.length = spnegoTokenLength;
314 free(spnegoToken);
315 spnegoToken = NULL;
316 infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
319 #endif
320 len = Curl_base64_encode(conn->data,
321 neg_ctx->output_token.value,
322 neg_ctx->output_token.length,
323 &encoded);
325 if(len == 0)
326 return CURLE_OUT_OF_MEMORY;
328 conn->allocptr.userpwd =
329 aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
330 neg_ctx->protocol, encoded);
331 free(encoded);
332 Curl_cleanup_negotiate (conn->data);
333 return (conn->allocptr.userpwd == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
336 static void cleanup(struct negotiatedata *neg_ctx)
338 OM_uint32 minor_status;
339 if(neg_ctx->context != GSS_C_NO_CONTEXT)
340 gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
342 if(neg_ctx->output_token.length != 0)
343 gss_release_buffer(&minor_status, &neg_ctx->output_token);
345 if(neg_ctx->server_name != GSS_C_NO_NAME)
346 gss_release_name(&minor_status, &neg_ctx->server_name);
348 memset(neg_ctx, 0, sizeof(*neg_ctx));
351 void Curl_cleanup_negotiate(struct SessionHandle *data)
353 cleanup(&data->state.negotiate);
354 cleanup(&data->state.proxyneg);
358 #endif
359 #endif