1/* 2 * Copyright (c) 2022 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 "cert_verify.h" 17 18#include <sys/stat.h> 19#include "dump.h" 20#include "openssl_util.h" 21#include "pkg_utils.h" 22#include "utils.h" 23 24using namespace std; 25using namespace Updater; 26namespace Hpackage { 27extern "C" __attribute__((constructor)) void RegisterCertHelper(void) 28{ 29 CertVerify::GetInstance().RegisterCertHelper(std::make_unique<SingleCertHelper>()); 30} 31 32void CertVerify::RegisterCertHelper(std::unique_ptr<CertHelper> ptr) 33{ 34 helper_ = std::move(ptr); 35} 36 37CertVerify &CertVerify::GetInstance() 38{ 39 static CertVerify certVerify; 40 return certVerify; 41} 42 43int32_t CertVerify::Init() 44{ 45 if (helper_ == nullptr) { 46 PKG_LOGE("helper_ null error"); 47 return -1; 48 } 49 return helper_->Init(); 50} 51 52int32_t CertVerify::CheckCertChain(STACK_OF(X509) *certStack, X509 *cert) 53{ 54 if (helper_ == nullptr) { 55 PKG_LOGE("helper_ null error"); 56 return -1; 57 } 58 return helper_->CertChainCheck(certStack, cert); 59} 60 61SingleCertHelper::~SingleCertHelper() 62{ 63 if (rootInfo_.rootCert != nullptr) { 64 X509_free(rootInfo_.rootCert); 65 } 66} 67 68int32_t SingleCertHelper::Init() 69{ 70 int32_t ret = InitRootCert(); 71 if (ret != 0) { 72 PKG_LOGE("Init root cert fail"); 73 return -1; 74 } 75 return 0; 76} 77 78int32_t SingleCertHelper::CertChainCheck(STACK_OF(X509) * certStack, X509 *cert) 79{ 80 UNUSED(certStack); 81 if (cert == nullptr) { 82 return -1; 83 } 84 85 return VerifySingleCert(cert); 86} 87 88int32_t SingleCertHelper::InitRootCert() 89{ 90#ifndef DIFF_PATCH_SDK 91 X509 *rootCert = GetX509CertFromPemFile(Utils::GetCertName()); 92 if (rootCert == nullptr) { 93 PKG_LOGE("Get root cert fail, file: %s", Utils::GetCertName().c_str()); 94 UPDATER_LAST_WORD(-1); 95 return -1; 96 } 97 if (rootInfo_.rootCert != nullptr) { 98 X509_free(rootInfo_.rootCert); 99 rootInfo_.rootCert = nullptr; 100 } 101 rootInfo_.rootCert = rootCert; 102 rootInfo_.subject = GetX509CertSubjectName(rootCert); 103 rootInfo_.issuer = GetX509CertIssuerName(rootCert); 104#endif 105 106 return 0; 107} 108 109int32_t SingleCertHelper::VerifySingleCert(X509 *cert) 110{ 111 int32_t ret = CompareCertSubjectAndIssuer(cert); 112 if (ret != 0) { 113 PKG_LOGE("compare cert subject and issuer fail"); 114 return -1; 115 } 116 117 return ((VerifyX509CertByIssuerCert(cert, rootInfo_.rootCert)) ? 0 : -1); 118} 119 120int32_t SingleCertHelper::CompareCertSubjectAndIssuer(X509 *cert) 121{ 122 string certSubject = GetX509CertSubjectName(cert); 123 string certIssuer = GetX509CertIssuerName(cert); 124 if (rootInfo_.subject.compare(certSubject) == 0 && 125 rootInfo_.issuer.compare(certIssuer) == 0) { 126 return 0; 127 } 128 129 return -1; 130} 131 132int32_t CertHelper::Init() 133{ 134 return 0; 135} 136} // namespace Hpackage 137