2016-09-13 20:13:48 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Verify a given OTA package with the specifed certificate.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import argparse
|
2018-10-12 19:30:39 +02:00
|
|
|
import logging
|
2016-09-13 20:13:48 +02:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-09-01 01:52:55 +02:00
|
|
|
import zipfile
|
2016-09-13 20:13:48 +02:00
|
|
|
from hashlib import sha1
|
|
|
|
from hashlib import sha256
|
|
|
|
|
2017-12-15 21:21:44 +01:00
|
|
|
import common
|
2016-09-13 20:13:48 +02:00
|
|
|
|
2018-10-12 19:30:39 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-09-01 01:52:55 +02:00
|
|
|
|
|
|
|
def CertUsesSha256(cert):
|
2016-09-13 20:13:48 +02:00
|
|
|
"""Check if the cert uses SHA-256 hashing algorithm."""
|
|
|
|
|
|
|
|
cmd = ['openssl', 'x509', '-text', '-noout', '-in', cert]
|
2019-06-25 19:04:24 +02:00
|
|
|
cert_dump = common.RunAndCheckOutput(cmd, stdout=subprocess.PIPE)
|
2016-09-13 20:13:48 +02:00
|
|
|
|
|
|
|
algorithm = re.search(r'Signature Algorithm: ([a-zA-Z0-9]+)', cert_dump)
|
|
|
|
assert algorithm, "Failed to identify the signature algorithm."
|
|
|
|
|
|
|
|
assert not algorithm.group(1).startswith('ecdsa'), (
|
|
|
|
'This script doesn\'t support verifying ECDSA signed package yet.')
|
|
|
|
|
|
|
|
return algorithm.group(1).startswith('sha256')
|
|
|
|
|
|
|
|
|
2017-09-01 01:52:55 +02:00
|
|
|
def VerifyPackage(cert, package):
|
2016-09-13 20:13:48 +02:00
|
|
|
"""Verify the given package with the certificate.
|
|
|
|
|
|
|
|
(Comments from bootable/recovery/verifier.cpp:)
|
|
|
|
|
|
|
|
An archive with a whole-file signature will end in six bytes:
|
|
|
|
|
|
|
|
(2-byte signature start) $ff $ff (2-byte comment size)
|
|
|
|
|
|
|
|
(As far as the ZIP format is concerned, these are part of the
|
|
|
|
archive comment.) We start by reading this footer, this tells
|
|
|
|
us how far back from the end we have to start reading to find
|
|
|
|
the whole comment.
|
|
|
|
"""
|
|
|
|
|
|
|
|
print('Package: %s' % (package,))
|
|
|
|
print('Certificate: %s' % (cert,))
|
|
|
|
|
|
|
|
# Read in the package.
|
2019-06-25 19:04:24 +02:00
|
|
|
with open(package, 'rb') as package_file:
|
2016-09-13 20:13:48 +02:00
|
|
|
package_bytes = package_file.read()
|
|
|
|
|
|
|
|
length = len(package_bytes)
|
|
|
|
assert length >= 6, "Not big enough to contain footer."
|
|
|
|
|
2019-06-25 19:04:24 +02:00
|
|
|
footer = bytearray(package_bytes[-6:])
|
2016-09-13 20:13:48 +02:00
|
|
|
assert footer[2] == 0xff and footer[3] == 0xff, "Footer is wrong."
|
|
|
|
|
|
|
|
signature_start_from_end = (footer[1] << 8) + footer[0]
|
|
|
|
assert signature_start_from_end > 6, "Signature start is in the footer."
|
|
|
|
|
|
|
|
signature_start = length - signature_start_from_end
|
|
|
|
|
|
|
|
# Determine how much of the file is covered by the signature. This is
|
|
|
|
# everything except the signature data and length, which includes all of the
|
|
|
|
# EOCD except for the comment length field (2 bytes) and the comment data.
|
|
|
|
comment_len = (footer[5] << 8) + footer[4]
|
|
|
|
signed_len = length - comment_len - 2
|
|
|
|
|
|
|
|
print('Package length: %d' % (length,))
|
|
|
|
print('Comment length: %d' % (comment_len,))
|
|
|
|
print('Signed data length: %d' % (signed_len,))
|
|
|
|
print('Signature start: %d' % (signature_start,))
|
|
|
|
|
2017-09-01 01:52:55 +02:00
|
|
|
use_sha256 = CertUsesSha256(cert)
|
2016-09-13 20:13:48 +02:00
|
|
|
print('Use SHA-256: %s' % (use_sha256,))
|
|
|
|
|
2017-12-15 21:21:44 +01:00
|
|
|
h = sha256() if use_sha256 else sha1()
|
2016-09-13 20:13:48 +02:00
|
|
|
h.update(package_bytes[:signed_len])
|
|
|
|
package_digest = h.hexdigest().lower()
|
|
|
|
|
2017-09-01 01:52:55 +02:00
|
|
|
print('Digest: %s' % (package_digest,))
|
2016-09-13 20:13:48 +02:00
|
|
|
|
|
|
|
# Get the signature from the input package.
|
|
|
|
signature = package_bytes[signature_start:-6]
|
2016-09-19 22:54:38 +02:00
|
|
|
sig_file = common.MakeTempFile(prefix='sig-')
|
2016-09-13 20:13:48 +02:00
|
|
|
with open(sig_file, 'wb') as f:
|
|
|
|
f.write(signature)
|
|
|
|
|
|
|
|
# Parse the signature and get the hash.
|
|
|
|
cmd = ['openssl', 'asn1parse', '-inform', 'DER', '-in', sig_file]
|
2019-06-25 19:04:24 +02:00
|
|
|
sig = common.RunAndCheckOutput(cmd, stdout=subprocess.PIPE)
|
2016-09-13 20:13:48 +02:00
|
|
|
|
2019-06-25 19:04:24 +02:00
|
|
|
digest_line = sig.rstrip().split('\n')[-1]
|
2016-09-13 20:13:48 +02:00
|
|
|
digest_string = digest_line.split(':')[3]
|
2016-09-19 22:54:38 +02:00
|
|
|
digest_file = common.MakeTempFile(prefix='digest-')
|
2016-09-13 20:13:48 +02:00
|
|
|
with open(digest_file, 'wb') as f:
|
2019-06-25 19:04:24 +02:00
|
|
|
f.write(bytearray.fromhex(digest_string))
|
2016-09-13 20:13:48 +02:00
|
|
|
|
|
|
|
# Verify the digest by outputing the decrypted result in ASN.1 structure.
|
2016-09-19 22:54:38 +02:00
|
|
|
decrypted_file = common.MakeTempFile(prefix='decrypted-')
|
2016-09-13 20:13:48 +02:00
|
|
|
cmd = ['openssl', 'rsautl', '-verify', '-certin', '-inkey', cert,
|
|
|
|
'-in', digest_file, '-out', decrypted_file]
|
2019-06-25 19:04:24 +02:00
|
|
|
common.RunAndCheckOutput(cmd, stdout=subprocess.PIPE)
|
2016-09-13 20:13:48 +02:00
|
|
|
|
|
|
|
# Parse the output ASN.1 structure.
|
|
|
|
cmd = ['openssl', 'asn1parse', '-inform', 'DER', '-in', decrypted_file]
|
2019-06-25 19:04:24 +02:00
|
|
|
decrypted_output = common.RunAndCheckOutput(cmd, stdout=subprocess.PIPE)
|
2016-09-13 20:13:48 +02:00
|
|
|
|
2019-06-25 19:04:24 +02:00
|
|
|
digest_line = decrypted_output.rstrip().split('\n')[-1]
|
2016-09-13 20:13:48 +02:00
|
|
|
digest_string = digest_line.split(':')[3].lower()
|
|
|
|
|
|
|
|
# Verify that the two digest strings match.
|
|
|
|
assert package_digest == digest_string, "Verification failed."
|
|
|
|
|
|
|
|
# Verified successfully upon reaching here.
|
2017-09-01 01:52:55 +02:00
|
|
|
print('\nWhole package signature VERIFIED\n')
|
|
|
|
|
|
|
|
|
|
|
|
def VerifyAbOtaPayload(cert, package):
|
|
|
|
"""Verifies the payload and metadata signatures in an A/B OTA payload."""
|
|
|
|
package_zip = zipfile.ZipFile(package, 'r')
|
|
|
|
if 'payload.bin' not in package_zip.namelist():
|
|
|
|
common.ZipClose(package_zip)
|
|
|
|
return
|
|
|
|
|
|
|
|
print('Verifying A/B OTA payload signatures...')
|
|
|
|
|
2017-12-15 21:21:44 +01:00
|
|
|
# Dump pubkey from the certificate.
|
2018-02-04 21:13:35 +01:00
|
|
|
pubkey = common.MakeTempFile(prefix="key-", suffix=".pem")
|
2019-06-25 19:04:24 +02:00
|
|
|
with open(pubkey, 'w') as pubkey_fp:
|
2018-02-04 21:13:35 +01:00
|
|
|
pubkey_fp.write(common.ExtractPublicKey(cert))
|
|
|
|
|
|
|
|
package_dir = common.MakeTempDir(prefix='package-')
|
2017-09-01 01:52:55 +02:00
|
|
|
|
2017-12-15 21:21:44 +01:00
|
|
|
# Signature verification with delta_generator.
|
2017-09-01 01:52:55 +02:00
|
|
|
payload_file = package_zip.extract('payload.bin', package_dir)
|
2017-12-15 21:21:44 +01:00
|
|
|
cmd = ['delta_generator',
|
|
|
|
'--in_file=' + payload_file,
|
|
|
|
'--public_key=' + pubkey]
|
2019-06-25 19:04:24 +02:00
|
|
|
common.RunAndCheckOutput(cmd)
|
2017-09-01 01:52:55 +02:00
|
|
|
common.ZipClose(package_zip)
|
|
|
|
|
|
|
|
# Verified successfully upon reaching here.
|
|
|
|
print('\nPayload signatures VERIFIED\n\n')
|
2016-09-13 20:13:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('certificate', help='The certificate to be used.')
|
|
|
|
parser.add_argument('package', help='The OTA package to be verified.')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-10-12 19:30:39 +02:00
|
|
|
common.InitLogging()
|
|
|
|
|
2017-09-01 01:52:55 +02:00
|
|
|
VerifyPackage(args.certificate, args.package)
|
|
|
|
VerifyAbOtaPayload(args.certificate, args.package)
|
2016-09-13 20:13:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except AssertionError as err:
|
|
|
|
print('\n ERROR: %s\n' % (err,))
|
|
|
|
sys.exit(1)
|
2017-09-01 01:52:55 +02:00
|
|
|
finally:
|
|
|
|
common.Cleanup()
|