2016-03-13 19:02:42 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-05-24 01:02:33 +03:00
|
|
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2016-03-13 19:02:42 +03:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-04-06 12:57:48 +03:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2016-03-13 19:02:42 +03: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.
|
|
|
|
*
|
|
|
|
* RFC4178 Simple and Protected GSS-API Negotiation Mechanism
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
|
|
|
#if defined(USE_WINDOWS_SSPI) && defined(USE_SPNEGO)
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include "vauth/vauth.h"
|
|
|
|
#include "urldata.h"
|
|
|
|
#include "curl_base64.h"
|
|
|
|
#include "warnless.h"
|
|
|
|
#include "curl_multibyte.h"
|
|
|
|
#include "sendf.h"
|
2017-04-06 22:31:45 +03:00
|
|
|
#include "strerror.h"
|
2016-03-13 19:02:42 +03:00
|
|
|
|
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2016-03-20 14:56:07 +03:00
|
|
|
/*
|
|
|
|
* Curl_auth_is_spnego_supported()
|
|
|
|
*
|
|
|
|
* This is used to evaluate if SPNEGO (Negotiate) is supported.
|
|
|
|
*
|
|
|
|
* Parameters: None
|
|
|
|
*
|
|
|
|
* Returns TRUE if Negotiate is supported by Windows SSPI.
|
|
|
|
*/
|
|
|
|
bool Curl_auth_is_spnego_supported(void)
|
|
|
|
{
|
2016-03-24 22:03:58 +03:00
|
|
|
PSecPkgInfo SecurityPackage;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
|
|
|
|
/* Query the security package for Negotiate */
|
|
|
|
status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
|
|
|
|
TEXT(SP_NAME_NEGOTIATE),
|
|
|
|
&SecurityPackage);
|
|
|
|
|
2019-09-06 16:43:51 +03:00
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
|
|
if(status == SEC_E_OK) {
|
|
|
|
s_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-24 22:03:58 +03:00
|
|
|
return (status == SEC_E_OK ? TRUE : FALSE);
|
2016-03-20 14:56:07 +03:00
|
|
|
}
|
|
|
|
|
2016-03-13 19:02:42 +03:00
|
|
|
/*
|
|
|
|
* Curl_auth_decode_spnego_message()
|
|
|
|
*
|
|
|
|
* This is used to decode an already encoded SPNEGO (Negotiate) challenge
|
|
|
|
* message.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* data [in] - The session handle.
|
2018-10-03 11:27:27 +03:00
|
|
|
* user [in] - The user name in the format User or Domain\User.
|
|
|
|
* password [in] - The user's password.
|
2016-04-06 02:21:07 +03:00
|
|
|
* service [in] - The service type such as http, smtp, pop or imap.
|
2016-04-01 21:07:49 +03:00
|
|
|
* host [in] - The host name.
|
2016-03-13 19:02:42 +03:00
|
|
|
* chlg64 [in] - The optional base64 encoded challenge message.
|
|
|
|
* nego [in/out] - The Negotiate data struct being used and modified.
|
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
2016-06-21 16:47:12 +03:00
|
|
|
CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
|
2016-03-13 19:02:42 +03:00
|
|
|
const char *user,
|
|
|
|
const char *password,
|
|
|
|
const char *service,
|
|
|
|
const char *host,
|
|
|
|
const char *chlg64,
|
|
|
|
struct negotiatedata *nego)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
size_t chlglen = 0;
|
|
|
|
unsigned char *chlg = NULL;
|
|
|
|
PSecPkgInfo SecurityPackage;
|
2019-01-29 20:26:31 +03:00
|
|
|
SecBuffer chlg_buf[2];
|
2016-03-13 19:02:42 +03:00
|
|
|
SecBuffer resp_buf;
|
|
|
|
SecBufferDesc chlg_desc;
|
|
|
|
SecBufferDesc resp_desc;
|
|
|
|
unsigned long attrs;
|
|
|
|
TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
|
|
|
|
|
|
|
|
#if defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
(void) data;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(nego->context && nego->status == SEC_E_OK) {
|
|
|
|
/* We finished successfully our part of authentication, but server
|
|
|
|
* rejected it (since we're again here). Exit with an error since we
|
|
|
|
* can't invent anything better */
|
2019-05-11 14:57:42 +03:00
|
|
|
Curl_auth_cleanup_spnego(nego);
|
2016-03-13 19:02:42 +03:00
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
}
|
|
|
|
|
2016-04-02 08:41:29 +03:00
|
|
|
if(!nego->spn) {
|
2016-04-03 19:32:10 +03:00
|
|
|
/* Generate our SPN */
|
2016-04-03 22:26:03 +03:00
|
|
|
nego->spn = Curl_auth_build_spn(service, host, NULL);
|
2016-04-02 08:41:29 +03:00
|
|
|
if(!nego->spn)
|
2016-03-13 19:02:42 +03:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!nego->output_token) {
|
|
|
|
/* Query the security package for Negotiate */
|
|
|
|
nego->status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
|
|
|
|
TEXT(SP_NAME_NEGOTIATE),
|
|
|
|
&SecurityPackage);
|
2020-05-08 03:43:45 +03:00
|
|
|
if(nego->status != SEC_E_OK) {
|
|
|
|
failf(data, "SSPI: couldn't get auth info\n");
|
|
|
|
return CURLE_AUTH_ERROR;
|
|
|
|
}
|
2016-03-13 19:02:42 +03:00
|
|
|
|
|
|
|
nego->token_max = SecurityPackage->cbMaxToken;
|
|
|
|
|
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
|
|
s_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
|
|
|
|
/* Allocate our output buffer */
|
|
|
|
nego->output_token = malloc(nego->token_max);
|
|
|
|
if(!nego->output_token)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!nego->credentials) {
|
2018-04-14 22:52:00 +03:00
|
|
|
/* Do we have credentials to use or are we using single sign-on? */
|
2016-03-13 19:02:42 +03:00
|
|
|
if(user && *user) {
|
|
|
|
/* Populate our identity structure */
|
|
|
|
result = Curl_create_sspi_identity(user, password, &nego->identity);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Allow proper cleanup of the identity structure */
|
|
|
|
nego->p_identity = &nego->identity;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Use the current Windows user */
|
|
|
|
nego->p_identity = NULL;
|
|
|
|
|
|
|
|
/* Allocate our credentials handle */
|
2018-04-14 23:42:04 +03:00
|
|
|
nego->credentials = calloc(1, sizeof(CredHandle));
|
2016-03-13 19:02:42 +03:00
|
|
|
if(!nego->credentials)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Acquire our credentials handle */
|
|
|
|
nego->status =
|
|
|
|
s_pSecFn->AcquireCredentialsHandle(NULL,
|
|
|
|
(TCHAR *)TEXT(SP_NAME_NEGOTIATE),
|
|
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
|
|
|
nego->p_identity, NULL, NULL,
|
|
|
|
nego->credentials, &expiry);
|
|
|
|
if(nego->status != SEC_E_OK)
|
2019-08-14 10:47:17 +03:00
|
|
|
return CURLE_AUTH_ERROR;
|
2016-03-13 19:02:42 +03:00
|
|
|
|
|
|
|
/* Allocate our new context handle */
|
2018-04-14 23:42:04 +03:00
|
|
|
nego->context = calloc(1, sizeof(CtxtHandle));
|
2016-03-13 19:02:42 +03:00
|
|
|
if(!nego->context)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2016-04-03 19:32:10 +03:00
|
|
|
if(chlg64 && *chlg64) {
|
2016-03-13 19:02:42 +03:00
|
|
|
/* Decode the base-64 encoded challenge message */
|
|
|
|
if(*chlg64 != '=') {
|
|
|
|
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we have a valid challenge message */
|
|
|
|
if(!chlg) {
|
|
|
|
infof(data, "SPNEGO handshake failure (empty challenge message)\n");
|
|
|
|
|
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the challenge "input" security buffer */
|
2019-01-29 20:26:31 +03:00
|
|
|
chlg_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
chlg_desc.cBuffers = 1;
|
|
|
|
chlg_desc.pBuffers = &chlg_buf[0];
|
|
|
|
chlg_buf[0].BufferType = SECBUFFER_TOKEN;
|
|
|
|
chlg_buf[0].pvBuffer = chlg;
|
|
|
|
chlg_buf[0].cbBuffer = curlx_uztoul(chlglen);
|
|
|
|
|
|
|
|
#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
|
|
|
|
/* ssl context comes from Schannel.
|
|
|
|
* When extended protection is used in IIS server,
|
|
|
|
* we have to pass a second SecBuffer to the SecBufferDesc
|
|
|
|
* otherwise IIS will not pass the authentication (401 response).
|
|
|
|
* Minimum supported version is Windows 7.
|
|
|
|
* https://docs.microsoft.com/en-us/security-updates
|
|
|
|
* /SecurityAdvisories/2009/973811
|
|
|
|
*/
|
|
|
|
if(nego->sslContext) {
|
|
|
|
SEC_CHANNEL_BINDINGS channelBindings;
|
|
|
|
SecPkgContext_Bindings pkgBindings;
|
|
|
|
pkgBindings.Bindings = &channelBindings;
|
|
|
|
nego->status = s_pSecFn->QueryContextAttributes(
|
|
|
|
nego->sslContext,
|
|
|
|
SECPKG_ATTR_ENDPOINT_BINDINGS,
|
|
|
|
&pkgBindings
|
|
|
|
);
|
|
|
|
if(nego->status == SEC_E_OK) {
|
|
|
|
chlg_desc.cBuffers++;
|
|
|
|
chlg_buf[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
|
|
|
|
chlg_buf[1].cbBuffer = pkgBindings.BindingsLength;
|
|
|
|
chlg_buf[1].pvBuffer = pkgBindings.Bindings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-13 19:02:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the response "output" security buffer */
|
|
|
|
resp_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
resp_desc.cBuffers = 1;
|
|
|
|
resp_desc.pBuffers = &resp_buf;
|
|
|
|
resp_buf.BufferType = SECBUFFER_TOKEN;
|
|
|
|
resp_buf.pvBuffer = nego->output_token;
|
|
|
|
resp_buf.cbBuffer = curlx_uztoul(nego->token_max);
|
|
|
|
|
|
|
|
/* Generate our challenge-response message */
|
|
|
|
nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
|
|
|
|
chlg ? nego->context :
|
|
|
|
NULL,
|
2016-04-02 08:41:29 +03:00
|
|
|
nego->spn,
|
2016-03-13 19:02:42 +03:00
|
|
|
ISC_REQ_CONFIDENTIALITY,
|
|
|
|
0, SECURITY_NATIVE_DREP,
|
|
|
|
chlg ? &chlg_desc : NULL,
|
|
|
|
0, nego->context,
|
|
|
|
&resp_desc, &attrs,
|
|
|
|
&expiry);
|
|
|
|
|
2016-04-03 19:32:10 +03:00
|
|
|
/* Free the decoded challenge as it is not required anymore */
|
|
|
|
free(chlg);
|
|
|
|
|
2016-03-13 19:02:42 +03:00
|
|
|
if(GSS_ERROR(nego->status)) {
|
2019-02-25 20:12:51 +03:00
|
|
|
char buffer[STRERROR_LEN];
|
2017-04-06 22:31:45 +03:00
|
|
|
failf(data, "InitializeSecurityContext failed: %s",
|
2019-02-25 20:12:51 +03:00
|
|
|
Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
|
2019-05-11 09:23:09 +03:00
|
|
|
|
2019-08-20 12:44:18 +03:00
|
|
|
if(nego->status == (DWORD)SEC_E_INSUFFICIENT_MEMORY)
|
2019-05-11 09:23:09 +03:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return CURLE_AUTH_ERROR;
|
2016-03-13 19:02:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(nego->status == SEC_I_COMPLETE_NEEDED ||
|
|
|
|
nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
|
|
|
|
nego->status = s_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
|
|
|
|
if(GSS_ERROR(nego->status)) {
|
2019-05-11 09:23:09 +03:00
|
|
|
char buffer[STRERROR_LEN];
|
|
|
|
failf(data, "CompleteAuthToken failed: %s",
|
|
|
|
Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
|
|
|
|
|
2019-08-20 12:44:18 +03:00
|
|
|
if(nego->status == (DWORD)SEC_E_INSUFFICIENT_MEMORY)
|
2019-05-11 09:23:09 +03:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return CURLE_AUTH_ERROR;
|
2016-03-13 19:02:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nego->output_token_length = resp_buf.cbBuffer;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_auth_create_spnego_message()
|
|
|
|
*
|
|
|
|
* This is used to generate an already encoded SPNEGO (Negotiate) response
|
|
|
|
* message ready for sending to the recipient.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* data [in] - The session handle.
|
|
|
|
* nego [in/out] - The Negotiate data struct being used and modified.
|
|
|
|
* outptr [in/out] - The address where a pointer to newly allocated memory
|
|
|
|
* holding the result will be stored upon completion.
|
|
|
|
* outlen [out] - The length of the output message.
|
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
2016-06-21 16:47:12 +03:00
|
|
|
CURLcode Curl_auth_create_spnego_message(struct Curl_easy *data,
|
2016-03-13 19:02:42 +03:00
|
|
|
struct negotiatedata *nego,
|
|
|
|
char **outptr, size_t *outlen)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
/* Base64 encode the already generated response */
|
|
|
|
result = Curl_base64_encode(data,
|
2016-11-23 10:30:18 +03:00
|
|
|
(const char *) nego->output_token,
|
2016-03-13 19:02:42 +03:00
|
|
|
nego->output_token_length,
|
|
|
|
outptr, outlen);
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2016-08-19 15:46:07 +03:00
|
|
|
if(!*outptr || !*outlen) {
|
|
|
|
free(*outptr);
|
2016-03-13 19:02:42 +03:00
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
2016-08-19 15:46:07 +03:00
|
|
|
}
|
2016-03-13 19:02:42 +03:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-05-11 14:57:42 +03:00
|
|
|
* Curl_auth_cleanup_spnego()
|
2016-03-13 19:02:42 +03:00
|
|
|
*
|
|
|
|
* This is used to clean up the SPNEGO (Negotiate) specific data.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* nego [in/out] - The Negotiate data struct being cleaned up.
|
|
|
|
*
|
|
|
|
*/
|
2019-05-11 14:57:42 +03:00
|
|
|
void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
|
2016-03-13 19:02:42 +03:00
|
|
|
{
|
|
|
|
/* Free our security context */
|
|
|
|
if(nego->context) {
|
|
|
|
s_pSecFn->DeleteSecurityContext(nego->context);
|
|
|
|
free(nego->context);
|
|
|
|
nego->context = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free our credentials handle */
|
|
|
|
if(nego->credentials) {
|
|
|
|
s_pSecFn->FreeCredentialsHandle(nego->credentials);
|
|
|
|
free(nego->credentials);
|
|
|
|
nego->credentials = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free our identity */
|
|
|
|
Curl_sspi_free_identity(nego->p_identity);
|
|
|
|
nego->p_identity = NULL;
|
|
|
|
|
|
|
|
/* Free the SPN and output token */
|
2016-04-02 08:41:29 +03:00
|
|
|
Curl_safefree(nego->spn);
|
2016-03-13 19:02:42 +03:00
|
|
|
Curl_safefree(nego->output_token);
|
|
|
|
|
|
|
|
/* Reset any variables */
|
2016-03-13 21:51:46 +03:00
|
|
|
nego->status = 0;
|
2016-03-13 19:02:42 +03:00
|
|
|
nego->token_max = 0;
|
2018-09-10 10:18:01 +03:00
|
|
|
nego->noauthpersist = FALSE;
|
|
|
|
nego->havenoauthpersist = FALSE;
|
|
|
|
nego->havenegdata = FALSE;
|
|
|
|
nego->havemultiplerequests = FALSE;
|
2016-03-13 19:02:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_WINDOWS_SSPI && USE_SPNEGO */
|