1 /*
2  * Copyright (c) 2020 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 SECURITY_APP_CENTRALDIECTORY_H
17 #define SECURITY_APP_CENTRALDIECTORY_H
18 
19 #include <stdint.h>
20 
21 #include "mbedtls/pk.h"
22 #include "app_verify_pub.h"
23 #include "app_file.h"
24 #include "app_common.h"
25 
26 #ifdef __cplusplus
27 #if __cplusplus
28 extern "C" {
29 #endif
30 #endif
31 
32 #define HAP_SIG_BLOCK_MAGIC_HI_OLD 3617552046287187010LL
33 #define HAP_SIG_BLOCK_MAGIC_LO_OLD 2334950737560224072LL
34 #define HAP_SIG_BLOCK_MAGIC_HI 4497797983070462062LL
35 #define HAP_SIG_BLOCK_MAGIC_LO 7451613641622775868LL
36 #define HAP_SIG_BLOCK_MIN_SIZE 32
37 #define UINT16_MAX_VALUE 0xffff
38 #define HAP_EOCD_MAGIC 0x06054b50
39 #define HAP_FIRST_LEVEL_CHUNK_PREFIX 0x5a
40 #define HAP_SECOND_LEVEL_CHUNK_PREFIX 0xa5
41 #define VERSION_FOR_NEW_MAGIC_NUM 3
42 
43 typedef struct {
44     void *buffer;
45     int32_t len;
46 } HapBuf;
47 
48 #pragma pack(4)
49 /* hw sign head */
50 typedef struct {
51     uint32_t blockNum;
52     unsigned long long size;
53     unsigned long long magicLow;
54     unsigned long long magicHigh;
55     uint32_t version;
56 } HwSignHead;
57 #pragma pack()
58 
59 #pragma pack(2)
60 typedef struct {
61     int32_t magic;
62     short diskNum;
63     short startNum;
64     short coreDirNumOnDisk;
65     short coreDirNum;
66     int32_t coreDirSize;
67     int32_t coreDirOffset;
68     short commentLen;
69 } MinEocd;
70 #pragma pack()
71 
72 typedef struct {
73     MinEocd eocdHead;
74     char *comment;
75 } HapEocd;
76 
77 typedef struct {
78     HwSignHead *signHead;
79     int32_t fullSignBlockOffset;
80     int32_t hapCoreDirOffset;
81     int32_t hapEocdOffset;
82     int32_t hapEocdSize;
83     int32_t fileSize;
84     int32_t version;
85     int32_t certType;
86 } SignatureInfo;
87 
88 bool FindSignature(const FileRead *hapFile, SignatureInfo *signInfo);
89 bool CreateHapBuffer(HapBuf *hapBuffer, int32_t len);
90 int32_t ReadFileFullyFromOffset(const HapBuf *buffer, int32_t offset, const FileRead *file);
91 void HapSetInt32(const HapBuf *buffer, int32_t offset, int32_t value);
92 void ClearHapBuffer(HapBuf *hapBuffer);
93 void HapPutByte(const HapBuf *hapBuffer, int32_t offset, char value);
94 void HapPutData(const HapBuf *hapBuffer, int32_t offset, const unsigned char *data, int32_t len);
95 
96 #ifdef __cplusplus
97 #if __cplusplus
98 }
99 #endif
100 #endif
101 
102 #endif
103