• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <filesystem>
17 
18 #include "app_clone_key_manager.h"
19 #include "crypto/key_manager.h"
20 #include "utils/storage_radar.h"
21 #include "utils/string_utils.h"
22 #include "storage_service_errno.h"
23 #include "storage_service_log.h"
24 
25 using namespace OHOS::StorageService;
26 namespace OHOS {
27 namespace StorageDaemon {
28 constexpr const char *NEED_RESTORE_PATH = "/data/service/el1/public/storage_daemon/sd/el1/%d/latest/need_restore";
29 
ActiveAppCloneUserKey(unsigned int & failedUserId)30 int AppCloneKeyManager::ActiveAppCloneUserKey(unsigned int &failedUserId)
31 {
32     for (int userId = StorageService::START_APP_CLONE_USER_ID;
33          userId < StorageService::MAX_APP_CLONE_USER_ID; userId++) {
34         std::string keyPath = StringPrintf(NEED_RESTORE_PATH, userId);
35         std::error_code errCode;
36         if (!std::filesystem::exists(keyPath, errCode)) {
37             LOGD("restore path do not exist, errCode %{public}d", errCode.value());
38             continue;
39         }
40         int ret = KeyManager::GetInstance().ActiveCeSceSeceUserKey(userId, EL2_KEY, {}, {});
41         if (ret != E_OK) {
42             failedUserId = static_cast<unsigned int>(userId);
43             LOGE("Active app clone user %{public}u el2 failed, ret=%{public}d.", userId, ret);
44             StorageRadar::ReportActiveUserKey("ActiveUserKey::ActiveAppCloneUserKey", userId, ret, "EL2");
45             return ret;
46         }
47         ret = KeyManager::GetInstance().ActiveCeSceSeceUserKey(userId, EL3_KEY, {}, {});
48         if (ret != E_OK) {
49             failedUserId = static_cast<unsigned int>(userId);
50             LOGE("Active app clone user %{public}u el3 failed, ret=%{public}d.", userId, ret);
51             StorageRadar::ReportActiveUserKey("ActiveUserKey::ActiveAppCloneUserKey", userId, ret, "EL3");
52             return ret;
53         }
54         ret = KeyManager::GetInstance().ActiveCeSceSeceUserKey(userId, EL4_KEY, {}, {});
55         if (ret != E_OK) {
56             failedUserId = static_cast<unsigned int>(userId);
57             LOGE("Active app clone user %{public}u el4 failed, ret=%{public}d.", userId, ret);
58             StorageRadar::ReportActiveUserKey("ActiveUserKey::ActiveAppCloneUserKey", userId, ret, "EL4");
59             return ret;
60         }
61         LOGI("ActiveAppCloneUserKey::userkey %{public}u activated.", userId);
62         return E_OK;
63     }
64     LOGE("ActiveAppCloneUserKey::Did not find app clone userId in valid range = {219 ~ 239}.");
65     return E_NOT_SUPPORT;
66 }
67 } // namespace StorageDaemon
68 } // namespace OHOS