hostcheck.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(USE_OPENSSL) \
  26. || defined(USE_SCHANNEL)
  27. /* these backends use functions from this file */
  28. #ifdef HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #ifdef HAVE_NETINET_IN6_H
  32. #include <netinet/in6.h>
  33. #endif
  34. #include "curl_memrchr.h"
  35. #include "hostcheck.h"
  36. #include "strcase.h"
  37. #include "hostip.h"
  38. #include "curl_memory.h"
  39. /* The last #include file should be: */
  40. #include "memdebug.h"
  41. /* check the two input strings with given length, but do not
  42. assume they end in nul-bytes */
  43. static bool pmatch(const char *hostname, size_t hostlen,
  44. const char *pattern, size_t patternlen)
  45. {
  46. if(hostlen != patternlen)
  47. return FALSE;
  48. return strncasecompare(hostname, pattern, hostlen);
  49. }
  50. /*
  51. * Match a hostname against a wildcard pattern.
  52. * E.g.
  53. * "foo.host.com" matches "*.host.com".
  54. *
  55. * We use the matching rule described in RFC6125, section 6.4.3.
  56. * https://datatracker.ietf.org/doc/html/rfc6125#section-6.4.3
  57. *
  58. * In addition: ignore trailing dots in the host names and wildcards, so that
  59. * the names are used normalized. This is what the browsers do.
  60. *
  61. * Do not allow wildcard matching on IP numbers. There are apparently
  62. * certificates being used with an IP address in the CN field, thus making no
  63. * apparent distinction between a name and an IP. We need to detect the use of
  64. * an IP address and not wildcard match on such names.
  65. *
  66. * Only match on "*" being used for the leftmost label, not "a*", "a*b" nor
  67. * "*b".
  68. *
  69. * Return TRUE on a match. FALSE if not.
  70. *
  71. * @unittest: 1397
  72. */
  73. static bool hostmatch(const char *hostname,
  74. size_t hostlen,
  75. const char *pattern,
  76. size_t patternlen)
  77. {
  78. const char *pattern_label_end;
  79. DEBUGASSERT(pattern);
  80. DEBUGASSERT(patternlen);
  81. DEBUGASSERT(hostname);
  82. DEBUGASSERT(hostlen);
  83. /* normalize pattern and hostname by stripping off trailing dots */
  84. if(hostname[hostlen-1]=='.')
  85. hostlen--;
  86. if(pattern[patternlen-1]=='.')
  87. patternlen--;
  88. if(strncmp(pattern, "*.", 2))
  89. return pmatch(hostname, hostlen, pattern, patternlen);
  90. /* detect IP address as hostname and fail the match if so */
  91. else if(Curl_host_is_ipnum(hostname))
  92. return FALSE;
  93. /* We require at least 2 dots in the pattern to avoid too wide wildcard
  94. match. */
  95. pattern_label_end = memchr(pattern, '.', patternlen);
  96. if(!pattern_label_end ||
  97. (memrchr(pattern, '.', patternlen) == pattern_label_end))
  98. return pmatch(hostname, hostlen, pattern, patternlen);
  99. else {
  100. const char *hostname_label_end = memchr(hostname, '.', hostlen);
  101. if(hostname_label_end) {
  102. size_t skiphost = hostname_label_end - hostname;
  103. size_t skiplen = pattern_label_end - pattern;
  104. return pmatch(hostname_label_end, hostlen - skiphost,
  105. pattern_label_end, patternlen - skiplen);
  106. }
  107. }
  108. return FALSE;
  109. }
  110. /*
  111. * Curl_cert_hostcheck() returns TRUE if a match and FALSE if not.
  112. */
  113. bool Curl_cert_hostcheck(const char *match, size_t matchlen,
  114. const char *hostname, size_t hostlen)
  115. {
  116. if(match && *match && hostname && *hostname)
  117. return hostmatch(hostname, hostlen, match, matchlen);
  118. return FALSE;
  119. }
  120. #endif /* OPENSSL or SCHANNEL */