Skip to content

Commit 6c68eaf

Browse files
Brint E. KriebelSteve Kondik
authored andcommitted
releasetools: Add bootable image signing
Set PRODUCT_PRIVATE_KEY to the path of the boot image signing certificate to sign an image for secure boot. Change-Id: I2c767c6f9c3740bed3d7094d2d0b50a075e08abc
1 parent ec0d162 commit 6c68eaf

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

tools/releasetools/common.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ def BuildBootableImage(sourcedir, fs_config_file, info_dict=None):
296296

297297
ramdisk_img = tempfile.NamedTemporaryFile()
298298
img = tempfile.NamedTemporaryFile()
299+
bootimg_key = os.getenv("PRODUCT_PRIVATE_KEY", None)
299300

300301
if os.access(fs_config_file, os.F_OK):
301302
cmd = ["mkbootfs", "-f", fs_config_file, os.path.join(sourcedir, "RAMDISK")]
@@ -362,8 +363,9 @@ def BuildBootableImage(sourcedir, fs_config_file, info_dict=None):
362363

363364
fn = os.path.join(sourcedir, "pagesize")
364365
if os.access(fn, os.F_OK):
366+
kernel_pagesize=open(fn).read().rstrip("\n")
365367
cmd.append("--pagesize")
366-
cmd.append(open(fn).read().rstrip("\n"))
368+
cmd.append(kernel_pagesize)
367369

368370
args = info_dict.get("mkbootimg_args", None)
369371
if args and args.strip():
@@ -376,6 +378,42 @@ def BuildBootableImage(sourcedir, fs_config_file, info_dict=None):
376378
assert p.returncode == 0, "mkbootimg of %s image failed" % (
377379
os.path.basename(sourcedir),)
378380

381+
if bootimg_key and os.path.exists(bootimg_key) and kernel_pagesize > 0:
382+
print "Signing bootable image..."
383+
bootimg_key_passwords = {}
384+
bootimg_key_passwords.update(PasswordManager().GetPasswords(bootimg_key.split()))
385+
bootimg_key_password = bootimg_key_passwords[bootimg_key]
386+
if bootimg_key_password is not None:
387+
bootimg_key_password += "\n"
388+
img_sha256 = tempfile.NamedTemporaryFile()
389+
img_sig = tempfile.NamedTemporaryFile()
390+
img_sig_padded = tempfile.NamedTemporaryFile()
391+
img_secure = tempfile.NamedTemporaryFile()
392+
p = Run(["openssl", "dgst", "-sha256", "-binary", "-out", img_sha256.name, img.name],
393+
stdout=subprocess.PIPE)
394+
p.communicate()
395+
assert p.returncode == 0, "signing of bootable image failed"
396+
p = Run(["openssl", "rsautl", "-sign", "-in", img_sha256.name, "-inkey", bootimg_key, "-out",
397+
img_sig.name, "-passin", "stdin"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
398+
p.communicate(bootimg_key_password)
399+
assert p.returncode == 0, "signing of bootable image failed"
400+
p = Run(["dd", "if=/dev/zero", "of=%s" % img_sig_padded.name, "bs=%s" % kernel_pagesize,
401+
"count=1"], stdout=subprocess.PIPE)
402+
p.communicate()
403+
assert p.returncode == 0, "signing of bootable image failed"
404+
p = Run(["dd", "if=%s" % img_sig.name, "of=%s" % img_sig_padded.name, "conv=notrunc"],
405+
stdout=subprocess.PIPE)
406+
p.communicate()
407+
assert p.returncode == 0, "signing of bootable image failed"
408+
p = Run(["cat", img.name, img_sig_padded.name], stdout=img_secure.file.fileno())
409+
p.communicate()
410+
assert p.returncode == 0, "signing of bootable image failed"
411+
shutil.copyfile(img_secure.name, img.name)
412+
img_sha256.close()
413+
img_sig.close()
414+
img_sig_padded.close()
415+
img_secure.close()
416+
379417
if info_dict.get("verity_key", None):
380418
path = "/" + os.path.basename(sourcedir).lower()
381419
cmd = ["boot_signer", path, img.name, info_dict["verity_key"] + ".pk8", info_dict["verity_key"] + ".x509.pem", img.name]

0 commit comments

Comments
 (0)