1 /*
2  * Copyright (C) 2021 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 "init/device_type_manager.h"
17 
18 #include "common/hap_verify_log.h"
19 #include "init/trusted_root_ca.h"
20 #include "init/trusted_source_manager.h"
21 
22 #ifdef SUPPORT_GET_DEVICE_TYPES
23 #include "security_device_mode.h"
24 #endif // SUPPORT_GET_DEVICE_TYPES
25 
26 namespace OHOS {
27 namespace Security {
28 namespace Verify {
GetInstance()29 DeviceTypeManager& DeviceTypeManager::GetInstance()
30 {
31     static DeviceTypeManager deviceTypeManager;
32     return deviceTypeManager;
33 }
34 
DeviceTypeManager()35 DeviceTypeManager::DeviceTypeManager() : deviceType(false), getDeviceTypeMtx()
36 {
37 }
38 
~DeviceTypeManager()39 DeviceTypeManager::~DeviceTypeManager()
40 {
41 }
42 
GetDeviceType() const43 bool DeviceTypeManager::GetDeviceType() const
44 {
45 #ifndef SUPPORT_GET_DEVICE_TYPES
46     return false;
47 #else
48     return InvokeIsDevelopmentMode();
49 #endif // SUPPORT_GET_DEVICE_TYPES
50 }
51 
GetDeviceTypeInfo()52 bool DeviceTypeManager::GetDeviceTypeInfo()
53 {
54     bool currentDeviceType = GetDeviceType();
55     HAPVERIFY_LOG_DEBUG("current device is type: %{public}d", static_cast<int>(currentDeviceType));
56 
57     if (currentDeviceType == deviceType) {
58         return currentDeviceType;
59     }
60 
61     TrustedRootCa& rootCertsObj = TrustedRootCa::GetInstance();
62     TrustedSourceManager& trustedAppSourceManager = TrustedSourceManager::GetInstance();
63     getDeviceTypeMtx.lock();
64     if (currentDeviceType) {
65         /* Device type change from  commercial to debugging */
66         bool ret = rootCertsObj.EnableDebug() && trustedAppSourceManager.EnableDebug();
67         if (!ret) {
68             HAPVERIFY_LOG_ERROR("Enable debug failed");
69             rootCertsObj.DisableDebug();
70             trustedAppSourceManager.DisableDebug();
71             getDeviceTypeMtx.unlock();
72             return currentDeviceType;
73         }
74     } else {
75         /* Device type change from debugging to commercial */
76         rootCertsObj.DisableDebug();
77         trustedAppSourceManager.DisableDebug();
78     }
79     deviceType = currentDeviceType;
80     getDeviceTypeMtx.unlock();
81     return currentDeviceType;
82 }
83 } // namespace Verify
84 } // namespace Security
85 } // namespace OHOS
86