Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """ |
| 18 | Signs all the APK files in a target-files zipfile, producing a new |
| 19 | target-files zip. |
| 20 | |
| 21 | Usage: sign_target_files_apks [flags] input_target_files output_target_files |
| 22 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 23 | -e (--extra_apks) <name,name,...=key> |
| 24 | Add extra APK name/key pairs as though they appeared in |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 25 | apkcerts.txt (so mappings specified by -k and -d are applied). |
| 26 | Keys specified in -e override any value for that app contained |
| 27 | in the apkcerts.txt file. Option may be repeated to give |
| 28 | multiple extra packages. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 29 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 30 | --skip_apks_with_path_prefix <prefix> |
| 31 | Skip signing an APK if it has the matching prefix in its path. The prefix |
| 32 | should be matching the entry name, which has partition names in upper |
| 33 | case, e.g. "VENDOR/app/", or "SYSTEM_OTHER/preloads/". Option may be |
| 34 | repeated to give multiple prefixes. |
| 35 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 36 | -k (--key_mapping) <src_key=dest_key> |
| 37 | Add a mapping from the key name as specified in apkcerts.txt (the |
| 38 | src_key) to the real key you wish to sign the package with |
| 39 | (dest_key). Option may be repeated to give multiple key |
| 40 | mappings. |
| 41 | |
| 42 | -d (--default_key_mappings) <dir> |
| 43 | Set up the following key mappings: |
| 44 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 45 | $devkey/devkey ==> $dir/releasekey |
| 46 | $devkey/testkey ==> $dir/releasekey |
| 47 | $devkey/media ==> $dir/media |
| 48 | $devkey/shared ==> $dir/shared |
| 49 | $devkey/platform ==> $dir/platform |
| 50 | |
| 51 | where $devkey is the directory part of the value of |
| 52 | default_system_dev_certificate from the input target-files's |
| 53 | META/misc_info.txt. (Defaulting to "build/target/product/security" |
| 54 | if the value is not present in misc_info. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 55 | |
| 56 | -d and -k options are added to the set of mappings in the order |
| 57 | in which they appear on the command line. |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 58 | |
| 59 | -o (--replace_ota_keys) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 60 | Replace the certificate (public key) used by OTA package verification |
| 61 | with the ones specified in the input target_files zip (in the |
| 62 | META/otakeys.txt file). Key remapping (-k and -d) is performed on the |
| 63 | keys. For A/B devices, the payload verification key will be replaced |
| 64 | as well. If there're multiple OTA keys, only the first one will be used |
| 65 | for payload verification. |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 66 | |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 67 | -t (--tag_changes) <+tag>,<-tag>,... |
| 68 | Comma-separated list of changes to make to the set of tags (in |
| 69 | the last component of the build fingerprint). Prefix each with |
| 70 | '+' or '-' to indicate whether that tag should be added or |
| 71 | removed. Changes are processed in the order they appear. |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 72 | Default value is "-test-keys,-dev-keys,+release-keys". |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 73 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 74 | --replace_verity_private_key <key> |
| 75 | Replace the private key used for verity signing. It expects a filename |
| 76 | WITHOUT the extension (e.g. verity_key). |
| 77 | |
| 78 | --replace_verity_public_key <key> |
| 79 | Replace the certificate (public key) used for verity verification. The |
| 80 | key file replaces the one at BOOT/RAMDISK/verity_key (or ROOT/verity_key |
| 81 | for devices using system_root_image). It expects the key filename WITH |
| 82 | the extension (e.g. verity_key.pub). |
| 83 | |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 84 | --replace_verity_keyid <path_to_X509_PEM_cert_file> |
| 85 | Replace the veritykeyid in BOOT/cmdline of input_target_file_zip |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 86 | with keyid of the cert pointed by <path_to_X509_PEM_cert_file>. |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 87 | |
| 88 | --avb_{boot,system,vendor,dtbo,vbmeta}_algorithm <algorithm> |
| 89 | --avb_{boot,system,vendor,dtbo,vbmeta}_key <key> |
| 90 | Use the specified algorithm (e.g. SHA256_RSA4096) and the key to AVB-sign |
| 91 | the specified image. Otherwise it uses the existing values in info dict. |
| 92 | |
| 93 | --avb_{boot,system,vendor,dtbo,vbmeta}_extra_args <args> |
| 94 | Specify any additional args that are needed to AVB-sign the image |
| 95 | (e.g. "--signing_helper /path/to/helper"). The args will be appended to |
| 96 | the existing ones in info dict. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 97 | """ |
| 98 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 99 | from __future__ import print_function |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 100 | |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 101 | import base64 |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 102 | import copy |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 103 | import errno |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 104 | import gzip |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 105 | import os |
| 106 | import re |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 107 | import shutil |
Tao Bao | 9fdd00f | 2017-07-12 11:57:05 -0700 | [diff] [blame] | 108 | import stat |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 109 | import subprocess |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 110 | import sys |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 111 | import tempfile |
| 112 | import zipfile |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 113 | from xml.etree import ElementTree |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 114 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 115 | import add_img_to_target_files |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 116 | import common |
| 117 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 118 | |
| 119 | if sys.hexversion < 0x02070000: |
| 120 | print("Python 2.7 or newer is required.", file=sys.stderr) |
| 121 | sys.exit(1) |
| 122 | |
| 123 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 124 | OPTIONS = common.OPTIONS |
| 125 | |
| 126 | OPTIONS.extra_apks = {} |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 127 | OPTIONS.skip_apks_with_path_prefix = set() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 128 | OPTIONS.key_map = {} |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 129 | OPTIONS.rebuild_recovery = False |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 130 | OPTIONS.replace_ota_keys = False |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 131 | OPTIONS.replace_verity_public_key = False |
| 132 | OPTIONS.replace_verity_private_key = False |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 133 | OPTIONS.replace_verity_keyid = False |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 134 | OPTIONS.tag_changes = ("-test-keys", "-dev-keys", "+release-keys") |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 135 | OPTIONS.avb_keys = {} |
| 136 | OPTIONS.avb_algorithms = {} |
| 137 | OPTIONS.avb_extra_args = {} |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 138 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 139 | |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 140 | def GetApkCerts(certmap): |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 141 | # apply the key remapping to the contents of the file |
| 142 | for apk, cert in certmap.iteritems(): |
| 143 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
| 144 | |
| 145 | # apply all the -e options, overriding anything in the file |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 146 | for apk, cert in OPTIONS.extra_apks.iteritems(): |
Doug Zongker | decf995 | 2009-12-15 17:27:49 -0800 | [diff] [blame] | 147 | if not cert: |
| 148 | cert = "PRESIGNED" |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 149 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 150 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 151 | return certmap |
| 152 | |
| 153 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 154 | def GetApkFileInfo(filename, compressed_extension, skipped_prefixes): |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 155 | """Returns the APK info based on the given filename. |
| 156 | |
| 157 | Checks if the given filename (with path) looks like an APK file, by taking the |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 158 | compressed extension into consideration. If it appears to be an APK file, |
| 159 | further checks if the APK file should be skipped when signing, based on the |
| 160 | given path prefixes. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 161 | |
| 162 | Args: |
| 163 | filename: Path to the file. |
| 164 | compressed_extension: The extension string of compressed APKs (e.g. ".gz"), |
| 165 | or None if there's no compressed APKs. |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 166 | skipped_prefixes: A set/list/tuple of the path prefixes to be skipped. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 167 | |
| 168 | Returns: |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 169 | (is_apk, is_compressed, should_be_skipped): is_apk indicates whether the |
| 170 | given filename is an APK file. is_compressed indicates whether the APK file |
| 171 | is compressed (only meaningful when is_apk is True). should_be_skipped |
| 172 | indicates whether the filename matches any of the given prefixes to be |
| 173 | skipped. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 174 | |
| 175 | Raises: |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 176 | AssertionError: On invalid compressed_extension or skipped_prefixes inputs. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 177 | """ |
| 178 | assert compressed_extension is None or compressed_extension.startswith('.'), \ |
| 179 | "Invalid compressed_extension arg: '{}'".format(compressed_extension) |
| 180 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 181 | # skipped_prefixes should be one of set/list/tuple types. Other types such as |
| 182 | # str shouldn't be accepted. |
| 183 | assert (isinstance(skipped_prefixes, tuple) or |
| 184 | isinstance(skipped_prefixes, set) or |
| 185 | isinstance(skipped_prefixes, list)), \ |
| 186 | "Invalid skipped_prefixes input type: {}".format( |
| 187 | type(skipped_prefixes)) |
| 188 | |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 189 | compressed_apk_extension = ( |
| 190 | ".apk" + compressed_extension if compressed_extension else None) |
| 191 | is_apk = (filename.endswith(".apk") or |
| 192 | (compressed_apk_extension and |
| 193 | filename.endswith(compressed_apk_extension))) |
| 194 | if not is_apk: |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 195 | return (False, False, False) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 196 | |
| 197 | is_compressed = (compressed_apk_extension and |
| 198 | filename.endswith(compressed_apk_extension)) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 199 | should_be_skipped = filename.startswith(tuple(skipped_prefixes)) |
| 200 | return (True, is_compressed, should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 201 | |
| 202 | |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 203 | def CheckAllApksSigned(input_tf_zip, apk_key_map, compressed_extension): |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 204 | """Checks that all the APKs have keys specified, otherwise errors out. |
| 205 | |
| 206 | Args: |
| 207 | input_tf_zip: An open target_files zip file. |
| 208 | apk_key_map: A dict of known signing keys key'd by APK names. |
| 209 | compressed_extension: The extension string of compressed APKs, such as |
| 210 | ".gz", or None if there's no compressed APKs. |
| 211 | |
| 212 | Raises: |
| 213 | AssertionError: On finding unknown APKs. |
| 214 | """ |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 215 | unknown_apks = [] |
| 216 | for info in input_tf_zip.infolist(): |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 217 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 218 | info.filename, compressed_extension, OPTIONS.skip_apks_with_path_prefix) |
| 219 | if not is_apk or should_be_skipped: |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 220 | continue |
| 221 | name = os.path.basename(info.filename) |
| 222 | if is_compressed: |
| 223 | name = name[:-len(compressed_extension)] |
| 224 | if name not in apk_key_map: |
| 225 | unknown_apks.append(name) |
| 226 | |
| 227 | assert not unknown_apks, \ |
| 228 | ("No key specified for:\n {}\n" |
| 229 | "Use '-e <apkname>=' to specify a key (which may be an empty string to " |
| 230 | "not sign this apk).".format("\n ".join(unknown_apks))) |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 231 | |
| 232 | |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 233 | def SignApk(data, keyname, pw, platform_api_level, codename_to_api_level_map, |
| 234 | is_compressed): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 235 | unsigned = tempfile.NamedTemporaryFile() |
| 236 | unsigned.write(data) |
| 237 | unsigned.flush() |
| 238 | |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 239 | if is_compressed: |
| 240 | uncompressed = tempfile.NamedTemporaryFile() |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 241 | with gzip.open(unsigned.name, "rb") as in_file, \ |
| 242 | open(uncompressed.name, "wb") as out_file: |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 243 | shutil.copyfileobj(in_file, out_file) |
| 244 | |
| 245 | # Finally, close the "unsigned" file (which is gzip compressed), and then |
| 246 | # replace it with the uncompressed version. |
| 247 | # |
| 248 | # TODO(narayan): All this nastiness can be avoided if python 3.2 is in use, |
| 249 | # we could just gzip / gunzip in-memory buffers instead. |
| 250 | unsigned.close() |
| 251 | unsigned = uncompressed |
| 252 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 253 | signed = tempfile.NamedTemporaryFile() |
| 254 | |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 255 | # For pre-N builds, don't upgrade to SHA-256 JAR signatures based on the APK's |
| 256 | # minSdkVersion to avoid increasing incremental OTA update sizes. If an APK |
| 257 | # didn't change, we don't want its signature to change due to the switch |
| 258 | # from SHA-1 to SHA-256. |
| 259 | # By default, APK signer chooses SHA-256 signatures if the APK's minSdkVersion |
| 260 | # is 18 or higher. For pre-N builds we disable this mechanism by pretending |
| 261 | # that the APK's minSdkVersion is 1. |
| 262 | # For N+ builds, we let APK signer rely on the APK's minSdkVersion to |
| 263 | # determine whether to use SHA-256. |
| 264 | min_api_level = None |
| 265 | if platform_api_level > 23: |
| 266 | # Let APK signer choose whether to use SHA-1 or SHA-256, based on the APK's |
| 267 | # minSdkVersion attribute |
| 268 | min_api_level = None |
| 269 | else: |
| 270 | # Force APK signer to use SHA-1 |
| 271 | min_api_level = 1 |
| 272 | |
| 273 | common.SignFile(unsigned.name, signed.name, keyname, pw, |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 274 | min_api_level=min_api_level, |
| 275 | codename_to_api_level_map=codename_to_api_level_map) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 276 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 277 | data = None |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 278 | if is_compressed: |
| 279 | # Recompress the file after it has been signed. |
| 280 | compressed = tempfile.NamedTemporaryFile() |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 281 | with open(signed.name, "rb") as in_file, \ |
| 282 | gzip.open(compressed.name, "wb") as out_file: |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 283 | shutil.copyfileobj(in_file, out_file) |
| 284 | |
| 285 | data = compressed.read() |
| 286 | compressed.close() |
| 287 | else: |
| 288 | data = signed.read() |
| 289 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 290 | unsigned.close() |
| 291 | signed.close() |
| 292 | |
| 293 | return data |
| 294 | |
| 295 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 296 | def ProcessTargetFiles(input_tf_zip, output_tf_zip, misc_info, |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 297 | apk_key_map, key_passwords, platform_api_level, |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 298 | codename_to_api_level_map, |
| 299 | compressed_extension): |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 300 | # maxsize measures the maximum filename length, including the ones to be |
| 301 | # skipped. |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 302 | maxsize = max( |
| 303 | [len(os.path.basename(i.filename)) for i in input_tf_zip.infolist() |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 304 | if GetApkFileInfo(i.filename, compressed_extension, [])[0]]) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 305 | system_root_image = misc_info.get("system_root_image") == "true" |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 306 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 307 | for info in input_tf_zip.infolist(): |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 308 | filename = info.filename |
| 309 | if filename.startswith("IMAGES/"): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 310 | continue |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 311 | |
Tao Bao | 33bf268 | 2019-01-11 12:37:35 -0800 | [diff] [blame] | 312 | # Skip split super images, which will be re-generated during signing. |
| 313 | if filename.startswith("OTA/") and filename.endswith(".img"): |
| 314 | continue |
| 315 | |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 316 | data = input_tf_zip.read(filename) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 317 | out_info = copy.copy(info) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 318 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 319 | filename, compressed_extension, OPTIONS.skip_apks_with_path_prefix) |
| 320 | |
| 321 | if is_apk and should_be_skipped: |
| 322 | # Copy skipped APKs verbatim. |
| 323 | print( |
| 324 | "NOT signing: %s\n" |
| 325 | " (skipped due to matching prefix)" % (filename,)) |
| 326 | common.ZipWriteStr(output_tf_zip, out_info, data) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 327 | |
Tao Bao | f2cffbd | 2015-07-22 12:33:18 -0700 | [diff] [blame] | 328 | # Sign APKs. |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 329 | elif is_apk: |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 330 | name = os.path.basename(filename) |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 331 | if is_compressed: |
| 332 | name = name[:-len(compressed_extension)] |
| 333 | |
Doug Zongker | 43874f8 | 2009-04-14 14:05:15 -0700 | [diff] [blame] | 334 | key = apk_key_map[name] |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 335 | if key not in common.SPECIAL_CERT_STRINGS: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 336 | print(" signing: %-*s (%s)" % (maxsize, name, key)) |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 337 | signed_data = SignApk(data, key, key_passwords[key], platform_api_level, |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 338 | codename_to_api_level_map, is_compressed) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 339 | common.ZipWriteStr(output_tf_zip, out_info, signed_data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 340 | else: |
| 341 | # an APK we're not supposed to sign. |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 342 | print( |
| 343 | "NOT signing: %s\n" |
| 344 | " (skipped due to special cert string)" % (name,)) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 345 | common.ZipWriteStr(output_tf_zip, out_info, data) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 346 | |
| 347 | # System properties. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 348 | elif filename in ("SYSTEM/build.prop", |
| 349 | "VENDOR/build.prop", |
| 350 | "SYSTEM/etc/prop.default", |
| 351 | "BOOT/RAMDISK/prop.default", |
| 352 | "BOOT/RAMDISK/default.prop", # legacy |
| 353 | "ROOT/default.prop", # legacy |
| 354 | "RECOVERY/RAMDISK/prop.default", |
| 355 | "RECOVERY/RAMDISK/default.prop"): # legacy |
| 356 | print("Rewriting %s:" % (filename,)) |
Hung-ying Tyan | 7eb6a92 | 2017-05-01 21:56:26 +0800 | [diff] [blame] | 357 | if stat.S_ISLNK(info.external_attr >> 16): |
| 358 | new_data = data |
| 359 | else: |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 360 | new_data = RewriteProps(data) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 361 | common.ZipWriteStr(output_tf_zip, out_info, new_data) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 362 | |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 363 | # Replace the certs in *mac_permissions.xml (there could be multiple, such |
| 364 | # as {system,vendor}/etc/selinux/{plat,nonplat}_mac_permissions.xml). |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 365 | elif filename.endswith("mac_permissions.xml"): |
| 366 | print("Rewriting %s with new keys." % (filename,)) |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 367 | new_data = ReplaceCerts(data) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 368 | common.ZipWriteStr(output_tf_zip, out_info, new_data) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 369 | |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 370 | # Ask add_img_to_target_files to rebuild the recovery patch if needed. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 371 | elif filename in ("SYSTEM/recovery-from-boot.p", |
| 372 | "SYSTEM/etc/recovery.img", |
| 373 | "SYSTEM/bin/install-recovery.sh"): |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 374 | OPTIONS.rebuild_recovery = True |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 375 | |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 376 | # Don't copy OTA certs if we're replacing them. |
Tao Bao | 696bb33 | 2018-08-17 16:27:01 -0700 | [diff] [blame] | 377 | elif ( |
| 378 | OPTIONS.replace_ota_keys and |
| 379 | filename in ( |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 380 | "BOOT/RAMDISK/system/etc/security/otacerts.zip", |
Tao Bao | 696bb33 | 2018-08-17 16:27:01 -0700 | [diff] [blame] | 381 | "BOOT/RAMDISK/system/etc/update_engine/update-payload-key.pub.pem", |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 382 | "RECOVERY/RAMDISK/system/etc/security/otacerts.zip", |
Tao Bao | 696bb33 | 2018-08-17 16:27:01 -0700 | [diff] [blame] | 383 | "SYSTEM/etc/security/otacerts.zip", |
| 384 | "SYSTEM/etc/update_engine/update-payload-key.pub.pem")): |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 385 | pass |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 386 | |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 387 | # Skip META/misc_info.txt since we will write back the new values later. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 388 | elif filename == "META/misc_info.txt": |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 389 | pass |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 390 | |
| 391 | # Skip verity public key if we will replace it. |
Michael Runge | 947894f | 2014-10-14 20:58:38 -0700 | [diff] [blame] | 392 | elif (OPTIONS.replace_verity_public_key and |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 393 | filename in ("BOOT/RAMDISK/verity_key", |
| 394 | "ROOT/verity_key")): |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 395 | pass |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 396 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 397 | # Skip verity keyid (for system_root_image use) if we will replace it. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 398 | elif OPTIONS.replace_verity_keyid and filename == "BOOT/cmdline": |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 399 | pass |
| 400 | |
Tianjie Xu | 4f09900 | 2016-08-11 18:04:27 -0700 | [diff] [blame] | 401 | # Skip the care_map as we will regenerate the system/vendor images. |
Tianjie Xu | 4c05f4a | 2018-09-14 16:24:41 -0700 | [diff] [blame] | 402 | elif filename == "META/care_map.pb" or filename == "META/care_map.txt": |
Tianjie Xu | 4f09900 | 2016-08-11 18:04:27 -0700 | [diff] [blame] | 403 | pass |
| 404 | |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 405 | # A non-APK file; copy it verbatim. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 406 | else: |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 407 | common.ZipWriteStr(output_tf_zip, out_info, data) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 408 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 409 | if OPTIONS.replace_ota_keys: |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 410 | ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 411 | |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 412 | # Replace the keyid string in misc_info dict. |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 413 | if OPTIONS.replace_verity_private_key: |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 414 | ReplaceVerityPrivateKey(misc_info, OPTIONS.replace_verity_private_key[1]) |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 415 | |
| 416 | if OPTIONS.replace_verity_public_key: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 417 | dest = "ROOT/verity_key" if system_root_image else "BOOT/RAMDISK/verity_key" |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 418 | # We are replacing the one in boot image only, since the one under |
| 419 | # recovery won't ever be needed. |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 420 | ReplaceVerityPublicKey( |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 421 | output_tf_zip, dest, OPTIONS.replace_verity_public_key[1]) |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 422 | |
| 423 | # Replace the keyid string in BOOT/cmdline. |
| 424 | if OPTIONS.replace_verity_keyid: |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 425 | ReplaceVerityKeyId(input_tf_zip, output_tf_zip, |
| 426 | OPTIONS.replace_verity_keyid[1]) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 427 | |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 428 | # Replace the AVB signing keys, if any. |
| 429 | ReplaceAvbSigningKeys(misc_info) |
| 430 | |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 431 | # Write back misc_info with the latest values. |
| 432 | ReplaceMiscInfoTxt(input_tf_zip, output_tf_zip, misc_info) |
| 433 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 434 | |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 435 | def ReplaceCerts(data): |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 436 | """Replaces all the occurences of X.509 certs with the new ones. |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 437 | |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 438 | The mapping info is read from OPTIONS.key_map. Non-existent certificate will |
| 439 | be skipped. After the replacement, it additionally checks for duplicate |
| 440 | entries, which would otherwise fail the policy loading code in |
| 441 | frameworks/base/services/core/java/com/android/server/pm/SELinuxMMAC.java. |
| 442 | |
| 443 | Args: |
| 444 | data: Input string that contains a set of X.509 certs. |
| 445 | |
| 446 | Returns: |
| 447 | A string after the replacement. |
| 448 | |
| 449 | Raises: |
| 450 | AssertionError: On finding duplicate entries. |
| 451 | """ |
| 452 | for old, new in OPTIONS.key_map.iteritems(): |
| 453 | if OPTIONS.verbose: |
| 454 | print(" Replacing %s.x509.pem with %s.x509.pem" % (old, new)) |
| 455 | |
| 456 | try: |
| 457 | with open(old + ".x509.pem") as old_fp: |
| 458 | old_cert16 = base64.b16encode( |
| 459 | common.ParseCertificate(old_fp.read())).lower() |
| 460 | with open(new + ".x509.pem") as new_fp: |
| 461 | new_cert16 = base64.b16encode( |
| 462 | common.ParseCertificate(new_fp.read())).lower() |
| 463 | except IOError as e: |
| 464 | if OPTIONS.verbose or e.errno != errno.ENOENT: |
| 465 | print(" Error accessing %s: %s.\nSkip replacing %s.x509.pem with " |
| 466 | "%s.x509.pem." % (e.filename, e.strerror, old, new)) |
| 467 | continue |
| 468 | |
| 469 | # Only match entire certs. |
| 470 | pattern = "\\b" + old_cert16 + "\\b" |
| 471 | (data, num) = re.subn(pattern, new_cert16, data, flags=re.IGNORECASE) |
| 472 | |
| 473 | if OPTIONS.verbose: |
| 474 | print(" Replaced %d occurence(s) of %s.x509.pem with %s.x509.pem" % ( |
| 475 | num, old, new)) |
| 476 | |
| 477 | # Verify that there're no duplicate entries after the replacement. Note that |
| 478 | # it's only checking entries with global seinfo at the moment (i.e. ignoring |
| 479 | # the ones with inner packages). (Bug: 69479366) |
| 480 | root = ElementTree.fromstring(data) |
| 481 | signatures = [signer.attrib['signature'] for signer in root.findall('signer')] |
| 482 | assert len(signatures) == len(set(signatures)), \ |
| 483 | "Found duplicate entries after cert replacement: {}".format(data) |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 484 | |
| 485 | return data |
| 486 | |
| 487 | |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 488 | def EditTags(tags): |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 489 | """Applies the edits to the tag string as specified in OPTIONS.tag_changes. |
| 490 | |
| 491 | Args: |
| 492 | tags: The input string that contains comma-separated tags. |
| 493 | |
| 494 | Returns: |
| 495 | The updated tags (comma-separated and sorted). |
| 496 | """ |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 497 | tags = set(tags.split(",")) |
| 498 | for ch in OPTIONS.tag_changes: |
| 499 | if ch[0] == "-": |
| 500 | tags.discard(ch[1:]) |
| 501 | elif ch[0] == "+": |
| 502 | tags.add(ch[1:]) |
| 503 | return ",".join(sorted(tags)) |
| 504 | |
| 505 | |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 506 | def RewriteProps(data): |
| 507 | """Rewrites the system properties in the given string. |
| 508 | |
| 509 | Each property is expected in 'key=value' format. The properties that contain |
| 510 | build tags (i.e. test-keys, dev-keys) will be updated accordingly by calling |
| 511 | EditTags(). |
| 512 | |
| 513 | Args: |
| 514 | data: Input string, separated by newlines. |
| 515 | |
| 516 | Returns: |
| 517 | The string with modified properties. |
| 518 | """ |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 519 | output = [] |
| 520 | for line in data.split("\n"): |
| 521 | line = line.strip() |
| 522 | original_line = line |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 523 | if line and line[0] != '#' and "=" in line: |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 524 | key, value = line.split("=", 1) |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 525 | if key in ("ro.build.fingerprint", "ro.build.thumbprint", |
| 526 | "ro.vendor.build.fingerprint", "ro.vendor.build.thumbprint"): |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 527 | pieces = value.split("/") |
| 528 | pieces[-1] = EditTags(pieces[-1]) |
| 529 | value = "/".join(pieces) |
Tao Bao | cb7ff77 | 2015-09-11 15:27:56 -0700 | [diff] [blame] | 530 | elif key == "ro.bootimage.build.fingerprint": |
| 531 | pieces = value.split("/") |
| 532 | pieces[-1] = EditTags(pieces[-1]) |
| 533 | value = "/".join(pieces) |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 534 | elif key == "ro.build.description": |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 535 | pieces = value.split(" ") |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 536 | assert len(pieces) == 5 |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 537 | pieces[-1] = EditTags(pieces[-1]) |
| 538 | value = " ".join(pieces) |
| 539 | elif key == "ro.build.tags": |
| 540 | value = EditTags(value) |
Doug Zongker | a8608a7 | 2013-07-23 11:51:04 -0700 | [diff] [blame] | 541 | elif key == "ro.build.display.id": |
| 542 | # change, eg, "JWR66N dev-keys" to "JWR66N" |
| 543 | value = value.split() |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 544 | if len(value) > 1 and value[-1].endswith("-keys"): |
Andrew Boie | 73d5abb | 2013-12-11 12:42:03 -0800 | [diff] [blame] | 545 | value.pop() |
| 546 | value = " ".join(value) |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 547 | line = key + "=" + value |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 548 | if line != original_line: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 549 | print(" replace: ", original_line) |
| 550 | print(" with: ", line) |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 551 | output.append(line) |
| 552 | return "\n".join(output) + "\n" |
| 553 | |
| 554 | |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 555 | def WriteOtacerts(output_zip, filename, keys): |
| 556 | """Constructs a zipfile from given keys; and writes it to output_zip. |
| 557 | |
| 558 | Args: |
| 559 | output_zip: The output target_files zip. |
| 560 | filename: The archive name in the output zip. |
| 561 | keys: A list of public keys to use during OTA package verification. |
| 562 | """ |
| 563 | |
| 564 | try: |
| 565 | from StringIO import StringIO |
| 566 | except ImportError: |
| 567 | from io import StringIO |
| 568 | temp_file = StringIO() |
| 569 | certs_zip = zipfile.ZipFile(temp_file, "w") |
| 570 | for k in keys: |
| 571 | common.ZipWrite(certs_zip, k) |
| 572 | common.ZipClose(certs_zip) |
| 573 | common.ZipWriteStr(output_zip, filename, temp_file.getvalue()) |
| 574 | |
| 575 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 576 | def ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info): |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 577 | try: |
| 578 | keylist = input_tf_zip.read("META/otakeys.txt").split() |
| 579 | except KeyError: |
T.R. Fullhart | a28acc6 | 2013-03-18 10:31:26 -0700 | [diff] [blame] | 580 | raise common.ExternalError("can't read META/otakeys.txt from input") |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 581 | |
Tao Bao | f718f90 | 2017-11-09 10:10:10 -0800 | [diff] [blame] | 582 | extra_recovery_keys = misc_info.get("extra_recovery_keys") |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 583 | if extra_recovery_keys: |
| 584 | extra_recovery_keys = [OPTIONS.key_map.get(k, k) + ".x509.pem" |
| 585 | for k in extra_recovery_keys.split()] |
| 586 | if extra_recovery_keys: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 587 | print("extra recovery-only key(s): " + ", ".join(extra_recovery_keys)) |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 588 | else: |
| 589 | extra_recovery_keys = [] |
| 590 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 591 | mapped_keys = [] |
| 592 | for k in keylist: |
| 593 | m = re.match(r"^(.*)\.x509\.pem$", k) |
| 594 | if not m: |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 595 | raise common.ExternalError( |
| 596 | "can't parse \"%s\" from META/otakeys.txt" % (k,)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 597 | k = m.group(1) |
| 598 | mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem") |
| 599 | |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 600 | if mapped_keys: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 601 | print("using:\n ", "\n ".join(mapped_keys)) |
| 602 | print("for OTA package verification") |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 603 | else: |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 604 | devkey = misc_info.get("default_system_dev_certificate", |
| 605 | "build/target/product/security/testkey") |
Tao Bao | f718f90 | 2017-11-09 10:10:10 -0800 | [diff] [blame] | 606 | mapped_devkey = OPTIONS.key_map.get(devkey, devkey) |
| 607 | if mapped_devkey != devkey: |
| 608 | misc_info["default_system_dev_certificate"] = mapped_devkey |
| 609 | mapped_keys.append(mapped_devkey + ".x509.pem") |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 610 | print("META/otakeys.txt has no keys; using %s for OTA package" |
| 611 | " verification." % (mapped_keys[0],)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 612 | |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 613 | # recovery now uses the same x509.pem version of the keys. |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 614 | # extra_recovery_keys are used only in recovery. |
Tom Cherry | 2929cad | 2018-09-20 11:04:37 -0700 | [diff] [blame] | 615 | if misc_info.get("recovery_as_boot") == "true": |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 616 | recovery_keys_location = "BOOT/RAMDISK/system/etc/security/otacerts.zip" |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 617 | else: |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 618 | recovery_keys_location = "RECOVERY/RAMDISK/system/etc/security/otacerts.zip" |
| 619 | |
| 620 | WriteOtacerts(output_tf_zip, recovery_keys_location, |
| 621 | mapped_keys + extra_recovery_keys) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 622 | |
| 623 | # SystemUpdateActivity uses the x509.pem version of the keys, but |
| 624 | # put into a zipfile system/etc/security/otacerts.zip. |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 625 | # We DO NOT include the extra_recovery_keys (if any) here. |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 626 | WriteOtacerts(output_tf_zip, "SYSTEM/etc/security/otacerts.zip", mapped_keys) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 627 | |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 628 | # For A/B devices, update the payload verification key. |
| 629 | if misc_info.get("ab_update") == "true": |
| 630 | # Unlike otacerts.zip that may contain multiple keys, we can only specify |
| 631 | # ONE payload verification key. |
| 632 | if len(mapped_keys) > 1: |
| 633 | print("\n WARNING: Found more than one OTA keys; Using the first one" |
| 634 | " as payload verification key.\n\n") |
| 635 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 636 | print("Using %s for payload verification." % (mapped_keys[0],)) |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 637 | pubkey = common.ExtractPublicKey(mapped_keys[0]) |
Tao Bao | 13b6962 | 2016-07-06 15:28:59 -0700 | [diff] [blame] | 638 | common.ZipWriteStr( |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 639 | output_tf_zip, |
Tao Bao | 13b6962 | 2016-07-06 15:28:59 -0700 | [diff] [blame] | 640 | "SYSTEM/etc/update_engine/update-payload-key.pub.pem", |
| 641 | pubkey) |
Alex Deymo | b3e8ce6 | 2016-08-04 16:06:12 -0700 | [diff] [blame] | 642 | common.ZipWriteStr( |
| 643 | output_tf_zip, |
Tao Bao | 696bb33 | 2018-08-17 16:27:01 -0700 | [diff] [blame] | 644 | "BOOT/RAMDISK/system/etc/update_engine/update-payload-key.pub.pem", |
Alex Deymo | b3e8ce6 | 2016-08-04 16:06:12 -0700 | [diff] [blame] | 645 | pubkey) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 646 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 647 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 648 | def ReplaceVerityPublicKey(output_zip, filename, key_path): |
| 649 | """Replaces the verity public key at the given path in the given zip. |
| 650 | |
| 651 | Args: |
| 652 | output_zip: The output target_files zip. |
| 653 | filename: The archive name in the output zip. |
| 654 | key_path: The path to the public key. |
| 655 | """ |
| 656 | print("Replacing verity public key with %s" % (key_path,)) |
| 657 | common.ZipWrite(output_zip, key_path, arcname=filename) |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 658 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 659 | |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 660 | def ReplaceVerityPrivateKey(misc_info, key_path): |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 661 | """Replaces the verity private key in misc_info dict. |
| 662 | |
| 663 | Args: |
| 664 | misc_info: The info dict. |
| 665 | key_path: The path to the private key in PKCS#8 format. |
| 666 | """ |
| 667 | print("Replacing verity private key with %s" % (key_path,)) |
Andrew Boie | d083f0b | 2014-09-15 16:01:07 -0700 | [diff] [blame] | 668 | misc_info["verity_key"] = key_path |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 669 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 670 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 671 | def ReplaceVerityKeyId(input_zip, output_zip, key_path): |
| 672 | """Replaces the veritykeyid parameter in BOOT/cmdline. |
| 673 | |
| 674 | Args: |
| 675 | input_zip: The input target_files zip, which should be already open. |
| 676 | output_zip: The output target_files zip, which should be already open and |
| 677 | writable. |
| 678 | key_path: The path to the PEM encoded X.509 certificate. |
| 679 | """ |
| 680 | in_cmdline = input_zip.read("BOOT/cmdline") |
| 681 | # Copy in_cmdline to output_zip if veritykeyid is not present. |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 682 | if "veritykeyid" not in in_cmdline: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 683 | common.ZipWriteStr(output_zip, "BOOT/cmdline", in_cmdline) |
| 684 | return |
| 685 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 686 | out_buffer = [] |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 687 | for param in in_cmdline.split(): |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 688 | if "veritykeyid" not in param: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 689 | out_buffer.append(param) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 690 | continue |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 691 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 692 | # Extract keyid using openssl command. |
| 693 | p = common.Run(["openssl", "x509", "-in", key_path, "-text"], |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 694 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 695 | keyid, stderr = p.communicate() |
| 696 | assert p.returncode == 0, "Failed to dump certificate: {}".format(stderr) |
| 697 | keyid = re.search( |
| 698 | r'keyid:([0-9a-fA-F:]*)', keyid).group(1).replace(':', '').lower() |
| 699 | print("Replacing verity keyid with {}".format(keyid)) |
| 700 | out_buffer.append("veritykeyid=id:%s" % (keyid,)) |
| 701 | |
| 702 | out_cmdline = ' '.join(out_buffer).strip() + '\n' |
| 703 | common.ZipWriteStr(output_zip, "BOOT/cmdline", out_cmdline) |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 704 | |
| 705 | |
| 706 | def ReplaceMiscInfoTxt(input_zip, output_zip, misc_info): |
| 707 | """Replaces META/misc_info.txt. |
| 708 | |
| 709 | Only writes back the ones in the original META/misc_info.txt. Because the |
| 710 | current in-memory dict contains additional items computed at runtime. |
| 711 | """ |
| 712 | misc_info_old = common.LoadDictionaryFromLines( |
| 713 | input_zip.read('META/misc_info.txt').split('\n')) |
| 714 | items = [] |
| 715 | for key in sorted(misc_info): |
| 716 | if key in misc_info_old: |
| 717 | items.append('%s=%s' % (key, misc_info[key])) |
| 718 | common.ZipWriteStr(output_zip, "META/misc_info.txt", '\n'.join(items)) |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 719 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 720 | |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 721 | def ReplaceAvbSigningKeys(misc_info): |
| 722 | """Replaces the AVB signing keys.""" |
| 723 | |
| 724 | AVB_FOOTER_ARGS_BY_PARTITION = { |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 725 | 'boot' : 'avb_boot_add_hash_footer_args', |
| 726 | 'dtbo' : 'avb_dtbo_add_hash_footer_args', |
| 727 | 'recovery' : 'avb_recovery_add_hash_footer_args', |
| 728 | 'system' : 'avb_system_add_hashtree_footer_args', |
| 729 | 'vendor' : 'avb_vendor_add_hashtree_footer_args', |
| 730 | 'vbmeta' : 'avb_vbmeta_args', |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | def ReplaceAvbPartitionSigningKey(partition): |
| 734 | key = OPTIONS.avb_keys.get(partition) |
| 735 | if not key: |
| 736 | return |
| 737 | |
| 738 | algorithm = OPTIONS.avb_algorithms.get(partition) |
| 739 | assert algorithm, 'Missing AVB signing algorithm for %s' % (partition,) |
| 740 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 741 | print('Replacing AVB signing key for %s with "%s" (%s)' % ( |
| 742 | partition, key, algorithm)) |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 743 | misc_info['avb_' + partition + '_algorithm'] = algorithm |
| 744 | misc_info['avb_' + partition + '_key_path'] = key |
| 745 | |
| 746 | extra_args = OPTIONS.avb_extra_args.get(partition) |
| 747 | if extra_args: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 748 | print('Setting extra AVB signing args for %s to "%s"' % ( |
| 749 | partition, extra_args)) |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 750 | args_key = AVB_FOOTER_ARGS_BY_PARTITION[partition] |
| 751 | misc_info[args_key] = (misc_info.get(args_key, '') + ' ' + extra_args) |
| 752 | |
| 753 | for partition in AVB_FOOTER_ARGS_BY_PARTITION: |
| 754 | ReplaceAvbPartitionSigningKey(partition) |
| 755 | |
| 756 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 757 | def BuildKeyMap(misc_info, key_mapping_options): |
| 758 | for s, d in key_mapping_options: |
| 759 | if s is None: # -d option |
| 760 | devkey = misc_info.get("default_system_dev_certificate", |
| 761 | "build/target/product/security/testkey") |
| 762 | devkeydir = os.path.dirname(devkey) |
| 763 | |
| 764 | OPTIONS.key_map.update({ |
| 765 | devkeydir + "/testkey": d + "/releasekey", |
| 766 | devkeydir + "/devkey": d + "/releasekey", |
| 767 | devkeydir + "/media": d + "/media", |
| 768 | devkeydir + "/shared": d + "/shared", |
| 769 | devkeydir + "/platform": d + "/platform", |
| 770 | }) |
| 771 | else: |
| 772 | OPTIONS.key_map[s] = d |
| 773 | |
| 774 | |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 775 | def GetApiLevelAndCodename(input_tf_zip): |
| 776 | data = input_tf_zip.read("SYSTEM/build.prop") |
| 777 | api_level = None |
| 778 | codename = None |
| 779 | for line in data.split("\n"): |
| 780 | line = line.strip() |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 781 | if line and line[0] != '#' and "=" in line: |
| 782 | key, value = line.split("=", 1) |
| 783 | key = key.strip() |
| 784 | if key == "ro.build.version.sdk": |
| 785 | api_level = int(value.strip()) |
| 786 | elif key == "ro.build.version.codename": |
| 787 | codename = value.strip() |
| 788 | |
| 789 | if api_level is None: |
| 790 | raise ValueError("No ro.build.version.sdk in SYSTEM/build.prop") |
| 791 | if codename is None: |
| 792 | raise ValueError("No ro.build.version.codename in SYSTEM/build.prop") |
| 793 | |
| 794 | return (api_level, codename) |
| 795 | |
| 796 | |
| 797 | def GetCodenameToApiLevelMap(input_tf_zip): |
| 798 | data = input_tf_zip.read("SYSTEM/build.prop") |
| 799 | api_level = None |
| 800 | codenames = None |
| 801 | for line in data.split("\n"): |
| 802 | line = line.strip() |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 803 | if line and line[0] != '#' and "=" in line: |
| 804 | key, value = line.split("=", 1) |
| 805 | key = key.strip() |
| 806 | if key == "ro.build.version.sdk": |
| 807 | api_level = int(value.strip()) |
| 808 | elif key == "ro.build.version.all_codenames": |
| 809 | codenames = value.strip().split(",") |
| 810 | |
| 811 | if api_level is None: |
| 812 | raise ValueError("No ro.build.version.sdk in SYSTEM/build.prop") |
| 813 | if codenames is None: |
| 814 | raise ValueError("No ro.build.version.all_codenames in SYSTEM/build.prop") |
| 815 | |
| 816 | result = dict() |
| 817 | for codename in codenames: |
| 818 | codename = codename.strip() |
| 819 | if len(codename) > 0: |
| 820 | result[codename] = api_level |
| 821 | return result |
| 822 | |
| 823 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 824 | def main(argv): |
| 825 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 826 | key_mapping_options = [] |
| 827 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 828 | def option_handler(o, a): |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 829 | if o in ("-e", "--extra_apks"): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 830 | names, key = a.split("=") |
| 831 | names = names.split(",") |
| 832 | for n in names: |
| 833 | OPTIONS.extra_apks[n] = key |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 834 | elif o == "--skip_apks_with_path_prefix": |
| 835 | # Sanity check the prefix, which must be in all upper case. |
| 836 | prefix = a.split('/')[0] |
| 837 | if not prefix or prefix != prefix.upper(): |
| 838 | raise ValueError("Invalid path prefix '%s'" % (a,)) |
| 839 | OPTIONS.skip_apks_with_path_prefix.add(a) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 840 | elif o in ("-d", "--default_key_mappings"): |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 841 | key_mapping_options.append((None, a)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 842 | elif o in ("-k", "--key_mapping"): |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 843 | key_mapping_options.append(a.split("=", 1)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 844 | elif o in ("-o", "--replace_ota_keys"): |
| 845 | OPTIONS.replace_ota_keys = True |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 846 | elif o in ("-t", "--tag_changes"): |
| 847 | new = [] |
| 848 | for i in a.split(","): |
| 849 | i = i.strip() |
| 850 | if not i or i[0] not in "-+": |
| 851 | raise ValueError("Bad tag change '%s'" % (i,)) |
| 852 | new.append(i[0] + i[1:].strip()) |
| 853 | OPTIONS.tag_changes = tuple(new) |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 854 | elif o == "--replace_verity_public_key": |
| 855 | OPTIONS.replace_verity_public_key = (True, a) |
| 856 | elif o == "--replace_verity_private_key": |
| 857 | OPTIONS.replace_verity_private_key = (True, a) |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 858 | elif o == "--replace_verity_keyid": |
| 859 | OPTIONS.replace_verity_keyid = (True, a) |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 860 | elif o == "--avb_vbmeta_key": |
| 861 | OPTIONS.avb_keys['vbmeta'] = a |
| 862 | elif o == "--avb_vbmeta_algorithm": |
| 863 | OPTIONS.avb_algorithms['vbmeta'] = a |
| 864 | elif o == "--avb_vbmeta_extra_args": |
| 865 | OPTIONS.avb_extra_args['vbmeta'] = a |
| 866 | elif o == "--avb_boot_key": |
| 867 | OPTIONS.avb_keys['boot'] = a |
| 868 | elif o == "--avb_boot_algorithm": |
| 869 | OPTIONS.avb_algorithms['boot'] = a |
| 870 | elif o == "--avb_boot_extra_args": |
| 871 | OPTIONS.avb_extra_args['boot'] = a |
| 872 | elif o == "--avb_dtbo_key": |
| 873 | OPTIONS.avb_keys['dtbo'] = a |
| 874 | elif o == "--avb_dtbo_algorithm": |
| 875 | OPTIONS.avb_algorithms['dtbo'] = a |
| 876 | elif o == "--avb_dtbo_extra_args": |
| 877 | OPTIONS.avb_extra_args['dtbo'] = a |
| 878 | elif o == "--avb_system_key": |
| 879 | OPTIONS.avb_keys['system'] = a |
| 880 | elif o == "--avb_system_algorithm": |
| 881 | OPTIONS.avb_algorithms['system'] = a |
| 882 | elif o == "--avb_system_extra_args": |
| 883 | OPTIONS.avb_extra_args['system'] = a |
| 884 | elif o == "--avb_vendor_key": |
| 885 | OPTIONS.avb_keys['vendor'] = a |
| 886 | elif o == "--avb_vendor_algorithm": |
| 887 | OPTIONS.avb_algorithms['vendor'] = a |
| 888 | elif o == "--avb_vendor_extra_args": |
| 889 | OPTIONS.avb_extra_args['vendor'] = a |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 890 | else: |
| 891 | return False |
| 892 | return True |
| 893 | |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 894 | args = common.ParseOptions( |
| 895 | argv, __doc__, |
| 896 | extra_opts="e:d:k:ot:", |
| 897 | extra_long_opts=[ |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 898 | "extra_apks=", |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 899 | "skip_apks_with_path_prefix=", |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 900 | "default_key_mappings=", |
| 901 | "key_mapping=", |
| 902 | "replace_ota_keys", |
| 903 | "tag_changes=", |
| 904 | "replace_verity_public_key=", |
| 905 | "replace_verity_private_key=", |
| 906 | "replace_verity_keyid=", |
| 907 | "avb_vbmeta_algorithm=", |
| 908 | "avb_vbmeta_key=", |
| 909 | "avb_vbmeta_extra_args=", |
| 910 | "avb_boot_algorithm=", |
| 911 | "avb_boot_key=", |
| 912 | "avb_boot_extra_args=", |
| 913 | "avb_dtbo_algorithm=", |
| 914 | "avb_dtbo_key=", |
| 915 | "avb_dtbo_extra_args=", |
| 916 | "avb_system_algorithm=", |
| 917 | "avb_system_key=", |
| 918 | "avb_system_extra_args=", |
| 919 | "avb_vendor_algorithm=", |
| 920 | "avb_vendor_key=", |
| 921 | "avb_vendor_extra_args=", |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 922 | ], |
| 923 | extra_option_handler=option_handler) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 924 | |
| 925 | if len(args) != 2: |
| 926 | common.Usage(__doc__) |
| 927 | sys.exit(1) |
| 928 | |
| 929 | input_zip = zipfile.ZipFile(args[0], "r") |
Tao Bao | 2b8f489 | 2017-06-13 12:54:58 -0700 | [diff] [blame] | 930 | output_zip = zipfile.ZipFile(args[1], "w", |
| 931 | compression=zipfile.ZIP_DEFLATED, |
| 932 | allowZip64=True) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 933 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 934 | misc_info = common.LoadInfoDict(input_zip) |
| 935 | |
| 936 | BuildKeyMap(misc_info, key_mapping_options) |
| 937 | |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 938 | certmap, compressed_extension = common.ReadApkCerts(input_zip) |
| 939 | apk_key_map = GetApkCerts(certmap) |
| 940 | CheckAllApksSigned(input_zip, apk_key_map, compressed_extension) |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 941 | |
| 942 | key_passwords = common.GetKeyPasswords(set(apk_key_map.values())) |
Tao Bao | 9aa4b9b | 2016-09-29 17:53:56 -0700 | [diff] [blame] | 943 | platform_api_level, _ = GetApiLevelAndCodename(input_zip) |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 944 | codename_to_api_level_map = GetCodenameToApiLevelMap(input_zip) |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 945 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 946 | ProcessTargetFiles(input_zip, output_zip, misc_info, |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 947 | apk_key_map, key_passwords, |
| 948 | platform_api_level, |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 949 | codename_to_api_level_map, |
| 950 | compressed_extension) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 951 | |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 952 | common.ZipClose(input_zip) |
| 953 | common.ZipClose(output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 954 | |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 955 | # Skip building userdata.img and cache.img when signing the target files. |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 956 | new_args = ["--is_signing"] |
| 957 | # add_img_to_target_files builds the system image from scratch, so the |
| 958 | # recovery patch is guaranteed to be regenerated there. |
| 959 | if OPTIONS.rebuild_recovery: |
| 960 | new_args.append("--rebuild_recovery") |
| 961 | new_args.append(args[1]) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 962 | add_img_to_target_files.main(new_args) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 963 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 964 | print("done.") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 965 | |
| 966 | |
| 967 | if __name__ == '__main__': |
| 968 | try: |
| 969 | main(sys.argv[1:]) |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 970 | except common.ExternalError as e: |
| 971 | print("\n ERROR: %s\n" % (e,)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 972 | sys.exit(1) |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 973 | finally: |
| 974 | common.Cleanup() |