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 "SolidSyslogMbedTlsStream.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 "SolidSyslogMbedTlsStreamErrors.h"
14 : : #include "SolidSyslogMbedTlsStreamPrivate.h"
15 : : #include "SolidSyslogNullStream.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogStream;
21 : :
22 : : static inline bool MbedTlsStream_IsValidConfig(const struct SolidSyslogMbedTlsStreamConfig* config);
23 : : static inline size_t MbedTlsStream_IndexFromHandle(const struct SolidSyslogStream* base);
24 : : static inline void MbedTlsStream_CleanupAtIndex(size_t index, void* context);
25 : :
26 : : static bool MbedTlsStream_InUse[SOLIDSYSLOG_TLS_STREAM_POOL_SIZE];
27 : : static struct SolidSyslogMbedTlsStream MbedTlsStream_Pool[SOLIDSYSLOG_TLS_STREAM_POOL_SIZE];
28 : : static struct SolidSyslogPoolAllocator MbedTlsStream_Allocator = {
29 : : MbedTlsStream_InUse,
30 : : SOLIDSYSLOG_TLS_STREAM_POOL_SIZE
31 : : };
32 : :
33 : 231 : struct SolidSyslogStream* SolidSyslogMbedTlsStream_Create(const struct SolidSyslogMbedTlsStreamConfig* config)
34 : : {
35 : 231 : struct SolidSyslogStream* handle = SolidSyslogNullStream_Get();
36 [ + + ]: 231 : if (MbedTlsStream_IsValidConfig(config))
37 : : {
38 : 221 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&MbedTlsStream_Allocator);
39 [ + + ]: 221 : if (SolidSyslogPoolAllocator_IndexIsValid(&MbedTlsStream_Allocator, index) == true)
40 : : {
41 : 217 : SolidSyslogMbedTlsStream_Initialise(&MbedTlsStream_Pool[index].Base, config);
42 : 217 : handle = &MbedTlsStream_Pool[index].Base;
43 : : }
44 : : else
45 : : {
46 : 4 : MbedTlsStream_Report(
47 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
48 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
49 : : SOLIDSYSLOG_TLS_STREAM_ERROR_POOL_EXHAUSTED
50 : : );
51 : : }
52 : : }
53 : 231 : return handle;
54 : : }
55 : :
56 : 231 : static inline bool MbedTlsStream_IsValidConfig(const struct SolidSyslogMbedTlsStreamConfig* config)
57 : : {
58 : 231 : bool valid = false;
59 [ + + ]: 231 : if (config == NULL)
60 : : {
61 : 2 : MbedTlsStream_Report(
62 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
63 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
64 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_CONFIG
65 : : );
66 : : }
67 [ + + ]: 229 : else if (config->Transport == NULL)
68 : : {
69 : 2 : MbedTlsStream_Report(
70 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
71 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
72 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_TRANSPORT
73 : : );
74 : : }
75 [ + + ]: 227 : else if (config->Sleep == NULL)
76 : : {
77 : 2 : MbedTlsStream_Report(
78 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
79 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
80 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_SLEEP
81 : : );
82 : : }
83 [ + + ]: 225 : else if (config->Rng == NULL)
84 : : {
85 : 2 : MbedTlsStream_Report(
86 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
87 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
88 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_RNG
89 : : );
90 : : }
91 [ + + ]: 223 : else if (config->Credentials == NULL)
92 : : {
93 : 2 : MbedTlsStream_Report(
94 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
95 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
96 : : SOLIDSYSLOG_TLS_STREAM_ERROR_NULL_CREDENTIALS
97 : : );
98 : : }
99 : : else
100 : : {
101 : 221 : valid = true;
102 : : }
103 : 231 : return valid;
104 : : }
105 : :
106 : 224 : void SolidSyslogMbedTlsStream_Destroy(struct SolidSyslogStream* base)
107 : : {
108 : 224 : size_t index = MbedTlsStream_IndexFromHandle(base);
109 : 224 : bool released =
110 [ + + + + ]: 442 : SolidSyslogPoolAllocator_IndexIsValid(&MbedTlsStream_Allocator, index) &&
111 : 218 : SolidSyslogPoolAllocator_FreeIfInUse(&MbedTlsStream_Allocator, index, MbedTlsStream_CleanupAtIndex, NULL);
112 [ + + ]: 224 : if (!released)
113 : : {
114 : 7 : MbedTlsStream_Report(
115 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
116 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
117 : : SOLIDSYSLOG_TLS_STREAM_ERROR_UNKNOWN_DESTROY
118 : : );
119 : : }
120 : 224 : }
121 : :
122 : 224 : static inline size_t MbedTlsStream_IndexFromHandle(const struct SolidSyslogStream* base)
123 : : {
124 : 224 : size_t result = SOLIDSYSLOG_TLS_STREAM_POOL_SIZE;
125 [ + + ]: 230 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_TLS_STREAM_POOL_SIZE; poolIndex++)
126 : : {
127 [ + + ]: 224 : if (base == &MbedTlsStream_Pool[poolIndex].Base)
128 : : {
129 : 218 : result = poolIndex;
130 : 218 : break;
131 : : }
132 : : }
133 : 224 : return result;
134 : : }
135 : :
136 : 217 : static inline void MbedTlsStream_CleanupAtIndex(size_t index, void* context)
137 : : {
138 : : (void) context;
139 : 217 : SolidSyslogMbedTlsStream_Cleanup(&MbedTlsStream_Pool[index].Base);
140 : 217 : }
|