hostcheck.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #include "hostcheck.h"
  30. #include "rawstr.h"
  31. #include "inet_pton.h"
  32. #include "curl_memory.h"
  33. /* The last #include file should be: */
  34. #include "memdebug.h"
  35. /*
  36. * Match a hostname against a wildcard pattern.
  37. * E.g.
  38. * "foo.host.com" matches "*.host.com".
  39. *
  40. * We use the matching rule described in RFC6125, section 6.4.3.
  41. * http://tools.ietf.org/html/rfc6125#section-6.4.3
  42. *
  43. * In addition: ignore trailing dots in the host names and wildcards, so that
  44. * the names are used normalized. This is what the browsers do.
  45. *
  46. * Do not allow wildcard matching on IP numbers. There are apparently
  47. * certificates being used with an IP address in the CN field, thus making no
  48. * apparent distinction between a name and an IP. We need to detect the use of
  49. * an IP address and not wildcard match on such names.
  50. *
  51. * NOTE: hostmatch() gets called with copied buffers so that it can modify the
  52. * contents at will.
  53. */
  54. static int hostmatch(char *hostname, char *pattern)
  55. {
  56. const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
  57. int wildcard_enabled;
  58. size_t prefixlen, suffixlen;
  59. struct in_addr ignored;
  60. #ifdef ENABLE_IPV6
  61. struct sockaddr_in6 si6;
  62. #endif
  63. /* normalize pattern and hostname by stripping off trailing dots */
  64. size_t len = strlen(hostname);
  65. if(hostname[len-1]=='.')
  66. hostname[len-1]=0;
  67. len = strlen(pattern);
  68. if(pattern[len-1]=='.')
  69. pattern[len-1]=0;
  70. pattern_wildcard = strchr(pattern, '*');
  71. if(pattern_wildcard == NULL)
  72. return Curl_raw_equal(pattern, hostname) ?
  73. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  74. /* detect IP address as hostname and fail the match if so */
  75. if(Curl_inet_pton(AF_INET, hostname, &ignored) > 0)
  76. return CURL_HOST_NOMATCH;
  77. #ifdef ENABLE_IPV6
  78. else if(Curl_inet_pton(AF_INET6, hostname, &si6.sin6_addr) > 0)
  79. return CURL_HOST_NOMATCH;
  80. #endif
  81. /* We require at least 2 dots in pattern to avoid too wide wildcard
  82. match. */
  83. wildcard_enabled = 1;
  84. pattern_label_end = strchr(pattern, '.');
  85. if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
  86. pattern_wildcard > pattern_label_end ||
  87. Curl_raw_nequal(pattern, "xn--", 4)) {
  88. wildcard_enabled = 0;
  89. }
  90. if(!wildcard_enabled)
  91. return Curl_raw_equal(pattern, hostname) ?
  92. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  93. hostname_label_end = strchr(hostname, '.');
  94. if(hostname_label_end == NULL ||
  95. !Curl_raw_equal(pattern_label_end, hostname_label_end))
  96. return CURL_HOST_NOMATCH;
  97. /* The wildcard must match at least one character, so the left-most
  98. label of the hostname is at least as large as the left-most label
  99. of the pattern. */
  100. if(hostname_label_end - hostname < pattern_label_end - pattern)
  101. return CURL_HOST_NOMATCH;
  102. prefixlen = pattern_wildcard - pattern;
  103. suffixlen = pattern_label_end - (pattern_wildcard+1);
  104. return Curl_raw_nequal(pattern, hostname, prefixlen) &&
  105. Curl_raw_nequal(pattern_wildcard+1, hostname_label_end - suffixlen,
  106. suffixlen) ?
  107. CURL_HOST_MATCH : CURL_HOST_NOMATCH;
  108. }
  109. int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
  110. {
  111. char *matchp;
  112. char *hostp;
  113. int res = 0;
  114. if(!match_pattern || !*match_pattern ||
  115. !hostname || !*hostname) /* sanity check */
  116. ;
  117. else {
  118. matchp = strdup(match_pattern);
  119. if(matchp) {
  120. hostp = strdup(hostname);
  121. if(hostp) {
  122. if(hostmatch(hostp, matchp) == CURL_HOST_MATCH)
  123. res= 1;
  124. free(hostp);
  125. }
  126. free(matchp);
  127. }
  128. }
  129. return res;
  130. }
  131. #endif /* SSLEAY or AXTLS or QSOSSL or GSKIT */