blob: ab24706506de997f0cf38eff1262ad65b66e0267 [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
Doug Zongker831840e2011-09-22 10:28:04 -070039 $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 Zongkereef39442009-04-02 12:14:19 -070049
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 Zongker8e931bf2009-04-06 15:21:45 -070052
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 Zongker17aa9442009-04-17 10:15:58 -070058
Doug Zongkerae877012009-04-21 10:04:51 -070059 -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 Zongker831840e2011-09-22 10:28:04 -070064 Default value is "-test-keys,-dev-keys,+release-keys".
Doug Zongkerae877012009-04-21 10:04:51 -070065
Doug Zongkereef39442009-04-02 12:14:19 -070066"""
67
68import sys
69
70if sys.hexversion < 0x02040000:
71 print >> sys.stderr, "Python 2.4 or newer is required."
72 sys.exit(1)
73
Robert Craig817c5742013-04-19 10:59:22 -040074import base64
Doug Zongker8e931bf2009-04-06 15:21:45 -070075import cStringIO
76import copy
Robert Craig817c5742013-04-19 10:59:22 -040077import errno
Doug Zongkereef39442009-04-02 12:14:19 -070078import os
79import re
80import subprocess
81import tempfile
82import zipfile
83
84import common
85
86OPTIONS = common.OPTIONS
87
88OPTIONS.extra_apks = {}
89OPTIONS.key_map = {}
Doug Zongker8e931bf2009-04-06 15:21:45 -070090OPTIONS.replace_ota_keys = False
Doug Zongker831840e2011-09-22 10:28:04 -070091OPTIONS.tag_changes = ("-test-keys", "-dev-keys", "+release-keys")
Doug Zongkereef39442009-04-02 12:14:19 -070092
93def GetApkCerts(tf_zip):
Doug Zongkerf6a53aa2009-12-15 15:06:55 -080094 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 Zongkerad88c7c2009-04-14 12:34:27 -0700101 for apk, cert in OPTIONS.extra_apks.iteritems():
Doug Zongkerdecf9952009-12-15 17:27:49 -0800102 if not cert:
103 cert = "PRESIGNED"
Doug Zongkerad88c7c2009-04-14 12:34:27 -0700104 certmap[apk] = OPTIONS.key_map.get(cert, cert)
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800105
Doug Zongkereef39442009-04-02 12:14:19 -0700106 return certmap
107
108
Doug Zongkereb338ef2009-05-20 16:50:49 -0700109def 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 Zongkereef39442009-04-02 12:14:19 -0700126def 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 Zongkereb338ef2009-05-20 16:50:49 -0700142def SignApks(input_tf_zip, output_tf_zip, apk_key_map, key_passwords):
Doug Zongkereef39442009-04-02 12:14:19 -0700143 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 Zongker8e931bf2009-04-06 15:21:45 -0700149 out_info = copy.copy(info)
Doug Zongkereef39442009-04-02 12:14:19 -0700150 if info.filename.endswith(".apk"):
151 name = os.path.basename(info.filename)
Doug Zongker43874f82009-04-14 14:05:15 -0700152 key = apk_key_map[name]
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800153 if key not in common.SPECIAL_CERT_STRINGS:
Doug Zongker43874f82009-04-14 14:05:15 -0700154 print " signing: %-*s (%s)" % (maxsize, name, key)
Doug Zongkereef39442009-04-02 12:14:19 -0700155 signed_data = SignApk(data, key, key_passwords[key])
Doug Zongker8e931bf2009-04-06 15:21:45 -0700156 output_tf_zip.writestr(out_info, signed_data)
Doug Zongkereef39442009-04-02 12:14:19 -0700157 else:
158 # an APK we're not supposed to sign.
Doug Zongker43874f82009-04-14 14:05:15 -0700159 print "NOT signing: %s" % (name,)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700160 output_tf_zip.writestr(out_info, data)
161 elif info.filename in ("SYSTEM/build.prop",
162 "RECOVERY/RAMDISK/default.prop"):
Doug Zongker17aa9442009-04-17 10:15:58 -0700163 print "rewriting %s:" % (info.filename,)
164 new_data = RewriteProps(data)
165 output_tf_zip.writestr(out_info, new_data)
Robert Craig817c5742013-04-19 10:59:22 -0400166 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 Zongkereef39442009-04-02 12:14:19 -0700170 else:
171 # a non-APK file; copy it verbatim
Doug Zongker8e931bf2009-04-06 15:21:45 -0700172 output_tf_zip.writestr(out_info, data)
173
174
Robert Craig817c5742013-04-19 10:59:22 -0400175def 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 Zongkerc09abc82010-01-11 13:09:15 -0800205def 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 Zongker17aa9442009-04-17 10:15:58 -0700217def 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 Zongkerc09abc82010-01-11 13:09:15 -0800225 pieces = value.split("/")
226 pieces[-1] = EditTags(pieces[-1])
227 value = "/".join(pieces)
Doug Zongker17aa9442009-04-17 10:15:58 -0700228 elif key == "ro.build.description":
Doug Zongkerc09abc82010-01-11 13:09:15 -0800229 pieces = value.split(" ")
Doug Zongker17aa9442009-04-17 10:15:58 -0700230 assert len(pieces) == 5
Doug Zongkerc09abc82010-01-11 13:09:15 -0800231 pieces[-1] = EditTags(pieces[-1])
232 value = " ".join(pieces)
233 elif key == "ro.build.tags":
234 value = EditTags(value)
Doug Zongkera8608a72013-07-23 11:51:04 -0700235 elif key == "ro.build.display.id":
236 # change, eg, "JWR66N dev-keys" to "JWR66N"
237 value = value.split()
Andrew Boie73d5abb2013-12-11 12:42:03 -0800238 if len(value) > 1 and value[-1].endswith("-keys"):
239 value.pop()
240 value = " ".join(value)
Doug Zongkerc09abc82010-01-11 13:09:15 -0800241 line = key + "=" + value
Doug Zongker17aa9442009-04-17 10:15:58 -0700242 if line != original_line:
243 print " replace: ", original_line
244 print " with: ", line
245 output.append(line)
246 return "\n".join(output) + "\n"
247
248
Doug Zongker831840e2011-09-22 10:28:04 -0700249def ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info):
Doug Zongker8e931bf2009-04-06 15:21:45 -0700250 try:
251 keylist = input_tf_zip.read("META/otakeys.txt").split()
252 except KeyError:
T.R. Fullharta28acc62013-03-18 10:31:26 -0700253 raise common.ExternalError("can't read META/otakeys.txt from input")
Doug Zongker8e931bf2009-04-06 15:21:45 -0700254
Doug Zongkere121d6a2011-02-01 14:13:52 -0800255 extra_recovery_keys = misc_info.get("extra_recovery_keys", None)
256 if extra_recovery_keys:
257 extra_recovery_keys = [OPTIONS.key_map.get(k, k) + ".x509.pem"
258 for k in extra_recovery_keys.split()]
259 if extra_recovery_keys:
260 print "extra recovery-only key(s): " + ", ".join(extra_recovery_keys)
261 else:
262 extra_recovery_keys = []
263
Doug Zongker8e931bf2009-04-06 15:21:45 -0700264 mapped_keys = []
265 for k in keylist:
266 m = re.match(r"^(.*)\.x509\.pem$", k)
267 if not m:
T.R. Fullharta28acc62013-03-18 10:31:26 -0700268 raise common.ExternalError("can't parse \"%s\" from META/otakeys.txt" % (k,))
Doug Zongker8e931bf2009-04-06 15:21:45 -0700269 k = m.group(1)
270 mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem")
271
Doug Zongkere05628c2009-08-20 17:38:42 -0700272 if mapped_keys:
273 print "using:\n ", "\n ".join(mapped_keys)
274 print "for OTA package verification"
275 else:
Doug Zongker831840e2011-09-22 10:28:04 -0700276 devkey = misc_info.get("default_system_dev_certificate",
277 "build/target/product/security/testkey")
Doug Zongkere05628c2009-08-20 17:38:42 -0700278 mapped_keys.append(
Doug Zongker831840e2011-09-22 10:28:04 -0700279 OPTIONS.key_map.get(devkey, devkey) + ".x509.pem")
Doug Zongkere05628c2009-08-20 17:38:42 -0700280 print "META/otakeys.txt has no keys; using", mapped_keys[0]
Doug Zongker8e931bf2009-04-06 15:21:45 -0700281
282 # recovery uses a version of the key that has been slightly
283 # predigested (by DumpPublicKey.java) and put in res/keys.
Doug Zongkere121d6a2011-02-01 14:13:52 -0800284 # extra_recovery_keys are used only in recovery.
Doug Zongker8e931bf2009-04-06 15:21:45 -0700285
Doug Zongker602a84e2009-06-18 08:35:12 -0700286 p = common.Run(["java", "-jar",
287 os.path.join(OPTIONS.search_path, "framework", "dumpkey.jar")]
Doug Zongkere121d6a2011-02-01 14:13:52 -0800288 + mapped_keys + extra_recovery_keys,
Doug Zongker8e931bf2009-04-06 15:21:45 -0700289 stdout=subprocess.PIPE)
290 data, _ = p.communicate()
291 if p.returncode != 0:
T.R. Fullharta28acc62013-03-18 10:31:26 -0700292 raise common.ExternalError("failed to run dumpkeys")
Doug Zongker048e7ca2009-06-15 14:31:53 -0700293 common.ZipWriteStr(output_tf_zip, "RECOVERY/RAMDISK/res/keys", data)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700294
295 # SystemUpdateActivity uses the x509.pem version of the keys, but
296 # put into a zipfile system/etc/security/otacerts.zip.
Doug Zongkere121d6a2011-02-01 14:13:52 -0800297 # We DO NOT include the extra_recovery_keys (if any) here.
Doug Zongker8e931bf2009-04-06 15:21:45 -0700298
299 tempfile = cStringIO.StringIO()
300 certs_zip = zipfile.ZipFile(tempfile, "w")
301 for k in mapped_keys:
302 certs_zip.write(k)
303 certs_zip.close()
Doug Zongker048e7ca2009-06-15 14:31:53 -0700304 common.ZipWriteStr(output_tf_zip, "SYSTEM/etc/security/otacerts.zip",
305 tempfile.getvalue())
Doug Zongkereef39442009-04-02 12:14:19 -0700306
307
Doug Zongker831840e2011-09-22 10:28:04 -0700308def BuildKeyMap(misc_info, key_mapping_options):
309 for s, d in key_mapping_options:
310 if s is None: # -d option
311 devkey = misc_info.get("default_system_dev_certificate",
312 "build/target/product/security/testkey")
313 devkeydir = os.path.dirname(devkey)
314
315 OPTIONS.key_map.update({
316 devkeydir + "/testkey": d + "/releasekey",
317 devkeydir + "/devkey": d + "/releasekey",
318 devkeydir + "/media": d + "/media",
319 devkeydir + "/shared": d + "/shared",
320 devkeydir + "/platform": d + "/platform",
321 })
322 else:
323 OPTIONS.key_map[s] = d
324
325
Doug Zongkereef39442009-04-02 12:14:19 -0700326def main(argv):
327
Doug Zongker831840e2011-09-22 10:28:04 -0700328 key_mapping_options = []
329
Doug Zongkereef39442009-04-02 12:14:19 -0700330 def option_handler(o, a):
Doug Zongker05d3dea2009-06-22 11:32:31 -0700331 if o in ("-e", "--extra_apks"):
Doug Zongkereef39442009-04-02 12:14:19 -0700332 names, key = a.split("=")
333 names = names.split(",")
334 for n in names:
335 OPTIONS.extra_apks[n] = key
336 elif o in ("-d", "--default_key_mappings"):
Doug Zongker831840e2011-09-22 10:28:04 -0700337 key_mapping_options.append((None, a))
Doug Zongkereef39442009-04-02 12:14:19 -0700338 elif o in ("-k", "--key_mapping"):
Doug Zongker831840e2011-09-22 10:28:04 -0700339 key_mapping_options.append(a.split("=", 1))
Doug Zongker8e931bf2009-04-06 15:21:45 -0700340 elif o in ("-o", "--replace_ota_keys"):
341 OPTIONS.replace_ota_keys = True
Doug Zongkerae877012009-04-21 10:04:51 -0700342 elif o in ("-t", "--tag_changes"):
343 new = []
344 for i in a.split(","):
345 i = i.strip()
346 if not i or i[0] not in "-+":
347 raise ValueError("Bad tag change '%s'" % (i,))
348 new.append(i[0] + i[1:].strip())
349 OPTIONS.tag_changes = tuple(new)
Doug Zongkereef39442009-04-02 12:14:19 -0700350 else:
351 return False
352 return True
353
354 args = common.ParseOptions(argv, __doc__,
Doug Zongker05d3dea2009-06-22 11:32:31 -0700355 extra_opts="e:d:k:ot:",
356 extra_long_opts=["extra_apks=",
Doug Zongkereef39442009-04-02 12:14:19 -0700357 "default_key_mappings=",
Doug Zongker8e931bf2009-04-06 15:21:45 -0700358 "key_mapping=",
Doug Zongker17aa9442009-04-17 10:15:58 -0700359 "replace_ota_keys",
Doug Zongkerae877012009-04-21 10:04:51 -0700360 "tag_changes="],
Doug Zongkereef39442009-04-02 12:14:19 -0700361 extra_option_handler=option_handler)
362
363 if len(args) != 2:
364 common.Usage(__doc__)
365 sys.exit(1)
366
367 input_zip = zipfile.ZipFile(args[0], "r")
368 output_zip = zipfile.ZipFile(args[1], "w")
369
Doug Zongker831840e2011-09-22 10:28:04 -0700370 misc_info = common.LoadInfoDict(input_zip)
371
372 BuildKeyMap(misc_info, key_mapping_options)
373
Doug Zongkereb338ef2009-05-20 16:50:49 -0700374 apk_key_map = GetApkCerts(input_zip)
375 CheckAllApksSigned(input_zip, apk_key_map)
Doug Zongkereb338ef2009-05-20 16:50:49 -0700376
377 key_passwords = common.GetKeyPasswords(set(apk_key_map.values()))
378 SignApks(input_zip, output_zip, apk_key_map, key_passwords)
Doug Zongkereef39442009-04-02 12:14:19 -0700379
Doug Zongker8e931bf2009-04-06 15:21:45 -0700380 if OPTIONS.replace_ota_keys:
Doug Zongkerb11e2d72011-10-05 11:23:06 -0700381 ReplaceOtaKeys(input_zip, output_zip, misc_info)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700382
Doug Zongkereef39442009-04-02 12:14:19 -0700383 input_zip.close()
384 output_zip.close()
385
386 print "done."
387
388
389if __name__ == '__main__':
390 try:
391 main(sys.argv[1:])
392 except common.ExternalError, e:
393 print
394 print " ERROR: %s" % (e,)
395 print
396 sys.exit(1)