Skip to content

Commit 8b607a2

Browse files
committed
改善 SNI 匹配机制, 使用证书中的域名进行匹配.
1 parent fb3b7d7 commit 8b607a2

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

proxy/include/proxy/proxy_server.hpp

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4631,6 +4631,38 @@ R"x*x*x(<html>
46314631

46324632
virtual ~proxy_server() = default;
46334633

4634+
bool rfc2818_verification_match_pattern(
4635+
const char* pattern, std::size_t pattern_length, const char* host)
4636+
{
4637+
const char* p = pattern;
4638+
const char* p_end = p + pattern_length;
4639+
const char* h = host;
4640+
4641+
while (p != p_end && *h)
4642+
{
4643+
if (*p == '*')
4644+
{
4645+
++p;
4646+
while (*h && *h != '.')
4647+
{
4648+
if (rfc2818_verification_match_pattern(p, p_end - p, h++))
4649+
return true;
4650+
}
4651+
}
4652+
else if (std::tolower(*p) == std::tolower(*h))
4653+
{
4654+
++p;
4655+
++h;
4656+
}
4657+
else
4658+
{
4659+
return false;
4660+
}
4661+
}
4662+
4663+
return p == p_end && !*h;
4664+
}
4665+
46344666
pem_file determine_pem_type(const std::string& filepath) noexcept
46354667
{
46364668
pem_file result{ filepath, pem_type::none };
@@ -4954,10 +4986,24 @@ R"x*x*x(<html>
49544986

49554987
for (auto& ctx : m_certificates)
49564988
{
4957-
if (ctx.domain_ == servername && ctx.ssl_context_.has_value())
4989+
if (ctx.ssl_context_.has_value())
49584990
{
4959-
SSL_set_SSL_CTX(ssl, ctx.ssl_context_->native_handle());
4960-
return SSL_TLSEXT_ERR_OK;
4991+
if (rfc2818_verification_match_pattern(
4992+
ctx.domain_.c_str(), ctx.domain_.length(), servername))
4993+
{
4994+
SSL_set_SSL_CTX(ssl, ctx.ssl_context_->native_handle());
4995+
return SSL_TLSEXT_ERR_OK;
4996+
}
4997+
4998+
for (auto& alt_name : ctx.alt_names_)
4999+
{
5000+
if (rfc2818_verification_match_pattern(
5001+
alt_name.c_str(), alt_name.length(), servername))
5002+
{
5003+
SSL_set_SSL_CTX(ssl, ctx.ssl_context_->native_handle());
5004+
return SSL_TLSEXT_ERR_OK;
5005+
}
5006+
}
49615007
}
49625008
if (ctx.domain_.empty())
49635009
default_ctx = &ctx;

0 commit comments

Comments
 (0)