2014-04-02 16:21:12 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* RFC2831 DIGEST-MD5 authentication
|
|
|
|
* RFC4422 Simple Authentication and Security Layer (SASL)
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#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-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
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.
|
|
|
|
* instance [in] - The instance name such as the host nme or realm.
|
|
|
|
*
|
|
|
|
* 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) {
|
|
|
|
Curl_safefree(utf8_spn);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the UTF8 variant when operating with Unicode */
|
|
|
|
if(utf8_spn != tchar_spn)
|
|
|
|
Curl_safefree(utf8_spn);
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
* chlg64 [in] - Pointer to the base64 encoded challenge message.
|
|
|
|
* userp [in] - The user name.
|
|
|
|
* 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-08-10 06:01:27 -04:00
|
|
|
size_t resp_max = 0;
|
2014-04-06 07:08:11 -04:00
|
|
|
unsigned char *chlg = NULL;
|
2014-08-10 06:01:27 -04:00
|
|
|
unsigned char *resp = NULL;
|
2014-04-06 07:08:11 -04:00
|
|
|
CredHandle handle;
|
|
|
|
CtxtHandle ctx;
|
|
|
|
PSecPkgInfo SecurityPackage;
|
|
|
|
SEC_WINNT_AUTH_IDENTITY identity;
|
|
|
|
SecBuffer chlg_buf;
|
|
|
|
SecBuffer resp_buf;
|
|
|
|
SecBufferDesc chlg_desc;
|
|
|
|
SecBufferDesc resp_desc;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
unsigned long attrs;
|
|
|
|
TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
if(!chlg)
|
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
|
|
|
|
|
|
/* Ensure we have some login credientials as DigestSSP cannot use the current
|
|
|
|
Windows user like NTLMSSP can */
|
2014-04-06 09:10:18 -04:00
|
|
|
if(!userp || !*userp) {
|
|
|
|
Curl_safefree(chlg);
|
2014-04-06 07:08:11 -04:00
|
|
|
return CURLE_LOGIN_DENIED;
|
2014-04-06 09:10:18 -04:00
|
|
|
}
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
/* Query the security package for DigestSSP */
|
|
|
|
status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT("WDigest"),
|
|
|
|
&SecurityPackage);
|
2014-04-06 09:10:18 -04:00
|
|
|
if(status != SEC_E_OK) {
|
|
|
|
Curl_safefree(chlg);
|
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-08-10 06:01:27 -04:00
|
|
|
resp_max = SecurityPackage->cbMaxToken;
|
|
|
|
|
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 */
|
|
|
|
resp = malloc(resp_max);
|
|
|
|
if(!resp) {
|
|
|
|
Curl_safefree(chlg);
|
|
|
|
|
|
|
|
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) {
|
2014-08-10 06:01:27 -04:00
|
|
|
Curl_safefree(resp);
|
2014-08-10 05:33:10 -04:00
|
|
|
Curl_safefree(chlg);
|
|
|
|
|
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
|
|
|
|
|
|
|
/* Populate our identity structure */
|
|
|
|
result = Curl_create_sspi_identity(userp, passwdp, &identity);
|
|
|
|
if(result) {
|
|
|
|
Curl_safefree(spn);
|
2014-08-10 06:01:27 -04:00
|
|
|
Curl_safefree(resp);
|
2014-04-06 09:10:18 -04:00
|
|
|
Curl_safefree(chlg);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Acquire our credientials handle */
|
|
|
|
status = s_pSecFn->AcquireCredentialsHandle(NULL,
|
|
|
|
(TCHAR *) TEXT("WDigest"),
|
|
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
2014-04-06 08:29:29 -04:00
|
|
|
&identity, NULL, NULL,
|
2014-04-06 07:08:11 -04:00
|
|
|
&handle, &tsDummy);
|
|
|
|
|
|
|
|
if(status != SEC_E_OK) {
|
2014-04-06 08:29:29 -04:00
|
|
|
Curl_sspi_free_identity(&identity);
|
2014-04-06 07:08:11 -04:00
|
|
|
Curl_safefree(spn);
|
2014-08-10 06:01:27 -04:00
|
|
|
Curl_safefree(resp);
|
2014-04-06 09:10:18 -04:00
|
|
|
Curl_safefree(chlg);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
chlg_buf.pvBuffer = chlg;
|
|
|
|
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;
|
|
|
|
resp_buf.pvBuffer = resp;
|
2014-08-10 06:01:27 -04:00
|
|
|
resp_buf.cbBuffer = curlx_uztoul(resp_max);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
2014-04-06 08:29:29 -04:00
|
|
|
/* Generate our challenge-response message */
|
2014-08-09 12:04:10 -04:00
|
|
|
status = s_pSecFn->InitializeSecurityContext(&handle, NULL, spn, 0, 0, 0,
|
|
|
|
&chlg_desc, 0, &ctx,
|
|
|
|
&resp_desc, &attrs, &tsDummy);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
if(status == SEC_I_COMPLETE_AND_CONTINUE ||
|
|
|
|
status == SEC_I_CONTINUE_NEEDED)
|
|
|
|
s_pSecFn->CompleteAuthToken(&handle, &resp_desc);
|
|
|
|
else if(status != SEC_E_OK) {
|
|
|
|
s_pSecFn->FreeCredentialsHandle(&handle);
|
2014-04-06 08:29:29 -04:00
|
|
|
Curl_sspi_free_identity(&identity);
|
2014-04-06 07:08:11 -04:00
|
|
|
Curl_safefree(spn);
|
2014-08-10 06:01:27 -04:00
|
|
|
Curl_safefree(resp);
|
2014-04-06 09:10:18 -04:00
|
|
|
Curl_safefree(chlg);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Base64 encode the response */
|
|
|
|
result = Curl_base64_encode(data, (char *)resp, resp_buf.cbBuffer, outptr,
|
|
|
|
outlen);
|
|
|
|
|
|
|
|
/* Free our handles */
|
|
|
|
s_pSecFn->DeleteSecurityContext(&ctx);
|
|
|
|
s_pSecFn->FreeCredentialsHandle(&handle);
|
|
|
|
|
|
|
|
/* Free the identity structure */
|
2014-04-06 08:29:29 -04:00
|
|
|
Curl_sspi_free_identity(&identity);
|
2014-04-06 07:08:11 -04:00
|
|
|
|
|
|
|
/* Free the SPN */
|
|
|
|
Curl_safefree(spn);
|
|
|
|
|
2014-08-10 06:01:27 -04:00
|
|
|
/* Free the response buffer */
|
|
|
|
Curl_safefree(resp);
|
|
|
|
|
2014-04-06 09:10:18 -04:00
|
|
|
/* Free the decoeded challenge message */
|
|
|
|
Curl_safefree(chlg);
|
|
|
|
|
2014-04-06 07:08:11 -04:00
|
|
|
return result;
|
2014-04-02 16:21:12 -04:00
|
|
|
}
|
|
|
|
|
2014-08-09 11:26:58 -04:00
|
|
|
#endif /* !CURL_DISABLE_CRYPTO_AUTH */
|
|
|
|
|
|
|
|
#endif /* USE_WINDOWS_SSPI */
|