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