OpenVPN
ssl_verify.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-2018 OpenVPN Inc <sales@openvpn.net>
9  * Copyright (C) 2010-2018 Fox Crypto B.V. <openvpn@fox-it.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2
13  * as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #elif defined(_MSC_VER)
32 #include "config-msvc.h"
33 #endif
34 
35 #include "syshead.h"
36 
37 #include "base64.h"
38 #include "manage.h"
39 #include "otime.h"
40 #include "run_command.h"
41 #include "ssl_verify.h"
42 #include "ssl_verify_backend.h"
43 
44 #ifdef ENABLE_CRYPTO_OPENSSL
45 #include "ssl_verify_openssl.h"
46 #endif
47 
49 #define TLS_USERNAME_LEN 64
50 
51 static void
53 {
54  string_mod(str, CC_PRINT, CC_CRLF, '_');
55 }
56 
57 /*
58  * Export the untrusted IP address and port to the environment
59  */
60 static void
62 {
63  setenv_link_socket_actual(session->opt->es, "untrusted", &session->untrusted_addr, SA_IP_PORT);
64 }
65 
66 
72 static void
73 wipe_auth_token(struct tls_multi *multi)
74 {
75  if (multi)
76  {
77  if (multi->auth_token)
78  {
80  free(multi->auth_token);
81  }
82  multi->auth_token = NULL;
83  multi->auth_token_sent = false;
84  }
85 }
86 
87 
88 /*
89  * Remove authenticated state from all sessions in the given tunnel
90  */
91 static void
93 {
94  if (multi)
95  {
96  wipe_auth_token(multi);
97  for (int i = 0; i < TM_SIZE; ++i)
98  {
99  for (int j = 0; j < KS_SIZE; ++j)
100  {
101  multi->session[i].key[j].authenticated = false;
102  }
103  }
104  }
105 }
106 
107 /*
108  * Set the given session's common_name
109  */
110 static void
111 set_common_name(struct tls_session *session, const char *common_name)
112 {
113  if (session->common_name)
114  {
115  free(session->common_name);
116  session->common_name = NULL;
117 #ifdef ENABLE_PF
118  session->common_name_hashval = 0;
119 #endif
120  }
121  if (common_name)
122  {
123  /* FIXME: Last alloc will never be freed */
124  session->common_name = string_alloc(common_name, NULL);
125 #ifdef ENABLE_PF
126  {
127  const uint32_t len = (uint32_t) strlen(common_name);
128  if (len)
129  {
130  session->common_name_hashval = hash_func((const uint8_t *)common_name, len+1, 0);
131  }
132  else
133  {
134  session->common_name_hashval = 0;
135  }
136  }
137 #endif
138  }
139 }
140 
141 /*
142  * Retrieve the common name for the given tunnel's active session. If the
143  * common name is NULL or empty, return NULL if null is true, or "UNDEF" if
144  * null is false.
145  */
146 const char *
147 tls_common_name(const struct tls_multi *multi, const bool null)
148 {
149  const char *ret = NULL;
150  if (multi)
151  {
152  ret = multi->session[TM_ACTIVE].common_name;
153  }
154  if (ret && strlen(ret))
155  {
156  return ret;
157  }
158  else if (null)
159  {
160  return NULL;
161  }
162  else
163  {
164  return "UNDEF";
165  }
166 }
167 
168 /*
169  * Lock the common name for the given tunnel.
170  */
171 void
173 {
174  const char *cn = multi->session[TM_ACTIVE].common_name;
175  if (cn && !multi->locked_cn)
176  {
177  multi->locked_cn = string_alloc(cn, NULL);
178  }
179 }
180 
181 /*
182  * Lock the username for the given tunnel
183  */
184 static bool
185 tls_lock_username(struct tls_multi *multi, const char *username)
186 {
187  if (multi->locked_username)
188  {
189  if (!username || strcmp(username, multi->locked_username))
190  {
191  msg(D_TLS_ERRORS, "TLS Auth Error: username attempted to change from '%s' to '%s' -- tunnel disabled",
192  multi->locked_username,
193  np(username));
194 
195  /* disable the tunnel */
196  tls_deauthenticate(multi);
197  return false;
198  }
199  }
200  else
201  {
202  if (username)
203  {
204  multi->locked_username = string_alloc(username, NULL);
205  }
206  }
207  return true;
208 }
209 
210 const char *
211 tls_username(const struct tls_multi *multi, const bool null)
212 {
213  const char *ret = NULL;
214  if (multi)
215  {
216  ret = multi->locked_username;
217  }
218  if (ret && strlen(ret))
219  {
220  return ret;
221  }
222  else if (null)
223  {
224  return NULL;
225  }
226  else
227  {
228  return "UNDEF";
229  }
230 }
231 
232 void
233 cert_hash_remember(struct tls_session *session, const int error_depth,
234  const struct buffer *cert_hash)
235 {
236  if (error_depth >= 0 && error_depth < MAX_CERT_DEPTH)
237  {
238  if (!session->cert_hash_set)
239  {
240  ALLOC_OBJ_CLEAR(session->cert_hash_set, struct cert_hash_set);
241  }
242  if (!session->cert_hash_set->ch[error_depth])
243  {
244  ALLOC_OBJ(session->cert_hash_set->ch[error_depth], struct cert_hash);
245  }
246 
247  struct cert_hash *ch = session->cert_hash_set->ch[error_depth];
248  ASSERT(sizeof(ch->sha256_hash) == BLEN(cert_hash));
249  memcpy(ch->sha256_hash, BPTR(cert_hash), sizeof(ch->sha256_hash));
250  }
251 }
252 
253 void
255 {
256  if (chs)
257  {
258  int i;
259  for (i = 0; i < MAX_CERT_DEPTH; ++i)
260  {
261  free(chs->ch[i]);
262  }
263  free(chs);
264  }
265 }
266 
267 bool
268 cert_hash_compare(const struct cert_hash_set *chs1, const struct cert_hash_set *chs2)
269 {
270  if (chs1 && chs2)
271  {
272  int i;
273  for (i = 0; i < MAX_CERT_DEPTH; ++i)
274  {
275  const struct cert_hash *ch1 = chs1->ch[i];
276  const struct cert_hash *ch2 = chs2->ch[i];
277 
278  if (!ch1 && !ch2)
279  {
280  continue;
281  }
282  else if (ch1 && ch2 && !memcmp(ch1->sha256_hash, ch2->sha256_hash,
283  sizeof(ch1->sha256_hash)))
284  {
285  continue;
286  }
287  else
288  {
289  return false;
290  }
291  }
292  return true;
293  }
294  else if (!chs1 && !chs2)
295  {
296  return true;
297  }
298  else
299  {
300  return false;
301  }
302 }
303 
304 static struct cert_hash_set *
305 cert_hash_copy(const struct cert_hash_set *chs)
306 {
307  struct cert_hash_set *dest = NULL;
308  if (chs)
309  {
310  int i;
311  ALLOC_OBJ_CLEAR(dest, struct cert_hash_set);
312  for (i = 0; i < MAX_CERT_DEPTH; ++i)
313  {
314  const struct cert_hash *ch = chs->ch[i];
315  if (ch)
316  {
317  ALLOC_OBJ(dest->ch[i], struct cert_hash);
318  memcpy(dest->ch[i]->sha256_hash, ch->sha256_hash,
319  sizeof(dest->ch[i]->sha256_hash));
320  }
321  }
322  }
323  return dest;
324 }
325 void
327 {
328  const struct cert_hash_set *chs = multi->session[TM_ACTIVE].cert_hash_set;
329  if (chs && !multi->locked_cert_hash_set)
330  {
331  multi->locked_cert_hash_set = cert_hash_copy(chs);
332  }
333 }
334 
335 /*
336  * Returns the string associated with the given certificate type.
337  */
338 static const char *
340 {
341  switch (type)
342  {
344  return "SERVER";
345 
347  return "CLIENT";
348 
349  default:
350  return "?";
351  }
352 }
353 
354 /*
355  * Verify the peer's certificate fields.
356  *
357  * @param opt the tls options to verify against
358  * @param peer_cert the peer's certificate
359  * @param subject the peer's extracted subject name
360  * @param subject the peer's extracted common name
361  */
362 static result_t
363 verify_peer_cert(const struct tls_options *opt, openvpn_x509_cert_t *peer_cert,
364  const char *subject, const char *common_name)
365 {
366  /* verify certificate nsCertType */
367  if (opt->ns_cert_type != NS_CERT_CHECK_NONE)
368  {
369  if (SUCCESS == x509_verify_ns_cert_type(peer_cert, opt->ns_cert_type))
370  {
371  msg(D_HANDSHAKE, "VERIFY OK: nsCertType=%s",
373  }
374  else
375  {
376  msg(D_HANDSHAKE, "VERIFY nsCertType ERROR: %s, require nsCertType=%s",
377  subject, print_nsCertType(opt->ns_cert_type));
378  return FAILURE; /* Reject connection */
379  }
380  }
381 
382  /* verify certificate ku */
383  if (opt->remote_cert_ku[0] != 0)
384  {
385  if (SUCCESS == x509_verify_cert_ku(peer_cert, opt->remote_cert_ku, MAX_PARMS))
386  {
387  msg(D_HANDSHAKE, "VERIFY KU OK");
388  }
389  else
390  {
391  msg(D_HANDSHAKE, "VERIFY KU ERROR");
392  return FAILURE; /* Reject connection */
393  }
394  }
395 
396  /* verify certificate eku */
397  if (opt->remote_cert_eku != NULL)
398  {
399  if (SUCCESS == x509_verify_cert_eku(peer_cert, opt->remote_cert_eku))
400  {
401  msg(D_HANDSHAKE, "VERIFY EKU OK");
402  }
403  else
404  {
405  msg(D_HANDSHAKE, "VERIFY EKU ERROR");
406  return FAILURE; /* Reject connection */
407  }
408  }
409 
410  /* verify X509 name or username against --verify-x509-[user]name */
412  {
414  && strcmp(opt->verify_x509_name, subject) == 0)
416  && strcmp(opt->verify_x509_name, common_name) == 0)
418  && strncmp(opt->verify_x509_name, common_name,
419  strlen(opt->verify_x509_name)) == 0) )
420  {
421  msg(D_HANDSHAKE, "VERIFY X509NAME OK: %s", subject);
422  }
423  else
424  {
425  msg(D_HANDSHAKE, "VERIFY X509NAME ERROR: %s, must be %s",
426  subject, opt->verify_x509_name);
427  return FAILURE; /* Reject connection */
428  }
429  }
430 
431  return SUCCESS;
432 }
433 
434 /*
435  * Export the subject, common_name, and raw certificate fields to the
436  * environment for later verification by scripts and plugins.
437  */
438 static void
439 verify_cert_set_env(struct env_set *es, openvpn_x509_cert_t *peer_cert, int cert_depth,
440  const char *subject, const char *common_name,
441  const struct x509_track *x509_track)
442 {
443  char envname[64];
444  char *serial = NULL;
445  struct gc_arena gc = gc_new();
446 
447  /* Save X509 fields in environment */
448  if (x509_track)
449  {
450  x509_setenv_track(x509_track, es, cert_depth, peer_cert);
451  }
452  else
453  {
454  x509_setenv(es, cert_depth, peer_cert);
455  }
456 
457  /* export subject name string as environmental variable */
458  openvpn_snprintf(envname, sizeof(envname), "tls_id_%d", cert_depth);
459  setenv_str(es, envname, subject);
460 
461 #if 0
462  /* export common name string as environmental variable */
463  openvpn_snprintf(envname, sizeof(envname), "tls_common_name_%d", cert_depth);
464  setenv_str(es, envname, common_name);
465 #endif
466 
467  /* export X509 cert fingerprints */
468  {
469  struct buffer sha1 = x509_get_sha1_fingerprint(peer_cert, &gc);
470  struct buffer sha256 = x509_get_sha256_fingerprint(peer_cert, &gc);
471 
472  openvpn_snprintf(envname, sizeof(envname), "tls_digest_%d", cert_depth);
473  setenv_str(es, envname,
474  format_hex_ex(BPTR(&sha1), BLEN(&sha1), 0, 1, ":", &gc));
475 
476  openvpn_snprintf(envname, sizeof(envname), "tls_digest_sha256_%d",
477  cert_depth);
478  setenv_str(es, envname,
479  format_hex_ex(BPTR(&sha256), BLEN(&sha256), 0, 1, ":", &gc));
480  }
481 
482  /* export serial number as environmental variable */
483  serial = backend_x509_get_serial(peer_cert, &gc);
484  openvpn_snprintf(envname, sizeof(envname), "tls_serial_%d", cert_depth);
485  setenv_str(es, envname, serial);
486 
487  /* export serial number in hex as environmental variable */
488  serial = backend_x509_get_serial_hex(peer_cert, &gc);
489  openvpn_snprintf(envname, sizeof(envname), "tls_serial_hex_%d", cert_depth);
490  setenv_str(es, envname, serial);
491 
492  gc_free(&gc);
493 }
494 
495 /*
496  * call --tls-verify plug-in(s)
497  */
498 static result_t
499 verify_cert_call_plugin(const struct plugin_list *plugins, struct env_set *es,
500  int cert_depth, openvpn_x509_cert_t *cert, char *subject)
501 {
503  {
504  int ret;
505  struct argv argv = argv_new();
506 
507  argv_printf(&argv, "%d %s", cert_depth, subject);
508 
509  ret = plugin_call_ssl(plugins, OPENVPN_PLUGIN_TLS_VERIFY, &argv, NULL, es, cert_depth, cert);
510 
511  argv_reset(&argv);
512 
513  if (ret == OPENVPN_PLUGIN_FUNC_SUCCESS)
514  {
515  msg(D_HANDSHAKE, "VERIFY PLUGIN OK: depth=%d, %s",
516  cert_depth, subject);
517  }
518  else
519  {
520  msg(D_HANDSHAKE, "VERIFY PLUGIN ERROR: depth=%d, %s",
521  cert_depth, subject);
522  return FAILURE; /* Reject connection */
523  }
524  }
525  return SUCCESS;
526 }
527 
528 static const char *
529 verify_cert_export_cert(openvpn_x509_cert_t *peercert, const char *tmp_dir, struct gc_arena *gc)
530 {
531  FILE *peercert_file;
532  const char *peercert_filename = "";
533 
534  /* create tmp file to store peer cert */
535  if (!tmp_dir
536  || !(peercert_filename = platform_create_temp_file(tmp_dir, "pcf", gc)))
537  {
538  msg(M_NONFATAL, "Failed to create peer cert file");
539  return NULL;
540  }
541 
542  /* write peer-cert in tmp-file */
543  peercert_file = fopen(peercert_filename, "w+");
544  if (!peercert_file)
545  {
546  msg(M_NONFATAL|M_ERRNO, "Failed to open temporary file: %s",
547  peercert_filename);
548  return NULL;
549  }
550 
551  if (SUCCESS != x509_write_pem(peercert_file, peercert))
552  {
553  msg(M_NONFATAL, "Error writing PEM file containing certificate");
554  (void) platform_unlink(peercert_filename);
555  peercert_filename = NULL;
556  }
557 
558  fclose(peercert_file);
559  return peercert_filename;
560 }
561 
562 
563 /*
564  * run --tls-verify script
565  */
566 static result_t
567 verify_cert_call_command(const char *verify_command, struct env_set *es,
568  int cert_depth, openvpn_x509_cert_t *cert, char *subject, const char *verify_export_cert)
569 {
570  const char *tmp_file = NULL;
571  int ret;
572  struct gc_arena gc = gc_new();
573  struct argv argv = argv_new();
574 
575  setenv_str(es, "script_type", "tls-verify");
576 
577  if (verify_export_cert)
578  {
579  tmp_file = verify_cert_export_cert(cert, verify_export_cert, &gc);
580  if (!tmp_file)
581  {
582  ret = false;
583  goto cleanup;
584  }
585  setenv_str(es, "peer_cert", tmp_file);
586  }
587 
588  argv_parse_cmd(&argv, verify_command);
589  argv_printf_cat(&argv, "%d %s", cert_depth, subject);
590 
591  argv_msg_prefix(D_TLS_DEBUG, &argv, "TLS: executing verify command");
592  ret = openvpn_run_script(&argv, es, 0, "--tls-verify script");
593 
594  if (verify_export_cert)
595  {
596  if (tmp_file)
597  {
598  platform_unlink(tmp_file);
599  }
600  }
601 
602 cleanup:
603  gc_free(&gc);
604  argv_reset(&argv);
605 
606  if (ret)
607  {
608  msg(D_HANDSHAKE, "VERIFY SCRIPT OK: depth=%d, %s",
609  cert_depth, subject);
610  return SUCCESS;
611  }
612 
613  msg(D_HANDSHAKE, "VERIFY SCRIPT ERROR: depth=%d, %s",
614  cert_depth, subject);
615  return FAILURE; /* Reject connection */
616 }
617 
618 /*
619  * check peer cert against CRL directory
620  */
621 static result_t
622 verify_check_crl_dir(const char *crl_dir, openvpn_x509_cert_t *cert)
623 {
624  result_t ret = FAILURE;
625  char fn[256];
626  int fd = -1;
627  struct gc_arena gc = gc_new();
628 
629  char *serial = backend_x509_get_serial(cert, &gc);
630 
631  if (!openvpn_snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, OS_SPECIFIC_DIRSEP, serial))
632  {
633  msg(D_HANDSHAKE, "VERIFY CRL: filename overflow");
634  goto cleanup;
635  }
636  fd = platform_open(fn, O_RDONLY, 0);
637  if (fd >= 0)
638  {
639  msg(D_HANDSHAKE, "VERIFY CRL: certificate serial number %s is revoked", serial);
640  goto cleanup;
641  }
642 
643  ret = SUCCESS;
644 
645 cleanup:
646 
647  if (fd != -1)
648  {
649  close(fd);
650  }
651  gc_free(&gc);
652  return ret;
653 }
654 
655 result_t
656 verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
657 {
658  result_t ret = FAILURE;
659  char *subject = NULL;
660  char common_name[TLS_USERNAME_LEN+1] = {0}; /* null-terminated */
661  const struct tls_options *opt;
662  struct gc_arena gc = gc_new();
663 
664  opt = session->opt;
665  ASSERT(opt);
666 
667  session->verified = false;
668 
669  /* get the X509 name */
670  subject = x509_get_subject(cert, &gc);
671  if (!subject)
672  {
673  msg(D_TLS_ERRORS, "VERIFY ERROR: depth=%d, could not extract X509 "
674  "subject string from certificate", cert_depth);
675  goto cleanup;
676  }
677 
678  /* enforce character class restrictions in X509 name */
679  string_mod_remap_name(subject);
680  string_replace_leading(subject, '-', '_');
681 
682  /* extract the username (default is CN) */
683  if (SUCCESS != backend_x509_get_username(common_name, sizeof(common_name),
684  opt->x509_username_field, cert))
685  {
686  if (!cert_depth)
687  {
688  msg(D_TLS_ERRORS, "VERIFY ERROR: could not extract %s from X509 "
689  "subject string ('%s') -- note that the username length is "
690  "limited to %d characters",
691  opt->x509_username_field,
692  subject,
694  goto cleanup;
695  }
696  }
697 
698  /* enforce character class restrictions in common name */
699  string_mod_remap_name(common_name);
700 
701  /* warn if cert chain is too deep */
702  if (cert_depth >= MAX_CERT_DEPTH)
703  {
704  msg(D_TLS_ERRORS, "TLS Error: Convoluted certificate chain detected with depth [%d] greater than %d", cert_depth, MAX_CERT_DEPTH);
705  goto cleanup; /* Reject connection */
706  }
707 
708  /* verify level 1 cert, i.e. the CA that signed our leaf cert */
709  if (cert_depth == 1 && opt->verify_hash)
710  {
711  struct buffer ca_hash = {0};
712 
713  switch (opt->verify_hash_algo)
714  {
715  case MD_SHA1:
716  ca_hash = x509_get_sha1_fingerprint(cert, &gc);
717  break;
718 
719  case MD_SHA256:
720  ca_hash = x509_get_sha256_fingerprint(cert, &gc);
721  break;
722 
723  default:
724  /* This should normally not happen at all; the algorithm used
725  * is parsed by add_option() [options.c] and set to a predefined
726  * value in an enumerated type. So if this unlikely scenario
727  * happens, consider this a failure
728  */
729  msg(M_WARN, "Unexpected invalid algorithm used with "
730  "--verify-hash (%i)", opt->verify_hash_algo);
731  ret = FAILURE;
732  goto cleanup;
733  }
734 
735  if (memcmp(BPTR(&ca_hash), opt->verify_hash, BLEN(&ca_hash)))
736  {
737  msg(D_TLS_ERRORS, "TLS Error: level-1 certificate hash verification failed");
738  goto cleanup;
739  }
740  }
741 
742  /* save common name in session object */
743  if (cert_depth == 0)
744  {
745  set_common_name(session, common_name);
746  }
747 
748  session->verify_maxlevel = max_int(session->verify_maxlevel, cert_depth);
749 
750  /* export certificate values to the environment */
751  verify_cert_set_env(opt->es, cert, cert_depth, subject, common_name,
752  opt->x509_track);
753 
754  /* export current untrusted IP */
755  setenv_untrusted(session);
756 
757  /* If this is the peer's own certificate, verify it */
758  if (cert_depth == 0 && SUCCESS != verify_peer_cert(opt, cert, subject, common_name))
759  {
760  goto cleanup;
761  }
762 
763  /* call --tls-verify plug-in(s), if registered */
764  if (SUCCESS != verify_cert_call_plugin(opt->plugins, opt->es, cert_depth, cert, subject))
765  {
766  goto cleanup;
767  }
768 
769  /* run --tls-verify script */
771  opt->es, cert_depth, cert, subject, opt->verify_export_cert))
772  {
773  goto cleanup;
774  }
775 
776  /* check peer cert against CRL */
777  if (opt->crl_file)
778  {
779  if (opt->ssl_flags & SSLF_CRL_VERIFY_DIR)
780  {
781  if (SUCCESS != verify_check_crl_dir(opt->crl_file, cert))
782  {
783  goto cleanup;
784  }
785  }
786  else
787  {
788  if (tls_verify_crl_missing(opt))
789  {
790  msg(D_TLS_ERRORS, "VERIFY ERROR: CRL not loaded");
791  goto cleanup;
792  }
793  }
794  }
795 
796  msg(D_HANDSHAKE, "VERIFY OK: depth=%d, %s", cert_depth, subject);
797  session->verified = true;
798  ret = SUCCESS;
799 
800 cleanup:
801 
802  if (ret != SUCCESS)
803  {
804  tls_clear_error(); /* always? */
805  session->verified = false; /* double sure? */
806  }
807  gc_free(&gc);
808 
809  return ret;
810 }
811 
812 /* ***************************************************************************
813 * Functions for the management of deferred authentication when using
814 * user/password authentication.
815 *************************************************************************** */
816 
817 #ifdef ENABLE_DEF_AUTH
818 /* key_state_test_auth_control_file return values,
819  * NOTE: acf_merge indexing depends on these values */
820 #define ACF_UNDEFINED 0
821 #define ACF_SUCCEEDED 1
822 #define ACF_DISABLED 2
823 #define ACF_FAILED 3
824 #endif
825 
826 #ifdef MANAGEMENT_DEF_AUTH
827 void
828 man_def_auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
829 {
830  if (multi->client_reason)
831  {
832  free(multi->client_reason);
833  multi->client_reason = NULL;
834  }
835  if (client_reason && strlen(client_reason))
836  {
837  /* FIXME: Last alloc will never be freed */
838  multi->client_reason = string_alloc(client_reason, NULL);
839  }
840 }
841 
842 static inline unsigned int
843 man_def_auth_test(const struct key_state *ks)
844 {
845  if (management_enable_def_auth(management))
846  {
847  return ks->mda_status;
848  }
849  else
850  {
851  return ACF_DISABLED;
852  }
853 }
854 #endif /* ifdef MANAGEMENT_DEF_AUTH */
855 
856 #ifdef PLUGIN_DEF_AUTH
857 
858 /*
859  * auth_control_file functions
860  */
861 
862 void
864 {
865  if (ks && ks->auth_control_file)
866  {
867  platform_unlink(ks->auth_control_file);
868  free(ks->auth_control_file);
869  ks->auth_control_file = NULL;
870  }
871 }
872 
873 static bool
874 key_state_gen_auth_control_file(struct key_state *ks, const struct tls_options *opt)
875 {
876  struct gc_arena gc = gc_new();
877 
879  const char *acf = platform_create_temp_file(opt->tmp_dir, "acf", &gc);
880  if (acf)
881  {
882  ks->auth_control_file = string_alloc(acf, NULL);
883  setenv_str(opt->es, "auth_control_file", ks->auth_control_file);
884  }
885 
886  gc_free(&gc);
887  return acf;
888 }
889 
890 static unsigned int
891 key_state_test_auth_control_file(struct key_state *ks)
892 {
893  if (ks && ks->auth_control_file)
894  {
895  unsigned int ret = ks->auth_control_status;
896  if (ret == ACF_UNDEFINED)
897  {
898  FILE *fp = fopen(ks->auth_control_file, "r");
899  if (fp)
900  {
901  const int c = fgetc(fp);
902  if (c == '1')
903  {
904  ret = ACF_SUCCEEDED;
905  }
906  else if (c == '0')
907  {
908  ret = ACF_FAILED;
909  }
910  fclose(fp);
911  ks->auth_control_status = ret;
912  }
913  }
914  return ret;
915  }
916  return ACF_DISABLED;
917 }
918 
919 #endif /* ifdef PLUGIN_DEF_AUTH */
920 
921 /*
922  * Return current session authentication state. Return
923  * value is TLS_AUTHENTICATION_x.
924  */
925 
926 int
927 tls_authentication_status(struct tls_multi *multi, const int latency)
928 {
929  bool deferred = false;
930  bool success = false;
931  bool active = false;
932 
933 #ifdef ENABLE_DEF_AUTH
934  static const unsigned char acf_merge[] =
935  {
936  ACF_UNDEFINED, /* s1=ACF_UNDEFINED s2=ACF_UNDEFINED */
937  ACF_UNDEFINED, /* s1=ACF_UNDEFINED s2=ACF_SUCCEEDED */
938  ACF_UNDEFINED, /* s1=ACF_UNDEFINED s2=ACF_DISABLED */
939  ACF_FAILED, /* s1=ACF_UNDEFINED s2=ACF_FAILED */
940  ACF_UNDEFINED, /* s1=ACF_SUCCEEDED s2=ACF_UNDEFINED */
941  ACF_SUCCEEDED, /* s1=ACF_SUCCEEDED s2=ACF_SUCCEEDED */
942  ACF_SUCCEEDED, /* s1=ACF_SUCCEEDED s2=ACF_DISABLED */
943  ACF_FAILED, /* s1=ACF_SUCCEEDED s2=ACF_FAILED */
944  ACF_UNDEFINED, /* s1=ACF_DISABLED s2=ACF_UNDEFINED */
945  ACF_SUCCEEDED, /* s1=ACF_DISABLED s2=ACF_SUCCEEDED */
946  ACF_DISABLED, /* s1=ACF_DISABLED s2=ACF_DISABLED */
947  ACF_FAILED, /* s1=ACF_DISABLED s2=ACF_FAILED */
948  ACF_FAILED, /* s1=ACF_FAILED s2=ACF_UNDEFINED */
949  ACF_FAILED, /* s1=ACF_FAILED s2=ACF_SUCCEEDED */
950  ACF_FAILED, /* s1=ACF_FAILED s2=ACF_DISABLED */
951  ACF_FAILED /* s1=ACF_FAILED s2=ACF_FAILED */
952  };
953 #endif /* ENABLE_DEF_AUTH */
954 
955  if (multi)
956  {
957  int i;
958 
959 #ifdef ENABLE_DEF_AUTH
960  if (latency && multi->tas_last && multi->tas_last + latency >= now)
961  {
963  }
964  multi->tas_last = now;
965 #endif /* ENABLE_DEF_AUTH */
966 
967  for (i = 0; i < KEY_SCAN_SIZE; ++i)
968  {
969  struct key_state *ks = multi->key_scan[i];
970  if (DECRYPT_KEY_ENABLED(multi, ks))
971  {
972  active = true;
973  if (ks->authenticated)
974  {
975 #ifdef ENABLE_DEF_AUTH
976  unsigned int s1 = ACF_DISABLED;
977  unsigned int s2 = ACF_DISABLED;
978 #ifdef PLUGIN_DEF_AUTH
979  s1 = key_state_test_auth_control_file(ks);
980 #endif /* PLUGIN_DEF_AUTH */
981 #ifdef MANAGEMENT_DEF_AUTH
982  s2 = man_def_auth_test(ks);
983 #endif /* MANAGEMENT_DEF_AUTH */
984  ASSERT(s1 < 4 && s2 < 4);
985  switch (acf_merge[(s1<<2) + s2])
986  {
987  case ACF_SUCCEEDED:
988  case ACF_DISABLED:
989  success = true;
990  ks->auth_deferred = false;
991  break;
992 
993  case ACF_UNDEFINED:
994  if (now < ks->auth_deferred_expire)
995  {
996  deferred = true;
997  }
998  break;
999 
1000  case ACF_FAILED:
1001  ks->authenticated = false;
1002  break;
1003 
1004  default:
1005  ASSERT(0);
1006  }
1007 #else /* !ENABLE_DEF_AUTH */
1008  success = true;
1009 #endif /* ENABLE_DEF_AUTH */
1010  }
1011  }
1012  }
1013  }
1014 
1015 #if 0
1016  dmsg(D_TLS_ERRORS, "TAS: a=%d s=%d d=%d", active, success, deferred);
1017 #endif
1018 
1019  if (success)
1020  {
1022  }
1023  else if (!active || deferred)
1024  {
1026  }
1027  else
1028  {
1030  }
1031 }
1032 
1033 #ifdef MANAGEMENT_DEF_AUTH
1034 /*
1035  * For deferred auth, this is where the management interface calls (on server)
1036  * to indicate auth failure/success.
1037  */
1038 bool
1039 tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, const bool auth, const char *client_reason)
1040 {
1041  bool ret = false;
1042  if (multi)
1043  {
1044  int i;
1045  man_def_auth_set_client_reason(multi, client_reason);
1046  for (i = 0; i < KEY_SCAN_SIZE; ++i)
1047  {
1048  struct key_state *ks = multi->key_scan[i];
1049  if (ks->mda_key_id == mda_key_id)
1050  {
1051  ks->mda_status = auth ? ACF_SUCCEEDED : ACF_FAILED;
1052  ret = true;
1053  }
1054  }
1055  }
1056  return ret;
1057 }
1058 #endif /* ifdef MANAGEMENT_DEF_AUTH */
1059 
1060 
1061 /* ****************************************************************************
1062  * Functions to verify username and password
1063  *
1064  * Authenticate a client using username/password.
1065  * Runs on server.
1066  *
1067  * If you want to add new authentication methods,
1068  * this is the place to start.
1069  *************************************************************************** */
1070 
1071 /*
1072  * Verify the user name and password using a script
1073  */
1074 static bool
1076 {
1077  struct gc_arena gc = gc_new();
1078  struct argv argv = argv_new();
1079  const char *tmp_file = "";
1080  bool ret = false;
1081 
1082  /* Is username defined? */
1083  if ((session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL) || strlen(up->username))
1084  {
1085  /* Set environmental variables prior to calling script */
1086  setenv_str(session->opt->es, "script_type", "user-pass-verify");
1087 
1089  {
1090  struct status_output *so;
1091 
1092  tmp_file = platform_create_temp_file(session->opt->tmp_dir, "up",
1093  &gc);
1094  if (tmp_file)
1095  {
1096  so = status_open(tmp_file, 0, -1, NULL, STATUS_OUTPUT_WRITE);
1097  status_printf(so, "%s", up->username);
1098  status_printf(so, "%s", up->password);
1099  if (!status_close(so))
1100  {
1101  msg(D_TLS_ERRORS, "TLS Auth Error: could not write username/password to file: %s",
1102  tmp_file);
1103  goto done;
1104  }
1105  }
1106  else
1107  {
1108  msg(D_TLS_ERRORS, "TLS Auth Error: could not create write "
1109  "username/password to temp file");
1110  }
1111  }
1112  else
1113  {
1114  setenv_str(session->opt->es, "username", up->username);
1115  setenv_str(session->opt->es, "password", up->password);
1116  }
1117 
1118  /* setenv incoming cert common name for script */
1119  setenv_str(session->opt->es, "common_name", session->common_name);
1120 
1121  /* setenv client real IP address */
1122  setenv_untrusted(session);
1123 
1124  /* format command line */
1126  argv_printf_cat(&argv, "%s", tmp_file);
1127 
1128  /* call command */
1129  ret = openvpn_run_script(&argv, session->opt->es, 0,
1130  "--auth-user-pass-verify");
1131 
1133  {
1134  setenv_del(session->opt->es, "password");
1135  }
1136  }
1137  else
1138  {
1139  msg(D_TLS_ERRORS, "TLS Auth Error: peer provided a blank username");
1140  }
1141 
1142 done:
1143  if (tmp_file && strlen(tmp_file) > 0)
1144  {
1145  platform_unlink(tmp_file);
1146  }
1147 
1148  argv_reset(&argv);
1149  gc_free(&gc);
1150  return ret;
1151 }
1152 
1153 /*
1154  * Verify the username and password using a plugin
1155  */
1156 static int
1158 {
1159  int retval = OPENVPN_PLUGIN_FUNC_ERROR;
1160 #ifdef PLUGIN_DEF_AUTH
1161  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1162 #endif
1163 
1164  /* Is username defined? */
1165  if ((session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL) || strlen(up->username))
1166  {
1167  /* set username/password in private env space */
1168  setenv_str(session->opt->es, "username", up->username);
1169  setenv_str(session->opt->es, "password", up->password);
1170 
1171  /* setenv incoming cert common name for script */
1172  setenv_str(session->opt->es, "common_name", session->common_name);
1173 
1174  /* setenv client real IP address */
1175  setenv_untrusted(session);
1176 
1177 #ifdef PLUGIN_DEF_AUTH
1178  /* generate filename for deferred auth control file */
1179  if (!key_state_gen_auth_control_file(ks, session->opt))
1180  {
1181  msg(D_TLS_ERRORS, "TLS Auth Error (%s): "
1182  "could not create deferred auth control file", __func__);
1183  goto cleanup;
1184  }
1185 #endif
1186 
1187  /* call command */
1188  retval = plugin_call(session->opt->plugins, OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY, NULL, NULL, session->opt->es);
1189 
1190 #ifdef PLUGIN_DEF_AUTH
1191  /* purge auth control filename (and file itself) for non-deferred returns */
1192  if (retval != OPENVPN_PLUGIN_FUNC_DEFERRED)
1193  {
1195  }
1196 #endif
1197 
1198  setenv_del(session->opt->es, "password");
1199  }
1200  else
1201  {
1202  msg(D_TLS_ERRORS, "TLS Auth Error (verify_user_pass_plugin): peer provided a blank username");
1203  }
1204 
1205 cleanup:
1206  return retval;
1207 }
1208 
1209 
1210 #ifdef MANAGEMENT_DEF_AUTH
1211 /*
1212  * MANAGEMENT_DEF_AUTH internal ssl_verify.c status codes
1213  */
1214 #define KMDA_ERROR 0
1215 #define KMDA_SUCCESS 1
1216 #define KMDA_UNDEF 2
1217 #define KMDA_DEF 3
1218 
1219 static int
1220 verify_user_pass_management(struct tls_session *session, const struct user_pass *up)
1221 {
1222  int retval = KMDA_ERROR;
1223  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1224 
1225  /* Is username defined? */
1226  if ((session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL) || strlen(up->username))
1227  {
1228  /* set username/password in private env space */
1229  setenv_str(session->opt->es, "username", up->username);
1230  setenv_str(session->opt->es, "password", up->password);
1231 
1232  /* setenv incoming cert common name for script */
1233  setenv_str(session->opt->es, "common_name", session->common_name);
1234 
1235  /* setenv client real IP address */
1236  setenv_untrusted(session);
1237 
1238  if (management)
1239  {
1240  management_notify_client_needing_auth(management, ks->mda_key_id, session->opt->mda_context, session->opt->es);
1241  }
1242 
1243  setenv_del(session->opt->es, "password");
1244 
1245  retval = KMDA_SUCCESS;
1246  }
1247  else
1248  {
1249  msg(D_TLS_ERRORS, "TLS Auth Error (verify_user_pass_management): peer provided a blank username");
1250  }
1251 
1252  return retval;
1253 }
1254 #endif /* ifdef MANAGEMENT_DEF_AUTH */
1255 
1256 /*
1257  * Main username/password verification entry point
1258  */
1259 void
1260 verify_user_pass(struct user_pass *up, struct tls_multi *multi,
1261  struct tls_session *session)
1262 {
1263  int s1 = OPENVPN_PLUGIN_FUNC_SUCCESS;
1264  bool s2 = true;
1265  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1266 
1267 #ifdef MANAGEMENT_DEF_AUTH
1268  int man_def_auth = KMDA_UNDEF;
1269 
1270  if (management_enable_def_auth(management))
1271  {
1272  man_def_auth = KMDA_DEF;
1273  }
1274 #endif
1275 
1276  /* enforce character class restrictions in username/password */
1278  string_mod(up->password, CC_PRINT, CC_CRLF, '_');
1279 
1280  /* If server is configured with --auth-gen-token and we have an
1281  * authentication token for this client, this authentication
1282  * round will be done internally using the token instead of
1283  * calling any external authentication modules.
1284  */
1285  if (session->opt->auth_token_generate && multi->auth_token_sent
1286  && NULL != multi->auth_token)
1287  {
1288  unsigned int ssl_flags = session->opt->ssl_flags;
1289 
1290  /* Ensure that the username has not changed */
1291  if (!tls_lock_username(multi, up->username))
1292  {
1293  /* auth-token cleared in tls_lock_username() on failure */
1294  ks->authenticated = false;
1295  return;
1296  }
1297 
1298  /* If auth-token lifetime has been enabled,
1299  * ensure the token has not expired
1300  */
1301  if (session->opt->auth_token_lifetime > 0
1302  && (multi->auth_token_tstamp + session->opt->auth_token_lifetime) < now)
1303  {
1304  msg(D_HANDSHAKE, "Auth-token for client expired\n");
1305  wipe_auth_token(multi);
1306  ks->authenticated = false;
1307  return;
1308  }
1309 
1310  /* The core authentication of the token itself */
1311  if (memcmp_constant_time(multi->auth_token, up->password,
1312  strlen(multi->auth_token)) != 0)
1313  {
1314  ks->authenticated = false;
1315  tls_deauthenticate(multi);
1316 
1317  msg(D_TLS_ERRORS, "TLS Auth Error: Auth-token verification "
1318  "failed for username '%s' %s", up->username,
1319  (ssl_flags & SSLF_USERNAME_AS_COMMON_NAME) ? "[CN SET]" : "");
1320  }
1321  else
1322  {
1323  ks->authenticated = true;
1324 
1325  if (ssl_flags & SSLF_USERNAME_AS_COMMON_NAME)
1326  {
1327  set_common_name(session, up->username);
1328  }
1329  msg(D_HANDSHAKE, "TLS: Username/auth-token authentication "
1330  "succeeded for username '%s' %s",
1331  up->username,
1332  (ssl_flags & SSLF_USERNAME_AS_COMMON_NAME) ? "[CN SET]" : "");
1333  }
1334  return;
1335  }
1336 
1337  /* call plugin(s) and/or script */
1338 #ifdef MANAGEMENT_DEF_AUTH
1339  if (man_def_auth == KMDA_DEF)
1340  {
1341  man_def_auth = verify_user_pass_management(session, up);
1342  }
1343 #endif
1345  {
1346  s1 = verify_user_pass_plugin(session, up);
1347  }
1348  if (session->opt->auth_user_pass_verify_script)
1349  {
1350  s2 = verify_user_pass_script(session, up);
1351  }
1352 
1353  /* check sizing of username if it will become our common name */
1354  if ((session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME) && strlen(up->username) > TLS_USERNAME_LEN)
1355  {
1356  msg(D_TLS_ERRORS, "TLS Auth Error: --username-as-common name specified and username is longer than the maximum permitted Common Name length of %d characters", TLS_USERNAME_LEN);
1358  }
1359 
1360  /* auth succeeded? */
1361  if ((s1 == OPENVPN_PLUGIN_FUNC_SUCCESS
1362 #ifdef PLUGIN_DEF_AUTH
1364 #endif
1365  ) && s2
1366 #ifdef MANAGEMENT_DEF_AUTH
1367  && man_def_auth != KMDA_ERROR
1368 #endif
1369  && tls_lock_username(multi, up->username))
1370  {
1371  ks->authenticated = true;
1372 #ifdef PLUGIN_DEF_AUTH
1373  if (s1 == OPENVPN_PLUGIN_FUNC_DEFERRED)
1374  {
1375  ks->auth_deferred = true;
1376  }
1377 #endif
1378 #ifdef MANAGEMENT_DEF_AUTH
1379  if (man_def_auth != KMDA_UNDEF)
1380  {
1381  ks->auth_deferred = true;
1382  }
1383 #endif
1384 
1385  if ((session->opt->auth_token_generate) && (NULL == multi->auth_token))
1386  {
1387  /* Server is configured with --auth-gen-token but no token has yet
1388  * been generated for this client. Generate one and save it.
1389  */
1390  uint8_t tok[AUTH_TOKEN_SIZE];
1391 
1392  if (!rand_bytes(tok, AUTH_TOKEN_SIZE))
1393  {
1394  msg( M_FATAL, "Failed to get enough randomness for "
1395  "authentication token");
1396  }
1397 
1398  /* The token should be longer than the input when
1399  * being base64 encoded
1400  */
1402  &multi->auth_token) > AUTH_TOKEN_SIZE);
1403  multi->auth_token_tstamp = now;
1404  dmsg(D_SHOW_KEYS, "Generated token for client: %s",
1405  multi->auth_token);
1406  }
1407 
1408  if ((session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME))
1409  {
1410  set_common_name(session, up->username);
1411  }
1412 
1413 #ifdef ENABLE_DEF_AUTH
1414  msg(D_HANDSHAKE, "TLS: Username/Password authentication %s for username '%s' %s",
1415  ks->auth_deferred ? "deferred" : "succeeded",
1416  up->username,
1417  (session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME) ? "[CN SET]" : "");
1418 #else
1419  msg(D_HANDSHAKE, "TLS: Username/Password authentication %s for username '%s' %s",
1420  "succeeded",
1421  up->username,
1422  (session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME) ? "[CN SET]" : "");
1423 #endif
1424  }
1425  else
1426  {
1427  msg(D_TLS_ERRORS, "TLS Auth Error: Auth Username/Password verification failed for peer");
1428  }
1429 }
1430 
1431 void
1432 verify_final_auth_checks(struct tls_multi *multi, struct tls_session *session)
1433 {
1434  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1435 
1436  /* While it shouldn't really happen, don't allow the common name to be NULL */
1437  if (!session->common_name)
1438  {
1439  set_common_name(session, "");
1440  }
1441 
1442  /* Don't allow the CN to change once it's been locked */
1443  if (ks->authenticated && multi->locked_cn)
1444  {
1445  const char *cn = session->common_name;
1446  if (cn && strcmp(cn, multi->locked_cn))
1447  {
1448  msg(D_TLS_ERRORS, "TLS Auth Error: TLS object CN attempted to change from '%s' to '%s' -- tunnel disabled",
1449  multi->locked_cn,
1450  cn);
1451 
1452  /* change the common name back to its original value and disable the tunnel */
1453  set_common_name(session, multi->locked_cn);
1454  tls_deauthenticate(multi);
1455  }
1456  }
1457 
1458  /* Don't allow the cert hashes to change once they have been locked */
1459  if (ks->authenticated && multi->locked_cert_hash_set)
1460  {
1461  const struct cert_hash_set *chs = session->cert_hash_set;
1462  if (chs && !cert_hash_compare(chs, multi->locked_cert_hash_set))
1463  {
1464  msg(D_TLS_ERRORS, "TLS Auth Error: TLS object CN=%s client-provided SSL certs unexpectedly changed during mid-session reauth",
1465  session->common_name);
1466 
1467  /* disable the tunnel */
1468  tls_deauthenticate(multi);
1469  }
1470  }
1471 
1472  /* verify --client-config-dir based authentication */
1473  if (ks->authenticated && session->opt->client_config_dir_exclusive)
1474  {
1475  struct gc_arena gc = gc_new();
1476 
1477  const char *cn = session->common_name;
1478  const char *path = platform_gen_path(session->opt->client_config_dir_exclusive,
1479  cn, &gc);
1480  if (!cn || !strcmp(cn, CCD_DEFAULT) || !platform_test_file(path))
1481  {
1482  ks->authenticated = false;
1483  wipe_auth_token(multi);
1484  msg(D_TLS_ERRORS, "TLS Auth Error: --client-config-dir authentication failed for common name '%s' file='%s'",
1485  session->common_name,
1486  path ? path : "UNDEF");
1487  }
1488 
1489  gc_free(&gc);
1490  }
1491 }
1492 
1493 void
1495 {
1496  struct env_item *item = es->list;
1497  while (item)
1498  {
1499  struct env_item *next = item->next;
1500  if (item->string
1501  && 0 == strncmp("X509_", item->string, strlen("X509_")))
1502  {
1503  env_set_del(es, item->string);
1504  }
1505  item = next;
1506  }
1507 }
static bool tls_lock_username(struct tls_multi *multi, const char *username)
Definition: ssl_verify.c:185
#define VERIFY_X509_SUBJECT_RDN
Definition: ssl_verify.h:65
#define TM_ACTIVE
Active tls_session.
Definition: ssl_common.h:465
const char * tmp_dir
Definition: ssl_common.h:320
Security parameter state of one TLS and data channel key session.
Definition: ssl_common.h:161
struct key_state key[KS_SIZE]
Definition: ssl_common.h:445
const char * crl_file
Definition: ssl_common.h:284
#define SSLF_CRL_VERIFY_DIR
Definition: ssl_common.h:344
static void verify_cert_set_env(struct env_set *es, openvpn_x509_cert_t *peer_cert, int cert_depth, const char *subject, const char *common_name, const struct x509_track *x509_track)
Definition: ssl_verify.c:439
#define ALLOC_OBJ(dptr, type)
Definition: buffer.h:1045
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:688
#define D_TLS_DEBUG
Definition: errlevel.h:159
struct argv argv_new(void)
Definition: argv.c:52
unsigned int auth_token_lifetime
Definition: ssl_common.h:324
#define STATUS_OUTPUT_WRITE
Definition: status.h:51
#define SSLF_USERNAME_AS_COMMON_NAME
Definition: ssl_common.h:341
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition: buffer.h:401
#define VERIFY_X509_SUBJECT_DN
Definition: ssl_verify.h:64
#define OS_SPECIFIC_DIRSEP
Definition: syshead.h:478
time_t auth_token_tstamp
timestamp of the generated token
Definition: ssl_common.h:572
struct tls_options * opt
Definition: ssl_common.h:410
int verify_maxlevel
Definition: ssl_common.h:430
#define SSLF_AUTH_USER_PASS_OPTIONAL
Definition: ssl_common.h:342
#define SA_IP_PORT
Definition: socket.h:401
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
bool verified
Definition: ssl_common.h:440
static int plugin_call(const struct plugin_list *pl, const int type, const struct argv *av, struct plugin_return *pr, struct env_set *es)
Definition: plugin.h:201
const char * auth_user_pass_verify_script
Definition: ssl_common.h:318
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:503
#define NS_CERT_CHECK_CLIENT
Do not perform Netscape certificate type verification.
Definition: ssl_verify.h:216
#define D_TLS_ERRORS
Definition: errlevel.h:59
#define CC_PRINT
Definition: buffer.h:915
X509 openvpn_x509_cert_t
#define ASSERT(x)
Definition: error.h:221
int tls_authentication_status(struct tls_multi *multi, const int latency)
Definition: ssl_verify.c:927
const char * client_config_dir_exclusive
Definition: ssl_common.h:327
int verify_x509_type
Definition: ssl_common.h:282
result_t x509_verify_cert_ku(openvpn_x509_cert_t *x509, const unsigned *const expected_ku, int expected_len)
struct cert_hash_set * cert_hash_set
Definition: ssl_common.h:434
int plugin_call_ssl(const struct plugin_list *pl, const int type, const struct argv *av, struct plugin_return *pr, struct env_set *es, int certdepth, openvpn_x509_cert_t *current_cert)
Definition: plugin.c:780
#define M_FATAL
Definition: error.h:94
void cert_hash_remember(struct tls_session *session, const int error_depth, const struct buffer *cert_hash)
Definition: ssl_verify.c:233
static const char * verify_cert_export_cert(openvpn_x509_cert_t *peercert, const char *tmp_dir, struct gc_arena *gc)
Definition: ssl_verify.c:529
static void tls_deauthenticate(struct tls_multi *multi)
Definition: ssl_verify.c:92
char * backend_x509_get_serial_hex(openvpn_x509_cert_t *cert, struct gc_arena *gc)
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition: env_set.c:285
#define MAX_PARMS
Definition: options.h:51
#define TLS_USERNAME_LEN
Maximum length of common name.
Definition: ssl_verify.c:49
#define DECRYPT_KEY_ENABLED(multi, ks)
Check whether the ks key_state is ready to receive data channel packets.
Definition: ssl_verify.h:89
bool auth_token_generate
Generate auth-tokens on successful user/pass auth, set via options->auth_token_generate.
Definition: ssl_common.h:322
static void string_mod_remap_name(char *str)
Definition: ssl_verify.c:52
Structure containing the hash for a single certificate.
Definition: ssl_verify.h:54
#define OPENVPN_PLUGIN_FUNC_SUCCESS
bool authenticated
Definition: ssl_common.h:201
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer...
Definition: ssl_common.h:578
Structure containing the hashes for a full certificate chain.
Definition: ssl_verify.h:59
bool platform_unlink(const char *filename)
Definition: platform.c:287
void status_printf(struct status_output *so, const char *format,...)
Definition: status.c:239
char username[USER_PASS_LEN]
Definition: misc.h:74
result_t verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
Definition: ssl_verify.c:656
char * x509_username_field
Definition: ssl_common.h:291
void setenv_del(struct env_set *es, const char *name)
Definition: env_set.c:330
struct status_output * status_open(const char *filename, const int refresh_freq, const int msglevel, const struct virtual_output *vout, const unsigned int flags)
Definition: status.c:63
#define VERIFY_X509_NONE
Definition: ssl_verify.h:63
#define TLS_AUTHENTICATION_UNDEFINED
Definition: ssl_verify.h:71
bool status_close(struct status_output *so)
Definition: status.c:203
struct key_state * key_scan[KEY_SCAN_SIZE]
List of key_state objects in the order they should be scanned by data channel modules.
Definition: ssl_common.h:511
#define BPTR(buf)
Definition: buffer.h:124
static const char * print_nsCertType(int type)
Definition: ssl_verify.c:339
#define KEY_SCAN_SIZE
Definition: ssl_common.h:487
bool openvpn_snprintf(char *str, size_t size, const char *format,...)
Definition: buffer.c:299
static result_t verify_check_crl_dir(const char *crl_dir, openvpn_x509_cert_t *cert)
Definition: ssl_verify.c:622
#define OPENVPN_PLUGIN_FUNC_DEFERRED
#define KS_SIZE
Size of the tls_session.key array.
Definition: ssl_common.h:381
struct env_item * list
Definition: env_set.h:44
bool auth_user_pass_verify_script_via_file
Definition: ssl_common.h:319
int platform_open(const char *path, int flags, int mode)
Definition: platform.c:315
#define OPENVPN_PLUGIN_TLS_VERIFY
#define AUTH_TOKEN_SIZE
Size of server side generated auth tokens.
Definition: ssl_common.h:385
void setenv_link_socket_actual(struct env_set *es, const char *name_prefix, const struct link_socket_actual *act, const unsigned int flags)
Definition: socket.c:3119
#define OPENVPN_PLUGIN_FUNC_ERROR
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
static const char * np(const char *str)
Definition: simple.c:108
static struct gc_arena gc_new(void)
Definition: buffer.h:1015
#define KS_PRIMARY
Primary key state index.
Definition: ssl_common.h:377
unsigned remote_cert_ku[MAX_PARMS]
Definition: ssl_common.h:287
time_t now
Definition: otime.c:36
void key_state_rm_auth_control_file(struct key_state *ks)
Remove the given key state&#39;s auth control file, if it exists.
void x509_setenv_track(const struct x509_track *xt, struct env_set *es, const int depth, openvpn_x509_cert_t *x509)
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition: buffer.h:1050
char * locked_cn
Definition: ssl_common.h:540
bool platform_test_file(const char *filename)
Return true if filename can be opened for read.
Definition: platform.c:461
bool env_set_del(struct env_set *es, const char *str)
Definition: env_set.c:185
static void wipe_auth_token(struct tls_multi *multi)
Wipes the authentication token out of the memory, frees and cleans up related buffers and flags...
Definition: ssl_verify.c:73
int ns_cert_type
Definition: ssl_common.h:286
#define MAX_CERT_DEPTH
Maximum certificate depth we will allow.
Definition: ssl_verify.h:51
result_t x509_verify_cert_eku(openvpn_x509_cert_t *x509, const char *const expected_oid)
hash_algo_type verify_hash_algo
Definition: ssl_common.h:290
unsigned __int32 uint32_t
Definition: config-msvc.h:121
#define NS_CERT_CHECK_NONE
Do not perform Netscape certificate type verification.
Definition: ssl_verify.h:212
void argv_msg_prefix(const int msglev, const struct argv *a, const char *prefix)
Definition: argv.c:214
struct cert_hash_set * locked_cert_hash_set
Definition: ssl_common.h:542
const char * verify_x509_name
Definition: ssl_common.h:283
char * x509_get_subject(openvpn_x509_cert_t *cert, struct gc_arena *gc)
bool cert_hash_compare(const struct cert_hash_set *chs1, const struct cert_hash_set *chs2)
Compares certificates hashes, returns true if hashes are equal.
Definition: ssl_verify.c:268
static result_t verify_peer_cert(const struct tls_options *opt, openvpn_x509_cert_t *peer_cert, const char *subject, const char *common_name)
Definition: ssl_verify.c:363
const char * platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc)
Put a directory and filename together.
Definition: platform.c:395
const struct plugin_list * plugins
Definition: ssl_common.h:331
void argv_parse_cmd(struct argv *a, const char *s)
Definition: argv.c:337
struct env_item * next
Definition: env_set.h:39
struct link_socket_actual untrusted_addr
Definition: ssl_common.h:443
char * string
Definition: env_set.h:38
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Definition: buffer.c:522
const char * verify_export_cert
Definition: ssl_common.h:281
int openvpn_base64_encode(const void *data, int size, char **str)
Definition: base64.c:54
#define VERIFY_X509_SUBJECT_RDN_PREFIX
Definition: ssl_verify.h:66
#define dmsg
Definition: error.h:174
char * backend_x509_get_serial(openvpn_x509_cert_t *cert, struct gc_arena *gc)
#define TM_SIZE
Size of the tls_multi.session array.
Definition: ssl_common.h:470
#define TLS_AUTHENTICATION_FAILED
Definition: ssl_verify.h:69
static void set_common_name(struct tls_session *session, const char *common_name)
Definition: ssl_verify.c:111
#define CCD_DEFAULT
Definition: common.h:71
static void setenv_untrusted(struct tls_session *session)
Definition: ssl_verify.c:61
#define BLEN(buf)
Definition: buffer.h:127
time_t auth_deferred_expire
Definition: ssl_common.h:202
void verify_user_pass(struct user_pass *up, struct tls_multi *multi, struct tls_session *session)
Verify the given username and password, using either an external script, a plugin, or the management interface.
Definition: ssl_verify.c:1260
#define D_HANDSHAKE
Definition: errlevel.h:72
static int max_int(int x, int y)
Definition: integer.h:44
uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
Definition: list.c:590
unsigned __int8 uint8_t
Definition: config-msvc.h:123
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Definition: buffer.c:1081
result_t x509_verify_ns_cert_type(openvpn_x509_cert_t *cert, const int usage)
struct buffer x509_get_sha1_fingerprint(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Retrieve the certificate&#39;s SHA1 fingerprint.
char * auth_token
If server sends a generated auth-token, this is the token to use for future user/pass authentications...
Definition: ssl_common.h:568
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition: plugin.c:894
bool tls_verify_crl_missing(const struct tls_options *opt)
Return true iff a CRL is configured, but is not loaded.
void string_replace_leading(char *str, const char match, const char replace)
Definition: buffer.c:1132
void argv_printf_cat(struct argv *a, const char *format,...)
Definition: argv.c:328
const char * verify_command
Definition: ssl_common.h:280
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:407
#define msg
Definition: error.h:173
Definition: misc.h:62
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
static result_t verify_cert_call_plugin(const struct plugin_list *plugins, struct env_set *es, int cert_depth, openvpn_x509_cert_t *cert, char *subject)
Definition: ssl_verify.c:499
#define TLS_AUTHENTICATION_SUCCEEDED
Definition: ssl_verify.h:68
#define D_SHOW_KEYS
Definition: errlevel.h:117
result_t x509_write_pem(FILE *peercert_file, openvpn_x509_cert_t *peercert)
char * locked_username
Definition: ssl_common.h:541
char * common_name
Definition: ssl_common.h:432
const char * tls_common_name(const struct tls_multi *multi, const bool null)
Returns the common name field for the given tunnel.
Definition: ssl_verify.c:147
static int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
Definition: crypto.h:500
uint8_t * verify_hash
Definition: ssl_common.h:289
void tls_clear_error(void)
Clear the underlying SSL library&#39;s error state.
Definition: ssl_openssl.c:99
static bool verify_user_pass_script(struct tls_session *session, const struct user_pass *up)
Definition: ssl_verify.c:1075
const char * tls_username(const struct tls_multi *multi, const bool null)
Returns the username field for the given tunnel.
Definition: ssl_verify.c:211
#define free
Definition: cmocka.c:1850
void tls_x509_clear_env(struct env_set *es)
Remove any X509_ env variables from env_set es.
Definition: ssl_verify.c:1494
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
void tls_lock_common_name(struct tls_multi *multi)
Locks the common name field for the given tunnel.
Definition: ssl_verify.c:172
void tls_lock_cert_hash_set(struct tls_multi *multi)
Locks the certificate hash set used in the given tunnel.
Definition: ssl_verify.c:326
const char * platform_create_temp_file(const char *directory, const char *prefix, struct gc_arena *gc)
Create a temporary file in directory, returns the filename of the created file.
Definition: platform.c:342
#define TLS_AUTHENTICATION_DEFERRED
Definition: ssl_verify.h:70
void cert_hash_free(struct cert_hash_set *chs)
Frees the given set of certificate hashes.
Definition: ssl_verify.c:254
void argv_reset(struct argv *a)
Definition: argv.c:60
struct buffer x509_get_sha256_fingerprint(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Retrieve the certificate&#39;s SHA256 fingerprint.
#define M_ERRNO
Definition: error.h:99
char password[USER_PASS_LEN]
Definition: misc.h:75
static struct cert_hash_set * cert_hash_copy(const struct cert_hash_set *chs)
Definition: ssl_verify.c:305
void verify_final_auth_checks(struct tls_multi *multi, struct tls_session *session)
Perform final authentication checks, including locking of the cn, the allowed certificate hashes...
Definition: ssl_verify.c:1432
Definition: argv.h:35
#define CC_CRLF
Definition: buffer.h:944
#define M_NONFATAL
Definition: error.h:95
static int verify_user_pass_plugin(struct tls_session *session, const struct user_pass *up)
Definition: ssl_verify.c:1157
char * dest
Definition: compat-lz4.h:431
const struct x509_track * x509_track
Definition: ssl_common.h:355
static bool openvpn_run_script(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *hook)
Definition: run_command.h:53
bool auth_token_sent
If server uses –auth-gen-token and token has been sent to client.
Definition: ssl_common.h:573
unsigned int ssl_flags
Definition: ssl_common.h:349
static result_t verify_cert_call_command(const char *verify_command, struct env_set *es, int cert_depth, openvpn_x509_cert_t *cert, char *subject, const char *verify_export_cert)
Definition: ssl_verify.c:567
void argv_printf(struct argv *a, const char *format,...)
Definition: argv.c:318
#define M_WARN
Definition: error.h:96
struct env_set * es
Definition: ssl_common.h:330
struct cert_hash * ch[MAX_CERT_DEPTH]
Array of certificate hashes.
Definition: ssl_verify.h:60
void x509_setenv(struct env_set *es, int cert_depth, openvpn_x509_cert_t *cert)
result_t
Result of verification function.
const char * remote_cert_eku
Definition: ssl_common.h:288
result_t backend_x509_get_username(char *common_name, int cn_len, char *x509_username_field, openvpn_x509_cert_t *peer_cert)
unsigned char sha256_hash[256/8]
Definition: ssl_verify.h:55
#define NS_CERT_CHECK_SERVER
Do not perform Netscape certificate type verification.
Definition: ssl_verify.h:214