1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2009, 2011, Markus Moeller, <markus_moeller@compuserve.com>
9 * Copyright (C) 2012 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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 */
check_gss_err(struct Curl_easy * data,OM_uint32 major_status,OM_uint32 minor_status,const char * function)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
Curl_SOCKS5_gssapi_negotiate(int sockindex,struct connectdata * conn)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 + strlen(conn->socks_proxy.host.name)+1;
153 snprintf(service.value, service.length+1, "%s@%s",
154 serviceptr, conn->socks_proxy.host.name);
155
156 gss_major_status = gss_import_name(&gss_minor_status, &service,
157 GSS_C_NT_HOSTBASED_SERVICE, &server);
158 }
159
160 gss_release_buffer(&gss_status, &service); /* clear allocated memory */
161
162 if(check_gss_err(data, gss_major_status,
163 gss_minor_status, "gss_import_name()")) {
164 failf(data, "Failed to create service name.");
165 gss_release_name(&gss_status, &server);
166 return CURLE_COULDNT_CONNECT;
167 }
168
169 /* As long as we need to keep sending some context info, and there's no */
170 /* errors, keep sending it... */
171 for(;;) {
172 gss_major_status = Curl_gss_init_sec_context(data,
173 &gss_minor_status,
174 &gss_context,
175 server,
176 &Curl_krb5_mech_oid,
177 NULL,
178 gss_token,
179 &gss_send_token,
180 TRUE,
181 &gss_ret_flags);
182
183 if(gss_token != GSS_C_NO_BUFFER)
184 gss_release_buffer(&gss_status, &gss_recv_token);
185 if(check_gss_err(data, gss_major_status,
186 gss_minor_status, "gss_init_sec_context")) {
187 gss_release_name(&gss_status, &server);
188 gss_release_buffer(&gss_status, &gss_recv_token);
189 gss_release_buffer(&gss_status, &gss_send_token);
190 gss_delete_sec_context(&gss_status, &gss_context, NULL);
191 failf(data, "Failed to initial GSS-API token.");
192 return CURLE_COULDNT_CONNECT;
193 }
194
195 if(gss_send_token.length != 0) {
196 socksreq[0] = 1; /* GSS-API subnegotiation version */
197 socksreq[1] = 1; /* authentication message type */
198 us_length = htons((short)gss_send_token.length);
199 memcpy(socksreq+2, &us_length, sizeof(short));
200
201 code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
202 if(code || (4 != written)) {
203 failf(data, "Failed to send GSS-API authentication request.");
204 gss_release_name(&gss_status, &server);
205 gss_release_buffer(&gss_status, &gss_recv_token);
206 gss_release_buffer(&gss_status, &gss_send_token);
207 gss_delete_sec_context(&gss_status, &gss_context, NULL);
208 return CURLE_COULDNT_CONNECT;
209 }
210
211 code = Curl_write_plain(conn, sock, (char *)gss_send_token.value,
212 gss_send_token.length, &written);
213
214 if(code || ((ssize_t)gss_send_token.length != written)) {
215 failf(data, "Failed to send GSS-API authentication token.");
216 gss_release_name(&gss_status, &server);
217 gss_release_buffer(&gss_status, &gss_recv_token);
218 gss_release_buffer(&gss_status, &gss_send_token);
219 gss_delete_sec_context(&gss_status, &gss_context, NULL);
220 return CURLE_COULDNT_CONNECT;
221 }
222
223 }
224
225 gss_release_buffer(&gss_status, &gss_send_token);
226 gss_release_buffer(&gss_status, &gss_recv_token);
227 if(gss_major_status != GSS_S_CONTINUE_NEEDED) break;
228
229 /* analyse response */
230
231 /* GSS-API response looks like
232 * +----+------+-----+----------------+
233 * |VER | MTYP | LEN | TOKEN |
234 * +----+------+----------------------+
235 * | 1 | 1 | 2 | up to 2^16 - 1 |
236 * +----+------+-----+----------------+
237 */
238
239 result=Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
240 if(result || (actualread != 4)) {
241 failf(data, "Failed to receive GSS-API authentication response.");
242 gss_release_name(&gss_status, &server);
243 gss_delete_sec_context(&gss_status, &gss_context, NULL);
244 return CURLE_COULDNT_CONNECT;
245 }
246
247 /* ignore the first (VER) byte */
248 if(socksreq[1] == 255) { /* status / message type */
249 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
250 socksreq[0], socksreq[1]);
251 gss_release_name(&gss_status, &server);
252 gss_delete_sec_context(&gss_status, &gss_context, NULL);
253 return CURLE_COULDNT_CONNECT;
254 }
255
256 if(socksreq[1] != 1) { /* status / messgae type */
257 failf(data, "Invalid GSS-API authentication response type (%d %d).",
258 socksreq[0], socksreq[1]);
259 gss_release_name(&gss_status, &server);
260 gss_delete_sec_context(&gss_status, &gss_context, NULL);
261 return CURLE_COULDNT_CONNECT;
262 }
263
264 memcpy(&us_length, socksreq+2, sizeof(short));
265 us_length = ntohs(us_length);
266
267 gss_recv_token.length=us_length;
268 gss_recv_token.value=malloc(us_length);
269 if(!gss_recv_token.value) {
270 failf(data,
271 "Could not allocate memory for GSS-API authentication "
272 "response token.");
273 gss_release_name(&gss_status, &server);
274 gss_delete_sec_context(&gss_status, &gss_context, NULL);
275 return CURLE_OUT_OF_MEMORY;
276 }
277
278 result=Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
279 gss_recv_token.length, &actualread);
280
281 if(result || (actualread != us_length)) {
282 failf(data, "Failed to receive GSS-API authentication token.");
283 gss_release_name(&gss_status, &server);
284 gss_release_buffer(&gss_status, &gss_recv_token);
285 gss_delete_sec_context(&gss_status, &gss_context, NULL);
286 return CURLE_COULDNT_CONNECT;
287 }
288
289 gss_token = &gss_recv_token;
290 }
291
292 gss_release_name(&gss_status, &server);
293
294 /* Everything is good so far, user was authenticated! */
295 gss_major_status = gss_inquire_context (&gss_minor_status, gss_context,
296 &gss_client_name, NULL, NULL, NULL,
297 NULL, NULL, NULL);
298 if(check_gss_err(data, gss_major_status,
299 gss_minor_status, "gss_inquire_context")) {
300 gss_delete_sec_context(&gss_status, &gss_context, NULL);
301 gss_release_name(&gss_status, &gss_client_name);
302 failf(data, "Failed to determine user name.");
303 return CURLE_COULDNT_CONNECT;
304 }
305 gss_major_status = gss_display_name(&gss_minor_status, gss_client_name,
306 &gss_send_token, NULL);
307 if(check_gss_err(data, gss_major_status,
308 gss_minor_status, "gss_display_name")) {
309 gss_delete_sec_context(&gss_status, &gss_context, NULL);
310 gss_release_name(&gss_status, &gss_client_name);
311 gss_release_buffer(&gss_status, &gss_send_token);
312 failf(data, "Failed to determine user name.");
313 return CURLE_COULDNT_CONNECT;
314 }
315 user=malloc(gss_send_token.length+1);
316 if(!user) {
317 gss_delete_sec_context(&gss_status, &gss_context, NULL);
318 gss_release_name(&gss_status, &gss_client_name);
319 gss_release_buffer(&gss_status, &gss_send_token);
320 return CURLE_OUT_OF_MEMORY;
321 }
322
323 memcpy(user, gss_send_token.value, gss_send_token.length);
324 user[gss_send_token.length] = '\0';
325 gss_release_name(&gss_status, &gss_client_name);
326 gss_release_buffer(&gss_status, &gss_send_token);
327 infof(data, "SOCKS5 server authencticated user %s with GSS-API.\n",user);
328 free(user);
329 user=NULL;
330
331 /* Do encryption */
332 socksreq[0] = 1; /* GSS-API subnegotiation version */
333 socksreq[1] = 2; /* encryption message type */
334
335 gss_enc = 0; /* no data protection */
336 /* do confidentiality protection if supported */
337 if(gss_ret_flags & GSS_C_CONF_FLAG)
338 gss_enc = 2;
339 /* else do integrity protection */
340 else if(gss_ret_flags & GSS_C_INTEG_FLAG)
341 gss_enc = 1;
342
343 infof(data, "SOCKS5 server supports GSS-API %s data protection.\n",
344 (gss_enc==0)?"no":((gss_enc==1)?"integrity":"confidentiality"));
345 /* force for the moment to no data protection */
346 gss_enc = 0;
347 /*
348 * Sending the encryption type in clear seems wrong. It should be
349 * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
350 * The NEC reference implementations on which this is based is
351 * therefore at fault
352 *
353 * +------+------+------+.......................+
354 * + ver | mtyp | len | token |
355 * +------+------+------+.......................+
356 * + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
357 * +------+------+------+.......................+
358 *
359 * Where:
360 *
361 * - "ver" is the protocol version number, here 1 to represent the
362 * first version of the SOCKS/GSS-API protocol
363 *
364 * - "mtyp" is the message type, here 2 to represent a protection
365 * -level negotiation message
366 *
367 * - "len" is the length of the "token" field in octets
368 *
369 * - "token" is the GSS-API encapsulated protection level
370 *
371 * The token is produced by encapsulating an octet containing the
372 * required protection level using gss_seal()/gss_wrap() with conf_req
373 * set to FALSE. The token is verified using gss_unseal()/
374 * gss_unwrap().
375 *
376 */
377 if(data->set.socks5_gssapi_nec) {
378 us_length = htons((short)1);
379 memcpy(socksreq+2, &us_length, sizeof(short));
380 }
381 else {
382 gss_send_token.length = 1;
383 gss_send_token.value = malloc(1);
384 if(!gss_send_token.value) {
385 gss_delete_sec_context(&gss_status, &gss_context, NULL);
386 return CURLE_OUT_OF_MEMORY;
387 }
388 memcpy(gss_send_token.value, &gss_enc, 1);
389
390 gss_major_status = gss_wrap(&gss_minor_status, gss_context, 0,
391 GSS_C_QOP_DEFAULT, &gss_send_token,
392 &gss_conf_state, &gss_w_token);
393
394 if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_wrap")) {
395 gss_release_buffer(&gss_status, &gss_send_token);
396 gss_release_buffer(&gss_status, &gss_w_token);
397 gss_delete_sec_context(&gss_status, &gss_context, NULL);
398 failf(data, "Failed to wrap GSS-API encryption value into token.");
399 return CURLE_COULDNT_CONNECT;
400 }
401 gss_release_buffer(&gss_status, &gss_send_token);
402
403 us_length = htons((short)gss_w_token.length);
404 memcpy(socksreq+2, &us_length, sizeof(short));
405 }
406
407 code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
408 if(code || (4 != written)) {
409 failf(data, "Failed to send GSS-API encryption request.");
410 gss_release_buffer(&gss_status, &gss_w_token);
411 gss_delete_sec_context(&gss_status, &gss_context, NULL);
412 return CURLE_COULDNT_CONNECT;
413 }
414
415 if(data->set.socks5_gssapi_nec) {
416 memcpy(socksreq, &gss_enc, 1);
417 code = Curl_write_plain(conn, sock, socksreq, 1, &written);
418 if(code || ( 1 != written)) {
419 failf(data, "Failed to send GSS-API encryption type.");
420 gss_delete_sec_context(&gss_status, &gss_context, NULL);
421 return CURLE_COULDNT_CONNECT;
422 }
423 }
424 else {
425 code = Curl_write_plain(conn, sock, (char *)gss_w_token.value,
426 gss_w_token.length, &written);
427 if(code || ((ssize_t)gss_w_token.length != written)) {
428 failf(data, "Failed to send GSS-API encryption type.");
429 gss_release_buffer(&gss_status, &gss_w_token);
430 gss_delete_sec_context(&gss_status, &gss_context, NULL);
431 return CURLE_COULDNT_CONNECT;
432 }
433 gss_release_buffer(&gss_status, &gss_w_token);
434 }
435
436 result=Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
437 if(result || (actualread != 4)) {
438 failf(data, "Failed to receive GSS-API encryption response.");
439 gss_delete_sec_context(&gss_status, &gss_context, NULL);
440 return CURLE_COULDNT_CONNECT;
441 }
442
443 /* ignore the first (VER) byte */
444 if(socksreq[1] == 255) { /* status / message type */
445 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
446 socksreq[0], socksreq[1]);
447 gss_delete_sec_context(&gss_status, &gss_context, NULL);
448 return CURLE_COULDNT_CONNECT;
449 }
450
451 if(socksreq[1] != 2) { /* status / messgae type */
452 failf(data, "Invalid GSS-API encryption response type (%d %d).",
453 socksreq[0], socksreq[1]);
454 gss_delete_sec_context(&gss_status, &gss_context, NULL);
455 return CURLE_COULDNT_CONNECT;
456 }
457
458 memcpy(&us_length, socksreq+2, sizeof(short));
459 us_length = ntohs(us_length);
460
461 gss_recv_token.length= us_length;
462 gss_recv_token.value=malloc(gss_recv_token.length);
463 if(!gss_recv_token.value) {
464 gss_delete_sec_context(&gss_status, &gss_context, NULL);
465 return CURLE_OUT_OF_MEMORY;
466 }
467 result=Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
468 gss_recv_token.length, &actualread);
469
470 if(result || (actualread != us_length)) {
471 failf(data, "Failed to receive GSS-API encryptrion type.");
472 gss_release_buffer(&gss_status, &gss_recv_token);
473 gss_delete_sec_context(&gss_status, &gss_context, NULL);
474 return CURLE_COULDNT_CONNECT;
475 }
476
477 if(!data->set.socks5_gssapi_nec) {
478 gss_major_status = gss_unwrap(&gss_minor_status, gss_context,
479 &gss_recv_token, &gss_w_token,
480 0, GSS_C_QOP_DEFAULT);
481
482 if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_unwrap")) {
483 gss_release_buffer(&gss_status, &gss_recv_token);
484 gss_release_buffer(&gss_status, &gss_w_token);
485 gss_delete_sec_context(&gss_status, &gss_context, NULL);
486 failf(data, "Failed to unwrap GSS-API encryption value into token.");
487 return CURLE_COULDNT_CONNECT;
488 }
489 gss_release_buffer(&gss_status, &gss_recv_token);
490
491 if(gss_w_token.length != 1) {
492 failf(data, "Invalid GSS-API encryption response length (%d).",
493 gss_w_token.length);
494 gss_release_buffer(&gss_status, &gss_w_token);
495 gss_delete_sec_context(&gss_status, &gss_context, NULL);
496 return CURLE_COULDNT_CONNECT;
497 }
498
499 memcpy(socksreq, gss_w_token.value, gss_w_token.length);
500 gss_release_buffer(&gss_status, &gss_w_token);
501 }
502 else {
503 if(gss_recv_token.length != 1) {
504 failf(data, "Invalid GSS-API encryption response length (%d).",
505 gss_recv_token.length);
506 gss_release_buffer(&gss_status, &gss_recv_token);
507 gss_delete_sec_context(&gss_status, &gss_context, NULL);
508 return CURLE_COULDNT_CONNECT;
509 }
510
511 memcpy(socksreq, gss_recv_token.value, gss_recv_token.length);
512 gss_release_buffer(&gss_status, &gss_recv_token);
513 }
514
515 infof(data, "SOCKS5 access with%s protection granted.\n",
516 (socksreq[0]==0)?"out GSS-API data":
517 ((socksreq[0]==1)?" GSS-API integrity":" GSS-API confidentiality"));
518
519 conn->socks5_gssapi_enctype = socksreq[0];
520 if(socksreq[0] == 0)
521 gss_delete_sec_context(&gss_status, &gss_context, NULL);
522
523 return CURLE_OK;
524 }
525
526 #endif /* HAVE_GSSAPI && !CURL_DISABLE_PROXY */
527