hostcheck.c 4.1 KB

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