libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
sslcontext.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <promeki/namespace.h>
14#include <promeki/string.h>
15#include <promeki/buffer.h>
16#include <promeki/filepath.h>
17#include <promeki/error.h>
18#include <promeki/sharedptr.h>
19#include <promeki/list.h>
20#include <promeki/datatype.h>
21#include <promeki/result.h>
22
23PROMEKI_NAMESPACE_BEGIN
24
25class DataStream;
26
192class SslContext {
193 public:
194 PROMEKI_DATATYPE(SslContext, DataTypeSslContext, 1)
195
196
197 using List = ::promeki::List<SslContext>;
198
200 enum SslProtocol {
201 TlsV1_2,
202 TlsV1_3,
203 SecureProtocols
204 };
205
266 enum SecurityProfile {
267 Strict,
268 Compatible,
269 };
270
286 static bool hasTlsSupport();
287
300 SslContext();
301
303 ~SslContext();
304
305 // The copy / move special members are defined
306 // out-of-line because @ref Impl is incomplete here —
307 // an inline @c =default would instantiate the
308 // @c SharedPtr internals against the forward-declared
309 // type, miss the @c IsSharedObject trait, and route
310 // through @c SharedPtrProxy<Impl>::~SharedPtrProxy()
311 // which would invoke @c delete on an incomplete type.
312
314 SslContext(const SslContext &other);
315
317 SslContext &operator=(const SslContext &other);
318
320 SslContext(SslContext &&other) noexcept;
321
323 SslContext &operator=(SslContext &&other) noexcept;
324
335 bool isValid() const;
336
338 bool operator==(const SslContext &other) const { return _d == other._d; }
339
341 bool operator!=(const SslContext &other) const { return !(*this == other); }
342
358 String toString() const;
359
361 void setProtocol(SslProtocol protocol);
362
364 SslProtocol protocol() const;
365
381 void setSecurityProfile(SecurityProfile profile);
382
384 SecurityProfile securityProfile() const;
385
386 // ----------------------------------------------------
387 // Server-side credentials
388 // ----------------------------------------------------
389
398 Error setCertificate(const FilePath &file);
399
401 Error setCertificate(const Buffer &certData);
402
409 Error setPrivateKey(const FilePath &file, const String &passphrase = String());
410
423 Error setPrivateKey(const Buffer &keyData, const String &passphrase = String());
424
425 // ----------------------------------------------------
426 // Trust store (used for peer verification on both sides)
427 // ----------------------------------------------------
428
434 Error setCaCertificates(const FilePath &caFile);
435
437 Error setCaCertificates(const Buffer &caData);
438
447 Error setSystemCaCertificates();
448
466 void setVerifyPeer(bool enable);
467
469 bool verifyPeer() const;
470
490 void setRequireClientCert(bool require);
491
493 bool requireClientCert() const;
494
496 void setVerifyDepth(int depth);
497
499 int verifyDepth() const;
500
502 bool hasCertificate() const;
503
505 bool hasCaCertificates() const;
506
518 void *nativeConfig() const;
519
532 Error writeToStream(DataStream &s) const;
533
542 template <uint32_t V> static Result<SslContext> readFromStream(DataStream &s);
543
544 private:
545 struct Impl;
546
547 // mutable: @c nativeConfig is a @c const accessor but
548 // lazy-initializes the underlying @c mbedtls_ssl_config
549 // on first call, so the @c SharedPtr handle has to be
550 // reachable through a non-const @c modify() path.
551 mutable SharedPtr<Impl, false> _d;
552};
553
554// Primary template for @ref SslContext::readFromStream: always
555// reports @ref Error::NotSupported, regardless of wire version, for
556// the reasons documented on the declaration. Inline so the
557// @ref PROMEKI_DATATYPE @c dispatchRead body can resolve it without
558// dragging the cpp into header consumers.
559template <uint32_t V> inline Result<SslContext> SslContext::readFromStream(DataStream &) {
560 return Result<SslContext>(SslContext(), Error::NotSupported);
561}
562
563PROMEKI_NAMESPACE_END
564
565#endif // PROMEKI_ENABLE_CORE