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