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> |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 24 | Add extra APK/APEX name/key pairs as though they appeared in apkcerts.txt |
| 25 | or apexkeys.txt (so mappings specified by -k and -d are applied). Keys |
| 26 | specified in -e override any value for that app contained in the |
| 27 | apkcerts.txt file, or the container key for an APEX. Option may be |
| 28 | repeated to give multiple extra packages. |
| 29 | |
| 30 | --extra_apex_payload_key <name=key> |
| 31 | Add a mapping for APEX package name to payload signing key, which will |
| 32 | override the default payload signing key in apexkeys.txt. Note that the |
| 33 | container key should be overridden via the `--extra_apks` flag above. |
| 34 | Option may be repeated for multiple APEXes. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 35 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 36 | --skip_apks_with_path_prefix <prefix> |
| 37 | Skip signing an APK if it has the matching prefix in its path. The prefix |
| 38 | should be matching the entry name, which has partition names in upper |
| 39 | case, e.g. "VENDOR/app/", or "SYSTEM_OTHER/preloads/". Option may be |
| 40 | repeated to give multiple prefixes. |
| 41 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 42 | -k (--key_mapping) <src_key=dest_key> |
| 43 | Add a mapping from the key name as specified in apkcerts.txt (the |
| 44 | src_key) to the real key you wish to sign the package with |
| 45 | (dest_key). Option may be repeated to give multiple key |
| 46 | mappings. |
| 47 | |
| 48 | -d (--default_key_mappings) <dir> |
| 49 | Set up the following key mappings: |
| 50 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 51 | $devkey/devkey ==> $dir/releasekey |
| 52 | $devkey/testkey ==> $dir/releasekey |
| 53 | $devkey/media ==> $dir/media |
| 54 | $devkey/shared ==> $dir/shared |
| 55 | $devkey/platform ==> $dir/platform |
| 56 | |
| 57 | where $devkey is the directory part of the value of |
| 58 | default_system_dev_certificate from the input target-files's |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 59 | META/misc_info.txt. (Defaulting to "build/make/target/product/security" |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 60 | if the value is not present in misc_info. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 61 | |
| 62 | -d and -k options are added to the set of mappings in the order |
| 63 | in which they appear on the command line. |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 64 | |
| 65 | -o (--replace_ota_keys) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 66 | Replace the certificate (public key) used by OTA package verification |
| 67 | with the ones specified in the input target_files zip (in the |
| 68 | META/otakeys.txt file). Key remapping (-k and -d) is performed on the |
| 69 | keys. For A/B devices, the payload verification key will be replaced |
| 70 | as well. If there're multiple OTA keys, only the first one will be used |
| 71 | for payload verification. |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 72 | |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 73 | -t (--tag_changes) <+tag>,<-tag>,... |
| 74 | Comma-separated list of changes to make to the set of tags (in |
| 75 | the last component of the build fingerprint). Prefix each with |
| 76 | '+' or '-' to indicate whether that tag should be added or |
| 77 | removed. Changes are processed in the order they appear. |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 78 | Default value is "-test-keys,-dev-keys,+release-keys". |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 79 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 80 | --replace_verity_private_key <key> |
| 81 | Replace the private key used for verity signing. It expects a filename |
| 82 | WITHOUT the extension (e.g. verity_key). |
| 83 | |
| 84 | --replace_verity_public_key <key> |
| 85 | Replace the certificate (public key) used for verity verification. The |
| 86 | key file replaces the one at BOOT/RAMDISK/verity_key (or ROOT/verity_key |
| 87 | for devices using system_root_image). It expects the key filename WITH |
| 88 | the extension (e.g. verity_key.pub). |
| 89 | |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 90 | --replace_verity_keyid <path_to_X509_PEM_cert_file> |
| 91 | Replace the veritykeyid in BOOT/cmdline of input_target_file_zip |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 92 | 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] | 93 | |
Bowgo Tsai | e4544b1 | 2019-02-27 10:15:51 +0800 | [diff] [blame] | 94 | --avb_{boot,system,system_other,vendor,dtbo,vbmeta}_algorithm <algorithm> |
| 95 | --avb_{boot,system,system_other,vendor,dtbo,vbmeta}_key <key> |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 96 | Use the specified algorithm (e.g. SHA256_RSA4096) and the key to AVB-sign |
| 97 | the specified image. Otherwise it uses the existing values in info dict. |
| 98 | |
Bowgo Tsai | e4544b1 | 2019-02-27 10:15:51 +0800 | [diff] [blame] | 99 | --avb_{apex,boot,system,system_other,vendor,dtbo,vbmeta}_extra_args <args> |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 100 | Specify any additional args that are needed to AVB-sign the image |
| 101 | (e.g. "--signing_helper /path/to/helper"). The args will be appended to |
| 102 | the existing ones in info dict. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 103 | """ |
| 104 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 105 | from __future__ import print_function |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 106 | |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 107 | import base64 |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 108 | import copy |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 109 | import errno |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 110 | import gzip |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 111 | import itertools |
Tao Bao | badceb2 | 2019-03-15 09:33:43 -0700 | [diff] [blame] | 112 | import logging |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 113 | import os |
| 114 | import re |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 115 | import shutil |
Tao Bao | 9fdd00f | 2017-07-12 11:57:05 -0700 | [diff] [blame] | 116 | import stat |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 117 | import subprocess |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 118 | import sys |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 119 | import tempfile |
| 120 | import zipfile |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 121 | from xml.etree import ElementTree |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 122 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 123 | import add_img_to_target_files |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 124 | import apex_utils |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 125 | import common |
| 126 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 127 | |
| 128 | if sys.hexversion < 0x02070000: |
| 129 | print("Python 2.7 or newer is required.", file=sys.stderr) |
| 130 | sys.exit(1) |
| 131 | |
| 132 | |
Tao Bao | badceb2 | 2019-03-15 09:33:43 -0700 | [diff] [blame] | 133 | logger = logging.getLogger(__name__) |
| 134 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 135 | OPTIONS = common.OPTIONS |
| 136 | |
| 137 | OPTIONS.extra_apks = {} |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 138 | OPTIONS.extra_apex_payload_keys = {} |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 139 | OPTIONS.skip_apks_with_path_prefix = set() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 140 | OPTIONS.key_map = {} |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 141 | OPTIONS.rebuild_recovery = False |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 142 | OPTIONS.replace_ota_keys = False |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 143 | OPTIONS.replace_verity_public_key = False |
| 144 | OPTIONS.replace_verity_private_key = False |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 145 | OPTIONS.replace_verity_keyid = False |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 146 | OPTIONS.tag_changes = ("-test-keys", "-dev-keys", "+release-keys") |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 147 | OPTIONS.avb_keys = {} |
| 148 | OPTIONS.avb_algorithms = {} |
| 149 | OPTIONS.avb_extra_args = {} |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 150 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 151 | |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 152 | def GetApkCerts(certmap): |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 153 | # apply the key remapping to the contents of the file |
| 154 | for apk, cert in certmap.iteritems(): |
| 155 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
| 156 | |
| 157 | # apply all the -e options, overriding anything in the file |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 158 | for apk, cert in OPTIONS.extra_apks.iteritems(): |
Doug Zongker | decf995 | 2009-12-15 17:27:49 -0800 | [diff] [blame] | 159 | if not cert: |
| 160 | cert = "PRESIGNED" |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 161 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 162 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 163 | return certmap |
| 164 | |
| 165 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 166 | def GetApexKeys(keys_info, key_map): |
| 167 | """Gets APEX payload and container signing keys by applying the mapping rules. |
| 168 | |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 169 | Presigned payload / container keys will be set accordingly. |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 170 | |
| 171 | Args: |
| 172 | keys_info: A dict that maps from APEX filenames to a tuple of (payload_key, |
| 173 | container_key). |
| 174 | key_map: A dict that overrides the keys, specified via command-line input. |
| 175 | |
| 176 | Returns: |
| 177 | A dict that contains the updated APEX key mapping, which should be used for |
| 178 | the current signing. |
Tao Bao | f98fa10 | 2019-04-24 14:51:25 -0700 | [diff] [blame] | 179 | |
| 180 | Raises: |
| 181 | AssertionError: On invalid container / payload key overrides. |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 182 | """ |
| 183 | # Apply all the --extra_apex_payload_key options to override the payload |
| 184 | # signing keys in the given keys_info. |
| 185 | for apex, key in OPTIONS.extra_apex_payload_keys.items(): |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 186 | if not key: |
| 187 | key = 'PRESIGNED' |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 188 | keys_info[apex] = (key, keys_info[apex][1]) |
| 189 | |
| 190 | # Apply the key remapping to container keys. |
| 191 | for apex, (payload_key, container_key) in keys_info.items(): |
| 192 | keys_info[apex] = (payload_key, key_map.get(container_key, container_key)) |
| 193 | |
| 194 | # Apply all the --extra_apks options to override the container keys. |
| 195 | for apex, key in OPTIONS.extra_apks.items(): |
| 196 | # Skip non-APEX containers. |
| 197 | if apex not in keys_info: |
| 198 | continue |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 199 | if not key: |
| 200 | key = 'PRESIGNED' |
Tao Bao | fa9de0a | 2019-03-18 10:24:17 -0700 | [diff] [blame] | 201 | keys_info[apex] = (keys_info[apex][0], key_map.get(key, key)) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 202 | |
Tao Bao | f98fa10 | 2019-04-24 14:51:25 -0700 | [diff] [blame] | 203 | # A PRESIGNED container entails a PRESIGNED payload. Apply this to all the |
| 204 | # APEX key pairs. However, a PRESIGNED container with non-PRESIGNED payload |
| 205 | # (overridden via commandline) indicates a config error, which should not be |
| 206 | # allowed. |
| 207 | for apex, (payload_key, container_key) in keys_info.items(): |
| 208 | if container_key != 'PRESIGNED': |
| 209 | continue |
| 210 | if apex in OPTIONS.extra_apex_payload_keys: |
| 211 | payload_override = OPTIONS.extra_apex_payload_keys[apex] |
| 212 | assert payload_override == '', \ |
| 213 | ("Invalid APEX key overrides: {} has PRESIGNED container but " |
| 214 | "non-PRESIGNED payload key {}").format(apex, payload_override) |
| 215 | if payload_key != 'PRESIGNED': |
| 216 | print( |
| 217 | "Setting {} payload as PRESIGNED due to PRESIGNED container".format( |
| 218 | apex)) |
| 219 | keys_info[apex] = ('PRESIGNED', 'PRESIGNED') |
| 220 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 221 | return keys_info |
| 222 | |
| 223 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 224 | def GetApkFileInfo(filename, compressed_extension, skipped_prefixes): |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 225 | """Returns the APK info based on the given filename. |
| 226 | |
| 227 | 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] | 228 | compressed extension into consideration. If it appears to be an APK file, |
| 229 | further checks if the APK file should be skipped when signing, based on the |
| 230 | given path prefixes. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 231 | |
| 232 | Args: |
| 233 | filename: Path to the file. |
| 234 | compressed_extension: The extension string of compressed APKs (e.g. ".gz"), |
| 235 | or None if there's no compressed APKs. |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 236 | 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] | 237 | |
| 238 | Returns: |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 239 | (is_apk, is_compressed, should_be_skipped): is_apk indicates whether the |
| 240 | given filename is an APK file. is_compressed indicates whether the APK file |
| 241 | is compressed (only meaningful when is_apk is True). should_be_skipped |
| 242 | indicates whether the filename matches any of the given prefixes to be |
| 243 | skipped. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 244 | |
| 245 | Raises: |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 246 | AssertionError: On invalid compressed_extension or skipped_prefixes inputs. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 247 | """ |
| 248 | assert compressed_extension is None or compressed_extension.startswith('.'), \ |
| 249 | "Invalid compressed_extension arg: '{}'".format(compressed_extension) |
| 250 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 251 | # skipped_prefixes should be one of set/list/tuple types. Other types such as |
| 252 | # str shouldn't be accepted. |
Tao Bao | badceb2 | 2019-03-15 09:33:43 -0700 | [diff] [blame] | 253 | assert isinstance(skipped_prefixes, (set, list, tuple)), \ |
| 254 | "Invalid skipped_prefixes input type: {}".format(type(skipped_prefixes)) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 255 | |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 256 | compressed_apk_extension = ( |
| 257 | ".apk" + compressed_extension if compressed_extension else None) |
| 258 | is_apk = (filename.endswith(".apk") or |
| 259 | (compressed_apk_extension and |
| 260 | filename.endswith(compressed_apk_extension))) |
| 261 | if not is_apk: |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 262 | return (False, False, False) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 263 | |
| 264 | is_compressed = (compressed_apk_extension and |
| 265 | filename.endswith(compressed_apk_extension)) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 266 | should_be_skipped = filename.startswith(tuple(skipped_prefixes)) |
| 267 | return (True, is_compressed, should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 268 | |
| 269 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 270 | def CheckApkAndApexKeysAvailable(input_tf_zip, known_keys, |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 271 | compressed_extension, apex_keys): |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 272 | """Checks that all the APKs and APEXes have keys specified. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 273 | |
| 274 | Args: |
| 275 | input_tf_zip: An open target_files zip file. |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 276 | known_keys: A set of APKs and APEXes that have known signing keys. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 277 | compressed_extension: The extension string of compressed APKs, such as |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 278 | '.gz', or None if there's no compressed APKs. |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 279 | apex_keys: A dict that contains the key mapping from APEX name to |
| 280 | (payload_key, container_key). |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 281 | |
| 282 | Raises: |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 283 | AssertionError: On finding unknown APKs and APEXes. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 284 | """ |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 285 | unknown_files = [] |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 286 | for info in input_tf_zip.infolist(): |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 287 | # Handle APEXes first, e.g. SYSTEM/apex/com.android.tzdata.apex. |
| 288 | if (info.filename.startswith('SYSTEM/apex') and |
| 289 | info.filename.endswith('.apex')): |
| 290 | name = os.path.basename(info.filename) |
| 291 | if name not in known_keys: |
| 292 | unknown_files.append(name) |
| 293 | continue |
| 294 | |
| 295 | # And APKs. |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 296 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 297 | info.filename, compressed_extension, OPTIONS.skip_apks_with_path_prefix) |
| 298 | if not is_apk or should_be_skipped: |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 299 | continue |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 300 | |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 301 | name = os.path.basename(info.filename) |
| 302 | if is_compressed: |
| 303 | name = name[:-len(compressed_extension)] |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 304 | if name not in known_keys: |
| 305 | unknown_files.append(name) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 306 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 307 | assert not unknown_files, \ |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 308 | ("No key specified for:\n {}\n" |
| 309 | "Use '-e <apkname>=' to specify a key (which may be an empty string to " |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 310 | "not sign this apk).".format("\n ".join(unknown_files))) |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 311 | |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 312 | # For all the APEXes, double check that we won't have an APEX that has only |
Tao Bao | f98fa10 | 2019-04-24 14:51:25 -0700 | [diff] [blame] | 313 | # one of the payload / container keys set. Note that non-PRESIGNED container |
| 314 | # with PRESIGNED payload could be allowed but currently unsupported. It would |
| 315 | # require changing SignApex implementation. |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 316 | if not apex_keys: |
| 317 | return |
| 318 | |
| 319 | invalid_apexes = [] |
| 320 | for info in input_tf_zip.infolist(): |
| 321 | if (not info.filename.startswith('SYSTEM/apex') or |
| 322 | not info.filename.endswith('.apex')): |
| 323 | continue |
| 324 | |
| 325 | name = os.path.basename(info.filename) |
| 326 | (payload_key, container_key) = apex_keys[name] |
| 327 | if ((payload_key in common.SPECIAL_CERT_STRINGS and |
| 328 | container_key not in common.SPECIAL_CERT_STRINGS) or |
| 329 | (payload_key not in common.SPECIAL_CERT_STRINGS and |
| 330 | container_key in common.SPECIAL_CERT_STRINGS)): |
| 331 | invalid_apexes.append( |
| 332 | "{}: payload_key {}, container_key {}".format( |
| 333 | name, payload_key, container_key)) |
| 334 | |
| 335 | assert not invalid_apexes, \ |
| 336 | "Invalid APEX keys specified:\n {}\n".format( |
| 337 | "\n ".join(invalid_apexes)) |
| 338 | |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 339 | |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 340 | def SignApk(data, keyname, pw, platform_api_level, codename_to_api_level_map, |
| 341 | is_compressed): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 342 | unsigned = tempfile.NamedTemporaryFile() |
| 343 | unsigned.write(data) |
| 344 | unsigned.flush() |
| 345 | |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 346 | if is_compressed: |
| 347 | uncompressed = tempfile.NamedTemporaryFile() |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 348 | with gzip.open(unsigned.name, "rb") as in_file, \ |
| 349 | open(uncompressed.name, "wb") as out_file: |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 350 | shutil.copyfileobj(in_file, out_file) |
| 351 | |
| 352 | # Finally, close the "unsigned" file (which is gzip compressed), and then |
| 353 | # replace it with the uncompressed version. |
| 354 | # |
| 355 | # TODO(narayan): All this nastiness can be avoided if python 3.2 is in use, |
| 356 | # we could just gzip / gunzip in-memory buffers instead. |
| 357 | unsigned.close() |
| 358 | unsigned = uncompressed |
| 359 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 360 | signed = tempfile.NamedTemporaryFile() |
| 361 | |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 362 | # For pre-N builds, don't upgrade to SHA-256 JAR signatures based on the APK's |
| 363 | # minSdkVersion to avoid increasing incremental OTA update sizes. If an APK |
| 364 | # didn't change, we don't want its signature to change due to the switch |
| 365 | # from SHA-1 to SHA-256. |
| 366 | # By default, APK signer chooses SHA-256 signatures if the APK's minSdkVersion |
| 367 | # is 18 or higher. For pre-N builds we disable this mechanism by pretending |
| 368 | # that the APK's minSdkVersion is 1. |
| 369 | # For N+ builds, we let APK signer rely on the APK's minSdkVersion to |
| 370 | # determine whether to use SHA-256. |
| 371 | min_api_level = None |
| 372 | if platform_api_level > 23: |
| 373 | # Let APK signer choose whether to use SHA-1 or SHA-256, based on the APK's |
| 374 | # minSdkVersion attribute |
| 375 | min_api_level = None |
| 376 | else: |
| 377 | # Force APK signer to use SHA-1 |
| 378 | min_api_level = 1 |
| 379 | |
| 380 | common.SignFile(unsigned.name, signed.name, keyname, pw, |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 381 | min_api_level=min_api_level, |
| 382 | codename_to_api_level_map=codename_to_api_level_map) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 383 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 384 | data = None |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 385 | if is_compressed: |
| 386 | # Recompress the file after it has been signed. |
| 387 | compressed = tempfile.NamedTemporaryFile() |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 388 | with open(signed.name, "rb") as in_file, \ |
| 389 | gzip.open(compressed.name, "wb") as out_file: |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 390 | shutil.copyfileobj(in_file, out_file) |
| 391 | |
| 392 | data = compressed.read() |
| 393 | compressed.close() |
| 394 | else: |
| 395 | data = signed.read() |
| 396 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 397 | unsigned.close() |
| 398 | signed.close() |
| 399 | |
| 400 | return data |
| 401 | |
| 402 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 403 | def SignApex(apex_data, payload_key, container_key, container_pw, |
| 404 | codename_to_api_level_map, signing_args=None): |
| 405 | """Signs the current APEX with the given payload/container keys. |
| 406 | |
| 407 | Args: |
| 408 | apex_data: Raw APEX data. |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 409 | payload_key: The path to payload signing key (w/ extension). |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 410 | container_key: The path to container signing key (w/o extension). |
| 411 | container_pw: The matching password of the container_key, or None. |
| 412 | codename_to_api_level_map: A dict that maps from codename to API level. |
| 413 | signing_args: Additional args to be passed to the payload signer. |
| 414 | |
| 415 | Returns: |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 416 | The path to the signed APEX file. |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 417 | """ |
| 418 | apex_file = common.MakeTempFile(prefix='apex-', suffix='.apex') |
| 419 | with open(apex_file, 'wb') as apex_fp: |
| 420 | apex_fp.write(apex_data) |
| 421 | |
| 422 | APEX_PAYLOAD_IMAGE = 'apex_payload.img' |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 423 | APEX_PUBKEY = 'apex_pubkey' |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 424 | |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 425 | # 1a. Extract and sign the APEX_PAYLOAD_IMAGE entry with the given |
| 426 | # payload_key. |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 427 | payload_dir = common.MakeTempDir(prefix='apex-payload-') |
| 428 | with zipfile.ZipFile(apex_file) as apex_fd: |
| 429 | payload_file = apex_fd.extract(APEX_PAYLOAD_IMAGE, payload_dir) |
| 430 | |
| 431 | payload_info = apex_utils.ParseApexPayloadInfo(payload_file) |
| 432 | apex_utils.SignApexPayload( |
| 433 | payload_file, |
| 434 | payload_key, |
| 435 | payload_info['apex.key'], |
| 436 | payload_info['Algorithm'], |
| 437 | payload_info['Salt'], |
| 438 | signing_args) |
| 439 | |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 440 | # 1b. Update the embedded payload public key. |
| 441 | payload_public_key = common.ExtractAvbPublicKey(payload_key) |
| 442 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 443 | common.ZipDelete(apex_file, APEX_PAYLOAD_IMAGE) |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 444 | common.ZipDelete(apex_file, APEX_PUBKEY) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 445 | apex_zip = zipfile.ZipFile(apex_file, 'a') |
| 446 | common.ZipWrite(apex_zip, payload_file, arcname=APEX_PAYLOAD_IMAGE) |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 447 | common.ZipWrite(apex_zip, payload_public_key, arcname=APEX_PUBKEY) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 448 | common.ZipClose(apex_zip) |
| 449 | |
Tao Bao | ffc9a30 | 2019-03-22 23:16:58 -0700 | [diff] [blame] | 450 | # 2. Align the files at page boundary (same as in apexer). |
| 451 | aligned_apex = common.MakeTempFile( |
| 452 | prefix='apex-container-', suffix='.apex') |
| 453 | common.RunAndCheckOutput( |
| 454 | ['zipalign', '-f', '4096', apex_file, aligned_apex]) |
| 455 | |
| 456 | # 3. Sign the APEX container with container_key. |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 457 | signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.apex') |
Tao Bao | ffc9a30 | 2019-03-22 23:16:58 -0700 | [diff] [blame] | 458 | |
| 459 | # Specify the 4K alignment when calling SignApk. |
| 460 | extra_signapk_args = OPTIONS.extra_signapk_args[:] |
| 461 | extra_signapk_args.extend(['-a', '4096']) |
| 462 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 463 | common.SignFile( |
Tao Bao | ffc9a30 | 2019-03-22 23:16:58 -0700 | [diff] [blame] | 464 | aligned_apex, |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 465 | signed_apex, |
| 466 | container_key, |
| 467 | container_pw, |
Tao Bao | ffc9a30 | 2019-03-22 23:16:58 -0700 | [diff] [blame] | 468 | codename_to_api_level_map=codename_to_api_level_map, |
| 469 | extra_signapk_args=extra_signapk_args) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 470 | |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 471 | return signed_apex |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 472 | |
| 473 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 474 | def ProcessTargetFiles(input_tf_zip, output_tf_zip, misc_info, |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 475 | apk_keys, apex_keys, key_passwords, |
| 476 | platform_api_level, codename_to_api_level_map, |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 477 | compressed_extension): |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 478 | # maxsize measures the maximum filename length, including the ones to be |
| 479 | # skipped. |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 480 | maxsize = max( |
| 481 | [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] | 482 | if GetApkFileInfo(i.filename, compressed_extension, [])[0]]) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 483 | system_root_image = misc_info.get("system_root_image") == "true" |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 484 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 485 | for info in input_tf_zip.infolist(): |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 486 | filename = info.filename |
| 487 | if filename.startswith("IMAGES/"): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 488 | continue |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 489 | |
Tao Bao | 33bf268 | 2019-01-11 12:37:35 -0800 | [diff] [blame] | 490 | # Skip split super images, which will be re-generated during signing. |
| 491 | if filename.startswith("OTA/") and filename.endswith(".img"): |
| 492 | continue |
| 493 | |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 494 | data = input_tf_zip.read(filename) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 495 | out_info = copy.copy(info) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 496 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 497 | filename, compressed_extension, OPTIONS.skip_apks_with_path_prefix) |
| 498 | |
| 499 | if is_apk and should_be_skipped: |
| 500 | # Copy skipped APKs verbatim. |
| 501 | print( |
| 502 | "NOT signing: %s\n" |
| 503 | " (skipped due to matching prefix)" % (filename,)) |
| 504 | common.ZipWriteStr(output_tf_zip, out_info, data) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 505 | |
Tao Bao | f2cffbd | 2015-07-22 12:33:18 -0700 | [diff] [blame] | 506 | # Sign APKs. |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 507 | elif is_apk: |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 508 | name = os.path.basename(filename) |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 509 | if is_compressed: |
| 510 | name = name[:-len(compressed_extension)] |
| 511 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 512 | key = apk_keys[name] |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 513 | if key not in common.SPECIAL_CERT_STRINGS: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 514 | print(" signing: %-*s (%s)" % (maxsize, name, key)) |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 515 | signed_data = SignApk(data, key, key_passwords[key], platform_api_level, |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 516 | codename_to_api_level_map, is_compressed) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 517 | common.ZipWriteStr(output_tf_zip, out_info, signed_data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 518 | else: |
| 519 | # an APK we're not supposed to sign. |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 520 | print( |
| 521 | "NOT signing: %s\n" |
| 522 | " (skipped due to special cert string)" % (name,)) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 523 | common.ZipWriteStr(output_tf_zip, out_info, data) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 524 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 525 | # Sign bundled APEX files. |
| 526 | elif filename.startswith("SYSTEM/apex") and filename.endswith(".apex"): |
| 527 | name = os.path.basename(filename) |
| 528 | payload_key, container_key = apex_keys[name] |
| 529 | |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 530 | # We've asserted not having a case with only one of them PRESIGNED. |
| 531 | if (payload_key not in common.SPECIAL_CERT_STRINGS and |
| 532 | container_key not in common.SPECIAL_CERT_STRINGS): |
| 533 | print(" signing: %-*s container (%s)" % ( |
| 534 | maxsize, name, container_key)) |
| 535 | print(" : %-*s payload (%s)" % ( |
| 536 | maxsize, name, payload_key)) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 537 | |
Tao Bao | 9c0f443 | 2019-04-01 21:25:05 -0700 | [diff] [blame] | 538 | signed_apex = SignApex( |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 539 | data, |
| 540 | payload_key, |
| 541 | container_key, |
| 542 | key_passwords[container_key], |
| 543 | codename_to_api_level_map, |
| 544 | OPTIONS.avb_extra_args.get('apex')) |
| 545 | common.ZipWrite(output_tf_zip, signed_apex, filename) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 546 | |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 547 | else: |
| 548 | print( |
| 549 | "NOT signing: %s\n" |
| 550 | " (skipped due to special cert string)" % (name,)) |
| 551 | common.ZipWriteStr(output_tf_zip, out_info, data) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 552 | |
| 553 | # AVB public keys for the installed APEXes, which will be updated later. |
| 554 | elif (os.path.dirname(filename) == 'SYSTEM/etc/security/apex' and |
| 555 | filename != 'SYSTEM/etc/security/apex/'): |
| 556 | continue |
| 557 | |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 558 | # System properties. |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 559 | elif filename in ("SYSTEM/build.prop", |
| 560 | "VENDOR/build.prop", |
| 561 | "SYSTEM/etc/prop.default", |
| 562 | "BOOT/RAMDISK/prop.default", |
| 563 | "BOOT/RAMDISK/default.prop", # legacy |
| 564 | "ROOT/default.prop", # legacy |
| 565 | "RECOVERY/RAMDISK/prop.default", |
| 566 | "RECOVERY/RAMDISK/default.prop"): # legacy |
| 567 | print("Rewriting %s:" % (filename,)) |
Hung-ying Tyan | 7eb6a92 | 2017-05-01 21:56:26 +0800 | [diff] [blame] | 568 | if stat.S_ISLNK(info.external_attr >> 16): |
| 569 | new_data = data |
| 570 | else: |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 571 | new_data = RewriteProps(data) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 572 | common.ZipWriteStr(output_tf_zip, out_info, new_data) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 573 | |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 574 | # Replace the certs in *mac_permissions.xml (there could be multiple, such |
| 575 | # as {system,vendor}/etc/selinux/{plat,nonplat}_mac_permissions.xml). |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 576 | elif filename.endswith("mac_permissions.xml"): |
| 577 | print("Rewriting %s with new keys." % (filename,)) |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 578 | new_data = ReplaceCerts(data) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 579 | common.ZipWriteStr(output_tf_zip, out_info, new_data) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 580 | |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 581 | # 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] | 582 | elif filename in ("SYSTEM/recovery-from-boot.p", |
| 583 | "SYSTEM/etc/recovery.img", |
| 584 | "SYSTEM/bin/install-recovery.sh"): |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 585 | OPTIONS.rebuild_recovery = True |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 586 | |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 587 | # Don't copy OTA certs if we're replacing them. |
Tao Bao | 696bb33 | 2018-08-17 16:27:01 -0700 | [diff] [blame] | 588 | elif ( |
| 589 | OPTIONS.replace_ota_keys and |
| 590 | filename in ( |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 591 | "BOOT/RAMDISK/system/etc/security/otacerts.zip", |
Tao Bao | 696bb33 | 2018-08-17 16:27:01 -0700 | [diff] [blame] | 592 | "BOOT/RAMDISK/system/etc/update_engine/update-payload-key.pub.pem", |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 593 | "RECOVERY/RAMDISK/system/etc/security/otacerts.zip", |
Tao Bao | 696bb33 | 2018-08-17 16:27:01 -0700 | [diff] [blame] | 594 | "SYSTEM/etc/security/otacerts.zip", |
| 595 | "SYSTEM/etc/update_engine/update-payload-key.pub.pem")): |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 596 | pass |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 597 | |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 598 | # 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] | 599 | elif filename == "META/misc_info.txt": |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 600 | pass |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 601 | |
| 602 | # Skip verity public key if we will replace it. |
Michael Runge | 947894f | 2014-10-14 20:58:38 -0700 | [diff] [blame] | 603 | elif (OPTIONS.replace_verity_public_key and |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 604 | filename in ("BOOT/RAMDISK/verity_key", |
| 605 | "ROOT/verity_key")): |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 606 | pass |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 607 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 608 | # 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] | 609 | elif OPTIONS.replace_verity_keyid and filename == "BOOT/cmdline": |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 610 | pass |
| 611 | |
Tianjie Xu | 4f09900 | 2016-08-11 18:04:27 -0700 | [diff] [blame] | 612 | # Skip the care_map as we will regenerate the system/vendor images. |
Tianjie Xu | 4c05f4a | 2018-09-14 16:24:41 -0700 | [diff] [blame] | 613 | elif filename == "META/care_map.pb" or filename == "META/care_map.txt": |
Tianjie Xu | 4f09900 | 2016-08-11 18:04:27 -0700 | [diff] [blame] | 614 | pass |
| 615 | |
Bowgo Tsai | e4544b1 | 2019-02-27 10:15:51 +0800 | [diff] [blame] | 616 | # Updates system_other.avbpubkey in /product/etc/. |
| 617 | elif filename in ( |
| 618 | "PRODUCT/etc/security/avb/system_other.avbpubkey", |
| 619 | "SYSTEM/product/etc/security/avb/system_other.avbpubkey"): |
| 620 | # Only update system_other's public key, if the corresponding signing |
| 621 | # key is specified via --avb_system_other_key. |
| 622 | signing_key = OPTIONS.avb_keys.get("system_other") |
| 623 | if signing_key: |
| 624 | public_key = common.ExtractAvbPublicKey(signing_key) |
| 625 | print(" Rewriting AVB public key of system_other in /product") |
| 626 | common.ZipWrite(output_tf_zip, public_key, filename) |
| 627 | |
Bowgo Tsai | 78369eb | 2019-04-23 12:28:44 +0800 | [diff] [blame] | 628 | # Should NOT sign boot-debug.img. |
| 629 | elif filename in ( |
| 630 | "BOOT/RAMDISK/force_debuggable", |
| 631 | "RECOVERY/RAMDISK/force_debuggable" |
| 632 | "RECOVERY/RAMDISK/first_stage_ramdisk/force_debuggable"): |
| 633 | raise common.ExternalError("debuggable boot.img cannot be signed") |
| 634 | |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 635 | # A non-APK file; copy it verbatim. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 636 | else: |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 637 | common.ZipWriteStr(output_tf_zip, out_info, data) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 638 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 639 | if OPTIONS.replace_ota_keys: |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 640 | ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 641 | |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 642 | # Replace the keyid string in misc_info dict. |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 643 | if OPTIONS.replace_verity_private_key: |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 644 | ReplaceVerityPrivateKey(misc_info, OPTIONS.replace_verity_private_key[1]) |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 645 | |
| 646 | if OPTIONS.replace_verity_public_key: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 647 | 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] | 648 | # We are replacing the one in boot image only, since the one under |
| 649 | # recovery won't ever be needed. |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 650 | ReplaceVerityPublicKey( |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 651 | output_tf_zip, dest, OPTIONS.replace_verity_public_key[1]) |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 652 | |
| 653 | # Replace the keyid string in BOOT/cmdline. |
| 654 | if OPTIONS.replace_verity_keyid: |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 655 | ReplaceVerityKeyId(input_tf_zip, output_tf_zip, |
| 656 | OPTIONS.replace_verity_keyid[1]) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 657 | |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 658 | # Replace the AVB signing keys, if any. |
| 659 | ReplaceAvbSigningKeys(misc_info) |
| 660 | |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 661 | # Write back misc_info with the latest values. |
| 662 | ReplaceMiscInfoTxt(input_tf_zip, output_tf_zip, misc_info) |
| 663 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 664 | |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 665 | def ReplaceCerts(data): |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 666 | """Replaces all the occurences of X.509 certs with the new ones. |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 667 | |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 668 | The mapping info is read from OPTIONS.key_map. Non-existent certificate will |
| 669 | be skipped. After the replacement, it additionally checks for duplicate |
| 670 | entries, which would otherwise fail the policy loading code in |
| 671 | frameworks/base/services/core/java/com/android/server/pm/SELinuxMMAC.java. |
| 672 | |
| 673 | Args: |
| 674 | data: Input string that contains a set of X.509 certs. |
| 675 | |
| 676 | Returns: |
| 677 | A string after the replacement. |
| 678 | |
| 679 | Raises: |
| 680 | AssertionError: On finding duplicate entries. |
| 681 | """ |
| 682 | for old, new in OPTIONS.key_map.iteritems(): |
| 683 | if OPTIONS.verbose: |
| 684 | print(" Replacing %s.x509.pem with %s.x509.pem" % (old, new)) |
| 685 | |
| 686 | try: |
| 687 | with open(old + ".x509.pem") as old_fp: |
| 688 | old_cert16 = base64.b16encode( |
| 689 | common.ParseCertificate(old_fp.read())).lower() |
| 690 | with open(new + ".x509.pem") as new_fp: |
| 691 | new_cert16 = base64.b16encode( |
| 692 | common.ParseCertificate(new_fp.read())).lower() |
| 693 | except IOError as e: |
| 694 | if OPTIONS.verbose or e.errno != errno.ENOENT: |
| 695 | print(" Error accessing %s: %s.\nSkip replacing %s.x509.pem with " |
| 696 | "%s.x509.pem." % (e.filename, e.strerror, old, new)) |
| 697 | continue |
| 698 | |
| 699 | # Only match entire certs. |
| 700 | pattern = "\\b" + old_cert16 + "\\b" |
| 701 | (data, num) = re.subn(pattern, new_cert16, data, flags=re.IGNORECASE) |
| 702 | |
| 703 | if OPTIONS.verbose: |
| 704 | print(" Replaced %d occurence(s) of %s.x509.pem with %s.x509.pem" % ( |
| 705 | num, old, new)) |
| 706 | |
| 707 | # Verify that there're no duplicate entries after the replacement. Note that |
| 708 | # it's only checking entries with global seinfo at the moment (i.e. ignoring |
| 709 | # the ones with inner packages). (Bug: 69479366) |
| 710 | root = ElementTree.fromstring(data) |
| 711 | signatures = [signer.attrib['signature'] for signer in root.findall('signer')] |
| 712 | assert len(signatures) == len(set(signatures)), \ |
| 713 | "Found duplicate entries after cert replacement: {}".format(data) |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 714 | |
| 715 | return data |
| 716 | |
| 717 | |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 718 | def EditTags(tags): |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 719 | """Applies the edits to the tag string as specified in OPTIONS.tag_changes. |
| 720 | |
| 721 | Args: |
| 722 | tags: The input string that contains comma-separated tags. |
| 723 | |
| 724 | Returns: |
| 725 | The updated tags (comma-separated and sorted). |
| 726 | """ |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 727 | tags = set(tags.split(",")) |
| 728 | for ch in OPTIONS.tag_changes: |
| 729 | if ch[0] == "-": |
| 730 | tags.discard(ch[1:]) |
| 731 | elif ch[0] == "+": |
| 732 | tags.add(ch[1:]) |
| 733 | return ",".join(sorted(tags)) |
| 734 | |
| 735 | |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 736 | def RewriteProps(data): |
| 737 | """Rewrites the system properties in the given string. |
| 738 | |
| 739 | Each property is expected in 'key=value' format. The properties that contain |
| 740 | build tags (i.e. test-keys, dev-keys) will be updated accordingly by calling |
| 741 | EditTags(). |
| 742 | |
| 743 | Args: |
| 744 | data: Input string, separated by newlines. |
| 745 | |
| 746 | Returns: |
| 747 | The string with modified properties. |
| 748 | """ |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 749 | output = [] |
| 750 | for line in data.split("\n"): |
| 751 | line = line.strip() |
| 752 | original_line = line |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 753 | if line and line[0] != '#' and "=" in line: |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 754 | key, value = line.split("=", 1) |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 755 | if key in ("ro.build.fingerprint", "ro.build.thumbprint", |
| 756 | "ro.vendor.build.fingerprint", "ro.vendor.build.thumbprint"): |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 757 | pieces = value.split("/") |
| 758 | pieces[-1] = EditTags(pieces[-1]) |
| 759 | value = "/".join(pieces) |
Tao Bao | cb7ff77 | 2015-09-11 15:27:56 -0700 | [diff] [blame] | 760 | elif key == "ro.bootimage.build.fingerprint": |
| 761 | pieces = value.split("/") |
| 762 | pieces[-1] = EditTags(pieces[-1]) |
| 763 | value = "/".join(pieces) |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 764 | elif key == "ro.build.description": |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 765 | pieces = value.split(" ") |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 766 | assert len(pieces) == 5 |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 767 | pieces[-1] = EditTags(pieces[-1]) |
| 768 | value = " ".join(pieces) |
| 769 | elif key == "ro.build.tags": |
| 770 | value = EditTags(value) |
Doug Zongker | a8608a7 | 2013-07-23 11:51:04 -0700 | [diff] [blame] | 771 | elif key == "ro.build.display.id": |
| 772 | # change, eg, "JWR66N dev-keys" to "JWR66N" |
| 773 | value = value.split() |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 774 | if len(value) > 1 and value[-1].endswith("-keys"): |
Andrew Boie | 73d5abb | 2013-12-11 12:42:03 -0800 | [diff] [blame] | 775 | value.pop() |
| 776 | value = " ".join(value) |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 777 | line = key + "=" + value |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 778 | if line != original_line: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 779 | print(" replace: ", original_line) |
| 780 | print(" with: ", line) |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 781 | output.append(line) |
| 782 | return "\n".join(output) + "\n" |
| 783 | |
| 784 | |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 785 | def WriteOtacerts(output_zip, filename, keys): |
| 786 | """Constructs a zipfile from given keys; and writes it to output_zip. |
| 787 | |
| 788 | Args: |
| 789 | output_zip: The output target_files zip. |
| 790 | filename: The archive name in the output zip. |
| 791 | keys: A list of public keys to use during OTA package verification. |
| 792 | """ |
| 793 | |
| 794 | try: |
| 795 | from StringIO import StringIO |
| 796 | except ImportError: |
| 797 | from io import StringIO |
| 798 | temp_file = StringIO() |
| 799 | certs_zip = zipfile.ZipFile(temp_file, "w") |
| 800 | for k in keys: |
| 801 | common.ZipWrite(certs_zip, k) |
| 802 | common.ZipClose(certs_zip) |
| 803 | common.ZipWriteStr(output_zip, filename, temp_file.getvalue()) |
| 804 | |
| 805 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 806 | def ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info): |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 807 | try: |
| 808 | keylist = input_tf_zip.read("META/otakeys.txt").split() |
| 809 | except KeyError: |
T.R. Fullhart | a28acc6 | 2013-03-18 10:31:26 -0700 | [diff] [blame] | 810 | raise common.ExternalError("can't read META/otakeys.txt from input") |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 811 | |
Tao Bao | f718f90 | 2017-11-09 10:10:10 -0800 | [diff] [blame] | 812 | extra_recovery_keys = misc_info.get("extra_recovery_keys") |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 813 | if extra_recovery_keys: |
| 814 | extra_recovery_keys = [OPTIONS.key_map.get(k, k) + ".x509.pem" |
| 815 | for k in extra_recovery_keys.split()] |
| 816 | if extra_recovery_keys: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 817 | print("extra recovery-only key(s): " + ", ".join(extra_recovery_keys)) |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 818 | else: |
| 819 | extra_recovery_keys = [] |
| 820 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 821 | mapped_keys = [] |
| 822 | for k in keylist: |
| 823 | m = re.match(r"^(.*)\.x509\.pem$", k) |
| 824 | if not m: |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 825 | raise common.ExternalError( |
| 826 | "can't parse \"%s\" from META/otakeys.txt" % (k,)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 827 | k = m.group(1) |
| 828 | mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem") |
| 829 | |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 830 | if mapped_keys: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 831 | print("using:\n ", "\n ".join(mapped_keys)) |
| 832 | print("for OTA package verification") |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 833 | else: |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 834 | devkey = misc_info.get("default_system_dev_certificate", |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 835 | "build/make/target/product/security/testkey") |
Tao Bao | f718f90 | 2017-11-09 10:10:10 -0800 | [diff] [blame] | 836 | mapped_devkey = OPTIONS.key_map.get(devkey, devkey) |
| 837 | if mapped_devkey != devkey: |
| 838 | misc_info["default_system_dev_certificate"] = mapped_devkey |
| 839 | mapped_keys.append(mapped_devkey + ".x509.pem") |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 840 | print("META/otakeys.txt has no keys; using %s for OTA package" |
| 841 | " verification." % (mapped_keys[0],)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 842 | |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 843 | # recovery now uses the same x509.pem version of the keys. |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 844 | # extra_recovery_keys are used only in recovery. |
Tom Cherry | 2929cad | 2018-09-20 11:04:37 -0700 | [diff] [blame] | 845 | if misc_info.get("recovery_as_boot") == "true": |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 846 | recovery_keys_location = "BOOT/RAMDISK/system/etc/security/otacerts.zip" |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 847 | else: |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 848 | recovery_keys_location = "RECOVERY/RAMDISK/system/etc/security/otacerts.zip" |
| 849 | |
| 850 | WriteOtacerts(output_tf_zip, recovery_keys_location, |
| 851 | mapped_keys + extra_recovery_keys) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 852 | |
| 853 | # SystemUpdateActivity uses the x509.pem version of the keys, but |
| 854 | # put into a zipfile system/etc/security/otacerts.zip. |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 855 | # We DO NOT include the extra_recovery_keys (if any) here. |
Tianjie Xu | ffbe6b9 | 2018-10-19 14:34:15 -0700 | [diff] [blame] | 856 | WriteOtacerts(output_tf_zip, "SYSTEM/etc/security/otacerts.zip", mapped_keys) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 857 | |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 858 | # For A/B devices, update the payload verification key. |
| 859 | if misc_info.get("ab_update") == "true": |
| 860 | # Unlike otacerts.zip that may contain multiple keys, we can only specify |
| 861 | # ONE payload verification key. |
| 862 | if len(mapped_keys) > 1: |
| 863 | print("\n WARNING: Found more than one OTA keys; Using the first one" |
| 864 | " as payload verification key.\n\n") |
| 865 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 866 | print("Using %s for payload verification." % (mapped_keys[0],)) |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 867 | pubkey = common.ExtractPublicKey(mapped_keys[0]) |
Tao Bao | 13b6962 | 2016-07-06 15:28:59 -0700 | [diff] [blame] | 868 | common.ZipWriteStr( |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 869 | output_tf_zip, |
Tao Bao | 13b6962 | 2016-07-06 15:28:59 -0700 | [diff] [blame] | 870 | "SYSTEM/etc/update_engine/update-payload-key.pub.pem", |
| 871 | pubkey) |
Alex Deymo | b3e8ce6 | 2016-08-04 16:06:12 -0700 | [diff] [blame] | 872 | common.ZipWriteStr( |
| 873 | output_tf_zip, |
Tao Bao | 696bb33 | 2018-08-17 16:27:01 -0700 | [diff] [blame] | 874 | "BOOT/RAMDISK/system/etc/update_engine/update-payload-key.pub.pem", |
Alex Deymo | b3e8ce6 | 2016-08-04 16:06:12 -0700 | [diff] [blame] | 875 | pubkey) |
Tao Bao | a80ed22 | 2016-06-16 14:41:24 -0700 | [diff] [blame] | 876 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 877 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 878 | def ReplaceVerityPublicKey(output_zip, filename, key_path): |
| 879 | """Replaces the verity public key at the given path in the given zip. |
| 880 | |
| 881 | Args: |
| 882 | output_zip: The output target_files zip. |
| 883 | filename: The archive name in the output zip. |
| 884 | key_path: The path to the public key. |
| 885 | """ |
| 886 | print("Replacing verity public key with %s" % (key_path,)) |
| 887 | common.ZipWrite(output_zip, key_path, arcname=filename) |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 888 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 889 | |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 890 | def ReplaceVerityPrivateKey(misc_info, key_path): |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 891 | """Replaces the verity private key in misc_info dict. |
| 892 | |
| 893 | Args: |
| 894 | misc_info: The info dict. |
| 895 | key_path: The path to the private key in PKCS#8 format. |
| 896 | """ |
| 897 | print("Replacing verity private key with %s" % (key_path,)) |
Andrew Boie | d083f0b | 2014-09-15 16:01:07 -0700 | [diff] [blame] | 898 | misc_info["verity_key"] = key_path |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 899 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 900 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 901 | def ReplaceVerityKeyId(input_zip, output_zip, key_path): |
| 902 | """Replaces the veritykeyid parameter in BOOT/cmdline. |
| 903 | |
| 904 | Args: |
| 905 | input_zip: The input target_files zip, which should be already open. |
| 906 | output_zip: The output target_files zip, which should be already open and |
| 907 | writable. |
| 908 | key_path: The path to the PEM encoded X.509 certificate. |
| 909 | """ |
| 910 | in_cmdline = input_zip.read("BOOT/cmdline") |
| 911 | # Copy in_cmdline to output_zip if veritykeyid is not present. |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 912 | if "veritykeyid" not in in_cmdline: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 913 | common.ZipWriteStr(output_zip, "BOOT/cmdline", in_cmdline) |
| 914 | return |
| 915 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 916 | out_buffer = [] |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 917 | for param in in_cmdline.split(): |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 918 | if "veritykeyid" not in param: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 919 | out_buffer.append(param) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 920 | continue |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 921 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 922 | # Extract keyid using openssl command. |
| 923 | p = common.Run(["openssl", "x509", "-in", key_path, "-text"], |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 924 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 925 | keyid, stderr = p.communicate() |
| 926 | assert p.returncode == 0, "Failed to dump certificate: {}".format(stderr) |
| 927 | keyid = re.search( |
| 928 | r'keyid:([0-9a-fA-F:]*)', keyid).group(1).replace(':', '').lower() |
| 929 | print("Replacing verity keyid with {}".format(keyid)) |
| 930 | out_buffer.append("veritykeyid=id:%s" % (keyid,)) |
| 931 | |
| 932 | out_cmdline = ' '.join(out_buffer).strip() + '\n' |
| 933 | common.ZipWriteStr(output_zip, "BOOT/cmdline", out_cmdline) |
Tao Bao | 46a5999 | 2017-06-05 11:55:16 -0700 | [diff] [blame] | 934 | |
| 935 | |
| 936 | def ReplaceMiscInfoTxt(input_zip, output_zip, misc_info): |
| 937 | """Replaces META/misc_info.txt. |
| 938 | |
| 939 | Only writes back the ones in the original META/misc_info.txt. Because the |
| 940 | current in-memory dict contains additional items computed at runtime. |
| 941 | """ |
| 942 | misc_info_old = common.LoadDictionaryFromLines( |
| 943 | input_zip.read('META/misc_info.txt').split('\n')) |
| 944 | items = [] |
| 945 | for key in sorted(misc_info): |
| 946 | if key in misc_info_old: |
| 947 | items.append('%s=%s' % (key, misc_info[key])) |
| 948 | common.ZipWriteStr(output_zip, "META/misc_info.txt", '\n'.join(items)) |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 949 | |
Tao Bao | 8adcfd1 | 2016-06-17 17:01:22 -0700 | [diff] [blame] | 950 | |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 951 | def ReplaceAvbSigningKeys(misc_info): |
| 952 | """Replaces the AVB signing keys.""" |
| 953 | |
| 954 | AVB_FOOTER_ARGS_BY_PARTITION = { |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 955 | 'boot' : 'avb_boot_add_hash_footer_args', |
| 956 | 'dtbo' : 'avb_dtbo_add_hash_footer_args', |
| 957 | 'recovery' : 'avb_recovery_add_hash_footer_args', |
| 958 | 'system' : 'avb_system_add_hashtree_footer_args', |
Bowgo Tsai | e4544b1 | 2019-02-27 10:15:51 +0800 | [diff] [blame] | 959 | 'system_other' : 'avb_system_other_add_hashtree_footer_args', |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 960 | 'vendor' : 'avb_vendor_add_hashtree_footer_args', |
| 961 | 'vbmeta' : 'avb_vbmeta_args', |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | def ReplaceAvbPartitionSigningKey(partition): |
| 965 | key = OPTIONS.avb_keys.get(partition) |
| 966 | if not key: |
| 967 | return |
| 968 | |
| 969 | algorithm = OPTIONS.avb_algorithms.get(partition) |
| 970 | assert algorithm, 'Missing AVB signing algorithm for %s' % (partition,) |
| 971 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 972 | print('Replacing AVB signing key for %s with "%s" (%s)' % ( |
| 973 | partition, key, algorithm)) |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 974 | misc_info['avb_' + partition + '_algorithm'] = algorithm |
| 975 | misc_info['avb_' + partition + '_key_path'] = key |
| 976 | |
| 977 | extra_args = OPTIONS.avb_extra_args.get(partition) |
| 978 | if extra_args: |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 979 | print('Setting extra AVB signing args for %s to "%s"' % ( |
| 980 | partition, extra_args)) |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 981 | args_key = AVB_FOOTER_ARGS_BY_PARTITION[partition] |
| 982 | misc_info[args_key] = (misc_info.get(args_key, '') + ' ' + extra_args) |
| 983 | |
| 984 | for partition in AVB_FOOTER_ARGS_BY_PARTITION: |
| 985 | ReplaceAvbPartitionSigningKey(partition) |
| 986 | |
| 987 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 988 | def BuildKeyMap(misc_info, key_mapping_options): |
| 989 | for s, d in key_mapping_options: |
| 990 | if s is None: # -d option |
| 991 | devkey = misc_info.get("default_system_dev_certificate", |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 992 | "build/make/target/product/security/testkey") |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 993 | devkeydir = os.path.dirname(devkey) |
| 994 | |
| 995 | OPTIONS.key_map.update({ |
| 996 | devkeydir + "/testkey": d + "/releasekey", |
| 997 | devkeydir + "/devkey": d + "/releasekey", |
| 998 | devkeydir + "/media": d + "/media", |
| 999 | devkeydir + "/shared": d + "/shared", |
| 1000 | devkeydir + "/platform": d + "/platform", |
| 1001 | }) |
| 1002 | else: |
| 1003 | OPTIONS.key_map[s] = d |
| 1004 | |
| 1005 | |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 1006 | def GetApiLevelAndCodename(input_tf_zip): |
| 1007 | data = input_tf_zip.read("SYSTEM/build.prop") |
| 1008 | api_level = None |
| 1009 | codename = None |
| 1010 | for line in data.split("\n"): |
| 1011 | line = line.strip() |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 1012 | if line and line[0] != '#' and "=" in line: |
| 1013 | key, value = line.split("=", 1) |
| 1014 | key = key.strip() |
| 1015 | if key == "ro.build.version.sdk": |
| 1016 | api_level = int(value.strip()) |
| 1017 | elif key == "ro.build.version.codename": |
| 1018 | codename = value.strip() |
| 1019 | |
| 1020 | if api_level is None: |
| 1021 | raise ValueError("No ro.build.version.sdk in SYSTEM/build.prop") |
| 1022 | if codename is None: |
| 1023 | raise ValueError("No ro.build.version.codename in SYSTEM/build.prop") |
| 1024 | |
| 1025 | return (api_level, codename) |
| 1026 | |
| 1027 | |
| 1028 | def GetCodenameToApiLevelMap(input_tf_zip): |
| 1029 | data = input_tf_zip.read("SYSTEM/build.prop") |
| 1030 | api_level = None |
| 1031 | codenames = None |
| 1032 | for line in data.split("\n"): |
| 1033 | line = line.strip() |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 1034 | if line and line[0] != '#' and "=" in line: |
| 1035 | key, value = line.split("=", 1) |
| 1036 | key = key.strip() |
| 1037 | if key == "ro.build.version.sdk": |
| 1038 | api_level = int(value.strip()) |
| 1039 | elif key == "ro.build.version.all_codenames": |
| 1040 | codenames = value.strip().split(",") |
| 1041 | |
| 1042 | if api_level is None: |
| 1043 | raise ValueError("No ro.build.version.sdk in SYSTEM/build.prop") |
| 1044 | if codenames is None: |
| 1045 | raise ValueError("No ro.build.version.all_codenames in SYSTEM/build.prop") |
| 1046 | |
| 1047 | result = dict() |
| 1048 | for codename in codenames: |
| 1049 | codename = codename.strip() |
Tao Bao | badceb2 | 2019-03-15 09:33:43 -0700 | [diff] [blame] | 1050 | if codename: |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 1051 | result[codename] = api_level |
| 1052 | return result |
| 1053 | |
| 1054 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1055 | def ReadApexKeysInfo(tf_zip): |
| 1056 | """Parses the APEX keys info from a given target-files zip. |
| 1057 | |
| 1058 | Given a target-files ZipFile, parses the META/apexkeys.txt entry and returns a |
| 1059 | dict that contains the mapping from APEX names (e.g. com.android.tzdata) to a |
| 1060 | tuple of (payload_key, container_key). |
| 1061 | |
| 1062 | Args: |
| 1063 | tf_zip: The input target_files ZipFile (already open). |
| 1064 | |
| 1065 | Returns: |
| 1066 | (payload_key, container_key): payload_key contains the path to the payload |
| 1067 | signing key; container_key contains the path to the container signing |
| 1068 | key. |
| 1069 | """ |
| 1070 | keys = {} |
| 1071 | for line in tf_zip.read("META/apexkeys.txt").split("\n"): |
| 1072 | line = line.strip() |
| 1073 | if not line: |
| 1074 | continue |
| 1075 | matches = re.match( |
| 1076 | r'^name="(?P<NAME>.*)"\s+' |
| 1077 | r'public_key="(?P<PAYLOAD_PUBLIC_KEY>.*)"\s+' |
| 1078 | r'private_key="(?P<PAYLOAD_PRIVATE_KEY>.*)"\s+' |
| 1079 | r'container_certificate="(?P<CONTAINER_CERT>.*)"\s+' |
| 1080 | r'container_private_key="(?P<CONTAINER_PRIVATE_KEY>.*)"$', |
| 1081 | line) |
| 1082 | if not matches: |
| 1083 | continue |
| 1084 | |
| 1085 | name = matches.group('NAME') |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1086 | payload_private_key = matches.group("PAYLOAD_PRIVATE_KEY") |
| 1087 | |
| 1088 | def CompareKeys(pubkey, pubkey_suffix, privkey, privkey_suffix): |
| 1089 | pubkey_suffix_len = len(pubkey_suffix) |
| 1090 | privkey_suffix_len = len(privkey_suffix) |
| 1091 | return (pubkey.endswith(pubkey_suffix) and |
| 1092 | privkey.endswith(privkey_suffix) and |
| 1093 | pubkey[:-pubkey_suffix_len] == privkey[:-privkey_suffix_len]) |
| 1094 | |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 1095 | # Sanity check on the container key names, as we'll carry them without the |
| 1096 | # extensions. This doesn't apply to payload keys though, which we will use |
| 1097 | # full names only. |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1098 | container_cert = matches.group("CONTAINER_CERT") |
| 1099 | container_private_key = matches.group("CONTAINER_PRIVATE_KEY") |
Tao Bao | f454c3a | 2019-04-24 23:53:42 -0700 | [diff] [blame^] | 1100 | if container_cert == 'PRESIGNED' and container_private_key == 'PRESIGNED': |
| 1101 | container_key = 'PRESIGNED' |
| 1102 | elif CompareKeys( |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1103 | container_cert, OPTIONS.public_key_suffix, |
| 1104 | container_private_key, OPTIONS.private_key_suffix): |
Tao Bao | f454c3a | 2019-04-24 23:53:42 -0700 | [diff] [blame^] | 1105 | container_key = container_cert[:-len(OPTIONS.public_key_suffix)] |
| 1106 | else: |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1107 | raise ValueError("Failed to parse container keys: \n{}".format(line)) |
| 1108 | |
Tao Bao | f454c3a | 2019-04-24 23:53:42 -0700 | [diff] [blame^] | 1109 | keys[name] = (payload_private_key, container_key) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1110 | |
| 1111 | return keys |
| 1112 | |
| 1113 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1114 | def main(argv): |
| 1115 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1116 | key_mapping_options = [] |
| 1117 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1118 | def option_handler(o, a): |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 1119 | if o in ("-e", "--extra_apks"): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1120 | names, key = a.split("=") |
| 1121 | names = names.split(",") |
| 1122 | for n in names: |
| 1123 | OPTIONS.extra_apks[n] = key |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1124 | elif o == "--extra_apex_payload_key": |
| 1125 | apex_name, key = a.split("=") |
| 1126 | OPTIONS.extra_apex_payload_keys[apex_name] = key |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 1127 | elif o == "--skip_apks_with_path_prefix": |
| 1128 | # Sanity check the prefix, which must be in all upper case. |
| 1129 | prefix = a.split('/')[0] |
| 1130 | if not prefix or prefix != prefix.upper(): |
| 1131 | raise ValueError("Invalid path prefix '%s'" % (a,)) |
| 1132 | OPTIONS.skip_apks_with_path_prefix.add(a) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1133 | elif o in ("-d", "--default_key_mappings"): |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1134 | key_mapping_options.append((None, a)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1135 | elif o in ("-k", "--key_mapping"): |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1136 | key_mapping_options.append(a.split("=", 1)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 1137 | elif o in ("-o", "--replace_ota_keys"): |
| 1138 | OPTIONS.replace_ota_keys = True |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 1139 | elif o in ("-t", "--tag_changes"): |
| 1140 | new = [] |
| 1141 | for i in a.split(","): |
| 1142 | i = i.strip() |
| 1143 | if not i or i[0] not in "-+": |
| 1144 | raise ValueError("Bad tag change '%s'" % (i,)) |
| 1145 | new.append(i[0] + i[1:].strip()) |
| 1146 | OPTIONS.tag_changes = tuple(new) |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 1147 | elif o == "--replace_verity_public_key": |
| 1148 | OPTIONS.replace_verity_public_key = (True, a) |
| 1149 | elif o == "--replace_verity_private_key": |
| 1150 | OPTIONS.replace_verity_private_key = (True, a) |
Badhri Jagan Sridharan | 35c9b12 | 2016-06-16 19:58:44 -0700 | [diff] [blame] | 1151 | elif o == "--replace_verity_keyid": |
| 1152 | OPTIONS.replace_verity_keyid = (True, a) |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 1153 | elif o == "--avb_vbmeta_key": |
| 1154 | OPTIONS.avb_keys['vbmeta'] = a |
| 1155 | elif o == "--avb_vbmeta_algorithm": |
| 1156 | OPTIONS.avb_algorithms['vbmeta'] = a |
| 1157 | elif o == "--avb_vbmeta_extra_args": |
| 1158 | OPTIONS.avb_extra_args['vbmeta'] = a |
| 1159 | elif o == "--avb_boot_key": |
| 1160 | OPTIONS.avb_keys['boot'] = a |
| 1161 | elif o == "--avb_boot_algorithm": |
| 1162 | OPTIONS.avb_algorithms['boot'] = a |
| 1163 | elif o == "--avb_boot_extra_args": |
| 1164 | OPTIONS.avb_extra_args['boot'] = a |
| 1165 | elif o == "--avb_dtbo_key": |
| 1166 | OPTIONS.avb_keys['dtbo'] = a |
| 1167 | elif o == "--avb_dtbo_algorithm": |
| 1168 | OPTIONS.avb_algorithms['dtbo'] = a |
| 1169 | elif o == "--avb_dtbo_extra_args": |
| 1170 | OPTIONS.avb_extra_args['dtbo'] = a |
| 1171 | elif o == "--avb_system_key": |
| 1172 | OPTIONS.avb_keys['system'] = a |
| 1173 | elif o == "--avb_system_algorithm": |
| 1174 | OPTIONS.avb_algorithms['system'] = a |
| 1175 | elif o == "--avb_system_extra_args": |
| 1176 | OPTIONS.avb_extra_args['system'] = a |
Bowgo Tsai | e4544b1 | 2019-02-27 10:15:51 +0800 | [diff] [blame] | 1177 | elif o == "--avb_system_other_key": |
| 1178 | OPTIONS.avb_keys['system_other'] = a |
| 1179 | elif o == "--avb_system_other_algorithm": |
| 1180 | OPTIONS.avb_algorithms['system_other'] = a |
| 1181 | elif o == "--avb_system_other_extra_args": |
| 1182 | OPTIONS.avb_extra_args['system_other'] = a |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 1183 | elif o == "--avb_vendor_key": |
| 1184 | OPTIONS.avb_keys['vendor'] = a |
| 1185 | elif o == "--avb_vendor_algorithm": |
| 1186 | OPTIONS.avb_algorithms['vendor'] = a |
| 1187 | elif o == "--avb_vendor_extra_args": |
| 1188 | OPTIONS.avb_extra_args['vendor'] = a |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1189 | elif o == "--avb_apex_extra_args": |
| 1190 | OPTIONS.avb_extra_args['apex'] = a |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1191 | else: |
| 1192 | return False |
| 1193 | return True |
| 1194 | |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 1195 | args = common.ParseOptions( |
| 1196 | argv, __doc__, |
| 1197 | extra_opts="e:d:k:ot:", |
| 1198 | extra_long_opts=[ |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 1199 | "extra_apks=", |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1200 | "extra_apex_payload_key=", |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 1201 | "skip_apks_with_path_prefix=", |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 1202 | "default_key_mappings=", |
| 1203 | "key_mapping=", |
| 1204 | "replace_ota_keys", |
| 1205 | "tag_changes=", |
| 1206 | "replace_verity_public_key=", |
| 1207 | "replace_verity_private_key=", |
| 1208 | "replace_verity_keyid=", |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1209 | "avb_apex_extra_args=", |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 1210 | "avb_vbmeta_algorithm=", |
| 1211 | "avb_vbmeta_key=", |
| 1212 | "avb_vbmeta_extra_args=", |
| 1213 | "avb_boot_algorithm=", |
| 1214 | "avb_boot_key=", |
| 1215 | "avb_boot_extra_args=", |
| 1216 | "avb_dtbo_algorithm=", |
| 1217 | "avb_dtbo_key=", |
| 1218 | "avb_dtbo_extra_args=", |
| 1219 | "avb_system_algorithm=", |
| 1220 | "avb_system_key=", |
| 1221 | "avb_system_extra_args=", |
Bowgo Tsai | e4544b1 | 2019-02-27 10:15:51 +0800 | [diff] [blame] | 1222 | "avb_system_other_algorithm=", |
| 1223 | "avb_system_other_key=", |
| 1224 | "avb_system_other_extra_args=", |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 1225 | "avb_vendor_algorithm=", |
| 1226 | "avb_vendor_key=", |
| 1227 | "avb_vendor_extra_args=", |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 1228 | ], |
| 1229 | extra_option_handler=option_handler) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1230 | |
| 1231 | if len(args) != 2: |
| 1232 | common.Usage(__doc__) |
| 1233 | sys.exit(1) |
| 1234 | |
Tao Bao | badceb2 | 2019-03-15 09:33:43 -0700 | [diff] [blame] | 1235 | common.InitLogging() |
| 1236 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1237 | input_zip = zipfile.ZipFile(args[0], "r") |
Tao Bao | 2b8f489 | 2017-06-13 12:54:58 -0700 | [diff] [blame] | 1238 | output_zip = zipfile.ZipFile(args[1], "w", |
| 1239 | compression=zipfile.ZIP_DEFLATED, |
| 1240 | allowZip64=True) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1241 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1242 | misc_info = common.LoadInfoDict(input_zip) |
| 1243 | |
| 1244 | BuildKeyMap(misc_info, key_mapping_options) |
| 1245 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1246 | apk_keys_info, compressed_extension = common.ReadApkCerts(input_zip) |
| 1247 | apk_keys = GetApkCerts(apk_keys_info) |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 1248 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1249 | apex_keys_info = ReadApexKeysInfo(input_zip) |
| 1250 | apex_keys = GetApexKeys(apex_keys_info, apk_keys) |
| 1251 | |
| 1252 | CheckApkAndApexKeysAvailable( |
| 1253 | input_zip, |
| 1254 | set(apk_keys.keys()) | set(apex_keys.keys()), |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 1255 | compressed_extension, |
| 1256 | apex_keys) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1257 | |
| 1258 | key_passwords = common.GetKeyPasswords( |
| 1259 | set(apk_keys.values()) | set(itertools.chain(*apex_keys.values()))) |
Tao Bao | 9aa4b9b | 2016-09-29 17:53:56 -0700 | [diff] [blame] | 1260 | platform_api_level, _ = GetApiLevelAndCodename(input_zip) |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 1261 | codename_to_api_level_map = GetCodenameToApiLevelMap(input_zip) |
Alex Klyubin | 2cfd1d1 | 2016-01-13 10:32:47 -0800 | [diff] [blame] | 1262 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 1263 | ProcessTargetFiles(input_zip, output_zip, misc_info, |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 1264 | apk_keys, apex_keys, key_passwords, |
| 1265 | platform_api_level, codename_to_api_level_map, |
Narayan Kamath | a07bf04 | 2017-08-14 14:49:21 +0100 | [diff] [blame] | 1266 | compressed_extension) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 1267 | |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 1268 | common.ZipClose(input_zip) |
| 1269 | common.ZipClose(output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1270 | |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 1271 | # Skip building userdata.img and cache.img when signing the target files. |
Tianjie Xu | 616fbeb | 2017-05-23 14:51:02 -0700 | [diff] [blame] | 1272 | new_args = ["--is_signing"] |
| 1273 | # add_img_to_target_files builds the system image from scratch, so the |
| 1274 | # recovery patch is guaranteed to be regenerated there. |
| 1275 | if OPTIONS.rebuild_recovery: |
| 1276 | new_args.append("--rebuild_recovery") |
| 1277 | new_args.append(args[1]) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 1278 | add_img_to_target_files.main(new_args) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 1279 | |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 1280 | print("done.") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1281 | |
| 1282 | |
| 1283 | if __name__ == '__main__': |
| 1284 | try: |
| 1285 | main(sys.argv[1:]) |
Tao Bao | 0c28d2d | 2017-12-24 10:37:38 -0800 | [diff] [blame] | 1286 | except common.ExternalError as e: |
| 1287 | print("\n ERROR: %s\n" % (e,)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1288 | sys.exit(1) |
Tao Bao | 639118f | 2017-06-19 15:48:02 -0700 | [diff] [blame] | 1289 | finally: |
| 1290 | common.Cleanup() |