The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <limits.h> |
| 20 | #include <sys/stat.h> |
| 21 | |
| 22 | #include "amend/amend.h" |
| 23 | #include "common.h" |
| 24 | #include "install.h" |
| 25 | #include "mincrypt/rsa.h" |
| 26 | #include "minui/minui.h" |
| 27 | #include "minzip/SysUtil.h" |
| 28 | #include "minzip/Zip.h" |
| 29 | #include "mtdutils/mounts.h" |
| 30 | #include "mtdutils/mtdutils.h" |
| 31 | #include "roots.h" |
| 32 | #include "verifier.h" |
| 33 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #define ASSUMED_UPDATE_SCRIPT_NAME "META-INF/com/google/android/update-script" |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 35 | #define PUBLIC_KEYS_FILE "/res/keys" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 36 | |
| 37 | static const ZipEntry * |
| 38 | find_update_script(ZipArchive *zip) |
| 39 | { |
| 40 | //TODO: Get the location of this script from the MANIFEST.MF file |
| 41 | return mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME); |
| 42 | } |
| 43 | |
| 44 | static int read_data(ZipArchive *zip, const ZipEntry *entry, |
| 45 | char** ppData, int* pLength) { |
| 46 | int len = (int)mzGetZipEntryUncompLen(entry); |
| 47 | if (len <= 0) { |
| 48 | LOGE("Bad data length %d\n", len); |
| 49 | return -1; |
| 50 | } |
| 51 | char *data = malloc(len + 1); |
| 52 | if (data == NULL) { |
| 53 | LOGE("Can't allocate %d bytes for data\n", len + 1); |
| 54 | return -2; |
| 55 | } |
| 56 | bool ok = mzReadZipEntry(zip, entry, data, len); |
| 57 | if (!ok) { |
| 58 | LOGE("Error while reading data\n"); |
| 59 | free(data); |
| 60 | return -3; |
| 61 | } |
| 62 | data[len] = '\0'; // not necessary, but just to be safe |
| 63 | *ppData = data; |
| 64 | if (pLength) { |
| 65 | *pLength = len; |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | handle_update_script(ZipArchive *zip, const ZipEntry *update_script_entry) |
| 72 | { |
| 73 | /* Read the entire script into a buffer. |
| 74 | */ |
| 75 | int script_len; |
| 76 | char* script_data; |
| 77 | if (read_data(zip, update_script_entry, &script_data, &script_len) < 0) { |
| 78 | LOGE("Can't read update script\n"); |
| 79 | return INSTALL_ERROR; |
| 80 | } |
| 81 | |
| 82 | /* Parse the script. Note that the script and parse tree are never freed. |
| 83 | */ |
| 84 | const AmCommandList *commands = parseAmendScript(script_data, script_len); |
| 85 | if (commands == NULL) { |
| 86 | LOGE("Syntax error in update script\n"); |
| 87 | return INSTALL_ERROR; |
| 88 | } else { |
| 89 | UnterminatedString name = mzGetZipEntryFileName(update_script_entry); |
| 90 | LOGI("Parsed %.*s\n", name.len, name.str); |
| 91 | } |
| 92 | |
| 93 | /* Execute the script. |
| 94 | */ |
| 95 | int ret = execCommandList((ExecContext *)1, commands); |
| 96 | if (ret != 0) { |
| 97 | int num = ret; |
| 98 | char *line, *next = script_data; |
| 99 | while (next != NULL && ret-- > 0) { |
| 100 | line = next; |
| 101 | next = memchr(line, '\n', script_data + script_len - line); |
| 102 | if (next != NULL) *next++ = '\0'; |
| 103 | } |
| 104 | LOGE("Failure at line %d:\n%s\n", num, next ? line : "(not found)"); |
| 105 | return INSTALL_ERROR; |
| 106 | } |
| 107 | |
Doug Zongker | 07e1dca | 2009-05-28 19:02:45 -0700 | [diff] [blame] | 108 | LOGI("Installation complete.\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 109 | return INSTALL_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | static int |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 113 | handle_update_package(const char *path, ZipArchive *zip, |
| 114 | const RSAPublicKey *keys, int numKeys) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 115 | { |
| 116 | // Give verification half the progress bar... |
| 117 | ui_print("Verifying update package...\n"); |
| 118 | ui_show_progress( |
| 119 | VERIFICATION_PROGRESS_FRACTION, |
| 120 | VERIFICATION_PROGRESS_TIME); |
| 121 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 122 | if (!verify_jar_signature(zip, keys, numKeys)) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 123 | LOGE("Verification failed\n"); |
| 124 | return INSTALL_CORRUPT; |
| 125 | } |
| 126 | |
| 127 | // Update should take the rest of the progress bar. |
| 128 | ui_print("Installing update...\n"); |
| 129 | |
| 130 | const ZipEntry *script_entry; |
| 131 | script_entry = find_update_script(zip); |
| 132 | if (script_entry == NULL) { |
| 133 | LOGE("Can't find update script\n"); |
| 134 | return INSTALL_CORRUPT; |
| 135 | } |
| 136 | |
| 137 | if (register_package_root(zip, path) < 0) { |
| 138 | LOGE("Can't register package root\n"); |
| 139 | return INSTALL_ERROR; |
| 140 | } |
| 141 | |
| 142 | int ret = handle_update_script(zip, script_entry); |
| 143 | register_package_root(NULL, NULL); // Unregister package root |
| 144 | return ret; |
| 145 | } |
| 146 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 147 | // Reads a file containing one or more public keys as produced by |
| 148 | // DumpPublicKey: this is an RSAPublicKey struct as it would appear |
| 149 | // as a C source literal, eg: |
| 150 | // |
| 151 | // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 152 | // |
| 153 | // (Note that the braces and commas in this example are actual |
| 154 | // characters the parser expects to find in the file; the ellipses |
| 155 | // indicate more numbers omitted from this example.) |
| 156 | // |
| 157 | // The file may contain multiple keys in this format, separated by |
| 158 | // commas. The last key must not be followed by a comma. |
| 159 | // |
| 160 | // Returns NULL if the file failed to parse, or if it contain zero keys. |
| 161 | static RSAPublicKey* |
| 162 | load_keys(const char* filename, int* numKeys) { |
| 163 | RSAPublicKey* out = NULL; |
| 164 | *numKeys = 0; |
| 165 | |
| 166 | FILE* f = fopen(filename, "r"); |
| 167 | if (f == NULL) { |
| 168 | LOGE("opening %s: %s\n", filename, strerror(errno)); |
| 169 | goto exit; |
| 170 | } |
| 171 | |
| 172 | int i; |
| 173 | bool done = false; |
| 174 | while (!done) { |
| 175 | ++*numKeys; |
| 176 | out = realloc(out, *numKeys * sizeof(RSAPublicKey)); |
| 177 | RSAPublicKey* key = out + (*numKeys - 1); |
| 178 | if (fscanf(f, " { %i , %i , { %i", |
| 179 | &(key->len), &(key->n0inv), &(key->n[0])) != 3) { |
| 180 | goto exit; |
| 181 | } |
| 182 | if (key->len != RSANUMWORDS) { |
| 183 | LOGE("key length (%d) does not match expected size\n", key->len); |
| 184 | goto exit; |
| 185 | } |
| 186 | for (i = 1; i < key->len; ++i) { |
| 187 | if (fscanf(f, " , %i", &(key->n[i])) != 1) goto exit; |
| 188 | } |
| 189 | if (fscanf(f, " } , { %i", &(key->rr[0])) != 1) goto exit; |
| 190 | for (i = 1; i < key->len; ++i) { |
| 191 | if (fscanf(f, " , %i", &(key->rr[i])) != 1) goto exit; |
| 192 | } |
| 193 | fscanf(f, " } } "); |
| 194 | |
| 195 | // if the line ends in a comma, this file has more keys. |
| 196 | switch (fgetc(f)) { |
| 197 | case ',': |
| 198 | // more keys to come. |
| 199 | break; |
| 200 | |
| 201 | case EOF: |
| 202 | done = true; |
| 203 | break; |
| 204 | |
| 205 | default: |
| 206 | LOGE("unexpected character between keys\n"); |
| 207 | goto exit; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | fclose(f); |
| 212 | return out; |
| 213 | |
| 214 | exit: |
| 215 | if (f) fclose(f); |
| 216 | free(out); |
| 217 | *numKeys = 0; |
| 218 | return NULL; |
| 219 | } |
| 220 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 221 | int |
| 222 | install_package(const char *root_path) |
| 223 | { |
| 224 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 225 | ui_print("Finding update package...\n"); |
| 226 | ui_show_indeterminate_progress(); |
| 227 | LOGI("Update location: %s\n", root_path); |
| 228 | |
| 229 | if (ensure_root_path_mounted(root_path) != 0) { |
| 230 | LOGE("Can't mount %s\n", root_path); |
| 231 | return INSTALL_CORRUPT; |
| 232 | } |
| 233 | |
| 234 | char path[PATH_MAX] = ""; |
| 235 | if (translate_root_path(root_path, path, sizeof(path)) == NULL) { |
| 236 | LOGE("Bad path %s\n", root_path); |
| 237 | return INSTALL_CORRUPT; |
| 238 | } |
| 239 | |
| 240 | ui_print("Opening update package...\n"); |
| 241 | LOGI("Update file path: %s\n", path); |
| 242 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 243 | int numKeys; |
| 244 | RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); |
| 245 | if (loadedKeys == NULL) { |
| 246 | LOGE("Failed to load keys\n"); |
| 247 | return INSTALL_CORRUPT; |
| 248 | } |
| 249 | LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); |
| 250 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 251 | /* Try to open the package. |
| 252 | */ |
| 253 | ZipArchive zip; |
| 254 | int err = mzOpenZipArchive(path, &zip); |
| 255 | if (err != 0) { |
| 256 | LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad"); |
| 257 | return INSTALL_CORRUPT; |
| 258 | } |
| 259 | |
| 260 | /* Verify and install the contents of the package. |
| 261 | */ |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 262 | int status = handle_update_package(path, &zip, loadedKeys, numKeys); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 263 | mzCloseZipArchive(&zip); |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 264 | free(loadedKeys); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 265 | return status; |
| 266 | } |