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 "session_key_manager.h"
17 #include "asset_adapter.h"
18
19 #include "distributed_device_profile_errors.h"
20 #include "distributed_device_profile_log.h"
21
22 namespace OHOS {
23 namespace DistributedDeviceProfile {
24 IMPLEMENT_SINGLE_INSTANCE(SessionKeyManager);
25 namespace {
26 const std::string TAG = "SessionKeyManager";
27 }
28
PutSessionKey(uint32_t userId,const std::vector<uint8_t> & sessionKey,int32_t & sessionKeyId)29 int32_t SessionKeyManager::PutSessionKey(uint32_t userId,
30 const std::vector<uint8_t>& sessionKey, int32_t& sessionKeyId)
31 {
32 if (userId == 0 || sessionKey.empty() || sessionKey.size() > MAX_SESSIONKEY_SIZE) {
33 HILOGE("params is invalid");
34 return DP_INVALID_PARAMS;
35 }
36 GeneratedSessionKeyId(userId, sessionKeyId);
37 AssetValue aliasValue = { .blob = { static_cast<uint32_t>(sizeof(sessionKeyId)),
38 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(&sessionKeyId)) } };
39 AssetValue userIdValue = { .u32 = userId };
40 AssetValue secretValue = { .blob = { static_cast<uint32_t>(sessionKey.size()),
41 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(sessionKey.data())) } };
42 AssetValue accessibilityValue = { .u32 = SEC_ASSET_ACCESSIBILITY_DEVICE_FIRST_UNLOCKED };
43
44 AssetAttr attr[] = {
45 { .tag = SEC_ASSET_TAG_ALIAS, .value = aliasValue },
46 { .tag = SEC_ASSET_TAG_USER_ID, .value = userIdValue },
47 { .tag = SEC_ASSET_TAG_SECRET, .value = secretValue },
48 { .tag = SEC_ASSET_TAG_ACCESSIBILITY, .value = accessibilityValue }
49 };
50
51 int32_t ret = AssetAdapter::GetInstance().PutAsset(attr, sizeof(attr) / sizeof(attr[0]));
52 if (ret != DP_SUCCESS) {
53 HILOGE("PutSessionKey failed");
54 return ret;
55 }
56 return DP_SUCCESS;
57 }
58
GetSessionKey(uint32_t userId,int32_t sessionKeyId,std::vector<uint8_t> & sessionKey)59 int32_t SessionKeyManager::GetSessionKey(uint32_t userId,
60 int32_t sessionKeyId, std::vector<uint8_t>& sessionKey)
61 {
62 if (userId == 0) {
63 HILOGE("params is invalid");
64 return DP_INVALID_PARAMS;
65 }
66 AssetValue aliasValue = { .blob = { static_cast<uint32_t>(sizeof(sessionKeyId)),
67 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(&sessionKeyId)) } };
68 AssetValue userIdValue = { .u32 = userId };
69 AssetValue returnValue = { .u32 = SEC_ASSET_RETURN_ALL };
70 AssetAttr attr[] = {
71 { .tag = SEC_ASSET_TAG_ALIAS, .value = aliasValue },
72 { .tag = SEC_ASSET_TAG_USER_ID, .value = userIdValue },
73 { .tag = SEC_ASSET_TAG_RETURN_TYPE, .value = returnValue }
74 };
75
76 AssetResultSet resultSet = {0};
77 int32_t ret = AssetAdapter::GetInstance().GetAsset(attr, sizeof(attr) / sizeof(attr[0]), &resultSet);
78 if (ret != DP_SUCCESS) {
79 HILOGE("GetAsset failed");
80 AssetAdapter::GetInstance().FreeResultSet(&resultSet);
81 return ret;
82 }
83
84 AssetAttr* secret = AssetAdapter::GetInstance().ParseAttr(resultSet.results, SEC_ASSET_TAG_SECRET);
85 if (secret == nullptr) {
86 HILOGE("ParseAttr failed");
87 AssetAdapter::GetInstance().FreeResultSet(&resultSet);
88 return DP_GET_ASSET_ERROE;
89 }
90 uint32_t length = secret->value.blob.size;
91 uint8_t* data = secret->value.blob.data;
92 if (data == nullptr || length == 0 || length > MAX_SESSIONKEY_SIZE) {
93 HILOGE("result invalid");
94 data = nullptr;
95 AssetAdapter::GetInstance().FreeResultSet(&resultSet);
96 return DP_GET_ASSET_ERROE;
97 }
98 sessionKey = std::vector<uint8_t>(data, data + length);
99 AssetAdapter::GetInstance().FreeResultSet(&resultSet);
100 return DP_SUCCESS;
101 }
102
UpdateSessionKey(uint32_t userId,int32_t sessionKeyId,const std::vector<uint8_t> & sessionKey)103 int32_t SessionKeyManager::UpdateSessionKey(uint32_t userId,
104 int32_t sessionKeyId, const std::vector<uint8_t>& sessionKey)
105 {
106 if (userId == 0) {
107 HILOGE("params is invalid");
108 return DP_INVALID_PARAMS;
109 }
110 AssetValue aliasValue = { .blob = { static_cast<uint32_t>(sizeof(sessionKeyId)),
111 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(&sessionKeyId)) } };
112 AssetValue userIdValue = { .u32 = userId };
113 AssetValue secretValue = { .blob = { static_cast<uint32_t>(sessionKey.size()),
114 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(sessionKey.data())) } };
115
116 AssetAttr attrQuery[] = {
117 { .tag = SEC_ASSET_TAG_ALIAS, .value = aliasValue },
118 { .tag = SEC_ASSET_TAG_USER_ID, .value = userIdValue }
119 };
120 AssetAttr attrUpdate[] = {
121 { .tag = SEC_ASSET_TAG_SECRET, .value = secretValue }
122 };
123
124 int32_t ret = AssetAdapter::GetInstance().UpdateAsset(attrQuery, sizeof(attrQuery) / sizeof(attrQuery[0]),
125 attrUpdate, sizeof(attrUpdate) / sizeof(attrUpdate[0]));
126 if (ret != DP_SUCCESS) {
127 HILOGE("UpdateSessionKey failed");
128 return ret;
129 }
130 return DP_SUCCESS;
131 }
132
DeleteSessionKey(uint32_t userId,int32_t sessionKeyId)133 int32_t SessionKeyManager::DeleteSessionKey(uint32_t userId, int32_t sessionKeyId)
134 {
135 if (userId == 0) {
136 HILOGE("params is invalid");
137 return DP_INVALID_PARAMS;
138 }
139 AssetValue aliasValue = { .blob = { static_cast<uint32_t>(sizeof(sessionKeyId)),
140 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(&sessionKeyId)) } };
141 AssetValue userIdValue = { .u32 = userId };
142 AssetAttr attr[] = {
143 { .tag = SEC_ASSET_TAG_ALIAS, .value = aliasValue },
144 { .tag = SEC_ASSET_TAG_USER_ID, .value = userIdValue }
145 };
146
147 int32_t ret = AssetAdapter::GetInstance().DeleteAsset(attr, sizeof(attr) / sizeof(attr[0]));
148 if (ret != DP_SUCCESS) {
149 HILOGE("DeleteAsset failed");
150 return ret;
151 }
152 return DP_SUCCESS;
153 }
154
GeneratedSessionKeyId(uint32_t userId,int32_t & sessionKeyId)155 void SessionKeyManager::GeneratedSessionKeyId(uint32_t userId, int32_t& sessionKeyId)
156 {
157 int32_t ret = 0;
158 int32_t randomNumber = 0;
159 do {
160 int32_t seed = std::chrono::system_clock::now().time_since_epoch().count();
161 std::default_random_engine generator(seed);
162 std::uniform_int_distribution<int32_t> distribution(0, INT32_MAX);
163 randomNumber = distribution(generator);
164 std::vector<uint8_t> sessionKey;
165 ret = GetSessionKey(userId, randomNumber, sessionKey);
166 } while (ret == DP_SUCCESS);
167 sessionKeyId = randomNumber;
168 return;
169 }
170 } // namespace DistributedDeviceProfile
171 } // namespace OHOS
172