Coverage Report

Created: 2026-12-01 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/S2OPC/src/PubSub/security/sopc_pubsub_sks.c
Line
Count
Source
1
/*
2
 * Licensed to Systerel under one or more contributor license
3
 * agreements. See the NOTICE file distributed with this work
4
 * for additional information regarding copyright ownership.
5
 * Systerel licenses this file to you under the Apache
6
 * License, Version 2.0 (the "License"); you may not use this
7
 * file except in compliance with the License. You may obtain
8
 * a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
#include <stdbool.h>
21
22
#include "sopc_assert.h"
23
#include "sopc_common_constants.h"
24
#include "sopc_macros.h"
25
#include "sopc_mem_alloc.h"
26
#include "sopc_pubsub_constants.h"
27
#include "sopc_pubsub_sks.h"
28
#include "sopc_sk_secu_group_managers.h"
29
30
void SOPC_PubSubSKS_Init(void)
31
0
{
32
    // Initialize the SK Managers for security groups
33
0
    SOPC_SK_SecurityGroup_Managers_Init();
34
0
}
35
36
void SOPC_PubSubSKS_Clear(void)
37
0
{
38
    // Clear the SK Managers for security groups
39
0
    SOPC_SK_SecurityGroup_Managers_Clear();
40
0
}
41
42
bool SOPC_PubSubSKS_AddSkManager(SOPC_SKManager* skm)
43
0
{
44
0
    if (NULL == skm || NULL == skm->securityGroupId)
45
0
    {
46
0
        return false;
47
0
    }
48
49
0
    SOPC_SKManager* prevSkManager = SOPC_SK_SecurityGroup_GetSkManager(skm->securityGroupId);
50
0
    bool result = SOPC_SK_SecurityGroup_SetSkManager(skm);
51
0
    if (result && NULL != prevSkManager)
52
0
    {
53
        // If a previous SK Manager exists, delete it
54
0
        SOPC_SKManager_Clear(prevSkManager);
55
0
        SOPC_Free(prevSkManager);
56
0
    }
57
0
    return result;
58
0
}
59
60
SOPC_PubSubSKS_Keys* SOPC_PubSubSKS_GetSecurityKeys(const char* securityGroupid, uint32_t tokenId)
61
0
{
62
0
    SOPC_PubSubSKS_Keys* returnedKeys = NULL;
63
0
    SOPC_PubSubSKS_GetUpdateSecurityKeys(securityGroupid, tokenId, &returnedKeys);
64
0
    return returnedKeys;
65
0
}
66
67
SOPC_PubSub_SecurityStatus SOPC_PubSubSKS_GetUpdateSecurityKeys(const char* securityGroupId,
68
                                                                uint32_t tokenId,
69
                                                                SOPC_PubSubSKS_Keys** securityGroup)
70
0
{
71
0
    SOPC_PubSubSKS_Keys* returnedKeys = NULL;
72
73
    // Get keys from manager
74
0
    SOPC_String* SecurityPolicyUri = NULL;
75
0
    uint32_t FirstTokenId = 0;
76
0
    SOPC_ByteString* Keys = NULL;
77
0
    uint32_t NbKeys = 0;
78
0
    uint64_t TimeToNextKey = 0;
79
0
    uint64_t KeyLifetime = 0;
80
0
    SOPC_SKManager* skm = SOPC_SK_SecurityGroup_GetSkManager(securityGroupId);
81
0
    SOPC_PubSub_SecurityStatus secuStatus = SOPC_PUBSUB_STATUS_SECURITY_OK;
82
83
    // Invalid arguments
84
0
    if (NULL == skm || NULL == securityGroup)
85
0
    {
86
0
        return SOPC_PUBSUB_STATUS_SECURITY_INVALID_PARAMETERS;
87
0
    }
88
89
    // First check if the SG token id is not already the one requested (still current one or still the one requested)
90
0
    if (*securityGroup != NULL && (SOPC_PUBSUB_SKS_CURRENT_TOKENID == tokenId || (*securityGroup)->tokenId == tokenId))
91
0
    {
92
        // Ask for 0 keys only to know if the token id requested is available
93
0
        SOPC_ReturnStatus status =
94
0
            SOPC_SKManager_GetKeys(skm, tokenId, 0, NULL, &FirstTokenId, &Keys, &NbKeys, &TimeToNextKey, &KeyLifetime);
95
96
0
        if (SOPC_STATUS_OK != status)
97
0
        {
98
            // SKmanager fail to get keys
99
0
            return SOPC_PUBSUB_STATUS_SECURITY_NOK;
100
0
        }
101
0
        else if ((*securityGroup)->tokenId == FirstTokenId)
102
0
        {
103
            // the token id that will be returned is the same as SG current token id
104
            // => nothing to do, SG already has the requested and up to date token
105
0
            return SOPC_PUBSUB_STATUS_SECURITY_OK;
106
0
        }
107
0
    }
108
109
    // If there is an available key to update in our SG, ask for this key (one key needed)
110
0
    SOPC_ReturnStatus status = SOPC_SKManager_GetKeys(skm, tokenId, 1, &SecurityPolicyUri, &FirstTokenId, &Keys,
111
0
                                                      &NbKeys, &TimeToNextKey, &KeyLifetime);
112
113
0
    if (SOPC_STATUS_OK != status)
114
0
    {
115
        // SKmanager fail to get keys
116
0
        secuStatus = SOPC_PUBSUB_STATUS_SECURITY_NOK;
117
0
    }
118
0
    else if (NbKeys <= 0)
119
0
    {
120
        // No keys available from SK Manager
121
0
        secuStatus = SOPC_PUBSUB_STATUS_SECURITY_NO_KEYS;
122
0
    }
123
0
    else
124
0
    {
125
        /** Check if received keys are different from the one in use */
