6 /** A picky certificate name check:
7 * check if the pattern or string in s1 (from a certificate) matches the
8 * hostname (in s2), returns true if matched.
10 * The only place where a wildcard is allowed is in the leftmost
12 int name_match(const char *p1, const char *p2) {
13 const char *const dom = "0123456789.";
16 /* blank patterns never match */
20 /* disallow wildcards in certificates for domain literals
22 if (strspn(p1+(*p1 == '*' ? 1 : 0), dom) == strlen(p1))
25 /* disallow wildcards for domain literals */
26 if (strspn(p2, dom) == strlen(p2))
29 if (wildcard_ok && p1[0] == '*' && p1[1] == '.') {
35 /* make sure CAs don't wildcard top-level domains by requiring there
36 * are at least two dots in wildcarded X.509 CN/SANs */
38 for(tmp = p1; *tmp; tmp += strcspn(tmp, ".")) {
45 if (number_dots >= 2) {
53 return (0 == strcasecmp(p1, p2));
62 /* print test and return true on failure */
63 static int test(const char *p1, const char *p2, int expect) {
64 int match = name_match(p1, p2);
66 printf("name_match(\"%s\", \"%s\") == %d (%d expected)\n", p1, p2, match, expect);
67 return expect != match;
70 int main(int argc, const char **argv) {
73 if (argc > 1 && 0 == strcmp(argv[1], "-v"))
76 rc |= test("example.org", "example.org", 1);
77 rc |= test("*example.org", "foo.example.org", 0);
78 rc |= test("*.example.org", "foo.example.org", 1);
79 rc |= test("*.168.23.23", "192.168.23.23", 0);
80 rc |= test("*.com", "example.com", 0);
82 printf("x509_name_match: ");
83 puts(rc ? "FAIL" : "PASS");