1 /* 2 * Copyright (c) 2021-2023 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 package com.ohos.hapsigntool.hap.entity; 17 18 import com.ohos.hapsigntool.utils.ByteArrayUtils; 19 20 import java.io.IOException; 21 import java.nio.ByteBuffer; 22 import java.nio.ByteOrder; 23 24 /** 25 * define class of hap signature block head 26 * 27 * @since 2021-12-13 28 */ 29 public class SignHead { 30 /** 31 * length of sign head 32 */ 33 public static final int SIGN_HEAD_LEN = 32; 34 35 /** 36 * sign hap magic string 16Bytes-Magic 37 */ 38 public static final char[] MAGIC = "hw signed app ".toCharArray(); 39 40 /** 41 * sign elf magic string 16Bytes-Magic 42 */ 43 public static final char[] ELF_MAGIC = "elf sign block ".toCharArray(); 44 45 /** 46 * sign block version 4-Bytes, version is 1.0.0.0 47 */ 48 public static final char[] VERSION = "1000".toCharArray(); 49 50 private static final int NUM_OF_BLOCK = 2; // number of sub-block 51 52 private static final int RESERVE_LENGTH = 4; 53 54 private char[] reserve = new char[RESERVE_LENGTH]; 55 56 /** 57 * get serialization of HwSignHead file type of bin 58 * 59 * @param subBlockSize the total size of all sub-blocks 60 * @return Byte array after serialization of HwSignHead 61 */ getSignHead(int subBlockSize)62 public byte[] getSignHead(int subBlockSize) { 63 int size = subBlockSize; // total size of sub-block 64 byte[] signHead = new byte[SIGN_HEAD_LEN]; 65 int start = 0; 66 try { 67 start = ByteArrayUtils.insertCharToByteArray(signHead, start, MAGIC); 68 if (start < 0) { 69 throw new IOException(); 70 } 71 start = ByteArrayUtils.insertCharToByteArray(signHead, start, VERSION); 72 if (start < 0) { 73 throw new IOException(); 74 } 75 start = ByteArrayUtils.insertIntToByteArray(signHead, start, size); 76 if (start < 0) { 77 throw new IOException(); 78 } 79 start = ByteArrayUtils.insertIntToByteArray(signHead, start, NUM_OF_BLOCK); 80 if (start < 0) { 81 throw new IOException(); 82 } 83 start = ByteArrayUtils.insertCharToByteArray(signHead, start, reserve); 84 if (start < 0) { 85 throw new IOException(); 86 } 87 } catch (IOException e) { 88 return new byte[0]; 89 } 90 return signHead; 91 } 92 93 /** 94 * get serialization of HwSignHead file type of elf 95 * 96 * @param subBlockSize the total size of all sub-blocks 97 * @param subBlockNum the sign block num 98 * @return Byte array after serialization of HwSignHead 99 */ getSignHeadLittleEndian(int subBlockSize, int subBlockNum)100 public byte[] getSignHeadLittleEndian(int subBlockSize, int subBlockNum) { 101 ByteBuffer bf = ByteBuffer.allocate(SIGN_HEAD_LEN).order(ByteOrder.LITTLE_ENDIAN); 102 for (char c : ELF_MAGIC) { 103 bf.put((byte) c); 104 } 105 for (char c : VERSION) { 106 bf.put((byte) c); 107 } 108 bf.putInt(subBlockSize); 109 bf.putInt(subBlockNum); 110 for (char c : reserve) { 111 bf.put((byte) c); 112 } 113 return bf.array(); 114 } 115 } 116