2009-01-28 16:33:58 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-02-14 10:16:54 -05:00
|
|
|
* Copyright (C) 2012 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2017-09-09 17:09:06 -04:00
|
|
|
* Copyright (C) 2009, Markus Moeller, <markus_moeller@compuserve.com>
|
2009-01-28 16:33:58 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2009-01-28 16:33:58 -05: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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2009-01-28 16:33:58 -05:00
|
|
|
|
2015-01-09 15:29:26 -05:00
|
|
|
#if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_PROXY)
|
2009-01-28 16:33:58 -05:00
|
|
|
|
2011-07-13 16:54:54 -04:00
|
|
|
#include "curl_gssapi.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "connect.h"
|
|
|
|
#include "timeval.h"
|
|
|
|
#include "socks.h"
|
|
|
|
#include "warnless.h"
|
2016-04-29 09:46:40 -04:00
|
|
|
|
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2010-01-23 12:31:54 -05:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2009-01-28 16:33:58 -05:00
|
|
|
|
2011-05-26 09:44:53 -04:00
|
|
|
static gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
|
|
|
|
|
2009-01-28 16:33:58 -05:00
|
|
|
/*
|
2014-07-21 03:53:47 -04:00
|
|
|
* Helper GSS-API error functions.
|
2009-01-28 16:33:58 -05:00
|
|
|
*/
|
2016-06-21 09:47:12 -04:00
|
|
|
static int check_gss_err(struct Curl_easy *data,
|
2009-01-28 16:33:58 -05:00
|
|
|
OM_uint32 major_status,
|
|
|
|
OM_uint32 minor_status,
|
2016-11-23 01:53:24 -05:00
|
|
|
const char *function)
|
2009-01-28 16:33:58 -05:00
|
|
|
{
|
|
|
|
if(GSS_ERROR(major_status)) {
|
2015-03-17 08:41:49 -04:00
|
|
|
OM_uint32 maj_stat, min_stat;
|
2009-01-28 16:33:58 -05:00
|
|
|
OM_uint32 msg_ctx = 0;
|
|
|
|
gss_buffer_desc status_string;
|
|
|
|
char buf[1024];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
msg_ctx = 0;
|
|
|
|
while(!msg_ctx) {
|
|
|
|
/* convert major status code (GSS-API error) to text */
|
|
|
|
maj_stat = gss_display_status(&min_stat, major_status,
|
|
|
|
GSS_C_GSS_CODE,
|
|
|
|
GSS_C_NULL_OID,
|
|
|
|
&msg_ctx, &status_string);
|
|
|
|
if(maj_stat == GSS_S_COMPLETE) {
|
|
|
|
if(sizeof(buf) > len + status_string.length + 1) {
|
2017-09-09 17:55:08 -04:00
|
|
|
strcpy(buf + len, (char *) status_string.value);
|
2009-01-28 16:33:58 -05:00
|
|
|
len += status_string.length;
|
|
|
|
}
|
|
|
|
gss_release_buffer(&min_stat, &status_string);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gss_release_buffer(&min_stat, &status_string);
|
|
|
|
}
|
|
|
|
if(sizeof(buf) > len + 3) {
|
2017-09-09 17:55:08 -04:00
|
|
|
strcpy(buf + len, ".\n");
|
2009-01-28 16:33:58 -05:00
|
|
|
len += 2;
|
|
|
|
}
|
|
|
|
msg_ctx = 0;
|
|
|
|
while(!msg_ctx) {
|
|
|
|
/* convert minor status code (underlying routine error) to text */
|
|
|
|
maj_stat = gss_display_status(&min_stat, minor_status,
|
|
|
|
GSS_C_MECH_CODE,
|
|
|
|
GSS_C_NULL_OID,
|
|
|
|
&msg_ctx, &status_string);
|
|
|
|
if(maj_stat == GSS_S_COMPLETE) {
|
2011-03-15 19:19:26 -04:00
|
|
|
if(sizeof(buf) > len + status_string.length)
|
2017-09-09 17:55:08 -04:00
|
|
|
strcpy(buf + len, (char *) status_string.value);
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_buffer(&min_stat, &status_string);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gss_release_buffer(&min_stat, &status_string);
|
|
|
|
}
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "GSS-API error: %s failed:\n%s", function, buf);
|
2015-03-17 08:05:01 -04:00
|
|
|
return 1;
|
2009-01-28 16:33:58 -05:00
|
|
|
}
|
|
|
|
|
2015-03-17 08:05:01 -04:00
|
|
|
return 0;
|
2009-01-28 16:33:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_SOCKS5_gssapi_negotiate(int sockindex,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2009-01-28 16:33:58 -05:00
|
|
|
curl_socket_t sock = conn->sock[sockindex];
|
|
|
|
CURLcode code;
|
|
|
|
ssize_t actualread;
|
|
|
|
ssize_t written;
|
|
|
|
int result;
|
|
|
|
OM_uint32 gss_major_status, gss_minor_status, gss_status;
|
|
|
|
OM_uint32 gss_ret_flags;
|
|
|
|
int gss_conf_state, gss_enc;
|
|
|
|
gss_buffer_desc service = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc gss_send_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc gss_recv_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc gss_w_token = GSS_C_EMPTY_BUFFER;
|
2020-05-13 06:56:53 -04:00
|
|
|
gss_buffer_desc *gss_token = GSS_C_NO_BUFFER;
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_name_t server = GSS_C_NO_NAME;
|
|
|
|
gss_name_t gss_client_name = GSS_C_NO_NAME;
|
2011-04-06 01:30:09 -04:00
|
|
|
unsigned short us_length;
|
2017-09-09 17:09:06 -04:00
|
|
|
char *user = NULL;
|
2014-07-21 03:53:47 -04:00
|
|
|
unsigned char socksreq[4]; /* room for GSS-API exchange header only */
|
2016-04-09 15:47:05 -04:00
|
|
|
const char *serviceptr = data->set.str[STRING_PROXY_SERVICE_NAME] ?
|
|
|
|
data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd";
|
2016-11-16 12:49:15 -05:00
|
|
|
const size_t serviceptr_length = strlen(serviceptr);
|
2009-01-28 16:33:58 -05:00
|
|
|
|
2014-07-21 03:53:47 -04:00
|
|
|
/* GSS-API request looks like
|
2009-01-28 16:33:58 -05:00
|
|
|
* +----+------+-----+----------------+
|
|
|
|
* |VER | MTYP | LEN | TOKEN |
|
|
|
|
* +----+------+----------------------+
|
|
|
|
* | 1 | 1 | 2 | up to 2^16 - 1 |
|
|
|
|
* +----+------+-----+----------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* prepare service name */
|
2015-03-17 08:41:49 -04:00
|
|
|
if(strchr(serviceptr, '/')) {
|
2016-11-16 12:49:15 -05:00
|
|
|
service.length = serviceptr_length;
|
|
|
|
service.value = malloc(service.length);
|
2009-01-28 16:33:58 -05:00
|
|
|
if(!service.value)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
memcpy(service.value, serviceptr, service.length);
|
|
|
|
|
|
|
|
gss_major_status = gss_import_name(&gss_minor_status, &service,
|
|
|
|
(gss_OID) GSS_C_NULL_OID, &server);
|
|
|
|
}
|
|
|
|
else {
|
2016-11-16 12:49:15 -05:00
|
|
|
service.value = malloc(serviceptr_length +
|
2017-09-09 17:55:08 -04:00
|
|
|
strlen(conn->socks_proxy.host.name) + 2);
|
2009-01-28 16:33:58 -05:00
|
|
|
if(!service.value)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2017-09-09 17:55:08 -04:00
|
|
|
service.length = serviceptr_length +
|
|
|
|
strlen(conn->socks_proxy.host.name) + 1;
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(service.value, service.length + 1, "%s@%s",
|
|
|
|
serviceptr, conn->socks_proxy.host.name);
|
2009-01-28 16:33:58 -05:00
|
|
|
|
|
|
|
gss_major_status = gss_import_name(&gss_minor_status, &service,
|
2015-01-09 15:29:26 -05:00
|
|
|
GSS_C_NT_HOSTBASED_SERVICE, &server);
|
2009-01-28 16:33:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
gss_release_buffer(&gss_status, &service); /* clear allocated memory */
|
|
|
|
|
2015-03-17 08:41:49 -04:00
|
|
|
if(check_gss_err(data, gss_major_status,
|
|
|
|
gss_minor_status, "gss_import_name()")) {
|
2009-01-28 16:33:58 -05:00
|
|
|
failf(data, "Failed to create service name.");
|
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
(void)curlx_nonblock(sock, FALSE);
|
|
|
|
|
2009-01-28 16:33:58 -05:00
|
|
|
/* As long as we need to keep sending some context info, and there's no */
|
|
|
|
/* errors, keep sending it... */
|
|
|
|
for(;;) {
|
2011-07-19 13:10:43 -04:00
|
|
|
gss_major_status = Curl_gss_init_sec_context(data,
|
|
|
|
&gss_minor_status,
|
2011-06-10 11:26:34 -04:00
|
|
|
&gss_context,
|
|
|
|
server,
|
2014-07-23 12:56:19 -04:00
|
|
|
&Curl_krb5_mech_oid,
|
2011-06-10 11:16:06 -04:00
|
|
|
NULL,
|
|
|
|
gss_token,
|
|
|
|
&gss_send_token,
|
2014-12-04 16:59:20 -05:00
|
|
|
TRUE,
|
2011-06-10 11:26:34 -04:00
|
|
|
&gss_ret_flags);
|
2009-01-28 16:33:58 -05:00
|
|
|
|
|
|
|
if(gss_token != GSS_C_NO_BUFFER)
|
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
2015-03-17 08:41:49 -04:00
|
|
|
if(check_gss_err(data, gss_major_status,
|
|
|
|
gss_minor_status, "gss_init_sec_context")) {
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to initial GSS-API token.");
|
2009-01-28 16:33:58 -05:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(gss_send_token.length != 0) {
|
2014-07-21 03:53:47 -04:00
|
|
|
socksreq[0] = 1; /* GSS-API subnegotiation version */
|
2009-01-28 16:33:58 -05:00
|
|
|
socksreq[1] = 1; /* authentication message type */
|
|
|
|
us_length = htons((short)gss_send_token.length);
|
2017-09-09 17:55:08 -04:00
|
|
|
memcpy(socksreq + 2, &us_length, sizeof(short));
|
2009-01-28 16:33:58 -05:00
|
|
|
|
|
|
|
code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(code || (4 != written)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to send GSS-API authentication request.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
code = Curl_write_plain(conn, sock, (char *)gss_send_token.value,
|
|
|
|
gss_send_token.length, &written);
|
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(code || ((ssize_t)gss_send_token.length != written)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to send GSS-API authentication token.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
2020-03-30 04:55:31 -04:00
|
|
|
if(gss_major_status != GSS_S_CONTINUE_NEEDED)
|
|
|
|
break;
|
2009-01-28 16:33:58 -05:00
|
|
|
|
|
|
|
/* analyse response */
|
|
|
|
|
2014-07-21 03:53:47 -04:00
|
|
|
/* GSS-API response looks like
|
2009-01-28 16:33:58 -05:00
|
|
|
* +----+------+-----+----------------+
|
|
|
|
* |VER | MTYP | LEN | TOKEN |
|
|
|
|
* +----+------+----------------------+
|
|
|
|
* | 1 | 1 | 2 | up to 2^16 - 1 |
|
|
|
|
* +----+------+-----+----------------+
|
|
|
|
*/
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
result = Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result || (actualread != 4)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to receive GSS-API authentication response.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore the first (VER) byte */
|
|
|
|
if(socksreq[1] == 255) { /* status / message type */
|
|
|
|
failf(data, "User was rejected by the SOCKS5 server (%d %d).",
|
|
|
|
socksreq[0], socksreq[1]);
|
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(socksreq[1] != 1) { /* status / messgae type */
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Invalid GSS-API authentication response type (%d %d).",
|
2009-01-28 16:33:58 -05:00
|
|
|
socksreq[0], socksreq[1]);
|
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:55:08 -04:00
|
|
|
memcpy(&us_length, socksreq + 2, sizeof(short));
|
2009-01-28 16:33:58 -05:00
|
|
|
us_length = ntohs(us_length);
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
gss_recv_token.length = us_length;
|
|
|
|
gss_recv_token.value = malloc(us_length);
|
2009-01-28 16:33:58 -05:00
|
|
|
if(!gss_recv_token.value) {
|
|
|
|
failf(data,
|
2014-07-21 03:53:47 -04:00
|
|
|
"Could not allocate memory for GSS-API authentication "
|
2009-01-28 16:33:58 -05:00
|
|
|
"response token.");
|
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
result = Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
|
|
|
|
gss_recv_token.length, &actualread);
|
2009-01-28 16:33:58 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result || (actualread != us_length)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to receive GSS-API authentication token.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
gss_token = &gss_recv_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
gss_release_name(&gss_status, &server);
|
|
|
|
|
|
|
|
/* Everything is good so far, user was authenticated! */
|
2016-12-13 17:34:59 -05:00
|
|
|
gss_major_status = gss_inquire_context(&gss_minor_status, gss_context,
|
|
|
|
&gss_client_name, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL);
|
2015-03-17 08:41:49 -04:00
|
|
|
if(check_gss_err(data, gss_major_status,
|
|
|
|
gss_minor_status, "gss_inquire_context")) {
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
gss_release_name(&gss_status, &gss_client_name);
|
|
|
|
failf(data, "Failed to determine user name.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
gss_major_status = gss_display_name(&gss_minor_status, gss_client_name,
|
|
|
|
&gss_send_token, NULL);
|
2015-03-17 08:41:49 -04:00
|
|
|
if(check_gss_err(data, gss_major_status,
|
|
|
|
gss_minor_status, "gss_display_name")) {
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
gss_release_name(&gss_status, &gss_client_name);
|
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
|
|
|
failf(data, "Failed to determine user name.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2017-09-09 17:55:08 -04:00
|
|
|
user = malloc(gss_send_token.length + 1);
|
2009-01-28 16:33:58 -05:00
|
|
|
if(!user) {
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
gss_release_name(&gss_status, &gss_client_name);
|
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(user, gss_send_token.value, gss_send_token.length);
|
|
|
|
user[gss_send_token.length] = '\0';
|
|
|
|
gss_release_name(&gss_status, &gss_client_name);
|
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
2014-07-21 03:53:47 -04:00
|
|
|
infof(data, "SOCKS5 server authencticated user %s with GSS-API.\n",user);
|
2009-01-28 16:33:58 -05:00
|
|
|
free(user);
|
2017-09-09 17:09:06 -04:00
|
|
|
user = NULL;
|
2009-01-28 16:33:58 -05:00
|
|
|
|
|
|
|
/* Do encryption */
|
2014-07-21 03:53:47 -04:00
|
|
|
socksreq[0] = 1; /* GSS-API subnegotiation version */
|
2009-01-28 16:33:58 -05:00
|
|
|
socksreq[1] = 2; /* encryption message type */
|
|
|
|
|
|
|
|
gss_enc = 0; /* no data protection */
|
|
|
|
/* do confidentiality protection if supported */
|
|
|
|
if(gss_ret_flags & GSS_C_CONF_FLAG)
|
|
|
|
gss_enc = 2;
|
|
|
|
/* else do integrity protection */
|
|
|
|
else if(gss_ret_flags & GSS_C_INTEG_FLAG)
|
|
|
|
gss_enc = 1;
|
|
|
|
|
2014-07-21 03:53:47 -04:00
|
|
|
infof(data, "SOCKS5 server supports GSS-API %s data protection.\n",
|
2017-09-09 17:09:06 -04:00
|
|
|
(gss_enc == 0)?"no":((gss_enc==1)?"integrity":"confidentiality"));
|
2009-01-28 16:33:58 -05:00
|
|
|
/* force for the moment to no data protection */
|
|
|
|
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-09 17:55:08 -04:00
|
|
|
memcpy(socksreq + 2, &us_length, sizeof(short));
|
2009-01-28 16:33:58 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
gss_send_token.length = 1;
|
|
|
|
gss_send_token.value = malloc(1);
|
|
|
|
if(!gss_send_token.value) {
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(gss_send_token.value, &gss_enc, 1);
|
|
|
|
|
|
|
|
gss_major_status = gss_wrap(&gss_minor_status, gss_context, 0,
|
|
|
|
GSS_C_QOP_DEFAULT, &gss_send_token,
|
|
|
|
&gss_conf_state, &gss_w_token);
|
|
|
|
|
2015-03-17 08:41:49 -04:00
|
|
|
if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_wrap")) {
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
|
|
|
gss_release_buffer(&gss_status, &gss_w_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to wrap GSS-API encryption value into token.");
|
2009-01-28 16:33:58 -05:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
gss_release_buffer(&gss_status, &gss_send_token);
|
|
|
|
|
|
|
|
us_length = htons((short)gss_w_token.length);
|
2017-09-09 17:55:08 -04:00
|
|
|
memcpy(socksreq + 2, &us_length, sizeof(short));
|
2009-01-28 16:33:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(code || (4 != written)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to send GSS-API encryption request.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_buffer(&gss_status, &gss_w_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(data->set.socks5_gssapi_nec) {
|
|
|
|
memcpy(socksreq, &gss_enc, 1);
|
|
|
|
code = Curl_write_plain(conn, sock, socksreq, 1, &written);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(code || ( 1 != written)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to send GSS-API encryption type.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2009-01-28 16:33:58 -05:00
|
|
|
code = Curl_write_plain(conn, sock, (char *)gss_w_token.value,
|
|
|
|
gss_w_token.length, &written);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(code || ((ssize_t)gss_w_token.length != written)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to send GSS-API encryption type.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_buffer(&gss_status, &gss_w_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
gss_release_buffer(&gss_status, &gss_w_token);
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
result = Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result || (actualread != 4)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to receive GSS-API encryption response.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore the first (VER) byte */
|
|
|
|
if(socksreq[1] == 255) { /* status / message type */
|
|
|
|
failf(data, "User was rejected by the SOCKS5 server (%d %d).",
|
|
|
|
socksreq[0], socksreq[1]);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(socksreq[1] != 2) { /* status / messgae type */
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Invalid GSS-API encryption response type (%d %d).",
|
2009-01-28 16:33:58 -05:00
|
|
|
socksreq[0], socksreq[1]);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:55:08 -04:00
|
|
|
memcpy(&us_length, socksreq + 2, sizeof(short));
|
2009-01-28 16:33:58 -05:00
|
|
|
us_length = ntohs(us_length);
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
gss_recv_token.length = us_length;
|
|
|
|
gss_recv_token.value = malloc(gss_recv_token.length);
|
2009-01-28 16:33:58 -05:00
|
|
|
if(!gss_recv_token.value) {
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-09-09 17:09:06 -04:00
|
|
|
result = Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
|
|
|
|
gss_recv_token.length, &actualread);
|
2009-01-28 16:33:58 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result || (actualread != us_length)) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to receive GSS-API encryptrion type.");
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!data->set.socks5_gssapi_nec) {
|
|
|
|
gss_major_status = gss_unwrap(&gss_minor_status, gss_context,
|
|
|
|
&gss_recv_token, &gss_w_token,
|
|
|
|
0, GSS_C_QOP_DEFAULT);
|
|
|
|
|
2015-03-17 08:41:49 -04:00
|
|
|
if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_unwrap")) {
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
gss_release_buffer(&gss_status, &gss_w_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Failed to unwrap GSS-API encryption value into token.");
|
2009-01-28 16:33:58 -05:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
|
|
|
|
if(gss_w_token.length != 1) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Invalid GSS-API encryption response length (%d).",
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_w_token.length);
|
|
|
|
gss_release_buffer(&gss_status, &gss_w_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2015-03-17 08:41:49 -04:00
|
|
|
memcpy(socksreq, gss_w_token.value, gss_w_token.length);
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_buffer(&gss_status, &gss_w_token);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(gss_recv_token.length != 1) {
|
2014-07-21 03:53:47 -04:00
|
|
|
failf(data, "Invalid GSS-API encryption response length (%d).",
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_recv_token.length);
|
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2015-03-17 08:41:49 -04:00
|
|
|
memcpy(socksreq, gss_recv_token.value, gss_recv_token.length);
|
2009-01-28 16:33:58 -05:00
|
|
|
gss_release_buffer(&gss_status, &gss_recv_token);
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
(void)curlx_nonblock(sock, TRUE);
|
|
|
|
|
2009-01-28 16:33:58 -05:00
|
|
|
infof(data, "SOCKS5 access with%s protection granted.\n",
|
2017-09-09 17:09:06 -04:00
|
|
|
(socksreq[0] == 0)?"out GSS-API data":
|
|
|
|
((socksreq[0] == 1)?" GSS-API integrity":" GSS-API confidentiality"));
|
2009-01-28 16:33:58 -05:00
|
|
|
|
|
|
|
conn->socks5_gssapi_enctype = socksreq[0];
|
|
|
|
if(socksreq[0] == 0)
|
|
|
|
gss_delete_sec_context(&gss_status, &gss_context, NULL);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2009-01-30 14:29:25 -05:00
|
|
|
|
2015-01-09 15:29:26 -05:00
|
|
|
#endif /* HAVE_GSSAPI && !CURL_DISABLE_PROXY */
|