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 "SolidSyslogMbedTlsAesGcmPolicy.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 "SolidSyslogMbedTlsAesGcmPolicyErrors.h"
14 : : #include "SolidSyslogMbedTlsAesGcmPolicyPrivate.h"
15 : : #include "SolidSyslogNullSecurityPolicy.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogSecurityPolicyDefinition.h"
19 : : #include "SolidSyslogTunables.h"
20 : :
21 : : static inline bool MbedTlsAesGcmPolicy_ConfigIsValid(const struct SolidSyslogMbedTlsAesGcmPolicyConfig* config);
22 : : static inline size_t MbedTlsAesGcmPolicy_IndexFromHandle(const struct SolidSyslogSecurityPolicy* base);
23 : : static inline void MbedTlsAesGcmPolicy_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool MbedTlsAesGcmPolicy_InUse[SOLIDSYSLOG_AES_GCM_POLICY_POOL_SIZE];
26 : : static struct SolidSyslogMbedTlsAesGcmPolicy MbedTlsAesGcmPolicy_Pool[SOLIDSYSLOG_AES_GCM_POLICY_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator MbedTlsAesGcmPolicy_Allocator = {
28 : : MbedTlsAesGcmPolicy_InUse,
29 : : SOLIDSYSLOG_AES_GCM_POLICY_POOL_SIZE
30 : : };
31 : :
32 : 46 : struct SolidSyslogSecurityPolicy* SolidSyslogMbedTlsAesGcmPolicy_Create(
33 : : const struct SolidSyslogMbedTlsAesGcmPolicyConfig* config
34 : : )
35 : : {
36 : 46 : struct SolidSyslogSecurityPolicy* handle = SolidSyslogNullSecurityPolicy_Get();
37 [ + + ]: 46 : if (MbedTlsAesGcmPolicy_ConfigIsValid(config))
38 : : {
39 : 42 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&MbedTlsAesGcmPolicy_Allocator);
40 [ + + ]: 42 : if (SolidSyslogPoolAllocator_IndexIsValid(&MbedTlsAesGcmPolicy_Allocator, index) == true)
41 : : {
42 : 40 : SolidSyslogMbedTlsAesGcmPolicy_Initialise(&MbedTlsAesGcmPolicy_Pool[index].Base, config);
43 : 40 : handle = &MbedTlsAesGcmPolicy_Pool[index].Base;
44 : : }
45 : : else
46 : : {
47 : 2 : MbedTlsAesGcmPolicy_Report(
48 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
49 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
50 : : SOLIDSYSLOG_AES_GCM_POLICY_ERROR_POOL_EXHAUSTED
51 : : );
52 : : }
53 : : }
54 : : else
55 : : {
56 : 4 : MbedTlsAesGcmPolicy_Report(
57 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
58 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
59 : : SOLIDSYSLOG_AES_GCM_POLICY_ERROR_BAD_CONFIG
60 : : );
61 : : }
62 : 46 : return handle;
63 : : }
64 : :
65 : 44 : void SolidSyslogMbedTlsAesGcmPolicy_Destroy(struct SolidSyslogSecurityPolicy* base)
66 : : {
67 : 44 : size_t index = MbedTlsAesGcmPolicy_IndexFromHandle(base);
68 [ + + + + ]: 85 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&MbedTlsAesGcmPolicy_Allocator, index) &&
69 : 41 : SolidSyslogPoolAllocator_FreeIfInUse(
70 : : &MbedTlsAesGcmPolicy_Allocator,
71 : : index,
72 : : MbedTlsAesGcmPolicy_CleanupAtIndex,
73 : : NULL
74 : : );
75 [ + + ]: 44 : if (!released)
76 : : {
77 : 4 : MbedTlsAesGcmPolicy_Report(
78 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
79 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
80 : : SOLIDSYSLOG_AES_GCM_POLICY_ERROR_UNKNOWN_DESTROY
81 : : );
82 : : }
83 : 44 : }
84 : :
85 : 46 : static inline bool MbedTlsAesGcmPolicy_ConfigIsValid(const struct SolidSyslogMbedTlsAesGcmPolicyConfig* config)
86 : : {
87 [ + + + + : 46 : return (config != NULL) && (config->GetKey != NULL) && (config->Rng != NULL);
+ + ]
88 : : }
89 : :
90 : 44 : static inline size_t MbedTlsAesGcmPolicy_IndexFromHandle(const struct SolidSyslogSecurityPolicy* base)
91 : : {
92 : 44 : size_t result = SOLIDSYSLOG_AES_GCM_POLICY_POOL_SIZE;
93 [ + + ]: 47 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_AES_GCM_POLICY_POOL_SIZE; poolIndex++)
94 : : {
95 [ + + ]: 44 : if (base == &MbedTlsAesGcmPolicy_Pool[poolIndex].Base)
96 : : {
97 : 41 : result = poolIndex;
98 : 41 : break;
99 : : }
100 : : }
101 : 44 : return result;
102 : : }
103 : :
104 : 40 : static inline void MbedTlsAesGcmPolicy_CleanupAtIndex(size_t index, void* context)
105 : : {
106 : : (void) context;
107 : 40 : SolidSyslogMbedTlsAesGcmPolicy_Cleanup(&MbedTlsAesGcmPolicy_Pool[index].Base);
108 : 40 : }
|