xref: /freebsd/crypto/krb5/src/tests/gssapi/t_saslname.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 static void
dump_known_mech_attrs(gss_OID mech)33*7f2fe78bSCy Schubert dump_known_mech_attrs(gss_OID mech)
34*7f2fe78bSCy Schubert {
35*7f2fe78bSCy Schubert     OM_uint32 major, minor;
36*7f2fe78bSCy Schubert     gss_OID_set mech_attrs = GSS_C_NO_OID_SET;
37*7f2fe78bSCy Schubert     gss_OID_set known_attrs = GSS_C_NO_OID_SET;
38*7f2fe78bSCy Schubert     size_t i;
39*7f2fe78bSCy Schubert 
40*7f2fe78bSCy Schubert     major = gss_inquire_attrs_for_mech(&minor, mech, &mech_attrs,
41*7f2fe78bSCy Schubert                                        &known_attrs);
42*7f2fe78bSCy Schubert     check_gsserr("gss_inquire_attrs_for_mech", major, minor);
43*7f2fe78bSCy Schubert 
44*7f2fe78bSCy Schubert     printf("Known attributes\n");
45*7f2fe78bSCy Schubert     printf("----------------\n");
46*7f2fe78bSCy Schubert     for (i = 0; i < known_attrs->count; i++) {
47*7f2fe78bSCy Schubert         gss_buffer_desc name = GSS_C_EMPTY_BUFFER;
48*7f2fe78bSCy Schubert         gss_buffer_desc short_desc = GSS_C_EMPTY_BUFFER;
49*7f2fe78bSCy Schubert         gss_buffer_desc long_desc = GSS_C_EMPTY_BUFFER;
50*7f2fe78bSCy Schubert 
51*7f2fe78bSCy Schubert         major = gss_display_mech_attr(&minor, &known_attrs->elements[i],
52*7f2fe78bSCy Schubert                                       &name, &short_desc, &long_desc);
53*7f2fe78bSCy Schubert         check_gsserr("gss_display_mech_attr", major, minor);
54*7f2fe78bSCy Schubert         printf("%.*s (%.*s): %.*s\n", (int)short_desc.length,
55*7f2fe78bSCy Schubert                (char *)short_desc.value, (int)name.length, (char *)name.value,
56*7f2fe78bSCy Schubert                (int)long_desc.length, (char *)long_desc.value);
57*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &name);
58*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &short_desc);
59*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &long_desc);
60*7f2fe78bSCy Schubert     }
61*7f2fe78bSCy Schubert     printf("\n");
62*7f2fe78bSCy Schubert     (void)gss_release_oid_set(&minor, &mech_attrs);
63*7f2fe78bSCy Schubert     (void)gss_release_oid_set(&minor, &known_attrs);
64*7f2fe78bSCy Schubert }
65*7f2fe78bSCy Schubert 
66*7f2fe78bSCy Schubert static void
dump_mech_attrs(gss_OID mech)67*7f2fe78bSCy Schubert dump_mech_attrs(gss_OID mech)
68*7f2fe78bSCy Schubert {
69*7f2fe78bSCy Schubert     OM_uint32 major, minor;
70*7f2fe78bSCy Schubert     gss_OID_set mech_attrs = GSS_C_NO_OID_SET;
71*7f2fe78bSCy Schubert     gss_OID_set known_attrs = GSS_C_NO_OID_SET;
72*7f2fe78bSCy Schubert     size_t i;
73*7f2fe78bSCy Schubert 
74*7f2fe78bSCy Schubert     major = gss_inquire_attrs_for_mech(&minor, mech, &mech_attrs,
75*7f2fe78bSCy Schubert                                        &known_attrs);
76*7f2fe78bSCy Schubert     check_gsserr("gss_inquire_attrs_for_mech", major, minor);
77*7f2fe78bSCy Schubert 
78*7f2fe78bSCy Schubert     printf("Mech attrs:  ");
79*7f2fe78bSCy Schubert 
80*7f2fe78bSCy Schubert     for (i = 0; i < mech_attrs->count; i++) {
81*7f2fe78bSCy Schubert         gss_buffer_desc name = GSS_C_EMPTY_BUFFER;
82*7f2fe78bSCy Schubert         gss_buffer_desc short_desc = GSS_C_EMPTY_BUFFER;
83*7f2fe78bSCy Schubert         gss_buffer_desc long_desc = GSS_C_EMPTY_BUFFER;
84*7f2fe78bSCy Schubert 
85*7f2fe78bSCy Schubert         major = gss_display_mech_attr(&minor, &mech_attrs->elements[i],
86*7f2fe78bSCy Schubert                                       &name, &short_desc, &long_desc);
87*7f2fe78bSCy Schubert         check_gsserr("gss_display_mech_attr", major, minor);
88*7f2fe78bSCy Schubert         printf("%.*s ", (int)name.length, (char *)name.value);
89*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &name);
90*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &short_desc);
91*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &long_desc);
92*7f2fe78bSCy Schubert     }
93*7f2fe78bSCy Schubert     printf("\n");
94*7f2fe78bSCy Schubert 
95*7f2fe78bSCy Schubert     (void)gss_release_oid_set(&minor, &mech_attrs);
96*7f2fe78bSCy Schubert     (void)gss_release_oid_set(&minor, &known_attrs);
97*7f2fe78bSCy Schubert }
98*7f2fe78bSCy Schubert 
99*7f2fe78bSCy Schubert int
main(int argc,char * argv[])100*7f2fe78bSCy Schubert main(int argc, char *argv[])
101*7f2fe78bSCy Schubert {
102*7f2fe78bSCy Schubert     gss_OID_set mechs;
103*7f2fe78bSCy Schubert     OM_uint32 major, minor;
104*7f2fe78bSCy Schubert     size_t i;
105*7f2fe78bSCy Schubert 
106*7f2fe78bSCy Schubert     major = gss_indicate_mechs(&minor, &mechs);
107*7f2fe78bSCy Schubert     check_gsserr("gss_indicate_mechs", major, minor);
108*7f2fe78bSCy Schubert     if (mechs->count > 0)
109*7f2fe78bSCy Schubert         dump_known_mech_attrs(mechs->elements);
110*7f2fe78bSCy Schubert 
111*7f2fe78bSCy Schubert     for (i = 0; i < mechs->count; i++) {
112*7f2fe78bSCy Schubert         gss_buffer_desc oidstr = GSS_C_EMPTY_BUFFER;
113*7f2fe78bSCy Schubert         gss_buffer_desc sasl_mech_name = GSS_C_EMPTY_BUFFER;
114*7f2fe78bSCy Schubert         gss_buffer_desc mech_name = GSS_C_EMPTY_BUFFER;
115*7f2fe78bSCy Schubert         gss_buffer_desc mech_description = GSS_C_EMPTY_BUFFER;
116*7f2fe78bSCy Schubert         gss_OID oid = GSS_C_NO_OID;
117*7f2fe78bSCy Schubert 
118*7f2fe78bSCy Schubert         major = gss_oid_to_str(&minor, &mechs->elements[i], &oidstr);
119*7f2fe78bSCy Schubert         if (GSS_ERROR(major))
120*7f2fe78bSCy Schubert             continue;
121*7f2fe78bSCy Schubert 
122*7f2fe78bSCy Schubert         major = gss_inquire_saslname_for_mech(&minor, &mechs->elements[i],
123*7f2fe78bSCy Schubert                                               &sasl_mech_name, &mech_name,
124*7f2fe78bSCy Schubert                                               &mech_description);
125*7f2fe78bSCy Schubert         if (GSS_ERROR(major)) {
126*7f2fe78bSCy Schubert             gss_release_buffer(&minor, &oidstr);
127*7f2fe78bSCy Schubert             continue;
128*7f2fe78bSCy Schubert         }
129*7f2fe78bSCy Schubert 
130*7f2fe78bSCy Schubert         printf("-------------------------------------------------------------"
131*7f2fe78bSCy Schubert                "-----------------\n");
132*7f2fe78bSCy Schubert         printf("OID        : %.*s\n", (int)oidstr.length,
133*7f2fe78bSCy Schubert                (char *)oidstr.value);
134*7f2fe78bSCy Schubert         printf("SASL mech  : %.*s\n", (int)sasl_mech_name.length,
135*7f2fe78bSCy Schubert                (char *)sasl_mech_name.value);
136*7f2fe78bSCy Schubert         printf("Mech name  : %.*s\n", (int)mech_name.length,
137*7f2fe78bSCy Schubert                (char *)mech_name.value);
138*7f2fe78bSCy Schubert         printf("Mech desc  : %.*s\n", (int)mech_description.length,
139*7f2fe78bSCy Schubert                (char *)mech_description.value);
140*7f2fe78bSCy Schubert         dump_mech_attrs(&mechs->elements[i]);
141*7f2fe78bSCy Schubert         printf("-------------------------------------------------------------"
142*7f2fe78bSCy Schubert                "-----------------\n");
143*7f2fe78bSCy Schubert 
144*7f2fe78bSCy Schubert         major = gss_inquire_mech_for_saslname(&minor, &sasl_mech_name, &oid);
145*7f2fe78bSCy Schubert         check_gsserr("gss_inquire_mech_for_saslname", major, minor);
146*7f2fe78bSCy Schubert 
147*7f2fe78bSCy Schubert         if (oid == GSS_C_NO_OID ||
148*7f2fe78bSCy Schubert             (oid->length != mechs->elements[i].length &&
149*7f2fe78bSCy Schubert              memcmp(oid->elements, mechs->elements[i].elements,
150*7f2fe78bSCy Schubert                     oid->length) != 0)) {
151*7f2fe78bSCy Schubert             (void)gss_release_buffer(&minor, &oidstr);
152*7f2fe78bSCy Schubert             (void)gss_oid_to_str(&minor, oid, &oidstr);
153*7f2fe78bSCy Schubert             fprintf(stderr, "Got different OID %.*s for mechanism %.*s\n",
154*7f2fe78bSCy Schubert                     (int)oidstr.length, (char *)oidstr.value,
155*7f2fe78bSCy Schubert                     (int)sasl_mech_name.length, (char *)sasl_mech_name.value);
156*7f2fe78bSCy Schubert         }
157*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &oidstr);
158*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &sasl_mech_name);
159*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &mech_name);
160*7f2fe78bSCy Schubert         (void)gss_release_buffer(&minor, &mech_description);
161*7f2fe78bSCy Schubert     }
162*7f2fe78bSCy Schubert 
163*7f2fe78bSCy Schubert     (void)gss_release_oid_set(&minor, &mechs);
164*7f2fe78bSCy Schubert     return 0;
165*7f2fe78bSCy Schubert }
166