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 72F6BC433EF for ; Fri, 25 Feb 2022 14:59:20 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 449D783D08; Fri, 25 Feb 2022 15:58:40 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=softathome.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=softathome1.onmicrosoft.com header.i=@softathome1.onmicrosoft.com header.b="bRRcceV4"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 552B783CD8; Fri, 25 Feb 2022 15:58:12 +0100 (CET) Received: from FRA01-PR2-obe.outbound.protection.outlook.com (mail-pr2fra01on0619.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e18::619]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E8AF583C71 for ; Fri, 25 Feb 2022 15:58:00 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=softathome.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=philippe.reynes@softathome.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YpkiJzPgQEQemUNbvrW9vvCV1qs5B679VnXdQ4CsOGpB9iQcyrStpla/CqAj1gDGnYVDeVgpYtSh5Zo/2o3ZbVSZ2jVZVavWWC9OLA3YdVFyBQZuVXvSwam0EqI+idlzo+4IsiFu9nmZh8a3mpJ58kBI9CFCQ9uTZigvo9z4vrrBTIZxgazLP5b+b62wcPWYftVW9VPDs9YrBCpEsUbswpK8E+jsV3ehmBZKvQDwt226EWZ+1i0cye7FODflKlKi16A0Js8R28QE2FMhLNMjLF1pgKYnbJBQ1rhopbtyAdxtYEs7w+12I249qw0RUmuciNep88MjWWRqTl2LFCMuew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3117ocJD8AohAUBL8QiIptkwjBvTS/BYiXUa9+8yrLo=; b=GBUa8pY8Nyi9Lhqd1d0HYTXA7c0zQyOvvNuxFIvABuJFQ03JUCvnJ0KSIjr8ZEYbv8b0nTUu+9C8XFtMqWHb0e26aRMizlJNCXu/AQT2HHSPZMBy91BSihL6m8QCs572177sIItVTacleJn7uFBpkAuF9D/zvrHtI+MVuVct4b4c5CZf14KO3+c2XVLbKzzKGYMoznNRBOCGhqBoWxvS3nItj558fL0hWThQEDaXwHc9xPIXiB1VrLTiUaWVjo9Z7MP+yk7m47/Li0EPOPtq9pKTyB0fw98KCRGK/JB36EMwKb8qHy3cvz4uQCDk6S7CoHlRDI2PcKFLlduOSVV0jQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 149.6.166.170) smtp.rcpttodomain=chromium.org smtp.mailfrom=softathome.com; dmarc=bestguesspass action=none header.from=softathome.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=softathome1.onmicrosoft.com; s=selector1-softathome1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3117ocJD8AohAUBL8QiIptkwjBvTS/BYiXUa9+8yrLo=; b=bRRcceV4Wot15U/Iwf+oE4EUEIo+JOW/wrIo8pv4H0fiqUI0W6bZSt+BMD9aO0arGqYvJkIMMg/ipS1FqRxDwyO3Uh2yXAV2CQYYu4JGgDBslJQzNl2YRBEIgvDMc3CgHUtV1l9Ysj4Rx5bU+mFqZViigs6qrY07gawHbRVK7T7gb2lip9993ZHnk+Z4fNaHtIbpA2CjfQKoGEvxbwMhZu8wYQzko1tOaFTdc0L4viwqUr5/eckmVt/cN/C119MgSWOoMNhgICpI9EVWcZ37oINATTFyG+Lv5XJSfUcO7+flOuvsUdPQ/hXTBgVGYD0EldB9sf0b7Cp6ZsLQdRI8bQ== Received: from PR0P264CA0238.FRAP264.PROD.OUTLOOK.COM (2603:10a6:100:1e::34) by PR1P264MB4135.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:253::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.24; Fri, 25 Feb 2022 14:57:59 +0000 Received: from PR2FRA01FT005.eop-fra01.prod.protection.outlook.com (2603:10a6:100:1e:cafe::b4) by PR0P264CA0238.outlook.office365.com (2603:10a6:100:1e::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.22 via Frontend Transport; Fri, 25 Feb 2022 14:57:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 149.6.166.170) smtp.mailfrom=softathome.com; dkim=none (message not signed) header.d=none;dmarc=bestguesspass action=none header.from=softathome.com; Received-SPF: Pass (protection.outlook.com: domain of softathome.com designates 149.6.166.170 as permitted sender) receiver=protection.outlook.com; client-ip=149.6.166.170; helo=proxy.softathome.com; Received: from proxy.softathome.com (149.6.166.170) by PR2FRA01FT005.mail.protection.outlook.com (10.152.48.98) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.22 via Frontend Transport; Fri, 25 Feb 2022 14:57:59 +0000 Received: from localhost.localdomain (unknown [192.168.72.32]) by proxy.softathome.com (Postfix) with ESMTPSA id F295720037; Fri, 25 Feb 2022 15:57:58 +0100 (CET) From: Philippe Reynes To: sjg@chromium.org, rasmus.villemoes@prevas.dk Cc: u-boot@lists.denx.de, Philippe Reynes Subject: [PATCH v6 10/16] mkimage: add public key for image pre-load stage Date: Fri, 25 Feb 2022 15:57:48 +0100 Message-Id: <20220225145754.30217-11-philippe.reynes@softathome.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220225145754.30217-1-philippe.reynes@softathome.com> References: <20220225145754.30217-1-philippe.reynes@softathome.com> X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email MIME-Version: 1.0 Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: 42a3a127-cf2d-42be-118b-08d9f86f36ac X-MS-TrafficTypeDiagnostic: PR1P264MB4135:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qVpn0c10+BZOxwGPduSivGjDLkTVjWcrTRd213Wc29bR56jpPmTr1fz8p+s13PKxrJ5q21HV0AgSKgsI7/Z9iwGDB45p2QIHFBU/qhD1pVUZnZhZxThF6NWoPqzqHs8Urslx3se1jUNbngdAjufIBlzP0X7taXegLhtDIQ6Yx0KO6Y6DitOK6AcS5AF4t/D1SsbOQV4hJ+Miw7U3gv9pd0wVwd/PsQOvBmOAPiiBIlKt/5wG1pZVpF5TKeZPLnR4nHFPE5e4J5T9ZtHi+5d8Q52bd5MHVraGDRCtIE/80wvaGwDOBJ8tgQwv1QhT8XMfXl3IA5JBGuLMZtFjHZ94YPquukTYMgksh4akidjYIyAQ22nIbuzl+mVrYQLKu9PH1L6WUPXJyDXp/nit+5QpEwhKITml3llotl4+Dwm1j3CINoZKeu2QMt0ssGU/2TWphE62sHBlTOGGP0FLS/y7yjtaalqskf+MI3MByyEhVAUaD/uAb7FBl6isgEhJWYrfyyurYiicQ9yJe74pCGrBcdBzsVWw2SqySHuvwdbr4gilWywkEaE92XhB2C/+hevV5puOhBIjJvr3yZqNTYsEDDJnptko1Z785+xMiOXrGPjniwwXkSaZV4SyL0Sgwh+327BZxZPPesoP0U/Ewhqc2mY+PRBl3Sa+7BQIkb3k9XVs2kuktVLh9aUWAdiau9r11fB3L33SkOgxgs4jISW9Gw== X-Forefront-Antispam-Report: CIP:149.6.166.170; CTRY:FR; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:proxy.softathome.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(40460700003)(1076003)(107886003)(2616005)(82310400004)(6666004)(508600001)(82960400001)(81166007)(356005)(426003)(83380400001)(86362001)(6266002)(36860700001)(47076005)(336012)(4326008)(8676002)(26005)(6966003)(316002)(70586007)(70206006)(36756003)(186003)(5660300002)(8936002)(44832011)(2906002)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: softathome.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2022 14:57:59.1472 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 42a3a127-cf2d-42be-118b-08d9f86f36ac X-MS-Exchange-CrossTenant-Id: aa10e044-e405-4c10-8353-36b4d0cce511 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=aa10e044-e405-4c10-8353-36b4d0cce511; Ip=[149.6.166.170]; Helo=[proxy.softathome.com] X-MS-Exchange-CrossTenant-AuthSource: PR2FRA01FT005.eop-fra01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR1P264MB4135 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 This commit enhances mkimage to update the node /image/pre-load/sig with the public key. Signed-off-by: Philippe Reynes --- include/image.h | 15 ++++++ tools/fit_image.c | 3 ++ tools/image-host.c | 114 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) diff --git a/include/image.h b/include/image.h index 496b7af3f3..498eb7f2e3 100644 --- a/include/image.h +++ b/include/image.h @@ -1019,6 +1019,21 @@ int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, int fit_set_timestamp(void *fit, int noffset, time_t timestamp); +/** + * fit_pre_load_data() - add public key to fdt blob + * + * Adds public key to the node pre load. + * + * @keydir: Directory containing keys + * @keydest: FDT blob to write public key + * @fit: Pointer to the FIT format image header + * + * returns: + * 0, on success + * < 0, on failure + */ +int fit_pre_load_data(const char *keydir, void *keydest, void *fit); + int fit_cipher_data(const char *keydir, void *keydest, void *fit, const char *comment, int require_keys, const char *engine_id, const char *cmdname); diff --git a/tools/fit_image.c b/tools/fit_image.c index 15f7c82d61..1884a2eb0b 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -59,6 +59,9 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, ret = fit_set_timestamp(ptr, 0, time); } + if (!ret) + ret = fit_pre_load_data(params->keydir, dest_blob, ptr); + if (!ret) { ret = fit_cipher_data(params->keydir, dest_blob, ptr, params->comment, diff --git a/tools/image-host.c b/tools/image-host.c index eaeb76545c..ab6f756cf1 100644 --- a/tools/image-host.c +++ b/tools/image-host.c @@ -14,6 +14,11 @@ #include #include +#include +#include + +#define IMAGE_PRE_LOAD_PATH "/image/pre-load/sig" + /** * fit_set_hash_value - set hash value in requested has node * @fit: pointer to the FIT format image header @@ -1111,6 +1116,115 @@ static int fit_config_add_verification_data(const char *keydir, return 0; } +/* + * 0) open file (open) + * 1) read certificate (PEM_read_X509) + * 2) get public key (X509_get_pubkey) + * 3) provide der format (d2i_RSAPublicKey) + */ +static int read_pub_key(const char *keydir, const void *name, + unsigned char **pubkey, int *pubkey_len) +{ + char path[1024]; + EVP_PKEY *key = NULL; + X509 *cert; + FILE *f; + int ret; + + memset(path, 0, 1024); + snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name); + + /* Open certificate file */ + f = fopen(path, "r"); + if (!f) { + fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", + path, strerror(errno)); + return -EACCES; + } + + /* Read the certificate */ + cert = NULL; + if (!PEM_read_X509(f, &cert, NULL, NULL)) { + printf("Couldn't read certificate"); + ret = -EINVAL; + goto err_cert; + } + + /* Get the public key from the certificate. */ + key = X509_get_pubkey(cert); + if (!key) { + printf("Couldn't read public key\n"); + ret = -EINVAL; + goto err_pubkey; + } + + /* Get DER form */ + ret = i2d_PublicKey(key, pubkey); + if (ret < 0) { + printf("Couldn't get DER form\n"); + ret = -EINVAL; + goto err_pubkey; + } + + *pubkey_len = ret; + ret = 0; + +err_pubkey: + X509_free(cert); +err_cert: + fclose(f); + return ret; +} + +int fit_pre_load_data(const char *keydir, void *keydest, void *fit) +{ + int pre_load_noffset; + const void *algo_name; + const void *key_name; + unsigned char *pubkey = NULL; + int ret, pubkey_len; + + if (!keydir || !keydest || !fit) + return 0; + + /* Search node pre-load sig */ + pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH); + if (pre_load_noffset < 0) { + ret = 0; + goto out; + } + + algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL); + key_name = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL); + + /* Check that all mandatory properties are present */ + if (!algo_name || !key_name) { + if (!algo_name) + printf("The property algo-name is missing in the node %s\n", + IMAGE_PRE_LOAD_PATH); + if (!key_name) + printf("The property key-name is missing in the node %s\n", + IMAGE_PRE_LOAD_PATH); + ret = -ENODATA; + goto out; + } + + /* Read public key */ + ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len); + if (ret < 0) + goto out; + + /* Add the public key to the device tree */ + ret = fdt_setprop(keydest, pre_load_noffset, "public-key", + pubkey, pubkey_len); + if (ret) + printf("Can't set public-key in node %s (ret = %d)\n", + IMAGE_PRE_LOAD_PATH, ret); + + out: + return ret; +} + int fit_cipher_data(const char *keydir, void *keydest, void *fit, const char *comment, int require_keys, const char *engine_id, const char *cmdname) -- 2.17.1