1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
24 #ifdef USE_WINDOWS_SSPI
26 #ifndef CURL_DISABLE_HTTP
27 /* -- WIN32 approved -- */
37 #include "curl_base64.h"
38 #include "http_negotiate.h"
39 #include "curl_memory.h"
41 #define _MPRINTF_REPLACE /* use our functions only */
42 #include <curl/mprintf.h>
44 /* The last #include file should be: */
48 get_gss_name(struct connectdata *conn, bool proxy, char *server)
50 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
51 &conn->data->state.negotiate;
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(server, 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 ULONG context_attributes;
96 size_t len = 0, input_token_len = 0;
100 while(*header && ISSPACE(*header))
103 if(checkprefix("GSS-Negotiate", header)) {
104 protocol = "GSS-Negotiate";
107 else if(checkprefix("Negotiate", header)) {
108 protocol = "Negotiate";
114 if(neg_ctx->context) {
115 if(neg_ctx->gss != gss) {
120 neg_ctx->protocol = protocol;
124 if(neg_ctx->context && neg_ctx->status == SEC_E_OK) {
125 /* We finished successfully our part of authentication, but server
126 * rejected it (since we're again here). Exit with an error since we
127 * can't invent anything better */
128 Curl_cleanup_negotiate(conn->data);
132 if(strlen(neg_ctx->server_name) == 0 &&
133 (ret = get_gss_name(conn, proxy, neg_ctx->server_name)))
136 if (!neg_ctx->max_token_length) {
137 PSecPkgInfo SecurityPackage;
138 ret = s_pSecFn->QuerySecurityPackageInfo((SEC_CHAR *)"Negotiate",
143 /* Allocate input and output buffers according to the max token size
144 as indicated by the security package */
145 neg_ctx->max_token_length = SecurityPackage->cbMaxToken;
146 neg_ctx->output_token = (BYTE *)malloc(neg_ctx->max_token_length);
147 s_pSecFn->FreeContextBuffer(SecurityPackage);
150 /* Obtain the input token, if any */
151 header += strlen(neg_ctx->protocol);
152 while(*header && ISSPACE(*header))
155 len = strlen(header);
157 input_token = malloc(neg_ctx->max_token_length);
161 input_token_len = Curl_base64_decode(header,
162 (unsigned char **)&input_token);
163 if(input_token_len == 0)
167 if ( !input_token ) {
168 /* first call in a new negotiation, we have to require credentials,
169 and allocate memory for the context */
171 neg_ctx->credentials = (CredHandle *)malloc(sizeof(CredHandle));
172 neg_ctx->context = (CtxtHandle *)malloc(sizeof(CtxtHandle));
174 if ( !neg_ctx->credentials || !neg_ctx->context)
178 s_pSecFn->AcquireCredentialsHandle(NULL, (SEC_CHAR *)"Negotiate",
179 SECPKG_CRED_OUTBOUND, NULL, NULL,
180 NULL, NULL, neg_ctx->credentials,
182 if ( neg_ctx->status != SEC_E_OK )
186 /* prepare the output buffers, and input buffers if present */
187 out_buff_desc.ulVersion = 0;
188 out_buff_desc.cBuffers = 1;
189 out_buff_desc.pBuffers = &out_sec_buff;
191 out_sec_buff.cbBuffer = neg_ctx->max_token_length;
192 out_sec_buff.BufferType = SECBUFFER_TOKEN;
193 out_sec_buff.pvBuffer = neg_ctx->output_token;
197 in_buff_desc.ulVersion = 0;
198 in_buff_desc.cBuffers = 1;
199 in_buff_desc.pBuffers = &out_sec_buff;
201 in_sec_buff.cbBuffer = input_token_len;
202 in_sec_buff.BufferType = SECBUFFER_TOKEN;
203 in_sec_buff.pvBuffer = input_token;
206 neg_ctx->status = s_pSecFn->InitializeSecurityContext(
207 neg_ctx->credentials,
208 input_token ? neg_ctx->context : 0,
209 neg_ctx->server_name,
210 ISC_REQ_CONFIDENTIALITY,
212 SECURITY_NATIVE_DREP,
213 input_token ? &in_buff_desc : 0,
220 if ( GSS_ERROR(neg_ctx->status) )
223 if ( neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
224 neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE ) {
225 neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
227 if ( GSS_ERROR(neg_ctx->status) )
231 neg_ctx->output_token_length = out_sec_buff.cbBuffer;
237 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
239 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
240 &conn->data->state.negotiate;
241 char *encoded = NULL;
245 len = Curl_base64_encode(conn->data,
246 (const char*)neg_ctx->output_token,
247 neg_ctx->output_token_length,
251 return CURLE_OUT_OF_MEMORY;
253 userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
254 neg_ctx->protocol, encoded);
257 conn->allocptr.proxyuserpwd = userp;
259 conn->allocptr.userpwd = userp;
261 Curl_cleanup_negotiate (conn->data);
262 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
265 static void cleanup(struct negotiatedata *neg_ctx)
267 if(neg_ctx->context) {
268 s_pSecFn->DeleteSecurityContext(neg_ctx->context);
269 free(neg_ctx->context);
270 neg_ctx->context = 0;
273 if(neg_ctx->credentials) {
274 s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
275 free(neg_ctx->credentials);
276 neg_ctx->credentials = 0;
279 if(neg_ctx->output_token) {
280 free(neg_ctx->output_token);
281 neg_ctx->output_token = 0;
285 void Curl_cleanup_negotiate(struct SessionHandle *data)
287 cleanup(&data->state.negotiate);
288 cleanup(&data->state.proxyneg);