blob: 1778615a64587421a7b3f29a2c224d14510afdc2 [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
22 --container_key <key>
23 Mandatory flag that specifies the container signing key.
24
25 --payload_key <key>
26 Mandatory flag that specifies the payload signing key.
27
28 --payload_extra_args <args>
29 Optional flag that specifies any extra args to be passed to payload signer
30 (e.g. --payload_extra_args="--signing_helper_with_files /path/to/helper").
31"""
32
33import logging
34import shutil
35import sys
36
37import apex_utils
38import common
39
40logger = logging.getLogger(__name__)
41
42
43def main(argv):
44
45 options = {}
46
47 def option_handler(o, a):
48 if o == '--container_key':
49 # Strip the suffix if any, as common.SignFile expects no suffix.
50 DEFAULT_CONTAINER_KEY_SUFFIX = '.x509.pem'
51 if a.endswith(DEFAULT_CONTAINER_KEY_SUFFIX):
52 a = a[:-len(DEFAULT_CONTAINER_KEY_SUFFIX)]
53 options['container_key'] = a
54 elif o == '--payload_key':
55 options['payload_key'] = a
56 elif o == '--payload_extra_args':
57 options['payload_extra_args'] = a
58 else:
59 return False
60 return True
61
62 args = common.ParseOptions(
63 argv, __doc__,
64 extra_opts='',
65 extra_long_opts=[
66 'container_key=',
67 'payload_extra_args=',
68 'payload_key=',
69 ],
70 extra_option_handler=option_handler)
71
72 if (len(args) != 2 or 'container_key' not in options or
73 'payload_key' not in options):
74 common.Usage(__doc__)
75 sys.exit(1)
76
77 common.InitLogging()
78
79 input_zip = args[0]
80 output_zip = args[1]
81 with open(input_zip) as input_fp:
82 apex_data = input_fp.read()
83
84 signed_apex = apex_utils.SignApex(
85 apex_data,
86 payload_key=options['payload_key'],
87 container_key=options['container_key'],
88 container_pw=None,
89 codename_to_api_level_map=None,
90 signing_args=options.get('payload_extra_args'))
91
92 shutil.copyfile(signed_apex, output_zip)
93 logger.info("done.")
94
95
96if __name__ == '__main__':
97 try:
98 main(sys.argv[1:])
99 except common.ExternalError:
100 logger.exception("\n ERROR:\n")
101 sys.exit(1)
102 finally:
103 common.Cleanup()