126
0
        if (*securityGroup == NULL || (*securityGroup)->tokenId != FirstTokenId)
127
0
        {
128
            /** Fill Outputs **/
129
130
            // Initialize returned keys if GetKeys returned valid Keys corresponding to requested token
131
0
            SOPC_ByteString* byteString = NULL;
132
0
            if ((SOPC_PUBSUB_SKS_CURRENT_TOKENID == tokenId || tokenId == FirstTokenId))
133
0
            {
134
0
                byteString = &Keys[0];
135
0
                if ((32 + 32 + 4) == byteString->Length)
136
0
                {
137
0
                    returnedKeys = SOPC_Calloc(1, sizeof(SOPC_PubSubSKS_Keys));
138
0
                }
139
0
                secuStatus = NULL != returnedKeys ? SOPC_PUBSUB_STATUS_SECURITY_OK : SOPC_PUBSUB_STATUS_SECURITY_NOK;
140
0
            }
141
0
            else if (FirstTokenId < tokenId)
142
0
            {
143
0
                secuStatus = SOPC_PUBSUB_STATUS_SECURITY_SKS_KEYS_OLDER;
144
0
            }
145
0
            else // FirstTokenId > tokenId
146
0
            {
147
                // Received Keys are newer than the one required
148
0
                secuStatus = SOPC_PUBSUB_STATUS_SECURITY_SKS_KEYS_NEWER;
149
0
            }
150
151
0
            if (NULL != returnedKeys)
152
0
            {
153
0
                returnedKeys->tokenId = FirstTokenId;
154
0
                returnedKeys->signingKey = SOPC_SecretBuffer_NewFromExposedBuffer(byteString->Data, 32);
155
0
                returnedKeys->encryptKey = SOPC_SecretBuffer_NewFromExposedBuffer(&(byteString->Data[32]), 32);
156
0
                returnedKeys->keyNonce = SOPC_SecretBuffer_NewFromExposedBuffer(&(byteString->Data[64]), 4);
157
0
                if (NULL == returnedKeys->signingKey || NULL == returnedKeys->encryptKey ||
158
0
                    NULL == returnedKeys->keyNonce)
159
0
                {
160
0
                    SOPC_PubSubSKS_Keys_Delete(returnedKeys);
161
0
                    SOPC_Free(returnedKeys);
162
0
                    returnedKeys = NULL;
163
0
                    secuStatus = SOPC_PUBSUB_STATUS_SECURITY_NOK;
164
0
                }
165
0
            }
166
0
        }
