blob: 2516e15ca4b9852ce476c70a48f25e9027346a91 [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").
34"""
35
36import logging
37import shutil
38import sys
39
40import apex_utils
41import common
42
43logger = logging.getLogger(__name__)
44
45
Tao Bao1ac886e2019-06-26 11:58:22 -070046def SignApexFile(avbtool, apex_file, payload_key, container_key,
47 signing_args=None):
Tao Baoc9c1b862019-06-26 14:54:14 -070048 """Signs the given apex file."""
49 with open(apex_file, 'rb') as input_fp:
50 apex_data = input_fp.read()
51
52 return apex_utils.SignApex(
Tao Bao1ac886e2019-06-26 11:58:22 -070053 avbtool,
Tao Baoc9c1b862019-06-26 14:54:14 -070054 apex_data,
55 payload_key=payload_key,
56 container_key=container_key,
57 container_pw=None,
58 codename_to_api_level_map=None,
59 signing_args=signing_args)
60
61
Tao Baoe7354ba2019-05-09 16:54:15 -070062def main(argv):
63
64 options = {}
65
66 def option_handler(o, a):
Tao Bao1ac886e2019-06-26 11:58:22 -070067 if o == '--avbtool':
68 options['avbtool'] = a
69 elif o == '--container_key':
Tao Baoe7354ba2019-05-09 16:54:15 -070070 # Strip the suffix if any, as common.SignFile expects no suffix.
71 DEFAULT_CONTAINER_KEY_SUFFIX = '.x509.pem'
72 if a.endswith(DEFAULT_CONTAINER_KEY_SUFFIX):
73 a = a[:-len(DEFAULT_CONTAINER_KEY_SUFFIX)]
74 options['container_key'] = a
75 elif o == '--payload_key':
76 options['payload_key'] = a
77 elif o == '--payload_extra_args':
78 options['payload_extra_args'] = a
79 else:
80 return False
81 return True
82
83 args = common.ParseOptions(
84 argv, __doc__,
85 extra_opts='',
86 extra_long_opts=[
Tao Bao1ac886e2019-06-26 11:58:22 -070087 'avbtool=',
Tao Baoe7354ba2019-05-09 16:54:15 -070088 'container_key=',
89 'payload_extra_args=',
90 'payload_key=',
91 ],
92 extra_option_handler=option_handler)
93
94 if (len(args) != 2 or 'container_key' not in options or
95 'payload_key' not in options):
96 common.Usage(__doc__)
97 sys.exit(1)
98
99 common.InitLogging()
100
Tao Baoc9c1b862019-06-26 14:54:14 -0700101 signed_apex = SignApexFile(
Tao Bao1ac886e2019-06-26 11:58:22 -0700102 options.get('avbtool', 'avbtool'),
Tao Baoc9c1b862019-06-26 14:54:14 -0700103 args[0],
104 options['payload_key'],
105 options['container_key'],
106 options.get('payload_extra_args'))
107 shutil.copyfile(signed_apex, args[1])
Tao Baoe7354ba2019-05-09 16:54:15 -0700108 logger.info("done.")
109
110
111if __name__ == '__main__':
112 try:
113 main(sys.argv[1:])
114 except common.ExternalError:
115 logger.exception("\n ERROR:\n")
116 sys.exit(1)
117 finally:
118 common.Cleanup()