117fd14ceSopenharmony_ci/* 217fd14ceSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 317fd14ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 417fd14ceSopenharmony_ci * you may not use this file except in compliance with the License. 517fd14ceSopenharmony_ci * You may obtain a copy of the License at 617fd14ceSopenharmony_ci * 717fd14ceSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 817fd14ceSopenharmony_ci * 917fd14ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1017fd14ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1117fd14ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1217fd14ceSopenharmony_ci * See the License for the specific language governing permissions and 1317fd14ceSopenharmony_ci * limitations under the License. 1417fd14ceSopenharmony_ci */ 1517fd14ceSopenharmony_ci 1617fd14ceSopenharmony_ci#include "cred_manager.h" 1717fd14ceSopenharmony_ci 1817fd14ceSopenharmony_ci#include "account_related_cred_plugin.h" 1917fd14ceSopenharmony_ci#include "device_auth_defines.h" 2017fd14ceSopenharmony_ci#include "hc_log.h" 2117fd14ceSopenharmony_ci#include "hc_vector.h" 2217fd14ceSopenharmony_ci#include "hitrace_adapter.h" 2317fd14ceSopenharmony_ci 2417fd14ceSopenharmony_ciDECLARE_HC_VECTOR(CredPluginVec, CredPlugin *); 2517fd14ceSopenharmony_ciIMPLEMENT_HC_VECTOR(CredPluginVec, CredPlugin *, 2) 2617fd14ceSopenharmony_ci 2717fd14ceSopenharmony_cistatic CredPluginVec g_credPluginVec; 2817fd14ceSopenharmony_ci 2917fd14ceSopenharmony_ciint32_t ProcCred(int32_t pluginName, int32_t osAccountId, int32_t cmdId, CJson *in, CJson *out) 3017fd14ceSopenharmony_ci{ 3117fd14ceSopenharmony_ci uint32_t index; 3217fd14ceSopenharmony_ci CredPlugin **pluginPtr; 3317fd14ceSopenharmony_ci FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) { 3417fd14ceSopenharmony_ci if ((*pluginPtr)->pluginName == pluginName) { 3517fd14ceSopenharmony_ci return (*pluginPtr)->procCred(osAccountId, cmdId, in, out); 3617fd14ceSopenharmony_ci } 3717fd14ceSopenharmony_ci } 3817fd14ceSopenharmony_ci LOGE("[CredMgr]: There is no matched cred plugin. [Name]: %d", pluginName); 3917fd14ceSopenharmony_ci return HC_ERR_NOT_SUPPORT; 4017fd14ceSopenharmony_ci} 4117fd14ceSopenharmony_ci 4217fd14ceSopenharmony_ciint32_t InitCredMgr(void) 4317fd14ceSopenharmony_ci{ 4417fd14ceSopenharmony_ci g_credPluginVec = CREATE_HC_VECTOR(CredPluginVec); 4517fd14ceSopenharmony_ci CredPlugin *plugin = GetAccountRelatedCredPlugin(); 4617fd14ceSopenharmony_ci if (plugin != NULL) { 4717fd14ceSopenharmony_ci int32_t res = plugin->init(); 4817fd14ceSopenharmony_ci if (res == HC_SUCCESS) { 4917fd14ceSopenharmony_ci (void)g_credPluginVec.pushBackT(&g_credPluginVec, plugin); 5017fd14ceSopenharmony_ci } else { 5117fd14ceSopenharmony_ci LOGW("[CredMgr]: Init account related cred plugin fail. [Res]: %d", res); 5217fd14ceSopenharmony_ci } 5317fd14ceSopenharmony_ci } 5417fd14ceSopenharmony_ci LOGI("[CredMgr]: Init success!"); 5517fd14ceSopenharmony_ci return HC_SUCCESS; 5617fd14ceSopenharmony_ci} 5717fd14ceSopenharmony_ci 5817fd14ceSopenharmony_civoid DestroyCredMgr(void) 5917fd14ceSopenharmony_ci{ 6017fd14ceSopenharmony_ci uint32_t index; 6117fd14ceSopenharmony_ci CredPlugin **pluginPtr; 6217fd14ceSopenharmony_ci FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) { 6317fd14ceSopenharmony_ci (*pluginPtr)->destroy(); 6417fd14ceSopenharmony_ci } 6517fd14ceSopenharmony_ci DESTROY_HC_VECTOR(CredPluginVec, &g_credPluginVec); 6617fd14ceSopenharmony_ci} 6717fd14ceSopenharmony_ci 6817fd14ceSopenharmony_ciint32_t AddCredPlugin(const CredPlugin *plugin) 6917fd14ceSopenharmony_ci{ 7017fd14ceSopenharmony_ci if (plugin == NULL || plugin->init == NULL || 7117fd14ceSopenharmony_ci plugin->destroy == NULL || plugin->procCred == NULL) { 7217fd14ceSopenharmony_ci LOGE("The plugin is invalid."); 7317fd14ceSopenharmony_ci return HC_ERR_INVALID_PARAMS; 7417fd14ceSopenharmony_ci } 7517fd14ceSopenharmony_ci int32_t res = plugin->init(); 7617fd14ceSopenharmony_ci if (res != HC_SUCCESS) { 7717fd14ceSopenharmony_ci LOGE("[CredMgr]: Init cred plugin fail. [Res]: %d", res); 7817fd14ceSopenharmony_ci return HC_ERR_INIT_FAILED; 7917fd14ceSopenharmony_ci } 8017fd14ceSopenharmony_ci bool isNeedReplace = false; 8117fd14ceSopenharmony_ci uint32_t index; 8217fd14ceSopenharmony_ci CredPlugin **pluginPtr; 8317fd14ceSopenharmony_ci FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) { 8417fd14ceSopenharmony_ci if ((*pluginPtr)->pluginName == plugin->pluginName) { 8517fd14ceSopenharmony_ci isNeedReplace = true; 8617fd14ceSopenharmony_ci break; 8717fd14ceSopenharmony_ci } 8817fd14ceSopenharmony_ci } 8917fd14ceSopenharmony_ci if (g_credPluginVec.pushBack(&g_credPluginVec, &plugin) == NULL) { 9017fd14ceSopenharmony_ci LOGE("[CredMgr]: Push cred plugin to vector fail."); 9117fd14ceSopenharmony_ci plugin->destroy(); 9217fd14ceSopenharmony_ci return HC_ERR_ALLOC_MEMORY; 9317fd14ceSopenharmony_ci } 9417fd14ceSopenharmony_ci if (isNeedReplace) { 9517fd14ceSopenharmony_ci LOGI("[CredMgr]: Replace cred plugin. [Name]: %d", plugin->pluginName); 9617fd14ceSopenharmony_ci HC_VECTOR_POPELEMENT(&g_credPluginVec, pluginPtr, index); 9717fd14ceSopenharmony_ci } else { 9817fd14ceSopenharmony_ci LOGI("[CredMgr]: Add new cred plugin. [Name]: %d", plugin->pluginName); 9917fd14ceSopenharmony_ci } 10017fd14ceSopenharmony_ci return HC_SUCCESS; 10117fd14ceSopenharmony_ci} 10217fd14ceSopenharmony_ci 10317fd14ceSopenharmony_civoid DelCredPlugin(int32_t pluginName) 10417fd14ceSopenharmony_ci{ 10517fd14ceSopenharmony_ci uint32_t index; 10617fd14ceSopenharmony_ci CredPlugin **pluginPtr; 10717fd14ceSopenharmony_ci FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) { 10817fd14ceSopenharmony_ci if ((*pluginPtr)->pluginName == pluginName) { 10917fd14ceSopenharmony_ci LOGI("[CredMgr]: Delete cred plugin success. [Name]: %d", pluginName); 11017fd14ceSopenharmony_ci (*pluginPtr)->destroy(); 11117fd14ceSopenharmony_ci HC_VECTOR_POPELEMENT(&g_credPluginVec, pluginPtr, index); 11217fd14ceSopenharmony_ci break; 11317fd14ceSopenharmony_ci } 11417fd14ceSopenharmony_ci } 11517fd14ceSopenharmony_ci} 116