Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2016 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 | Verify a given OTA package with the specifed certificate. |
| 19 | """ |
| 20 | |
| 21 | from __future__ import print_function |
| 22 | |
| 23 | import argparse |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 24 | import re |
| 25 | import subprocess |
| 26 | import sys |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 27 | import zipfile |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 28 | |
| 29 | from hashlib import sha1 |
| 30 | from hashlib import sha256 |
| 31 | |
Tao Bao | 750385e | 2017-12-15 12:21:44 -0800 | [diff] [blame] | 32 | import common |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 33 | |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 34 | |
| 35 | def CertUsesSha256(cert): |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 36 | """Check if the cert uses SHA-256 hashing algorithm.""" |
| 37 | |
| 38 | cmd = ['openssl', 'x509', '-text', '-noout', '-in', cert] |
| 39 | p1 = common.Run(cmd, stdout=subprocess.PIPE) |
| 40 | cert_dump, _ = p1.communicate() |
| 41 | |
| 42 | algorithm = re.search(r'Signature Algorithm: ([a-zA-Z0-9]+)', cert_dump) |
| 43 | assert algorithm, "Failed to identify the signature algorithm." |
| 44 | |
| 45 | assert not algorithm.group(1).startswith('ecdsa'), ( |
| 46 | 'This script doesn\'t support verifying ECDSA signed package yet.') |
| 47 | |
| 48 | return algorithm.group(1).startswith('sha256') |
| 49 | |
| 50 | |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 51 | def VerifyPackage(cert, package): |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 52 | """Verify the given package with the certificate. |
| 53 | |
| 54 | (Comments from bootable/recovery/verifier.cpp:) |
| 55 | |
| 56 | An archive with a whole-file signature will end in six bytes: |
| 57 | |
| 58 | (2-byte signature start) $ff $ff (2-byte comment size) |
| 59 | |
| 60 | (As far as the ZIP format is concerned, these are part of the |
| 61 | archive comment.) We start by reading this footer, this tells |
| 62 | us how far back from the end we have to start reading to find |
| 63 | the whole comment. |
| 64 | """ |
| 65 | |
| 66 | print('Package: %s' % (package,)) |
| 67 | print('Certificate: %s' % (cert,)) |
| 68 | |
| 69 | # Read in the package. |
| 70 | with open(package) as package_file: |
| 71 | package_bytes = package_file.read() |
| 72 | |
| 73 | length = len(package_bytes) |
| 74 | assert length >= 6, "Not big enough to contain footer." |
| 75 | |
| 76 | footer = [ord(x) for x in package_bytes[-6:]] |
| 77 | assert footer[2] == 0xff and footer[3] == 0xff, "Footer is wrong." |
| 78 | |
| 79 | signature_start_from_end = (footer[1] << 8) + footer[0] |
| 80 | assert signature_start_from_end > 6, "Signature start is in the footer." |
| 81 | |
| 82 | signature_start = length - signature_start_from_end |
| 83 | |
| 84 | # Determine how much of the file is covered by the signature. This is |
| 85 | # everything except the signature data and length, which includes all of the |
| 86 | # EOCD except for the comment length field (2 bytes) and the comment data. |
| 87 | comment_len = (footer[5] << 8) + footer[4] |
| 88 | signed_len = length - comment_len - 2 |
| 89 | |
| 90 | print('Package length: %d' % (length,)) |
| 91 | print('Comment length: %d' % (comment_len,)) |
| 92 | print('Signed data length: %d' % (signed_len,)) |
| 93 | print('Signature start: %d' % (signature_start,)) |
| 94 | |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 95 | use_sha256 = CertUsesSha256(cert) |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 96 | print('Use SHA-256: %s' % (use_sha256,)) |
| 97 | |
Tao Bao | 750385e | 2017-12-15 12:21:44 -0800 | [diff] [blame] | 98 | h = sha256() if use_sha256 else sha1() |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 99 | h.update(package_bytes[:signed_len]) |
| 100 | package_digest = h.hexdigest().lower() |
| 101 | |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 102 | print('Digest: %s' % (package_digest,)) |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 103 | |
| 104 | # Get the signature from the input package. |
| 105 | signature = package_bytes[signature_start:-6] |
Tao Bao | 4c851b1 | 2016-09-19 13:54:38 -0700 | [diff] [blame] | 106 | sig_file = common.MakeTempFile(prefix='sig-') |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 107 | with open(sig_file, 'wb') as f: |
| 108 | f.write(signature) |
| 109 | |
| 110 | # Parse the signature and get the hash. |
| 111 | cmd = ['openssl', 'asn1parse', '-inform', 'DER', '-in', sig_file] |
| 112 | p1 = common.Run(cmd, stdout=subprocess.PIPE) |
| 113 | sig, _ = p1.communicate() |
| 114 | assert p1.returncode == 0, "Failed to parse the signature." |
| 115 | |
| 116 | digest_line = sig.strip().split('\n')[-1] |
| 117 | digest_string = digest_line.split(':')[3] |
Tao Bao | 4c851b1 | 2016-09-19 13:54:38 -0700 | [diff] [blame] | 118 | digest_file = common.MakeTempFile(prefix='digest-') |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 119 | with open(digest_file, 'wb') as f: |
| 120 | f.write(digest_string.decode('hex')) |
| 121 | |
| 122 | # Verify the digest by outputing the decrypted result in ASN.1 structure. |
Tao Bao | 4c851b1 | 2016-09-19 13:54:38 -0700 | [diff] [blame] | 123 | decrypted_file = common.MakeTempFile(prefix='decrypted-') |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 124 | cmd = ['openssl', 'rsautl', '-verify', '-certin', '-inkey', cert, |
| 125 | '-in', digest_file, '-out', decrypted_file] |
| 126 | p1 = common.Run(cmd, stdout=subprocess.PIPE) |
| 127 | p1.communicate() |
| 128 | assert p1.returncode == 0, "Failed to run openssl rsautl -verify." |
| 129 | |
| 130 | # Parse the output ASN.1 structure. |
| 131 | cmd = ['openssl', 'asn1parse', '-inform', 'DER', '-in', decrypted_file] |
| 132 | p1 = common.Run(cmd, stdout=subprocess.PIPE) |
| 133 | decrypted_output, _ = p1.communicate() |
| 134 | assert p1.returncode == 0, "Failed to parse the output." |
| 135 | |
| 136 | digest_line = decrypted_output.strip().split('\n')[-1] |
| 137 | digest_string = digest_line.split(':')[3].lower() |
| 138 | |
| 139 | # Verify that the two digest strings match. |
| 140 | assert package_digest == digest_string, "Verification failed." |
| 141 | |
| 142 | # Verified successfully upon reaching here. |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 143 | print('\nWhole package signature VERIFIED\n') |
| 144 | |
| 145 | |
| 146 | def VerifyAbOtaPayload(cert, package): |
| 147 | """Verifies the payload and metadata signatures in an A/B OTA payload.""" |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 148 | package_zip = zipfile.ZipFile(package, 'r') |
| 149 | if 'payload.bin' not in package_zip.namelist(): |
| 150 | common.ZipClose(package_zip) |
| 151 | return |
| 152 | |
| 153 | print('Verifying A/B OTA payload signatures...') |
| 154 | |
Tao Bao | 750385e | 2017-12-15 12:21:44 -0800 | [diff] [blame] | 155 | # Dump pubkey from the certificate. |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 156 | pubkey = common.MakeTempFile(prefix="key-", suffix=".pem") |
| 157 | with open(pubkey, 'wb') as pubkey_fp: |
| 158 | pubkey_fp.write(common.ExtractPublicKey(cert)) |
Tao Bao | 750385e | 2017-12-15 12:21:44 -0800 | [diff] [blame] | 159 | |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 160 | package_dir = common.MakeTempDir(prefix='package-') |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 161 | |
Tao Bao | 750385e | 2017-12-15 12:21:44 -0800 | [diff] [blame] | 162 | # Signature verification with delta_generator. |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 163 | payload_file = package_zip.extract('payload.bin', package_dir) |
Tao Bao | 750385e | 2017-12-15 12:21:44 -0800 | [diff] [blame] | 164 | cmd = ['delta_generator', |
| 165 | '--in_file=' + payload_file, |
| 166 | '--public_key=' + pubkey] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame^] | 167 | proc = common.Run(cmd) |
Tao Bao | 750385e | 2017-12-15 12:21:44 -0800 | [diff] [blame] | 168 | stdoutdata, _ = proc.communicate() |
| 169 | assert proc.returncode == 0, \ |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame^] | 170 | 'Failed to verify payload with delta_generator: {}\n{}'.format( |
| 171 | package, stdoutdata) |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 172 | common.ZipClose(package_zip) |
| 173 | |
| 174 | # Verified successfully upon reaching here. |
| 175 | print('\nPayload signatures VERIFIED\n\n') |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 176 | |
| 177 | |
| 178 | def main(): |
| 179 | parser = argparse.ArgumentParser() |
| 180 | parser.add_argument('certificate', help='The certificate to be used.') |
| 181 | parser.add_argument('package', help='The OTA package to be verified.') |
| 182 | args = parser.parse_args() |
| 183 | |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 184 | VerifyPackage(args.certificate, args.package) |
| 185 | VerifyAbOtaPayload(args.certificate, args.package) |
Tao Bao | 9c63fb5 | 2016-09-13 11:13:48 -0700 | [diff] [blame] | 186 | |
| 187 | |
| 188 | if __name__ == '__main__': |
| 189 | try: |
| 190 | main() |
| 191 | except AssertionError as err: |
| 192 | print('\n ERROR: %s\n' % (err,)) |
| 193 | sys.exit(1) |
Tao Bao | a198b1e | 2017-08-31 16:52:55 -0700 | [diff] [blame] | 194 | finally: |
| 195 | common.Cleanup() |