Pass the correct signature size to payload generator

The signature size will be 512 bytes when signing the payload
with 4096 bits key. This cl determines the key size with
"openssl rsa -modulus"

The new key in testdata is generated by
"openssl genrsa -out testkey 4096"

Bug: 129163830
Test: generate and verify an OTA package
Change-Id: I6662b0a0c553dc0fd84711312a1256b887e332fd
This commit is contained in:
xunchang 2019-04-08 23:04:58 -07:00
parent d7d6e9d671
commit 376cc7c452
3 changed files with 95 additions and 2 deletions

View File

@ -168,6 +168,9 @@ A/B OTA specific options
--payload_signer_args <args>
Specify the arguments needed for payload signer.
--payload_signer_key_size <key_size>
Specify the key size in bytes of the payload signer.
--skip_postinstall
Skip the postinstall hooks when generating an A/B OTA package (default:
False). Note that this discards ALL the hooks, including non-optional
@ -224,6 +227,7 @@ OPTIONS.stash_threshold = 0.8
OPTIONS.log_diff = None
OPTIONS.payload_signer = None
OPTIONS.payload_signer_args = []
OPTIONS.payload_signer_key_size = None
OPTIONS.extracted_input = None
OPTIONS.key_passwords = []
OPTIONS.skip_postinstall = False
@ -468,9 +472,35 @@ class PayloadSigner(object):
self.signer = "openssl"
self.signer_args = ["pkeyutl", "-sign", "-inkey", signing_key,
"-pkeyopt", "digest:sha256"]
self.key_size = self._GetKeySizeInBytes(signing_key)
else:
self.signer = OPTIONS.payload_signer
self.signer_args = OPTIONS.payload_signer_args
if OPTIONS.payload_signer_key_size:
self.key_size = int(OPTIONS.payload_signer_key_size)
assert self.key_size == 256 or self.key_size == 512, \
"Unsupported key size {}".format(OPTIONS.payload_signer_key_size)
else:
self.key_size = 256
@staticmethod
def _GetKeySizeInBytes(signing_key):
modulus_file = common.MakeTempFile(prefix="modulus-")
cmd = ["openssl", "rsa", "-inform", "PEM", "-in", signing_key, "-modulus",
"-noout", "-out", modulus_file]
common.RunAndCheckOutput(cmd, verbose=False)
with open(modulus_file) as f:
modulus_string = f.read()
# The modulus string has the format "Modulus=$data", where $data is the
# concatenation of hex dump of the modulus.
MODULUS_PREFIX = "Modulus="
assert modulus_string.startswith(MODULUS_PREFIX)
modulus_string = modulus_string[len(MODULUS_PREFIX):]
key_size = len(modulus_string) / 2
assert key_size == 256 or key_size == 512, \
"Unsupported key size {}".format(key_size)
return key_size
def Sign(self, in_file):
"""Signs the given input file. Returns the output filename."""
@ -539,7 +569,7 @@ class Payload(object):
metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
cmd = ["brillo_update_payload", "hash",
"--unsigned_payload", self.payload_file,
"--signature_size", "256",
"--signature_size", str(payload_signer.key_size),
"--metadata_hash_file", metadata_sig_file,
"--payload_hash_file", payload_sig_file]
common.RunAndCheckOutput(cmd)
@ -554,7 +584,7 @@ class Payload(object):
cmd = ["brillo_update_payload", "sign",
"--unsigned_payload", self.payload_file,
"--payload", signed_payload_file,
"--signature_size", "256",
"--signature_size", str(payload_signer.key_size),
"--metadata_signature_file", signed_metadata_sig_file,
"--payload_signature_file", signed_payload_sig_file]
common.RunAndCheckOutput(cmd)
@ -2087,6 +2117,8 @@ def main(argv):
OPTIONS.payload_signer = a
elif o == "--payload_signer_args":
OPTIONS.payload_signer_args = shlex.split(a)
elif o == "--payload_signer_key_size":
OPTIONS.payload_signer_key_size = a
elif o == "--extracted_input_target_files":
OPTIONS.extracted_input = a
elif o == "--skip_postinstall":
@ -2125,6 +2157,7 @@ def main(argv):
"log_diff=",
"payload_signer=",
"payload_signer_args=",
"payload_signer_key_size=",
"extracted_input_target_files=",
"skip_postinstall",
"retrofit_dynamic_partitions",

View File

@ -1182,6 +1182,7 @@ class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
def test_init(self):
payload_signer = PayloadSigner()
self.assertEqual('openssl', payload_signer.signer)
self.assertEqual(256, payload_signer.key_size)
def test_init_withPassword(self):
common.OPTIONS.package_key = os.path.join(
@ -1195,9 +1196,16 @@ class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
def test_init_withExternalSigner(self):
common.OPTIONS.payload_signer = 'abc'
common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
common.OPTIONS.payload_signer_key_size = '512'
payload_signer = PayloadSigner()
self.assertEqual('abc', payload_signer.signer)
self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
self.assertEqual(512, payload_signer.key_size)
def test_GetKeySizeInBytes_512Bytes(self):
signing_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
key_size = PayloadSigner._GetKeySizeInBytes(signing_key)
self.assertEqual(512, key_size)
def test_Sign(self):
payload_signer = PayloadSigner()

View File

@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQC7y8EH4O8M9aA7
UhaWLlW5ceQxZi0P7DNOgog/82SIZh1/Vv0S8KCu9LcngK60oIejPU3k9zb0Mpl2
4OEtupbOq9SV0nyRIp33rs9EJ0zm8keZ2jEfwTubdhE4GumlwkbfYHlMPmpufqOq
uxKfCOUZk3ZEC5RFDBqVLGrf9m22ITMQwNGj/u/mtAg2UXRGy534eU1evHOFH8tD
IxXd378m+gRY+bPi6fOkrJgAw6N01NwwsHRxxQsVgp8m1EKpPJ+ARRtP6YYtNsNB
KTOXqZw18OD7eG7yVONAf7oSOMNRK7qg6ZU6YN/y1k+YZF3D/1HU9THoDXIpBHoW
R2SpEM63Ua8ilSmx9PfoaDn27VrpjIcq50HqKAXOvclxOCyVD0pRkrryPpevYCVu
x9/InmW3K4dPiAy0KmmS4ZSLUJnr6Lnkt8C3VxXHqZ4T7MlgrjrO70YqCxeDvYE0
KL/e/UeJ69nANybkDThhBkxkOC1vvik3VXO4ITPEKCnLHdvdj8rwkjqf3Ex5A0g8
XaH0l6I72pqxXi8nnU9udLrUEdHUT1KHzjnZBVP6aomcDy5Gnbb6wXTBTkB7fdQB
nGcqT0DH67PqJE/rCUguVSmX4KGyOVrr5S1GQTg1EmHbF2Kf6P0YJpcVU7PHRE8s
BisP39wzoE5XsIpn/aKpWdpcfqxjTQIDAQABAoICAQCB/vtyLryLpgPyzFIiR5TD
uBkUMPyEhybE9ArI6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