Stroika Library 3.0d23x
 
Loading...
Searching...
No Matches
Providers/OpenSSL/ServerContext.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#if qStroika_HasComponent_OpenSSL
7#include <openssl/evp.h>
8#include <openssl/ssl.h>
9#endif
10
11#include "Stroika/Foundation/Cryptography/Providers/OpenSSL/Certificate.h"
13#include "Stroika/Foundation/Cryptography/Providers/OpenSSL/PrivateKey.h"
15#include "Stroika/Foundation/Execution/Exceptions.h"
17
18#include "ServerContext.h"
19
20using namespace Stroika::Foundation;
22using namespace Stroika::Foundation::Cryptography;
23using namespace Stroika::Foundation::Cryptography::PKI;
24using namespace Stroika::Foundation::Cryptography::Providers;
25using namespace Stroika::Foundation::Cryptography::Providers::OpenSSL;
26using namespace Stroika::Foundation::Debug;
27
28using Memory::MakeSharedPtr;
29
30// Comment this in to turn on aggressive noisy DbgTrace in this module
31// #define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
32
33#if qStroika_HasComponent_OpenSSL
34namespace {
35 using OpenSSL::ServerContext::Options;
36 struct Rep_ final : OpenSSL::ServerContext::IRep, Memory::UseBlockAllocationIfAppropriate<Rep_> {
37 OpenSSL::ServerContext::LibRepType fCtx_;
38
39 Rep_ (const Options& o)
40 : fCtx_{::SSL_CTX_new (o.fMethod)}
41 {
42 RequireNotNull (get<Cryptography::PKI::Certificate::Ptr> (o.fCertificate));
43 OpenSSL::Exception::ThrowLastErrorIfFailed (::SSL_CTX_use_certificate (
44 fCtx_.get (), OpenSSL::Certificate::Ptr{get<Cryptography::PKI::Certificate::Ptr> (o.fCertificate)}.Get_X509 ()));
45 RequireNotNull (get<PKI::PrivateKey::Ptr> (o.fCertificate));
46 OpenSSL::Exception::ThrowLastErrorIfFailed (
47 ::SSL_CTX_use_PrivateKey (fCtx_.get (), OpenSSL::PrivateKey::Ptr{get<PKI::PrivateKey::Ptr> (o.fCertificate)}.Get_EVP_PKEY ()));
48 }
49 virtual SSL_CTX* Get_SSL_CTX () const override
50 {
51 return fCtx_.get ();
52 }
53 };
54}
55
56auto OpenSSL::ServerContext::New (const Options& o) -> Ptr
57{
58 return MakeSharedPtr<Rep_> (o);
59}
60#endif
#define RequireNotNull(p)
Definition Assertions.h:347
conditional_t< qStroika_Foundation_Memory_PreferBlockAllocation and andTrueCheck, BlockAllocationUseHelper< T >, Common::Empty > UseBlockAllocationIfAppropriate
Use this to enable block allocation for a particular class. Beware of subclassing.