diff options
author | tb <> | 2023-05-12 08:28:05 +0000 |
---|---|---|
committer | tb <> | 2023-05-12 08:28:05 +0000 |
commit | c0d1d7edec5f5e62f73a8edb97224efab101f927 (patch) | |
tree | d09a5606f6528a2f6734706c50e6aa25f6dab0dd /src | |
parent | 92b0b72c0fff75c20ec0d5b3f2bbc12eba120778 (diff) | |
download | openbsd-c0d1d7edec5f5e62f73a8edb97224efab101f927.tar.gz openbsd-c0d1d7edec5f5e62f73a8edb97224efab101f927.tar.bz2 openbsd-c0d1d7edec5f5e62f73a8edb97224efab101f927.zip |
Add regress coverage for {s2i,i2s}_ASN1_OCTET_STRING
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/asn1/Makefile | 3 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/asn1/asn1oct.c | 269 |
2 files changed, 271 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/Makefile b/src/regress/lib/libcrypto/asn1/Makefile index e1a2606349..0ecc857901 100644 --- a/src/regress/lib/libcrypto/asn1/Makefile +++ b/src/regress/lib/libcrypto/asn1/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.23 2022/12/02 00:47:32 tb Exp $ | 1 | # $OpenBSD: Makefile,v 1.24 2023/05/12 08:28:05 tb Exp $ |
2 | 2 | ||
3 | PROGS = \ | 3 | PROGS = \ |
4 | asn1api \ | 4 | asn1api \ |
@@ -6,6 +6,7 @@ PROGS = \ | |||
6 | asn1complex \ | 6 | asn1complex \ |
7 | asn1evp \ | 7 | asn1evp \ |
8 | asn1object \ | 8 | asn1object \ |
9 | asn1oct \ | ||
9 | asn1string_copy \ | 10 | asn1string_copy \ |
10 | asn1_string_to_utf8 \ | 11 | asn1_string_to_utf8 \ |
11 | asn1time \ | 12 | asn1time \ |
diff --git a/src/regress/lib/libcrypto/asn1/asn1oct.c b/src/regress/lib/libcrypto/asn1/asn1oct.c new file mode 100644 index 0000000000..7c5708f052 --- /dev/null +++ b/src/regress/lib/libcrypto/asn1/asn1oct.c | |||
@@ -0,0 +1,269 @@ | |||
1 | /* $OpenBSD: asn1oct.c,v 1.1 2023/05/12 08:28:05 tb Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <err.h> | ||
20 | #include <string.h> | ||
21 | |||
22 | #include <openssl/asn1.h> | ||
23 | #include <openssl/x509v3.h> | ||
24 | |||
25 | #define TESTBUFFER_SIZE 20 | ||
26 | |||
27 | static const struct i2s_asn1_octet_string_test { | ||
28 | const char *desc; | ||
29 | const uint8_t buf[TESTBUFFER_SIZE]; | ||
30 | long len; | ||
31 | const char *want; | ||
32 | } i2s_test[] = { | ||
33 | { | ||
34 | .desc = "Empty buffer gives empty string", | ||
35 | .buf = { 0x00, }, | ||
36 | .len = 0, | ||
37 | .want = "", | ||
38 | }, | ||
39 | { | ||
40 | .desc = "all hex digits", | ||
41 | .buf = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, }, | ||
42 | .len = 8, | ||
43 | .want = "01:23:45:67:89:AB:CD:EF", | ||
44 | }, | ||
45 | { | ||
46 | .desc = "all hex digits, scrambled", | ||
47 | .buf = { 0x98, 0x24, 0xbf, 0x3a, 0xc7, 0xd6, 0x01, 0x5e, }, | ||
48 | .len = 8, | ||
49 | .want = "98:24:BF:3A:C7:D6:01:5E", | ||
50 | }, | ||
51 | { | ||
52 | .desc = "Embedded 0 byte", | ||
53 | .buf = { 0x7a, 0x00, 0xbb, }, | ||
54 | .len = 3, | ||
55 | .want = "7A:00:BB", | ||
56 | }, | ||
57 | { | ||
58 | .desc = "All zeroes", | ||
59 | .buf = { 0x00, 0x00, 0x00, 0x00, 0x00, }, | ||
60 | .len = 4, | ||
61 | .want = "00:00:00:00", | ||
62 | }, | ||
63 | { | ||
64 | .desc = "All bits set", | ||
65 | .buf = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }, | ||
66 | .len = 8, | ||
67 | .want = "FF:FF:FF:FF:FF:FF:FF:FF", | ||
68 | }, | ||
69 | { | ||
70 | .desc = "negative length", | ||
71 | .buf = { 0x00, }, | ||
72 | .len = -1, | ||
73 | }, | ||
74 | }; | ||
75 | |||
76 | #define N_I2S_TESTS (sizeof(i2s_test) / sizeof(i2s_test[0])) | ||
77 | |||
78 | static int | ||
79 | test_i2s_ASN1_OCTET_STRING(const struct i2s_asn1_octet_string_test *test) | ||
80 | { | ||
81 | ASN1_OCTET_STRING *aos = NULL; | ||
82 | int should_fail = test->want == NULL; | ||
83 | char *got = NULL; | ||
84 | int failed = 0; | ||
85 | |||
86 | if ((aos = ASN1_OCTET_STRING_new()) == NULL) | ||
87 | errx(1, "ASN1_OCTET_STRING_new"); | ||
88 | |||
89 | if (!ASN1_STRING_set(aos, (void *)test->buf, test->len)) | ||
90 | errx(1, "ASN1_STRING_set"); | ||
91 | |||
92 | if ((got = i2s_ASN1_OCTET_STRING(NULL, aos)) == NULL) { | ||
93 | if (!should_fail) | ||
94 | errx(1, "i2s_ASN1_OCTET_STRING"); | ||
95 | } | ||
96 | |||
97 | if (!should_fail && strcmp(test->want, got) != 0) { | ||
98 | fprintf(stderr, "%s: \"%s\" failed: want \"%s\", got \"%s\"\n", | ||
99 | __func__, test->desc, test->want, got); | ||
100 | failed |= 1; | ||
101 | } | ||
102 | |||
103 | ASN1_OCTET_STRING_free(aos); | ||
104 | free(got); | ||
105 | |||
106 | return failed; | ||
107 | } | ||
108 | |||
109 | static int | ||
110 | test_new_ASN1_OCTET_STRING(void) | ||
111 | { | ||
112 | ASN1_OCTET_STRING *aos = NULL; | ||
113 | char *got; | ||
114 | int failed = 0; | ||
115 | |||
116 | if ((aos = ASN1_OCTET_STRING_new()) == NULL) | ||
117 | errx(1, "ASN1_OCTET_STRING_new"); | ||
118 | if ((got = i2s_ASN1_OCTET_STRING(NULL, aos)) == NULL) | ||
119 | errx(1, "foo i2s_ASN1_OCTET_STRING"); | ||
120 | |||
121 | if (strcmp("", got) != 0) { | ||
122 | fprintf(stderr, "%s failed: want \"\", got \"%s\"\n", | ||
123 | __func__, got); | ||
124 | failed |= 1; | ||
125 | } | ||
126 | |||
127 | ASN1_OCTET_STRING_free(aos); | ||
128 | free(got); | ||
129 | |||
130 | return failed; | ||
131 | } | ||
132 | |||
133 | static int | ||
134 | run_i2s_ASN1_OCTET_STRING_tests(void) | ||
135 | { | ||
136 | size_t i; | ||
137 | int failed = 0; | ||
138 | |||
139 | failed |= test_new_ASN1_OCTET_STRING(); | ||
140 | |||
141 | for (i = 0; i < N_I2S_TESTS; i++) | ||
142 | failed |= test_i2s_ASN1_OCTET_STRING(&i2s_test[i]); | ||
143 | |||
144 | return failed; | ||
145 | } | ||
146 | |||
147 | static const struct s2i_asn1_octet_string_test { | ||
148 | const char *desc; | ||
149 | const char *in; | ||
150 | const char *want; | ||
151 | } s2i_test[] = { | ||
152 | /* Tests that should succeed. */ | ||
153 | { | ||
154 | .desc = "empty string", | ||
155 | .in = "", | ||
156 | .want = "", | ||
157 | }, | ||
158 | { | ||
159 | .desc = "a 0 octet", | ||
160 | .in = "00", | ||
161 | .want = "00", | ||
162 | }, | ||
163 | { | ||
164 | .desc = "a 0 octet with stupid colons", | ||
165 | .in = ":::00:::::", | ||
166 | .want = "00", | ||
167 | }, | ||
168 | { | ||
169 | .desc = "more stupid colons", | ||
170 | .in = ":::C0fF::Ee:::::", | ||
171 | .want = "C0:FF:EE", | ||
172 | }, | ||
173 | { | ||
174 | .desc = "all hex digits", | ||
175 | .in = "0123456789abcdef", | ||
176 | .want = "01:23:45:67:89:AB:CD:EF", | ||
177 | }, | ||
178 | |||
179 | /* Tests that should fail. */ | ||
180 | { | ||
181 | .desc = "colons between hex digits", | ||
182 | .in = "A:F", | ||
183 | }, | ||
184 | { | ||
185 | .desc = "more colons between hex digits", | ||
186 | .in = "5:7", | ||
187 | }, | ||
188 | { | ||
189 | .desc = "one hex digit", | ||
190 | .in = "1", | ||
191 | }, | ||
192 | { | ||
193 | .desc = "three hex digits", | ||
194 | .in = "bad", | ||
195 | }, | ||
196 | { | ||
197 | .desc = "three hex digits, colon after first digit", | ||
198 | .in = "b:ad", | ||
199 | }, | ||
200 | { | ||
201 | .desc = "three hex digits, colon after second digit", | ||
202 | .in = "ba:d", | ||
203 | }, | ||
204 | { | ||
205 | .desc = "non-hex digit", | ||
206 | .in = "g00d", | ||
207 | }, | ||
208 | { | ||
209 | .desc = "trailing non-hex digit", | ||
210 | .in = "d00der", | ||
211 | }, | ||
212 | }; | ||
213 | |||
214 | #define N_S2I_TESTS (sizeof(s2i_test) / sizeof(s2i_test[0])) | ||
215 | |||
216 | static int | ||
217 | test_s2i_ASN1_OCTET_STRING(const struct s2i_asn1_octet_string_test *test) | ||
218 | { | ||
219 | ASN1_OCTET_STRING *aos = NULL; | ||
220 | char *got = NULL; | ||
221 | int should_fail = test->want == NULL; | ||
222 | int failed = 0; | ||
223 | |||
224 | if ((aos = s2i_ASN1_OCTET_STRING(NULL, NULL, test->in)) == NULL) { | ||
225 | if (!should_fail) | ||
226 | errx(1, "%s: s2i_ASN1_OCTET_STRING", test->desc); | ||
227 | goto done; | ||
228 | } | ||
229 | |||
230 | if ((got = i2s_ASN1_OCTET_STRING(NULL, aos)) == NULL) | ||
231 | errx(1, "%s: i2s_ASN1_OCTET_STRING", test->desc); | ||
232 | |||
233 | if (strcmp(test->want, got) != 0) { | ||
234 | fprintf(stderr, "%s: \"%s\" failed: want \"%s\", got \"%s\"\n", | ||
235 | __func__, test->desc, test->want, got); | ||
236 | failed |= 1; | ||
237 | } | ||
238 | |||
239 | done: | ||
240 | ASN1_OCTET_STRING_free(aos); | ||
241 | free(got); | ||
242 | |||
243 | return failed; | ||
244 | } | ||
245 | |||
246 | static int | ||
247 | run_s2i_ASN1_OCTET_STRING_tests(void) | ||
248 | { | ||
249 | size_t i; | ||
250 | int failed = 0; | ||
251 | |||
252 | failed |= test_new_ASN1_OCTET_STRING(); | ||
253 | |||
254 | for (i = 0; i < N_S2I_TESTS; i++) | ||
255 | failed |= test_s2i_ASN1_OCTET_STRING(&s2i_test[i]); | ||
256 | |||
257 | return failed; | ||
258 | } | ||
259 | |||
260 | int | ||
261 | main(void) | ||
262 | { | ||
263 | int failed = 0; | ||
264 | |||
265 | failed |= run_i2s_ASN1_OCTET_STRING_tests(); | ||
266 | failed |= run_s2i_ASN1_OCTET_STRING_tests(); | ||
267 | |||
268 | return failed; | ||
269 | } | ||