• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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.codesigning.fsverity;
17 
18 import com.ohos.hapsigntool.codesigning.exception.FsVerityDigestException;
19 
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22 
23 /**
24  * fsverity descriptor and signature
25  * 1) u32 type: 0x1 fsverity descriptor
26  * 2) u32 length: fsverity descriptor size
27  * 3) FsVerityDescriptor: fsVerityDescriptor
28  * 4) u8[] signature: signature after signing the data in byte array representation
29  *
30  * @since 2023/09/08
31  */
32 public class FsVerityDescriptorWithSign {
33     private int type = FsVerityDescriptor.FS_VERITY_DESCRIPTOR_TYPE;
34 
35     private int length;
36 
37     private FsVerityDescriptor fsVerityDescriptor;
38 
39     private byte[] signature = new byte[0];
40 
41     /**
42      * Constructor of FsVerityDescriptorWithSign
43      *
44      * @param fsVerityDescriptor fs-verity descriptor
45      * @param signature          signature
46      */
FsVerityDescriptorWithSign(FsVerityDescriptor fsVerityDescriptor, byte[] signature)47     public FsVerityDescriptorWithSign(FsVerityDescriptor fsVerityDescriptor, byte[] signature) {
48         this.fsVerityDescriptor = fsVerityDescriptor;
49         if (signature != null) {
50             this.signature = signature;
51         }
52         length = FsVerityDescriptor.DESCRIPTOR_SIZE + this.signature.length;
53     }
54 
55     /**
56      * Constructor of FsVerityDescriptorWithSign
57      *
58      * @param type               fs-verity descriptor type
59      * @param length             fs-verity descriptor with signature size
60      * @param fsVerityDescriptor fs-verity descriptor
61      * @param signature signature
62      */
FsVerityDescriptorWithSign(int type, int length, FsVerityDescriptor fsVerityDescriptor, byte[] signature)63     public FsVerityDescriptorWithSign(int type, int length, FsVerityDescriptor fsVerityDescriptor, byte[] signature) {
64         this.type = type;
65         this.length = length;
66         this.fsVerityDescriptor = fsVerityDescriptor;
67         this.signature = signature;
68     }
69 
70     /**
71      * Returns byte size of FsVerityDescriptorWithSign
72      *
73      * @return byte size of FsVerityDescriptorWithSign
74      */
size()75     public int size() {
76         return Integer.BYTES * 2 + FsVerityDescriptor.DESCRIPTOR_SIZE + signature.length;
77     }
78 
79     /**
80      * Converts FsVerityDescriptorWithSign to a newly created byte array
81      *
82      * @return Byte array representation of FsVerityDescriptorWithSign
83      * @throws FsVerityDigestException if error
84      */
toByteArray()85     public byte[] toByteArray() throws FsVerityDigestException {
86         ByteBuffer buffer = ByteBuffer.allocate(size()).order(ByteOrder.LITTLE_ENDIAN);
87         buffer.putInt(type);
88         buffer.putInt(length);
89         buffer.put(fsVerityDescriptor.toByteArray());
90         buffer.put(signature);
91         return buffer.array();
92     }
93 
getFsVerityDescriptor()94     public FsVerityDescriptor getFsVerityDescriptor() {
95         return fsVerityDescriptor;
96     }
97 
getSignature()98     public byte[] getSignature() {
99         return signature;
100     }
101 }
102