| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os.incremental; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.os.ParcelFileDescriptor; |
| 22 |
|
| 23 |
import java.io.ByteArrayInputStream; |
| 24 |
import java.io.ByteArrayOutputStream; |
| 25 |
import java.io.EOFException; |
| 26 |
import java.io.IOException; |
| 27 |
import java.io.InputStream; |
| 28 |
import java.io.OutputStream; |
| 29 |
import java.nio.ByteBuffer; |
| 30 |
import java.nio.ByteOrder; |
| 31 |
import java.util.ArrayList; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class V4Signature { |
| 39 |
public static final String EXT = ".idsig"; |
| 40 |
public static final int SUPPORTED_VERSION = 2; |
| 41 |
|
| 42 |
public static final int HASHING_ALGORITHM_SHA256 = 1; |
| 43 |
public static final byte LOG2_BLOCK_SIZE_4096_BYTES = 12; |
| 44 |
|
| 45 |
public static final int INCFS_MAX_SIGNATURE_SIZE = 8096; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static class HashingInfo { |
| 51 |
public final int hashAlgorithm; |
| 52 |
public final byte log2BlockSize; |
| 53 |
@Nullable public final byte[] salt; |
| 54 |
@Nullable public final byte[] rawRootHash; |
| 55 |
|
| 56 |
HashingInfo(int hashAlgorithm, byte log2BlockSize, byte[] salt, byte[] rawRootHash) { |
| 57 |
this.hashAlgorithm = hashAlgorithm; |
| 58 |
this.log2BlockSize = log2BlockSize; |
| 59 |
this.salt = salt; |
| 60 |
this.rawRootHash = rawRootHash; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@NonNull |
| 67 |
public static HashingInfo fromByteArray(@NonNull byte[] bytes) throws IOException { |
| 68 |
ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); |
| 69 |
final int hashAlgorithm = buffer.getInt(); |
| 70 |
final byte log2BlockSize = buffer.get(); |
| 71 |
byte[] salt = readBytes(buffer); |
| 72 |
byte[] rawRootHash = readBytes(buffer); |
| 73 |
return new HashingInfo(hashAlgorithm, log2BlockSize, salt, rawRootHash); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static class SigningInfo { |
| 81 |
public final byte[] apkDigest; |
| 82 |
public final byte[] certificate; |
| 83 |
public final byte[] additionalData; |
| 84 |
public final byte[] publicKey; |
| 85 |
public final int signatureAlgorithmId; |
| 86 |
public final byte[] signature; |
| 87 |
|
| 88 |
SigningInfo(byte[] apkDigest, byte[] certificate, byte[] additionalData, |
| 89 |
byte[] publicKey, int signatureAlgorithmId, byte[] signature) { |
| 90 |
this.apkDigest = apkDigest; |
| 91 |
this.certificate = certificate; |
| 92 |
this.additionalData = additionalData; |
| 93 |
this.publicKey = publicKey; |
| 94 |
this.signatureAlgorithmId = signatureAlgorithmId; |
| 95 |
this.signature = signature; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static SigningInfo fromByteArray(byte[] bytes) throws IOException { |
| 102 |
return fromByteBuffer(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN)); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static SigningInfo fromByteBuffer(ByteBuffer buffer) throws IOException { |
| 109 |
byte[] apkDigest = readBytes(buffer); |
| 110 |
byte[] certificate = readBytes(buffer); |
| 111 |
byte[] additionalData = readBytes(buffer); |
| 112 |
byte[] publicKey = readBytes(buffer); |
| 113 |
int signatureAlgorithmId = buffer.getInt(); |
| 114 |
byte[] signature = readBytes(buffer); |
| 115 |
return new SigningInfo(apkDigest, certificate, additionalData, publicKey, |
| 116 |
signatureAlgorithmId, signature); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static class SigningInfoBlock { |
| 124 |
public final int blockId; |
| 125 |
public final byte[] signingInfo; |
| 126 |
|
| 127 |
public SigningInfoBlock(int blockId, byte[] signingInfo) { |
| 128 |
this.blockId = blockId; |
| 129 |
this.signingInfo = signingInfo; |
| 130 |
} |
| 131 |
|
| 132 |
static SigningInfoBlock fromByteBuffer(ByteBuffer buffer) throws IOException { |
| 133 |
int blockId = buffer.getInt(); |
| 134 |
byte[] signingInfo = readBytes(buffer); |
| 135 |
return new SigningInfoBlock(blockId, signingInfo); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static class SigningInfos { |
| 143 |
|
| 144 |
public final SigningInfo signingInfo; |
| 145 |
|
| 146 |
public final SigningInfoBlock[] signingInfoBlocks; |
| 147 |
|
| 148 |
public SigningInfos(SigningInfo signingInfo) { |
| 149 |
this.signingInfo = signingInfo; |
| 150 |
this.signingInfoBlocks = new SigningInfoBlock[0]; |
| 151 |
} |
| 152 |
|
| 153 |
public SigningInfos(SigningInfo signingInfo, SigningInfoBlock... signingInfoBlocks) { |
| 154 |
this.signingInfo = signingInfo; |
| 155 |
this.signingInfoBlocks = signingInfoBlocks; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static SigningInfos fromByteArray(byte[] bytes) throws IOException { |
| 162 |
ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); |
| 163 |
SigningInfo signingInfo = SigningInfo.fromByteBuffer(buffer); |
| 164 |
if (!buffer.hasRemaining()) { |
| 165 |
return new SigningInfos(signingInfo); |
| 166 |
} |
| 167 |
ArrayList<SigningInfoBlock> signingInfoBlocks = new ArrayList<>(1); |
| 168 |
while (buffer.hasRemaining()) { |
| 169 |
signingInfoBlocks.add(SigningInfoBlock.fromByteBuffer(buffer)); |
| 170 |
} |
| 171 |
return new SigningInfos(signingInfo, |
| 172 |
signingInfoBlocks.toArray(new SigningInfoBlock[signingInfoBlocks.size()])); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
public final int version; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
@Nullable public final byte[] hashingInfo; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
@Nullable public final byte[] signingInfos; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public static V4Signature readFrom(ParcelFileDescriptor pfd) throws IOException { |
| 194 |
try (InputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(pfd.dup())) { |
| 195 |
return readFrom(stream); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
@NonNull |
| 203 |
public static V4Signature readFrom(@NonNull byte[] bytes) throws IOException { |
| 204 |
try (InputStream stream = new ByteArrayInputStream(bytes)) { |
| 205 |
return readFrom(stream); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public byte[] toByteArray() { |
| 213 |
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { |
| 214 |
this.writeTo(stream); |
| 215 |
return stream.toByteArray(); |
| 216 |
} catch (IOException e) { |
| 217 |
return null; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public static byte[] getSignedData(long fileSize, HashingInfo hashingInfo, |
| 228 |
SigningInfo signingInfo) { |
| 229 |
final int size = |
| 230 |
4 + 8 + 4 + 1 + bytesSize( |
| 231 |
hashingInfo.salt) + bytesSize(hashingInfo.rawRootHash) + bytesSize( |
| 232 |
signingInfo.apkDigest) + bytesSize(signingInfo.certificate) + bytesSize( |
| 233 |
signingInfo.additionalData); |
| 234 |
ByteBuffer buffer = ByteBuffer.allocate(size).order(ByteOrder.LITTLE_ENDIAN); |
| 235 |
buffer.putInt(size); |
| 236 |
buffer.putLong(fileSize); |
| 237 |
buffer.putInt(hashingInfo.hashAlgorithm); |
| 238 |
buffer.put(hashingInfo.log2BlockSize); |
| 239 |
writeBytes(buffer, hashingInfo.salt); |
| 240 |
writeBytes(buffer, hashingInfo.rawRootHash); |
| 241 |
writeBytes(buffer, signingInfo.apkDigest); |
| 242 |
writeBytes(buffer, signingInfo.certificate); |
| 243 |
writeBytes(buffer, signingInfo.additionalData); |
| 244 |
return buffer.array(); |
| 245 |
} |
| 246 |
|
| 247 |
public boolean isVersionSupported() { |
| 248 |
return this.version == SUPPORTED_VERSION; |
| 249 |
} |
| 250 |
|
| 251 |
private V4Signature(int version, @Nullable byte[] hashingInfo, @Nullable byte[] signingInfos) { |
| 252 |
this.version = version; |
| 253 |
this.hashingInfo = hashingInfo; |
| 254 |
this.signingInfos = signingInfos; |
| 255 |
} |
| 256 |
|
| 257 |
private static V4Signature readFrom(InputStream stream) throws IOException { |
| 258 |
final int version = readIntLE(stream); |
| 259 |
int maxSize = INCFS_MAX_SIGNATURE_SIZE; |
| 260 |
final byte[] hashingInfo = readBytes(stream, maxSize); |
| 261 |
if (hashingInfo != null) { |
| 262 |
maxSize -= hashingInfo.length; |
| 263 |
} |
| 264 |
final byte[] signingInfo = readBytes(stream, maxSize); |
| 265 |
return new V4Signature(version, hashingInfo, signingInfo); |
| 266 |
} |
| 267 |
|
| 268 |
private void writeTo(OutputStream stream) throws IOException { |
| 269 |
writeIntLE(stream, this.version); |
| 270 |
writeBytes(stream, this.hashingInfo); |
| 271 |
writeBytes(stream, this.signingInfos); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
private static int bytesSize(byte[] bytes) { |
| 276 |
return 4 + (bytes == null ? 0 : bytes.length); |
| 277 |
} |
| 278 |
|
| 279 |
private static void readFully(InputStream stream, byte[] buffer) throws IOException { |
| 280 |
int len = buffer.length; |
| 281 |
int n = 0; |
| 282 |
while (n < len) { |
| 283 |
int count = stream.read(buffer, n, len - n); |
| 284 |
if (count < 0) { |
| 285 |
throw new EOFException(); |
| 286 |
} |
| 287 |
n += count; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
private static int readIntLE(InputStream stream) throws IOException { |
| 292 |
final byte[] buffer = new byte[4]; |
| 293 |
readFully(stream, buffer); |
| 294 |
return ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); |
| 295 |
} |
| 296 |
|
| 297 |
private static void writeIntLE(OutputStream stream, int v) throws IOException { |
| 298 |
final byte[] buffer = ByteBuffer.wrap(new byte[4]).order(ByteOrder.LITTLE_ENDIAN).putInt( |
| 299 |
v).array(); |
| 300 |
stream.write(buffer); |
| 301 |
} |
| 302 |
|
| 303 |
private static byte[] readBytes(InputStream stream, int maxSize) throws IOException { |
| 304 |
try { |
| 305 |
final int size = readIntLE(stream); |
| 306 |
if (size > maxSize) { |
| 307 |
throw new IOException( |
| 308 |
"Signature is too long. Max allowed is " + INCFS_MAX_SIGNATURE_SIZE); |
| 309 |
} |
| 310 |
final byte[] bytes = new byte[size]; |
| 311 |
readFully(stream, bytes); |
| 312 |
return bytes; |
| 313 |
} catch (EOFException ignored) { |
| 314 |
return null; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
private static byte[] readBytes(ByteBuffer buffer) throws IOException { |
| 319 |
if (buffer.remaining() < 4) { |
| 320 |
throw new EOFException(); |
| 321 |
} |
| 322 |
final int size = buffer.getInt(); |
| 323 |
if (buffer.remaining() < size) { |
| 324 |
throw new EOFException(); |
| 325 |
} |
| 326 |
final byte[] bytes = new byte[size]; |
| 327 |
buffer.get(bytes); |
| 328 |
return bytes; |
| 329 |
} |
| 330 |
|
| 331 |
private static void writeBytes(OutputStream stream, byte[] bytes) throws IOException { |
| 332 |
if (bytes == null) { |
| 333 |
writeIntLE(stream, 0); |
| 334 |
return; |
| 335 |
} |
| 336 |
writeIntLE(stream, bytes.length); |
| 337 |
stream.write(bytes); |
| 338 |
} |
| 339 |
|
| 340 |
private static void writeBytes(ByteBuffer buffer, byte[] bytes) { |
| 341 |
if (bytes == null) { |
| 342 |
buffer.putInt(0); |
| 343 |
return; |
| 344 |
} |
| 345 |
buffer.putInt(bytes.length); |
| 346 |
buffer.put(bytes); |
| 347 |
} |
| 348 |
} |