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