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