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.
21 * $Id: http_negotiate.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
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 -- */
41 #include "curl_base64.h"
42 #include "http_negotiate.h"
46 # include <spnegohelp.h>
49 # include <openssl/objects.h>
54 # error "Can't compile SPNEGO support without OpenSSL."
58 #define _MPRINTF_REPLACE /* use our functions only */
59 #include <curl/mprintf.h>
61 /* The last #include file should be: */
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
;
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 */
86 token
.length
= strlen(service
) + 1 + strlen(proxy
? conn
->proxy
.name
:
88 if(token
.length
+ 1 > sizeof(name
))
91 snprintf(name
, sizeof(name
), "%s@%s", service
, proxy
? conn
->proxy
.name
:
94 token
.value
= (void *) name
;
95 major_status
= gss_import_name(&minor_status
,
97 GSS_C_NT_HOSTBASED_SERVICE
,
100 return GSS_ERROR(major_status
) ? -1 : 0;
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
;
112 snprintf(buf
, sizeof(buf
), "%s", prefix
);
115 maj_stat
= gss_display_status(&min_stat
,
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
,
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
;
145 const char* protocol
;
147 while(*header
&& ISSPACE(*header
))
149 if(checkprefix("GSS-Negotiate", header
)) {
150 protocol
= "GSS-Negotiate";
153 else if(checkprefix("Negotiate", header
)) {
154 protocol
= "Negotiate";
160 if(neg_ctx
->context
) {
161 if(neg_ctx
->gss
!= gss
) {
166 neg_ctx
->protocol
= protocol
;
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
);
178 if(neg_ctx
->server_name
== NULL
&&
179 (ret
= get_gss_name(conn
, proxy
, &neg_ctx
->server_name
)))
182 header
+= strlen(neg_ctx
->protocol
);
183 while(*header
&& ISSPACE(*header
))
186 len
= strlen(header
);
188 int rawlen
= Curl_base64_decode(header
,
189 (unsigned char **)&input_token
.value
);
192 input_token
.length
= rawlen
;
194 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
195 if(checkprefix("Negotiate", header
)) {
196 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(conn
->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(conn
->data
, "Parse SPNEGO Target Token succeeded\n");
241 major_status
= gss_init_sec_context(&minor_status
,
244 neg_ctx
->server_name
,
248 GSS_C_NO_CHANNEL_BINDINGS
,
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: ");
264 if(output_token
.length
== 0) {
268 neg_ctx
->output_token
= output_token
;
269 /* conn->bits.close = FALSE; */
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
;
282 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
283 if(checkprefix("Negotiate", neg_ctx
->protocol
)) {
284 ASN1_OBJECT
* object
= NULL
;
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
,
303 &spnegoTokenLength
)) {
305 responseToken
= NULL
;
306 infof(conn
->data
, "Make SPNEGO Initial Token failed\n");
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
;
316 infof(conn
->data
, "Make SPNEGO Initial Token succeeded\n");
320 len
= Curl_base64_encode(conn
->data
,
321 neg_ctx
->output_token
.value
,
322 neg_ctx
->output_token
.length
,
326 return CURLE_OUT_OF_MEMORY
;
328 conn
->allocptr
.userpwd
=
329 aprintf("%sAuthorization: %s %s\r\n", proxy
? "Proxy-" : "",
330 neg_ctx
->protocol
, 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
);