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 "SolidSyslogWolfSslSettings.h"
6 : :
7 : : #include "SolidSyslogWolfSslStream.h"
8 : :
9 : : #include <stdbool.h>
10 : : #include <stddef.h>
11 : : #include <stdint.h>
12 : :
13 : : #include "SolidSyslogError.h"
14 : : #include "SolidSyslogErrorCategory.h"
15 : : #include "SolidSyslogNullStream.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : : #include "SolidSyslogWolfSslStreamErrors.h"
20 : : #include "SolidSyslogWolfSslStreamPrivate.h"
21 : :
22 : : struct SolidSyslogStream;
23 : :
24 : : static inline bool WolfSslStream_IsValidConfig(const struct SolidSyslogWolfSslStreamConfig* config);
25 : : static inline size_t WolfSslStream_IndexFromHandle(const struct SolidSyslogStream* base);
26 : : static inline void WolfSslStream_CleanupAtIndex(size_t index, void* context);
27 : :
28 : : static bool WolfSslStream_InUse[SOLIDSYSLOG_TLS_STREAM_POOL_SIZE];
29 : : static struct SolidSyslogWolfSslStream WolfSslStream_Pool[SOLIDSYSLOG_TLS_STREAM_POOL_SIZE];
30 : : static struct SolidSyslogPoolAllocator WolfSslStream_Allocator = {
31 : : WolfSslStream_InUse,
32 : : SOLIDSYSLOG_TLS_STREAM_POOL_SIZE
33 : : };
34 : :
35 : 307 : struct SolidSyslogStream* SolidSyslogWolfSslStream_Create(const struct SolidSyslogWolfSslStreamConfig* config)
36 : : {
37 : 307 : struct SolidSyslogStream* handle = SolidSyslogNullStream_Get();
38 [ + + ]: 307 : if (WolfSslStream_IsValidConfig(config))
39 : : {
40 : 299 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&WolfSslStream_Allocator);
41 [ + + ]: 299 : if (SolidSyslogPoolAllocator_IndexIsValid(&WolfSslStream_Allocator, index) == true)
42 : : {
43 : 295 : SolidSyslogWolfSslStream_Initialise(&WolfSslStream_Pool[index].Base, config);
44 : 295 : handle = &WolfSslStream_Pool[index].Base;
45 : : }
46 : : else
47 : : {
48 : 4 : WolfSslStream_Report(
49 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
50 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
51 : : SOLIDSYSLOG_TLS_STREAM_ERROR_POOL_EXHAUSTED
52 : : );
53 : : }
54 : : }
55 : 307 : return handle;
56 : : }
57 : :
58 : 307 : static inline bool WolfSslStream_IsValidConfig(const struct SolidSyslogWolfSslStreamConfig* config)
59 : : {
60 : 307 : bool valid = false;
61 [ + + ]: 307 : if (config == NULL)
62 : : {
63 : 2 : WolfSslStream_Report(
64 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
65 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
66 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_CONFIG
67 : : );
68 : : }
69 [ + + ]: 305 : else if (config->Transport == NULL)
70 : : {
71 : 2 : WolfSslStream_Report(
72 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
73 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
74 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_TRANSPORT
75 : : );
76 : : }
77 [ + + ]: 303 : else if (config->Sleep == NULL)
78 : : {
79 : 2 : WolfSslStream_Report(
80 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
81 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
82 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_SLEEP
83 : : );
84 : : }
85 [ + + ]: 301 : else if (config->Credentials == NULL)
86 : : {
87 : 2 : WolfSslStream_Report(
88 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
89 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
90 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_CREDENTIALS
91 : : );
92 : : }
93 : : else
94 : : {
95 : 299 : valid = true;
96 : : }
97 : 307 : return valid;
98 : : }
99 : :
100 : 302 : void SolidSyslogWolfSslStream_Destroy(struct SolidSyslogStream* base)
101 : : {
102 : 302 : size_t index = WolfSslStream_IndexFromHandle(base);
103 : 302 : bool released =
104 [ + + + + ]: 598 : SolidSyslogPoolAllocator_IndexIsValid(&WolfSslStream_Allocator, index) &&
105 : 296 : SolidSyslogPoolAllocator_FreeIfInUse(&WolfSslStream_Allocator, index, WolfSslStream_CleanupAtIndex, NULL);
106 [ + + ]: 302 : if (!released)
107 : : {
108 : 7 : WolfSslStream_Report(
109 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
110 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
111 : : SOLIDSYSLOG_TLS_STREAM_ERROR_UNKNOWN_DESTROY
112 : : );
113 : : }
114 : 302 : }
115 : :
116 : 302 : static inline size_t WolfSslStream_IndexFromHandle(const struct SolidSyslogStream* base)
117 : : {
118 : 302 : size_t result = SOLIDSYSLOG_TLS_STREAM_POOL_SIZE;
119 [ + + ]: 308 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_TLS_STREAM_POOL_SIZE; poolIndex++)
120 : : {
121 [ + + ]: 302 : if (base == &WolfSslStream_Pool[poolIndex].Base)
122 : : {
123 : 296 : result = poolIndex;
124 : 296 : break;
125 : : }
126 : : }
127 : 302 : return result;
128 : : }
129 : :
130 : 295 : static inline void WolfSslStream_CleanupAtIndex(size_t index, void* context)
131 : : {
132 : : (void) context;
133 : 295 : SolidSyslogWolfSslStream_Cleanup(&WolfSslStream_Pool[index].Base);
134 : 295 : }
|