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 #ifdef L2_STANDARD
17 #ifdef HUKS_ENABLE_UPGRADE_KEY_STORAGE_SECURE_LEVEL
18 #include "hks_upgrade.h"
19 #include "hks_log.h"
20 #include "hks_template.h"
21 #include "hks_type.h"
22 #include "hks_osaccount_check.h"
23
24 #include <atomic>
25
26 #ifdef HAS_OS_ACCOUNT_PART
27 #include "os_account_manager.h"
28 #endif // HAS_OS_ACCOUNT_PART
29
30 namespace {
31 static volatile std::atomic_bool g_isCeUpgradeSucc = false;
32
HksIsOsAccountVerified(const int32_t userId)33 static bool HksIsOsAccountVerified(const int32_t userId)
34 {
35 bool isVerified = false;
36 #ifdef HAS_OS_ACCOUNT_PART
37 HKS_LOG_D("enter HksIsOsAccountVerified");
38 OHOS::AccountSA::OsAccountManager::IsOsAccountVerified(userId, isVerified);
39 HKS_IF_NOT_TRUE_LOGE(isVerified, "os account verify failed, userid is : %" LOG_PUBLIC "d", userId)
40 #else
41 HKS_LOG_E("os account not support");
42 #endif
43 return isVerified;
44 }
45 }
46
HksTransferFileIfNeed(const uint32_t storageLevel,const int32_t storeUserId)47 extern "C" void HksTransferFileIfNeed(const uint32_t storageLevel, const int32_t storeUserId)
48 {
49 bool flag = false;
50 HKS_IF_NOT_TRUE_RETURN_VOID(std::atomic_compare_exchange_strong(&g_isCeUpgradeSucc, &flag, true));
51 HKS_LOG_I("never HksTransferFileIfNeed before, first time upgrade ce!");
52 HKS_IF_NOT_TRUE_RETURN_VOID(storageLevel == HKS_AUTH_STORAGE_LEVEL_CE && HksIsOsAccountVerified(storeUserId))
53 OHOS::Security::Hks::HksUpgradeOnUserUnlock(storeUserId);
54 }
55
56 #endif // HUKS_ENABLE_UPGRADE_KEY_STORAGE_SECURE_LEVEL
57 #endif // L2_STANDARD