1 /*
2  * Copyright (c) 2024-2024 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 SIGNATRUETOOLS_VERIFY_HAP_V2_H
16 #define SIGNATRUETOOLS_VERIFY_HAP_V2_H
17 #include <string>
18 
19 #include "byte_buffer.h"
20 #include "random_access_file.h"
21 #include "profile_verify.h"
22 #include "verify_hap_openssl_utils.h"
23 #include "signature_info.h"
24 #include "options.h"
25 #include "file_utils.h"
26 
27 namespace OHOS {
28 namespace SignatureTools {
29 class VerifyHap {
30 public:
31     static const int32_t HEX_PRINT_LENGTH;
32     static const int32_t DIGEST_BLOCK_LEN_OFFSET;
33     static const int32_t DIGEST_ALGORITHM_OFFSET;
34     static const int32_t DIGEST_LEN_OFFSET;
35     static const int32_t DIGEST_OFFSET_IN_CONTENT;
36     static const std::string HAP_APP_PATTERN;
37     static const std::string HQF_APP_PATTERN;
38     static const std::string HSP_APP_PATTERN;
39     static const std::string APP_APP_PATTERN;
40     static const int OFFSET_ZERO = 0;
41     static const int OFFSET_FOUR = 4;
42     static const int OFFSET_EIGHT = 8;
43 
44     VerifyHap();
45     VerifyHap(bool isPrintCert);
46     void setIsPrintCert(bool printCert);
47 
48     bool HapOutPutPkcs7(PKCS7* p7, const std::string& outPutPath);
49 
50     DLL_EXPORT bool CheckFilePath(const std::string& filePath, std::string& standardFilePath);
51 
52     bool outputOptionalBlocks(const std::string& outputProfileFile, const std::string& outputProofFile,
53                                  const std::string& outputPropertyFile,
54                                  const std::vector<OptionalBlock>& optionBlocks);
55     bool writeOptionalBytesToFile(const OptionalBlock& optionalBlock, const std::string& path);
56 
57     bool HapOutPutCertChain(std::vector<X509*>& certs, const std::string& outPutPath);
58 
59     int32_t Verify(const std::string& filePath, Options* options);
60 
61     int32_t WriteVerifyOutput(Pkcs7Context& pkcs7Context, std::vector<int8_t>& profile, Options* options);
62 
63     int32_t Verify(RandomAccessFile& hapFile, Options* options, const std::string& filePath);
64 
65     bool CheckCodeSign(const std::string& hapFilePath, const std::vector<OptionalBlock>& optionalBlocks)const;
66     static int GetProfileContent(const std::string profile, std::string& ret);
67 
68     bool VerifyAppPkcs7(Pkcs7Context& pkcs7Context, const ByteBuffer& hapSignatureBlock);
69     DLL_EXPORT bool GetDigestAndAlgorithm(Pkcs7Context& digest);
70     static bool PrintCertChainToCmd(std::vector<X509*>& certChain);
71 
72 private:
73     bool isPrintCert;
74 };
75 } // namespace SignatureTools
76 } // namespace OHOS
77 #endif // SIGNATRUETOOLS_VERIFY_HAP_V2_H