Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
LibraryContext.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Cryptography_OpenSSL_LibraryContext_h_
5#define _Stroika_Foundation_Cryptography_OpenSSL_LibraryContext_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <string>
10#include <string_view>
11
12#if qStroika_HasComponent_OpenSSL
13#include <openssl/evp.h>
14#endif
15
17#include "Stroika/Foundation/Common/Common.h"
18#include "Stroika/Foundation/Common/Property.h"
19#include "Stroika/Foundation/Containers/Association.h"
20#include "Stroika/Foundation/Containers/Set.h"
22
25
26/**
27 * \file
28 */
29
30#if qStroika_HasComponent_OpenSSL
31namespace Stroika::Foundation::Cryptography::Providers::OpenSSL {
32
33 using Characters::String;
34 using Containers::Set;
35
36 /**
37 * For now, provide no way to access additional library contexts, as I have no such need right now
38 * and the docs don't make it super clear/simple.
39 */
40 class LibraryContext {
41 private:
42 /**
43 */
44 LibraryContext ();
45
46 public:
47 LibraryContext (const LibraryContext&) = delete;
48
49 public:
50 /**
51 */
52 ~LibraryContext ();
53
54 public:
55 /**
56 * For now, only way to access this functionality is via the default library context.
57 *
58 * Docs not clear on this, but probably best to making any changes to this BEFORE making any other openssl calls
59 */
60 static LibraryContext sDefault;
61
62 public:
63 static inline constexpr string_view kDefaultProvider = "default"sv;
64
65 public:
66 static inline constexpr string_view kLegacyProvider = "legacy"sv;
67
68 public:
69 /**
70 * Loads the given provider, but tracks count, so if loaded 4 times, takes 4 unloads to remove.
71 */
72 nonvirtual void LoadProvider (const String& providerName);
73
74 public:
75 /**
76 * \pre the provider has been successfully loaded with LoadProvider, and the number of loads is greater than the number of
77 * unloads before this.
78 */
79 nonvirtual void UnLoadProvider (const String& providerName);
80
81 public:
82 /**
83 * Stack based wrapper on LoadProvider/UnLoadProvider()
84 */
85 class TemporarilyAddProvider {
86 public:
87 TemporarilyAddProvider () = delete;
88 TemporarilyAddProvider (const TemporarilyAddProvider&) = delete;
89 TemporarilyAddProvider (LibraryContext* context, const String& providerName);
90 ~TemporarilyAddProvider ();
91
92 private:
93 LibraryContext* fContext_;
94 String fProviderName_;
95 };
96
97 public:
98 /**
99 * @brief Return all the cipher algorithms provided by (the linked copy of) openssl.
100 */
101 Common::ReadOnlyProperty<Set<CipherAlgorithm>> availableCipherAlgorithms;
102
103 public:
104 /**
105 * @brief Return all the standard/safe (tbd) cipher algorithms provided by (the linked copy of) openssl.
106 */
107 Common::ReadOnlyProperty<Set<CipherAlgorithm>> standardCipherAlgorithms;
108
109 public:
110 /**
111 * @brief Return all the digest algorithms provided by (the linked copy of) openssl.
112 */
113 Common::ReadOnlyProperty<Set<DigestAlgorithm>> availableDigestAlgorithms;
114
115 public:
116 /**
117 * @brief Return all the standard/safe (tbd) digest algorithms provided by (the linked copy of) openssl.
118 */
119 Common::ReadOnlyProperty<Set<DigestAlgorithm>> standardDigestAlgorithms;
120
121 private:
122 struct LibraryInit_ {
123 LibraryInit_ ();
124 ~LibraryInit_ () = default;
125 };
126
127 private:
128 [[no_unique_address]] LibraryInit_ fLibraryInit_;
129 [[no_unique_address]] Debug::AssertExternallySynchronizedMutex fThisAssertExternallySynchronized_;
130
131#if OPENSSL_VERSION_MAJOR >= 3
132 Containers::Association<String, ::OSSL_PROVIDER*> fLoadedProviders_; // providers maybe listed multiple times here, if loaded multiple times by callers
133#endif
134 };
135 inline LibraryContext LibraryContext::sDefault;
136
137}
138
139#endif
140
141/*
142 ********************************************************************************
143 ***************************** Implementation Details ***************************
144 ********************************************************************************
145 */
146#include "LibraryContext.inl"
147
148#endif /*_Stroika_Foundation_Cryptography_OpenSSL_LibraryContext_h_*/