- David McCreedy made changes to allow base64 encoding/decoding to work on
[platform/upstream/curl.git] / lib / http_negotiate.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, 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_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, gss_name_t *server)
53 {
54   struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
55   OM_uint32 major_status, minor_status;
56   gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
57   char name[2048];
58   const char* service;
59
60   /* GSSAPI implementation by Globus (known as GSI) requires the name to be
61      of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
62      of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
63      Change following lines if you want to use GSI */
64
65   /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
66
67   if (neg_ctx->gss)
68     service = "KHTTP";
69   else
70     service = "HTTP";
71
72   token.length = strlen(service) + 1 + strlen(conn->host.name) + 1;
73   if (token.length + 1 > sizeof(name))
74     return EMSGSIZE;
75
76   snprintf(name, sizeof(name), "%s@%s", service, conn->host.name);
77
78   token.value = (void *) name;
79   major_status = gss_import_name(&minor_status,
80                                  &token,
81                                  GSS_C_NT_HOSTBASED_SERVICE,
82                                  server);
83
84   return GSS_ERROR(major_status) ? -1 : 0;
85 }
86
87 static void
88 log_gss_error(struct connectdata *conn, OM_uint32 error_status, char *prefix)
89 {
90   OM_uint32 maj_stat, min_stat;
91   OM_uint32 msg_ctx = 0;
92   gss_buffer_desc status_string;
93   char buf[1024];
94   size_t len;
95
96   snprintf(buf, sizeof(buf), "%s", prefix);
97   len = strlen(buf);
98   do {
99     maj_stat = gss_display_status (&min_stat,
100                                    error_status,
101                                    GSS_C_MECH_CODE,
102                                    GSS_C_NO_OID,
103                                    &msg_ctx,
104                                    &status_string);
105       if (sizeof(buf) > len + status_string.length + 1) {
106         snprintf(buf + len, sizeof(buf) - len,
107                  ": %s", (char*) status_string.value);
108       len += status_string.length;
109     }
110     gss_release_buffer(&min_stat, &status_string);
111   } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
112
113   infof(conn->data, "%s", buf);
114 }
115
116 int Curl_input_negotiate(struct connectdata *conn, char *header)
117 {
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;
122   int ret;
123   size_t len;
124   bool gss;
125   const char* protocol;
126
127   while(*header && ISSPACE(*header))
128     header++;
129   if(checkprefix("GSS-Negotiate", header)) {
130     protocol = "GSS-Negotiate";
131     gss = TRUE;
132   }
133   else if (checkprefix("Negotiate", header)) {
134     protocol = "Negotiate";
135     gss = FALSE;
136   }
137   else
138     return -1;
139
140   if (neg_ctx->context) {
141     if (neg_ctx->gss != gss) {
142       return -1;
143     }
144   }
145   else {
146     neg_ctx->protocol = protocol;
147     neg_ctx->gss = gss;
148   }
149
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);
155     return -1;
156   }
157
158   if (neg_ctx->server_name == NULL &&
159       (ret = get_gss_name(conn, &neg_ctx->server_name)))
160     return ret;
161
162   header += strlen(neg_ctx->protocol);
163   while(*header && ISSPACE(*header))
164     header++;
165
166   len = strlen(header);
167   if (len > 0) {
168     int rawlen = Curl_base64_decode(header, (unsigned char **)&input_token.value);
169     if (rawlen < 0)
170       return -1;
171     input_token.length = rawlen;
172
173 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
174     if (checkprefix("Negotiate", header)) {
175         ASN1_OBJECT *   object            = NULL;
176         int             rc                = 1;
177         unsigned char * spnegoToken       = NULL;
178         size_t          spnegoTokenLength = 0;
179         unsigned char * mechToken         = NULL;
180         size_t          mechTokenLength   = 0;
181
182         spnegoToken = malloc(input_token.length);
183         if (input_token.value == NULL)
184           return ENOMEM;
185         spnegoTokenLength = input_token.length;
186
187         object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
188         if (!parseSpnegoTargetToken(spnegoToken,
189                                     spnegoTokenLength,
190                                     NULL,
191                                     NULL,
192                                     &mechToken,
193                                     &mechTokenLength,
194                                     NULL,
195                                     NULL)) {
196           free(spnegoToken);
197           spnegoToken = NULL;
198           infof(conn->data, "Parse SPNEGO Target Token failed\n");
199         }
200         else {
201           free(input_token.value);
202           input_token.value = NULL;
203           input_token.value = malloc(mechTokenLength);
204           memcpy(input_token.value, mechToken,mechTokenLength);
205           input_token.length = mechTokenLength;
206           free(mechToken);
207           mechToken = NULL;
208           infof(conn->data, "Parse SPNEGO Target Token succeeded\n");
209         }
210     }
211 #endif
212   }
213
214   major_status = gss_init_sec_context(&minor_status,
215                                       GSS_C_NO_CREDENTIAL,
216                                       &neg_ctx->context,
217                                       neg_ctx->server_name,
218                                       GSS_C_NO_OID,
219                                       GSS_C_DELEG_FLAG,
220                                       0,
221                                       GSS_C_NO_CHANNEL_BINDINGS,
222                                       &input_token,
223                                       NULL,
224                                       &output_token,
225                                       NULL,
226                                       NULL);
227   if (input_token.length > 0)
228     gss_release_buffer(&minor_status2, &input_token);
229   neg_ctx->status = major_status;
230   if (GSS_ERROR(major_status)) {
231     /* Curl_cleanup_negotiate(conn->data) ??? */
232     log_gss_error(conn, minor_status,
233                   (char *)"gss_init_sec_context() failed: ");
234     return -1;
235   }
236
237   if (output_token.length == 0) {
238     return -1;
239   }
240
241   neg_ctx->output_token = output_token;
242   /* conn->bits.close = FALSE; */
243
244   return 0;
245 }
246
247
248 CURLcode Curl_output_negotiate(struct connectdata *conn)
249 {
250   struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
251   OM_uint32 minor_status;
252   char *encoded = NULL;
253   int len;
254
255 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
256   if (checkprefix("Negotiate",neg_ctx->protocol)) {
257     ASN1_OBJECT *   object            = NULL;
258     int             rc                = 1;
259     unsigned char * spnegoToken       = NULL;
260     size_t          spnegoTokenLength = 0;
261     unsigned char * responseToken       = NULL;
262     size_t          responseTokenLength = 0;
263
264     responseToken = malloc(neg_ctx->output_token.length);
265     if ( responseToken == NULL)
266       return CURLE_OUT_OF_MEMORY;
267     memcpy(responseToken, neg_ctx->output_token.value,
268            neg_ctx->output_token.length);
269     responseTokenLength = neg_ctx->output_token.length;
270
271     object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
272     if (!makeSpnegoInitialToken (object,
273                                  responseToken,
274                                  responseTokenLength,
275                                  &spnegoToken,
276                                  &spnegoTokenLength)) {
277       free(responseToken);
278       responseToken = NULL;
279       infof(conn->data, "Make SPNEGO Initial Token failed\n");
280     }
281     else {
282       free(neg_ctx->output_token.value);
283       responseToken = NULL;
284       neg_ctx->output_token.value = malloc(spnegoTokenLength);
285       memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
286       neg_ctx->output_token.length = spnegoTokenLength;
287       free(spnegoToken);
288       spnegoToken = NULL;
289       infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
290     }
291   }
292 #endif
293   len = Curl_base64_encode(conn->data,
294                            neg_ctx->output_token.value,
295                            neg_ctx->output_token.length,
296                            &encoded);
297
298   if (len < 0)
299     return CURLE_OUT_OF_MEMORY;
300
301   conn->allocptr.userpwd =
302     aprintf("Authorization: %s %s\r\n", neg_ctx->protocol, encoded);
303   free(encoded);
304   gss_release_buffer(&minor_status, &neg_ctx->output_token);
305   return (conn->allocptr.userpwd == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
306 }
307
308 void Curl_cleanup_negotiate(struct SessionHandle *data)
309 {
310   OM_uint32 minor_status;
311   struct negotiatedata *neg_ctx = &data->state.negotiate;
312
313   if (neg_ctx->context != GSS_C_NO_CONTEXT)
314     gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
315
316   if (neg_ctx->output_token.length != 0)
317     gss_release_buffer(&minor_status, &neg_ctx->output_token);
318
319   if (neg_ctx->server_name != GSS_C_NO_NAME)
320     gss_release_name(&minor_status, &neg_ctx->server_name);
321
322   memset(neg_ctx, 0, sizeof(*neg_ctx));
323 }
324
325
326 #endif
327 #endif