blob: 91e03f454a3c86bf69ed84747b2c284635e04f59 [file] [log] [blame]
Doug Zongkereef39442009-04-02 12:14:19 -07001#!/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"""
18Signs all the APK files in a target-files zipfile, producing a new
19target-files zip.
20
21Usage: sign_target_files_apks [flags] input_target_files output_target_files
22
Doug Zongkereef39442009-04-02 12:14:19 -070023 -e (--extra_apks) <name,name,...=key>
24 Add extra APK name/key pairs as though they appeared in
Doug Zongkerad88c7c2009-04-14 12:34:27 -070025 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 Zongkereef39442009-04-02 12:14:19 -070029
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
39 build/target/product/security/testkey ==> $dir/releasekey
40 build/target/product/security/media ==> $dir/media
41 build/target/product/security/shared ==> $dir/shared
42 build/target/product/security/platform ==> $dir/platform
43
44 -d and -k options are added to the set of mappings in the order
45 in which they appear on the command line.
Doug Zongker8e931bf2009-04-06 15:21:45 -070046
47 -o (--replace_ota_keys)
48 Replace the certificate (public key) used by OTA package
49 verification with the one specified in the input target_files
50 zip (in the META/otakeys.txt file). Key remapping (-k and -d)
51 is performed on this key.
Doug Zongker17aa9442009-04-17 10:15:58 -070052
Doug Zongkerae877012009-04-21 10:04:51 -070053 -t (--tag_changes) <+tag>,<-tag>,...
54 Comma-separated list of changes to make to the set of tags (in
55 the last component of the build fingerprint). Prefix each with
56 '+' or '-' to indicate whether that tag should be added or
57 removed. Changes are processed in the order they appear.
Doug Zongker5f5f08d2009-10-22 16:09:34 -070058 Default value is "-test-keys,+release-keys".
Doug Zongkerae877012009-04-21 10:04:51 -070059
Doug Zongkereef39442009-04-02 12:14:19 -070060"""
61
62import sys
63
64if sys.hexversion < 0x02040000:
65 print >> sys.stderr, "Python 2.4 or newer is required."
66 sys.exit(1)
67
Doug Zongker8e931bf2009-04-06 15:21:45 -070068import cStringIO
69import copy
Doug Zongkereef39442009-04-02 12:14:19 -070070import os
71import re
72import subprocess
73import tempfile
74import zipfile
75
76import common
77
78OPTIONS = common.OPTIONS
79
80OPTIONS.extra_apks = {}
81OPTIONS.key_map = {}
Doug Zongker8e931bf2009-04-06 15:21:45 -070082OPTIONS.replace_ota_keys = False
Doug Zongker5f5f08d2009-10-22 16:09:34 -070083OPTIONS.tag_changes = ("-test-keys", "+release-keys")
Doug Zongkereef39442009-04-02 12:14:19 -070084
85def GetApkCerts(tf_zip):
Doug Zongkerf6a53aa2009-12-15 15:06:55 -080086 certmap = common.ReadApkCerts(tf_zip)
87
88 # apply the key remapping to the contents of the file
89 for apk, cert in certmap.iteritems():
90 certmap[apk] = OPTIONS.key_map.get(cert, cert)
91
92 # apply all the -e options, overriding anything in the file
Doug Zongkerad88c7c2009-04-14 12:34:27 -070093 for apk, cert in OPTIONS.extra_apks.iteritems():
Doug Zongkerdecf9952009-12-15 17:27:49 -080094 if not cert:
95 cert = "PRESIGNED"
Doug Zongkerad88c7c2009-04-14 12:34:27 -070096 certmap[apk] = OPTIONS.key_map.get(cert, cert)
Doug Zongkerf6a53aa2009-12-15 15:06:55 -080097
Doug Zongkereef39442009-04-02 12:14:19 -070098 return certmap
99
100
Doug Zongkereb338ef2009-05-20 16:50:49 -0700101def CheckAllApksSigned(input_tf_zip, apk_key_map):
102 """Check that all the APKs we want to sign have keys specified, and
103 error out if they don't."""
104 unknown_apks = []
105 for info in input_tf_zip.infolist():
106 if info.filename.endswith(".apk"):
107 name = os.path.basename(info.filename)
108 if name not in apk_key_map:
109 unknown_apks.append(name)
110 if unknown_apks:
111 print "ERROR: no key specified for:\n\n ",
112 print "\n ".join(unknown_apks)
113 print "\nUse '-e <apkname>=' to specify a key (which may be an"
114 print "empty string to not sign this apk)."
115 sys.exit(1)
116
117
Doug Zongkereef39442009-04-02 12:14:19 -0700118def SignApk(data, keyname, pw):
119 unsigned = tempfile.NamedTemporaryFile()
120 unsigned.write(data)
121 unsigned.flush()
122
123 signed = tempfile.NamedTemporaryFile()
124
125 common.SignFile(unsigned.name, signed.name, keyname, pw, align=4)
126
127 data = signed.read()
128 unsigned.close()
129 signed.close()
130
131 return data
132
133
Doug Zongkereb338ef2009-05-20 16:50:49 -0700134def SignApks(input_tf_zip, output_tf_zip, apk_key_map, key_passwords):
Doug Zongkereef39442009-04-02 12:14:19 -0700135 maxsize = max([len(os.path.basename(i.filename))
136 for i in input_tf_zip.infolist()
137 if i.filename.endswith('.apk')])
138
139 for info in input_tf_zip.infolist():
140 data = input_tf_zip.read(info.filename)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700141 out_info = copy.copy(info)
Doug Zongkereef39442009-04-02 12:14:19 -0700142 if info.filename.endswith(".apk"):
143 name = os.path.basename(info.filename)
Doug Zongker43874f82009-04-14 14:05:15 -0700144 key = apk_key_map[name]
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800145 if key not in common.SPECIAL_CERT_STRINGS:
Doug Zongker43874f82009-04-14 14:05:15 -0700146 print " signing: %-*s (%s)" % (maxsize, name, key)
Doug Zongkereef39442009-04-02 12:14:19 -0700147 signed_data = SignApk(data, key, key_passwords[key])
Doug Zongker8e931bf2009-04-06 15:21:45 -0700148 output_tf_zip.writestr(out_info, signed_data)
Doug Zongkereef39442009-04-02 12:14:19 -0700149 else:
150 # an APK we're not supposed to sign.
Doug Zongker43874f82009-04-14 14:05:15 -0700151 print "NOT signing: %s" % (name,)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700152 output_tf_zip.writestr(out_info, data)
153 elif info.filename in ("SYSTEM/build.prop",
154 "RECOVERY/RAMDISK/default.prop"):
Doug Zongker17aa9442009-04-17 10:15:58 -0700155 print "rewriting %s:" % (info.filename,)
156 new_data = RewriteProps(data)
157 output_tf_zip.writestr(out_info, new_data)
Doug Zongkereef39442009-04-02 12:14:19 -0700158 else:
159 # a non-APK file; copy it verbatim
Doug Zongker8e931bf2009-04-06 15:21:45 -0700160 output_tf_zip.writestr(out_info, data)
161
162
Doug Zongker17aa9442009-04-17 10:15:58 -0700163def RewriteProps(data):
164 output = []
165 for line in data.split("\n"):
166 line = line.strip()
167 original_line = line
168 if line and line[0] != '#':
169 key, value = line.split("=", 1)
170 if key == "ro.build.fingerprint":
171 pieces = line.split("/")
172 tags = set(pieces[-1].split(","))
Doug Zongkerae877012009-04-21 10:04:51 -0700173 for ch in OPTIONS.tag_changes:
174 if ch[0] == "-":
175 tags.discard(ch[1:])
176 elif ch[0] == "+":
177 tags.add(ch[1:])
Doug Zongker17aa9442009-04-17 10:15:58 -0700178 line = "/".join(pieces[:-1] + [",".join(sorted(tags))])
179 elif key == "ro.build.description":
180 pieces = line.split(" ")
181 assert len(pieces) == 5
182 tags = set(pieces[-1].split(","))
Doug Zongkerae877012009-04-21 10:04:51 -0700183 for ch in OPTIONS.tag_changes:
184 if ch[0] == "-":
185 tags.discard(ch[1:])
186 elif ch[0] == "+":
187 tags.add(ch[1:])
Doug Zongker17aa9442009-04-17 10:15:58 -0700188 line = " ".join(pieces[:-1] + [",".join(sorted(tags))])
189 if line != original_line:
190 print " replace: ", original_line
191 print " with: ", line
192 output.append(line)
193 return "\n".join(output) + "\n"
194
195
Doug Zongker8e931bf2009-04-06 15:21:45 -0700196def ReplaceOtaKeys(input_tf_zip, output_tf_zip):
197 try:
198 keylist = input_tf_zip.read("META/otakeys.txt").split()
199 except KeyError:
200 raise ExternalError("can't read META/otakeys.txt from input")
201
202 mapped_keys = []
203 for k in keylist:
204 m = re.match(r"^(.*)\.x509\.pem$", k)
205 if not m:
206 raise ExternalError("can't parse \"%s\" from META/otakeys.txt" % (k,))
207 k = m.group(1)
208 mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem")
209
Doug Zongkere05628c2009-08-20 17:38:42 -0700210 if mapped_keys:
211 print "using:\n ", "\n ".join(mapped_keys)
212 print "for OTA package verification"
213 else:
214 mapped_keys.append(
215 OPTIONS.key_map["build/target/product/security/testkey"] + ".x509.pem")
216 print "META/otakeys.txt has no keys; using", mapped_keys[0]
Doug Zongker8e931bf2009-04-06 15:21:45 -0700217
218 # recovery uses a version of the key that has been slightly
219 # predigested (by DumpPublicKey.java) and put in res/keys.
220
Doug Zongker602a84e2009-06-18 08:35:12 -0700221 p = common.Run(["java", "-jar",
222 os.path.join(OPTIONS.search_path, "framework", "dumpkey.jar")]
223 + mapped_keys,
Doug Zongker8e931bf2009-04-06 15:21:45 -0700224 stdout=subprocess.PIPE)
225 data, _ = p.communicate()
226 if p.returncode != 0:
227 raise ExternalError("failed to run dumpkeys")
Doug Zongker048e7ca2009-06-15 14:31:53 -0700228 common.ZipWriteStr(output_tf_zip, "RECOVERY/RAMDISK/res/keys", data)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700229
230 # SystemUpdateActivity uses the x509.pem version of the keys, but
231 # put into a zipfile system/etc/security/otacerts.zip.
232
233 tempfile = cStringIO.StringIO()
234 certs_zip = zipfile.ZipFile(tempfile, "w")
235 for k in mapped_keys:
236 certs_zip.write(k)
237 certs_zip.close()
Doug Zongker048e7ca2009-06-15 14:31:53 -0700238 common.ZipWriteStr(output_tf_zip, "SYSTEM/etc/security/otacerts.zip",
239 tempfile.getvalue())
Doug Zongkereef39442009-04-02 12:14:19 -0700240
241
242def main(argv):
243
244 def option_handler(o, a):
Doug Zongker05d3dea2009-06-22 11:32:31 -0700245 if o in ("-e", "--extra_apks"):
Doug Zongkereef39442009-04-02 12:14:19 -0700246 names, key = a.split("=")
247 names = names.split(",")
248 for n in names:
249 OPTIONS.extra_apks[n] = key
250 elif o in ("-d", "--default_key_mappings"):
251 OPTIONS.key_map.update({
252 "build/target/product/security/testkey": "%s/releasekey" % (a,),
253 "build/target/product/security/media": "%s/media" % (a,),
254 "build/target/product/security/shared": "%s/shared" % (a,),
255 "build/target/product/security/platform": "%s/platform" % (a,),
256 })
257 elif o in ("-k", "--key_mapping"):
258 s, d = a.split("=")
259 OPTIONS.key_map[s] = d
Doug Zongker8e931bf2009-04-06 15:21:45 -0700260 elif o in ("-o", "--replace_ota_keys"):
261 OPTIONS.replace_ota_keys = True
Doug Zongkerae877012009-04-21 10:04:51 -0700262 elif o in ("-t", "--tag_changes"):
263 new = []
264 for i in a.split(","):
265 i = i.strip()
266 if not i or i[0] not in "-+":
267 raise ValueError("Bad tag change '%s'" % (i,))
268 new.append(i[0] + i[1:].strip())
269 OPTIONS.tag_changes = tuple(new)
Doug Zongkereef39442009-04-02 12:14:19 -0700270 else:
271 return False
272 return True
273
274 args = common.ParseOptions(argv, __doc__,
Doug Zongker05d3dea2009-06-22 11:32:31 -0700275 extra_opts="e:d:k:ot:",
276 extra_long_opts=["extra_apks=",
Doug Zongkereef39442009-04-02 12:14:19 -0700277 "default_key_mappings=",
Doug Zongker8e931bf2009-04-06 15:21:45 -0700278 "key_mapping=",
Doug Zongker17aa9442009-04-17 10:15:58 -0700279 "replace_ota_keys",
Doug Zongkerae877012009-04-21 10:04:51 -0700280 "tag_changes="],
Doug Zongkereef39442009-04-02 12:14:19 -0700281 extra_option_handler=option_handler)
282
283 if len(args) != 2:
284 common.Usage(__doc__)
285 sys.exit(1)
286
287 input_zip = zipfile.ZipFile(args[0], "r")
288 output_zip = zipfile.ZipFile(args[1], "w")
289
Doug Zongkereb338ef2009-05-20 16:50:49 -0700290 apk_key_map = GetApkCerts(input_zip)
291 CheckAllApksSigned(input_zip, apk_key_map)
Doug Zongkereb338ef2009-05-20 16:50:49 -0700292
293 key_passwords = common.GetKeyPasswords(set(apk_key_map.values()))
294 SignApks(input_zip, output_zip, apk_key_map, key_passwords)
Doug Zongkereef39442009-04-02 12:14:19 -0700295
Doug Zongker8e931bf2009-04-06 15:21:45 -0700296 if OPTIONS.replace_ota_keys:
297 ReplaceOtaKeys(input_zip, output_zip)
298
Doug Zongkereef39442009-04-02 12:14:19 -0700299 input_zip.close()
300 output_zip.close()
301
302 print "done."
303
304
305if __name__ == '__main__':
306 try:
307 main(sys.argv[1:])
308 except common.ExternalError, e:
309 print
310 print " ERROR: %s" % (e,)
311 print
312 sys.exit(1)