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