xref: /freebsd/crypto/krb5/src/tests/gssapi/t_oid.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/gssapi/t_oid.c - Test OID manipulation functions */
3 /*
4  * Copyright (C) 2012 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "common.h"
38 
39 static struct {
40     char *canonical;
41     char *variant;
42     gss_OID_desc oid;
43 } tests[] = {
44     /* GSS_C_NT_USER_NAME */
45     { "{ 1 2 840 113554 1 2 1 1 }", "1.2.840.113554.1.2.1.1",
46       { 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x01\x01" } },
47     /* GSS_C_NT_MACHINE_UID_NAME */
48     { "{ 1 2 840 113554 1 2 1 2 }", "1 2 840 113554 1 2 1 2",
49       { 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x01\x02" } },
50     /* GSS_C_NT_STRING_UID_NAME */
51     { "{ 1 2 840 113554 1 2 1 3 }", "{1 2 840 113554 1 2 1 3}",
52       { 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x01\x03" } },
53     /* GSS_C_NT_HOSTBASED_SERVICE_X */
54     { "{ 1 3 6 1 5 6 2 }", "{  1  3  6  1  5  6  2  }",
55       { 6, "\x2B\x06\x01\x05\x06\x02" } },
56     /* GSS_C_NT_ANONYMOUS */
57     { "{ 1 3 6 1 5 6 3 }", "{ 01 03 06 01 05 06 03 }",
58       { 6, "\x2B\x06\x01\x05\x06\x03" } },
59     /* GSS_KRB5_NT_PRINCIPAL_NAME */
60     { "{ 1 2 840 113554 1 2 2 1 }", " {01 2 840 113554 1 2 2 1  } ",
61       { 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02\x01" } },
62     /* GSS_KRB5_NT_ENTERPRISE_NAME */
63     { "{ 1 2 840 113554 1 2 2 6 }", " {1.2.840.113554.1.2.2.6} ",
64       { 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02\x06" } },
65     /* gss_krb5_nt_principal */
66     { "{ 1 2 840 113554 1 2 2 2 }", "{1.2.840.113554.1.2.2.2}",
67       { 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02\x02" } },
68     /* gss_mech_krb5 */
69     { "{ 1 2 840 113554 1 2 2 }", "{ 1.2.840.113554.1.2.2 }",
70       { 9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02" } },
71     /* gss_mech_krb5_old */
72     { "{ 1 3 5 1 5 2 }", "001 . 003 . 005 . 001 . 005 . 002",
73       { 5, "\x2B\x05\x01\x05\x02" } },
74     /* gss_mech_krb5_wrong */
75     { "{ 1 2 840 48018 1 2 2 }", "1.2.840.48018.1.2.2 trailing garbage",
76       { 9, "\x2A\x86\x48\x82\xF7\x12\x01\x02\x02" } },
77     /* gss_mech_iakerb */
78     { "{ 1 3 6 1 5 2 5 }", "{ 1 3 6 1 5 2 5 } trailing garbage",
79       { 6, "\x2B\x06\x01\x05\x02\x05" } },
80     /* SPNEGO */
81     { "{ 1 3 6 1 5 5 2 }", "{1 3 6 1 5 5 2} trailing garbage",
82       { 6, "\x2B\x06\x01\x05\x05\x02" } },
83     /* Edge cases for the first two arcs */
84     { "{ 0 0 }", NULL, { 1, "\x00" } },
85     { "{ 0 39 }", NULL, { 1, "\x27" } },
86     { "{ 1 0 }", NULL, { 1, "\x28" } },
87     { "{ 1 39 }", NULL, { 1, "\x4F" } },
88     { "{ 2 0 }", NULL, { 1, "\x50" } },
89     { "{ 2 40 }", NULL, { 1, "\x78" } },
90     { "{ 2 47 }", NULL, { 1, "\x7F" } },
91     { "{ 2 48 }", NULL, { 2, "\x81\x00" } },
92     { "{ 2 16304 }", NULL, { 3, "\x81\x80\x00" } },
93     /* Zero-valued arcs */
94     { "{ 0 0 0 }", NULL, { 2, "\x00\x00" } },
95     { "{ 0 0 1 0 }", NULL, { 3, "\x00\x01\x00" } },
96     { "{ 0 0 128 0 }", NULL, { 4, "\x00\x81\x00\x00 " } },
97     { "{ 0 0 0 1 }", NULL, { 3, "\x00\x00\x01" } },
98     { "{ 0 0 128 0 1 0 128 }", NULL,
99       { 8, "\x00\x81\x00\x00\x01\x00\x81\x00 " } }
100 };
101 
102 static char *invalid_strings[] = {
103     "",
104     "{}",
105     "{",
106     "}",
107     "  ",
108     " { } ",
109     "x",
110     "+1 1",
111     "-1.1",
112     "1.+0",
113     "+0.1",
114     "{ 1 garbage }",
115     "{ 1 }",
116     "{ 0 40 }",
117     "{ 1 40 }",
118     "{ 1 128 }",
119     "{ 1 1",
120     "{ 1 2 3 4 +5 }",
121     "{ 1.2.-3.4.5 }"
122 };
123 
124 static int
oid_equal(gss_OID o1,gss_OID o2)125 oid_equal(gss_OID o1, gss_OID o2)
126 {
127     return o1->length == o2->length &&
128         memcmp(o1->elements, o2->elements, o1->length) == 0;
129 }
130 
131 int
main()132 main()
133 {
134     size_t i;
135     OM_uint32 major, minor;
136     gss_buffer_desc buf;
137     gss_OID oid;
138     gss_OID_set set;
139     int status = 0, present;
140 
141     for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
142         /* Check that this test's OID converts to its canonical string form. */
143         major = gss_oid_to_str(&minor, &tests[i].oid, &buf);
144         check_gsserr("gss_oid_to_str", major, minor);
145         if (buf.length != strlen(tests[i].canonical) + 1 ||
146             memcmp(buf.value, tests[i].canonical, buf.length) != 0) {
147             status = 1;
148             printf("test %d: OID converts to %.*s, wanted %s\n", (int)i,
149                    (int)buf.length, (char *)buf.value, tests[i].canonical);
150         }
151         (void)gss_release_buffer(&minor, &buf);
152 
153         /* Check that this test's canonical string form converts to its OID. */
154         buf.value = tests[i].canonical;
155         buf.length = strlen(tests[i].canonical);
156         major = gss_str_to_oid(&minor, &buf, &oid);
157         check_gsserr("gss_str_to_oid", major, minor);
158         if (!oid_equal(oid, &tests[i].oid)) {
159             status = 1;
160             printf("test %d: %s converts to wrong OID\n", (int)i,
161                    tests[i].canonical);
162             display_oid("wanted", &tests[i].oid);
163             display_oid("actual", oid);
164         }
165         (void)gss_release_oid(&minor, &oid);
166 
167         /* Check that this test's variant string form converts to its OID. */
168         if (tests[i].variant == NULL)
169             continue;
170         buf.value = tests[i].variant;
171         buf.length = strlen(tests[i].variant);
172         major = gss_str_to_oid(&minor, &buf, &oid);
173         check_gsserr("gss_str_to_oid", major, minor);
174         if (!oid_equal(oid, &tests[i].oid)) {
175             status = 1;
176             printf("test %d: %s converts to wrong OID\n", (int)i,
177                    tests[i].variant);
178             display_oid("wanted", &tests[i].oid);
179             display_oid("actual", oid);
180         }
181         (void)gss_release_oid(&minor, &oid);
182     }
183 
184     for (i = 0; i < sizeof(invalid_strings) / sizeof(*invalid_strings); i++) {
185         buf.value = invalid_strings[i];
186         buf.length = strlen(invalid_strings[i]);
187         major = gss_str_to_oid(&minor, &buf, &oid);
188         if (major == GSS_S_COMPLETE) {
189             status = 1;
190             printf("invalid %d: %s converted when it should not have\n",
191                    (int)i, invalid_strings[i]);
192             (void)gss_release_oid(&minor, &oid);
193         }
194     }
195 
196     major = gss_create_empty_oid_set(&minor, &set);
197     check_gsserr("gss_create_empty_oid_set", major, minor);
198     for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
199         major = gss_add_oid_set_member(&minor, &tests[i].oid, &set);
200         check_gsserr("gss_add_oid_set_member", major, minor);
201     }
202     if (set->count != i) {
203         status = 1;
204         printf("oid set has wrong size: wanted %d, actual %d\n", (int)i,
205                (int)set->count);
206     }
207     for (i = 0; i < set->count; i++) {
208         if (!oid_equal(&set->elements[i], &tests[i].oid)) {
209             status = 1;
210             printf("oid set has wrong element %d\n", (int)i);
211             display_oid("wanted", &tests[i].oid);
212             display_oid("actual", &set->elements[i]);
213         }
214         major = gss_test_oid_set_member(&minor, &tests[i].oid, set, &present);
215         check_gsserr("gss_test_oid_set_member", major, minor);
216         if (!present) {
217             status = 1;
218             printf("oid set does not contain OID %d\n", (int)i);
219             display_oid("wanted", &tests[i].oid);
220         }
221     }
222     (void)gss_release_oid_set(&minor, &set);
223     return status;
224 }
225