1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/gssapi/t_lifetime.c - display cred and context lifetimes */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2017 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 #include <stdio.h>
34*7f2fe78bSCy Schubert #include <stdlib.h>
35*7f2fe78bSCy Schubert #include <assert.h>
36*7f2fe78bSCy Schubert #include "common.h"
37*7f2fe78bSCy Schubert
38*7f2fe78bSCy Schubert /*
39*7f2fe78bSCy Schubert * Using the default credential, exercise the GSS functions which accept or
40*7f2fe78bSCy Schubert * produce lifetimes. Display the following results, one per line, as ASCII
41*7f2fe78bSCy Schubert * integers or the string "indefinite":
42*7f2fe78bSCy Schubert *
43*7f2fe78bSCy Schubert * initiator cred lifetime according to gss_acquire_cred()
44*7f2fe78bSCy Schubert * initiator cred lifetime according to gss_inquire_cred()
45*7f2fe78bSCy Schubert * acceptor cred lifetime according to gss_acquire_cred()
46*7f2fe78bSCy Schubert * acceptor cred lifetime according to gss_inquire_cred()
47*7f2fe78bSCy Schubert * initiator context lifetime according to gss_init_sec_context()
48*7f2fe78bSCy Schubert * initiator context lifetime according to gss_inquire_context()
49*7f2fe78bSCy Schubert * initiator context lifetime according to gss_context_time()
50*7f2fe78bSCy Schubert * acceptor context lifetime according to gss_init_sec_context()
51*7f2fe78bSCy Schubert * acceptor context lifetime according to gss_inquire_context()
52*7f2fe78bSCy Schubert * acceptor context lifetime according to gss_context_time()
53*7f2fe78bSCy Schubert */
54*7f2fe78bSCy Schubert
55*7f2fe78bSCy Schubert static void
display_time(OM_uint32 tval)56*7f2fe78bSCy Schubert display_time(OM_uint32 tval)
57*7f2fe78bSCy Schubert {
58*7f2fe78bSCy Schubert if (tval == GSS_C_INDEFINITE)
59*7f2fe78bSCy Schubert puts("indefinite");
60*7f2fe78bSCy Schubert else
61*7f2fe78bSCy Schubert printf("%u\n", (unsigned int)tval);
62*7f2fe78bSCy Schubert }
63*7f2fe78bSCy Schubert
64*7f2fe78bSCy Schubert int
main(int argc,char * argv[])65*7f2fe78bSCy Schubert main(int argc, char *argv[])
66*7f2fe78bSCy Schubert {
67*7f2fe78bSCy Schubert OM_uint32 minor, major;
68*7f2fe78bSCy Schubert gss_cred_id_t icred, acred;
69*7f2fe78bSCy Schubert gss_name_t tname;
70*7f2fe78bSCy Schubert gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;
71*7f2fe78bSCy Schubert gss_buffer_desc itok = GSS_C_EMPTY_BUFFER, atok = GSS_C_EMPTY_BUFFER;
72*7f2fe78bSCy Schubert OM_uint32 time_req = GSS_C_INDEFINITE, time_rec;
73*7f2fe78bSCy Schubert
74*7f2fe78bSCy Schubert if (argc < 2 || argc > 3) {
75*7f2fe78bSCy Schubert fprintf(stderr, "Usage: %s targetname [time_req]\n", argv[0]);
76*7f2fe78bSCy Schubert return 1;
77*7f2fe78bSCy Schubert }
78*7f2fe78bSCy Schubert tname = import_name(argv[1]);
79*7f2fe78bSCy Schubert if (argc >= 3)
80*7f2fe78bSCy Schubert time_req = atoll(argv[2]);
81*7f2fe78bSCy Schubert
82*7f2fe78bSCy Schubert /* Get initiator cred and display its lifetime according to
83*7f2fe78bSCy Schubert * gss_acquire_cred and gss_inquire_cred. */
84*7f2fe78bSCy Schubert major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,
85*7f2fe78bSCy Schubert GSS_C_INITIATE, &icred, NULL, &time_rec);
86*7f2fe78bSCy Schubert check_gsserr("gss_acquire_cred(initiate)", major, minor);
87*7f2fe78bSCy Schubert display_time(time_rec);
88*7f2fe78bSCy Schubert major = gss_inquire_cred(&minor, icred, NULL, &time_rec, NULL, NULL);
89*7f2fe78bSCy Schubert check_gsserr("gss_inquire_cred(initiate)", major, minor);
90*7f2fe78bSCy Schubert display_time(time_rec);
91*7f2fe78bSCy Schubert
92*7f2fe78bSCy Schubert /* Get acceptor cred and display its lifetime according to gss_acquire_cred
93*7f2fe78bSCy Schubert * and gss_inquire_cred. */
94*7f2fe78bSCy Schubert major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,
95*7f2fe78bSCy Schubert GSS_C_ACCEPT, &acred, NULL, &time_rec);
96*7f2fe78bSCy Schubert check_gsserr("gss_acquire_cred(accept)", major, minor);
97*7f2fe78bSCy Schubert display_time(time_rec);
98*7f2fe78bSCy Schubert major = gss_inquire_cred(&minor, acred, NULL, &time_rec, NULL, NULL);
99*7f2fe78bSCy Schubert check_gsserr("gss_inquire_cred(accept)", major, minor);
100*7f2fe78bSCy Schubert display_time(time_rec);
101*7f2fe78bSCy Schubert
102*7f2fe78bSCy Schubert /* Make an initiator context and display its lifetime according to
103*7f2fe78bSCy Schubert * gss_init_sec_context, gss_inquire_context, and gss_context_time. */
104*7f2fe78bSCy Schubert major = gss_init_sec_context(&minor, icred, &ictx, tname, &mech_krb5, 0,
105*7f2fe78bSCy Schubert time_req, GSS_C_NO_CHANNEL_BINDINGS, &atok,
106*7f2fe78bSCy Schubert NULL, &itok, NULL, &time_rec);
107*7f2fe78bSCy Schubert check_gsserr("gss_init_sec_context", major, minor);
108*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE);
109*7f2fe78bSCy Schubert display_time(time_rec);
110*7f2fe78bSCy Schubert major = gss_inquire_context(&minor, ictx, NULL, NULL, &time_rec, NULL,
111*7f2fe78bSCy Schubert NULL, NULL, NULL);
112*7f2fe78bSCy Schubert check_gsserr("gss_inquire_context(initiate)", major, minor);
113*7f2fe78bSCy Schubert display_time(time_rec);
114*7f2fe78bSCy Schubert major = gss_context_time(&minor, ictx, &time_rec);
115*7f2fe78bSCy Schubert check_gsserr("gss_context_time(initiate)", major, minor);
116*7f2fe78bSCy Schubert display_time(time_rec);
117*7f2fe78bSCy Schubert
118*7f2fe78bSCy Schubert major = gss_accept_sec_context(&minor, &actx, acred, &itok,
119*7f2fe78bSCy Schubert GSS_C_NO_CHANNEL_BINDINGS, NULL,
120*7f2fe78bSCy Schubert NULL, &atok, NULL, &time_rec, NULL);
121*7f2fe78bSCy Schubert check_gsserr("gss_accept_sec_context", major, minor);
122*7f2fe78bSCy Schubert assert(major == GSS_S_COMPLETE);
123*7f2fe78bSCy Schubert display_time(time_rec);
124*7f2fe78bSCy Schubert major = gss_inquire_context(&minor, actx, NULL, NULL, &time_rec, NULL,
125*7f2fe78bSCy Schubert NULL, NULL, NULL);
126*7f2fe78bSCy Schubert check_gsserr("gss_inquire_context(accept)", major, minor);
127*7f2fe78bSCy Schubert display_time(time_rec);
128*7f2fe78bSCy Schubert major = gss_context_time(&minor, actx, &time_rec);
129*7f2fe78bSCy Schubert check_gsserr("gss_context_time(accept)", major, minor);
130*7f2fe78bSCy Schubert display_time(time_rec);
131*7f2fe78bSCy Schubert
132*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &itok);
133*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &atok);
134*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &tname);
135*7f2fe78bSCy Schubert (void)gss_release_cred(&minor, &icred);
136*7f2fe78bSCy Schubert (void)gss_release_cred(&minor, &acred);
137*7f2fe78bSCy Schubert (void)gss_delete_sec_context(&minor, &ictx, NULL);
138*7f2fe78bSCy Schubert (void)gss_delete_sec_context(&minor, &actx, NULL);
139*7f2fe78bSCy Schubert return 0;
140*7f2fe78bSCy Schubert }
141