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-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2026 Sentyron B.V. <openvpn@sentyron.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, see <https://www.gnu.org/licenses/>.
22 */
23
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "syshead.h"
34#include <string.h>
35
36#include "base64.h"
37#include "manage.h"
38#include "otime.h"
39#include "run_command.h"
40#include "ssl_verify.h"
41#include "ssl_verify_backend.h"
42
43#ifdef ENABLE_CRYPTO_OPENSSL
44#include "ssl_verify_openssl.h"
45#endif
46#include "auth_token.h"
47#include "push.h"
48#include "ssl_util.h"
49
50static void
52{
53 string_mod(str, CC_PRINT, CC_CRLF, '_');
54}
55
56/*
57 * Export the untrusted IP address and port to the environment
58 */
59static void
61{
62 setenv_link_socket_actual(session->opt->es, "untrusted", &session->untrusted_addr, SA_IP_PORT);
63}
64
65/*
66 * Remove authenticated state from all sessions in the given tunnel
67 */
68static void
70{
71 if (multi)
72 {
73 wipe_auth_token(multi);
74 for (int i = 0; i < TM_SIZE; ++i)
75 {
76 for (int j = 0; j < KS_SIZE; ++j)
77 {
79 }
80 }
81 }
82}
83
84void
85set_common_name(struct tls_session *session, const char *common_name)
86{
87 if (session->common_name)
88 {
89 free(session->common_name);
90 session->common_name = NULL;
91 }
92 if (common_name)
93 {
94 /* FIXME: Last alloc will never be freed */
95 session->common_name = string_alloc(common_name, NULL);
96 }
97 /* update common name in env */
98 setenv_str(session->opt->es, "common_name", common_name);
99}
100
101/*
102 * Retrieve the common name for the given tunnel's active session. If the
103 * common name is NULL or empty, return NULL if null is true, or "UNDEF" if
104 * null is false.
105 */
106const char *
107tls_common_name(const struct tls_multi *multi, const bool null)
108{
109 const char *ret = NULL;
110 if (multi)
111 {
112 ret = multi->session[TM_ACTIVE].common_name;
113 }
114 if (ret && strlen(ret))
115 {
116 return ret;
117 }
118 else if (null)
119 {
120 return NULL;
121 }
122 else
123 {
124 return "UNDEF";
125 }
126}
127
128/*
129 * Lock the common name for the given tunnel.
130 */
131void
133{
134 const char *cn = multi->session[TM_ACTIVE].common_name;
135 if (cn && !multi->locked_cn)
136 {
137 multi->locked_cn = string_alloc(cn, NULL);
138 }
139}
140
141/*
142 * Lock the username for the given tunnel
143 */
144static bool
145tls_lock_username(struct tls_multi *multi, const char *username)
146{
147 if (multi->locked_username)
148 {
149 /* If the username has been overridden, we accept both the original
150 * username and the changed username */
151 if (strcmp(username, multi->locked_username) != 0
152 && (!multi->locked_original_username
153 || strcmp(username, multi->locked_original_username) != 0))
154 {
156 "TLS Auth Error: username attempted to change from '%s' to '%s' -- tunnel disabled",
157 multi->locked_username, username);
158
159 /* disable the tunnel */
160 tls_deauthenticate(multi);
161 return false;
162 }
163 }
164 else
165 {
166 multi->locked_username = string_alloc(username, NULL);
167 }
168 return true;
169}
170
171const char *
172tls_username(const struct tls_multi *multi, const bool null)
173{
174 const char *ret = NULL;
175 if (multi)
176 {
177 ret = multi->locked_username;
178 }
179 if (ret && strlen(ret))
180 {
181 return ret;
182 }
183 else if (null)
184 {
185 return NULL;
186 }
187 else
188 {
189 return "UNDEF";
190 }
191}
192
193void
194cert_hash_remember(struct tls_session *session, const int error_depth,
195 const struct buffer *cert_hash)
196{
197 if (error_depth >= 0 && error_depth < MAX_CERT_DEPTH)
198 {
199 if (!session->cert_hash_set)
200 {
201 ALLOC_OBJ_CLEAR(session->cert_hash_set, struct cert_hash_set);
202 }
203 if (!session->cert_hash_set->ch[error_depth])
204 {
205 ALLOC_OBJ(session->cert_hash_set->ch[error_depth], struct cert_hash);
206 }
207
208 struct cert_hash *ch = session->cert_hash_set->ch[error_depth];
209 ASSERT(sizeof(ch->sha256_hash) == BLEN(cert_hash));
210 memcpy(ch->sha256_hash, BPTR(cert_hash), sizeof(ch->sha256_hash));
211 }
212}
213
214void
216{
217 if (chs)
218 {
219 int i;
220 for (i = 0; i < MAX_CERT_DEPTH; ++i)
221 {
222 free(chs->ch[i]);
223 }
224 free(chs);
225 }
226}
227
228bool
229cert_hash_compare(const struct cert_hash_set *chs1, const struct cert_hash_set *chs2)
230{
231 if (chs1 && chs2)
232 {
233 int i;
234 for (i = 0; i < MAX_CERT_DEPTH; ++i)
235 {
236 const struct cert_hash *ch1 = chs1->ch[i];
237 const struct cert_hash *ch2 = chs2->ch[i];
238
239 if (!ch1 && !ch2)
240 {
241 continue;
242 }
243 else if (ch1 && ch2
244 && !memcmp(ch1->sha256_hash, ch2->sha256_hash, sizeof(ch1->sha256_hash)))
245 {
246 continue;
247 }
248 else
249 {
250 return false;
251 }
252 }
253 return true;
254 }
255 else if (!chs1 && !chs2)
256 {
257 return true;
258 }
259 else
260 {
261 return false;
262 }
263}
264
265static struct cert_hash_set *
267{
268 struct cert_hash_set *dest = NULL;
269 if (chs)
270 {
271 int i;
272 ALLOC_OBJ_CLEAR(dest, struct cert_hash_set);
273 for (i = 0; i < MAX_CERT_DEPTH; ++i)
274 {
275 const struct cert_hash *ch = chs->ch[i];
276 if (ch)
277 {
278 ALLOC_OBJ(dest->ch[i], struct cert_hash);
279 memcpy(dest->ch[i]->sha256_hash, ch->sha256_hash, sizeof(dest->ch[i]->sha256_hash));
280 }
281 }
282 }
283 return dest;
284}
285void
287{
288 const struct cert_hash_set *chs = multi->session[TM_ACTIVE].cert_hash_set;
289 if (chs && !multi->locked_cert_hash_set)
290 {
292 }
293}
294
295/*
296 * Returns the string associated with the given certificate type.
297 */
298static const char *
300{
301 switch (type)
302 {
304 return "SERVER";
305
307 return "CLIENT";
308
309 default:
310 return "?";
311 }
312}
313
314/*
315 * Verify the peer's certificate fields.
316 *
317 * @param opt the tls options to verify against
318 * @param peer_cert the peer's certificate
319 * @param subject the peer's extracted subject name
320 * @param subject the peer's extracted common name
321 */
322static result_t
323verify_peer_cert(const struct tls_options *opt, openvpn_x509_cert_t *peer_cert, const char *subject,
324 const char *common_name)
325{
326 /* verify certificate nsCertType */
328 {
329 if (SUCCESS == x509_verify_ns_cert_type(peer_cert, opt->ns_cert_type))
330 {
331 msg(D_HANDSHAKE, "VERIFY OK: nsCertType=%s", print_nsCertType(opt->ns_cert_type));
332 }
333 else
334 {
335 msg(D_HANDSHAKE, "VERIFY nsCertType ERROR: %s, require nsCertType=%s", subject,
337 return FAILURE; /* Reject connection */
338 }
339 }
340
341 /* verify certificate ku */
342 if (opt->remote_cert_ku[0] != 0)
343 {
344 if (SUCCESS == x509_verify_cert_ku(peer_cert, opt->remote_cert_ku, MAX_PARMS))
345 {
346 msg(D_HANDSHAKE, "VERIFY KU OK");
347 }
348 else
349 {
350 msg(D_HANDSHAKE, "VERIFY KU ERROR");
351 return FAILURE; /* Reject connection */
352 }
353 }
354
355 /* verify certificate eku */
356 if (opt->remote_cert_eku != NULL)
357 {
358 if (SUCCESS == x509_verify_cert_eku(peer_cert, opt->remote_cert_eku))
359 {
360 msg(D_HANDSHAKE, "VERIFY EKU OK");
361 }
362 else
363 {
364 msg(D_HANDSHAKE, "VERIFY EKU ERROR");
365 return FAILURE; /* Reject connection */
366 }
367 }
368
369 /* verify X509 name or username against --verify-x509-[user]name */
371 {
372 const char *err_fmt = "VERIFY X509NAME ERROR: %s, must be %s";
373 const char *match_str = common_name;
374 bool verified = false;
375 switch (opt->verify_x509_type)
376 {
378 match_str = subject;
379 verified = !strcmp(opt->verify_x509_name, match_str);
380 break;
382 verified = !strcmp(opt->verify_x509_name, match_str);
383 break;
385 err_fmt = "VERIFY X509NAME ERROR: %s, must start with %s";
386 verified = !strncmp(opt->verify_x509_name, match_str, strlen(opt->verify_x509_name));
387 break;
388 default:
389 ASSERT(0); /* should not happen */
390 break;
391 }
392 if (!verified)
393 {
394 msg(D_HANDSHAKE, err_fmt, match_str, opt->verify_x509_name);
395 return FAILURE;
396 }
397 msg(D_HANDSHAKE, "VERIFY X509NAME OK: %s", subject);
398 }
399
400 return SUCCESS;
401}
402
403/*
404 * Export the subject, common_name, and raw certificate fields to the
405 * environment for later verification by scripts and plugins.
406 */
407static void
408verify_cert_set_env(struct env_set *es, openvpn_x509_cert_t *peer_cert, int cert_depth,
409 const char *subject, const struct x509_track *x509_track)
410{
411 char envname[64];
412 char *serial = NULL;
413 struct gc_arena gc = gc_new();
414
415 /* Save X509 fields in environment */
416 if (x509_track)
417 {
418 x509_setenv_track(x509_track, es, cert_depth, peer_cert);
419 }
420 else
421 {
422 x509_setenv(es, cert_depth, peer_cert);
423 }
424
425 /* export subject name string as environmental variable */
426 snprintf(envname, sizeof(envname), "tls_id_%d", cert_depth);
427 setenv_str(es, envname, subject);
428
429 /* export X509 cert fingerprints */
430 {
433
434 snprintf(envname, sizeof(envname), "tls_digest_%d", cert_depth);
435 setenv_str(es, envname, format_hex_ex(BPTR(&sha1), BLEN(&sha1), 0, 1, ":", &gc));
436
437 snprintf(envname, sizeof(envname), "tls_digest_sha256_%d", cert_depth);
438 setenv_str(es, envname, format_hex_ex(BPTR(&sha256), BLEN(&sha256), 0, 1, ":", &gc));
439 }
440
441 /* export serial number as environmental variable */
443 snprintf(envname, sizeof(envname), "tls_serial_%d", cert_depth);
445
446 /* export serial number in hex as environmental variable */
448 snprintf(envname, sizeof(envname), "tls_serial_hex_%d", cert_depth);
450
451 gc_free(&gc);
452}
453
458static bool
460 const char *pem_export_fname)
461{
462 /* export the path to the current certificate in pem file format */
463 setenv_str(es, "peer_cert", pem_export_fname);
464
466}
467
468static void
470{
471 env_set_del(es, "peer_cert");
473 {
475 }
476}
477
478/*
479 * call --tls-verify plug-in(s)
480 */
481static result_t
482verify_cert_call_plugin(const struct plugin_list *plugins, struct env_set *es, int cert_depth,
483 openvpn_x509_cert_t *cert, char *subject)
484{
486 {
487 int ret;
488 struct argv argv = argv_new();
489
490 argv_printf(&argv, "%d %s", cert_depth, subject);
491
492 ret =
493 plugin_call_ssl(plugins, OPENVPN_PLUGIN_TLS_VERIFY, &argv, NULL, es, cert_depth, cert);
494
495 argv_free(&argv);
496
498 {
499 msg(D_HANDSHAKE, "VERIFY PLUGIN OK: depth=%d, %s", cert_depth, subject);
500 }
501 else
502 {
503 msg(D_HANDSHAKE, "VERIFY PLUGIN ERROR: depth=%d, %s", cert_depth, subject);
504 return FAILURE; /* Reject connection */
505 }
506 }
507 return SUCCESS;
508}
509
510/*
511 * run --tls-verify script
512 */
513static result_t
514verify_cert_call_command(const char *verify_command, struct env_set *es, int cert_depth,
515 char *subject)
516{
517 int ret;
518 struct gc_arena gc = gc_new();
519 struct argv argv = argv_new();
520
521 setenv_str(es, "script_type", "tls-verify");
522
523 argv_parse_cmd(&argv, verify_command);
524 argv_printf_cat(&argv, "%d %s", cert_depth, subject);
525
526 argv_msg_prefix(D_TLS_DEBUG, &argv, "TLS: executing verify command");
527 ret = openvpn_run_script(&argv, es, 0, "--tls-verify script");
528
529 gc_free(&gc);
530 argv_free(&argv);
531
532 if (ret)
533 {
534 msg(D_HANDSHAKE, "VERIFY SCRIPT OK: depth=%d, %s", cert_depth, subject);
535 return SUCCESS;
536 }
537
538 msg(D_HANDSHAKE, "VERIFY SCRIPT ERROR: depth=%d, %s", cert_depth, subject);
539 return FAILURE; /* Reject connection */
540}
541
542/*
543 * check peer cert against CRL directory
544 */
545static result_t
546verify_check_crl_dir(const char *crl_dir, openvpn_x509_cert_t *cert, const char *subject,
547 int cert_depth)
548{
549 result_t ret = FAILURE;
550 char fn[256];
551 int fd = -1;
552 struct gc_arena gc = gc_new();
553
554 char *serial = backend_x509_get_serial(cert, &gc);
555 if (!serial)
556 {
557 msg(D_HANDSHAKE, "VERIFY CRL: depth=%d, %s, serial number is not available", cert_depth,
558 subject);
559 goto cleanup;
560 }
561
562 if (!checked_snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATOR, serial))
563 {
564 msg(D_HANDSHAKE, "VERIFY CRL: filename overflow");
565 goto cleanup;
566 }
567 fd = platform_open(fn, O_RDONLY, 0);
568 if (fd >= 0)
569 {
570 msg(D_HANDSHAKE, "VERIFY CRL: depth=%d, %s, serial=%s is revoked", cert_depth, subject,
571 serial);
572 goto cleanup;
573 }
574
575 ret = SUCCESS;
576
577cleanup:
578
579 if (fd != -1)
580 {
581 close(fd);
582 }
583 gc_free(&gc);
584 return ret;
585}
586
588verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
589{
590 /* need to define these variables here so goto cleanup will always have
591 * them defined */
592 result_t ret = FAILURE;
593 struct gc_arena gc = gc_new();
594 const char *pem_export_fname = NULL;
595
596 const struct tls_options *opt = session->opt;
597 ASSERT(opt);
598
599 session->verified = false;
600
601 /* get the X509 name */
602 char *subject = x509_get_subject(cert, &gc);
603 if (!subject)
604 {
606 "VERIFY ERROR: depth=%d, could not extract X509 "
607 "subject string from certificate",
608 cert_depth);
609 goto cleanup;
610 }
611
612 /* enforce character class restrictions in X509 name */
613 string_mod_remap_name(subject);
614 string_replace_leading(subject, '-', '_');
615
616 /* extract the username (default is CN) */
617 struct buffer buf = alloc_buf_gc(256, &gc);
618 for (int i = 0; opt->x509_username_field[i] != NULL; i++)
619 {
620 char username[TLS_USERNAME_LEN + 1] = { 0 }; /* null-terminated */
621
622 if (SUCCESS
623 != backend_x509_get_username(username, sizeof(username), opt->x509_username_field[i],
624 cert))
625 {
626 if (!cert_depth)
627 {
629 "VERIFY ERROR: could not extract %s from X509 "
630 "subject string ('%s') -- note that the field length is "
631 "limited to %d characters",
633 goto cleanup;
634 }
635 break;
636 }
637 if (!buf_printf(&buf, i ? "_%s" : "%s", username))
638 {
639 if (!cert_depth)
640 {
642 "VERIFY ERROR: could not append %s from X509 "
643 "certificate -- note that the username length is "
644 "limited to %d characters",
645 opt->x509_username_field[i], buf.capacity - 1);
646 goto cleanup;
647 }
648 break;
649 }
650 }
651
652 char *common_name = BSTR(&buf);
653 if (!common_name)
654 {
656 "VERIFY ERROR: depth=%d, could not extract X509 "
657 "username string from certificate",
658 cert_depth);
659 goto cleanup;
660 }
661
662 /* enforce character class restrictions in common name */
663 string_mod_remap_name(common_name);
664
665 /* warn if cert chain is too deep */
667 {
669 "TLS Error: Convoluted certificate chain detected with depth [%d] greater than %d",
671 goto cleanup; /* Reject connection */
672 }
673
674 if (cert_depth == opt->verify_hash_depth && opt->verify_hash)
675 {
676 struct buffer cert_fp = { 0 };
677
678 switch (opt->verify_hash_algo)
679 {
680 case MD_SHA1:
682 break;
683
684 case MD_SHA256:
686 break;
687
688 default:
689 /* This should normally not happen at all; the algorithm used
690 * is parsed by add_option() [options.c] and set to a predefined
691 * value in an enumerated type. So if this unlikely scenario
692 * happens, consider this a failure
693 */
694 msg(M_WARN,
695 "Unexpected invalid algorithm used with "
696 "--verify-hash (%i)",
697 opt->verify_hash_algo);
698 ret = FAILURE;
699 goto cleanup;
700 }
701
702 struct verify_hash_list *current_hash = opt->verify_hash;
703
704 while (current_hash)
705 {
706 if (memcmp_constant_time(BPTR(&cert_fp), current_hash->hash, BLENZ(&cert_fp)) == 0)
707 {
708 break;
709 }
710 current_hash = current_hash->next;
711 }
712
713 if (!current_hash)
714 {
715 const char *hex_fp = format_hex_ex(BPTR(&cert_fp), BLEN(&cert_fp), 0, 1, ":", &gc);
717 "TLS Error: --tls-verify/--peer-fingerprint "
718 "certificate hash verification failed. (got certificate "
719 "fingerprint: %s)",
720 hex_fp);
721 goto cleanup;
722 }
723 }
724
725 /* save common name in session object */
726 if (cert_depth == 0)
727 {
728 set_common_name(session, common_name);
729 }
730
731 session->verify_maxlevel = max_int(session->verify_maxlevel, cert_depth);
732
733 if (opt->export_peer_cert_dir)
734 {
735 pem_export_fname = platform_create_temp_file(opt->export_peer_cert_dir, "pef", &gc);
736
737 if (!pem_export_fname || !verify_cert_cert_export_env(opt->es, cert, pem_export_fname))
738 {
740 "TLS Error: Failed to export certificate for "
741 "--tls-export-cert in %s",
743 goto cleanup;
744 }
745 }
746 /* export certificate values to the environment */
747 verify_cert_set_env(opt->es, cert, cert_depth, subject, opt->x509_track);
748
749 /* export current untrusted IP */
751
752 /* If this is the peer's own certificate, verify it */
753 if (cert_depth == 0 && SUCCESS != verify_peer_cert(opt, cert, subject, common_name))
754 {
755 goto cleanup;
756 }
757
758 /* call --tls-verify plug-in(s), if registered */
759 if (SUCCESS != verify_cert_call_plugin(opt->plugins, opt->es, cert_depth, cert, subject))
760 {
761 goto cleanup;
762 }
763
764 /* run --tls-verify script */
765 if (opt->verify_command
766 && SUCCESS != verify_cert_call_command(opt->verify_command, opt->es, cert_depth, subject))
767 {
768 goto cleanup;
769 }
770
771 /* check peer cert against CRL */
772 if (opt->crl_file)
773 {
775 {
776 if (SUCCESS != verify_check_crl_dir(opt->crl_file, cert, subject, cert_depth))
777 {
778 goto cleanup;
779 }
780 }
781 else
782 {
783 if (tls_verify_crl_missing(opt))
784 {
785 msg(D_TLS_ERRORS, "VERIFY ERROR: CRL not loaded");
786 goto cleanup;
787 }
788 }
789 }
790
791 msg(D_HANDSHAKE, "VERIFY OK: depth=%d, %s", cert_depth, subject);
792 session->verified = true;
793 ret = SUCCESS;
794
795cleanup:
796 verify_cert_cert_delete_env(opt->es, pem_export_fname);
797 if (ret != SUCCESS)
798 {
799 tls_clear_error(); /* always? */
800 session->verified = false; /* double sure? */
801 }
802
803 gc_free(&gc);
804
805 return ret;
806}
807
808/* ***************************************************************************
809 * Functions for the management of deferred authentication when using
810 * user/password authentication.
811 *************************************************************************** */
812
813void
814auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
815{
816 free(multi->client_reason);
817 multi->client_reason = NULL;
818
819 if (client_reason && strlen(client_reason))
820 {
821 multi->client_reason = string_alloc(client_reason, NULL);
822 }
823}
824
825#ifdef ENABLE_MANAGEMENT
826
827static inline enum auth_deferred_result
829{
831 {
832 return ks->mda_status;
833 }
834 else
835 {
836 return ACF_DISABLED;
837 }
838}
839#endif /* ifdef ENABLE_MANAGEMENT */
840
845static void
847{
848 if (ads && ads->auth_pending_file)
849 {
851 free(ads->auth_pending_file);
852 ads->auth_pending_file = NULL;
853 }
854}
855
859static bool
860check_auth_pending_method(const char *peer_info, const char *method)
861{
862 struct gc_arena gc = gc_new();
863
864 char *iv_sso = extract_var_peer_info(peer_info, "IV_SSO=", &gc);
865 if (!iv_sso)
866 {
867 gc_free(&gc);
868 return false;
869 }
870
871 const char *client_method = strtok(iv_sso, ",");
872 bool supported = false;
873
874 while (client_method)
875 {
876 if (0 == strcmp(client_method, method))
877 {
878 supported = true;
879 break;
880 }
881 client_method = strtok(NULL, ",");
882 }
883
884 gc_free(&gc);
885 return supported;
886}
887
896static bool
898 struct tls_multi *multi,
899 struct tls_session *session)
900{
901 bool ret = true;
902 if (ads->auth_pending_file)
903 {
904 struct buffer_list *lines = buffer_list_file(ads->auth_pending_file, 1024);
905 if (lines && lines->head)
906 {
907 /* Must have at least three lines. further lines are ignored for
908 * forward compatibility */
909 if (!lines->head->next || !lines->head->next->next)
910 {
911 msg(M_WARN, "auth pending control file is not at least "
912 "three lines long.");
913 buffer_list_free(lines);
914 return false;
915 }
916 struct buffer *timeout_buf = &lines->head->buf;
917 struct buffer *iv_buf = &lines->head->next->buf;
918 struct buffer *extra_buf = &lines->head->next->next->buf;
919
920 /* Remove newline chars at the end of the lines */
924
925 long timeout = strtol(BSTR(timeout_buf), NULL, 10);
926 if (timeout <= 0)
927 {
928 msg(M_WARN, "could not parse auth pending file timeout");
930 return false;
931 }
932
933 const char *pending_method = BSTR(iv_buf);
935 {
936 char buf[128];
937 snprintf(buf, sizeof(buf),
938 "Authentication failed, required pending auth "
939 "method '%s' not supported",
941 auth_set_client_reason(multi, buf);
942 msg(M_INFO,
943 "Client does not supported auth pending method '%s'",
945 ret = false;
946 }
947 else
948 {
950 (unsigned int)timeout);
951 }
952 }
953
955 }
957 return ret;
958}
959
964void
966{
967 if (ads->auth_control_file)
968 {
969 platform_unlink(ads->auth_control_file);
970 free(ads->auth_control_file);
971 ads->auth_control_file = NULL;
972 }
973 if (ads->auth_failed_reason_file)
974 {
975 platform_unlink(ads->auth_failed_reason_file);
976 free(ads->auth_failed_reason_file);
977 ads->auth_failed_reason_file = NULL;
978 }
980}
981
988static bool
990{
991 struct gc_arena gc = gc_new();
992
994 const char *acf = platform_create_temp_file(opt->tmp_dir, "acf", &gc);
995 const char *apf = platform_create_temp_file(opt->tmp_dir, "apf", &gc);
996 const char *afr = platform_create_temp_file(opt->tmp_dir, "afr", &gc);
997
998 if (acf && apf && afr)
999 {
1000 ads->auth_control_file = string_alloc(acf, NULL);
1001 ads->auth_pending_file = string_alloc(apf, NULL);
1002 ads->auth_failed_reason_file = string_alloc(afr, NULL);
1003
1004 setenv_str(opt->es, "auth_control_file", ads->auth_control_file);
1005 setenv_str(opt->es, "auth_pending_file", ads->auth_pending_file);
1006 setenv_str(opt->es, "auth_failed_reason_file", ads->auth_failed_reason_file);
1007 }
1008
1009 gc_free(&gc);
1010 return (acf && apf && afr);
1011}
1012
1017static char *
1019 struct gc_arena *gc)
1020{
1021 char *ret = NULL;
1022 if (ads->auth_failed_reason_file)
1023 {
1024 struct buffer reason = buffer_read_from_file(ads->auth_failed_reason_file, gc);
1025
1026 if (BLEN(&reason))
1027 {
1028 ret = BSTR(&reason);
1029 }
1030 }
1031 return ret;
1032}
1033
1034
1045static enum auth_deferred_result
1047{
1048 if (ads->auth_control_file)
1049 {
1050 unsigned int ret = ads->auth_control_status;
1051 if (ret == ACF_PENDING && !cached)
1052 {
1053 FILE *fp = fopen(ads->auth_control_file, "r");
1054 if (fp)
1055 {
1056 const int c = fgetc(fp);
1057 if (c == '1')
1058 {
1060 }
1061 else if (c == '0')
1062 {
1063 ret = ACF_FAILED;
1064 }
1065 fclose(fp);
1066 ads->auth_control_status = ret;
1067 }
1068 }
1069 return ret;
1070 }
1071 return ACF_DISABLED;
1072}
1073
1081static void
1083{
1084 if (ks->authenticated == KS_AUTH_FALSE)
1085 {
1086 return;
1087 }
1088 else
1089 {
1095#ifdef ENABLE_MANAGEMENT
1097#endif
1098 ASSERT(auth_plugin < 4 && auth_script < 4 && auth_man < 4);
1099
1101 {
1103 return;
1104 }
1106 || auth_man == ACF_PENDING)
1107 {
1108 if (now >= ks->auth_deferred_expire)
1109 {
1110 /* Window to authenticate the key has expired, mark
1111 * the key as unauthenticated */
1113 }
1114 }
1115 else
1116 {
1117 /* all auth states (auth_plugin, auth_script, auth_man)
1118 * are either ACF_DISABLED or ACF_SUCCEDED now, which
1119 * translates to "not checked" or "auth succeeded"
1120 */
1122 }
1123 }
1124}
1125
1126
1133static time_t cache_intervals[] = { 0, 0, 0, 0, 0, 1, 1, 2, 2, 4, 8 };
1134
1139static bool
1141{
1142 unsigned int idx = min_uint(multi->tas_cache_num_updates, SIZE(cache_intervals) - 1);
1144 return multi->tas_cache_last_update + latency >= now;
1145}
1146
1147enum tls_auth_status
1149{
1150 bool deferred = false;
1151
1152 /* at least one valid key has successfully completed authentication */
1153 bool success = false;
1154
1155 /* at least one key is enabled for decryption */
1156 int active = 0;
1157
1158 /* at least one key already failed authentication */
1159 bool failed_auth = false;
1160
1162
1163 for (int i = 0; i < KEY_SCAN_SIZE; ++i)
1164 {
1165 struct key_state *ks = get_key_scan(multi, i);
1166 if (TLS_AUTHENTICATED(multi, ks))
1167 {
1168 active++;
1169 update_key_auth_status(cached, ks);
1170
1171 if (ks->authenticated == KS_AUTH_FALSE)
1172 {
1173 failed_auth = true;
1174 }
1175 else if (ks->authenticated == KS_AUTH_DEFERRED)
1176 {
1177 deferred = true;
1178 }
1179 else if (ks->authenticated == KS_AUTH_TRUE)
1180 {
1181 success = true;
1182 }
1183 }
1184 }
1185
1186 /* we did not rely on a cached result, remember the cache update time */
1187 if (!cached)
1188 {
1189 multi->tas_cache_last_update = now;
1190 multi->tas_cache_num_updates++;
1191 }
1192
1193#if 0
1194 dmsg(D_TLS_ERRORS, "TAS: a=%d s=%d d=%d f=%d", active, success, deferred, failed_auth);
1195#endif
1196 if (failed_auth)
1197 {
1198 struct gc_arena gc = gc_new();
1199 const struct key_state *ks = get_primary_key(multi);
1200 const char *plugin_message =
1202 const char *script_message =
1204
1205 if (plugin_message)
1206 {
1207 auth_set_client_reason(multi, plugin_message);
1208 }
1209 if (script_message)
1210 {
1211 auth_set_client_reason(multi, script_message);
1212 }
1213
1214 /* We have at least one session that failed authentication. There
1215 * might be still another session with valid keys.
1216 * Although our protocol allows keeping the VPN session alive
1217 * with the other session (and we actually did that in earlier
1218 * version, this behaviour is really strange from a user (admin)
1219 * experience */
1220 gc_free(&gc);
1222 }
1223 else if (success)
1224 {
1226 }
1227 else if (active == 0 || deferred)
1228 {
1229 /* We have a deferred authentication and no currently active key
1230 * (first auth, no renegotiation) */
1232 }
1233 else
1234 {
1235 /* at least one key is active but none is fully authenticated (!success)
1236 * and all active are either failed authed or expired deferred auth */
1238 }
1239}
1240
1241#ifdef ENABLE_MANAGEMENT
1242/*
1243 * For deferred auth, this is where the management interface calls (on server)
1244 * to indicate auth failure/success.
1245 */
1246bool
1247tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, const bool auth,
1248 const char *client_reason)
1249{
1250 bool ret = false;
1251 if (multi)
1252 {
1253 int i;
1254 auth_set_client_reason(multi, client_reason);
1255 for (i = 0; i < KEY_SCAN_SIZE; ++i)
1256 {
1257 struct key_state *ks = get_key_scan(multi, i);
1258 if (ks->mda_key_id == mda_key_id)
1259 {
1260 ks->mda_status = auth ? ACF_SUCCEEDED : ACF_FAILED;
1261 ret = true;
1262 }
1263 }
1264 }
1265 return ret;
1266}
1267#endif /* ifdef ENABLE_MANAGEMENT */
1268
1269
1270/* ****************************************************************************
1271 * Functions to verify username and password
1272 *
1273 * Authenticate a client using username/password.
1274 * Runs on server.
1275 *
1276 * If you want to add new authentication methods,
1277 * this is the place to start.
1278 *************************************************************************** */
1279
1283static void
1285{
1286 struct gc_arena gc = gc_new();
1288 if (msg)
1289 {
1291 }
1292 gc_free(&gc);
1293}
1294/*
1295 * Verify the user name and password using a script
1296 */
1297static int
1299 const struct user_pass *up)
1300{
1301 struct gc_arena gc = gc_new();
1302 struct argv argv = argv_new();
1303 const char *tmp_file = "";
1304 int retval = OPENVPN_PLUGIN_FUNC_ERROR;
1305 struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1306
1307 /* Set environmental variables prior to calling script */
1308 setenv_str(session->opt->es, "script_type", "user-pass-verify");
1309
1310 /* format command line */
1311 argv_parse_cmd(&argv, session->opt->auth_user_pass_verify_script);
1312
1313 if (session->opt->auth_user_pass_verify_script_via_file)
1314 {
1315 struct status_output *so;
1316
1317 tmp_file = platform_create_temp_file(session->opt->tmp_dir, "up", &gc);
1318 if (tmp_file)
1319 {
1320 so = status_open(tmp_file, 0, -1, NULL, STATUS_OUTPUT_WRITE);
1321 status_printf(so, "%s", up->username);
1322 status_printf(so, "%s", up->password);
1323 if (!status_close(so))
1324 {
1325 msg(D_TLS_ERRORS, "TLS Auth Error: could not write username/password to file: %s",
1326 tmp_file);
1327 goto done;
1328 }
1329 /* pass temp file name to script */
1330 argv_printf_cat(&argv, "%s", tmp_file);
1331 }
1332 }
1333 else
1334 {
1335 /* username env is already set by set_verify_user_pass_env */
1336 setenv_str(session->opt->es, "password", up->password);
1337 }
1338
1339 /* pre-create files for deferred auth control */
1341 {
1343 "TLS Auth Error (%s): "
1344 "could not create deferred auth control file",
1345 __func__);
1347 goto error;
1348 }
1349
1350 /* call command */
1351 int script_ret =
1352 openvpn_run_script(&argv, session->opt->es, S_EXITCODE, "--auth-user-pass-verify");
1353 switch (script_ret)
1354 {
1355 case 0:
1357 break;
1358
1359 case 2:
1361 break;
1362
1363 default:
1366 break;
1367 }
1368 if (retval == OPENVPN_PLUGIN_FUNC_DEFERRED)
1369 {
1370 /* Check if we the plugin has written the pending auth control
1371 * file and send the pending auth to the client */
1373 {
1376 }
1377 }
1378 else
1379 {
1380 /* purge auth control filename (and file itself) for non-deferred returns */
1382 }
1383
1384done:
1385 if (tmp_file && strlen(tmp_file) > 0)
1386 {
1387 platform_unlink(tmp_file);
1388 }
1389
1390error:
1391 if (!session->opt->auth_user_pass_verify_script_via_file)
1392 {
1393 setenv_del(session->opt->es, "password");
1394 }
1395
1396 argv_free(&argv);
1397 gc_free(&gc);
1398 return retval;
1399}
1400
1401#ifdef ENABLE_PLUGIN
1402void
1403verify_crresponse_plugin(struct tls_multi *multi, const char *cr_response)
1404{
1405 struct tls_session *session = &multi->session[TM_ACTIVE];
1406 setenv_str(session->opt->es, "crresponse", cr_response);
1407
1408 plugin_call(session->opt->plugins, OPENVPN_PLUGIN_CLIENT_CRRESPONSE, NULL, NULL,
1409 session->opt->es);
1410
1411 setenv_del(session->opt->es, "crresponse");
1412}
1413#endif
1414
1415void
1416verify_crresponse_script(struct tls_multi *multi, const char *cr_response)
1417{
1418 struct tls_session *session = &multi->session[TM_ACTIVE];
1419
1420 if (!session->opt->client_crresponse_script)
1421 {
1422 return;
1423 }
1424 struct argv argv = argv_new();
1425 struct gc_arena gc = gc_new();
1426
1427 setenv_str(session->opt->es, "script_type", "client-crresponse");
1428
1429 /* Since cr response might be sensitive, like a stupid way to query
1430 * a password via 2FA, we pass it via file instead environment */
1431 const char *tmp_file = platform_create_temp_file(session->opt->tmp_dir, "cr", &gc);
1432 static const char *openerrmsg = "TLS CR Response Error: could not write "
1433 "crtext challenge response to file: %s";
1434
1435 if (tmp_file)
1436 {
1437 struct status_output *so = status_open(tmp_file, 0, -1, NULL, STATUS_OUTPUT_WRITE);
1438 status_printf(so, "%s", cr_response);
1439 if (!status_close(so))
1440 {
1441 msg(D_TLS_ERRORS, openerrmsg, tmp_file);
1442 tls_deauthenticate(multi);
1443 goto done;
1444 }
1445 }
1446 else
1447 {
1448 msg(D_TLS_ERRORS, openerrmsg, "creating file failed");
1449 tls_deauthenticate(multi);
1450 goto done;
1451 }
1452
1453 argv_parse_cmd(&argv, session->opt->client_crresponse_script);
1454 argv_printf_cat(&argv, "%s", tmp_file);
1455
1456
1457 if (!openvpn_run_script(&argv, session->opt->es, 0, "--client-crresponse"))
1458 {
1459 tls_deauthenticate(multi);
1460 }
1461done:
1462 argv_free(&argv);
1463 gc_free(&gc);
1464}
1465
1466/*
1467 * Verify the username and password using a plugin
1468 */
1469static int
1471 const struct user_pass *up)
1472{
1473 int retval = OPENVPN_PLUGIN_FUNC_ERROR;
1474 struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1475
1476 /* set password in private env space */
1477 setenv_str(session->opt->es, "password", up->password);
1478
1479 /* generate filename for deferred auth control file */
1481 {
1483 "TLS Auth Error (%s): "
1484 "could not create deferred auth control file",
1485 __func__);
1486 return retval;
1487 }
1488
1489 /* call command */
1490 retval = plugin_call(session->opt->plugins, OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY, NULL, NULL,
1491 session->opt->es);
1492
1493 if (retval == OPENVPN_PLUGIN_FUNC_DEFERRED)
1494 {
1495 /* Check if the plugin has written the pending auth control
1496 * file and send the pending auth to the client */
1498 {
1500 }
1501 }
1502
1503 if (retval == OPENVPN_PLUGIN_FUNC_ERROR)
1504 {
1506 }
1507
1508 if (retval != OPENVPN_PLUGIN_FUNC_DEFERRED)
1509 {
1510 /* purge auth control filename (and file itself) for non-deferred returns */
1512 }
1513
1514 setenv_del(session->opt->es, "password");
1515
1516 return retval;
1517}
1518
1519
1520#ifdef ENABLE_MANAGEMENT
1521/*
1522 * management deferred internal ssl_verify.c status codes
1523 */
1524#define KMDA_ERROR 0
1525#define KMDA_SUCCESS 1
1526#define KMDA_UNDEF 2
1527#define KMDA_DEF 3
1528
1529static int
1531{
1532 int retval = KMDA_ERROR;
1533 struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1534
1535 /* set username/password in private env space */
1536 setenv_str(session->opt->es, "password", up->password);
1537
1538 if (management)
1539 {
1541 session->opt->es);
1542 }
1543
1544 setenv_del(session->opt->es, "password");
1545
1546 retval = KMDA_SUCCESS;
1547
1548 return retval;
1549}
1550#endif /* ifdef ENABLE_MANAGEMENT */
1551
1552static bool
1554{
1555 /* Is username defined? */
1556 if ((session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL) || strlen(up->username))
1557 {
1558 setenv_str(session->opt->es, "username", up->username);
1559
1560 /* setenv incoming cert common name for script */
1561 setenv_str(session->opt->es, "common_name", session->common_name);
1562
1563 /* setenv client real IP address */
1565
1566 /*
1567 * if we are using auth-gen-token, send also the session id of auth gen token to
1568 * allow the management to figure out if it is a new session or a continued one
1569 */
1570 add_session_token_env(session, multi, up);
1571 return true;
1572 }
1573 else
1574 {
1575 msg(D_TLS_ERRORS, "TLS Auth Error: peer provided a blank username");
1576 return false;
1577 }
1578}
1579
1580bool
1581ssl_verify_username_length(struct tls_session *session, const char *username)
1582{
1583 if ((session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME)
1584 && strlen(username) > TLS_USERNAME_LEN)
1585 {
1587 "TLS Auth Error: --username-as-common name specified and "
1588 "username is longer than the maximum permitted Common Name "
1589 "length of %d characters",
1591 return false;
1592 }
1593 else
1594 {
1595 return true;
1596 }
1597}
1598
1605void
1606verify_user_pass(struct user_pass *up, struct tls_multi *multi, struct tls_session *session)
1607{
1608 struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1609
1610 ASSERT(up && !up->protected);
1611
1612#ifdef ENABLE_MANAGEMENT
1613 int man_def_auth = KMDA_UNDEF;
1614
1616 {
1617 man_def_auth = KMDA_DEF;
1618 }
1619#endif
1620
1621 /* enforce character class restrictions in username/password */
1623 string_mod(up->password, CC_PRINT, CC_CRLF, '_');
1624
1625 /*
1626 * If auth token succeeds we skip the auth
1627 * methods unless otherwise specified
1628 */
1629 bool skip_auth = false;
1630
1631 /* Replace username early if override-username is in effect but only
1632 * if client is sending the original username */
1633 if (multi->locked_original_username
1634 && strncmp(up->username, multi->locked_original_username, sizeof(up->username)) == 0)
1635 {
1637 "TLS: Replacing client provided username '%s' with "
1638 "username from override-user '%s'",
1639 up->username, multi->locked_username);
1640 strncpy(up->username, multi->locked_username, sizeof(up->username));
1641 }
1642
1643 /*
1644 * If server is configured with --auth-gen-token and the client sends
1645 * something that looks like an authentication token, this
1646 * round will be done internally using the token instead of
1647 * calling any external authentication modules.
1648 */
1649 if (session->opt->auth_token_generate && is_auth_token(up->password))
1650 {
1652
1653 /* If this is the first time we see an auth-token in this multi session,
1654 * save it as initial auth token. This ensures using the
1655 * same session ID and initial timestamp in new tokens */
1656 if (!multi->auth_token_initial)
1657 {
1658 multi->auth_token_initial = strdup(up->password);
1659 }
1660
1661 if (session->opt->auth_token_call_auth)
1662 {
1663 /*
1664 * we do not care about the result here because it is
1665 * the responsibility of the external authentication to
1666 * decide what to do with the result
1667 */
1668 }
1670 {
1671 /*
1672 * We do not want the EXPIRED or EMPTY USER flags here so check
1673 * for equality with AUTH_TOKEN_HMAC_OK
1674 */
1675 msg(M_WARN,
1676 "TLS: Username/auth-token authentication "
1677 "succeeded for username '%s'",
1678 up->username);
1679 skip_auth = true;
1680 }
1681 else
1682 {
1683 wipe_auth_token(multi);
1685 msg(M_WARN,
1686 "TLS: Username/auth-token authentication "
1687 "failed for username '%s'",
1688 up->username);
1689 return;
1690 }
1691 }
1692
1693 int plugin_status = OPENVPN_PLUGIN_FUNC_SUCCESS;
1694 int script_status = OPENVPN_PLUGIN_FUNC_SUCCESS;
1695 /* Set the environment variables used by all auth variants */
1696 if (!set_verify_user_pass_env(up, multi, session))
1697 {
1698 skip_auth = true;
1699 plugin_status = OPENVPN_PLUGIN_FUNC_ERROR;
1700 }
1701
1702 /* call plugin(s) and/or script */
1703 if (!skip_auth)
1704 {
1705#ifdef ENABLE_MANAGEMENT
1706 if (man_def_auth == KMDA_DEF)
1707 {
1708 man_def_auth = verify_user_pass_management(session, up);
1709 }
1710#endif
1712 {
1713 plugin_status = verify_user_pass_plugin(session, multi, up);
1714 }
1715
1716 if (session->opt->auth_user_pass_verify_script)
1717 {
1718 script_status = verify_user_pass_script(session, multi, up);
1719 }
1720 }
1721
1722 /* check sizing of username if it will become our common name */
1724 {
1725 plugin_status = OPENVPN_PLUGIN_FUNC_ERROR;
1726 script_status = OPENVPN_PLUGIN_FUNC_ERROR;
1727 }
1728
1729 /* auth succeeded? */
1730 bool plugin_ok = plugin_status == OPENVPN_PLUGIN_FUNC_SUCCESS
1731 || plugin_status == OPENVPN_PLUGIN_FUNC_DEFERRED;
1732
1733 bool script_ok = script_status == OPENVPN_PLUGIN_FUNC_SUCCESS
1734 || script_status == OPENVPN_PLUGIN_FUNC_DEFERRED;
1735
1736 if (script_ok && plugin_ok && tls_lock_username(multi, up->username)
1737#ifdef ENABLE_MANAGEMENT
1738 && man_def_auth != KMDA_ERROR
1739#endif
1740 )
1741 {
1743 if (plugin_status == OPENVPN_PLUGIN_FUNC_DEFERRED
1744 || script_status == OPENVPN_PLUGIN_FUNC_DEFERRED)
1745 {
1747 }
1748#ifdef ENABLE_MANAGEMENT
1749 if (man_def_auth != KMDA_UNDEF)
1750 {
1751 if (skip_auth)
1752 {
1754 }
1755 else
1756 {
1758 }
1759 }
1760#endif
1761 if ((session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME))
1762 {
1764 }
1765
1766 if ((session->opt->auth_token_generate))
1767 {
1768 /*
1769 * If we accepted a (not expired) token, i.e.
1770 * initial auth via token on new connection, we need
1771 * to store the auth-token in multi->auth_token, so
1772 * the initial timestamp and session id can be extracted from it
1773 */
1776 {
1777 multi->auth_token = strdup(up->password);
1778 }
1779
1780 /*
1781 * Server is configured with --auth-gen-token. Generate or renew
1782 * the token.
1783 */
1784 generate_auth_token(up, multi);
1785 }
1786
1787 msg(D_HANDSHAKE, "TLS: Username/Password authentication %s for username '%s' %s",
1788 (ks->authenticated == KS_AUTH_DEFERRED) ? "deferred" : "succeeded", up->username,
1789 (session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME) ? "[CN SET]" : "");
1790 }
1791 else
1792 {
1794 msg(D_TLS_ERRORS, "TLS Auth Error: Auth Username/Password verification failed for peer");
1795 }
1796}
1797
1798void
1800{
1801 struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1802
1803 /* While it shouldn't really happen, don't allow the common name to be NULL */
1804 if (!session->common_name)
1805 {
1807 }
1808
1809 /* Don't allow the CN to change once it's been locked */
1810 if (ks->authenticated > KS_AUTH_FALSE && multi->locked_cn)
1811 {
1812 const char *cn = session->common_name;
1813 if (cn && strcmp(cn, multi->locked_cn))
1814 {
1816 "TLS Auth Error: TLS object CN attempted to change from '%s' to '%s' -- tunnel disabled",
1817 multi->locked_cn, cn);
1818
1819 /* change the common name back to its original value and disable the tunnel */
1821 tls_deauthenticate(multi);
1822 }
1823 }
1824
1825 /* Don't allow the cert hashes to change once they have been locked */
1827 {
1828 const struct cert_hash_set *chs = session->cert_hash_set;
1829 if (chs && !cert_hash_compare(chs, multi->locked_cert_hash_set))
1830 {
1832 "TLS Auth Error: TLS object CN=%s client-provided SSL certs unexpectedly changed during mid-session reauth",
1833 session->common_name);
1834
1835 /* disable the tunnel */
1836 tls_deauthenticate(multi);
1837 }
1838 }
1839
1840 /* verify --client-config-dir based authentication */
1841 if (ks->authenticated > KS_AUTH_FALSE && session->opt->client_config_dir_exclusive)
1842 {
1843 struct gc_arena gc = gc_new();
1844
1845 const char *cn = session->common_name;
1846 const char *path = platform_gen_path(session->opt->client_config_dir_exclusive, cn, &gc);
1847 if (!cn || !strcmp(cn, CCD_DEFAULT) || !platform_test_file(path))
1848 {
1850 wipe_auth_token(multi);
1852 "TLS Auth Error: --client-config-dir authentication failed for common name '%s' file='%s'",
1853 session->common_name, path ? path : "UNDEF");
1854 }
1855
1856 gc_free(&gc);
1857 }
1858}
1859
1860void
1862{
1863 struct env_item *item = es->list;
1864 while (item)
1865 {
1866 struct env_item *next = item->next;
1867 if (item->string && 0 == strncmp("X509_", item->string, strlen("X509_")))
1868 {
1869 env_set_del(es, item->string);
1870 }
1871 item = next;
1872 }
1873}
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:481
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:101
void argv_msg_prefix(const msglvl_t msglevel, const struct argv *a, const char *prefix)
Similar to argv_msg() but prefixes the messages being written with a given string.
Definition argv.c:259
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:438
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:462
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:87
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:161
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:291
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:38
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:398
static bool is_auth_token(const char *password)
Return if the password string has the format of a password.
Definition auth_token.h:121
bool buf_printf(struct buffer *buf, const char *format,...)
Definition buffer.c:241
void string_replace_leading(char *str, const char match, const char replace)
Definition buffer.c:1120
struct buffer_list * buffer_list_file(const char *fn, int max_line_len)
Definition buffer.c:1350
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1182
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
char * format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Definition buffer.c:483
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Modifies a string in place by replacing certain classes of characters of it with a specified characte...
Definition buffer.c:1054
struct buffer buffer_read_from_file(const char *filename, struct gc_arena *gc)
buffer_read_from_file - copy the content of a file into a buffer
Definition buffer.c:1373
bool checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
Definition buffer.c:1138
char * string_alloc(const char *str, struct gc_arena *gc)
Definition buffer.c:648
void buf_chomp(struct buffer *buf)
Definition buffer.c:553
#define ALLOC_OBJ(dptr, type)
Definition buffer.h:1083
#define BSTR(buf)
Definition buffer.h:129
#define BPTR(buf)
Definition buffer.h:123
#define CC_CRLF
carriage return or newline
Definition buffer.h:915
#define BLEN(buf)
Definition buffer.h:126
#define BLENZ(buf)
Definition buffer.h:127
static void gc_free(struct gc_arena *a)
Definition buffer.h:1049
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:886
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition buffer.h:1088
static struct gc_arena gc_new(void)
Definition buffer.h:1041
#define CCD_DEFAULT
Definition common.h:63
#define ENABLE_MANAGEMENT
Definition config.h:53
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
@ MD_SHA256
@ MD_SHA1
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:307
bool env_set_del(struct env_set *es, const char *str)
Definition env_set.c:183
void setenv_del(struct env_set *es, const char *name)
Definition env_set.c:352
#define D_HANDSHAKE
Definition errlevel.h:71
#define D_MULTI_LOW
Definition errlevel.h:85
#define D_TLS_ERRORS
Definition errlevel.h:58
#define M_INFO
Definition errlevel.h:54
#define D_TLS_DEBUG
Definition errlevel.h:164
#define KS_SIZE
Size of the tls_session.key array.
Definition ssl_common.h:469
#define KS_PRIMARY
Primary key state index.
Definition ssl_common.h:465
#define TM_SIZE
Size of the tls_multi.session \ array.
Definition ssl_common.h:550
#define TM_ACTIVE
Active tls_session.
Definition ssl_common.h:545
#define TLS_AUTHENTICATED(multi, ks)
Check whether the ks key_state has finished the key exchange part of the OpenVPN hand shake.
Definition ssl_verify.h:113
static unsigned int min_uint(unsigned int x, unsigned int y)
Definition integer.h:66
static int max_int(int x, int y)
Definition integer.h:92
static SERVICE_STATUS status
Definition interactive.c:51
void management_notify_client_needing_auth(struct management *management, const unsigned int mda_key_id, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3069
static bool management_enable_def_auth(const struct management *man)
Definition manage.h:440
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
#define OPENVPN_PLUGIN_TLS_VERIFY
X509 openvpn_x509_cert_t
#define OPENVPN_PLUGIN_FUNC_DEFERRED
#define OPENVPN_PLUGIN_FUNC_SUCCESS
#define OPENVPN_PLUGIN_FUNC_ERROR
#define SIZE(x)
Definition basic.h:29
#define dmsg(flags,...)
Definition error.h:172
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
#define MAX_PARMS
Definition options.h:51
time_t now
Definition otime.c:33
bool platform_test_file(const char *filename)
Return true if filename can be opened for read.
Definition platform.c:655
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:540
const char * platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc)
Put a directory and filename together.
Definition platform.c:591
bool platform_unlink(const char *filename)
Definition platform.c:487
int platform_open(const char *path, int flags, int mode)
Definition platform.c:513
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:782
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition plugin.c:904
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:195
bool send_auth_pending_messages(struct tls_multi *tls_multi, struct tls_session *session, const char *extra, unsigned int timeout)
Sends the auth pending control messages to a client.
Definition push.c:438
#define S_EXITCODE
Instead of returning 1/0 for success/fail, return exit code when between 0 and 255 and -1 otherwise.
Definition run_command.h:53
static int openvpn_run_script(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *hook)
Will run a script and return the exit code of the script if between 0 and 255, -1 otherwise.
Definition run_command.h:89
void setenv_link_socket_actual(struct env_set *es, const char *name_prefix, const struct link_socket_actual *act, const unsigned int flags)
#define SA_IP_PORT
Definition socket_util.h:99
void tls_clear_error(void)
Clear the underlying SSL library's error state.
#define AUTH_TOKEN_HMAC_OK
Auth-token sent from client has valid hmac.
Definition ssl_common.h:687
#define KEY_SCAN_SIZE
Definition ssl_common.h:567
auth_deferred_result
Definition ssl_common.h:175
@ ACF_PENDING
deferred auth still pending
Definition ssl_common.h:176
@ ACF_SUCCEEDED
deferred auth has suceeded
Definition ssl_common.h:177
@ ACF_FAILED
deferred auth has failed
Definition ssl_common.h:179
@ ACF_DISABLED
deferred auth is not used
Definition ssl_common.h:178
#define AUTH_TOKEN_EXPIRED
Auth-token sent from client has expired.
Definition ssl_common.h:689
static struct key_state * get_key_scan(struct tls_multi *multi, int index)
gets an item of key_state objects in the order they should be scanned by data channel modules.
Definition ssl_common.h:733
#define SSLF_AUTH_USER_PASS_OPTIONAL
Definition ssl_common.h:427
@ KS_AUTH_TRUE
Key state is authenticated.
Definition ssl_common.h:157
@ KS_AUTH_FALSE
Key state is not authenticated
Definition ssl_common.h:154
@ KS_AUTH_DEFERRED
Key state authentication is being deferred, by async auth.
Definition ssl_common.h:155
#define SSLF_CRL_VERIFY_DIR
Definition ssl_common.h:429
static const struct key_state * get_primary_key(const struct tls_multi *multi)
gets an item of key_state objects in the order they should be scanned by data channel modules.
Definition ssl_common.h:756
#define SSLF_USERNAME_AS_COMMON_NAME
Definition ssl_common.h:426
char * extract_var_peer_info(const char *peer_info, const char *var, struct gc_arena *gc)
Extracts a variable from peer info, the returned string will be allocated using the supplied gc_arena...
Definition ssl_util.c:31
SSL utility functions.
static result_t verify_cert_call_command(const char *verify_command, struct env_set *es, int cert_depth, char *subject)
Definition ssl_verify.c:514
static void verify_cert_set_env(struct env_set *es, openvpn_x509_cert_t *peer_cert, int cert_depth, const char *subject, const struct x509_track *x509_track)
Definition ssl_verify.c:408
void key_state_rm_auth_control_files(struct auth_deferred_status *ads)
Removes auth_pending and auth_control files from file system and key_state structure.
Definition ssl_verify.c:965
static bool key_state_gen_auth_control_files(struct auth_deferred_status *ads, const struct tls_options *opt)
Generates and creates the control files used for deferred authentification in the temporary directory...
Definition ssl_verify.c:989
static struct cert_hash_set * cert_hash_copy(const struct cert_hash_set *chs)
Definition ssl_verify.c:266
static void key_state_rm_auth_pending_file(struct auth_deferred_status *ads)
Removes auth_pending file from the file system and key_state structure.
Definition ssl_verify.c:846
#define KMDA_SUCCESS
bool ssl_verify_username_length(struct tls_session *session, const char *username)
Checks if the username length is valid to use.
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:546
#define KMDA_DEF
bool tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, const bool auth, const char *client_reason)
static void verify_cert_cert_delete_env(struct env_set *es, const char *pem_export_fname)
Definition ssl_verify.c:469
static void tls_deauthenticate(struct tls_multi *multi)
Definition ssl_verify.c:69
void verify_crresponse_plugin(struct tls_multi *multi, const char *cr_response)
Call the plugin OPENVPN_PLUGIN_CLIENT_CRRESPONSE.
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:229
void tls_x509_clear_env(struct env_set *es)
Remove any X509_ env variables from env_set es.
static void update_key_auth_status(bool cached, struct key_state *ks)
This method takes a key_state and if updates the state of the key if it is deferred.
void tls_lock_cert_hash_set(struct tls_multi *multi)
Locks the certificate hash set used in the given tunnel.
Definition ssl_verify.c:286
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:323
result_t verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
Definition ssl_verify.c:588
void tls_lock_common_name(struct tls_multi *multi)
Locks the common name field for the given tunnel.
Definition ssl_verify.c:132
static enum auth_deferred_result man_def_auth_test(const struct key_state *ks)
Definition ssl_verify.c:828
#define KMDA_UNDEF
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,...
static bool tls_lock_username(struct tls_multi *multi, const char *username)
Definition ssl_verify.c:145
static int verify_user_pass_plugin(struct tls_session *session, struct tls_multi *multi, const struct user_pass *up)
static bool key_state_check_auth_pending_file(struct auth_deferred_status *ads, struct tls_multi *multi, struct tls_session *session)
Checks if the deferred state should also send auth pending request to the client.
Definition ssl_verify.c:897
const char * tls_username(const struct tls_multi *multi, const bool null)
Returns the username field for the given tunnel.
Definition ssl_verify.c:172
void set_common_name(struct tls_session *session, const char *common_name)
Sets the common name field for the given tunnel.
Definition ssl_verify.c:85
#define KMDA_ERROR
static char * key_state_check_auth_failed_message_file(const struct auth_deferred_status *ads, struct gc_arena *gc)
Checks if the auth failed reason file has any content and if yes it will be returned as string alloca...
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:814
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:482
enum tls_auth_status tls_authentication_status(struct tls_multi *multi)
Return current session authentication state of the tls_multi structure This will return TLS_AUTHENTIC...
void verify_user_pass(struct user_pass *up, struct tls_multi *multi, struct tls_session *session)
Main username/password verification entry point.
static bool tls_authentication_status_use_cache(struct tls_multi *multi)
uses cache_intervals times to determine if we should update the cache.
static void check_for_client_reason(struct tls_multi *multi, struct auth_deferred_status *status)
Check if the script/plugin left a message in the auth failed message file and relay it to the user.
static const char * print_nsCertType(int type)
Definition ssl_verify.c:299
void verify_crresponse_script(struct tls_multi *multi, const char *cr_response)
Runs the –client-crresponse script if one is defined.
static time_t cache_intervals[]
The minimum times to have passed to update the cache.
static int verify_user_pass_script(struct tls_session *session, struct tls_multi *multi, const struct user_pass *up)
static enum auth_deferred_result key_state_test_auth_control_file(struct auth_deferred_status *ads, bool cached)
Checks the auth control status from a file.
static void string_mod_remap_name(char *str)
Definition ssl_verify.c:51
void cert_hash_remember(struct tls_session *session, const int error_depth, const struct buffer *cert_hash)
Definition ssl_verify.c:194
static void setenv_untrusted(struct tls_session *session)
Definition ssl_verify.c:60
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:107
static int verify_user_pass_management(struct tls_session *session, const struct user_pass *up)
static bool verify_cert_cert_export_env(struct env_set *es, openvpn_x509_cert_t *peer_cert, const char *pem_export_fname)
Exports the certificate in peer_cert into the environment and adds the filname.
Definition ssl_verify.c:459
void cert_hash_free(struct cert_hash_set *chs)
Frees the given set of certificate hashes.
Definition ssl_verify.c:215
static bool check_auth_pending_method(const char *peer_info, const char *method)
Check peer_info if the client supports the requested pending auth method.
Definition ssl_verify.c:860
static bool set_verify_user_pass_env(struct user_pass *up, struct tls_multi *multi, struct tls_session *session)
Control Channel Verification Module.
tls_auth_status
Definition ssl_verify.h:74
@ TLS_AUTHENTICATION_DEFERRED
Definition ssl_verify.h:77
@ TLS_AUTHENTICATION_SUCCEEDED
Definition ssl_verify.h:75
@ TLS_AUTHENTICATION_FAILED
Definition ssl_verify.h:76
#define VERIFY_X509_SUBJECT_DN
Definition ssl_verify.h:69
#define VERIFY_X509_SUBJECT_RDN
Definition ssl_verify.h:70
#define NS_CERT_CHECK_CLIENT
Do not perform Netscape certificate type verification.
Definition ssl_verify.h:254
#define VERIFY_X509_NONE
Definition ssl_verify.h:68
#define TLS_USERNAME_LEN
Maximum length of common name.
Definition ssl_verify.h:54
#define VERIFY_X509_SUBJECT_RDN_PREFIX
Definition ssl_verify.h:71
#define MAX_CERT_DEPTH
Maximum certificate depth we will allow.
Definition ssl_verify.h:51
#define NS_CERT_CHECK_NONE
Do not perform Netscape certificate type verification.
Definition ssl_verify.h:250
#define NS_CERT_CHECK_SERVER
Do not perform Netscape certificate type verification.
Definition ssl_verify.h:252
Control Channel Verification Module library-specific backend interface.
struct buffer x509_get_sha256_fingerprint(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Retrieve the certificate's SHA256 fingerprint.
void x509_setenv_track(const struct x509_track *xt, struct env_set *es, const int depth, openvpn_x509_cert_t *x509)
void x509_setenv(struct env_set *es, int cert_depth, openvpn_x509_cert_t *cert)
bool tls_verify_crl_missing(const struct tls_options *opt)
Return true iff a CRL is configured, but is not loaded.
result_t backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *filename)
result_t x509_verify_ns_cert_type(openvpn_x509_cert_t *cert, const int usage)
char * backend_x509_get_serial_hex(openvpn_x509_cert_t *cert, struct gc_arena *gc)
struct buffer x509_get_sha1_fingerprint(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Retrieve the certificate's SHA1 fingerprint.
result_t x509_verify_cert_ku(openvpn_x509_cert_t *x509, const unsigned *const expected_ku, size_t expected_len)
char * x509_get_subject(openvpn_x509_cert_t *cert, struct gc_arena *gc)
char * backend_x509_get_serial(openvpn_x509_cert_t *cert, struct gc_arena *gc)
result_t backend_x509_get_username(char *common_name, size_t cn_len, char *x509_username_field, openvpn_x509_cert_t *peer_cert)
result_t
Result of verification function.
@ FAILURE
@ SUCCESS
result_t x509_verify_cert_eku(openvpn_x509_cert_t *x509, const char *const expected_oid)
Control Channel Verification Module OpenSSL backend.
void status_printf(struct status_output *so, const char *format,...)
Definition status.c:212
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:59
bool status_close(struct status_output *so)
Definition status.c:178
#define STATUS_OUTPUT_WRITE
Definition status.h:51
Definition argv.h:35
char * auth_failed_reason_file
Definition ssl_common.h:168
struct buffer_entry * next
Definition buffer.h:1145
struct buffer_entry * head
Definition buffer.h:1150
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int capacity
Size in bytes of memory allocated by malloc().
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
Structure containing the hashes for a full certificate chain.
Definition ssl_verify.h:64
struct cert_hash * ch[MAX_CERT_DEPTH]
Array of certificate hashes.
Definition ssl_verify.h:65
Structure containing the hash for a single certificate.
Definition ssl_verify.h:58
unsigned char sha256_hash[256/8]
Definition ssl_verify.h:59
char * string
Definition env_set.h:38
struct env_item * next
Definition env_set.h:39
struct env_item * list
Definition env_set.h:45
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Security parameter state of one TLS and data channel key session.
Definition ssl_common.h:208
unsigned int auth_token_state_flags
The state of the auth-token sent from the client.
Definition ssl_common.h:211
struct auth_deferred_status plugin_auth
Definition ssl_common.h:268
unsigned int mda_key_id
Definition ssl_common.h:263
struct auth_deferred_status script_auth
Definition ssl_common.h:269
enum auth_deferred_result mda_status
Definition ssl_common.h:264
enum ks_auth_state authenticated
Definition ssl_common.h:259
time_t auth_deferred_expire
Definition ssl_common.h:260
Security parameter state for a single VPN tunnel.
Definition ssl_common.h:612
char * auth_token_initial
The first auth-token we sent to a client.
Definition ssl_common.h:684
char * peer_info
A multi-line string of general-purpose info received from peer over control channel.
Definition ssl_common.h:673
char * locked_username
The locked username is the username we assume the client is using.
Definition ssl_common.h:650
unsigned int tas_cache_num_updates
The number of times we updated the cache.
Definition ssl_common.h:664
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer.
Definition ssl_common.h:712
struct cert_hash_set * locked_cert_hash_set
Definition ssl_common.h:656
time_t tas_cache_last_update
Time of last when we updated the cached state of tls_authentication_status deferred files.
Definition ssl_common.h:661
char * locked_cn
Our locked common name, username, and cert hashes (cannot change during the life of this tls_multi ob...
Definition ssl_common.h:645
char * client_reason
An error message to send to client on AUTH_FAILED.
Definition ssl_common.h:667
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:679
char * locked_original_username
The username that client initially used before being overridden by –override-user.
Definition ssl_common.h:654
struct env_set * es
Definition ssl_common.h:414
unsigned remote_cert_ku[MAX_PARMS]
Definition ssl_common.h:359
const char * tmp_dir
Definition ssl_common.h:395
int verify_hash_depth
Definition ssl_common.h:362
const struct plugin_list * plugins
Definition ssl_common.h:416
const char * export_peer_cert_dir
Definition ssl_common.h:396
char * x509_username_field[MAX_PARMS]
Definition ssl_common.h:365
const char * verify_command
Definition ssl_common.h:353
struct verify_hash_list * verify_hash
Definition ssl_common.h:361
int verify_x509_type
Definition ssl_common.h:354
const char * verify_x509_name
Definition ssl_common.h:355
const struct x509_track * x509_track
Definition ssl_common.h:441
unsigned int ssl_flags
Definition ssl_common.h:435
hash_algo_type verify_hash_algo
Definition ssl_common.h:364
const char * crl_file
Definition ssl_common.h:356
const char * remote_cert_eku
Definition ssl_common.h:360
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:490
struct key_state key[KS_SIZE]
Definition ssl_common.h:525
struct cert_hash_set * cert_hash_set
Definition ssl_common.h:518
char * common_name
Definition ssl_common.h:516
bool protected
Definition misc.h:61
char password[USER_PASS_LEN]
Definition misc.h:71
char username[USER_PASS_LEN]
Definition misc.h:70
struct verify_hash_list * next
Definition options.h:250
uint8_t hash[SHA256_DIGEST_LENGTH]
Definition options.h:249
#define PATH_SEPARATOR
Definition syshead.h:432
struct env_set * es
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:133