• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "crypto_delay_handler.h"
17 #include "storage_service_log.h"
18 #include "storage_service_errno.h"
19 #include "utils/storage_radar.h"
20 
21 using namespace OHOS::StorageService;
22 namespace OHOS {
23 namespace StorageDaemon {
24 constexpr int32_t DEFAULT_CHECK_INTERVAL = 10 * 1000; // 10s
25 
DelayHandler(uint32_t userId)26 DelayHandler::DelayHandler(uint32_t userId): timerId_(0), cancelled_(false), userId_(userId)
27 {
28     timer_.Setup();
29 }
30 
~DelayHandler()31 DelayHandler::~DelayHandler()
32 {
33     LOGI("DelayHandler Destructor.");
34     timer_.Shutdown();
35     LOGI("DelayHandler::Destruct success.");
36 }
37 
StartDelayTask(const std::shared_ptr<BaseKey> & el4Key)38 void DelayHandler::StartDelayTask(const std::shared_ptr<BaseKey>& el4Key)
39 {
40     LOGI("DelayHandler::StartDelayTask: enter.");
41     CancelDelayTask();
42     std::lock_guard<std::mutex> lock(handlerMutex_);
43     if (el4Key == nullptr) {
44         LOGI("elKey is nullptr do not clean.");
45         return;
46     }
47     el4Key_ = el4Key;
48     cancelled_ = false;
49     std::string curTime = std::to_string(GetTickCount());
50     std::string expExeTime = std::to_string(GetTickCount() + DEFAULT_CHECK_INTERVAL);
51     timerId_ = timer_.Register([this, expExeTime]() {
52             std::string realExeTime = std::to_string(GetTickCount());
53             LOGI("DelayHandler: EXECUTE for user=%{public}d, curTime=%{public}s ms, expExeTime=%{public}s ms.",
54                 userId_, realExeTime.c_str(), expExeTime.c_str());
55             DeactiveEl3El4El5();
56         }, DEFAULT_CHECK_INTERVAL, true);
57 
58     LOGI("DelayHandler: start timer for user=%{public}d, curTime=%{public}s ms, exeTime=%{public}s ms success.",
59         userId_, curTime.c_str(), expExeTime.c_str());
60 }
61 
CancelDelayTask()62 void DelayHandler::CancelDelayTask()
63 {
64     LOGD("DelayHandler::CancelDelayTask:: enter.");
65     std::lock_guard<std::mutex> lock(handlerMutex_);
66     timer_.Unregister(timerId_);
67     cancelled_ = true;
68 
69     LOGD("DelayHandler::CancelDelayTask:: success.");
70 }
71 
DeactiveEl3El4El5()72 void DelayHandler::DeactiveEl3El4El5()
73 {
74     LOGD("DelayHandler::DeactiveEl3El4El5:: enter.");
75     std::lock_guard<std::mutex> lock(handlerMutex_);
76     if (el4Key_ == nullptr) {
77         LOGI("DelayHandler::DeactiveEl3El4El5:: elKey is nullptr do not clean.");
78         StorageRadar::ReportUpdateUserAuth("DeactiveEl3El4El5", userId_, E_PARAMS_INVALID, "EL4", "");
79         return;
80     }
81     if (cancelled_) {
82         LOGI(" DelayHandler: task is cancelled.");
83         return;
84     }
85     int32_t ret = el4Key_->LockUserScreen(userId_, FSCRYPT_SDP_ECE_CLASS);
86     if (ret != E_OK) {
87         LOGE("DelayHandler::DeactiveEl3El4El5:: Clear user %{public}u key failed.", userId_);
88         StorageRadar::ReportUpdateUserAuth("DeactiveEl3El4El5::LockUserScreen", userId_, E_SYS_KERNEL_ERR, "EL4", "");
89         return;
90     }
91     cancelled_ = false;
92     LOGW("success");
93 }
94 
GetTickCount()95 int64_t DelayHandler::GetTickCount()
96 {
97     return std::chrono::duration_cast<std::chrono::milliseconds>(
98         std::chrono::system_clock::now().time_since_epoch()
99     ).count();
100 }
101 } // StorageDaemon
102 } // OHOS