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