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 "SolidSyslogMbedTlsHmacSha256Policy.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 "SolidSyslogMbedTlsHmacSha256PolicyErrors.h"
14 : : #include "SolidSyslogMbedTlsHmacSha256PolicyPrivate.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 MbedTlsHmacSha256Policy_ConfigIsValid(const struct SolidSyslogMbedTlsHmacSha256PolicyConfig* config);
22 : : static inline size_t MbedTlsHmacSha256Policy_IndexFromHandle(const struct SolidSyslogSecurityPolicy* base);
23 : : static inline void MbedTlsHmacSha256Policy_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool MbedTlsHmacSha256Policy_InUse[SOLIDSYSLOG_HMAC_SHA256_POLICY_POOL_SIZE];
26 : : static struct SolidSyslogMbedTlsHmacSha256Policy MbedTlsHmacSha256Policy_Pool[SOLIDSYSLOG_HMAC_SHA256_POLICY_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator MbedTlsHmacSha256Policy_Allocator = {
28 : : MbedTlsHmacSha256Policy_InUse,
29 : : SOLIDSYSLOG_HMAC_SHA256_POLICY_POOL_SIZE
30 : : };
31 : :
32 : 34 : struct SolidSyslogSecurityPolicy* SolidSyslogMbedTlsHmacSha256Policy_Create(
33 : : const struct SolidSyslogMbedTlsHmacSha256PolicyConfig* config
34 : : )
35 : : {
36 : 34 : struct SolidSyslogSecurityPolicy* handle = SolidSyslogNullSecurityPolicy_Get();
37 [ + + ]: 34 : if (MbedTlsHmacSha256Policy_ConfigIsValid(config))
38 : : {
39 : 31 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&MbedTlsHmacSha256Policy_Allocator);
40 [ + + ]: 31 : if (SolidSyslogPoolAllocator_IndexIsValid(&MbedTlsHmacSha256Policy_Allocator, index) == true)
41 : : {
42 : 29 : SolidSyslogMbedTlsHmacSha256Policy_Initialise(&MbedTlsHmacSha256Policy_Pool[index].Base, config);
43 : 29 : handle = &MbedTlsHmacSha256Policy_Pool[index].Base;
44 : : }
45 : : else
46 : : {
47 : 2 : MbedTlsHmacSha256Policy_Report(
48 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
49 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
50 : : SOLIDSYSLOG_HMAC_SHA256_POLICY_ERROR_POOL_EXHAUSTED
51 : : );
52 : : }
53 : : }
54 : : else
55 : : {
56 : 3 : MbedTlsHmacSha256Policy_Report(
57 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
58 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
59 : : SOLIDSYSLOG_HMAC_SHA256_POLICY_ERROR_BAD_CONFIG
60 : : );
61 : : }
62 : 34 : return handle;
63 : : }
64 : :
65 : 33 : void SolidSyslogMbedTlsHmacSha256Policy_Destroy(struct SolidSyslogSecurityPolicy* base)
66 : : {
67 : 33 : size_t index = MbedTlsHmacSha256Policy_IndexFromHandle(base);
68 [ + + + + ]: 63 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&MbedTlsHmacSha256Policy_Allocator, index) &&
69 : 30 : SolidSyslogPoolAllocator_FreeIfInUse(
70 : : &MbedTlsHmacSha256Policy_Allocator,
71 : : index,
72 : : MbedTlsHmacSha256Policy_CleanupAtIndex,
73 : : NULL
74 : : );
75 [ + + ]: 33 : if (!released)
76 : : {
77 : 4 : MbedTlsHmacSha256Policy_Report(
78 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
79 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
80 : : SOLIDSYSLOG_HMAC_SHA256_POLICY_ERROR_UNKNOWN_DESTROY
81 : : );
82 : : }
83 : 33 : }
84 : :
85 : 34 : static inline bool MbedTlsHmacSha256Policy_ConfigIsValid(const struct SolidSyslogMbedTlsHmacSha256PolicyConfig* config)
86 : : {
87 [ + + + + ]: 34 : return (config != NULL) && (config->GetKey != NULL);
88 : : }
89 : :
90 : 33 : static inline size_t MbedTlsHmacSha256Policy_IndexFromHandle(const struct SolidSyslogSecurityPolicy* base)
91 : : {
92 : 33 : size_t result = SOLIDSYSLOG_HMAC_SHA256_POLICY_POOL_SIZE;
93 [ + + ]: 36 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_HMAC_SHA256_POLICY_POOL_SIZE; poolIndex++)
94 : : {
95 [ + + ]: 33 : if (base == &MbedTlsHmacSha256Policy_Pool[poolIndex].Base)
96 : : {
97 : 30 : result = poolIndex;
98 : 30 : break;
99 : : }
100 : : }
101 : 33 : return result;
102 : : }
103 : :
104 : 29 : static inline void MbedTlsHmacSha256Policy_CleanupAtIndex(size_t index, void* context)
105 : : {
106 : : (void) context;
107 : 29 : SolidSyslogMbedTlsHmacSha256Policy_Cleanup(&MbedTlsHmacSha256Policy_Pool[index].Base);
108 : 29 : }
|