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 | |
| 70 | if sys.hexversion < 0x02040000: |
| 71 | print >> sys.stderr, "Python 2.4 or newer is required." |
| 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 |
| 80 | import subprocess |
| 81 | import tempfile |
| 82 | import zipfile |
| 83 | |
| 84 | import common |
| 85 | |
| 86 | OPTIONS = common.OPTIONS |
| 87 | |
| 88 | OPTIONS.extra_apks = {} |
| 89 | OPTIONS.key_map = {} |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 90 | OPTIONS.replace_ota_keys = False |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 91 | OPTIONS.tag_changes = ("-test-keys", "-dev-keys", "+release-keys") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 92 | |
| 93 | def GetApkCerts(tf_zip): |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 94 | certmap = common.ReadApkCerts(tf_zip) |
| 95 | |
| 96 | # apply the key remapping to the contents of the file |
| 97 | for apk, cert in certmap.iteritems(): |
| 98 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
| 99 | |
| 100 | # apply all the -e options, overriding anything in the file |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 101 | for apk, cert in OPTIONS.extra_apks.iteritems(): |
Doug Zongker | decf995 | 2009-12-15 17:27:49 -0800 | [diff] [blame] | 102 | if not cert: |
| 103 | cert = "PRESIGNED" |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 104 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 105 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 106 | return certmap |
| 107 | |
| 108 | |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 109 | def CheckAllApksSigned(input_tf_zip, apk_key_map): |
| 110 | """Check that all the APKs we want to sign have keys specified, and |
| 111 | error out if they don't.""" |
| 112 | unknown_apks = [] |
| 113 | for info in input_tf_zip.infolist(): |
| 114 | if info.filename.endswith(".apk"): |
| 115 | name = os.path.basename(info.filename) |
| 116 | if name not in apk_key_map: |
| 117 | unknown_apks.append(name) |
| 118 | if unknown_apks: |
| 119 | print "ERROR: no key specified for:\n\n ", |
| 120 | print "\n ".join(unknown_apks) |
| 121 | print "\nUse '-e <apkname>=' to specify a key (which may be an" |
| 122 | print "empty string to not sign this apk)." |
| 123 | sys.exit(1) |
| 124 | |
| 125 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 126 | def SignApk(data, keyname, pw): |
| 127 | unsigned = tempfile.NamedTemporaryFile() |
| 128 | unsigned.write(data) |
| 129 | unsigned.flush() |
| 130 | |
| 131 | signed = tempfile.NamedTemporaryFile() |
| 132 | |
| 133 | common.SignFile(unsigned.name, signed.name, keyname, pw, align=4) |
| 134 | |
| 135 | data = signed.read() |
| 136 | unsigned.close() |
| 137 | signed.close() |
| 138 | |
| 139 | return data |
| 140 | |
| 141 | |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 142 | def SignApks(input_tf_zip, output_tf_zip, apk_key_map, key_passwords): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 143 | maxsize = max([len(os.path.basename(i.filename)) |
| 144 | for i in input_tf_zip.infolist() |
| 145 | if i.filename.endswith('.apk')]) |
| 146 | |
| 147 | for info in input_tf_zip.infolist(): |
| 148 | data = input_tf_zip.read(info.filename) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 149 | out_info = copy.copy(info) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 150 | if info.filename.endswith(".apk"): |
| 151 | name = os.path.basename(info.filename) |
Doug Zongker | 43874f8 | 2009-04-14 14:05:15 -0700 | [diff] [blame] | 152 | key = apk_key_map[name] |
Doug Zongker | f6a53aa | 2009-12-15 15:06:55 -0800 | [diff] [blame] | 153 | if key not in common.SPECIAL_CERT_STRINGS: |
Doug Zongker | 43874f8 | 2009-04-14 14:05:15 -0700 | [diff] [blame] | 154 | print " signing: %-*s (%s)" % (maxsize, name, key) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 155 | signed_data = SignApk(data, key, key_passwords[key]) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 156 | output_tf_zip.writestr(out_info, signed_data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 157 | else: |
| 158 | # an APK we're not supposed to sign. |
Doug Zongker | 43874f8 | 2009-04-14 14:05:15 -0700 | [diff] [blame] | 159 | print "NOT signing: %s" % (name,) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 160 | output_tf_zip.writestr(out_info, data) |
| 161 | elif info.filename in ("SYSTEM/build.prop", |
| 162 | "RECOVERY/RAMDISK/default.prop"): |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 163 | print "rewriting %s:" % (info.filename,) |
| 164 | new_data = RewriteProps(data) |
| 165 | output_tf_zip.writestr(out_info, new_data) |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame^] | 166 | elif info.filename.endswith("mac_permissions.xml"): |
| 167 | print "rewriting %s with new keys." % (info.filename,) |
| 168 | new_data = ReplaceCerts(data) |
| 169 | output_tf_zip.writestr(out_info, new_data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 170 | else: |
| 171 | # a non-APK file; copy it verbatim |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 172 | output_tf_zip.writestr(out_info, data) |
| 173 | |
| 174 | |
Robert Craig | 817c574 | 2013-04-19 10:59:22 -0400 | [diff] [blame^] | 175 | def ReplaceCerts(data): |
| 176 | """Given a string of data, replace all occurences of a set |
| 177 | of X509 certs with a newer set of X509 certs and return |
| 178 | the updated data string.""" |
| 179 | for old, new in OPTIONS.key_map.iteritems(): |
| 180 | try: |
| 181 | if OPTIONS.verbose: |
| 182 | print " Replacing %s.x509.pem with %s.x509.pem" % (old, new) |
| 183 | f = open(old + ".x509.pem") |
| 184 | old_cert16 = base64.b16encode(common.ParseCertificate(f.read())).lower() |
| 185 | f.close() |
| 186 | f = open(new + ".x509.pem") |
| 187 | new_cert16 = base64.b16encode(common.ParseCertificate(f.read())).lower() |
| 188 | f.close() |
| 189 | # Only match entire certs. |
| 190 | pattern = "\\b"+old_cert16+"\\b" |
| 191 | (data, num) = re.subn(pattern, new_cert16, data, flags=re.IGNORECASE) |
| 192 | if OPTIONS.verbose: |
| 193 | print " Replaced %d occurence(s) of %s.x509.pem with " \ |
| 194 | "%s.x509.pem" % (num, old, new) |
| 195 | except IOError, e: |
| 196 | if (e.errno == errno.ENOENT and not OPTIONS.verbose): |
| 197 | continue |
| 198 | |
| 199 | print " Error accessing %s. %s. Skip replacing %s.x509.pem " \ |
| 200 | "with %s.x509.pem." % (e.filename, e.strerror, old, new) |
| 201 | |
| 202 | return data |
| 203 | |
| 204 | |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 205 | def EditTags(tags): |
| 206 | """Given a string containing comma-separated tags, apply the edits |
| 207 | specified in OPTIONS.tag_changes and return the updated string.""" |
| 208 | tags = set(tags.split(",")) |
| 209 | for ch in OPTIONS.tag_changes: |
| 210 | if ch[0] == "-": |
| 211 | tags.discard(ch[1:]) |
| 212 | elif ch[0] == "+": |
| 213 | tags.add(ch[1:]) |
| 214 | return ",".join(sorted(tags)) |
| 215 | |
| 216 | |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 217 | def RewriteProps(data): |
| 218 | output = [] |
| 219 | for line in data.split("\n"): |
| 220 | line = line.strip() |
| 221 | original_line = line |
| 222 | if line and line[0] != '#': |
| 223 | key, value = line.split("=", 1) |
| 224 | if key == "ro.build.fingerprint": |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 225 | pieces = value.split("/") |
| 226 | pieces[-1] = EditTags(pieces[-1]) |
| 227 | value = "/".join(pieces) |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 228 | elif key == "ro.build.description": |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 229 | pieces = value.split(" ") |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 230 | assert len(pieces) == 5 |
Doug Zongker | c09abc8 | 2010-01-11 13:09:15 -0800 | [diff] [blame] | 231 | pieces[-1] = EditTags(pieces[-1]) |
| 232 | value = " ".join(pieces) |
| 233 | elif key == "ro.build.tags": |
| 234 | value = EditTags(value) |
| 235 | line = key + "=" + value |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 236 | if line != original_line: |
| 237 | print " replace: ", original_line |
| 238 | print " with: ", line |
| 239 | output.append(line) |
| 240 | return "\n".join(output) + "\n" |
| 241 | |
| 242 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 243 | def ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info): |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 244 | try: |
| 245 | keylist = input_tf_zip.read("META/otakeys.txt").split() |
| 246 | except KeyError: |
T.R. Fullhart | 37e1052 | 2013-03-18 10:31:26 -0700 | [diff] [blame] | 247 | raise common.ExternalError("can't read META/otakeys.txt from input") |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 248 | |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 249 | extra_recovery_keys = misc_info.get("extra_recovery_keys", None) |
| 250 | if extra_recovery_keys: |
| 251 | extra_recovery_keys = [OPTIONS.key_map.get(k, k) + ".x509.pem" |
| 252 | for k in extra_recovery_keys.split()] |
| 253 | if extra_recovery_keys: |
| 254 | print "extra recovery-only key(s): " + ", ".join(extra_recovery_keys) |
| 255 | else: |
| 256 | extra_recovery_keys = [] |
| 257 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 258 | mapped_keys = [] |
| 259 | for k in keylist: |
| 260 | m = re.match(r"^(.*)\.x509\.pem$", k) |
| 261 | if not m: |
T.R. Fullhart | 37e1052 | 2013-03-18 10:31:26 -0700 | [diff] [blame] | 262 | raise common.ExternalError("can't parse \"%s\" from META/otakeys.txt" % (k,)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 263 | k = m.group(1) |
| 264 | mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem") |
| 265 | |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 266 | if mapped_keys: |
| 267 | print "using:\n ", "\n ".join(mapped_keys) |
| 268 | print "for OTA package verification" |
| 269 | else: |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 270 | devkey = misc_info.get("default_system_dev_certificate", |
| 271 | "build/target/product/security/testkey") |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 272 | mapped_keys.append( |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 273 | OPTIONS.key_map.get(devkey, devkey) + ".x509.pem") |
Doug Zongker | e05628c | 2009-08-20 17:38:42 -0700 | [diff] [blame] | 274 | print "META/otakeys.txt has no keys; using", mapped_keys[0] |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 275 | |
| 276 | # recovery uses a version of the key that has been slightly |
| 277 | # predigested (by DumpPublicKey.java) and put in res/keys. |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 278 | # extra_recovery_keys are used only in recovery. |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 279 | |
Doug Zongker | 602a84e | 2009-06-18 08:35:12 -0700 | [diff] [blame] | 280 | p = common.Run(["java", "-jar", |
| 281 | os.path.join(OPTIONS.search_path, "framework", "dumpkey.jar")] |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 282 | + mapped_keys + extra_recovery_keys, |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 283 | stdout=subprocess.PIPE) |
| 284 | data, _ = p.communicate() |
| 285 | if p.returncode != 0: |
T.R. Fullhart | 37e1052 | 2013-03-18 10:31:26 -0700 | [diff] [blame] | 286 | raise common.ExternalError("failed to run dumpkeys") |
Doug Zongker | 048e7ca | 2009-06-15 14:31:53 -0700 | [diff] [blame] | 287 | common.ZipWriteStr(output_tf_zip, "RECOVERY/RAMDISK/res/keys", data) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 288 | |
| 289 | # SystemUpdateActivity uses the x509.pem version of the keys, but |
| 290 | # put into a zipfile system/etc/security/otacerts.zip. |
Doug Zongker | e121d6a | 2011-02-01 14:13:52 -0800 | [diff] [blame] | 291 | # We DO NOT include the extra_recovery_keys (if any) here. |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 292 | |
| 293 | tempfile = cStringIO.StringIO() |
| 294 | certs_zip = zipfile.ZipFile(tempfile, "w") |
| 295 | for k in mapped_keys: |
| 296 | certs_zip.write(k) |
| 297 | certs_zip.close() |
Doug Zongker | 048e7ca | 2009-06-15 14:31:53 -0700 | [diff] [blame] | 298 | common.ZipWriteStr(output_tf_zip, "SYSTEM/etc/security/otacerts.zip", |
| 299 | tempfile.getvalue()) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 300 | |
| 301 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 302 | def BuildKeyMap(misc_info, key_mapping_options): |
| 303 | for s, d in key_mapping_options: |
| 304 | if s is None: # -d option |
| 305 | devkey = misc_info.get("default_system_dev_certificate", |
| 306 | "build/target/product/security/testkey") |
| 307 | devkeydir = os.path.dirname(devkey) |
| 308 | |
| 309 | OPTIONS.key_map.update({ |
| 310 | devkeydir + "/testkey": d + "/releasekey", |
| 311 | devkeydir + "/devkey": d + "/releasekey", |
| 312 | devkeydir + "/media": d + "/media", |
| 313 | devkeydir + "/shared": d + "/shared", |
| 314 | devkeydir + "/platform": d + "/platform", |
| 315 | }) |
| 316 | else: |
| 317 | OPTIONS.key_map[s] = d |
| 318 | |
| 319 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 320 | def main(argv): |
| 321 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 322 | key_mapping_options = [] |
| 323 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 324 | def option_handler(o, a): |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 325 | if o in ("-e", "--extra_apks"): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 326 | names, key = a.split("=") |
| 327 | names = names.split(",") |
| 328 | for n in names: |
| 329 | OPTIONS.extra_apks[n] = key |
| 330 | elif o in ("-d", "--default_key_mappings"): |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 331 | key_mapping_options.append((None, a)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 332 | elif o in ("-k", "--key_mapping"): |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 333 | key_mapping_options.append(a.split("=", 1)) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 334 | elif o in ("-o", "--replace_ota_keys"): |
| 335 | OPTIONS.replace_ota_keys = True |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 336 | elif o in ("-t", "--tag_changes"): |
| 337 | new = [] |
| 338 | for i in a.split(","): |
| 339 | i = i.strip() |
| 340 | if not i or i[0] not in "-+": |
| 341 | raise ValueError("Bad tag change '%s'" % (i,)) |
| 342 | new.append(i[0] + i[1:].strip()) |
| 343 | OPTIONS.tag_changes = tuple(new) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 344 | else: |
| 345 | return False |
| 346 | return True |
| 347 | |
| 348 | args = common.ParseOptions(argv, __doc__, |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 349 | extra_opts="e:d:k:ot:", |
| 350 | extra_long_opts=["extra_apks=", |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 351 | "default_key_mappings=", |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 352 | "key_mapping=", |
Doug Zongker | 17aa944 | 2009-04-17 10:15:58 -0700 | [diff] [blame] | 353 | "replace_ota_keys", |
Doug Zongker | ae87701 | 2009-04-21 10:04:51 -0700 | [diff] [blame] | 354 | "tag_changes="], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 355 | extra_option_handler=option_handler) |
| 356 | |
| 357 | if len(args) != 2: |
| 358 | common.Usage(__doc__) |
| 359 | sys.exit(1) |
| 360 | |
| 361 | input_zip = zipfile.ZipFile(args[0], "r") |
| 362 | output_zip = zipfile.ZipFile(args[1], "w") |
| 363 | |
Doug Zongker | 831840e | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 364 | misc_info = common.LoadInfoDict(input_zip) |
| 365 | |
| 366 | BuildKeyMap(misc_info, key_mapping_options) |
| 367 | |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 368 | apk_key_map = GetApkCerts(input_zip) |
| 369 | CheckAllApksSigned(input_zip, apk_key_map) |
Doug Zongker | eb338ef | 2009-05-20 16:50:49 -0700 | [diff] [blame] | 370 | |
| 371 | key_passwords = common.GetKeyPasswords(set(apk_key_map.values())) |
| 372 | SignApks(input_zip, output_zip, apk_key_map, key_passwords) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 373 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 374 | if OPTIONS.replace_ota_keys: |
Doug Zongker | b11e2d7 | 2011-10-05 11:23:06 -0700 | [diff] [blame] | 375 | ReplaceOtaKeys(input_zip, output_zip, misc_info) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 376 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 377 | input_zip.close() |
| 378 | output_zip.close() |
| 379 | |
| 380 | print "done." |
| 381 | |
| 382 | |
| 383 | if __name__ == '__main__': |
| 384 | try: |
| 385 | main(sys.argv[1:]) |
| 386 | except common.ExternalError, e: |
| 387 | print |
| 388 | print " ERROR: %s" % (e,) |
| 389 | print |
| 390 | sys.exit(1) |