OpenVPN
socks.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23/*
24 * 2004-01-30: Added Socks5 proxy support, see RFC 1928
25 * (Christof Meerwald, https://cmeerw.org)
26 *
27 * 2010-10-10: Added Socks5 plain text authentication support (RFC 1929)
28 * (Pierre Bourdon <delroth@gmail.com>)
29 */
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include "syshead.h"
36
37#include "socks.h"
38#include "common.h"
39#include "misc.h"
40#include "win32.h"
41#include "socket.h"
42#include "fdmisc.h"
43#include "misc.h"
44#include "proxy.h"
45#include "forward.h"
46
47#include "memdbg.h"
48
49#define UP_TYPE_SOCKS "SOCKS Proxy"
50
51struct socks_proxy_info *
52socks_proxy_new(const char *server, const char *port, const char *authfile)
53{
54 struct socks_proxy_info *p;
55
57
59 ASSERT(port);
60
61 strlcpy(p->server, server, sizeof(p->server));
62 p->port = port;
63
64 if (authfile)
65 {
66 strlcpy(p->authfile, authfile, sizeof(p->authfile));
67 }
68 else
69 {
70 p->authfile[0] = 0;
71 }
72
73 p->defined = true;
74
75 return p;
76}
77
78void
80{
81 free(sp);
82}
83
84static bool
85socks_proxy_recv_char(uint8_t *c, const char *name, socket_descriptor_t sd,
86 const struct event_timeout *server_poll_timeout,
87 volatile int *signal_received)
88{
89 struct timeval tv;
90 tv.tv_sec = get_server_poll_remaining_time(server_poll_timeout);
91 tv.tv_usec = 0;
92
93 return proxy_recv_char(c, name, sd, &tv, signal_received);
94}
95
96static bool
98 const struct event_timeout *server_poll_timeout,
99 volatile int *signal_received)
100{
101 char to_send[516];
102 struct user_pass creds;
103 bool ret = false;
104
105 CLEAR(creds);
107 {
108 msg(M_NONFATAL, "SOCKS failed to get username/password.");
109 goto cleanup;
110 }
111
112 if ((strlen(creds.username) > 255) || (strlen(creds.password) > 255))
113 {
114 msg(M_NONFATAL, "SOCKS username and/or password exceeds 255 characters. "
115 "Authentication not possible.");
116 goto cleanup;
117 }
118
119 ASSERT(checked_snprintf(to_send, sizeof(to_send), "\x01%c%s%c%s",
120 (int)strlen(creds.username), creds.username,
121 (int)strlen(creds.password), creds.password));
122 if (!proxy_send(sd, to_send, strlen(to_send)))
123 {
124 goto cleanup;
125 }
126
127 int len = 0;
128 uint8_t buf[2];
129 while (len < 2)
130 {
131 uint8_t c;
132
133 if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, signal_received))
134 {
135 goto cleanup;
136 }
137 /* store char in buffer */
138 buf[len++] = c;
139 }
140
141 /* VER = 5, SUCCESS = 0 --> auth success */
142 if (buf[0] != 5 || buf[1] != 0)
143 {
144 msg(D_LINK_ERRORS, "socks_username_password_auth: server refused the authentication");
145 goto cleanup;
146 }
147
148 ret = true;
149cleanup:
150 secure_memzero(&creds, sizeof(creds));
151 secure_memzero(to_send, sizeof(to_send));
152 return ret;
153}
154
155static bool
157 const struct event_timeout *server_poll_timeout, volatile int *signal_received)
158{
159 uint8_t buf[2];
160 int len = 0;
161
162 /* VER = 5, NMETHODS = 1, METHODS = [0 (no auth)] */
163 uint8_t method_sel[3] = { 0x05, 0x01, 0x00 };
164 if (p->authfile[0])
165 {
166 method_sel[2] = 0x02; /* METHODS = [2 (plain login)] */
167 }
168 if (!proxy_send(sd, method_sel, sizeof(method_sel)))
169 {
170 return false;
171 }
172
173 while (len < 2)
174 {
175 uint8_t c;
176
177 if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, signal_received))
178 {
179 return false;
180 }
181 /* store char in buffer */
182 buf[len++] = c;
183 }
184
185 /* VER == 5 */
186 if (buf[0] != 5)
187 {
188 msg(D_LINK_ERRORS, "socks_handshake: Socks proxy returned bad status");
189 return false;
190 }
191
192 /* validate that the auth method returned is the one sent */
193 if (buf[1] != method_sel[2])
194 {
195 msg(D_LINK_ERRORS, "socks_handshake: Socks proxy returned unexpected auth");
196 return false;
197 }
198
199 /* select the appropriate authentication method */
200 switch (buf[1])
201 {
202 case 0: /* no authentication */
203 break;
204
205 case 2: /* login/password */
206 if (!p->authfile[0])
207 {
209 "socks_handshake: server asked for username/login auth but we were "
210 "not provided any credentials");
211 return false;
212 }
213
214 if (!socks_username_password_auth(p, sd, server_poll_timeout, signal_received))
215 {
216 return false;
217 }
218
219 break;
220
221 default: /* unknown auth method */
222 msg(D_LINK_ERRORS, "socks_handshake: unknown SOCKS auth method");
223 return false;
224 }
225
226 return true;
227}
228
229#if defined(__GNUC__) || defined(__clang__)
230#pragma GCC diagnostic push
231#pragma GCC diagnostic ignored "-Wsign-conversion"
232#endif
233
234static bool
236 const struct event_timeout *server_poll_timeout, volatile int *signal_received)
237{
238 uint8_t atyp = 0;
239 int alen = 0;
240 int len = 0;
241 char buf[270]; /* 4 + alen(max 256) + 2 */
242
243 if (addr != NULL)
244 {
245 addr->addr.in4.sin_family = AF_INET;
246 addr->addr.in4.sin_addr.s_addr = htonl(INADDR_ANY);
247 addr->addr.in4.sin_port = htons(0);
248 }
249
250 while (len < 4 + alen + 2)
251 {
252 uint8_t c;
253
254 if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, signal_received))
255 {
256 return false;
257 }
258
259 if (len == 3)
260 {
261 atyp = c;
262 }
263
264 if (len == 4)
265 {
266 switch (atyp)
267 {
268 case 1: /* IP V4 */
269 alen = 4;
270 break;
271
272 case 3: /* DOMAINNAME */
273 /* RFC 1928, section 5: 1 byte length, <n> bytes name,
274 * so the total "address length" is (length+1)
275 */
276 alen = c + 1;
277 break;
278
279 case 4: /* IP V6 */
280 alen = 16;
281 break;
282
283 default:
284 msg(D_LINK_ERRORS, "recv_socks_reply: Socks proxy returned bad address type");
285 return false;
286 }
287 }
288
289 /* store char in buffer */
290 if (len < (int)sizeof(buf))
291 {
292 buf[len] = c;
293 }
294 ++len;
295 }
296
297 /* VER == 5 && REP == 0 (succeeded) */
298 if (buf[0] != 5 || buf[1] != 0)
299 {
300 msg(D_LINK_ERRORS, "recv_socks_reply: Socks proxy returned bad reply");
301 return false;
302 }
303
304 /* ATYP == 1 (IP V4 address) */
305 if (atyp == 1 && addr != NULL)
306 {
307 memcpy(&addr->addr.in4.sin_addr, buf + 4, sizeof(addr->addr.in4.sin_addr));
308 memcpy(&addr->addr.in4.sin_port, buf + 8, sizeof(addr->addr.in4.sin_port));
309 struct gc_arena gc = gc_new();
310 msg(M_INFO, "SOCKS proxy wants us to send UDP to %s", print_openvpn_sockaddr(addr, &gc));
311 gc_free(&gc);
312 }
313
314
315 return true;
316}
317
318#if defined(__GNUC__) || defined(__clang__)
319#pragma GCC diagnostic pop
320#endif
321
322static int
323port_from_servname(const char *servname)
324{
325 int port = 0;
326 port = atoi(servname);
327 if (port > 0 && port < 65536)
328 {
329 return port;
330 }
331
332 const struct servent *service = getservbyname(servname, NULL);
333 if (service)
334 {
335 return service->s_port;
336 }
337
338 return 0;
339}
340
341void
343 socket_descriptor_t sd, /* already open to proxy */
344 const char *host, /* openvpn server remote */
345 const char *servname, /* openvpn server port */
346 const struct event_timeout *server_poll_timeout,
347 struct signal_info *sig_info)
348{
349 char buf[270];
350 size_t len;
351
352 if (!socks_handshake(p, sd, server_poll_timeout, &sig_info->signal_received))
353 {
354 goto error;
355 }
356
357 /* format Socks CONNECT message */
358 buf[0] = '\x05'; /* VER = 5 */
359 buf[1] = '\x01'; /* CMD = 1 (CONNECT) */
360 buf[2] = '\x00'; /* RSV */
361 buf[3] = '\x03'; /* ATYP = 3 (DOMAINNAME) */
362
363 len = strlen(host);
364 len = (5 + len + 2 > sizeof(buf)) ? (sizeof(buf) - 5 - 2) : len;
365
366 buf[4] = (char)len;
367 memcpy(buf + 5, host, len);
368
369 int port = port_from_servname(servname);
370 if (port == 0)
371 {
372 msg(D_LINK_ERRORS, "establish_socks_proxy_passthrough: Cannot convert %s to port number",
373 servname);
374 goto error;
375 }
376
377 buf[5 + len] = (char)(port >> 8);
378 buf[5 + len + 1] = (char)(port & 0xff);
379
380 if (!proxy_send(sd, buf, 5 + len + 2))
381 {
382 goto error;
383 }
384
385 /* receive reply from Socks proxy and discard */
386 if (!recv_socks_reply(sd, NULL, server_poll_timeout, &sig_info->signal_received))
387 {
388 goto error;
389 }
390
391 return;
392
393error:
394 /* SOFT-SIGUSR1 -- socks error */
395 register_signal(sig_info, SIGUSR1, "socks-error");
396 return;
397}
398
399void
401 socket_descriptor_t ctrl_sd, /* already open to proxy */
402 struct openvpn_sockaddr *relay_addr,
403 const struct event_timeout *server_poll_timeout,
404 struct signal_info *sig_info)
405{
406 if (!socks_handshake(p, ctrl_sd, server_poll_timeout, &sig_info->signal_received))
407 {
408 goto error;
409 }
410
411 {
412 /* send Socks UDP ASSOCIATE message */
413 /* VER = 5, CMD = 3 (UDP ASSOCIATE), RSV = 0, ATYP = 1 (IP V4),
414 * BND.ADDR = 0, BND.PORT = 0 */
415 const ssize_t size =
416 send(ctrl_sd, "\x05\x03\x00\x01\x00\x00\x00\x00\x00\x00", 10, MSG_NOSIGNAL);
417 if (size != 10)
418 {
419 msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port write failed on send()", __func__);
420 goto error;
421 }
422 }
423
424 /* receive reply from Socks proxy */
425 CLEAR(*relay_addr);
426 if (!recv_socks_reply(ctrl_sd, relay_addr, server_poll_timeout, &sig_info->signal_received))
427 {
428 goto error;
429 }
430 if (!relay_addr->addr.in4.sin_addr.s_addr)
431 {
432 msg(D_LINK_ERRORS, "%s: Socks proxy did not answer with IPv4 address", __func__);
433 goto error;
434 }
435
436 return;
437
438error:
439 /* SOFT-SIGUSR1 -- socks error */
440 register_signal(sig_info, SIGUSR1, "socks-error");
441 return;
442}
443
444/*
445 * Remove the socks5 header from an incoming
446 * UDP packet, setting *from to the source address.
447 *
448 * Run after UDP read.
449 */
450void
452{
453 int atyp;
454
455 if (BLEN(buf) < SOCKS_UDPv4_HEADROOM)
456 {
457 goto error;
458 }
459
460 buf_read_u16(buf);
461 if (buf_read_u8(buf) != 0)
462 {
463 goto error;
464 }
465
466 atyp = buf_read_u8(buf);
467 if (atyp != 1) /* ATYP == 1 (IP V4) */
468 {
469 goto error;
470 }
471
472 buf_read(buf, &from->dest.addr.in4.sin_addr, sizeof(from->dest.addr.in4.sin_addr));
473 buf_read(buf, &from->dest.addr.in4.sin_port, sizeof(from->dest.addr.in4.sin_port));
474
475 return;
476
477error:
478 buf->len = 0;
479}
480
481/*
482 * Add a socks header prior to UDP write.
483 * *to is the destination address.
484 *
485 * Run before UDP write.
486 * Returns the size of the header.
487 */
488int
490{
491 /*
492 * Get a subset buffer prepended to buf --
493 * we expect these bytes will be here because
494 * we always allocate space for these bytes
495 */
496 struct buffer head = buf_sub(buf, SOCKS_UDPv4_HEADROOM, true);
497
498 /* crash if not enough headroom in buf */
499 ASSERT(buf_defined(&head));
500
501 buf_write_u16(&head, 0); /* RSV = 0 */
502 buf_write_u8(&head, 0); /* FRAG = 0 */
503 buf_write_u8(&head, '\x01'); /* ATYP = 1 (IP V4) */
504 buf_write(&head, &to->dest.addr.in4.sin_addr, sizeof(to->dest.addr.in4.sin_addr));
505 buf_write(&head, &to->dest.addr.in4.sin_port, sizeof(to->dest.addr.in4.sin_port));
506
508}
struct buffer buf_sub(struct buffer *buf, int size, bool prepend)
Return a sub-buffer of another buffer.
Definition buffer.c:212
bool checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
Definition buffer.c:1150
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Append a uint16_t to a buffer in network byte order.
Definition buffer.h:1302
static int buf_read_u16(struct buffer *buf)
Read and consume a uint16_t from the front of a buffer.
Definition buffer.h:1516
static bool buf_read(struct buffer *src, void *dest, int size)
Read bytes from the front of a buffer into a caller-supplied destination.
Definition buffer.h:1456
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:748
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1288
static int buf_read_u8(struct buffer *buf)
Read and consume a uint8_t from the front of a buffer.
Definition buffer.h:1495
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2036
static bool buf_defined(const struct buffer *buf)
Return true iff buf has a non-NULL data pointer.
Definition buffer.h:399
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
size_t strlcpy(char *dest, const char *src, size_t maxlen)
#define M_INFO
Definition errlevel.h:54
#define D_LINK_ERRORS
Definition errlevel.h:56
int get_server_poll_remaining_time(const struct event_timeout *server_poll_timeout)
Definition forward.c:520
Interface functions to the internal and external multiplexers.
static SERVICE_STATUS_HANDLE service
Definition interactive.c:51
#define GET_USER_PASS_MANAGEMENT
Definition misc.h:113
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Retrieves the user credentials from various sources depending on the flags.
Definition misc.h:155
#define SOCKS_UDPv4_HEADROOM
Definition mtu.h:106
#define CLEAR(x)
Definition basic.h:32
#define M_NONFATAL
Definition error.h:91
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_ERRNO
Definition error.h:95
bool proxy_recv_char(uint8_t *c, const char *name, socket_descriptor_t sd, struct timeval *timeout, volatile int *signal_received)
Definition proxy.c:65
bool proxy_send(socket_descriptor_t sd, const void *buf, size_t len)
Definition proxy.c:192
void register_signal(struct signal_info *si, int signum, const char *signal_text)
Register a soft signal in the signal_info struct si respecting priority.
Definition sig.c:228
#define MSG_NOSIGNAL
Definition socket.h:251
static const char * print_openvpn_sockaddr(const struct openvpn_sockaddr *addr, struct gc_arena *gc)
Definition socket_util.h:71
static int port_from_servname(const char *servname)
Definition socks.c:323
void establish_socks_proxy_passthru(const struct socks_proxy_info *p, socket_descriptor_t sd, const char *host, const char *servname, const struct event_timeout *server_poll_timeout, struct signal_info *sig_info)
Definition socks.c:342
static bool socks_proxy_recv_char(uint8_t *c, const char *name, socket_descriptor_t sd, const struct event_timeout *server_poll_timeout, volatile int *signal_received)
Definition socks.c:85
static bool socks_username_password_auth(const struct socks_proxy_info *p, socket_descriptor_t sd, const struct event_timeout *server_poll_timeout, volatile int *signal_received)
Definition socks.c:97
static bool recv_socks_reply(socket_descriptor_t sd, struct openvpn_sockaddr *addr, const struct event_timeout *server_poll_timeout, volatile int *signal_received)
Definition socks.c:235
static bool socks_handshake(const struct socks_proxy_info *p, socket_descriptor_t sd, const struct event_timeout *server_poll_timeout, volatile int *signal_received)
Definition socks.c:156
int socks_process_outgoing_udp(struct buffer *buf, const struct link_socket_actual *to)
Definition socks.c:489
void establish_socks_proxy_udpassoc(const struct socks_proxy_info *p, socket_descriptor_t ctrl_sd, struct openvpn_sockaddr *relay_addr, const struct event_timeout *server_poll_timeout, struct signal_info *sig_info)
Definition socks.c:400
#define UP_TYPE_SOCKS
Definition socks.c:49
void socks_process_incoming_udp(struct buffer *buf, struct link_socket_actual *from)
Definition socks.c:451
struct socks_proxy_info * socks_proxy_new(const char *server, const char *port, const char *authfile)
Definition socks.c:52
void socks_proxy_close(struct socks_proxy_info *sp)
Definition socks.c:79
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
union openvpn_sockaddr::@051240265251124304325377241125360350250341115171 addr
struct sockaddr_in in4
Definition socket_util.h:43
volatile int signal_received
Definition sig.h:42
char authfile[256]
Definition socks.h:44
char server[128]
Definition socks.h:42
const char * port
Definition socks.h:43
char password[USER_PASS_LEN]
Definition misc.h:71
char username[USER_PASS_LEN]
Definition misc.h:70
SOCKET socket_descriptor_t
Definition syshead.h:441
#define SIGUSR1
Definition syshead.h:57
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:138