1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/gssapi/t_add_cred.c - gss_add_cred() tests */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2018 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert * are met:
10*7f2fe78bSCy Schubert *
11*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert *
14*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert * distribution.
18*7f2fe78bSCy Schubert *
19*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert */
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert * This program tests the mechglue behavior of gss_add_cred(). It relies on a
35*7f2fe78bSCy Schubert * krb5 keytab and credentials being present so that initiator and acceptor
36*7f2fe78bSCy Schubert * credentials can be acquired, but does not use them to initiate or accept any
37*7f2fe78bSCy Schubert * requests.
38*7f2fe78bSCy Schubert */
39*7f2fe78bSCy Schubert
40*7f2fe78bSCy Schubert #include <stdio.h>
41*7f2fe78bSCy Schubert #include <assert.h>
42*7f2fe78bSCy Schubert
43*7f2fe78bSCy Schubert #include "common.h"
44*7f2fe78bSCy Schubert
45*7f2fe78bSCy Schubert int
main()46*7f2fe78bSCy Schubert main()
47*7f2fe78bSCy Schubert {
48*7f2fe78bSCy Schubert OM_uint32 minor, major;
49*7f2fe78bSCy Schubert gss_cred_id_t cred1, cred2;
50*7f2fe78bSCy Schubert gss_cred_usage_t usage;
51*7f2fe78bSCy Schubert gss_name_t name;
52*7f2fe78bSCy Schubert
53*7f2fe78bSCy Schubert /* Check that we get the expected error if we pass neither an input nor an
54*7f2fe78bSCy Schubert * output cred handle. */
55*7f2fe78bSCy Schubert major = gss_add_cred(&minor, GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME,
56*7f2fe78bSCy Schubert &mech_krb5, GSS_C_INITIATE, GSS_C_INDEFINITE,
57*7f2fe78bSCy Schubert GSS_C_INDEFINITE, NULL, NULL, NULL, NULL);
58*7f2fe78bSCy Schubert assert(major == (GSS_S_CALL_INACCESSIBLE_WRITE | GSS_S_NO_CRED));
59*7f2fe78bSCy Schubert
60*7f2fe78bSCy Schubert /* Regression test for #8737: make sure that desired_name is honored when
61*7f2fe78bSCy Schubert * creating a credential by passing in a non-matching name. */
62*7f2fe78bSCy Schubert name = import_name("p:does/not/match@WRONG_REALM");
63*7f2fe78bSCy Schubert major = gss_add_cred(&minor, GSS_C_NO_CREDENTIAL, name, &mech_krb5,
64*7f2fe78bSCy Schubert GSS_C_INITIATE, GSS_C_INDEFINITE, GSS_C_INDEFINITE,
65*7f2fe78bSCy Schubert &cred1, NULL, NULL, NULL);
66*7f2fe78bSCy Schubert assert(major == GSS_S_NO_CRED);
67*7f2fe78bSCy Schubert gss_release_name(&minor, &name);
68*7f2fe78bSCy Schubert
69*7f2fe78bSCy Schubert /* Create cred1 with a krb5 initiator cred by passing an output handle but
70*7f2fe78bSCy Schubert * no input handle. */
71*7f2fe78bSCy Schubert major = gss_add_cred(&minor, GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME,
72*7f2fe78bSCy Schubert &mech_krb5, GSS_C_INITIATE, GSS_C_INDEFINITE,
73*7f2fe78bSCy Schubert GSS_C_INDEFINITE, &cred1, NULL, NULL, NULL);
74*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE);
75*7f2fe78bSCy Schubert
76*7f2fe78bSCy Schubert /* Verify that cred1 has the expected mechanism creds. */
77*7f2fe78bSCy Schubert major = gss_inquire_cred_by_mech(&minor, cred1, &mech_krb5, NULL, NULL,
78*7f2fe78bSCy Schubert NULL, &usage);
79*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE && usage == GSS_C_INITIATE);
80*7f2fe78bSCy Schubert major = gss_inquire_cred_by_mech(&minor, cred1, &mech_iakerb, NULL, NULL,
81*7f2fe78bSCy Schubert NULL, &usage);
82*7f2fe78bSCy Schubert assert(major == GSS_S_NO_CRED);
83*7f2fe78bSCy Schubert
84*7f2fe78bSCy Schubert /* Check that we get the expected error if we try to add another krb5 mech
85*7f2fe78bSCy Schubert * cred to cred1. */
86*7f2fe78bSCy Schubert major = gss_add_cred(&minor, cred1, GSS_C_NO_NAME, &mech_krb5,
87*7f2fe78bSCy Schubert GSS_C_INITIATE, GSS_C_INDEFINITE, GSS_C_INDEFINITE,
88*7f2fe78bSCy Schubert NULL, NULL, NULL, NULL);
89*7f2fe78bSCy Schubert assert(major == GSS_S_DUPLICATE_ELEMENT);
90*7f2fe78bSCy Schubert
91*7f2fe78bSCy Schubert /* Add an IAKERB acceptor mech cred to cred1. */
92*7f2fe78bSCy Schubert major = gss_add_cred(&minor, cred1, GSS_C_NO_NAME, &mech_iakerb,
93*7f2fe78bSCy Schubert GSS_C_ACCEPT, GSS_C_INDEFINITE, GSS_C_INDEFINITE,
94*7f2fe78bSCy Schubert NULL, NULL, NULL, NULL);
95*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE);
96*7f2fe78bSCy Schubert
97*7f2fe78bSCy Schubert /* Verify cred1 mechanism creds. */
98*7f2fe78bSCy Schubert major = gss_inquire_cred_by_mech(&minor, cred1, &mech_krb5, NULL, NULL,
99*7f2fe78bSCy Schubert NULL, &usage);
100*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE && usage == GSS_C_INITIATE);
101*7f2fe78bSCy Schubert major = gss_inquire_cred_by_mech(&minor, cred1, &mech_iakerb, NULL, NULL,
102*7f2fe78bSCy Schubert NULL, &usage);
103*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE && usage == GSS_C_ACCEPT);
104*7f2fe78bSCy Schubert
105*7f2fe78bSCy Schubert /* Start over with another new cred. */
106*7f2fe78bSCy Schubert gss_release_cred(&minor, &cred1);
107*7f2fe78bSCy Schubert major = gss_add_cred(&minor, GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME,
108*7f2fe78bSCy Schubert &mech_krb5, GSS_C_ACCEPT, GSS_C_INDEFINITE,
109*7f2fe78bSCy Schubert GSS_C_INDEFINITE, &cred1, NULL, NULL, NULL);
110*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE);
111*7f2fe78bSCy Schubert
112*7f2fe78bSCy Schubert /* Create an expanded cred by passing both an output handle and an input
113*7f2fe78bSCy Schubert * handle. */
114*7f2fe78bSCy Schubert major = gss_add_cred(&minor, cred1, GSS_C_NO_NAME, &mech_iakerb,
115*7f2fe78bSCy Schubert GSS_C_INITIATE, GSS_C_INDEFINITE, GSS_C_INDEFINITE,
116*7f2fe78bSCy Schubert &cred2, NULL, NULL, NULL);
117*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE);
118*7f2fe78bSCy Schubert
119*7f2fe78bSCy Schubert /* Verify mechanism creds in cred1 and cred2. */
120*7f2fe78bSCy Schubert major = gss_inquire_cred_by_mech(&minor, cred1, &mech_krb5, NULL, NULL,
121*7f2fe78bSCy Schubert NULL, &usage);
122*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE && usage == GSS_C_ACCEPT);
123*7f2fe78bSCy Schubert major = gss_inquire_cred_by_mech(&minor, cred1, &mech_iakerb, NULL, NULL,
124*7f2fe78bSCy Schubert NULL, &usage);
125*7f2fe78bSCy Schubert assert(major == GSS_S_NO_CRED);
126*7f2fe78bSCy Schubert major = gss_inquire_cred_by_mech(&minor, cred2, &mech_krb5, NULL, NULL,
127*7f2fe78bSCy Schubert NULL, &usage);
128*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE && usage == GSS_C_ACCEPT);
129*7f2fe78bSCy Schubert major = gss_inquire_cred_by_mech(&minor, cred2, &mech_iakerb, NULL, NULL,
130*7f2fe78bSCy Schubert NULL, &usage);
131*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE && usage == GSS_C_INITIATE);
132*7f2fe78bSCy Schubert
133*7f2fe78bSCy Schubert gss_release_cred(&minor, &cred1);
134*7f2fe78bSCy Schubert gss_release_cred(&minor, &cred2);
135*7f2fe78bSCy Schubert
136*7f2fe78bSCy Schubert return 0;
137*7f2fe78bSCy Schubert }
138