Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Providers/OpenSSL/ServerContext.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. 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"
16
17#include "ServerContext.h"
18
19using namespace Stroika::Foundation;
21using namespace Stroika::Foundation::Cryptography;
22using namespace Stroika::Foundation::Cryptography::PKI;
23using namespace Stroika::Foundation::Cryptography::Providers;
24using namespace Stroika::Foundation::Cryptography::Providers::OpenSSL;
25using namespace Stroika::Foundation::Debug;
26
27// Comment this in to turn on aggressive noisy DbgTrace in this module
28// #define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
29
30#if qStroika_HasComponent_OpenSSL
31namespace {
32 using OpenSSL::ServerContext::Options;
33 struct Rep_ : OpenSSL::ServerContext::IRep {
34 OpenSSL::ServerContext::LibRepType fCtx_;
35
36 Rep_ (const Options& o)
37 : fCtx_{::SSL_CTX_new (o.fMethod)}
38 {
39 RequireNotNull (get<Cryptography::PKI::Certificate::Ptr> (o.fCertificate));
40 OpenSSL::Exception::ThrowLastErrorIfFailed (::SSL_CTX_use_certificate (
41 fCtx_.get (), OpenSSL::Certificate::Ptr{get<Cryptography::PKI::Certificate::Ptr> (o.fCertificate)}.Get_X509 ()));
42 RequireNotNull (get<PKI::PrivateKey::Ptr> (o.fCertificate));
43 OpenSSL::Exception::ThrowLastErrorIfFailed (
44 ::SSL_CTX_use_PrivateKey (fCtx_.get (), OpenSSL::PrivateKey::Ptr{get<PKI::PrivateKey::Ptr> (o.fCertificate)}.Get_EVP_PKEY ()));
45 }
46 virtual SSL_CTX* Get_SSL_CTX () const override
47 {
48 return fCtx_.get ();
49 }
50 };
51}
52
53auto OpenSSL::ServerContext::New (const Options& o) -> Ptr
54{
55 return make_shared<Rep_> (o);
56}
57#endif
#define RequireNotNull(p)
Definition Assertions.h:347