1*bf6873c5SCy Schubert /*
2*bf6873c5SCy Schubert * Tests for anonymous FAST support in pam-krb5.
3*bf6873c5SCy Schubert *
4*bf6873c5SCy Schubert * Tests for anonymous Flexible Authentication Secure Tunneling, a mechanism
5*bf6873c5SCy Schubert * for improving the preauthentication part of the Kerberos protocol and
6*bf6873c5SCy Schubert * protecting it against various attacks.
7*bf6873c5SCy Schubert *
8*bf6873c5SCy Schubert * This is broken out from the other FAST tests because it uses PKINIT, and
9*bf6873c5SCy Schubert * PKINIT code cannot be tested under valgrind with MIT Kerberos due to some
10*bf6873c5SCy Schubert * bug in valgrind.
11*bf6873c5SCy Schubert *
12*bf6873c5SCy Schubert * Written by Russ Allbery <eagle@eyrie.org>
13*bf6873c5SCy Schubert * Copyright 2017, 2020 Russ Allbery <eagle@eyrie.org>
14*bf6873c5SCy Schubert * Copyright 2012
15*bf6873c5SCy Schubert * The Board of Trustees of the Leland Stanford Junior University
16*bf6873c5SCy Schubert *
17*bf6873c5SCy Schubert * SPDX-License-Identifier: BSD-3-clause or GPL-1+
18*bf6873c5SCy Schubert */
19*bf6873c5SCy Schubert
20*bf6873c5SCy Schubert #include <config.h>
21*bf6873c5SCy Schubert #include <portable/krb5.h>
22*bf6873c5SCy Schubert #include <portable/system.h>
23*bf6873c5SCy Schubert
24*bf6873c5SCy Schubert #include <tests/fakepam/script.h>
25*bf6873c5SCy Schubert #include <tests/tap/kerberos.h>
26*bf6873c5SCy Schubert
27*bf6873c5SCy Schubert
28*bf6873c5SCy Schubert /*
29*bf6873c5SCy Schubert * Test whether anonymous authentication works. If this doesn't, we need to
30*bf6873c5SCy Schubert * skip the tests of anonymous FAST.
31*bf6873c5SCy Schubert */
32*bf6873c5SCy Schubert static bool
anon_fast_works(void)33*bf6873c5SCy Schubert anon_fast_works(void)
34*bf6873c5SCy Schubert {
35*bf6873c5SCy Schubert krb5_context ctx;
36*bf6873c5SCy Schubert krb5_error_code retval;
37*bf6873c5SCy Schubert krb5_principal princ = NULL;
38*bf6873c5SCy Schubert char *realm;
39*bf6873c5SCy Schubert krb5_creds creds;
40*bf6873c5SCy Schubert krb5_get_init_creds_opt *opts = NULL;
41*bf6873c5SCy Schubert
42*bf6873c5SCy Schubert /* Construct the anonymous principal name. */
43*bf6873c5SCy Schubert retval = krb5_init_context(&ctx);
44*bf6873c5SCy Schubert if (retval != 0)
45*bf6873c5SCy Schubert bail("cannot initialize Kerberos");
46*bf6873c5SCy Schubert retval = krb5_get_default_realm(ctx, &realm);
47*bf6873c5SCy Schubert if (retval != 0)
48*bf6873c5SCy Schubert bail("cannot get default realm");
49*bf6873c5SCy Schubert retval = krb5_build_principal_ext(
50*bf6873c5SCy Schubert ctx, &princ, (unsigned int) strlen(realm), realm,
51*bf6873c5SCy Schubert strlen(KRB5_WELLKNOWN_NAME), KRB5_WELLKNOWN_NAME,
52*bf6873c5SCy Schubert strlen(KRB5_ANON_NAME), KRB5_ANON_NAME, NULL);
53*bf6873c5SCy Schubert if (retval != 0)
54*bf6873c5SCy Schubert bail("cannot construct anonymous principal");
55*bf6873c5SCy Schubert krb5_free_default_realm(ctx, realm);
56*bf6873c5SCy Schubert
57*bf6873c5SCy Schubert /* Obtain the credentials. */
58*bf6873c5SCy Schubert memset(&creds, 0, sizeof(creds));
59*bf6873c5SCy Schubert retval = krb5_get_init_creds_opt_alloc(ctx, &opts);
60*bf6873c5SCy Schubert if (retval != 0)
61*bf6873c5SCy Schubert bail("cannot create credential options");
62*bf6873c5SCy Schubert krb5_get_init_creds_opt_set_anonymous(opts, 1);
63*bf6873c5SCy Schubert krb5_get_init_creds_opt_set_tkt_life(opts, 60);
64*bf6873c5SCy Schubert retval = krb5_get_init_creds_password(ctx, &creds, princ, NULL, NULL, NULL,
65*bf6873c5SCy Schubert 0, NULL, opts);
66*bf6873c5SCy Schubert
67*bf6873c5SCy Schubert /* Clean up. */
68*bf6873c5SCy Schubert if (princ != NULL)
69*bf6873c5SCy Schubert krb5_free_principal(ctx, princ);
70*bf6873c5SCy Schubert if (opts != NULL)
71*bf6873c5SCy Schubert krb5_get_init_creds_opt_free(ctx, opts);
72*bf6873c5SCy Schubert krb5_free_cred_contents(ctx, &creds);
73*bf6873c5SCy Schubert
74*bf6873c5SCy Schubert /* Return whether authentication succeeded. */
75*bf6873c5SCy Schubert return (retval == 0);
76*bf6873c5SCy Schubert }
77*bf6873c5SCy Schubert
78*bf6873c5SCy Schubert
79*bf6873c5SCy Schubert int
main(void)80*bf6873c5SCy Schubert main(void)
81*bf6873c5SCy Schubert {
82*bf6873c5SCy Schubert struct script_config config;
83*bf6873c5SCy Schubert struct kerberos_config *krbconf;
84*bf6873c5SCy Schubert
85*bf6873c5SCy Schubert /* Skip the test if FAST is not available. */
86*bf6873c5SCy Schubert #ifndef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_FAST_CCACHE_NAME
87*bf6873c5SCy Schubert skip_all("FAST support not available");
88*bf6873c5SCy Schubert #endif
89*bf6873c5SCy Schubert
90*bf6873c5SCy Schubert /* Initialize Kerberos configuration. */
91*bf6873c5SCy Schubert krbconf = kerberos_setup(TAP_KRB_NEEDS_PASSWORD);
92*bf6873c5SCy Schubert memset(&config, 0, sizeof(config));
93*bf6873c5SCy Schubert config.user = krbconf->username;
94*bf6873c5SCy Schubert config.authtok = krbconf->password;
95*bf6873c5SCy Schubert config.extra[0] = krbconf->userprinc;
96*bf6873c5SCy Schubert kerberos_generate_conf(krbconf->realm);
97*bf6873c5SCy Schubert
98*bf6873c5SCy Schubert /* Skip the test if anonymous PKINIT doesn't work. */
99*bf6873c5SCy Schubert if (!anon_fast_works())
100*bf6873c5SCy Schubert skip_all("anonymous PKINIT failed");
101*bf6873c5SCy Schubert
102*bf6873c5SCy Schubert /* Test anonymous FAST. */
103*bf6873c5SCy Schubert plan_lazy();
104*bf6873c5SCy Schubert run_script("data/scripts/fast/anonymous", &config);
105*bf6873c5SCy Schubert run_script("data/scripts/fast/anonymous-debug", &config);
106*bf6873c5SCy Schubert
107*bf6873c5SCy Schubert return 0;
108*bf6873c5SCy Schubert }
109