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@", "..",
11 "*.com", "*com", "*.*.com", "*com", "com*", "*example.com",
12 "*@example.com", "test@*.example.com",
13 "example.com", "www.example.com", "test.www.example.com",
14 "*.example.com", "*.www.example.com", "test.*.example.com", "www.*.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] does not match [*.example.com]",
25 "set CN: host: [*.example.com] matches [a.example.com]",
26 "set CN: host: [*.example.com] matches [b.example.com]",
27 "set CN: host: [*.example.com] matches [www.example.com]",
28 "set CN: host: [*.example.com] matches [xn--rger-koa.example.com]",
29 "set CN: host: [test.*.example.com] does not match [test.*.example.com]",
30 "set CN: host: [test.*.example.com] matches [test.www.example.com]",
31 "set CN: host: [*.www.example.com] does not match [*.www.example.com]",
32 "set CN: host: [*.www.example.com] matches [test.www.example.com]",
33 "set emailAddress: email: [postmaster@example.com] does not match [Postmaster@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 dnsName: host: [*.example.com] matches [www.example.com]",
38 "set dnsName: host: [*.example.com] does not match [*.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: [*.www.example.com] does not match [*.www.example.com]",
44 "set dnsName: host: [test.*.example.com] matches [test.www.example.com]",
45 "set dnsName: host: [test.*.example.com] does not match [test.*.example.com]",
46 "set rfc822Name: email: [postmaster@example.com] does not match [Postmaster@example.com]",
47 "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@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]",
53 static int is_exception(const char *msg)
56 for (p = exceptions; *p; ++p)
57 if (strcmp(msg, *p) == 0)
62 static int set_cn(X509 *crt, ...)
74 nid = va_arg(ap, int);
77 name = va_arg(ap, const char *);
78 if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_ASC,
79 (unsigned char *)name,
83 if (!X509_set_subject_name(crt, n))
93 int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
94 X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex,
95 int nid, int crit, ASN1_OCTET_STRING *data);
96 int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
99 static int set_altname(X509 *crt, ...)
102 GENERAL_NAMES *gens = NULL;
103 GENERAL_NAME *gen = NULL;
104 ASN1_IA5STRING *ia5 = NULL;
107 gens = sk_GENERAL_NAME_new_null();
113 type = va_arg(ap, int);
116 name = va_arg(ap, const char *);
118 gen = GENERAL_NAME_new();
121 ia5 = ASN1_IA5STRING_new();
124 if (!ASN1_STRING_set(ia5, name, -1))
130 GENERAL_NAME_set0_value(gen, type, ia5);
136 sk_GENERAL_NAME_push(gens, gen);
139 if (!X509_add1_ext_i2d(crt, NID_subject_alt_name, gens, 0, 0))
143 ASN1_IA5STRING_free(ia5);
144 GENERAL_NAME_free(gen);
145 GENERAL_NAMES_free(gens);
150 static int set_cn1(X509 *crt, const char *name)
152 return set_cn(crt, NID_commonName, name, 0);
156 static int set_cn_and_email(X509 *crt, const char *name)
158 return set_cn(crt, NID_commonName, name,
159 NID_pkcs9_emailAddress, "dummy@example.com", 0);
162 static int set_cn2(X509 *crt, const char *name)
164 return set_cn(crt, NID_commonName, "dummy value",
165 NID_commonName, name, 0);
168 static int set_cn3(X509 *crt, const char *name)
170 return set_cn(crt, NID_commonName, name,
171 NID_commonName, "dummy value", 0);
174 static int set_email1(X509 *crt, const char *name)
176 return set_cn(crt, NID_pkcs9_emailAddress, name, 0);
179 static int set_email2(X509 *crt, const char *name)
181 return set_cn(crt, NID_pkcs9_emailAddress, "dummy@example.com",
182 NID_pkcs9_emailAddress, name, 0);
185 static int set_email3(X509 *crt, const char *name)
187 return set_cn(crt, NID_pkcs9_emailAddress, name,
188 NID_pkcs9_emailAddress, "dummy@example.com", 0);
191 static int set_email_and_cn(X509 *crt, const char *name)
193 return set_cn(crt, NID_pkcs9_emailAddress, name,
194 NID_commonName, "www.example.org", 0);
197 static int set_altname_dns(X509 *crt, const char *name)
199 return set_altname(crt, GEN_DNS, name, 0);
202 static int set_altname_email(X509 *crt, const char *name)
204 return set_altname(crt, GEN_EMAIL, name, 0);
209 int (*fn)(X509 *, const char *);
215 static const struct set_name_fn name_fns[] =
217 {set_cn1, "set CN", 1, 0},
218 {set_cn2, "set CN", 1, 0},
219 {set_cn3, "set CN", 1, 0},
220 {set_cn_and_email, "set CN", 1, 0},
221 {set_email1, "set emailAddress", 0, 1},
222 {set_email2, "set emailAddress", 0, 1},
223 {set_email3, "set emailAddress", 0, 1},
224 {set_email_and_cn, "set emailAddress", 0, 1},
225 {set_altname_dns, "set dnsName", 1, 0},
226 {set_altname_email, "set rfc822Name", 0, 1},
230 static X509 *make_cert()
234 X509_NAME *issuer = NULL;
238 if (!X509_set_version(crt, 3))
243 X509_NAME_free(issuer);
249 static void check_message(const struct set_name_fn *fn, const char *op,
250 const char *nameincert, int match, const char *name)
255 BIO_snprintf(msg, sizeof(msg), "%s: %s: [%s] %s [%s]",
256 fn->name, op, nameincert,
257 match ? "matches" : "does not match", name);
258 if (is_exception(msg))
264 static void run_cert(X509 *crt, const char *nameincert,
265 const struct set_name_fn *fn)
267 const char *const *pname = names;
270 int samename = strcasecmp(nameincert, *pname) == 0;
271 size_t namelen = strlen(*pname);
272 char *name = malloc(namelen);
274 memcpy(name, *pname, namelen);
276 ret = X509_check_host(crt, (const unsigned char *)name,
281 fprintf(stderr, "internal error in X509_check_host");
286 if (ret == 1 && !samename)
288 if (ret == 0 && samename)
293 check_message(fn, "host", nameincert, match, *pname);
295 ret = X509_check_host(crt, (const unsigned char *)name,
296 namelen, X509_CHECK_FLAG_NO_WILDCARDS);
300 fprintf(stderr, "internal error in X509_check_host");
305 if (ret == 1 && !samename)
307 if (ret == 0 && samename)
312 check_message(fn, "host-no-wildcards",
313 nameincert, match, *pname);
315 ret = X509_check_email(crt, (const unsigned char *)name,
320 if (ret && !samename)
322 if (!ret && samename && strchr(nameincert, '@') != NULL)
327 check_message(fn, "email", nameincert, match, *pname);
336 const struct set_name_fn *pfn = name_fns;
338 const char *const *pname = names;
341 X509 *crt = make_cert();
344 fprintf(stderr, "make_cert failed\n");
347 if (!pfn->fn(crt, *pname))
349 fprintf(stderr, "X509 name setting failed\n");
352 run_cert(crt, *pname, pfn);
358 return errors > 0 ? 1 : 0;