Coverage Report

Created: 2026-10-28 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/S2OPC/src/ClientServer/configuration/sopc_user_manager.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_macros.h"
24
#include "sopc_mem_alloc.h"
25
#include "sopc_toolkit_async_api.h"
26
#include "sopc_types.h"
27
#include "sopc_user_manager_internal.h"
28
29
SOPC_ReturnStatus SOPC_UserAuthentication_IsValidUserIdentity(SOPC_UserAuthentication_Manager* authenticationManager,
30
                                                              const SOPC_ExtensionObject* pUser,
31
                                                              SOPC_UserAuthentication_Status* pUserAuthenticated)
32
0
{
33
0
    SOPC_ASSERT(NULL != authenticationManager);
34
0
    SOPC_ASSERT(NULL != pUser);
35
0
    SOPC_ASSERT(NULL != pUserAuthenticated);
36
0
    SOPC_ASSERT(NULL != authenticationManager->pFunctions);
37
0
    SOPC_ASSERT(NULL != authenticationManager->pFunctions->pFuncValidateUserIdentity);
38
39
    /* UACTT tests expect that a UserName identity token with empty username is invalid. */
40
0
    if (&OpcUa_UserNameIdentityToken_EncodeableType == pUser->Body.Object.ObjType)
41
0
    {
42
0
        OpcUa_UserNameIdentityToken* token = pUser->Body.Object.Value;
43
0
        if (0 >= token->UserName.Length)
44
0
        {
45
0
            *pUserAuthenticated = SOPC_USER_AUTHENTICATION_INVALID_TOKEN;
46
0
            return SOPC_STATUS_OK;
47
0
        }
48
0
    }
49
50
0
    return (authenticationManager->pFunctions->pFuncValidateUserIdentity)(authenticationManager, pUser,
51
0
                                                                          pUserAuthenticated);
52
0
}
53
54
void SOPC_UserAuthentication_PKIProviderUpdateCb(uintptr_t updateParam)
55
0
{
56
0
    SOPC_UNUSED_ARG(updateParam);
57
0
    SOPC_ToolkitServer_AsyncReEvalUserCertSessions();
58
0
}
59
60
SOPC_ReturnStatus SOPC_UserAuthorization_IsAuthorizedOperation(const SOPC_CallContext* callContextPtr,
61
                                                               SOPC_UserWithAuthorization* userWithAuthorization,
62
                                                               SOPC_UserAuthorization_OperationType operationType,
63
                                                               const SOPC_NodeId* nodeId,
64
                                                               uint32_t attributeId,
65
                                                               bool* pbOperationAuthorized)
66
0
{
67
0
    SOPC_ASSERT(NULL != userWithAuthorization);
68
0
    SOPC_ASSERT(NULL != nodeId);
69
0
    SOPC_ASSERT(NULL != pbOperationAuthorized);
70
0
    SOPC_User* user = userWithAuthorization->user;
71
0
    SOPC_UserAuthorization_Manager* authorizationManager = userWithAuthorization->authorizationManager;
72
0
    SOPC_ASSERT(NULL != user);
73
0
    SOPC_ASSERT(NULL != authorizationManager);
74
0
    SOPC_ASSERT(NULL != authorizationManager->pFunctions);
75
0
    SOPC_ASSERT(NULL != authorizationManager->pFunctions->pFuncAuthorizeOperation);
76
77
0
    return (authorizationManager->pFunctions->pFuncAuthorizeOperation)(
78
0
        callContextPtr, authorizationManager, operationType, nodeId, attributeId, user, pbOperationAuthorized);
79
0
}
80
81
void SOPC_UserAuthentication_FreeManager(SOPC_UserAuthentication_Manager** ppAuthenticationManager)
82
0
{
83
0
    if (NULL == ppAuthenticationManager || NULL == *ppAuthenticationManager)
84
0
    {
85
0
        return;
86
0
    }
87
88
0
    SOPC_UserAuthentication_Manager* authenticationManager = *ppAuthenticationManager;
89
0
    SOPC_ASSERT(NULL != authenticationManager->pFunctions);
90
0
    SOPC_ASSERT(NULL != authenticationManager->pFunctions->pFuncFree);
91
0
    authenticationManager->pFunctions->pFuncFree(authenticationManager);
92
0
    *ppAuthenticationManager = NULL;
93
0
}
94
95
void SOPC_UserAuthorization_FreeManager(SOPC_UserAuthorization_Manager** ppAuthorizationManager)
96
0
{
97
0
    if (NULL == ppAuthorizationManager || NULL == *ppAuthorizationManager)
98
0
    {
99
0
        return;
100
0
    }
101
102
0
    SOPC_UserAuthorization_Manager* authorizationManager = *ppAuthorizationManager;
103
0
    SOPC_ASSERT(NULL != authorizationManager->pFunctions);
104
0
    SOPC_ASSERT(NULL != authorizationManager->pFunctions->pFuncFree);
105
0
    authorizationManager->pFunctions->pFuncFree(authorizationManager);
106
0
    *ppAuthorizationManager = NULL;
107
0
}
108
109
/** \brief A helper implementation of the validate UserIdentity callback, which always returns OK. */
110
static SOPC_ReturnStatus AuthenticateAllowAll(SOPC_UserAuthentication_Manager* authenticationManager,
111
                                              const SOPC_ExtensionObject* pUserIdentity,
112
                                              SOPC_UserAuthentication_Status* pUserAuthenticated)
