1 #include <openssl/x509.h>
2 #include <openssl/x509v3.h>
6 static const char *const names[] =
8 "a", "b", ".", "*", "@",
9 ".a", "a.", ".b", "b.", ".*", "*.", "*@", "@*", "a@", "@a", "b@", "..",
10 "@@", "**", "*.com", "*com", "*.*.com", "*com", "com*", "*example.com",
11 "*@example.com", "test@*.example.com", "example.com", "www.example.com",
12 "test.www.example.com", "*.example.com", "*.www.example.com",
13 "test.*.example.com", "www.*.com",
14 ".www.example.com", "*www.example.com",
15 "example.net", "xn--rger-koa.example.com",
16 "a.example.com", "b.example.com",
17 "postmaster@example.com", "Postmaster@example.com",
18 "postmaster@EXAMPLE.COM",
22 static const char *const exceptions[] =
24 "set CN: host: [*.example.com] matches [a.example.com]",
25 "set CN: host: [*.example.com] matches [b.example.com]",
26 "set CN: host: [*.example.com] matches [www.example.com]",
27 "set CN: host: [*.example.com] matches [xn--rger-koa.example.com]",
28 "set CN: host: [*.www.example.com] matches [test.www.example.com]",
29 "set CN: host: [*.www.example.com] matches [.www.example.com]",
30 "set CN: host: [*www.example.com] matches [www.example.com]",
31 "set CN: host: [test.www.example.com] matches [.www.example.com]",
32 "set CN: host-no-wildcards: [*.www.example.com] matches [.www.example.com]",
33 "set CN: host-no-wildcards: [test.www.example.com] matches [.www.example.com]",
34 "set emailAddress: email: [postmaster@example.com] does not match [Postmaster@example.com]",
35 "set emailAddress: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]",
36 "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@example.com]",
37 "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]",
38 "set dnsName: host: [*.example.com] matches [www.example.com]",
39 "set dnsName: host: [*.example.com] matches [a.example.com]",
40 "set dnsName: host: [*.example.com] matches [b.example.com]",
41 "set dnsName: host: [*.example.com] matches [xn--rger-koa.example.com]",
42 "set dnsName: host: [*.www.example.com] matches [test.www.example.com]",
43 "set dnsName: host-no-wildcards: [*.www.example.com] matches [.www.example.com]",
44 "set dnsName: host-no-wildcards: [test.www.example.com] matches [.www.example.com]",
45 "set dnsName: host: [*.www.example.com] matches [.www.example.com]",
46 "set dnsName: host: [*www.example.com] matches [www.example.com]",
47 "set dnsName: host: [test.www.example.com] matches [.www.example.com]",
48 "set rfc822Name: email: [postmaster@example.com] does not match [Postmaster@example.com]",
49 "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@example.com]",
50 "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]",
51 "set rfc822Name: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]",
55 static int is_exception(const char *msg)
58 for (p = exceptions; *p; ++p)
59 if (strcmp(msg, *p) == 0)
64 static int set_cn(X509 *crt, ...)
76 nid = va_arg(ap, int);
79 name = va_arg(ap, const char *);
80 if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_ASC,
81 (unsigned char *)name,
85 if (!X509_set_subject_name(crt, n))
95 int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
96 X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex,
97 int nid, int crit, ASN1_OCTET_STRING *data);
98 int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
101 static int set_altname(X509 *crt, ...)
104 GENERAL_NAMES *gens = NULL;
105 GENERAL_NAME *gen = NULL;
106 ASN1_IA5STRING *ia5 = NULL;
109 gens = sk_GENERAL_NAME_new_null();
115 type = va_arg(ap, int);
118 name = va_arg(ap, const char *);
120 gen = GENERAL_NAME_new();
123 ia5 = ASN1_IA5STRING_new();
126 if (!ASN1_STRING_set(ia5, name, -1))
132 GENERAL_NAME_set0_value(gen, type, ia5);
138 sk_GENERAL_NAME_push(gens, gen);
141 if (!X509_add1_ext_i2d(crt, NID_subject_alt_name, gens, 0, 0))
145 ASN1_IA5STRING_free(ia5);
146 GENERAL_NAME_free(gen);
147 GENERAL_NAMES_free(gens);
152 static int set_cn1(X509 *crt, const char *name)
154 return set_cn(crt, NID_commonName, name, 0);
158 static int set_cn_and_email(X509 *crt, const char *name)
160 return set_cn(crt, NID_commonName, name,
161 NID_pkcs9_emailAddress, "dummy@example.com", 0);
164 static int set_cn2(X509 *crt, const char *name)
166 return set_cn(crt, NID_commonName, "dummy value",
167 NID_commonName, name, 0);
170 static int set_cn3(X509 *crt, const char *name)
172 return set_cn(crt, NID_commonName, name,
173 NID_commonName, "dummy value", 0);
176 static int set_email1(X509 *crt, const char *name)
178 return set_cn(crt, NID_pkcs9_emailAddress, name, 0);
181 static int set_email2(X509 *crt, const char *name)
183 return set_cn(crt, NID_pkcs9_emailAddress, "dummy@example.com",
184 NID_pkcs9_emailAddress, name, 0);
187 static int set_email3(X509 *crt, const char *name)
189 return set_cn(crt, NID_pkcs9_emailAddress, name,
190 NID_pkcs9_emailAddress, "dummy@example.com", 0);
193 static int set_email_and_cn(X509 *crt, const char *name)
195 return set_cn(crt, NID_pkcs9_emailAddress, name,
196 NID_commonName, "www.example.org", 0);
199 static int set_altname_dns(X509 *crt, const char *name)
201 return set_altname(crt, GEN_DNS, name, 0);
204 static int set_altname_email(X509 *crt, const char *name)
206 return set_altname(crt, GEN_EMAIL, name, 0);
211 int (*fn)(X509 *, const char *);
217 static const struct set_name_fn name_fns[] =
219 {set_cn1, "set CN", 1, 0},
220 {set_cn2, "set CN", 1, 0},
221 {set_cn3, "set CN", 1, 0},
222 {set_cn_and_email, "set CN", 1, 0},
223 {set_email1, "set emailAddress", 0, 1},
224 {set_email2, "set emailAddress", 0, 1},
225 {set_email3, "set emailAddress", 0, 1},
226 {set_email_and_cn, "set emailAddress", 0, 1},
227 {set_altname_dns, "set dnsName", 1, 0},
228 {set_altname_email, "set rfc822Name", 0, 1},
232 static X509 *make_cert()
236 X509_NAME *issuer = NULL;
240 if (!X509_set_version(crt, 3))
245 X509_NAME_free(issuer);
251 static void check_message(const struct set_name_fn *fn, const char *op,
252 const char *nameincert, int match, const char *name)
257 BIO_snprintf(msg, sizeof(msg), "%s: %s: [%s] %s [%s]",
258 fn->name, op, nameincert,
259 match ? "matches" : "does not match", name);
260 if (is_exception(msg))
266 static void run_cert(X509 *crt, const char *nameincert,
267 const struct set_name_fn *fn)
269 const char *const *pname = names;
272 int samename = strcasecmp(nameincert, *pname) == 0;
273 size_t namelen = strlen(*pname);
274 char *name = malloc(namelen);
276 memcpy(name, *pname, namelen);
278 ret = X509_check_host(crt, (const unsigned char *)name,
283 fprintf(stderr, "internal error in X509_check_host");
288 if (ret == 1 && !samename)
290 if (ret == 0 && samename)
295 check_message(fn, "host", nameincert, match, *pname);
297 ret = X509_check_host(crt, (const unsigned char *)name,
298 namelen, X509_CHECK_FLAG_NO_WILDCARDS);
302 fprintf(stderr, "internal error in X509_check_host");
307 if (ret == 1 && !samename)
309 if (ret == 0 && samename)
314 check_message(fn, "host-no-wildcards",
315 nameincert, match, *pname);
317 ret = X509_check_email(crt, (const unsigned char *)name,
322 if (ret && !samename)
324 if (!ret && samename && strchr(nameincert, '@') != NULL)
329 check_message(fn, "email", nameincert, match, *pname);
338 const struct set_name_fn *pfn = name_fns;
340 const char *const *pname = names;
343 X509 *crt = make_cert();
346 fprintf(stderr, "make_cert failed\n");
349 if (!pfn->fn(crt, *pname))
351 fprintf(stderr, "X509 name setting failed\n");
354 run_cert(crt, *pname, pfn);
360 return errors > 0 ? 1 : 0;