v3nametest.c 11 KB

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