| 6 |
| #include <unistd.h> |
| 7 | #include <stdbool.h> |
| 8 |
| 9 | #ifdef MAIN |
| 10 | #include <stdio.h> |
| 11 | #else |
| 12 |
| 13 | #include "common.h" |
| 14 | #include "openat.h" |
| 15 |
| 16 | #endif |
| 17 |
| 18 | static bool isApkSigBlock42(const char *buffer) { |
| 19 | // APK Sig Block 42 |
| 20 | return *buffer == 'A' |
| 21 | && *++buffer == 'P' |
| 22 | && *++buffer == 'K' |
| 23 | && *++buffer == ' ' |
| 24 | && *++buffer == 'S' |
| 25 | && *++buffer == 'i' |
| 26 | && *++buffer == 'g' |
| 27 | && *++buffer == ' ' |
| 28 | && *++buffer == 'B' |
| 29 | && *++buffer == 'l' |
| 30 | && *++buffer == 'o' |
| 31 | && *++buffer == 'c' |
| 32 | && *++buffer == 'k' |
| 33 | && *++buffer == ' ' |
| 34 | && *++buffer == '4' |
| 35 | && *++buffer == '2'; |
| 36 | } |
| 37 |
| 38 | int checkSignature(const char *path) { |
| 39 | unsigned char buffer[0x11] = {0}; |
| 40 | uint32_t size4; |
| 41 | uint64_t size8, size_of_block; |
| 42 |
| 43 | #ifdef DEBUG |
| 44 | LOGI("check signature for %s", path); |
| 45 | #endif |
| 46 |
| 47 | int sign = -1; |
| 48 | int fd = (int) openAt(AT_FDCWD, path, O_RDONLY); |
| 49 | #ifdef DEBUG_OPENAT |
| 50 | LOGI("openat %s returns %d", path, fd); |
| 51 | #endif |
| 52 | if (fd < 0) { |
| 53 | return sign; |
| 54 | } |
| 55 |
| 56 | sign = 1; |
| 57 | // https://en.wikipedia.org/wiki/Zip_(file_format)#End_of_central_directory_record_(EOCD) |
| 58 | for (int i = 0;; ++i) { |
| 59 | unsigned short n; |
| 60 | lseek(fd, -i - 2, SEEK_END); |
| 61 | read(fd, &n, 2); |
| 62 | if (n == i) { |
| 63 | lseek(fd, -22, SEEK_CUR); |
| 64 | read(fd, &size4, 4); |
| 65 | if ((size4 ^ 0xcafebabeu) == 0xccfbf1eeu) { |
| 66 | #ifdef MAIN |
| 67 | if (i > 0) { |
| 68 | printf("warning: comment length is %d\n", i); |
| 69 | } |
| 70 | #endif |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | if (i == 0xffff) { |
| 75 | #ifdef MAIN |
| 76 | printf("error: cannot find eocd\n"); |
| 77 | #endif |
| 78 | goto clean; |
| 79 | } |
| 80 | } |
| 81 |
| 82 | lseek(fd, 12, SEEK_CUR); |
| 83 | // offset |
| 84 | read(fd, &size4, 0x4); |
| 85 | lseek(fd, (off_t) (size4 - 0x18), SEEK_SET); |
| 86 |
| 87 | read(fd, &size8, 0x8); |
| 88 | read(fd, buffer, 0x10); |
| 89 | if (!isApkSigBlock42((char *) buffer)) { |
| 90 | goto clean; |
| 91 | } |
| 92 |
| 93 | lseek(fd, (off_t) (size4 - (size8 + 0x8)), SEEK_SET); |
| 94 | read(fd, &size_of_block, 0x8); |
| 95 | if (size_of_block != size8) { |
| 96 | goto clean; |
| 97 | } |
| 98 |
| 99 | for (;;) { |
| 100 | uint32_t id; |
| 101 | uint32_t offset; |
| 102 | read(fd, &size8, 0x8); // sequence length |
| 103 | if (size8 == size_of_block) { |
| 104 | break; |
| 105 | } |
| 106 | read(fd, &id, 0x4); // id |
| 107 | offset = 4; |
| 108 | #ifdef MAIN |
| 109 | printf("id: 0x%08x\n", id); |
| 110 | #endif |
| 111 | if ((id ^ 0xdeadbeefu) == 0xafa439f5u || (id ^ 0xdeadbeefu) == 0x2efed62f) { |
| 112 | read(fd, &size4, 0x4); // signer-sequence length |
| 113 | read(fd, &size4, 0x4); // signer length |
| 114 | read(fd, &size4, 0x4); // signed data length |
| 115 | offset += 0x4 * 3; |
| 116 |
| 117 | read(fd, &size4, 0x4); // digests-sequence length |
| 118 | lseek(fd, (off_t) (size4), SEEK_CUR);// skip digests |
| 119 | offset += 0x4 + size4; |
| 120 |
| 121 | read(fd, &size4, 0x4); // certificates length |
| 122 | read(fd, &size4, 0x4); // certificate length |
| 123 | offset += 0x4 * 2; |
| 124 | #ifdef MAIN |
| 125 | int hash = 1; |
| 126 | signed char c; |
| 127 | for (unsigned i = 0; i < size4; ++i) { |
| 128 | read(fd, &c, 0x1); |
| 129 | hash = 31 * hash + c; |
| 130 | } |
| 131 | offset += size4; |
| 132 | printf(" size: 0x%04x, hash: 0x%08x\n", size4, ((unsigned) hash) ^ 0x14131211u); |
| 133 | #else |
| 134 | #if defined(GENUINE_SIZE) && defined(GENUINE_HASH) |
| 135 | if (size4 == GENUINE_SIZE) { |
| 136 | int hash = 1; |
| 137 | signed char c; |
| 138 | for (unsigned i = 0; i < size4; ++i) { |
| 139 | read(fd, &c, 0x1); |
| 140 | hash = 31 * hash + c; |
| 141 | } |
| 142 | offset += size4; |
| 143 | if ((((unsigned) hash) ^ 0x14131211u) == GENUINE_HASH) { |
| 144 | sign = 0; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | #else |
| 149 | sign = 0; |
| 150 | break; |
| 151 | #endif |
| 152 | #endif |
| 153 | } |
| 154 | lseek(fd, (off_t) (size8 - offset), SEEK_CUR); |
| 155 | } |
| 156 |
| 157 | clean: |
| 158 | close(fd); |
| 159 |
| 160 | return sign; |
| 161 | } |
| 162 |
| 163 | #ifdef MAIN |
| 164 | int main(int argc, char **argv) { |
| 165 | if (argc > 1) { |
| 166 | checkSignature(argv[1]); |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 |