libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
sslsocket.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_TLS
13#include <promeki/error.h>
14#include <promeki/list.h>
15#include <promeki/namespace.h>
16#include <promeki/sharedptr.h>
17#include <promeki/sslcontext.h>
18#include <promeki/string.h>
19#include <promeki/stringlist.h>
20#include <promeki/tcpsocket.h>
21
22PROMEKI_NAMESPACE_BEGIN
23
57class SslSocket : public TcpSocket {
58 PROMEKI_OBJECT(SslSocket, TcpSocket)
59 public:
61 using List = ::promeki::List<SslSocket *>;
62
64 explicit SslSocket(ObjectBase *parent = nullptr);
65
67 ~SslSocket() override;
68
77 void setSslContext(SslContext ctx);
78
80 SslContext sslContext() const { return _ctx; }
81
95 Error startEncryption(const String &hostname = String());
96
105 Error startServerEncryption();
106
122 Error continueHandshake();
123
125 bool isEncrypted() const { return _state == Encrypted; }
126
133 String peerCertificateSubject() const;
134
135 // ----------------------------------------------------
136 // IODevice overrides — proxy through mbedtls_ssl_*
137 // ----------------------------------------------------
138
139 int64_t read(void *data, int64_t maxSize) override;
140 int64_t write(const void *data, int64_t maxSize) override;
141 int64_t bytesAvailable() const override;
142 Error close() override;
143
145 PROMEKI_SIGNAL(encrypted);
146
159 PROMEKI_SIGNAL(sslErrors, StringList);
160
161 private:
162 struct Impl;
163 // Declaration order is load-bearing: _d holds the
164 // mbedtls_ssl_context, which mbedtls_ssl_setup() binds to
165 // _ctx's mbedtls_ssl_config. ~Impl runs mbedtls_ssl_free(),
166 // which still dereferences that config, so _d MUST be
167 // destroyed before _ctx. Members destruct in reverse
168 // declaration order, hence _ctx is declared first.
169 SslContext _ctx;
170 SharedPtr<Impl> _d;
171
172 enum SslState {
173 NotEncrypted,
174 Handshaking,
175 Encrypted,
176 Failed
177 };
178 SslState _state = NotEncrypted;
179
180 // Captured at handshake-start time and consulted by
181 // log lines later in the lifecycle (handshake success
182 // / failure / close), so a single warn line in a
183 // field log identifies which peer was being talked
184 // to without the operator having to correlate with
185 // other lines.
186 String _hostname;
187 bool _isClient = true;
188
189 Error performHandshakeStep();
190};
191
192PROMEKI_NAMESPACE_END
193
194#endif // PROMEKI_ENABLE_TLS