hostcheck.c 4.2 KB

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