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 = KS_AUTH_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_free(&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_free(&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  const char *subject, int cert_depth)
604 {
605  result_t ret = FAILURE;
606  char fn[256];
607  int fd = -1;
608  struct gc_arena gc = gc_new();
609 
610  char *serial = backend_x509_get_serial(cert, &gc);
611  if (!serial)
612  {
613  msg(D_HANDSHAKE, "VERIFY CRL: depth=%d, %s, serial number is not available",
614  cert_depth, subject);
615  goto cleanup;
616  }
617 
618  if (!openvpn_snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, OS_SPECIFIC_DIRSEP, serial))
619  {
620  msg(D_HANDSHAKE, "VERIFY CRL: filename overflow");
621  goto cleanup;
622  }
623  fd = platform_open(fn, O_RDONLY, 0);
624  if (fd >= 0)
625  {
626  msg(D_HANDSHAKE, "VERIFY CRL: depth=%d, %s, serial=%s is revoked",
627  cert_depth, subject, serial);
628  goto cleanup;
629  }
630 
631  ret = SUCCESS;
632 
633 cleanup:
634 
635  if (fd != -1)
636  {
637  close(fd);
638  }
639  gc_free(&gc);
640  return ret;
641 }
642 
643 result_t
644 verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
645 {
646  result_t ret = FAILURE;
647  char *subject = NULL;
648  char common_name[TLS_USERNAME_LEN+1] = {0}; /* null-terminated */
649  const struct tls_options *opt;
650  struct gc_arena gc = gc_new();
651 
652  opt = session->opt;
653  ASSERT(opt);
654 
655  session->verified = false;
656 
657  /* get the X509 name */
658  subject = x509_get_subject(cert, &gc);
659  if (!subject)
660  {
661  msg(D_TLS_ERRORS, "VERIFY ERROR: depth=%d, could not extract X509 "
662  "subject string from certificate", cert_depth);
663  goto cleanup;
664  }
665 
666  /* enforce character class restrictions in X509 name */
667  string_mod_remap_name(subject);
668  string_replace_leading(subject, '-', '_');
669 
670  /* extract the username (default is CN) */
671  if (SUCCESS != backend_x509_get_username(common_name, sizeof(common_name),
672  opt->x509_username_field, cert))
673  {
674  if (!cert_depth)
675  {
676  msg(D_TLS_ERRORS, "VERIFY ERROR: could not extract %s from X509 "
677  "subject string ('%s') -- note that the username length is "
678  "limited to %d characters",
679  opt->x509_username_field,
680  subject,
682  goto cleanup;
683  }
684  }
685 
686  /* enforce character class restrictions in common name */
687  string_mod_remap_name(common_name);
688 
689  /* warn if cert chain is too deep */
690  if (cert_depth >= MAX_CERT_DEPTH)
691  {
692  msg(D_TLS_ERRORS, "TLS Error: Convoluted certificate chain detected with depth [%d] greater than %d", cert_depth, MAX_CERT_DEPTH);
693  goto cleanup; /* Reject connection */
694  }
695 
696  /* verify level 1 cert, i.e. the CA that signed our leaf cert */
697  if (cert_depth == 1 && opt->verify_hash)
698  {
699  struct buffer ca_hash = {0};
700 
701  switch (opt->verify_hash_algo)
702  {
703  case MD_SHA1:
704  ca_hash = x509_get_sha1_fingerprint(cert, &gc);
705  break;
706 
707  case MD_SHA256:
708  ca_hash = x509_get_sha256_fingerprint(cert, &gc);
709  break;
710 
711  default:
712  /* This should normally not happen at all; the algorithm used
713  * is parsed by add_option() [options.c] and set to a predefined
714  * value in an enumerated type. So if this unlikely scenario
715  * happens, consider this a failure
716  */
717  msg(M_WARN, "Unexpected invalid algorithm used with "
718  "--verify-hash (%i)", opt->verify_hash_algo);
719  ret = FAILURE;
720  goto cleanup;
721  }
722 
723  if (memcmp(BPTR(&ca_hash), opt->verify_hash, BLEN(&ca_hash)))
724  {
725  msg(D_TLS_ERRORS, "TLS Error: level-1 certificate hash verification failed");
726  goto cleanup;
727  }
728  }
729 
730  /* save common name in session object */
731  if (cert_depth == 0)
732  {
733  set_common_name(session, common_name);
734  }
735 
736  session->verify_maxlevel = max_int(session->verify_maxlevel, cert_depth);
737 
738  /* export certificate values to the environment */
739  verify_cert_set_env(opt->es, cert, cert_depth, subject, common_name,
740  opt->x509_track);
741 
742  /* export current untrusted IP */
743  setenv_untrusted(session);
744 
745  /* If this is the peer's own certificate, verify it */
746  if (cert_depth == 0 && SUCCESS != verify_peer_cert(opt, cert, subject, common_name))
747  {
748  goto cleanup;
749  }
750 
751  /* call --tls-verify plug-in(s), if registered */
752  if (SUCCESS != verify_cert_call_plugin(opt->plugins, opt->es, cert_depth, cert, subject))
753  {
754  goto cleanup;
755  }
756 
757  /* run --tls-verify script */
759  opt->es, cert_depth, cert, subject, opt->verify_export_cert))
760  {
761  goto cleanup;
762  }
763 
764  /* check peer cert against CRL */
765  if (opt->crl_file)
766  {
767  if (opt->ssl_flags & SSLF_CRL_VERIFY_DIR)
768  {
769  if (SUCCESS != verify_check_crl_dir(opt->crl_file, cert, subject, cert_depth))
770  {
771  goto cleanup;
772  }
773  }
774  else
775  {
776  if (tls_verify_crl_missing(opt))
777  {
778  msg(D_TLS_ERRORS, "VERIFY ERROR: CRL not loaded");
779  goto cleanup;
780  }
781  }
782  }
783 
784  msg(D_HANDSHAKE, "VERIFY OK: depth=%d, %s", cert_depth, subject);
785  session->verified = true;
786  ret = SUCCESS;
787 
788 cleanup:
789 
790  if (ret != SUCCESS)
791  {
792  tls_clear_error(); /* always? */
793  session->verified = false; /* double sure? */
794  }
795  gc_free(&gc);
796 
797  return ret;
798 }
799 
800 /* ***************************************************************************
801 * Functions for the management of deferred authentication when using
802 * user/password authentication.
803 *************************************************************************** */
804 
805 #ifdef ENABLE_DEF_AUTH
806 /* key_state_test_auth_control_file return values,
807  * NOTE: acf_merge indexing depends on these values */
808 #define ACF_UNDEFINED 0
809 #define ACF_SUCCEEDED 1
810 #define ACF_DISABLED 2
811 #define ACF_FAILED 3
812 #endif
813 
814 void
815 auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
816 {
817  if (multi->client_reason)
818  {
819  free(multi->client_reason);
820  multi->client_reason = NULL;
821  }
822  if (client_reason && strlen(client_reason))
823  {
824  multi->client_reason = string_alloc(client_reason, NULL);
825  }
826 }
827 
828 #ifdef MANAGEMENT_DEF_AUTH
829 
830 static inline unsigned int
831 man_def_auth_test(const struct key_state *ks)
832 {
833  if (management_enable_def_auth(management))
834  {
835  return ks->mda_status;
836  }
837  else
838  {
839  return ACF_DISABLED;
840  }
841 }
842 #endif /* ifdef MANAGEMENT_DEF_AUTH */
843 
844 #ifdef PLUGIN_DEF_AUTH
845 
846 /*
847  * auth_control_file functions
848  */
849 
850 void
852 {
853  if (ks && ks->auth_control_file)
854  {
855  platform_unlink(ks->auth_control_file);
856  free(ks->auth_control_file);
857  ks->auth_control_file = NULL;
858  }
859 }
860 
861 static bool
862 key_state_gen_auth_control_file(struct key_state *ks, const struct tls_options *opt)
863 {
864  struct gc_arena gc = gc_new();
865 
867  const char *acf = platform_create_temp_file(opt->tmp_dir, "acf", &gc);
868  if (acf)
869  {
870  ks->auth_control_file = string_alloc(acf, NULL);
871  setenv_str(opt->es, "auth_control_file", ks->auth_control_file);
872  }
873 
874  gc_free(&gc);
875  return acf;
876 }
877 
878 static unsigned int
879 key_state_test_auth_control_file(struct key_state *ks)
880 {
881  if (ks && ks->auth_control_file)
882  {
883  unsigned int ret = ks->auth_control_status;
884  if (ret == ACF_UNDEFINED)
885  {
886  FILE *fp = fopen(ks->auth_control_file, "r");
887  if (fp)
888  {
889  const int c = fgetc(fp);
890  if (c == '1')
891  {
892  ret = ACF_SUCCEEDED;
893  }
894  else if (c == '0')
895  {
896  ret = ACF_FAILED;
897  }
898  fclose(fp);
899  ks->auth_control_status = ret;
900  }
901  }
902  return ret;
903  }
904  return ACF_DISABLED;
905 }
906 
907 #endif /* ifdef PLUGIN_DEF_AUTH */
908 
909 /*
910  * Return current session authentication state. Return
911  * value is TLS_AUTHENTICATION_x.
912  */
913 
914 int
915 tls_authentication_status(struct tls_multi *multi, const int latency)
916 {
917  bool deferred = false;
918  bool success = false;
919  bool active = false;
920 
921 #ifdef ENABLE_DEF_AUTH
922  static const unsigned char acf_merge[] =
923  {
924  ACF_UNDEFINED, /* s1=ACF_UNDEFINED s2=ACF_UNDEFINED */
925  ACF_UNDEFINED, /* s1=ACF_UNDEFINED s2=ACF_SUCCEEDED */
926  ACF_UNDEFINED, /* s1=ACF_UNDEFINED s2=ACF_DISABLED */
927  ACF_FAILED, /* s1=ACF_UNDEFINED s2=ACF_FAILED */
928  ACF_UNDEFINED, /* s1=ACF_SUCCEEDED s2=ACF_UNDEFINED */
929  ACF_SUCCEEDED, /* s1=ACF_SUCCEEDED s2=ACF_SUCCEEDED */
930  ACF_SUCCEEDED, /* s1=ACF_SUCCEEDED s2=ACF_DISABLED */
931  ACF_FAILED, /* s1=ACF_SUCCEEDED s2=ACF_FAILED */
932  ACF_UNDEFINED, /* s1=ACF_DISABLED s2=ACF_UNDEFINED */
933  ACF_SUCCEEDED, /* s1=ACF_DISABLED s2=ACF_SUCCEEDED */
934  ACF_DISABLED, /* s1=ACF_DISABLED s2=ACF_DISABLED */
935  ACF_FAILED, /* s1=ACF_DISABLED s2=ACF_FAILED */
936  ACF_FAILED, /* s1=ACF_FAILED s2=ACF_UNDEFINED */
937  ACF_FAILED, /* s1=ACF_FAILED s2=ACF_SUCCEEDED */
938  ACF_FAILED, /* s1=ACF_FAILED s2=ACF_DISABLED */
939  ACF_FAILED /* s1=ACF_FAILED s2=ACF_FAILED */
940  };
941 #endif /* ENABLE_DEF_AUTH */
942 
943  if (multi)
944  {
945  int i;
946 
947 #ifdef ENABLE_DEF_AUTH
948  if (latency && multi->tas_last && multi->tas_last + latency >= now)
949  {
951  }
952  multi->tas_last = now;
953 #endif /* ENABLE_DEF_AUTH */
954 
955  for (i = 0; i < KEY_SCAN_SIZE; ++i)
956  {
957  struct key_state *ks = multi->key_scan[i];
958  if (DECRYPT_KEY_ENABLED(multi, ks))
959  {
960  active = true;
961  if (ks->authenticated > KS_AUTH_FALSE)
962  {
963 #ifdef ENABLE_DEF_AUTH
964  unsigned int s1 = ACF_DISABLED;
965  unsigned int s2 = ACF_DISABLED;
966 #ifdef PLUGIN_DEF_AUTH
967  s1 = key_state_test_auth_control_file(ks);
968 #endif /* PLUGIN_DEF_AUTH */
969 #ifdef MANAGEMENT_DEF_AUTH
970  s2 = man_def_auth_test(ks);
971 #endif /* MANAGEMENT_DEF_AUTH */
972  ASSERT(s1 < 4 && s2 < 4);
973  switch (acf_merge[(s1<<2) + s2])
974  {
975  case ACF_SUCCEEDED:
976  case ACF_DISABLED:
977  success = true;
979  break;
980 
981  case ACF_UNDEFINED:
982  if (now < ks->auth_deferred_expire)
983  {
984  deferred = true;
985  }
986  break;
987 
988  case ACF_FAILED:
990  break;
991 
992  default:
993  ASSERT(0);
994  }
995 #else /* !ENABLE_DEF_AUTH */
996  success = true;
997 #endif /* ENABLE_DEF_AUTH */
998  }
999  }
1000  }
1001  }
1002 
1003 #if 0
1004  dmsg(D_TLS_ERRORS, "TAS: a=%d s=%d d=%d", active, success, deferred);
1005 #endif
1006 
1007  if (success)
1008  {
1010  }
1011  else if (!active || deferred)
1012  {
1014  }
1015  else
1016  {
1018  }
1019 }
1020 
1021 #ifdef MANAGEMENT_DEF_AUTH
1022 /*
1023  * For deferred auth, this is where the management interface calls (on server)
1024  * to indicate auth failure/success.
1025  */
1026 bool
1027 tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, const bool auth, const char *client_reason)
1028 {
1029  bool ret = false;
1030  if (multi)
1031  {
1032  int i;
1033  auth_set_client_reason(multi, client_reason);
1034  for (i = 0; i < KEY_SCAN_SIZE; ++i)
1035  {
1036  struct key_state *ks = multi->key_scan[i];
1037  if (ks->mda_key_id == mda_key_id)
1038  {
1039  ks->mda_status = auth ? ACF_SUCCEEDED : ACF_FAILED;
1040  ret = true;
1041  }
1042  }
1043  }
1044  return ret;
1045 }
1046 #endif /* ifdef MANAGEMENT_DEF_AUTH */
1047 
1048 
1049 /* ****************************************************************************
1050  * Functions to verify username and password
1051  *
1052  * Authenticate a client using username/password.
1053  * Runs on server.
1054  *
1055  * If you want to add new authentication methods,
1056  * this is the place to start.
1057  *************************************************************************** */
1058 
1059 /*
1060  * Verify the user name and password using a script
1061  */
1062 static bool
1064  const struct user_pass *up)
1065 {
1066  struct gc_arena gc = gc_new();
1067  struct argv argv = argv_new();
1068  const char *tmp_file = "";
1069  bool ret = false;
1070 
1071  /* Is username defined? */
1072  if ((session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL) || strlen(up->username))
1073  {
1074  /* Set environmental variables prior to calling script */
1075  setenv_str(session->opt->es, "script_type", "user-pass-verify");
1076 
1078  {
1079  struct status_output *so;
1080 
1081  tmp_file = platform_create_temp_file(session->opt->tmp_dir, "up",
1082  &gc);
1083  if (tmp_file)
1084  {
1085  so = status_open(tmp_file, 0, -1, NULL, STATUS_OUTPUT_WRITE);
1086  status_printf(so, "%s", up->username);
1087  status_printf(so, "%s", up->password);
1088  if (!status_close(so))
1089  {
1090  msg(D_TLS_ERRORS, "TLS Auth Error: could not write username/password to file: %s",
1091  tmp_file);
1092  goto done;
1093  }
1094  }
1095  else
1096  {
1097  msg(D_TLS_ERRORS, "TLS Auth Error: could not create write "
1098  "username/password to temp file");
1099  }
1100  }
1101  else
1102  {
1103  setenv_str(session->opt->es, "username", up->username);
1104  setenv_str(session->opt->es, "password", up->password);
1105  }
1106 
1107  /* setenv incoming cert common name for script */
1108  setenv_str(session->opt->es, "common_name", session->common_name);
1109 
1110  /* setenv client real IP address */
1111  setenv_untrusted(session);
1112 
1113  /* add auth-token environment */
1114  add_session_token_env(session, multi, up);
1115 
1116  /* format command line */
1118  argv_printf_cat(&argv, "%s", tmp_file);
1119 
1120  /* call command */
1121  ret = openvpn_run_script(&argv, session->opt->es, 0,
1122  "--auth-user-pass-verify");
1123 
1125  {
1126  setenv_del(session->opt->es, "password");
1127  }
1128  }
1129  else
1130  {
1131  msg(D_TLS_ERRORS, "TLS Auth Error: peer provided a blank username");
1132  }
1133 
1134 done:
1135  if (tmp_file && strlen(tmp_file) > 0)
1136  {
1137  platform_unlink(tmp_file);
1138  }
1139 
1140  argv_free(&argv);
1141  gc_free(&gc);
1142  return ret;
1143 }
1144 
1145 /*
1146  * Verify the username and password using a plugin
1147  */
1148 static int
1150  const struct user_pass *up)
1151 {
1152  int retval = OPENVPN_PLUGIN_FUNC_ERROR;
1153 #ifdef PLUGIN_DEF_AUTH
1154  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1155 #endif
1156 
1157  /* Is username defined? */
1158  if ((session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL) || strlen(up->username))
1159  {
1160  /* set username/password in private env space */
1161  setenv_str(session->opt->es, "username", up->username);
1162  setenv_str(session->opt->es, "password", up->password);
1163 
1164  /* setenv incoming cert common name for script */
1165  setenv_str(session->opt->es, "common_name", session->common_name);
1166 
1167  /* setenv client real IP address */
1168  setenv_untrusted(session);
1169 
1170  /* add auth-token environment */
1171  add_session_token_env(session, multi, up);
1172 #ifdef PLUGIN_DEF_AUTH
1173  /* generate filename for deferred auth control file */
1174  if (!key_state_gen_auth_control_file(ks, session->opt))
1175  {
1176  msg(D_TLS_ERRORS, "TLS Auth Error (%s): "
1177  "could not create deferred auth control file", __func__);
1178  return retval;
1179  }
1180 #endif
1181 
1182  /* call command */
1183  retval = plugin_call(session->opt->plugins, OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY, NULL, NULL, session->opt->es);
1184 
1185 #ifdef PLUGIN_DEF_AUTH
1186  /* purge auth control filename (and file itself) for non-deferred returns */
1187  if (retval != OPENVPN_PLUGIN_FUNC_DEFERRED)
1188  {
1190  }
1191 #endif
1192 
1193  setenv_del(session->opt->es, "password");
1194  }
1195  else
1196  {
1197  msg(D_TLS_ERRORS, "TLS Auth Error (verify_user_pass_plugin): peer provided a blank username");
1198  }
1199 
1200  return retval;
1201 }
1202 
1203 
1204 #ifdef MANAGEMENT_DEF_AUTH
1205 /*
1206  * MANAGEMENT_DEF_AUTH internal ssl_verify.c status codes
1207  */
1208 #define KMDA_ERROR 0
1209 #define KMDA_SUCCESS 1
1210 #define KMDA_UNDEF 2
1211 #define KMDA_DEF 3
1212 
1213 static int
1214 verify_user_pass_management(struct tls_session *session,
1215  struct tls_multi *multi,
1216  const struct user_pass *up)
1217 {
1218  int retval = KMDA_ERROR;
1219  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1220 
1221  /* Is username defined? */
1222  if ((session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL) || strlen(up->username))
1223  {
1224  /* set username/password in private env space */
1225  setenv_str(session->opt->es, "username", up->username);
1226  setenv_str(session->opt->es, "password", up->password);
1227 
1228  /* setenv incoming cert common name for script */
1229  setenv_str(session->opt->es, "common_name", session->common_name);
1230 
1231  /* setenv client real IP address */
1232  setenv_untrusted(session);
1233 
1234  /*
1235  * if we are using auth-gen-token, send also the session id of auth gen token to
1236  * allow the management to figure out if it is a new session or a continued one
1237  */
1238  add_session_token_env(session, multi, up);
1239  if (management)
1240  {
1241  management_notify_client_needing_auth(management, ks->mda_key_id, session->opt->mda_context, session->opt->es);
1242  }
1243 
1244  setenv_del(session->opt->es, "password");
1245 
1246  retval = KMDA_SUCCESS;
1247  }
1248  else
1249  {
1250  msg(D_TLS_ERRORS, "TLS Auth Error (verify_user_pass_management): peer provided a blank username");
1251  }
1252 
1253  return retval;
1254 }
1255 #endif /* ifdef MANAGEMENT_DEF_AUTH */
1256 
1257 
1258 /*
1259  * Main username/password verification entry point
1260  *
1261  * Will set session->ks[KS_PRIMARY].authenticated according to
1262  * result of the username/password verification
1263  */
1264 void
1265 verify_user_pass(struct user_pass *up, struct tls_multi *multi,
1266  struct tls_session *session)
1267 {
1268  int s1 = OPENVPN_PLUGIN_FUNC_SUCCESS;
1269  bool s2 = true;
1270  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1271 
1272 #ifdef MANAGEMENT_DEF_AUTH
1273  int man_def_auth = KMDA_UNDEF;
1274 
1275  if (management_enable_def_auth(management))
1276  {
1277  man_def_auth = KMDA_DEF;
1278  }
1279 #endif
1280 
1281  /* enforce character class restrictions in username/password */
1283  string_mod(up->password, CC_PRINT, CC_CRLF, '_');
1284 
1285  /*
1286  * If auth token succeeds we skip the auth
1287  * methods unless otherwise specified
1288  */
1289  bool skip_auth = false;
1290 
1291  /*
1292  * If server is configured with --auth-gen-token and the client sends
1293  * something that looks like an authentication token, this
1294  * round will be done internally using the token instead of
1295  * calling any external authentication modules.
1296  */
1297  if (session->opt->auth_token_generate && is_auth_token(up->password))
1298  {
1299  multi->auth_token_state_flags = verify_auth_token(up, multi, session);
1300  if (session->opt->auth_token_call_auth)
1301  {
1302  /*
1303  * we do not care about the result here because it is
1304  * the responsibility of the external authentication to
1305  * decide what to do with the result
1306  */
1307  }
1308  else if (multi->auth_token_state_flags == AUTH_TOKEN_HMAC_OK)
1309  {
1310  /*
1311  * We do not want the EXPIRED or EMPTY USER flags here so check
1312  * for equality with AUTH_TOKEN_HMAC_OK
1313  */
1314  msg(M_WARN, "TLS: Username/auth-token authentication "
1315  "succeeded for username '%s'",
1316  up->username);
1317  skip_auth = true;
1318  }
1319  else
1320  {
1321  wipe_auth_token(multi);
1323  msg(M_WARN, "TLS: Username/auth-token authentication "
1324  "failed for username '%s'", up->username);
1325  return;
1326  }
1327  }
1328  /* call plugin(s) and/or script */
1329  if (!skip_auth)
1330  {
1331 #ifdef MANAGEMENT_DEF_AUTH
1332  if (man_def_auth==KMDA_DEF)
1333  {
1334  man_def_auth = verify_user_pass_management(session, multi, up);
1335  }
1336 #endif
1338  {
1339  s1 = verify_user_pass_plugin(session, multi, up);
1340  }
1341 
1342  if (session->opt->auth_user_pass_verify_script)
1343  {
1344  s2 = verify_user_pass_script(session, multi, up);
1345  }
1346  }
1347 
1348  /* check sizing of username if it will become our common name */
1349  if ((session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME)
1350  && strlen(up->username)>TLS_USERNAME_LEN)
1351  {
1352  msg(D_TLS_ERRORS,
1353  "TLS Auth Error: --username-as-common name specified and username is longer than the maximum permitted Common Name length of %d characters",
1356  }
1357  /* auth succeeded? */
1358  if ((s1 == OPENVPN_PLUGIN_FUNC_SUCCESS
1359 #ifdef PLUGIN_DEF_AUTH
1361 #endif
1362  ) && s2
1363 #ifdef MANAGEMENT_DEF_AUTH
1364  && man_def_auth != KMDA_ERROR
1365 #endif
1366  && tls_lock_username(multi, up->username))
1367  {
1369 #ifdef PLUGIN_DEF_AUTH
1370  if (s1 == OPENVPN_PLUGIN_FUNC_DEFERRED)
1371  {
1373  }
1374 #endif
1375 #ifdef MANAGEMENT_DEF_AUTH
1376  if (man_def_auth != KMDA_UNDEF)
1377  {
1379  }
1380 #endif
1381  if ((session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME))
1382  {
1383  set_common_name(session, up->username);
1384  }
1385 
1386  if ((session->opt->auth_token_generate))
1387  {
1388  /*
1389  * If we accepted a (not expired) token, i.e.
1390  * initial auth via token on new connection, we need
1391  * to store the auth-token in multi->auth_token, so
1392  * the initial timestamp and session id can be extracted from it
1393  */
1394  if (!multi->auth_token
1397  {
1398  multi->auth_token = strdup(up->password);
1399  }
1400 
1401  /*
1402  * Server is configured with --auth-gen-token. Generate or renew
1403  * the token.
1404  */
1405  generate_auth_token(up, multi);
1406  }
1407  /*
1408  * Auth token already sent to client, update auth-token on client.
1409  * The initial auth-token is sent as part of the push message, for this
1410  * update we need to schedule an extra push message.
1411  *
1412  * Otherwise the auth-token get pushed out as part of the "normal"
1413  * push-reply
1414  */
1415  if (multi->auth_token_initial)
1416  {
1417  /*
1418  * We do not explicitly schedule the sending of the
1419  * control message here but control message are only
1420  * postponed when the control channel is not yet fully
1421  * established and furthermore since this is called in
1422  * the middle of authentication, there are other messages
1423  * (new data channel keys) that are sent anyway and will
1424  * trigger schedueling
1425  */
1427  }
1428  msg(D_HANDSHAKE, "TLS: Username/Password authentication %s for username '%s' %s",
1429  (ks->authenticated == KS_AUTH_DEFERRED) ? "deferred" : "succeeded",
1430  up->username,
1431  (session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME) ? "[CN SET]" : "");
1432  }
1433  else
1434  {
1436  msg(D_TLS_ERRORS, "TLS Auth Error: Auth Username/Password verification failed for peer");
1437  }
1438 }
1439 
1440 void
1441 verify_final_auth_checks(struct tls_multi *multi, struct tls_session *session)
1442 {
1443  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1444 
1445  /* While it shouldn't really happen, don't allow the common name to be NULL */
1446  if (!session->common_name)
1447  {
1448  set_common_name(session, "");
1449  }
1450 
1451  /* Don't allow the CN to change once it's been locked */
1452  if (ks->authenticated > KS_AUTH_FALSE && multi->locked_cn)
1453  {
1454  const char *cn = session->common_name;
1455  if (cn && strcmp(cn, multi->locked_cn))
1456  {
1457  msg(D_TLS_ERRORS, "TLS Auth Error: TLS object CN attempted to change from '%s' to '%s' -- tunnel disabled",
1458  multi->locked_cn,
1459  cn);
1460 
1461  /* change the common name back to its original value and disable the tunnel */
1462  set_common_name(session, multi->locked_cn);
1463  tls_deauthenticate(multi);
1464  }
1465  }
1466 
1467  /* Don't allow the cert hashes to change once they have been locked */
1468  if (ks->authenticated > KS_AUTH_FALSE && multi->locked_cert_hash_set)
1469  {
1470  const struct cert_hash_set *chs = session->cert_hash_set;
1471  if (chs && !cert_hash_compare(chs, multi->locked_cert_hash_set))
1472  {
1473  msg(D_TLS_ERRORS, "TLS Auth Error: TLS object CN=%s client-provided SSL certs unexpectedly changed during mid-session reauth",
1474  session->common_name);
1475 
1476  /* disable the tunnel */
1477  tls_deauthenticate(multi);
1478  }
1479  }
1480 
1481  /* verify --client-config-dir based authentication */
1483  {
1484  struct gc_arena gc = gc_new();
1485 
1486  const char *cn = session->common_name;
1487  const char *path = platform_gen_path(session->opt->client_config_dir_exclusive,
1488  cn, &gc);
1489  if (!cn || !strcmp(cn, CCD_DEFAULT) || !platform_test_file(path))
1490  {
1492  wipe_auth_token(multi);
1493  msg(D_TLS_ERRORS, "TLS Auth Error: --client-config-dir authentication failed for common name '%s' file='%s'",
1494  session->common_name,
1495  path ? path : "UNDEF");
1496  }
1497 
1498  gc_free(&gc);
1499  }
1500 }
1501 
1502 void
1504 {
1505  struct env_item *item = es->list;
1506  while (item)
1507  {
1508  struct env_item *next = item->next;
1509  if (item->string
1510  && 0 == strncmp("X509_", item->string, strlen("X509_")))
1511  {
1512  env_set_del(es, item->string);
1513  }
1514  item = next;
1515  }
1516 }
#define M_NONFATAL
Definition: error.h:95
static bool tls_lock_username(struct tls_multi *multi, const char *username)
Definition: ssl_verify.c:165
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition: argv.c:466
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:585
#define VERIFY_X509_SUBJECT_RDN
Definition: ssl_verify.h:65
#define TM_ACTIVE
Active tls_session.
Definition: ssl_common.h:472
const char * tmp_dir
Definition: ssl_common.h:327
Security parameter state of one TLS and data channel key session.
Definition: ssl_common.h:178
struct key_state key[KS_SIZE]
Definition: ssl_common.h:452
const char * crl_file
Definition: ssl_common.h:292
#define SSLF_CRL_VERIFY_DIR
Definition: ssl_common.h:357
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:163
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition: argv.c:90
#define STATUS_OUTPUT_WRITE
Definition: status.h:51
#define SSLF_USERNAME_AS_COMMON_NAME
Definition: ssl_common.h:354
#define VERIFY_X509_SUBJECT_DN
Definition: ssl_verify.h:64
#define OS_SPECIFIC_DIRSEP
Definition: syshead.h:479
static bool verify_user_pass_script(struct tls_session *session, struct tls_multi *multi, const struct user_pass *up)
Definition: ssl_verify.c:1063
#define AUTH_TOKEN_EXPIRED
Auth-token sent from client has expired.
Definition: ssl_common.h:575
enum ks_auth_state authenticated
Definition: ssl_common.h:218
struct tls_options * opt
Definition: ssl_common.h:417
int verify_maxlevel
Definition: ssl_common.h:437
#define SSLF_AUTH_USER_PASS_OPTIONAL
Definition: ssl_common.h:355
#define SA_IP_PORT
Definition: socket.h:405
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:569
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
bool verified
Definition: ssl_common.h:447
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:202
#define dmsg
Definition: error.h:174
const char * auth_user_pass_verify_script
Definition: ssl_common.h:325
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:509
#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:527
X509 openvpn_x509_cert_t
int tls_authentication_status(struct tls_multi *multi, const int latency)
Definition: ssl_verify.c:915
const char * client_config_dir_exclusive
Definition: ssl_common.h:339
int verify_x509_type
Definition: ssl_common.h:290
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:441
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:787
Key state is authenticated.
Definition: ssl_common.h:153
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:330
static void string_mod_remap_name(char *str)
Definition: ssl_verify.c:54
static result_t verify_check_crl_dir(const char *crl_dir, openvpn_x509_cert_t *cert, const char *subject, int cert_depth)
Definition: ssl_verify.c:602
#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
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer...
Definition: ssl_common.h:597
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:226
char username[USER_PASS_LEN]
Definition: misc.h:75
result_t verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
Definition: ssl_verify.c:644
char * x509_username_field
Definition: ssl_common.h:299
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:190
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:517
#define BPTR(buf)
Definition: buffer.h:124
#define AUTH_TOKEN_HMAC_OK
Auth-token sent from client has valid hmac.
Definition: ssl_common.h:573
static const char * print_nsCertType(int type)
Definition: ssl_verify.c:319
#define KEY_SCAN_SIZE
Definition: ssl_common.h:494
bool openvpn_snprintf(char *str, size_t size, const char *format,...)
Definition: buffer.c:299
#define OPENVPN_PLUGIN_FUNC_DEFERRED
Key state authentication is being deferred, by async auth.
Definition: ssl_common.h:151
#define KS_SIZE
Size of the tls_session.key array.
Definition: ssl_common.h:394
struct env_item * list
Definition: env_set.h:44
bool auth_user_pass_verify_script_via_file
Definition: ssl_common.h:326
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:3137
#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:390
struct gc_arena gc
Definition: argv.h:36
Key state is not authenticated.
Definition: ssl_common.h:150
unsigned remote_cert_ku[MAX_PARMS]
Definition: ssl_common.h:295
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:1149
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:546
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:815
int ns_cert_type
Definition: ssl_common.h:294
#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:298
unsigned __int32 uint32_t
Definition: config-msvc.h:158
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)
Similar to argv_msg() but prefixes the messages being written with a given string.
Definition: argv.c:262
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:548
const char * verify_x509_name
Definition: ssl_common.h:291
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:344
struct env_item * next
Definition: env_set.h:39
struct link_socket_actual untrusted_addr
Definition: ssl_common.h:450
#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:289
char * client_reason
Definition: ssl_common.h:558
#define VERIFY_X509_SUBJECT_RDN_PREFIX
Definition: ssl_verify.h:66
bool auth_token_call_auth
always call normal authentication
Definition: ssl_common.h:333
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:477
#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:219
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:1265
#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:589
unsigned __int8 uint8_t
Definition: config-msvc.h:160
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:565
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition: plugin.c:901
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
const char * verify_command
Definition: ssl_common.h:288
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:414
Definition: misc.h:63
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:547
char * common_name
Definition: ssl_common.h:439
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:297
#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:1503
void argv_parse_cmd(struct argv *argres, const char *cmdstr)
Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot...
Definition: argv.c:485
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_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition: argv.c:104
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:76
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:1441
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:368
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:362
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
struct env_set * es
Definition: ssl_common.h:342
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:296
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
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition: argv.c:442
#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:127