OpenVPN
proxy.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-2023 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, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include "syshead.h"
29 
30 #include "common.h"
31 #include "misc.h"
32 #include "crypto.h"
33 #include "win32.h"
34 #include "socket.h"
35 #include "fdmisc.h"
36 #include "proxy.h"
37 #include "base64.h"
38 #include "httpdigest.h"
39 #include "ntlm.h"
40 #include "memdbg.h"
41 #include "forward.h"
42 
43 #define UP_TYPE_PROXY "HTTP Proxy"
44 
45 struct http_proxy_options *
47  struct gc_arena *gc)
48 {
49  if (!*hpo)
50  {
51  ALLOC_OBJ_CLEAR_GC(*hpo, struct http_proxy_options, gc);
52  /* http proxy defaults */
53  (*hpo)->http_version = "1.0";
54  }
55  return *hpo;
56 }
57 
58 
59 /* cached proxy username/password */
61 
62 static bool
64  char *buf,
65  int len,
66  const int timeout_sec,
67  const bool verbose,
68  struct buffer *lookahead,
69  volatile int *signal_received)
70 {
71  struct buffer la;
72  int lastc = 0;
73 
74  CLEAR(la);
75  if (lookahead)
76  {
77  la = *lookahead;
78  }
79 
80  while (true)
81  {
82  int status;
83  ssize_t size;
84  fd_set reads;
85  struct timeval tv;
86  uint8_t c;
87 
88  if (buf_defined(&la))
89  {
90  ASSERT(buf_init(&la, 0));
91  }
92 
93  FD_ZERO(&reads);
94  openvpn_fd_set(sd, &reads);
95  tv.tv_sec = timeout_sec;
96  tv.tv_usec = 0;
97 
98  status = select(sd + 1, &reads, NULL, NULL, &tv);
99 
100  get_signal(signal_received);
101  if (*signal_received)
102  {
103  goto error;
104  }
105 
106  /* timeout? */
107  if (status == 0)
108  {
109  if (verbose)
110  {
111  msg(D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read timeout expired");
112  }
113  goto error;
114  }
115 
116  /* error */
117  if (status < 0)
118  {
119  if (verbose)
120  {
121  msg(D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read failed on select()");
122  }
123  goto error;
124  }
125 
126  /* read single char */
127  size = recv(sd, (void *)&c, 1, MSG_NOSIGNAL);
128 
129  /* error? */
130  if (size != 1)
131  {
132  if (verbose)
133  {
134  msg(D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read failed on recv()");
135  }
136  goto error;
137  }
138 
139 #if 0
140  if (isprint(c))
141  {
142  msg(M_INFO, "PROXY: read '%c' (%d)", c, (int)c);
143  }
144  else
145  {
146  msg(M_INFO, "PROXY: read (%d)", (int)c);
147  }
148 #endif
149 
150  /* store char in buffer */
151  if (len > 1)
152  {
153  *buf++ = c;
154  --len;
155  }
156 
157  /* also store char in lookahead buffer */
158  if (buf_defined(&la))
159  {
160  buf_write_u8(&la, c);
161  if (!isprint(c) && !isspace(c)) /* not ascii? */
162  {
163  if (verbose)
164  {
165  msg(D_LINK_ERRORS | M_ERRNO, "recv_line: Non-ASCII character (%d) read on recv()", (int)c);
166  }
167  *lookahead = la;
168  return false;
169  }
170  }
171 
172  /* end of line? */
173  if (lastc == '\r' && c == '\n')
174  {
175  break;
176  }
177 
178  lastc = c;
179  }
180 
181  /* append trailing null */
182  if (len > 0)
183  {
184  *buf++ = '\0';
185  }
186 
187  return true;
188 
189 error:
190  return false;
191 }
192 
193 static bool
195  const char *buf)
196 {
197  const ssize_t size = send(sd, buf, strlen(buf), MSG_NOSIGNAL);
198  if (size != (ssize_t) strlen(buf))
199  {
200  msg(D_LINK_ERRORS | M_ERRNO, "send_line: TCP port write failed on send()");
201  return false;
202  }
203  return true;
204 }
205 
206 static bool
208  const char *src)
209 {
210  bool ret;
211 
212  struct buffer buf = alloc_buf(strlen(src) + 3);
213  ASSERT(buf_write(&buf, src, strlen(src)));
214  ASSERT(buf_write(&buf, "\r\n", 3));
215  ret = send_line(sd, BSTR(&buf));
216  free_buf(&buf);
217  return ret;
218 }
219 
220 static bool
222 {
223  return send_line_crlf(sd, "");
224 }
225 
226 uint8_t *
227 make_base64_string2(const uint8_t *str, int src_len, struct gc_arena *gc)
228 {
229  uint8_t *ret = NULL;
230  char *b64out = NULL;
231  ASSERT(openvpn_base64_encode((const void *)str, src_len, &b64out) >= 0);
232  ret = (uint8_t *) string_alloc(b64out, gc);
233  free(b64out);
234  return ret;
235 }
236 
237 uint8_t *
238 make_base64_string(const uint8_t *str, struct gc_arena *gc)
239 {
240  return make_base64_string2(str, strlen((const char *)str), gc);
241 }
242 
243 static const char *
245  struct gc_arena *gc)
246 {
247  struct buffer out = alloc_buf_gc(strlen(p->up.username) + strlen(p->up.password) + 2, gc);
248  ASSERT(strlen(p->up.username) > 0);
249  buf_printf(&out, "%s:%s", p->up.username, p->up.password);
250  return (const char *)make_base64_string((const uint8_t *)BSTR(&out), gc);
251 }
252 
253 static void
255 {
257 }
258 
259 static void
260 get_user_pass_http(struct http_proxy_info *p, const bool force)
261 {
262  /*
263  * in case of forced (re)load, make sure the static storage is set as
264  * undefined, otherwise get_user_pass() won't try to load any credential
265  */
266  if (force)
267  {
269  }
270 
272  {
273  unsigned int flags = GET_USER_PASS_MANAGEMENT;
274  if (p->queried_creds)
275  {
277  }
278  if (p->options.inline_creds)
279  {
281  }
283  p->options.auth_file,
285  flags);
286  p->queried_creds = true;
288  }
289 }
290 
291 #if 0
292 /* function only used in #if 0 debug statement */
293 static void
294 dump_residual(socket_descriptor_t sd,
295  int timeout,
296  volatile int *signal_received)
297 {
298  char buf[256];
299  while (true)
300  {
301  if (!recv_line(sd, buf, sizeof(buf), timeout, true, NULL, signal_received))
302  {
303  return;
304  }
305  chomp(buf);
306  msg(D_PROXY, "PROXY HEADER: '%s'", buf);
307  }
308 }
309 #endif
310 
311 /*
312  * Extract the Proxy-Authenticate header from the stream.
313  * Consumes all headers.
314  */
315 static int
317  int timeout,
318  char **data,
319  volatile int *signal_received)
320 {
321  char buf[256];
322  int ret = HTTP_AUTH_NONE;
323  while (true)
324  {
325  if (!recv_line(sd, buf, sizeof(buf), timeout, true, NULL, signal_received))
326  {
327  free(*data);
328  *data = NULL;
329  return HTTP_AUTH_NONE;
330  }
331  chomp(buf);
332  if (!strlen(buf))
333  {
334  return ret;
335  }
336  if (ret == HTTP_AUTH_NONE && !strncmp(buf, "Proxy-Authenticate: ", 20))
337  {
338  if (!strncmp(buf+20, "Basic ", 6))
339  {
340  msg(D_PROXY, "PROXY AUTH BASIC: '%s'", buf);
341  *data = string_alloc(buf+26, NULL);
342  ret = HTTP_AUTH_BASIC;
343  }
344 #if PROXY_DIGEST_AUTH
345  else if (!strncmp(buf+20, "Digest ", 7))
346  {
347  msg(D_PROXY, "PROXY AUTH DIGEST: '%s'", buf);
348  *data = string_alloc(buf+27, NULL);
349  ret = HTTP_AUTH_DIGEST;
350  }
351 #endif
352 #if NTLM
353  else if (!strncmp(buf+20, "NTLM", 4))
354  {
355  msg(D_PROXY, "PROXY AUTH NTLM: '%s'", buf);
356  *data = NULL;
357  ret = HTTP_AUTH_NTLM;
358  }
359 #endif
360  }
361  }
362 }
363 
364 static void
366 {
367  free(p->proxy_authenticate);
369 }
370 
371 /*
372  * Parse out key/value pairs from Proxy-Authenticate string.
373  * Return true on success, or false on parse failure.
374  */
375 static bool
376 get_key_value(const char *str, /* source string */
377  char *key, /* key stored here */
378  char *value, /* value stored here */
379  int max_key_len,
380  int max_value_len,
381  const char **endptr) /* next search position */
382 {
383  int c;
384  bool starts_with_quote = false;
385  bool escape = false;
386 
387  for (c = max_key_len-1; (*str && (*str != '=') && c--); )
388  {
389  *key++ = *str++;
390  }
391  *key = '\0';
392 
393  if ('=' != *str++)
394  {
395  /* no key/value found */
396  return false;
397  }
398 
399  if ('\"' == *str)
400  {
401  /* quoted string */
402  str++;
403  starts_with_quote = true;
404  }
405 
406  for (c = max_value_len-1; *str && c--; str++)
407  {
408  switch (*str)
409  {
410  case '\\':
411  if (!escape)
412  {
413  /* possibly the start of an escaped quote */
414  escape = true;
415  *value++ = '\\'; /* even though this is an escape character, we still
416  * store it as-is in the target buffer */
417  continue;
418  }
419  break;
420 
421  case ',':
422  if (!starts_with_quote)
423  {
424  /* this signals the end of the value if we didn't get a starting quote
425  * and then we do "sloppy" parsing */
426  c = 0; /* the end */
427  continue;
428  }
429  break;
430 
431  case '\r':
432  case '\n':
433  /* end of string */
434  c = 0;
435  continue;
436 
437  case '\"':
438  if (!escape && starts_with_quote)
439  {
440  /* end of string */
441  c = 0;
442  continue;
443  }
444  break;
445  }
446  escape = false;
447  *value++ = *str;
448  }
449  *value = '\0';
450 
451  *endptr = str;
452 
453  return true; /* success */
454 }
455 
456 static char *
457 get_pa_var(const char *key, const char *pa, struct gc_arena *gc)
458 {
459  char k[64];
460  char v[256];
461  const char *content = pa;
462 
463  while (true)
464  {
465  const int status = get_key_value(content, k, v, sizeof(k), sizeof(v), &content);
466  if (status)
467  {
468  if (!strcmp(key, k))
469  {
470  return string_alloc(v, gc);
471  }
472  }
473  else
474  {
475  return NULL;
476  }
477 
478  /* advance to start of next key */
479  if (*content == ',')
480  {
481  ++content;
482  }
483  while (*content && isspace(*content))
484  {
485  ++content;
486  }
487  }
488 }
489 
490 struct http_proxy_info *
492 {
493  struct http_proxy_info *p;
494 
495  if (!o || !o->server)
496  {
497  msg(M_FATAL, "HTTP_PROXY: server not specified");
498  }
499 
500  ASSERT( o->port);
501 
502  ALLOC_OBJ_CLEAR(p, struct http_proxy_info);
503  p->options = *o;
504 
505  /* parse authentication method */
507  if (o->auth_method_string)
508  {
509  if (!strcmp(o->auth_method_string, "none"))
510  {
512  }
513  else if (!strcmp(o->auth_method_string, "basic"))
514  {
516  }
517 #if NTLM
518  else if (!strcmp(o->auth_method_string, "ntlm"))
519  {
520  msg(M_INFO, "NTLM v1 authentication is deprecated and will be removed in "
521  "OpenVPN 2.7");
523  }
524  else if (!strcmp(o->auth_method_string, "ntlm2"))
525  {
527  }
528 #endif
529  else
530  {
531  msg(M_FATAL, "ERROR: unknown HTTP authentication method: '%s'",
532  o->auth_method_string);
533  }
534  }
535 
536  /* only basic and NTLM/NTLMv2 authentication supported so far */
538  {
539  get_user_pass_http(p, true);
540  }
541 
542 #if !NTLM
544  {
545  msg(M_FATAL, "Sorry, this version of " PACKAGE_NAME " was built without NTLM Proxy support.");
546  }
547 #endif
548 
549  p->defined = true;
550  return p;
551 }
552 
553 void
555 {
556  free(hp);
557 }
558 
559 static bool
561  socket_descriptor_t sd, /* already open to proxy */
562  const char *host, /* openvpn server remote */
563  const char *port /* openvpn server port */
564  )
565 {
566  char buf[512];
567  int i;
568  bool host_header_sent = false;
569 
570  /*
571  * Send custom headers if provided
572  * If content is NULL the whole header is in name
573  * Also remember if we already sent a Host: header
574  */
575  for (i = 0; i < MAX_CUSTOM_HTTP_HEADER && p->options.custom_headers[i].name; i++)
576  {
577  if (p->options.custom_headers[i].content)
578  {
579  openvpn_snprintf(buf, sizeof(buf), "%s: %s",
582  if (!strcasecmp(p->options.custom_headers[i].name, "Host"))
583  {
584  host_header_sent = true;
585  }
586  }
587  else
588  {
589  openvpn_snprintf(buf, sizeof(buf), "%s",
590  p->options.custom_headers[i].name);
591  if (!strncasecmp(p->options.custom_headers[i].name, "Host:", 5))
592  {
593  host_header_sent = true;
594  }
595  }
596 
597  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
598  if (!send_line_crlf(sd, buf))
599  {
600  return false;
601  }
602  }
603 
604  if (!host_header_sent)
605  {
606  openvpn_snprintf(buf, sizeof(buf), "Host: %s", host);
607  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
608  if (!send_line_crlf(sd, buf))
609  {
610  return false;
611  }
612  }
613 
614  /* send User-Agent string if provided */
615  if (p->options.user_agent)
616  {
617  openvpn_snprintf(buf, sizeof(buf), "User-Agent: %s",
618  p->options.user_agent);
619  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
620  if (!send_line_crlf(sd, buf))
621  {
622  return false;
623  }
624  }
625 
626  return true;
627 }
628 
629 
630 bool
632  socket_descriptor_t sd, /* already open to proxy */
633  const char *host, /* openvpn server remote */
634  const char *port, /* openvpn server port */
635  struct event_timeout *server_poll_timeout,
636  struct buffer *lookahead,
637  struct signal_info *sig_info)
638 {
639  struct gc_arena gc = gc_new();
640  char buf[512];
641  char buf2[129];
642  char get[80];
643  int status;
644  int nparms;
645  bool ret = false;
646  bool processed = false;
647  volatile int *signal_received = &sig_info->signal_received;
648 
649  /* get user/pass if not previously given */
650  if (p->auth_method == HTTP_AUTH_BASIC
652  || p->auth_method == HTTP_AUTH_NTLM)
653  {
654  get_user_pass_http(p, false);
655  }
656 
657  /* are we being called again after getting the digest server nonce in the previous transaction? */
659  {
660  nparms = 1;
661  status = 407;
662  }
663  else
664  {
665  /* format HTTP CONNECT message */
666  openvpn_snprintf(buf, sizeof(buf), "CONNECT %s:%s HTTP/%s",
667  host,
668  port,
669  p->options.http_version);
670 
671  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
672 
673  /* send HTTP CONNECT message to proxy */
674  if (!send_line_crlf(sd, buf))
675  {
676  goto error;
677  }
678 
679  if (!add_proxy_headers(p, sd, host, port))
680  {
681  goto error;
682  }
683 
684  /* auth specified? */
685  switch (p->auth_method)
686  {
687  case HTTP_AUTH_NONE:
688  break;
689 
690  case HTTP_AUTH_BASIC:
691  openvpn_snprintf(buf, sizeof(buf), "Proxy-Authorization: Basic %s",
693  msg(D_PROXY, "Attempting Basic Proxy-Authorization");
694  dmsg(D_SHOW_KEYS, "Send to HTTP proxy: '%s'", buf);
695  if (!send_line_crlf(sd, buf))
696  {
697  goto error;
698  }
699  break;
700 
701 #if NTLM
702  case HTTP_AUTH_NTLM:
703  case HTTP_AUTH_NTLM2:
704  /* keep-alive connection */
705  openvpn_snprintf(buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
706  if (!send_line_crlf(sd, buf))
707  {
708  goto error;
709  }
710 
711  openvpn_snprintf(buf, sizeof(buf), "Proxy-Authorization: NTLM %s",
712  ntlm_phase_1(p, &gc));
713  msg(D_PROXY, "Attempting NTLM Proxy-Authorization phase 1");
714  dmsg(D_SHOW_KEYS, "Send to HTTP proxy: '%s'", buf);
715  if (!send_line_crlf(sd, buf))
716  {
717  goto error;
718  }
719  break;
720 #endif
721 
722  default:
723  ASSERT(0);
724  }
725 
726  /* send empty CR, LF */
727  if (!send_crlf(sd))
728  {
729  goto error;
730  }
731 
732  /* receive reply from proxy */
733  if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
734  {
735  goto error;
736  }
737 
738  /* remove trailing CR, LF */
739  chomp(buf);
740 
741  msg(D_PROXY, "HTTP proxy returned: '%s'", buf);
742 
743  /* parse return string */
744  nparms = sscanf(buf, "%*s %d", &status);
745 
746  }
747 
748  /* check for a "407 Proxy Authentication Required" response */
749  while (nparms >= 1 && status == 407)
750  {
751  msg(D_PROXY, "Proxy requires authentication");
752 
753  if (p->auth_method == HTTP_AUTH_BASIC && !processed)
754  {
755  processed = true;
756  }
757  else if ((p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2) && !processed) /* check for NTLM */
758  {
759 #if NTLM
760  /* look for the phase 2 response */
761 
762  while (true)
763  {
764  if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
765  {
766  goto error;
767  }
768  chomp(buf);
769  msg(D_PROXY, "HTTP proxy returned: '%s'", buf);
770 
771  openvpn_snprintf(get, sizeof get, "%%*s NTLM %%%ds", (int) sizeof(buf2) - 1);
772  nparms = sscanf(buf, get, buf2);
773  buf2[128] = 0; /* we only need the beginning - ensure it's null terminated. */
774 
775  /* check for "Proxy-Authenticate: NTLM TlRM..." */
776  if (nparms == 1)
777  {
778  /* parse buf2 */
779  msg(D_PROXY, "auth string: '%s'", buf2);
780  break;
781  }
782  }
783  /* if we are here then auth string was got */
784  msg(D_PROXY, "Received NTLM Proxy-Authorization phase 2 response");
785 
786  /* receive and discard everything else */
787  while (recv_line(sd, NULL, 0, 2, true, NULL, signal_received))
788  {
789  }
790 
791  /* now send the phase 3 reply */
792 
793  /* format HTTP CONNECT message */
794  openvpn_snprintf(buf, sizeof(buf), "CONNECT %s:%s HTTP/%s",
795  host,
796  port,
797  p->options.http_version);
798 
799  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
800 
801  /* send HTTP CONNECT message to proxy */
802  if (!send_line_crlf(sd, buf))
803  {
804  goto error;
805  }
806 
807  /* keep-alive connection */
808  openvpn_snprintf(buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
809  if (!send_line_crlf(sd, buf))
810  {
811  goto error;
812  }
813 
814  /* send HOST etc, */
815  if (!add_proxy_headers(p, sd, host, port))
816  {
817  goto error;
818  }
819 
820  msg(D_PROXY, "Attempting NTLM Proxy-Authorization phase 3");
821  {
822  const char *np3 = ntlm_phase_3(p, buf2, &gc);
823  if (!np3)
824  {
825  msg(D_PROXY, "NTLM Proxy-Authorization phase 3 failed: received corrupted data from proxy server");
826  goto error;
827  }
828  openvpn_snprintf(buf, sizeof(buf), "Proxy-Authorization: NTLM %s", np3);
829  }
830 
831  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
832  if (!send_line_crlf(sd, buf))
833  {
834  goto error;
835  }
836  /* ok so far... */
837  /* send empty CR, LF */
838  if (!send_crlf(sd))
839  {
840  goto error;
841  }
842 
843  /* receive reply from proxy */
844  if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
845  {
846  goto error;
847  }
848 
849  /* remove trailing CR, LF */
850  chomp(buf);
851 
852  msg(D_PROXY, "HTTP proxy returned: '%s'", buf);
853 
854  /* parse return string */
855  nparms = sscanf(buf, "%*s %d", &status);
856  processed = true;
857 #endif /* if NTLM */
858  }
859 #if PROXY_DIGEST_AUTH
860  else if (p->auth_method == HTTP_AUTH_DIGEST && !processed)
861  {
862  char *pa = p->proxy_authenticate;
863  const int method = p->auth_method;
864  ASSERT(pa);
865 
866  if (method == HTTP_AUTH_DIGEST)
867  {
868  const char *http_method = "CONNECT";
869  const char *nonce_count = "00000001";
870  const char *qop = "auth";
871  const char *username = p->up.username;
872  const char *password = p->up.password;
873  char *opaque_kv = "";
874  char uri[128];
875  uint8_t cnonce_raw[8];
876  uint8_t *cnonce;
877  HASHHEX session_key;
878  HASHHEX response;
879 
880  const char *realm = get_pa_var("realm", pa, &gc);
881  const char *nonce = get_pa_var("nonce", pa, &gc);
882  const char *algor = get_pa_var("algorithm", pa, &gc);
883  const char *opaque = get_pa_var("opaque", pa, &gc);
884 
885  if (!realm || !nonce)
886  {
887  msg(D_LINK_ERRORS, "HTTP proxy: digest auth failed, malformed response "
888  "from server: realm= or nonce= missing" );
889  goto error;
890  }
891 
892  /* generate a client nonce */
893  ASSERT(rand_bytes(cnonce_raw, sizeof(cnonce_raw)));
894  cnonce = make_base64_string2(cnonce_raw, sizeof(cnonce_raw), &gc);
895 
896 
897  /* build the digest response */
898  openvpn_snprintf(uri, sizeof(uri), "%s:%s",
899  host,
900  port);
901 
902  if (opaque)
903  {
904  const int len = strlen(opaque)+16;
905  opaque_kv = gc_malloc(len, false, &gc);
906  openvpn_snprintf(opaque_kv, len, ", opaque=\"%s\"", opaque);
907  }
908 
909  DigestCalcHA1(algor,
910  username,
911  realm,
912  password,
913  nonce,
914  (char *)cnonce,
915  session_key);
917  nonce,
918  nonce_count,
919  (char *)cnonce,
920  qop,
921  http_method,
922  uri,
923  NULL,
924  response);
925 
926  /* format HTTP CONNECT message */
927  openvpn_snprintf(buf, sizeof(buf), "%s %s HTTP/%s",
928  http_method,
929  uri,
930  p->options.http_version);
931 
932  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
933 
934  /* send HTTP CONNECT message to proxy */
935  if (!send_line_crlf(sd, buf))
936  {
937  goto error;
938  }
939 
940  /* send HOST etc, */
941  if (!add_proxy_headers(p, sd, host, port))
942  {
943  goto error;
944  }
945 
946  /* send digest response */
947  openvpn_snprintf(buf, sizeof(buf), "Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s",
948  username,
949  realm,
950  nonce,
951  uri,
952  qop,
953  nonce_count,
954  cnonce,
955  response,
956  opaque_kv
957  );
958  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
959  if (!send_line_crlf(sd, buf))
960  {
961  goto error;
962  }
963  if (!send_crlf(sd))
964  {
965  goto error;
966  }
967 
968  /* receive reply from proxy */
969  if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
970  {
971  goto error;
972  }
973 
974  /* remove trailing CR, LF */
975  chomp(buf);
976 
977  msg(D_PROXY, "HTTP proxy returned: '%s'", buf);
978 
979  /* parse return string */
980  nparms = sscanf(buf, "%*s %d", &status);
981  processed = true;
982  }
983  else
984  {
985  msg(D_PROXY, "HTTP proxy: digest method not supported");
986  goto error;
987  }
988  }
989 #endif /* if PROXY_DIGEST_AUTH */
990  else if (p->options.auth_retry)
991  {
992  /* figure out what kind of authentication the proxy needs */
993  char *pa = NULL;
994  const int method = get_proxy_authenticate(sd,
995  get_server_poll_remaining_time(server_poll_timeout),
996  &pa,
997  signal_received);
998  if (method != HTTP_AUTH_NONE)
999  {
1000  if (pa)
1001  {
1002  msg(D_PROXY, "HTTP proxy authenticate '%s'", pa);
1003  }
1004  if (p->options.auth_retry == PAR_NCT && method == HTTP_AUTH_BASIC)
1005  {
1006  msg(D_PROXY, "HTTP proxy: support for basic auth and other cleartext proxy auth methods is disabled");
1007  free(pa);
1008  goto error;
1009  }
1010  p->auth_method = method;
1011  store_proxy_authenticate(p, pa);
1012  ret = true;
1013  goto done;
1014  }
1015  else
1016  {
1017  msg(D_PROXY, "HTTP proxy: do not recognize the authentication method required by proxy");
1018  free(pa);
1019  goto error;
1020  }
1021  }
1022  else
1023  {
1024  if (!processed)
1025  {
1026  msg(D_PROXY, "HTTP proxy: no support for proxy authentication method");
1027  }
1028  goto error;
1029  }
1030 
1031  /* clear state */
1032  if (p->options.auth_retry)
1033  {
1035  }
1036  store_proxy_authenticate(p, NULL);
1037  }
1038 
1039  /* check return code, success = 200 */
1040  if (nparms < 1 || status != 200)
1041  {
1042  msg(D_LINK_ERRORS, "HTTP proxy returned bad status");
1043 #if 0
1044  /* DEBUGGING -- show a multi-line HTTP error response */
1045  dump_residual(sd, get_server_poll_remaining_time(server_poll_timeout), signal_received);
1046 #endif
1047  goto error;
1048  }
1049 
1050  /* SUCCESS */
1051 
1052  /* receive line from proxy and discard */
1053  if (!recv_line(sd, NULL, 0, get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
1054  {
1055  goto error;
1056  }
1057 
1058  /*
1059  * Toss out any extraneous chars, but don't throw away the
1060  * start of the OpenVPN data stream (put it in lookahead).
1061  */
1062  while (recv_line(sd, NULL, 0, 2, false, lookahead, signal_received))
1063  {
1064  }
1065 
1066  /* reset queried_creds so that we don't think that the next creds request is due to an auth error */
1067  p->queried_creds = false;
1068 
1069 #if 0
1070  if (lookahead && BLEN(lookahead))
1071  {
1072  msg(M_INFO, "HTTP PROXY: lookahead: %s", format_hex(BPTR(lookahead), BLEN(lookahead), 0));
1073  }
1074 #endif
1075 
1076 done:
1077  gc_free(&gc);
1078  return ret;
1079 
1080 error:
1081  register_signal(sig_info, SIGUSR1, "HTTP proxy error"); /* SOFT-SIGUSR1 -- HTTP proxy error */
1082  gc_free(&gc);
1083  return ret;
1084 }
signal_info::signal_received
volatile int signal_received
Definition: sig.h:43
DigestCalcResponse
void DigestCalcResponse(IN HASHHEX HA1, IN char *pszNonce, IN char *pszNonceCount, IN char *pszCNonce, IN char *pszQop, IN char *pszMethod, IN char *pszDigestUri, IN HASHHEX HEntity, OUT HASHHEX Response)
Definition: httpdigest.c:107
make_base64_string2
uint8_t * make_base64_string2(const uint8_t *str, int src_len, struct gc_arena *gc)
Definition: proxy.c:227
HTTP_AUTH_NTLM2
#define HTTP_AUTH_NTLM2
Definition: proxy.h:35
static_proxy_user_pass
static struct user_pass static_proxy_user_pass
Definition: proxy.c:60
http_proxy_options::auth_file
const char * auth_file
Definition: proxy.h:54
M_INFO
#define M_INFO
Definition: errlevel.h:55
GET_USER_PASS_PREVIOUS_CREDS_FAILED
#define GET_USER_PASS_PREVIOUS_CREDS_FAILED
Definition: misc.h:115
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1011
send_line_crlf
static bool send_line_crlf(socket_descriptor_t sd, const char *src)
Definition: proxy.c:207
M_ERRNO
#define M_ERRNO
Definition: error.h:100
forward.h
get_proxy_authenticate
static int get_proxy_authenticate(socket_descriptor_t sd, int timeout, char **data, volatile int *signal_received)
Definition: proxy.c:316
http_proxy_options::http_version
const char * http_version
Definition: proxy.h:55
PACKAGE_NAME
#define PACKAGE_NAME
Definition: config.h:492
http_proxy_info::defined
bool defined
Definition: proxy.h:68
D_LINK_ERRORS
#define D_LINK_ERRORS
Definition: errlevel.h:57
M_FATAL
#define M_FATAL
Definition: error.h:95
win32.h
http_custom_header::name
const char * name
Definition: proxy.h:39
buf_init
#define buf_init(buf, offset)
Definition: buffer.h:209
BSTR
#define BSTR(buf)
Definition: buffer.h:129
user_pass::username
char username[USER_PASS_LEN]
Definition: misc.h:71
http_proxy_info
Definition: proxy.h:67
http_proxy_options::auth_method_string
const char * auth_method_string
Definition: proxy.h:53
alloc_buf_gc
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:88
user_pass::defined
bool defined
Definition: misc.h:58
get_user_pass_http
static void get_user_pass_http(struct http_proxy_info *p, const bool force)
Definition: proxy.c:260
openvpn_base64_encode
int openvpn_base64_encode(const void *data, int size, char **str)
Definition: base64.c:52
dmsg
#define dmsg(flags,...)
Definition: error.h:154
ntlm.h
http_proxy_info::options
struct http_proxy_options options
Definition: proxy.h:70
fdmisc.h
DigestCalcHA1
void DigestCalcHA1(IN char *pszAlg, IN char *pszUserName, IN char *pszRealm, IN char *pszPassword, IN char *pszNonce, IN char *pszCNonce, OUT HASHHEX SessionKey)
Definition: httpdigest.c:70
D_PROXY
#define D_PROXY
Definition: errlevel.h:74
http_proxy_info::auth_method
int auth_method
Definition: proxy.h:69
GET_USER_PASS_MANAGEMENT
#define GET_USER_PASS_MANAGEMENT
Definition: misc.h:109
http_proxy_options::inline_creds
bool inline_creds
Definition: proxy.h:58
key
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:149
HTTP_AUTH_NTLM
#define HTTP_AUTH_NTLM
Definition: proxy.h:34
establish_http_proxy_passthru
bool establish_http_proxy_passthru(struct http_proxy_info *p, socket_descriptor_t sd, const char *host, const char *port, struct event_timeout *server_poll_timeout, struct buffer *lookahead, struct signal_info *sig_info)
Definition: proxy.c:631
get_server_poll_remaining_time
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition: forward.c:496
CLEAR
#define CLEAR(x)
Definition: basic.h:33
string_alloc
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:693
ASSERT
#define ASSERT(x)
Definition: error.h:201
store_proxy_authenticate
static void store_proxy_authenticate(struct http_proxy_info *p, char *data)
Definition: proxy.c:365
MAX_CUSTOM_HTTP_HEADER
#define MAX_CUSTOM_HTTP_HEADER
Definition: proxy.h:43
http_proxy_options::user_agent
const char * user_agent
Definition: proxy.h:56
http_proxy_options::auth_retry
int auth_retry
Definition: proxy.h:51
BLEN
#define BLEN(buf)
Definition: buffer.h:127
purge_user_pass
void purge_user_pass(struct user_pass *up, const bool force)
Definition: misc.c:473
send_crlf
static bool send_crlf(socket_descriptor_t sd)
Definition: proxy.c:221
buf_write_u8
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Definition: buffer.h:710
HTTP_AUTH_DIGEST
#define HTTP_AUTH_DIGEST
Definition: proxy.h:33
UP_TYPE_PROXY
#define UP_TYPE_PROXY
Definition: proxy.c:43
misc.h
ALLOC_OBJ_CLEAR_GC
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Definition: buffer.h:1083
openvpn_fd_set
static void openvpn_fd_set(socket_descriptor_t fd, fd_set *setp)
Definition: fdmisc.h:40
http_proxy_options::port
const char * port
Definition: proxy.h:46
crypto.h
base64.h
get_user_pass
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Definition: misc.h:130
send_line
static bool send_line(socket_descriptor_t sd, const char *buf)
Definition: proxy.c:194
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
httpdigest.h
http_proxy_options::server
const char * server
Definition: proxy.h:45
rand_bytes
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
Definition: crypto_openssl.c:571
buf_write
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition: buffer.h:686
init_http_proxy_options_once
struct http_proxy_options * init_http_proxy_options_once(struct http_proxy_options **hpo, struct gc_arena *gc)
Definition: proxy.c:46
proxy.h
http-client.session_key
session_key
Definition: http-client.py:8
syshead.h
http_proxy_info::proxy_authenticate
char * proxy_authenticate
Definition: proxy.h:72
BPTR
#define BPTR(buf)
Definition: buffer.h:124
http_proxy_info::queried_creds
bool queried_creds
Definition: proxy.h:73
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
recv_line
static bool recv_line(socket_descriptor_t sd, char *buf, int len, const int timeout_sec, const bool verbose, struct buffer *lookahead, volatile int *signal_received)
Definition: proxy.c:63
free_buf
void free_buf(struct buffer *buf)
Definition: buffer.c:183
HTTP_AUTH_BASIC
#define HTTP_AUTH_BASIC
Definition: proxy.h:32
socket_descriptor_t
SOCKET socket_descriptor_t
Definition: syshead.h:429
common.h
get_pa_var
static char * get_pa_var(const char *key, const char *pa, struct gc_arena *gc)
Definition: proxy.c:457
chomp
void chomp(char *str)
Definition: buffer.c:658
clear_user_pass_http
static void clear_user_pass_http(void)
Definition: proxy.c:254
username_password_as_base64
static const char * username_password_as_base64(const struct http_proxy_info *p, struct gc_arena *gc)
Definition: proxy.c:244
gc_malloc
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition: buffer.c:380
HTTP_AUTH_NONE
#define HTTP_AUTH_NONE
Definition: proxy.h:31
ntlm_phase_3
const char * ntlm_phase_3(const struct http_proxy_info *p, const char *phase_2, struct gc_arena *gc)
Definition: ntlm.c:200
signal_info
Definition: sig.h:41
openvpn_snprintf
bool openvpn_snprintf(char *str, size_t size, const char *format,...)
Definition: buffer.c:294
status
static SERVICE_STATUS status
Definition: interactive.c:56
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1019
add_proxy_headers
static bool add_proxy_headers(struct http_proxy_info *p, socket_descriptor_t sd, const char *host, const char *port)
Definition: proxy.c:560
socket.h
ALLOC_OBJ_CLEAR
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition: buffer.h:1046
config.h
http_proxy_options
Definition: proxy.h:44
get_key_value
static bool get_key_value(const char *str, char *key, char *value, int max_key_len, int max_value_len, const char **endptr)
Definition: proxy.c:376
event_timeout
Definition: interval.h:136
format_hex
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
Definition: buffer.h:523
D_SHOW_KEYS
#define D_SHOW_KEYS
Definition: errlevel.h:121
user_pass::password
char password[USER_PASS_LEN]
Definition: misc.h:72
get_signal
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
Definition: sig.h:110
GET_USER_PASS_INLINE_CREDS
#define GET_USER_PASS_INLINE_CREDS
Definition: misc.h:121
MSG_NOSIGNAL
#define MSG_NOSIGNAL
Definition: socket.h:254
ntlm_phase_1
const char * ntlm_phase_1(const struct http_proxy_info *p, struct gc_arena *gc)
Definition: ntlm.c:183
PAR_NCT
#define PAR_NCT
Definition: proxy.h:50
make_base64_string
uint8_t * make_base64_string(const uint8_t *str, struct gc_arena *gc)
Definition: proxy.c:238
http_proxy_info::up
struct user_pass up
Definition: proxy.h:71
register_signal
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:231
http_custom_header::content
const char * content
Definition: proxy.h:40
user_pass
Definition: misc.h:56
alloc_buf
struct buffer alloc_buf(size_t size)
Definition: buffer.c:62
memdbg.h
http_proxy_new
struct http_proxy_info * http_proxy_new(const struct http_proxy_options *o)
Definition: proxy.c:491
msg
#define msg(flags,...)
Definition: error.h:150
http_proxy_options::custom_headers
struct http_custom_header custom_headers[MAX_CUSTOM_HTTP_HEADER]
Definition: proxy.h:57
buf_defined
static bool buf_defined(const struct buffer *buf)
Definition: buffer.h:228
buf_printf
bool buf_printf(struct buffer *buf, const char *format,...)
Definition: buffer.c:240
http_proxy_close
void http_proxy_close(struct http_proxy_info *hp)
Definition: proxy.c:554
buffer::data
uint8_t * data
Pointer to the allocated memory.
Definition: buffer.h:68