Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 16 | */ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include "ip_utils.h" |
| 20 | #include "certstore.h" |
| 21 | #include "opendht/crypto.h" |
| 22 | #include "ice_transport.h" |
| 23 | #include "tls_session.h" |
| 24 | |
| 25 | #include <functional> |
| 26 | #include <future> |
| 27 | #include <limits> |
| 28 | #include <map> |
| 29 | #include <memory> |
| 30 | #include <string> |
| 31 | #include <utility> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace dht { |
| 35 | namespace crypto { |
| 36 | struct PrivateKey; |
| 37 | struct Certificate; |
| 38 | } // namespace crypto |
| 39 | } // namespace dht |
| 40 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 41 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 42 | namespace tls { |
| 43 | class DhParams; |
| 44 | } |
| 45 | |
| 46 | using OnStateChangeCb = std::function<bool(tls::TlsSessionState state)>; |
| 47 | using OnReadyCb = std::function<void(bool ok)>; |
| 48 | using onShutdownCb = std::function<void(void)>; |
| 49 | |
Adrien Béraud | 75754b2 | 2023-10-17 09:16:06 -0400 | [diff] [blame] | 50 | static constexpr int ICE_COMP_ID_SIP_TRANSPORT {1}; |
| 51 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 52 | //============================================================================== |
| 53 | |
| 54 | class IceSocketEndpoint : public GenericSocket<uint8_t> |
| 55 | { |
| 56 | public: |
| 57 | using SocketType = GenericSocket<uint8_t>; |
| 58 | explicit IceSocketEndpoint(std::shared_ptr<IceTransport> ice, bool isSender); |
| 59 | ~IceSocketEndpoint(); |
| 60 | |
| 61 | void shutdown() override; |
Adrien Béraud | c3777bb | 2023-11-06 09:27:04 -0500 | [diff] [blame] | 62 | bool isReliable() const override { return ice_ ? ice_->isTCPEnabled() : false; } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 63 | bool isInitiator() const override { return ice_ ? ice_->isInitiator() : true; } |
| 64 | int maxPayload() const override |
| 65 | { |
| 66 | return 65536 /* The max for a RTP packet used to wrap data here */; |
| 67 | } |
| 68 | int waitForData(std::chrono::milliseconds timeout, std::error_code& ec) const override; |
| 69 | std::size_t read(ValueType* buf, std::size_t len, std::error_code& ec) override; |
| 70 | std::size_t write(const ValueType* buf, std::size_t len, std::error_code& ec) override; |
| 71 | |
| 72 | std::shared_ptr<IceTransport> underlyingICE() const { return ice_; } |
| 73 | |
| 74 | void setOnRecv(RecvCb&& cb) override |
| 75 | { |
| 76 | if (ice_) |
| 77 | ice_->setOnRecv(compId_, cb); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | std::shared_ptr<IceTransport> ice_ {nullptr}; |
| 82 | std::atomic_bool iceStopped {false}; |
| 83 | std::atomic_bool iceIsSender {false}; |
| 84 | uint8_t compId_ {1}; |
| 85 | }; |
| 86 | |
| 87 | //============================================================================== |
| 88 | |
| 89 | /// Implement a TLS session IO over a system socket |
| 90 | class TlsSocketEndpoint : public GenericSocket<uint8_t> |
| 91 | { |
| 92 | public: |
| 93 | using SocketType = GenericSocket<uint8_t>; |
| 94 | using Identity = std::pair<std::shared_ptr<dht::crypto::PrivateKey>, |
| 95 | std::shared_ptr<dht::crypto::Certificate>>; |
| 96 | |
| 97 | TlsSocketEndpoint(std::unique_ptr<IceSocketEndpoint>&& tr, |
| 98 | tls::CertificateStore& certStore, |
Adrien Béraud | 3f93ddf | 2023-07-21 14:46:22 -0400 | [diff] [blame] | 99 | const std::shared_ptr<asio::io_context>& ioContext, |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 100 | const Identity& local_identity, |
| 101 | const std::shared_future<tls::DhParams>& dh_params, |
| 102 | const dht::crypto::Certificate& peer_cert); |
| 103 | TlsSocketEndpoint(std::unique_ptr<IceSocketEndpoint>&& tr, |
| 104 | tls::CertificateStore& certStore, |
Adrien Béraud | 3f93ddf | 2023-07-21 14:46:22 -0400 | [diff] [blame] | 105 | const std::shared_ptr<asio::io_context>& ioContext, |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 106 | const Identity& local_identity, |
| 107 | const std::shared_future<tls::DhParams>& dh_params, |
| 108 | std::function<bool(const dht::crypto::Certificate&)>&& cert_check); |
| 109 | ~TlsSocketEndpoint(); |
| 110 | |
| 111 | bool isReliable() const override { return true; } |
| 112 | bool isInitiator() const override; |
| 113 | int maxPayload() const override; |
| 114 | void shutdown() override; |
| 115 | std::size_t read(ValueType* buf, std::size_t len, std::error_code& ec) override; |
| 116 | std::size_t write(const ValueType* buf, std::size_t len, std::error_code& ec) override; |
| 117 | |
| 118 | std::shared_ptr<dht::crypto::Certificate> peerCertificate() const; |
| 119 | |
| 120 | void setOnRecv(RecvCb&&) override |
| 121 | { |
| 122 | throw std::logic_error("TlsSocketEndpoint::setOnRecv not implemented"); |
| 123 | } |
| 124 | int waitForData(std::chrono::milliseconds timeout, std::error_code&) const override; |
| 125 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 126 | void setOnStateChange(OnStateChangeCb&& cb); |
| 127 | void setOnReady(OnReadyCb&& cb); |
| 128 | |
| 129 | IpAddr getLocalAddress() const; |
| 130 | IpAddr getRemoteAddress() const; |
| 131 | |
| 132 | void monitor() const; |
| 133 | |
| 134 | private: |
| 135 | class Impl; |
| 136 | std::unique_ptr<Impl> pimpl_; |
| 137 | }; |
| 138 | |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 139 | } // namespace dhtnet |