Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 1 | #!/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 | """ |
| 18 | Signs a standalone APEX file. |
| 19 | |
| 20 | Usage: sign_apex [flags] input_apex_file output_apex_file |
| 21 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame^] | 22 | --avbtool <avbtool> |
| 23 | Optional flag that specifies the AVB tool to use. Defaults to `avbtool`. |
| 24 | |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 25 | --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 | |
| 36 | import logging |
| 37 | import shutil |
| 38 | import sys |
| 39 | |
| 40 | import apex_utils |
| 41 | import common |
| 42 | |
| 43 | logger = logging.getLogger(__name__) |
| 44 | |
| 45 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame^] | 46 | def SignApexFile(avbtool, apex_file, payload_key, container_key, |
| 47 | signing_args=None): |
Tao Bao | c9c1b86 | 2019-06-26 14:54:14 -0700 | [diff] [blame] | 48 | """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 Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame^] | 53 | avbtool, |
Tao Bao | c9c1b86 | 2019-06-26 14:54:14 -0700 | [diff] [blame] | 54 | 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 Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 62 | def main(argv): |
| 63 | |
| 64 | options = {} |
| 65 | |
| 66 | def option_handler(o, a): |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame^] | 67 | if o == '--avbtool': |
| 68 | options['avbtool'] = a |
| 69 | elif o == '--container_key': |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 70 | # 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 Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame^] | 87 | 'avbtool=', |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 88 | '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 Bao | c9c1b86 | 2019-06-26 14:54:14 -0700 | [diff] [blame] | 101 | signed_apex = SignApexFile( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame^] | 102 | options.get('avbtool', 'avbtool'), |
Tao Bao | c9c1b86 | 2019-06-26 14:54:14 -0700 | [diff] [blame] | 103 | args[0], |
| 104 | options['payload_key'], |
| 105 | options['container_key'], |
| 106 | options.get('payload_extra_args')) |
| 107 | shutil.copyfile(signed_apex, args[1]) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 108 | logger.info("done.") |
| 109 | |
| 110 | |
| 111 | if __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() |