summaryrefslogtreecommitdiff
path: root/src/regress/lib
diff options
context:
space:
mode:
authortb <>2022-05-16 20:53:20 +0000
committertb <>2022-05-16 20:53:20 +0000
commit27e784dc1e3e5813d22270e673ff4b451758773a (patch)
tree3ac593d6a9bf6334ef7faae12e8b3e43eed43c15 /src/regress/lib
parent1d607d2dfce47831be44656db302b0726c09df3a (diff)
downloadopenbsd-27e784dc1e3e5813d22270e673ff4b451758773a.tar.gz
openbsd-27e784dc1e3e5813d22270e673ff4b451758773a.tar.bz2
openbsd-27e784dc1e3e5813d22270e673ff4b451758773a.zip
Add a simple test that exercises ASN1_STRING_to_UTF8() a little.
Diffstat (limited to 'src/regress/lib')
-rw-r--r--src/regress/lib/libcrypto/asn1/Makefile3
-rw-r--r--src/regress/lib/libcrypto/asn1/asn1_string_to_utf8.c128
2 files changed, 130 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/Makefile b/src/regress/lib/libcrypto/asn1/Makefile
index 0cb0c58d95..d7d8de8b6d 100644
--- a/src/regress/lib/libcrypto/asn1/Makefile
+++ b/src/regress/lib/libcrypto/asn1/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.16 2022/02/26 16:45:31 jsing Exp $ 1# $OpenBSD: Makefile,v 1.17 2022/05/16 20:53:20 tb Exp $
2 2
3PROGS = \ 3PROGS = \
4 asn1api \ 4 asn1api \
@@ -7,6 +7,7 @@ PROGS = \
7 asn1evp \ 7 asn1evp \
8 asn1object \ 8 asn1object \
9 asn1string_copy \ 9 asn1string_copy \
10 asn1_string_to_utf8 \
10 asn1time \ 11 asn1time \
11 asn1x509 \ 12 asn1x509 \
12 rfc5280time \ 13 rfc5280time \
diff --git a/src/regress/lib/libcrypto/asn1/asn1_string_to_utf8.c b/src/regress/lib/libcrypto/asn1/asn1_string_to_utf8.c
new file mode 100644
index 0000000000..2ead7b46c3
--- /dev/null
+++ b/src/regress/lib/libcrypto/asn1/asn1_string_to_utf8.c
@@ -0,0 +1,128 @@
1/* $OpenBSD: asn1_string_to_utf8.c,v 1.1 2022/05/16 20:53:20 tb Exp $ */
2/*
3 * Copyright (c) 2022 Theo Buehler <tb@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 <err.h>
19#include <string.h>
20
21#include <openssl/asn1.h>
22
23struct asn1_string_to_utf8_test_case {
24 const char *description;
25 const ASN1_ITEM *item;
26 const uint8_t der[32];
27 size_t der_len;
28 const uint8_t want[32];
29 int want_len;
30};
31
32static const struct asn1_string_to_utf8_test_case tests[] = {
33 {
34 .description = "hello",
35 .item = &ASN1_PRINTABLESTRING_it,
36 .der = {
37 0x13, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
38 },
39 .der_len = 7,
40 .want = {
41 0x68, 0x65, 0x6c, 0x6c, 0x6f,
42 },
43 .want_len = 5,
44 },
45 {
46 .description = "face with tears of joy",
47 .item = &ASN1_UTF8STRING_it,
48 .der = {
49 0x0c, 0x04, 0xF0, 0x9F, 0x98, 0x82,
50 },
51 .der_len = 6,
52 .want = {
53 0xF0, 0x9F, 0x98, 0x82,
54 },
55 .want_len = 4,
56 },
57 {
58 .description = "hi",
59 .item = &ASN1_IA5STRING_it,
60 .der = {
61 0x16, 0x02, 0x68, 0x69,
62 },
63 .der_len = 4,
64 .want = {
65 0x68, 0x69,
66 },
67 .want_len = 2,
68 },
69};
70
71const size_t N_TESTS = sizeof(tests) / sizeof(tests[0]);
72
73static int
74asn1_string_to_utf8_test(const struct asn1_string_to_utf8_test_case *test)
75{
76 ASN1_STRING *str = NULL;
77 const unsigned char *der;
78 unsigned char *out = NULL;
79 int ret;
80 int failed = 1;
81
82 der = test->der;
83 if ((str = (ASN1_STRING *)ASN1_item_d2i(NULL, &der, test->der_len,
84 test->item)) == NULL) {
85 warnx("ASN1_item_d2i failed");
86 goto err;
87 }
88
89 if ((ret = ASN1_STRING_to_UTF8(&out, str)) != test->want_len) {
90 warnx("ASN1_STRING_to_UTF8 failed: got %d, want %d", ret,
91 test->want_len);
92 goto err;
93 }
94
95 if (memcmp(out, test->want, test->want_len) != 0) {
96 warnx("memcmp failed");
97 goto err;
98 }
99
100 failed = 0;
101 err:
102 ASN1_STRING_free(str);
103 free(out);
104
105 return failed;
106}
107
108static int
109asn1_string_to_utf8_tests(void)
110{
111 size_t i;
112 int failed = 0;
113
114 for (i = 0; i < N_TESTS; i++)
115 failed |= asn1_string_to_utf8_test(&tests[i]);
116
117 return failed;
118}
119
120int
121main(void)
122{
123 int failed = 0;
124
125 failed |= asn1_string_to_utf8_tests();
126
127 return failed;
128}