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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id D58DCC433F5 for ; Sun, 10 Apr 2022 22:38:09 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7323B80508; Mon, 11 Apr 2022 00:38:07 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="HjzppEEm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id F0FAE829FF; Mon, 11 Apr 2022 00:38:04 +0200 (CEST) Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) (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 0EEA381119 for ; Mon, 11 Apr 2022 00:38:01 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=alpernebiyasak@gmail.com Received: by mail-ej1-x635.google.com with SMTP id t11so431614eju.13 for ; Sun, 10 Apr 2022 15:38:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:from:subject:to:cc :references:content-language:in-reply-to:content-transfer-encoding; bh=3WN3LXvrfCPxXleI6wDUo9IG3wzvxEsx/QXOBk4naDY=; b=HjzppEEmrTY/yPIZCcdZtYjroaOHpvxKAyajyxoIo/Pve5YxQjATSdOWNqZqAclrKV APzZH0WRXcwYZ2e4vpTOfsCIpD79xWraan+C+dUK28LwVFwcDHyqAyrMd0Y0jLpuOJnQ JfHakCx0O3a1iRCMfs49prM45G3+Lm0viXWjkQ3DKrv8C01fSIu4M7vj6GRJ7qmbhrhI T3kyfxBNMhOWplnF4mkwvAJoqGRG9mr6QJOXnvggUz5+BhJCHIiwywyoke/WJhj9OXfU BNBug+42lAJawA75oYSY5BsU58LM844S3xVQbQwCcOz20pK6kpeNnoByzHpHk1IGWkxz lE2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:from :subject:to:cc:references:content-language:in-reply-to :content-transfer-encoding; bh=3WN3LXvrfCPxXleI6wDUo9IG3wzvxEsx/QXOBk4naDY=; b=aq2GBfwfeQU3cS6TXgygl82XxY10RaX+0rHWIpeJ1Mt8M6To1QfNMCZdAMFUkUro8P VecG3VPeFn6hgdYhknn8ldm/pM5zkJZXTRMSWeaXQU59dWDKE/qFZK8bl3MpuyCGfz5+ PI27rjycQLkh0u3ldR821E8R5sFQEGnXn8FAoLTUY6LmZW5Y+aUz2Y+hoUzJbvC0GsOe LOnBvJKYNkOS19Oi4CaATM6ghA1jfWTzvpX3rdQ06WmEuJKnbS1AijQJhRnvBd6zYprK TNphGgPHXIjo+dkWb1aa/t09DscFH7jqSNka9CAjvzBiwbKWQ/W2dX5ltxaskIQESU3x ZL7w== X-Gm-Message-State: AOAM531IpT7pjY8ETsiLXi97zWnjCoGJwlXtAPVrYk1/EYUhgPadrf0Y nsRZhedC+jwpzxFoUyUoOF4= X-Google-Smtp-Source: ABdhPJzNiXeajdIVKa8hiJKqFlkcp69DDSe8Z1aOVoU60VfoQnHzmFDB0U/m8IdXUYRjl85DbXfPUw== X-Received: by 2002:a17:907:7f0e:b0:6e8:7f57:2d96 with SMTP id qf14-20020a1709077f0e00b006e87f572d96mr4617652ejc.7.1649630280394; Sun, 10 Apr 2022 15:38:00 -0700 (PDT) Received: from [192.168.0.74] ([178.233.178.185]) by smtp.gmail.com with ESMTPSA id f3-20020a056402004300b004162aa024c0sm14030778edu.76.2022.04.10.15.37.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 10 Apr 2022 15:37:59 -0700 (PDT) Message-ID: <51f675cc-31c7-a239-a068-c6b74d96c7cb@gmail.com> Date: Mon, 11 Apr 2022 01:37:56 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 From: Alper Nebi Yasak Subject: Re: [PATCH 3/3] binman: add test for sign option To: Ivan Mikhaylov , Sean Anderson Cc: U-Boot Mailing List , Ivan Mikhaylov , Simon Glass , Jan Kiszka References: <20220321214319.33254-1-fr0st61te@gmail.com> <20220321214319.33254-4-fr0st61te@gmail.com> <5d4f696f-5efe-c711-2116-906c833a5488@seco.com> <5fc59e13582d39991a538c2c0a16d1182d503887.camel@gmail.com> Content-Language: en-US In-Reply-To: <5fc59e13582d39991a538c2c0a16d1182d503887.camel@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.5 at phobos.denx.de X-Virus-Status: Clean I initially ignored this test because I didn't like the implementation anyway, but I disagree with some of Sean's comments here so I wanted to add on what I think. On 08/04/2022 22:26, Ivan Mikhaylov wrote: > On Fri, 2022-04-08 at 11:39 -0400, Sean Anderson wrote: >> On 3/21/22 5:43 PM, Ivan Mikhaylov wrote: >>> Add the test which provides sequence of actions: >>>   1. create the image from binman dts >>>   2. create public and private keys >>>   3. add public key into dtb with fdt_add_pubkey >>>   4. sign image with new sign option >>>   5. check with fit_check_sign >>> >>> Signed-off-by: Ivan Mikhaylov >>> --- >>>  tools/binman/ftest.py              | 42 +++++++++++++++++++ >>>  tools/binman/test/225_fit_sign.dts | 67 >>> ++++++++++++++++++++++++++++++ >>>  2 files changed, 109 insertions(+) >>>  create mode 100644 tools/binman/test/225_fit_sign.dts >>> >>> diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py >>> index 8f00db6945..8a3d3720c4 100644 >>> --- a/tools/binman/ftest.py >>> +++ b/tools/binman/ftest.py >>> @@ -3088,6 +3088,48 @@ class TestFunctional(unittest.TestCase): >>>          self.assertEqual(len(U_BOOT_DATA), entry.contents_size) >>>          self.assertEqual(len(U_BOOT_DATA), entry.size) >>>   >>> +    def testSignSimple(self): >>> +        """Test signing a single file""" >>> + >>> +        data = self._DoReadFileRealDtb('225_fit_sign.dts') >>> + >>> +        updated_fname = tools.GetOutputFilename('image- >>> updated.bin') >>> +        tools.WriteFile(updated_fname, data) >>> + >>> +        outdir = os.path.join(self._indir, 'extract') >>> +        einfos = control.ExtractEntries(updated_fname, None, >>> outdir, []) >>> + >>> +        dtb = tools.GetOutputFilename('source.dtb') >>> +        private_key = tools.GetOutputFilename('test_key.key') >>> +        public_key = tools.GetOutputFilename('test_key.crt') >>> +        fit = tools.GetOutputFilename('fit.fit') >>> +        key_dir = tools.GetOutputDir() >>> + >>> +        def check_sign(fit, key): >> >> please inline this, since it is only called once >> >>> +            try: >>> +                tools.Run('fit_check_sign', '-k', key, '-f', fit) >>> +            except: >>> +                return False >> >> you can just do a bare tools.Run() and if an exception is raised it >> will >> cause the test to fail. > > Ok, good to know. It will cause a test 'error' instead of a 'failure'. You should keep the try-except, but call self.fail(...) in the except case with a reasonable error message. Nesting multiple try blocks is a bit ugly, so you could keep the single-use function. It would be even better if you added more cases testing various signing conditions, with this as a helper function in the outer scope that all of them can use. You could extend it to check if the signature nodes in the updated fdtmap match the arguments passed to 'binman sign' (but your current implementation doesn't update those nodes). >> >>> +            return True >>> + >>> +        is_signed = False >>> +        try: >>> +            tools.Run('openssl', 'req', '-batch' , '-newkey', >>> 'rsa:4096', >>> +                      '-sha256', '-new',  '-nodes',  '-x509', '- >>> keyout', >>> +                      private_key, '-out', public_key) >>> +            tools.Run('fdt_add_pubkey', '-a', 'sha256,rsa4096', '- >>> k', key_dir, >>> +                      '-n', 'test_key', dtb) >>> +            with test_util.capture_sys_output() as (stdout, >>> stderr): >>> +                # do sign with private key >>> +                self._DoBinman('sign', '-i', updated_fname, '-k', >>> private_key, >>> +                               '-a', 'sha256,rsa4096', '-f', fit, >>> 'fit') >>> +                is_signed = check_sign(fit, dtb) >>> +        finally: >>> +            shutil.rmtree(key_dir) >>> + >>> +        self.assertEqual(is_signed, True) >> >> so no need for this assert here >>> + >>> + >>>      def _RunReplaceCmd(self, entry_name, data, decomp=True, >>> allow_resize=True, >>>                         dts='132_replace.dts'): >>>          """Replace an entry in an image New tests usually are added at the end of this file. IIRC they should be ordered by their test dts' number. >>> diff --git a/tools/binman/test/225_fit_sign.dts >>> b/tools/binman/test/225_fit_sign.dts >>> new file mode 100644 >>> index 0000000000..2bfa826106 >>> --- /dev/null >>> +++ b/tools/binman/test/225_fit_sign.dts >>> @@ -0,0 +1,67 @@ >>> +// SPDX-License-Identifier: GPL-2.0+ >>> + >>> +/dts-v1/; >>> + >>> +/ { >>> +       #address-cells = <1>; >>> +       #size-cells = <1>; >>> + >>> +       binman { >> >> I don't really understand what's going on in this test case. Binman mocks most entries' data during tests, the test images aren't meaningful or usable beyond what's in the python test case code. In this case, we only need a 'fit' entry with a signature we can run 'binman sign' on, and a 'fdtmap' so we can recognize that the built image has that 'fit' entry in it. The rest is just fluff. >> >>> +               size = <0x100000>; >>> +               allow-repack; >>> + >>> +               u-boot { >>> +               }; >>> + >>> +               fit { >>> +                       description = "U-Boot"; >>> +                       offset = <0x10000>; >>> +                       images { >>> +                               u-boot-1 { >>> +                                       description = "U-Boot"; >>> +                                       type = "standalone"; >>> +                                       arch = "arm64"; >>> +                                       os = "u-boot"; >>> +                                       compression = "none"; >>> +                                       hash-1 { >>> +                                               algo = "sha256"; >>> +                                       }; >> >> Shouldn't there be some kind of data here? > > Nope. You could add a 'u-boot' entry here. It's weird seeing a hash/signature for empty data. >> >>> +                               }; >>> + >>> +                               fdt-1 { >> >> Maybe use things like @fdt-SEQ, as seen in >> tools/binman/test/170_fit_fdt.dts? > > I'll check, thanks. If you do so, I would prefer that as an additional test case. No need to replace this one. >> >>> +                                       description = "test.dtb"; >>> +                                       type = "flat_dt"; >>> +                                       arch = "arm64"; >>> +                                       compression = "none"; >>> +                                       hash-1 { >>> +                                               algo = "sha256"; >>> +                                       }; >>> +                                       u-boot-spl-dtb { >>> +                                       }; >>> +                               }; >>> + >>> +                       }; >>> + >>> +                       configurations { >>> +                               default = "conf-1"; >>> +                               conf-1 { >>> +                                       description = "u-boot with >>> fdt"; >>> +                                       firmware = "u-boot-1"; >>> +                                       fdt = "fdt-1"; >>> +                                       signature-1 { >>> +                                               algo = >>> "sha256,rsa4096"; >>> +                                               key-name-hint = >>> "test_key"; >>> +                                               sign-images = >>> "firmware", "fdt"; >>> +                                       }; >>> + >>> +                               }; >>> +                       }; >>> +               }; >>> + >>> +               u-boot-nodtb { >>> +               }; >> >> Why do we have U-Boot again here without a device tree? > > It is done by mistake, same as for 'u-boot' on the top, I'll get rid > off both of them. Doesn't really matter IMO. A lot of test images have the entry-under-test sandwiched between other entries like this anyway. >> >>> +               fdtmap { >>> +               }; >>> +       }; >>> +}; >>> >> >> What is U-Boot supposed to be verified by? Shouldn't this package >> SPL? >> I guess that is out of scope? > > Only thing which should be verified here, that is FIT container is > signed with fit_check_sign utility. > > Thanks.