2
0

hostcheck.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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. #include "curl_setup.h"
  23. #if defined(USE_SSLEAY) || defined(USE_AXTLS) || defined(USE_QSOSSL) || \
  24. defined(USE_GSKIT)
  25. /* these backends use functions from this file */
  26. #include "hostcheck.h"
  27. #include "rawstr.h"
  28. /*
  29. * Match a hostname against a wildcard pattern.
  30. * E.g.
  31. * "foo.host.com" matches "*.host.com".
  32. *
  33. * We use the matching rule described in RFC6125, section 6.4.3.
  34. * http://tools.ietf.org/html/rfc6125#section-6.4.3
  35. */
  36. static int hostmatch(const char *hostname, const char *pattern)
  37. {
  38. const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
  39. int wildcard_enabled;
  40. size_t prefixlen, suffixlen;
  41. pattern_wildcard = strchr(pattern, '*');
  42. if(pattern_wildcard == NULL)
  43. return Curl_raw_equal(pattern, hostname) ?
  44. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  45. /* We require at least 2 dots in pattern to avoid too wide wildcard
  46. match. */
  47. wildcard_enabled = 1;
  48. pattern_label_end = strchr(pattern, '.');
  49. if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
  50. pattern_wildcard > pattern_label_end ||
  51. Curl_raw_nequal(pattern, "xn--", 4)) {
  52. wildcard_enabled = 0;
  53. }
  54. if(!wildcard_enabled)
  55. return Curl_raw_equal(pattern, hostname) ?
  56. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  57. hostname_label_end = strchr(hostname, '.');
  58. if(hostname_label_end == NULL ||
  59. !Curl_raw_equal(pattern_label_end, hostname_label_end))
  60. return CURL_HOST_NOMATCH;
  61. /* The wildcard must match at least one character, so the left-most
  62. label of the hostname is at least as large as the left-most label
  63. of the pattern. */
  64. if(hostname_label_end - hostname < pattern_label_end - pattern)
  65. return CURL_HOST_NOMATCH;
  66. prefixlen = pattern_wildcard - pattern;
  67. suffixlen = pattern_label_end - (pattern_wildcard+1);
  68. return Curl_raw_nequal(pattern, hostname, prefixlen) &&
  69. Curl_raw_nequal(pattern_wildcard+1, hostname_label_end - suffixlen,
  70. suffixlen) ?
  71. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  72. }
  73. int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
  74. {
  75. if(!match_pattern || !*match_pattern ||
  76. !hostname || !*hostname) /* sanity check */
  77. return 0;
  78. if(Curl_raw_equal(hostname, match_pattern)) /* trivial case */
  79. return 1;
  80. if(hostmatch(hostname,match_pattern) == CURL_HOST_MATCH)
  81. return 1;
  82. return 0;
  83. }
  84. #endif /* SSLEAY or AXTLS or QSOSSL or GSKIT */