summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2023-10-11 12:49:00 +0000
committertb <>2023-10-11 12:49:00 +0000
commitc325a3d32671c84545aa460fd4fe05bc9139d685 (patch)
treec2d2ec3747f34365809e44e29cb47b1a1b5f50ea
parent26780e7617c82cc58ea63df16fdf6869d1a80d22 (diff)
downloadopenbsd-c325a3d32671c84545aa460fd4fe05bc9139d685.tar.gz
openbsd-c325a3d32671c84545aa460fd4fe05bc9139d685.tar.bz2
openbsd-c325a3d32671c84545aa460fd4fe05bc9139d685.zip
Add regress coverage for X509_ALGOR_*
This covers the setters and getters. Serialization and deserialization as well as comparison is already well covered by the pieces of regress using certs. There is currently one printf indicating failure. This will be fixed shortly.
-rw-r--r--src/regress/lib/libcrypto/asn1/Makefile5
-rw-r--r--src/regress/lib/libcrypto/asn1/x509_algor.c374
2 files changed, 377 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/asn1/Makefile b/src/regress/lib/libcrypto/asn1/Makefile
index 0ecc857901..d00d41a95e 100644
--- a/src/regress/lib/libcrypto/asn1/Makefile
+++ b/src/regress/lib/libcrypto/asn1/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.24 2023/05/12 08:28:05 tb Exp $ 1# $OpenBSD: Makefile,v 1.25 2023/10/11 12:49:00 tb Exp $
2 2
3PROGS = \ 3PROGS = \
4 asn1api \ 4 asn1api \
@@ -12,7 +12,8 @@ PROGS = \
12 asn1time \ 12 asn1time \
13 asn1x509 \ 13 asn1x509 \
14 rfc5280time \ 14 rfc5280time \
15 string_table 15 string_table \
16 x509_algor
16 17
17DPADD+= ${LIBCRYPTO} 18DPADD+= ${LIBCRYPTO}
18WARNINGS= Yes 19WARNINGS= Yes
diff --git a/src/regress/lib/libcrypto/asn1/x509_algor.c b/src/regress/lib/libcrypto/asn1/x509_algor.c
new file mode 100644
index 0000000000..6e0e651221
--- /dev/null
+++ b/src/regress/lib/libcrypto/asn1/x509_algor.c
@@ -0,0 +1,374 @@
1/* $OpenBSD: x509_algor.c,v 1.1 2023/10/11 12:49:00 tb Exp $ */
2/*
3 * Copyright (c) 2023 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 <stdio.h>
20
21#include <openssl/x509.h>
22
23static int
24x509_algor_new_test(void)
25{
26 X509_ALGOR *alg = NULL;
27 const ASN1_OBJECT *aobj;
28 int failed = 1;
29
30 if ((alg = X509_ALGOR_new()) == NULL)
31 errx(1, "%s: X509_ALGOR_new", __func__);
32
33 if ((aobj = OBJ_nid2obj(NID_undef)) == NULL)
34 errx(1, "%s: OBJ_nid2obj", __func__);
35
36 if (alg->algorithm != aobj) {
37 fprintf(stderr, "FAIL: %s: want NID_undef OID\n", __func__);
38 goto failure;
39 }
40 if (alg->parameter != NULL) {
41 fprintf(stderr, "FAIL: %s: want NULL parameters\n", __func__);
42 goto failure;
43 }
44
45 failed = 0;
46
47 failure:
48 X509_ALGOR_free(alg);
49
50 return failed;
51}
52
53static int
54x509_algor_set0_test(void)
55{
56 X509_ALGOR *alg = NULL;
57 ASN1_TYPE *old_parameter;
58 ASN1_OBJECT *oid;
59 ASN1_INTEGER *aint = NULL, *aint_ref;
60 int ret;
61 int failed = 1;
62
63 if ((ret = X509_ALGOR_set0(NULL, NULL, 0, NULL)) != 0) {
64 fprintf(stderr, "FAIL: %s: X509_ALGOR_set0(NULL, NULL, 0, NULL)"
65 ", want: %d, got %d\n", __func__, 0, ret);
66 goto failure;
67 }
68
69 if ((alg = X509_ALGOR_new()) == NULL)
70 errx(1, "%s: X509_ALGOR_new", __func__);
71
72 /* This sets algorithm to NULL and allocates new parameters. */
73 if ((ret = X509_ALGOR_set0(alg, NULL, 0, NULL)) != 1) {
74 fprintf(stderr, "FAIL: %s: X509_ALGOR_set0(alg, NULL, 0, NULL)"
75 ", want: %d, got %d\n", __func__, 1, ret);
76 goto failure;
77 }
78 if (alg->algorithm != NULL) {
79 fprintf(stderr, "FAIL: %s: want NULL algorithm after "
80 "X509_ALGOR_set0(alg, NULL, 0, NULL)\n", __func__);
81 goto failure;
82 }
83 if ((old_parameter = alg->parameter) == NULL) {
84 fprintf(stderr, "FAIL: %s: want non-NULL parameter after "
85 "X509_ALGOR_set0(alg, NULL, 0, NULL)\n", __func__);
86 goto failure;
87 }
88 if (alg->parameter->type != V_ASN1_UNDEF) {
89 fprintf(stderr, "FAIL: %s: want %d parameter type after "
90 "X509_ALGOR_set0(alg, NULL, 0, NULL), got %d\n",
91 __func__, V_ASN1_UNDEF, alg->parameter->type);
92 goto failure;
93 }
94 if (alg->parameter->value.ptr != NULL) {
95 fprintf(stderr, "FAIL: %s: want NULL parameter value after "
96 "X509_ALGOR_set0(alg, NULL, 0, NULL)\n", __func__);
97 goto failure;
98 }
99
100 /* This should leave algorithm at NULL and parameters untouched. */
101 if ((ret = X509_ALGOR_set0(alg, NULL, 0, NULL)) != 1) {
102 fprintf(stderr, "FAIL: %s: X509_ALGOR_set0(alg, NULL, 0, NULL)"
103 ", want: %d, got %d\n", __func__, 1, ret);
104 goto failure;
105 }
106 if (alg->algorithm != NULL) {
107 fprintf(stderr, "FAIL: %s: want NULL algorithm after second"
108 "X509_ALGOR_set0(alg, NULL, 0, NULL)\n", __func__);
109 goto failure;
110 }
111 if (alg->parameter != old_parameter) {
112 fprintf(stderr, "FAIL: %s: parameter changed after second"
113 "X509_ALGOR_set0(alg, NULL, 0, NULL)\n", __func__);
114 goto failure;
115 }
116
117 /* This ignores pval (old_parameter). */
118 if ((ret = X509_ALGOR_set0(alg, NULL, 0, old_parameter)) != 1) {
119 fprintf(stderr, "FAIL: %s: X509_ALGOR_set0(alg, NULL, 0, ptr)"
120 ", want: %d, got %d\n", __func__, 1, ret);
121 goto failure;
122 }
123 if (alg->algorithm != NULL) {
124 fprintf(stderr, "FAIL: %s: want NULL algorithm after "
125 "X509_ALGOR_set0(alg, NULL, 0, ptr)\n", __func__);
126 goto failure;
127 }
128 if (alg->parameter == NULL) {
129 fprintf(stderr, "FAIL: %s: want non-NULL parameter after "
130 "X509_ALGOR_set0(alg, NULL, 0, ptr)\n", __func__);
131 goto failure;
132 }
133 if (alg->parameter->type != V_ASN1_UNDEF) {
134 fprintf(stderr, "FAIL: %s: want %d parameter type after "
135 "X509_ALGOR_set0(alg, NULL, 0, ptr), got %d\n",
136 __func__, V_ASN1_UNDEF, alg->parameter->type);
137 goto failure;
138 }
139 if (alg->parameter->value.ptr != NULL) {
140 fprintf(stderr, "FAIL: %s: want NULL parameter value after "
141 "X509_ALGOR_set0(alg, NULL, 0, ptr)\n", __func__);
142 goto failure;
143 }
144
145 old_parameter = NULL;
146
147 /* This frees parameters and ignores pval. */
148 if ((ret = X509_ALGOR_set0(alg, NULL, V_ASN1_UNDEF, NULL)) != 1) {
149 fprintf(stderr, "FAIL: %s: "
150 "X509_ALGOR_set0(alg, NULL, V_ASN1_UNDEF, NULL)"
151 ", want: %d, got %d\n", __func__, 1, ret);
152 goto failure;
153 }
154 if (alg->algorithm != NULL) {
155 fprintf(stderr, "FAIL: %s: want NULL algorithm after "
156 "X509_ALGOR_set0(alg, NULL, V_ASN1_UNDEF, NULL)\n", __func__);
157 goto failure;
158 }
159 if (alg->parameter != NULL) {
160 fprintf(stderr, "FAIL: %s: want NULL parameter after "
161 "X509_ALGOR_set0(alg, NULL, V_ASN1_UNDEF, NULL)\n", __func__);
162 goto failure;
163 }
164
165 /* This frees parameters and ignores "foo". */
166 if ((ret = X509_ALGOR_set0(alg, NULL, V_ASN1_UNDEF, "foo")) != 1) {
167 fprintf(stderr, "FAIL: %s: X509_ALGOR_set0(alg, NULL, 0, \"foo\")"
168 ", want: %d, got %d\n", __func__, 1, ret);
169 goto failure;
170 }
171 if (alg->algorithm != NULL) {
172 fprintf(stderr, "FAIL: %s: want NULL algorithm after "
173 "X509_ALGOR_set0(alg, NULL, V_ASN1_UNDEF, \"foo\")\n", __func__);
174 goto failure;
175 }
176 if (alg->parameter != NULL) {
177 fprintf(stderr, "FAIL: %s: want NULL parameter after "
178 "X509_ALGOR_set0(alg, NULL, V_ASN1_UNDEF, \"foo\")\n", __func__);
179 goto failure;
180 }
181
182 if ((oid = OBJ_nid2obj(NID_sha512_224)) == NULL) {
183 fprintf(stderr, "FAIL: %s: OBJ_nid2obj(NID_sha512_224)\n", __func__);
184 goto failure;
185 }
186 if ((aint = aint_ref = ASN1_INTEGER_new()) == NULL)
187 errx(1, "%s: ASN1_INTEGER_new()", __func__);
188 if (!ASN1_INTEGER_set_uint64(aint, 57))
189 errx(1, "%s: ASN1_INTEGER_set_uint64()", __func__);
190
191 if ((ret = X509_ALGOR_set0(alg, oid, V_ASN1_INTEGER, aint)) != 1) {
192 fprintf(stderr, "Fail: %s: "
193 "X509_ALGOR_set0(alg, oid, V_ASN1_INTEGER, anull)"
194 ", want: %d, got %d\n", __func__, 1, ret);
195 goto failure;
196 }
197 aint = NULL;
198 if (alg->algorithm != oid) {
199 fprintf(stderr, "FAIL: %s: unexpected oid on alg after "
200 "X509_ALGOR_set0(alg, oid, V_ASN1_INTEGER, anull)"
201 ", want: %d, got %d\n", __func__, 1, ret);
202 goto failure;
203 }
204 if (alg->parameter == NULL) {
205 fprintf(stderr, "FAIL: %s: expected non-NULL parameter after "
206 "X509_ALGOR_set0(alg, oid, V_ASN1_INTEGER, anull)"
207 ", want: %d, got %d\n", __func__, 1, ret);
208 goto failure;
209 }
210 if (alg->parameter->type != V_ASN1_INTEGER) {
211 fprintf(stderr, "FAIL: %s: want %d parameter type after "
212 "X509_ALGOR_set0(alg, oid, V_ASN1_INTEGER, anull), got %d\n",
213 __func__, V_ASN1_INTEGER, alg->parameter->type);
214 goto failure;
215 }
216 if (alg->parameter->value.asn1_string != aint_ref) {
217 fprintf(stderr, "FAIL: %s: unexpected parameter value after "
218 "X509_ALGOR_set0(alg, oid, V_ASN1_NULL, anull)\n", __func__);
219 goto failure;
220 }
221
222 failed = 0;
223
224 failure:
225 X509_ALGOR_free(alg);
226 ASN1_INTEGER_free(aint);
227
228 return failed;
229}
230
231static int
232x509_algor_get0_test(void)
233{
234 X509_ALGOR *alg;
235 const ASN1_OBJECT *aobj = NULL;
236 int ptype = 0;
237 const void *pval = NULL;
238 ASN1_OBJECT *oid;
239 ASN1_INTEGER *aint = NULL, *aint_ref = NULL;
240 int ret;
241 int failed = 1;
242
243 if ((alg = X509_ALGOR_new()) == NULL)
244 errx(1, "%s: X509_ALGOR_new", __func__);
245
246 X509_ALGOR_get0(&aobj, NULL, NULL, alg);
247 if (aobj == NULL) {
248 fprintf(stderr, "FAIL: %s: expected non-NULL aobj\n", __func__);
249 goto failure;
250 }
251 X509_ALGOR_get0(NULL, &ptype, NULL, alg);
252 if (ptype != V_ASN1_UNDEF) {
253 fprintf(stderr, "FAIL: %s: want %d, got %d\n",
254 __func__, V_ASN1_UNDEF, ptype);
255 goto failure;
256 }
257
258 if ((oid = OBJ_nid2obj(NID_ED25519)) == NULL)
259 errx(1, "%s: OBJ_nid2obj(NID_ED25519)", __func__);
260 if ((aint = aint_ref = ASN1_INTEGER_new()) == NULL)
261 errx(1, "%s: ASN1_INTEGER_new()", __func__);
262 if (!ASN1_INTEGER_set_uint64(aint, 99))
263 errx(1, "%s: ASN1_INTEGER_set_uint64()", __func__);
264
265 if ((ret = X509_ALGOR_set0(alg, oid, V_ASN1_INTEGER, aint)) != 1) {
266 fprintf(stderr, "Fail: %s: "
267 "X509_ALGOR_set0(alg, oid, V_ASN1_INTEGER, anull)"
268 ", want: %d, got %d\n", __func__, 1, ret);
269 goto failure;
270 }
271 aint = NULL;
272
273 X509_ALGOR_get0(&aobj, NULL, NULL, alg);
274 if (aobj != oid) {
275 fprintf(stderr, "FAIL: %s: expected Ed25519 oid\n", __func__);
276 goto failure;
277 }
278 X509_ALGOR_get0(NULL, &ptype, NULL, alg);
279 if (ptype != V_ASN1_INTEGER) {
280 fprintf(stderr, "FAIL: %s: expected %d, got %d\n",
281 __func__, V_ASN1_INTEGER, ptype);
282 goto failure;
283 }
284 pval = oid;
285 X509_ALGOR_get0(NULL, NULL, &pval, alg);
286 if (pval != NULL) {
287 /* XXX */
288 fprintf(stderr, "expected FAIL: %s: got non-NULL pval\n", __func__);
289 }
290
291 aobj = NULL;
292 ptype = V_ASN1_UNDEF;
293 pval = oid;
294 X509_ALGOR_get0(&aobj, &ptype, &pval, alg);
295 if (aobj != oid) {
296 fprintf(stderr, "FAIL: %s: expected Ed25519 oid 2\n", __func__);
297 goto failure;
298 }
299 if (ptype != V_ASN1_INTEGER) {
300 fprintf(stderr, "FAIL: %s: expected %d, got %d 2\n",
301 __func__, V_ASN1_INTEGER, ptype);
302 goto failure;
303 }
304 if (pval != aint_ref) {
305 fprintf(stderr, "FAIL: %s: expected ASN.1 integer\n", __func__);
306 goto failure;
307 }
308
309 failed = 0;
310
311 failure:
312 X509_ALGOR_free(alg);
313 ASN1_INTEGER_free(aint);
314
315 return failed;
316}
317
318static int
319x509_algor_set_md_test(void)
320{
321 X509_ALGOR *alg = NULL;
322 const ASN1_OBJECT *aobj;
323 int ptype = 0, nid = 0;
324 int failed = 1;
325
326 if ((alg = X509_ALGOR_new()) == NULL)
327 errx(1, "%s: X509_ALGOR_new", __func__);
328
329 X509_ALGOR_set_md(alg, EVP_sm3());
330 X509_ALGOR_get0(&aobj, &ptype, NULL, alg);
331 if ((nid = OBJ_obj2nid(aobj)) != NID_sm3) {
332 fprintf(stderr, "%s: sm3 want %d, got %d\n", __func__,
333 NID_sm3, nid);
334 goto failure;
335 }
336 if (ptype != V_ASN1_UNDEF) {
337 fprintf(stderr, "%s: sm3 want %d, got %d\n", __func__,
338 V_ASN1_UNDEF, ptype);
339 goto failure;
340 }
341
342 X509_ALGOR_set_md(alg, EVP_md5());
343 X509_ALGOR_get0(&aobj, &ptype, NULL, alg);
344 if ((nid = OBJ_obj2nid(aobj)) != NID_md5) {
345 fprintf(stderr, "%s: sm3 want %d, got %d\n", __func__,
346 NID_sm3, nid);
347 goto failure;
348 }
349 if (ptype != V_ASN1_NULL) {
350 fprintf(stderr, "%s: sm3 want %d, got %d\n", __func__,
351 V_ASN1_NULL, ptype);
352 goto failure;
353 }
354
355 failed = 0;
356
357 failure:
358 X509_ALGOR_free(alg);
359
360 return failed;
361}
362
363int
364main(void)
365{
366 int failed = 0;
367
368 failed |= x509_algor_new_test();
369 failed |= x509_algor_set0_test();
370 failed |= x509_algor_get0_test();
371 failed |= x509_algor_set_md_test();
372
373 return failed;
374}