mirror of
https://github.com/curl/curl.git
synced 2026-06-23 10:05:38 +03:00
Before this patch curl used the C preprocessor to override standard memory allocation symbols: malloc, calloc, strdup, realloc, free. The goal of these is to replace them with curl's debug wrappers in `CURLDEBUG` builds, another was to replace them with the wrappers calling user-defined allocators in libcurl. This solution needed a bunch of workarounds to avoid breaking external headers: it relied on include order to do the overriding last. For "unity" builds it needed to reset overrides before external includes. Also in test apps, which are always built as single source files. It also needed the `(symbol)` trick to avoid overrides in some places. This would still not fix cases where the standard symbols were macros. It was also fragile and difficult to figure out which was the actual function behind an alloc or free call in a specific piece of code. This in turn caused bugs where the wrong allocator was accidentally called. To avoid these problems, this patch replaces this solution with `curlx_`-prefixed allocator macros, and mapping them _once_ to either the libcurl wrappers, the debug wrappers or the standard ones, matching the rest of the code in libtests. This concludes the long journey to avoid redefining standard functions in the curl codebase. Note: I did not update `packages/OS400/*.c` sources. They did not `#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning the overrides were never applied to them. This may or may not have been correct. For now I suppressed the direct use of standard allocators via a local `.checksrc`. Probably they (except for `curlcl.c`) should be updated to include `curl_setup.h` and use the `curlx_` macros. This patch changes mappings in two places: - `lib/curl_threads.c` in libtests: Before this patch it mapped to libcurl allocators. After, it maps to standard allocators, like the rest of libtests code. - `units`: before this patch it mapped to standard allocators. After, it maps to libcurl allocators. Also: - drop all position-dependent `curl_memory.h` and `memdebug.h` includes, and delete the now unnecessary headers. - rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other allocators. - map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`). To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`. - multibyte: map `curlx_convert_*()` to `_strdup()` on Windows (was: `strdup()`). - src: do not reuse the `strdup` name for the local replacement. - lib509: call `_strdup()` on Windows (was: `strdup()`). - test1132: delete test obsoleted by this patch. - CHECKSRC.md: update text for `SNPRINTF`. - checksrc: ban standard allocator symbols. Follow-up tob12da22db1#18866 Follow-up todb98daab05#18844 Follow-up to4deea9396b#18814 Follow-up to9678ff5b1b#18776 Follow-up to10bac43b87#18774 Follow-up to20142f5d06#18634 Follow-up tobf7375ecc5#18503 Follow-up to9863599d69#18502 Follow-up to3bb5e58c10#17827 Closes #19626
465 lines
15 KiB
C
465 lines
15 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* Project ___| | | | _ \| |
|
|
* / __| | | | |_) | |
|
|
* | (__| |_| | _ <| |___
|
|
* \___|\___/|_| \_\_____|
|
|
*
|
|
* Copyright (C) Steve Holme, <steve_holme@hotmail.com>.
|
|
*
|
|
* 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 https://curl.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.
|
|
*
|
|
* SPDX-License-Identifier: curl
|
|
*
|
|
* RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
|
|
*
|
|
***************************************************************************/
|
|
|
|
#include "../curl_setup.h"
|
|
|
|
#if defined(USE_WINDOWS_SSPI) && defined(USE_KERBEROS5)
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "vauth.h"
|
|
#include "../urldata.h"
|
|
#include "../curlx/warnless.h"
|
|
#include "../curlx/multibyte.h"
|
|
#include "../sendf.h"
|
|
|
|
/*
|
|
* Curl_auth_is_gssapi_supported()
|
|
*
|
|
* This is used to evaluate if GSSAPI (Kerberos V5) is supported.
|
|
*
|
|
* Parameters: None
|
|
*
|
|
* Returns TRUE if Kerberos V5 is supported by Windows SSPI.
|
|
*/
|
|
bool Curl_auth_is_gssapi_supported(void)
|
|
{
|
|
PSecPkgInfo SecurityPackage;
|
|
SECURITY_STATUS status;
|
|
|
|
/* Query the security package for Kerberos */
|
|
status = Curl_pSecFn->QuerySecurityPackageInfo(
|
|
(TCHAR *)CURL_UNCONST(TEXT(SP_NAME_KERBEROS)),
|
|
&SecurityPackage);
|
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
if(status == SEC_E_OK) {
|
|
Curl_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
}
|
|
|
|
return status == SEC_E_OK;
|
|
}
|
|
|
|
/*
|
|
* Curl_auth_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:
|
|
*
|
|
* data [in] - The session handle.
|
|
* userp [in] - The username in the format User or Domain\User.
|
|
* passwdp [in] - The user's password.
|
|
* service [in] - The service type such as http, smtp, pop or imap.
|
|
* host [in] - The hostname.
|
|
* mutual_auth [in] - Flag specifying whether or not mutual authentication
|
|
* is enabled.
|
|
* chlg [in] - Optional challenge message.
|
|
* krb5 [in/out] - The Kerberos 5 data struct being used and modified.
|
|
* out [out] - The result storage.
|
|
*
|
|
* Returns CURLE_OK on success.
|
|
*/
|
|
CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
|
|
const char *userp,
|
|
const char *passwdp,
|
|
const char *service,
|
|
const char *host,
|
|
const bool mutual_auth,
|
|
const struct bufref *chlg,
|
|
struct kerberos5data *krb5,
|
|
struct bufref *out)
|
|
{
|
|
CURLcode result = CURLE_OK;
|
|
CtxtHandle context;
|
|
PSecPkgInfo SecurityPackage;
|
|
SecBuffer chlg_buf;
|
|
SecBuffer resp_buf;
|
|
SecBufferDesc chlg_desc;
|
|
SecBufferDesc resp_desc;
|
|
SECURITY_STATUS status;
|
|
unsigned long attrs;
|
|
|
|
if(!krb5->spn) {
|
|
/* Generate our SPN */
|
|
krb5->spn = Curl_auth_build_spn(service, host, NULL);
|
|
if(!krb5->spn)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
}
|
|
|
|
if(!krb5->output_token) {
|
|
/* Query the security package for Kerberos */
|
|
status = Curl_pSecFn->QuerySecurityPackageInfo(
|
|
(TCHAR *)CURL_UNCONST(TEXT(SP_NAME_KERBEROS)),
|
|
&SecurityPackage);
|
|
if(status != SEC_E_OK) {
|
|
failf(data, "SSPI: could not get auth info");
|
|
return CURLE_AUTH_ERROR;
|
|
}
|
|
|
|
krb5->token_max = SecurityPackage->cbMaxToken;
|
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
Curl_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
/* Allocate our response buffer */
|
|
krb5->output_token = curlx_malloc(krb5->token_max);
|
|
if(!krb5->output_token)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
}
|
|
|
|
if(!krb5->credentials) {
|
|
/* Do we have credentials to use or are we using single sign-on? */
|
|
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 = curlx_calloc(1, sizeof(CredHandle));
|
|
if(!krb5->credentials)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
/* Acquire our credentials handle */
|
|
status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
|
|
(TCHAR *)CURL_UNCONST(TEXT(SP_NAME_KERBEROS)),
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
|
krb5->p_identity, NULL, NULL,
|
|
krb5->credentials, NULL);
|
|
if(status != SEC_E_OK)
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
/* Allocate our new context handle */
|
|
krb5->context = curlx_calloc(1, sizeof(CtxtHandle));
|
|
if(!krb5->context)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
}
|
|
|
|
if(chlg) {
|
|
if(!Curl_bufref_len(chlg)) {
|
|
infof(data, "GSSAPI handshake failure (empty challenge message)");
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
}
|
|
|
|
/* 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 = CURL_UNCONST(Curl_bufref_ptr(chlg));
|
|
chlg_buf.cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
|
|
}
|
|
|
|
/* 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 = Curl_pSecFn->InitializeSecurityContext(krb5->credentials,
|
|
chlg ? krb5->context : NULL,
|
|
krb5->spn,
|
|
(mutual_auth ?
|
|
ISC_REQ_MUTUAL_AUTH : 0),
|
|
0, SECURITY_NATIVE_DREP,
|
|
chlg ? &chlg_desc : NULL, 0,
|
|
&context,
|
|
&resp_desc, &attrs, NULL);
|
|
|
|
if(status == SEC_E_INSUFFICIENT_MEMORY)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED)
|
|
return CURLE_AUTH_ERROR;
|
|
|
|
if(memcmp(&context, krb5->context, sizeof(context))) {
|
|
Curl_pSecFn->DeleteSecurityContext(krb5->context);
|
|
|
|
memcpy(krb5->context, &context, sizeof(context));
|
|
}
|
|
|
|
if(resp_buf.cbBuffer) {
|
|
result = Curl_bufref_memdup(out, resp_buf.pvBuffer, resp_buf.cbBuffer);
|
|
}
|
|
else if(mutual_auth)
|
|
Curl_bufref_set(out, "", 0, NULL);
|
|
else
|
|
Curl_bufref_set(out, NULL, 0, NULL);
|
|
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* Curl_auth_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.
|
|
* authzid [in] - The authorization identity if some.
|
|
* chlg [in] - The challenge message.
|
|
* krb5 [in/out] - The Kerberos 5 data struct being used and modified.
|
|
* out [out] - The result storage.
|
|
*
|
|
* Returns CURLE_OK on success.
|
|
*/
|
|
CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
|
|
const char *authzid,
|
|
const struct bufref *chlg,
|
|
struct kerberos5data *krb5,
|
|
struct bufref *out)
|
|
{
|
|
CURLcode result = CURLE_OK;
|
|
size_t offset = 0;
|
|
size_t messagelen = 0;
|
|
size_t appdatalen = 0;
|
|
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 char *indata;
|
|
unsigned long qop = 0;
|
|
unsigned long sec_layer = 0;
|
|
unsigned long max_size = 0;
|
|
SecPkgContext_Sizes sizes;
|
|
SECURITY_STATUS status;
|
|
|
|
#ifdef CURL_DISABLE_VERBOSE_STRINGS
|
|
(void)data;
|
|
#endif
|
|
|
|
/* Ensure we have a valid challenge message */
|
|
DEBUGASSERT(chlg);
|
|
if(!Curl_bufref_len(chlg)) {
|
|
infof(data, "GSSAPI handshake failure (empty security message)");
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
}
|
|
|
|
/* Get our response size information */
|
|
status = Curl_pSecFn->QueryContextAttributes(krb5->context,
|
|
SECPKG_ATTR_SIZES,
|
|
&sizes);
|
|
|
|
if(status == SEC_E_INSUFFICIENT_MEMORY)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
if(status != SEC_E_OK)
|
|
return CURLE_AUTH_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 = CURL_UNCONST(Curl_bufref_ptr(chlg));
|
|
input_buf[0].cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
|
|
input_buf[1].BufferType = SECBUFFER_DATA;
|
|
input_buf[1].pvBuffer = NULL;
|
|
input_buf[1].cbBuffer = 0;
|
|
|
|
/* Decrypt the inbound challenge and obtain the qop */
|
|
status = Curl_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop);
|
|
if(status != SEC_E_OK) {
|
|
infof(data, "GSSAPI handshake failure (empty security message)");
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
}
|
|
|
|
/* Not 4 octets long so fail as per RFC4752 Section 3.1 */
|
|
if(input_buf[1].cbBuffer != 4) {
|
|
infof(data, "GSSAPI handshake failure (invalid security data)");
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
}
|
|
|
|
/* Extract the security layer and the maximum message size */
|
|
indata = input_buf[1].pvBuffer;
|
|
sec_layer = indata[0];
|
|
max_size = ((unsigned long)indata[1] << 16) |
|
|
((unsigned long)indata[2] << 8) | indata[3];
|
|
|
|
/* Free the challenge as it is not required anymore */
|
|
Curl_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer);
|
|
|
|
/* Process the security layer */
|
|
if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) {
|
|
infof(data, "GSSAPI handshake failure (invalid security layer)");
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
}
|
|
sec_layer &= KERB_WRAP_NO_ENCRYPT; /* We do not support a security layer */
|
|
|
|
/* Process the maximum message size the server can receive */
|
|
if(max_size > 0) {
|
|
/* The server has told us it supports a maximum receive buffer, however, as
|
|
we do not require one unless we are encrypting data, we tell the server
|
|
our receive buffer is zero. */
|
|
max_size = 0;
|
|
}
|
|
|
|
/* Allocate the trailer */
|
|
trailer = curlx_malloc(sizes.cbSecurityTrailer);
|
|
if(!trailer)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
/* Allocate our message */
|
|
messagelen = 4;
|
|
if(authzid)
|
|
messagelen += strlen(authzid);
|
|
message = curlx_malloc(messagelen);
|
|
if(!message) {
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
goto out;
|
|
}
|
|
|
|
/* Populate the message with the security layer and client supported receive
|
|
message size. */
|
|
message[0] = sec_layer & 0xFF;
|
|
message[1] = (max_size >> 16) & 0xFF;
|
|
message[2] = (max_size >> 8) & 0xFF;
|
|
message[3] = max_size & 0xFF;
|
|
|
|
/* If given, append the authorization identity. */
|
|
|
|
if(authzid && *authzid)
|
|
memcpy(message + 4, authzid, messagelen - 4);
|
|
|
|
/* Allocate the padding */
|
|
padding = curlx_malloc(sizes.cbBlockSize);
|
|
if(!padding) {
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
goto out;
|
|
}
|
|
|
|
/* 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 = Curl_pSecFn->EncryptMessage(krb5->context, KERB_WRAP_NO_ENCRYPT,
|
|
&wrap_desc, 0);
|
|
if(status != SEC_E_OK) {
|
|
if(status == SEC_E_INSUFFICIENT_MEMORY)
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
else
|
|
result = CURLE_AUTH_ERROR;
|
|
goto out;
|
|
}
|
|
|
|
/* Allocate the encryption (wrap) buffer */
|
|
appdatalen = wrap_buf[0].cbBuffer + wrap_buf[1].cbBuffer +
|
|
wrap_buf[2].cbBuffer;
|
|
appdata = curlx_malloc(appdatalen);
|
|
if(!appdata) {
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
goto out;
|
|
}
|
|
|
|
/* 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);
|
|
|
|
out:
|
|
/* Free all of our local buffers */
|
|
curlx_free(padding);
|
|
curlx_free(message);
|
|
curlx_free(trailer);
|
|
|
|
if(result)
|
|
return result;
|
|
|
|
/* Return the response. */
|
|
Curl_bufref_set(out, appdata, appdatalen, curl_free);
|
|
return CURLE_OK;
|
|
}
|
|
|
|
/*
|
|
* Curl_auth_cleanup_gssapi()
|
|
*
|
|
* This is used to clean up the GSSAPI (Kerberos V5) specific data.
|
|
*
|
|
* Parameters:
|
|
*
|
|
* krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
|
|
*
|
|
*/
|
|
void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
|
|
{
|
|
/* Free our security context */
|
|
if(krb5->context) {
|
|
Curl_pSecFn->DeleteSecurityContext(krb5->context);
|
|
curlx_free(krb5->context);
|
|
krb5->context = NULL;
|
|
}
|
|
|
|
/* Free our credentials handle */
|
|
if(krb5->credentials) {
|
|
Curl_pSecFn->FreeCredentialsHandle(krb5->credentials);
|
|
curlx_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;
|
|
}
|
|
|
|
#endif /* USE_WINDOWS_SSPI && USE_KERBEROS5 */
|