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 #ifndef HAP_TRUSTED_ROOT_CA_H 16 #define HAP_TRUSTED_ROOT_CA_H 17 18 #include <string> 19 #include <unordered_map> 20 21 #include "openssl/x509.h" 22 23 #include "common/export_define.h" 24 #include "init/json_parser_utils.h" 25 26 namespace OHOS { 27 namespace Security { 28 namespace Verify { 29 using StringCertMap = std::unordered_map<std::string, X509*>; 30 31 class TrustedRootCa { 32 public: 33 DLL_EXPORT static TrustedRootCa& GetInstance(); 34 DLL_EXPORT bool Init(); 35 DLL_EXPORT void Recovery(); 36 DLL_EXPORT bool EnableDebug(); 37 DLL_EXPORT void DisableDebug(); 38 DLL_EXPORT X509* FindMatchedRoot(X509* caCert); 39 40 private: 41 TrustedRootCa(); 42 ~TrustedRootCa(); 43 44 /* Forbid external replication constructs and external replication */ 45 TrustedRootCa(const TrustedRootCa& trustedRoot) = delete; 46 TrustedRootCa& operator = (const TrustedRootCa& trustedRoot) = delete; 47 48 DLL_EXPORT bool GetTrustedRootCAFromJson(StringCertMap& rootCertMap, const std::string& filePath); 49 X509* FindMatchedRoot(const StringCertMap& rootCertMap, X509* caCert); 50 51 private: 52 static const std::string TRUSTED_ROOT_CA_FILE_PATH; 53 static const std::string TRUSTED_ROOT_CA_TEST_FILE_PATH; 54 StringCertMap rootCerts; 55 StringCertMap rootCertsForTest; 56 bool isInit; 57 bool isDebug; 58 }; 59 } // namespace Verify 60 } // namespace Security 61 } // namespace OHOS 62 #endif // HAP_TRUSTED_ROOT_CA_H 63