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 | |
| 23 | -s (--signapk_jar) <path> |
| 24 | Path of the signapks.jar file used to sign an individual APK |
| 25 | file. |
| 26 | |
| 27 | -e (--extra_apks) <name,name,...=key> |
| 28 | Add extra APK name/key pairs as though they appeared in |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 29 | apkcerts.txt (so mappings specified by -k and -d are applied). |
| 30 | Keys specified in -e override any value for that app contained |
| 31 | in the apkcerts.txt file. Option may be repeated to give |
| 32 | multiple extra packages. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 33 | |
| 34 | -k (--key_mapping) <src_key=dest_key> |
| 35 | Add a mapping from the key name as specified in apkcerts.txt (the |
| 36 | src_key) to the real key you wish to sign the package with |
| 37 | (dest_key). Option may be repeated to give multiple key |
| 38 | mappings. |
| 39 | |
| 40 | -d (--default_key_mappings) <dir> |
| 41 | Set up the following key mappings: |
| 42 | |
| 43 | build/target/product/security/testkey ==> $dir/releasekey |
| 44 | build/target/product/security/media ==> $dir/media |
| 45 | build/target/product/security/shared ==> $dir/shared |
| 46 | build/target/product/security/platform ==> $dir/platform |
| 47 | |
| 48 | -d and -k options are added to the set of mappings in the order |
| 49 | in which they appear on the command line. |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 50 | |
| 51 | -o (--replace_ota_keys) |
| 52 | Replace the certificate (public key) used by OTA package |
| 53 | verification with the one specified in the input target_files |
| 54 | zip (in the META/otakeys.txt file). Key remapping (-k and -d) |
| 55 | is performed on this key. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 56 | """ |
| 57 | |
| 58 | import sys |
| 59 | |
| 60 | if sys.hexversion < 0x02040000: |
| 61 | print >> sys.stderr, "Python 2.4 or newer is required." |
| 62 | sys.exit(1) |
| 63 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 64 | import cStringIO |
| 65 | import copy |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 66 | import os |
| 67 | import re |
| 68 | import subprocess |
| 69 | import tempfile |
| 70 | import zipfile |
| 71 | |
| 72 | import common |
| 73 | |
| 74 | OPTIONS = common.OPTIONS |
| 75 | |
| 76 | OPTIONS.extra_apks = {} |
| 77 | OPTIONS.key_map = {} |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 78 | OPTIONS.replace_ota_keys = False |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 79 | |
| 80 | def GetApkCerts(tf_zip): |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 81 | certmap = {} |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 82 | for line in tf_zip.read("META/apkcerts.txt").split("\n"): |
| 83 | line = line.strip() |
| 84 | if not line: continue |
| 85 | m = re.match(r'^name="(.*)"\s+certificate="(.*)\.x509\.pem"\s+' |
| 86 | r'private_key="\2\.pk8"$', line) |
| 87 | if not m: |
| 88 | raise SigningError("failed to parse line from apkcerts.txt:\n" + line) |
| 89 | certmap[m.group(1)] = OPTIONS.key_map.get(m.group(2), m.group(2)) |
Doug Zongker | ad88c7c | 2009-04-14 12:34:27 -0700 | [diff] [blame] | 90 | for apk, cert in OPTIONS.extra_apks.iteritems(): |
| 91 | certmap[apk] = OPTIONS.key_map.get(cert, cert) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 92 | return certmap |
| 93 | |
| 94 | |
| 95 | def SignApk(data, keyname, pw): |
| 96 | unsigned = tempfile.NamedTemporaryFile() |
| 97 | unsigned.write(data) |
| 98 | unsigned.flush() |
| 99 | |
| 100 | signed = tempfile.NamedTemporaryFile() |
| 101 | |
| 102 | common.SignFile(unsigned.name, signed.name, keyname, pw, align=4) |
| 103 | |
| 104 | data = signed.read() |
| 105 | unsigned.close() |
| 106 | signed.close() |
| 107 | |
| 108 | return data |
| 109 | |
| 110 | |
| 111 | def SignApks(input_tf_zip, output_tf_zip): |
| 112 | apk_key_map = GetApkCerts(input_tf_zip) |
| 113 | |
| 114 | key_passwords = common.GetKeyPasswords(set(apk_key_map.values())) |
| 115 | |
| 116 | maxsize = max([len(os.path.basename(i.filename)) |
| 117 | for i in input_tf_zip.infolist() |
| 118 | if i.filename.endswith('.apk')]) |
| 119 | |
| 120 | for info in input_tf_zip.infolist(): |
| 121 | data = input_tf_zip.read(info.filename) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 122 | out_info = copy.copy(info) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 123 | if info.filename.endswith(".apk"): |
| 124 | name = os.path.basename(info.filename) |
| 125 | key = apk_key_map.get(name, None) |
| 126 | if key is not None: |
| 127 | print "signing: %-*s (%s)" % (maxsize, name, key) |
| 128 | signed_data = SignApk(data, key, key_passwords[key]) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 129 | output_tf_zip.writestr(out_info, signed_data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 130 | else: |
| 131 | # an APK we're not supposed to sign. |
| 132 | print "skipping: %s" % (name,) |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 133 | output_tf_zip.writestr(out_info, data) |
| 134 | elif info.filename in ("SYSTEM/build.prop", |
| 135 | "RECOVERY/RAMDISK/default.prop"): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 136 | # Change build fingerprint to reflect the fact that apps are signed. |
| 137 | m = re.search(r"ro\.build\.fingerprint=.*\b(test-keys)\b.*", data) |
| 138 | if not m: |
| 139 | print 'WARNING: ro.build.fingerprint does not contain "test-keys"' |
| 140 | else: |
| 141 | data = data[:m.start(1)] + "release-keys" + data[m.end(1):] |
| 142 | m = re.search(r"ro\.build\.description=.*\b(test-keys)\b.*", data) |
| 143 | if not m: |
| 144 | print 'WARNING: ro.build.description does not contain "test-keys"' |
| 145 | else: |
| 146 | data = data[:m.start(1)] + "release-keys" + data[m.end(1):] |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 147 | output_tf_zip.writestr(out_info, data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 148 | else: |
| 149 | # a non-APK file; copy it verbatim |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 150 | output_tf_zip.writestr(out_info, data) |
| 151 | |
| 152 | |
| 153 | def ReplaceOtaKeys(input_tf_zip, output_tf_zip): |
| 154 | try: |
| 155 | keylist = input_tf_zip.read("META/otakeys.txt").split() |
| 156 | except KeyError: |
| 157 | raise ExternalError("can't read META/otakeys.txt from input") |
| 158 | |
| 159 | mapped_keys = [] |
| 160 | for k in keylist: |
| 161 | m = re.match(r"^(.*)\.x509\.pem$", k) |
| 162 | if not m: |
| 163 | raise ExternalError("can't parse \"%s\" from META/otakeys.txt" % (k,)) |
| 164 | k = m.group(1) |
| 165 | mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem") |
| 166 | |
| 167 | print "using:\n ", "\n ".join(mapped_keys) |
| 168 | print "for OTA package verification" |
| 169 | |
| 170 | # recovery uses a version of the key that has been slightly |
| 171 | # predigested (by DumpPublicKey.java) and put in res/keys. |
| 172 | |
| 173 | p = common.Run(["java", "-jar", OPTIONS.dumpkey_jar] + mapped_keys, |
| 174 | stdout=subprocess.PIPE) |
| 175 | data, _ = p.communicate() |
| 176 | if p.returncode != 0: |
| 177 | raise ExternalError("failed to run dumpkeys") |
| 178 | output_tf_zip.writestr("RECOVERY/RAMDISK/res/keys", data) |
| 179 | |
| 180 | # SystemUpdateActivity uses the x509.pem version of the keys, but |
| 181 | # put into a zipfile system/etc/security/otacerts.zip. |
| 182 | |
| 183 | tempfile = cStringIO.StringIO() |
| 184 | certs_zip = zipfile.ZipFile(tempfile, "w") |
| 185 | for k in mapped_keys: |
| 186 | certs_zip.write(k) |
| 187 | certs_zip.close() |
| 188 | output_tf_zip.writestr("SYSTEM/etc/security/otacerts.zip", |
| 189 | tempfile.getvalue()) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 190 | |
| 191 | |
| 192 | def main(argv): |
| 193 | |
| 194 | def option_handler(o, a): |
| 195 | if o in ("-s", "--signapk_jar"): |
| 196 | OPTIONS.signapk_jar = a |
| 197 | elif o in ("-e", "--extra_apks"): |
| 198 | names, key = a.split("=") |
| 199 | names = names.split(",") |
| 200 | for n in names: |
| 201 | OPTIONS.extra_apks[n] = key |
| 202 | elif o in ("-d", "--default_key_mappings"): |
| 203 | OPTIONS.key_map.update({ |
| 204 | "build/target/product/security/testkey": "%s/releasekey" % (a,), |
| 205 | "build/target/product/security/media": "%s/media" % (a,), |
| 206 | "build/target/product/security/shared": "%s/shared" % (a,), |
| 207 | "build/target/product/security/platform": "%s/platform" % (a,), |
| 208 | }) |
| 209 | elif o in ("-k", "--key_mapping"): |
| 210 | s, d = a.split("=") |
| 211 | OPTIONS.key_map[s] = d |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 212 | elif o in ("-o", "--replace_ota_keys"): |
| 213 | OPTIONS.replace_ota_keys = True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 214 | else: |
| 215 | return False |
| 216 | return True |
| 217 | |
| 218 | args = common.ParseOptions(argv, __doc__, |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 219 | extra_opts="s:e:d:k:o", |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 220 | extra_long_opts=["signapk_jar=", |
| 221 | "extra_apks=", |
| 222 | "default_key_mappings=", |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 223 | "key_mapping=", |
| 224 | "replace_ota_keys"], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 225 | extra_option_handler=option_handler) |
| 226 | |
| 227 | if len(args) != 2: |
| 228 | common.Usage(__doc__) |
| 229 | sys.exit(1) |
| 230 | |
| 231 | input_zip = zipfile.ZipFile(args[0], "r") |
| 232 | output_zip = zipfile.ZipFile(args[1], "w") |
| 233 | |
| 234 | SignApks(input_zip, output_zip) |
| 235 | |
Doug Zongker | 8e931bf | 2009-04-06 15:21:45 -0700 | [diff] [blame] | 236 | if OPTIONS.replace_ota_keys: |
| 237 | ReplaceOtaKeys(input_zip, output_zip) |
| 238 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 239 | input_zip.close() |
| 240 | output_zip.close() |
| 241 | |
| 242 | print "done." |
| 243 | |
| 244 | |
| 245 | if __name__ == '__main__': |
| 246 | try: |
| 247 | main(sys.argv[1:]) |
| 248 | except common.ExternalError, e: |
| 249 | print |
| 250 | print " ERROR: %s" % (e,) |
| 251 | print |
| 252 | sys.exit(1) |