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 | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame^] | 19 | import java.io.ByteArrayOutputStream; |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 20 | import java.io.DataInputStream; |
| 21 | import java.io.DataOutputStream; |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame^] | 22 | import java.io.File; |
| 23 | import java.io.FileInputStream; |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | |
| 26 | /** |
| 27 | * V4 signature fields. |
| 28 | * Keep in sync with APKSig master copy. |
| 29 | * @hide |
| 30 | */ |
| 31 | public class V4Signature { |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame^] | 32 | public static final String EXT = ".idsig"; |
| 33 | |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 34 | public final byte[] verityRootHash; |
| 35 | public final byte[] v3Digest; |
| 36 | public final byte[] pkcs7SignatureBlock; |
| 37 | |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame^] | 38 | /** |
| 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 Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame^] | 49 | /** |
| 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 Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 62 | } |
| 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 Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame^] | 71 | V4Signature(byte[] verityRootHash, byte[] v3Digest, byte[] pkcs7SignatureBlock) { |
| 72 | this.verityRootHash = verityRootHash; |
| 73 | this.v3Digest = v3Digest; |
| 74 | this.pkcs7SignatureBlock = pkcs7SignatureBlock; |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 75 | } |
| 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 Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame^] | 82 | |
| 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 Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 93 | } |