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 #ifndef CERT_VERIFY_H 17 #define CERT_VERIFY_H 18 19 #include <memory> 20 #include <string> 21 #include <openssl/x509.h> 22 #include "macros.h" 23 24 namespace Hpackage { 25 struct CertInfo { 26 X509 *rootCert = nullptr; 27 std::string subject {}; 28 std::string issuer {}; 29 }; 30 31 class CertHelper { 32 public: 33 virtual int32_t CertChainCheck(STACK_OF(X509) *certStack, X509 *cert) = 0; ~CertHelper()34 virtual ~CertHelper() {} 35 }; 36 37 class CertVerify { 38 DISALLOW_COPY_MOVE(CertVerify); 39 public: 40 void RegisterCertHelper(std::unique_ptr<CertHelper> ptr); 41 static CertVerify &GetInstance(); 42 int32_t CheckCertChain(STACK_OF(X509) *certStack, X509 *cert); 43 44 private: 45 CertVerify() = default; 46 ~CertVerify() = default; 47 std::unique_ptr<CertHelper> helper_ {}; 48 }; 49 50 class SingleCertHelper : public CertHelper { 51 public: 52 SingleCertHelper() = default; 53 virtual ~SingleCertHelper(); 54 55 int32_t CertChainCheck(STACK_OF(X509) *certStack, X509 *cert) override; 56 57 private: 58 int32_t InitRootCert(); 59 int32_t VerifySingleCert(X509 *cert); 60 int32_t CompareCertSubjectAndIssuer(X509 *cert); 61 62 CertInfo rootInfo_ {}; 63 }; 64 } // namespace Hpackage 65 66 #endif 67