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