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 "key_crypto_utils.h"
17
18 #include "os_account_manager.h"
19 #ifdef ENABLE_SCREENLOCK_MANAGER
20 #include "screenlock_manager.h"
21 #endif
22 #include "storage_service_log.h"
23 #include "utils/storage_radar.h"
24
25 using namespace OHOS::StorageService;
26 namespace OHOS {
27 namespace StorageService {
ForceLockUserScreen()28 void KeyCryptoUtils::ForceLockUserScreen()
29 {
30 LOGI("KeyCryptoUtils::ForceLockUserScreen");
31 #ifdef ENABLE_SCREENLOCK_MANAGER
32 std::vector<int32_t> ids;
33 int32_t ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
34 if (ret != ERR_OK || ids.empty()) {
35 LOGE("Query active userid failed, ret = %{public}u", ret);
36 StorageRadar::ReportOsAccountResult("ForceLockUserScreen::QueryActiveOsAccountIds", ret, DEFAULT_USERID);
37 return;
38 }
39 int reasonFlag = static_cast<int>(ScreenLock::StrongAuthReasonFlags::ACTIVE_REQUEST);
40 ret = ScreenLock::ScreenLockManager::GetInstance()->RequestStrongAuth(reasonFlag, ids[0]);
41 if (ret != ScreenLock::E_SCREENLOCK_OK) {
42 LOGE("Request strong auth by screen lock manager failed.");
43 StorageRadar::ReportOsAccountResult("ForceLockUserScreen::RequestStrongAuth", ret, ids[0]);
44 return;
45 }
46 ret = ScreenLock::ScreenLockManager::GetInstance()->Lock(ids[0]);
47 if (ret != ScreenLock::E_SCREENLOCK_OK) {
48 LOGE("Lock user screen by screen lock manager failed.");
49 StorageRadar::ReportOsAccountResult("ForceLockUserScreen::Lock", ret, ids[0]);
50 return;
51 }
52 LOGI("Force lock user screen and request strong auth success for userId = %{public}d.", ids[0]);
53 #endif
54 }
55
CheckAccountExists(unsigned int userId,bool & isOsAccountExists)56 int32_t KeyCryptoUtils::CheckAccountExists(unsigned int userId, bool &isOsAccountExists)
57 {
58 LOGW("CheckAccountExists");
59 #ifdef ENABLE_SCREENLOCK_MANAGER
60 int32_t ret = AccountSA::OsAccountManager::IsOsAccountExists(userId, isOsAccountExists);
61 if (ret != ERR_OK) {
62 LOGE("Check userId failed, ret = %{public}u", ret);
63 StorageRadar::ReportOsAccountResult("CheckAccountExists::IsOsAccountExists", ret, userId);
64 return ret;
65 }
66 LOGW("account %{public}d, is exists: %{public}d", userId, isOsAccountExists);
67 #endif
68 return 0;
69 }
70 }
71 }