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_SIGH_HAP_H
16 #define SIGNATRUETOOLS_SIGH_HAP_H
17 #include <limits.h>
18 #include <unordered_map>
19 #include <vector>
20 #include <memory>
21 
22 #include "hap_utils.h"
23 #include "signer_config.h"
24 #include "hap_signer_block_utils.h"
25 #include "securec.h"
26 
27 namespace OHOS {
28 namespace SignatureTools {
29 class SignHap {
30 public:
31     static bool Sign(DataSource* contents[], int32_t len, SignerConfig& config,
32                      std::vector<OptionalBlock>& optionalBlocks, ByteBuffer& result);
33     static bool ComputeDigests(const DigestParameter& digestParam, DataSource* contents[], int32_t len,
34                                const std::vector<OptionalBlock>& optionalBlocks, ByteBuffer& result);
35 
36 private:
37     static constexpr int INT_SIZE = 4;
38     static constexpr int CONTENT_VERSION = 2;
39     static constexpr int BLOCK_NUMBER = 1;
40     static constexpr int CONTENT_NUBER = 3;
41     static constexpr int STORED_ENTRY_SO_ALIGNMENT = 4096;
42     static constexpr int BUFFER_LENGTH = 4096;
43     static constexpr int BLOCK_COUNT = 4;
44     static constexpr int BLOCK_MAGIC = 16;
45     static constexpr int BLOCK_VERSION = 4;
46     static constexpr long INIT_OFFSET_LEN = 4L;
47     static constexpr int OPTIONAL_TYPE_SIZE = 4;
48     static constexpr int OPTIONAL_LENGTH_SIZE = 4;
49     static constexpr int OPTIONAL_OFFSET_SIZE = 4;
50     static void ExtractedResult(std::vector<OptionalBlock>& optionalBlocks, ByteBuffer& result,
51                                 std::unordered_map<int, int>& typeAndOffsetMap);
52     static bool GenerateHapSigningBlock(const std::string& hapSignatureSchemeBlock,
53                                         std::vector<OptionalBlock>& optionalBlocks,
54                                         int compatibleVersion, ByteBuffer& result);
55     static void EncodeListOfPairsToByteArray(const DigestParameter& digestParam,
56                                              const std::vector<std::pair<int32_t, ByteBuffer>>& contentDigests,
57                                              ByteBuffer& result);
58 };
59 } // namespace SignatureTools
60 } // namespace OHOS
61 #endif // SIGNATRUETOOLS_SIGH_HAP_H
62