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