• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "identity_service.h"
17 
18 #include "common_defs.h"
19 #include "credential_data_manager.h"
20 #include "hc_log.h"
21 #include "identity_service_impl.h"
22 #include "os_account_adapter.h"
23 
AddCredential(int32_t osAccountId,const char * requestParams,char ** returnData)24 int32_t AddCredential(int32_t osAccountId, const char *requestParams, char **returnData)
25 {
26     SET_LOG_MODE(TRACE_MODE);
27 
28     if (requestParams == NULL || returnData == NULL) {
29         LOGE("Failed to add credential, NULL params!");
30         return IS_ERR_INVALID_PARAMS;
31     }
32 
33     if (!IsOsAccountUnlocked(osAccountId)) {
34         LOGE("Os account is not unlocked!");
35         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
36     }
37 
38     return AddCredentialImpl(osAccountId, requestParams, returnData);
39 }
40 
ExportCredential(int32_t osAccountId,const char * credId,char ** returnData)41 int32_t ExportCredential(int32_t osAccountId, const char *credId, char **returnData)
42 {
43     SET_LOG_MODE(TRACE_MODE);
44 
45     if (credId == NULL || returnData == NULL) {
46         LOGE("Failed to export credential, NULL params!");
47         return IS_ERR_INVALID_PARAMS;
48     }
49 
50     if (!IsOsAccountUnlocked(osAccountId)) {
51         LOGE("Os account is not unlocked!");
52         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
53     }
54 
55     return ExportCredentialImpl(osAccountId, credId, returnData);
56 }
57 
QueryCredentialByParams(int32_t osAccountId,const char * requestParams,char ** returnData)58 int32_t QueryCredentialByParams(int32_t osAccountId, const char *requestParams, char **returnData)
59 {
60     SET_LOG_MODE(TRACE_MODE);
61 
62     if (requestParams == NULL || returnData == NULL) {
63         LOGE("Failed to query credential by params, NULL params!");
64         return IS_ERR_INVALID_PARAMS;
65     }
66 
67     if (!IsOsAccountUnlocked(osAccountId)) {
68         LOGE("Os account is not unlocked!");
69         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
70     }
71 
72     return QueryCredentialByParamsImpl(osAccountId, requestParams, returnData);
73 }
74 
QueryCredInfoByCredId(int32_t osAccountId,const char * credId,char ** returnData)75 int32_t QueryCredInfoByCredId(int32_t osAccountId, const char *credId, char **returnData)
76 {
77     SET_LOG_MODE(TRACE_MODE);
78 
79     if (credId == NULL || returnData == NULL) {
80         LOGE("Failed to query credential info by credId, NULL params!");
81         return IS_ERR_INVALID_PARAMS;
82     }
83 
84     if (!IsOsAccountUnlocked(osAccountId)) {
85         LOGE("Os account is not unlocked!");
86         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
87     }
88 
89     return QueryCredInfoByCredIdImpl(osAccountId, credId, returnData);
90 }
91 
DeleteCredential(int32_t osAccountId,const char * credId)92 int32_t DeleteCredential(int32_t osAccountId, const char *credId)
93 {
94     SET_LOG_MODE(TRACE_MODE);
95 
96     if (credId == NULL) {
97         LOGE("Failed to delete credential, NULL credId!");
98         return IS_ERR_INVALID_PARAMS;
99     }
100 
101     if (!IsOsAccountUnlocked(osAccountId)) {
102         LOGE("Os account is not unlocked!");
103         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
104     }
105 
106     return DeleteCredentialImpl(osAccountId, credId);
107 }
108 
DeleteCredByParams(int32_t osAccountId,const char * requestParams,char ** returnData)109 int32_t DeleteCredByParams(int32_t osAccountId, const char *requestParams, char **returnData)
110 {
111     SET_LOG_MODE(TRACE_MODE);
112 
113     if (requestParams == NULL || returnData == NULL) {
114         LOGE("Failed to batch delete credential, NULL params!");
115         return IS_ERR_INVALID_PARAMS;
116     }
117 
118     if (!IsOsAccountUnlocked(osAccountId)) {
119         LOGE("Os account is not unlocked!");
120         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
121     }
122     return DeleteCredByParamsImpl(osAccountId, requestParams, returnData);
123 }
124 
UpdateCredInfo(int32_t osAccountId,const char * credId,const char * requestParams)125 int32_t UpdateCredInfo(int32_t osAccountId, const char *credId, const char *requestParams)
126 {
127     SET_LOG_MODE(TRACE_MODE);
128 
129     if (credId == NULL || requestParams == NULL) {
130         LOGE("Failed to update credential, NULL params!");
131         return IS_ERR_INVALID_PARAMS;
132     }
133 
134     if (!IsOsAccountUnlocked(osAccountId)) {
135         LOGE("Os account is not unlocked!");
136         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
137     }
138 
139     return UpdateCredInfoImpl(osAccountId, credId, requestParams);
140 }
141 
BatchUpdateCredentials(int32_t osAccountId,const char * requestParams,char ** returnData)142 int32_t BatchUpdateCredentials(int32_t osAccountId, const char *requestParams, char **returnData)
143 {
144     SET_LOG_MODE(TRACE_MODE);
145 
146     if (requestParams == NULL || returnData == NULL) {
147         LOGE("Failed to batch update credential, NULL params!");
148         return IS_ERR_INVALID_PARAMS;
149     }
150 
151     if (!IsOsAccountUnlocked(osAccountId)) {
152         LOGE("Os account is not unlocked!");
153         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
154     }
155     return BatchUpdateCredsImpl(osAccountId, requestParams, returnData);
156 }
157 
AgreeCredential(int32_t osAccountId,const char * selfCredId,const char * requestParams,char ** returnData)158 int32_t AgreeCredential(int32_t osAccountId, const char *selfCredId, const char *requestParams, char **returnData)
159 {
160     SET_LOG_MODE(TRACE_MODE);
161 
162     if (selfCredId == NULL || requestParams == NULL || returnData == NULL) {
163         LOGE("Failed to agree credential, NULL params!");
164         return IS_ERR_INVALID_PARAMS;
165     }
166 
167     if (!IsOsAccountUnlocked(osAccountId)) {
168         LOGE("Os account is not unlocked!");
169         return IS_ERR_OS_ACCOUNT_NOT_UNLOCKED;
170     }
171 
172     return AgreeCredentialImpl(osAccountId, selfCredId, requestParams, returnData);
173 }
174 
RegisterChangeListener(const char * appId,CredChangeListener * listener)175 int32_t RegisterChangeListener(const char *appId, CredChangeListener *listener)
176 {
177     SET_LOG_MODE(TRACE_MODE);
178 
179     return IsCredListenerSupported() ? RegCredListener(appId, listener) : HC_ERR_NOT_SUPPORT;
180 }
181 
UnregisterChangeListener(const char * appId)182 int32_t UnregisterChangeListener(const char *appId)
183 {
184     SET_LOG_MODE(TRACE_MODE);
185 
186     return IsCredListenerSupported() ? UnRegCredListener(appId) : HC_ERR_NOT_SUPPORT;
187 }
188 
IsJsonString(const char * str)189 static bool IsJsonString(const char *str)
190 {
191     CJson *json = CreateJsonFromString(str);
192     if (json == NULL) {
193         return false;
194     }
195     FreeJson(json);
196     return true;
197 }
198 
DestroyInfo(char ** returnData)199 void DestroyInfo(char **returnData)
200 {
201     if (returnData == NULL || *returnData == NULL) {
202         return;
203     }
204     if (IsJsonString(*returnData)) {
205         FreeJsonString(*returnData);
206     } else {
207         HcFree(*returnData);
208     }
209     *returnData = NULL;
210 }
211 
InitIdentityService(void)212 int32_t InitIdentityService(void)
213 {
214     if (InitCredDatabase() != HC_SUCCESS) {
215         return IS_ERR_SERVICE_NEED_RESTART;
216     }
217     return IsCredListenerSupported() ? InitCredListener() : HC_SUCCESS;
218 }
219 
DestroyIdentityService(void)220 void DestroyIdentityService(void)
221 {
222     if (IsCredListenerSupported()) {
223         DestroyCredListener();
224     }
225     DestroyCredDatabase();
226 }