2014-04-02 16:21:12 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2015-03-03 06:36:18 -05:00
|
|
|
* Copyright (C) 2014 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2014-08-13 19:03:45 -04:00
|
|
|
* Copyright (C) 2014, Steve Holme, <steve_holme@hotmail.com>.
|
2014-04-02 16:21:12 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2014-11-14 16:05:38 -05:00
|
|
|
* RFC2617 Basic and Digest Access Authentication
|
2014-04-02 16:21:12 -04:00
|
|
|
* RFC2831 DIGEST-MD5 authentication
|
|
|
|
* RFC4422 Simple Authentication and Security Layer (SASL)
|
2014-08-13 19:03:45 -04:00
|
|
|
* RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
|
2014-04-02 16:21:12 -04:00
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
2014-08-09 11:26:58 -04:00
|
|
|
#if defined(USE_WINDOWS_SSPI)
|
2014-04-02 16:21:12 -04:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2014-04-06 11:11:17 -04:00
|
|
|
#include "curl_sasl.h"
|
2014-04-06 07:08:11 -04:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "curl_base64.h"
|
|
|
|
#include "warnless.h"
|
2014-04-06 11:09:19 -04:00
|
|
|
#include "curl_memory.h"
|
2014-08-09 11:26:58 -04:00
|
|
|
#include "curl_multibyte.h"
|
2014-11-16 07:58:04 -05:00
|
|
|
#include "sendf.h"
|
2014-11-06 10:05:36 -05:00
|
|
|
#include "strdup.h"
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-04-02 16:21:12 -04:00
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2014-08-09 11:26:58 -04:00
|
|
|
/*
|
|
|
|
* Curl_sasl_build_spn()
|
|
|
|
*
|
|
|
|
* This is used to build a SPN string in the format service/host.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* serivce [in] - The service type such as www, smtp, pop or imap.
|
2014-11-15 13:35:40 -05:00
|
|
|
* host [in] - The host name or realm.
|
2014-08-09 11:26:58 -04:00
|
|
|
*
|
|
|
|
* Returns a pointer to the newly allocated SPN.
|
|
|
|
*/
|
|
|
|
TCHAR *Curl_sasl_build_spn(const char *service, const char *host)
|
|
|
|
{
|
|
|
|
char *utf8_spn = NULL;
|
|
|
|
TCHAR *tchar_spn = NULL;
|
|
|
|
|
|
|
|
/* Note: We could use DsMakeSPN() or DsClientMakeSpnForTargetServer() rather
|
|
|
|
than doing this ourselves but the first is only available in Windows XP
|
|
|
|
and Windows Server 2003 and the latter is only available in Windows 2000
|
|
|
|
but not Windows95/98/ME or Windows NT4.0 unless the Active Directory
|
|
|
|
Client Extensions are installed. As such it is far simpler for us to
|
|
|
|
formulate the SPN instead. */
|
|
|
|
|
|
|
|
/* Allocate our UTF8 based SPN */
|
|
|
|
utf8_spn = aprintf("%s/%s", service, host);
|
|
|
|
if(!utf8_spn) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate our TCHAR based SPN */
|
|
|
|
tchar_spn = Curl_convert_UTF8_to_tchar(utf8_spn);
|
|
|
|
if(!tchar_spn) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(utf8_spn);
|
2014-08-09 11:26:58 -04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the UTF8 variant when operating with Unicode */
|
2014-09-10 08:32:56 -04:00
|
|
|
Curl_unicodefree(utf8_spn);
|
2014-08-09 11:26:58 -04:00
|
|
|
|
|
|
|
/* Return our newly allocated SPN */
|
|
|
|
return tchar_spn;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(CURL_DISABLE_CRYPTO_AUTH)
|
2014-04-02 16:21:12 -04:00
|
|
|
/*
|
|
|
|
* Curl_sasl_create_digest_md5_message()
|
|
|
|
*
|
|
|
|
* This is used to generate an already encoded DIGEST-MD5 response message
|
|
|
|
* ready for sending to the recipient.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* data [in] - The session handle.
|
2014-11-23 11:31:39 -05:00
|
|
|
* chlg64 [in] - The base64 encoded challenge message.
|
|
|
|
* userp [in] - The user name in the format User or Domain\User.
|
2014-04-02 16:21:12 -04:00
|
|
|
* passdwp [in] - The user's password.
|
|
|
|
* service [in] - The service type such as www, smtp, pop or imap.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_create_digest_md5_message(struct SessionHandle *data,
|
|
|
|
const char *chlg64,
|
|
|
|
const char *userp,
|
|
|
|
const char *passwdp,
|
|
|
|
const char *service,
|
|
|
|
char **outptr, size_t *outlen)
|
|
|
|
{
|
2014-04-06 07:08:11 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2014-08-09 12:04:10 -04:00
|
|
|
TCHAR *spn = NULL;
|
2014-04-06 07:08:11 -04:00
|
|
|
size_t chlglen = 0;
|
2014-11-06 18:12:18 -05:00
|
|
|
size_t token_max = 0;
|
|
|
|
unsigned char *input_token = NULL;
|
|
|
|
unsigned char *output_token = NULL;
|
|
|
|
CredHandle credentials;
|
|
|
|
CtxtHandle context;
|
2014-04-06 07:08:11 -04:00
|
|
|
PSecPkgInfo SecurityPackage;
|
|
|
|
SEC_WINNT_AUTH_IDENTITY identity;
|
2014-10-29 10:05:21 -04:00
|
|
|
SEC_WINNT_AUTH_IDENTITY *p_identity;
|
2014-04-06 07:08:11 -04:00
|
|
|
SecBuffer chlg_buf;
|
|
|
|
SecBuffer resp_buf;
|
|
|
|
SecBufferDesc chlg_desc;
|
|
|
|
SecBufferDesc resp_desc;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
unsigned long attrs;
|
2014-10-26 10:29:27 -04:00
|
|
|
TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
/* Decode the base-64 encoded challenge message */
|
|
|
|
if(strlen(chlg64) && *chlg64 != '=') {
|
2014-11-06 18:12:18 -05:00
|
|
|
result = Curl_base64_decode(chlg64, &input_token, &chlglen);
|
2014-04-06 07:08:11 -04:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we have a valid challenge message */
|
2014-11-16 08:27:03 -05:00
|
|
|
if(!input_token) {
|
|
|
|
infof(data, "DIGEST-MD5 handshake failure (empty challenge message)\n");
|
|
|
|
|
2014-04-06 07:08:11 -04:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2014-11-16 08:27:03 -05:00
|
|
|
}
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
/* Query the security package for DigestSSP */
|
2014-11-05 06:43:22 -05:00
|
|
|
status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
|
2014-04-06 07:08:11 -04:00
|
|
|
&SecurityPackage);
|
2014-04-06 09:10:18 -04:00
|
|
|
if(status != SEC_E_OK) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(input_token);
|
2014-08-10 06:01:27 -04:00
|
|
|
|
2014-04-06 07:08:11 -04:00
|
|
|
return CURLE_NOT_BUILT_IN;
|
2014-04-06 09:10:18 -04:00
|
|
|
}
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-11-06 18:12:18 -05:00
|
|
|
token_max = SecurityPackage->cbMaxToken;
|
2014-08-10 06:01:27 -04:00
|
|
|
|
2014-08-09 07:34:22 -04:00
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
|
|
s_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
|
2014-08-10 06:01:27 -04:00
|
|
|
/* Allocate our response buffer */
|
2014-11-06 18:12:18 -05:00
|
|
|
output_token = malloc(token_max);
|
|
|
|
if(!output_token) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(input_token);
|
2014-08-10 06:01:27 -04:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-08-09 12:04:10 -04:00
|
|
|
/* Generate our SPN */
|
|
|
|
spn = Curl_sasl_build_spn(service, data->easy_conn->host.name);
|
2014-08-10 05:33:10 -04:00
|
|
|
if(!spn) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(output_token);
|
|
|
|
free(input_token);
|
2014-08-10 05:33:10 -04:00
|
|
|
|
2014-04-06 07:08:11 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2014-08-10 05:33:10 -04:00
|
|
|
}
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-10-29 10:05:21 -04:00
|
|
|
if(userp && *userp) {
|
|
|
|
/* Populate our identity structure */
|
|
|
|
result = Curl_create_sspi_identity(userp, passwdp, &identity);
|
|
|
|
if(result) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(spn);
|
|
|
|
free(output_token);
|
|
|
|
free(input_token);
|
2014-10-29 10:05:21 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-10-29 10:05:21 -04:00
|
|
|
/* Allow proper cleanup of the identity structure */
|
|
|
|
p_identity = &identity;
|
2014-04-06 07:08:11 -04:00
|
|
|
}
|
2014-10-29 10:05:21 -04:00
|
|
|
else
|
|
|
|
/* Use the current Windows user */
|
|
|
|
p_identity = NULL;
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-10-15 16:48:59 -04:00
|
|
|
/* Acquire our credentials handle */
|
2014-04-06 07:08:11 -04:00
|
|
|
status = s_pSecFn->AcquireCredentialsHandle(NULL,
|
2014-11-05 06:43:22 -05:00
|
|
|
(TCHAR *) TEXT(SP_NAME_DIGEST),
|
2014-04-06 07:08:11 -04:00
|
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
2014-10-29 10:05:21 -04:00
|
|
|
p_identity, NULL, NULL,
|
2014-11-06 18:12:18 -05:00
|
|
|
&credentials, &expiry);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
if(status != SEC_E_OK) {
|
2014-10-29 10:05:21 -04:00
|
|
|
Curl_sspi_free_identity(p_identity);
|
2015-03-16 10:01:15 -04:00
|
|
|
free(spn);
|
|
|
|
free(output_token);
|
|
|
|
free(input_token);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-10-29 10:14:17 -04:00
|
|
|
return CURLE_LOGIN_DENIED;
|
2014-04-06 07:08:11 -04:00
|
|
|
}
|
|
|
|
|
2014-04-06 08:29:29 -04:00
|
|
|
/* Setup the challenge "input" security buffer */
|
2014-04-06 07:08:11 -04:00
|
|
|
chlg_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
chlg_desc.cBuffers = 1;
|
|
|
|
chlg_desc.pBuffers = &chlg_buf;
|
|
|
|
chlg_buf.BufferType = SECBUFFER_TOKEN;
|
2014-11-06 18:12:18 -05:00
|
|
|
chlg_buf.pvBuffer = input_token;
|
2014-04-06 07:08:11 -04:00
|
|
|
chlg_buf.cbBuffer = curlx_uztoul(chlglen);
|
|
|
|
|
2014-04-06 08:29:29 -04:00
|
|
|
/* Setup the response "output" security buffer */
|
2014-04-06 07:08:11 -04:00
|
|
|
resp_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
resp_desc.cBuffers = 1;
|
|
|
|
resp_desc.pBuffers = &resp_buf;
|
|
|
|
resp_buf.BufferType = SECBUFFER_TOKEN;
|
2014-11-06 18:12:18 -05:00
|
|
|
resp_buf.pvBuffer = output_token;
|
|
|
|
resp_buf.cbBuffer = curlx_uztoul(token_max);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-11-06 18:12:18 -05:00
|
|
|
/* Generate our response message */
|
|
|
|
status = s_pSecFn->InitializeSecurityContext(&credentials, NULL, spn,
|
|
|
|
0, 0, 0, &chlg_desc, 0,
|
|
|
|
&context, &resp_desc, &attrs,
|
|
|
|
&expiry);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-10-26 10:26:39 -04:00
|
|
|
if(status == SEC_I_COMPLETE_NEEDED ||
|
|
|
|
status == SEC_I_COMPLETE_AND_CONTINUE)
|
2014-11-06 18:12:18 -05:00
|
|
|
s_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
|
2014-10-26 10:26:39 -04:00
|
|
|
else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
|
2014-11-06 18:12:18 -05:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&credentials);
|
2014-10-29 10:05:21 -04:00
|
|
|
Curl_sspi_free_identity(p_identity);
|
2015-03-16 10:01:15 -04:00
|
|
|
free(spn);
|
|
|
|
free(output_token);
|
|
|
|
free(input_token);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Base64 encode the response */
|
2014-11-06 18:12:18 -05:00
|
|
|
result = Curl_base64_encode(data, (char *) output_token, resp_buf.cbBuffer,
|
|
|
|
outptr, outlen);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
/* Free our handles */
|
2014-11-06 18:12:18 -05:00
|
|
|
s_pSecFn->DeleteSecurityContext(&context);
|
|
|
|
s_pSecFn->FreeCredentialsHandle(&credentials);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
/* Free the identity structure */
|
2014-10-29 10:05:21 -04:00
|
|
|
Curl_sspi_free_identity(p_identity);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
/* Free the SPN */
|
2015-03-16 10:01:15 -04:00
|
|
|
free(spn);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-08-10 06:01:27 -04:00
|
|
|
/* Free the response buffer */
|
2015-03-16 10:01:15 -04:00
|
|
|
free(output_token);
|
2014-08-10 06:01:27 -04:00
|
|
|
|
2014-10-28 18:47:22 -04:00
|
|
|
/* Free the decoded challenge message */
|
2015-03-16 10:01:15 -04:00
|
|
|
free(input_token);
|
2014-04-06 09:10:18 -04:00
|
|
|
|
2014-04-06 07:08:11 -04:00
|
|
|
return result;
|
2014-04-02 16:21:12 -04:00
|
|
|
}
|
2014-11-06 05:39:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_decode_digest_http_message()
|
|
|
|
*
|
|
|
|
* This is used to decode a HTTP DIGEST challenge message into the seperate
|
|
|
|
* attributes.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
2014-11-23 11:31:39 -05:00
|
|
|
* chlg [in] - The challenge message.
|
2014-11-06 05:39:37 -05:00
|
|
|
* digest [in/out] - The digest data struct being used and modified.
|
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_decode_digest_http_message(const char *chlg,
|
|
|
|
struct digestdata *digest)
|
|
|
|
{
|
2014-11-06 10:05:36 -05:00
|
|
|
size_t chlglen = strlen(chlg);
|
|
|
|
|
2014-11-06 10:25:45 -05:00
|
|
|
/* We had an input token before and we got another one now. This means we
|
|
|
|
provided bad credentials in the previous request. */
|
|
|
|
if(digest->input_token)
|
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2014-11-06 05:39:37 -05:00
|
|
|
|
2014-11-06 06:42:49 -05:00
|
|
|
/* Simply store the challenge for use later */
|
2014-11-06 10:05:36 -05:00
|
|
|
digest->input_token = (BYTE *) Curl_memdup(chlg, chlglen);
|
2014-11-06 06:42:49 -05:00
|
|
|
if(!digest->input_token)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2014-11-06 10:05:36 -05:00
|
|
|
digest->input_token_len = chlglen;
|
|
|
|
|
2014-11-06 06:42:49 -05:00
|
|
|
return CURLE_OK;
|
2014-11-06 05:39:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_create_digest_http_message()
|
|
|
|
*
|
|
|
|
* This is used to generate a HTTP DIGEST response message ready for sending
|
|
|
|
* to the recipient.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* data [in] - The session handle.
|
2014-11-23 11:31:39 -05:00
|
|
|
* userp [in] - The user name in the format User or Domain\User.
|
2014-11-06 05:39:37 -05:00
|
|
|
* passdwp [in] - The user's password.
|
|
|
|
* request [in] - The HTTP request.
|
|
|
|
* uripath [in] - The path of the HTTP uri.
|
|
|
|
* digest [in/out] - The digest 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.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_create_digest_http_message(struct SessionHandle *data,
|
|
|
|
const char *userp,
|
|
|
|
const char *passwdp,
|
|
|
|
const unsigned char *request,
|
|
|
|
const unsigned char *uripath,
|
|
|
|
struct digestdata *digest,
|
|
|
|
char **outptr, size_t *outlen)
|
|
|
|
{
|
2014-11-06 07:10:50 -05:00
|
|
|
size_t token_max;
|
|
|
|
CredHandle credentials;
|
|
|
|
CtxtHandle context;
|
|
|
|
char *resp;
|
|
|
|
BYTE *output_token;
|
|
|
|
PSecPkgInfo SecurityPackage;
|
|
|
|
SEC_WINNT_AUTH_IDENTITY identity;
|
|
|
|
SEC_WINNT_AUTH_IDENTITY *p_identity;
|
|
|
|
SecBuffer chlg_buf[3];
|
|
|
|
SecBuffer resp_buf;
|
|
|
|
SecBufferDesc chlg_desc;
|
|
|
|
SecBufferDesc resp_desc;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
unsigned long attrs;
|
|
|
|
TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
|
|
|
|
|
2014-11-06 08:27:30 -05:00
|
|
|
(void) data;
|
|
|
|
|
2014-11-06 07:10:50 -05:00
|
|
|
/* Query the security package for DigestSSP */
|
|
|
|
status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
|
|
|
|
&SecurityPackage);
|
|
|
|
if(status != SEC_E_OK)
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
|
|
|
|
token_max = SecurityPackage->cbMaxToken;
|
|
|
|
|
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
|
|
s_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
|
|
|
|
/* Allocate the output buffer according to the max token size as indicated
|
|
|
|
by the security package */
|
|
|
|
output_token = malloc(token_max);
|
|
|
|
if(!output_token)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if(userp && *userp) {
|
|
|
|
/* Populate our identity structure */
|
|
|
|
if(Curl_create_sspi_identity(userp, passwdp, &identity))
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Allow proper cleanup of the identity structure */
|
|
|
|
p_identity = &identity;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Use the current Windows user */
|
|
|
|
p_identity = NULL;
|
|
|
|
|
|
|
|
/* Acquire our credentials handle */
|
|
|
|
status = s_pSecFn->AcquireCredentialsHandle(NULL,
|
|
|
|
(TCHAR *) TEXT(SP_NAME_DIGEST),
|
|
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
|
|
|
p_identity, NULL, NULL,
|
|
|
|
&credentials, &expiry);
|
|
|
|
if(status != SEC_E_OK) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(output_token);
|
2014-11-06 07:10:50 -05:00
|
|
|
|
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the challenge "input" security buffer if present */
|
|
|
|
chlg_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
chlg_desc.cBuffers = 3;
|
|
|
|
chlg_desc.pBuffers = chlg_buf;
|
|
|
|
chlg_buf[0].BufferType = SECBUFFER_TOKEN;
|
|
|
|
chlg_buf[0].pvBuffer = digest->input_token;
|
2014-11-06 10:05:36 -05:00
|
|
|
chlg_buf[0].cbBuffer = curlx_uztoul(digest->input_token_len);
|
2014-11-06 07:10:50 -05:00
|
|
|
chlg_buf[1].BufferType = SECBUFFER_PKG_PARAMS;
|
|
|
|
chlg_buf[1].pvBuffer = (void *)request;
|
|
|
|
chlg_buf[1].cbBuffer = curlx_uztoul(strlen((const char *) request));
|
|
|
|
chlg_buf[2].BufferType = SECBUFFER_PKG_PARAMS;
|
|
|
|
chlg_buf[2].pvBuffer = NULL;
|
|
|
|
chlg_buf[2].cbBuffer = 0;
|
|
|
|
|
|
|
|
/* 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 = output_token;
|
|
|
|
resp_buf.cbBuffer = curlx_uztoul(token_max);
|
|
|
|
|
|
|
|
/* Generate our reponse message */
|
|
|
|
status = s_pSecFn->InitializeSecurityContext(&credentials, NULL,
|
|
|
|
(TCHAR *) uripath,
|
|
|
|
ISC_REQ_USE_HTTP_STYLE, 0, 0,
|
|
|
|
&chlg_desc, 0, &context,
|
|
|
|
&resp_desc, &attrs, &expiry);
|
|
|
|
|
|
|
|
if(status == SEC_I_COMPLETE_NEEDED ||
|
|
|
|
status == SEC_I_COMPLETE_AND_CONTINUE)
|
|
|
|
s_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
|
|
|
|
else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
|
|
|
|
s_pSecFn->FreeCredentialsHandle(&credentials);
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(output_token);
|
2014-11-06 07:10:50 -05:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = malloc(resp_buf.cbBuffer + 1);
|
|
|
|
if(!resp) {
|
|
|
|
s_pSecFn->DeleteSecurityContext(&context);
|
|
|
|
s_pSecFn->FreeCredentialsHandle(&credentials);
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(output_token);
|
2014-11-06 07:10:50 -05:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the generated reponse */
|
|
|
|
memcpy(resp, resp_buf.pvBuffer, resp_buf.cbBuffer);
|
|
|
|
resp[resp_buf.cbBuffer] = 0x00;
|
|
|
|
|
|
|
|
/* Return the response */
|
|
|
|
*outptr = resp;
|
|
|
|
*outlen = resp_buf.cbBuffer;
|
|
|
|
|
|
|
|
/* Free our handles */
|
|
|
|
s_pSecFn->DeleteSecurityContext(&context);
|
|
|
|
s_pSecFn->FreeCredentialsHandle(&credentials);
|
|
|
|
|
|
|
|
/* Free the identity structure */
|
|
|
|
Curl_sspi_free_identity(p_identity);
|
|
|
|
|
|
|
|
/* Free the response buffer */
|
2015-03-16 10:01:15 -04:00
|
|
|
free(output_token);
|
2014-11-06 07:10:50 -05:00
|
|
|
|
|
|
|
return CURLE_OK;
|
2014-11-06 05:39:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_digest_cleanup()
|
|
|
|
*
|
|
|
|
* This is used to clean up the digest specific data.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* digest [in/out] - The digest data struct being cleaned up.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void Curl_sasl_digest_cleanup(struct digestdata *digest)
|
|
|
|
{
|
2014-11-06 06:30:45 -05:00
|
|
|
/* Free the input token */
|
|
|
|
Curl_safefree(digest->input_token);
|
2014-11-06 10:05:36 -05:00
|
|
|
|
|
|
|
/* Reset any variables */
|
|
|
|
digest->input_token_len = 0;
|
2014-11-06 05:39:37 -05:00
|
|
|
}
|
2014-08-09 11:26:58 -04:00
|
|
|
#endif /* !CURL_DISABLE_CRYPTO_AUTH */
|
|
|
|
|
2014-10-09 06:03:14 -04:00
|
|
|
#if defined USE_NTLM
|
2014-11-09 10:12:56 -05:00
|
|
|
/*
|
|
|
|
* Curl_sasl_create_ntlm_type1_message()
|
|
|
|
*
|
|
|
|
* This is used to generate an already encoded NTLM type-1 message ready for
|
|
|
|
* sending to the recipient.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* userp [in] - The user name in the format User or Domain\User.
|
|
|
|
* passdwp [in] - The user's password.
|
|
|
|
* ntlm [in/out] - The ntlm 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.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_create_ntlm_type1_message(const char *userp,
|
|
|
|
const char *passwdp,
|
|
|
|
struct ntlmdata *ntlm,
|
|
|
|
char **outptr, size_t *outlen)
|
|
|
|
{
|
2014-11-16 07:45:30 -05:00
|
|
|
PSecPkgInfo SecurityPackage;
|
|
|
|
SecBuffer type_1_buf;
|
|
|
|
SecBufferDesc type_1_desc;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
unsigned long attrs;
|
|
|
|
TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
|
|
|
|
|
|
|
|
/* Clean up any former leftovers and initialise to defaults */
|
|
|
|
Curl_sasl_ntlm_cleanup(ntlm);
|
|
|
|
|
|
|
|
/* Query the security package for NTLM */
|
|
|
|
status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_NTLM),
|
|
|
|
&SecurityPackage);
|
|
|
|
if(status != SEC_E_OK)
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
|
|
|
|
ntlm->token_max = SecurityPackage->cbMaxToken;
|
|
|
|
|
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
|
|
s_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
|
|
|
|
/* Allocate our output buffer */
|
|
|
|
ntlm->output_token = malloc(ntlm->token_max);
|
|
|
|
if(!ntlm->output_token)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if(userp && *userp) {
|
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
/* Populate our identity structure */
|
|
|
|
result = Curl_create_sspi_identity(userp, passwdp, &ntlm->identity);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Allow proper cleanup of the identity structure */
|
|
|
|
ntlm->p_identity = &ntlm->identity;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Use the current Windows user */
|
|
|
|
ntlm->p_identity = NULL;
|
|
|
|
|
|
|
|
/* Allocate our credentials handle */
|
|
|
|
ntlm->credentials = malloc(sizeof(CredHandle));
|
|
|
|
if(!ntlm->credentials)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
memset(ntlm->credentials, 0, sizeof(CredHandle));
|
|
|
|
|
|
|
|
/* Acquire our credentials handle */
|
|
|
|
status = s_pSecFn->AcquireCredentialsHandle(NULL,
|
|
|
|
(TCHAR *) TEXT(SP_NAME_NTLM),
|
|
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
|
|
|
ntlm->p_identity, NULL, NULL,
|
|
|
|
ntlm->credentials, &expiry);
|
|
|
|
if(status != SEC_E_OK)
|
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
|
|
|
|
/* Allocate our new context handle */
|
|
|
|
ntlm->context = malloc(sizeof(CtxtHandle));
|
|
|
|
if(!ntlm->context)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
memset(ntlm->context, 0, sizeof(CtxtHandle));
|
|
|
|
|
|
|
|
/* Setup the type-1 "output" security buffer */
|
|
|
|
type_1_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
type_1_desc.cBuffers = 1;
|
|
|
|
type_1_desc.pBuffers = &type_1_buf;
|
|
|
|
type_1_buf.BufferType = SECBUFFER_TOKEN;
|
|
|
|
type_1_buf.pvBuffer = ntlm->output_token;
|
|
|
|
type_1_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
|
|
|
|
|
|
|
|
/* Generate our type-1 message */
|
|
|
|
status = s_pSecFn->InitializeSecurityContext(ntlm->credentials, NULL,
|
|
|
|
(TCHAR *) TEXT(""),
|
|
|
|
0, 0, SECURITY_NETWORK_DREP,
|
|
|
|
NULL, 0,
|
|
|
|
ntlm->context, &type_1_desc,
|
|
|
|
&attrs, &expiry);
|
|
|
|
if(status == SEC_I_COMPLETE_NEEDED ||
|
|
|
|
status == SEC_I_COMPLETE_AND_CONTINUE)
|
|
|
|
s_pSecFn->CompleteAuthToken(ntlm->context, &type_1_desc);
|
|
|
|
else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED)
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
|
|
|
|
/* Base64 encode the response */
|
|
|
|
return Curl_base64_encode(NULL, (char *) ntlm->output_token,
|
|
|
|
type_1_buf.cbBuffer, outptr, outlen);
|
2014-11-09 10:12:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_decode_ntlm_type2_message()
|
|
|
|
*
|
|
|
|
* This is used to decode an already encoded NTLM type-2 message.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
2014-11-23 11:31:39 -05:00
|
|
|
* data [in] - The session handle.
|
|
|
|
* type2msg [in] - The base64 encoded type-2 message.
|
2014-11-09 10:12:56 -05:00
|
|
|
* ntlm [in/out] - The ntlm data struct being used and modified.
|
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_decode_ntlm_type2_message(struct SessionHandle *data,
|
|
|
|
const char *type2msg,
|
|
|
|
struct ntlmdata *ntlm)
|
|
|
|
{
|
2014-11-16 07:58:04 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
unsigned char *type2 = NULL;
|
|
|
|
size_t type2_len = 0;
|
|
|
|
|
2014-11-16 09:39:26 -05:00
|
|
|
#if defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
(void) data;
|
|
|
|
#endif
|
|
|
|
|
2014-11-16 07:58:04 -05:00
|
|
|
/* Decode the base-64 encoded type-2 message */
|
|
|
|
if(strlen(type2msg) && *type2msg != '=') {
|
|
|
|
result = Curl_base64_decode(type2msg, &type2, &type2_len);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we have a valid type-2 message */
|
|
|
|
if(!type2) {
|
|
|
|
infof(data, "NTLM handshake failure (empty type-2 message)\n");
|
|
|
|
|
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Simply store the challenge for use later */
|
|
|
|
ntlm->input_token = type2;
|
|
|
|
ntlm->input_token_len = type2_len;
|
|
|
|
|
|
|
|
return result;
|
2014-11-09 10:12:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_create_ntlm_type3_message()
|
|
|
|
*
|
|
|
|
* This is used to generate an already encoded NTLM type-3 message ready for
|
|
|
|
* sending to the recipient.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
2014-11-23 11:31:39 -05:00
|
|
|
* data [in] - The session handle.
|
2014-11-09 10:12:56 -05:00
|
|
|
* userp [in] - The user name in the format User or Domain\User.
|
|
|
|
* passdwp [in] - The user's password.
|
|
|
|
* ntlm [in/out] - The ntlm 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.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_create_ntlm_type3_message(struct SessionHandle *data,
|
|
|
|
const char *userp,
|
|
|
|
const char *passwdp,
|
|
|
|
struct ntlmdata *ntlm,
|
|
|
|
char **outptr, size_t *outlen)
|
|
|
|
{
|
2014-11-16 08:18:26 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
SecBuffer type_2_buf;
|
|
|
|
SecBuffer type_3_buf;
|
|
|
|
SecBufferDesc type_2_desc;
|
|
|
|
SecBufferDesc type_3_desc;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
unsigned long attrs;
|
|
|
|
TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
|
|
|
|
|
|
|
|
(void) passwdp;
|
|
|
|
(void) userp;
|
|
|
|
|
|
|
|
/* Setup the type-2 "input" security buffer */
|
|
|
|
type_2_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
type_2_desc.cBuffers = 1;
|
|
|
|
type_2_desc.pBuffers = &type_2_buf;
|
|
|
|
type_2_buf.BufferType = SECBUFFER_TOKEN;
|
|
|
|
type_2_buf.pvBuffer = ntlm->input_token;
|
|
|
|
type_2_buf.cbBuffer = curlx_uztoul(ntlm->input_token_len);
|
|
|
|
|
|
|
|
/* Setup the type-3 "output" security buffer */
|
|
|
|
type_3_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
type_3_desc.cBuffers = 1;
|
|
|
|
type_3_desc.pBuffers = &type_3_buf;
|
|
|
|
type_3_buf.BufferType = SECBUFFER_TOKEN;
|
|
|
|
type_3_buf.pvBuffer = ntlm->output_token;
|
|
|
|
type_3_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
|
|
|
|
|
|
|
|
/* Generate our type-3 message */
|
|
|
|
status = s_pSecFn->InitializeSecurityContext(ntlm->credentials,
|
|
|
|
ntlm->context,
|
|
|
|
(TCHAR *) TEXT(""),
|
|
|
|
0, 0, SECURITY_NETWORK_DREP,
|
|
|
|
&type_2_desc,
|
|
|
|
0, ntlm->context,
|
|
|
|
&type_3_desc,
|
|
|
|
&attrs, &expiry);
|
|
|
|
if(status != SEC_E_OK) {
|
|
|
|
infof(data, "NTLM handshake failure (type-3 message): Status=%x\n",
|
|
|
|
status);
|
|
|
|
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Base64 encode the response */
|
2014-11-16 09:39:26 -05:00
|
|
|
result = Curl_base64_encode(data, (char *) ntlm->output_token,
|
2014-11-16 08:18:26 -05:00
|
|
|
type_3_buf.cbBuffer, outptr, outlen);
|
|
|
|
|
|
|
|
Curl_sasl_ntlm_cleanup(ntlm);
|
|
|
|
|
|
|
|
return result;
|
2014-11-09 10:12:56 -05:00
|
|
|
}
|
|
|
|
|
2014-10-09 06:03:14 -04:00
|
|
|
/*
|
|
|
|
* Curl_sasl_ntlm_cleanup()
|
|
|
|
*
|
|
|
|
* This is used to clean up the ntlm specific data.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* ntlm [in/out] - The ntlm data struct being cleaned up.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void Curl_sasl_ntlm_cleanup(struct ntlmdata *ntlm)
|
|
|
|
{
|
|
|
|
/* Free our security context */
|
|
|
|
if(ntlm->context) {
|
|
|
|
s_pSecFn->DeleteSecurityContext(ntlm->context);
|
|
|
|
free(ntlm->context);
|
|
|
|
ntlm->context = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free our credentials handle */
|
|
|
|
if(ntlm->credentials) {
|
|
|
|
s_pSecFn->FreeCredentialsHandle(ntlm->credentials);
|
|
|
|
free(ntlm->credentials);
|
|
|
|
ntlm->credentials = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free our identity */
|
|
|
|
Curl_sspi_free_identity(ntlm->p_identity);
|
|
|
|
ntlm->p_identity = NULL;
|
|
|
|
|
|
|
|
/* Free the input and output tokens */
|
|
|
|
Curl_safefree(ntlm->input_token);
|
|
|
|
Curl_safefree(ntlm->output_token);
|
|
|
|
|
|
|
|
/* Reset any variables */
|
|
|
|
ntlm->token_max = 0;
|
|
|
|
}
|
|
|
|
#endif /* USE_NTLM */
|
|
|
|
|
2014-11-16 04:50:06 -05:00
|
|
|
#if defined(USE_KERBEROS5)
|
2014-08-13 19:03:45 -04:00
|
|
|
/*
|
|
|
|
* Curl_sasl_create_gssapi_user_message()
|
|
|
|
*
|
|
|
|
* This is used to generate an already encoded GSSAPI (Kerberos V5) user token
|
|
|
|
* message ready for sending to the recipient.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
2014-08-17 17:43:43 -04:00
|
|
|
* data [in] - The session handle.
|
2014-11-23 11:31:39 -05:00
|
|
|
* userp [in] - The user name in the format User or Domain\User.
|
2014-08-17 17:43:43 -04:00
|
|
|
* passdwp [in] - The user's password.
|
|
|
|
* service [in] - The service type such as www, smtp, pop or imap.
|
|
|
|
* mutual_auth [in] - Flag specifing whether or not mutual authentication
|
|
|
|
* is enabled.
|
2014-11-23 11:31:39 -05:00
|
|
|
* chlg64 [in] - The optional base64 encoded challenge message.
|
2014-08-17 17:43:43 -04:00
|
|
|
* krb5 [in/out] - The gssapi 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.
|
2014-08-13 19:03:45 -04:00
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
|
|
|
|
const char *userp,
|
|
|
|
const char *passwdp,
|
|
|
|
const char *service,
|
2014-08-17 17:43:43 -04:00
|
|
|
const bool mutual_auth,
|
2014-08-13 19:03:45 -04:00
|
|
|
const char *chlg64,
|
|
|
|
struct kerberos5data *krb5,
|
|
|
|
char **outptr, size_t *outlen)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
size_t chlglen = 0;
|
|
|
|
unsigned char *chlg = NULL;
|
|
|
|
CtxtHandle context;
|
|
|
|
PSecPkgInfo SecurityPackage;
|
|
|
|
SecBuffer chlg_buf;
|
|
|
|
SecBuffer resp_buf;
|
|
|
|
SecBufferDesc chlg_desc;
|
|
|
|
SecBufferDesc resp_desc;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
unsigned long attrs;
|
2014-10-26 10:29:27 -04:00
|
|
|
TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
if(!krb5->credentials) {
|
|
|
|
/* Query the security package for Kerberos */
|
2014-11-05 06:43:22 -05:00
|
|
|
status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
|
|
|
|
TEXT(SP_NAME_KERBEROS),
|
2014-08-13 19:03:45 -04:00
|
|
|
&SecurityPackage);
|
|
|
|
if(status != SEC_E_OK) {
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
}
|
|
|
|
|
|
|
|
krb5->token_max = SecurityPackage->cbMaxToken;
|
|
|
|
|
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
|
|
s_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
|
2014-10-15 16:20:25 -04:00
|
|
|
/* Allocate our response buffer */
|
|
|
|
krb5->output_token = malloc(krb5->token_max);
|
|
|
|
if(!krb5->output_token)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2014-08-13 19:03:45 -04:00
|
|
|
/* Generate our SPN */
|
|
|
|
krb5->spn = Curl_sasl_build_spn(service, data->easy_conn->host.name);
|
|
|
|
if(!krb5->spn)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if(userp && *userp) {
|
|
|
|
/* Populate our identity structure */
|
|
|
|
result = Curl_create_sspi_identity(userp, passwdp, &krb5->identity);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Allow proper cleanup of the identity structure */
|
|
|
|
krb5->p_identity = &krb5->identity;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Use the current Windows user */
|
|
|
|
krb5->p_identity = NULL;
|
|
|
|
|
|
|
|
/* Allocate our credentials handle */
|
|
|
|
krb5->credentials = malloc(sizeof(CredHandle));
|
|
|
|
if(!krb5->credentials)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
memset(krb5->credentials, 0, sizeof(CredHandle));
|
|
|
|
|
2014-10-15 16:48:59 -04:00
|
|
|
/* Acquire our credentials handle */
|
2014-08-13 19:03:45 -04:00
|
|
|
status = s_pSecFn->AcquireCredentialsHandle(NULL,
|
2014-11-05 06:43:22 -05:00
|
|
|
(TCHAR *)
|
|
|
|
TEXT(SP_NAME_KERBEROS),
|
2014-08-13 19:03:45 -04:00
|
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
|
|
|
krb5->p_identity, NULL, NULL,
|
2014-10-26 10:29:27 -04:00
|
|
|
krb5->credentials, &expiry);
|
2014-08-13 19:03:45 -04:00
|
|
|
if(status != SEC_E_OK)
|
2014-10-29 10:14:17 -04:00
|
|
|
return CURLE_LOGIN_DENIED;
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
/* Allocate our new context handle */
|
|
|
|
krb5->context = malloc(sizeof(CtxtHandle));
|
|
|
|
if(!krb5->context)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
memset(krb5->context, 0, sizeof(CtxtHandle));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Decode the base-64 encoded challenge message */
|
|
|
|
if(strlen(chlg64) && *chlg64 != '=') {
|
|
|
|
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we have a valid challenge message */
|
2014-11-16 08:27:03 -05:00
|
|
|
if(!chlg) {
|
|
|
|
infof(data, "GSSAPI handshake failure (empty challenge message)\n");
|
|
|
|
|
2014-08-13 19:03:45 -04:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2014-11-16 08:27:03 -05:00
|
|
|
}
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
/* Setup the challenge "input" security buffer */
|
|
|
|
chlg_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
chlg_desc.cBuffers = 1;
|
|
|
|
chlg_desc.pBuffers = &chlg_buf;
|
|
|
|
chlg_buf.BufferType = SECBUFFER_TOKEN;
|
|
|
|
chlg_buf.pvBuffer = chlg;
|
|
|
|
chlg_buf.cbBuffer = curlx_uztoul(chlglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 = krb5->output_token;
|
|
|
|
resp_buf.cbBuffer = curlx_uztoul(krb5->token_max);
|
|
|
|
|
|
|
|
/* Generate our challenge-response message */
|
|
|
|
status = s_pSecFn->InitializeSecurityContext(krb5->credentials,
|
|
|
|
chlg ? krb5->context : NULL,
|
|
|
|
krb5->spn,
|
2014-08-17 17:43:43 -04:00
|
|
|
(mutual_auth ?
|
2014-08-13 19:03:45 -04:00
|
|
|
ISC_REQ_MUTUAL_AUTH : 0),
|
|
|
|
0, SECURITY_NATIVE_DREP,
|
|
|
|
chlg ? &chlg_desc : NULL, 0,
|
|
|
|
&context,
|
|
|
|
&resp_desc, &attrs,
|
2014-10-26 10:29:27 -04:00
|
|
|
&expiry);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
2014-08-17 17:58:48 -04:00
|
|
|
if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(chlg);
|
2014-08-17 17:58:48 -04:00
|
|
|
|
2014-08-13 19:03:45 -04:00
|
|
|
return CURLE_RECV_ERROR;
|
2014-08-17 17:58:48 -04:00
|
|
|
}
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
if(memcmp(&context, krb5->context, sizeof(context))) {
|
|
|
|
s_pSecFn->DeleteSecurityContext(krb5->context);
|
|
|
|
|
|
|
|
memcpy(krb5->context, &context, sizeof(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resp_buf.cbBuffer) {
|
|
|
|
/* Base64 encode the response */
|
|
|
|
result = Curl_base64_encode(data, (char *)resp_buf.pvBuffer,
|
|
|
|
resp_buf.cbBuffer, outptr, outlen);
|
|
|
|
}
|
|
|
|
|
2014-08-17 17:58:48 -04:00
|
|
|
/* Free the decoded challenge */
|
2015-03-16 10:01:15 -04:00
|
|
|
free(chlg);
|
2014-08-17 17:58:48 -04:00
|
|
|
|
2014-08-13 19:03:45 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_create_gssapi_security_message()
|
|
|
|
*
|
|
|
|
* This is used to generate an already encoded GSSAPI (Kerberos V5) security
|
|
|
|
* token message ready for sending to the recipient.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* data [in] - The session handle.
|
2014-11-23 11:31:39 -05:00
|
|
|
* chlg64 [in] - The optional base64 encoded challenge message.
|
2014-08-13 19:03:45 -04:00
|
|
|
* krb5 [in/out] - The gssapi 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.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_create_gssapi_security_message(struct SessionHandle *data,
|
|
|
|
const char *chlg64,
|
|
|
|
struct kerberos5data *krb5,
|
|
|
|
char **outptr,
|
|
|
|
size_t *outlen)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t chlglen = 0;
|
|
|
|
size_t messagelen = 0;
|
|
|
|
size_t appdatalen = 0;
|
|
|
|
unsigned char *chlg = NULL;
|
|
|
|
unsigned char *trailer = NULL;
|
|
|
|
unsigned char *message = NULL;
|
|
|
|
unsigned char *padding = NULL;
|
|
|
|
unsigned char *appdata = NULL;
|
|
|
|
SecBuffer input_buf[2];
|
|
|
|
SecBuffer wrap_buf[3];
|
|
|
|
SecBufferDesc input_desc;
|
|
|
|
SecBufferDesc wrap_desc;
|
|
|
|
unsigned long indata = 0;
|
2014-08-14 18:50:31 -04:00
|
|
|
unsigned long outdata = 0;
|
2014-08-13 19:03:45 -04:00
|
|
|
unsigned long qop = 0;
|
|
|
|
unsigned long sec_layer = 0;
|
|
|
|
unsigned long max_size = 0;
|
|
|
|
SecPkgContext_Sizes sizes;
|
|
|
|
SecPkgCredentials_Names names;
|
|
|
|
SECURITY_STATUS status;
|
2014-09-10 08:32:56 -04:00
|
|
|
char *user_name;
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
/* Decode the base-64 encoded input message */
|
|
|
|
if(strlen(chlg64) && *chlg64 != '=') {
|
|
|
|
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we have a valid challenge message */
|
2014-11-16 08:27:03 -05:00
|
|
|
if(!chlg) {
|
|
|
|
infof(data, "GSSAPI handshake failure (empty security message)\n");
|
|
|
|
|
2014-08-13 19:03:45 -04:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2014-11-16 08:27:03 -05:00
|
|
|
}
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
/* Get our response size information */
|
|
|
|
status = s_pSecFn->QueryContextAttributes(krb5->context,
|
|
|
|
SECPKG_ATTR_SIZES,
|
|
|
|
&sizes);
|
|
|
|
if(status != SEC_E_OK) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(chlg);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the fully qualified username back from the context */
|
|
|
|
status = s_pSecFn->QueryCredentialsAttributes(krb5->credentials,
|
|
|
|
SECPKG_CRED_ATTR_NAMES,
|
|
|
|
&names);
|
|
|
|
if(status != SEC_E_OK) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(chlg);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the "input" security buffer */
|
|
|
|
input_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
input_desc.cBuffers = 2;
|
|
|
|
input_desc.pBuffers = input_buf;
|
|
|
|
input_buf[0].BufferType = SECBUFFER_STREAM;
|
|
|
|
input_buf[0].pvBuffer = chlg;
|
|
|
|
input_buf[0].cbBuffer = curlx_uztoul(chlglen);
|
|
|
|
input_buf[1].BufferType = SECBUFFER_DATA;
|
|
|
|
input_buf[1].pvBuffer = NULL;
|
|
|
|
input_buf[1].cbBuffer = 0;
|
|
|
|
|
2014-12-04 19:24:11 -05:00
|
|
|
/* Decrypt the inbound challenge and obtain the qop */
|
2014-08-13 19:03:45 -04:00
|
|
|
status = s_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop);
|
|
|
|
if(status != SEC_E_OK) {
|
2014-12-04 16:04:01 -05:00
|
|
|
infof(data, "GSSAPI handshake failure (empty security message)\n");
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(chlg);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
2014-08-17 17:36:22 -04:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2014-08-13 19:03:45 -04:00
|
|
|
}
|
|
|
|
|
2014-11-14 17:20:28 -05:00
|
|
|
/* Not 4 octets long so fail as per RFC4752 Section 3.1 */
|
2014-08-13 19:03:45 -04:00
|
|
|
if(input_buf[1].cbBuffer != 4) {
|
2014-12-04 16:04:01 -05:00
|
|
|
infof(data, "GSSAPI handshake failure (invalid security data)\n");
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(chlg);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
2014-08-17 17:36:22 -04:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2014-08-13 19:03:45 -04:00
|
|
|
}
|
|
|
|
|
2014-12-04 19:18:13 -05:00
|
|
|
/* Copy the data out and free the challenge as it is not required anymore */
|
2014-08-13 19:03:45 -04:00
|
|
|
memcpy(&indata, input_buf[1].pvBuffer, 4);
|
|
|
|
s_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer);
|
2015-03-16 10:01:15 -04:00
|
|
|
free(chlg);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
/* Extract the security layer */
|
|
|
|
sec_layer = indata & 0x000000FF;
|
|
|
|
if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) {
|
2014-12-04 16:04:01 -05:00
|
|
|
infof(data, "GSSAPI handshake failure (invalid security layer)\n");
|
|
|
|
|
2014-08-17 17:36:22 -04:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2014-08-13 19:03:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the maximum message size the server can receive */
|
|
|
|
max_size = ntohl(indata & 0xFFFFFF00);
|
2014-08-14 18:50:31 -04:00
|
|
|
if(max_size > 0) {
|
|
|
|
/* The server has told us it supports a maximum receive buffer, however, as
|
2014-12-04 19:24:11 -05:00
|
|
|
we don't require one unless we are encrypting data, we tell the server
|
2014-08-14 18:50:31 -04:00
|
|
|
our receive buffer is zero. */
|
|
|
|
max_size = 0;
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:03:45 -04:00
|
|
|
/* Allocate the trailer */
|
|
|
|
trailer = malloc(sizes.cbSecurityTrailer);
|
2014-12-04 19:18:13 -05:00
|
|
|
if(!trailer)
|
2014-08-13 19:03:45 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2014-09-10 08:32:56 -04:00
|
|
|
/* Convert the user name to UTF8 when operating with Unicode */
|
|
|
|
user_name = Curl_convert_tchar_to_UTF8(names.sUserName);
|
|
|
|
if(!user_name) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(trailer);
|
2014-09-10 08:32:56 -04:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:03:45 -04:00
|
|
|
/* Allocate our message */
|
2014-12-04 19:22:34 -05:00
|
|
|
messagelen = sizeof(outdata) + strlen(user_name) + 1;
|
2014-08-13 19:03:45 -04:00
|
|
|
message = malloc(messagelen);
|
|
|
|
if(!message) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(trailer);
|
2014-09-10 08:32:56 -04:00
|
|
|
Curl_unicodefree(user_name);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate the message with the security layer, client supported receive
|
2014-08-14 18:50:31 -04:00
|
|
|
message size and authorization identity including the 0x00 based
|
|
|
|
terminator. Note: Dispite RFC4752 Section 3.1 stating "The authorization
|
|
|
|
identity is not terminated with the zero-valued (%x00) octet." it seems
|
|
|
|
necessary to include it. */
|
2014-12-04 19:22:34 -05:00
|
|
|
outdata = htonl(max_size) | sec_layer;
|
|
|
|
memcpy(message, &outdata, sizeof(outdata));
|
|
|
|
strcpy((char *) message + sizeof(outdata), user_name);
|
2014-09-10 08:32:56 -04:00
|
|
|
Curl_unicodefree(user_name);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
/* Allocate the padding */
|
|
|
|
padding = malloc(sizes.cbBlockSize);
|
|
|
|
if(!padding) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(message);
|
|
|
|
free(trailer);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the "authentication data" security buffer */
|
|
|
|
wrap_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
wrap_desc.cBuffers = 3;
|
|
|
|
wrap_desc.pBuffers = wrap_buf;
|
|
|
|
wrap_buf[0].BufferType = SECBUFFER_TOKEN;
|
|
|
|
wrap_buf[0].pvBuffer = trailer;
|
|
|
|
wrap_buf[0].cbBuffer = sizes.cbSecurityTrailer;
|
|
|
|
wrap_buf[1].BufferType = SECBUFFER_DATA;
|
|
|
|
wrap_buf[1].pvBuffer = message;
|
|
|
|
wrap_buf[1].cbBuffer = curlx_uztoul(messagelen);
|
|
|
|
wrap_buf[2].BufferType = SECBUFFER_PADDING;
|
|
|
|
wrap_buf[2].pvBuffer = padding;
|
|
|
|
wrap_buf[2].cbBuffer = sizes.cbBlockSize;
|
|
|
|
|
|
|
|
/* Encrypt the data */
|
|
|
|
status = s_pSecFn->EncryptMessage(krb5->context, KERB_WRAP_NO_ENCRYPT,
|
|
|
|
&wrap_desc, 0);
|
|
|
|
if(status != SEC_E_OK) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(padding);
|
|
|
|
free(message);
|
|
|
|
free(trailer);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
2014-08-17 17:36:22 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2014-08-13 19:03:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the encryption (wrap) buffer */
|
|
|
|
appdatalen = wrap_buf[0].cbBuffer + wrap_buf[1].cbBuffer +
|
|
|
|
wrap_buf[2].cbBuffer;
|
|
|
|
appdata = malloc(appdatalen);
|
|
|
|
if(!appdata) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(padding);
|
|
|
|
free(message);
|
|
|
|
free(trailer);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate the encryption buffer */
|
|
|
|
memcpy(appdata, wrap_buf[0].pvBuffer, wrap_buf[0].cbBuffer);
|
|
|
|
offset += wrap_buf[0].cbBuffer;
|
|
|
|
memcpy(appdata + offset, wrap_buf[1].pvBuffer, wrap_buf[1].cbBuffer);
|
|
|
|
offset += wrap_buf[1].cbBuffer;
|
|
|
|
memcpy(appdata + offset, wrap_buf[2].pvBuffer, wrap_buf[2].cbBuffer);
|
|
|
|
|
|
|
|
/* Base64 encode the response */
|
|
|
|
result = Curl_base64_encode(data, (char *)appdata, appdatalen, outptr,
|
|
|
|
outlen);
|
|
|
|
|
|
|
|
/* Free all of our local buffers */
|
2015-03-16 10:01:15 -04:00
|
|
|
free(appdata);
|
|
|
|
free(padding);
|
|
|
|
free(message);
|
|
|
|
free(trailer);
|
2014-08-13 19:03:45 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-14 16:16:08 -05:00
|
|
|
/*
|
2014-12-05 02:48:16 -05:00
|
|
|
* Curl_sasl_gssapi_cleanup()
|
|
|
|
*
|
|
|
|
* This is used to clean up the gssapi specific data.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* krb5 [in/out] - The kerberos 5 data struct being cleaned up.
|
|
|
|
*
|
|
|
|
*/
|
2014-08-13 19:03:45 -04:00
|
|
|
void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5)
|
|
|
|
{
|
2014-10-15 16:48:59 -04:00
|
|
|
/* Free our security context */
|
2014-08-13 19:03:45 -04:00
|
|
|
if(krb5->context) {
|
|
|
|
s_pSecFn->DeleteSecurityContext(krb5->context);
|
|
|
|
free(krb5->context);
|
|
|
|
krb5->context = NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-15 16:48:59 -04:00
|
|
|
/* Free our credentials handle */
|
2014-08-13 19:03:45 -04:00
|
|
|
if(krb5->credentials) {
|
|
|
|
s_pSecFn->FreeCredentialsHandle(krb5->credentials);
|
|
|
|
free(krb5->credentials);
|
|
|
|
krb5->credentials = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free our identity */
|
|
|
|
Curl_sspi_free_identity(krb5->p_identity);
|
|
|
|
krb5->p_identity = NULL;
|
|
|
|
|
|
|
|
/* Free the SPN and output token */
|
|
|
|
Curl_safefree(krb5->spn);
|
|
|
|
Curl_safefree(krb5->output_token);
|
|
|
|
|
|
|
|
/* Reset any variables */
|
|
|
|
krb5->token_max = 0;
|
|
|
|
}
|
2014-11-16 04:50:06 -05:00
|
|
|
#endif /* USE_KERBEROS5 */
|
2014-08-13 19:03:45 -04:00
|
|
|
|
2014-08-09 11:26:58 -04:00
|
|
|
#endif /* USE_WINDOWS_SSPI */
|