Make botan’s policy configurable from a file

fix #3244
This commit is contained in:
louiz’
2017-04-30 15:14:54 +02:00
parent 1472adfa77
commit 7b3e0e0cf3
15 changed files with 207 additions and 24 deletions
+8 -6
View File
@@ -14,6 +14,8 @@
#ifdef BOTAN_FOUND
# include <botan/hex.h>
# include <botan/tls_exceptn.h>
# include <config/config.hpp>
# include <utils/dirname.hpp>
namespace
{
@@ -22,11 +24,6 @@ namespace
static Botan::AutoSeeded_RNG rng{};
return rng;
}
BiboumiTLSPolicy& get_policy()
{
static BiboumiTLSPolicy policy{};
return policy;
}
Botan::TLS::Session_Manager_In_Memory& get_session_manager()
{
static Botan::TLS::Session_Manager_In_Memory session_manager{get_rng()};
@@ -233,6 +230,11 @@ void TCPSocketHandler::consume_in_buffer(const std::size_t size)
void TCPSocketHandler::start_tls(const std::string& address, const std::string& port)
{
Botan::TLS::Server_Information server_info(address, "irc", std::stoul(port));
auto policy_directory = Config::get("policy_directory", utils::dirname(Config::get_filename()));
if (!policy_directory.empty() && policy_directory[policy_directory.size()-1] != '/')
policy_directory += '/';
this->policy.load(policy_directory + "policy.txt");
this->policy.load(policy_directory + address + ".policy.txt");
this->tls = std::make_unique<Botan::TLS::Client>(
# if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,11,32)
*this,
@@ -242,7 +244,7 @@ void TCPSocketHandler::start_tls(const std::string& address, const std::string&
[this](Botan::TLS::Alert alert, const Botan::byte*, size_t) { this->tls_alert(alert); },
[this](const Botan::TLS::Session& session) { return this->tls_session_established(session); },
# endif
get_session_manager(), this->credential_manager, get_policy(),
get_session_manager(), this->credential_manager, this->policy,
get_rng(), server_info, Botan::TLS::Protocol_Version::latest_tls_version());
}
+2 -15
View File
@@ -23,21 +23,7 @@
# include <botan/types.h>
# include <botan/botan.h>
# include <botan/tls_session_manager.h>
class BiboumiTLSPolicy: public Botan::TLS::Policy
{
public:
# if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,11,33)
bool use_ecc_point_compression() const override
{
return true;
}
bool require_cert_revocation_info() const override
{
return false;
}
# endif
};
# include <network/tls_policy.hpp>
# if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,11,32)
# define BOTAN_TLS_CALLBACKS_OVERRIDE override final
@@ -230,6 +216,7 @@ protected:
protected:
BasicCredentialsManager credential_manager;
private:
BiboumiTLSPolicy policy;
/**
* We use a unique_ptr because we may not want to create the object at
* all. The Botan::TLS::Client object generates a handshake message and
+48
View File
@@ -0,0 +1,48 @@
#include "biboumi.h"
#ifdef BOTAN_FOUND
#include <fstream>
#include <utils/tolower.hpp>
#include <network/tls_policy.hpp>
#include <logger/logger.hpp>
bool BiboumiTLSPolicy::load(const std::string& filename)
{
std::ifstream is(filename.data());
if (is)
{
try {
this->load(is);
log_info("Successfully loaded policy file: ", filename);
return true;
} catch (const Botan::Exception& e) {
log_error("Failed to parse policy_file ", filename, ": ", e.what());
return false;
}
}
log_info("Could not open policy file: ", filename);
return false;
}
void BiboumiTLSPolicy::load(std::istream& is)
{
const auto dict = Botan::read_cfg(is);
for (const auto& pair: dict)
{
// Workaround for options that are not overridden in Botan::TLS::Text_Policy
if (pair.first == "require_cert_revocation_info")
this->req_cert_revocation_info = !(pair.second == "0" || utils::tolower(pair.second) == "false");
else
this->set(pair.first, pair.second);
}
}
bool BiboumiTLSPolicy::require_cert_revocation_info() const
{
return this->req_cert_revocation_info;
}
#endif
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include "biboumi.h"
#ifdef BOTAN_FOUND
#include <botan/tls_policy.h>
class BiboumiTLSPolicy: public Botan::TLS::Text_Policy
{
public:
BiboumiTLSPolicy():
Botan::TLS::Text_Policy({})
{}
bool load(const std::string& filename);
void load(std::istream& iss);
BiboumiTLSPolicy(const BiboumiTLSPolicy &) = delete;
BiboumiTLSPolicy(BiboumiTLSPolicy &&) = delete;
BiboumiTLSPolicy &operator=(const BiboumiTLSPolicy &) = delete;
BiboumiTLSPolicy &operator=(BiboumiTLSPolicy &&) = delete;
bool require_cert_revocation_info() const override;
protected:
bool req_cert_revocation_info{true};
};
#endif