xref: /freebsd/crypto/krb5/src/tests/gssapi/t_export_name.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/gssapi/t_export_name.c - Test program for gss_export_name behavior */
3 /*
4  * Copyright 2012 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 /*
28  * Test program for gss_export_name, intended to be run from a Python test
29  * script.  Imports a name, canonicalizes it to a mech, exports it,
30  * re-imports/exports it to compare results, and then prints the hex form of
31  * the exported name followed by a newline.
32  *
33  * Usage: ./t_export_name [-k|-s] user:username|krb5:princ|host:service@host
34  *
35  * The name is imported as a username, krb5 principal, or hostbased name.
36  * By default or with -k, the name is canonicalized to the krb5 mech; -s
37  * indicates SPNEGO instead.
38  */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "common.h"
45 
46 static void
usage(void)47 usage(void)
48 {
49     fprintf(stderr, "Usage: t_export_name [-k|-s] name\n");
50     exit(1);
51 }
52 
53 int
main(int argc,char * argv[])54 main(int argc, char *argv[])
55 {
56     OM_uint32 minor, major;
57     gss_OID mech = (gss_OID)gss_mech_krb5;
58     gss_name_t name, mechname, impname;
59     gss_buffer_desc buf, buf2;
60     krb5_boolean use_composite = FALSE;
61     gss_OID ntype;
62     const char *name_arg;
63     char opt;
64 
65     /* Parse arguments. */
66     while (argc > 1 && argv[1][0] == '-') {
67         opt = argv[1][1];
68         argc--, argv++;
69         if (opt == 'k')
70             mech = &mech_krb5;
71         else if (opt == 's')
72             mech = &mech_spnego;
73         else if (opt == 'c')
74             use_composite = TRUE;
75         else
76             usage();
77     }
78     if (argc != 2)
79         usage();
80     name_arg = argv[1];
81 
82     /* Import the name. */
83     name = import_name(name_arg);
84 
85     /* Canonicalize and export the name. */
86     major = gss_canonicalize_name(&minor, name, mech, &mechname);
87     check_gsserr("gss_canonicalize_name", major, minor);
88     if (use_composite)
89         major = gss_export_name_composite(&minor, mechname, &buf);
90     else
91         major = gss_export_name(&minor, mechname, &buf);
92     check_gsserr("gss_export_name", major, minor);
93 
94     /* Import and re-export the name, and compare the results. */
95     ntype = use_composite ? GSS_C_NT_COMPOSITE_EXPORT : GSS_C_NT_EXPORT_NAME;
96     major = gss_import_name(&minor, &buf, ntype, &impname);
97     check_gsserr("gss_import_name", major, minor);
98     if (use_composite)
99         major = gss_export_name_composite(&minor, impname, &buf2);
100     else
101         major = gss_export_name(&minor, impname, &buf2);
102     check_gsserr("gss_export_name", major, minor);
103     if (buf.length != buf2.length ||
104         memcmp(buf.value, buf2.value, buf.length) != 0) {
105         fprintf(stderr, "Mismatched results:\n");
106         print_hex(stderr, &buf);
107         print_hex(stderr, &buf2);
108         return 1;
109     }
110 
111     print_hex(stdout, &buf);
112 
113     (void)gss_release_name(&minor, &name);
114     (void)gss_release_name(&minor, &mechname);
115     (void)gss_release_name(&minor, &impname);
116     (void)gss_release_buffer(&minor, &buf);
117     (void)gss_release_buffer(&minor, &buf2);
118     return 0;
119 }
120