blob: 6450a67572bfa9f3c9f01737fd90abfb64cdc309 [file] [log] [blame]
Alex Buynytskyycd4d3872020-02-08 17:50:50 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os.incremental;
18
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080019import java.io.ByteArrayOutputStream;
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080020import java.io.DataInputStream;
21import java.io.DataOutputStream;
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080022import java.io.File;
23import java.io.FileInputStream;
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080024import java.io.IOException;
25
26/**
27 * V4 signature fields.
28 * Keep in sync with APKSig master copy.
29 * @hide
30 */
31public class V4Signature {
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080032 public static final String EXT = ".idsig";
33
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080034 public final byte[] verityRootHash;
35 public final byte[] v3Digest;
36 public final byte[] pkcs7SignatureBlock;
37
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080038 /**
39 * Construct a V4Signature from .idsig file.
40 */
41 public static V4Signature readFrom(File file) {
42 try (DataInputStream stream = new DataInputStream(new FileInputStream(file))) {
43 return readFrom(stream);
44 } catch (IOException e) {
45 return null;
46 }
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080047 }
48
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080049 /**
50 * Store the V4Signature to a byte-array.
51 */
52 public byte[] toByteArray() {
53 try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
54 try (DataOutputStream steam = new DataOutputStream(byteArrayOutputStream)) {
55 this.writeTo(steam);
56 steam.flush();
57 }
58 return byteArrayOutputStream.toByteArray();
59 } catch (IOException e) {
60 return null;
61 }
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080062 }
63
64 static V4Signature readFrom(DataInputStream stream) throws IOException {
65 byte[] verityRootHash = readBytes(stream);
66 byte[] v3Digest = readBytes(stream);
67 byte[] pkcs7SignatureBlock = readBytes(stream);
68 return new V4Signature(verityRootHash, v3Digest, pkcs7SignatureBlock);
69 }
70
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080071 V4Signature(byte[] verityRootHash, byte[] v3Digest, byte[] pkcs7SignatureBlock) {
72 this.verityRootHash = verityRootHash;
73 this.v3Digest = v3Digest;
74 this.pkcs7SignatureBlock = pkcs7SignatureBlock;
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080075 }
76
77 void writeTo(DataOutputStream stream) throws IOException {
78 writeBytes(stream, this.verityRootHash);
79 writeBytes(stream, this.v3Digest);
80 writeBytes(stream, this.pkcs7SignatureBlock);
81 }
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080082
83 private static byte[] readBytes(DataInputStream stream) throws IOException {
84 byte[] result = new byte[stream.readInt()];
85 stream.read(result);
86 return result;
87 }
88
89 private static void writeBytes(DataOutputStream stream, byte[] bytes) throws IOException {
90 stream.writeInt(bytes.length);
91 stream.write(bytes);
92 }
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080093}