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