blob: a68f1eca4aa2312145af2470e33d70057cf6f1fa [file] [log] [blame]
Tao Baoe7354ba2019-05-09 16:54:15 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2019 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 a standalone APEX file.
19
20Usage: sign_apex [flags] input_apex_file output_apex_file
21
Tao Bao1ac886e2019-06-26 11:58:22 -070022 --avbtool <avbtool>
23 Optional flag that specifies the AVB tool to use. Defaults to `avbtool`.
24
Tao Baoe7354ba2019-05-09 16:54:15 -070025 --container_key <key>
26 Mandatory flag that specifies the container signing key.
27
28 --payload_key <key>
29 Mandatory flag that specifies the payload signing key.
30
31 --payload_extra_args <args>
32 Optional flag that specifies any extra args to be passed to payload signer
33 (e.g. --payload_extra_args="--signing_helper_with_files /path/to/helper").
Tianjie Xu88a759d2020-01-23 10:47:54 -080034
35 -e (--extra_apks) <name,name,...=key>
36 Add extra APK name/key pairs. This is useful to sign the apk files in the
37 apex payload image.
Baligh Uddind9fcafd2020-04-25 09:03:57 -070038
39 --codename_to_api_level_map Q:29,R:30,...
40 A Mapping of codename to api level. This is useful to provide sdk targeting
41 information to APK Signer.
Jooyung Han0f5a41d2021-10-27 03:53:21 +090042
43 --sign_tool <sign_tool>
44 Optional flag that specifies a custom signing tool for the contents of the apex.
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +010045
46 --sepolicy_key <key>
47 Optional flag that specifies the sepolicy signing key, defaults to payload_key.
48
49 --sepolicy_cert <cert>
50 Optional flag that specifies the sepolicy signing cert.
51
52 --fsverity_tool <path>
53 Optional flag that specifies the path to fsverity tool to sign SEPolicy, defaults to fsverity.
Tao Baoe7354ba2019-05-09 16:54:15 -070054"""
55
56import logging
57import shutil
58import sys
59
60import apex_utils
61import common
62
63logger = logging.getLogger(__name__)
Melisa Carranza Zunigae6d4fb52022-03-07 14:56:26 +010064OPTIONS = common.OPTIONS
Tao Baoe7354ba2019-05-09 16:54:15 -070065
66
Tianjie Xu88a759d2020-01-23 10:47:54 -080067def SignApexFile(avbtool, apex_file, payload_key, container_key, no_hashtree,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +010068 apk_keys=None, signing_args=None, codename_to_api_level_map=None, sign_tool=None,
69 sepolicy_key=None, sepolicy_cert=None, fsverity_tool=None):
Tao Baoc9c1b862019-06-26 14:54:14 -070070 """Signs the given apex file."""
71 with open(apex_file, 'rb') as input_fp:
72 apex_data = input_fp.read()
73
74 return apex_utils.SignApex(
Tao Bao1ac886e2019-06-26 11:58:22 -070075 avbtool,
Tao Baoc9c1b862019-06-26 14:54:14 -070076 apex_data,
77 payload_key=payload_key,
78 container_key=container_key,
79 container_pw=None,
Baligh Uddind9fcafd2020-04-25 09:03:57 -070080 codename_to_api_level_map=codename_to_api_level_map,
Baligh Uddinac936fd2019-12-04 08:30:32 -080081 no_hashtree=no_hashtree,
Tianjie Xu88a759d2020-01-23 10:47:54 -080082 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +090083 signing_args=signing_args,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +010084 sign_tool=sign_tool,
Melisa Carranza Zunigae6d4fb52022-03-07 14:56:26 +010085 is_sepolicy=apex_file.endswith(OPTIONS.sepolicy_name),
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +010086 sepolicy_key=sepolicy_key,
87 sepolicy_cert=sepolicy_cert,
88 fsverity_tool=fsverity_tool)
Tao Baoc9c1b862019-06-26 14:54:14 -070089
90
Tao Baoe7354ba2019-05-09 16:54:15 -070091def main(argv):
92
93 options = {}
94
95 def option_handler(o, a):
Tao Bao1ac886e2019-06-26 11:58:22 -070096 if o == '--avbtool':
97 options['avbtool'] = a
98 elif o == '--container_key':
Tao Baoe7354ba2019-05-09 16:54:15 -070099 # Strip the suffix if any, as common.SignFile expects no suffix.
100 DEFAULT_CONTAINER_KEY_SUFFIX = '.x509.pem'
101 if a.endswith(DEFAULT_CONTAINER_KEY_SUFFIX):
102 a = a[:-len(DEFAULT_CONTAINER_KEY_SUFFIX)]
103 options['container_key'] = a
104 elif o == '--payload_key':
105 options['payload_key'] = a
106 elif o == '--payload_extra_args':
107 options['payload_extra_args'] = a
Baligh Uddind9fcafd2020-04-25 09:03:57 -0700108 elif o == '--codename_to_api_level_map':
109 versions = a.split(",")
110 for v in versions:
111 key, value = v.split(":")
112 if 'codename_to_api_level_map' not in options:
113 options['codename_to_api_level_map'] = {}
114 options['codename_to_api_level_map'].update({key: value})
Tianjie Xu88a759d2020-01-23 10:47:54 -0800115 elif o in ("-e", "--extra_apks"):
116 names, key = a.split("=")
117 names = names.split(",")
118 for n in names:
119 if 'extra_apks' not in options:
120 options['extra_apks'] = {}
121 options['extra_apks'].update({n: key})
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900122 elif o == '--sign_tool':
123 options['sign_tool'] = a
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100124 elif o == '--sepolicy_key':
125 options['sepolicy_key'] = a
126 elif o == '--sepolicy_cert':
127 options['sepolicy_cert'] = a
128 elif o == '--fsverity_tool':
129 options['fsverity_tool'] = a
Tao Baoe7354ba2019-05-09 16:54:15 -0700130 else:
131 return False
132 return True
133
134 args = common.ParseOptions(
135 argv, __doc__,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800136 extra_opts='e:',
Tao Baoe7354ba2019-05-09 16:54:15 -0700137 extra_long_opts=[
Tao Bao1ac886e2019-06-26 11:58:22 -0700138 'avbtool=',
Baligh Uddind9fcafd2020-04-25 09:03:57 -0700139 'codename_to_api_level_map=',
Tao Baoe7354ba2019-05-09 16:54:15 -0700140 'container_key=',
141 'payload_extra_args=',
142 'payload_key=',
Tianjie Xu88a759d2020-01-23 10:47:54 -0800143 'extra_apks=',
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900144 'sign_tool=',
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100145 'sepolicy_key=',
146 'sepolicy_cert=',
147 'fsverity_tool='
Tao Baoe7354ba2019-05-09 16:54:15 -0700148 ],
149 extra_option_handler=option_handler)
150
151 if (len(args) != 2 or 'container_key' not in options or
152 'payload_key' not in options):
153 common.Usage(__doc__)
154 sys.exit(1)
155
156 common.InitLogging()
157
Tao Baoc9c1b862019-06-26 14:54:14 -0700158 signed_apex = SignApexFile(
Tao Bao1ac886e2019-06-26 11:58:22 -0700159 options.get('avbtool', 'avbtool'),
Tao Baoc9c1b862019-06-26 14:54:14 -0700160 args[0],
161 options['payload_key'],
162 options['container_key'],
Tao Bao448004a2019-09-19 07:55:02 -0700163 no_hashtree=False,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800164 apk_keys=options.get('extra_apks', {}),
Baligh Uddind9fcafd2020-04-25 09:03:57 -0700165 signing_args=options.get('payload_extra_args'),
166 codename_to_api_level_map=options.get(
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900167 'codename_to_api_level_map', {}),
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100168 sign_tool=options.get('sign_tool', None),
169 sepolicy_key=options.get('sepolicy_key', None),
170 sepolicy_cert=options.get('sepolicy_cert', None),
171 fsverity_tool=options.get('fsverity_tool', None))
Tao Baoc9c1b862019-06-26 14:54:14 -0700172 shutil.copyfile(signed_apex, args[1])
Tao Baoe7354ba2019-05-09 16:54:15 -0700173 logger.info("done.")
174
175
176if __name__ == '__main__':
177 try:
178 main(sys.argv[1:])
Tao Baoe7354ba2019-05-09 16:54:15 -0700179 finally:
180 common.Cleanup()