xref: /freebsd/crypto/krb5/src/tests/gssapi/t_s4u2proxy_krb5.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/gssapi/t_s4u2proxy_deleg.c - Test S4U2Proxy after krb5 auth */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright 2011 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All Rights Reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert  *   require a specific license from the United States Government.
9*7f2fe78bSCy Schubert  *   It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert  *   export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert  *
12*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
24*7f2fe78bSCy Schubert  * or implied warranty.
25*7f2fe78bSCy Schubert  */
26*7f2fe78bSCy Schubert 
27*7f2fe78bSCy Schubert #include <stdio.h>
28*7f2fe78bSCy Schubert #include <stdlib.h>
29*7f2fe78bSCy Schubert #include <string.h>
30*7f2fe78bSCy Schubert 
31*7f2fe78bSCy Schubert #include "common.h"
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert  * Usage: ./t_s4u2proxy_krb5 [--spnego] client_cache storage_cache
35*7f2fe78bSCy Schubert  *                                      [accname|-] service1 service2
36*7f2fe78bSCy Schubert  *
37*7f2fe78bSCy Schubert  * This program performs a regular Kerberos or SPNEGO authentication from the
38*7f2fe78bSCy Schubert  * default principal of client_cache to service1.  If that authentication
39*7f2fe78bSCy Schubert  * yields delegated credentials, the program stores those credentials in
40*7f2fe78bSCy Schubert  * sorage_ccache and uses that cache to perform a second authentication to
41*7f2fe78bSCy Schubert  * service2 using S4U2Proxy.
42*7f2fe78bSCy Schubert  *
43*7f2fe78bSCy Schubert  * The default keytab must contain keys for service1 and service2.  The default
44*7f2fe78bSCy Schubert  * ccache must contain a TGT for service1.  This program assumes that krb5 or
45*7f2fe78bSCy Schubert  * SPNEGO authentication requires only one token exchange.
46*7f2fe78bSCy Schubert  */
47*7f2fe78bSCy Schubert 
48*7f2fe78bSCy Schubert int
main(int argc,char * argv[])49*7f2fe78bSCy Schubert main(int argc, char *argv[])
50*7f2fe78bSCy Schubert {
51*7f2fe78bSCy Schubert     const char *client_ccname, *storage_ccname, *accname, *service1, *service2;
52*7f2fe78bSCy Schubert     krb5_context context = NULL;
53*7f2fe78bSCy Schubert     krb5_error_code ret;
54*7f2fe78bSCy Schubert     krb5_boolean use_spnego = FALSE;
55*7f2fe78bSCy Schubert     krb5_ccache storage_ccache = NULL;
56*7f2fe78bSCy Schubert     krb5_principal client_princ = NULL;
57*7f2fe78bSCy Schubert     OM_uint32 minor, major, flags;
58*7f2fe78bSCy Schubert     gss_buffer_desc buf = GSS_C_EMPTY_BUFFER;
59*7f2fe78bSCy Schubert     gss_OID mech;
60*7f2fe78bSCy Schubert     gss_OID_set mechs;
61*7f2fe78bSCy Schubert     gss_name_t acceptor_name = GSS_C_NO_NAME, client_name = GSS_C_NO_NAME;
62*7f2fe78bSCy Schubert     gss_name_t service1_name = GSS_C_NO_NAME, service2_name = GSS_C_NO_NAME;
63*7f2fe78bSCy Schubert     gss_cred_id_t service1_cred = GSS_C_NO_CREDENTIAL;
64*7f2fe78bSCy Schubert     gss_cred_id_t deleg_cred = GSS_C_NO_CREDENTIAL;
65*7f2fe78bSCy Schubert     gss_ctx_id_t initiator_context, acceptor_context;
66*7f2fe78bSCy Schubert 
67*7f2fe78bSCy Schubert     /* Parse arguments. */
68*7f2fe78bSCy Schubert     if (argc >= 2 && strcmp(argv[1], "--spnego") == 0) {
69*7f2fe78bSCy Schubert         use_spnego = TRUE;
70*7f2fe78bSCy Schubert         argc--;
71*7f2fe78bSCy Schubert         argv++;
72*7f2fe78bSCy Schubert     }
73*7f2fe78bSCy Schubert     if (argc != 6) {
74*7f2fe78bSCy Schubert         fprintf(stderr, "./t_s4u2proxy_krb5 [--spnego] client_ccache "
75*7f2fe78bSCy Schubert                 "storage_ccache [accname|-] service1 service2\n");
76*7f2fe78bSCy Schubert         return 1;
77*7f2fe78bSCy Schubert     }
78*7f2fe78bSCy Schubert     client_ccname = argv[1];
79*7f2fe78bSCy Schubert     storage_ccname = argv[2];
80*7f2fe78bSCy Schubert     accname = argv[3];
81*7f2fe78bSCy Schubert     service1 = argv[4];
82*7f2fe78bSCy Schubert     service2 = argv[5];
83*7f2fe78bSCy Schubert 
84*7f2fe78bSCy Schubert     mech = use_spnego ? &mech_spnego : &mech_krb5;
85*7f2fe78bSCy Schubert     mechs = use_spnego ? &mechset_spnego : &mechset_krb5;
86*7f2fe78bSCy Schubert     ret = krb5_init_context(&context);
87*7f2fe78bSCy Schubert     check_k5err(context, "krb5_init_context", ret);
88*7f2fe78bSCy Schubert 
89*7f2fe78bSCy Schubert     /* Get GSS_C_BOTH acceptor credentials, using the default ccache. */
90*7f2fe78bSCy Schubert     acceptor_name = GSS_C_NO_NAME;
91*7f2fe78bSCy Schubert     if (strcmp(accname, "-") != 0)
92*7f2fe78bSCy Schubert         acceptor_name = import_name(service1);
93*7f2fe78bSCy Schubert     major = gss_acquire_cred(&minor, acceptor_name, GSS_C_INDEFINITE,
94*7f2fe78bSCy Schubert                              mechs, GSS_C_BOTH, &service1_cred, NULL, NULL);
95*7f2fe78bSCy Schubert     check_gsserr("gss_acquire_cred(service1)", major, minor);
96*7f2fe78bSCy Schubert 
97*7f2fe78bSCy Schubert     /* Establish contexts using the client ccache. */
98*7f2fe78bSCy Schubert     service1_name = import_name(service1);
99*7f2fe78bSCy Schubert     major = gss_krb5_ccache_name(&minor, client_ccname, NULL);
100*7f2fe78bSCy Schubert     check_gsserr("gss_krb5_ccache_name(1)", major, minor);
101*7f2fe78bSCy Schubert     flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
102*7f2fe78bSCy Schubert     establish_contexts(mech, GSS_C_NO_CREDENTIAL, service1_cred, service1_name,
103*7f2fe78bSCy Schubert                        flags, &initiator_context, &acceptor_context,
104*7f2fe78bSCy Schubert                        &client_name, NULL, &deleg_cred);
105*7f2fe78bSCy Schubert 
106*7f2fe78bSCy Schubert     /* Display and remember the client principal. */
107*7f2fe78bSCy Schubert     major = gss_display_name(&minor, client_name, &buf, NULL);
108*7f2fe78bSCy Schubert     check_gsserr("gss_display_name(1)", major, minor);
109*7f2fe78bSCy Schubert     printf("auth1: %.*s\n", (int)buf.length, (char *)buf.value);
110*7f2fe78bSCy Schubert     /* Assumes buffer is null-terminated, which in our implementation it is. */
111*7f2fe78bSCy Schubert     ret = krb5_parse_name(context, buf.value, &client_princ);
112*7f2fe78bSCy Schubert     check_k5err(context, "krb5_parse_name", ret);
113*7f2fe78bSCy Schubert     (void)gss_release_buffer(&minor, &buf);
114*7f2fe78bSCy Schubert 
115*7f2fe78bSCy Schubert     if (deleg_cred == GSS_C_NO_CREDENTIAL) {
116*7f2fe78bSCy Schubert         printf("no credential delegated.\n");
117*7f2fe78bSCy Schubert         goto cleanup;
118*7f2fe78bSCy Schubert     }
119*7f2fe78bSCy Schubert 
120*7f2fe78bSCy Schubert     /* Take the opportunity to test cred export/import on the synthesized
121*7f2fe78bSCy Schubert      * S4U2Proxy delegated cred. */
122*7f2fe78bSCy Schubert     export_import_cred(&deleg_cred);
123*7f2fe78bSCy Schubert 
124*7f2fe78bSCy Schubert     /* Store the delegated credentials. */
125*7f2fe78bSCy Schubert     ret = krb5_cc_resolve(context, storage_ccname, &storage_ccache);
126*7f2fe78bSCy Schubert     check_k5err(context, "krb5_cc_resolve", ret);
127*7f2fe78bSCy Schubert     ret = krb5_cc_initialize(context, storage_ccache, client_princ);
128*7f2fe78bSCy Schubert     check_k5err(context, "krb5_cc_initialize", ret);
129*7f2fe78bSCy Schubert     major = gss_krb5_copy_ccache(&minor, deleg_cred, storage_ccache);
130*7f2fe78bSCy Schubert     check_gsserr("gss_krb5_copy_ccache", major, minor);
131*7f2fe78bSCy Schubert     ret = krb5_cc_close(context, storage_ccache);
132*7f2fe78bSCy Schubert     check_k5err(context, "krb5_cc_close", ret);
133*7f2fe78bSCy Schubert 
134*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &initiator_context, GSS_C_NO_BUFFER);
135*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &acceptor_context, GSS_C_NO_BUFFER);
136*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &client_name);
137*7f2fe78bSCy Schubert     (void)gss_release_cred(&minor, &deleg_cred);
138*7f2fe78bSCy Schubert 
139*7f2fe78bSCy Schubert     /* Establish contexts using the storage ccache. */
140*7f2fe78bSCy Schubert     service2_name = import_name(service2);
141*7f2fe78bSCy Schubert     major = gss_krb5_ccache_name(&minor, storage_ccname, NULL);
142*7f2fe78bSCy Schubert     check_gsserr("gss_krb5_ccache_name(2)", major, minor);
143*7f2fe78bSCy Schubert     establish_contexts(mech, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL,
144*7f2fe78bSCy Schubert                        service2_name, flags, &initiator_context,
145*7f2fe78bSCy Schubert                        &acceptor_context, &client_name, NULL, &deleg_cred);
146*7f2fe78bSCy Schubert 
147*7f2fe78bSCy Schubert     major = gss_display_name(&minor, client_name, &buf, NULL);
148*7f2fe78bSCy Schubert     check_gsserr("gss_display_name(2)", major, minor);
149*7f2fe78bSCy Schubert     printf("auth2: %.*s\n", (int)buf.length, (char *)buf.value);
150*7f2fe78bSCy Schubert     (void)gss_release_buffer(&minor, &buf);
151*7f2fe78bSCy Schubert 
152*7f2fe78bSCy Schubert cleanup:
153*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &acceptor_name);
154*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &client_name);
155*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &service1_name);
156*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &service2_name);
157*7f2fe78bSCy Schubert     (void)gss_release_cred(&minor, &service1_cred);
158*7f2fe78bSCy Schubert     (void)gss_release_cred(&minor, &deleg_cred);
159*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &initiator_context, GSS_C_NO_BUFFER);
160*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &acceptor_context, GSS_C_NO_BUFFER);
161*7f2fe78bSCy Schubert     krb5_free_principal(context, client_princ);
162*7f2fe78bSCy Schubert     krb5_free_context(context);
163*7f2fe78bSCy Schubert     return 0;
164*7f2fe78bSCy Schubert }
165