Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Providers/OpenSSL/ClientContext.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 "ClientContext.h"
18
19using namespace Stroika::Foundation;
21using namespace Stroika::Foundation::Cryptography;
22using namespace Stroika::Foundation::Cryptography::Providers;
23using namespace Stroika::Foundation::Cryptography::Providers::OpenSSL;
24using namespace Stroika::Foundation::Debug;
25
26// Comment this in to turn on aggressive noisy DbgTrace in this module
27// #define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
28
29#if qStroika_HasComponent_OpenSSL
30namespace {
31 using OpenSSL::ClientContext::Options;
32 struct Rep_ : OpenSSL::ClientContext::IRep {
33 OpenSSL::ClientContext::LibRepType fCtx_;
34
35 Rep_ (const Options& o)
36 : fCtx_{::SSL_CTX_new (::TLS_client_method ())}
37 {
38 if (o.fClientCertificate) {
39 Cryptography::PKI::Certificate::Ptr clientCert = get<PKI::Certificate::Ptr> (*o.fClientCertificate);
40 OpenSSL::Exception::ThrowLastErrorIfFailed (::SSL_CTX_use_certificate (fCtx_.get (), OpenSSL::Certificate::Ptr{clientCert}.Get_X509 ()));
41 PKI::PrivateKey::Ptr pkey = get<PKI::PrivateKey::Ptr> (*o.fClientCertificate);
42 OpenSSL::Exception::ThrowLastErrorIfFailed (::SSL_CTX_use_PrivateKey (fCtx_.get (), OpenSSL::PrivateKey::Ptr{pkey}.Get_EVP_PKEY ()));
43 }
44 }
45 SSL_CTX* Get_SSL_CTX () const override
46 {
47 return fCtx_.get ();
48 }
49 };
50}
51#endif
52
53#if qStroika_HasComponent_OpenSSL
54auto OpenSSL::ClientContext::New (const Options& o) -> Ptr
55{
56 return make_shared<Rep_> (o);
57}
58#endif