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 "interfaces/hap_verify.h"
17
18 #include <mutex>
19
20 #include "init/device_type_manager.h"
21 #include "init/hap_crl_manager.h"
22 #include "init/trusted_root_ca.h"
23 #include "init/trusted_source_manager.h"
24 #include "init/trusted_ticket_manager.h"
25 #include "provision/provision_verify.h"
26 #include "verify/hap_verify_v2.h"
27
28 namespace OHOS {
29 namespace Security {
30 namespace Verify {
31 static std::mutex g_mtx;
32 static bool g_isInit = false;
33
HapVerifyInit()34 bool HapVerifyInit()
35 {
36 TrustedRootCa& rootCertsObj = TrustedRootCa::GetInstance();
37 TrustedSourceManager& trustedAppSourceManager = TrustedSourceManager::GetInstance();
38 HapCrlManager& hapCrlManager = HapCrlManager::GetInstance();
39 DeviceTypeManager& deviceTypeManager = DeviceTypeManager::GetInstance();
40 TrustedTicketManager& trustedTicketSourceManager = TrustedTicketManager::GetInstance();
41 g_mtx.lock();
42 g_isInit = rootCertsObj.Init() && trustedAppSourceManager.Init();
43 if (!g_isInit) {
44 rootCertsObj.Recovery();
45 trustedAppSourceManager.Recovery();
46 }
47 trustedTicketSourceManager.Init();
48 hapCrlManager.Init();
49 deviceTypeManager.GetDeviceTypeInfo();
50 g_mtx.unlock();
51 return g_isInit;
52 }
53
EnableDebugMode()54 bool EnableDebugMode()
55 {
56 TrustedRootCa& rootCertsObj = TrustedRootCa::GetInstance();
57 TrustedSourceManager& trustedAppSourceManager = TrustedSourceManager::GetInstance();
58 g_mtx.lock();
59 bool ret = rootCertsObj.EnableDebug() && trustedAppSourceManager.EnableDebug();
60 if (!ret) {
61 rootCertsObj.DisableDebug();
62 trustedAppSourceManager.DisableDebug();
63 }
64 g_mtx.unlock();
65 return ret;
66 }
67
DisableDebugMode()68 void DisableDebugMode()
69 {
70 TrustedRootCa& rootCertsObj = TrustedRootCa::GetInstance();
71 TrustedSourceManager& trustedAppSourceManager = TrustedSourceManager::GetInstance();
72 g_mtx.lock();
73 rootCertsObj.DisableDebug();
74 trustedAppSourceManager.DisableDebug();
75 g_mtx.unlock();
76 }
77
HapVerify(const std::string & filePath,HapVerifyResult & hapVerifyResult)78 int HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult)
79 {
80 if (!g_isInit && !HapVerifyInit()) {
81 return VERIFY_SOURCE_INIT_FAIL;
82 }
83 HapVerifyV2 hapVerifyV2;
84 return hapVerifyV2.Verify(filePath, hapVerifyResult);
85 }
86
ParseHapProfile(const std::string & filePath,HapVerifyResult & hapVerifyV1Result)87 int ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result)
88 {
89 HapVerifyV2 hapVerifyV2;
90 return hapVerifyV2.ParseHapProfile(filePath, hapVerifyV1Result);
91 }
92 } // namespace Verify
93 } // namespace Security
94 } // namespace OHOS
95