- Fix the MIT / Heimdal check for good:
[platform/upstream/curl.git] / lib / http_negotiate.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23 #include "setup.h"
24
25 #ifdef HAVE_GSSAPI
26 #ifdef HAVE_OLD_GSSMIT
27 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
28 #endif
29
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>
37
38 #include "urldata.h"
39 #include "sendf.h"
40 #include "strequal.h"
41 #include "base64.h"
42 #include "http_negotiate.h"
43 #include "memory.h"
44
45 #define _MPRINTF_REPLACE /* use our functions only */
46 #include <curl/mprintf.h>
47
48 /* The last #include file should be: */
49 #include "memdebug.h"
50
51 static int
52 get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
53 {
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;
58   char name[2048];
59   const char* service;
60
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 */
65
66   /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
67
68   if(neg_ctx->gss)
69     service = "KHTTP";
70   else
71     service = "HTTP";
72
73   token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
74                                               conn->host.name) + 1;
75   if(token.length + 1 > sizeof(name))
76     return EMSGSIZE;
77
78   snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
79            conn->host.name);
80
81   token.value = (void *) name;
82   major_status = gss_import_name(&minor_status,
83                                  &token,
84                                  GSS_C_NT_HOSTBASED_SERVICE,
85                                  server);
86
87   return GSS_ERROR(major_status) ? -1 : 0;
88 }
89
90 static void
91 log_gss_error(struct connectdata *conn, OM_uint32 error_status, char *prefix)
92 {
93   OM_uint32 maj_stat, min_stat;
94   OM_uint32 msg_ctx = 0;
95   gss_buffer_desc status_string;
96   char buf[1024];
97   size_t len;
98
99   snprintf(buf, sizeof(buf), "%s", prefix);
100   len = strlen(buf);
101   do {
102     maj_stat = gss_display_status(&min_stat,
103                                   error_status,
104                                   GSS_C_MECH_CODE,
105                                   GSS_C_NO_OID,
106                                   &msg_ctx,
107                                   &status_string);
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;
112     }
113     gss_release_buffer(&min_stat, &status_string);
114   } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
115
116   infof(conn->data, "%s", buf);
117 }
118
119 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
120                          const char *header)
121 {
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;
127   int ret;
128   size_t len;
129   bool gss;
130   const char* protocol;
131
132   while(*header && ISSPACE(*header))
133     header++;
134   if(checkprefix("GSS-Negotiate", header)) {
135     protocol = "GSS-Negotiate";
136     gss = TRUE;
137   }
138   else if(checkprefix("Negotiate", header)) {
139     protocol = "Negotiate";
140     gss = FALSE;
141   }
142   else
143     return -1;
144
145   if(neg_ctx->context) {
146     if(neg_ctx->gss != gss) {
147       return -1;
148     }
149   }
150   else {
151     neg_ctx->protocol = protocol;
152     neg_ctx->gss = gss;
153   }
154
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);
160     return -1;
161   }
162
163   if(neg_ctx->server_name == NULL &&
164       (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
165     return ret;
166
167   header += strlen(neg_ctx->protocol);
168   while(*header && ISSPACE(*header))
169     header++;
170
171   len = strlen(header);
172   if(len > 0) {
173     int rawlen = Curl_base64_decode(header,
174                                     (unsigned char **)&input_token.value);
175     if(rawlen < 0)
176       return -1;
177     input_token.length = rawlen;
178
179 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
180     if(checkprefix("Negotiate", header)) {
181         ASN1_OBJECT *   object            = NULL;
182         int             rc                = 1;
183         unsigned char * spnegoToken       = NULL;
184         size_t          spnegoTokenLength = 0;
185         unsigned char * mechToken         = NULL;
186         size_t          mechTokenLength   = 0;
187
188         spnegoToken = malloc(input_token.length);
189         if(input_token.value == NULL)
190           return ENOMEM;
191         spnegoTokenLength = input_token.length;
192
193         object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
194         if(!parseSpnegoTargetToken(spnegoToken,
195                                     spnegoTokenLength,
196                                     NULL,
197                                     NULL,
198                                     &mechToken,
199                                     &mechTokenLength,
200                                     NULL,
201                                     NULL)) {
202           free(spnegoToken);
203           spnegoToken = NULL;
204           infof(conn->data, "Parse SPNEGO Target Token failed\n");
205         }
206         else {
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;
212           free(mechToken);
213           mechToken = NULL;
214           infof(conn->data, "Parse SPNEGO Target Token succeeded\n");
215         }
216     }
217 #endif
218   }
219
220   major_status = gss_init_sec_context(&minor_status,
221                                       GSS_C_NO_CREDENTIAL,
222                                       &neg_ctx->context,
223                                       neg_ctx->server_name,
224                                       GSS_C_NO_OID,
225                                       GSS_C_DELEG_FLAG,
226                                       0,
227                                       GSS_C_NO_CHANNEL_BINDINGS,
228                                       &input_token,
229                                       NULL,
230                                       &output_token,
231                                       NULL,
232                                       NULL);
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: ");
240     return -1;
241   }
242
243   if(output_token.length == 0) {
244     return -1;
245   }
246
247   neg_ctx->output_token = output_token;
248   /* conn->bits.close = FALSE; */
249
250   return 0;
251 }
252
253
254 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
255 {
256   struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
257     &conn->data->state.negotiate;
258   char *encoded = NULL;
259   int len;
260
261 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
262   if(checkprefix("Negotiate", neg_ctx->protocol)) {
263     ASN1_OBJECT *   object            = NULL;
264     int             rc                = 1;
265     unsigned char * spnegoToken       = NULL;
266     size_t          spnegoTokenLength = 0;
267     unsigned char * responseToken       = NULL;
268     size_t          responseTokenLength = 0;
269
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;
276
277     object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
278     if(!makeSpnegoInitialToken (object,
279                                  responseToken,
280                                  responseTokenLength,
281                                  &spnegoToken,
282                                  &spnegoTokenLength)) {
283       free(responseToken);
284       responseToken = NULL;
285       infof(conn->data, "Make SPNEGO Initial Token failed\n");
286     }
287     else {
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;
293       free(spnegoToken);
294       spnegoToken = NULL;
295       infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
296     }
297   }
298 #endif
299   len = Curl_base64_encode(conn->data,
300                            neg_ctx->output_token.value,
301                            neg_ctx->output_token.length,
302                            &encoded);
303
304   if(len == 0)
305     return CURLE_OUT_OF_MEMORY;
306
307   conn->allocptr.userpwd =
308     aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
309             neg_ctx->protocol, encoded);
310   free(encoded);
311   Curl_cleanup_negotiate (conn->data);
312   return (conn->allocptr.userpwd == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
313 }
314
315 static void cleanup(struct negotiatedata *neg_ctx)
316 {
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);
320
321   if(neg_ctx->output_token.length != 0)
322     gss_release_buffer(&minor_status, &neg_ctx->output_token);
323
324   if(neg_ctx->server_name != GSS_C_NO_NAME)
325     gss_release_name(&minor_status, &neg_ctx->server_name);
326
327   memset(neg_ctx, 0, sizeof(*neg_ctx));
328 }
329
330 void Curl_cleanup_negotiate(struct SessionHandle *data)
331 {
332   cleanup(&data->state.negotiate);
333   cleanup(&data->state.proxyneg);
334 }
335
336
337 #endif
338 #endif