1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
9 * Copyright (C) 2009, Markus Moeller, <markus_moeller@compuserve.com>
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.
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.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ***************************************************************************/
24 #include "curl_setup.h"
26 #if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_PROXY)
28 #include "curl_gssapi.h"
36 /* The last 3 #include files should be in this order */
37 #include "curl_printf.h"
38 #include "curl_memory.h"
41 static gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
44 * Helper GSS-API error functions.
46 static int check_gss_err(struct Curl_easy *data,
47 OM_uint32 major_status,
48 OM_uint32 minor_status,
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;
61 /* convert major status code (GSS-API error) to text */
62 maj_stat = gss_display_status(&min_stat, major_status,
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;
71 gss_release_buffer(&min_stat, &status_string);
74 gss_release_buffer(&min_stat, &status_string);
76 if(sizeof(buf) > len + 3) {
77 strcpy(buf + len, ".\n");
82 /* convert minor status code (underlying routine error) to text */
83 maj_stat = gss_display_status(&min_stat, minor_status,
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);
93 gss_release_buffer(&min_stat, &status_string);
95 failf(data, "GSS-API error: %s failed:\n%s", function, buf);
102 CURLcode Curl_SOCKS5_gssapi_negotiate(int sockindex,
103 struct connectdata *conn)
105 struct Curl_easy *data = conn->data;
106 curl_socket_t sock = conn->sock[sockindex];
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;
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);
128 /* GSS-API request looks like
129 * +----+------+-----+----------------+
130 * |VER | MTYP | LEN | TOKEN |
131 * +----+------+----------------------+
132 * | 1 | 1 | 2 | up to 2^16 - 1 |
133 * +----+------+-----+----------------+
136 /* prepare service name */
137 if(strchr(serviceptr, '/')) {
138 service.length = serviceptr_length;
139 service.value = malloc(service.length);
141 return CURLE_OUT_OF_MEMORY;
142 memcpy(service.value, serviceptr, service.length);
144 gss_major_status = gss_import_name(&gss_minor_status, &service,
145 (gss_OID) GSS_C_NULL_OID, &server);
148 service.value = malloc(serviceptr_length +
149 strlen(conn->socks_proxy.host.name) + 2);
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);
157 gss_major_status = gss_import_name(&gss_minor_status, &service,
158 GSS_C_NT_HOSTBASED_SERVICE, &server);
161 gss_release_buffer(&gss_status, &service); /* clear allocated memory */
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;
170 (void)curlx_nonblock(sock, FALSE);
172 /* As long as we need to keep sending some context info, and there's no */
173 /* errors, keep sending it... */
175 gss_major_status = Curl_gss_init_sec_context(data,
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;
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));
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;
214 code = Curl_write_plain(conn, sock, (char *)gss_send_token.value,
215 gss_send_token.length, &written);
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;
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)
233 /* analyse response */
235 /* GSS-API response looks like
236 * +----+------+-----+----------------+
237 * |VER | MTYP | LEN | TOKEN |
238 * +----+------+----------------------+
239 * | 1 | 1 | 2 | up to 2^16 - 1 |
240 * +----+------+-----+----------------+
243 result = Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
244 if(result || (actualread != 4)) {
245 failf(data, "Failed to receive GSS-API authentication response.");
246 gss_release_name(&gss_status, &server);
247 gss_delete_sec_context(&gss_status, &gss_context, NULL);
248 return CURLE_COULDNT_CONNECT;
251 /* ignore the first (VER) byte */
252 if(socksreq[1] == 255) { /* status / message type */
253 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
254 socksreq[0], socksreq[1]);
255 gss_release_name(&gss_status, &server);
256 gss_delete_sec_context(&gss_status, &gss_context, NULL);
257 return CURLE_COULDNT_CONNECT;
260 if(socksreq[1] != 1) { /* status / messgae type */
261 failf(data, "Invalid GSS-API authentication response type (%d %d).",
262 socksreq[0], socksreq[1]);
263 gss_release_name(&gss_status, &server);
264 gss_delete_sec_context(&gss_status, &gss_context, NULL);
265 return CURLE_COULDNT_CONNECT;
268 memcpy(&us_length, socksreq + 2, sizeof(short));
269 us_length = ntohs(us_length);
271 gss_recv_token.length = us_length;
272 gss_recv_token.value = malloc(us_length);
273 if(!gss_recv_token.value) {
275 "Could not allocate memory for GSS-API authentication "
277 gss_release_name(&gss_status, &server);
278 gss_delete_sec_context(&gss_status, &gss_context, NULL);
279 return CURLE_OUT_OF_MEMORY;
282 result = Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
283 gss_recv_token.length, &actualread);
285 if(result || (actualread != us_length)) {
286 failf(data, "Failed to receive GSS-API authentication token.");
287 gss_release_name(&gss_status, &server);
288 gss_release_buffer(&gss_status, &gss_recv_token);
289 gss_delete_sec_context(&gss_status, &gss_context, NULL);
290 return CURLE_COULDNT_CONNECT;
293 gss_token = &gss_recv_token;
296 gss_release_name(&gss_status, &server);
298 /* Everything is good so far, user was authenticated! */
299 gss_major_status = gss_inquire_context(&gss_minor_status, gss_context,
300 &gss_client_name, NULL, NULL, NULL,
302 if(check_gss_err(data, gss_major_status,
303 gss_minor_status, "gss_inquire_context")) {
304 gss_delete_sec_context(&gss_status, &gss_context, NULL);
305 gss_release_name(&gss_status, &gss_client_name);
306 failf(data, "Failed to determine user name.");
307 return CURLE_COULDNT_CONNECT;
309 gss_major_status = gss_display_name(&gss_minor_status, gss_client_name,
310 &gss_send_token, NULL);
311 if(check_gss_err(data, gss_major_status,
312 gss_minor_status, "gss_display_name")) {
313 gss_delete_sec_context(&gss_status, &gss_context, NULL);
314 gss_release_name(&gss_status, &gss_client_name);
315 gss_release_buffer(&gss_status, &gss_send_token);
316 failf(data, "Failed to determine user name.");
317 return CURLE_COULDNT_CONNECT;
319 user = malloc(gss_send_token.length + 1);
321 gss_delete_sec_context(&gss_status, &gss_context, NULL);
322 gss_release_name(&gss_status, &gss_client_name);
323 gss_release_buffer(&gss_status, &gss_send_token);
324 return CURLE_OUT_OF_MEMORY;
327 memcpy(user, gss_send_token.value, gss_send_token.length);
328 user[gss_send_token.length] = '\0';
329 gss_release_name(&gss_status, &gss_client_name);
330 gss_release_buffer(&gss_status, &gss_send_token);
331 infof(data, "SOCKS5 server authenticated user %s with GSS-API.\n",user);
336 socksreq[0] = 1; /* GSS-API subnegotiation version */
337 socksreq[1] = 2; /* encryption message type */
339 gss_enc = 0; /* no data protection */
340 /* do confidentiality protection if supported */
341 if(gss_ret_flags & GSS_C_CONF_FLAG)
343 /* else do integrity protection */
344 else if(gss_ret_flags & GSS_C_INTEG_FLAG)
347 infof(data, "SOCKS5 server supports GSS-API %s data protection.\n",
348 (gss_enc == 0)?"no":((gss_enc==1)?"integrity":"confidentiality"));
349 /* force for the moment to no data protection */
352 * Sending the encryption type in clear seems wrong. It should be
353 * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
354 * The NEC reference implementations on which this is based is
357 * +------+------+------+.......................+
358 * + ver | mtyp | len | token |
359 * +------+------+------+.......................+
360 * + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
361 * +------+------+------+.......................+
365 * - "ver" is the protocol version number, here 1 to represent the
366 * first version of the SOCKS/GSS-API protocol
368 * - "mtyp" is the message type, here 2 to represent a protection
369 * -level negotiation message
371 * - "len" is the length of the "token" field in octets
373 * - "token" is the GSS-API encapsulated protection level
375 * The token is produced by encapsulating an octet containing the
376 * required protection level using gss_seal()/gss_wrap() with conf_req
377 * set to FALSE. The token is verified using gss_unseal()/
381 if(data->set.socks5_gssapi_nec) {
382 us_length = htons((short)1);
383 memcpy(socksreq + 2, &us_length, sizeof(short));
386 gss_send_token.length = 1;
387 gss_send_token.value = malloc(1);
388 if(!gss_send_token.value) {
389 gss_delete_sec_context(&gss_status, &gss_context, NULL);
390 return CURLE_OUT_OF_MEMORY;
392 memcpy(gss_send_token.value, &gss_enc, 1);
394 gss_major_status = gss_wrap(&gss_minor_status, gss_context, 0,
395 GSS_C_QOP_DEFAULT, &gss_send_token,
396 &gss_conf_state, &gss_w_token);
398 if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_wrap")) {
399 gss_release_buffer(&gss_status, &gss_send_token);
400 gss_release_buffer(&gss_status, &gss_w_token);
401 gss_delete_sec_context(&gss_status, &gss_context, NULL);
402 failf(data, "Failed to wrap GSS-API encryption value into token.");
403 return CURLE_COULDNT_CONNECT;
405 gss_release_buffer(&gss_status, &gss_send_token);
407 us_length = htons((short)gss_w_token.length);
408 memcpy(socksreq + 2, &us_length, sizeof(short));
411 code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
412 if(code || (4 != written)) {
413 failf(data, "Failed to send GSS-API encryption request.");
414 gss_release_buffer(&gss_status, &gss_w_token);
415 gss_delete_sec_context(&gss_status, &gss_context, NULL);
416 return CURLE_COULDNT_CONNECT;
419 if(data->set.socks5_gssapi_nec) {
420 memcpy(socksreq, &gss_enc, 1);
421 code = Curl_write_plain(conn, sock, socksreq, 1, &written);
422 if(code || ( 1 != written)) {
423 failf(data, "Failed to send GSS-API encryption type.");
424 gss_delete_sec_context(&gss_status, &gss_context, NULL);
425 return CURLE_COULDNT_CONNECT;
429 code = Curl_write_plain(conn, sock, (char *)gss_w_token.value,
430 gss_w_token.length, &written);
431 if(code || ((ssize_t)gss_w_token.length != written)) {
432 failf(data, "Failed to send GSS-API encryption type.");
433 gss_release_buffer(&gss_status, &gss_w_token);
434 gss_delete_sec_context(&gss_status, &gss_context, NULL);
435 return CURLE_COULDNT_CONNECT;
437 gss_release_buffer(&gss_status, &gss_w_token);
440 result = Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
441 if(result || (actualread != 4)) {
442 failf(data, "Failed to receive GSS-API encryption response.");
443 gss_delete_sec_context(&gss_status, &gss_context, NULL);
444 return CURLE_COULDNT_CONNECT;
447 /* ignore the first (VER) byte */
448 if(socksreq[1] == 255) { /* status / message type */
449 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
450 socksreq[0], socksreq[1]);
451 gss_delete_sec_context(&gss_status, &gss_context, NULL);
452 return CURLE_COULDNT_CONNECT;
455 if(socksreq[1] != 2) { /* status / messgae type */
456 failf(data, "Invalid GSS-API encryption response type (%d %d).",
457 socksreq[0], socksreq[1]);
458 gss_delete_sec_context(&gss_status, &gss_context, NULL);
459 return CURLE_COULDNT_CONNECT;
462 memcpy(&us_length, socksreq + 2, sizeof(short));
463 us_length = ntohs(us_length);
465 gss_recv_token.length = us_length;
466 gss_recv_token.value = malloc(gss_recv_token.length);
467 if(!gss_recv_token.value) {
468 gss_delete_sec_context(&gss_status, &gss_context, NULL);
469 return CURLE_OUT_OF_MEMORY;
471 result = Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
472 gss_recv_token.length, &actualread);
474 if(result || (actualread != us_length)) {
475 failf(data, "Failed to receive GSS-API encryptrion type.");
476 gss_release_buffer(&gss_status, &gss_recv_token);
477 gss_delete_sec_context(&gss_status, &gss_context, NULL);
478 return CURLE_COULDNT_CONNECT;
481 if(!data->set.socks5_gssapi_nec) {
482 gss_major_status = gss_unwrap(&gss_minor_status, gss_context,
483 &gss_recv_token, &gss_w_token,
484 0, GSS_C_QOP_DEFAULT);
486 if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_unwrap")) {
487 gss_release_buffer(&gss_status, &gss_recv_token);
488 gss_release_buffer(&gss_status, &gss_w_token);
489 gss_delete_sec_context(&gss_status, &gss_context, NULL);
490 failf(data, "Failed to unwrap GSS-API encryption value into token.");
491 return CURLE_COULDNT_CONNECT;
493 gss_release_buffer(&gss_status, &gss_recv_token);
495 if(gss_w_token.length != 1) {
496 failf(data, "Invalid GSS-API encryption response length (%d).",
498 gss_release_buffer(&gss_status, &gss_w_token);
499 gss_delete_sec_context(&gss_status, &gss_context, NULL);
500 return CURLE_COULDNT_CONNECT;
503 memcpy(socksreq, gss_w_token.value, gss_w_token.length);
504 gss_release_buffer(&gss_status, &gss_w_token);
507 if(gss_recv_token.length != 1) {
508 failf(data, "Invalid GSS-API encryption response length (%d).",
509 gss_recv_token.length);
510 gss_release_buffer(&gss_status, &gss_recv_token);
511 gss_delete_sec_context(&gss_status, &gss_context, NULL);
512 return CURLE_COULDNT_CONNECT;
515 memcpy(socksreq, gss_recv_token.value, gss_recv_token.length);
516 gss_release_buffer(&gss_status, &gss_recv_token);
519 (void)curlx_nonblock(sock, TRUE);
521 infof(data, "SOCKS5 access with%s protection granted.\n",
522 (socksreq[0] == 0)?"out GSS-API data":
523 ((socksreq[0] == 1)?" GSS-API integrity":" GSS-API confidentiality"));
525 conn->socks5_gssapi_enctype = socksreq[0];
527 gss_delete_sec_context(&gss_status, &gss_context, NULL);
532 #endif /* HAVE_GSSAPI && !CURL_DISABLE_PROXY */