Skip to content

Commit 6f0e310

Browse files
authored
Feature/tls (#89)
* tls transport * build-related tiny fix * removed unused file
1 parent 7314b5d commit 6f0e310

21 files changed

+1204
-19
lines changed

example/01-echo/libp2p_echo_client.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <memory>
99

1010
#include <libp2p/common/literals.hpp>
11+
#include <libp2p/common/logger.hpp>
1112
#include <libp2p/host/basic_host.hpp>
1213
#include <libp2p/injector/host_injector.hpp>
1314
#include <libp2p/protocol/echo.hpp>
@@ -19,6 +20,12 @@ int main(int argc, char *argv[]) {
1920
using libp2p::crypto::PublicKey;
2021
using libp2p::common::operator""_unhex;
2122

23+
if (std::getenv("TRACE_DEBUG") != nullptr) {
24+
spdlog::set_level(spdlog::level::trace);
25+
} else {
26+
spdlog::set_level(spdlog::level::err);
27+
}
28+
2229
if (argc != 2) {
2330
std::cerr << "please, provide an address of the server\n";
2431
std::exit(EXIT_FAILURE);

example/01-echo/libp2p_echo_server.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#include <chrono>
76
#include <iostream>
87
#include <memory>
98
#include <string>

example/03-gossip/utility.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ namespace libp2p::protocol::example::utility {
6363
debug_logger->set_level(spdlog_level);
6464
logger->set_level(spdlog_level);
6565

66+
if (spdlog_level == spdlog::level::trace) {
67+
// for all loggers
68+
spdlog::set_level(spdlog_level);
69+
}
70+
6671
return logger;
6772
}
6873

include/libp2p/injector/network_injector.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <libp2p/security/secio.hpp>
3535
#include <libp2p/security/secio/exchange_message_marshaller_impl.hpp>
3636
#include <libp2p/security/secio/propose_message_marshaller_impl.hpp>
37+
#include <libp2p/security/tls.hpp>
3738
#include <libp2p/transport/impl/upgrader_impl.hpp>
3839
#include <libp2p/transport/tcp.hpp>
3940

@@ -281,7 +282,7 @@ namespace libp2p::injector {
281282
di::bind<protocol_muxer::ProtocolMuxer>().template to<protocol_muxer::Multiselect>(),
282283

283284
// default adaptors
284-
di::bind<security::SecurityAdaptor *[]>().template to<security::Plaintext, security::Secio>(), // NOLINT
285+
di::bind<security::SecurityAdaptor *[]>().template to<security::Plaintext, security::Secio, security::TlsAdaptor>(), // NOLINT
285286
di::bind<muxer::MuxerAdaptor *[]>().template to<muxer::Yamux, muxer::Mplex>(), // NOLINT
286287
di::bind<transport::TransportAdaptor *[]>().template to<transport::TcpTransport>(), // NOLINT
287288

include/libp2p/security/tls.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef LIBP2P_SECURITY_TLS_HPP
7+
#define LIBP2P_SECURITY_TLS_HPP
8+
9+
#include <libp2p/security/tls/tls_adaptor.hpp>
10+
11+
#endif // LIBP2P_SECURITY_TLS_HPP
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef LIBP2P_SECURITY_TLS_ADAPTOR_HPP
7+
#define LIBP2P_SECURITY_TLS_ADAPTOR_HPP
8+
9+
#include <boost/asio/io_context.hpp>
10+
#include <boost/asio/ssl.hpp>
11+
12+
#include <libp2p/crypto/key_marshaller.hpp>
13+
#include <libp2p/peer/identity_manager.hpp>
14+
#include <libp2p/security/security_adaptor.hpp>
15+
#include <libp2p/security/tls/tls_errors.hpp>
16+
17+
namespace libp2p::security {
18+
19+
/// TLS 1.3 security adaptor
20+
class TlsAdaptor : public SecurityAdaptor,
21+
public std::enable_shared_from_this<TlsAdaptor> {
22+
public:
23+
/// Dtor.
24+
~TlsAdaptor() override = default;
25+
26+
/// Ctor.
27+
TlsAdaptor(
28+
std::shared_ptr<peer::IdentityManager> idmgr,
29+
std::shared_ptr<boost::asio::io_context> io_context,
30+
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_marshaller);
31+
32+
/// Returns "/tls/1.0.0"
33+
peer::Protocol getProtocolId() const override;
34+
35+
/// Performs async handshake for inbound connection
36+
void secureInbound(std::shared_ptr<connection::RawConnection> inbound,
37+
SecConnCallbackFunc cb) override;
38+
39+
/// Performs async handshake for outbound connection
40+
void secureOutbound(std::shared_ptr<connection::RawConnection> outbound,
41+
const peer::PeerId &p, SecConnCallbackFunc cb) override;
42+
43+
private:
44+
/// Creates shared SSL context, generates certificate and private key
45+
outcome::result<void> setupContext();
46+
47+
/// Creates TLSConnection and starts handshake
48+
void asyncHandshake(std::shared_ptr<connection::RawConnection> conn,
49+
boost::optional<peer::PeerId> remote_peer,
50+
SecConnCallbackFunc cb);
51+
52+
/// Identity manager which contains this host's keys and peer id
53+
std::shared_ptr<peer::IdentityManager> idmgr_;
54+
55+
/// IO context, used to defer callback on error
56+
std::shared_ptr<boost::asio::io_context> io_context_;
57+
58+
/// Key marshaller, needed for custom cert extension
59+
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_marshaller_;
60+
61+
/// Shared ssl context
62+
std::shared_ptr<boost::asio::ssl::context> ssl_context_;
63+
};
64+
} // namespace libp2p::security
65+
66+
#endif // LIBP2P_SECURITY_TLS_ADAPTOR_HPP
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef LIBP2P_SECURITY_TLS_ERRORS_HPP
7+
#define LIBP2P_SECURITY_TLS_ERRORS_HPP
8+
9+
#include <libp2p/outcome/outcome.hpp>
10+
11+
namespace libp2p::security {
12+
13+
enum class TlsError : int {
14+
TLS_CTX_INIT_FAILED = 1,
15+
TLS_INCOMPATIBLE_TRANSPORT,
16+
TLS_NO_CERTIFICATE,
17+
TLS_INCOMPATIBLE_CERTIFICATE_EXTENSION,
18+
TLS_PEER_VERIFY_FAILED,
19+
TLS_UNEXPECTED_PEER_ID,
20+
TLS_REMOTE_PEER_NOT_AVAILABLE,
21+
TLS_REMOTE_PUBKEY_NOT_AVAILABLE,
22+
};
23+
} // namespace libp2p::security
24+
25+
OUTCOME_HPP_DECLARE_ERROR(libp2p::security, TlsError);
26+
27+
#endif // LIBP2P_SECURITY_TLS_ERRORS_HPP

include/libp2p/transport/tcp/tcp_connection.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <libp2p/connection/raw_connection.hpp>
1414
#include <libp2p/multi/multiaddress.hpp>
1515

16+
namespace libp2p::security { class TlsAdaptor; }
17+
1618
namespace libp2p::transport {
1719

1820
/**
@@ -97,6 +99,8 @@ namespace libp2p::transport {
9799

98100
boost::system::error_code handle_errcode(
99101
const boost::system::error_code &e) noexcept;
102+
103+
friend class security::TlsAdaptor;
100104
};
101105
} // namespace libp2p::transport
102106

src/network/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target_link_libraries(p2p_default_network
1515
p2p_mplex
1616
p2p_plaintext
1717
p2p_secio
18+
p2p_tls
1819
p2p_connection_manager
1920
p2p_transport_manager
2021
p2p_listener_manager

src/network/impl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ target_link_libraries(p2p_listener_manager
2020
Boost::boost
2121
p2p_multiaddress
2222
p2p_peer_id
23+
p2p_logger
2324
)
2425

2526

0 commit comments

Comments
 (0)