1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 ***************************************************************************/
23 #include "curl_setup.h"
25 #if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
29 #include "curl_gssapi.h"
31 #include "curl_base64.h"
32 #include "http_negotiate.h"
33 #include "curl_memory.h"
36 #define _MPRINTF_REPLACE /* use our functions only */
37 #include <curl/mprintf.h>
39 /* The last #include file should be: */
43 get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
45 OM_uint32 major_status, minor_status;
46 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
48 const char* service = "HTTP";
50 token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
52 if(token.length + 1 > sizeof(name))
55 snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
58 token.value = (void *) name;
59 major_status = gss_import_name(&minor_status,
61 GSS_C_NT_HOSTBASED_SERVICE,
64 return GSS_ERROR(major_status) ? -1 : 0;
67 /* returning zero (0) means success, everything else is treated as "failure"
68 with no care exactly what the failure was */
69 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
72 struct SessionHandle *data = conn->data;
73 struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
74 &data->state.negotiate;
75 OM_uint32 major_status, minor_status, discard_st;
76 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
77 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
83 if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
84 /* We finished successfully our part of authentication, but server
85 * rejected it (since we're again here). Exit with an error since we
86 * can't invent anything better */
87 Curl_cleanup_negotiate(data);
91 if(neg_ctx->server_name == NULL &&
92 (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
95 header += strlen("Negotiate");
96 while(*header && ISSPACE(*header))
101 result = Curl_base64_decode(header, (unsigned char **)&input_token.value,
103 if(result || rawlen == 0)
105 input_token.length = rawlen;
107 DEBUGASSERT(input_token.value != NULL);
110 major_status = Curl_gss_init_sec_context(data,
113 neg_ctx->server_name,
114 &Curl_spnego_mech_oid,
115 GSS_C_NO_CHANNEL_BINDINGS,
120 Curl_safefree(input_token.value);
122 neg_ctx->status = major_status;
123 if(GSS_ERROR(major_status)) {
124 if(output_token.value)
125 gss_release_buffer(&discard_st, &output_token);
126 Curl_gss_log_error(conn->data, minor_status,
127 "gss_init_sec_context() failed: ");
131 if(!output_token.value || !output_token.length) {
132 if(output_token.value)
133 gss_release_buffer(&discard_st, &output_token);
137 neg_ctx->output_token = output_token;
142 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
144 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
145 &conn->data->state.negotiate;
146 char *encoded = NULL;
150 OM_uint32 discard_st;
152 result = Curl_base64_encode(conn->data,
153 neg_ctx->output_token.value,
154 neg_ctx->output_token.length,
157 gss_release_buffer(&discard_st, &neg_ctx->output_token);
158 neg_ctx->output_token.value = NULL;
159 neg_ctx->output_token.length = 0;
163 if(!encoded || !len) {
164 gss_release_buffer(&discard_st, &neg_ctx->output_token);
165 neg_ctx->output_token.value = NULL;
166 neg_ctx->output_token.length = 0;
167 return CURLE_REMOTE_ACCESS_DENIED;
170 userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
173 Curl_safefree(conn->allocptr.proxyuserpwd);
174 conn->allocptr.proxyuserpwd = userp;
177 Curl_safefree(conn->allocptr.userpwd);
178 conn->allocptr.userpwd = userp;
181 Curl_safefree(encoded);
183 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
186 static void cleanup(struct negotiatedata *neg_ctx)
188 OM_uint32 minor_status;
189 if(neg_ctx->context != GSS_C_NO_CONTEXT)
190 gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
192 if(neg_ctx->output_token.value)
193 gss_release_buffer(&minor_status, &neg_ctx->output_token);
195 if(neg_ctx->server_name != GSS_C_NO_NAME)
196 gss_release_name(&minor_status, &neg_ctx->server_name);
198 memset(neg_ctx, 0, sizeof(*neg_ctx));
201 void Curl_cleanup_negotiate(struct SessionHandle *data)
203 cleanup(&data->state.negotiate);
204 cleanup(&data->state.proxyneg);
207 #endif /* HAVE_GSSAPI && !CURL_DISABLE_HTTP && USE_SPNEGO */