2009-01-29 05:33:58 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
|
|
* Copyright (C) Markus Moeller, <markus_moeller@compuserve.com>
|
2009-01-29 05:33:58 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2009-01-29 05:33:58 +08: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.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2009-01-29 05:33:58 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2009-01-29 05:33:58 +08:00
|
|
|
|
2012-09-07 02:51:30 +08:00
|
|
|
#if defined(USE_WINDOWS_SSPI) && !defined(CURL_DISABLE_PROXY)
|
2009-01-29 05:33:58 +08:00
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
2023-01-30 23:03:00 +08:00
|
|
|
#include "cfilters.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "connect.h"
|
|
|
|
#include "strerror.h"
|
|
|
|
#include "timeval.h"
|
|
|
|
#include "socks.h"
|
2009-01-30 04:32:27 +08:00
|
|
|
#include "curl_sspi.h"
|
2012-06-16 00:05:11 +08:00
|
|
|
#include "curl_multibyte.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "warnless.h"
|
2016-07-14 00:43:18 +08:00
|
|
|
#include "strdup.h"
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 19:36:18 +08:00
|
|
|
#include "curl_printf.h"
|
2010-01-24 01:31:54 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2009-01-29 05:33:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper sspi error functions.
|
|
|
|
*/
|
2021-01-18 18:56:50 +08:00
|
|
|
static int check_sspi_err(struct Curl_easy *data,
|
2012-06-12 14:50:10 +08:00
|
|
|
SECURITY_STATUS status,
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *function)
|
2009-01-29 05:33:58 +08:00
|
|
|
{
|
2012-06-12 14:50:10 +08:00
|
|
|
if(status != SEC_E_OK &&
|
|
|
|
status != SEC_I_COMPLETE_AND_CONTINUE &&
|
|
|
|
status != SEC_I_COMPLETE_NEEDED &&
|
|
|
|
status != SEC_I_CONTINUE_NEEDED) {
|
2019-02-26 01:12:51 +08:00
|
|
|
char buffer[STRERROR_LEN];
|
2021-01-18 18:56:50 +08:00
|
|
|
failf(data, "SSPI error: %s failed: %s", function,
|
2019-02-26 01:12:51 +08:00
|
|
|
Curl_sspi_strerror(status, buffer, sizeof(buffer)));
|
2009-01-29 11:39:10 +08:00
|
|
|
return 1;
|
2009-01-29 05:33:58 +08:00
|
|
|
}
|
2009-01-29 11:39:10 +08:00
|
|
|
return 0;
|
2009-01-29 05:33:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the SSPI-using version of this function */
|
2023-01-30 23:03:00 +08:00
|
|
|
CURLcode Curl_SOCKS5_gssapi_negotiate(struct Curl_cfilter *cf,
|
2021-01-18 18:56:50 +08:00
|
|
|
struct Curl_easy *data)
|
2009-01-29 05:33:58 +08:00
|
|
|
{
|
2023-01-30 23:03:00 +08:00
|
|
|
struct connectdata *conn = cf->conn;
|
|
|
|
curl_socket_t sock = conn->sock[cf->sockindex];
|
2009-01-29 05:33:58 +08:00
|
|
|
CURLcode code;
|
|
|
|
ssize_t actualread;
|
|
|
|
ssize_t written;
|
|
|
|
int result;
|
2014-07-21 15:53:47 +08:00
|
|
|
/* Needs GSS-API authentication */
|
2012-06-12 18:34:52 +08:00
|
|
|
SECURITY_STATUS status;
|
2012-06-12 14:50:10 +08:00
|
|
|
unsigned long sspi_ret_flags = 0;
|
2016-03-31 16:22:42 +08:00
|
|
|
unsigned char gss_enc;
|
2009-01-29 05:33:58 +08:00
|
|
|
SecBuffer sspi_send_token, sspi_recv_token, sspi_w_token[3];
|
|
|
|
SecBufferDesc input_desc, output_desc, wrap_desc;
|
|
|
|
SecPkgContext_Sizes sspi_sizes;
|
|
|
|
CredHandle cred_handle;
|
|
|
|
CtxtHandle sspi_context;
|
|
|
|
PCtxtHandle context_handle = NULL;
|
|
|
|
SecPkgCredentials_Names names;
|
|
|
|
TimeStamp expiry;
|
2012-06-12 14:50:10 +08:00
|
|
|
char *service_name = NULL;
|
2011-04-06 13:30:09 +08:00
|
|
|
unsigned short us_length;
|
2012-06-17 01:20:50 +08:00
|
|
|
unsigned long qop;
|
2014-07-21 15:53:47 +08:00
|
|
|
unsigned char socksreq[4]; /* room for GSS-API exchange header only */
|
2016-04-10 03:47:05 +08:00
|
|
|
const char *service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
|
2021-01-18 18:56:50 +08:00
|
|
|
data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd";
|
2016-11-17 01:49:15 +08:00
|
|
|
const size_t service_length = strlen(service);
|
2009-01-29 05:33:58 +08:00
|
|
|
|
2014-07-21 15:53:47 +08:00
|
|
|
/* GSS-API request looks like
|
2009-01-29 05:33:58 +08:00
|
|
|
* +----+------+-----+----------------+
|
|
|
|
* |VER | MTYP | LEN | TOKEN |
|
|
|
|
* +----+------+----------------------+
|
|
|
|
* | 1 | 1 | 2 | up to 2^16 - 1 |
|
|
|
|
* +----+------+-----+----------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* prepare service name */
|
2011-04-20 21:17:42 +08:00
|
|
|
if(strchr(service, '/')) {
|
2016-07-14 00:43:18 +08:00
|
|
|
service_name = strdup(service);
|
2009-01-29 05:33:58 +08:00
|
|
|
if(!service_name)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
2016-11-17 01:49:15 +08:00
|
|
|
service_name = malloc(service_length +
|
|
|
|
strlen(conn->socks_proxy.host.name) + 2);
|
2009-01-29 05:33:58 +08:00
|
|
|
if(!service_name)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2018-11-22 16:01:24 +08:00
|
|
|
msnprintf(service_name, service_length +
|
|
|
|
strlen(conn->socks_proxy.host.name) + 2, "%s/%s",
|
|
|
|
service, conn->socks_proxy.host.name);
|
2009-01-29 05:33:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
input_desc.cBuffers = 1;
|
|
|
|
input_desc.pBuffers = &sspi_recv_token;
|
|
|
|
input_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
|
|
|
|
sspi_recv_token.BufferType = SECBUFFER_TOKEN;
|
|
|
|
sspi_recv_token.cbBuffer = 0;
|
|
|
|
sspi_recv_token.pvBuffer = NULL;
|
|
|
|
|
|
|
|
output_desc.cBuffers = 1;
|
|
|
|
output_desc.pBuffers = &sspi_send_token;
|
|
|
|
output_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
|
|
|
|
sspi_send_token.BufferType = SECBUFFER_TOKEN;
|
|
|
|
sspi_send_token.cbBuffer = 0;
|
|
|
|
sspi_send_token.pvBuffer = NULL;
|
|
|
|
|
|
|
|
wrap_desc.cBuffers = 3;
|
|
|
|
wrap_desc.pBuffers = sspi_w_token;
|
|
|
|
wrap_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
|
|
|
|
cred_handle.dwLower = 0;
|
|
|
|
cred_handle.dwUpper = 0;
|
|
|
|
|
2012-06-16 00:05:11 +08:00
|
|
|
status = s_pSecFn->AcquireCredentialsHandle(NULL,
|
2012-06-17 01:20:50 +08:00
|
|
|
(TCHAR *) TEXT("Kerberos"),
|
2012-06-16 00:05:11 +08:00
|
|
|
SECPKG_CRED_OUTBOUND,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&cred_handle,
|
|
|
|
&expiry);
|
|
|
|
|
2021-01-18 18:56:50 +08:00
|
|
|
if(check_sspi_err(data, status, "AcquireCredentialsHandle")) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to acquire credentials.");
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2020-02-14 23:16:54 +08:00
|
|
|
(void)curlx_nonblock(sock, FALSE);
|
|
|
|
|
2009-01-29 05:33:58 +08:00
|
|
|
/* As long as we need to keep sending some context info, and there's no */
|
|
|
|
/* errors, keep sending it... */
|
|
|
|
for(;;) {
|
2012-06-17 01:20:50 +08:00
|
|
|
TCHAR *sname;
|
2012-07-06 04:16:15 +08:00
|
|
|
|
2019-04-14 04:55:51 +08:00
|
|
|
sname = curlx_convert_UTF8_to_tchar(service_name);
|
2012-06-16 00:05:11 +08:00
|
|
|
if(!sname)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2012-07-06 04:16:15 +08:00
|
|
|
|
2012-06-16 00:05:11 +08:00
|
|
|
status = s_pSecFn->InitializeSecurityContext(&cred_handle,
|
|
|
|
context_handle,
|
|
|
|
sname,
|
|
|
|
ISC_REQ_MUTUAL_AUTH |
|
|
|
|
ISC_REQ_ALLOCATE_MEMORY |
|
|
|
|
ISC_REQ_CONFIDENTIALITY |
|
|
|
|
ISC_REQ_REPLAY_DETECT,
|
|
|
|
0,
|
|
|
|
SECURITY_NATIVE_DREP,
|
|
|
|
&input_desc,
|
|
|
|
0,
|
|
|
|
&sspi_context,
|
|
|
|
&output_desc,
|
|
|
|
&sspi_ret_flags,
|
|
|
|
&expiry);
|
|
|
|
|
2019-04-14 04:55:51 +08:00
|
|
|
curlx_unicodefree(sname);
|
2009-01-29 05:33:58 +08:00
|
|
|
|
|
|
|
if(sspi_recv_token.pvBuffer) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
|
2009-01-29 05:33:58 +08:00
|
|
|
sspi_recv_token.pvBuffer = NULL;
|
|
|
|
sspi_recv_token.cbBuffer = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-18 18:56:50 +08:00
|
|
|
if(check_sspi_err(data, status, "InitializeSecurityContext")) {
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_recv_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to initialise security context.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:46:11 +08:00
|
|
|
if(sspi_send_token.cbBuffer) {
|
2014-07-21 15:53:47 +08:00
|
|
|
socksreq[0] = 1; /* GSS-API subnegotiation version */
|
2009-01-29 05:33:58 +08:00
|
|
|
socksreq[1] = 1; /* authentication message type */
|
|
|
|
us_length = htons((short)sspi_send_token.cbBuffer);
|
2017-09-10 05:55:08 +08:00
|
|
|
memcpy(socksreq + 2, &us_length, sizeof(short));
|
2009-01-29 05:33:58 +08:00
|
|
|
|
2023-01-30 23:03:00 +08:00
|
|
|
written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 4, &code);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(code || (4 != written)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to send SSPI authentication request.");
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_send_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
|
|
|
|
if(sspi_recv_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2023-01-30 23:03:00 +08:00
|
|
|
written = Curl_conn_cf_send(cf->next, data,
|
|
|
|
(char *)sspi_send_token.pvBuffer,
|
|
|
|
sspi_send_token.cbBuffer, &code);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to send SSPI authentication token.");
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_send_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
|
|
|
|
if(sspi_recv_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_send_token.pvBuffer) {
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
|
|
|
|
sspi_send_token.pvBuffer = NULL;
|
|
|
|
}
|
2009-01-29 05:33:58 +08:00
|
|
|
sspi_send_token.cbBuffer = 0;
|
2014-04-19 05:24:41 +08:00
|
|
|
|
|
|
|
if(sspi_recv_token.pvBuffer) {
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
|
|
|
|
sspi_recv_token.pvBuffer = NULL;
|
|
|
|
}
|
2009-01-29 05:33:58 +08:00
|
|
|
sspi_recv_token.cbBuffer = 0;
|
2014-04-19 05:24:41 +08:00
|
|
|
|
2012-06-12 18:34:52 +08:00
|
|
|
if(status != SEC_I_CONTINUE_NEEDED)
|
|
|
|
break;
|
2009-01-29 05:33:58 +08:00
|
|
|
|
|
|
|
/* analyse response */
|
|
|
|
|
2014-07-21 15:53:47 +08:00
|
|
|
/* GSS-API response looks like
|
2009-01-29 05:33:58 +08:00
|
|
|
* +----+------+-----+----------------+
|
|
|
|
* |VER | MTYP | LEN | TOKEN |
|
|
|
|
* +----+------+----------------------+
|
|
|
|
* | 1 | 1 | 2 | up to 2^16 - 1 |
|
|
|
|
* +----+------+-----+----------------+
|
|
|
|
*/
|
|
|
|
|
2023-01-30 23:03:00 +08:00
|
|
|
result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(result || (actualread != 4)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to receive SSPI authentication response.");
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore the first (VER) byte */
|
|
|
|
if(socksreq[1] == 255) { /* status / message type */
|
2013-07-24 22:43:13 +08:00
|
|
|
failf(data, "User was rejected by the SOCKS5 server (%u %u).",
|
|
|
|
(unsigned int)socksreq[0], (unsigned int)socksreq[1]);
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2021-10-23 02:59:11 +08:00
|
|
|
if(socksreq[1] != 1) { /* status / message type */
|
2013-07-24 22:43:13 +08:00
|
|
|
failf(data, "Invalid SSPI authentication response type (%u %u).",
|
|
|
|
(unsigned int)socksreq[0], (unsigned int)socksreq[1]);
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2017-09-10 05:55:08 +08:00
|
|
|
memcpy(&us_length, socksreq + 2, sizeof(short));
|
2009-01-29 05:33:58 +08:00
|
|
|
us_length = ntohs(us_length);
|
|
|
|
|
|
|
|
sspi_recv_token.cbBuffer = us_length;
|
|
|
|
sspi_recv_token.pvBuffer = malloc(us_length);
|
|
|
|
|
|
|
|
if(!sspi_recv_token.pvBuffer) {
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2023-01-30 23:03:00 +08:00
|
|
|
result = Curl_blockread_all(cf, data, (char *)sspi_recv_token.pvBuffer,
|
2011-08-08 17:10:17 +08:00
|
|
|
sspi_recv_token.cbBuffer, &actualread);
|
2009-01-29 05:33:58 +08:00
|
|
|
|
2014-10-24 04:56:35 +08:00
|
|
|
if(result || (actualread != us_length)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to receive SSPI authentication token.");
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_recv_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
context_handle = &sspi_context;
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:01:15 +08:00
|
|
|
free(service_name);
|
2009-01-29 05:33:58 +08:00
|
|
|
|
|
|
|
/* Everything is good so far, user was authenticated! */
|
2012-06-12 18:34:52 +08:00
|
|
|
status = s_pSecFn->QueryCredentialsAttributes(&cred_handle,
|
|
|
|
SECPKG_CRED_ATTR_NAMES,
|
|
|
|
&names);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred_handle);
|
2021-01-18 18:56:50 +08:00
|
|
|
if(check_sspi_err(data, status, "QueryCredentialAttributes")) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
|
|
|
s_pSecFn->FreeContextBuffer(names.sUserName);
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to determine user name.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2023-12-08 21:05:09 +08:00
|
|
|
else {
|
|
|
|
#ifndef CURL_DISABLE_VERBOSE_STRINGS
|
|
|
|
char *user_utf8 = curlx_convert_tchar_to_UTF8(names.sUserName);
|
|
|
|
infof(data, "SOCKS5 server authenticated user %s with GSS-API.",
|
|
|
|
(user_utf8 ? user_utf8 : "(unknown)"));
|
|
|
|
curlx_unicodefree(user_utf8);
|
|
|
|
#endif
|
|
|
|
s_pSecFn->FreeContextBuffer(names.sUserName);
|
|
|
|
}
|
2009-01-29 05:33:58 +08:00
|
|
|
|
|
|
|
/* Do encryption */
|
2014-07-21 15:53:47 +08:00
|
|
|
socksreq[0] = 1; /* GSS-API subnegotiation version */
|
2009-01-29 05:33:58 +08:00
|
|
|
socksreq[1] = 2; /* encryption message type */
|
|
|
|
|
|
|
|
gss_enc = 0; /* no data protection */
|
|
|
|
/* do confidentiality protection if supported */
|
|
|
|
if(sspi_ret_flags & ISC_REQ_CONFIDENTIALITY)
|
|
|
|
gss_enc = 2;
|
|
|
|
/* else do integrity protection */
|
|
|
|
else if(sspi_ret_flags & ISC_REQ_INTEGRITY)
|
|
|
|
gss_enc = 1;
|
|
|
|
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "SOCKS5 server supports GSS-API %s data protection.",
|
2017-09-10 05:09:06 +08:00
|
|
|
(gss_enc == 0)?"no":((gss_enc == 1)?"integrity":"confidentiality") );
|
2009-01-29 05:33:58 +08:00
|
|
|
/* force to no data protection, avoid encryption/decryption for now */
|
|
|
|
gss_enc = 0;
|
|
|
|
/*
|
|
|
|
* Sending the encryption type in clear seems wrong. It should be
|
|
|
|
* protected with gss_seal()/gss_wrap(). See RFC1961 extract below
|
|
|
|
* The NEC reference implementations on which this is based is
|
|
|
|
* therefore at fault
|
|
|
|
*
|
|
|
|
* +------+------+------+.......................+
|
|
|
|
* + ver | mtyp | len | token |
|
|
|
|
* +------+------+------+.......................+
|
|
|
|
* + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
|
|
|
|
* +------+------+------+.......................+
|
|
|
|
*
|
|
|
|
* Where:
|
|
|
|
*
|
|
|
|
* - "ver" is the protocol version number, here 1 to represent the
|
|
|
|
* first version of the SOCKS/GSS-API protocol
|
|
|
|
*
|
|
|
|
* - "mtyp" is the message type, here 2 to represent a protection
|
|
|
|
* -level negotiation message
|
|
|
|
*
|
|
|
|
* - "len" is the length of the "token" field in octets
|
|
|
|
*
|
|
|
|
* - "token" is the GSS-API encapsulated protection level
|
|
|
|
*
|
|
|
|
* The token is produced by encapsulating an octet containing the
|
|
|
|
* required protection level using gss_seal()/gss_wrap() with conf_req
|
|
|
|
* set to FALSE. The token is verified using gss_unseal()/
|
|
|
|
* gss_unwrap().
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(data->set.socks5_gssapi_nec) {
|
|
|
|
us_length = htons((short)1);
|
2017-09-10 05:55:08 +08:00
|
|
|
memcpy(socksreq + 2, &us_length, sizeof(short));
|
2009-01-29 05:33:58 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-06-16 00:05:11 +08:00
|
|
|
status = s_pSecFn->QueryContextAttributes(&sspi_context,
|
|
|
|
SECPKG_ATTR_SIZES,
|
|
|
|
&sspi_sizes);
|
2021-01-18 18:56:50 +08:00
|
|
|
if(check_sspi_err(data, status, "QueryContextAttributes")) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to query security context attributes.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
sspi_w_token[0].cbBuffer = sspi_sizes.cbSecurityTrailer;
|
|
|
|
sspi_w_token[0].BufferType = SECBUFFER_TOKEN;
|
|
|
|
sspi_w_token[0].pvBuffer = malloc(sspi_sizes.cbSecurityTrailer);
|
|
|
|
|
|
|
|
if(!sspi_w_token[0].pvBuffer) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
sspi_w_token[1].cbBuffer = 1;
|
|
|
|
sspi_w_token[1].pvBuffer = malloc(1);
|
2011-07-05 04:10:32 +08:00
|
|
|
if(!sspi_w_token[1].pvBuffer) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2015-03-17 20:41:49 +08:00
|
|
|
memcpy(sspi_w_token[1].pvBuffer, &gss_enc, 1);
|
2009-01-29 05:33:58 +08:00
|
|
|
sspi_w_token[2].BufferType = SECBUFFER_PADDING;
|
|
|
|
sspi_w_token[2].cbBuffer = sspi_sizes.cbBlockSize;
|
|
|
|
sspi_w_token[2].pvBuffer = malloc(sspi_sizes.cbBlockSize);
|
|
|
|
if(!sspi_w_token[2].pvBuffer) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-06-12 18:34:52 +08:00
|
|
|
status = s_pSecFn->EncryptMessage(&sspi_context,
|
|
|
|
KERB_WRAP_NO_ENCRYPT,
|
|
|
|
&wrap_desc,
|
|
|
|
0);
|
2021-01-18 18:56:50 +08:00
|
|
|
if(check_sspi_err(data, status, "EncryptMessage")) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to query security context attributes.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
sspi_send_token.cbBuffer = sspi_w_token[0].cbBuffer
|
|
|
|
+ sspi_w_token[1].cbBuffer
|
|
|
|
+ sspi_w_token[2].cbBuffer;
|
|
|
|
sspi_send_token.pvBuffer = malloc(sspi_send_token.cbBuffer);
|
|
|
|
if(!sspi_send_token.pvBuffer) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(sspi_send_token.pvBuffer, sspi_w_token[0].pvBuffer,
|
|
|
|
sspi_w_token[0].cbBuffer);
|
|
|
|
memcpy((PUCHAR) sspi_send_token.pvBuffer +(int)sspi_w_token[0].cbBuffer,
|
|
|
|
sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
|
|
|
|
memcpy((PUCHAR) sspi_send_token.pvBuffer
|
2017-09-10 05:55:08 +08:00
|
|
|
+ sspi_w_token[0].cbBuffer
|
|
|
|
+ sspi_w_token[1].cbBuffer,
|
2009-01-29 05:33:58 +08:00
|
|
|
sspi_w_token[2].pvBuffer, sspi_w_token[2].cbBuffer);
|
|
|
|
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
2009-01-29 05:33:58 +08:00
|
|
|
sspi_w_token[0].pvBuffer = NULL;
|
|
|
|
sspi_w_token[0].cbBuffer = 0;
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
|
2009-01-29 05:33:58 +08:00
|
|
|
sspi_w_token[1].pvBuffer = NULL;
|
|
|
|
sspi_w_token[1].cbBuffer = 0;
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
|
2009-01-29 05:33:58 +08:00
|
|
|
sspi_w_token[2].pvBuffer = NULL;
|
|
|
|
sspi_w_token[2].cbBuffer = 0;
|
|
|
|
|
|
|
|
us_length = htons((short)sspi_send_token.cbBuffer);
|
2017-09-10 05:55:08 +08:00
|
|
|
memcpy(socksreq + 2, &us_length, sizeof(short));
|
2009-01-29 05:33:58 +08:00
|
|
|
}
|
|
|
|
|
2023-01-30 23:03:00 +08:00
|
|
|
written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 4, &code);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(code || (4 != written)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to send SSPI encryption request.");
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_send_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(data->set.socks5_gssapi_nec) {
|
2015-03-17 20:41:49 +08:00
|
|
|
memcpy(socksreq, &gss_enc, 1);
|
2023-01-30 23:03:00 +08:00
|
|
|
written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 1, &code);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(code || (1 != written)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to send SSPI encryption type.");
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
|
|
|
else {
|
2023-01-30 23:03:00 +08:00
|
|
|
written = Curl_conn_cf_send(cf->next, data,
|
|
|
|
(char *)sspi_send_token.pvBuffer,
|
|
|
|
sspi_send_token.cbBuffer, &code);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to send SSPI encryption type.");
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_send_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_send_token.pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
|
2009-01-29 05:33:58 +08:00
|
|
|
}
|
|
|
|
|
2023-01-30 23:03:00 +08:00
|
|
|
result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(result || (actualread != 4)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to receive SSPI encryption response.");
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore the first (VER) byte */
|
|
|
|
if(socksreq[1] == 255) { /* status / message type */
|
2013-07-24 22:43:13 +08:00
|
|
|
failf(data, "User was rejected by the SOCKS5 server (%u %u).",
|
|
|
|
(unsigned int)socksreq[0], (unsigned int)socksreq[1]);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(socksreq[1] != 2) { /* status / message type */
|
2013-07-24 22:43:13 +08:00
|
|
|
failf(data, "Invalid SSPI encryption response type (%u %u).",
|
|
|
|
(unsigned int)socksreq[0], (unsigned int)socksreq[1]);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2017-09-10 05:55:08 +08:00
|
|
|
memcpy(&us_length, socksreq + 2, sizeof(short));
|
2009-01-29 05:33:58 +08:00
|
|
|
us_length = ntohs(us_length);
|
|
|
|
|
|
|
|
sspi_w_token[0].cbBuffer = us_length;
|
|
|
|
sspi_w_token[0].pvBuffer = malloc(us_length);
|
|
|
|
if(!sspi_w_token[0].pvBuffer) {
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2023-01-30 23:03:00 +08:00
|
|
|
result = Curl_blockread_all(cf, data, (char *)sspi_w_token[0].pvBuffer,
|
2012-06-12 18:34:52 +08:00
|
|
|
sspi_w_token[0].cbBuffer, &actualread);
|
2009-01-29 05:33:58 +08:00
|
|
|
|
2014-10-24 04:56:35 +08:00
|
|
|
if(result || (actualread != us_length)) {
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to receive SSPI encryption type.");
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!data->set.socks5_gssapi_nec) {
|
|
|
|
wrap_desc.cBuffers = 2;
|
|
|
|
sspi_w_token[0].BufferType = SECBUFFER_STREAM;
|
|
|
|
sspi_w_token[1].BufferType = SECBUFFER_DATA;
|
|
|
|
sspi_w_token[1].cbBuffer = 0;
|
|
|
|
sspi_w_token[1].pvBuffer = NULL;
|
|
|
|
|
2012-06-12 18:34:52 +08:00
|
|
|
status = s_pSecFn->DecryptMessage(&sspi_context,
|
|
|
|
&wrap_desc,
|
|
|
|
0,
|
|
|
|
&qop);
|
2009-01-29 05:33:58 +08:00
|
|
|
|
2021-01-18 18:56:50 +08:00
|
|
|
if(check_sspi_err(data, status, "DecryptMessage")) {
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_w_token[0].pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
if(sspi_w_token[1].pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
failf(data, "Failed to query security context attributes.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sspi_w_token[1].cbBuffer != 1) {
|
2013-07-24 22:43:13 +08:00
|
|
|
failf(data, "Invalid SSPI encryption response length (%lu).",
|
|
|
|
(unsigned long)sspi_w_token[1].cbBuffer);
|
2014-04-19 05:24:41 +08:00
|
|
|
if(sspi_w_token[0].pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
if(sspi_w_token[1].pvBuffer)
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2015-03-17 20:41:49 +08:00
|
|
|
memcpy(socksreq, sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
|
2011-04-20 21:17:42 +08:00
|
|
|
}
|
|
|
|
else {
|
2009-01-29 05:33:58 +08:00
|
|
|
if(sspi_w_token[0].cbBuffer != 1) {
|
2013-07-24 22:43:13 +08:00
|
|
|
failf(data, "Invalid SSPI encryption response length (%lu).",
|
|
|
|
(unsigned long)sspi_w_token[0].cbBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
2009-01-29 05:33:58 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2015-03-17 20:41:49 +08:00
|
|
|
memcpy(socksreq, sspi_w_token[0].pvBuffer, sspi_w_token[0].cbBuffer);
|
2009-01-30 04:32:27 +08:00
|
|
|
s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
|
2009-01-29 05:33:58 +08:00
|
|
|
}
|
2020-02-14 23:16:54 +08:00
|
|
|
(void)curlx_nonblock(sock, TRUE);
|
2009-01-29 05:33:58 +08:00
|
|
|
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "SOCKS5 access with%s protection granted.",
|
2017-09-10 05:09:06 +08:00
|
|
|
(socksreq[0] == 0)?"out GSS-API data":
|
|
|
|
((socksreq[0] == 1)?" GSS-API integrity":" GSS-API confidentiality"));
|
2009-01-29 05:33:58 +08:00
|
|
|
|
|
|
|
/* For later use if encryption is required
|
|
|
|
conn->socks5_gssapi_enctype = socksreq[0];
|
2011-04-20 21:17:42 +08:00
|
|
|
if(socksreq[0] != 0)
|
2012-06-12 18:34:52 +08:00
|
|
|
conn->socks5_sspi_context = sspi_context;
|
2009-01-29 05:33:58 +08:00
|
|
|
else {
|
2012-06-12 18:34:52 +08:00
|
|
|
s_pSecFn->DeleteSecurityContext(&sspi_context);
|
|
|
|
conn->socks5_sspi_context = sspi_context;
|
2009-01-29 05:33:58 +08:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
#endif
|