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