The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #include "common.h" |
| 18 | #include "verifier.h" |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 19 | #include "ui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 20 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include "mincrypt/rsa.h" |
| 22 | #include "mincrypt/sha.h" |
| 23 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | #include <string.h> |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <errno.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 28 | // Look for an RSA signature embedded in the .ZIP file comment given |
| 29 | // the path to the zip. Verify it matches one of the given public |
| 30 | // keys. |
| 31 | // |
| 32 | // Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered |
| 33 | // or no key matches the signature). |
| 34 | |
| 35 | int verify_file(const char* path, const RSAPublicKey *pKeys, unsigned int numKeys) { |
| 36 | ui_set_progress(0.0); |
| 37 | |
| 38 | FILE* f = fopen(path, "rb"); |
| 39 | if (f == NULL) { |
| 40 | LOGE("failed to open %s (%s)\n", path, strerror(errno)); |
| 41 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 44 | // An archive with a whole-file signature will end in six bytes: |
| 45 | // |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 46 | // (2-byte signature start) $ff $ff (2-byte comment size) |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 47 | // |
| 48 | // (As far as the ZIP format is concerned, these are part of the |
| 49 | // archive comment.) We start by reading this footer, this tells |
| 50 | // us how far back from the end we have to start reading to find |
| 51 | // the whole comment. |
| 52 | |
| 53 | #define FOOTER_SIZE 6 |
| 54 | |
| 55 | if (fseek(f, -FOOTER_SIZE, SEEK_END) != 0) { |
| 56 | LOGE("failed to seek in %s (%s)\n", path, strerror(errno)); |
| 57 | fclose(f); |
| 58 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 61 | unsigned char footer[FOOTER_SIZE]; |
| 62 | if (fread(footer, 1, FOOTER_SIZE, f) != FOOTER_SIZE) { |
| 63 | LOGE("failed to read footer from %s (%s)\n", path, strerror(errno)); |
| 64 | fclose(f); |
| 65 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 68 | if (footer[2] != 0xff || footer[3] != 0xff) { |
| 69 | fclose(f); |
| 70 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 73 | size_t comment_size = footer[4] + (footer[5] << 8); |
| 74 | size_t signature_start = footer[0] + (footer[1] << 8); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 75 | LOGI("comment is %d bytes; signature %d bytes from end\n", |
| 76 | comment_size, signature_start); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 77 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 78 | if (signature_start - FOOTER_SIZE < RSANUMBYTES) { |
| 79 | // "signature" block isn't big enough to contain an RSA block. |
| 80 | LOGE("signature is too short\n"); |
| 81 | fclose(f); |
| 82 | return VERIFY_FAILURE; |
| 83 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 84 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 85 | #define EOCD_HEADER_SIZE 22 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 86 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 87 | // The end-of-central-directory record is 22 bytes plus any |
| 88 | // comment length. |
| 89 | size_t eocd_size = comment_size + EOCD_HEADER_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 90 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 91 | if (fseek(f, -eocd_size, SEEK_END) != 0) { |
| 92 | LOGE("failed to seek in %s (%s)\n", path, strerror(errno)); |
| 93 | fclose(f); |
| 94 | return VERIFY_FAILURE; |
| 95 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 96 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 97 | // Determine how much of the file is covered by the signature. |
| 98 | // This is everything except the signature data and length, which |
| 99 | // includes all of the EOCD except for the comment length field (2 |
| 100 | // bytes) and the comment data. |
| 101 | size_t signed_len = ftell(f) + EOCD_HEADER_SIZE - 2; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 102 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 103 | unsigned char* eocd = (unsigned char*)malloc(eocd_size); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 104 | if (eocd == NULL) { |
| 105 | LOGE("malloc for EOCD record failed\n"); |
| 106 | fclose(f); |
| 107 | return VERIFY_FAILURE; |
| 108 | } |
| 109 | if (fread(eocd, 1, eocd_size, f) != eocd_size) { |
| 110 | LOGE("failed to read eocd from %s (%s)\n", path, strerror(errno)); |
| 111 | fclose(f); |
| 112 | return VERIFY_FAILURE; |
| 113 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 114 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 115 | // If this is really is the EOCD record, it will begin with the |
| 116 | // magic number $50 $4b $05 $06. |
| 117 | if (eocd[0] != 0x50 || eocd[1] != 0x4b || |
| 118 | eocd[2] != 0x05 || eocd[3] != 0x06) { |
| 119 | LOGE("signature length doesn't match EOCD marker\n"); |
| 120 | fclose(f); |
| 121 | return VERIFY_FAILURE; |
| 122 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 123 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 124 | size_t i; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 125 | for (i = 4; i < eocd_size-3; ++i) { |
| 126 | if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && |
Doug Zongker | c652e41 | 2009-12-08 15:30:09 -0800 | [diff] [blame] | 127 | eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 128 | // if the sequence $50 $4b $05 $06 appears anywhere after |
| 129 | // the real one, minzip will find the later (wrong) one, |
| 130 | // which could be exploitable. Fail verification if |
| 131 | // this sequence occurs anywhere after the real one. |
| 132 | LOGE("EOCD marker occurs after start of EOCD\n"); |
| 133 | fclose(f); |
| 134 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 138 | #define BUFFER_SIZE 4096 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 139 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 140 | SHA_CTX ctx; |
| 141 | SHA_init(&ctx); |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 142 | unsigned char* buffer = (unsigned char*)malloc(BUFFER_SIZE); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 143 | if (buffer == NULL) { |
| 144 | LOGE("failed to alloc memory for sha1 buffer\n"); |
| 145 | fclose(f); |
| 146 | return VERIFY_FAILURE; |
| 147 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 148 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 149 | double frac = -1.0; |
| 150 | size_t so_far = 0; |
| 151 | fseek(f, 0, SEEK_SET); |
| 152 | while (so_far < signed_len) { |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 153 | size_t size = BUFFER_SIZE; |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 154 | if (signed_len - so_far < size) size = signed_len - so_far; |
| 155 | if (fread(buffer, 1, size, f) != size) { |
| 156 | LOGE("failed to read data from %s (%s)\n", path, strerror(errno)); |
| 157 | fclose(f); |
| 158 | return VERIFY_FAILURE; |
| 159 | } |
| 160 | SHA_update(&ctx, buffer, size); |
| 161 | so_far += size; |
| 162 | double f = so_far / (double)signed_len; |
| 163 | if (f > frac + 0.02 || size == so_far) { |
| 164 | ui_set_progress(f); |
| 165 | frac = f; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 168 | fclose(f); |
| 169 | free(buffer); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 170 | |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 171 | const uint8_t* sha1 = SHA_final(&ctx); |
| 172 | for (i = 0; i < numKeys; ++i) { |
Doug Zongker | 73ae31c | 2009-12-09 17:01:45 -0800 | [diff] [blame] | 173 | // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 174 | // the signing tool appends after the signature itself. |
| 175 | if (RSA_verify(pKeys+i, eocd + eocd_size - 6 - RSANUMBYTES, |
| 176 | RSANUMBYTES, sha1)) { |
Doug Zongker | 4762633 | 2011-03-15 12:11:08 -0700 | [diff] [blame] | 177 | LOGI("whole-file signature verified against key %d\n", i); |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 178 | free(eocd); |
| 179 | return VERIFY_SUCCESS; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
Doug Zongker | 54e2e86 | 2009-08-17 13:21:04 -0700 | [diff] [blame] | 182 | free(eocd); |
| 183 | LOGE("failed to verify whole-file signature\n"); |
| 184 | return VERIFY_FAILURE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 185 | } |