Update the test framework so that the need for test_main is removed. Everything
[openssl.git] / test / v3nametest.c
1 /*
2  * Copyright 2012-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
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
10 #include <string.h>
11 #include "e_os.h"
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include "testutil.h"
15
16 static const char *const names[] = {
17     "a", "b", ".", "*", "@",
18     ".a", "a.", ".b", "b.", ".*", "*.", "*@", "@*", "a@", "@a", "b@", "..",
19     "-example.com", "example-.com",
20     "@@", "**", "*.com", "*com", "*.*.com", "*com", "com*", "*example.com",
21     "*@example.com", "test@*.example.com", "example.com", "www.example.com",
22     "test.www.example.com", "*.example.com", "*.www.example.com",
23     "test.*.example.com", "www.*.com",
24     ".www.example.com", "*www.example.com",
25     "example.net", "xn--rger-koa.example.com",
26     "*.xn--rger-koa.example.com", "www.xn--rger-koa.example.com",
27     "*.good--example.com", "www.good--example.com",
28     "*.xn--bar.com", "xn--foo.xn--bar.com",
29     "a.example.com", "b.example.com",
30     "postmaster@example.com", "Postmaster@example.com",
31     "postmaster@EXAMPLE.COM",
32     NULL
33 };
34
35 static const char *const exceptions[] = {
36     "set CN: host: [*.example.com] matches [a.example.com]",
37     "set CN: host: [*.example.com] matches [b.example.com]",
38     "set CN: host: [*.example.com] matches [www.example.com]",
39     "set CN: host: [*.example.com] matches [xn--rger-koa.example.com]",
40     "set CN: host: [*.www.example.com] matches [test.www.example.com]",
41     "set CN: host: [*.www.example.com] matches [.www.example.com]",
42     "set CN: host: [*www.example.com] matches [www.example.com]",
43     "set CN: host: [test.www.example.com] matches [.www.example.com]",
44     "set CN: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]",
45     "set CN: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]",
46     "set CN: host: [*.good--example.com] matches [www.good--example.com]",
47     "set CN: host-no-wildcards: [*.www.example.com] matches [.www.example.com]",
48     "set CN: host-no-wildcards: [test.www.example.com] matches [.www.example.com]",
49     "set emailAddress: email: [postmaster@example.com] does not match [Postmaster@example.com]",
50     "set emailAddress: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@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 dnsName: host: [*.example.com] matches [www.example.com]",
54     "set dnsName: host: [*.example.com] matches [a.example.com]",
55     "set dnsName: host: [*.example.com] matches [b.example.com]",
56     "set dnsName: host: [*.example.com] matches [xn--rger-koa.example.com]",
57     "set dnsName: host: [*.www.example.com] matches [test.www.example.com]",
58     "set dnsName: host-no-wildcards: [*.www.example.com] matches [.www.example.com]",
59     "set dnsName: host-no-wildcards: [test.www.example.com] matches [.www.example.com]",
60     "set dnsName: host: [*.www.example.com] matches [.www.example.com]",
61     "set dnsName: host: [*www.example.com] matches [www.example.com]",
62     "set dnsName: host: [test.www.example.com] matches [.www.example.com]",
63     "set dnsName: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]",
64     "set dnsName: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]",
65     "set dnsName: host: [*.good--example.com] matches [www.good--example.com]",
66     "set rfc822Name: email: [postmaster@example.com] does not match [Postmaster@example.com]",
67     "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@example.com]",
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     NULL
71 };
72
73 static int is_exception(const char *msg)
74 {
75     const char *const *p;
76
77     for (p = exceptions; *p; ++p)
78         if (strcmp(msg, *p) == 0)
79             return 1;
80     return 0;
81 }
82
83 static int set_cn(X509 *crt, ...)
84 {
85     int ret = 0;
86     X509_NAME *n = NULL;
87     va_list ap;
88
89     va_start(ap, crt);
90     n = X509_NAME_new();
91     if (n == NULL)
92         goto out;
93
94     while (1) {
95         int nid;
96         const char *name;
97
98         nid = va_arg(ap, int);
99         if (nid == 0)
100             break;
101         name = va_arg(ap, const char *);
102         if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_ASC,
103                                         (unsigned char *)name, -1, -1, 1))
104             goto out;
105     }
106     if (!X509_set_subject_name(crt, n))
107         goto out;
108     ret = 1;
109  out:
110     X509_NAME_free(n);
111     va_end(ap);
112     return ret;
113 }
114
115 /*-
116 int             X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
117 X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex,
118                         int nid, int crit, ASN1_OCTET_STRING *data);
119 int             X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
120 */
121
122 static int set_altname(X509 *crt, ...)
123 {
124     int ret = 0;
125     GENERAL_NAMES *gens = NULL;
126     GENERAL_NAME *gen = NULL;
127     ASN1_IA5STRING *ia5 = NULL;
128     va_list ap;
129     va_start(ap, crt);
130     gens = sk_GENERAL_NAME_new_null();
131     if (gens == NULL)
132         goto out;
133     while (1) {
134         int type;
135         const char *name;
136         type = va_arg(ap, int);
137         if (type == 0)
138             break;
139         name = va_arg(ap, const char *);
140
141         gen = GENERAL_NAME_new();
142         if (gen == NULL)
143             goto out;
144         ia5 = ASN1_IA5STRING_new();
145         if (ia5 == NULL)
146             goto out;
147         if (!ASN1_STRING_set(ia5, name, -1))
148             goto out;
149         switch (type) {
150         case GEN_EMAIL:
151         case GEN_DNS:
152             GENERAL_NAME_set0_value(gen, type, ia5);
153             ia5 = NULL;
154             break;
155         default:
156             abort();
157         }
158         sk_GENERAL_NAME_push(gens, gen);
159         gen = NULL;
160     }
161     if (!X509_add1_ext_i2d(crt, NID_subject_alt_name, gens, 0, 0))
162         goto out;
163     ret = 1;
164  out:
165     ASN1_IA5STRING_free(ia5);
166     GENERAL_NAME_free(gen);
167     GENERAL_NAMES_free(gens);
168     va_end(ap);
169     return ret;
170 }
171
172 static int set_cn1(X509 *crt, const char *name)
173 {
174     return set_cn(crt, NID_commonName, name, 0);
175 }
176
177 static int set_cn_and_email(X509 *crt, const char *name)
178 {
179     return set_cn(crt, NID_commonName, name,
180                   NID_pkcs9_emailAddress, "dummy@example.com", 0);
181 }
182
183 static int set_cn2(X509 *crt, const char *name)
184 {
185     return set_cn(crt, NID_commonName, "dummy value",
186                   NID_commonName, name, 0);
187 }
188
189 static int set_cn3(X509 *crt, const char *name)
190 {
191     return set_cn(crt, NID_commonName, name,
192                   NID_commonName, "dummy value", 0);
193 }
194
195 static int set_email1(X509 *crt, const char *name)
196 {
197     return set_cn(crt, NID_pkcs9_emailAddress, name, 0);
198 }
199
200 static int set_email2(X509 *crt, const char *name)
201 {
202     return set_cn(crt, NID_pkcs9_emailAddress, "dummy@example.com",
203                   NID_pkcs9_emailAddress, name, 0);
204 }
205
206 static int set_email3(X509 *crt, const char *name)
207 {
208     return set_cn(crt, NID_pkcs9_emailAddress, name,
209                   NID_pkcs9_emailAddress, "dummy@example.com", 0);
210 }
211
212 static int set_email_and_cn(X509 *crt, const char *name)
213 {
214     return set_cn(crt, NID_pkcs9_emailAddress, name,
215                   NID_commonName, "www.example.org", 0);
216 }
217
218 static int set_altname_dns(X509 *crt, const char *name)
219 {
220     return set_altname(crt, GEN_DNS, name, 0);
221 }
222
223 static int set_altname_email(X509 *crt, const char *name)
224 {
225     return set_altname(crt, GEN_EMAIL, name, 0);
226 }
227
228 struct set_name_fn {
229     int (*fn) (X509 *, const char *);
230     const char *name;
231     int host;
232     int email;
233 };
234
235 static const struct set_name_fn name_fns[] = {
236     {set_cn1, "set CN", 1, 0},
237     {set_cn2, "set CN", 1, 0},
238     {set_cn3, "set CN", 1, 0},
239     {set_cn_and_email, "set CN", 1, 0},
240     {set_email1, "set emailAddress", 0, 1},
241     {set_email2, "set emailAddress", 0, 1},
242     {set_email3, "set emailAddress", 0, 1},
243     {set_email_and_cn, "set emailAddress", 0, 1},
244     {set_altname_dns, "set dnsName", 1, 0},
245     {set_altname_email, "set rfc822Name", 0, 1},
246 };
247
248 static X509 *make_cert()
249 {
250     X509 *crt = NULL;
251
252     if (!TEST_ptr(crt = X509_new()))
253         return NULL;
254     if (!TEST_true(X509_set_version(crt, 2))) {
255         X509_free(crt);
256         return NULL;
257     }
258     return crt;
259 }
260
261 static int check_message(const struct set_name_fn *fn, const char *op,
262                          const char *nameincert, int match, const char *name)
263 {
264     char msg[1024];
265
266     if (match < 0)
267         return 1;
268     BIO_snprintf(msg, sizeof(msg), "%s: %s: [%s] %s [%s]",
269                  fn->name, op, nameincert,
270                  match ? "matches" : "does not match", name);
271     if (is_exception(msg))
272         return 1;
273     TEST_error("%s", msg);
274     return 0;
275 }
276
277 static int run_cert(X509 *crt, const char *nameincert,
278                      const struct set_name_fn *fn)
279 {
280     const char *const *pname = names;
281     int failed = 0;
282
283     for (; *pname != NULL; ++pname) {
284         int samename = strcasecmp(nameincert, *pname) == 0;
285         size_t namelen = strlen(*pname);
286         char *name = OPENSSL_malloc(namelen);
287         int match, ret;
288
289         memcpy(name, *pname, namelen);
290
291         match = -1;
292         if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen, 0, NULL),
293                          0)) {
294             failed = 1;
295         } else if (fn->host) {
296             if (ret == 1 && !samename)
297                 match = 1;
298             if (ret == 0 && samename)
299                 match = 0;
300         } else if (ret == 1)
301             match = 1;
302         if (!TEST_true(check_message(fn, "host", nameincert, match, *pname)))
303             failed = 1;
304
305         match = -1;
306         if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen,
307                                                X509_CHECK_FLAG_NO_WILDCARDS,
308                                                NULL), 0)) {
309             failed = 1;
310         } else if (fn->host) {
311             if (ret == 1 && !samename)
312                 match = 1;
313             if (ret == 0 && samename)
314                 match = 0;
315         } else if (ret == 1)
316             match = 1;
317         if (!TEST_true(check_message(fn, "host-no-wildcards",
318                                      nameincert, match, *pname)))
319             failed = 1;
320
321         match = -1;
322         ret = X509_check_email(crt, name, namelen, 0);
323         if (fn->email) {
324             if (ret && !samename)
325                 match = 1;
326             if (!ret && samename && strchr(nameincert, '@') != NULL)
327                 match = 0;
328         } else if (ret)
329             match = 1;
330         if (!TEST_true(check_message(fn, "email", nameincert, match, *pname)))
331             failed = 1;
332         OPENSSL_free(name);
333     }
334
335     return failed == 0;
336 }
337
338 static int call_run_cert(int i)
339 {
340     int failed = 0;
341     const struct set_name_fn *pfn = &name_fns[i];
342     X509 *crt;
343     const char *const *pname;
344
345     TEST_info("%s", pfn->name);
346     for (pname = names; *pname != NULL; pname++) {
347         if (!TEST_ptr(crt = make_cert())
348              || !TEST_true(pfn->fn(crt, *pname))
349              || !run_cert(crt, *pname, pfn))
350             failed = 1;
351         X509_free(crt);
352     }
353     return failed == 0;
354 }
355
356 int setup_tests(void)
357 {
358     ADD_ALL_TESTS(call_run_cert, OSSL_NELEM(name_fns));
359     return 1;
360 }