xref: /freebsd/crypto/krb5/src/tests/gssapi/t_gssexts.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
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 /*
33*7f2fe78bSCy Schubert  * Test program for protocol transition (S4U2Self) and constrained delegation
34*7f2fe78bSCy Schubert  * (S4U2Proxy)
35*7f2fe78bSCy Schubert  *
36*7f2fe78bSCy Schubert  * Note: because of name canonicalization, the following tips may help
37*7f2fe78bSCy Schubert  * when configuring with Active Directory:
38*7f2fe78bSCy Schubert  *
39*7f2fe78bSCy Schubert  * - Create a computer account FOO$
40*7f2fe78bSCy Schubert  * - Set the UPN to host/foo.domain (no suffix); this is necessary to
41*7f2fe78bSCy Schubert  *   be able to send an AS-REQ as this principal, otherwise you would
42*7f2fe78bSCy Schubert  *   need to use the canonical name (FOO$), which will cause principal
43*7f2fe78bSCy Schubert  *   comparison errors in gss_accept_sec_context().
44*7f2fe78bSCy Schubert  * - Add a SPN of host/foo.domain
45*7f2fe78bSCy Schubert  * - Configure the computer account to support constrained delegation with
46*7f2fe78bSCy Schubert  *   protocol transition (Trust this computer for delegation to specified
47*7f2fe78bSCy Schubert  *   services only / Use any authentication protocol)
48*7f2fe78bSCy Schubert  * - Add host/foo.domain to the keytab (possibly easiest to do this
49*7f2fe78bSCy Schubert  *   with ktadd)
50*7f2fe78bSCy Schubert  *
51*7f2fe78bSCy Schubert  * For S4U2Proxy to work the TGT must be forwardable too.
52*7f2fe78bSCy Schubert  *
53*7f2fe78bSCy Schubert  * Usage eg:
54*7f2fe78bSCy Schubert  *
55*7f2fe78bSCy Schubert  * kinit -k -t test.keytab -f 'host/test.win.mit.edu@WIN.MIT.EDU'
56*7f2fe78bSCy Schubert  * ./t_s4u p:delegtest@WIN.MIT.EDU p:HOST/WIN-EQ7E4AA2WR8.win.mit.edu@WIN.MIT.EDU test.keytab
57*7f2fe78bSCy Schubert  */
58*7f2fe78bSCy Schubert 
59*7f2fe78bSCy Schubert static int use_spnego = 0;
60*7f2fe78bSCy Schubert 
61*7f2fe78bSCy Schubert static void
test_prf(gss_ctx_id_t initiatorContext,gss_ctx_id_t acceptorContext,int flags)62*7f2fe78bSCy Schubert test_prf(gss_ctx_id_t initiatorContext, gss_ctx_id_t acceptorContext,
63*7f2fe78bSCy Schubert          int flags)
64*7f2fe78bSCy Schubert {
65*7f2fe78bSCy Schubert     gss_buffer_desc constant;
66*7f2fe78bSCy Schubert     OM_uint32 major, minor;
67*7f2fe78bSCy Schubert     unsigned int i;
68*7f2fe78bSCy Schubert     gss_buffer_desc initiatorPrf;
69*7f2fe78bSCy Schubert     gss_buffer_desc acceptorPrf;
70*7f2fe78bSCy Schubert 
71*7f2fe78bSCy Schubert     constant.value = "gss prf test";
72*7f2fe78bSCy Schubert     constant.length = strlen((char *)constant.value);
73*7f2fe78bSCy Schubert 
74*7f2fe78bSCy Schubert     initiatorPrf.value = NULL;
75*7f2fe78bSCy Schubert     acceptorPrf.value = NULL;
76*7f2fe78bSCy Schubert 
77*7f2fe78bSCy Schubert     major = gss_pseudo_random(&minor, initiatorContext, flags, &constant, 19,
78*7f2fe78bSCy Schubert                               &initiatorPrf);
79*7f2fe78bSCy Schubert     check_gsserr("gss_pseudo_random", major, minor);
80*7f2fe78bSCy Schubert 
81*7f2fe78bSCy Schubert     printf("%s\n", flags == GSS_C_PRF_KEY_FULL ?
82*7f2fe78bSCy Schubert            "PRF_KEY_FULL" : "PRF_KEY_PARTIAL");
83*7f2fe78bSCy Schubert 
84*7f2fe78bSCy Schubert     printf("Initiator PRF: ");
85*7f2fe78bSCy Schubert     for (i = 0; i < initiatorPrf.length; i++)
86*7f2fe78bSCy Schubert         printf("%02x ", ((char *)initiatorPrf.value)[i] & 0xFF);
87*7f2fe78bSCy Schubert     printf("\n");
88*7f2fe78bSCy Schubert 
89*7f2fe78bSCy Schubert     major = gss_pseudo_random(&minor, acceptorContext, flags, &constant, 19,
90*7f2fe78bSCy Schubert                               &acceptorPrf);
91*7f2fe78bSCy Schubert     check_gsserr("gss_pseudo_random", major, minor);
92*7f2fe78bSCy Schubert 
93*7f2fe78bSCy Schubert     printf("Acceptor  PRF: ");
94*7f2fe78bSCy Schubert     for (i = 0; i < acceptorPrf.length; i++)
95*7f2fe78bSCy Schubert         printf("%02x ", ((char *)acceptorPrf.value)[i] & 0xFF);
96*7f2fe78bSCy Schubert     printf("\n");
97*7f2fe78bSCy Schubert 
98*7f2fe78bSCy Schubert     if (acceptorPrf.length != initiatorPrf.length ||
99*7f2fe78bSCy Schubert         memcmp(acceptorPrf.value, initiatorPrf.value, initiatorPrf.length)) {
100*7f2fe78bSCy Schubert         fprintf(stderr, "Initiator and acceptor PRF output does not match\n");
101*7f2fe78bSCy Schubert         exit(1);
102*7f2fe78bSCy Schubert     }
103*7f2fe78bSCy Schubert 
104*7f2fe78bSCy Schubert     (void)gss_release_buffer(&minor, &initiatorPrf);
105*7f2fe78bSCy Schubert     (void)gss_release_buffer(&minor, &acceptorPrf);
106*7f2fe78bSCy Schubert }
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert static void
init_accept_sec_context(gss_cred_id_t claimant_cred_handle,gss_cred_id_t verifier_cred_handle,gss_cred_id_t * deleg_cred_handle)109*7f2fe78bSCy Schubert init_accept_sec_context(gss_cred_id_t claimant_cred_handle,
110*7f2fe78bSCy Schubert                         gss_cred_id_t verifier_cred_handle,
111*7f2fe78bSCy Schubert                         gss_cred_id_t *deleg_cred_handle)
112*7f2fe78bSCy Schubert {
113*7f2fe78bSCy Schubert     OM_uint32 major, minor, flags;
114*7f2fe78bSCy Schubert     gss_name_t source_name = GSS_C_NO_NAME, target_name = GSS_C_NO_NAME;
115*7f2fe78bSCy Schubert     gss_ctx_id_t initiator_context, acceptor_context;
116*7f2fe78bSCy Schubert     gss_OID mech;
117*7f2fe78bSCy Schubert 
118*7f2fe78bSCy Schubert     *deleg_cred_handle = GSS_C_NO_CREDENTIAL;
119*7f2fe78bSCy Schubert 
120*7f2fe78bSCy Schubert     major = gss_inquire_cred(&minor, verifier_cred_handle, &target_name, NULL,
121*7f2fe78bSCy Schubert                              NULL, NULL);
122*7f2fe78bSCy Schubert     check_gsserr("gss_inquire_cred", major, minor);
123*7f2fe78bSCy Schubert     display_canon_name("Target name", target_name, &mech_krb5);
124*7f2fe78bSCy Schubert 
125*7f2fe78bSCy Schubert     mech = use_spnego ? &mech_spnego : &mech_krb5;
126*7f2fe78bSCy Schubert     display_oid("Target mech", mech);
127*7f2fe78bSCy Schubert 
128*7f2fe78bSCy Schubert     flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
129*7f2fe78bSCy Schubert     establish_contexts(mech, claimant_cred_handle, verifier_cred_handle,
130*7f2fe78bSCy Schubert                        target_name, flags, &initiator_context,
131*7f2fe78bSCy Schubert                        &acceptor_context, &source_name, NULL,
132*7f2fe78bSCy Schubert                        deleg_cred_handle);
133*7f2fe78bSCy Schubert 
134*7f2fe78bSCy Schubert     test_prf(initiator_context, acceptor_context, GSS_C_PRF_KEY_FULL);
135*7f2fe78bSCy Schubert     test_prf(initiator_context, acceptor_context, GSS_C_PRF_KEY_PARTIAL);
136*7f2fe78bSCy Schubert 
137*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &source_name);
138*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &acceptor_context, NULL);
139*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &initiator_context, NULL);
140*7f2fe78bSCy Schubert }
141*7f2fe78bSCy Schubert 
142*7f2fe78bSCy Schubert static void
get_default_cred(const char * keytab_name,gss_OID_set mechs,gss_cred_id_t * impersonator_cred_handle)143*7f2fe78bSCy Schubert get_default_cred(const char *keytab_name, gss_OID_set mechs,
144*7f2fe78bSCy Schubert                  gss_cred_id_t *impersonator_cred_handle)
145*7f2fe78bSCy Schubert {
146*7f2fe78bSCy Schubert     OM_uint32 major = GSS_S_FAILURE, minor;
147*7f2fe78bSCy Schubert     krb5_error_code ret;
148*7f2fe78bSCy Schubert     krb5_context context = NULL;
149*7f2fe78bSCy Schubert     krb5_keytab keytab = NULL;
150*7f2fe78bSCy Schubert     krb5_principal keytab_principal = NULL;
151*7f2fe78bSCy Schubert     krb5_ccache ccache = NULL;
152*7f2fe78bSCy Schubert 
153*7f2fe78bSCy Schubert     if (keytab_name != NULL) {
154*7f2fe78bSCy Schubert         ret = krb5_init_context(&context);
155*7f2fe78bSCy Schubert         check_k5err(context, "krb5_init_context", ret);
156*7f2fe78bSCy Schubert 
157*7f2fe78bSCy Schubert         ret = krb5_kt_resolve(context, keytab_name, &keytab);
158*7f2fe78bSCy Schubert         check_k5err(context, "krb5_kt_resolve", ret);
159*7f2fe78bSCy Schubert 
160*7f2fe78bSCy Schubert         ret = krb5_cc_default(context, &ccache);
161*7f2fe78bSCy Schubert         check_k5err(context, "krb5_cc_default", ret);
162*7f2fe78bSCy Schubert 
163*7f2fe78bSCy Schubert         ret = krb5_cc_get_principal(context, ccache, &keytab_principal);
164*7f2fe78bSCy Schubert         check_k5err(context, "krb5_cc_get_principal", ret);
165*7f2fe78bSCy Schubert 
166*7f2fe78bSCy Schubert         major = gss_krb5_import_cred(&minor, ccache, keytab_principal, keytab,
167*7f2fe78bSCy Schubert                                      impersonator_cred_handle);
168*7f2fe78bSCy Schubert         check_gsserr("gss_krb5_import_cred", major, minor);
169*7f2fe78bSCy Schubert 
170*7f2fe78bSCy Schubert         krb5_free_principal(context, keytab_principal);
171*7f2fe78bSCy Schubert         krb5_cc_close(context, ccache);
172*7f2fe78bSCy Schubert         krb5_kt_close(context, keytab);
173*7f2fe78bSCy Schubert         krb5_free_context(context);
174*7f2fe78bSCy Schubert     } else {
175*7f2fe78bSCy Schubert         major = gss_acquire_cred(&minor, GSS_C_NO_NAME, GSS_C_INDEFINITE,
176*7f2fe78bSCy Schubert                                  mechs, GSS_C_BOTH, impersonator_cred_handle,
177*7f2fe78bSCy Schubert                                  NULL, NULL);
178*7f2fe78bSCy Schubert         check_gsserr("gss_acquire_cred", major, minor);
179*7f2fe78bSCy Schubert     }
180*7f2fe78bSCy Schubert }
181*7f2fe78bSCy Schubert 
182*7f2fe78bSCy Schubert int
main(int argc,char * argv[])183*7f2fe78bSCy Schubert main(int argc, char *argv[])
184*7f2fe78bSCy Schubert {
185*7f2fe78bSCy Schubert     OM_uint32 minor, major;
186*7f2fe78bSCy Schubert     gss_cred_id_t impersonator_cred_handle = GSS_C_NO_CREDENTIAL;
187*7f2fe78bSCy Schubert     gss_cred_id_t user_cred_handle = GSS_C_NO_CREDENTIAL;
188*7f2fe78bSCy Schubert     gss_cred_id_t delegated_cred_handle = GSS_C_NO_CREDENTIAL;
189*7f2fe78bSCy Schubert     gss_name_t user = GSS_C_NO_NAME, target = GSS_C_NO_NAME;
190*7f2fe78bSCy Schubert     gss_OID_set mechs, actual_mechs = GSS_C_NO_OID_SET;
191*7f2fe78bSCy Schubert     uid_t uid;
192*7f2fe78bSCy Schubert 
193*7f2fe78bSCy Schubert     if (argc < 2 || argc > 5) {
194*7f2fe78bSCy Schubert         fprintf(stderr, "Usage: %s [--spnego] [user] "
195*7f2fe78bSCy Schubert                 "[proxy-target] [keytab]\n", argv[0]);
196*7f2fe78bSCy Schubert         fprintf(stderr, "       proxy-target and keytab are optional\n");
197*7f2fe78bSCy Schubert         exit(1);
198*7f2fe78bSCy Schubert     }
199*7f2fe78bSCy Schubert 
200*7f2fe78bSCy Schubert     if (strcmp(argv[1], "--spnego") == 0) {
201*7f2fe78bSCy Schubert         use_spnego++;
202*7f2fe78bSCy Schubert         argc--;
203*7f2fe78bSCy Schubert         argv++;
204*7f2fe78bSCy Schubert     }
205*7f2fe78bSCy Schubert 
206*7f2fe78bSCy Schubert     user = import_name(argv[1]);
207*7f2fe78bSCy Schubert 
208*7f2fe78bSCy Schubert     major = gss_pname_to_uid(&minor, user, NULL, &uid);
209*7f2fe78bSCy Schubert     check_gsserr("gss_pname_to_uid(user)", major, minor);
210*7f2fe78bSCy Schubert 
211*7f2fe78bSCy Schubert     if (argc > 2 && strcmp(argv[2], "-") != 0)
212*7f2fe78bSCy Schubert         target = import_name(argv[2]);
213*7f2fe78bSCy Schubert 
214*7f2fe78bSCy Schubert     mechs = use_spnego ? &mechset_spnego : &mechset_krb5;
215*7f2fe78bSCy Schubert 
216*7f2fe78bSCy Schubert     get_default_cred((argc > 3) ? argv[3] : NULL, mechs,
217*7f2fe78bSCy Schubert                      &impersonator_cred_handle);
218*7f2fe78bSCy Schubert 
219*7f2fe78bSCy Schubert     printf("Protocol transition tests follow\n");
220*7f2fe78bSCy Schubert     printf("-----------------------------------\n\n");
221*7f2fe78bSCy Schubert 
222*7f2fe78bSCy Schubert     /* get S4U2Self cred */
223*7f2fe78bSCy Schubert     major = gss_acquire_cred_impersonate_name(&minor, impersonator_cred_handle,
224*7f2fe78bSCy Schubert                                               user, GSS_C_INDEFINITE, mechs,
225*7f2fe78bSCy Schubert                                               GSS_C_INITIATE,
226*7f2fe78bSCy Schubert                                               &user_cred_handle, &actual_mechs,
227*7f2fe78bSCy Schubert                                               NULL);
228*7f2fe78bSCy Schubert     check_gsserr("gss_acquire_cred_impersonate_name", major, minor);
229*7f2fe78bSCy Schubert 
230*7f2fe78bSCy Schubert     /* Try to store it in default ccache */
231*7f2fe78bSCy Schubert     major = gss_store_cred(&minor, user_cred_handle, GSS_C_INITIATE,
232*7f2fe78bSCy Schubert                            &mechs->elements[0], 1, 1, NULL, NULL);
233*7f2fe78bSCy Schubert     check_gsserr("gss_store_cred", major, minor);
234*7f2fe78bSCy Schubert 
235*7f2fe78bSCy Schubert     init_accept_sec_context(user_cred_handle, impersonator_cred_handle,
236*7f2fe78bSCy Schubert                             &delegated_cred_handle);
237*7f2fe78bSCy Schubert 
238*7f2fe78bSCy Schubert     printf("\n");
239*7f2fe78bSCy Schubert 
240*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &user);
241*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &target);
242*7f2fe78bSCy Schubert     (void)gss_release_cred(&minor, &delegated_cred_handle);
243*7f2fe78bSCy Schubert     (void)gss_release_cred(&minor, &impersonator_cred_handle);
244*7f2fe78bSCy Schubert     (void)gss_release_cred(&minor, &user_cred_handle);
245*7f2fe78bSCy Schubert     (void)gss_release_oid_set(&minor, &actual_mechs);
246*7f2fe78bSCy Schubert     return 0;
247*7f2fe78bSCy Schubert }
248