AtmobApkUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package com.atmob.channelreader;
  2. import java.io.IOException;
  3. import java.nio.BufferUnderflowException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;
  6. import java.nio.channels.FileChannel;
  7. import java.util.LinkedHashMap;
  8. import java.util.Map;
  9. final class AtmobApkUtil {
  10. private AtmobApkUtil() {
  11. super();
  12. }
  13. /**
  14. * APK Signing Block Magic Code: magic “APK Sig Block 42” (16 bytes)
  15. * "APK Sig Block 42" : 41 50 4B 20 53 69 67 20 42 6C 6F 63 6B 20 34 32
  16. */
  17. public static final long APK_SIG_BLOCK_MAGIC_HI = 0x3234206b636f6c42L; // LITTLE_ENDIAN, High
  18. public static final long APK_SIG_BLOCK_MAGIC_LO = 0x20676953204b5041L; // LITTLE_ENDIAN, Low
  19. private static final int APK_SIG_BLOCK_MIN_SIZE = 32;
  20. /*
  21. The v2 signature of the APK is stored as an ID-value pair with ID 0x7109871a
  22. (https://source.android.com/security/apksigning/v2.html#apk-signing-block)
  23. */
  24. public static final int APK_SIGNATURE_SCHEME_V2_BLOCK_ID = 0x7109871a;
  25. /**
  26. * The padding in APK SIG BLOCK (V3 scheme introduced)
  27. * See https://android.googlesource.com/platform/tools/apksig/+/master/src/main/java/com/android/apksig/internal/apk/ApkSigningBlockUtils.java
  28. */
  29. public static final int VERITY_PADDING_BLOCK_ID = 0x42726577;
  30. public static final int ANDROID_COMMON_PAGE_ALIGNMENT_BYTES = 4096;
  31. // Our Channel Block ID
  32. public static final int APK_CHANNEL_BLOCK_ID = 0x71777777;
  33. public static final String DEFAULT_CHARSET = "UTF-8";
  34. private static final int ZIP_EOCD_REC_MIN_SIZE = 22;
  35. private static final int ZIP_EOCD_REC_SIG = 0x06054b50;
  36. private static final int UINT16_MAX_VALUE = 0xffff;
  37. private static final int ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET = 20;
  38. public static long getCommentLength(final FileChannel fileChannel) throws IOException {
  39. // End of central directory record (EOCD)
  40. // Offset Bytes Description[23]
  41. // 0 4 End of central directory signature = 0x06054b50
  42. // 4 2 Number of this disk
  43. // 6 2 Disk where central directory starts
  44. // 8 2 Number of central directory records on this disk
  45. // 10 2 Total number of central directory records
  46. // 12 4 Size of central directory (bytes)
  47. // 16 4 Offset of start of central directory, relative to start of archive
  48. // 20 2 Comment length (n)
  49. // 22 n Comment
  50. // For a zip with no archive comment, the
  51. // end-of-central-directory record will be 22 bytes long, so
  52. // we expect to find the EOCD marker 22 bytes from the end.
  53. final long archiveSize = fileChannel.size();
  54. if (archiveSize < ZIP_EOCD_REC_MIN_SIZE) {
  55. throw new IOException("APK too small for ZIP End of Central Directory (EOCD) record");
  56. }
  57. // ZIP End of Central Directory (EOCD) record is located at the very end of the ZIP archive.
  58. // The record can be identified by its 4-byte signature/magic which is located at the very
  59. // beginning of the record. A complication is that the record is variable-length because of
  60. // the comment field.
  61. // The algorithm for locating the ZIP EOCD record is as follows. We search backwards from
  62. // end of the buffer for the EOCD record signature. Whenever we find a signature, we check
  63. // the candidate record's comment length is such that the remainder of the record takes up
  64. // exactly the remaining bytes in the buffer. The search is bounded because the maximum
  65. // size of the comment field is 65535 bytes because the field is an unsigned 16-bit number.
  66. final long maxCommentLength = Math.min(archiveSize - ZIP_EOCD_REC_MIN_SIZE, UINT16_MAX_VALUE);
  67. final long eocdWithEmptyCommentStartPosition = archiveSize - ZIP_EOCD_REC_MIN_SIZE;
  68. for (int expectedCommentLength = 0; expectedCommentLength <= maxCommentLength;
  69. expectedCommentLength++) {
  70. final long eocdStartPos = eocdWithEmptyCommentStartPosition - expectedCommentLength;
  71. final ByteBuffer byteBuffer = ByteBuffer.allocate(4);
  72. fileChannel.position(eocdStartPos);
  73. fileChannel.read(byteBuffer);
  74. byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
  75. if (byteBuffer.getInt(0) == ZIP_EOCD_REC_SIG) {
  76. final ByteBuffer commentLengthByteBuffer = ByteBuffer.allocate(2);
  77. fileChannel.position(eocdStartPos + ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET);
  78. fileChannel.read(commentLengthByteBuffer);
  79. commentLengthByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
  80. final int actualCommentLength = commentLengthByteBuffer.getShort(0);
  81. if (actualCommentLength == expectedCommentLength) {
  82. return actualCommentLength;
  83. }
  84. }
  85. }
  86. throw new IOException("ZIP End of Central Directory (EOCD) record not found");
  87. }
  88. public static long findCentralDirStartOffset(final FileChannel fileChannel) throws IOException {
  89. return findCentralDirStartOffset(fileChannel, getCommentLength(fileChannel));
  90. }
  91. public static long findCentralDirStartOffset(final FileChannel fileChannel, final long commentLength) throws IOException {
  92. // End of central directory record (EOCD)
  93. // Offset Bytes Description[23]
  94. // 0 4 End of central directory signature = 0x06054b50
  95. // 4 2 Number of this disk
  96. // 6 2 Disk where central directory starts
  97. // 8 2 Number of central directory records on this disk
  98. // 10 2 Total number of central directory records
  99. // 12 4 Size of central directory (bytes)
  100. // 16 4 Offset of start of central directory, relative to start of archive
  101. // 20 2 Comment length (n)
  102. // 22 n Comment
  103. // For a zip with no archive comment, the
  104. // end-of-central-directory record will be 22 bytes long, so
  105. // we expect to find the EOCD marker 22 bytes from the end.
  106. final ByteBuffer zipCentralDirectoryStart = ByteBuffer.allocate(4);
  107. zipCentralDirectoryStart.order(ByteOrder.LITTLE_ENDIAN);
  108. fileChannel.position(fileChannel.size() - commentLength - 6); // 6 = 2 (Comment length) + 4 (Offset of start of central directory, relative to start of archive)
  109. fileChannel.read(zipCentralDirectoryStart);
  110. final long centralDirStartOffset = zipCentralDirectoryStart.getInt(0);
  111. return centralDirStartOffset;
  112. }
  113. public static Pair<ByteBuffer, Long> findApkSigningBlock(
  114. final FileChannel fileChannel) throws IOException, SignatureNotFoundException {
  115. final long centralDirOffset = findCentralDirStartOffset(fileChannel);
  116. return findApkSigningBlock(fileChannel, centralDirOffset);
  117. }
  118. public static Pair<ByteBuffer, Long> findApkSigningBlock(
  119. final FileChannel fileChannel, final long centralDirOffset) throws IOException, SignatureNotFoundException {
  120. // Find the APK Signing Block. The block immediately precedes the Central Directory.
  121. // FORMAT:
  122. // OFFSET DATA TYPE DESCRIPTION
  123. // * @+0 bytes uint64: size in bytes (excluding this field)
  124. // * @+8 bytes payload
  125. // * @-24 bytes uint64: size in bytes (same as the one above)
  126. // * @-16 bytes uint128: magic
  127. if (centralDirOffset < APK_SIG_BLOCK_MIN_SIZE) {
  128. throw new SignatureNotFoundException(
  129. "APK too small for APK Signing Block. ZIP Central Directory offset: "
  130. + centralDirOffset);
  131. }
  132. // Read the magic and offset in file from the footer section of the block:
  133. // * uint64: size of block
  134. // * 16 bytes: magic
  135. fileChannel.position(centralDirOffset - 24);
  136. final ByteBuffer footer = ByteBuffer.allocate(24);
  137. fileChannel.read(footer);
  138. footer.order(ByteOrder.LITTLE_ENDIAN);
  139. if ((footer.getLong(8) != APK_SIG_BLOCK_MAGIC_LO)
  140. || (footer.getLong(16) != APK_SIG_BLOCK_MAGIC_HI)) {
  141. throw new SignatureNotFoundException(
  142. "No APK Signing Block before ZIP Central Directory");
  143. }
  144. // Read and compare size fields
  145. final long apkSigBlockSizeInFooter = footer.getLong(0);
  146. if ((apkSigBlockSizeInFooter < footer.capacity())
  147. || (apkSigBlockSizeInFooter > Integer.MAX_VALUE - 8)) {
  148. throw new SignatureNotFoundException(
  149. "APK Signing Block size out of range: " + apkSigBlockSizeInFooter);
  150. }
  151. final int totalSize = (int) (apkSigBlockSizeInFooter + 8);
  152. final long apkSigBlockOffset = centralDirOffset - totalSize;
  153. if (apkSigBlockOffset < 0) {
  154. throw new SignatureNotFoundException(
  155. "APK Signing Block offset out of range: " + apkSigBlockOffset);
  156. }
  157. fileChannel.position(apkSigBlockOffset);
  158. final ByteBuffer apkSigBlock = ByteBuffer.allocate(totalSize);
  159. fileChannel.read(apkSigBlock);
  160. apkSigBlock.order(ByteOrder.LITTLE_ENDIAN);
  161. final long apkSigBlockSizeInHeader = apkSigBlock.getLong(0);
  162. if (apkSigBlockSizeInHeader != apkSigBlockSizeInFooter) {
  163. throw new SignatureNotFoundException(
  164. "APK Signing Block sizes in header and footer do not match: "
  165. + apkSigBlockSizeInHeader + " vs " + apkSigBlockSizeInFooter);
  166. }
  167. return Pair.of(apkSigBlock, apkSigBlockOffset);
  168. }
  169. public static Map<Integer, ByteBuffer> findIdValues(final ByteBuffer apkSigningBlock) throws SignatureNotFoundException {
  170. checkByteOrderLittleEndian(apkSigningBlock);
  171. // FORMAT:
  172. // OFFSET DATA TYPE DESCRIPTION
  173. // * @+0 bytes uint64: size in bytes (excluding this field)
  174. // * @+8 bytes pairs
  175. // * @-24 bytes uint64: size in bytes (same as the one above)
  176. // * @-16 bytes uint128: magic
  177. final ByteBuffer pairs = sliceFromTo(apkSigningBlock, 8, apkSigningBlock.capacity() - 24);
  178. final Map<Integer, ByteBuffer> idValues = new LinkedHashMap<Integer, ByteBuffer>(); // keep order
  179. int entryCount = 0;
  180. while (pairs.hasRemaining()) {
  181. entryCount++;
  182. if (pairs.remaining() < 8) {
  183. throw new SignatureNotFoundException(
  184. "Insufficient data to read size of APK Signing Block entry #" + entryCount);
  185. }
  186. final long lenLong = pairs.getLong();
  187. if ((lenLong < 4) || (lenLong > Integer.MAX_VALUE)) {
  188. throw new SignatureNotFoundException(
  189. "APK Signing Block entry #" + entryCount
  190. + " size out of range: " + lenLong);
  191. }
  192. final int len = (int) lenLong;
  193. final int nextEntryPos = pairs.position() + len;
  194. if (len > pairs.remaining()) {
  195. throw new SignatureNotFoundException(
  196. "APK Signing Block entry #" + entryCount + " size out of range: " + len
  197. + ", available: " + pairs.remaining());
  198. }
  199. final int id = pairs.getInt();
  200. idValues.put(id, getByteBuffer(pairs, len - 4));
  201. pairs.position(nextEntryPos);
  202. }
  203. return idValues;
  204. }
  205. /**
  206. * Returns new byte buffer whose content is a shared subsequence of this buffer's content
  207. * between the specified start (inclusive) and end (exclusive) positions. As opposed to
  208. * {@link ByteBuffer#slice()}, the returned buffer's byte order is the same as the source
  209. * buffer's byte order.
  210. */
  211. private static ByteBuffer sliceFromTo(final ByteBuffer source, final int start, final int end) {
  212. if (start < 0) {
  213. throw new IllegalArgumentException("start: " + start);
  214. }
  215. if (end < start) {
  216. throw new IllegalArgumentException("end < start: " + end + " < " + start);
  217. }
  218. final int capacity = source.capacity();
  219. if (end > source.capacity()) {
  220. throw new IllegalArgumentException("end > capacity: " + end + " > " + capacity);
  221. }
  222. final int originalLimit = source.limit();
  223. final int originalPosition = source.position();
  224. try {
  225. source.position(0);
  226. source.limit(end);
  227. source.position(start);
  228. final ByteBuffer result = source.slice();
  229. result.order(source.order());
  230. return result;
  231. } finally {
  232. source.position(0);
  233. source.limit(originalLimit);
  234. source.position(originalPosition);
  235. }
  236. }
  237. /**
  238. * Relative <em>get</em> method for reading {@code size} number of bytes from the current
  239. * position of this buffer.
  240. * <p>
  241. * <p>This method reads the next {@code size} bytes at this buffer's current position,
  242. * returning them as a {@code ByteBuffer} with start set to 0, limit and capacity set to
  243. * {@code size}, byte order set to this buffer's byte order; and then increments the position by
  244. * {@code size}.
  245. */
  246. private static ByteBuffer getByteBuffer(final ByteBuffer source, final int size)
  247. throws BufferUnderflowException {
  248. if (size < 0) {
  249. throw new IllegalArgumentException("size: " + size);
  250. }
  251. final int originalLimit = source.limit();
  252. final int position = source.position();
  253. final int limit = position + size;
  254. if ((limit < position) || (limit > originalLimit)) {
  255. throw new BufferUnderflowException();
  256. }
  257. source.limit(limit);
  258. try {
  259. final ByteBuffer result = source.slice();
  260. result.order(source.order());
  261. source.position(limit);
  262. return result;
  263. } finally {
  264. source.limit(originalLimit);
  265. }
  266. }
  267. private static void checkByteOrderLittleEndian(final ByteBuffer buffer) {
  268. if (buffer.order() != ByteOrder.LITTLE_ENDIAN) {
  269. throw new IllegalArgumentException("ByteBuffer byte order must be little endian");
  270. }
  271. }
  272. }