Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.5KB

  1. //
  2. // ssl/impl/rfc2818_verification.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
  11. #define ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cctype>
  17. #include <cstring>
  18. #include "asio/ip/address.hpp"
  19. #include "asio/ssl/rfc2818_verification.hpp"
  20. #include "asio/ssl/detail/openssl_types.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace ssl {
  24. bool rfc2818_verification::operator()(
  25. bool preverified, verify_context& ctx) const
  26. {
  27. using namespace std; // For memcmp.
  28. // Don't bother looking at certificates that have failed pre-verification.
  29. if (!preverified)
  30. return false;
  31. // We're only interested in checking the certificate at the end of the chain.
  32. int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
  33. if (depth > 0)
  34. return true;
  35. // Try converting the host name to an address. If it is an address then we
  36. // need to look for an IP address in the certificate rather than a host name.
  37. asio::error_code ec;
  38. ip::address address = ip::make_address(host_, ec);
  39. bool is_address = !ec;
  40. X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
  41. // Go through the alternate names in the certificate looking for matching DNS
  42. // or IP address entries.
  43. GENERAL_NAMES* gens = static_cast<GENERAL_NAMES*>(
  44. X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0));
  45. for (int i = 0; i < sk_GENERAL_NAME_num(gens); ++i)
  46. {
  47. GENERAL_NAME* gen = sk_GENERAL_NAME_value(gens, i);
  48. if (gen->type == GEN_DNS && !is_address)
  49. {
  50. ASN1_IA5STRING* domain = gen->d.dNSName;
  51. if (domain->type == V_ASN1_IA5STRING && domain->data && domain->length)
  52. {
  53. const char* pattern = reinterpret_cast<const char*>(domain->data);
  54. std::size_t pattern_length = domain->length;
  55. if (match_pattern(pattern, pattern_length, host_.c_str()))
  56. {
  57. GENERAL_NAMES_free(gens);
  58. return true;
  59. }
  60. }
  61. }
  62. else if (gen->type == GEN_IPADD && is_address)
  63. {
  64. ASN1_OCTET_STRING* ip_address = gen->d.iPAddress;
  65. if (ip_address->type == V_ASN1_OCTET_STRING && ip_address->data)
  66. {
  67. if (address.is_v4() && ip_address->length == 4)
  68. {
  69. ip::address_v4::bytes_type bytes = address.to_v4().to_bytes();
  70. if (memcmp(bytes.data(), ip_address->data, 4) == 0)
  71. {
  72. GENERAL_NAMES_free(gens);
  73. return true;
  74. }
  75. }
  76. else if (address.is_v6() && ip_address->length == 16)
  77. {
  78. ip::address_v6::bytes_type bytes = address.to_v6().to_bytes();
  79. if (memcmp(bytes.data(), ip_address->data, 16) == 0)
  80. {
  81. GENERAL_NAMES_free(gens);
  82. return true;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. GENERAL_NAMES_free(gens);
  89. // No match in the alternate names, so try the common names. We should only
  90. // use the "most specific" common name, which is the last one in the list.
  91. X509_NAME* name = X509_get_subject_name(cert);
  92. int i = -1;
  93. ASN1_STRING* common_name = 0;
  94. while ((i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0)
  95. {
  96. X509_NAME_ENTRY* name_entry = X509_NAME_get_entry(name, i);
  97. common_name = X509_NAME_ENTRY_get_data(name_entry);
  98. }
  99. if (common_name && common_name->data && common_name->length)
  100. {
  101. const char* pattern = reinterpret_cast<const char*>(common_name->data);
  102. std::size_t pattern_length = common_name->length;
  103. if (match_pattern(pattern, pattern_length, host_.c_str()))
  104. return true;
  105. }
  106. return false;
  107. }
  108. bool rfc2818_verification::match_pattern(const char* pattern,
  109. std::size_t pattern_length, const char* host)
  110. {
  111. using namespace std; // For tolower.
  112. const char* p = pattern;
  113. const char* p_end = p + pattern_length;
  114. const char* h = host;
  115. while (p != p_end && *h)
  116. {
  117. if (*p == '*')
  118. {
  119. ++p;
  120. while (*h && *h != '.')
  121. if (match_pattern(p, p_end - p, h++))
  122. return true;
  123. }
  124. else if (tolower(*p) == tolower(*h))
  125. {
  126. ++p;
  127. ++h;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
  134. return p == p_end && !*h;
  135. }
  136. } // namespace ssl
  137. } // namespace asio
  138. #include "asio/detail/pop_options.hpp"
  139. #endif // ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP