blob: a5807095d07d4494137297903c3b6000b6d98903 [file] [log] [blame]
Tao Bao9c63fb52016-09-13 11:13:48 -07001#!/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"""
18Verify a given OTA package with the specifed certificate.
19"""
20
21from __future__ import print_function
22
23import argparse
Tao Bao9c63fb52016-09-13 11:13:48 -070024import re
25import subprocess
26import sys
Tao Baoa198b1e2017-08-31 16:52:55 -070027import zipfile
Tao Bao9c63fb52016-09-13 11:13:48 -070028
29from hashlib import sha1
30from hashlib import sha256
31
Tao Bao750385e2017-12-15 12:21:44 -080032import common
Tao Bao9c63fb52016-09-13 11:13:48 -070033
Tao Baoa198b1e2017-08-31 16:52:55 -070034
35def CertUsesSha256(cert):
Tao Bao9c63fb52016-09-13 11:13:48 -070036 """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 Baoa198b1e2017-08-31 16:52:55 -070051def VerifyPackage(cert, package):
Tao Bao9c63fb52016-09-13 11:13:48 -070052 """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 Baoa198b1e2017-08-31 16:52:55 -070095 use_sha256 = CertUsesSha256(cert)
Tao Bao9c63fb52016-09-13 11:13:48 -070096 print('Use SHA-256: %s' % (use_sha256,))
97
Tao Bao750385e2017-12-15 12:21:44 -080098 h = sha256() if use_sha256 else sha1()
Tao Bao9c63fb52016-09-13 11:13:48 -070099 h.update(package_bytes[:signed_len])
100 package_digest = h.hexdigest().lower()
101
Tao Baoa198b1e2017-08-31 16:52:55 -0700102 print('Digest: %s' % (package_digest,))
Tao Bao9c63fb52016-09-13 11:13:48 -0700103
104 # Get the signature from the input package.
105 signature = package_bytes[signature_start:-6]
Tao Bao4c851b12016-09-19 13:54:38 -0700106 sig_file = common.MakeTempFile(prefix='sig-')
Tao Bao9c63fb52016-09-13 11:13:48 -0700107 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 Bao4c851b12016-09-19 13:54:38 -0700118 digest_file = common.MakeTempFile(prefix='digest-')
Tao Bao9c63fb52016-09-13 11:13:48 -0700119 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 Bao4c851b12016-09-19 13:54:38 -0700123 decrypted_file = common.MakeTempFile(prefix='decrypted-')
Tao Bao9c63fb52016-09-13 11:13:48 -0700124 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 Baoa198b1e2017-08-31 16:52:55 -0700143 print('\nWhole package signature VERIFIED\n')
144
145
146def VerifyAbOtaPayload(cert, package):
147 """Verifies the payload and metadata signatures in an A/B OTA payload."""
Tao Baoa198b1e2017-08-31 16:52:55 -0700148 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 Bao750385e2017-12-15 12:21:44 -0800155 # Dump pubkey from the certificate.
Tao Bao04e1f012018-02-04 12:13:35 -0800156 pubkey = common.MakeTempFile(prefix="key-", suffix=".pem")
157 with open(pubkey, 'wb') as pubkey_fp:
158 pubkey_fp.write(common.ExtractPublicKey(cert))
Tao Bao750385e2017-12-15 12:21:44 -0800159
Tao Bao04e1f012018-02-04 12:13:35 -0800160 package_dir = common.MakeTempDir(prefix='package-')
Tao Baoa198b1e2017-08-31 16:52:55 -0700161
Tao Bao750385e2017-12-15 12:21:44 -0800162 # Signature verification with delta_generator.
Tao Baoa198b1e2017-08-31 16:52:55 -0700163 payload_file = package_zip.extract('payload.bin', package_dir)
Tao Bao750385e2017-12-15 12:21:44 -0800164 cmd = ['delta_generator',
165 '--in_file=' + payload_file,
166 '--public_key=' + pubkey]
Tao Bao73dd4f42018-10-04 16:25:33 -0700167 proc = common.Run(cmd)
Tao Bao750385e2017-12-15 12:21:44 -0800168 stdoutdata, _ = proc.communicate()
169 assert proc.returncode == 0, \
Tao Bao73dd4f42018-10-04 16:25:33 -0700170 'Failed to verify payload with delta_generator: {}\n{}'.format(
171 package, stdoutdata)
Tao Baoa198b1e2017-08-31 16:52:55 -0700172 common.ZipClose(package_zip)
173
174 # Verified successfully upon reaching here.
175 print('\nPayload signatures VERIFIED\n\n')
Tao Bao9c63fb52016-09-13 11:13:48 -0700176
177
178def 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 Baoa198b1e2017-08-31 16:52:55 -0700184 VerifyPackage(args.certificate, args.package)
185 VerifyAbOtaPayload(args.certificate, args.package)
Tao Bao9c63fb52016-09-13 11:13:48 -0700186
187
188if __name__ == '__main__':
189 try:
190 main()
191 except AssertionError as err:
192 print('\n ERROR: %s\n' % (err,))
193 sys.exit(1)
Tao Baoa198b1e2017-08-31 16:52:55 -0700194 finally:
195 common.Cleanup()