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