2011-01-05 11:01:07 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2015-01-17 06:20:35 -05:00
|
|
|
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-01-05 11:01:07 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-01-05 11:01:07 -05:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2011-07-26 11:23:27 -04:00
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2011-01-05 11:01:07 -05:00
|
|
|
|
|
|
|
#ifdef USE_WINDOWS_SSPI
|
|
|
|
|
2014-07-21 03:53:46 -04:00
|
|
|
#if !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "rawstr.h"
|
|
|
|
#include "warnless.h"
|
2011-01-05 11:01:07 -05:00
|
|
|
#include "curl_base64.h"
|
2014-08-09 12:56:51 -04:00
|
|
|
#include "curl_sasl.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "http_negotiate.h"
|
2012-06-15 12:05:11 -04:00
|
|
|
#include "curl_multibyte.h"
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2015-03-24 18:12:03 -04:00
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2015-01-17 06:56:27 -05:00
|
|
|
CURLcode Curl_input_negotiate(struct connectdata *conn, bool proxy,
|
|
|
|
const char *header)
|
2011-01-05 11:01:07 -05:00
|
|
|
{
|
2015-04-30 06:13:49 -04:00
|
|
|
struct SessionHandle *data = conn->data;
|
2014-08-08 17:39:19 -04:00
|
|
|
BYTE *input_token = NULL;
|
2011-01-05 11:01:07 -05:00
|
|
|
SecBufferDesc out_buff_desc;
|
|
|
|
SecBuffer out_sec_buff;
|
|
|
|
SecBufferDesc in_buff_desc;
|
|
|
|
SecBuffer in_sec_buff;
|
2015-01-17 06:24:06 -05:00
|
|
|
SECURITY_STATUS status;
|
2015-01-17 06:27:36 -05:00
|
|
|
unsigned long attrs;
|
|
|
|
TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
|
2011-01-05 11:01:07 -05:00
|
|
|
size_t len = 0, input_token_len = 0;
|
2015-01-17 06:20:35 -05:00
|
|
|
CURLcode result;
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-07-11 16:45:25 -04:00
|
|
|
/* Point to the username and password */
|
|
|
|
const char *userp;
|
|
|
|
const char *passwdp;
|
|
|
|
|
|
|
|
/* Point to the correct struct with this */
|
|
|
|
struct negotiatedata *neg_ctx;
|
|
|
|
|
|
|
|
if(proxy) {
|
|
|
|
userp = conn->proxyuser;
|
|
|
|
passwdp = conn->proxypasswd;
|
2015-04-30 06:13:49 -04:00
|
|
|
neg_ctx = &data->state.proxyneg;
|
2014-07-11 16:45:25 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
userp = conn->user;
|
|
|
|
passwdp = conn->passwd;
|
2015-04-30 06:13:49 -04:00
|
|
|
neg_ctx = &data->state.negotiate;
|
2014-07-11 16:45:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Not set means empty */
|
|
|
|
if(!userp)
|
|
|
|
userp = "";
|
|
|
|
|
|
|
|
if(!passwdp)
|
|
|
|
passwdp = "";
|
|
|
|
|
2011-01-05 11:01:07 -05:00
|
|
|
if(neg_ctx->context && neg_ctx->status == SEC_E_OK) {
|
2011-04-19 09:54:13 -04:00
|
|
|
/* We finished successfully our part of authentication, but server
|
2011-01-05 11:01:07 -05:00
|
|
|
* rejected it (since we're again here). Exit with an error since we
|
|
|
|
* can't invent anything better */
|
2015-04-30 06:13:49 -04:00
|
|
|
Curl_cleanup_negotiate(data);
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_LOGIN_DENIED;
|
2011-01-05 11:01:07 -05:00
|
|
|
}
|
|
|
|
|
2014-08-09 12:56:51 -04:00
|
|
|
if(!neg_ctx->server_name) {
|
2014-08-09 13:00:01 -04:00
|
|
|
/* Check proxy auth requested but no given proxy name */
|
|
|
|
if(proxy && !conn->proxy.name)
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
2014-08-09 13:00:01 -04:00
|
|
|
|
|
|
|
/* Generate our SPN */
|
2015-03-29 08:52:31 -04:00
|
|
|
neg_ctx->server_name = Curl_sasl_build_spn(
|
|
|
|
proxy ? data->set.str[STRING_PROXY_SERVICE_NAME] :
|
|
|
|
data->set.str[STRING_SERVICE_NAME],
|
|
|
|
proxy ? conn->proxy.name : conn->host.name);
|
2014-08-09 13:00:01 -04:00
|
|
|
if(!neg_ctx->server_name)
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-08-13 18:05:05 -04:00
|
|
|
}
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2011-05-16 05:19:03 -04:00
|
|
|
if(!neg_ctx->output_token) {
|
2011-01-05 11:01:07 -05:00
|
|
|
PSecPkgInfo SecurityPackage;
|
2015-01-17 06:24:06 -05:00
|
|
|
status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
|
|
|
|
TEXT(SP_NAME_NEGOTIATE),
|
|
|
|
&SecurityPackage);
|
|
|
|
if(status != SEC_E_OK)
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_NOT_BUILT_IN;
|
2011-01-05 11:01:07 -05:00
|
|
|
|
|
|
|
/* Allocate input and output buffers according to the max token size
|
|
|
|
as indicated by the security package */
|
2014-10-26 10:36:05 -04:00
|
|
|
neg_ctx->token_max = SecurityPackage->cbMaxToken;
|
|
|
|
neg_ctx->output_token = malloc(neg_ctx->token_max);
|
2011-01-05 11:01:07 -05:00
|
|
|
s_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Obtain the input token, if any */
|
2014-07-21 03:53:44 -04:00
|
|
|
header += strlen("Negotiate");
|
2011-01-05 11:01:07 -05:00
|
|
|
while(*header && ISSPACE(*header))
|
|
|
|
header++;
|
|
|
|
|
|
|
|
len = strlen(header);
|
2011-05-16 05:19:03 -04:00
|
|
|
if(!len) {
|
2014-08-05 19:12:53 -04:00
|
|
|
/* Is this the first call in a new negotiation? */
|
|
|
|
if(neg_ctx->context) {
|
|
|
|
/* The server rejected our authentication and hasn't suppled any more
|
|
|
|
negotiation mechanisms */
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_LOGIN_DENIED;
|
2014-08-05 19:12:53 -04:00
|
|
|
}
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-08-05 19:12:53 -04:00
|
|
|
/* We have to acquire credentials and allocate memory for the context */
|
2011-06-02 13:42:24 -04:00
|
|
|
neg_ctx->credentials = malloc(sizeof(CredHandle));
|
|
|
|
neg_ctx->context = malloc(sizeof(CtxtHandle));
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2011-04-22 17:01:30 -04:00
|
|
|
if(!neg_ctx->credentials || !neg_ctx->context)
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-07-11 16:45:25 -04:00
|
|
|
if(userp && *userp) {
|
|
|
|
/* Populate our identity structure */
|
2015-01-17 06:20:35 -05:00
|
|
|
result = Curl_create_sspi_identity(userp, passwdp, &neg_ctx->identity);
|
|
|
|
if(result)
|
2015-01-17 06:56:27 -05:00
|
|
|
return result;
|
2014-07-11 16:45:25 -04:00
|
|
|
|
|
|
|
/* Allow proper cleanup of the identity structure */
|
|
|
|
neg_ctx->p_identity = &neg_ctx->identity;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Use the current Windows user */
|
|
|
|
neg_ctx->p_identity = NULL;
|
|
|
|
|
|
|
|
/* Acquire our credientials handle */
|
2011-01-05 11:01:07 -05:00
|
|
|
neg_ctx->status =
|
2012-06-15 15:50:57 -04:00
|
|
|
s_pSecFn->AcquireCredentialsHandle(NULL,
|
2014-11-05 06:43:22 -05:00
|
|
|
(TCHAR *) TEXT(SP_NAME_NEGOTIATE),
|
2014-07-11 16:45:25 -04:00
|
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
|
|
|
neg_ctx->p_identity, NULL, NULL,
|
2014-10-26 10:29:27 -04:00
|
|
|
neg_ctx->credentials, &expiry);
|
2011-04-22 17:01:30 -04:00
|
|
|
if(neg_ctx->status != SEC_E_OK)
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_LOGIN_DENIED;
|
2011-01-05 11:01:07 -05:00
|
|
|
}
|
2011-05-16 05:19:03 -04:00
|
|
|
else {
|
2015-01-17 06:20:35 -05:00
|
|
|
result = Curl_base64_decode(header,
|
|
|
|
(unsigned char **)&input_token,
|
|
|
|
&input_token_len);
|
2015-01-17 06:56:27 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2015-01-17 06:59:44 -05:00
|
|
|
if(!input_token_len) {
|
2015-04-30 06:13:49 -04:00
|
|
|
infof(data,
|
2015-01-17 06:59:44 -05:00
|
|
|
"Negotiate handshake failure (empty challenge message)\n");
|
|
|
|
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2015-01-17 06:59:44 -05:00
|
|
|
}
|
2011-05-16 05:19:03 -04:00
|
|
|
}
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-08-08 17:39:19 -04:00
|
|
|
/* Setup the "output" security buffer */
|
|
|
|
out_buff_desc.ulVersion = SECBUFFER_VERSION;
|
2011-01-05 11:01:07 -05:00
|
|
|
out_buff_desc.cBuffers = 1;
|
|
|
|
out_buff_desc.pBuffers = &out_sec_buff;
|
|
|
|
out_sec_buff.BufferType = SECBUFFER_TOKEN;
|
|
|
|
out_sec_buff.pvBuffer = neg_ctx->output_token;
|
2014-10-26 10:36:05 -04:00
|
|
|
out_sec_buff.cbBuffer = curlx_uztoul(neg_ctx->token_max);
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-08-08 17:39:19 -04:00
|
|
|
/* Setup the "input" security buffer if present */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(input_token) {
|
2014-08-08 17:39:19 -04:00
|
|
|
in_buff_desc.ulVersion = SECBUFFER_VERSION;
|
2011-01-05 11:01:07 -05:00
|
|
|
in_buff_desc.cBuffers = 1;
|
2012-01-01 16:36:32 -05:00
|
|
|
in_buff_desc.pBuffers = &in_sec_buff;
|
2011-01-05 11:01:07 -05:00
|
|
|
in_sec_buff.BufferType = SECBUFFER_TOKEN;
|
|
|
|
in_sec_buff.pvBuffer = input_token;
|
2014-08-08 17:39:19 -04:00
|
|
|
in_sec_buff.cbBuffer = curlx_uztoul(input_token_len);
|
2011-01-05 11:01:07 -05:00
|
|
|
}
|
|
|
|
|
2014-08-08 17:39:19 -04:00
|
|
|
/* Generate our message */
|
2011-01-05 11:01:07 -05:00
|
|
|
neg_ctx->status = s_pSecFn->InitializeSecurityContext(
|
|
|
|
neg_ctx->credentials,
|
2014-08-08 17:39:19 -04:00
|
|
|
input_token ? neg_ctx->context : NULL,
|
2014-08-09 12:56:51 -04:00
|
|
|
neg_ctx->server_name,
|
2011-01-05 11:01:07 -05:00
|
|
|
ISC_REQ_CONFIDENTIALITY,
|
|
|
|
0,
|
|
|
|
SECURITY_NATIVE_DREP,
|
2014-08-08 17:39:19 -04:00
|
|
|
input_token ? &in_buff_desc : NULL,
|
2011-01-05 11:01:07 -05:00
|
|
|
0,
|
|
|
|
neg_ctx->context,
|
|
|
|
&out_buff_desc,
|
2015-01-17 06:27:36 -05:00
|
|
|
&attrs,
|
2014-10-26 10:29:27 -04:00
|
|
|
&expiry);
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(input_token);
|
2012-06-15 12:05:11 -04:00
|
|
|
|
2011-04-22 17:01:30 -04:00
|
|
|
if(GSS_ERROR(neg_ctx->status))
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2011-04-22 17:01:30 -04:00
|
|
|
if(neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
|
|
|
|
neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE) {
|
2011-01-05 11:01:07 -05:00
|
|
|
neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
|
|
|
|
&out_buff_desc);
|
2011-04-22 17:01:30 -04:00
|
|
|
if(GSS_ERROR(neg_ctx->status))
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_RECV_ERROR;
|
2011-01-05 11:01:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
neg_ctx->output_token_length = out_sec_buff.cbBuffer;
|
|
|
|
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_OK;
|
2011-01-05 11:01:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
|
|
|
|
{
|
|
|
|
struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
|
|
|
|
&conn->data->state.negotiate;
|
|
|
|
char *encoded = NULL;
|
2011-08-24 02:07:36 -04:00
|
|
|
size_t len = 0;
|
2011-01-05 11:01:07 -05:00
|
|
|
char *userp;
|
2016-03-13 13:09:58 -04:00
|
|
|
CURLcode result;
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2016-03-13 13:09:58 -04:00
|
|
|
result = Curl_base64_encode(conn->data,
|
|
|
|
(const char*)neg_ctx->output_token,
|
|
|
|
neg_ctx->output_token_length,
|
|
|
|
&encoded, &len);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-08-08 17:39:19 -04:00
|
|
|
if(!len)
|
2011-08-24 02:07:36 -04:00
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-07-21 03:53:44 -04:00
|
|
|
userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
|
|
|
|
encoded);
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-07-24 12:55:12 -04:00
|
|
|
if(proxy) {
|
|
|
|
Curl_safefree(conn->allocptr.proxyuserpwd);
|
2011-01-05 11:01:07 -05:00
|
|
|
conn->allocptr.proxyuserpwd = userp;
|
2014-07-24 12:55:12 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Curl_safefree(conn->allocptr.userpwd);
|
2011-01-05 11:01:07 -05:00
|
|
|
conn->allocptr.userpwd = userp;
|
2014-07-24 12:55:12 -04:00
|
|
|
}
|
2016-03-13 13:09:58 -04:00
|
|
|
|
2011-01-05 11:01:07 -05:00
|
|
|
free(encoded);
|
2016-03-13 13:09:58 -04:00
|
|
|
|
2011-01-05 11:01:07 -05:00
|
|
|
return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup(struct negotiatedata *neg_ctx)
|
|
|
|
{
|
2014-10-26 10:51:05 -04:00
|
|
|
/* Free our security context */
|
2011-01-05 11:01:07 -05:00
|
|
|
if(neg_ctx->context) {
|
|
|
|
s_pSecFn->DeleteSecurityContext(neg_ctx->context);
|
|
|
|
free(neg_ctx->context);
|
2014-08-08 17:39:19 -04:00
|
|
|
neg_ctx->context = NULL;
|
2011-01-05 11:01:07 -05:00
|
|
|
}
|
|
|
|
|
2014-10-26 10:51:05 -04:00
|
|
|
/* Free our credentials handle */
|
2011-01-05 11:01:07 -05:00
|
|
|
if(neg_ctx->credentials) {
|
|
|
|
s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
|
|
|
|
free(neg_ctx->credentials);
|
2014-08-08 17:39:19 -04:00
|
|
|
neg_ctx->credentials = NULL;
|
2011-01-05 11:01:07 -05:00
|
|
|
}
|
2011-05-16 05:19:03 -04:00
|
|
|
|
2014-10-26 10:51:05 -04:00
|
|
|
/* Free our identity */
|
|
|
|
Curl_sspi_free_identity(neg_ctx->p_identity);
|
|
|
|
neg_ctx->p_identity = NULL;
|
2014-07-11 16:45:25 -04:00
|
|
|
|
2014-10-26 10:51:05 -04:00
|
|
|
/* Free the SPN and output token */
|
2014-08-09 12:56:51 -04:00
|
|
|
Curl_safefree(neg_ctx->server_name);
|
2014-10-26 10:51:05 -04:00
|
|
|
Curl_safefree(neg_ctx->output_token);
|
2014-08-09 12:56:51 -04:00
|
|
|
|
2014-10-26 10:51:05 -04:00
|
|
|
/* Reset any variables */
|
|
|
|
neg_ctx->token_max = 0;
|
2011-01-05 11:01:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_cleanup_negotiate(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
cleanup(&data->state.negotiate);
|
|
|
|
cleanup(&data->state.proxyneg);
|
|
|
|
}
|
|
|
|
|
2014-07-21 03:53:46 -04:00
|
|
|
#endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */
|
2011-01-05 11:01:07 -05:00
|
|
|
|
2014-04-05 20:22:47 -04:00
|
|
|
#endif /* USE_WINDOWS_SSPI */
|