]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/v3nametest.c
Update copyright year
[thirdparty/openssl.git] / test / v3nametest.c
CommitLineData
440e5d80 1/*
fecb3aae 2 * Copyright 2012-2022 The OpenSSL Project Authors. All Rights Reserved.
440e5d80 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
440e5d80
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
f3ab6c16 10#include <string.h>
2b1aa198
RL
11
12#include <openssl/e_os2.h>
d88926f1
DSH
13#include <openssl/x509.h>
14#include <openssl/x509v3.h>
2b1aa198 15#include "internal/nelem.h"
f3ab6c16 16#include "testutil.h"
d88926f1 17
0f113f3e
MC
18static const char *const names[] = {
19 "a", "b", ".", "*", "@",
20 ".a", "a.", ".b", "b.", ".*", "*.", "*@", "@*", "a@", "@a", "b@", "..",
9f9a3926 21 "-example.com", "example-.com",
0f113f3e
MC
22 "@@", "**", "*.com", "*com", "*.*.com", "*com", "com*", "*example.com",
23 "*@example.com", "test@*.example.com", "example.com", "www.example.com",
24 "test.www.example.com", "*.example.com", "*.www.example.com",
25 "test.*.example.com", "www.*.com",
26 ".www.example.com", "*www.example.com",
27 "example.net", "xn--rger-koa.example.com",
9f9a3926
ZL
28 "*.xn--rger-koa.example.com", "www.xn--rger-koa.example.com",
29 "*.good--example.com", "www.good--example.com",
30 "*.xn--bar.com", "xn--foo.xn--bar.com",
0f113f3e
MC
31 "a.example.com", "b.example.com",
32 "postmaster@example.com", "Postmaster@example.com",
33 "postmaster@EXAMPLE.COM",
34 NULL
35};
d88926f1 36
0f113f3e
MC
37static const char *const exceptions[] = {
38 "set CN: host: [*.example.com] matches [a.example.com]",
39 "set CN: host: [*.example.com] matches [b.example.com]",
40 "set CN: host: [*.example.com] matches [www.example.com]",
41 "set CN: host: [*.example.com] matches [xn--rger-koa.example.com]",
42 "set CN: host: [*.www.example.com] matches [test.www.example.com]",
43 "set CN: host: [*.www.example.com] matches [.www.example.com]",
44 "set CN: host: [*www.example.com] matches [www.example.com]",
45 "set CN: host: [test.www.example.com] matches [.www.example.com]",
9f9a3926
ZL
46 "set CN: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]",
47 "set CN: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]",
48 "set CN: host: [*.good--example.com] matches [www.good--example.com]",
0f113f3e
MC
49 "set CN: host-no-wildcards: [*.www.example.com] matches [.www.example.com]",
50 "set CN: host-no-wildcards: [test.www.example.com] matches [.www.example.com]",
51 "set emailAddress: email: [postmaster@example.com] does not match [Postmaster@example.com]",
52 "set emailAddress: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]",
53 "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@example.com]",
54 "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]",
55 "set dnsName: host: [*.example.com] matches [www.example.com]",
56 "set dnsName: host: [*.example.com] matches [a.example.com]",
57 "set dnsName: host: [*.example.com] matches [b.example.com]",
58 "set dnsName: host: [*.example.com] matches [xn--rger-koa.example.com]",
59 "set dnsName: host: [*.www.example.com] matches [test.www.example.com]",
60 "set dnsName: host-no-wildcards: [*.www.example.com] matches [.www.example.com]",
61 "set dnsName: host-no-wildcards: [test.www.example.com] matches [.www.example.com]",
62 "set dnsName: host: [*.www.example.com] matches [.www.example.com]",
63 "set dnsName: host: [*www.example.com] matches [www.example.com]",
64 "set dnsName: host: [test.www.example.com] matches [.www.example.com]",
9f9a3926
ZL
65 "set dnsName: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]",
66 "set dnsName: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]",
67 "set dnsName: host: [*.good--example.com] matches [www.good--example.com]",
0f113f3e
MC
68 "set rfc822Name: email: [postmaster@example.com] does not match [Postmaster@example.com]",
69 "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@example.com]",
70 "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]",
71 "set rfc822Name: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]",
72 NULL
73};
d88926f1
DSH
74
75static int is_exception(const char *msg)
0f113f3e
MC
76{
77 const char *const *p;
f3ab6c16 78
0f113f3e
MC
79 for (p = exceptions; *p; ++p)
80 if (strcmp(msg, *p) == 0)
81 return 1;
82 return 0;
83}
d88926f1
DSH
84
85static int set_cn(X509 *crt, ...)
0f113f3e
MC
86{
87 int ret = 0;
88 X509_NAME *n = NULL;
89 va_list ap;
f3ab6c16 90
0f113f3e
MC
91 va_start(ap, crt);
92 n = X509_NAME_new();
93 if (n == NULL)
94 goto out;
f3ab6c16 95
0f113f3e
MC
96 while (1) {
97 int nid;
98 const char *name;
f3ab6c16 99
0f113f3e
MC
100 nid = va_arg(ap, int);
101 if (nid == 0)
102 break;
103 name = va_arg(ap, const char *);
104 if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_ASC,
105 (unsigned char *)name, -1, -1, 1))
106 goto out;
107 }
108 if (!X509_set_subject_name(crt, n))
109 goto out;
110 ret = 1;
d88926f1 111 out:
0f113f3e
MC
112 X509_NAME_free(n);
113 va_end(ap);
114 return ret;
115}
d88926f1 116
1d97c843 117/*-
0f113f3e 118int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
d88926f1 119X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex,
0f113f3e
MC
120 int nid, int crit, ASN1_OCTET_STRING *data);
121int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
d88926f1
DSH
122*/
123
124static int set_altname(X509 *crt, ...)
0f113f3e
MC
125{
126 int ret = 0;
127 GENERAL_NAMES *gens = NULL;
128 GENERAL_NAME *gen = NULL;
129 ASN1_IA5STRING *ia5 = NULL;
130 va_list ap;
131 va_start(ap, crt);
132 gens = sk_GENERAL_NAME_new_null();
133 if (gens == NULL)
134 goto out;
135 while (1) {
136 int type;
137 const char *name;
138 type = va_arg(ap, int);
139 if (type == 0)
140 break;
141 name = va_arg(ap, const char *);
d88926f1 142
0f113f3e
MC
143 gen = GENERAL_NAME_new();
144 if (gen == NULL)
145 goto out;
146 ia5 = ASN1_IA5STRING_new();
147 if (ia5 == NULL)
148 goto out;
149 if (!ASN1_STRING_set(ia5, name, -1))
150 goto out;
151 switch (type) {
152 case GEN_EMAIL:
153 case GEN_DNS:
154 GENERAL_NAME_set0_value(gen, type, ia5);
155 ia5 = NULL;
156 break;
157 default:
158 abort();
159 }
160 sk_GENERAL_NAME_push(gens, gen);
161 gen = NULL;
162 }
163 if (!X509_add1_ext_i2d(crt, NID_subject_alt_name, gens, 0, 0))
164 goto out;
165 ret = 1;
d88926f1 166 out:
0f113f3e
MC
167 ASN1_IA5STRING_free(ia5);
168 GENERAL_NAME_free(gen);
169 GENERAL_NAMES_free(gens);
170 va_end(ap);
171 return ret;
172}
d88926f1
DSH
173
174static int set_cn1(X509 *crt, const char *name)
0f113f3e
MC
175{
176 return set_cn(crt, NID_commonName, name, 0);
177}
d88926f1
DSH
178
179static int set_cn_and_email(X509 *crt, const char *name)
0f113f3e
MC
180{
181 return set_cn(crt, NID_commonName, name,
182 NID_pkcs9_emailAddress, "dummy@example.com", 0);
183}
d88926f1
DSH
184
185static int set_cn2(X509 *crt, const char *name)
0f113f3e
MC
186{
187 return set_cn(crt, NID_commonName, "dummy value",
188 NID_commonName, name, 0);
189}
d88926f1
DSH
190
191static int set_cn3(X509 *crt, const char *name)
0f113f3e
MC
192{
193 return set_cn(crt, NID_commonName, name,
194 NID_commonName, "dummy value", 0);
195}
d88926f1
DSH
196
197static int set_email1(X509 *crt, const char *name)
0f113f3e
MC
198{
199 return set_cn(crt, NID_pkcs9_emailAddress, name, 0);
200}
d88926f1
DSH
201
202static int set_email2(X509 *crt, const char *name)
0f113f3e
MC
203{
204 return set_cn(crt, NID_pkcs9_emailAddress, "dummy@example.com",
205 NID_pkcs9_emailAddress, name, 0);
206}
d88926f1
DSH
207
208static int set_email3(X509 *crt, const char *name)
0f113f3e
MC
209{
210 return set_cn(crt, NID_pkcs9_emailAddress, name,
211 NID_pkcs9_emailAddress, "dummy@example.com", 0);
212}
d88926f1
DSH
213
214static int set_email_and_cn(X509 *crt, const char *name)
0f113f3e
MC
215{
216 return set_cn(crt, NID_pkcs9_emailAddress, name,
217 NID_commonName, "www.example.org", 0);
218}
d88926f1
DSH
219
220static int set_altname_dns(X509 *crt, const char *name)
0f113f3e
MC
221{
222 return set_altname(crt, GEN_DNS, name, 0);
223}
d88926f1
DSH
224
225static int set_altname_email(X509 *crt, const char *name)
0f113f3e
MC
226{
227 return set_altname(crt, GEN_EMAIL, name, 0);
228}
d88926f1 229
0f113f3e
MC
230struct set_name_fn {
231 int (*fn) (X509 *, const char *);
232 const char *name;
233 int host;
234 int email;
235};
d88926f1 236
0f113f3e
MC
237static const struct set_name_fn name_fns[] = {
238 {set_cn1, "set CN", 1, 0},
239 {set_cn2, "set CN", 1, 0},
240 {set_cn3, "set CN", 1, 0},
241 {set_cn_and_email, "set CN", 1, 0},
242 {set_email1, "set emailAddress", 0, 1},
243 {set_email2, "set emailAddress", 0, 1},
244 {set_email3, "set emailAddress", 0, 1},
245 {set_email_and_cn, "set emailAddress", 0, 1},
246 {set_altname_dns, "set dnsName", 1, 0},
247 {set_altname_email, "set rfc822Name", 0, 1},
0f113f3e 248};
d88926f1 249
3cb7c5cf 250static X509 *make_cert(void)
0f113f3e 251{
0f113f3e 252 X509 *crt = NULL;
d88926f1 253
f3ab6c16
RS
254 if (!TEST_ptr(crt = X509_new()))
255 return NULL;
cdf63a37 256 if (!TEST_true(X509_set_version(crt, X509_VERSION_3))) {
f3ab6c16
RS
257 X509_free(crt);
258 return NULL;
259 }
260 return crt;
261}
d88926f1 262
f3ab6c16
RS
263static int check_message(const struct set_name_fn *fn, const char *op,
264 const char *nameincert, int match, const char *name)
0f113f3e
MC
265{
266 char msg[1024];
f3ab6c16 267
0f113f3e 268 if (match < 0)
f3ab6c16 269 return 1;
0f113f3e
MC
270 BIO_snprintf(msg, sizeof(msg), "%s: %s: [%s] %s [%s]",
271 fn->name, op, nameincert,
272 match ? "matches" : "does not match", name);
273 if (is_exception(msg))
f3ab6c16
RS
274 return 1;
275 TEST_error("%s", msg);
276 return 0;
0f113f3e 277}
d88926f1 278
f3ab6c16 279static int run_cert(X509 *crt, const char *nameincert,
0f113f3e
MC
280 const struct set_name_fn *fn)
281{
282 const char *const *pname = names;
f3ab6c16
RS
283 int failed = 0;
284
285 for (; *pname != NULL; ++pname) {
fba140c7 286 int samename = OPENSSL_strcasecmp(nameincert, *pname) == 0;
0f113f3e 287 size_t namelen = strlen(*pname);
fb33f994 288 char *name = OPENSSL_malloc(namelen + 1);
0f113f3e 289 int match, ret;
f3ab6c16 290
fb33f994 291 memcpy(name, *pname, namelen + 1);
d88926f1 292
0f113f3e 293 match = -1;
f3ab6c16
RS
294 if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen, 0, NULL),
295 0)) {
296 failed = 1;
0f113f3e
MC
297 } else if (fn->host) {
298 if (ret == 1 && !samename)
299 match = 1;
300 if (ret == 0 && samename)
301 match = 0;
302 } else if (ret == 1)
303 match = 1;
f3ab6c16
RS
304 if (!TEST_true(check_message(fn, "host", nameincert, match, *pname)))
305 failed = 1;
d88926f1 306
0f113f3e 307 match = -1;
f3ab6c16
RS
308 if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen,
309 X509_CHECK_FLAG_NO_WILDCARDS,
310 NULL), 0)) {
311 failed = 1;
0f113f3e
MC
312 } else if (fn->host) {
313 if (ret == 1 && !samename)
314 match = 1;
315 if (ret == 0 && samename)
316 match = 0;
317 } else if (ret == 1)
318 match = 1;
f3ab6c16
RS
319 if (!TEST_true(check_message(fn, "host-no-wildcards",
320 nameincert, match, *pname)))
321 failed = 1;
d88926f1 322
0f113f3e 323 match = -1;
f3ab6c16 324 ret = X509_check_email(crt, name, namelen, 0);
0f113f3e
MC
325 if (fn->email) {
326 if (ret && !samename)
327 match = 1;
328 if (!ret && samename && strchr(nameincert, '@') != NULL)
329 match = 0;
330 } else if (ret)
331 match = 1;
f3ab6c16
RS
332 if (!TEST_true(check_message(fn, "email", nameincert, match, *pname)))
333 failed = 1;
334 OPENSSL_free(name);
0f113f3e 335 }
f3ab6c16
RS
336
337 return failed == 0;
0f113f3e 338}
d88926f1 339
f3ab6c16 340static int call_run_cert(int i)
0f113f3e 341{
f3ab6c16
RS
342 int failed = 0;
343 const struct set_name_fn *pfn = &name_fns[i];
344 X509 *crt;
345 const char *const *pname;
346
347 TEST_info("%s", pfn->name);
348 for (pname = names; *pname != NULL; pname++) {
349 if (!TEST_ptr(crt = make_cert())
350 || !TEST_true(pfn->fn(crt, *pname))
351 || !run_cert(crt, *pname, pfn))
352 failed = 1;
353 X509_free(crt);
0f113f3e 354 }
f3ab6c16
RS
355 return failed == 0;
356}
357
7eea331e 358static struct gennamedata {
97ab3c4b
MC
359 const unsigned char der[22];
360 size_t derlen;
361} gennames[] = {
362 {
363 /*
364 * [0] {
365 * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
366 * [0] {
367 * SEQUENCE {}
368 * }
369 * }
370 */
371 {
372 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
373 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x30, 0x00
374 },
375 21
376 }, {
377 /*
378 * [0] {
379 * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
380 * [0] {
381 * [APPLICATION 0] {}
382 * }
383 * }
384 */
385 {
386 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
387 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x60, 0x00
388 },
389 21
390 }, {
391 /*
392 * [0] {
393 * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
394 * [0] {
395 * UTF8String { "a" }
396 * }
397 * }
398 */
399 {
400 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
401 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x61
402 },
403 22
404 }, {
405 /*
406 * [0] {
407 * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.2 }
408 * [0] {
409 * UTF8String { "a" }
410 * }
411 * }
412 */
413 {
414 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
415 0x01, 0x84, 0xb7, 0x09, 0x02, 0x02, 0xa0, 0x03, 0x0c, 0x01, 0x61
416 },
417 22
418 }, {
419 /*
420 * [0] {
421 * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
422 * [0] {
423 * UTF8String { "b" }
424 * }
425 * }
426 */
427 {
428 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
429 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x62
430 },
431 22
432 }, {
433 /*
434 * [0] {
435 * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
436 * [0] {
437 * BOOLEAN { TRUE }
438 * }
439 * }
440 */
441 {
442 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
443 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0xff
444 },
445 22
446 }, {
447 /*
448 * [0] {
449 * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
450 * [0] {
451 * BOOLEAN { FALSE }
452 * }
453 * }
454 */
455 {
456 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
457 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0x00
458 },
459 22
460 }, {
461 /* [1 PRIMITIVE] { "a" } */
462 {
463 0x81, 0x01, 0x61
464 },
465 3
466 }, {
467 /* [1 PRIMITIVE] { "b" } */
468 {
469 0x81, 0x01, 0x62
470 },
471 3
472 }, {
473 /* [2 PRIMITIVE] { "a" } */
474 {
475 0x82, 0x01, 0x61
476 },
477 3
478 }, {
479 /* [2 PRIMITIVE] { "b" } */
480 {
481 0x82, 0x01, 0x62
482 },
483 3
484 }, {
485 /*
486 * [4] {
487 * SEQUENCE {
488 * SET {
489 * SEQUENCE {
490 * # commonName
491 * OBJECT_IDENTIFIER { 2.5.4.3 }
492 * UTF8String { "a" }
493 * }
494 * }
495 * }
496 * }
497 */
498 {
499 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55,
500 0x04, 0x03, 0x0c, 0x01, 0x61
501 },
502 16
503 }, {
504 /*
505 * [4] {
506 * SEQUENCE {
507 * SET {
508 * SEQUENCE {
509 * # commonName
510 * OBJECT_IDENTIFIER { 2.5.4.3 }
511 * UTF8String { "b" }
512 * }
513 * }
514 * }
515 * }
516 */
517 {
518 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55,
519 0x04, 0x03, 0x0c, 0x01, 0x62
520 },
521 16
522 }, {
523 /*
524 * [5] {
525 * [1] {
526 * UTF8String { "a" }
527 * }
528 * }
529 */
530 {
531 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x61
532 },
533 7
534 }, {
535 /*
536 * [5] {
537 * [1] {
538 * UTF8String { "b" }
539 * }
540 * }
541 */
542 {
543 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x62
544 },
545 7
546 }, {
547 /*
548 * [5] {
549 * [0] {
550 * UTF8String {}
551 * }
552 * [1] {
553 * UTF8String { "a" }
554 * }
555 * }
556 */
557 {
558 0xa5, 0x09, 0xa0, 0x02, 0x0c, 0x00, 0xa1, 0x03, 0x0c, 0x01, 0x61
559 },
560 11
561 }, {
562 /*
563 * [5] {
564 * [0] {
565 * UTF8String { "a" }
566 * }
567 * [1] {
568 * UTF8String { "a" }
569 * }
570 * }
571 */
572 {
573 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x61, 0xa1, 0x03, 0x0c, 0x01,
574 0x61
575 },
576 12
577 }, {
578 /*
579 * [5] {
580 * [0] {
581 * UTF8String { "b" }
582 * }
583 * [1] {
584 * UTF8String { "a" }
585 * }
586 * }
587 */
588 {
589 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x62, 0xa1, 0x03, 0x0c, 0x01,
590 0x61
591 },
592 12
593 }, {
594 /* [6 PRIMITIVE] { "a" } */
595 {
596 0x86, 0x01, 0x61
597 },
598 3
599 }, {
600 /* [6 PRIMITIVE] { "b" } */
601 {
602 0x86, 0x01, 0x62
603 },
604 3
605 }, {
606 /* [7 PRIMITIVE] { `11111111` } */
607 {
608 0x87, 0x04, 0x11, 0x11, 0x11, 0x11
609 },
610 6
611 }, {
612 /* [7 PRIMITIVE] { `22222222`} */
613 {
614 0x87, 0x04, 0x22, 0x22, 0x22, 0x22
615 },
616 6
617 }, {
618 /* [7 PRIMITIVE] { `11111111111111111111111111111111` } */
619 {
620 0x87, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
621 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
622 },
623 18
624 }, {
625 /* [7 PRIMITIVE] { `22222222222222222222222222222222` } */
626 {
627 0x87, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
628 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
629 },
630 18
631 }, {
632 /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.1 } */
633 {
634 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84,
635 0xb7, 0x09, 0x02, 0x01
636 },
637 15
638 }, {
639 /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.2 } */
640 {
641 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84,
642 0xb7, 0x09, 0x02, 0x02
643 },
644 15
645 }
646};
647
648static int test_GENERAL_NAME_cmp(void)
649{
650 size_t i, j;
651 GENERAL_NAME **namesa = OPENSSL_malloc(sizeof(*namesa)
652 * OSSL_NELEM(gennames));
653 GENERAL_NAME **namesb = OPENSSL_malloc(sizeof(*namesb)
654 * OSSL_NELEM(gennames));
655 int testresult = 0;
656
657 if (!TEST_ptr(namesa) || !TEST_ptr(namesb))
658 goto end;
659
660 for (i = 0; i < OSSL_NELEM(gennames); i++) {
661 const unsigned char *derp = gennames[i].der;
662
663 /*
664 * We create two versions of each GENERAL_NAME so that we ensure when
665 * we compare them they are always different pointers.
666 */
667 namesa[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen);
668 derp = gennames[i].der;
669 namesb[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen);
670 if (!TEST_ptr(namesa[i]) || !TEST_ptr(namesb[i]))
671 goto end;
672 }
673
674 /* Every name should be equal to itself and not equal to any others. */
675 for (i = 0; i < OSSL_NELEM(gennames); i++) {
676 for (j = 0; j < OSSL_NELEM(gennames); j++) {
677 if (i == j) {
678 if (!TEST_int_eq(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0))
679 goto end;
680 } else {
681 if (!TEST_int_ne(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0))
682 goto end;
683 }
684 }
685 }
686 testresult = 1;
687
688 end:
689 for (i = 0; i < OSSL_NELEM(gennames); i++) {
690 if (namesa != NULL)
691 GENERAL_NAME_free(namesa[i]);
692 if (namesb != NULL)
693 GENERAL_NAME_free(namesb[i]);
694 }
695 OPENSSL_free(namesa);
696 OPENSSL_free(namesb);
697
698 return testresult;
699}
700
ad887416 701int setup_tests(void)
f3ab6c16 702{
ad887416 703 ADD_ALL_TESTS(call_run_cert, OSSL_NELEM(name_fns));
97ab3c4b 704 ADD_TEST(test_GENERAL_NAME_cmp);
ad887416 705 return 1;
0f113f3e 706}