Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / tests / gssapi / t_credstore.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2011 Red Hat, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation files
7  * (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "common.h"
31
32 static void
33 usage(void)
34 {
35     fprintf(stderr,
36             "Usage: t_credstore [-sabi] principal [{key value} ...]\n");
37     exit(1);
38 }
39
40 int
41 main(int argc, char *argv[])
42 {
43     OM_uint32 minor, major;
44     gss_key_value_set_desc store;
45     gss_name_t name;
46     gss_cred_usage_t cred_usage = GSS_C_BOTH;
47     gss_OID_set mechs = GSS_C_NO_OID_SET;
48     gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
49     gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;
50     gss_buffer_desc itok, atok;
51     krb5_boolean store_creds = FALSE, replay = FALSE;
52     char opt;
53
54     /* Parse options. */
55     for (argv++; *argv != NULL && **argv == '-'; argv++) {
56         opt = (*argv)[1];
57         if (opt == 's')
58             store_creds = TRUE;
59         else if (opt == 'r')
60             replay = TRUE;
61         else if (opt == 'a')
62             cred_usage = GSS_C_ACCEPT;
63         else if (opt == 'b')
64             cred_usage = GSS_C_BOTH;
65         else if (opt == 'i')
66             cred_usage = GSS_C_INITIATE;
67         else
68             usage();
69     }
70
71     /* Get the principal name. */
72     if (*argv == NULL)
73         usage();
74     name = import_name(*argv++);
75
76     /* Put any remaining arguments into the store. */
77     store.elements = calloc(argc, sizeof(struct gss_key_value_element_struct));
78     if (!store.elements)
79         errout("OOM");
80     store.count = 0;
81     while (*argv != NULL) {
82         if (*(argv + 1) == NULL)
83             usage();
84         store.elements[store.count].key = *argv;
85         store.elements[store.count].value = *(argv + 1);
86         store.count++;
87         argv += 2;
88     }
89
90     if (store_creds) {
91         /* Acquire default creds and try to store them in the cred store. */
92         major = gss_acquire_cred(&minor, GSS_C_NO_NAME, 0, GSS_C_NO_OID_SET,
93                                  GSS_C_INITIATE, &cred, NULL, NULL);
94         check_gsserr("gss_acquire_cred", major, minor);
95
96         major = gss_store_cred_into(&minor, cred, GSS_C_INITIATE,
97                                     GSS_C_NO_OID, 1, 0, &store, NULL, NULL);
98         check_gsserr("gss_store_cred_into", major, minor);
99
100         gss_release_cred(&minor, &cred);
101     }
102
103     /* Try to acquire creds from store. */
104     major = gss_acquire_cred_from(&minor, name, 0, mechs, cred_usage,
105                                   &store, &cred, NULL, NULL);
106     check_gsserr("gss_acquire_cred_from", major, minor);
107
108     if (replay) {
109         /* Induce a replay using cred as the acceptor cred, to test the replay
110          * cache indicated by the store. */
111         major = gss_init_sec_context(&minor, GSS_C_NO_CREDENTIAL, &ictx, name,
112                                      &mech_krb5, 0, GSS_C_INDEFINITE,
113                                      GSS_C_NO_CHANNEL_BINDINGS,
114                                      GSS_C_NO_BUFFER, NULL, &itok, NULL, NULL);
115         check_gsserr("gss_init_sec_context", major, minor);
116         (void)gss_delete_sec_context(&minor, &ictx, NULL);
117
118         major = gss_accept_sec_context(&minor, &actx, cred, &itok,
119                                        GSS_C_NO_CHANNEL_BINDINGS, NULL, NULL,
120                                        &atok, NULL, NULL, NULL);
121         check_gsserr("gss_accept_sec_context(1)", major, minor);
122         (void)gss_release_buffer(&minor, &atok);
123         (void)gss_delete_sec_context(&minor, &actx, NULL);
124
125         major = gss_accept_sec_context(&minor, &actx, cred, &itok,
126                                        GSS_C_NO_CHANNEL_BINDINGS, NULL, NULL,
127                                        &atok, NULL, NULL, NULL);
128         check_gsserr("gss_accept_sec_context(2)", major, minor);
129         (void)gss_release_buffer(&minor, &itok);
130         (void)gss_release_buffer(&minor, &atok);
131         (void)gss_delete_sec_context(&minor, &actx, NULL);
132     }
133
134     gss_release_name(&minor, &name);
135     gss_release_cred(&minor, &cred);
136     free(store.elements);
137     return 0;
138 }