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