| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util.apk; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
import java.io.RandomAccessFile; |
| 24 |
import java.nio.ByteBuffer; |
| 25 |
import java.nio.ByteOrder; |
| 26 |
import java.security.DigestException; |
| 27 |
import java.security.MessageDigest; |
| 28 |
import java.security.NoSuchAlgorithmException; |
| 29 |
import java.util.ArrayList; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public abstract class VerityBuilder { |
| 43 |
private VerityBuilder() {} |
| 44 |
|
| 45 |
private static final int CHUNK_SIZE_BYTES = 4096; |
| 46 |
private static final int DIGEST_SIZE_BYTES = 32; |
| 47 |
private static final int FSVERITY_HEADER_SIZE_BYTES = 64; |
| 48 |
private static final int ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE = 4; |
| 49 |
private static final int ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET = 16; |
| 50 |
private static final String JCA_DIGEST_ALGORITHM = "SHA-256"; |
| 51 |
private static final byte[] DEFAULT_SALT = new byte[8]; |
| 52 |
|
| 53 |
|
| 54 |
public static class VerityResult { |
| 55 |
|
| 56 |
public final ByteBuffer verityData; |
| 57 |
|
| 58 |
|
| 59 |
public final int merkleTreeSize; |
| 60 |
|
| 61 |
|
| 62 |
public final byte[] rootHash; |
| 63 |
|
| 64 |
private VerityResult(ByteBuffer verityData, int merkleTreeSize, byte[] rootHash) { |
| 65 |
this.verityData = verityData; |
| 66 |
this.merkleTreeSize = merkleTreeSize; |
| 67 |
this.rootHash = rootHash; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
@NonNull |
| 81 |
public static VerityResult generateApkVerityTree(@NonNull RandomAccessFile apk, |
| 82 |
@Nullable SignatureInfo signatureInfo, @NonNull ByteBufferFactory bufferFactory) |
| 83 |
throws IOException, SecurityException, NoSuchAlgorithmException, DigestException { |
| 84 |
return generateVerityTreeInternal(apk, bufferFactory, signatureInfo); |
| 85 |
} |
| 86 |
|
| 87 |
@NonNull |
| 88 |
private static VerityResult generateVerityTreeInternal(@NonNull RandomAccessFile apk, |
| 89 |
@NonNull ByteBufferFactory bufferFactory, @Nullable SignatureInfo signatureInfo) |
| 90 |
throws IOException, SecurityException, NoSuchAlgorithmException, DigestException { |
| 91 |
long signingBlockSize = |
| 92 |
signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset; |
| 93 |
long dataSize = apk.getChannel().size() - signingBlockSize; |
| 94 |
int[] levelOffset = calculateVerityLevelOffset(dataSize); |
| 95 |
int merkleTreeSize = levelOffset[levelOffset.length - 1]; |
| 96 |
|
| 97 |
ByteBuffer output = bufferFactory.create( |
| 98 |
merkleTreeSize |
| 99 |
+ CHUNK_SIZE_BYTES); |
| 100 |
output.order(ByteOrder.LITTLE_ENDIAN); |
| 101 |
ByteBuffer tree = slice(output, 0, merkleTreeSize); |
| 102 |
byte[] apkRootHash = generateVerityTreeInternal(apk, signatureInfo, DEFAULT_SALT, |
| 103 |
levelOffset, tree); |
| 104 |
return new VerityResult(output, merkleTreeSize, apkRootHash); |
| 105 |
} |
| 106 |
|
| 107 |
static void generateApkVerityFooter(@NonNull RandomAccessFile apk, |
| 108 |
@NonNull SignatureInfo signatureInfo, @NonNull ByteBuffer footerOutput) |
| 109 |
throws IOException { |
| 110 |
footerOutput.order(ByteOrder.LITTLE_ENDIAN); |
| 111 |
generateApkVerityHeader(footerOutput, apk.getChannel().size(), DEFAULT_SALT); |
| 112 |
long signingBlockSize = |
| 113 |
signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset; |
| 114 |
generateApkVerityExtensions(footerOutput, signatureInfo.apkSigningBlockOffset, |
| 115 |
signingBlockSize, signatureInfo.eocdOffset); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static byte[] generateFsVerityRootHash(@NonNull String apkPath, byte[] salt, |
| 132 |
@NonNull ByteBufferFactory bufferFactory) |
| 133 |
throws IOException, NoSuchAlgorithmException, DigestException { |
| 134 |
try (RandomAccessFile apk = new RandomAccessFile(apkPath, "r")) { |
| 135 |
int[] levelOffset = calculateVerityLevelOffset(apk.length()); |
| 136 |
int merkleTreeSize = levelOffset[levelOffset.length - 1]; |
| 137 |
|
| 138 |
ByteBuffer output = bufferFactory.create( |
| 139 |
merkleTreeSize |
| 140 |
+ CHUNK_SIZE_BYTES); |
| 141 |
output.order(ByteOrder.LITTLE_ENDIAN); |
| 142 |
ByteBuffer tree = slice(output, 0, merkleTreeSize); |
| 143 |
return generateFsVerityTreeInternal(apk, salt, levelOffset, tree); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
@NonNull |
| 157 |
static byte[] generateApkVerity(@NonNull String apkPath, |
| 158 |
@NonNull ByteBufferFactory bufferFactory, @NonNull SignatureInfo signatureInfo) |
| 159 |
throws IOException, SignatureNotFoundException, SecurityException, DigestException, |
| 160 |
NoSuchAlgorithmException { |
| 161 |
try (RandomAccessFile apk = new RandomAccessFile(apkPath, "r")) { |
| 162 |
VerityResult result = generateVerityTreeInternal(apk, bufferFactory, signatureInfo); |
| 163 |
ByteBuffer footer = slice(result.verityData, result.merkleTreeSize, |
| 164 |
result.verityData.limit()); |
| 165 |
generateApkVerityFooter(apk, signatureInfo, footer); |
| 166 |
|
| 167 |
footer.putInt(footer.position() + 4); |
| 168 |
result.verityData.limit(result.merkleTreeSize + footer.position()); |
| 169 |
return result.rootHash; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
private static class BufferedDigester implements DataDigester { |
| 177 |
|
| 178 |
private static final int BUFFER_SIZE = CHUNK_SIZE_BYTES; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
private int mBytesDigestedSinceReset; |
| 186 |
|
| 187 |
|
| 188 |
private final ByteBuffer mOutput; |
| 189 |
|
| 190 |
private final MessageDigest mMd; |
| 191 |
private final byte[] mDigestBuffer = new byte[DIGEST_SIZE_BYTES]; |
| 192 |
private final byte[] mSalt; |
| 193 |
|
| 194 |
private BufferedDigester(@Nullable byte[] salt, @NonNull ByteBuffer output) |
| 195 |
throws NoSuchAlgorithmException { |
| 196 |
mSalt = salt; |
| 197 |
mOutput = output.slice(); |
| 198 |
mMd = MessageDigest.getInstance(JCA_DIGEST_ALGORITHM); |
| 199 |
if (mSalt != null) { |
| 200 |
mMd.update(mSalt); |
| 201 |
} |
| 202 |
mBytesDigestedSinceReset = 0; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
@Override |
| 212 |
public void consume(ByteBuffer buffer) throws DigestException { |
| 213 |
int offset = buffer.position(); |
| 214 |
int remaining = buffer.remaining(); |
| 215 |
while (remaining > 0) { |
| 216 |
int allowance = (int) Math.min(remaining, BUFFER_SIZE - mBytesDigestedSinceReset); |
| 217 |
|
| 218 |
buffer.limit(buffer.position() + allowance); |
| 219 |
mMd.update(buffer); |
| 220 |
offset += allowance; |
| 221 |
remaining -= allowance; |
| 222 |
mBytesDigestedSinceReset += allowance; |
| 223 |
|
| 224 |
if (mBytesDigestedSinceReset == BUFFER_SIZE) { |
| 225 |
mMd.digest(mDigestBuffer, 0, mDigestBuffer.length); |
| 226 |
mOutput.put(mDigestBuffer); |
| 227 |
|
| 228 |
if (mSalt != null) { |
| 229 |
mMd.update(mSalt); |
| 230 |
} |
| 231 |
mBytesDigestedSinceReset = 0; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
public void assertEmptyBuffer() throws DigestException { |
| 237 |
if (mBytesDigestedSinceReset != 0) { |
| 238 |
throw new IllegalStateException("Buffer is not empty: " + mBytesDigestedSinceReset); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
private void fillUpLastOutputChunk() { |
| 243 |
int lastBlockSize = (int) (mOutput.position() % BUFFER_SIZE); |
| 244 |
if (lastBlockSize == 0) { |
| 245 |
return; |
| 246 |
} |
| 247 |
mOutput.put(ByteBuffer.allocate(BUFFER_SIZE - lastBlockSize)); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private static void consumeByChunk(DataDigester digester, DataSource source, int chunkSize) |
| 256 |
throws IOException, DigestException { |
| 257 |
long inputRemaining = source.size(); |
| 258 |
long inputOffset = 0; |
| 259 |
while (inputRemaining > 0) { |
| 260 |
int size = (int) Math.min(inputRemaining, chunkSize); |
| 261 |
source.feedIntoDataDigester(digester, inputOffset, size); |
| 262 |
inputOffset += size; |
| 263 |
inputRemaining -= size; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private static final int MMAP_REGION_SIZE_BYTES = 1024 * 1024; |
| 270 |
|
| 271 |
private static void generateFsVerityDigestAtLeafLevel(RandomAccessFile file, |
| 272 |
@Nullable byte[] salt, ByteBuffer output) |
| 273 |
throws IOException, NoSuchAlgorithmException, DigestException { |
| 274 |
BufferedDigester digester = new BufferedDigester(salt, output); |
| 275 |
|
| 276 |
|
| 277 |
consumeByChunk(digester, |
| 278 |
DataSource.create(file.getFD(), 0, file.length()), |
| 279 |
MMAP_REGION_SIZE_BYTES); |
| 280 |
|
| 281 |
|
| 282 |
int lastIncompleteChunkSize = (int) (file.length() % CHUNK_SIZE_BYTES); |
| 283 |
if (lastIncompleteChunkSize != 0) { |
| 284 |
digester.consume(ByteBuffer.allocate(CHUNK_SIZE_BYTES - lastIncompleteChunkSize)); |
| 285 |
} |
| 286 |
digester.assertEmptyBuffer(); |
| 287 |
|
| 288 |
|
| 289 |
digester.fillUpLastOutputChunk(); |
| 290 |
} |
| 291 |
|
| 292 |
private static void generateApkVerityDigestAtLeafLevel(RandomAccessFile apk, |
| 293 |
SignatureInfo signatureInfo, byte[] salt, ByteBuffer output) |
| 294 |
throws IOException, NoSuchAlgorithmException, DigestException { |
| 295 |
BufferedDigester digester = new BufferedDigester(salt, output); |
| 296 |
|
| 297 |
|
| 298 |
consumeByChunk(digester, |
| 299 |
DataSource.create(apk.getFD(), 0, signatureInfo.apkSigningBlockOffset), |
| 300 |
MMAP_REGION_SIZE_BYTES); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
long eocdCdOffsetFieldPosition = |
| 305 |
signatureInfo.eocdOffset + ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET; |
| 306 |
consumeByChunk(digester, |
| 307 |
DataSource.create(apk.getFD(), signatureInfo.centralDirOffset, |
| 308 |
eocdCdOffsetFieldPosition - signatureInfo.centralDirOffset), |
| 309 |
MMAP_REGION_SIZE_BYTES); |
| 310 |
|
| 311 |
|
| 312 |
ByteBuffer alternativeCentralDirOffset = ByteBuffer.allocate( |
| 313 |
ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE).order(ByteOrder.LITTLE_ENDIAN); |
| 314 |
alternativeCentralDirOffset.putInt(Math.toIntExact(signatureInfo.apkSigningBlockOffset)); |
| 315 |
alternativeCentralDirOffset.flip(); |
| 316 |
digester.consume(alternativeCentralDirOffset); |
| 317 |
|
| 318 |
|
| 319 |
long offsetAfterEocdCdOffsetField = |
| 320 |
eocdCdOffsetFieldPosition + ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE; |
| 321 |
consumeByChunk(digester, |
| 322 |
DataSource.create(apk.getFD(), offsetAfterEocdCdOffsetField, |
| 323 |
apk.getChannel().size() - offsetAfterEocdCdOffsetField), |
| 324 |
MMAP_REGION_SIZE_BYTES); |
| 325 |
|
| 326 |
|
| 327 |
int lastIncompleteChunkSize = (int) (apk.getChannel().size() % CHUNK_SIZE_BYTES); |
| 328 |
if (lastIncompleteChunkSize != 0) { |
| 329 |
digester.consume(ByteBuffer.allocate(CHUNK_SIZE_BYTES - lastIncompleteChunkSize)); |
| 330 |
} |
| 331 |
digester.assertEmptyBuffer(); |
| 332 |
|
| 333 |
|
| 334 |
digester.fillUpLastOutputChunk(); |
| 335 |
} |
| 336 |
|
| 337 |
@NonNull |
| 338 |
private static byte[] generateFsVerityTreeInternal(@NonNull RandomAccessFile apk, |
| 339 |
@Nullable byte[] salt, @NonNull int[] levelOffset, @NonNull ByteBuffer output) |
| 340 |
throws IOException, NoSuchAlgorithmException, DigestException { |
| 341 |
|
| 342 |
generateFsVerityDigestAtLeafLevel(apk, salt, |
| 343 |
slice(output, levelOffset[levelOffset.length - 2], |
| 344 |
levelOffset[levelOffset.length - 1])); |
| 345 |
|
| 346 |
|
| 347 |
for (int level = levelOffset.length - 3; level >= 0; level--) { |
| 348 |
ByteBuffer inputBuffer = slice(output, levelOffset[level + 1], levelOffset[level + 2]); |
| 349 |
ByteBuffer outputBuffer = slice(output, levelOffset[level], levelOffset[level + 1]); |
| 350 |
|
| 351 |
DataSource source = new ByteBufferDataSource(inputBuffer); |
| 352 |
BufferedDigester digester = new BufferedDigester(salt, outputBuffer); |
| 353 |
consumeByChunk(digester, source, CHUNK_SIZE_BYTES); |
| 354 |
digester.assertEmptyBuffer(); |
| 355 |
digester.fillUpLastOutputChunk(); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
byte[] rootHash = new byte[DIGEST_SIZE_BYTES]; |
| 360 |
BufferedDigester digester = new BufferedDigester(salt, ByteBuffer.wrap(rootHash)); |
| 361 |
digester.consume(slice(output, 0, CHUNK_SIZE_BYTES)); |
| 362 |
digester.assertEmptyBuffer(); |
| 363 |
return rootHash; |
| 364 |
} |
| 365 |
|
| 366 |
@NonNull |
| 367 |
private static byte[] generateVerityTreeInternal(@NonNull RandomAccessFile apk, |
| 368 |
@Nullable SignatureInfo signatureInfo, @Nullable byte[] salt, |
| 369 |
@NonNull int[] levelOffset, @NonNull ByteBuffer output) |
| 370 |
throws IOException, NoSuchAlgorithmException, DigestException { |
| 371 |
|
| 372 |
assertSigningBlockAlignedAndHasFullPages(signatureInfo); |
| 373 |
generateApkVerityDigestAtLeafLevel(apk, signatureInfo, salt, slice(output, |
| 374 |
levelOffset[levelOffset.length - 2], levelOffset[levelOffset.length - 1])); |
| 375 |
|
| 376 |
|
| 377 |
for (int level = levelOffset.length - 3; level >= 0; level--) { |
| 378 |
ByteBuffer inputBuffer = slice(output, levelOffset[level + 1], levelOffset[level + 2]); |
| 379 |
ByteBuffer outputBuffer = slice(output, levelOffset[level], levelOffset[level + 1]); |
| 380 |
|
| 381 |
DataSource source = new ByteBufferDataSource(inputBuffer); |
| 382 |
BufferedDigester digester = new BufferedDigester(salt, outputBuffer); |
| 383 |
consumeByChunk(digester, source, CHUNK_SIZE_BYTES); |
| 384 |
digester.assertEmptyBuffer(); |
| 385 |
digester.fillUpLastOutputChunk(); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
byte[] rootHash = new byte[DIGEST_SIZE_BYTES]; |
| 390 |
BufferedDigester digester = new BufferedDigester(salt, ByteBuffer.wrap(rootHash)); |
| 391 |
digester.consume(slice(output, 0, CHUNK_SIZE_BYTES)); |
| 392 |
digester.assertEmptyBuffer(); |
| 393 |
return rootHash; |
| 394 |
} |
| 395 |
|
| 396 |
private static ByteBuffer generateApkVerityHeader(ByteBuffer buffer, long fileSize, |
| 397 |
byte[] salt) { |
| 398 |
if (salt.length != 8) { |
| 399 |
throw new IllegalArgumentException("salt is not 8 bytes long"); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
buffer.put("TrueBrew".getBytes()); |
| 404 |
|
| 405 |
buffer.put((byte) 1); |
| 406 |
buffer.put((byte) 0); |
| 407 |
buffer.put((byte) 12); |
| 408 |
buffer.put((byte) 7); |
| 409 |
|
| 410 |
buffer.putShort((short) 1); |
| 411 |
buffer.putShort((short) 1); |
| 412 |
|
| 413 |
buffer.putInt(0); |
| 414 |
buffer.putInt(0); |
| 415 |
|
| 416 |
buffer.putLong(fileSize); |
| 417 |
|
| 418 |
buffer.put((byte) 2); |
| 419 |
buffer.put((byte) 0); |
| 420 |
buffer.put(salt); |
| 421 |
skip(buffer, 22); |
| 422 |
|
| 423 |
return buffer; |
| 424 |
} |
| 425 |
|
| 426 |
private static ByteBuffer generateApkVerityExtensions(ByteBuffer buffer, |
| 427 |
long signingBlockOffset, long signingBlockSize, long eocdOffset) { |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
final int kSizeOfFsverityExtensionHeader = 8; |
| 441 |
final int kExtensionSizeAlignment = 8; |
| 442 |
|
| 443 |
{ |
| 444 |
|
| 445 |
final int kSizeOfFsverityElidedExtension = 16; |
| 446 |
|
| 447 |
|
| 448 |
buffer.putInt(kSizeOfFsverityExtensionHeader + kSizeOfFsverityElidedExtension); |
| 449 |
buffer.putShort((short) 1); |
| 450 |
skip(buffer, 2); |
| 451 |
|
| 452 |
|
| 453 |
buffer.putLong(signingBlockOffset); |
| 454 |
buffer.putLong(signingBlockSize); |
| 455 |
} |
| 456 |
|
| 457 |
{ |
| 458 |
|
| 459 |
final int kTotalSize = kSizeOfFsverityExtensionHeader |
| 460 |
+ 8 |
| 461 |
+ ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE; |
| 462 |
|
| 463 |
buffer.putInt(kTotalSize); |
| 464 |
buffer.putShort((short) 2); |
| 465 |
skip(buffer, 2); |
| 466 |
|
| 467 |
|
| 468 |
buffer.putLong(eocdOffset + ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET); |
| 469 |
buffer.putInt(Math.toIntExact(signingBlockOffset)); |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
int kPadding = kExtensionSizeAlignment - kTotalSize % kExtensionSizeAlignment; |
| 474 |
if (kPadding == kExtensionSizeAlignment) { |
| 475 |
kPadding = 0; |
| 476 |
} |
| 477 |
skip(buffer, kPadding); |
| 478 |
} |
| 479 |
|
| 480 |
return buffer; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
private static int[] calculateVerityLevelOffset(long fileSize) { |
| 490 |
ArrayList<Long> levelSize = new ArrayList<>(); |
| 491 |
while (true) { |
| 492 |
long levelDigestSize = divideRoundup(fileSize, CHUNK_SIZE_BYTES) * DIGEST_SIZE_BYTES; |
| 493 |
long chunksSize = CHUNK_SIZE_BYTES * divideRoundup(levelDigestSize, CHUNK_SIZE_BYTES); |
| 494 |
levelSize.add(chunksSize); |
| 495 |
if (levelDigestSize <= CHUNK_SIZE_BYTES) { |
| 496 |
break; |
| 497 |
} |
| 498 |
fileSize = levelDigestSize; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
int[] levelOffset = new int[levelSize.size() + 1]; |
| 503 |
levelOffset[0] = 0; |
| 504 |
for (int i = 0; i < levelSize.size(); i++) { |
| 505 |
|
| 506 |
levelOffset[i + 1] = levelOffset[i] |
| 507 |
+ Math.toIntExact(levelSize.get(levelSize.size() - i - 1)); |
| 508 |
} |
| 509 |
return levelOffset; |
| 510 |
} |
| 511 |
|
| 512 |
private static void assertSigningBlockAlignedAndHasFullPages( |
| 513 |
@NonNull SignatureInfo signatureInfo) { |
| 514 |
if (signatureInfo.apkSigningBlockOffset % CHUNK_SIZE_BYTES != 0) { |
| 515 |
throw new IllegalArgumentException( |
| 516 |
"APK Signing Block does not start at the page boundary: " |
| 517 |
+ signatureInfo.apkSigningBlockOffset); |
| 518 |
} |
| 519 |
|
| 520 |
if ((signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset) |
| 521 |
% CHUNK_SIZE_BYTES != 0) { |
| 522 |
throw new IllegalArgumentException( |
| 523 |
"Size of APK Signing Block is not a multiple of 4096: " |
| 524 |
+ (signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset)); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
private static ByteBuffer slice(ByteBuffer buffer, int begin, int end) { |
| 530 |
ByteBuffer b = buffer.duplicate(); |
| 531 |
b.position(0); |
| 532 |
b.limit(end); |
| 533 |
b.position(begin); |
| 534 |
return b.slice(); |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
private static void skip(ByteBuffer buffer, int bytes) { |
| 539 |
buffer.position(buffer.position() + bytes); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
private static long divideRoundup(long dividend, long divisor) { |
| 544 |
return (dividend + divisor - 1) / divisor; |
| 545 |
} |
| 546 |
} |