1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert * Copyright 2009 by the Massachusetts Institute of Technology.
4*7f2fe78bSCy Schubert * All Rights Reserved.
5*7f2fe78bSCy Schubert *
6*7f2fe78bSCy Schubert * Export of this software from the United States of America may
7*7f2fe78bSCy Schubert * require a specific license from the United States Government.
8*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
9*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
10*7f2fe78bSCy Schubert *
11*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
13*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
14*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
15*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
16*7f2fe78bSCy Schubert * the name of M.I.T. not be used in advertising or publicity pertaining
17*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
18*7f2fe78bSCy Schubert * permission. Furthermore if you modify this software you must label
19*7f2fe78bSCy Schubert * your software as modified software and not distribute it in such a
20*7f2fe78bSCy Schubert * fashion that it might be confused with the original M.I.T. software.
21*7f2fe78bSCy Schubert * M.I.T. makes no representations about the suitability of
22*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
23*7f2fe78bSCy Schubert * or implied warranty.
24*7f2fe78bSCy Schubert */
25*7f2fe78bSCy Schubert
26*7f2fe78bSCy Schubert #include <stdio.h>
27*7f2fe78bSCy Schubert #include <stdlib.h>
28*7f2fe78bSCy Schubert #include <string.h>
29*7f2fe78bSCy Schubert
30*7f2fe78bSCy Schubert #include "common.h"
31*7f2fe78bSCy Schubert
32*7f2fe78bSCy Schubert static int use_spnego = 0;
33*7f2fe78bSCy Schubert
34*7f2fe78bSCy Schubert static void
display_name(const char * tag,gss_name_t name)35*7f2fe78bSCy Schubert display_name(const char *tag, gss_name_t name)
36*7f2fe78bSCy Schubert {
37*7f2fe78bSCy Schubert OM_uint32 major, minor;
38*7f2fe78bSCy Schubert gss_buffer_desc buf;
39*7f2fe78bSCy Schubert
40*7f2fe78bSCy Schubert major = gss_display_name(&minor, name, &buf, NULL);
41*7f2fe78bSCy Schubert check_gsserr("gss_display_name", major, minor);
42*7f2fe78bSCy Schubert
43*7f2fe78bSCy Schubert printf("%s:\t%.*s\n", tag, (int)buf.length, (char *)buf.value);
44*7f2fe78bSCy Schubert
45*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &buf);
46*7f2fe78bSCy Schubert }
47*7f2fe78bSCy Schubert
48*7f2fe78bSCy Schubert static void
test_export_import_name(gss_name_t name)49*7f2fe78bSCy Schubert test_export_import_name(gss_name_t name)
50*7f2fe78bSCy Schubert {
51*7f2fe78bSCy Schubert OM_uint32 major, minor;
52*7f2fe78bSCy Schubert gss_buffer_desc exported_name = GSS_C_EMPTY_BUFFER;
53*7f2fe78bSCy Schubert gss_name_t imported_name = GSS_C_NO_NAME;
54*7f2fe78bSCy Schubert gss_name_t imported_name_comp = GSS_C_NO_NAME;
55*7f2fe78bSCy Schubert unsigned int i;
56*7f2fe78bSCy Schubert
57*7f2fe78bSCy Schubert major = gss_export_name_composite(&minor, name, &exported_name);
58*7f2fe78bSCy Schubert check_gsserr("gss_export_name_composite", major, minor);
59*7f2fe78bSCy Schubert
60*7f2fe78bSCy Schubert printf("Exported name:\n");
61*7f2fe78bSCy Schubert for (i = 0; i < exported_name.length; i++) {
62*7f2fe78bSCy Schubert if ((i % 32) == 0)
63*7f2fe78bSCy Schubert printf("\n");
64*7f2fe78bSCy Schubert printf("%02x", ((char *)exported_name.value)[i] & 0xFF);
65*7f2fe78bSCy Schubert }
66*7f2fe78bSCy Schubert printf("\n");
67*7f2fe78bSCy Schubert
68*7f2fe78bSCy Schubert major = gss_import_name(&minor, &exported_name, GSS_C_NT_EXPORT_NAME,
69*7f2fe78bSCy Schubert &imported_name);
70*7f2fe78bSCy Schubert check_gsserr("gss_import_name", major, minor);
71*7f2fe78bSCy Schubert
72*7f2fe78bSCy Schubert major = gss_import_name(&minor, &exported_name, GSS_C_NT_COMPOSITE_EXPORT,
73*7f2fe78bSCy Schubert &imported_name_comp);
74*7f2fe78bSCy Schubert check_gsserr("gss_import_name", major, minor);
75*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &exported_name);
76*7f2fe78bSCy Schubert
77*7f2fe78bSCy Schubert printf("\n");
78*7f2fe78bSCy Schubert display_canon_name("Re-imported name", imported_name, &mech_krb5);
79*7f2fe78bSCy Schubert printf("Re-imported attributes:\n\n");
80*7f2fe78bSCy Schubert enumerate_attributes(imported_name, 0);
81*7f2fe78bSCy Schubert
82*7f2fe78bSCy Schubert display_name("Re-imported (as composite) name", imported_name_comp);
83*7f2fe78bSCy Schubert printf("Re-imported (as composite) attributes:\n\n");
84*7f2fe78bSCy Schubert enumerate_attributes(imported_name_comp, 0);
85*7f2fe78bSCy Schubert
86*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &imported_name);
87*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &imported_name_comp);
88*7f2fe78bSCy Schubert }
89*7f2fe78bSCy Schubert
90*7f2fe78bSCy Schubert static void
test_greet_authz_data(gss_name_t name)91*7f2fe78bSCy Schubert test_greet_authz_data(gss_name_t name)
92*7f2fe78bSCy Schubert {
93*7f2fe78bSCy Schubert OM_uint32 major, minor;
94*7f2fe78bSCy Schubert gss_buffer_desc attr;
95*7f2fe78bSCy Schubert gss_buffer_desc value;
96*7f2fe78bSCy Schubert
97*7f2fe78bSCy Schubert attr.value = "urn:greet:greeting";
98*7f2fe78bSCy Schubert attr.length = strlen((char *)attr.value);
99*7f2fe78bSCy Schubert
100*7f2fe78bSCy Schubert major = gss_delete_name_attribute(&minor, name, &attr);
101*7f2fe78bSCy Schubert if (major == GSS_S_UNAVAILABLE) {
102*7f2fe78bSCy Schubert fprintf(stderr, "Warning: greet_client plugin not installed\n");
103*7f2fe78bSCy Schubert exit(1);
104*7f2fe78bSCy Schubert }
105*7f2fe78bSCy Schubert check_gsserr("gss_delete_name_attribute", major, minor);
106*7f2fe78bSCy Schubert
107*7f2fe78bSCy Schubert value.value = "Hello, acceptor world!";
108*7f2fe78bSCy Schubert value.length = strlen((char *)value.value);
109*7f2fe78bSCy Schubert major = gss_set_name_attribute(&minor, name, 1, &attr, &value);
110*7f2fe78bSCy Schubert if (major == GSS_S_UNAVAILABLE)
111*7f2fe78bSCy Schubert return;
112*7f2fe78bSCy Schubert check_gsserr("gss_set_name_attribute", major, minor);
113*7f2fe78bSCy Schubert }
114*7f2fe78bSCy Schubert
115*7f2fe78bSCy Schubert static void
test_map_name_to_any(gss_name_t name)116*7f2fe78bSCy Schubert test_map_name_to_any(gss_name_t name)
117*7f2fe78bSCy Schubert {
118*7f2fe78bSCy Schubert OM_uint32 major, minor;
119*7f2fe78bSCy Schubert gss_buffer_desc type_id;
120*7f2fe78bSCy Schubert krb5_pac pac;
121*7f2fe78bSCy Schubert krb5_context context = NULL;
122*7f2fe78bSCy Schubert krb5_error_code ret;
123*7f2fe78bSCy Schubert size_t len, i;
124*7f2fe78bSCy Schubert krb5_ui_4 *types;
125*7f2fe78bSCy Schubert
126*7f2fe78bSCy Schubert type_id.value = "mspac";
127*7f2fe78bSCy Schubert type_id.length = strlen((char *)type_id.value);
128*7f2fe78bSCy Schubert
129*7f2fe78bSCy Schubert major = gss_map_name_to_any(&minor, name, 1, &type_id, (gss_any_t *)&pac);
130*7f2fe78bSCy Schubert if (major == GSS_S_UNAVAILABLE)
131*7f2fe78bSCy Schubert return;
132*7f2fe78bSCy Schubert check_gsserr("gss_map_name_to_any", major, minor);
133*7f2fe78bSCy Schubert
134*7f2fe78bSCy Schubert ret = krb5_init_context(&context);
135*7f2fe78bSCy Schubert check_k5err(context, "krb5_init_context", ret);
136*7f2fe78bSCy Schubert
137*7f2fe78bSCy Schubert if (krb5_pac_get_types(context, pac, &len, &types) == 0) {
138*7f2fe78bSCy Schubert printf("PAC buffer types:");
139*7f2fe78bSCy Schubert for (i = 0; i < len; i++)
140*7f2fe78bSCy Schubert printf(" %d", types[i]);
141*7f2fe78bSCy Schubert printf("\n");
142*7f2fe78bSCy Schubert free(types);
143*7f2fe78bSCy Schubert }
144*7f2fe78bSCy Schubert
145*7f2fe78bSCy Schubert (void)gss_release_any_name_mapping(&minor, name, &type_id,
146*7f2fe78bSCy Schubert (gss_any_t *)&pac);
147*7f2fe78bSCy Schubert }
148*7f2fe78bSCy Schubert
149*7f2fe78bSCy Schubert static void
init_accept_sec_context(gss_cred_id_t verifier_cred_handle)150*7f2fe78bSCy Schubert init_accept_sec_context(gss_cred_id_t verifier_cred_handle)
151*7f2fe78bSCy Schubert {
152*7f2fe78bSCy Schubert OM_uint32 major, minor, flags;
153*7f2fe78bSCy Schubert gss_name_t source_name = GSS_C_NO_NAME, target_name = GSS_C_NO_NAME;
154*7f2fe78bSCy Schubert gss_ctx_id_t initiator_context, acceptor_context;
155*7f2fe78bSCy Schubert gss_OID mech = use_spnego ? &mech_spnego : &mech_krb5;
156*7f2fe78bSCy Schubert
157*7f2fe78bSCy Schubert major = gss_inquire_cred(&minor, verifier_cred_handle, &target_name, NULL,
158*7f2fe78bSCy Schubert NULL, NULL);
159*7f2fe78bSCy Schubert check_gsserr("gss_inquire_cred", major, minor);
160*7f2fe78bSCy Schubert
161*7f2fe78bSCy Schubert display_canon_name("Target name", target_name, &mech_krb5);
162*7f2fe78bSCy Schubert
163*7f2fe78bSCy Schubert flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
164*7f2fe78bSCy Schubert establish_contexts(mech, verifier_cred_handle, verifier_cred_handle,
165*7f2fe78bSCy Schubert target_name, flags, &initiator_context,
166*7f2fe78bSCy Schubert &acceptor_context, &source_name, NULL, NULL);
167*7f2fe78bSCy Schubert
168*7f2fe78bSCy Schubert display_canon_name("Source name", source_name, &mech_krb5);
169*7f2fe78bSCy Schubert enumerate_attributes(source_name, 1);
170*7f2fe78bSCy Schubert test_export_import_name(source_name);
171*7f2fe78bSCy Schubert test_map_name_to_any(source_name);
172*7f2fe78bSCy Schubert
173*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &source_name);
174*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &target_name);
175*7f2fe78bSCy Schubert (void)gss_delete_sec_context(&minor, &initiator_context, NULL);
176*7f2fe78bSCy Schubert (void)gss_delete_sec_context(&minor, &acceptor_context, NULL);
177*7f2fe78bSCy Schubert }
178*7f2fe78bSCy Schubert
179*7f2fe78bSCy Schubert int
main(int argc,char * argv[])180*7f2fe78bSCy Schubert main(int argc, char *argv[])
181*7f2fe78bSCy Schubert {
182*7f2fe78bSCy Schubert OM_uint32 minor, major;
183*7f2fe78bSCy Schubert gss_cred_id_t cred_handle = GSS_C_NO_CREDENTIAL;
184*7f2fe78bSCy Schubert gss_OID_set mechs, actual_mechs = GSS_C_NO_OID_SET;
185*7f2fe78bSCy Schubert gss_name_t tmp_name, name;
186*7f2fe78bSCy Schubert
187*7f2fe78bSCy Schubert if (argc > 1 && strcmp(argv[1], "--spnego") == 0) {
188*7f2fe78bSCy Schubert use_spnego++;
189*7f2fe78bSCy Schubert argc--;
190*7f2fe78bSCy Schubert argv++;
191*7f2fe78bSCy Schubert }
192*7f2fe78bSCy Schubert
193*7f2fe78bSCy Schubert if (argc < 2) {
194*7f2fe78bSCy Schubert fprintf(stderr, "Usage: %s [--spnego] principal [keytab]\n", argv[0]);
195*7f2fe78bSCy Schubert exit(1);
196*7f2fe78bSCy Schubert }
197*7f2fe78bSCy Schubert
198*7f2fe78bSCy Schubert tmp_name = import_name(argv[1]);
199*7f2fe78bSCy Schubert major = gss_canonicalize_name(&minor, tmp_name, &mech_krb5, &name);
200*7f2fe78bSCy Schubert check_gsserr("gss_canonicalze_name", major, minor);
201*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &tmp_name);
202*7f2fe78bSCy Schubert
203*7f2fe78bSCy Schubert test_greet_authz_data(name);
204*7f2fe78bSCy Schubert
205*7f2fe78bSCy Schubert if (argc >= 3) {
206*7f2fe78bSCy Schubert major = krb5_gss_register_acceptor_identity(argv[2]);
207*7f2fe78bSCy Schubert check_gsserr("krb5_gss_register_acceptor_identity", major, minor);
208*7f2fe78bSCy Schubert }
209*7f2fe78bSCy Schubert
210*7f2fe78bSCy Schubert mechs = use_spnego ? &mechset_spnego : &mechset_krb5;
211*7f2fe78bSCy Schubert
212*7f2fe78bSCy Schubert /* get default cred */
213*7f2fe78bSCy Schubert major = gss_acquire_cred(&minor, name, GSS_C_INDEFINITE, mechs, GSS_C_BOTH,
214*7f2fe78bSCy Schubert &cred_handle, &actual_mechs, NULL);
215*7f2fe78bSCy Schubert check_gsserr("gss_acquire_cred", major, minor);
216*7f2fe78bSCy Schubert
217*7f2fe78bSCy Schubert (void)gss_release_oid_set(&minor, &actual_mechs);
218*7f2fe78bSCy Schubert
219*7f2fe78bSCy Schubert init_accept_sec_context(cred_handle);
220*7f2fe78bSCy Schubert
221*7f2fe78bSCy Schubert printf("\n");
222*7f2fe78bSCy Schubert
223*7f2fe78bSCy Schubert (void)gss_release_cred(&minor, &cred_handle);
224*7f2fe78bSCy Schubert (void)gss_release_oid_set(&minor, &actual_mechs);
225*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &name);
226*7f2fe78bSCy Schubert return 0;
227*7f2fe78bSCy Schubert }
228