Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Cryptography/Providers/OpenSSL/Exception.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/err.h>
8#include <openssl/evp.h>
9#endif
10
11#include "Stroika/Foundation/Containers/Common.h"
13#include "Stroika/Foundation/Execution/Common.h"
15
16#include "Exception.h"
17
18using namespace Stroika::Foundation;
20using namespace Stroika::Foundation::Cryptography;
21using namespace Stroika::Foundation::Cryptography::Providers::OpenSSL;
22using namespace Stroika::Foundation::Memory;
23
25
26#if qStroika_HasComponent_OpenSSL
27namespace {
28 Synchronized<bool> sNamesSupported_{true}; // @todo note locking not done right yet here - not safely...
29 Synchronized<bool> sNamesLoaded_{false};
30
31 struct ErrStringIniter_ {
32 ~ErrStringIniter_ ()
33 {
34 if (sNamesSupported_) {
35 auto l = sNamesLoaded_.rwget ();
36 if (static_cast<bool> (l)) {
37 ERR_free_strings ();
38 l = false;
39 }
40 }
41 }
42 } _InitOpenSSLErrStrings_;
43 void LoadStringsIfNeeded_ ()
44 {
45 if (not sNamesLoaded_ and sNamesSupported_) {
46 auto l = sNamesLoaded_.rwget ();
47 if (not static_cast<bool> (l)) {
48 ERR_load_crypto_strings ();
49 l = true;
50 }
51 }
52 }
53}
54
55/*
56 ********************************************************************************
57 ***************************** Cryptography::Exception **************************
58 ********************************************************************************
59 */
60Exception::Exception (InternalErrorCodeType errorCode)
61 : inherited{GetMessage (errorCode)}
62 , fErrorCode_{errorCode}
63{
64}
65
66Exception::InternalErrorCodeType Exception::GetErrorCode () const
67{
68 return fErrorCode_;
69}
70
71Characters::String Exception::GetMessage (InternalErrorCodeType errorCode)
72{
73 LoadStringsIfNeeded_ ();
74 char buf[10 * 1024];
75 buf[0] = '\0';
76 ::ERR_error_string_n (errorCode, buf, NEltsOf (buf));
78}
79
80[[noreturn]] void Exception::ThrowLastError ()
81{
82 Execution::Throw (Exception{ERR_get_error ()});
83}
84
85bool Exception::GetNamesSupported ()
86{
87 return sNamesSupported_.load ();
88}
89
90void Exception::SetNamesSupported (bool openSSLStringsSupported)
91{
92 auto l = sNamesSupported_.rwget ();
93 if (static_cast<bool> (l) != openSSLStringsSupported) {
94 if (openSSLStringsSupported) {
95 // nothing todo - just
96 }
97 else {
98 auto lNamesLoaded = sNamesLoaded_.rwget ();
99 if (static_cast<bool> (l)) {
100 ERR_free_strings ();
101 lNamesLoaded = false;
102 }
103 }
104 sNamesSupported_ = openSSLStringsSupported;
105 }
106}
107#endif
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
static String FromNarrowSDKString(const char *from)
Definition String.inl:470
Wrap any object with Synchronized<> and it can be used similarly to the base type,...
nonvirtual WritableReference rwget()
get a read-write smart pointer to the underlying Synchronized<> object, holding the full lock the who...