xref: /freebsd/crypto/krb5/src/tests/gssapi/t_err.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/gssapi/t_err.c - Test accept_sec_context error generation */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2013 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert  * This test program verifies that the krb5 gss_accept_sec_context can produce
35*7f2fe78bSCy Schubert  * error tokens and that gss_init_sec_context can interpret them.
36*7f2fe78bSCy Schubert  */
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert #include <stdio.h>
39*7f2fe78bSCy Schubert #include <stdlib.h>
40*7f2fe78bSCy Schubert #include <string.h>
41*7f2fe78bSCy Schubert #include <assert.h>
42*7f2fe78bSCy Schubert 
43*7f2fe78bSCy Schubert #include "common.h"
44*7f2fe78bSCy Schubert 
45*7f2fe78bSCy Schubert static void
check_replay_error(const char * msg,OM_uint32 major,OM_uint32 minor)46*7f2fe78bSCy Schubert check_replay_error(const char *msg, OM_uint32 major, OM_uint32 minor)
47*7f2fe78bSCy Schubert {
48*7f2fe78bSCy Schubert     OM_uint32 tmpmin, msg_ctx = 0;
49*7f2fe78bSCy Schubert     const char *replay = "Request is a replay";
50*7f2fe78bSCy Schubert     gss_buffer_desc m;
51*7f2fe78bSCy Schubert 
52*7f2fe78bSCy Schubert     if (major != GSS_S_FAILURE) {
53*7f2fe78bSCy Schubert         fprintf(stderr, "%s: expected major code GSS_S_FAILURE\n", msg);
54*7f2fe78bSCy Schubert         check_gsserr(msg, major, minor);
55*7f2fe78bSCy Schubert         exit(1);
56*7f2fe78bSCy Schubert     }
57*7f2fe78bSCy Schubert 
58*7f2fe78bSCy Schubert     (void)gss_display_status(&tmpmin, minor, GSS_C_MECH_CODE, GSS_C_NULL_OID,
59*7f2fe78bSCy Schubert                              &msg_ctx, &m);
60*7f2fe78bSCy Schubert     if (m.length != strlen(replay) || memcmp(m.value, replay, m.length) != 0) {
61*7f2fe78bSCy Schubert         fprintf(stderr, "%s: expected replay error; got %.*s\n", msg,
62*7f2fe78bSCy Schubert                 (int)m.length, (char *)m.value);
63*7f2fe78bSCy Schubert         exit(1);
64*7f2fe78bSCy Schubert     }
65*7f2fe78bSCy Schubert     (void)gss_release_buffer(&tmpmin, &m);
66*7f2fe78bSCy Schubert }
67*7f2fe78bSCy Schubert 
68*7f2fe78bSCy Schubert int
main(int argc,char * argv[])69*7f2fe78bSCy Schubert main(int argc, char *argv[])
70*7f2fe78bSCy Schubert {
71*7f2fe78bSCy Schubert     OM_uint32 minor, major, flags;
72*7f2fe78bSCy Schubert     gss_OID mech = &mech_krb5;
73*7f2fe78bSCy Schubert     gss_name_t tname;
74*7f2fe78bSCy Schubert     gss_buffer_desc itok, atok;
75*7f2fe78bSCy Schubert     gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;
76*7f2fe78bSCy Schubert 
77*7f2fe78bSCy Schubert     argv++;
78*7f2fe78bSCy Schubert     if (*argv != NULL && strcmp(*argv, "--spnego") == 0) {
79*7f2fe78bSCy Schubert         mech = &mech_spnego;
80*7f2fe78bSCy Schubert         argv++;
81*7f2fe78bSCy Schubert     }
82*7f2fe78bSCy Schubert     if (*argv == NULL || argv[1] != NULL) {
83*7f2fe78bSCy Schubert         fprintf(stderr, "Usage: t_err targetname\n");
84*7f2fe78bSCy Schubert         return 1;
85*7f2fe78bSCy Schubert     }
86*7f2fe78bSCy Schubert     tname = import_name(*argv);
87*7f2fe78bSCy Schubert 
88*7f2fe78bSCy Schubert     /* Get the initial context token. */
89*7f2fe78bSCy Schubert     flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_MUTUAL_FLAG;
90*7f2fe78bSCy Schubert     major = gss_init_sec_context(&minor, GSS_C_NO_CREDENTIAL, &ictx, tname,
91*7f2fe78bSCy Schubert                                  mech, flags, GSS_C_INDEFINITE,
92*7f2fe78bSCy Schubert                                  GSS_C_NO_CHANNEL_BINDINGS, GSS_C_NO_BUFFER,
93*7f2fe78bSCy Schubert                                  NULL, &itok, NULL, NULL);
94*7f2fe78bSCy Schubert     check_gsserr("gss_init_sec_context(1)", major, minor);
95*7f2fe78bSCy Schubert     assert(major == GSS_S_CONTINUE_NEEDED);
96*7f2fe78bSCy Schubert 
97*7f2fe78bSCy Schubert     /* Process this token into an acceptor context, then discard it. */
98*7f2fe78bSCy Schubert     major = gss_accept_sec_context(&minor, &actx, GSS_C_NO_CREDENTIAL, &itok,
99*7f2fe78bSCy Schubert                                    GSS_C_NO_CHANNEL_BINDINGS, NULL,
100*7f2fe78bSCy Schubert                                    NULL, &atok, NULL, NULL, NULL);
101*7f2fe78bSCy Schubert     check_gsserr("gss_accept_sec_context(1)", major, minor);
102*7f2fe78bSCy Schubert     (void)gss_release_buffer(&minor, &atok);
103*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &actx, NULL);
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert     /* Process the same token again, producing a replay error. */
106*7f2fe78bSCy Schubert     major = gss_accept_sec_context(&minor, &actx, GSS_C_NO_CREDENTIAL, &itok,
107*7f2fe78bSCy Schubert                                    GSS_C_NO_CHANNEL_BINDINGS, NULL,
108*7f2fe78bSCy Schubert                                    NULL, &atok, NULL, NULL, NULL);
109*7f2fe78bSCy Schubert     check_replay_error("gss_accept_sec_context(2)", major, minor);
110*7f2fe78bSCy Schubert     assert(atok.length != 0);
111*7f2fe78bSCy Schubert 
112*7f2fe78bSCy Schubert     /* Send the error token back the initiator. */
113*7f2fe78bSCy Schubert     (void)gss_release_buffer(&minor, &itok);
114*7f2fe78bSCy Schubert     major = gss_init_sec_context(&minor, GSS_C_NO_CREDENTIAL, &ictx, tname,
115*7f2fe78bSCy Schubert                                  mech, flags, GSS_C_INDEFINITE,
116*7f2fe78bSCy Schubert                                  GSS_C_NO_CHANNEL_BINDINGS, &atok,
117*7f2fe78bSCy Schubert                                  NULL, &itok, NULL, NULL);
118*7f2fe78bSCy Schubert     check_replay_error("gss_init_sec_context(2)", major, minor);
119*7f2fe78bSCy Schubert 
120*7f2fe78bSCy Schubert     (void)gss_release_name(&minor, &tname);
121*7f2fe78bSCy Schubert     (void)gss_release_buffer(&minor, &itok);
122*7f2fe78bSCy Schubert     (void)gss_release_buffer(&minor, &atok);
123*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &ictx, NULL);
124*7f2fe78bSCy Schubert     (void)gss_delete_sec_context(&minor, &actx, NULL);
125*7f2fe78bSCy Schubert     return 0;
126*7f2fe78bSCy Schubert }
127