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) != HC_SUCCESS) {
45 LOGE("Init mutex failed");
46 HcFree(g_sessionMutex);
47 g_sessionMutex = NULL;
48 return HC_ERROR;
49 }
50 }
51 g_sessionMutex->lock(g_sessionMutex);
52 if (!g_IsinitSessionNodeVec) {
53 g_IsinitSessionNodeVec = true;
54 g_SessionNodeVec = CREATE_HC_VECTOR(SessionNodeVec);
55 }
56 g_sessionMutex->unlock(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 g_SessionNodeVec.pushBack(&g_SessionNodeVec, &sessionNode);
93 return HC_SUCCESS;
94 }
95
DeleteSessionInner(KeyAgreeSession * session)96 static uint32_t DeleteSessionInner(KeyAgreeSession *session)
97 {
98 if (session == NULL) {
99 LOGE("Can not get session or sessionId");
100 return HC_ERR_INVALID_PARAMS;
101 }
102 if (g_IsinitSessionNodeVec == false) {
103 LOGE("Manager is not create!");
104 return HC_ERROR;
105 }
106 uint32_t index;
107 SessionNode *sessionNode = NULL;
108 FOR_EACH_HC_VECTOR(g_SessionNodeVec, index, sessionNode) {
109 if ((sessionNode != NULL) && (sessionNode->session != NULL) &&
110 (sessionNode->session->sessionId == session->sessionId)) {
111 SessionNode tempSessionNode;
112 HC_VECTOR_POPELEMENT(&g_SessionNodeVec, &tempSessionNode, index);
113 return HC_SUCCESS;
114 }
115 }
116 DestroyKeyAgreeSessionMgr();
117 return HC_ERROR;
118 }
119
GetSessionInner(KeyAgreeSession * session)120 static SpekeSession *GetSessionInner(KeyAgreeSession *session)
121 {
122 if (session == NULL) {
123 LOGE("Can not get session or sessionId");
124 return NULL;
125 }
126 if (g_IsinitSessionNodeVec == false) {
127 LOGE("Manager is not create!");
128 return NULL;
129 }
130 uint32_t index;
131 SessionNode *sessionNode = NULL;
132 FOR_EACH_HC_VECTOR(g_SessionNodeVec, index, sessionNode) {
133 if ((sessionNode != NULL) && (sessionNode->session != NULL) &&
134 (sessionNode->session->sessionId == session->sessionId)) {
135 return sessionNode->spekeSession;
136 }
137 }
138 return NULL;
139 }
140
141 static KeyAgreeSessionManager g_sessionManager = {
142 .addSession = AddSessionInner,
143 .deleteSession = DeleteSessionInner,
144 .getSession = GetSessionInner,
145 };
146
GetManagerInstance(void)147 KeyAgreeSessionManager *GetManagerInstance(void)
148 {
149 return &g_sessionManager;
150 }