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