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 "SolidSyslogOpenSslPemFileCredentials.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : :
10 : : #include "SolidSyslogError.h"
11 : : #include "SolidSyslogErrorCategory.h"
12 : : #include "SolidSyslogOpenSslCredentialsDefinition.h"
13 : : #include "SolidSyslogOpenSslNullCredentials.h"
14 : : #include "SolidSyslogOpenSslPemFileCredentialsPrivate.h"
15 : : #include "SolidSyslogPoolAllocator.h"
16 : : #include "SolidSyslogTlsFingerprint.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : static inline size_t OpenSslPemFileCredentials_IndexFromHandle(const struct SolidSyslogOpenSslCredentials* base);
21 : : static inline void OpenSslPemFileCredentials_CleanupAtIndex(size_t index, void* context);
22 : :
23 : : static bool OpenSslPemFileCredentials_InUse[SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE];
24 : : static struct SolidSyslogOpenSslPemFileCredentials
25 : : OpenSslPemFileCredentials_Pool[SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE];
26 : : static struct SolidSyslogPoolAllocator OpenSslPemFileCredentials_Allocator = {
27 : : OpenSslPemFileCredentials_InUse,
28 : : SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE
29 : : };
30 : :
31 : 65 : struct SolidSyslogOpenSslCredentials* SolidSyslogOpenSslPemFileCredentials_Create(
32 : : const struct SolidSyslogOpenSslPemFileCredentialsConfig* config
33 : : )
34 : : {
35 : 65 : struct SolidSyslogOpenSslCredentials* handle = SolidSyslogOpenSslNullCredentials_Get();
36 [ + + ]: 65 : if (config == NULL)
37 : : {
38 : 2 : OpenSslPemFileCredentials_Report(
39 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
40 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
41 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_NULL_CONFIG
42 : : );
43 : : }
44 [ + + ]: 63 : else if (!SolidSyslogTlsFingerprint_ListIsPresent(config->PeerFingerprints, config->PeerFingerprintCount))
45 : : {
46 : 4 : OpenSslPemFileCredentials_Report(
47 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
48 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
49 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_NULL_PEER_FINGERPRINT
50 : : );
51 : : }
52 : : else
53 : : {
54 : 59 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&OpenSslPemFileCredentials_Allocator);
55 [ + + ]: 59 : if (SolidSyslogPoolAllocator_IndexIsValid(&OpenSslPemFileCredentials_Allocator, index) == true)
56 : : {
57 : 57 : SolidSyslogOpenSslPemFileCredentials_Initialise(&OpenSslPemFileCredentials_Pool[index].Base, config);
58 : 57 : handle = &OpenSslPemFileCredentials_Pool[index].Base;
59 : : }
60 : : else
61 : : {
62 : 2 : OpenSslPemFileCredentials_Report(
63 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
64 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
65 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_POOL_EXHAUSTED
66 : : );
67 : : }
68 : : }
69 : 65 : return handle;
70 : : }
71 : :
72 : 59 : void SolidSyslogOpenSslPemFileCredentials_Destroy(struct SolidSyslogOpenSslCredentials* base)
73 : : {
74 : 59 : size_t index = OpenSslPemFileCredentials_IndexFromHandle(base);
75 [ + + + + ]: 117 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&OpenSslPemFileCredentials_Allocator, index) &&
76 : 58 : SolidSyslogPoolAllocator_FreeIfInUse(
77 : : &OpenSslPemFileCredentials_Allocator,
78 : : index,
79 : : OpenSslPemFileCredentials_CleanupAtIndex,
80 : : NULL
81 : : );
82 [ + + ]: 59 : if (!released)
83 : : {
84 : 2 : OpenSslPemFileCredentials_Report(
85 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
86 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
87 : : SOLIDSYSLOG_TLS_CREDENTIALS_ERROR_UNKNOWN_DESTROY
88 : : );
89 : : }
90 : 59 : }
91 : :
92 : 59 : static inline size_t OpenSslPemFileCredentials_IndexFromHandle(const struct SolidSyslogOpenSslCredentials* base)
93 : : {
94 : 59 : size_t result = SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE;
95 [ + + ]: 60 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_TLS_CREDENTIALS_POOL_SIZE; poolIndex++)
96 : : {
97 [ + + ]: 59 : if (base == &OpenSslPemFileCredentials_Pool[poolIndex].Base)
98 : : {
99 : 58 : result = poolIndex;
100 : 58 : break;
101 : : }
102 : : }
103 : 59 : return result;
104 : : }
105 : :
106 : 57 : static inline void OpenSslPemFileCredentials_CleanupAtIndex(size_t index, void* context)
107 : : {
108 : : (void) context;
109 : : (void) index;
110 : 57 : }
|