xref: /freebsd/crypto/heimdal/lib/asn1/check-der.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * Copyright (c) 1999 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include <stdio.h>
38 #include <string.h>
39 #include <err.h>
40 #include <roken.h>
41 
42 #include <libasn1.h>
43 
44 RCSID("$Id: check-der.c,v 1.7 1999/12/02 17:05:01 joda Exp $");
45 
46 static void
47 print_bytes (unsigned const char *buf, size_t len)
48 {
49     int i;
50 
51     for (i = 0; i < len; ++i)
52 	printf ("%02x ", buf[i]);
53 }
54 
55 struct test_case {
56     void *val;
57     int byte_len;
58     const unsigned char *bytes;
59     char *name;
60 };
61 
62 static int
63 generic_test (const struct test_case *tests,
64 	      unsigned ntests,
65 	      size_t data_size,
66 	      int (*encode)(unsigned char *, size_t, void *, size_t *),
67 	      int (*length)(void *),
68 	      int (*decode)(unsigned char *, size_t, void *, size_t *),
69 	      int (*cmp)(void *a, void *b))
70 {
71     unsigned char buf[4711];
72     int i;
73     int failures = 0;
74     void *val = malloc (data_size);
75 
76     if (data_size != 0 && val == NULL)
77 	err (1, "malloc");
78 
79     for (i = 0; i < ntests; ++i) {
80 	int ret;
81 	size_t sz, consumed_sz, length_sz;
82 	unsigned char *beg;
83 
84 	ret = (*encode) (buf + sizeof(buf) - 1, sizeof(buf),
85 			 tests[i].val, &sz);
86 	beg = buf + sizeof(buf) - sz;
87 	if (ret != 0) {
88 	    printf ("encoding of %s failed\n", tests[i].name);
89 	    ++failures;
90 	}
91 	if (sz != tests[i].byte_len) {
92 	    printf ("encoding of %s has wrong len (%lu != %lu)\n",
93 		    tests[i].name,
94 		    (unsigned long)sz, (unsigned long)tests[i].byte_len);
95 	    ++failures;
96 	}
97 
98 	length_sz = (*length) (tests[i].val);
99 	if (sz != length_sz) {
100 	    printf ("length for %s is bad (%lu != %lu)\n",
101 		    tests[i].name, (unsigned long)length_sz, (unsigned long)sz);
102 	    ++failures;
103 	}
104 
105 	if (memcmp (beg, tests[i].bytes, tests[i].byte_len) != 0) {
106 	    printf ("encoding of %s has bad bytes:\n"
107 		    "correct: ", tests[i].name);
108 	    print_bytes (tests[i].bytes, tests[i].byte_len);
109 	    printf ("\nactual:  ");
110 	    print_bytes (beg, sz);
111 	    printf ("\n");
112 	    ++failures;
113 	}
114 	ret = (*decode) (beg, sz, val, &consumed_sz);
115 	if (ret != 0) {
116 	    printf ("decoding of %s failed\n", tests[i].name);
117 	    ++failures;
118 	}
119 	if (sz != consumed_sz) {
120 	    printf ("different length decoding %s (%ld != %ld)\n",
121 		    tests[i].name,
122 		    (unsigned long)sz, (unsigned long)consumed_sz);
123 	    ++failures;
124 	}
125 	if ((*cmp)(val, tests[i].val) != 0) {
126 	    printf ("%s: comparison failed\n", tests[i].name);
127 	    ++failures;
128 	}
129     }
130     free (val);
131     return failures;
132 }
133 
134 static int
135 cmp_integer (void *a, void *b)
136 {
137     int *ia = (int *)a;
138     int *ib = (int *)b;
139 
140     return *ib - *ia;
141 }
142 
143 static int
144 test_integer (void)
145 {
146     struct test_case tests[] = {
147 	{NULL, 3, "\x02\x01\x00"},
148 	{NULL, 3, "\x02\x01\x7f"},
149 	{NULL, 4, "\x02\x02\x00\x80"},
150 	{NULL, 4, "\x02\x02\x01\x00"},
151 	{NULL, 3, "\x02\x01\x80"},
152 	{NULL, 4, "\x02\x02\xff\x7f"},
153 	{NULL, 3, "\x02\x01\xff"},
154 	{NULL, 4, "\x02\x02\xff\x01"},
155 	{NULL, 4, "\x02\x02\x00\xff"},
156 	{NULL, 6, "\x02\x04\x80\x00\x00\x00"},
157 	{NULL, 6, "\x02\x04\x7f\xff\xff\xff"}
158     };
159 
160     int values[] = {0, 127, 128, 256, -128, -129, -1, -255, 255,
161 		    0x80000000, 0x7fffffff};
162     int i;
163     int ntests = sizeof(tests) / sizeof(*tests);
164 
165     for (i = 0; i < ntests; ++i) {
166 	tests[i].val = &values[i];
167 	asprintf (&tests[i].name, "integer %d", values[i]);
168     }
169 
170     return generic_test (tests, ntests, sizeof(int),
171 			 (int (*)(unsigned char *, size_t,
172 				  void *, size_t *))encode_integer,
173 			 (int (*)(void *))length_integer,
174 			 (int (*)(unsigned char *, size_t,
175 				  void *, size_t *))decode_integer,
176 			 cmp_integer);
177 }
178 
179 static int
180 cmp_octet_string (void *a, void *b)
181 {
182     octet_string *oa = (octet_string *)a;
183     octet_string *ob = (octet_string *)b;
184 
185     if (oa->length != ob->length)
186 	return ob->length - oa->length;
187 
188     return (memcmp (oa->data, ob->data, oa->length));
189 }
190 
191 static int
192 test_octet_string (void)
193 {
194     octet_string s1 = {8, "\x01\x23\x45\x67\x89\xab\xcd\xef"};
195 
196     struct test_case tests[] = {
197 	{NULL, 10, "\x04\x08\x01\x23\x45\x67\x89\xab\xcd\xef"}
198     };
199     int ntests = sizeof(tests) / sizeof(*tests);
200 
201     tests[0].val = &s1;
202     asprintf (&tests[0].name, "a octet string");
203 
204     return generic_test (tests, ntests, sizeof(octet_string),
205 			 (int (*)(unsigned char *, size_t,
206 				  void *, size_t *))encode_octet_string,
207 			 (int (*)(void *))length_octet_string,
208 			 (int (*)(unsigned char *, size_t,
209 				  void *, size_t *))decode_octet_string,
210 			 cmp_octet_string);
211 }
212 
213 static int
214 cmp_general_string (void *a, void *b)
215 {
216     unsigned char **sa = (unsigned char **)a;
217     unsigned char **sb = (unsigned char **)b;
218 
219     return strcmp (*sa, *sb);
220 }
221 
222 static int
223 test_general_string (void)
224 {
225     unsigned char *s1 = "Test User 1";
226 
227     struct test_case tests[] = {
228 	{NULL, 13, "\x1b\x0b\x54\x65\x73\x74\x20\x55\x73\x65\x72\x20\x31"}
229     };
230     int ntests = sizeof(tests) / sizeof(*tests);
231 
232     tests[0].val = &s1;
233     asprintf (&tests[0].name, "the string \"%s\"", s1);
234 
235     return generic_test (tests, ntests, sizeof(unsigned char *),
236 			 (int (*)(unsigned char *, size_t,
237 				  void *, size_t *))encode_general_string,
238 			 (int (*)(void *))length_general_string,
239 			 (int (*)(unsigned char *, size_t,
240 				  void *, size_t *))decode_general_string,
241 			 cmp_general_string);
242 }
243 
244 static int
245 cmp_generalized_time (void *a, void *b)
246 {
247     time_t *ta = (time_t *)a;
248     time_t *tb = (time_t *)b;
249 
250     return *tb - *ta;
251 }
252 
253 static int
254 test_generalized_time (void)
255 {
256     struct test_case tests[] = {
257 	{NULL, 17, "\x18\x0f""19700101000000Z"},
258 	{NULL, 17, "\x18\x0f""19851106210627Z"}
259     };
260     time_t values[] = {0, 500159187};
261     int i;
262     int ntests = sizeof(tests) / sizeof(*tests);
263 
264     for (i = 0; i < ntests; ++i) {
265 	tests[i].val = &values[i];
266 	asprintf (&tests[i].name, "time %d", (int)values[i]);
267     }
268 
269     return generic_test (tests, ntests, sizeof(time_t),
270 			 (int (*)(unsigned char *, size_t,
271 				  void *, size_t *))encode_generalized_time,
272 			 (int (*)(void *))length_generalized_time,
273 			 (int (*)(unsigned char *, size_t,
274 				  void *, size_t *))decode_generalized_time,
275 			 cmp_generalized_time);
276 }
277 
278 int
279 main(int argc, char **argv)
280 {
281     int ret = 0;
282 
283     ret += test_integer ();
284     ret += test_octet_string ();
285     ret += test_general_string ();
286     ret += test_generalized_time ();
287 
288     return ret;
289 }
290