summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/regress/lib/libcrypto/Makefile3
-rw-r--r--src/regress/lib/libcrypto/objects/Makefile9
-rw-r--r--src/regress/lib/libcrypto/objects/objectstest.c438
3 files changed, 449 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/Makefile b/src/regress/lib/libcrypto/Makefile
index 1c346f6e3b..f29e594535 100644
--- a/src/regress/lib/libcrypto/Makefile
+++ b/src/regress/lib/libcrypto/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.42 2021/12/05 13:01:08 jsing Exp $ 1# $OpenBSD: Makefile,v 1.43 2022/02/11 16:34:23 jsing Exp $
2 2
3SUBDIR += aead 3SUBDIR += aead
4SUBDIR += aeswrap 4SUBDIR += aeswrap
@@ -34,6 +34,7 @@ SUBDIR += ige
34SUBDIR += init 34SUBDIR += init
35SUBDIR += md4 35SUBDIR += md4
36SUBDIR += md5 36SUBDIR += md5
37SUBDIR += objects
37SUBDIR += pbkdf2 38SUBDIR += pbkdf2
38SUBDIR += pem 39SUBDIR += pem
39SUBDIR += pkcs7 40SUBDIR += pkcs7
diff --git a/src/regress/lib/libcrypto/objects/Makefile b/src/regress/lib/libcrypto/objects/Makefile
new file mode 100644
index 0000000000..363023e13c
--- /dev/null
+++ b/src/regress/lib/libcrypto/objects/Makefile
@@ -0,0 +1,9 @@
1# $OpenBSD: Makefile,v 1.1 2022/02/11 16:34:23 jsing Exp $
2
3PROG= objectstest
4LDADD= -lcrypto
5DPADD= ${LIBCRYPTO}
6WARNINGS= Yes
7CFLAGS+= -DLIBRESSL_INTERNAL -Werror
8
9.include <bsd.regress.mk>
diff --git a/src/regress/lib/libcrypto/objects/objectstest.c b/src/regress/lib/libcrypto/objects/objectstest.c
new file mode 100644
index 0000000000..1a9674cb48
--- /dev/null
+++ b/src/regress/lib/libcrypto/objects/objectstest.c
@@ -0,0 +1,438 @@
1/* $OpenBSD: objectstest.c,v 1.1 2022/02/11 16:34:23 jsing Exp $ */
2/*
3 * Copyright (c) 2017, 2022 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <openssl/objects.h>
19
20#include <err.h>
21#include <stdio.h>
22#include <string.h>
23
24static void
25hexdump(const unsigned char *buf, size_t len)
26{
27 size_t i;
28
29 for (i = 1; i <= len; i++)
30 fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n");
31
32 fprintf(stderr, "\n");
33}
34
35static int
36obj_compare_bytes(const char *label, const unsigned char *d1, int len1,
37 const unsigned char *d2, int len2)
38{
39 if (len1 != len2) {
40 fprintf(stderr, "FAIL: %s - byte lengths differ "
41 "(%i != %i)\n", label, len1, len2);
42 fprintf(stderr, "Got:\n");
43 hexdump(d1, len1);
44 fprintf(stderr, "Want:\n");
45 hexdump(d2, len2);
46 return 0;
47 }
48 if (memcmp(d1, d2, len1) != 0) {
49 fprintf(stderr, "FAIL: %s - bytes differ\n", label);
50 fprintf(stderr, "Got:\n");
51 hexdump(d1, len1);
52 fprintf(stderr, "Want:\n");
53 hexdump(d2, len2);
54 return 0;
55 }
56 return 1;
57}
58
59struct obj_test {
60 const char *oid;
61 const char *sn;
62 const char *ln;
63 int nid;
64 uint8_t data[255];
65 size_t data_len;
66};
67
68struct obj_test obj_tests[] = {
69 {
70 .oid = NULL,
71 .sn = "UNDEF",
72 .ln = "undefined",
73 .nid = NID_undef,
74 },
75 {
76 .oid = "2.5.4.10",
77 .sn = "O",
78 .ln = "organizationName",
79 .nid = NID_organizationName,
80 .data = {
81 0x55, 0x04, 0x0a,
82 },
83 .data_len = 3,
84 },
85 {
86 .oid = "2.5.4.8",
87 .sn = "ST",
88 .ln = "stateOrProvinceName",
89 .nid = NID_stateOrProvinceName,
90 .data = {
91 0x55, 0x04, 0x08,
92 },
93 .data_len = 3,
94 },
95 {
96 .oid = "1.3.6.1.4.1.11129.2.4.5",
97 .sn = "ct_cert_scts",
98 .ln = "CT Certificate SCTs",
99 .nid = NID_ct_cert_scts,
100 .data = {
101 0x2b, 0x06, 0x01, 0x04, 0x01, 0xd6, 0x79, 0x02,
102 0x04, 0x05,
103 },
104 .data_len = 10,
105 },
106 {
107 .oid = "1.3.6.1.4.1",
108 .sn = "enterprises",
109 .ln = "Enterprises",
110 .nid = NID_Enterprises,
111 .data = {
112 0x2b, 0x06, 0x01, 0x04, 0x01,
113 },
114 .data_len = 5,
115 },
116 {
117 .oid = "1.3.6.1.4.1.5454.1.70.6.11.2",
118 .nid = NID_undef,
119 .data = {
120 0x2b, 0x06, 0x01, 0x04, 0x01, 0xaa, 0x4e, 0x01,
121 0x46, 0x06, 0x0b, 0x02,
122 },
123 .data_len = 12,
124 },
125 {
126 .oid = "1.3.6.1.4.1.890.1.5.8.60.102.2",
127 .nid = NID_undef,
128 .data = {
129 0x2b, 0x06, 0x01, 0x04, 0x01, 0x86, 0x7a, 0x01,
130 0x05, 0x08, 0x3c, 0x66, 0x02,
131 },
132 .data_len = 13,
133 },
134 {
135 .oid = "1.3.6.1.4.1.173.7.3.4.1.1.26",
136 .nid = NID_undef,
137 .data = {
138 0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0x2d, 0x07,
139 0x03, 0x04, 0x01, 0x01, 0x1a,
140 },
141 .data_len = 13,
142 },
143};
144
145#define N_OBJ_TESTS (sizeof(obj_tests) / sizeof(*obj_tests))
146
147static int
148obj_name_test(struct obj_test *ot)
149{
150 const char *ln, *sn;
151 int nid;
152 int failed = 1;
153
154 if (ot->ln != NULL) {
155 if ((nid = OBJ_ln2nid(ot->ln)) != ot->nid) {
156 fprintf(stderr, "FAIL: OBJ_ln2nid() for '%s' = %d, "
157 "want %d\n", ot->ln, nid, ot->nid);
158 goto failed;
159 }
160 if ((ln = OBJ_nid2ln(ot->nid)) == NULL) {
161 fprintf(stderr, "FAIL: OBJ_nid2ln() for '%s' returned "
162 "NULL\n", ot->oid);
163 goto failed;
164 }
165 if (strcmp(ln, ot->ln) != 0) {
166 fprintf(stderr, "FAIL: OBJ_nid2ln() for '%s' = '%s', "
167 "want '%s'\n", ot->oid, ln, ot->ln);
168 goto failed;
169 }
170 }
171 if (ot->sn != NULL) {
172 if ((nid = OBJ_sn2nid(ot->sn)) != ot->nid) {
173 fprintf(stderr, "FAIL: OBJ_sn2nid() for '%s' = %d, "
174 "want %d\n", ot->sn, nid, ot->nid);
175 goto failed;
176 }
177 if ((sn = OBJ_nid2sn(ot->nid)) == NULL) {
178 fprintf(stderr, "FAIL: OBJ_nid2sn() for '%s' returned "
179 "NULL\n", ot->oid);
180 goto failed;
181 }
182 if (strcmp(sn, ot->sn) != 0) {
183 fprintf(stderr, "FAIL: OBJ_nid2sn() for '%s' = '%s', "
184 "want '%s'\n", ot->oid, sn, ot->sn);
185 goto failed;
186 }
187 }
188
189 failed = 0;
190
191 failed:
192 return failed;
193}
194
195static int
196obj_name_tests(void)
197{
198 int failed = 0;
199 size_t i;
200
201 for (i = 0; i < N_OBJ_TESTS; i++)
202 failed |= obj_name_test(&obj_tests[i]);
203
204 return failed;
205}
206
207static int
208obj_nid_test(struct obj_test *ot)
209{
210 ASN1_OBJECT *obj = NULL;
211 int nid;
212 int failed = 1;
213
214 if (ot->nid == NID_undef)
215 return 0;
216
217 if ((obj = OBJ_nid2obj(ot->nid)) == NULL) {
218 fprintf(stderr, "FAIL: OBJ_nid2obj() failed for '%s' (NID %d)\n",
219 ot->oid, ot->nid);
220 goto failed;
221 }
222 if ((nid = OBJ_obj2nid(obj)) != ot->nid) {
223 fprintf(stderr, "FAIL: OBJ_obj2nid() failed for '%s' - got %d, "
224 "want %d\n", ot->oid ? ot->oid : "undef", nid, ot->nid);
225 goto failed;
226 }
227
228 failed = 0;
229
230 failed:
231 ASN1_OBJECT_free(obj);
232
233 return failed;
234}
235
236static int
237obj_nid_tests(void)
238{
239 int failed = 0;
240 size_t i;
241
242 for (i = 0; i < N_OBJ_TESTS; i++)
243 failed |= obj_nid_test(&obj_tests[i]);
244
245 return failed;
246}
247
248static int
249obj_oid_test(struct obj_test *ot)
250{
251 ASN1_OBJECT *obj = NULL;
252 char buf[1024];
253 int len, nid;
254 int failed = 1;
255
256 if (ot->oid == NULL)
257 return 0;
258
259 /* XXX - need to also test with no_name == 0. */
260
261 if ((obj = OBJ_txt2obj(ot->oid, 1)) == NULL) {
262 fprintf(stderr, "FAIL: OBJ_txt2obj() failed for '%s'\n", ot->oid);
263 goto failed;
264 }
265 if ((nid = OBJ_txt2nid(ot->oid)) != ot->nid) {
266 fprintf(stderr, "FAIL: OBJ_txt2nid() failed for '%s', got %d "
267 "want %d\n", ot->oid, nid, ot->nid);
268 goto failed;
269 }
270
271 if (!obj_compare_bytes("object data", OBJ_get0_data(obj), OBJ_length(obj),
272 ot->data, ot->data_len))
273 goto failed;
274
275 len = OBJ_obj2txt(buf, sizeof(buf), obj, 1);
276 if (len <= 0 || (size_t)len >= sizeof(buf)) {
277 fprintf(stderr, "FAIL: OBJ_obj2txt() failed for '%s'\n", ot->oid);
278 goto failed;
279 }
280 if (strcmp(buf, ot->oid) != 0) {
281 fprintf(stderr, "FAIL: OBJ_obj2txt() returned '%s', want '%s'\n",
282 buf, ot->oid);
283 goto failed;
284 }
285
286 failed = 0;
287
288 failed:
289 ASN1_OBJECT_free(obj);
290
291 return failed;
292}
293
294static int
295obj_oid_tests(void)
296{
297 int failed = 0;
298 size_t i;
299
300 for (i = 0; i < N_OBJ_TESTS; i++)
301 failed |= obj_oid_test(&obj_tests[i]);
302
303 return failed;
304}
305
306/* OID 1.3.18446744073709551615 (64 bits). */
307const uint8_t asn1_large_oid1[] = {
308 0x06, 0x0b,
309 0x2b, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
310 0xff, 0xff, 0x7f,
311};
312
313/* OID 1.3.18446744073709551616 (65 bits). */
314const uint8_t asn1_large_oid2[] = {
315 0x06, 0x0b,
316 0x2b, 0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
317 0x80, 0x80, 0x00,
318};
319
320/* OID 1.3.340282366920938463463374607431768211455 (128 bits). */
321const uint8_t asn1_large_oid3[] = {
322 0x06, 0x14,
323 0x2b, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
324 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
325 0xff, 0xff, 0xff, 0x7f,
326};
327
328/* OID 1.3.115792089237316195423570985008687907853269984665640564039457584007913129639935 (256 bits). */
329const uint8_t asn1_large_oid4[] = {
330 0x06, 0x26,
331 0x2b, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
332 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
333 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
334 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
335 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
336};
337
338struct oid_large_test {
339 const char *oid;
340 const uint8_t *asn1_der;
341 size_t asn1_der_len;
342 int obj2txt;
343};
344
345struct oid_large_test oid_large_tests[] = {
346 {
347 .oid = "1.3.18446744073709551615",
348 .asn1_der = asn1_large_oid1,
349 .asn1_der_len = sizeof(asn1_large_oid1),
350 .obj2txt = 1,
351 },
352 {
353 .oid = "1.3.18446744073709551616",
354 .asn1_der = asn1_large_oid2,
355 .asn1_der_len = sizeof(asn1_large_oid2),
356 .obj2txt = 1,
357 },
358 {
359 .oid = "1.3.340282366920938463463374607431768211455",
360 .asn1_der = asn1_large_oid3,
361 .asn1_der_len = sizeof(asn1_large_oid3),
362 .obj2txt = 1,
363 },
364 {
365 .oid = "1.3.115792089237316195423570985008687907853269984665640"
366 "564039457584007913129639935",
367 .asn1_der = asn1_large_oid4,
368 .asn1_der_len = sizeof(asn1_large_oid4),
369 .obj2txt = 1,
370 },
371};
372
373#define N_OID_LARGE_TESTS (sizeof(oid_large_tests) / sizeof(*oid_large_tests))
374
375static int
376obj_oid_large_test(size_t test_no, struct oid_large_test *olt)
377{
378 ASN1_OBJECT *obj = NULL;
379 const uint8_t *p;
380 char buf[1024];
381 int len;
382 int failed = 1;
383
384 p = olt->asn1_der;
385 if ((obj = d2i_ASN1_OBJECT(NULL, &p, olt->asn1_der_len)) == NULL) {
386 fprintf(stderr, "FAIL: d2i_ASN1_OBJECT() failed for large "
387 "oid %zu\n", test_no);
388 goto failed;
389 }
390 len = OBJ_obj2txt(buf, sizeof(buf), obj, 1);
391 if (len < 0 || (size_t)len >= sizeof(buf)) {
392 fprintf(stderr, "FAIL: OBJ_obj2txt() failed for large "
393 "oid %zu\n", test_no);
394 goto failed;
395 }
396 if ((len != 0) != olt->obj2txt) {
397 fprintf(stderr, "FAIL: OBJ_obj2txt() failed for large "
398 "oid %zu\n", test_no);
399 goto failed;
400 }
401 if (len != 0 && strcmp(buf, olt->oid) != 0) {
402 fprintf(stderr, "FAIL: OBJ_obj2txt() returned '%s', want '%s'\n",
403 buf, olt->oid);
404 goto failed;
405 }
406
407 failed = 0;
408
409 failed:
410 ASN1_OBJECT_free(obj);
411
412 return failed;
413}
414
415static int
416obj_oid_large_tests(void)
417{
418 int failed = 0;
419 size_t i;
420
421 for (i = 0; i < N_OID_LARGE_TESTS; i++)
422 failed |= obj_oid_large_test(i, &oid_large_tests[i]);
423
424 return failed;
425}
426
427int
428main(int argc, char **argv)
429{
430 int failed = 0;
431
432 failed |= obj_name_tests();
433 failed |= obj_nid_tests();
434 failed |= obj_oid_tests();
435 failed |= obj_oid_large_tests();
436
437 return (failed);
438}