xref: /freebsd/crypto/krb5/src/kdc/t_ndr.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* kdc/t_ndr.c - tests for ndr.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2021 by Red Hat, Inc.
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 /*
34*7f2fe78bSCy Schubert  * Unit tests for the NDR marshalling/unmarshalling in ndr.c
35*7f2fe78bSCy Schubert  */
36*7f2fe78bSCy Schubert 
37*7f2fe78bSCy Schubert #include "k5-int.h"
38*7f2fe78bSCy Schubert #include "kdc_util.h"
39*7f2fe78bSCy Schubert 
40*7f2fe78bSCy Schubert /*
41*7f2fe78bSCy Schubert  * Three S4U_DELEGATION_INFO buffers decoded from communication with AD 2019:
42*7f2fe78bSCy Schubert  *
43*7f2fe78bSCy Schubert  * - svc1/adserver.ad.test@AD.TEST to svc2/adserver.ad.test@AD.TEST
44*7f2fe78bSCy Schubert  * - svc1/adserver.ad.test@AD.TEST to longsvc/adserver.ad.test@AD.TEST
45*7f2fe78bSCy Schubert  * - svc1/adserver.ad.test@AD.TEST to svc2/adserver.ad.test@AD.TEST then
46*7f2fe78bSCy Schubert  *                                 to longsvc/adserver.ad.test@AD.TEST
47*7f2fe78bSCy Schubert  */
48*7f2fe78bSCy Schubert static uint8_t s4u_di_short[] = {
49*7f2fe78bSCy Schubert     0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0xa0, 0x00, 0x00, 0x00,
50*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x2a, 0x00, 0x2c, 0x00,
51*7f2fe78bSCy Schubert     0x04, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00,
52*7f2fe78bSCy Schubert     0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
53*7f2fe78bSCy Schubert     0x73, 0x00, 0x76, 0x00, 0x63, 0x00, 0x32, 0x00, 0x2f, 0x00, 0x61, 0x00,
54*7f2fe78bSCy Schubert     0x64, 0x00, 0x73, 0x00, 0x65, 0x00, 0x72, 0x00, 0x76, 0x00, 0x65, 0x00,
55*7f2fe78bSCy Schubert     0x72, 0x00, 0x2e, 0x00, 0x61, 0x00, 0x64, 0x00, 0x2e, 0x00, 0x74, 0x00,
56*7f2fe78bSCy Schubert     0x65, 0x00, 0x73, 0x00, 0x74, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
57*7f2fe78bSCy Schubert     0x3a, 0x00, 0x3c, 0x00, 0x0c, 0x00, 0x02, 0x00, 0x1e, 0x00, 0x00, 0x00,
58*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x73, 0x00, 0x76, 0x00,
59*7f2fe78bSCy Schubert     0x63, 0x00, 0x31, 0x00, 0x2f, 0x00, 0x61, 0x00, 0x64, 0x00, 0x73, 0x00,
60*7f2fe78bSCy Schubert     0x65, 0x00, 0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00,
61*7f2fe78bSCy Schubert     0x61, 0x00, 0x64, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00,
62*7f2fe78bSCy Schubert     0x74, 0x00, 0x40, 0x00, 0x41, 0x00, 0x44, 0x00, 0x2e, 0x00, 0x54, 0x00,
63*7f2fe78bSCy Schubert     0x45, 0x00, 0x53, 0x00, 0x54, 0x00, 0x00, 0x00,
64*7f2fe78bSCy Schubert };
65*7f2fe78bSCy Schubert 
66*7f2fe78bSCy Schubert static uint8_t s4u_di_long[] = {
67*7f2fe78bSCy Schubert     0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0xa8, 0x00, 0x00, 0x00,
68*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x30, 0x00, 0x32, 0x00,
69*7f2fe78bSCy Schubert     0x04, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00,
70*7f2fe78bSCy Schubert     0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
71*7f2fe78bSCy Schubert     0x6c, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x67, 0x00, 0x73, 0x00, 0x76, 0x00,
72*7f2fe78bSCy Schubert     0x63, 0x00, 0x2f, 0x00, 0x61, 0x00, 0x64, 0x00, 0x73, 0x00, 0x65, 0x00,
73*7f2fe78bSCy Schubert     0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00, 0x61, 0x00,
74*7f2fe78bSCy Schubert     0x64, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00, 0x74, 0x00,
75*7f2fe78bSCy Schubert     0x01, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x3c, 0x00, 0x0c, 0x00, 0x02, 0x00,
76*7f2fe78bSCy Schubert     0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00,
77*7f2fe78bSCy Schubert     0x73, 0x00, 0x76, 0x00, 0x63, 0x00, 0x31, 0x00, 0x2f, 0x00, 0x61, 0x00,
78*7f2fe78bSCy Schubert     0x64, 0x00, 0x73, 0x00, 0x65, 0x00, 0x72, 0x00, 0x76, 0x00, 0x65, 0x00,
79*7f2fe78bSCy Schubert     0x72, 0x00, 0x2e, 0x00, 0x61, 0x00, 0x64, 0x00, 0x2e, 0x00, 0x74, 0x00,
80*7f2fe78bSCy Schubert     0x65, 0x00, 0x73, 0x00, 0x74, 0x00, 0x40, 0x00, 0x41, 0x00, 0x44, 0x00,
81*7f2fe78bSCy Schubert     0x2e, 0x00, 0x54, 0x00, 0x45, 0x00, 0x53, 0x00, 0x54, 0x00, 0x00, 0x00,
82*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x00,
83*7f2fe78bSCy Schubert };
84*7f2fe78bSCy Schubert 
85*7f2fe78bSCy Schubert static uint8_t s4u_di_double[] = {
86*7f2fe78bSCy Schubert     0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0xf8, 0x00, 0x00, 0x00,
87*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x30, 0x00, 0x32, 0x00,
88*7f2fe78bSCy Schubert     0x04, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00,
89*7f2fe78bSCy Schubert     0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
90*7f2fe78bSCy Schubert     0x6c, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x67, 0x00, 0x73, 0x00, 0x76, 0x00,
91*7f2fe78bSCy Schubert     0x63, 0x00, 0x2f, 0x00, 0x61, 0x00, 0x64, 0x00, 0x73, 0x00, 0x65, 0x00,
92*7f2fe78bSCy Schubert     0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00, 0x61, 0x00,
93*7f2fe78bSCy Schubert     0x64, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00, 0x74, 0x00,
94*7f2fe78bSCy Schubert     0x02, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x3c, 0x00, 0x0c, 0x00, 0x02, 0x00,
95*7f2fe78bSCy Schubert     0x3a, 0x00, 0x3c, 0x00, 0x10, 0x00, 0x02, 0x00, 0x1e, 0x00, 0x00, 0x00,
96*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x73, 0x00, 0x76, 0x00,
97*7f2fe78bSCy Schubert     0x63, 0x00, 0x31, 0x00, 0x2f, 0x00, 0x61, 0x00, 0x64, 0x00, 0x73, 0x00,
98*7f2fe78bSCy Schubert     0x65, 0x00, 0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00,
99*7f2fe78bSCy Schubert     0x61, 0x00, 0x64, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00,
100*7f2fe78bSCy Schubert     0x74, 0x00, 0x40, 0x00, 0x41, 0x00, 0x44, 0x00, 0x2e, 0x00, 0x54, 0x00,
101*7f2fe78bSCy Schubert     0x45, 0x00, 0x53, 0x00, 0x54, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
102*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x73, 0x00, 0x76, 0x00,
103*7f2fe78bSCy Schubert     0x63, 0x00, 0x32, 0x00, 0x2f, 0x00, 0x61, 0x00, 0x64, 0x00, 0x73, 0x00,
104*7f2fe78bSCy Schubert     0x65, 0x00, 0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00,
105*7f2fe78bSCy Schubert     0x61, 0x00, 0x64, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00,
106*7f2fe78bSCy Schubert     0x74, 0x00, 0x40, 0x00, 0x41, 0x00, 0x44, 0x00, 0x2e, 0x00, 0x54, 0x00,
107*7f2fe78bSCy Schubert     0x45, 0x00, 0x53, 0x00, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108*7f2fe78bSCy Schubert };
109*7f2fe78bSCy Schubert 
110*7f2fe78bSCy Schubert static uint8_t fuzz1[] = {
111*7f2fe78bSCy Schubert     0x01, 0x10, 0x08, 0x20, 0x20, 0x20, 0x20, 0x20, 0x24, 0x00, 0x00, 0x00,
112*7f2fe78bSCy Schubert     0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
113*7f2fe78bSCy Schubert     0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0xff, 0xff, 0xff,
114*7f2fe78bSCy Schubert     0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00,
115*7f2fe78bSCy Schubert     0x20, 0x20, 0x20, 0x20
116*7f2fe78bSCy Schubert };
117*7f2fe78bSCy Schubert 
118*7f2fe78bSCy Schubert static uint8_t fuzz2[] = {
119*7f2fe78bSCy Schubert     0x01, 0x10, 0x08, 0x00, 0x00, 0xff, 0xff, 0xff, 0x24, 0x00, 0x00, 0x00,
120*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121*7f2fe78bSCy Schubert     0x00, 0x1e, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x16, 0x00, 0x00,
122*7f2fe78bSCy Schubert     0x1e, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123*7f2fe78bSCy Schubert     0x00, 0x00, 0x00, 0x1e
124*7f2fe78bSCy Schubert };
125*7f2fe78bSCy Schubert 
126*7f2fe78bSCy Schubert static void
test_dec_enc(uint8_t * blob,size_t len,char * name,int fail)127*7f2fe78bSCy Schubert test_dec_enc(uint8_t *blob, size_t len, char *name, int fail)
128*7f2fe78bSCy Schubert {
129*7f2fe78bSCy Schubert     krb5_data data_in, data_out;
130*7f2fe78bSCy Schubert     struct pac_s4u_delegation_info *di = NULL;
131*7f2fe78bSCy Schubert     krb5_error_code ret;
132*7f2fe78bSCy Schubert     int eq;
133*7f2fe78bSCy Schubert     size_t i;
134*7f2fe78bSCy Schubert 
135*7f2fe78bSCy Schubert     printf("Checking blob %s...\n", name);
136*7f2fe78bSCy Schubert 
137*7f2fe78bSCy Schubert     data_in = make_data(blob, len);
138*7f2fe78bSCy Schubert     ret = ndr_dec_delegation_info(&data_in, &di);
139*7f2fe78bSCy Schubert     if (fail) {
140*7f2fe78bSCy Schubert         if (!ret) {
141*7f2fe78bSCy Schubert             printf("%s: unexpected decode success\n", name);
142*7f2fe78bSCy Schubert             exit(1);
143*7f2fe78bSCy Schubert         }
144*7f2fe78bSCy Schubert         printf("%s: failed as expected\n", name);
145*7f2fe78bSCy Schubert         return;
146*7f2fe78bSCy Schubert     } else if (ret) {
147*7f2fe78bSCy Schubert         printf("%s: bad decode (%d): %s\n", name, ret, strerror(ret));
148*7f2fe78bSCy Schubert         exit(1);
149*7f2fe78bSCy Schubert     }
150*7f2fe78bSCy Schubert 
151*7f2fe78bSCy Schubert     printf("%s, %d\n", di->proxy_target, di->transited_services_length);
152*7f2fe78bSCy Schubert     for (i = 0; i < di->transited_services_length; i++)
153*7f2fe78bSCy Schubert         printf("    %s\n", di->transited_services[i]);
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     ret = ndr_enc_delegation_info(di, &data_out);
156*7f2fe78bSCy Schubert     ndr_free_delegation_info(di);
157*7f2fe78bSCy Schubert     if (ret) {
158*7f2fe78bSCy Schubert         printf("%s: bad encode (%d): %s\n", name, ret, strerror(ret));
159*7f2fe78bSCy Schubert         exit(1);
160*7f2fe78bSCy Schubert     }
161*7f2fe78bSCy Schubert 
162*7f2fe78bSCy Schubert     eq = data_eq(data_in, data_out);
163*7f2fe78bSCy Schubert     krb5_free_data_contents(NULL, &data_out);
164*7f2fe78bSCy Schubert     if (!eq) {
165*7f2fe78bSCy Schubert         printf("%s: re-encoding did not produce the same result\n", name);
166*7f2fe78bSCy Schubert         exit(1);
167*7f2fe78bSCy Schubert     }
168*7f2fe78bSCy Schubert 
169*7f2fe78bSCy Schubert     printf("%s matched\n\n", name);
170*7f2fe78bSCy Schubert }
171*7f2fe78bSCy Schubert 
172*7f2fe78bSCy Schubert #define RUN_TEST(blob) test_dec_enc(blob, sizeof(blob), #blob, 0)
173*7f2fe78bSCy Schubert #define RUN_TEST_FAIL(blob) test_dec_enc(blob, sizeof(blob), #blob, 1)
174*7f2fe78bSCy Schubert 
175*7f2fe78bSCy Schubert int
main()176*7f2fe78bSCy Schubert main()
177*7f2fe78bSCy Schubert {
178*7f2fe78bSCy Schubert     printf("Running NDR tests...\n");
179*7f2fe78bSCy Schubert 
180*7f2fe78bSCy Schubert     RUN_TEST(s4u_di_short);
181*7f2fe78bSCy Schubert     RUN_TEST(s4u_di_long);
182*7f2fe78bSCy Schubert     RUN_TEST(s4u_di_double);
183*7f2fe78bSCy Schubert     RUN_TEST_FAIL(fuzz1);
184*7f2fe78bSCy Schubert     RUN_TEST_FAIL(fuzz2);
185*7f2fe78bSCy Schubert 
186*7f2fe78bSCy Schubert     printf("Passed NDR tests\n");
187*7f2fe78bSCy Schubert     return 0;
188*7f2fe78bSCy Schubert }
189