hostcheck.c 4.8 KB

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