113
0
{
114
0
    SOPC_UNUSED_ARG(authenticationManager);
115
0
    SOPC_UNUSED_ARG(pUserIdentity);
116
0
    SOPC_ASSERT(NULL != pUserAuthenticated);
117
118
0
    *pUserAuthenticated = SOPC_USER_AUTHENTICATION_OK;
119
0
    return SOPC_STATUS_OK;
120
0
}
121
122
/** \brief A helper implementation of the authorize R/W operation callback, which always returns OK. */
123
static SOPC_ReturnStatus AuthorizeAllowAll(const SOPC_CallContext* callContextPtr,
124
                                           SOPC_UserAuthorization_Manager* authorizationManager,
125
                                           SOPC_UserAuthorization_OperationType operationType,
126
                                           const SOPC_NodeId* nodeId,
127
                                           uint32_t attributeId,
128
                                           const SOPC_User* pUser,
129
                                           bool* pbOperationAuthorized)
130
0
{
131
0
    SOPC_UNUSED_ARG(callContextPtr);
132
0
    SOPC_UNUSED_ARG(authorizationManager);
133
0
    SOPC_UNUSED_ARG(operationType);
134
0
    SOPC_UNUSED_ARG(nodeId);
135
0
    SOPC_UNUSED_ARG(attributeId);
136
0
    SOPC_UNUSED_ARG(pUser);
137
0
    SOPC_ASSERT(NULL != pbOperationAuthorized);
138
139
0
    *pbOperationAuthorized = true;
140
0
    return SOPC_STATUS_OK;
141
0
}
142
143
static const SOPC_UserAuthentication_Functions AlwaysAuthenticateFunctions = {
144
    .pFuncFree = (SOPC_UserAuthentication_Free_Func*) &SOPC_Free,
145
    .pFuncValidateUserIdentity = AuthenticateAllowAll};
146
147
static const SOPC_UserAuthorization_Functions AuthorizeAllowAllFunctions = {
148
    .pFuncFree = (SOPC_UserAuthorization_Free_Func*) &SOPC_Free,
149
    .pFuncAuthorizeOperation = AuthorizeAllowAll};
150
151
SOPC_UserAuthentication_Manager* SOPC_UserAuthentication_CreateManager_AllowAll(void)
152
0
{
153
0
    SOPC_UserAuthentication_Manager* authenticationManager = SOPC_Calloc(1, sizeof(SOPC_UserAuthentication_Manager));
154
155
0
    if (NULL == authenticationManager)
156
0
    {
157
0
        return NULL;
158
0
    }
159
160
0
    authenticationManager->pFunctions = &AlwaysAuthenticateFunctions;
161
0
    return authenticationManager;
162
0
}
163
164
SOPC_UserAuthorization_Manager* SOPC_UserAuthorization_CreateManager_AllowAll(void)
165
0
{
166
0
    SOPC_UserAuthorization_Manager* authorizationManager = SOPC_Calloc(1, sizeof(SOPC_UserAuthorization_Manager));
167
168
0
    if (NULL == authorizationManager)
169
0
    {
170
0
        return NULL;
171
0
    }
172
173
0
    authorizationManager->pFunctions = &AuthorizeAllowAllFunctions;
174
0
    return authorizationManager;
175
0
}
176
177
SOPC_UserWithAuthorization* SOPC_UserWithAuthorization_CreateFromIdentityToken(
178
    const SOPC_ExtensionObject* pUserIdentity,
179
    SOPC_UserAuthorization_Manager* authorizationManager)
