Merge "releasetools: Clean up ReplaceVerityKeyId and add tests." am: b35128521d

am: 2a0f657c56

Change-Id: I0da4fccfbf7d6873de59e5a019cb13f3414e3b40
This commit is contained in:
Tao Bao 2018-01-03 19:46:56 +00:00 committed by android-build-merger
commit 413a88b386
2 changed files with 163 additions and 23 deletions

View file

@ -577,31 +577,39 @@ def ReplaceVerityPrivateKey(misc_info, key_path):
misc_info["verity_key"] = key_path
def ReplaceVerityKeyId(targetfile_input_zip, targetfile_output_zip, key_path):
in_cmdline = targetfile_input_zip.read("BOOT/cmdline")
# copy in_cmdline to output_zip if veritykeyid is not present in in_cmdline
def ReplaceVerityKeyId(input_zip, output_zip, key_path):
"""Replaces the veritykeyid parameter in BOOT/cmdline.
Args:
input_zip: The input target_files zip, which should be already open.
output_zip: The output target_files zip, which should be already open and
writable.
key_path: The path to the PEM encoded X.509 certificate.
"""
in_cmdline = input_zip.read("BOOT/cmdline")
# Copy in_cmdline to output_zip if veritykeyid is not present.
if "veritykeyid" not in in_cmdline:
common.ZipWriteStr(targetfile_output_zip, "BOOT/cmdline", in_cmdline)
return in_cmdline
common.ZipWriteStr(output_zip, "BOOT/cmdline", in_cmdline)
return
out_buffer = []
for param in in_cmdline.split():
if "veritykeyid" in param:
# extract keyid using openssl command
p = common.Run(
["openssl", "x509", "-in", key_path, "-text"],
stdout=subprocess.PIPE)
keyid, stderr = p.communicate()
keyid = re.search(
r'keyid:([0-9a-fA-F:]*)', keyid).group(1).replace(':', '').lower()
print("Replacing verity keyid with %s error=%s" % (keyid, stderr))
out_buffer.append("veritykeyid=id:%s" % (keyid,))
else:
if "veritykeyid" not in param:
out_buffer.append(param)
continue
out_cmdline = ' '.join(out_buffer)
out_cmdline = out_cmdline.strip()
print("out_cmdline %s" % (out_cmdline))
common.ZipWriteStr(targetfile_output_zip, "BOOT/cmdline", out_cmdline)
# Extract keyid using openssl command.
p = common.Run(["openssl", "x509", "-in", key_path, "-text"],
stdout=subprocess.PIPE)
keyid, stderr = p.communicate()
assert p.returncode == 0, "Failed to dump certificate: {}".format(stderr)
keyid = re.search(
r'keyid:([0-9a-fA-F:]*)', keyid).group(1).replace(':', '').lower()
print("Replacing verity keyid with {}".format(keyid))
out_buffer.append("veritykeyid=id:%s" % (keyid,))
out_cmdline = ' '.join(out_buffer).strip() + '\n'
common.ZipWriteStr(output_zip, "BOOT/cmdline", out_cmdline)
def ReplaceMiscInfoTxt(input_zip, output_zip, misc_info):

View file

