• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "key_agree_session_manager.h"
17 
18 #include "hc_log.h"
19 #include "hc_mutex.h"
20 #include "hc_vector.h"
21 #include "key_agree_sdk.h"
22 #include "key_agree_session.h"
23 
24 typedef struct {
25     KeyAgreeSession *session;
26     SpekeSession *spekeSession;
27 } SessionNode;
28 
29 DECLARE_HC_VECTOR(SessionNodeVec, SessionNode);
30 IMPLEMENT_HC_VECTOR(SessionNodeVec, SessionNode, 1);
31 
32 static SessionNodeVec g_SessionNodeVec;
33 static bool g_IsinitSessionNodeVec = false;
34 static HcMutex *g_sessionMutex = NULL;
35 
CreateKeyAgreeSessionMgr(void)36 static int32_t CreateKeyAgreeSessionMgr(void)
37 {
38     if (g_sessionMutex == NULL) {
39         g_sessionMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0);
40         if (g_sessionMutex == NULL) {
41             LOGE("Alloc sessionMutex failed");
42             return HC_ERR_ALLOC_MEMORY;
43         }
44         if (InitHcMutex(g_sessionMutex, false) != HC_SUCCESS) {
45             LOGE("Init mutex failed");
46             HcFree(g_sessionMutex);
47             g_sessionMutex = NULL;
48             return HC_ERROR;
49         }
50     }
51     (void)LockHcMutex(g_sessionMutex);
52     if (!g_IsinitSessionNodeVec) {
53         g_IsinitSessionNodeVec = true;
54         g_SessionNodeVec = CREATE_HC_VECTOR(SessionNodeVec);
55     }
56     UnlockHcMutex(g_sessionMutex);
57     return HC_SUCCESS;
58 }
59 
DestroyKeyAgreeSessionMgr(void)60 static void DestroyKeyAgreeSessionMgr(void)
61 {
62     if (g_SessionNodeVec.size(&g_SessionNodeVec) == 0) {
63         LOGI("Session vec is empty, destroy vec");
64         DESTROY_HC_VECTOR(SessionNodeVec, &g_SessionNodeVec);
65         g_IsinitSessionNodeVec = false;
66         if (g_sessionMutex != NULL) {
67             DestroyHcMutex(g_sessionMutex);
68             HcFree(g_sessionMutex);
69             g_sessionMutex = NULL;
70         }
71         return;
72     }
73     LOGI("Session vec is not empty, do nothing");
74 }
75 
AddSessionInner(KeyAgreeSession * session,SpekeSession * spekeSession)76 static uint32_t AddSessionInner(KeyAgreeSession *session, SpekeSession *spekeSession)
77 {
78     if (session == NULL) {
79         LOGE("Can not get session or sessionId!");
80         return HC_ERR_INVALID_PARAMS;
81     }
82     if (g_IsinitSessionNodeVec == false) {
83         LOGI("Manager is not create, start create session manager!");
84         if (CreateKeyAgreeSessionMgr() != HC_SUCCESS) {
85             LOGE("Create Session Mgr fail!");
86             return HC_ERROR;
87         }
88     }
89     SessionNode sessionNode;
90     sessionNode.session = session;
91     sessionNode.spekeSession = spekeSession;
92     if (g_SessionNodeVec.pushBack(&g_SessionNodeVec, &sessionNode) == NULL) {
93         LOGE("Failed to push session node!");
94         DestroyKeyAgreeSessionMgr();
95         return HC_ERR_ALLOC_MEMORY;
96     }
97     return HC_SUCCESS;
98 }
99 
DeleteSessionInner(KeyAgreeSession * session)100 static uint32_t DeleteSessionInner(KeyAgreeSession *session)
101 {
102     if (session == NULL) {
103         LOGE("Can not get session or sessionId");
104         return HC_ERR_INVALID_PARAMS;
105     }
106     if (g_IsinitSessionNodeVec == false) {
107         LOGE("Manager is not create!");
108         return HC_ERROR;
109     }
110     uint32_t index;
111     SessionNode *sessionNode = NULL;
112     FOR_EACH_HC_VECTOR(g_SessionNodeVec, index, sessionNode) {
113         if ((sessionNode != NULL) && (sessionNode->session != NULL) &&
114             (sessionNode->session->sessionId == session->sessionId)) {
115             SessionNode tempSessionNode;
116             HC_VECTOR_POPELEMENT(&g_SessionNodeVec, &tempSessionNode, index);
117             return HC_SUCCESS;
118         }
119     }
120     DestroyKeyAgreeSessionMgr();
121     return HC_ERROR;
122 }
123 
GetSessionInner(KeyAgreeSession * session)124 static SpekeSession *GetSessionInner(KeyAgreeSession *session)
125 {
126     if (session == NULL) {
127         LOGE("Can not get session or sessionId");
128         return NULL;
129     }
130     if (g_IsinitSessionNodeVec == false) {
131         LOGE("Manager is not create!");
132         return NULL;
133     }
134     uint32_t index;
135     SessionNode *sessionNode = NULL;
136     FOR_EACH_HC_VECTOR(g_SessionNodeVec, index, sessionNode) {
137         if ((sessionNode != NULL) && (sessionNode->session != NULL) &&
138             (sessionNode->session->sessionId == session->sessionId)) {
139             return sessionNode->spekeSession;
140         }
141     }
142     return NULL;
143 }
144 
145 static KeyAgreeSessionManager g_sessionManager = {
146     .addSession = AddSessionInner,
147     .deleteSession = DeleteSessionInner,
148     .getSession = GetSessionInner,
149 };
150 
GetManagerInstance(void)151 KeyAgreeSessionManager *GetManagerInstance(void)
152 {
153     return &g_sessionManager;
154 }