blob: 6516917afd9d3fa1b6858e7248172f617274d994 [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 Buynytskyy17d9da02020-02-12 16:03:30 -080019import java.io.ByteArrayInputStream;
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080020import java.io.ByteArrayOutputStream;
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080021import java.io.DataInputStream;
22import java.io.DataOutputStream;
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080023import java.io.File;
24import java.io.FileInputStream;
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080025import java.io.IOException;
26
27/**
28 * V4 signature fields.
29 * Keep in sync with APKSig master copy.
30 * @hide
31 */
32public class V4Signature {
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080033 public static final String EXT = ".idsig";
34
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080035 public final byte[] verityRootHash;
36 public final byte[] v3Digest;
37 public final byte[] pkcs7SignatureBlock;
38
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080039 /**
40 * Construct a V4Signature from .idsig file.
41 */
42 public static V4Signature readFrom(File file) {
43 try (DataInputStream stream = new DataInputStream(new FileInputStream(file))) {
44 return readFrom(stream);
45 } catch (IOException e) {
46 return null;
47 }
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080048 }
49
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080050 /**
Alex Buynytskyy17d9da02020-02-12 16:03:30 -080051 * Construct a V4Signature from .idsig file.
52 */
53 public static V4Signature readFrom(byte[] bytes) throws IOException {
54 try (DataInputStream stream = new DataInputStream(new ByteArrayInputStream(bytes))) {
55 return readFrom(stream);
56 }
57 }
58
59 /**
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080060 * Store the V4Signature to a byte-array.
61 */
62 public byte[] toByteArray() {
63 try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
64 try (DataOutputStream steam = new DataOutputStream(byteArrayOutputStream)) {
65 this.writeTo(steam);
66 steam.flush();
67 }
68 return byteArrayOutputStream.toByteArray();
69 } catch (IOException e) {
70 return null;
71 }
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080072 }
73
74 static V4Signature readFrom(DataInputStream stream) throws IOException {
75 byte[] verityRootHash = readBytes(stream);
76 byte[] v3Digest = readBytes(stream);
77 byte[] pkcs7SignatureBlock = readBytes(stream);
78 return new V4Signature(verityRootHash, v3Digest, pkcs7SignatureBlock);
79 }
80
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080081 V4Signature(byte[] verityRootHash, byte[] v3Digest, byte[] pkcs7SignatureBlock) {
82 this.verityRootHash = verityRootHash;
83 this.v3Digest = v3Digest;
84 this.pkcs7SignatureBlock = pkcs7SignatureBlock;
Alex Buynytskyycd4d3872020-02-08 17:50:50 -080085 }
86
87 void writeTo(DataOutputStream stream) throws IOException {
88 writeBytes(stream, this.verityRootHash);
89 writeBytes(stream, this.v3Digest);
90 writeBytes(stream, this.pkcs7SignatureBlock);
91 }
Alex Buynytskyy8e9e6a32020-02-08 14:26:45 -080092
93 private static byte[] readBytes(DataInputStream stream) throws IOException {
94 byte[] result = new byte[stream.readInt()];
95 stream.read(result);
96 return result;
97 }
98
99 private static void writeBytes(DataOutputStream stream, byte[] bytes) throws IOException {
100 stream.writeInt(bytes.length);
101 stream.write(bytes);
102 }
Alex Buynytskyycd4d3872020-02-08 17:50:50 -0800103}