hostname.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Go-OpenSSL notice:
  3. * This file is required for all OpenSSL versions prior to 1.1.0. This simply
  4. * provides the new 1.1.0 X509_check_* methods for hostname validation if they
  5. * don't already exist.
  6. */
  7. #include <openssl/x509.h>
  8. #ifndef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
  9. /* portions from x509v3.h and v3_utl.c */
  10. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  11. * project.
  12. */
  13. /* ====================================================================
  14. * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. *
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. *
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. *
  28. * 3. All advertising materials mentioning features or use of this
  29. * software must display the following acknowledgment:
  30. * "This product includes software developed by the OpenSSL Project
  31. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  32. *
  33. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  34. * endorse or promote products derived from this software without
  35. * prior written permission. For written permission, please contact
  36. * licensing@OpenSSL.org.
  37. *
  38. * 5. Products derived from this software may not be called "OpenSSL"
  39. * nor may "OpenSSL" appear in their names without prior written
  40. * permission of the OpenSSL Project.
  41. *
  42. * 6. Redistributions of any form whatsoever must retain the following
  43. * acknowledgment:
  44. * "This product includes software developed by the OpenSSL Project
  45. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  48. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  51. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  56. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  57. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  58. * OF THE POSSIBILITY OF SUCH DAMAGE.
  59. * ====================================================================
  60. *
  61. * This product includes cryptographic software written by Eric Young
  62. * (eay@cryptsoft.com). This product includes software written by Tim
  63. * Hudson (tjh@cryptsoft.com).
  64. *
  65. */
  66. /* X509 v3 extension utilities */
  67. #include <string.h>
  68. #include <stdlib.h>
  69. #include <openssl/ssl.h>
  70. #include <openssl/conf.h>
  71. #include <openssl/x509v3.h>
  72. #define X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT 0x1
  73. #define X509_CHECK_FLAG_NO_WILDCARDS 0x2
  74. typedef int (*equal_fn)(const unsigned char *pattern, size_t pattern_len,
  75. const unsigned char *subject, size_t subject_len);
  76. /* Compare while ASCII ignoring case. */
  77. static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
  78. const unsigned char *subject, size_t subject_len)
  79. {
  80. if (pattern_len != subject_len)
  81. return 0;
  82. while (pattern_len)
  83. {
  84. unsigned char l = *pattern;
  85. unsigned char r = *subject;
  86. /* The pattern must not contain NUL characters. */
  87. if (l == 0)
  88. return 0;
  89. if (l != r)
  90. {
  91. if ('A' <= l && l <= 'Z')
  92. l = (l - 'A') + 'a';
  93. if ('A' <= r && r <= 'Z')
  94. r = (r - 'A') + 'a';
  95. if (l != r)
  96. return 0;
  97. }
  98. ++pattern;
  99. ++subject;
  100. --pattern_len;
  101. }
  102. return 1;
  103. }
  104. /* Compare using memcmp. */
  105. static int equal_case(const unsigned char *pattern, size_t pattern_len,
  106. const unsigned char *subject, size_t subject_len)
  107. {
  108. /* The pattern must not contain NUL characters. */
  109. if (memchr(pattern, '\0', pattern_len) != NULL)
  110. return 0;
  111. if (pattern_len != subject_len)
  112. return 0;
  113. return !memcmp(pattern, subject, pattern_len);
  114. }
  115. /* RFC 5280, section 7.5, requires that only the domain is compared in
  116. a case-insensitive manner. */
  117. static int equal_email(const unsigned char *a, size_t a_len,
  118. const unsigned char *b, size_t b_len)
  119. {
  120. size_t i = a_len;
  121. if (a_len != b_len)
  122. return 0;
  123. /* We search backwards for the '@' character, so that we do
  124. not have to deal with quoted local-parts. The domain part
  125. is compared in a case-insensitive manner. */
  126. while (i > 0)
  127. {
  128. --i;
  129. if (a[i] == '@' || b[i] == '@')
  130. {
  131. if (!equal_nocase(a + i, a_len - i,
  132. b + i, a_len - i))
  133. return 0;
  134. break;
  135. }
  136. }
  137. if (i == 0)
  138. i = a_len;
  139. return equal_case(a, i, b, i);
  140. }
  141. /* Compare the prefix and suffix with the subject, and check that the
  142. characters in-between are valid. */
  143. static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
  144. const unsigned char *suffix, size_t suffix_len,
  145. const unsigned char *subject, size_t subject_len)
  146. {
  147. const unsigned char *wildcard_start;
  148. const unsigned char *wildcard_end;
  149. const unsigned char *p;
  150. if (subject_len < prefix_len + suffix_len)
  151. return 0;
  152. if (!equal_nocase(prefix, prefix_len, subject, prefix_len))
  153. return 0;
  154. wildcard_start = subject + prefix_len;
  155. wildcard_end = subject + (subject_len - suffix_len);
  156. if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len))
  157. return 0;
  158. /* The wildcard must match at least one character. */
  159. if (wildcard_start == wildcard_end)
  160. return 0;
  161. /* Check that the part matched by the wildcard contains only
  162. permitted characters and only matches a single label. */
  163. for (p = wildcard_start; p != wildcard_end; ++p)
  164. if (!(('0' <= *p && *p <= '9') ||
  165. ('A' <= *p && *p <= 'Z') ||
  166. ('a' <= *p && *p <= 'z') ||
  167. *p == '-'))
  168. return 0;
  169. return 1;
  170. }
  171. /* Checks if the memory region consistens of [0-9A-Za-z.-]. */
  172. static int valid_domain_characters(const unsigned char *p, size_t len)
  173. {
  174. while (len)
  175. {
  176. if (!(('0' <= *p && *p <= '9') ||
  177. ('A' <= *p && *p <= 'Z') ||
  178. ('a' <= *p && *p <= 'z') ||
  179. *p == '-' || *p == '.'))
  180. return 0;
  181. ++p;
  182. --len;
  183. }
  184. return 1;
  185. }
  186. /* Find the '*' in a wildcard pattern. If no such character is found
  187. or the pattern is otherwise invalid, returns NULL. */
  188. static const unsigned char *wildcard_find_star(const unsigned char *pattern,
  189. size_t pattern_len)
  190. {
  191. const unsigned char *star = memchr(pattern, '*', pattern_len);
  192. size_t dot_count = 0;
  193. const unsigned char *suffix_start;
  194. size_t suffix_length;
  195. if (star == NULL)
  196. return NULL;
  197. suffix_start = star + 1;
  198. suffix_length = (pattern + pattern_len) - (star + 1);
  199. if (!(valid_domain_characters(pattern, star - pattern) &&
  200. valid_domain_characters(suffix_start, suffix_length)))
  201. return NULL;
  202. /* Check that the suffix matches at least two labels. */
  203. while (suffix_length)
  204. {
  205. if (*suffix_start == '.')
  206. ++dot_count;
  207. ++suffix_start;
  208. --suffix_length;
  209. }
  210. if (dot_count < 2)
  211. return NULL;
  212. return star;
  213. }
  214. /* Compare using wildcards. */
  215. static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
  216. const unsigned char *subject, size_t subject_len)
  217. {
  218. const unsigned char *star = wildcard_find_star(pattern, pattern_len);
  219. if (star == NULL)
  220. return equal_nocase(pattern, pattern_len,
  221. subject, subject_len);
  222. return wildcard_match(pattern, star - pattern,
  223. star + 1, (pattern + pattern_len) - star - 1,
  224. subject, subject_len);
  225. }
  226. /* Compare an ASN1_STRING to a supplied string. If they match
  227. * return 1. If cmp_type > 0 only compare if string matches the
  228. * type, otherwise convert it to UTF8.
  229. */
  230. static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
  231. const unsigned char *b, size_t blen)
  232. {
  233. if (!a->data || !a->length)
  234. return 0;
  235. if (cmp_type > 0)
  236. {
  237. if (cmp_type != a->type)
  238. return 0;
  239. if (cmp_type == V_ASN1_IA5STRING)
  240. return equal(a->data, a->length, b, blen);
  241. if (a->length == (int)blen && !memcmp(a->data, b, blen))
  242. return 1;
  243. else
  244. return 0;
  245. }
  246. else
  247. {
  248. int astrlen, rv;
  249. unsigned char *astr;
  250. astrlen = ASN1_STRING_to_UTF8(&astr, a);
  251. if (astrlen < 0)
  252. return -1;
  253. rv = equal(astr, astrlen, b, blen);
  254. OPENSSL_free(astr);
  255. return rv;
  256. }
  257. }
  258. static int do_x509_check(X509 *x, const unsigned char *chk, size_t chklen,
  259. unsigned int flags, int check_type)
  260. {
  261. STACK_OF(GENERAL_NAME) *gens = NULL;
  262. X509_NAME *name = NULL;
  263. int i;
  264. int cnid;
  265. int alt_type;
  266. equal_fn equal;
  267. if (check_type == GEN_EMAIL)
  268. {
  269. cnid = NID_pkcs9_emailAddress;
  270. alt_type = V_ASN1_IA5STRING;
  271. equal = equal_email;
  272. }
  273. else if (check_type == GEN_DNS)
  274. {
  275. cnid = NID_commonName;
  276. alt_type = V_ASN1_IA5STRING;
  277. if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
  278. equal = equal_nocase;
  279. else
  280. equal = equal_wildcard;
  281. }
  282. else
  283. {
  284. cnid = 0;
  285. alt_type = V_ASN1_OCTET_STRING;
  286. equal = equal_case;
  287. }
  288. if (chklen == 0)
  289. chklen = strlen((const char *)chk);
  290. gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  291. if (gens)
  292. {
  293. int rv = 0;
  294. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++)
  295. {
  296. GENERAL_NAME *gen;
  297. ASN1_STRING *cstr;
  298. gen = sk_GENERAL_NAME_value(gens, i);
  299. if(gen->type != check_type)
  300. continue;
  301. if (check_type == GEN_EMAIL)
  302. cstr = gen->d.rfc822Name;
  303. else if (check_type == GEN_DNS)
  304. cstr = gen->d.dNSName;
  305. else
  306. cstr = gen->d.iPAddress;
  307. if (do_check_string(cstr, alt_type, equal, chk, chklen))
  308. {
  309. rv = 1;
  310. break;
  311. }
  312. }
  313. GENERAL_NAMES_free(gens);
  314. if (rv)
  315. return 1;
  316. if (!(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) || !cnid)
  317. return 0;
  318. }
  319. i = -1;
  320. name = X509_get_subject_name(x);
  321. while((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0)
  322. {
  323. X509_NAME_ENTRY *ne;
  324. ASN1_STRING *str;
  325. ne = X509_NAME_get_entry(name, i);
  326. str = X509_NAME_ENTRY_get_data(ne);
  327. if (do_check_string(str, -1, equal, chk, chklen))
  328. return 1;
  329. }
  330. return 0;
  331. }
  332. #if OPENSSL_VERSION_NUMBER < 0x1000200fL
  333. int X509_check_host(X509 *x, const unsigned char *chk, size_t chklen,
  334. unsigned int flags, char **peername)
  335. {
  336. return do_x509_check(x, chk, chklen, flags, GEN_DNS);
  337. }
  338. int X509_check_email(X509 *x, const unsigned char *chk, size_t chklen,
  339. unsigned int flags)
  340. {
  341. return do_x509_check(x, chk, chklen, flags, GEN_EMAIL);
  342. }
  343. int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
  344. unsigned int flags)
  345. {
  346. return do_x509_check(x, chk, chklen, flags, GEN_IPADD);
  347. }
  348. #endif /* OPENSSL_VERSION_NUMBER < 0x1000200fL */
  349. #endif