1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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 #ifdef USE_WINDOWS_SSPI
27 #ifndef CURL_DISABLE_HTTP
33 #include "curl_base64.h"
34 #include "http_negotiate.h"
35 #include "curl_memory.h"
36 #include "curl_multibyte.h"
38 #define _MPRINTF_REPLACE /* use our functions only */
39 #include <curl/mprintf.h>
41 /* The last #include file should be: */
45 get_gss_name(struct connectdata *conn, bool proxy,
46 struct negotiatedata *neg_ctx)
51 if(proxy && !conn->proxy.name)
52 /* proxy auth requested but no given proxy name, error out! */
55 /* GSSAPI implementation by Globus (known as GSI) requires the name to be
56 of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
57 of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
58 Change following lines if you want to use GSI */
60 /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name,
61 and SSPI then generates an NTLM token. When using <service>/<fqdn> a
62 Kerberos token is generated. */
69 length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
71 if(length + 1 > sizeof(neg_ctx->server_name))
74 snprintf(neg_ctx->server_name, sizeof(neg_ctx->server_name), "%s/%s",
75 service, proxy ? conn->proxy.name : conn->host.name);
80 /* returning zero (0) means success, everything else is treated as "failure"
81 with no care exactly what the failure was */
82 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
85 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
86 &conn->data->state.negotiate;
87 BYTE *input_token = 0;
88 SecBufferDesc out_buff_desc;
89 SecBuffer out_sec_buff;
90 SecBufferDesc in_buff_desc;
91 SecBuffer in_sec_buff;
92 unsigned long context_attributes;
96 size_t len = 0, input_token_len = 0;
101 while(*header && ISSPACE(*header))
104 if(checkprefix("GSS-Negotiate", header)) {
105 protocol = "GSS-Negotiate";
108 else if(checkprefix("Negotiate", header)) {
109 protocol = "Negotiate";
115 if(neg_ctx->context) {
116 if(neg_ctx->gss != gss) {
121 neg_ctx->protocol = protocol;
125 if(neg_ctx->context && neg_ctx->status == SEC_E_OK) {
126 /* We finished successfully our part of authentication, but server
127 * rejected it (since we're again here). Exit with an error since we
128 * can't invent anything better */
129 Curl_cleanup_negotiate(conn->data);
133 if(0 == strlen(neg_ctx->server_name)) {
134 ret = get_gss_name(conn, proxy, neg_ctx);
139 if(!neg_ctx->output_token) {
140 PSecPkgInfo SecurityPackage;
141 ret = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT("Negotiate"),
146 /* Allocate input and output buffers according to the max token size
147 as indicated by the security package */
148 neg_ctx->max_token_length = SecurityPackage->cbMaxToken;
149 neg_ctx->output_token = malloc(neg_ctx->max_token_length);
150 s_pSecFn->FreeContextBuffer(SecurityPackage);
153 /* Obtain the input token, if any */
154 header += strlen(neg_ctx->protocol);
155 while(*header && ISSPACE(*header))
158 len = strlen(header);
160 /* first call in a new negotation, we have to acquire credentials,
161 and allocate memory for the context */
163 neg_ctx->credentials = malloc(sizeof(CredHandle));
164 neg_ctx->context = malloc(sizeof(CtxtHandle));
166 if(!neg_ctx->credentials || !neg_ctx->context)
170 s_pSecFn->AcquireCredentialsHandle(NULL,
171 (TCHAR *) TEXT("Negotiate"),
172 SECPKG_CRED_OUTBOUND, NULL, NULL,
173 NULL, NULL, neg_ctx->credentials,
175 if(neg_ctx->status != SEC_E_OK)
179 input_token = malloc(neg_ctx->max_token_length);
183 error = Curl_base64_decode(header,
184 (unsigned char **)&input_token,
186 if(error || input_token_len == 0)
190 /* prepare the output buffers, and input buffers if present */
191 out_buff_desc.ulVersion = 0;
192 out_buff_desc.cBuffers = 1;
193 out_buff_desc.pBuffers = &out_sec_buff;
195 out_sec_buff.cbBuffer = curlx_uztoul(neg_ctx->max_token_length);
196 out_sec_buff.BufferType = SECBUFFER_TOKEN;
197 out_sec_buff.pvBuffer = neg_ctx->output_token;
201 in_buff_desc.ulVersion = 0;
202 in_buff_desc.cBuffers = 1;
203 in_buff_desc.pBuffers = &in_sec_buff;
205 in_sec_buff.cbBuffer = curlx_uztoul(input_token_len);
206 in_sec_buff.BufferType = SECBUFFER_TOKEN;
207 in_sec_buff.pvBuffer = input_token;
210 sname = Curl_convert_UTF8_to_tchar(neg_ctx->server_name);
212 return CURLE_OUT_OF_MEMORY;
214 neg_ctx->status = s_pSecFn->InitializeSecurityContext(
215 neg_ctx->credentials,
216 input_token ? neg_ctx->context : 0,
218 ISC_REQ_CONFIDENTIALITY,
220 SECURITY_NATIVE_DREP,
221 input_token ? &in_buff_desc : 0,
228 Curl_unicodefree(sname);
230 if(GSS_ERROR(neg_ctx->status))
233 if(neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
234 neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE) {
235 neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
237 if(GSS_ERROR(neg_ctx->status))
241 neg_ctx->output_token_length = out_sec_buff.cbBuffer;
247 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
249 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
250 &conn->data->state.negotiate;
251 char *encoded = NULL;
256 error = Curl_base64_encode(conn->data,
257 (const char*)neg_ctx->output_token,
258 neg_ctx->output_token_length,
264 return CURLE_REMOTE_ACCESS_DENIED;
266 userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
267 neg_ctx->protocol, encoded);
270 conn->allocptr.proxyuserpwd = userp;
272 conn->allocptr.userpwd = userp;
274 Curl_cleanup_negotiate (conn->data);
275 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
278 static void cleanup(struct negotiatedata *neg_ctx)
280 if(neg_ctx->context) {
281 s_pSecFn->DeleteSecurityContext(neg_ctx->context);
282 free(neg_ctx->context);
283 neg_ctx->context = 0;
286 if(neg_ctx->credentials) {
287 s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
288 free(neg_ctx->credentials);
289 neg_ctx->credentials = 0;
292 if(neg_ctx->output_token) {
293 free(neg_ctx->output_token);
294 neg_ctx->output_token = 0;
297 neg_ctx->max_token_length = 0;
300 void Curl_cleanup_negotiate(struct SessionHandle *data)
302 cleanup(&data->state.negotiate);
303 cleanup(&data->state.proxyneg);