167
0
    }
168
169
0
    SOPC_String_Clear(SecurityPolicyUri);
170
0
    SOPC_Free(SecurityPolicyUri);
171
0
    for (uint32_t i = 0; i < NbKeys && NULL != Keys; i++)
172
0
    {
173
0
        SOPC_ByteString_Clear(&Keys[i]);
174
0
    }
175
0
    SOPC_Free(Keys);
176
177
    /** Update the key with new one */
178
0
    if (NULL != returnedKeys)
179
0
    {
180
0
        if (*securityGroup != NULL)
181
0
        {
182
0
            SOPC_PubSubSKS_Keys_Delete(*securityGroup);
183
0
            SOPC_Free(*securityGroup);
184
0
        }
185
0
        (*securityGroup) = returnedKeys;
186
0
    }
187
0
    return secuStatus;
188
0
}
189
190
void SOPC_PubSubSKS_Keys_Delete(SOPC_PubSubSKS_Keys* keys)
191
0
{
192
0
    if (NULL != keys)
193
0
    {
194
0
        SOPC_SecretBuffer_DeleteClear(keys->signingKey);
195
0
        keys->signingKey = NULL;
196
0
        SOPC_SecretBuffer_DeleteClear(keys->encryptKey);
197
0
        keys->encryptKey = NULL;
198
0
        SOPC_SecretBuffer_DeleteClear(keys->keyNonce);
199
0
        keys->keyNonce = NULL;
200
0
    }
201
0
}
202
203
static bool mayAddSecurityGroupId(const char* securityGroupId)
204
0
{
205
0
    SOPC_ASSERT(NULL != securityGroupId);
206
0
    bool result = true;
207
0
    if (NULL == SOPC_SK_SecurityGroup_GetSkManager(securityGroupId))
208
0
    {
209
0
        SOPC_SKManager* skm = SOPC_SKManager_Create(securityGroupId, 0);
210
211
0
        if (NULL != skm)
212
0
        {
213
0
            result = SOPC_PubSubSKS_AddSkManager(skm);
214
0
        }
215
0
        else
216
0
        {
217
0
            SOPC_SKManager_Clear(skm);
218
0
            SOPC_Free(skm);
219
0
            result = false;
220
0
        }
221
0
    }
222
0
    return result;
223
0
}
224
225
bool SOPC_PubSubSKS_CreateManagersFromConfig(const SOPC_PubSubConfiguration* pubSubConfig)
226
0
{
227
0
    if (NULL == pubSubConfig)
228
0
    {
229
0
        return false;
230
0
    }
231
232
0
    bool result = true;
233
234
    // Process publisher connections
235
0
    for (uint32_t i = 0; result && i < SOPC_PubSubConfiguration_Nb_PubConnection(pubSubConfig); i++)
236
0
    {
237
0
        const SOPC_PubSubConnection* pubConnection = SOPC_PubSubConfiguration_Get_PubConnection_At(pubSubConfig, i);
238
239
        // Check writer groups
240
0
        for (uint16_t j = 0; result && j < SOPC_PubSubConnection_Nb_WriterGroup(pubConnection); j++)
241
0
        {
242
0
            const SOPC_WriterGroup* group = SOPC_PubSubConnection_Get_WriterGroup_At(pubConnection, j);
243
0
            const char* securityGroupId = SOPC_WriterGroup_Get_SecurityGroupId(group);
244
0
            const SOPC_SecurityMode_Type secuMode = SOPC_WriterGroup_Get_SecurityMode(group);
245
246
0
            if (NULL == securityGroupId && SOPC_SecurityMode_None != secuMode)
247
0
            {
248
0
                result = false;
249
0
            }
250
0
            else if (SOPC_SecurityMode_None != secuMode)
251
0
            {
252
                // Ensure the security group id is added to the SKManager
253
0
                result = mayAddSecurityGroupId(securityGroupId);
254
0
            }
255
0
        }
256
0
    }
257
258
    // Process subscriber connections
259
0
    for (uint32_t i = 0; result && i < SOPC_PubSubConfiguration_Nb_SubConnection(pubSubConfig); i++)
260
0
    {
261
0
        const SOPC_PubSubConnection* subConnection = SOPC_PubSubConfiguration_Get_SubConnection_At(pubSubConfig, i);
262
263
        // Check reader groups
264
0
        for (uint16_t j = 0; result && j < SOPC_PubSubConnection_Nb_ReaderGroup(subConnection); j++)
265
0
        {
266
0
            const SOPC_ReaderGroup* group = SOPC_PubSubConnection_Get_ReaderGroup_At(subConnection, j);
267
0
            const char* securityGroupId = SOPC_ReaderGroup_Get_SecurityGroupId(group);
268
0
            const SOPC_SecurityMode_Type secuMode = SOPC_ReaderGroup_Get_SecurityMode(group);
269
270
0
            if (NULL == securityGroupId && SOPC_SecurityMode_None != secuMode)
271
0
            {
272
0
                result = false;
273
0
            }
274
0
            else if (SOPC_SecurityMode_None != secuMode)
275
0
            {
276
                // Ensure the security group id is added to the SKManager
277
0
                result = mayAddSecurityGroupId(securityGroupId);
278
0
            }
279
0
        }
280
0
    }
281
282
0
    return result;
283
0
}
284
285
typedef struct
286
{
287
    SOPC_SKscheduler* scheduler;
288
    SOPC_SKBuilder* builder;
289
    SOPC_SKProvider* provider;
290
    uint32_t msPeriod;
291
    bool result;
292
} AddTaskContext;
293
294
static void addTask(const char* securityGroupId, SOPC_SKManager* manager, void* userData)
295
0
{
296
0
    SOPC_UNUSED_ARG(securityGroupId);
297
0
    AddTaskContext* ctx = (AddTaskContext*) userData;
298
0
    if (!ctx->result)
299
0
    {
300
0
        return; // Stop on first error
301
0
    }
302
303
0
    SOPC_ReturnStatus status =
304
0
        SOPC_SKscheduler_AddTask(ctx->scheduler, ctx->builder, ctx->provider, manager, ctx->msPeriod);
305
0
    ctx->result = (SOPC_STATUS_OK == status);
306
0
}
307
308
bool SOPC_PubSubSKS_AddTasks(SOPC_SKscheduler* scheduler,
309
                             SOPC_SKBuilder* builder,
310
                             SOPC_SKProvider* provider,
311
                             uint32_t msFirstUpdate)
312
0
{
313
0
    if (NULL == scheduler || NULL == builder || NULL == provider)
314
0
    {
315
0
        return false;
316
0
    }
317
318
0
    AddTaskContext context = {
319
0
        .scheduler = scheduler, .builder = builder, .provider = provider, .msPeriod = msFirstUpdate, .result = true};
320
321
0
    SOPC_SecurityGroup_ForEach(addTask, &context);
322
323
0
    return context.result;
324
0
}
325
326
SOPC_SKManager* SOPC_PubSubSKS_GetSkManager(const char* securityGroupId)
327
0
{
328
0
    if (NULL == securityGroupId)
329
0
    {
330
0
        return NULL;
331
0
    }
332
0
    return SOPC_SK_SecurityGroup_GetSkManager(securityGroupId);
333
0
}