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-2024 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  const char *auth_file = p->options.auth_file;
275  if (p->options.auth_file_up)
276  {
277  auth_file = p->options.auth_file_up;
278  }
280  {
282  }
283  if (p->options.inline_creds)
284  {
286  }
288  auth_file,
290  flags);
292  }
293 
294  /*
295  * Using cached credentials
296  */
297  p->queried_creds = true;
299 }
300 
301 #if 0
302 /* function only used in #if 0 debug statement */
303 static void
304 dump_residual(socket_descriptor_t sd,
305  int timeout,
306  volatile int *signal_received)
307 {
308  char buf[256];
309  while (true)
310  {
311  if (!recv_line(sd, buf, sizeof(buf), timeout, true, NULL, signal_received))
312  {
313  return;
314  }
315  chomp(buf);
316  msg(D_PROXY, "PROXY HEADER: '%s'", buf);
317  }
318 }
319 #endif
320 
321 /*
322  * Extract the Proxy-Authenticate header from the stream.
323  * Consumes all headers.
324  */
325 static int
327  int timeout,
328  char **data,
329  volatile int *signal_received)
330 {
331  char buf[256];
332  int ret = HTTP_AUTH_NONE;
333  while (true)
334  {
335  if (!recv_line(sd, buf, sizeof(buf), timeout, true, NULL, signal_received))
336  {
337  free(*data);
338  *data = NULL;
339  return HTTP_AUTH_NONE;
340  }
341  chomp(buf);
342  if (!strlen(buf))
343  {
344  return ret;
345  }
346  if (ret == HTTP_AUTH_NONE && !strncmp(buf, "Proxy-Authenticate: ", 20))
347  {
348  if (!strncmp(buf+20, "Basic ", 6))
349  {
350  msg(D_PROXY, "PROXY AUTH BASIC: '%s'", buf);
351  *data = string_alloc(buf+26, NULL);
352  ret = HTTP_AUTH_BASIC;
353  }
354 #if PROXY_DIGEST_AUTH
355  else if (!strncmp(buf+20, "Digest ", 7))
356  {
357  msg(D_PROXY, "PROXY AUTH DIGEST: '%s'", buf);
358  *data = string_alloc(buf+27, NULL);
359  ret = HTTP_AUTH_DIGEST;
360  }
361 #endif
362 #if NTLM
363  else if (!strncmp(buf+20, "NTLM", 4))
364  {
365  msg(D_PROXY, "PROXY AUTH NTLM: '%s'", buf);
366  *data = NULL;
367  ret = HTTP_AUTH_NTLM2;
368  }
369 #endif
370  }
371  }
372 }
373 
374 static void
376 {
377  free(p->proxy_authenticate);
379 }
380 
381 /*
382  * Parse out key/value pairs from Proxy-Authenticate string.
383  * Return true on success, or false on parse failure.
384  */
385 static bool
386 get_key_value(const char *str, /* source string */
387  char *key, /* key stored here */
388  char *value, /* value stored here */
389  int max_key_len,
390  int max_value_len,
391  const char **endptr) /* next search position */
392 {
393  int c;
394  bool starts_with_quote = false;
395  bool escape = false;
396 
397  for (c = max_key_len-1; (*str && (*str != '=') && c--); )
398  {
399  *key++ = *str++;
400  }
401  *key = '\0';
402 
403  if ('=' != *str++)
404  {
405  /* no key/value found */
406  return false;
407  }
408 
409  if ('\"' == *str)
410  {
411  /* quoted string */
412  str++;
413  starts_with_quote = true;
414  }
415 
416  for (c = max_value_len-1; *str && c--; str++)
417  {
418  switch (*str)
419  {
420  case '\\':
421  if (!escape)
422  {
423  /* possibly the start of an escaped quote */
424  escape = true;
425  *value++ = '\\'; /* even though this is an escape character, we still
426  * store it as-is in the target buffer */
427  continue;
428  }
429  break;
430 
431  case ',':
432  if (!starts_with_quote)
433  {
434  /* this signals the end of the value if we didn't get a starting quote
435  * and then we do "sloppy" parsing */
436  c = 0; /* the end */
437  continue;
438  }
439  break;
440 
441  case '\r':
442  case '\n':
443  /* end of string */
444  c = 0;
445  continue;
446 
447  case '\"':
448  if (!escape && starts_with_quote)
449  {
450  /* end of string */
451  c = 0;
452  continue;
453  }
454  break;
455  }
456  escape = false;
457  *value++ = *str;
458  }
459  *value = '\0';
460 
461  *endptr = str;
462 
463  return true; /* success */
464 }
465 
466 static char *
467 get_pa_var(const char *key, const char *pa, struct gc_arena *gc)
468 {
469  char k[64];
470  char v[256];
471  const char *content = pa;
472 
473  while (true)
474  {
475  const int status = get_key_value(content, k, v, sizeof(k), sizeof(v), &content);
476  if (status)
477  {
478  if (!strcmp(key, k))
479  {
480  return string_alloc(v, gc);
481  }
482  }
483  else
484  {
485  return NULL;
486  }
487 
488  /* advance to start of next key */
489  if (*content == ',')
490  {
491  ++content;
492  }
493  while (*content && isspace(*content))
494  {
495  ++content;
496  }
497  }
498 }
499 
500 struct http_proxy_info *
502 {
503  struct http_proxy_info *p;
504 
505  if (!o || !o->server)
506  {
507  msg(M_FATAL, "HTTP_PROXY: server not specified");
508  }
509 
510  ASSERT(o->port);
511 
512  ALLOC_OBJ_CLEAR(p, struct http_proxy_info);
513  p->options = *o;
514 
515  /* parse authentication method */
517  if (o->auth_method_string)
518  {
519  if (!strcmp(o->auth_method_string, "none"))
520  {
522  }
523  else if (!strcmp(o->auth_method_string, "basic"))
524  {
526  }
527 #if NTLM
528  else if (!strcmp(o->auth_method_string, "ntlm"))
529  {
530  msg(M_WARN, "NTLM v1 authentication has been removed in OpenVPN 2.7. Will try to use NTLM v2 authentication.");
532  }
533  else if (!strcmp(o->auth_method_string, "ntlm2"))
534  {
536  }
537 #endif
538  else
539  {
540  msg(M_FATAL, "ERROR: unknown HTTP authentication method: '%s'",
541  o->auth_method_string);
542  }
543  }
544 
545  /* When basic or NTLMv2 authentication is requested, get credentials now.
546  * In case of "auto" negotiation credentials will be retrieved later once
547  * we know whether we need any. */
549  {
551  }
552 
553 #if !NTLM
554  if (p->auth_method == HTTP_AUTH_NTLM2)
555  {
556  msg(M_FATAL, "Sorry, this version of " PACKAGE_NAME " was built without NTLM Proxy support.");
557  }
558 #endif
559 
560  p->defined = true;
561  return p;
562 }
563 
564 void
566 {
567  free(hp);
568 }
569 
570 static bool
572  socket_descriptor_t sd, /* already open to proxy */
573  const char *host, /* openvpn server remote */
574  const char *port /* openvpn server port */
575  )
576 {
577  char buf[512];
578  int i;
579  bool host_header_sent = false;
580 
581  /*
582  * Send custom headers if provided
583  * If content is NULL the whole header is in name
584  * Also remember if we already sent a Host: header
585  */
586  for (i = 0; i < MAX_CUSTOM_HTTP_HEADER && p->options.custom_headers[i].name; i++)
587  {
588  if (p->options.custom_headers[i].content)
589  {
590  snprintf(buf, sizeof(buf), "%s: %s",
593  if (!strcasecmp(p->options.custom_headers[i].name, "Host"))
594  {
595  host_header_sent = true;
596  }
597  }
598  else
599  {
600  snprintf(buf, sizeof(buf), "%s",
601  p->options.custom_headers[i].name);
602  if (!strncasecmp(p->options.custom_headers[i].name, "Host:", 5))
603  {
604  host_header_sent = true;
605  }
606  }
607 
608  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
609  if (!send_line_crlf(sd, buf))
610  {
611  return false;
612  }
613  }
614 
615  if (!host_header_sent)
616  {
617  snprintf(buf, sizeof(buf), "Host: %s", host);
618  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
619  if (!send_line_crlf(sd, buf))
620  {
621  return false;
622  }
623  }
624 
625  /* send User-Agent string if provided */
626  if (p->options.user_agent)
627  {
628  snprintf(buf, sizeof(buf), "User-Agent: %s",
629  p->options.user_agent);
630  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
631  if (!send_line_crlf(sd, buf))
632  {
633  return false;
634  }
635  }
636 
637  return true;
638 }
639 
640 
641 bool
643  socket_descriptor_t sd, /* already open to proxy */
644  const char *host, /* openvpn server remote */
645  const char *port, /* openvpn server port */
646  struct event_timeout *server_poll_timeout,
647  struct buffer *lookahead,
648  struct signal_info *sig_info)
649 {
650  struct gc_arena gc = gc_new();
651  char buf[512];
652  int status;
653  int nparms;
654  bool ret = false;
655  bool processed = false;
656  volatile int *signal_received = &sig_info->signal_received;
657 
658  /* get user/pass if not previously given */
659  if (p->auth_method == HTTP_AUTH_BASIC
661  || p->auth_method == HTTP_AUTH_NTLM2)
662  {
663  get_user_pass_http(p, false);
664 
665  if (p->up.nocache)
666  {
668  }
669  }
670 
671  /* are we being called again after getting the digest server nonce in the previous transaction? */
673  {
674  nparms = 1;
675  status = 407;
676  }
677  else
678  {
679  /* format HTTP CONNECT message */
680  snprintf(buf, sizeof(buf), "CONNECT %s:%s HTTP/%s",
681  host,
682  port,
683  p->options.http_version);
684 
685  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
686 
687  /* send HTTP CONNECT message to proxy */
688  if (!send_line_crlf(sd, buf))
689  {
690  goto error;
691  }
692 
693  if (!add_proxy_headers(p, sd, host, port))
694  {
695  goto error;
696  }
697 
698  /* auth specified? */
699  switch (p->auth_method)
700  {
701  case HTTP_AUTH_NONE:
702  break;
703 
704  case HTTP_AUTH_BASIC:
705  snprintf(buf, sizeof(buf), "Proxy-Authorization: Basic %s",
707  msg(D_PROXY, "Attempting Basic Proxy-Authorization");
708  dmsg(D_SHOW_KEYS, "Send to HTTP proxy: '%s'", buf);
709  if (!send_line_crlf(sd, buf))
710  {
711  goto error;
712  }
713  break;
714 
715 #if NTLM
716  case HTTP_AUTH_NTLM2:
717  /* keep-alive connection */
718  snprintf(buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
719  if (!send_line_crlf(sd, buf))
720  {
721  goto error;
722  }
723 
724  snprintf(buf, sizeof(buf), "Proxy-Authorization: NTLM %s",
725  ntlm_phase_1(p, &gc));
726  msg(D_PROXY, "Attempting NTLM Proxy-Authorization phase 1");
727  dmsg(D_SHOW_KEYS, "Send to HTTP proxy: '%s'", buf);
728  if (!send_line_crlf(sd, buf))
729  {
730  goto error;
731  }
732  break;
733 #endif
734 
735  default:
736  ASSERT(0);
737  }
738 
739  /* send empty CR, LF */
740  if (!send_crlf(sd))
741  {
742  goto error;
743  }
744 
745  /* receive reply from proxy */
746  if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
747  {
748  goto error;
749  }
750 
751  /* remove trailing CR, LF */
752  chomp(buf);
753 
754  msg(D_PROXY, "HTTP proxy returned: '%s'", buf);
755 
756  /* parse return string */
757  nparms = sscanf(buf, "%*s %d", &status);
758 
759  }
760 
761  /* check for a "407 Proxy Authentication Required" response */
762  while (nparms >= 1 && status == 407)
763  {
764  msg(D_PROXY, "Proxy requires authentication");
765 
766  if (p->auth_method == HTTP_AUTH_BASIC && !processed)
767  {
768  processed = true;
769  }
770  else if (p->auth_method == HTTP_AUTH_NTLM2 && !processed) /* check for NTLM */
771  {
772 #if NTLM
773  /* look for the phase 2 response */
774  char buf2[512];
775  while (true)
776  {
777  if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
778  {
779  goto error;
780  }
781  chomp(buf);
782  msg(D_PROXY, "HTTP proxy returned: '%s'", buf);
783 
784  char get[80];
785  CLEAR(buf2);
786  snprintf(get, sizeof(get), "%%*s NTLM %%%zus", sizeof(buf2) - 1);
787  nparms = sscanf(buf, get, buf2);
788 
789  /* check for "Proxy-Authenticate: NTLM TlRM..." */
790  if (nparms == 1)
791  {
792  /* parse buf2 */
793  msg(D_PROXY, "auth string: '%s'", buf2);
794  break;
795  }
796  }
797  /* if we are here then auth string was got */
798  msg(D_PROXY, "Received NTLM Proxy-Authorization phase 2 response");
799 
800  /* receive and discard everything else */
801  while (recv_line(sd, NULL, 0, 2, true, NULL, signal_received))
802  {
803  }
804 
805  /* now send the phase 3 reply */
806 
807  /* format HTTP CONNECT message */
808  snprintf(buf, sizeof(buf), "CONNECT %s:%s HTTP/%s",
809  host,
810  port,
811  p->options.http_version);
812 
813  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
814 
815  /* send HTTP CONNECT message to proxy */
816  if (!send_line_crlf(sd, buf))
817  {
818  goto error;
819  }
820 
821  /* keep-alive connection */
822  snprintf(buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
823  if (!send_line_crlf(sd, buf))
824  {
825  goto error;
826  }
827 
828  /* send HOST etc, */
829  if (!add_proxy_headers(p, sd, host, port))
830  {
831  goto error;
832  }
833 
834  msg(D_PROXY, "Attempting NTLM Proxy-Authorization phase 3");
835  {
836  const char *np3 = ntlm_phase_3(p, buf2, &gc);
837  if (!np3)
838  {
839  msg(D_PROXY, "NTLM Proxy-Authorization phase 3 failed: received corrupted data from proxy server");
840  goto error;
841  }
842  snprintf(buf, sizeof(buf), "Proxy-Authorization: NTLM %s", np3);
843  }
844 
845  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
846  if (!send_line_crlf(sd, buf))
847  {
848  goto error;
849  }
850  /* ok so far... */
851  /* send empty CR, LF */
852  if (!send_crlf(sd))
853  {
854  goto error;
855  }
856 
857  /* receive reply from proxy */
858  if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
859  {
860  goto error;
861  }
862 
863  /* remove trailing CR, LF */
864  chomp(buf);
865 
866  msg(D_PROXY, "HTTP proxy returned: '%s'", buf);
867 
868  /* parse return string */
869  nparms = sscanf(buf, "%*s %d", &status);
870  processed = true;
871 #endif /* if NTLM */
872  }
873 #if PROXY_DIGEST_AUTH
874  else if (p->auth_method == HTTP_AUTH_DIGEST && !processed)
875  {
876  char *pa = p->proxy_authenticate;
877  const int method = p->auth_method;
878  ASSERT(pa);
879 
880  if (method == HTTP_AUTH_DIGEST)
881  {
882  const char *http_method = "CONNECT";
883  const char *nonce_count = "00000001";
884  const char *qop = "auth";
885  const char *username = p->up.username;
886  const char *password = p->up.password;
887  char *opaque_kv = "";
888  char uri[128];
889  uint8_t cnonce_raw[8];
890  uint8_t *cnonce;
891  HASHHEX session_key;
892  HASHHEX response;
893 
894  const char *realm = get_pa_var("realm", pa, &gc);
895  const char *nonce = get_pa_var("nonce", pa, &gc);
896  const char *algor = get_pa_var("algorithm", pa, &gc);
897  const char *opaque = get_pa_var("opaque", pa, &gc);
898 
899  if (!realm || !nonce)
900  {
901  msg(D_LINK_ERRORS, "HTTP proxy: digest auth failed, malformed response "
902  "from server: realm= or nonce= missing" );
903  goto error;
904  }
905 
906  /* generate a client nonce */
907  ASSERT(rand_bytes(cnonce_raw, sizeof(cnonce_raw)));
908  cnonce = make_base64_string2(cnonce_raw, sizeof(cnonce_raw), &gc);
909 
910 
911  /* build the digest response */
912  snprintf(uri, sizeof(uri), "%s:%s",
913  host,
914  port);
915 
916  if (opaque)
917  {
918  const int len = strlen(opaque)+16;
919  opaque_kv = gc_malloc(len, false, &gc);
920  snprintf(opaque_kv, len, ", opaque=\"%s\"", opaque);
921  }
922 
923  DigestCalcHA1(algor,
924  username,
925  realm,
926  password,
927  nonce,
928  (char *)cnonce,
929  session_key);
931  nonce,
932  nonce_count,
933  (char *)cnonce,
934  qop,
935  http_method,
936  uri,
937  NULL,
938  response);
939 
940  /* format HTTP CONNECT message */
941  snprintf(buf, sizeof(buf), "%s %s HTTP/%s",
942  http_method,
943  uri,
944  p->options.http_version);
945 
946  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
947 
948  /* send HTTP CONNECT message to proxy */
949  if (!send_line_crlf(sd, buf))
950  {
951  goto error;
952  }
953 
954  /* send HOST etc, */
955  if (!add_proxy_headers(p, sd, host, port))
956  {
957  goto error;
958  }
959 
960  /* send digest response */
961  int sret = snprintf(buf, sizeof(buf), "Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s",
962  username,
963  realm,
964  nonce,
965  uri,
966  qop,
967  nonce_count,
968  cnonce,
969  response,
970  opaque_kv
971  );
972  if (sret >= sizeof(buf))
973  {
974  goto error;
975  }
976 
977  msg(D_PROXY, "Send to HTTP proxy: '%s'", buf);
978  if (!send_line_crlf(sd, buf))
979  {
980  goto error;
981  }
982  if (!send_crlf(sd))
983  {
984  goto error;
985  }
986 
987  /* receive reply from proxy */
988  if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
989  {
990  goto error;
991  }
992 
993  /* remove trailing CR, LF */
994  chomp(buf);
995 
996  msg(D_PROXY, "HTTP proxy returned: '%s'", buf);
997 
998  /* parse return string */
999  nparms = sscanf(buf, "%*s %d", &status);
1000  processed = true;
1001  }
1002  else
1003  {
1004  msg(D_PROXY, "HTTP proxy: digest method not supported");
1005  goto error;
1006  }
1007  }
1008 #endif /* if PROXY_DIGEST_AUTH */
1009  else if (p->options.auth_retry)
1010  {
1011  /* figure out what kind of authentication the proxy needs */
1012  char *pa = NULL;
1013  const int method = get_proxy_authenticate(sd,
1014  get_server_poll_remaining_time(server_poll_timeout),
1015  &pa,
1016  signal_received);
1017  if (method != HTTP_AUTH_NONE)
1018  {
1019  if (pa)
1020  {
1021  msg(D_PROXY, "HTTP proxy authenticate '%s'", pa);
1022  }
1023  if (p->options.auth_retry == PAR_NCT && method == HTTP_AUTH_BASIC)
1024  {
1025  msg(D_PROXY, "HTTP proxy: support for basic auth and other cleartext proxy auth methods is disabled");
1026  free(pa);
1027  goto error;
1028  }
1029  p->auth_method = method;
1030  store_proxy_authenticate(p, pa);
1031  ret = true;
1032  goto done;
1033  }
1034  else
1035  {
1036  msg(D_PROXY, "HTTP proxy: do not recognize the authentication method required by proxy");
1037  free(pa);
1038  goto error;
1039  }
1040  }
1041  else
1042  {
1043  if (!processed)
1044  {
1045  msg(D_PROXY, "HTTP proxy: no support for proxy authentication method");
1046  }
1047  goto error;
1048  }
1049  }
1050 
1051  /* check return code, success = 200 */
1052  if (nparms < 1 || status != 200)
1053  {
1054  msg(D_LINK_ERRORS, "HTTP proxy returned bad status");
1055 #if 0
1056  /* DEBUGGING -- show a multi-line HTTP error response */
1057  dump_residual(sd, get_server_poll_remaining_time(server_poll_timeout), signal_received);
1058 #endif
1059  goto error;
1060  }
1061 
1062  /* SUCCESS */
1063 
1064  /* receive line from proxy and discard */
1065  if (!recv_line(sd, NULL, 0, get_server_poll_remaining_time(server_poll_timeout), true, NULL, signal_received))
1066  {
1067  goto error;
1068  }
1069 
1070  /*
1071  * Toss out any extraneous chars, but don't throw away the
1072  * start of the OpenVPN data stream (put it in lookahead).
1073  */
1074  while (recv_line(sd, NULL, 0, 2, false, lookahead, signal_received))
1075  {
1076  }
1077 
1078  /* reset queried_creds so that we don't think that the next creds request is due to an auth error */
1079  p->queried_creds = false;
1080 
1081 #if 0
1082  if (lookahead && BLEN(lookahead))
1083  {
1084  msg(M_INFO, "HTTP PROXY: lookahead: %s", format_hex(BPTR(lookahead), BLEN(lookahead), 0));
1085  }
1086 #endif
1087 
1088 done:
1089  gc_free(&gc);
1090  return ret;
1091 
1092 error:
1093  register_signal(sig_info, SIGUSR1, "HTTP proxy error"); /* SOFT-SIGUSR1 -- HTTP proxy error */
1094  gc_free(&gc);
1095  return ret;
1096 }
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:113
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1030
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:94
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:326
http_proxy_options::http_version
const char * http_version
Definition: proxy.h:56
PACKAGE_NAME
#define PACKAGE_NAME
Definition: config.h:492
http_proxy_info::defined
bool defined
Definition: proxy.h:71
D_LINK_ERRORS
#define D_LINK_ERRORS
Definition: errlevel.h:57
M_FATAL
#define M_FATAL
Definition: error.h:89
win32.h
http_custom_header::name
const char * name
Definition: proxy.h:39
buf_init
#define buf_init(buf, offset)
Definition: buffer.h:209
http_proxy_options::auth_file_up
const char * auth_file_up
Definition: proxy.h:55
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:70
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:148
ntlm.h
http_proxy_info::options
struct http_proxy_options options
Definition: proxy.h:73
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:72
GET_USER_PASS_MANAGEMENT
#define GET_USER_PASS_MANAGEMENT
Definition: misc.h:107
http_proxy_options::inline_creds
bool inline_creds
Definition: proxy.h:59
key
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:149
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:642
http_proxy_options::nocache
bool nocache
Definition: proxy.h:61
get_server_poll_remaining_time
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition: forward.c:531
CLEAR
#define CLEAR(x)
Definition: basic.h:33
string_alloc
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:667
ASSERT
#define ASSERT(x)
Definition: error.h:195
store_proxy_authenticate
static void store_proxy_authenticate(struct http_proxy_info *p, char *data)
Definition: proxy.c:375
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:57
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:697
HTTP_AUTH_DIGEST
#define HTTP_AUTH_DIGEST
Definition: proxy.h:33
UP_TYPE_PROXY
#define UP_TYPE_PROXY
Definition: proxy.c:43
misc.h
M_WARN
#define M_WARN
Definition: error.h:91
ALLOC_OBJ_CLEAR_GC
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Definition: buffer.h:1102
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
http_proxy_options::first_time
bool first_time
Definition: proxy.h:60
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)
Retrieves the user credentials from various sources depending on the flags.
Definition: misc.h:147
user_pass::nocache
bool nocache
Definition: misc.h:62
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:592
buf_write
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition: buffer.h:673
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:75
BPTR
#define BPTR(buf)
Definition: buffer.h:124
http_proxy_info::queried_creds
bool queried_creds
Definition: proxy.h:76
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:467
chomp
void chomp(char *str)
Definition: buffer.c:632
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:354
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:192
signal_info
Definition: sig.h:41
status
static SERVICE_STATUS status
Definition: interactive.c:53
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1038
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:571
socket.h
ALLOC_OBJ_CLEAR
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition: buffer.h:1065
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:386
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:510
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:119
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:175
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:74
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:501
msg
#define msg(flags,...)
Definition: error.h:144
http_proxy_options::custom_headers
struct http_custom_header custom_headers[MAX_CUSTOM_HTTP_HEADER]
Definition: proxy.h:58
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:565
buffer::data
uint8_t * data
Pointer to the allocated memory.
Definition: buffer.h:68