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 "SolidSyslogMbedTlsHandleCredentials.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : :
10 : : #include "SolidSyslogError.h"
11 : : #include "SolidSyslogErrorCategory.h"
12 : : #include "SolidSyslogMbedTlsCredentialsDefinition.h"
13 : : #include "SolidSyslogMbedTlsHandleCredentialsPrivate.h"
14 : : #include "SolidSyslogMbedTlsNullCredentials.h"
15 : : #include "SolidSyslogPoolAllocator.h"
16 : : #include "SolidSyslogTlsFingerprint.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : static inline bool MbedTlsHandleCredentials_IsValidConfig(const struct SolidSyslogMbedTlsHandleCredentialsConfig* config
21 : : );
22 : : static inline size_t MbedTlsHandleCredentials_IndexFromHandle(const struct SolidSyslogMbedTlsCredentials* base);
23 : : static inline void MbedTlsHandleCredentials_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool MbedTlsHandleCredentials_InUse[SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE];
26 : : static struct SolidSyslogMbedTlsHandleCredentials MbedTlsHandleCredentials_Pool[SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator MbedTlsHandleCredentials_Allocator = {
28 : : MbedTlsHandleCredentials_InUse,
29 : : SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE
30 : : };
31 : :
32 : 60 : struct SolidSyslogMbedTlsCredentials* SolidSyslogMbedTlsHandleCredentials_Create(
33 : : const struct SolidSyslogMbedTlsHandleCredentialsConfig* config
34 : : )
35 : : {
36 : 60 : struct SolidSyslogMbedTlsCredentials* handle = SolidSyslogMbedTlsNullCredentials_Get();
37 [ + + ]: 60 : if (MbedTlsHandleCredentials_IsValidConfig(config))
38 : : {
39 : 53 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&MbedTlsHandleCredentials_Allocator);
40 [ + + ]: 53 : if (SolidSyslogPoolAllocator_IndexIsValid(&MbedTlsHandleCredentials_Allocator, index) == true)
41 : : {
42 : 51 : SolidSyslogMbedTlsHandleCredentials_Initialise(&MbedTlsHandleCredentials_Pool[index].Base, config);
43 : 51 : handle = &MbedTlsHandleCredentials_Pool[index].Base;
44 : : }
45 : : else
46 : : {
47 : 2 : MbedTlsHandleCredentials_Report(
48 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
49 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
50 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_POOL_EXHAUSTED
51 : : );
52 : : }
53 : : }
54 : 60 : return handle;
55 : : }
56 : :
57 : : /* The RNG is checked here rather than where it is used, so a wiring fault is
58 : : * one Create-time report instead of a surprise on the connection that first
59 : : * presents a client credential. */
60 : 60 : static inline bool MbedTlsHandleCredentials_IsValidConfig(const struct SolidSyslogMbedTlsHandleCredentialsConfig* config
61 : : )
62 : : {
63 : 60 : bool valid = false;
64 [ + + ]: 60 : if (config == NULL)
65 : : {
66 : 2 : MbedTlsHandleCredentials_Report(
67 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
68 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
69 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_NULL_CONFIG
70 : : );
71 : : }
72 [ + + ]: 58 : else if (config->Rng == NULL)
73 : : {
74 : 2 : MbedTlsHandleCredentials_Report(
75 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
76 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
77 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_NULL_RNG
78 : : );
79 : : }
80 [ + + ]: 56 : else if (!SolidSyslogTlsFingerprint_ListIsPresent(config->PeerFingerprints, config->PeerFingerprintCount))
81 : : {
82 : 3 : MbedTlsHandleCredentials_Report(
83 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
84 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
85 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_NULL_PEER_FINGERPRINT
86 : : );
87 : : }
88 : : else
89 : : {
90 : 53 : valid = true;
91 : : }
92 : 60 : return valid;
93 : : }
94 : :
95 : 53 : void SolidSyslogMbedTlsHandleCredentials_Destroy(struct SolidSyslogMbedTlsCredentials* base)
96 : : {
97 : 53 : size_t index = MbedTlsHandleCredentials_IndexFromHandle(base);
98 [ + + + + ]: 105 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&MbedTlsHandleCredentials_Allocator, index) &&
99 : 52 : SolidSyslogPoolAllocator_FreeIfInUse(
100 : : &MbedTlsHandleCredentials_Allocator,
101 : : index,
102 : : MbedTlsHandleCredentials_CleanupAtIndex,
103 : : NULL
104 : : );
105 [ + + ]: 53 : if (!released)
106 : : {
107 : 2 : MbedTlsHandleCredentials_Report(
108 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
109 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
110 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_UNKNOWN_DESTROY
111 : : );
112 : : }
113 : 53 : }
114 : :
115 : 53 : static inline size_t MbedTlsHandleCredentials_IndexFromHandle(const struct SolidSyslogMbedTlsCredentials* base)
116 : : {
117 : 53 : size_t result = SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE;
118 [ + + ]: 54 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE; poolIndex++)
119 : : {
120 [ + + ]: 53 : if (base == &MbedTlsHandleCredentials_Pool[poolIndex].Base)
121 : : {
122 : 52 : result = poolIndex;
123 : 52 : break;
124 : : }
125 : : }
126 : 53 : return result;
127 : : }
128 : :
129 : 51 : static inline void MbedTlsHandleCredentials_CleanupAtIndex(size_t index, void* context)
130 : : {
131 : : (void) context;
132 : : (void) index;
133 : 51 : }
|