curl_hostcheck.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* This file is an amalgamation of hostcheck.c and most of rawstr.c
  23. from cURL. The contents of the COPYING file mentioned above are:
  24. COPYRIGHT AND PERMISSION NOTICE
  25. Copyright (c) 1996 - 2013, Daniel Stenberg, <daniel@haxx.se>.
  26. All rights reserved.
  27. Permission to use, copy, modify, and distribute this software for any purpose
  28. with or without fee is hereby granted, provided that the above copyright
  29. notice and this permission notice appear in all copies.
  30. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
  33. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  34. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  35. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  36. OR OTHER DEALINGS IN THE SOFTWARE.
  37. Except as contained in this notice, the name of a copyright holder shall not
  38. be used in advertising or otherwise to promote the sale, use or other dealings
  39. in this Software without prior written authorization of the copyright holder.
  40. */
  41. #include "curl_hostcheck.h"
  42. #include <string.h>
  43. /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
  44. its behavior is altered by the current locale. */
  45. static char Curl_raw_toupper(char in)
  46. {
  47. switch (in) {
  48. case 'a':
  49. return 'A';
  50. case 'b':
  51. return 'B';
  52. case 'c':
  53. return 'C';
  54. case 'd':
  55. return 'D';
  56. case 'e':
  57. return 'E';
  58. case 'f':
  59. return 'F';
  60. case 'g':
  61. return 'G';
  62. case 'h':
  63. return 'H';
  64. case 'i':
  65. return 'I';
  66. case 'j':
  67. return 'J';
  68. case 'k':
  69. return 'K';
  70. case 'l':
  71. return 'L';
  72. case 'm':
  73. return 'M';
  74. case 'n':
  75. return 'N';
  76. case 'o':
  77. return 'O';
  78. case 'p':
  79. return 'P';
  80. case 'q':
  81. return 'Q';
  82. case 'r':
  83. return 'R';
  84. case 's':
  85. return 'S';
  86. case 't':
  87. return 'T';
  88. case 'u':
  89. return 'U';
  90. case 'v':
  91. return 'V';
  92. case 'w':
  93. return 'W';
  94. case 'x':
  95. return 'X';
  96. case 'y':
  97. return 'Y';
  98. case 'z':
  99. return 'Z';
  100. }
  101. return in;
  102. }
  103. /*
  104. * Curl_raw_equal() is for doing "raw" case insensitive strings. This is meant
  105. * to be locale independent and only compare strings we know are safe for
  106. * this. See http://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
  107. * some further explanation to why this function is necessary.
  108. *
  109. * The function is capable of comparing a-z case insensitively even for
  110. * non-ascii.
  111. */
  112. static int Curl_raw_equal(const char *first, const char *second)
  113. {
  114. while(*first && *second) {
  115. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
  116. /* get out of the loop as soon as they don't match */
  117. break;
  118. first++;
  119. second++;
  120. }
  121. /* we do the comparison here (possibly again), just to make sure that if the
  122. loop above is skipped because one of the strings reached zero, we must not
  123. return this as a successful match */
  124. return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
  125. }
  126. static int Curl_raw_nequal(const char *first, const char *second, size_t max)
  127. {
  128. while(*first && *second && max) {
  129. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
  130. break;
  131. }
  132. max--;
  133. first++;
  134. second++;
  135. }
  136. if(0 == max)
  137. return 1; /* they are equal this far */
  138. return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
  139. }
  140. /*
  141. * Match a hostname against a wildcard pattern.
  142. * E.g.
  143. * "foo.host.com" matches "*.host.com".
  144. *
  145. * We use the matching rule described in RFC6125, section 6.4.3.
  146. * http://tools.ietf.org/html/rfc6125#section-6.4.3
  147. */
  148. static int hostmatch(const char *hostname, const char *pattern)
  149. {
  150. const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
  151. int wildcard_enabled;
  152. size_t prefixlen, suffixlen;
  153. pattern_wildcard = strchr(pattern, '*');
  154. if(pattern_wildcard == NULL)
  155. return Curl_raw_equal(pattern, hostname) ?
  156. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  157. /* We require at least 2 dots in pattern to avoid too wide wildcard
  158. match. */
  159. wildcard_enabled = 1;
  160. pattern_label_end = strchr(pattern, '.');
  161. if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
  162. pattern_wildcard > pattern_label_end ||
  163. Curl_raw_nequal(pattern, "xn--", 4)) {
  164. wildcard_enabled = 0;
  165. }
  166. if(!wildcard_enabled)
  167. return Curl_raw_equal(pattern, hostname) ?
  168. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  169. hostname_label_end = strchr(hostname, '.');
  170. if(hostname_label_end == NULL ||
  171. !Curl_raw_equal(pattern_label_end, hostname_label_end))
  172. return CURL_HOST_NOMATCH;
  173. /* The wildcard must match at least one character, so the left-most
  174. label of the hostname is at least as large as the left-most label
  175. of the pattern. */
  176. if(hostname_label_end - hostname < pattern_label_end - pattern)
  177. return CURL_HOST_NOMATCH;
  178. prefixlen = pattern_wildcard - pattern;
  179. suffixlen = pattern_label_end - (pattern_wildcard+1);
  180. return Curl_raw_nequal(pattern, hostname, prefixlen) &&
  181. Curl_raw_nequal(pattern_wildcard+1, hostname_label_end - suffixlen,
  182. suffixlen) ?
  183. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  184. }
  185. int Tool_Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
  186. {
  187. if(!match_pattern || !*match_pattern ||
  188. !hostname || !*hostname) /* sanity check */
  189. return 0;
  190. if(Curl_raw_equal(hostname, match_pattern)) /* trivial case */
  191. return 1;
  192. if(hostmatch(hostname,match_pattern) == CURL_HOST_MATCH)
  193. return 1;
  194. return 0;
  195. }