xref: /freebsd/crypto/krb5/src/tests/gssapi/t_lifetime.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/gssapi/t_lifetime.c - display cred and context lifetimes */
3 /*
4  * Copyright (C) 2017 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <assert.h>
36 #include "common.h"
37 
38 /*
39  * Using the default credential, exercise the GSS functions which accept or
40  * produce lifetimes.  Display the following results, one per line, as ASCII
41  * integers or the string "indefinite":
42  *
43  *   initiator cred lifetime according to gss_acquire_cred()
44  *   initiator cred lifetime according to gss_inquire_cred()
45  *   acceptor cred lifetime according to gss_acquire_cred()
46  *   acceptor cred lifetime according to gss_inquire_cred()
47  *   initiator context lifetime according to gss_init_sec_context()
48  *   initiator context lifetime according to gss_inquire_context()
49  *   initiator context lifetime according to gss_context_time()
50  *   acceptor context lifetime according to gss_init_sec_context()
51  *   acceptor context lifetime according to gss_inquire_context()
52  *   acceptor context lifetime according to gss_context_time()
53  */
54 
55 static void
display_time(OM_uint32 tval)56 display_time(OM_uint32 tval)
57 {
58     if (tval == GSS_C_INDEFINITE)
59         puts("indefinite");
60     else
61         printf("%u\n", (unsigned int)tval);
62 }
63 
64 int
main(int argc,char * argv[])65 main(int argc, char *argv[])
66 {
67     OM_uint32 minor, major;
68     gss_cred_id_t icred, acred;
69     gss_name_t tname;
70     gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;
71     gss_buffer_desc itok = GSS_C_EMPTY_BUFFER, atok = GSS_C_EMPTY_BUFFER;
72     OM_uint32 time_req = GSS_C_INDEFINITE, time_rec;
73 
74     if (argc < 2 || argc > 3) {
75         fprintf(stderr, "Usage: %s targetname [time_req]\n", argv[0]);
76         return 1;
77     }
78     tname = import_name(argv[1]);
79     if (argc >= 3)
80         time_req = atoll(argv[2]);
81 
82     /* Get initiator cred and display its lifetime according to
83      * gss_acquire_cred and gss_inquire_cred. */
84     major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,
85                              GSS_C_INITIATE, &icred, NULL, &time_rec);
86     check_gsserr("gss_acquire_cred(initiate)", major, minor);
87     display_time(time_rec);
88     major = gss_inquire_cred(&minor, icred, NULL, &time_rec, NULL, NULL);
89     check_gsserr("gss_inquire_cred(initiate)", major, minor);
90     display_time(time_rec);
91 
92     /* Get acceptor cred and display its lifetime according to gss_acquire_cred
93      * and gss_inquire_cred. */
94     major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,
95                              GSS_C_ACCEPT, &acred, NULL, &time_rec);
96     check_gsserr("gss_acquire_cred(accept)", major, minor);
97     display_time(time_rec);
98     major = gss_inquire_cred(&minor, acred, NULL, &time_rec, NULL, NULL);
99     check_gsserr("gss_inquire_cred(accept)", major, minor);
100     display_time(time_rec);
101 
102     /* Make an initiator context and display its lifetime according to
103      * gss_init_sec_context, gss_inquire_context, and gss_context_time. */
104     major = gss_init_sec_context(&minor, icred, &ictx, tname, &mech_krb5, 0,
105                                  time_req, GSS_C_NO_CHANNEL_BINDINGS, &atok,
106                                  NULL, &itok, NULL, &time_rec);
107     check_gsserr("gss_init_sec_context", major, minor);
108     assert(major == GSS_S_COMPLETE);
109     display_time(time_rec);
110     major = gss_inquire_context(&minor, ictx, NULL, NULL, &time_rec, NULL,
111                                 NULL, NULL, NULL);
112     check_gsserr("gss_inquire_context(initiate)", major, minor);
113     display_time(time_rec);
114     major = gss_context_time(&minor, ictx, &time_rec);
115     check_gsserr("gss_context_time(initiate)", major, minor);
116     display_time(time_rec);
117 
118     major = gss_accept_sec_context(&minor, &actx, acred, &itok,
119                                    GSS_C_NO_CHANNEL_BINDINGS, NULL,
120                                    NULL, &atok, NULL, &time_rec, NULL);
121     check_gsserr("gss_accept_sec_context", major, minor);
122     assert(major == GSS_S_COMPLETE);
123     display_time(time_rec);
124     major = gss_inquire_context(&minor, actx, NULL, NULL, &time_rec, NULL,
125                                 NULL, NULL, NULL);
126     check_gsserr("gss_inquire_context(accept)", major, minor);
127     display_time(time_rec);
128     major = gss_context_time(&minor, actx, &time_rec);
129     check_gsserr("gss_context_time(accept)", major, minor);
130     display_time(time_rec);
131 
132     (void)gss_release_buffer(&minor, &itok);
133     (void)gss_release_buffer(&minor, &atok);
134     (void)gss_release_name(&minor, &tname);
135     (void)gss_release_cred(&minor, &icred);
136     (void)gss_release_cred(&minor, &acred);
137     (void)gss_delete_sec_context(&minor, &ictx, NULL);
138     (void)gss_delete_sec_context(&minor, &actx, NULL);
139     return 0;
140 }
141