1 /*
2  * Copyright (C) 2021 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 "das_lite_token_manager.h"
17 #include "alg_loader.h"
18 #include "das_task_common.h"
19 #include "hc_log.h"
20 #include "iso_base_cur_task.h"
21 
UnregisterLocalIdentityLite(const TokenManagerParams * params)22 static int32_t UnregisterLocalIdentityLite(const TokenManagerParams *params)
23 {
24     Uint8Buff pkgNameBuff = { params->pkgName.val, params->pkgName.length };
25     Uint8Buff serviceTypeBuff = { params->serviceType.val, params->serviceType.length };
26 
27     uint8_t isoKeyAliasVal[ISO_KEY_ALIAS_LEN] = { 0 };
28     Uint8Buff isoKeyAliasBuff = { isoKeyAliasVal, ISO_KEY_ALIAS_LEN };
29     Uint8Buff authIdBuff = { params->authId.val, params->authId.length };
30     int32_t res = GenerateKeyAlias(&pkgNameBuff, &serviceTypeBuff, KEY_ALIAS_AUTH_TOKEN, &authIdBuff,
31         &isoKeyAliasBuff);
32     if (res != HC_SUCCESS) {
33         LOGE("Failed to generate authtoken alias!");
34         return res;
35     }
36     LOGI("AuthCode alias(HEX): %x%x%x%x****.", isoKeyAliasVal[DEV_AUTH_ZERO], isoKeyAliasVal[DEV_AUTH_ONE],
37         isoKeyAliasVal[DEV_AUTH_TWO], isoKeyAliasVal[DEV_AUTH_THREE]);
38 
39     const AlgLoader *loader = GetLoaderInstance();
40     res = loader->deleteKey(&isoKeyAliasBuff, false, params->osAccountId);
41     if (res != HC_SUCCESS) {
42         LOGE("Failed to delete authtoken!");
43         return res;
44     }
45     LOGI("AuthCode deleted successfully!");
46 
47     return HC_SUCCESS;
48 }
49 
DeletePeerAuthInfoLite(const TokenManagerParams * params)50 static int32_t DeletePeerAuthInfoLite(const TokenManagerParams *params)
51 {
52     Uint8Buff pkgNameBuff = { params->pkgName.val, params->pkgName.length };
53     Uint8Buff serviceTypeBuff = { params->serviceType.val, params->serviceType.length };
54 
55     uint8_t isoKeyAliasVal[ISO_KEY_ALIAS_LEN] = { 0 };
56     Uint8Buff isoKeyAliasBuff = { isoKeyAliasVal, ISO_KEY_ALIAS_LEN };
57     Uint8Buff authIdBuff = { params->authId.val, params->authId.length };
58     int32_t res = GenerateKeyAlias(&pkgNameBuff, &serviceTypeBuff, KEY_ALIAS_AUTH_TOKEN, &authIdBuff,
59         &isoKeyAliasBuff);
60     if (res != HC_SUCCESS) {
61         LOGE("Failed to generate authtoken alias!");
62         return res;
63     }
64     LOGI("AuthCode alias(HEX): %x%x%x%x****.", isoKeyAliasVal[DEV_AUTH_ZERO], isoKeyAliasVal[DEV_AUTH_ONE],
65         isoKeyAliasVal[DEV_AUTH_TWO], isoKeyAliasVal[DEV_AUTH_THREE]);
66 
67     const AlgLoader *loader = GetLoaderInstance();
68     res = loader->deleteKey(&isoKeyAliasBuff, false, params->osAccountId);
69     if (res != HC_SUCCESS) {
70         LOGE("Failed to delete authtoken!");
71         return res;
72     }
73     LOGI("AuthCode deleted successfully!");
74 
75     // try to delete upgrade auth token if exist.
76     uint8_t isoUpgradeKeyAliasVal[ISO_UPGRADE_KEY_ALIAS_LEN] = { 0 };
77     Uint8Buff isoUpgradeKeyAliasBuff = { isoUpgradeKeyAliasVal, ISO_UPGRADE_KEY_ALIAS_LEN };
78     res = GenerateKeyAlias(&pkgNameBuff, &serviceTypeBuff, params->userType, &authIdBuff, &isoUpgradeKeyAliasBuff);
79     if (res != HC_SUCCESS) {
80         LOGE("Failed to generate upgrade auth token alias!");
81         return res;
82     }
83     res = ToLowerCase(&isoUpgradeKeyAliasBuff);
84     if (res != HC_SUCCESS) {
85         LOGE("Failed to convert peer key alias to lower case!");
86         return res;
87     }
88     LOGI("Upgrade auth code alias(HEX): %x%x%x%x****.", isoUpgradeKeyAliasVal[DEV_AUTH_ZERO],
89         isoUpgradeKeyAliasVal[DEV_AUTH_ONE], isoUpgradeKeyAliasVal[DEV_AUTH_TWO],
90         isoUpgradeKeyAliasVal[DEV_AUTH_THREE]);
91     res = loader->deleteKey(&isoUpgradeKeyAliasBuff, true, params->osAccountId);
92     if (res != HC_SUCCESS) {
93         LOGE("Failed to delete upgrade auth token!");
94         return res;
95     }
96     LOGI("Upgrade auth code deleted successfully!");
97 
98     return HC_SUCCESS;
99 }
100 
101 TokenManager g_symTokenManagerInstance = {
102     .registerLocalIdentity = NULL,
103     .unregisterLocalIdentity = UnregisterLocalIdentityLite,
104     .deletePeerAuthInfo = DeletePeerAuthInfoLite,
105     .computeAndSavePsk = NULL,
106     .getPublicKey = NULL,
107 };
108 
GetLiteTokenManagerInstance(void)109 const TokenManager *GetLiteTokenManagerInstance(void)
110 {
111     return &g_symTokenManagerInstance;
112 }