xref: /freebsd/crypto/krb5/src/tests/gssapi/t_saslname.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2009  by the Massachusetts Institute of Technology.
4  * All Rights Reserved.
5  *
6  * Export of this software from the United States of America may
7  *   require a specific license from the United States Government.
8  *   It is the responsibility of any person or organization contemplating
9  *   export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "common.h"
31 
32 static void
dump_known_mech_attrs(gss_OID mech)33 dump_known_mech_attrs(gss_OID mech)
34 {
35     OM_uint32 major, minor;
36     gss_OID_set mech_attrs = GSS_C_NO_OID_SET;
37     gss_OID_set known_attrs = GSS_C_NO_OID_SET;
38     size_t i;
39 
40     major = gss_inquire_attrs_for_mech(&minor, mech, &mech_attrs,
41                                        &known_attrs);
42     check_gsserr("gss_inquire_attrs_for_mech", major, minor);
43 
44     printf("Known attributes\n");
45     printf("----------------\n");
46     for (i = 0; i < known_attrs->count; i++) {
47         gss_buffer_desc name = GSS_C_EMPTY_BUFFER;
48         gss_buffer_desc short_desc = GSS_C_EMPTY_BUFFER;
49         gss_buffer_desc long_desc = GSS_C_EMPTY_BUFFER;
50 
51         major = gss_display_mech_attr(&minor, &known_attrs->elements[i],
52                                       &name, &short_desc, &long_desc);
53         check_gsserr("gss_display_mech_attr", major, minor);
54         printf("%.*s (%.*s): %.*s\n", (int)short_desc.length,
55                (char *)short_desc.value, (int)name.length, (char *)name.value,
56                (int)long_desc.length, (char *)long_desc.value);
57         (void)gss_release_buffer(&minor, &name);
58         (void)gss_release_buffer(&minor, &short_desc);
59         (void)gss_release_buffer(&minor, &long_desc);
60     }
61     printf("\n");
62     (void)gss_release_oid_set(&minor, &mech_attrs);
63     (void)gss_release_oid_set(&minor, &known_attrs);
64 }
65 
66 static void
dump_mech_attrs(gss_OID mech)67 dump_mech_attrs(gss_OID mech)
68 {
69     OM_uint32 major, minor;
70     gss_OID_set mech_attrs = GSS_C_NO_OID_SET;
71     gss_OID_set known_attrs = GSS_C_NO_OID_SET;
72     size_t i;
73 
74     major = gss_inquire_attrs_for_mech(&minor, mech, &mech_attrs,
75                                        &known_attrs);
76     check_gsserr("gss_inquire_attrs_for_mech", major, minor);
77 
78     printf("Mech attrs:  ");
79 
80     for (i = 0; i < mech_attrs->count; i++) {
81         gss_buffer_desc name = GSS_C_EMPTY_BUFFER;
82         gss_buffer_desc short_desc = GSS_C_EMPTY_BUFFER;
83         gss_buffer_desc long_desc = GSS_C_EMPTY_BUFFER;
84 
85         major = gss_display_mech_attr(&minor, &mech_attrs->elements[i],
86                                       &name, &short_desc, &long_desc);
87         check_gsserr("gss_display_mech_attr", major, minor);
88         printf("%.*s ", (int)name.length, (char *)name.value);
89         (void)gss_release_buffer(&minor, &name);
90         (void)gss_release_buffer(&minor, &short_desc);
91         (void)gss_release_buffer(&minor, &long_desc);
92     }
93     printf("\n");
94 
95     (void)gss_release_oid_set(&minor, &mech_attrs);
96     (void)gss_release_oid_set(&minor, &known_attrs);
97 }
98 
99 int
main(int argc,char * argv[])100 main(int argc, char *argv[])
101 {
102     gss_OID_set mechs;
103     OM_uint32 major, minor;
104     size_t i;
105 
106     major = gss_indicate_mechs(&minor, &mechs);
107     check_gsserr("gss_indicate_mechs", major, minor);
108     if (mechs->count > 0)
109         dump_known_mech_attrs(mechs->elements);
110 
111     for (i = 0; i < mechs->count; i++) {
112         gss_buffer_desc oidstr = GSS_C_EMPTY_BUFFER;
113         gss_buffer_desc sasl_mech_name = GSS_C_EMPTY_BUFFER;
114         gss_buffer_desc mech_name = GSS_C_EMPTY_BUFFER;
115         gss_buffer_desc mech_description = GSS_C_EMPTY_BUFFER;
116         gss_OID oid = GSS_C_NO_OID;
117 
118         major = gss_oid_to_str(&minor, &mechs->elements[i], &oidstr);
119         if (GSS_ERROR(major))
120             continue;
121 
122         major = gss_inquire_saslname_for_mech(&minor, &mechs->elements[i],
123                                               &sasl_mech_name, &mech_name,
124                                               &mech_description);
125         if (GSS_ERROR(major)) {
126             gss_release_buffer(&minor, &oidstr);
127             continue;
128         }
129 
130         printf("-------------------------------------------------------------"
131                "-----------------\n");
132         printf("OID        : %.*s\n", (int)oidstr.length,
133                (char *)oidstr.value);
134         printf("SASL mech  : %.*s\n", (int)sasl_mech_name.length,
135                (char *)sasl_mech_name.value);
136         printf("Mech name  : %.*s\n", (int)mech_name.length,
137                (char *)mech_name.value);
138         printf("Mech desc  : %.*s\n", (int)mech_description.length,
139                (char *)mech_description.value);
140         dump_mech_attrs(&mechs->elements[i]);
141         printf("-------------------------------------------------------------"
142                "-----------------\n");
143 
144         major = gss_inquire_mech_for_saslname(&minor, &sasl_mech_name, &oid);
145         check_gsserr("gss_inquire_mech_for_saslname", major, minor);
146 
147         if (oid == GSS_C_NO_OID ||
148             (oid->length != mechs->elements[i].length &&
149              memcmp(oid->elements, mechs->elements[i].elements,
150                     oid->length) != 0)) {
151             (void)gss_release_buffer(&minor, &oidstr);
152             (void)gss_oid_to_str(&minor, oid, &oidstr);
153             fprintf(stderr, "Got different OID %.*s for mechanism %.*s\n",
154                     (int)oidstr.length, (char *)oidstr.value,
155                     (int)sasl_mech_name.length, (char *)sasl_mech_name.value);
156         }
157         (void)gss_release_buffer(&minor, &oidstr);
158         (void)gss_release_buffer(&minor, &sasl_mech_name);
159         (void)gss_release_buffer(&minor, &mech_name);
160         (void)gss_release_buffer(&minor, &mech_description);
161     }
162 
163     (void)gss_release_oid_set(&minor, &mechs);
164     return 0;
165 }
166