Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os.incremental; |
| 18 | |
Alex Buynytskyy | 17d9da0 | 2020-02-12 16:03:30 -0800 | [diff] [blame^] | 19 | import java.io.ByteArrayInputStream; |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 20 | import java.io.ByteArrayOutputStream; |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 21 | import java.io.DataInputStream; |
| 22 | import java.io.DataOutputStream; |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 23 | import java.io.File; |
| 24 | import java.io.FileInputStream; |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 25 | import java.io.IOException; |
| 26 | |
| 27 | /** |
| 28 | * V4 signature fields. |
| 29 | * Keep in sync with APKSig master copy. |
| 30 | * @hide |
| 31 | */ |
| 32 | public class V4Signature { |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 33 | public static final String EXT = ".idsig"; |
| 34 | |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 35 | public final byte[] verityRootHash; |
| 36 | public final byte[] v3Digest; |
| 37 | public final byte[] pkcs7SignatureBlock; |
| 38 | |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 39 | /** |
| 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 Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 50 | /** |
Alex Buynytskyy | 17d9da0 | 2020-02-12 16:03:30 -0800 | [diff] [blame^] | 51 | * 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 Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 60 | * 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 Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 72 | } |
| 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 Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 81 | V4Signature(byte[] verityRootHash, byte[] v3Digest, byte[] pkcs7SignatureBlock) { |
| 82 | this.verityRootHash = verityRootHash; |
| 83 | this.v3Digest = v3Digest; |
| 84 | this.pkcs7SignatureBlock = pkcs7SignatureBlock; |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 85 | } |
| 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 Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 92 | |
| 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 Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 103 | } |