180
0
{
181
0
    SOPC_ASSERT(NULL != pUserIdentity);
182
0
    SOPC_ASSERT(SOPC_ExtObjBodyEncoding_Object == pUserIdentity->Encoding);
183
0
    SOPC_ASSERT(&OpcUa_AnonymousIdentityToken_EncodeableType == pUserIdentity->Body.Object.ObjType ||
184
0
                &OpcUa_UserNameIdentityToken_EncodeableType == pUserIdentity->Body.Object.ObjType ||
185
0
                &OpcUa_X509IdentityToken_EncodeableType == pUserIdentity->Body.Object.ObjType);
186
187
0
    SOPC_UserWithAuthorization* userauthz = SOPC_Calloc(1, sizeof(SOPC_UserWithAuthorization));
188
0
    if (NULL == userauthz)
189
0
    {
190
0
        return NULL;
191
0
    }
192
193
0
    userauthz->authorizationManager = authorizationManager;
194
0
    if (&OpcUa_AnonymousIdentityToken_EncodeableType == pUserIdentity->Body.Object.ObjType)
195
0
    {
196
0
        SOPC_GCC_DIAGNOSTIC_IGNORE_CAST_CONST
197
0
        userauthz->user = (SOPC_User*) SOPC_User_GetAnonymous();
198
0
        SOPC_GCC_DIAGNOSTIC_RESTORE
199
0
    }
200
0
    else if (&OpcUa_UserNameIdentityToken_EncodeableType == pUserIdentity->Body.Object.ObjType)
201
0
    {
202
0
        OpcUa_UserNameIdentityToken* user_name_tok = pUserIdentity->Body.Object.Value;
203
0
        userauthz->user = SOPC_User_CreateUsername(&user_name_tok->UserName);
204
0
    }
205
0
    else if (&OpcUa_X509IdentityToken_EncodeableType == pUserIdentity->Body.Object.ObjType)
206
0
    {
207
0
        OpcUa_X509IdentityToken* x509_tok = pUserIdentity->Body.Object.Value;
208
0
        userauthz->user = SOPC_User_CreateCertificate(&x509_tok->CertificateData);
209
0
    }
210
211
0
    if (NULL == userauthz->user)
212
0
    {
213
0
        SOPC_Free(userauthz);
214
0
        userauthz = NULL;
215
0
    }
216
217
0
    return userauthz;
218
0
}
219
220
SOPC_UserWithAuthorization* SOPC_UserWithAuthorization_CreateLocal(SOPC_UserAuthorization_Manager* authorizationManager)
221
0
{
222
0
    SOPC_UserWithAuthorization* userauthz = SOPC_Calloc(1, sizeof(SOPC_UserWithAuthorization));
223
0
    if (NULL == userauthz)
224
0
    {
225
0
        return NULL;
226
0
    }
227
228
0
    userauthz->authorizationManager = authorizationManager;
229
0
    SOPC_GCC_DIAGNOSTIC_IGNORE_CAST_CONST
230
0
    userauthz->user = (SOPC_User*) SOPC_User_GetLocal();
231
0
    SOPC_GCC_DIAGNOSTIC_RESTORE
232
233
0
    if (NULL == userauthz->user)
234
0
    {
235
0
        SOPC_Free(userauthz);
236
0
        userauthz = NULL;
237
0
    }
238
239
0
    return userauthz;
240
0
}
241
242
SOPC_UserAuthorization_Manager* SOPC_UserWithAuthorization_GetManager(SOPC_UserWithAuthorization* userWithAuthorization)
243
0
{
244
0
    SOPC_ASSERT(NULL != userWithAuthorization);
245
0
    return userWithAuthorization->authorizationManager;
246
0
}
247
248
const SOPC_User* SOPC_UserWithAuthorization_GetUser(SOPC_UserWithAuthorization* userWithAuthorization)
249
0
{
250
0
    SOPC_ASSERT(NULL != userWithAuthorization);
251
0
    return userWithAuthorization->user;
252
0
}
253
254
void SOPC_UserWithAuthorization_Free(SOPC_UserWithAuthorization** ppUserWithAuthorization)
255
0
{
256
0
    if (NULL == ppUserWithAuthorization || NULL == *ppUserWithAuthorization)
257
0
    {
258
0
        return;
259
0
    }
260
261
0
    SOPC_UserWithAuthorization* userauthz = *ppUserWithAuthorization;
262
0
    SOPC_User_Free(&userauthz->user);
263
0
    SOPC_Free(userauthz);
264
    *ppUserWithAuthorization = NULL;
265
0
}