blob: e671cd5594a0eabb4c2be91e8583c20ca2a62218 [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>.
Tao Baoc218a472017-06-19 15:48:02 -070081
82 --avb_{boot,system,vendor,dtbo,vbmeta}_algorithm <algorithm>
83 --avb_{boot,system,vendor,dtbo,vbmeta}_key <key>
84 Use the specified algorithm (e.g. SHA256_RSA4096) and the key to AVB-sign
85 the specified image. Otherwise it uses the existing values in info dict.
86
87 --avb_{boot,system,vendor,dtbo,vbmeta}_extra_args <args>
88 Specify any additional args that are needed to AVB-sign the image
89 (e.g. "--signing_helper /path/to/helper"). The args will be appended to
90 the existing ones in info dict.
Doug Zongkereef39442009-04-02 12:14:19 -070091"""
92
93import sys
94
Doug Zongkercf6d5a92014-02-18 10:57:07 -080095if sys.hexversion < 0x02070000:
96 print >> sys.stderr, "Python 2.7 or newer is required."
Doug Zongkereef39442009-04-02 12:14:19 -070097 sys.exit(1)
98
Robert Craig817c5742013-04-19 10:59:22 -040099import base64
Doug Zongker8e931bf2009-04-06 15:21:45 -0700100import cStringIO
101import copy
Robert Craig817c5742013-04-19 10:59:22 -0400102import errno
Doug Zongkereef39442009-04-02 12:14:19 -0700103import os
104import re
Tao Bao9fdd00f2017-07-12 11:57:05 -0700105import stat
Doug Zongkereef39442009-04-02 12:14:19 -0700106import subprocess
107import tempfile
108import zipfile
109
Doug Zongker3c84f562014-07-31 11:06:30 -0700110import add_img_to_target_files
Doug Zongkereef39442009-04-02 12:14:19 -0700111import common
112
113OPTIONS = common.OPTIONS
114
115OPTIONS.extra_apks = {}
116OPTIONS.key_map = {}
Tianjie Xu616fbeb2017-05-23 14:51:02 -0700117OPTIONS.rebuild_recovery = False
Doug Zongker8e931bf2009-04-06 15:21:45 -0700118OPTIONS.replace_ota_keys = False
Geremy Condraf19b3652014-07-29 17:54:54 -0700119OPTIONS.replace_verity_public_key = False
120OPTIONS.replace_verity_private_key = False
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700121OPTIONS.replace_verity_keyid = False
Doug Zongker831840e2011-09-22 10:28:04 -0700122OPTIONS.tag_changes = ("-test-keys", "-dev-keys", "+release-keys")
Tao Baoc218a472017-06-19 15:48:02 -0700123OPTIONS.avb_keys = {}
124OPTIONS.avb_algorithms = {}
125OPTIONS.avb_extra_args = {}
Doug Zongkereef39442009-04-02 12:14:19 -0700126
127def GetApkCerts(tf_zip):
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800128 certmap = common.ReadApkCerts(tf_zip)
129
130 # apply the key remapping to the contents of the file
131 for apk, cert in certmap.iteritems():
132 certmap[apk] = OPTIONS.key_map.get(cert, cert)
133
134 # apply all the -e options, overriding anything in the file
Doug Zongkerad88c7c2009-04-14 12:34:27 -0700135 for apk, cert in OPTIONS.extra_apks.iteritems():
Doug Zongkerdecf9952009-12-15 17:27:49 -0800136 if not cert:
137 cert = "PRESIGNED"
Doug Zongkerad88c7c2009-04-14 12:34:27 -0700138 certmap[apk] = OPTIONS.key_map.get(cert, cert)
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800139
Doug Zongkereef39442009-04-02 12:14:19 -0700140 return certmap
141
142
Doug Zongkereb338ef2009-05-20 16:50:49 -0700143def CheckAllApksSigned(input_tf_zip, apk_key_map):
144 """Check that all the APKs we want to sign have keys specified, and
145 error out if they don't."""
146 unknown_apks = []
147 for info in input_tf_zip.infolist():
148 if info.filename.endswith(".apk"):
149 name = os.path.basename(info.filename)
150 if name not in apk_key_map:
151 unknown_apks.append(name)
152 if unknown_apks:
153 print "ERROR: no key specified for:\n\n ",
154 print "\n ".join(unknown_apks)
155 print "\nUse '-e <apkname>=' to specify a key (which may be an"
156 print "empty string to not sign this apk)."
157 sys.exit(1)
158
159
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800160def SignApk(data, keyname, pw, platform_api_level, codename_to_api_level_map):
Doug Zongkereef39442009-04-02 12:14:19 -0700161 unsigned = tempfile.NamedTemporaryFile()
162 unsigned.write(data)
163 unsigned.flush()
164
165 signed = tempfile.NamedTemporaryFile()
166
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800167 # For pre-N builds, don't upgrade to SHA-256 JAR signatures based on the APK's
168 # minSdkVersion to avoid increasing incremental OTA update sizes. If an APK
169 # didn't change, we don't want its signature to change due to the switch
170 # from SHA-1 to SHA-256.
171 # By default, APK signer chooses SHA-256 signatures if the APK's minSdkVersion
172 # is 18 or higher. For pre-N builds we disable this mechanism by pretending
173 # that the APK's minSdkVersion is 1.
174 # For N+ builds, we let APK signer rely on the APK's minSdkVersion to
175 # determine whether to use SHA-256.
176 min_api_level = None
177 if platform_api_level > 23:
178 # Let APK signer choose whether to use SHA-1 or SHA-256, based on the APK's
179 # minSdkVersion attribute
180 min_api_level = None
181 else:
182 # Force APK signer to use SHA-1
183 min_api_level = 1
184
185 common.SignFile(unsigned.name, signed.name, keyname, pw,
186 min_api_level=min_api_level,
187 codename_to_api_level_map=codename_to_api_level_map)
Doug Zongkereef39442009-04-02 12:14:19 -0700188
189 data = signed.read()
190 unsigned.close()
191 signed.close()
192
193 return data
194
195
Doug Zongker412c02f2014-02-13 10:58:24 -0800196def ProcessTargetFiles(input_tf_zip, output_tf_zip, misc_info,
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800197 apk_key_map, key_passwords, platform_api_level,
198 codename_to_api_level_map):
Michael Rungedc2661a2014-06-03 14:43:11 -0700199
Doug Zongkereef39442009-04-02 12:14:19 -0700200 maxsize = max([len(os.path.basename(i.filename))
201 for i in input_tf_zip.infolist()
202 if i.filename.endswith('.apk')])
Tao Baoa80ed222016-06-16 14:41:24 -0700203 system_root_image = misc_info.get("system_root_image") == "true"
Doug Zongker412c02f2014-02-13 10:58:24 -0800204
Doug Zongkereef39442009-04-02 12:14:19 -0700205 for info in input_tf_zip.infolist():
Dan Albert8b72aef2015-03-23 19:13:21 -0700206 if info.filename.startswith("IMAGES/"):
207 continue
Doug Zongker3c84f562014-07-31 11:06:30 -0700208
Doug Zongkereef39442009-04-02 12:14:19 -0700209 data = input_tf_zip.read(info.filename)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700210 out_info = copy.copy(info)
Doug Zongker412c02f2014-02-13 10:58:24 -0800211
Tao Baof2cffbd2015-07-22 12:33:18 -0700212 # Sign APKs.
Doug Zongkereef39442009-04-02 12:14:19 -0700213 if info.filename.endswith(".apk"):
214 name = os.path.basename(info.filename)
Doug Zongker43874f82009-04-14 14:05:15 -0700215 key = apk_key_map[name]
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800216 if key not in common.SPECIAL_CERT_STRINGS:
Doug Zongker43874f82009-04-14 14:05:15 -0700217 print " signing: %-*s (%s)" % (maxsize, name, key)
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800218 signed_data = SignApk(data, key, key_passwords[key], platform_api_level,
219 codename_to_api_level_map)
Tao Bao2ed665a2015-04-01 11:21:55 -0700220 common.ZipWriteStr(output_tf_zip, out_info, signed_data)
Doug Zongkereef39442009-04-02 12:14:19 -0700221 else:
222 # an APK we're not supposed to sign.
Doug Zongker43874f82009-04-14 14:05:15 -0700223 print "NOT signing: %s" % (name,)
Tao Bao2ed665a2015-04-01 11:21:55 -0700224 common.ZipWriteStr(output_tf_zip, out_info, data)
Tao Baoa80ed222016-06-16 14:41:24 -0700225
226 # System properties.
Doug Zongker8e931bf2009-04-06 15:21:45 -0700227 elif info.filename in ("SYSTEM/build.prop",
Jesse Zhao2625d272015-02-06 09:49:55 -0800228 "VENDOR/build.prop",
Hung-ying Tyanf829b402017-05-01 21:56:26 +0800229 "SYSTEM/etc/prop.default",
230 "BOOT/RAMDISK/prop.default",
231 "BOOT/RAMDISK/default.prop", # legacy
232 "ROOT/default.prop", # legacy
233 "RECOVERY/RAMDISK/prop.default",
234 "RECOVERY/RAMDISK/default.prop"): # legacy
Doug Zongker17aa9442009-04-17 10:15:58 -0700235 print "rewriting %s:" % (info.filename,)
Hung-ying Tyanf829b402017-05-01 21:56:26 +0800236 if stat.S_ISLNK(info.external_attr >> 16):
237 new_data = data
238 else:
239 new_data = RewriteProps(data, misc_info)
Tao Bao2ed665a2015-04-01 11:21:55 -0700240 common.ZipWriteStr(output_tf_zip, out_info, new_data)
Tao Baoa80ed222016-06-16 14:41:24 -0700241
Robert Craig817c5742013-04-19 10:59:22 -0400242 elif info.filename.endswith("mac_permissions.xml"):
243 print "rewriting %s with new keys." % (info.filename,)
244 new_data = ReplaceCerts(data)
Tao Bao2ed665a2015-04-01 11:21:55 -0700245 common.ZipWriteStr(output_tf_zip, out_info, new_data)
Tao Baoa80ed222016-06-16 14:41:24 -0700246
Tianjie Xu616fbeb2017-05-23 14:51:02 -0700247 # Ask add_img_to_target_files to rebuild the recovery patch if needed.
Doug Zongker412c02f2014-02-13 10:58:24 -0800248 elif info.filename in ("SYSTEM/recovery-from-boot.p",
Tao Baof2cffbd2015-07-22 12:33:18 -0700249 "SYSTEM/etc/recovery.img",
Doug Zongker412c02f2014-02-13 10:58:24 -0800250 "SYSTEM/bin/install-recovery.sh"):
Tianjie Xu616fbeb2017-05-23 14:51:02 -0700251 OPTIONS.rebuild_recovery = True
Tao Baoa80ed222016-06-16 14:41:24 -0700252
253 # Don't copy OTA keys if we're replacing them.
Doug Zongker412c02f2014-02-13 10:58:24 -0800254 elif (OPTIONS.replace_ota_keys and
Tao Baoa80ed222016-06-16 14:41:24 -0700255 info.filename in (
256 "BOOT/RAMDISK/res/keys",
Alex Deymob3e8ce62016-08-04 16:06:12 -0700257 "BOOT/RAMDISK/etc/update_engine/update-payload-key.pub.pem",
Tao Baoa80ed222016-06-16 14:41:24 -0700258 "RECOVERY/RAMDISK/res/keys",
259 "SYSTEM/etc/security/otacerts.zip",
260 "SYSTEM/etc/update_engine/update-payload-key.pub.pem")):
Doug Zongker412c02f2014-02-13 10:58:24 -0800261 pass
Tao Baoa80ed222016-06-16 14:41:24 -0700262
Tao Bao57ae9a22017-06-05 11:55:16 -0700263 # Skip META/misc_info.txt since we will write back the new values later.
264 elif info.filename == "META/misc_info.txt":
Geremy Condraf19b3652014-07-29 17:54:54 -0700265 pass
Tao Bao8adcfd12016-06-17 17:01:22 -0700266
267 # Skip verity public key if we will replace it.
Michael Runge947894f2014-10-14 20:58:38 -0700268 elif (OPTIONS.replace_verity_public_key and
Tao Bao7a5bf8a2015-07-21 18:01:20 -0700269 info.filename in ("BOOT/RAMDISK/verity_key",
Tao Bao8adcfd12016-06-17 17:01:22 -0700270 "ROOT/verity_key")):
Geremy Condraf19b3652014-07-29 17:54:54 -0700271 pass
Tao Baoa80ed222016-06-16 14:41:24 -0700272
Tao Bao8adcfd12016-06-17 17:01:22 -0700273 # Skip verity keyid (for system_root_image use) if we will replace it.
274 elif (OPTIONS.replace_verity_keyid and
275 info.filename == "BOOT/cmdline"):
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700276 pass
277
Tianjie Xu4f099002016-08-11 18:04:27 -0700278 # Skip the care_map as we will regenerate the system/vendor images.
Tao Bao9aa4b9b2016-09-29 17:53:56 -0700279 elif info.filename == "META/care_map.txt":
Tianjie Xu4f099002016-08-11 18:04:27 -0700280 pass
281
Tao Baoa80ed222016-06-16 14:41:24 -0700282 # A non-APK file; copy it verbatim.
Doug Zongkereef39442009-04-02 12:14:19 -0700283 else:
Tao Bao2ed665a2015-04-01 11:21:55 -0700284 common.ZipWriteStr(output_tf_zip, out_info, data)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700285
Doug Zongker412c02f2014-02-13 10:58:24 -0800286 if OPTIONS.replace_ota_keys:
Tianjie Xu616fbeb2017-05-23 14:51:02 -0700287 ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info)
Doug Zongker412c02f2014-02-13 10:58:24 -0800288
Tao Bao57ae9a22017-06-05 11:55:16 -0700289 # Replace the keyid string in misc_info dict.
Tao Bao8adcfd12016-06-17 17:01:22 -0700290 if OPTIONS.replace_verity_private_key:
Tao Bao57ae9a22017-06-05 11:55:16 -0700291 ReplaceVerityPrivateKey(misc_info, OPTIONS.replace_verity_private_key[1])
Tao Bao8adcfd12016-06-17 17:01:22 -0700292
293 if OPTIONS.replace_verity_public_key:
294 if system_root_image:
295 dest = "ROOT/verity_key"
296 else:
297 dest = "BOOT/RAMDISK/verity_key"
298 # We are replacing the one in boot image only, since the one under
299 # recovery won't ever be needed.
Tianjie Xu616fbeb2017-05-23 14:51:02 -0700300 ReplaceVerityPublicKey(
Tao Bao8adcfd12016-06-17 17:01:22 -0700301 output_tf_zip, dest, OPTIONS.replace_verity_public_key[1])
Tao Bao8adcfd12016-06-17 17:01:22 -0700302
303 # Replace the keyid string in BOOT/cmdline.
304 if OPTIONS.replace_verity_keyid:
Tianjie Xu616fbeb2017-05-23 14:51:02 -0700305 ReplaceVerityKeyId(input_tf_zip, output_tf_zip,
306 OPTIONS.replace_verity_keyid[1])
Doug Zongker412c02f2014-02-13 10:58:24 -0800307
Tao Baoc218a472017-06-19 15:48:02 -0700308 # Replace the AVB signing keys, if any.
309 ReplaceAvbSigningKeys(misc_info)
310
Tao Bao57ae9a22017-06-05 11:55:16 -0700311 # Write back misc_info with the latest values.
312 ReplaceMiscInfoTxt(input_tf_zip, output_tf_zip, misc_info)
313
Doug Zongker8e931bf2009-04-06 15:21:45 -0700314
Robert Craig817c5742013-04-19 10:59:22 -0400315def ReplaceCerts(data):
316 """Given a string of data, replace all occurences of a set
317 of X509 certs with a newer set of X509 certs and return
318 the updated data string."""
319 for old, new in OPTIONS.key_map.iteritems():
320 try:
321 if OPTIONS.verbose:
322 print " Replacing %s.x509.pem with %s.x509.pem" % (old, new)
323 f = open(old + ".x509.pem")
324 old_cert16 = base64.b16encode(common.ParseCertificate(f.read())).lower()
325 f.close()
326 f = open(new + ".x509.pem")
327 new_cert16 = base64.b16encode(common.ParseCertificate(f.read())).lower()
328 f.close()
329 # Only match entire certs.
330 pattern = "\\b"+old_cert16+"\\b"
331 (data, num) = re.subn(pattern, new_cert16, data, flags=re.IGNORECASE)
332 if OPTIONS.verbose:
333 print " Replaced %d occurence(s) of %s.x509.pem with " \
334 "%s.x509.pem" % (num, old, new)
Dan Albert8b72aef2015-03-23 19:13:21 -0700335 except IOError as e:
336 if e.errno == errno.ENOENT and not OPTIONS.verbose:
Robert Craig817c5742013-04-19 10:59:22 -0400337 continue
338
339 print " Error accessing %s. %s. Skip replacing %s.x509.pem " \
340 "with %s.x509.pem." % (e.filename, e.strerror, old, new)
341
342 return data
343
344
Doug Zongkerc09abc82010-01-11 13:09:15 -0800345def EditTags(tags):
346 """Given a string containing comma-separated tags, apply the edits
347 specified in OPTIONS.tag_changes and return the updated string."""
348 tags = set(tags.split(","))
349 for ch in OPTIONS.tag_changes:
350 if ch[0] == "-":
351 tags.discard(ch[1:])
352 elif ch[0] == "+":
353 tags.add(ch[1:])
354 return ",".join(sorted(tags))
355
356
Michael Rungedc2661a2014-06-03 14:43:11 -0700357def RewriteProps(data, misc_info):
Doug Zongker17aa9442009-04-17 10:15:58 -0700358 output = []
359 for line in data.split("\n"):
360 line = line.strip()
361 original_line = line
Michael Rungedc2661a2014-06-03 14:43:11 -0700362 if line and line[0] != '#' and "=" in line:
Doug Zongker17aa9442009-04-17 10:15:58 -0700363 key, value = line.split("=", 1)
Michael Rungee07c75a2014-12-09 13:54:23 -0800364 if (key in ("ro.build.fingerprint", "ro.vendor.build.fingerprint")
Michael Rungedc2661a2014-06-03 14:43:11 -0700365 and misc_info.get("oem_fingerprint_properties") is None):
366 pieces = value.split("/")
367 pieces[-1] = EditTags(pieces[-1])
368 value = "/".join(pieces)
Michael Rungee07c75a2014-12-09 13:54:23 -0800369 elif (key in ("ro.build.thumbprint", "ro.vendor.build.thumbprint")
Dan Albert8b72aef2015-03-23 19:13:21 -0700370 and misc_info.get("oem_fingerprint_properties") is not None):
Doug Zongkerc09abc82010-01-11 13:09:15 -0800371 pieces = value.split("/")
372 pieces[-1] = EditTags(pieces[-1])
373 value = "/".join(pieces)
Tao Baocb7ff772015-09-11 15:27:56 -0700374 elif key == "ro.bootimage.build.fingerprint":
375 pieces = value.split("/")
376 pieces[-1] = EditTags(pieces[-1])
377 value = "/".join(pieces)
Doug Zongker17aa9442009-04-17 10:15:58 -0700378 elif key == "ro.build.description":
Doug Zongkerc09abc82010-01-11 13:09:15 -0800379 pieces = value.split(" ")
Doug Zongker17aa9442009-04-17 10:15:58 -0700380 assert len(pieces) == 5
Doug Zongkerc09abc82010-01-11 13:09:15 -0800381 pieces[-1] = EditTags(pieces[-1])
382 value = " ".join(pieces)
383 elif key == "ro.build.tags":
384 value = EditTags(value)
Doug Zongkera8608a72013-07-23 11:51:04 -0700385 elif key == "ro.build.display.id":
386 # change, eg, "JWR66N dev-keys" to "JWR66N"
387 value = value.split()
Michael Rungedc2661a2014-06-03 14:43:11 -0700388 if len(value) > 1 and value[-1].endswith("-keys"):
Andrew Boie73d5abb2013-12-11 12:42:03 -0800389 value.pop()
390 value = " ".join(value)
Doug Zongkerc09abc82010-01-11 13:09:15 -0800391 line = key + "=" + value
Doug Zongker17aa9442009-04-17 10:15:58 -0700392 if line != original_line:
393 print " replace: ", original_line
394 print " with: ", line
395 output.append(line)
396 return "\n".join(output) + "\n"
397
398
Doug Zongker831840e2011-09-22 10:28:04 -0700399def ReplaceOtaKeys(input_tf_zip, output_tf_zip, misc_info):
Doug Zongker8e931bf2009-04-06 15:21:45 -0700400 try:
401 keylist = input_tf_zip.read("META/otakeys.txt").split()
402 except KeyError:
T.R. Fullharta28acc62013-03-18 10:31:26 -0700403 raise common.ExternalError("can't read META/otakeys.txt from input")
Doug Zongker8e931bf2009-04-06 15:21:45 -0700404
Doug Zongkere121d6a2011-02-01 14:13:52 -0800405 extra_recovery_keys = misc_info.get("extra_recovery_keys", None)
406 if extra_recovery_keys:
407 extra_recovery_keys = [OPTIONS.key_map.get(k, k) + ".x509.pem"
408 for k in extra_recovery_keys.split()]
409 if extra_recovery_keys:
410 print "extra recovery-only key(s): " + ", ".join(extra_recovery_keys)
411 else:
412 extra_recovery_keys = []
413
Doug Zongker8e931bf2009-04-06 15:21:45 -0700414 mapped_keys = []
415 for k in keylist:
416 m = re.match(r"^(.*)\.x509\.pem$", k)
417 if not m:
Doug Zongker412c02f2014-02-13 10:58:24 -0800418 raise common.ExternalError(
419 "can't parse \"%s\" from META/otakeys.txt" % (k,))
Doug Zongker8e931bf2009-04-06 15:21:45 -0700420 k = m.group(1)
421 mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem")
422
Doug Zongkere05628c2009-08-20 17:38:42 -0700423 if mapped_keys:
424 print "using:\n ", "\n ".join(mapped_keys)
425 print "for OTA package verification"
426 else:
Doug Zongker831840e2011-09-22 10:28:04 -0700427 devkey = misc_info.get("default_system_dev_certificate",
428 "build/target/product/security/testkey")
Doug Zongkere05628c2009-08-20 17:38:42 -0700429 mapped_keys.append(
Doug Zongker831840e2011-09-22 10:28:04 -0700430 OPTIONS.key_map.get(devkey, devkey) + ".x509.pem")
Tao Baoa80ed222016-06-16 14:41:24 -0700431 print("META/otakeys.txt has no keys; using %s for OTA package"
432 " verification." % (mapped_keys[0],))
Doug Zongker8e931bf2009-04-06 15:21:45 -0700433
434 # recovery uses a version of the key that has been slightly
435 # predigested (by DumpPublicKey.java) and put in res/keys.
Doug Zongkere121d6a2011-02-01 14:13:52 -0800436 # extra_recovery_keys are used only in recovery.
Tao Baoe95540e2016-11-08 12:08:53 -0800437 cmd = ([OPTIONS.java_path] + OPTIONS.java_args +
438 ["-jar",
439 os.path.join(OPTIONS.search_path, "framework", "dumpkey.jar")] +
440 mapped_keys + extra_recovery_keys)
441 p = common.Run(cmd, stdout=subprocess.PIPE)
Doug Zongker412c02f2014-02-13 10:58:24 -0800442 new_recovery_keys, _ = p.communicate()
Doug Zongker8e931bf2009-04-06 15:21:45 -0700443 if p.returncode != 0:
T.R. Fullharta28acc62013-03-18 10:31:26 -0700444 raise common.ExternalError("failed to run dumpkeys")
Tao Baoa80ed222016-06-16 14:41:24 -0700445
446 # system_root_image puts the recovery keys at BOOT/RAMDISK.
447 if misc_info.get("system_root_image") == "true":
448 recovery_keys_location = "BOOT/RAMDISK/res/keys"
449 else:
450 recovery_keys_location = "RECOVERY/RAMDISK/res/keys"
451 common.ZipWriteStr(output_tf_zip, recovery_keys_location, new_recovery_keys)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700452
453 # SystemUpdateActivity uses the x509.pem version of the keys, but
454 # put into a zipfile system/etc/security/otacerts.zip.
Doug Zongkere121d6a2011-02-01 14:13:52 -0800455 # We DO NOT include the extra_recovery_keys (if any) here.
Doug Zongker8e931bf2009-04-06 15:21:45 -0700456
Dan Albert8b72aef2015-03-23 19:13:21 -0700457 temp_file = cStringIO.StringIO()
458 certs_zip = zipfile.ZipFile(temp_file, "w")
Doug Zongker8e931bf2009-04-06 15:21:45 -0700459 for k in mapped_keys:
Tao Bao83cd79d2016-04-11 23:05:52 -0700460 common.ZipWrite(certs_zip, k)
461 common.ZipClose(certs_zip)
Doug Zongker048e7ca2009-06-15 14:31:53 -0700462 common.ZipWriteStr(output_tf_zip, "SYSTEM/etc/security/otacerts.zip",
Dan Albert8b72aef2015-03-23 19:13:21 -0700463 temp_file.getvalue())
Doug Zongkereef39442009-04-02 12:14:19 -0700464
Tao Baoa80ed222016-06-16 14:41:24 -0700465 # For A/B devices, update the payload verification key.
466 if misc_info.get("ab_update") == "true":
467 # Unlike otacerts.zip that may contain multiple keys, we can only specify
468 # ONE payload verification key.
469 if len(mapped_keys) > 1:
470 print("\n WARNING: Found more than one OTA keys; Using the first one"
471 " as payload verification key.\n\n")
472
473 print "Using %s for payload verification." % (mapped_keys[0],)
Tao Bao13b69622016-07-06 15:28:59 -0700474 cmd = common.Run(
475 ["openssl", "x509", "-pubkey", "-noout", "-in", mapped_keys[0]],
476 stdout=subprocess.PIPE)
477 pubkey, _ = cmd.communicate()
478 common.ZipWriteStr(
Tao Baoa80ed222016-06-16 14:41:24 -0700479 output_tf_zip,
Tao Bao13b69622016-07-06 15:28:59 -0700480 "SYSTEM/etc/update_engine/update-payload-key.pub.pem",
481 pubkey)
Alex Deymob3e8ce62016-08-04 16:06:12 -0700482 common.ZipWriteStr(
483 output_tf_zip,
484 "BOOT/RAMDISK/etc/update_engine/update-payload-key.pub.pem",
485 pubkey)
Tao Baoa80ed222016-06-16 14:41:24 -0700486
Doug Zongker412c02f2014-02-13 10:58:24 -0800487 return new_recovery_keys
488
Tao Bao8adcfd12016-06-17 17:01:22 -0700489
Tao Bao7a5bf8a2015-07-21 18:01:20 -0700490def ReplaceVerityPublicKey(targetfile_zip, filename, key_path):
Tao Bao57ae9a22017-06-05 11:55:16 -0700491 print "Replacing verity public key with %s" % (key_path,)
492 common.ZipWrite(targetfile_zip, key_path, arcname=filename)
Geremy Condraf19b3652014-07-29 17:54:54 -0700493
Tao Bao8adcfd12016-06-17 17:01:22 -0700494
Tao Bao57ae9a22017-06-05 11:55:16 -0700495def ReplaceVerityPrivateKey(misc_info, key_path):
496 print "Replacing verity private key with %s" % (key_path,)
Andrew Boied083f0b2014-09-15 16:01:07 -0700497 misc_info["verity_key"] = key_path
Doug Zongkereef39442009-04-02 12:14:19 -0700498
Tao Bao8adcfd12016-06-17 17:01:22 -0700499
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700500def ReplaceVerityKeyId(targetfile_input_zip, targetfile_output_zip, keypath):
501 in_cmdline = targetfile_input_zip.read("BOOT/cmdline")
502 # copy in_cmdline to output_zip if veritykeyid is not present in in_cmdline
503 if "veritykeyid" not in in_cmdline:
504 common.ZipWriteStr(targetfile_output_zip, "BOOT/cmdline", in_cmdline)
505 return in_cmdline
506 out_cmdline = []
507 for param in in_cmdline.split():
508 if "veritykeyid" in param:
509 # extract keyid using openssl command
Tao Bao9aa4b9b2016-09-29 17:53:56 -0700510 p = common.Run(
511 ["openssl", "x509", "-in", keypath, "-text"],
512 stdout=subprocess.PIPE)
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700513 keyid, stderr = p.communicate()
Tao Bao9aa4b9b2016-09-29 17:53:56 -0700514 keyid = re.search(
515 r'keyid:([0-9a-fA-F:]*)', keyid).group(1).replace(':', '').lower()
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700516 print "Replacing verity keyid with %s error=%s" % (keyid, stderr)
517 out_cmdline.append("veritykeyid=id:%s" % (keyid,))
518 else:
519 out_cmdline.append(param)
520
521 out_cmdline = ' '.join(out_cmdline)
522 out_cmdline = out_cmdline.strip()
523 print "out_cmdline %s" % (out_cmdline)
524 common.ZipWriteStr(targetfile_output_zip, "BOOT/cmdline", out_cmdline)
Tao Bao57ae9a22017-06-05 11:55:16 -0700525
526
527def ReplaceMiscInfoTxt(input_zip, output_zip, misc_info):
528 """Replaces META/misc_info.txt.
529
530 Only writes back the ones in the original META/misc_info.txt. Because the
531 current in-memory dict contains additional items computed at runtime.
532 """
533 misc_info_old = common.LoadDictionaryFromLines(
534 input_zip.read('META/misc_info.txt').split('\n'))
535 items = []
536 for key in sorted(misc_info):
537 if key in misc_info_old:
538 items.append('%s=%s' % (key, misc_info[key]))
539 common.ZipWriteStr(output_zip, "META/misc_info.txt", '\n'.join(items))
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700540
Tao Bao8adcfd12016-06-17 17:01:22 -0700541
Tao Baoc218a472017-06-19 15:48:02 -0700542def ReplaceAvbSigningKeys(misc_info):
543 """Replaces the AVB signing keys."""
544
545 AVB_FOOTER_ARGS_BY_PARTITION = {
546 'boot' : 'avb_boot_add_hash_footer_args',
547 'dtbo' : 'avb_dtbo_add_hash_footer_args',
548 'system' : 'avb_system_add_hashtree_footer_args',
549 'vendor' : 'avb_vendor_add_hashtree_footer_args',
550 'vbmeta' : 'avb_vbmeta_args',
551 }
552
553 def ReplaceAvbPartitionSigningKey(partition):
554 key = OPTIONS.avb_keys.get(partition)
555 if not key:
556 return
557
558 algorithm = OPTIONS.avb_algorithms.get(partition)
559 assert algorithm, 'Missing AVB signing algorithm for %s' % (partition,)
560
561 print 'Replacing AVB signing key for %s with "%s" (%s)' % (
562 partition, key, algorithm)
563 misc_info['avb_' + partition + '_algorithm'] = algorithm
564 misc_info['avb_' + partition + '_key_path'] = key
565
566 extra_args = OPTIONS.avb_extra_args.get(partition)
567 if extra_args:
568 print 'Setting extra AVB signing args for %s to "%s"' % (
569 partition, extra_args)
570 args_key = AVB_FOOTER_ARGS_BY_PARTITION[partition]
571 misc_info[args_key] = (misc_info.get(args_key, '') + ' ' + extra_args)
572
573 for partition in AVB_FOOTER_ARGS_BY_PARTITION:
574 ReplaceAvbPartitionSigningKey(partition)
575
576
Doug Zongker831840e2011-09-22 10:28:04 -0700577def BuildKeyMap(misc_info, key_mapping_options):
578 for s, d in key_mapping_options:
579 if s is None: # -d option
580 devkey = misc_info.get("default_system_dev_certificate",
581 "build/target/product/security/testkey")
582 devkeydir = os.path.dirname(devkey)
583
584 OPTIONS.key_map.update({
585 devkeydir + "/testkey": d + "/releasekey",
586 devkeydir + "/devkey": d + "/releasekey",
587 devkeydir + "/media": d + "/media",
588 devkeydir + "/shared": d + "/shared",
589 devkeydir + "/platform": d + "/platform",
590 })
591 else:
592 OPTIONS.key_map[s] = d
593
594
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800595def GetApiLevelAndCodename(input_tf_zip):
596 data = input_tf_zip.read("SYSTEM/build.prop")
597 api_level = None
598 codename = None
599 for line in data.split("\n"):
600 line = line.strip()
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800601 if line and line[0] != '#' and "=" in line:
602 key, value = line.split("=", 1)
603 key = key.strip()
604 if key == "ro.build.version.sdk":
605 api_level = int(value.strip())
606 elif key == "ro.build.version.codename":
607 codename = value.strip()
608
609 if api_level is None:
610 raise ValueError("No ro.build.version.sdk in SYSTEM/build.prop")
611 if codename is None:
612 raise ValueError("No ro.build.version.codename in SYSTEM/build.prop")
613
614 return (api_level, codename)
615
616
617def GetCodenameToApiLevelMap(input_tf_zip):
618 data = input_tf_zip.read("SYSTEM/build.prop")
619 api_level = None
620 codenames = None
621 for line in data.split("\n"):
622 line = line.strip()
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800623 if line and line[0] != '#' and "=" in line:
624 key, value = line.split("=", 1)
625 key = key.strip()
626 if key == "ro.build.version.sdk":
627 api_level = int(value.strip())
628 elif key == "ro.build.version.all_codenames":
629 codenames = value.strip().split(",")
630
631 if api_level is None:
632 raise ValueError("No ro.build.version.sdk in SYSTEM/build.prop")
633 if codenames is None:
634 raise ValueError("No ro.build.version.all_codenames in SYSTEM/build.prop")
635
636 result = dict()
637 for codename in codenames:
638 codename = codename.strip()
639 if len(codename) > 0:
640 result[codename] = api_level
641 return result
642
643
Doug Zongkereef39442009-04-02 12:14:19 -0700644def main(argv):
645
Doug Zongker831840e2011-09-22 10:28:04 -0700646 key_mapping_options = []
647
Doug Zongkereef39442009-04-02 12:14:19 -0700648 def option_handler(o, a):
Doug Zongker05d3dea2009-06-22 11:32:31 -0700649 if o in ("-e", "--extra_apks"):
Doug Zongkereef39442009-04-02 12:14:19 -0700650 names, key = a.split("=")
651 names = names.split(",")
652 for n in names:
653 OPTIONS.extra_apks[n] = key
654 elif o in ("-d", "--default_key_mappings"):
Doug Zongker831840e2011-09-22 10:28:04 -0700655 key_mapping_options.append((None, a))
Doug Zongkereef39442009-04-02 12:14:19 -0700656 elif o in ("-k", "--key_mapping"):
Doug Zongker831840e2011-09-22 10:28:04 -0700657 key_mapping_options.append(a.split("=", 1))
Doug Zongker8e931bf2009-04-06 15:21:45 -0700658 elif o in ("-o", "--replace_ota_keys"):
659 OPTIONS.replace_ota_keys = True
Doug Zongkerae877012009-04-21 10:04:51 -0700660 elif o in ("-t", "--tag_changes"):
661 new = []
662 for i in a.split(","):
663 i = i.strip()
664 if not i or i[0] not in "-+":
665 raise ValueError("Bad tag change '%s'" % (i,))
666 new.append(i[0] + i[1:].strip())
667 OPTIONS.tag_changes = tuple(new)
Geremy Condraf19b3652014-07-29 17:54:54 -0700668 elif o == "--replace_verity_public_key":
669 OPTIONS.replace_verity_public_key = (True, a)
670 elif o == "--replace_verity_private_key":
671 OPTIONS.replace_verity_private_key = (True, a)
Badhri Jagan Sridharan35c9b122016-06-16 19:58:44 -0700672 elif o == "--replace_verity_keyid":
673 OPTIONS.replace_verity_keyid = (True, a)
Tao Baoc218a472017-06-19 15:48:02 -0700674 elif o == "--avb_vbmeta_key":
675 OPTIONS.avb_keys['vbmeta'] = a
676 elif o == "--avb_vbmeta_algorithm":
677 OPTIONS.avb_algorithms['vbmeta'] = a
678 elif o == "--avb_vbmeta_extra_args":
679 OPTIONS.avb_extra_args['vbmeta'] = a
680 elif o == "--avb_boot_key":
681 OPTIONS.avb_keys['boot'] = a
682 elif o == "--avb_boot_algorithm":
683 OPTIONS.avb_algorithms['boot'] = a
684 elif o == "--avb_boot_extra_args":
685 OPTIONS.avb_extra_args['boot'] = a
686 elif o == "--avb_dtbo_key":
687 OPTIONS.avb_keys['dtbo'] = a
688 elif o == "--avb_dtbo_algorithm":
689 OPTIONS.avb_algorithms['dtbo'] = a
690 elif o == "--avb_dtbo_extra_args":
691 OPTIONS.avb_extra_args['dtbo'] = a
692 elif o == "--avb_system_key":
693 OPTIONS.avb_keys['system'] = a
694 elif o == "--avb_system_algorithm":
695 OPTIONS.avb_algorithms['system'] = a
696 elif o == "--avb_system_extra_args":
697 OPTIONS.avb_extra_args['system'] = a
698 elif o == "--avb_vendor_key":
699 OPTIONS.avb_keys['vendor'] = a
700 elif o == "--avb_vendor_algorithm":
701 OPTIONS.avb_algorithms['vendor'] = a
702 elif o == "--avb_vendor_extra_args":
703 OPTIONS.avb_extra_args['vendor'] = a
Doug Zongkereef39442009-04-02 12:14:19 -0700704 else:
705 return False
706 return True
707
Tao Baoc218a472017-06-19 15:48:02 -0700708 args = common.ParseOptions(
709 argv, __doc__,
710 extra_opts="e:d:k:ot:",
711 extra_long_opts=[
712 "extra_apks=",
713 "default_key_mappings=",
714 "key_mapping=",
715 "replace_ota_keys",
716 "tag_changes=",
717 "replace_verity_public_key=",
718 "replace_verity_private_key=",
719 "replace_verity_keyid=",
720 "avb_vbmeta_algorithm=",
721 "avb_vbmeta_key=",
722 "avb_vbmeta_extra_args=",
723 "avb_boot_algorithm=",
724 "avb_boot_key=",
725 "avb_boot_extra_args=",
726 "avb_dtbo_algorithm=",
727 "avb_dtbo_key=",
728 "avb_dtbo_extra_args=",
729 "avb_system_algorithm=",
730 "avb_system_key=",
731 "avb_system_extra_args=",
732 "avb_vendor_algorithm=",
733 "avb_vendor_key=",
734 "avb_vendor_extra_args=",
735 ],
736 extra_option_handler=option_handler)
Doug Zongkereef39442009-04-02 12:14:19 -0700737
738 if len(args) != 2:
739 common.Usage(__doc__)
740 sys.exit(1)
741
742 input_zip = zipfile.ZipFile(args[0], "r")
743 output_zip = zipfile.ZipFile(args[1], "w")
744
Doug Zongker831840e2011-09-22 10:28:04 -0700745 misc_info = common.LoadInfoDict(input_zip)
746
747 BuildKeyMap(misc_info, key_mapping_options)
748
Doug Zongkereb338ef2009-05-20 16:50:49 -0700749 apk_key_map = GetApkCerts(input_zip)
750 CheckAllApksSigned(input_zip, apk_key_map)
Doug Zongkereb338ef2009-05-20 16:50:49 -0700751
752 key_passwords = common.GetKeyPasswords(set(apk_key_map.values()))
Tao Bao9aa4b9b2016-09-29 17:53:56 -0700753 platform_api_level, _ = GetApiLevelAndCodename(input_zip)
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800754 codename_to_api_level_map = GetCodenameToApiLevelMap(input_zip)
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800755
Doug Zongker412c02f2014-02-13 10:58:24 -0800756 ProcessTargetFiles(input_zip, output_zip, misc_info,
Alex Klyubin2cfd1d12016-01-13 10:32:47 -0800757 apk_key_map, key_passwords,
758 platform_api_level,
759 codename_to_api_level_map)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700760
Tao Bao2ed665a2015-04-01 11:21:55 -0700761 common.ZipClose(input_zip)
762 common.ZipClose(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -0700763
Tianjie Xub48589a2016-08-03 19:21:52 -0700764 # Skip building userdata.img and cache.img when signing the target files.
Tianjie Xu616fbeb2017-05-23 14:51:02 -0700765 new_args = ["--is_signing"]
766 # add_img_to_target_files builds the system image from scratch, so the
767 # recovery patch is guaranteed to be regenerated there.
768 if OPTIONS.rebuild_recovery:
769 new_args.append("--rebuild_recovery")
770 new_args.append(args[1])
Tianjie Xub48589a2016-08-03 19:21:52 -0700771 add_img_to_target_files.main(new_args)
Doug Zongker3c84f562014-07-31 11:06:30 -0700772
Doug Zongkereef39442009-04-02 12:14:19 -0700773 print "done."
774
775
776if __name__ == '__main__':
777 try:
778 main(sys.argv[1:])
779 except common.ExternalError, e:
780 print
781 print " ERROR: %s" % (e,)
782 print
783 sys.exit(1)
Tao Baoc218a472017-06-19 15:48:02 -0700784 finally:
785 common.Cleanup()