Stroika Library 3.0d16
 
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
24#if qStroika_HasComponent_OpenSSL && defined(_MSC_VER)
25// Use #pragma comment lib instead of explicit entry in the lib entry of the project file
26#if OPENSSL_VERSION_NUMBER < 0x1010000fL
27#pragma comment(lib, "libeay32.lib")
28#pragma comment(lib, "ssleay32.lib")
29#else
30#pragma comment(lib, "libcrypto.lib")
31#pragma comment(lib, "libssl.lib")
32#pragma comment(lib, "ws2_32.lib")
33#pragma comment(lib, "crypt32.lib")
34#endif
35#endif
36
38
39#if qStroika_HasComponent_OpenSSL
40namespace {
41 Synchronized<bool> sNamesSupported_{true}; // @todo note locking not done right yet here - not safely...
42 Synchronized<bool> sNamesLoaded_{false};
43
44 struct ErrStringIniter_ {
45 ~ErrStringIniter_ ()
46 {
47 if (sNamesSupported_) {
48 auto l = sNamesLoaded_.rwget ();
49 if (static_cast<bool> (l)) {
50 ERR_free_strings ();
51 l = false;
52 }
53 }
54 }
55 } _InitOpenSSLErrStrings_;
56 void LoadStringsIfNeeded_ ()
57 {
58 if (not sNamesLoaded_ and sNamesSupported_) {
59 auto l = sNamesLoaded_.rwget ();
60 if (not static_cast<bool> (l)) {
61 ERR_load_crypto_strings ();
62 l = true;
63 }
64 }
65 }
66}
67
68/*
69 ********************************************************************************
70 ***************************** Cryptography::Exception **************************
71 ********************************************************************************
72 */
73Exception::Exception (InternalErrorCodeType errorCode)
74 : inherited{GetMessage (errorCode)}
75 , fErrorCode_{errorCode}
76{
77}
78
79Exception::InternalErrorCodeType Exception::GetErrorCode () const
80{
81 return fErrorCode_;
82}
83
84Characters::String Exception::GetMessage (InternalErrorCodeType errorCode)
85{
86 LoadStringsIfNeeded_ ();
87 char buf[10 * 1024];
88 buf[0] = '\0';
89 ::ERR_error_string_n (errorCode, buf, NEltsOf (buf));
91}
92
93[[noreturn]] void Exception::ThrowLastError ()
94{
95 Execution::Throw (Exception{ERR_get_error ()});
96}
97
98bool Exception::GetNamesSupported ()
99{
100 return sNamesSupported_.load ();
101}
102
103void Exception::SetNamesSupported (bool openSSLStringsSupported)
104{
105 auto l = sNamesSupported_.rwget ();
106 if (static_cast<bool> (l) != openSSLStringsSupported) {
107 if (openSSLStringsSupported) {
108 // nothing todo - just
109 }
110 else {
111 auto lNamesLoaded = sNamesLoaded_.rwget ();
112 if (static_cast<bool> (l)) {
113 ERR_free_strings ();
114 lNamesLoaded = false;
115 }
116 }
117 sNamesSupported_ = openSSLStringsSupported;
118 }
119}
120#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...
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43