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