Skip to content

Commit b8d8ebf

Browse files
authored
Pile of fix for h2 (#305)
1 parent 5d52d7a commit b8d8ebf

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

trantor/net/inner/TcpConnectionImpl.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,8 @@ void TcpConnectionImpl::startEncryption(
12981298
return;
12991299
}
13001300
auto sslContextPtr = newSSLContext(*policy, isServer);
1301-
tlsProviderPtr_ = newTLSProvider(this, policy, sslContextPtr);
1301+
tlsProviderPtr_ =
1302+
newTLSProvider(this, std::move(policy), std::move(sslContextPtr));
13021303
tlsProviderPtr_->setWriteCallback(onSslWrite);
13031304
tlsProviderPtr_->setErrorCallback(onSslError);
13041305
tlsProviderPtr_->setHandshakeCallback(onHandshakeFinished);
@@ -1311,9 +1312,9 @@ void TcpConnectionImpl::startEncryption(
13111312

13121313
void TcpConnectionImpl::onSslError(TcpConnection *self, SSLError err)
13131314
{
1314-
self->forceClose();
13151315
if (self->sslErrorCallback_)
13161316
self->sslErrorCallback_(err);
1317+
self->forceClose();
13171318
}
13181319
void TcpConnectionImpl::onHandshakeFinished(TcpConnection *self)
13191320
{

trantor/net/inner/tlsprovider/BotanTLSProvider.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ static std::once_flag systemCertStoreInitFlag;
3232

3333
using namespace trantor;
3434

35+
static std::string join(const std::vector<std::string> &vec,
36+
const std::string &delim)
37+
{
38+
std::string ret;
39+
for (auto const &str : vec)
40+
{
41+
if (ret.empty() == false)
42+
ret += delim;
43+
ret += str;
44+
}
45+
return ret;
46+
}
47+
3548
class Credentials : public Botan::Credentials_Manager
3649
{
3750
public:
@@ -328,6 +341,28 @@ struct BotanTLSProvider : public TLSProvider,
328341
messageCallback_(conn_, &recvBuffer_);
329342
}
330343

344+
std::string tls_server_choose_app_protocol(
345+
const std::vector<std::string> &client_protos) override
346+
{
347+
assert(contextPtr_->isServer);
348+
if (policyPtr_->getAlpnProtocols().empty() || client_protos.empty())
349+
return "";
350+
351+
for (auto const &proto : client_protos)
352+
{
353+
if (std::find(policyPtr_->getAlpnProtocols().begin(),
354+
policyPtr_->getAlpnProtocols().end(),
355+
proto) != policyPtr_->getAlpnProtocols().end())
356+
return proto;
357+
}
358+
359+
throw Botan::TLS::TLS_Exception(
360+
Botan::TLS::Alert::NoApplicationProtocol,
361+
"No supported application protocol found. Client offered: " +
362+
join(client_protos, ", ") + " but we support: " +
363+
join(policyPtr_->getAlpnProtocols(), ", "));
364+
}
365+
331366
void tls_alert(Botan::TLS::Alert alert) override
332367
{
333368
if (alert.type() == Botan::TLS::Alert::CloseNotify)

trantor/utils/crypto/botan.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <botan/hash.h>
22
#include <trantor/utils/Utilities.h>
33

4+
#include <cassert>
5+
46
namespace trantor
57
{
68
namespace utils
@@ -36,6 +38,7 @@ Hash256 sha3(const void* data, size_t len)
3638
{
3739
Hash256 hash;
3840
auto sha3 = Botan::HashFunction::create("SHA-3(256)");
41+
assert(sha3 != nullptr);
3942
sha3->update((const unsigned char*)data, len);
4043
sha3->final((unsigned char*)&hash);
4144
return hash;
@@ -45,6 +48,7 @@ Hash256 blake2b(const void* data, size_t len)
4548
{
4649
Hash256 hash;
4750
auto blake2b = Botan::HashFunction::create("BLAKE2b(256)");
51+
assert(blake2b != nullptr);
4852
blake2b->update((const unsigned char*)data, len);
4953
blake2b->final((unsigned char*)&hash);
5054
return hash;

0 commit comments

Comments
 (0)