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