| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 2012 - 2020, Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* Copyright (C) 2009, Markus Moeller, <[email protected]> |
| 10 |
* |
| 11 |
* This software is licensed as described in the file COPYING, which |
| 12 |
* you should have received as part of this distribution. The terms |
| 13 |
* are also available at https://curl.haxx.se/docs/copyright.html. |
| 14 |
* |
| 15 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 16 |
* copies of the Software, and permit persons to whom the Software is |
| 17 |
* furnished to do so, under the terms of the COPYING file. |
| 18 |
* |
| 19 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 20 |
* KIND, either express or implied. |
| 21 |
* |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include "curl_setup.h" |
| 25 |
|
| 26 |
#if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_PROXY) |
| 27 |
|
| 28 |
#include "curl_gssapi.h" |
| 29 |
#include "urldata.h" |
| 30 |
#include "sendf.h" |
| 31 |
#include "connect.h" |
| 32 |
#include "timeval.h" |
| 33 |
#include "socks.h" |
| 34 |
#include "warnless.h" |
| 35 |
|
| 36 |
/* The last 3 #include files should be in this order */ |
| 37 |
#include "curl_printf.h" |
| 38 |
#include "curl_memory.h" |
| 39 |
#include "memdebug.h" |
| 40 |
|
| 41 |
static gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT; |
| 42 |
|
| 43 |
/* |
| 44 |
* Helper GSS-API error functions. |
| 45 |
*/ |
| 46 |
static int check_gss_err(struct Curl_easy *data, |
| 47 |
OM_uint32 major_status, |
| 48 |
OM_uint32 minor_status, |
| 49 |
const char *function) |
| 50 |
{ |
| 51 |
if(GSS_ERROR(major_status)) { |
| 52 |
OM_uint32 maj_stat, min_stat; |
| 53 |
OM_uint32 msg_ctx = 0; |
| 54 |
gss_buffer_desc status_string; |
| 55 |
char buf[1024]; |
| 56 |
size_t len; |
| 57 |
|
| 58 |
len = 0; |
| 59 |
msg_ctx = 0; |
| 60 |
while(!msg_ctx) { |
| 61 |
/* convert major status code (GSS-API error) to text */ |
| 62 |
maj_stat = gss_display_status(&min_stat, major_status, |
| 63 |
GSS_C_GSS_CODE, |
| 64 |
GSS_C_NULL_OID, |
| 65 |
&msg_ctx, &status_string); |
| 66 |
if(maj_stat == GSS_S_COMPLETE) { |
| 67 |
if(sizeof(buf) > len + status_string.length + 1) { |
| 68 |
strcpy(buf + len, (char *) status_string.value); |
| 69 |
len += status_string.length; |
| 70 |
} |
| 71 |
gss_release_buffer(&min_stat, &status_string); |
| 72 |
break; |
| 73 |
} |
| 74 |
gss_release_buffer(&min_stat, &status_string); |
| 75 |
} |
| 76 |
if(sizeof(buf) > len + 3) { |
| 77 |
strcpy(buf + len, ".\n"); |
| 78 |
len += 2; |
| 79 |
} |
| 80 |
msg_ctx = 0; |
| 81 |
while(!msg_ctx) { |
| 82 |
/* convert minor status code (underlying routine error) to text */ |
| 83 |
maj_stat = gss_display_status(&min_stat, minor_status, |
| 84 |
GSS_C_MECH_CODE, |
| 85 |
GSS_C_NULL_OID, |
| 86 |
&msg_ctx, &status_string); |
| 87 |
if(maj_stat == GSS_S_COMPLETE) { |
| 88 |
if(sizeof(buf) > len + status_string.length) |
| 89 |
strcpy(buf + len, (char *) status_string.value); |
| 90 |
gss_release_buffer(&min_stat, &status_string); |
| 91 |
break; |
| 92 |
} |
| 93 |
gss_release_buffer(&min_stat, &status_string); |
| 94 |
} |
| 95 |
failf(data, "GSS-API error: %s failed:\n%s", function, buf); |
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
CURLcode Curl_SOCKS5_gssapi_negotiate(int sockindex, |
| 103 |
struct connectdata *conn) |
| 104 |
{ |
| 105 |
struct Curl_easy *data = conn->data; |
| 106 |
curl_socket_t sock = conn->sock[sockindex]; |
| 107 |
CURLcode code; |
| 108 |
ssize_t actualread; |
| 109 |
ssize_t written; |
| 110 |
int result; |
| 111 |
OM_uint32 gss_major_status, gss_minor_status, gss_status; |
| 112 |
OM_uint32 gss_ret_flags; |
| 113 |
int gss_conf_state, gss_enc; |
| 114 |
gss_buffer_desc service = GSS_C_EMPTY_BUFFER; |
| 115 |
gss_buffer_desc gss_send_token = GSS_C_EMPTY_BUFFER; |
| 116 |
gss_buffer_desc gss_recv_token = GSS_C_EMPTY_BUFFER; |
| 117 |
gss_buffer_desc gss_w_token = GSS_C_EMPTY_BUFFER; |
| 118 |
gss_buffer_desc* gss_token = GSS_C_NO_BUFFER; |
| 119 |
gss_name_t server = GSS_C_NO_NAME; |
| 120 |
gss_name_t gss_client_name = GSS_C_NO_NAME; |
| 121 |
unsigned short us_length; |
| 122 |
char *user = NULL; |
| 123 |
unsigned char socksreq[4]; /* room for GSS-API exchange header only */ |
| 124 |
const char *serviceptr = data->set.str[STRING_PROXY_SERVICE_NAME] ? |
| 125 |
data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd"; |
| 126 |
const size_t serviceptr_length = strlen(serviceptr); |
| 127 |
|
| 128 |
/* GSS-API request looks like |
| 129 |
* +----+------+-----+----------------+ |
| 130 |
* |VER | MTYP | LEN | TOKEN | |
| 131 |
* +----+------+----------------------+ |
| 132 |
* | 1 | 1 | 2 | up to 2^16 - 1 | |
| 133 |
* +----+------+-----+----------------+ |
| 134 |
*/ |
| 135 |
|
| 136 |
/* prepare service name */ |
| 137 |
if(strchr(serviceptr, '/')) { |
| 138 |
service.length = serviceptr_length; |
| 139 |
service.value = malloc(service.length); |
| 140 |
if(!service.value) |
| 141 |
return CURLE_OUT_OF_MEMORY; |
| 142 |
memcpy(service.value, serviceptr, service.length); |
| 143 |
|
| 144 |
gss_major_status = gss_import_name(&gss_minor_status, &service, |
| 145 |
(gss_OID) GSS_C_NULL_OID, &server); |
| 146 |
} |
| 147 |
else { |
| 148 |
service.value = malloc(serviceptr_length + |
| 149 |
strlen(conn->socks_proxy.host.name) + 2); |
| 150 |
if(!service.value) |
| 151 |
return CURLE_OUT_OF_MEMORY; |
| 152 |
service.length = serviceptr_length + |
| 153 |
strlen(conn->socks_proxy.host.name) + 1; |
| 154 |
msnprintf(service.value, service.length + 1, "%s@%s", |
| 155 |
serviceptr, conn->socks_proxy.host.name); |
| 156 |
|
| 157 |
gss_major_status = gss_import_name(&gss_minor_status, &service, |
| 158 |
GSS_C_NT_HOSTBASED_SERVICE, &server); |
| 159 |
} |
| 160 |
|
| 161 |
gss_release_buffer(&gss_status, &service); /* clear allocated memory */ |
| 162 |
|
| 163 |
if(check_gss_err(data, gss_major_status, |
| 164 |
gss_minor_status, "gss_import_name()")) { |
| 165 |
failf(data, "Failed to create service name."); |
| 166 |
gss_release_name(&gss_status, &server); |
| 167 |
return CURLE_COULDNT_CONNECT; |
| 168 |
} |
| 169 |
|
| 170 |
(void)curlx_nonblock(sock, FALSE); |
| 171 |
|
| 172 |
/* As long as we need to keep sending some context info, and there's no */ |
| 173 |
/* errors, keep sending it... */ |
| 174 |
for(;;) { |
| 175 |
gss_major_status = Curl_gss_init_sec_context(data, |
| 176 |
&gss_minor_status, |
| 177 |
&gss_context, |
| 178 |
server, |
| 179 |
&Curl_krb5_mech_oid, |
| 180 |
NULL, |
| 181 |
gss_token, |
| 182 |
&gss_send_token, |
| 183 |
TRUE, |
| 184 |
&gss_ret_flags); |
| 185 |
|
| 186 |
if(gss_token != GSS_C_NO_BUFFER) |
| 187 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 188 |
if(check_gss_err(data, gss_major_status, |
| 189 |
gss_minor_status, "gss_init_sec_context")) { |
| 190 |
gss_release_name(&gss_status, &server); |
| 191 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 192 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 193 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 194 |
failf(data, "Failed to initial GSS-API token."); |
| 195 |
return CURLE_COULDNT_CONNECT; |
| 196 |
} |
| 197 |
|
| 198 |
if(gss_send_token.length != 0) { |
| 199 |
socksreq[0] = 1; /* GSS-API subnegotiation version */ |
| 200 |
socksreq[1] = 1; /* authentication message type */ |
| 201 |
us_length = htons((short)gss_send_token.length); |
| 202 |
memcpy(socksreq + 2, &us_length, sizeof(short)); |
| 203 |
|
| 204 |
code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written); |
| 205 |
if(code || (4 != written)) { |
| 206 |
failf(data, "Failed to send GSS-API authentication request."); |
| 207 |
gss_release_name(&gss_status, &server); |
| 208 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 209 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 210 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 211 |
return CURLE_COULDNT_CONNECT; |
| 212 |
} |
| 213 |
|
| 214 |
code = Curl_write_plain(conn, sock, (char *)gss_send_token.value, |
| 215 |
gss_send_token.length, &written); |
| 216 |
|
| 217 |
if(code || ((ssize_t)gss_send_token.length != written)) { |
| 218 |
failf(data, "Failed to send GSS-API authentication token."); |
| 219 |
gss_release_name(&gss_status, &server); |
| 220 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 221 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 222 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 223 |
return CURLE_COULDNT_CONNECT; |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 229 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 230 |
if(gss_major_status != GSS_S_CONTINUE_NEEDED) break; |
| 231 |
|
| 232 |
/* analyse response */ |
| 233 |
|
| 234 |
/* GSS-API response looks like |
| 235 |
* +----+------+-----+----------------+ |
| 236 |
* |VER | MTYP | LEN | TOKEN | |
| 237 |
* +----+------+----------------------+ |
| 238 |
* | 1 | 1 | 2 | up to 2^16 - 1 | |
| 239 |
* +----+------+-----+----------------+ |
| 240 |
*/ |
| 241 |
|
| 242 |
result = Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread); |
| 243 |
if(result || (actualread != 4)) { |
| 244 |
failf(data, "Failed to receive GSS-API authentication response."); |
| 245 |
gss_release_name(&gss_status, &server); |
| 246 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 247 |
return CURLE_COULDNT_CONNECT; |
| 248 |
} |
| 249 |
|
| 250 |
/* ignore the first (VER) byte */ |
| 251 |
if(socksreq[1] == 255) { /* status / message type */ |
| 252 |
failf(data, "User was rejected by the SOCKS5 server (%d %d).", |
| 253 |
socksreq[0], socksreq[1]); |
| 254 |
gss_release_name(&gss_status, &server); |
| 255 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 256 |
return CURLE_COULDNT_CONNECT; |
| 257 |
} |
| 258 |
|
| 259 |
if(socksreq[1] != 1) { /* status / messgae type */ |
| 260 |
failf(data, "Invalid GSS-API authentication response type (%d %d).", |
| 261 |
socksreq[0], socksreq[1]); |
| 262 |
gss_release_name(&gss_status, &server); |
| 263 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 264 |
return CURLE_COULDNT_CONNECT; |
| 265 |
} |
| 266 |
|
| 267 |
memcpy(&us_length, socksreq + 2, sizeof(short)); |
| 268 |
us_length = ntohs(us_length); |
| 269 |
|
| 270 |
gss_recv_token.length = us_length; |
| 271 |
gss_recv_token.value = malloc(us_length); |
| 272 |
if(!gss_recv_token.value) { |
| 273 |
failf(data, |
| 274 |
"Could not allocate memory for GSS-API authentication " |
| 275 |
"response token."); |
| 276 |
gss_release_name(&gss_status, &server); |
| 277 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 278 |
return CURLE_OUT_OF_MEMORY; |
| 279 |
} |
| 280 |
|
| 281 |
result = Curl_blockread_all(conn, sock, (char *)gss_recv_token.value, |
| 282 |
gss_recv_token.length, &actualread); |
| 283 |
|
| 284 |
if(result || (actualread != us_length)) { |
| 285 |
failf(data, "Failed to receive GSS-API authentication token."); |
| 286 |
gss_release_name(&gss_status, &server); |
| 287 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 288 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 289 |
return CURLE_COULDNT_CONNECT; |
| 290 |
} |
| 291 |
|
| 292 |
gss_token = &gss_recv_token; |
| 293 |
} |
| 294 |
|
| 295 |
gss_release_name(&gss_status, &server); |
| 296 |
|
| 297 |
/* Everything is good so far, user was authenticated! */ |
| 298 |
gss_major_status = gss_inquire_context(&gss_minor_status, gss_context, |
| 299 |
&gss_client_name, NULL, NULL, NULL, |
| 300 |
NULL, NULL, NULL); |
| 301 |
if(check_gss_err(data, gss_major_status, |
| 302 |
gss_minor_status, "gss_inquire_context")) { |
| 303 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 304 |
gss_release_name(&gss_status, &gss_client_name); |
| 305 |
failf(data, "Failed to determine user name."); |
| 306 |
return CURLE_COULDNT_CONNECT; |
| 307 |
} |
| 308 |
gss_major_status = gss_display_name(&gss_minor_status, gss_client_name, |
| 309 |
&gss_send_token, NULL); |
| 310 |
if(check_gss_err(data, gss_major_status, |
| 311 |
gss_minor_status, "gss_display_name")) { |
| 312 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 313 |
gss_release_name(&gss_status, &gss_client_name); |
| 314 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 315 |
failf(data, "Failed to determine user name."); |
| 316 |
return CURLE_COULDNT_CONNECT; |
| 317 |
} |
| 318 |
user = malloc(gss_send_token.length + 1); |
| 319 |
if(!user) { |
| 320 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 321 |
gss_release_name(&gss_status, &gss_client_name); |
| 322 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 323 |
return CURLE_OUT_OF_MEMORY; |
| 324 |
} |
| 325 |
|
| 326 |
memcpy(user, gss_send_token.value, gss_send_token.length); |
| 327 |
user[gss_send_token.length] = '\0'; |
| 328 |
gss_release_name(&gss_status, &gss_client_name); |
| 329 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 330 |
infof(data, "SOCKS5 server authencticated user %s with GSS-API.\n",user); |
| 331 |
free(user); |
| 332 |
user = NULL; |
| 333 |
|
| 334 |
/* Do encryption */ |
| 335 |
socksreq[0] = 1; /* GSS-API subnegotiation version */ |
| 336 |
socksreq[1] = 2; /* encryption message type */ |
| 337 |
|
| 338 |
gss_enc = 0; /* no data protection */ |
| 339 |
/* do confidentiality protection if supported */ |
| 340 |
if(gss_ret_flags & GSS_C_CONF_FLAG) |
| 341 |
gss_enc = 2; |
| 342 |
/* else do integrity protection */ |
| 343 |
else if(gss_ret_flags & GSS_C_INTEG_FLAG) |
| 344 |
gss_enc = 1; |
| 345 |
|
| 346 |
infof(data, "SOCKS5 server supports GSS-API %s data protection.\n", |
| 347 |
(gss_enc == 0)?"no":((gss_enc==1)?"integrity":"confidentiality")); |
| 348 |
/* force for the moment to no data protection */ |
| 349 |
gss_enc = 0; |
| 350 |
/* |
| 351 |
* Sending the encryption type in clear seems wrong. It should be |
| 352 |
* protected with gss_seal()/gss_wrap(). See RFC1961 extract below |
| 353 |
* The NEC reference implementations on which this is based is |
| 354 |
* therefore at fault |
| 355 |
* |
| 356 |
* +------+------+------+.......................+ |
| 357 |
* + ver | mtyp | len | token | |
| 358 |
* +------+------+------+.......................+ |
| 359 |
* + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets | |
| 360 |
* +------+------+------+.......................+ |
| 361 |
* |
| 362 |
* Where: |
| 363 |
* |
| 364 |
* - "ver" is the protocol version number, here 1 to represent the |
| 365 |
* first version of the SOCKS/GSS-API protocol |
| 366 |
* |
| 367 |
* - "mtyp" is the message type, here 2 to represent a protection |
| 368 |
* -level negotiation message |
| 369 |
* |
| 370 |
* - "len" is the length of the "token" field in octets |
| 371 |
* |
| 372 |
* - "token" is the GSS-API encapsulated protection level |
| 373 |
* |
| 374 |
* The token is produced by encapsulating an octet containing the |
| 375 |
* required protection level using gss_seal()/gss_wrap() with conf_req |
| 376 |
* set to FALSE. The token is verified using gss_unseal()/ |
| 377 |
* gss_unwrap(). |
| 378 |
* |
| 379 |
*/ |
| 380 |
if(data->set.socks5_gssapi_nec) { |
| 381 |
us_length = htons((short)1); |
| 382 |
memcpy(socksreq + 2, &us_length, sizeof(short)); |
| 383 |
} |
| 384 |
else { |
| 385 |
gss_send_token.length = 1; |
| 386 |
gss_send_token.value = malloc(1); |
| 387 |
if(!gss_send_token.value) { |
| 388 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 389 |
return CURLE_OUT_OF_MEMORY; |
| 390 |
} |
| 391 |
memcpy(gss_send_token.value, &gss_enc, 1); |
| 392 |
|
| 393 |
gss_major_status = gss_wrap(&gss_minor_status, gss_context, 0, |
| 394 |
GSS_C_QOP_DEFAULT, &gss_send_token, |
| 395 |
&gss_conf_state, &gss_w_token); |
| 396 |
|
| 397 |
if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_wrap")) { |
| 398 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 399 |
gss_release_buffer(&gss_status, &gss_w_token); |
| 400 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 401 |
failf(data, "Failed to wrap GSS-API encryption value into token."); |
| 402 |
return CURLE_COULDNT_CONNECT; |
| 403 |
} |
| 404 |
gss_release_buffer(&gss_status, &gss_send_token); |
| 405 |
|
| 406 |
us_length = htons((short)gss_w_token.length); |
| 407 |
memcpy(socksreq + 2, &us_length, sizeof(short)); |
| 408 |
} |
| 409 |
|
| 410 |
code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written); |
| 411 |
if(code || (4 != written)) { |
| 412 |
failf(data, "Failed to send GSS-API encryption request."); |
| 413 |
gss_release_buffer(&gss_status, &gss_w_token); |
| 414 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 415 |
return CURLE_COULDNT_CONNECT; |
| 416 |
} |
| 417 |
|
| 418 |
if(data->set.socks5_gssapi_nec) { |
| 419 |
memcpy(socksreq, &gss_enc, 1); |
| 420 |
code = Curl_write_plain(conn, sock, socksreq, 1, &written); |
| 421 |
if(code || ( 1 != written)) { |
| 422 |
failf(data, "Failed to send GSS-API encryption type."); |
| 423 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 424 |
return CURLE_COULDNT_CONNECT; |
| 425 |
} |
| 426 |
} |
| 427 |
else { |
| 428 |
code = Curl_write_plain(conn, sock, (char *)gss_w_token.value, |
| 429 |
gss_w_token.length, &written); |
| 430 |
if(code || ((ssize_t)gss_w_token.length != written)) { |
| 431 |
failf(data, "Failed to send GSS-API encryption type."); |
| 432 |
gss_release_buffer(&gss_status, &gss_w_token); |
| 433 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 434 |
return CURLE_COULDNT_CONNECT; |
| 435 |
} |
| 436 |
gss_release_buffer(&gss_status, &gss_w_token); |
| 437 |
} |
| 438 |
|
| 439 |
result = Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread); |
| 440 |
if(result || (actualread != 4)) { |
| 441 |
failf(data, "Failed to receive GSS-API encryption response."); |
| 442 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 443 |
return CURLE_COULDNT_CONNECT; |
| 444 |
} |
| 445 |
|
| 446 |
/* ignore the first (VER) byte */ |
| 447 |
if(socksreq[1] == 255) { /* status / message type */ |
| 448 |
failf(data, "User was rejected by the SOCKS5 server (%d %d).", |
| 449 |
socksreq[0], socksreq[1]); |
| 450 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 451 |
return CURLE_COULDNT_CONNECT; |
| 452 |
} |
| 453 |
|
| 454 |
if(socksreq[1] != 2) { /* status / messgae type */ |
| 455 |
failf(data, "Invalid GSS-API encryption response type (%d %d).", |
| 456 |
socksreq[0], socksreq[1]); |
| 457 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 458 |
return CURLE_COULDNT_CONNECT; |
| 459 |
} |
| 460 |
|
| 461 |
memcpy(&us_length, socksreq + 2, sizeof(short)); |
| 462 |
us_length = ntohs(us_length); |
| 463 |
|
| 464 |
gss_recv_token.length = us_length; |
| 465 |
gss_recv_token.value = malloc(gss_recv_token.length); |
| 466 |
if(!gss_recv_token.value) { |
| 467 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 468 |
return CURLE_OUT_OF_MEMORY; |
| 469 |
} |
| 470 |
result = Curl_blockread_all(conn, sock, (char *)gss_recv_token.value, |
| 471 |
gss_recv_token.length, &actualread); |
| 472 |
|
| 473 |
if(result || (actualread != us_length)) { |
| 474 |
failf(data, "Failed to receive GSS-API encryptrion type."); |
| 475 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 476 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 477 |
return CURLE_COULDNT_CONNECT; |
| 478 |
} |
| 479 |
|
| 480 |
if(!data->set.socks5_gssapi_nec) { |
| 481 |
gss_major_status = gss_unwrap(&gss_minor_status, gss_context, |
| 482 |
&gss_recv_token, &gss_w_token, |
| 483 |
0, GSS_C_QOP_DEFAULT); |
| 484 |
|
| 485 |
if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_unwrap")) { |
| 486 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 487 |
gss_release_buffer(&gss_status, &gss_w_token); |
| 488 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 489 |
failf(data, "Failed to unwrap GSS-API encryption value into token."); |
| 490 |
return CURLE_COULDNT_CONNECT; |
| 491 |
} |
| 492 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 493 |
|
| 494 |
if(gss_w_token.length != 1) { |
| 495 |
failf(data, "Invalid GSS-API encryption response length (%d).", |
| 496 |
gss_w_token.length); |
| 497 |
gss_release_buffer(&gss_status, &gss_w_token); |
| 498 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 499 |
return CURLE_COULDNT_CONNECT; |
| 500 |
} |
| 501 |
|
| 502 |
memcpy(socksreq, gss_w_token.value, gss_w_token.length); |
| 503 |
gss_release_buffer(&gss_status, &gss_w_token); |
| 504 |
} |
| 505 |
else { |
| 506 |
if(gss_recv_token.length != 1) { |
| 507 |
failf(data, "Invalid GSS-API encryption response length (%d).", |
| 508 |
gss_recv_token.length); |
| 509 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 510 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 511 |
return CURLE_COULDNT_CONNECT; |
| 512 |
} |
| 513 |
|
| 514 |
memcpy(socksreq, gss_recv_token.value, gss_recv_token.length); |
| 515 |
gss_release_buffer(&gss_status, &gss_recv_token); |
| 516 |
} |
| 517 |
|
| 518 |
(void)curlx_nonblock(sock, TRUE); |
| 519 |
|
| 520 |
infof(data, "SOCKS5 access with%s protection granted.\n", |
| 521 |
(socksreq[0] == 0)?"out GSS-API data": |
| 522 |
((socksreq[0] == 1)?" GSS-API integrity":" GSS-API confidentiality")); |
| 523 |
|
| 524 |
conn->socks5_gssapi_enctype = socksreq[0]; |
| 525 |
if(socksreq[0] == 0) |
| 526 |
gss_delete_sec_context(&gss_status, &gss_context, NULL); |
| 527 |
|
| 528 |
return CURLE_OK; |
| 529 |
} |
| 530 |
|
| 531 |
#endif /* HAVE_GSSAPI && !CURL_DISABLE_PROXY */ |