Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EE77C433EF for ; Fri, 12 Nov 2021 19:31:12 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8F38B61039 for ; Fri, 12 Nov 2021 19:31:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8F38B61039 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 26D9783061; Fri, 12 Nov 2021 20:30:10 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="lTTjpR1I"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id F19AF83069; Fri, 12 Nov 2021 20:29:28 +0100 (CET) Received: from mail-oi1-x230.google.com (mail-oi1-x230.google.com [IPv6:2607:f8b0:4864:20::230]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6EF6383070 for ; Fri, 12 Nov 2021 20:29:06 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-oi1-x230.google.com with SMTP id q25so14192029oiw.0 for ; Fri, 12 Nov 2021 11:29:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gxGChbDyTgCSosUbCEMMpjhPqud0irx6lpIUc9tSZQo=; b=lTTjpR1IM3Gq8r1s0TgOq8p7wlOODdF87hRfAe8SA7QWZ4Au261aFR0HaBrw9Fyljm 3aC4de8IRpZcKrPfa24JKfCUcLaxt+scbK1/hlL9CUkY+rwqNt5bt4kcQPKeGci6Ap/F 9sijj2J/T1ICPEpRBEQ7A/grKhAkXetvuY7F0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gxGChbDyTgCSosUbCEMMpjhPqud0irx6lpIUc9tSZQo=; b=1a3Rjyr4DqOo9ImMiqPRoA7ODmgvf0GHhMEZdFHRgYapqtmHjcA/H8gZpBhTn5rYuF d7GpgH+RCZC0l0ohJ7OVcciCMLXKGqshipzVk2FgdBDv6w4+bSO+DhWzHG1I9hclo8FL 66RfsgwDKIWMmiWOCJZK4E41Y0X/S4PyBQl/N1MZba8sv2Hy12w1Xxn961BetJ9EpKrk fjKE32kW4r+NsOl/MYpfOR012K7I5LaRkBeeP9yiyz0YtJe1halJXiA8PPx8L42aouKs 9PpH5SkkYDzFLJqOH9+jTB0gW+sG1HgvsMZiXLeumR9qc/Vhse2oKB/SrgZ2mW7fO0J5 Pfzw== X-Gm-Message-State: AOAM53366u5xDGFtXTizYBvy+5o5Xbh34saNOP621P2qiWebtHppaePn hYZ3rDno3XxxQPwQ6jXr3Gk4I72Arl+9hA== X-Google-Smtp-Source: ABdhPJw6/asj2FWXd2qnhRkGmLpt2Ipww8rDtCbs46ETghb4DilcasgSRE/c5PnxBFBhGjHpxa74cQ== X-Received: by 2002:a05:6808:11c9:: with SMTP id p9mr27697145oiv.169.1636745344904; Fri, 12 Nov 2021 11:29:04 -0800 (PST) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id n20sm1283527ooe.7.2021.11.12.11.29.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Nov 2021 11:29:04 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Rob Herring , Tom Rini , Bill Mills , Simon Glass Subject: [PATCH 15/16] test: Add a test for FDT signing Date: Fri, 12 Nov 2021 12:28:16 -0700 Message-Id: <20211112192817.199075-16-sjg@chromium.org> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog In-Reply-To: <20211112192817.199075-1-sjg@chromium.org> References: <20211112192817.199075-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.35 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Add a test which checks that signing and checking work, including signing an FDT twice. Signed-off-by: Simon Glass --- test/py/tests/test_fdt_sign.py | 83 ++++++++++++++++++++++++++++++++ test/py/tests/test_vboot.py | 21 ++------ test/py/tests/vboot/sign-fdt.dts | 23 +++++++++ test/py/tests/vboot_comm.py | 22 +++++++++ 4 files changed, 131 insertions(+), 18 deletions(-) create mode 100644 test/py/tests/test_fdt_sign.py create mode 100644 test/py/tests/vboot/sign-fdt.dts create mode 100644 test/py/tests/vboot_comm.py diff --git a/test/py/tests/test_fdt_sign.py b/test/py/tests/test_fdt_sign.py new file mode 100644 index 00000000000..72d58211159 --- /dev/null +++ b/test/py/tests/test_fdt_sign.py @@ -0,0 +1,83 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright 2021 Google LLC +# +# U-Boot FDT-signing test + +import os + +import u_boot_utils as util +import vboot_comm + +def test_fdt_sign(u_boot_console): + def dtc(dts): + """Run the device tree compiler to compile a .dts file + + The output file will be the same as the input file but with a .dtb + extension. + + Args: + dts: Device tree file to compile. + """ + dtb = dts.replace('.dts', '.dtb') + util.run_and_log(cons, 'dtc %s %s%s -O dtb -p 0x1000 ' + '-o %s%s' % (dtc_args, datadir, dts, tmpdir, dtb)) + + cons = u_boot_console + datadir = os.path.join(cons.config.source_dir, 'test/py/tests/vboot/') + fdt_sign = os.path.join(cons.config.build_dir, 'tools/fdt_sign') + fdt_check_sign = os.path.join(cons.config.build_dir, 'tools/fdt_check_sign') + + tmpdir = os.path.join(cons.config.result_dir, 'fdt_sign') + '/' + if not os.path.exists(tmpdir): + os.mkdir(tmpdir) + + dtb = '%ssandbox-u-boot.dtb' % tmpdir + dtc_args = '-I dts -O dtb -i %s' % tmpdir + dtc('sign-fdt.dts') + dtc('sandbox-u-boot.dts') + + vboot_comm.create_rsa_pair(cons, tmpdir, 'dev') + + # Sign and check that it verifies + signed = os.path.join(tmpdir, 'sign-fdt.dtb') + cmd = [fdt_sign, '-f', signed, '-G', os.path.join(tmpdir, 'dev.key'), + '-K', dtb, '-k', tmpdir, '-r'] + util.run_and_log(cons, ' '.join(cmd)) + + cmd = [fdt_check_sign, '-f', signed, '-k', dtb] + util.run_and_log(cons, ' '.join(cmd)) + + # Update the chosen node, which dpes not affect things since the signature + # omits that node + util.run_and_log(cons, 'fdtput -t bx %s /chosen fred 1' % signed) + + cmd = [fdt_check_sign, '-f', signed, '-k', dtb] + util.run_and_log(cons, ' '.join(cmd)) + + # Update the alias node, which should break things because that is included + # in the signature + util.run_and_log(cons, 'fdtput -t bx %s /aliases fred 1' % signed) + + cmd = [fdt_check_sign, '-f', signed, '-k', dtb] + util.run_and_log_expect_exception(cons, cmd, 1, 'Verification failed') + + # Regenerate the original devictree and sign it + dtc('sign-fdt.dts') + dtc('sandbox-u-boot.dts') + out = os.path.join(tmpdir, 'out-fdt.dtb') + cmd = [fdt_sign, '-f', signed, '-G', os.path.join(tmpdir, 'dev.key'), + '-K', dtb, '-k', tmpdir, '-r', '-o', out] + util.run_and_log(cons, ' '.join(cmd)) + + cmd = [fdt_check_sign, '-f', out, '-k', dtb] + util.run_and_log(cons, ' '.join(cmd)) + + # Create a new key and sign with that too + vboot_comm.create_rsa_pair(cons, tmpdir, 'prod') + cmd = [fdt_sign, '-f', out, '-G', os.path.join(tmpdir, 'prod.key'), + '-K', dtb, '-k', tmpdir, '-r'] + util.run_and_log(cons, ' '.join(cmd)) + + # Now check that both signatures are valid + cmd = [fdt_check_sign, '-f', out, '-k', dtb] + util.run_and_log(cons, ' '.join(cmd)) diff --git a/test/py/tests/test_vboot.py b/test/py/tests/test_vboot.py index 095e00cce36..8458210691d 100644 --- a/test/py/tests/test_vboot.py +++ b/test/py/tests/test_vboot.py @@ -29,6 +29,7 @@ import shutil import struct import pytest import u_boot_utils as util +import vboot_comm import vboot_forge import vboot_evil @@ -173,22 +174,6 @@ def test_vboot(u_boot_console, name, sha_algo, padding, sign_options, required, handle.write(struct.pack(">I", size)) return struct.unpack(">I", total_size)[0] - def create_rsa_pair(name): - """Generate a new RSA key paid and certificate - - Args: - name: Name of of the key (e.g. 'dev') - """ - public_exponent = 65537 - util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %s%s.key ' - '-pkeyopt rsa_keygen_bits:2048 ' - '-pkeyopt rsa_keygen_pubexp:%d' % - (tmpdir, name, public_exponent)) - - # Create a certificate containing the public key - util.run_and_log(cons, 'openssl req -batch -new -x509 -key %s%s.key ' - '-out %s%s.crt' % (tmpdir, name, tmpdir, name)) - def test_with_algo(sha_algo, padding, sign_options): """Test verified boot with the given hash algorithm. @@ -377,8 +362,8 @@ def test_vboot(u_boot_console, name, sha_algo, padding, sign_options, required, dtb = '%ssandbox-u-boot.dtb' % tmpdir sig_node = '/configurations/conf-1/signature' - create_rsa_pair('dev') - create_rsa_pair('prod') + vboot_comm.create_rsa_pair(cons, tmpdir, 'dev') + vboot_comm.create_rsa_pair(cons, tmpdir, 'prod') # Create a number kernel image with zeroes with open('%stest-kernel.bin' % tmpdir, 'wb') as fd: diff --git a/test/py/tests/vboot/sign-fdt.dts b/test/py/tests/vboot/sign-fdt.dts new file mode 100644 index 00000000000..2c5b84976b1 --- /dev/null +++ b/test/py/tests/vboot/sign-fdt.dts @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + model = "Google Snow"; + chosen { + bootargs = "console=tty1"; + stdout-path = "serial3:115200n8"; + }; + aliases { + serail0 = "/serial@12c30000"; + }; + + serial@12c30000 { + compatible = "samsung,exynos4210-uart"; + reg = <0x12c30000 0x00000100>; + u-boot,dm-pre-reloc; + }; +}; diff --git a/test/py/tests/vboot_comm.py b/test/py/tests/vboot_comm.py new file mode 100644 index 00000000000..7e1690e89f8 --- /dev/null +++ b/test/py/tests/vboot_comm.py @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright 2021 Google LLC +# +# Common functions + +import u_boot_utils as util + +def create_rsa_pair(cons, tmpdir, name): + """Generate a new RSA key pair and certificate + + Args: + name: Name of the key (e.g. 'dev') + """ + public_exponent = 65537 + util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %s%s.key ' + '-pkeyopt rsa_keygen_bits:2048 ' + '-pkeyopt rsa_keygen_pubexp:%d' % + (tmpdir, name, public_exponent)) + + # Create a certificate containing the public key + util.run_and_log(cons, 'openssl req -batch -new -x509 -key %s%s.key ' + '-out %s%s.crt' % (tmpdir, name, tmpdir, name)) -- 2.34.0.rc1.387.gb447b232ab-goog