blob: d3e242b34b87ce2483611c0a04d2deef644de53a [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.
Tao Baoe7354ba2019-05-09 16:54:15 -070045"""
46
47import logging
48import shutil
49import sys
50
51import apex_utils
52import common
53
54logger = logging.getLogger(__name__)
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020055OPTIONS = common.OPTIONS
Tao Baoe7354ba2019-05-09 16:54:15 -070056
57
Tianjie Xu88a759d2020-01-23 10:47:54 -080058def SignApexFile(avbtool, apex_file, payload_key, container_key, no_hashtree,
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +000059 apk_keys=None, signing_args=None, codename_to_api_level_map=None, sign_tool=None):
Tao Baoc9c1b862019-06-26 14:54:14 -070060 """Signs the given apex file."""
61 with open(apex_file, 'rb') as input_fp:
62 apex_data = input_fp.read()
63
64 return apex_utils.SignApex(
Tao Bao1ac886e2019-06-26 11:58:22 -070065 avbtool,
Tao Baoc9c1b862019-06-26 14:54:14 -070066 apex_data,
67 payload_key=payload_key,
68 container_key=container_key,
69 container_pw=None,
Baligh Uddind9fcafd2020-04-25 09:03:57 -070070 codename_to_api_level_map=codename_to_api_level_map,
Baligh Uddinac936fd2019-12-04 08:30:32 -080071 no_hashtree=no_hashtree,
Tianjie Xu88a759d2020-01-23 10:47:54 -080072 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +090073 signing_args=signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020074 sign_tool=sign_tool,
75 is_sepolicy=apex_file.endswith(OPTIONS.sepolicy_name))
Tao Baoc9c1b862019-06-26 14:54:14 -070076
77
Tao Baoe7354ba2019-05-09 16:54:15 -070078def main(argv):
79
80 options = {}
81
82 def option_handler(o, a):
Tao Bao1ac886e2019-06-26 11:58:22 -070083 if o == '--avbtool':
84 options['avbtool'] = a
85 elif o == '--container_key':
Tao Baoe7354ba2019-05-09 16:54:15 -070086 # Strip the suffix if any, as common.SignFile expects no suffix.
87 DEFAULT_CONTAINER_KEY_SUFFIX = '.x509.pem'
88 if a.endswith(DEFAULT_CONTAINER_KEY_SUFFIX):
89 a = a[:-len(DEFAULT_CONTAINER_KEY_SUFFIX)]
90 options['container_key'] = a
91 elif o == '--payload_key':
92 options['payload_key'] = a
93 elif o == '--payload_extra_args':
94 options['payload_extra_args'] = a
Baligh Uddind9fcafd2020-04-25 09:03:57 -070095 elif o == '--codename_to_api_level_map':
96 versions = a.split(",")
97 for v in versions:
98 key, value = v.split(":")
99 if 'codename_to_api_level_map' not in options:
100 options['codename_to_api_level_map'] = {}
101 options['codename_to_api_level_map'].update({key: value})
Tianjie Xu88a759d2020-01-23 10:47:54 -0800102 elif o in ("-e", "--extra_apks"):
103 names, key = a.split("=")
104 names = names.split(",")
105 for n in names:
106 if 'extra_apks' not in options:
107 options['extra_apks'] = {}
108 options['extra_apks'].update({n: key})
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900109 elif o == '--sign_tool':
110 options['sign_tool'] = a
Tao Baoe7354ba2019-05-09 16:54:15 -0700111 else:
112 return False
113 return True
114
115 args = common.ParseOptions(
116 argv, __doc__,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800117 extra_opts='e:',
Tao Baoe7354ba2019-05-09 16:54:15 -0700118 extra_long_opts=[
Tao Bao1ac886e2019-06-26 11:58:22 -0700119 'avbtool=',
Baligh Uddind9fcafd2020-04-25 09:03:57 -0700120 'codename_to_api_level_map=',
Tao Baoe7354ba2019-05-09 16:54:15 -0700121 'container_key=',
122 'payload_extra_args=',
123 'payload_key=',
Tianjie Xu88a759d2020-01-23 10:47:54 -0800124 'extra_apks=',
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900125 'sign_tool=',
Tao Baoe7354ba2019-05-09 16:54:15 -0700126 ],
127 extra_option_handler=option_handler)
128
129 if (len(args) != 2 or 'container_key' not in options or
130 'payload_key' not in options):
131 common.Usage(__doc__)
132 sys.exit(1)
133
134 common.InitLogging()
135
Tao Baoc9c1b862019-06-26 14:54:14 -0700136 signed_apex = SignApexFile(
Tao Bao1ac886e2019-06-26 11:58:22 -0700137 options.get('avbtool', 'avbtool'),
Tao Baoc9c1b862019-06-26 14:54:14 -0700138 args[0],
139 options['payload_key'],
140 options['container_key'],
Tao Bao448004a2019-09-19 07:55:02 -0700141 no_hashtree=False,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800142 apk_keys=options.get('extra_apks', {}),
Baligh Uddind9fcafd2020-04-25 09:03:57 -0700143 signing_args=options.get('payload_extra_args'),
144 codename_to_api_level_map=options.get(
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900145 'codename_to_api_level_map', {}),
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +0000146 sign_tool=options.get('sign_tool', None))
Tao Baoc9c1b862019-06-26 14:54:14 -0700147 shutil.copyfile(signed_apex, args[1])
Tao Baoe7354ba2019-05-09 16:54:15 -0700148 logger.info("done.")
149
150
151if __name__ == '__main__':
152 try:
153 main(sys.argv[1:])
Tao Baoe7354ba2019-05-09 16:54:15 -0700154 finally:
155 common.Cleanup()