@ -16,13 +16,22 @@
from __future__ import print_function
import tempfile
import unittest
import zipfile
from sign_target_files_apks import EditTags, RewriteProps
import common
from sign_target_files_apks import EditTags, ReplaceVerityKeyId, RewriteProps
class SignTargetFilesApksTest(unittest.TestCase):
def setUp(self):
self.tempdir = common.MakeTempDir()
def tearDown(self):
common.Cleanup()
def test_EditTags(self):
self.assertEqual(EditTags('dev-keys'), ('release-keys'))
self.assertEqual(EditTags('test-keys'), ('release-keys'))
@ -59,9 +68,132 @@ class SignTargetFilesApksTest(unittest.TestCase):
)
# Assert the case for each individual line.
for input, output in props:
self.assertEqual(RewriteProps(input), output)
for prop, output in props:
self.assertEqual(RewriteProps(prop), output)
# Concatenate all the input lines.
self.assertEqual(RewriteProps('\n'.join([prop[0] for prop in props])),
''.join([prop[1] for prop in props]))
def test_ReplaceVerityKeyId(self):
BOOT_CMDLINE1 = (
"console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 "
"androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 "
"lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 "
"buildvariant=userdebug "
"veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f\n")
BOOT_CMDLINE2 = (
"console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 "
"androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 "
"lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 "
"buildvariant=userdebug "
"veritykeyid=id:485900563d272c46ae118605a47419ac09ca8c11\n")
# From build/target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QYJKoZIhvcNAQEFBQADggEB
AHO3NSvDE5jFvMehGGtS8BnFYdFKRIglDMc4niWSzhzOVYRH4WajxdtBWc5fx0ix
NF/+hVKVhP6AIOQa+++sk+HIi7RvioPPbhjcsVlZe7cUEGrLSSveGouQyc+j0+m6
JF84kszIl5GGNMTnx0XRPO+g8t6h5LWfnVydgZfpGRRg+WHewk1U2HlvTjIceb0N
dcoJ8WKJAFWdcuE7VIm4w+vF/DYX/A2Oyzr2+QRhmYSv1cusgAeC1tvH4ap+J1Lg
UnOu5Kh/FqPLLSwNVQp4Bu7b9QFfqK8Moj84bj88NqRGZgDyqzuTrFxn6FW7dmyA
yttuAJAEAymk1mipd9+zp38=
-----END CERTIFICATE-----
"""
# From build/target/product/security/testkey.x509.pem.
VERITY_CERTIFICATE2 = """-----BEGIN CERTIFICATE-----
MIIEqDCCA5CgAwIBAgIJAJNurL4H8gHfMA0GCSqGSIb3DQEBBQUAMIGUMQswCQYD
VQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4g
VmlldzEQMA4GA1UEChMHQW5kcm9pZDEQMA4GA1UECxMHQW5kcm9pZDEQMA4GA1UE
AxMHQW5kcm9pZDEiMCAGCSqGSIb3DQEJARYTYW5kcm9pZEBhbmRyb2lkLmNvbTAe
Fw0wODAyMjkwMTMzNDZaFw0zNTA3MTcwMTMzNDZaMIGUMQswCQYDVQQGEwJVUzET
MBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEQMA4G
A1UEChMHQW5kcm9pZDEQMA4GA1UECxMHQW5kcm9pZDEQMA4GA1UEAxMHQW5kcm9p
ZDEiMCAGCSqGSIb3DQEJARYTYW5kcm9pZEBhbmRyb2lkLmNvbTCCASAwDQYJKoZI
hvcNAQEBBQADggENADCCAQgCggEBANaTGQTexgskse3HYuDZ2CU+Ps1s6x3i/waM
qOi8qM1r03hupwqnbOYOuw+ZNVn/2T53qU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)
with zipfile.ZipFile(input_file.name, 'w') as input_zip:
input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE1)
# Test with the first certificate.
cert_file = tempfile.NamedTemporaryFile(
delete=False, suffix='.x509.pem', dir=self.tempdir)
cert_file.write(VERITY_CERTIFICATE1)
cert_file.close()
output_file = tempfile.NamedTemporaryFile(
delete=False, suffix='.zip', dir=self.tempdir)
with zipfile.ZipFile(input_file.name, 'r') as input_zip, \
zipfile.ZipFile(output_file.name, 'w') as output_zip:
ReplaceVerityKeyId(input_zip, output_zip, cert_file.name)
with zipfile.ZipFile(output_file.name) as output_zip:
self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline'))
# Test with the second certificate.
with open(cert_file.name, 'w') as cert_file_fp:
cert_file_fp.write(VERITY_CERTIFICATE2)
with zipfile.ZipFile(input_file.name, 'r') as input_zip, \
zipfile.ZipFile(output_file.name, 'w') as output_zip:
ReplaceVerityKeyId(input_zip, output_zip, cert_file.name)
with zipfile.ZipFile(output_file.name) as output_zip:
self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline'))
def test_ReplaceVerityKeyId_no_veritykeyid(self):
BOOT_CMDLINE = (
"console=ttyHSL0,115200,n8 androidboot.hardware=bullhead boot_cpus=0-5 "
"lpm_levels.sleep_disabled=1 msm_poweroff.download_mode=0 "
"loop.max_part=7\n")
input_file = tempfile.NamedTemporaryFile(
delete=False, suffix='.zip', dir=self.tempdir)
with zipfile.ZipFile(input_file.name, 'w') as input_zip:
input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE)
output_file = tempfile.NamedTemporaryFile(
delete=False, suffix='.zip', dir=self.tempdir)
with zipfile.ZipFile(input_file.name, 'r') as input_zip, \
zipfile.ZipFile(output_file.name, 'w') as output_zip:
ReplaceVerityKeyId(input_zip, output_zip, None)
with zipfile.ZipFile(output_file.name) as output_zip:
self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline'))