blob: c97402604ed16058ad3491a9804151918a6bfd03 [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 Zongkerad88c7c2009-04-14 12:34:27 -070086 certmap = {}
Doug Zongkereef39442009-04-02 12:14:19 -070087 for line in tf_zip.read("META/apkcerts.txt").split("\n"):
88 line = line.strip()
89 if not line: continue
90 m = re.match(r'^name="(.*)"\s+certificate="(.*)\.x509\.pem"\s+'
91 r'private_key="\2\.pk8"$', line)
92 if not m:
93 raise SigningError("failed to parse line from apkcerts.txt:\n" + line)
94 certmap[m.group(1)] = OPTIONS.key_map.get(m.group(2), m.group(2))
Doug Zongkerad88c7c2009-04-14 12:34:27 -070095 for apk, cert in OPTIONS.extra_apks.iteritems():
96 certmap[apk] = OPTIONS.key_map.get(cert, cert)
Doug Zongkereef39442009-04-02 12:14:19 -070097 return certmap
98
99
Doug Zongkereb338ef2009-05-20 16:50:49 -0700100def CheckAllApksSigned(input_tf_zip, apk_key_map):
101 """Check that all the APKs we want to sign have keys specified, and
102 error out if they don't."""
103 unknown_apks = []
104 for info in input_tf_zip.infolist():
105 if info.filename.endswith(".apk"):
106 name = os.path.basename(info.filename)
107 if name not in apk_key_map:
108 unknown_apks.append(name)
109 if unknown_apks:
110 print "ERROR: no key specified for:\n\n ",
111 print "\n ".join(unknown_apks)
112 print "\nUse '-e <apkname>=' to specify a key (which may be an"
113 print "empty string to not sign this apk)."
114 sys.exit(1)
115
116
Doug Zongkereef39442009-04-02 12:14:19 -0700117def SignApk(data, keyname, pw):
118 unsigned = tempfile.NamedTemporaryFile()
119 unsigned.write(data)
120 unsigned.flush()
121
122 signed = tempfile.NamedTemporaryFile()
123
124 common.SignFile(unsigned.name, signed.name, keyname, pw, align=4)
125
126 data = signed.read()
127 unsigned.close()
128 signed.close()
129
130 return data
131
132
Doug Zongkereb338ef2009-05-20 16:50:49 -0700133def SignApks(input_tf_zip, output_tf_zip, apk_key_map, key_passwords):
Doug Zongkereef39442009-04-02 12:14:19 -0700134 maxsize = max([len(os.path.basename(i.filename))
135 for i in input_tf_zip.infolist()
136 if i.filename.endswith('.apk')])
137
138 for info in input_tf_zip.infolist():
139 data = input_tf_zip.read(info.filename)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700140 out_info = copy.copy(info)
Doug Zongkereef39442009-04-02 12:14:19 -0700141 if info.filename.endswith(".apk"):
142 name = os.path.basename(info.filename)
Doug Zongker43874f82009-04-14 14:05:15 -0700143 key = apk_key_map[name]
144 if key:
145 print " signing: %-*s (%s)" % (maxsize, name, key)
Doug Zongkereef39442009-04-02 12:14:19 -0700146 signed_data = SignApk(data, key, key_passwords[key])
Doug Zongker8e931bf2009-04-06 15:21:45 -0700147 output_tf_zip.writestr(out_info, signed_data)
Doug Zongkereef39442009-04-02 12:14:19 -0700148 else:
149 # an APK we're not supposed to sign.
Doug Zongker43874f82009-04-14 14:05:15 -0700150 print "NOT signing: %s" % (name,)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700151 output_tf_zip.writestr(out_info, data)
152 elif info.filename in ("SYSTEM/build.prop",
153 "RECOVERY/RAMDISK/default.prop"):
Doug Zongker17aa9442009-04-17 10:15:58 -0700154 print "rewriting %s:" % (info.filename,)
155 new_data = RewriteProps(data)
156 output_tf_zip.writestr(out_info, new_data)
Doug Zongkereef39442009-04-02 12:14:19 -0700157 else:
158 # a non-APK file; copy it verbatim
Doug Zongker8e931bf2009-04-06 15:21:45 -0700159 output_tf_zip.writestr(out_info, data)
160
161
Doug Zongker17aa9442009-04-17 10:15:58 -0700162def RewriteProps(data):
163 output = []
164 for line in data.split("\n"):
165 line = line.strip()
166 original_line = line
167 if line and line[0] != '#':
168 key, value = line.split("=", 1)
169 if key == "ro.build.fingerprint":
170 pieces = line.split("/")
171 tags = set(pieces[-1].split(","))
Doug Zongkerae877012009-04-21 10:04:51 -0700172 for ch in OPTIONS.tag_changes:
173 if ch[0] == "-":
174 tags.discard(ch[1:])
175 elif ch[0] == "+":
176 tags.add(ch[1:])
Doug Zongker17aa9442009-04-17 10:15:58 -0700177 line = "/".join(pieces[:-1] + [",".join(sorted(tags))])
178 elif key == "ro.build.description":
179 pieces = line.split(" ")
180 assert len(pieces) == 5
181 tags = set(pieces[-1].split(","))
Doug Zongkerae877012009-04-21 10:04:51 -0700182 for ch in OPTIONS.tag_changes:
183 if ch[0] == "-":
184 tags.discard(ch[1:])
185 elif ch[0] == "+":
186 tags.add(ch[1:])
Doug Zongker17aa9442009-04-17 10:15:58 -0700187 line = " ".join(pieces[:-1] + [",".join(sorted(tags))])
188 if line != original_line:
189 print " replace: ", original_line
190 print " with: ", line
191 output.append(line)
192 return "\n".join(output) + "\n"
193
194
Doug Zongker8e931bf2009-04-06 15:21:45 -0700195def ReplaceOtaKeys(input_tf_zip, output_tf_zip):
196 try:
197 keylist = input_tf_zip.read("META/otakeys.txt").split()
198 except KeyError:
199 raise ExternalError("can't read META/otakeys.txt from input")
200
201 mapped_keys = []
202 for k in keylist:
203 m = re.match(r"^(.*)\.x509\.pem$", k)
204 if not m:
205 raise ExternalError("can't parse \"%s\" from META/otakeys.txt" % (k,))
206 k = m.group(1)
207 mapped_keys.append(OPTIONS.key_map.get(k, k) + ".x509.pem")
208
Doug Zongkere05628c2009-08-20 17:38:42 -0700209 if mapped_keys:
210 print "using:\n ", "\n ".join(mapped_keys)
211 print "for OTA package verification"
212 else:
213 mapped_keys.append(
214 OPTIONS.key_map["build/target/product/security/testkey"] + ".x509.pem")
215 print "META/otakeys.txt has no keys; using", mapped_keys[0]
Doug Zongker8e931bf2009-04-06 15:21:45 -0700216
217 # recovery uses a version of the key that has been slightly
218 # predigested (by DumpPublicKey.java) and put in res/keys.
219
Doug Zongker602a84e2009-06-18 08:35:12 -0700220 p = common.Run(["java", "-jar",
221 os.path.join(OPTIONS.search_path, "framework", "dumpkey.jar")]
222 + mapped_keys,
Doug Zongker8e931bf2009-04-06 15:21:45 -0700223 stdout=subprocess.PIPE)
224 data, _ = p.communicate()
225 if p.returncode != 0:
226 raise ExternalError("failed to run dumpkeys")
Doug Zongker048e7ca2009-06-15 14:31:53 -0700227 common.ZipWriteStr(output_tf_zip, "RECOVERY/RAMDISK/res/keys", data)
Doug Zongker8e931bf2009-04-06 15:21:45 -0700228
229 # SystemUpdateActivity uses the x509.pem version of the keys, but
230 # put into a zipfile system/etc/security/otacerts.zip.
231
232 tempfile = cStringIO.StringIO()
233 certs_zip = zipfile.ZipFile(tempfile, "w")
234 for k in mapped_keys:
235 certs_zip.write(k)
236 certs_zip.close()
Doug Zongker048e7ca2009-06-15 14:31:53 -0700237 common.ZipWriteStr(output_tf_zip, "SYSTEM/etc/security/otacerts.zip",
238 tempfile.getvalue())
Doug Zongkereef39442009-04-02 12:14:19 -0700239
240
241def main(argv):
242
243 def option_handler(o, a):
Doug Zongker05d3dea2009-06-22 11:32:31 -0700244 if o in ("-e", "--extra_apks"):
Doug Zongkereef39442009-04-02 12:14:19 -0700245 names, key = a.split("=")
246 names = names.split(",")
247 for n in names:
248 OPTIONS.extra_apks[n] = key
249 elif o in ("-d", "--default_key_mappings"):
250 OPTIONS.key_map.update({
251 "build/target/product/security/testkey": "%s/releasekey" % (a,),
252 "build/target/product/security/media": "%s/media" % (a,),
253 "build/target/product/security/shared": "%s/shared" % (a,),
254 "build/target/product/security/platform": "%s/platform" % (a,),
255 })
256 elif o in ("-k", "--key_mapping"):
257 s, d = a.split("=")
258 OPTIONS.key_map[s] = d
Doug Zongker8e931bf2009-04-06 15:21:45 -0700259 elif o in ("-o", "--replace_ota_keys"):
260 OPTIONS.replace_ota_keys = True
Doug Zongkerae877012009-04-21 10:04:51 -0700261 elif o in ("-t", "--tag_changes"):
262 new = []
263 for i in a.split(","):
264 i = i.strip()
265 if not i or i[0] not in "-+":
266 raise ValueError("Bad tag change '%s'" % (i,))
267 new.append(i[0] + i[1:].strip())
268 OPTIONS.tag_changes = tuple(new)
Doug Zongkereef39442009-04-02 12:14:19 -0700269 else:
270 return False
271 return True
272
273 args = common.ParseOptions(argv, __doc__,
Doug Zongker05d3dea2009-06-22 11:32:31 -0700274 extra_opts="e:d:k:ot:",
275 extra_long_opts=["extra_apks=",
Doug Zongkereef39442009-04-02 12:14:19 -0700276 "default_key_mappings=",
Doug Zongker8e931bf2009-04-06 15:21:45 -0700277 "key_mapping=",
Doug Zongker17aa9442009-04-17 10:15:58 -0700278 "replace_ota_keys",
Doug Zongkerae877012009-04-21 10:04:51 -0700279 "tag_changes="],
Doug Zongkereef39442009-04-02 12:14:19 -0700280 extra_option_handler=option_handler)
281
282 if len(args) != 2:
283 common.Usage(__doc__)
284 sys.exit(1)
285
286 input_zip = zipfile.ZipFile(args[0], "r")
287 output_zip = zipfile.ZipFile(args[1], "w")
288
Doug Zongkereb338ef2009-05-20 16:50:49 -0700289 apk_key_map = GetApkCerts(input_zip)
290 CheckAllApksSigned(input_zip, apk_key_map)
Doug Zongkereb338ef2009-05-20 16:50:49 -0700291
292 key_passwords = common.GetKeyPasswords(set(apk_key_map.values()))
293 SignApks(input_zip, output_zip, apk_key_map, key_passwords)
Doug Zongkereef39442009-04-02 12:14:19 -0700294
Doug Zongker8e931bf2009-04-06 15:21:45 -0700295 if OPTIONS.replace_ota_keys:
296 ReplaceOtaKeys(input_zip, output_zip)
297
Doug Zongkereef39442009-04-02 12:14:19 -0700298 input_zip.close()
299 output_zip.close()
300
301 print "done."
302
303
304if __name__ == '__main__':
305 try:
306 main(sys.argv[1:])
307 except common.ExternalError, e:
308 print
309 print " ERROR: %s" % (e,)
310 print
311 sys.exit(1)