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) |
| 54 | Replace the certificate (public key) used by OTA package |
| 55 | verification with the one specified in the input target_files |
| 56 | zip (in the META/otakeys.txt file). Key remapping (-k and -d) |
| 57 | is performed on this key. |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 58 | |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 59 | -t (--tag_changes) <+tag>,<-tag>,... |
| 60 | Comma-separated list of changes to make to the set of tags (in |
| 61 | the last component of the build fingerprint). Prefix each with |
| 62 | '+' or '-' to indicate whether that tag should be added or |
| 63 | removed. Changes are processed in the order they appear. |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 64 | Default value is "-test-keys,-dev-keys,+release-keys". |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 65 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 66 | """ |
| 67 | |
| 68 | import sys |
| 69 | |
Doug Zongker | cf6d5a9 | 2014-02-18 10:57:07 -0800 | [diff] [blame] | 70 | if sys.hexversion < 0x02070000: |
| 71 | print >> sys.stderr, "Python 2.7 or newer is required." |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 72 | sys.exit(1) |
| 73 | |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 74 | import base64 |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 75 | import cStringIO |
| 76 | import copy |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 77 | import errno |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 78 | import os |
| 79 | import re |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 80 | import shutil |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 81 | import subprocess |
| 82 | import tempfile |
| 83 | import zipfile |
| 84 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 85 | import add_img_to_target_files |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 86 | import common |
| 87 | |
| 88 | OPTIONS = common.OPTIONS |
| 89 | |
| 90 | OPTIONS.extra_apks = {} |
| 91 | OPTIONS.key_map = {} |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 92 | OPTIONS.replace_ota_keys = False |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 93 | OPTIONS.replace_verity_public_key = False |
| 94 | OPTIONS.replace_verity_private_key = False |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 95 | OPTIONS.tag_changes = ("-test-keys", "-dev-keys", "+release-keys") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 96 | |
| 97 | def GetApkCerts(tf_zip): |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 98 | certmap = common.ReadApkCerts(tf_zip) |
| 99 | |
| 100 | # apply the key remapping to the contents of the file |
| 101 | for apk, cert in certmap.iteritems(): |
| 102 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
| 103 | |
| 104 | # apply all the -e options, overriding anything in the file |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 105 | for apk, cert in OPTIONS.extra_apks.iteritems(): |
Doug Zongker | decf995 | 2009-12-15 17:27:49 -0800 | [diff] [blame] | 106 | if not cert: |
| 107 | cert = "PRESIGNED" |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 108 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 109 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 110 | return certmap |
| 111 | |
| 112 | |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 113 | def CheckAllApksSigned(input_tf_zip, apk_key_map): |
| 114 | """Check that all the APKs we want to sign have keys specified, and |
| 115 | error out if they don't.""" |
| 116 | unknown_apks = [] |
| 117 | for info in input_tf_zip.infolist(): |
| 118 | if info.filename.endswith(".apk"): |
| 119 | name = os.path.basename(info.filename) |
| 120 | if name not in apk_key_map: |
| 121 | unknown_apks.append(name) |
| 122 | if unknown_apks: |
| 123 | print "ERROR: no key specified for:\n\n ", |
| 124 | print "\n ".join(unknown_apks) |
| 125 | print "\nUse '-e <apkname>=' to specify a key (which may be an" |
| 126 | print "empty string to not sign this apk)." |
| 127 | sys.exit(1) |
| 128 | |
| 129 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 130 | def SignApk(data, keyname, pw): |
| 131 | unsigned = tempfile.NamedTemporaryFile() |
| 132 | unsigned.write(data) |
| 133 | unsigned.flush() |
| 134 | |
| 135 | signed = tempfile.NamedTemporaryFile() |
| 136 | |
| 137 | common.SignFile(unsigned.name, signed.name, keyname, pw, align=4) |
| 138 | |
| 139 | data = signed.read() |
| 140 | unsigned.close() |
| 141 | signed.close() |
| 142 | |
| 143 | return data |
| 144 | |
| 145 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 146 | def ProcessTargetFiles(input_tf_zip, output_tf_zip, misc_info, |
| 147 | apk_key_map, key_passwords): |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 148 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 149 | maxsize = max([len(os.path.basename(i.filename)) |
| 150 | for i in input_tf_zip.infolist() |
| 151 | if i.filename.endswith('.apk')]) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 152 | rebuild_recovery = False |
| 153 | |
| 154 | tmpdir = tempfile.mkdtemp() |
| 155 | def write_to_temp(fn, attr, data): |
| 156 | fn = os.path.join(tmpdir, fn) |
| 157 | if fn.endswith("/"): |
| 158 | fn = os.path.join(tmpdir, fn) |
| 159 | os.mkdir(fn) |
| 160 | else: |
| 161 | d = os.path.dirname(fn) |
| 162 | if d and not os.path.exists(d): |
| 163 | os.makedirs(d) |
| 164 | |
| 165 | if attr >> 16 == 0xa1ff: |
| 166 | os.symlink(data, fn) |
| 167 | else: |
| 168 | with open(fn, "wb") as f: |
| 169 | f.write(data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 170 | |
| 171 | for info in input_tf_zip.infolist(): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 172 | if info.filename.startswith("IMAGES/"): |
| 173 | continue |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 174 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 175 | data = input_tf_zip.read(info.filename) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 176 | out_info = copy.copy(info) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 177 | |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 178 | if (info.filename == "META/misc_info.txt" and |
Michael Runge | 947894f | 2014-10-14 20:58:38 -0700 | [diff] [blame] | 179 | OPTIONS.replace_verity_private_key): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 180 | ReplaceVerityPrivateKey(input_tf_zip, output_tf_zip, misc_info, |
| 181 | OPTIONS.replace_verity_private_key[1]) |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 182 | elif (info.filename == "BOOT/RAMDISK/verity_key" and |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 183 | OPTIONS.replace_verity_public_key): |
| 184 | new_data = ReplaceVerityPublicKey(output_tf_zip, |
| 185 | OPTIONS.replace_verity_public_key[1]) |
Andrew Boie | d083f0b | 2014-09-15 16:01:07 -0700 | [diff] [blame] | 186 | write_to_temp(info.filename, info.external_attr, new_data) |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 187 | elif (info.filename.startswith("BOOT/") or |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 188 | info.filename.startswith("RECOVERY/") or |
| 189 | info.filename.startswith("META/") or |
| 190 | info.filename == "SYSTEM/etc/recovery-resource.dat"): |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 191 | write_to_temp(info.filename, info.external_attr, data) |
| 192 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 193 | if info.filename.endswith(".apk"): |
| 194 | name = os.path.basename(info.filename) |
Doug Zongker | 43874f8 | 2009-04-14 14:05:15 -0700 | [diff] [blame] | 195 | key = apk_key_map[name] |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 196 | if key not in common.SPECIAL_CERT_STRINGS: |
Doug Zongker | 43874f8 | 2009-04-14 14:05:15 -0700 | [diff] [blame] | 197 | print " signing: %-*s (%s)" % (maxsize, name, key) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 198 | signed_data = SignApk(data, key, key_passwords[key]) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 199 | output_tf_zip.writestr(out_info, signed_data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 200 | else: |
| 201 | # an APK we're not supposed to sign. |
Doug Zongker | 43874f8 | 2009-04-14 14:05:15 -0700 | [diff] [blame] | 202 | print "NOT signing: %s" % (name,) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 203 | output_tf_zip.writestr(out_info, data) |
| 204 | elif info.filename in ("SYSTEM/build.prop", |
Jesse Zhao | 2625d27 | 2015-02-06 09:49:55 -0800 | [diff] [blame] | 205 | "VENDOR/build.prop", |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 206 | "RECOVERY/RAMDISK/default.prop"): |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 207 | print "rewriting %s:" % (info.filename,) |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 208 | new_data = RewriteProps(data, misc_info) |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 209 | output_tf_zip.writestr(out_info, new_data) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 210 | if info.filename == "RECOVERY/RAMDISK/default.prop": |
| 211 | write_to_temp(info.filename, info.external_attr, new_data) |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 212 | elif info.filename.endswith("mac_permissions.xml"): |
| 213 | print "rewriting %s with new keys." % (info.filename,) |
| 214 | new_data = ReplaceCerts(data) |
| 215 | output_tf_zip.writestr(out_info, new_data) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 216 | elif info.filename in ("SYSTEM/recovery-from-boot.p", |
| 217 | "SYSTEM/bin/install-recovery.sh"): |
| 218 | rebuild_recovery = True |
| 219 | elif (OPTIONS.replace_ota_keys and |
| 220 | info.filename in ("RECOVERY/RAMDISK/res/keys", |
| 221 | "SYSTEM/etc/security/otacerts.zip")): |
| 222 | # don't copy these files if we're regenerating them below |
| 223 | pass |
Michael Runge | 947894f | 2014-10-14 20:58:38 -0700 | [diff] [blame] | 224 | elif (OPTIONS.replace_verity_private_key and |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 225 | info.filename == "META/misc_info.txt"): |
| 226 | pass |
Michael Runge | 947894f | 2014-10-14 20:58:38 -0700 | [diff] [blame] | 227 | elif (OPTIONS.replace_verity_public_key and |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 228 | info.filename == "BOOT/RAMDISK/verity_key"): |
| 229 | pass |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 230 | else: |
| 231 | # a non-APK file; copy it verbatim |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 232 | output_tf_zip.writestr(out_info, data) |
| 233 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 234 | if OPTIONS.replace_ota_keys: |
| 235 | new_recovery_keys = ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info) |
| 236 | if new_recovery_keys: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 237 | write_to_temp("RECOVERY/RAMDISK/res/keys", 0o755 << 16, new_recovery_keys) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 238 | |
| 239 | if rebuild_recovery: |
| 240 | recovery_img = common.GetBootableImage( |
| 241 | "recovery.img", "recovery.img", tmpdir, "RECOVERY", info_dict=misc_info) |
| 242 | boot_img = common.GetBootableImage( |
| 243 | "boot.img", "boot.img", tmpdir, "BOOT", info_dict=misc_info) |
| 244 | |
| 245 | def output_sink(fn, data): |
| 246 | output_tf_zip.writestr("SYSTEM/"+fn, data) |
| 247 | |
| 248 | common.MakeRecoveryPatch(tmpdir, output_sink, recovery_img, boot_img, |
| 249 | info_dict=misc_info) |
| 250 | |
| 251 | shutil.rmtree(tmpdir) |
| 252 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 253 | |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 254 | def ReplaceCerts(data): |
| 255 | """Given a string of data, replace all occurences of a set |
| 256 | of X509 certs with a newer set of X509 certs and return |
| 257 | the updated data string.""" |
| 258 | for old, new in OPTIONS.key_map.iteritems(): |
| 259 | try: |
| 260 | if OPTIONS.verbose: |
| 261 | print " Replacing %s.x509.pem with %s.x509.pem" % (old, new) |
| 262 | f = open(old + ".x509.pem") |
| 263 | old_cert16 = base64.b16encode(common.ParseCertificate(f.read())).lower() |
| 264 | f.close() |
| 265 | f = open(new + ".x509.pem") |
| 266 | new_cert16 = base64.b16encode(common.ParseCertificate(f.read())).lower() |
| 267 | f.close() |
| 268 | # Only match entire certs. |
| 269 | pattern = "\\b"+old_cert16+"\\b" |
| 270 | (data, num) = re.subn(pattern, new_cert16, data, flags=re.IGNORECASE) |
| 271 | if OPTIONS.verbose: |
| 272 | print " Replaced %d occurence(s) of %s.x509.pem with " \ |
| 273 | "%s.x509.pem" % (num, old, new) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 274 | except IOError as e: |
| 275 | if e.errno == errno.ENOENT and not OPTIONS.verbose: |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame] | 276 | continue |
| 277 | |
| 278 | print " Error accessing %s. %s. Skip replacing %s.x509.pem " \ |
| 279 | "with %s.x509.pem." % (e.filename, e.strerror, old, new) |
| 280 | |
| 281 | return data |
| 282 | |
| 283 | |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 284 | def EditTags(tags): |
| 285 | """Given a string containing comma-separated tags, apply the edits |
| 286 | specified in OPTIONS.tag_changes and return the updated string.""" |
| 287 | tags = set(tags.split(",")) |
| 288 | for ch in OPTIONS.tag_changes: |
| 289 | if ch[0] == "-": |
| 290 | tags.discard(ch[1:]) |
| 291 | elif ch[0] == "+": |
| 292 | tags.add(ch[1:]) |
| 293 | return ",".join(sorted(tags)) |
| 294 | |
| 295 | |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 296 | def RewriteProps(data, misc_info): |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 297 | output = [] |
| 298 | for line in data.split("\n"): |
| 299 | line = line.strip() |
| 300 | original_line = line |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 301 | if line and line[0] != '#' and "=" in line: |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 302 | key, value = line.split("=", 1) |
Michael Runge | e07c75a | 2014-12-09 13:54:23 -0800 | [diff] [blame] | 303 | if (key in ("ro.build.fingerprint", "ro.vendor.build.fingerprint") |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 304 | and misc_info.get("oem_fingerprint_properties") is None): |
| 305 | pieces = value.split("/") |
| 306 | pieces[-1] = EditTags(pieces[-1]) |
| 307 | value = "/".join(pieces) |
Michael Runge | e07c75a | 2014-12-09 13:54:23 -0800 | [diff] [blame] | 308 | elif (key in ("ro.build.thumbprint", "ro.vendor.build.thumbprint") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 309 | and misc_info.get("oem_fingerprint_properties") is not None): |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 310 | pieces = value.split("/") |
| 311 | pieces[-1] = EditTags(pieces[-1]) |
| 312 | value = "/".join(pieces) |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 313 | elif key == "ro.build.description": |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 314 | pieces = value.split(" ") |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 315 | assert len(pieces) == 5 |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 316 | pieces[-1] = EditTags(pieces[-1]) |
| 317 | value = " ".join(pieces) |
| 318 | elif key == "ro.build.tags": |
| 319 | value = EditTags(value) |
Doug Zongker | a8608a7 | 2013-07-23 11:51:04 -0700 | [diff] [blame] | 320 | elif key == "ro.build.display.id": |
| 321 | # change, eg, "JWR66N dev-keys" to "JWR66N" |
| 322 | value = value.split() |
Michael Runge | dc2661a | 2014-06-03 14:43:11 -0700 | [diff] [blame] | 323 | if len(value) > 1 and value[-1].endswith("-keys"): |
Andrew Boie | 73d5abb | 2013-12-11 12:42:03 -0800 | [diff] [blame] | 324 | value.pop() |
| 325 | value = " ".join(value) |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 326 | line = key + "=" + value |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 327 | if line != original_line: |
| 328 | print " replace: ", original_line |
| 329 | print " with: ", line |
| 330 | output.append(line) |
| 331 | return "\n".join(output) + "\n" |
| 332 | |
| 333 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 334 | def ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info): |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 335 | try: |
| 336 | keylist = input_tf_zip.read("META/otakeys.txt").split() |
| 337 | except KeyError: |
T.R. Fullhart | a28acc6 | 2013-03-18 10:31:26 -0700 | [diff] [blame] | 338 | raise common.ExternalError("can't read META/otakeys.txt from input") |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 339 | |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 340 | extra_recovery_keys = misc_info.get("extra_recovery_keys", None) |
| 341 | if extra_recovery_keys: |
| 342 | extra_recovery_keys = [OPTIONS.key_map.get(k, k) + ".x509.pem" |
| 343 | for k in extra_recovery_keys.split()] |
| 344 | if extra_recovery_keys: |
| 345 | print "extra recovery-only key(s): " + ", ".join(extra_recovery_keys) |
| 346 | else: |
| 347 | extra_recovery_keys = [] |
| 348 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 349 | mapped_keys = [] |
| 350 | for k in keylist: |
| 351 | m = re.match(r"^(.*)\.x509\.pem$", k) |
| 352 | if not m: |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 353 | raise common.ExternalError( |
| 354 | "can't parse \"%s\" from META/otakeys.txt" % (k,)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 355 | k = m.group(1) |
| 356 | mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem") |
| 357 | |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 358 | if mapped_keys: |
| 359 | print "using:\n ", "\n ".join(mapped_keys) |
| 360 | print "for OTA package verification" |
| 361 | else: |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 362 | devkey = misc_info.get("default_system_dev_certificate", |
| 363 | "build/target/product/security/testkey") |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 364 | mapped_keys.append( |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 365 | OPTIONS.key_map.get(devkey, devkey) + ".x509.pem") |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 366 | print "META/otakeys.txt has no keys; using", mapped_keys[0] |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 367 | |
| 368 | # recovery uses a version of the key that has been slightly |
| 369 | # predigested (by DumpPublicKey.java) and put in res/keys. |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 370 | # extra_recovery_keys are used only in recovery. |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 371 | |
Doug Zongker | 602a84e | 2009-06-18 08:35:12 -0700 | [diff] [blame] | 372 | p = common.Run(["java", "-jar", |
| 373 | os.path.join(OPTIONS.search_path, "framework", "dumpkey.jar")] |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 374 | + mapped_keys + extra_recovery_keys, |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 375 | stdout=subprocess.PIPE) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 376 | new_recovery_keys, _ = p.communicate() |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 377 | if p.returncode != 0: |
T.R. Fullhart | a28acc6 | 2013-03-18 10:31:26 -0700 | [diff] [blame] | 378 | raise common.ExternalError("failed to run dumpkeys") |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 379 | common.ZipWriteStr(output_tf_zip, "RECOVERY/RAMDISK/res/keys", |
| 380 | new_recovery_keys) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 381 | |
| 382 | # SystemUpdateActivity uses the x509.pem version of the keys, but |
| 383 | # put into a zipfile system/etc/security/otacerts.zip. |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 384 | # We DO NOT include the extra_recovery_keys (if any) here. |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 385 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 386 | temp_file = cStringIO.StringIO() |
| 387 | certs_zip = zipfile.ZipFile(temp_file, "w") |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 388 | for k in mapped_keys: |
| 389 | certs_zip.write(k) |
| 390 | certs_zip.close() |
Doug Zongker | 048e7ca | 2009-06-15 14:31:53 -0700 | [diff] [blame] | 391 | common.ZipWriteStr(output_tf_zip, "SYSTEM/etc/security/otacerts.zip", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 392 | temp_file.getvalue()) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 393 | |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 394 | return new_recovery_keys |
| 395 | |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 396 | def ReplaceVerityPublicKey(targetfile_zip, key_path): |
| 397 | print "Replacing verity public key with %s" % key_path |
| 398 | with open(key_path) as f: |
Andrew Boie | d083f0b | 2014-09-15 16:01:07 -0700 | [diff] [blame] | 399 | data = f.read() |
| 400 | common.ZipWriteStr(targetfile_zip, "BOOT/RAMDISK/verity_key", data) |
| 401 | return data |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 402 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame^] | 403 | def ReplaceVerityPrivateKey(targetfile_input_zip, targetfile_output_zip, |
| 404 | misc_info, key_path): |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 405 | print "Replacing verity private key with %s" % key_path |
| 406 | current_key = misc_info["verity_key"] |
| 407 | original_misc_info = targetfile_input_zip.read("META/misc_info.txt") |
| 408 | new_misc_info = original_misc_info.replace(current_key, key_path) |
| 409 | common.ZipWriteStr(targetfile_output_zip, "META/misc_info.txt", new_misc_info) |
Andrew Boie | d083f0b | 2014-09-15 16:01:07 -0700 | [diff] [blame] | 410 | misc_info["verity_key"] = key_path |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 411 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 412 | def BuildKeyMap(misc_info, key_mapping_options): |
| 413 | for s, d in key_mapping_options: |
| 414 | if s is None: # -d option |
| 415 | devkey = misc_info.get("default_system_dev_certificate", |
| 416 | "build/target/product/security/testkey") |
| 417 | devkeydir = os.path.dirname(devkey) |
| 418 | |
| 419 | OPTIONS.key_map.update({ |
| 420 | devkeydir + "/testkey": d + "/releasekey", |
| 421 | devkeydir + "/devkey": d + "/releasekey", |
| 422 | devkeydir + "/media": d + "/media", |
| 423 | devkeydir + "/shared": d + "/shared", |
| 424 | devkeydir + "/platform": d + "/platform", |
| 425 | }) |
| 426 | else: |
| 427 | OPTIONS.key_map[s] = d |
| 428 | |
| 429 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 430 | def main(argv): |
| 431 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 432 | key_mapping_options = [] |
| 433 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 434 | def option_handler(o, a): |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 435 | if o in ("-e", "--extra_apks"): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 436 | names, key = a.split("=") |
| 437 | names = names.split(",") |
| 438 | for n in names: |
| 439 | OPTIONS.extra_apks[n] = key |
| 440 | elif o in ("-d", "--default_key_mappings"): |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 441 | key_mapping_options.append((None, a)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 442 | elif o in ("-k", "--key_mapping"): |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 443 | key_mapping_options.append(a.split("=", 1)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 444 | elif o in ("-o", "--replace_ota_keys"): |
| 445 | OPTIONS.replace_ota_keys = True |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 446 | elif o in ("-t", "--tag_changes"): |
| 447 | new = [] |
| 448 | for i in a.split(","): |
| 449 | i = i.strip() |
| 450 | if not i or i[0] not in "-+": |
| 451 | raise ValueError("Bad tag change '%s'" % (i,)) |
| 452 | new.append(i[0] + i[1:].strip()) |
| 453 | OPTIONS.tag_changes = tuple(new) |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 454 | elif o == "--replace_verity_public_key": |
| 455 | OPTIONS.replace_verity_public_key = (True, a) |
| 456 | elif o == "--replace_verity_private_key": |
| 457 | OPTIONS.replace_verity_private_key = (True, a) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 458 | else: |
| 459 | return False |
| 460 | return True |
| 461 | |
| 462 | args = common.ParseOptions(argv, __doc__, |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 463 | extra_opts="e:d:k:ot:", |
| 464 | extra_long_opts=["extra_apks=", |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 465 | "default_key_mappings=", |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 466 | "key_mapping=", |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 467 | "replace_ota_keys", |
Geremy Condra | f19b365 | 2014-07-29 17:54:54 -0700 | [diff] [blame] | 468 | "tag_changes=", |
| 469 | "replace_verity_public_key=", |
| 470 | "replace_verity_private_key="], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 471 | extra_option_handler=option_handler) |
| 472 | |
| 473 | if len(args) != 2: |
| 474 | common.Usage(__doc__) |
| 475 | sys.exit(1) |
| 476 | |
| 477 | input_zip = zipfile.ZipFile(args[0], "r") |
| 478 | output_zip = zipfile.ZipFile(args[1], "w") |
| 479 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 480 | misc_info = common.LoadInfoDict(input_zip) |
| 481 | |
| 482 | BuildKeyMap(misc_info, key_mapping_options) |
| 483 | |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 484 | apk_key_map = GetApkCerts(input_zip) |
| 485 | CheckAllApksSigned(input_zip, apk_key_map) |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 486 | |
| 487 | key_passwords = common.GetKeyPasswords(set(apk_key_map.values())) |
Doug Zongker | 412c02f | 2014-02-13 10:58:24 -0800 | [diff] [blame] | 488 | ProcessTargetFiles(input_zip, output_zip, misc_info, |
| 489 | apk_key_map, key_passwords) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 490 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 491 | input_zip.close() |
| 492 | output_zip.close() |
| 493 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 494 | add_img_to_target_files.AddImagesToTargetFiles(args[1]) |
| 495 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 496 | print "done." |
| 497 | |
| 498 | |
| 499 | if __name__ == '__main__': |
| 500 | try: |
| 501 | main(sys.argv[1:]) |
| 502 | except common.ExternalError, e: |
| 503 | print |
| 504 | print " ERROR: %s" % (e,) |
| 505 | print |
| 506 | sys.exit(1) |