Branch data Line data Source code
1 : : /* SPDX-FileCopyrightText: Copyright 2026 Cozens Software Solutions Limited
2 : : * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 OR LicenseRef-PolyForm-Internal-Use-1.0.0 OR LicenseRef-COSOSO-Commercial
3 : : */
4 : :
5 : : #include "SolidSyslogWolfSslPemBufferCredentials.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : :
10 : : #include "SolidSyslogError.h"
11 : : #include "SolidSyslogErrorCategory.h"
12 : : #include "SolidSyslogPoolAllocator.h"
13 : : #include "SolidSyslogPrival.h"
14 : : #include "SolidSyslogTlsFingerprint.h"
15 : : #include "SolidSyslogTunables.h"
16 : : #include "SolidSyslogWolfSslCredentialsDefinition.h"
17 : : #include "SolidSyslogWolfSslNullCredentials.h"
18 : : #include "SolidSyslogWolfSslPemBufferCredentialsPrivate.h"
19 : :
20 : : static inline bool WolfSslPemBufferCredentials_IsValidConfig(
21 : : const struct SolidSyslogWolfSslPemBufferCredentialsConfig* config
22 : : );
23 : : static inline size_t WolfSslPemBufferCredentials_IndexFromHandle(const struct SolidSyslogWolfSslCredentials* base);
24 : : static inline void WolfSslPemBufferCredentials_CleanupAtIndex(size_t index, void* context);
25 : :
26 : : static bool WolfSslPemBufferCredentials_InUse[SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE];
27 : : static struct SolidSyslogWolfSslPemBufferCredentials
28 : : WolfSslPemBufferCredentials_Pool[SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE];
29 : : static struct SolidSyslogPoolAllocator WolfSslPemBufferCredentials_Allocator = {
30 : : WolfSslPemBufferCredentials_InUse,
31 : : SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE
32 : : };
33 : :
34 : 157 : struct SolidSyslogWolfSslCredentials* SolidSyslogWolfSslPemBufferCredentials_Create(
35 : : const struct SolidSyslogWolfSslPemBufferCredentialsConfig* config
36 : : )
37 : : {
38 : 157 : struct SolidSyslogWolfSslCredentials* handle = SolidSyslogWolfSslNullCredentials_Get();
39 [ + + ]: 157 : if (WolfSslPemBufferCredentials_IsValidConfig(config))
40 : : {
41 : 154 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&WolfSslPemBufferCredentials_Allocator);
42 [ + + ]: 154 : if (SolidSyslogPoolAllocator_IndexIsValid(&WolfSslPemBufferCredentials_Allocator, index) == true)
43 : : {
44 : 153 : SolidSyslogWolfSslPemBufferCredentials_Initialise(&WolfSslPemBufferCredentials_Pool[index].Base, config);
45 : 153 : handle = &WolfSslPemBufferCredentials_Pool[index].Base;
46 : : }
47 : : else
48 : : {
49 : 1 : WolfSslPemBufferCredentials_Report(
50 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
51 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
52 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_POOL_EXHAUSTED
53 : : );
54 : : }
55 : : }
56 : 157 : return handle;
57 : : }
58 : :
59 : 157 : static inline bool WolfSslPemBufferCredentials_IsValidConfig(
60 : : const struct SolidSyslogWolfSslPemBufferCredentialsConfig* config
61 : : )
62 : : {
63 : 157 : bool valid = false;
64 [ + + ]: 157 : if (config == NULL)
65 : : {
66 : 1 : WolfSslPemBufferCredentials_Report(
67 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
68 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
69 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_NULL_CONFIG
70 : : );
71 : : }
72 [ + + ]: 156 : else if (!SolidSyslogTlsFingerprint_ListIsPresent(config->PeerFingerprints, config->PeerFingerprintCount))
73 : : {
74 : 2 : WolfSslPemBufferCredentials_Report(
75 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
76 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
77 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_NULL_PEER_FINGERPRINT
78 : : );
79 : : }
80 : : else
81 : : {
82 : 154 : valid = true;
83 : : }
84 : 157 : return valid;
85 : : }
86 : :
87 : 157 : void SolidSyslogWolfSslPemBufferCredentials_Destroy(struct SolidSyslogWolfSslCredentials* base)
88 : : {
89 : 157 : size_t index = WolfSslPemBufferCredentials_IndexFromHandle(base);
90 [ + + + + ]: 311 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&WolfSslPemBufferCredentials_Allocator, index) &&
91 : 154 : SolidSyslogPoolAllocator_FreeIfInUse(
92 : : &WolfSslPemBufferCredentials_Allocator,
93 : : index,
94 : : WolfSslPemBufferCredentials_CleanupAtIndex,
95 : : NULL
96 : : );
97 [ + + ]: 157 : if (!released)
98 : : {
99 : 4 : WolfSslPemBufferCredentials_Report(
100 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
101 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
102 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_UNKNOWN_DESTROY
103 : : );
104 : : }
105 : 157 : }
106 : :
107 : 157 : static inline size_t WolfSslPemBufferCredentials_IndexFromHandle(const struct SolidSyslogWolfSslCredentials* base)
108 : : {
109 : 157 : size_t result = SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE;
110 [ + + ]: 160 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE; poolIndex++)
111 : : {
112 [ + + ]: 157 : if (base == &WolfSslPemBufferCredentials_Pool[poolIndex].Base)
113 : : {
114 : 154 : result = poolIndex;
115 : 154 : break;
116 : : }
117 : : }
118 : 157 : return result;
119 : : }
120 : :
121 : 153 : static inline void WolfSslPemBufferCredentials_CleanupAtIndex(size_t index, void* context)
122 : : {
123 : : (void) context;
124 : 153 : SolidSyslogWolfSslPemBufferCredentials_Cleanup(&WolfSslPemBufferCredentials_Pool[index].Base);
125 : 153 : }
|