xref: /freebsd/crypto/krb5/src/tests/gssapi/t_bindings.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 2020 by Red Hat, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in
15  *   the documentation and/or other materials provided with the
16  *   distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35 
36 #include "common.h"
37 
38 /*
39  * Establish contexts (without and with GSS_C_DCE_STYLE) with the default
40  * initiator name, a specified principal name as target name, initiator
41  * bindings, and acceptor bindings.  If any call is unsuccessful, display an
42  * error message.  Output "yes" or "no" to indicate whether the contexts were
43  * reported as channel-bound on the acceptor.  Exit with status 0 if all
44  * operations are successful, or 1 if not.
45  *
46  * Usage: ./t_bindings [-s] [-b] targetname icb acb
47  *
48  * An icb or abc value of "-" will not specify channel bindings.  The -s flag
49  * uses the SPNEGO mechanism instead of the krb5 mecanism.  The -b flag
50  * includes GSS_C_CHANNEL_BOUND in req_flags, which requests strict enforcement
51  * of channel bindings by the acceptor.
52  */
53 
54 int
main(int argc,char * argv[])55 main(int argc, char *argv[])
56 {
57     OM_uint32 client_flags = 0;
58     OM_uint32 minor, flags1, flags2;
59     gss_name_t target_name;
60     gss_ctx_id_t ictx, actx;
61     struct gss_channel_bindings_struct icb_data = {0}, acb_data = {0};
62     gss_channel_bindings_t icb = GSS_C_NO_CHANNEL_BINDINGS;
63     gss_channel_bindings_t acb = GSS_C_NO_CHANNEL_BINDINGS;
64     gss_OID_desc *mech = GSS_C_NO_OID;
65 
66     argv++;
67     argc--;
68 
69     if (*argv != NULL && strcmp(*argv, "-s") == 0) {
70         mech = &mech_spnego;
71         argv++;
72         argc--;
73     }
74 
75     if (*argv != NULL && strcmp(*argv, "-b") == 0) {
76         client_flags |= GSS_C_CHANNEL_BOUND_FLAG;
77         argv++;
78         argc--;
79     }
80 
81     if (mech == GSS_C_NO_OID)
82         mech = &mech_krb5;
83 
84     if (argc != 3) {
85         fprintf(stderr, "Usage: t_bindings [-s] [-b] targetname icb acb\n");
86         return 1;
87     }
88 
89     target_name = import_name(argv[0]);
90 
91     if (strcmp(argv[1], "-") != 0) {
92         icb_data.application_data.length = strlen(argv[1]);
93         icb_data.application_data.value = argv[1];
94         icb = &icb_data;
95     }
96 
97     if (strcmp(argv[2], "-") != 0) {
98         acb_data.application_data.length = strlen(argv[2]);
99         acb_data.application_data.value = argv[2];
100         acb = &acb_data;
101     }
102 
103     establish_contexts_ex(mech, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL,
104                           target_name, client_flags, &ictx, &actx, icb, acb,
105                           &flags1, NULL, NULL, NULL);
106 
107     /* Try again with GSS_C_DCE_STYLE */
108     (void)gss_delete_sec_context(&minor, &ictx, NULL);
109     (void)gss_delete_sec_context(&minor, &actx, NULL);
110 
111     client_flags |= GSS_C_DCE_STYLE;
112     establish_contexts_ex(mech, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL,
113                           target_name, client_flags, &ictx, &actx, icb, acb,
114                           &flags2, NULL, NULL, NULL);
115     assert((flags1 & GSS_C_CHANNEL_BOUND_FLAG) ==
116            (flags2 & GSS_C_CHANNEL_BOUND_FLAG));
117     printf("%s\n", (flags1 & GSS_C_CHANNEL_BOUND_FLAG) ? "yes" : "no");
118 
119     (void)gss_delete_sec_context(&minor, &ictx, NULL);
120     (void)gss_delete_sec_context(&minor, &actx, NULL);
121     (void)gss_release_name(&minor, &target_name);
122 
123     return 0;
124 }
125