blob: b0128dc10e3033197a0fc934882a6a6e02755b33 [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.
Tao Baoe7354ba2019-05-09 16:54:15 -070038"""
39
40import logging
41import shutil
42import sys
43
44import apex_utils
45import common
46
47logger = logging.getLogger(__name__)
48
49
Tianjie Xu88a759d2020-01-23 10:47:54 -080050def SignApexFile(avbtool, apex_file, payload_key, container_key, no_hashtree,
51 apk_keys=None, signing_args=None):
Tao Baoc9c1b862019-06-26 14:54:14 -070052 """Signs the given apex file."""
53 with open(apex_file, 'rb') as input_fp:
54 apex_data = input_fp.read()
55
56 return apex_utils.SignApex(
Tao Bao1ac886e2019-06-26 11:58:22 -070057 avbtool,
Tao Baoc9c1b862019-06-26 14:54:14 -070058 apex_data,
59 payload_key=payload_key,
60 container_key=container_key,
61 container_pw=None,
62 codename_to_api_level_map=None,
Baligh Uddinac936fd2019-12-04 08:30:32 -080063 no_hashtree=no_hashtree,
Tianjie Xu88a759d2020-01-23 10:47:54 -080064 apk_keys=apk_keys,
Tao Baoc9c1b862019-06-26 14:54:14 -070065 signing_args=signing_args)
66
67
Tao Baoe7354ba2019-05-09 16:54:15 -070068def main(argv):
69
70 options = {}
71
72 def option_handler(o, a):
Tao Bao1ac886e2019-06-26 11:58:22 -070073 if o == '--avbtool':
74 options['avbtool'] = a
75 elif o == '--container_key':
Tao Baoe7354ba2019-05-09 16:54:15 -070076 # Strip the suffix if any, as common.SignFile expects no suffix.
77 DEFAULT_CONTAINER_KEY_SUFFIX = '.x509.pem'
78 if a.endswith(DEFAULT_CONTAINER_KEY_SUFFIX):
79 a = a[:-len(DEFAULT_CONTAINER_KEY_SUFFIX)]
80 options['container_key'] = a
81 elif o == '--payload_key':
82 options['payload_key'] = a
83 elif o == '--payload_extra_args':
84 options['payload_extra_args'] = a
Tianjie Xu88a759d2020-01-23 10:47:54 -080085 elif o in ("-e", "--extra_apks"):
86 names, key = a.split("=")
87 names = names.split(",")
88 for n in names:
89 if 'extra_apks' not in options:
90 options['extra_apks'] = {}
91 options['extra_apks'].update({n: key})
Tao Baoe7354ba2019-05-09 16:54:15 -070092 else:
93 return False
94 return True
95
96 args = common.ParseOptions(
97 argv, __doc__,
Tianjie Xu88a759d2020-01-23 10:47:54 -080098 extra_opts='e:',
Tao Baoe7354ba2019-05-09 16:54:15 -070099 extra_long_opts=[
Tao Bao1ac886e2019-06-26 11:58:22 -0700100 'avbtool=',
Tao Baoe7354ba2019-05-09 16:54:15 -0700101 'container_key=',
102 'payload_extra_args=',
103 'payload_key=',
Tianjie Xu88a759d2020-01-23 10:47:54 -0800104 'extra_apks=',
Tao Baoe7354ba2019-05-09 16:54:15 -0700105 ],
106 extra_option_handler=option_handler)
107
108 if (len(args) != 2 or 'container_key' not in options or
109 'payload_key' not in options):
110 common.Usage(__doc__)
111 sys.exit(1)
112
113 common.InitLogging()
114
Tao Baoc9c1b862019-06-26 14:54:14 -0700115 signed_apex = SignApexFile(
Tao Bao1ac886e2019-06-26 11:58:22 -0700116 options.get('avbtool', 'avbtool'),
Tao Baoc9c1b862019-06-26 14:54:14 -0700117 args[0],
118 options['payload_key'],
119 options['container_key'],
Tao Bao448004a2019-09-19 07:55:02 -0700120 no_hashtree=False,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800121 apk_keys=options.get('extra_apks', {}),
Tao Bao448004a2019-09-19 07:55:02 -0700122 signing_args=options.get('payload_extra_args'))
Tao Baoc9c1b862019-06-26 14:54:14 -0700123 shutil.copyfile(signed_apex, args[1])
Tao Baoe7354ba2019-05-09 16:54:15 -0700124 logger.info("done.")
125
126
127if __name__ == '__main__':
128 try:
129 main(sys.argv[1:])
130 except common.ExternalError:
131 logger.exception("\n ERROR:\n")
132 sys.exit(1)
133 finally:
134 common.Cleanup()