blob: 2e0b44dacbc4274fd6c52f044ea9d7ce34da230a [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)
Tao Baoa80ed222016-06-16 14:41:24 -070054 Replace the certificate (public key) used by OTA package verification
55 with the ones specified in the input target_files zip (in the
56 META/otakeys.txt file). Key remapping (-k and -d) is performed on the
57 keys. For A/B devices, the payload verification key will be replaced
58 as well. If there're multiple OTA keys, only the first one will be used
59 for payload verification.
Doug Zongker17aa9442009-04-17 10:15:58 -070060
Doug Zongkerae877012009-04-21 10:04:51 -070061 -t (--tag_changes) <+tag>,<-tag>,...
62 Comma-separated list of changes to make to the set of tags (in
63 the last component of the build fingerprint). Prefix each with
64 '+' or '-' to indicate whether that tag should be added or
65 removed. Changes are processed in the order they appear.
Doug Zongker831840e2011-09-22 10:28:04 -070066 Default value is "-test-keys,-dev-keys,+release-keys".
Doug Zongkerae877012009-04-21 10:04:51 -070067
Tao Bao8adcfd12016-06-17 17:01:22 -070068 --replace_verity_private_key <key>
69 Replace the private key used for verity signing. It expects a filename
70 WITHOUT the extension (e.g. verity_key).
71
72 --replace_verity_public_key <key>
73 Replace the certificate (public key) used for verity verification. The
74 key file replaces the one at BOOT/RAMDISK/verity_key (or ROOT/verity_key
75 for devices using system_root_image). It expects the key filename WITH
76 the extension (e.g. verity_key.pub).
77
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -070078 --replace_verity_keyid <path_to_X509_PEM_cert_file>
79 Replace the veritykeyid in BOOT/cmdline of input_target_file_zip
Tao Bao8adcfd12016-06-17 17:01:22 -070080 with keyid of the cert pointed by <path_to_X509_PEM_cert_file>.
Doug Zongkereef39442009-04-02 12:14:19 -070081"""
82
83import sys
84
Doug Zongkercf6d5a92014-02-18 10:57:07 -080085if sys.hexversion < 0x02070000:
86 print >> sys.stderr, "Python 2.7 or newer is required."
Doug Zongkereef39442009-04-02 12:14:19 -070087 sys.exit(1)
88
Robert Craig817c5742013-04-19 10:59:22 -040089import base64
Doug Zongker8e931bf2009-04-06 15:21:45 -070090import cStringIO
91import copy
Robert Craig817c5742013-04-19 10:59:22 -040092import errno
Doug Zongkereef39442009-04-02 12:14:19 -070093import os
94import re
Doug Zongker412c02f2014-02-13 10:58:24 -080095import shutil
Tao Bao406050b2017-05-22 23:16:32 -070096import stat
Doug Zongkereef39442009-04-02 12:14:19 -070097import subprocess
98import tempfile
99import zipfile
100
Doug Zongker3c84f562014-07-31 11:06:30 -0700101import add_img_to_target_files
Doug Zongkereef39442009-04-02 12:14:19 -0700102import common
103
104OPTIONS = common.OPTIONS
105
106OPTIONS.extra_apks = {}
107OPTIONS.key_map = {}
Doug Zongker8e931bf2009-04-06 15:21:45 -0700108OPTIONS.replace_ota_keys = False
Geremy Condraf19b3652014-07-29 17:54:54 -0700109OPTIONS.replace_verity_public_key = False
110OPTIONS.replace_verity_private_key = False
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700111OPTIONS.replace_verity_keyid = False
Doug Zongker831840e2011-09-22 10:28:04 -0700112OPTIONS.tag_changes = ("-test-keys", "-dev-keys", "+release-keys")
Doug Zongkereef39442009-04-02 12:14:19 -0700113
114def GetApkCerts(tf_zip):
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800115 certmap = common.ReadApkCerts(tf_zip)
116
117 # apply the key remapping to the contents of the file
118 for apk, cert in certmap.iteritems():
119 certmap[apk] = OPTIONS.key_map.get(cert, cert)
120
121 # apply all the -e options, overriding anything in the file
Doug Zongkerad88c7c2009-04-14 12:34:27 -0700122 for apk, cert in OPTIONS.extra_apks.iteritems():
Doug Zongkerdecf9952009-12-15 17:27:49 -0800123 if not cert:
124 cert = "PRESIGNED"
Doug Zongkerad88c7c2009-04-14 12:34:27 -0700125 certmap[apk] = OPTIONS.key_map.get(cert, cert)
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800126
Doug Zongkereef39442009-04-02 12:14:19 -0700127 return certmap
128
129
Doug Zongkereb338ef2009-05-20 16:50:49 -0700130def CheckAllApksSigned(input_tf_zip, apk_key_map):
131 """Check that all the APKs we want to sign have keys specified, and
132 error out if they don't."""
133 unknown_apks = []
134 for info in input_tf_zip.infolist():
135 if info.filename.endswith(".apk"):
136 name = os.path.basename(info.filename)
137 if name not in apk_key_map:
138 unknown_apks.append(name)
139 if unknown_apks:
140 print "ERROR: no key specified for:\n\n ",
141 print "\n ".join(unknown_apks)
142 print "\nUse '-e <apkname>=' to specify a key (which may be an"
143 print "empty string to not sign this apk)."
144 sys.exit(1)
145
146
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800147def SignApk(data, keyname, pw, platform_api_level, codename_to_api_level_map):
Doug Zongkereef39442009-04-02 12:14:19 -0700148 unsigned = tempfile.NamedTemporaryFile()
149 unsigned.write(data)
150 unsigned.flush()
151
152 signed = tempfile.NamedTemporaryFile()
153
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800154 # For pre-N builds, don't upgrade to SHA-256 JAR signatures based on the APK's
155 # minSdkVersion to avoid increasing incremental OTA update sizes. If an APK
156 # didn't change, we don't want its signature to change due to the switch
157 # from SHA-1 to SHA-256.
158 # By default, APK signer chooses SHA-256 signatures if the APK's minSdkVersion
159 # is 18 or higher. For pre-N builds we disable this mechanism by pretending
160 # that the APK's minSdkVersion is 1.
161 # For N+ builds, we let APK signer rely on the APK's minSdkVersion to
162 # determine whether to use SHA-256.
163 min_api_level = None
164 if platform_api_level > 23:
165 # Let APK signer choose whether to use SHA-1 or SHA-256, based on the APK's
166 # minSdkVersion attribute
167 min_api_level = None
168 else:
169 # Force APK signer to use SHA-1
170 min_api_level = 1
171
172 common.SignFile(unsigned.name, signed.name, keyname, pw,
173 min_api_level=min_api_level,
174 codename_to_api_level_map=codename_to_api_level_map)
Doug Zongkereef39442009-04-02 12:14:19 -0700175
176 data = signed.read()
177 unsigned.close()
178 signed.close()
179
180 return data
181
182
Doug Zongker412c02f2014-02-13 10:58:24 -0800183def ProcessTargetFiles(input_tf_zip, output_tf_zip, misc_info,
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800184 apk_key_map, key_passwords, platform_api_level,
185 codename_to_api_level_map):
Michael Rungedc2661a2014-06-03 14:43:11 -0700186
Doug Zongkereef39442009-04-02 12:14:19 -0700187 maxsize = max([len(os.path.basename(i.filename))
188 for i in input_tf_zip.infolist()
189 if i.filename.endswith('.apk')])
Doug Zongker412c02f2014-02-13 10:58:24 -0800190 rebuild_recovery = False
Tao Baoa80ed222016-06-16 14:41:24 -0700191 system_root_image = misc_info.get("system_root_image") == "true"
Doug Zongker412c02f2014-02-13 10:58:24 -0800192
Tao Baoa80ed222016-06-16 14:41:24 -0700193 # tmpdir will only be used to regenerate the recovery-from-boot patch.
Doug Zongker412c02f2014-02-13 10:58:24 -0800194 tmpdir = tempfile.mkdtemp()
Tao Bao406050b2017-05-22 23:16:32 -0700195 # We're not setting the permissions precisely as in attr, because that work
196 # will be handled by mkbootfs (using the values from the canned or the
197 # compiled-in fs_config).
Doug Zongker412c02f2014-02-13 10:58:24 -0800198 def write_to_temp(fn, attr, data):
199 fn = os.path.join(tmpdir, fn)
200 if fn.endswith("/"):
201 fn = os.path.join(tmpdir, fn)
202 os.mkdir(fn)
203 else:
204 d = os.path.dirname(fn)
205 if d and not os.path.exists(d):
206 os.makedirs(d)
207
Tao Bao406050b2017-05-22 23:16:32 -0700208 if stat.S_ISLNK(attr >> 16):
Doug Zongker412c02f2014-02-13 10:58:24 -0800209 os.symlink(data, fn)
210 else:
211 with open(fn, "wb") as f:
212 f.write(data)
Doug Zongkereef39442009-04-02 12:14:19 -0700213
214 for info in input_tf_zip.infolist():
Dan Albert8b72aef2015-03-23 19:13:21 -0700215 if info.filename.startswith("IMAGES/"):
216 continue
Doug Zongker3c84f562014-07-31 11:06:30 -0700217
Doug Zongkereef39442009-04-02 12:14:19 -0700218 data = input_tf_zip.read(info.filename)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700219 out_info = copy.copy(info)
Doug Zongker412c02f2014-02-13 10:58:24 -0800220
Tao Baof2cffbd2015-07-22 12:33:18 -0700221 # Sign APKs.
Doug Zongkereef39442009-04-02 12:14:19 -0700222 if info.filename.endswith(".apk"):
223 name = os.path.basename(info.filename)
Doug Zongker43874f82009-04-14 14:05:15 -0700224 key = apk_key_map[name]
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800225 if key not in common.SPECIAL_CERT_STRINGS:
Doug Zongker43874f82009-04-14 14:05:15 -0700226 print " signing: %-*s (%s)" % (maxsize, name, key)
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800227 signed_data = SignApk(data, key, key_passwords[key], platform_api_level,
228 codename_to_api_level_map)
Tao Bao2ed665a2015-04-01 11:21:55 -0700229 common.ZipWriteStr(output_tf_zip, out_info, signed_data)
Doug Zongkereef39442009-04-02 12:14:19 -0700230 else:
231 # an APK we're not supposed to sign.
Doug Zongker43874f82009-04-14 14:05:15 -0700232 print "NOT signing: %s" % (name,)
Tao Bao2ed665a2015-04-01 11:21:55 -0700233 common.ZipWriteStr(output_tf_zip, out_info, data)
Tao Baoa80ed222016-06-16 14:41:24 -0700234
235 # System properties.
Doug Zongker8e931bf2009-04-06 15:21:45 -0700236 elif info.filename in ("SYSTEM/build.prop",
Jesse Zhao2625d272015-02-06 09:49:55 -0800237 "VENDOR/build.prop",
Tao Baocb7ff772015-09-11 15:27:56 -0700238 "BOOT/RAMDISK/default.prop",
Tao Bao28e2fa12016-08-11 11:00:58 -0700239 "ROOT/default.prop",
Doug Zongker8e931bf2009-04-06 15:21:45 -0700240 "RECOVERY/RAMDISK/default.prop"):
Doug Zongker17aa9442009-04-17 10:15:58 -0700241 print "rewriting %s:" % (info.filename,)
Michael Rungedc2661a2014-06-03 14:43:11 -0700242 new_data = RewriteProps(data, misc_info)
Tao Bao2ed665a2015-04-01 11:21:55 -0700243 common.ZipWriteStr(output_tf_zip, out_info, new_data)
Tao Baocb7ff772015-09-11 15:27:56 -0700244 if info.filename in ("BOOT/RAMDISK/default.prop",
Tao Bao28e2fa12016-08-11 11:00:58 -0700245 "ROOT/default.prop",
Tao Baocb7ff772015-09-11 15:27:56 -0700246 "RECOVERY/RAMDISK/default.prop"):
Doug Zongker412c02f2014-02-13 10:58:24 -0800247 write_to_temp(info.filename, info.external_attr, new_data)
Tao Baoa80ed222016-06-16 14:41:24 -0700248
Robert Craig817c5742013-04-19 10:59:22 -0400249 elif info.filename.endswith("mac_permissions.xml"):
250 print "rewriting %s with new keys." % (info.filename,)
251 new_data = ReplaceCerts(data)
Tao Bao2ed665a2015-04-01 11:21:55 -0700252 common.ZipWriteStr(output_tf_zip, out_info, new_data)
Tao Baoa80ed222016-06-16 14:41:24 -0700253
254 # Trigger a rebuild of the recovery patch if needed.
Doug Zongker412c02f2014-02-13 10:58:24 -0800255 elif info.filename in ("SYSTEM/recovery-from-boot.p",
Tao Baof2cffbd2015-07-22 12:33:18 -0700256 "SYSTEM/etc/recovery.img",
Doug Zongker412c02f2014-02-13 10:58:24 -0800257 "SYSTEM/bin/install-recovery.sh"):
258 rebuild_recovery = True
Tao Baoa80ed222016-06-16 14:41:24 -0700259
260 # Don't copy OTA keys if we're replacing them.
Doug Zongker412c02f2014-02-13 10:58:24 -0800261 elif (OPTIONS.replace_ota_keys and
Tao Baoa80ed222016-06-16 14:41:24 -0700262 info.filename in (
263 "BOOT/RAMDISK/res/keys",
Alex Deymob3e8ce62016-08-04 16:06:12 -0700264 "BOOT/RAMDISK/etc/update_engine/update-payload-key.pub.pem",
Tao Baoa80ed222016-06-16 14:41:24 -0700265 "RECOVERY/RAMDISK/res/keys",
266 "SYSTEM/etc/security/otacerts.zip",
267 "SYSTEM/etc/update_engine/update-payload-key.pub.pem")):
Doug Zongker412c02f2014-02-13 10:58:24 -0800268 pass
Tao Baoa80ed222016-06-16 14:41:24 -0700269
Tao Bao8adcfd12016-06-17 17:01:22 -0700270 # Skip META/misc_info.txt if we will replace the verity private key later.
Michael Runge947894f2014-10-14 20:58:38 -0700271 elif (OPTIONS.replace_verity_private_key and
Geremy Condraf19b3652014-07-29 17:54:54 -0700272 info.filename == "META/misc_info.txt"):
273 pass
Tao Bao8adcfd12016-06-17 17:01:22 -0700274
275 # Skip verity public key if we will replace it.
Michael Runge947894f2014-10-14 20:58:38 -0700276 elif (OPTIONS.replace_verity_public_key and
Tao Bao7a5bf8a2015-07-21 18:01:20 -0700277 info.filename in ("BOOT/RAMDISK/verity_key",
Tao Bao8adcfd12016-06-17 17:01:22 -0700278 "ROOT/verity_key")):
Geremy Condraf19b3652014-07-29 17:54:54 -0700279 pass
Tao Baoa80ed222016-06-16 14:41:24 -0700280
Tao Bao8adcfd12016-06-17 17:01:22 -0700281 # Skip verity keyid (for system_root_image use) if we will replace it.
282 elif (OPTIONS.replace_verity_keyid and
283 info.filename == "BOOT/cmdline"):
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700284 pass
285
Tianjie Xu4f099002016-08-11 18:04:27 -0700286 # Skip the care_map as we will regenerate the system/vendor images.
Tao Bao9aa4b9b2016-09-29 17:53:56 -0700287 elif info.filename == "META/care_map.txt":
Tianjie Xu4f099002016-08-11 18:04:27 -0700288 pass
289
Tao Baoa80ed222016-06-16 14:41:24 -0700290 # Copy BOOT/, RECOVERY/, META/, ROOT/ to rebuild recovery patch. This case
291 # must come AFTER other matching rules.
292 elif (info.filename.startswith("BOOT/") or
293 info.filename.startswith("RECOVERY/") or
294 info.filename.startswith("META/") or
295 info.filename.startswith("ROOT/") or
296 info.filename == "SYSTEM/etc/recovery-resource.dat"):
297 write_to_temp(info.filename, info.external_attr, data)
298 common.ZipWriteStr(output_tf_zip, out_info, data)
299
300 # A non-APK file; copy it verbatim.
Doug Zongkereef39442009-04-02 12:14:19 -0700301 else:
Tao Bao2ed665a2015-04-01 11:21:55 -0700302 common.ZipWriteStr(output_tf_zip, out_info, data)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700303
Doug Zongker412c02f2014-02-13 10:58:24 -0800304 if OPTIONS.replace_ota_keys:
305 new_recovery_keys = ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info)
306 if new_recovery_keys:
Tao Baoa80ed222016-06-16 14:41:24 -0700307 if system_root_image:
308 recovery_keys_location = "BOOT/RAMDISK/res/keys"
309 else:
310 recovery_keys_location = "RECOVERY/RAMDISK/res/keys"
311 # The "new_recovery_keys" has been already written into the output_tf_zip
312 # while calling ReplaceOtaKeys(). We're just putting the same copy to
313 # tmpdir in case we need to regenerate the recovery-from-boot patch.
314 write_to_temp(recovery_keys_location, 0o755 << 16, new_recovery_keys)
Doug Zongker412c02f2014-02-13 10:58:24 -0800315
Tao Bao8adcfd12016-06-17 17:01:22 -0700316 # Replace the keyid string in META/misc_info.txt.
317 if OPTIONS.replace_verity_private_key:
318 ReplaceVerityPrivateKey(input_tf_zip, output_tf_zip, misc_info,
319 OPTIONS.replace_verity_private_key[1])
320
321 if OPTIONS.replace_verity_public_key:
322 if system_root_image:
323 dest = "ROOT/verity_key"
324 else:
325 dest = "BOOT/RAMDISK/verity_key"
326 # We are replacing the one in boot image only, since the one under
327 # recovery won't ever be needed.
328 new_data = ReplaceVerityPublicKey(
329 output_tf_zip, dest, OPTIONS.replace_verity_public_key[1])
330 write_to_temp(dest, 0o755 << 16, new_data)
331
332 # Replace the keyid string in BOOT/cmdline.
333 if OPTIONS.replace_verity_keyid:
334 new_cmdline = ReplaceVerityKeyId(input_tf_zip, output_tf_zip,
335 OPTIONS.replace_verity_keyid[1])
336 # Writing the new cmdline to tmpdir is redundant as the bootimage
337 # gets build in the add_image_to_target_files and rebuild_recovery
338 # is not exercised while building the boot image for the A/B
339 # path
340 write_to_temp("BOOT/cmdline", 0o755 << 16, new_cmdline)
341
Doug Zongker412c02f2014-02-13 10:58:24 -0800342 if rebuild_recovery:
343 recovery_img = common.GetBootableImage(
344 "recovery.img", "recovery.img", tmpdir, "RECOVERY", info_dict=misc_info)
345 boot_img = common.GetBootableImage(
346 "boot.img", "boot.img", tmpdir, "BOOT", info_dict=misc_info)
347
348 def output_sink(fn, data):
Tao Bao2ed665a2015-04-01 11:21:55 -0700349 common.ZipWriteStr(output_tf_zip, "SYSTEM/" + fn, data)
Doug Zongker412c02f2014-02-13 10:58:24 -0800350
351 common.MakeRecoveryPatch(tmpdir, output_sink, recovery_img, boot_img,
352 info_dict=misc_info)
353
354 shutil.rmtree(tmpdir)
355
Doug Zongker8e931bf2009-04-06 15:21:45 -0700356
Robert Craig817c5742013-04-19 10:59:22 -0400357def ReplaceCerts(data):
358 """Given a string of data, replace all occurences of a set
359 of X509 certs with a newer set of X509 certs and return
360 the updated data string."""
361 for old, new in OPTIONS.key_map.iteritems():
362 try:
363 if OPTIONS.verbose:
364 print " Replacing %s.x509.pem with %s.x509.pem" % (old, new)
365 f = open(old + ".x509.pem")
366 old_cert16 = base64.b16encode(common.ParseCertificate(f.read())).lower()
367 f.close()
368 f = open(new + ".x509.pem")
369 new_cert16 = base64.b16encode(common.ParseCertificate(f.read())).lower()
370 f.close()
371 # Only match entire certs.
372 pattern = "\\b"+old_cert16+"\\b"
373 (data, num) = re.subn(pattern, new_cert16, data, flags=re.IGNORECASE)
374 if OPTIONS.verbose:
375 print " Replaced %d occurence(s) of %s.x509.pem with " \
376 "%s.x509.pem" % (num, old, new)
Dan Albert8b72aef2015-03-23 19:13:21 -0700377 except IOError as e:
378 if e.errno == errno.ENOENT and not OPTIONS.verbose:
Robert Craig817c5742013-04-19 10:59:22 -0400379 continue
380
381 print " Error accessing %s. %s. Skip replacing %s.x509.pem " \
382 "with %s.x509.pem." % (e.filename, e.strerror, old, new)
383
384 return data
385
386
Doug Zongkerc09abc82010-01-11 13:09:15 -0800387def EditTags(tags):
388 """Given a string containing comma-separated tags, apply the edits
389 specified in OPTIONS.tag_changes and return the updated string."""
390 tags = set(tags.split(","))
391 for ch in OPTIONS.tag_changes:
392 if ch[0] == "-":
393 tags.discard(ch[1:])
394 elif ch[0] == "+":
395 tags.add(ch[1:])
396 return ",".join(sorted(tags))
397
398
Michael Rungedc2661a2014-06-03 14:43:11 -0700399def RewriteProps(data, misc_info):
Doug Zongker17aa9442009-04-17 10:15:58 -0700400 output = []
401 for line in data.split("\n"):
402 line = line.strip()
403 original_line = line
Michael Rungedc2661a2014-06-03 14:43:11 -0700404 if line and line[0] != '#' and "=" in line:
Doug Zongker17aa9442009-04-17 10:15:58 -0700405 key, value = line.split("=", 1)
Michael Rungee07c75a2014-12-09 13:54:23 -0800406 if (key in ("ro.build.fingerprint", "ro.vendor.build.fingerprint")
Michael Rungedc2661a2014-06-03 14:43:11 -0700407 and misc_info.get("oem_fingerprint_properties") is None):
408 pieces = value.split("/")
409 pieces[-1] = EditTags(pieces[-1])
410 value = "/".join(pieces)
Michael Rungee07c75a2014-12-09 13:54:23 -0800411 elif (key in ("ro.build.thumbprint", "ro.vendor.build.thumbprint")
Dan Albert8b72aef2015-03-23 19:13:21 -0700412 and misc_info.get("oem_fingerprint_properties") is not None):
Doug Zongkerc09abc82010-01-11 13:09:15 -0800413 pieces = value.split("/")
414 pieces[-1] = EditTags(pieces[-1])
415 value = "/".join(pieces)
Tao Baocb7ff772015-09-11 15:27:56 -0700416 elif key == "ro.bootimage.build.fingerprint":
417 pieces = value.split("/")
418 pieces[-1] = EditTags(pieces[-1])
419 value = "/".join(pieces)
Doug Zongker17aa9442009-04-17 10:15:58 -0700420 elif key == "ro.build.description":
Doug Zongkerc09abc82010-01-11 13:09:15 -0800421 pieces = value.split(" ")
Doug Zongker17aa9442009-04-17 10:15:58 -0700422 assert len(pieces) == 5
Doug Zongkerc09abc82010-01-11 13:09:15 -0800423 pieces[-1] = EditTags(pieces[-1])
424 value = " ".join(pieces)
425 elif key == "ro.build.tags":
426 value = EditTags(value)
Doug Zongkera8608a72013-07-23 11:51:04 -0700427 elif key == "ro.build.display.id":
428 # change, eg, "JWR66N dev-keys" to "JWR66N"
429 value = value.split()
Michael Rungedc2661a2014-06-03 14:43:11 -0700430 if len(value) > 1 and value[-1].endswith("-keys"):
Andrew Boie73d5abb2013-12-11 12:42:03 -0800431 value.pop()
432 value = " ".join(value)
Doug Zongkerc09abc82010-01-11 13:09:15 -0800433 line = key + "=" + value
Doug Zongker17aa9442009-04-17 10:15:58 -0700434 if line != original_line:
435 print " replace: ", original_line
436 print " with: ", line
437 output.append(line)
438 return "\n".join(output) + "\n"
439
440
Doug Zongker831840e2011-09-22 10:28:04 -0700441def ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info):
Doug Zongker8e931bf2009-04-06 15:21:45 -0700442 try:
443 keylist = input_tf_zip.read("META/otakeys.txt").split()
444 except KeyError:
T.R. Fullharta28acc62013-03-18 10:31:26 -0700445 raise common.ExternalError("can't read META/otakeys.txt from input")
Doug Zongker8e931bf2009-04-06 15:21:45 -0700446
Doug Zongkere121d6a2011-02-01 14:13:52 -0800447 extra_recovery_keys = misc_info.get("extra_recovery_keys", None)
448 if extra_recovery_keys:
449 extra_recovery_keys = [OPTIONS.key_map.get(k, k) + ".x509.pem"
450 for k in extra_recovery_keys.split()]
451 if extra_recovery_keys:
452 print "extra recovery-only key(s): " + ", ".join(extra_recovery_keys)
453 else:
454 extra_recovery_keys = []
455
Doug Zongker8e931bf2009-04-06 15:21:45 -0700456 mapped_keys = []
457 for k in keylist:
458 m = re.match(r"^(.*)\.x509\.pem$", k)
459 if not m:
Doug Zongker412c02f2014-02-13 10:58:24 -0800460 raise common.ExternalError(
461 "can't parse \"%s\" from META/otakeys.txt" % (k,))
Doug Zongker8e931bf2009-04-06 15:21:45 -0700462 k = m.group(1)
463 mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem")
464
Doug Zongkere05628c2009-08-20 17:38:42 -0700465 if mapped_keys:
466 print "using:\n ", "\n ".join(mapped_keys)
467 print "for OTA package verification"
468 else:
Doug Zongker831840e2011-09-22 10:28:04 -0700469 devkey = misc_info.get("default_system_dev_certificate",
470 "build/target/product/security/testkey")
Doug Zongkere05628c2009-08-20 17:38:42 -0700471 mapped_keys.append(
Doug Zongker831840e2011-09-22 10:28:04 -0700472 OPTIONS.key_map.get(devkey, devkey) + ".x509.pem")
Tao Baoa80ed222016-06-16 14:41:24 -0700473 print("META/otakeys.txt has no keys; using %s for OTA package"
474 " verification." % (mapped_keys[0],))
Doug Zongker8e931bf2009-04-06 15:21:45 -0700475
476 # recovery uses a version of the key that has been slightly
477 # predigested (by DumpPublicKey.java) and put in res/keys.
Doug Zongkere121d6a2011-02-01 14:13:52 -0800478 # extra_recovery_keys are used only in recovery.
Tao Baoe95540e2016-11-08 12:08:53 -0800479 cmd = ([OPTIONS.java_path] + OPTIONS.java_args +
480 ["-jar",
481 os.path.join(OPTIONS.search_path, "framework", "dumpkey.jar")] +
482 mapped_keys + extra_recovery_keys)
483 p = common.Run(cmd, stdout=subprocess.PIPE)
Doug Zongker412c02f2014-02-13 10:58:24 -0800484 new_recovery_keys, _ = p.communicate()
Doug Zongker8e931bf2009-04-06 15:21:45 -0700485 if p.returncode != 0:
T.R. Fullharta28acc62013-03-18 10:31:26 -0700486 raise common.ExternalError("failed to run dumpkeys")
Tao Baoa80ed222016-06-16 14:41:24 -0700487
488 # system_root_image puts the recovery keys at BOOT/RAMDISK.
489 if misc_info.get("system_root_image") == "true":
490 recovery_keys_location = "BOOT/RAMDISK/res/keys"
491 else:
492 recovery_keys_location = "RECOVERY/RAMDISK/res/keys"
493 common.ZipWriteStr(output_tf_zip, recovery_keys_location, new_recovery_keys)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700494
495 # SystemUpdateActivity uses the x509.pem version of the keys, but
496 # put into a zipfile system/etc/security/otacerts.zip.
Doug Zongkere121d6a2011-02-01 14:13:52 -0800497 # We DO NOT include the extra_recovery_keys (if any) here.
Doug Zongker8e931bf2009-04-06 15:21:45 -0700498
Dan Albert8b72aef2015-03-23 19:13:21 -0700499 temp_file = cStringIO.StringIO()
500 certs_zip = zipfile.ZipFile(temp_file, "w")
Doug Zongker8e931bf2009-04-06 15:21:45 -0700501 for k in mapped_keys:
Tao Bao83cd79d2016-04-11 23:05:52 -0700502 common.ZipWrite(certs_zip, k)
503 common.ZipClose(certs_zip)
Doug Zongker048e7ca2009-06-15 14:31:53 -0700504 common.ZipWriteStr(output_tf_zip, "SYSTEM/etc/security/otacerts.zip",
Dan Albert8b72aef2015-03-23 19:13:21 -0700505 temp_file.getvalue())
Doug Zongkereef39442009-04-02 12:14:19 -0700506
Tao Baoa80ed222016-06-16 14:41:24 -0700507 # For A/B devices, update the payload verification key.
508 if misc_info.get("ab_update") == "true":
509 # Unlike otacerts.zip that may contain multiple keys, we can only specify
510 # ONE payload verification key.
511 if len(mapped_keys) > 1:
512 print("\n WARNING: Found more than one OTA keys; Using the first one"
513 " as payload verification key.\n\n")
514
515 print "Using %s for payload verification." % (mapped_keys[0],)
Tao Bao13b69622016-07-06 15:28:59 -0700516 cmd = common.Run(
517 ["openssl", "x509", "-pubkey", "-noout", "-in", mapped_keys[0]],
518 stdout=subprocess.PIPE)
519 pubkey, _ = cmd.communicate()
520 common.ZipWriteStr(
Tao Baoa80ed222016-06-16 14:41:24 -0700521 output_tf_zip,
Tao Bao13b69622016-07-06 15:28:59 -0700522 "SYSTEM/etc/update_engine/update-payload-key.pub.pem",
523 pubkey)
Alex Deymob3e8ce62016-08-04 16:06:12 -0700524 common.ZipWriteStr(
525 output_tf_zip,
526 "BOOT/RAMDISK/etc/update_engine/update-payload-key.pub.pem",
527 pubkey)
Tao Baoa80ed222016-06-16 14:41:24 -0700528
Doug Zongker412c02f2014-02-13 10:58:24 -0800529 return new_recovery_keys
530
Tao Bao8adcfd12016-06-17 17:01:22 -0700531
Tao Bao7a5bf8a2015-07-21 18:01:20 -0700532def ReplaceVerityPublicKey(targetfile_zip, filename, key_path):
Geremy Condraf19b3652014-07-29 17:54:54 -0700533 print "Replacing verity public key with %s" % key_path
534 with open(key_path) as f:
Andrew Boied083f0b2014-09-15 16:01:07 -0700535 data = f.read()
Tao Bao7a5bf8a2015-07-21 18:01:20 -0700536 common.ZipWriteStr(targetfile_zip, filename, data)
Andrew Boied083f0b2014-09-15 16:01:07 -0700537 return data
Geremy Condraf19b3652014-07-29 17:54:54 -0700538
Tao Bao8adcfd12016-06-17 17:01:22 -0700539
Dan Albert8b72aef2015-03-23 19:13:21 -0700540def ReplaceVerityPrivateKey(targetfile_input_zip, targetfile_output_zip,
541 misc_info, key_path):
Geremy Condraf19b3652014-07-29 17:54:54 -0700542 print "Replacing verity private key with %s" % key_path
543 current_key = misc_info["verity_key"]
544 original_misc_info = targetfile_input_zip.read("META/misc_info.txt")
545 new_misc_info = original_misc_info.replace(current_key, key_path)
546 common.ZipWriteStr(targetfile_output_zip, "META/misc_info.txt", new_misc_info)
Andrew Boied083f0b2014-09-15 16:01:07 -0700547 misc_info["verity_key"] = key_path
Doug Zongkereef39442009-04-02 12:14:19 -0700548
Tao Bao8adcfd12016-06-17 17:01:22 -0700549
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700550def ReplaceVerityKeyId(targetfile_input_zip, targetfile_output_zip, keypath):
551 in_cmdline = targetfile_input_zip.read("BOOT/cmdline")
552 # copy in_cmdline to output_zip if veritykeyid is not present in in_cmdline
553 if "veritykeyid" not in in_cmdline:
554 common.ZipWriteStr(targetfile_output_zip, "BOOT/cmdline", in_cmdline)
555 return in_cmdline
556 out_cmdline = []
557 for param in in_cmdline.split():
558 if "veritykeyid" in param:
559 # extract keyid using openssl command
Tao Bao9aa4b9b2016-09-29 17:53:56 -0700560 p = common.Run(
561 ["openssl", "x509", "-in", keypath, "-text"],
562 stdout=subprocess.PIPE)
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700563 keyid, stderr = p.communicate()
Tao Bao9aa4b9b2016-09-29 17:53:56 -0700564 keyid = re.search(
565 r'keyid:([0-9a-fA-F:]*)', keyid).group(1).replace(':', '').lower()
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700566 print "Replacing verity keyid with %s error=%s" % (keyid, stderr)
567 out_cmdline.append("veritykeyid=id:%s" % (keyid,))
568 else:
569 out_cmdline.append(param)
570
571 out_cmdline = ' '.join(out_cmdline)
572 out_cmdline = out_cmdline.strip()
573 print "out_cmdline %s" % (out_cmdline)
574 common.ZipWriteStr(targetfile_output_zip, "BOOT/cmdline", out_cmdline)
575 return out_cmdline
576
Tao Bao8adcfd12016-06-17 17:01:22 -0700577
Doug Zongker831840e2011-09-22 10:28:04 -0700578def BuildKeyMap(misc_info, key_mapping_options):
579 for s, d in key_mapping_options:
580 if s is None: # -d option
581 devkey = misc_info.get("default_system_dev_certificate",
582 "build/target/product/security/testkey")
583 devkeydir = os.path.dirname(devkey)
584
585 OPTIONS.key_map.update({
586 devkeydir + "/testkey": d + "/releasekey",
587 devkeydir + "/devkey": d + "/releasekey",
588 devkeydir + "/media": d + "/media",
589 devkeydir + "/shared": d + "/shared",
590 devkeydir + "/platform": d + "/platform",
591 })
592 else:
593 OPTIONS.key_map[s] = d
594
595
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800596def GetApiLevelAndCodename(input_tf_zip):
597 data = input_tf_zip.read("SYSTEM/build.prop")
598 api_level = None
599 codename = None
600 for line in data.split("\n"):
601 line = line.strip()
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800602 if line and line[0] != '#' and "=" in line:
603 key, value = line.split("=", 1)
604 key = key.strip()
605 if key == "ro.build.version.sdk":
606 api_level = int(value.strip())
607 elif key == "ro.build.version.codename":
608 codename = value.strip()
609
610 if api_level is None:
611 raise ValueError("No ro.build.version.sdk in SYSTEM/build.prop")
612 if codename is None:
613 raise ValueError("No ro.build.version.codename in SYSTEM/build.prop")
614
615 return (api_level, codename)
616
617
618def GetCodenameToApiLevelMap(input_tf_zip):
619 data = input_tf_zip.read("SYSTEM/build.prop")
620 api_level = None
621 codenames = None
622 for line in data.split("\n"):
623 line = line.strip()
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800624 if line and line[0] != '#' and "=" in line:
625 key, value = line.split("=", 1)
626 key = key.strip()
627 if key == "ro.build.version.sdk":
628 api_level = int(value.strip())
629 elif key == "ro.build.version.all_codenames":
630 codenames = value.strip().split(",")
631
632 if api_level is None:
633 raise ValueError("No ro.build.version.sdk in SYSTEM/build.prop")
634 if codenames is None:
635 raise ValueError("No ro.build.version.all_codenames in SYSTEM/build.prop")
636
637 result = dict()
638 for codename in codenames:
639 codename = codename.strip()
640 if len(codename) > 0:
641 result[codename] = api_level
642 return result
643
644
Doug Zongkereef39442009-04-02 12:14:19 -0700645def main(argv):
646
Doug Zongker831840e2011-09-22 10:28:04 -0700647 key_mapping_options = []
648
Doug Zongkereef39442009-04-02 12:14:19 -0700649 def option_handler(o, a):
Doug Zongker05d3dea2009-06-22 11:32:31 -0700650 if o in ("-e", "--extra_apks"):
Doug Zongkereef39442009-04-02 12:14:19 -0700651 names, key = a.split("=")
652 names = names.split(",")
653 for n in names:
654 OPTIONS.extra_apks[n] = key
655 elif o in ("-d", "--default_key_mappings"):
Doug Zongker831840e2011-09-22 10:28:04 -0700656 key_mapping_options.append((None, a))
Doug Zongkereef39442009-04-02 12:14:19 -0700657 elif o in ("-k", "--key_mapping"):
Doug Zongker831840e2011-09-22 10:28:04 -0700658 key_mapping_options.append(a.split("=", 1))
Doug Zongker8e931bf2009-04-06 15:21:45 -0700659 elif o in ("-o", "--replace_ota_keys"):
660 OPTIONS.replace_ota_keys = True
Doug Zongkerae877012009-04-21 10:04:51 -0700661 elif o in ("-t", "--tag_changes"):
662 new = []
663 for i in a.split(","):
664 i = i.strip()
665 if not i or i[0] not in "-+":
666 raise ValueError("Bad tag change '%s'" % (i,))
667 new.append(i[0] + i[1:].strip())
668 OPTIONS.tag_changes = tuple(new)
Geremy Condraf19b3652014-07-29 17:54:54 -0700669 elif o == "--replace_verity_public_key":
670 OPTIONS.replace_verity_public_key = (True, a)
671 elif o == "--replace_verity_private_key":
672 OPTIONS.replace_verity_private_key = (True, a)
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700673 elif o == "--replace_verity_keyid":
674 OPTIONS.replace_verity_keyid = (True, a)
Doug Zongkereef39442009-04-02 12:14:19 -0700675 else:
676 return False
677 return True
678
679 args = common.ParseOptions(argv, __doc__,
Doug Zongker05d3dea2009-06-22 11:32:31 -0700680 extra_opts="e:d:k:ot:",
681 extra_long_opts=["extra_apks=",
Doug Zongkereef39442009-04-02 12:14:19 -0700682 "default_key_mappings=",
Doug Zongker8e931bf2009-04-06 15:21:45 -0700683 "key_mapping=",
Doug Zongker17aa9442009-04-17 10:15:58 -0700684 "replace_ota_keys",
Geremy Condraf19b3652014-07-29 17:54:54 -0700685 "tag_changes=",
686 "replace_verity_public_key=",
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700687 "replace_verity_private_key=",
688 "replace_verity_keyid="],
Doug Zongkereef39442009-04-02 12:14:19 -0700689 extra_option_handler=option_handler)
690
691 if len(args) != 2:
692 common.Usage(__doc__)
693 sys.exit(1)
694
695 input_zip = zipfile.ZipFile(args[0], "r")
696 output_zip = zipfile.ZipFile(args[1], "w")
697
Doug Zongker831840e2011-09-22 10:28:04 -0700698 misc_info = common.LoadInfoDict(input_zip)
699
700 BuildKeyMap(misc_info, key_mapping_options)
701
Doug Zongkereb338ef2009-05-20 16:50:49 -0700702 apk_key_map = GetApkCerts(input_zip)
703 CheckAllApksSigned(input_zip, apk_key_map)
Doug Zongkereb338ef2009-05-20 16:50:49 -0700704
705 key_passwords = common.GetKeyPasswords(set(apk_key_map.values()))
Tao Bao9aa4b9b2016-09-29 17:53:56 -0700706 platform_api_level, _ = GetApiLevelAndCodename(input_zip)
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800707 codename_to_api_level_map = GetCodenameToApiLevelMap(input_zip)
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800708
Doug Zongker412c02f2014-02-13 10:58:24 -0800709 ProcessTargetFiles(input_zip, output_zip, misc_info,
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800710 apk_key_map, key_passwords,
711 platform_api_level,
712 codename_to_api_level_map)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700713
Tao Bao2ed665a2015-04-01 11:21:55 -0700714 common.ZipClose(input_zip)
715 common.ZipClose(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -0700716
Tianjie Xub48589a2016-08-03 19:21:52 -0700717 # Skip building userdata.img and cache.img when signing the target files.
718 new_args = ["--is_signing", args[1]]
719 add_img_to_target_files.main(new_args)
Doug Zongker3c84f562014-07-31 11:06:30 -0700720
Doug Zongkereef39442009-04-02 12:14:19 -0700721 print "done."
722
723
724if __name__ == '__main__':
725 try:
726 main(sys.argv[1:])
727 except common.ExternalError, e:
728 print
729 print " ERROR: %s" % (e,)
730 print
731 sys.exit(1)