From patchwork Wed May 12 04:57:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 435448 Delivered-To: patch@linaro.org Received: by 2002:a02:c901:0:0:0:0:0 with SMTP id t1csp4488835jao; Tue, 11 May 2021 21:58:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx095DPnfoYKd0uDtEhLrTRPrKwaqM1qdlp7JNaP9COxH323p/XM8avD5RNqEUmFBh2Mz4m X-Received: by 2002:a17:906:2ac5:: with SMTP id m5mr35668124eje.517.1620795522082; Tue, 11 May 2021 21:58:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620795522; cv=none; d=google.com; s=arc-20160816; b=XVpv7bS63jRtcJGZHVcGAu2F9mTPD3UjY73BeKMNVi2CxOq0f2a5IsFZujCuuXGS9/ 5Vqdvq5waWhE/ABZ2rPj+/Snd19ZnBCWZsYxSx9XiPhNKIn1YU/exdY5tiL/LvCXiEBS aV5hanVWOmqiuSNGzJngpNbSTYFBQ06fbR8ejavi8HPKPTg8OSsPZyz2MhxeFfnNxV9n LCdTgGevgLAUc66DIoS8rKJPZWLKS6UvVhlgahn4FjABXdvQTLNAGAOGCmoKxBwjskDj dXaCGMP9H7kB9PdduM8FRd27aikFQbEDrm2XzFB+/J4fTYveAv3MFiN73yJB98jEWgTn G5yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=FOJO8TiCIMmh8k/6j+zK4rWzed+FALcjnFWUTqW25IQ=; b=ofnhiuKyxMCXA6EMWwFN/Kzvlocgr5luGbaL/iZb7g7RRr48SizFgYNudd9NKIN11+ Y14sUxFkiwEKT4f1Urr8yWfx6Qft4oVjJont9HIbnhV3R+2y6s7p6gf/Y2nt0TMxmakH ruik2khFB5ZH7HYR/E8c92uOoIAMI5qpfJ1I1Wiyl2R3LnQYJ7Xxy1rO/80MbG9/bp6U NmIOgIcaHr97Tb4oefeepvCG4cHLQ3F+915fTbTkqgdnEfF9iHdOwlmmYSO50/CVArU+ El28twsJ3vTdJAos1NGjj0qE2kqy++vdjPhGvZjknhFrB2QLTcJjtLj/48itUPROKdn2 7aBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=xYuvIC7+; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id j6si4950779edv.294.2021.05.11.21.58.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 21:58:42 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=xYuvIC7+; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C54B882D3A; Wed, 12 May 2021 06:58:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org 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=linaro.org header.i=@linaro.org header.b="xYuvIC7+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B4D8882D1C; Wed, 12 May 2021 06:58:30 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) (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 1EAD082AF0 for ; Wed, 12 May 2021 06:58:26 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pg1-x533.google.com with SMTP id i14so17336653pgk.5 for ; Tue, 11 May 2021 21:58:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FOJO8TiCIMmh8k/6j+zK4rWzed+FALcjnFWUTqW25IQ=; b=xYuvIC7+Wtys3DTtQsb0e9kqC8rjCyHfBoCohThEHiDcbAy/b/UwcyHzdlvsiLrxTa rT/hEYMezhHla8Y2CiPeldi8AiKhQ4TC4fBJjO2rVgrD4yu5qbxw5Z9NMZjY/zzqZwv0 9lI+0dq6XfyYIgFLJGi3bk+HCB1FXLcg22HEhq3lhecqnQC4E8i0Zdecak/BezNLTEgu T80af8zrL3Ga3FdHu1cWyX1x+SYuet8KCv3ZdeQOkzcwRlBC+B2u792/N8zNCH4OgXLW 1pXxC9ReqUhpU5DTH5NWfd9FuDROH5CadUK33objsmof0X6A6ZdXFaR2Ht7qcGocH32x cJ5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FOJO8TiCIMmh8k/6j+zK4rWzed+FALcjnFWUTqW25IQ=; b=Lg9hm8HPSx0pHadCb1MzOT/pP1eodyHPEmos3lKhXj54Bs9pTZAisqzELbv965VbeV Tm9aK/Quj4pLqYxTCkzgAauG4AiVBWxoAq+4UQMBammp2/0KYAt//sTK0aRAJlCNSRWn jyQ8kDfrgU3z3Hew+sLnDjVwROi3FscxCQ/LlNuJsQNTOCpf03DTTBvACoKNF2pLMbXo bVqDAjE4K0fo9boCFjHUtVZgyLATiph2kvtzdLstcr3VN+9UBPC/Gt+WPfuH4TBvLous TQU7ussivXBAnvxH8UDt8nbBVP6nFm5y32e0kgmDAFyt7ZCzpTbcN2Bf/Y1AScJB1ZdW V1ug== X-Gm-Message-State: AOAM530obJbWwIutC1rtWO4M0bFRpUQTXSDZ8lYWfSzDtGOG6ikdBrKY g4x67UGJNSfwRKwSP/ntlefN3g== X-Received: by 2002:a63:490:: with SMTP id 138mr33833272pge.99.1620795504462; Tue, 11 May 2021 21:58:24 -0700 (PDT) Received: from localhost.localdomain (p3dd30534.tkyea130.ap.so-net.ne.jp. [61.211.5.52]) by smtp.gmail.com with ESMTPSA id q194sm15188202pfc.62.2021.05.11.21.58.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 21:58:24 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, masami.hiramatsu@linaro.org, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH 1/4] tools: mkeficapsule: add firmwware image signing Date: Wed, 12 May 2021 13:57:50 +0900 Message-Id: <20210512045753.62288-2-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210512045753.62288-1-takahiro.akashi@linaro.org> References: <20210512045753.62288-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.4 at phobos.denx.de X-Virus-Status: Clean With this enhancement, mkeficapsule will be able to create a capsule file with a signature which will be verified later by FMP's SetImage(). We will have to specify addtional command parameters: -monotonic-cout : monotonic count -private-key : private key file -certificate : certificate file Only when those parameters are given, a signature will be added to a capsule file. Users are expected to maintain the monotonic count for each firmware image. Signed-off-by: AKASHI Takahiro --- tools/Makefile | 4 + tools/mkeficapsule.c | 324 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 303 insertions(+), 25 deletions(-) -- 2.31.0 diff --git a/tools/Makefile b/tools/Makefile index d020c55d6644..02eae0286e20 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -231,6 +231,10 @@ hostprogs-$(CONFIG_MIPS) += mips-relocs hostprogs-$(CONFIG_ASN1_COMPILER) += asn1_compiler HOSTCFLAGS_asn1_compiler.o = -idirafter $(srctree)/include +ifneq ($(CONFIG_EFI_CAPSULE_AUTHENTICATE),) +HOSTLDLIBS_mkeficapsule += \ + $(shell pkg-config --libs libssl libcrypto 2> /dev/null || echo "-lssl -lcrypto") +endif mkeficapsule-objs := mkeficapsule.o $(LIBFDT_OBJS) hostprogs-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += mkeficapsule diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c index de0a62898886..34ff1bdd82eb 100644 --- a/tools/mkeficapsule.c +++ b/tools/mkeficapsule.c @@ -18,7 +18,17 @@ #include #include -#include "fdt_host.h" +#include +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) +#include +#include +#include +#include +#include +#include +#endif + +#include typedef __u8 u8; typedef __u16 u16; @@ -46,6 +56,13 @@ efi_guid_t efi_guid_image_type_uboot_fit = EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID; efi_guid_t efi_guid_image_type_uboot_raw = EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID; +efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID; + +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) +static const char *opts_short = "f:r:i:I:v:D:K:P:C:m:dOh"; +#else +static const char *opts_short = "f:r:i:I:v:D:K:Oh"; +#endif static struct option options[] = { {"fit", required_argument, NULL, 'f'}, @@ -54,6 +71,12 @@ static struct option options[] = { {"instance", required_argument, NULL, 'I'}, {"dtb", required_argument, NULL, 'D'}, {"public key", required_argument, NULL, 'K'}, +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) + {"private-key", required_argument, NULL, 'P'}, + {"certificate", required_argument, NULL, 'C'}, + {"monotonic-count", required_argument, NULL, 'm'}, + {"dump-sig", no_argument, NULL, 'd'}, +#endif {"overlay", no_argument, NULL, 'O'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0}, @@ -70,6 +93,12 @@ static void print_usage(void) "\t-I, --instance update hardware instance\n" "\t-K, --public-key public key esl file\n" "\t-D, --dtb dtb file\n" +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) + "\t-P, --private-key private key file\n" + "\t-C, --certificate signer's certificate file\n" + "\t-m, --monotonic-count monotonic count\n" + "\t-d, --dump_sig dump signature (*.p7)\n" +#endif "\t-O, --overlay the dtb file is an overlay\n" "\t-h, --help print a help message\n", tool_name); @@ -249,12 +278,167 @@ err: return ret; } +struct auth_context { + char *key_file; + char *cert_file; + u8 *image_data; + size_t image_size; + struct efi_firmware_image_authentication auth; + u8 *sig_data; + size_t sig_size; +}; + +static int dump_sig; + +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) +static EVP_PKEY *fileio_read_pkey(const char *filename) +{ + EVP_PKEY *key = NULL; + BIO *bio; + + bio = BIO_new_file(filename, "r"); + if (!bio) + goto out; + + key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); + +out: + BIO_free_all(bio); + if (!key) { + printf("Can't load key from file '%s'\n", filename); + ERR_print_errors_fp(stderr); + } + + return key; +} + +static X509 *fileio_read_cert(const char *filename) +{ + X509 *cert = NULL; + BIO *bio; + + bio = BIO_new_file(filename, "r"); + if (!bio) + goto out; + + cert = PEM_read_bio_X509(bio, NULL, NULL, NULL); + +out: + BIO_free_all(bio); + if (!cert) { + printf("Can't load certificate from file '%s'\n", filename); + ERR_print_errors_fp(stderr); + } + + return cert; +} + +static int create_auth_data(struct auth_context *ctx) +{ + EVP_PKEY *key = NULL; + X509 *cert = NULL; + BIO *data_bio = NULL; + const EVP_MD *md; + PKCS7 *p7; + int flags, ret = -1; + + OpenSSL_add_all_digests(); + OpenSSL_add_all_ciphers(); + ERR_load_crypto_strings(); + + key = fileio_read_pkey(ctx->key_file); + if (!key) + goto err; + cert = fileio_read_cert(ctx->cert_file); + if (!cert) + goto err; + + /* + * create a BIO, containing: + * * firmware image + * * monotonic count + * in this order! + * See EDK2's FmpAuthenticatedHandlerRsa2048Sha256() + */ + data_bio = BIO_new(BIO_s_mem()); + BIO_write(data_bio, ctx->image_data, ctx->image_size); + BIO_write(data_bio, &ctx->auth.monotonic_count, + sizeof(ctx->auth.monotonic_count)); + + md = EVP_get_digestbyname("SHA256"); + if (!md) + goto err; + + /* create signature */ + /* TODO: maybe add PKCS7_NOATTR and PKCS7_NOSMIMECAP */ + flags = PKCS7_BINARY | PKCS7_DETACHED; + p7 = PKCS7_sign(NULL, NULL, NULL, data_bio, flags | PKCS7_PARTIAL); + if (!p7) + goto err; + if (!PKCS7_sign_add_signer(p7, cert, key, md, flags)) + goto err; + if (!PKCS7_final(p7, data_bio, flags)) + goto err; + + /* convert pkcs7 into DER */ + ctx->sig_data = NULL; + ctx->sig_size = ASN1_item_i2d((ASN1_VALUE *)p7, &ctx->sig_data, + ASN1_ITEM_rptr(PKCS7)); + if (!ctx->sig_size) + goto err; + + /* fill auth_info */ + ctx->auth.auth_info.hdr.dwLength = sizeof(ctx->auth.auth_info) + + ctx->sig_size; + ctx->auth.auth_info.hdr.wRevision = WIN_CERT_REVISION_2_0; + ctx->auth.auth_info.hdr.wCertificateType = WIN_CERT_TYPE_EFI_GUID; + memcpy(&ctx->auth.auth_info.cert_type, &efi_guid_cert_type_pkcs7, + sizeof(efi_guid_cert_type_pkcs7)); + + ret = 0; +err: + BIO_free_all(data_bio); + EVP_PKEY_free(key); + X509_free(cert); + + return ret; +} + +static int dump_signature(const char *path, u8 *signature, size_t sig_size) +{ + char *sig_path; + FILE *f; + size_t size; + int ret = -1; + + sig_path = malloc(strlen(path) + 3 + 1); + if (!sig_path) + return ret; + + sprintf(sig_path, "%s.p7", path); + f = fopen(sig_path, "w"); + if (!f) + goto err; + + size = fwrite(signature, 1, sig_size, f); + if (size == sig_size) + ret = 0; + + fclose(f); +err: + free(sig_path); + return ret; +} +#endif + static int create_fwbin(char *path, char *bin, efi_guid_t *guid, - unsigned long index, unsigned long instance) + unsigned long index, unsigned long instance, + uint64_t mcount, char *privkey_file, char *cert_file) { struct efi_capsule_header header; struct efi_firmware_management_capsule_header capsule; struct efi_firmware_management_capsule_image_header image; + struct auth_context auth_context; FILE *f, *g; struct stat bin_stat; u8 *data; @@ -266,6 +450,7 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, printf("\tbin: %s\n\ttype: %pUl\n", bin, guid); printf("\tindex: %ld\n\tinstance: %ld\n", index, instance); #endif + auth_context.sig_size = 0; g = fopen(bin, "r"); if (!g) { @@ -281,11 +466,36 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, printf("cannot allocate memory: %zx\n", (size_t)bin_stat.st_size); goto err_1; } - f = fopen(path, "w"); - if (!f) { - printf("cannot open %s\n", path); + + size = fread(data, 1, bin_stat.st_size, g); + if (size < bin_stat.st_size) { + printf("read failed (%zx)\n", size); goto err_2; } + + /* first, calculate signature to determine its size */ +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) + if (privkey_file && cert_file) { + auth_context.key_file = privkey_file; + auth_context.cert_file = cert_file; + auth_context.auth.monotonic_count = mcount; + auth_context.image_data = data; + auth_context.image_size = bin_stat.st_size; + + if (create_auth_data(&auth_context)) { + printf("Signing firmware image failed\n"); + goto err_3; + } + + if (dump_sig && + dump_signature(path, auth_context.sig_data, + auth_context.sig_size)) { + printf("Creating signature file failed\n"); + goto err_3; + } + } +#endif + header.capsule_guid = efi_guid_fm_capsule; header.header_size = sizeof(header); /* TODO: The current implementation ignores flags */ @@ -294,11 +504,20 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, + sizeof(capsule) + sizeof(u64) + sizeof(image) + bin_stat.st_size; + if (auth_context.sig_size) + header.capsule_image_size += sizeof(auth_context.auth) + + auth_context.sig_size; + + f = fopen(path, "w"); + if (!f) { + printf("cannot open %s\n", path); + goto err_3; + } size = fwrite(&header, 1, sizeof(header), f); if (size < sizeof(header)) { printf("write failed (%zx)\n", size); - goto err_3; + goto err_4; } capsule.version = 0x00000001; @@ -307,13 +526,13 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, size = fwrite(&capsule, 1, sizeof(capsule), f); if (size < (sizeof(capsule))) { printf("write failed (%zx)\n", size); - goto err_3; + goto err_4; } offset = sizeof(capsule) + sizeof(u64); size = fwrite(&offset, 1, sizeof(offset), f); if (size < sizeof(offset)) { printf("write failed (%zx)\n", size); - goto err_3; + goto err_4; } image.version = 0x00000003; @@ -323,34 +542,61 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, image.reserved[1] = 0; image.reserved[2] = 0; image.update_image_size = bin_stat.st_size; + if (auth_context.sig_size) + image.update_image_size += sizeof(auth_context.auth) + + auth_context.sig_size; image.update_vendor_code_size = 0; /* none */ image.update_hardware_instance = instance; image.image_capsule_support = 0; + if (auth_context.sig_size) + image.image_capsule_support |= CAPSULE_SUPPORT_AUTHENTICATION; size = fwrite(&image, 1, sizeof(image), f); if (size < sizeof(image)) { printf("write failed (%zx)\n", size); - goto err_3; + goto err_4; } - size = fread(data, 1, bin_stat.st_size, g); - if (size < bin_stat.st_size) { - printf("read failed (%zx)\n", size); - goto err_3; + +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) + if (auth_context.sig_size) { + size = fwrite(&auth_context.auth, 1, + sizeof(auth_context.auth), f); + if (size < sizeof(auth_context.auth)) { + printf("write failed (%zx)\n", size); + goto err_4; + } + size = fwrite(auth_context.sig_data, 1, + auth_context.sig_size, f); + if (size < auth_context.sig_size) { + printf("write failed (%zx)\n", size); + goto err_4; + } } +#endif + size = fwrite(data, 1, bin_stat.st_size, f); if (size < bin_stat.st_size) { printf("write failed (%zx)\n", size); - goto err_3; + goto err_4; } fclose(f); fclose(g); free(data); +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) + if (auth_context.sig_size) + OPENSSL_free(auth_context.sig_data); +#endif return 0; -err_3: +err_4: fclose(f); +err_3: +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) + if (auth_context.sig_size) + OPENSSL_free(auth_context.sig_data); +#endif err_2: free(data); err_1: @@ -359,10 +605,6 @@ err_1: return -1; } -/* - * Usage: - * $ mkeficapsule -f - */ int main(int argc, char **argv) { char *file; @@ -370,6 +612,8 @@ int main(int argc, char **argv) char *dtb_file; efi_guid_t *guid; unsigned long index, instance; + uint64_t mcount; + char *privkey_file, *cert_file; int c, idx; int ret; bool overlay = false; @@ -380,8 +624,12 @@ int main(int argc, char **argv) guid = NULL; index = 0; instance = 0; + mcount = 0; + privkey_file = NULL; + cert_file = NULL; + dump_sig = 0; for (;;) { - c = getopt_long(argc, argv, "f:r:i:I:v:D:K:Oh", options, &idx); + c = getopt_long(argc, argv, opts_short, options, &idx); if (c == -1) break; @@ -422,6 +670,28 @@ int main(int argc, char **argv) } dtb_file = optarg; break; +#if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) + case 'P': + if (privkey_file) { + printf("Private Key already specified\n"); + return -1; + } + privkey_file = optarg; + break; + case 'C': + if (cert_file) { + printf("Certificate file already specified\n"); + return -1; + } + cert_file = optarg; + break; + case 'm': + mcount = strtoul(optarg, NULL, 0); + break; + case 'd': + dump_sig = 1; + break; +#endif case 'O': overlay = true; break; @@ -431,8 +701,12 @@ int main(int argc, char **argv) } } - /* need a fit image file or raw image file */ - if (!file && !pkey_file && !dtb_file) { + /* check necessary parameters */ + if ((file && (!(optind < argc) || + (privkey_file && !cert_file) || + (!privkey_file && cert_file))) || + ((pkey_file && !dtb_file) || + (!pkey_file && dtb_file))) { print_usage(); exit(EXIT_FAILURE); } @@ -442,12 +716,12 @@ int main(int argc, char **argv) if (ret == -1) { printf("Adding public key to the dtb failed\n"); exit(EXIT_FAILURE); - } else { - exit(EXIT_SUCCESS); } } - if (create_fwbin(argv[optind], file, guid, index, instance) + if (optind < argc && + create_fwbin(argv[optind], file, guid, index, instance, + mcount, privkey_file, cert_file) < 0) { printf("Creating firmware capsule failed\n"); exit(EXIT_FAILURE); From patchwork Wed May 12 04:57:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 435449 Delivered-To: patch@linaro.org Received: by 2002:a02:c901:0:0:0:0:0 with SMTP id t1csp4488913jao; Tue, 11 May 2021 21:58:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfPXqXzSqZAMOHQo2WtEy2dk+6lxDcCoaxoLSr7rgj9S3YF1p7Zh7i7OacZhlrU1xMpMfH X-Received: by 2002:a17:906:80cd:: with SMTP id a13mr35230879ejx.109.1620795531977; Tue, 11 May 2021 21:58:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620795531; cv=none; d=google.com; s=arc-20160816; b=Jl1uEADZBRRT3/Qnr2kF+nXZR7lcP/rM3lnmVT6RvVXLt0rz39x8y8zQzQ/8gNIrln MrH0v6oZPUFz/f7nqQrEU9DPi+6m6PLg9W78mZU7t1Pgd5xc8/qYP5o67BXA4QMG4zfG w/1BuRgk0mGTxV9YLRPBmZg8Hhq+Tmwa6wlsmlICmnOZG1UzuA727lVBaR5+5R2/ul8I XCTzr9/7XvcgM0m9oAR6yrIvAM7sufcbV9no6JIHPh861F2DAq/2ttM9KIn7fhYD3xO2 46xM1IehH+oL1NIBwXb4AaAzNNfCRCGLP2wUzvaYwHdxm5LrHL+eneWWaJxH0QknrNhe 9aZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zROjG3ewo5uhLB4jv/8e+Slu5BDSwvAWWVZQ1Gf79ZE=; b=yXNa9fJy5S2uoG31tACw0t1+qFX/B93+FAoHy2LItiuqhNPYYtVWXX2jM/kf8vOGfU ki8i5Qun70blcUog+5XwBYiRhpZgZo1dIsVHWTEcqvamxunbyDF+ZHCRb6JSicpCEAtU dmS2zHR/4K1P0LJzw8UxlAgDoURvF/NEwEW1xgRplrMPzlvzUev0Ev6KSAQ3xcvaNbIW NrFNQYyL/1Z67O0Xk+y64xyY8eSbLkZPGwU7qMhUvDiUhS3BMELM6X6uwLmyVStXkD1M fGYFChYmFGVP5Ma0xq2Lx2+k++BqZ8H5jcFJqtcMnTDld/SP3wQv1y6YXwvYO19jBsaP 9TMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=T9z0Nc96; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id v18si7195297ejd.644.2021.05.11.21.58.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 21:58:51 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=T9z0Nc96; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1937B82D4C; Wed, 12 May 2021 06:58:37 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org 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=linaro.org header.i=@linaro.org header.b="T9z0Nc96"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2C2C382D13; Wed, 12 May 2021 06:58:33 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) (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 DEBB282D13 for ; Wed, 12 May 2021 06:58:28 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pl1-x62b.google.com with SMTP id 69so7296979plc.5 for ; Tue, 11 May 2021 21:58:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zROjG3ewo5uhLB4jv/8e+Slu5BDSwvAWWVZQ1Gf79ZE=; b=T9z0Nc96mSBfKc5hiyH2WmTJBrX/UlNVRyxht7sKZUSDfm+QkjLINdEODAiUw60zoj vzZJzYmqTVjpKWBH41yQWUjENp+Fkce1nwkcVMHZgw3GJ3Kq4DnlEQdr1Is2G23ot5I5 jyW2mYv+IVA38Nz9nQdGJodj+57H8muZMyQ0nZXjYJwGU9KAiXQrQnWiNYYUbiaJhvDi B9RxwzECOpMOYjQbSU23HYNsc1xUChSBuVHGH8G4E30Zjf8ObJn5EkMs+sxLT36+el30 ysNpR7FFfPvBYYHwmR0Ka5T8jBWrBaANFvn4pZ55YVbX4OpDTJ2L3E/a30P5pRTuF18f xBNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zROjG3ewo5uhLB4jv/8e+Slu5BDSwvAWWVZQ1Gf79ZE=; b=UPYXu+c9CHh2JABAYaxqheVZBqz6BrVJk4lQf/cUcI/3UNtbPQYPzpg4+BtpaAdHke FbH9J2nBYb1naviC1sBLZc9Q7l5qpUVo7ZfyDVXrTK9a/Tbvtc2Q1wLX2wh7sn0JTQP0 gF6+/hSioWUJTBYdga8yUPuU1ayll2sLYFvqcKGsQ/VIPPQ56jAcybsvNtVPmRHdoCBm C5kUR+69C6l5+9WKfKYWc/KaUqRbn4NffIAYxE+3ygSxYMUDXarlxZtavUD1ONg+O/d/ oOXoEZmqTTsMZfZdxFV2KJvd02VFlCjFwiGYmZ415qpsc9UGDxNdjJW9wJMtcKXjJ5qB mWQw== X-Gm-Message-State: AOAM533hVqZjXFWq/sN4qBXeS2grJmL2NepzKnWkN6wt0x7k7AaDr4R1 lXuurM/mkL0+Uw8zf22Ga7guMQ== X-Received: by 2002:a17:90a:fa0e:: with SMTP id cm14mr8876098pjb.59.1620795507163; Tue, 11 May 2021 21:58:27 -0700 (PDT) Received: from localhost.localdomain (p3dd30534.tkyea130.ap.so-net.ne.jp. [61.211.5.52]) by smtp.gmail.com with ESMTPSA id q194sm15188202pfc.62.2021.05.11.21.58.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 21:58:26 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, masami.hiramatsu@linaro.org, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH 2/4] tools: mkeficapsule: remove device-tree related operation Date: Wed, 12 May 2021 13:57:51 +0900 Message-Id: <20210512045753.62288-3-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210512045753.62288-1-takahiro.akashi@linaro.org> References: <20210512045753.62288-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.4 at phobos.denx.de X-Virus-Status: Clean As we discussed, "-K" and "-D" options have nothing to do with creating a capsule file. The same result can be obtained by using standard commands like: === signature.dts === /dts-v1/; /plugin/; &{/} { signature { capsule-key = /incbin/("SIGNER.esl"); }; }; === $ dtc -@ -I dts -O dtb -o signature.dtbo signature.dts $ fdtoverlay -i test.dtb -o test_sig.dtb -v signature.dtbo So just remove this feature. (Effectively revert the commit 322c813f4bec ("mkeficapsule: Add support for embedding public key in a dtb").) The same feature is implemented by a shell script (tools/fdtsig.sh). Signed-off-by: AKASHI Takahiro --- Makefile | 7 +- tools/Makefile | 1 - tools/fdtsig.sh | 40 ++++++++ tools/mkeficapsule.c | 235 ++----------------------------------------- 4 files changed, 50 insertions(+), 233 deletions(-) create mode 100755 tools/fdtsig.sh -- 2.31.0 diff --git a/Makefile b/Makefile index 63b1f0143220..9806464357e0 100644 --- a/Makefile +++ b/Makefile @@ -1015,9 +1015,8 @@ cmd_pad_cat = $(cmd_objcopy) && $(append) || { rm -f $@; false; } quiet_cmd_lzma = LZMA $@ cmd_lzma = lzma -c -z -k -9 $< > $@ -quiet_cmd_mkeficapsule = MKEFICAPSULE $@ -cmd_mkeficapsule = $(objtree)/tools/mkeficapsule -K $(CONFIG_EFI_PKEY_FILE) \ - -D $@ +quiet_cmd_fdtsig = FDTSIG $@ +cmd_fdtsig = $(srctree)/tools/fdtsig.sh $(CONFIG_EFI_PKEY_FILE) $@ cfg: u-boot.cfg @@ -1114,7 +1113,7 @@ dtbs: dts/dt.dtb ifeq ($(CONFIG_EFI_CAPSULE_AUTHENTICATE)$(CONFIG_EFI_PKEY_DTB_EMBED),yy) dts/dt.dtb: u-boot tools $(Q)$(MAKE) $(build)=dts dtbs - $(call cmd,mkeficapsule) + $(call cmd,fdtsig) else dts/dt.dtb: u-boot $(Q)$(MAKE) $(build)=dts dtbs diff --git a/tools/Makefile b/tools/Makefile index 02eae0286e20..71a52719620c 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -235,7 +235,6 @@ ifneq ($(CONFIG_EFI_CAPSULE_AUTHENTICATE),) HOSTLDLIBS_mkeficapsule += \ $(shell pkg-config --libs libssl libcrypto 2> /dev/null || echo "-lssl -lcrypto") endif -mkeficapsule-objs := mkeficapsule.o $(LIBFDT_OBJS) hostprogs-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += mkeficapsule # We build some files with extra pedantic flags to try to minimize things diff --git a/tools/fdtsig.sh b/tools/fdtsig.sh new file mode 100755 index 000000000000..aaa0a9190845 --- /dev/null +++ b/tools/fdtsig.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0+ +# +# script to add a certificate (efi-signature-list) to dtb blob + +usage() { + if [ -n "$*" ]; then + echo "ERROR: $*" + fi + echo "Usage: "$(basename $0) " " +} + +if [ "$#" -ne 2 ]; then + usage "Arguments missing" + exit 1 +fi + +ESL=$1 +DTB=$2 +NEW_DTB=$(basename $DTB)_tmp +SIG=signature + +cat << 'EOF' > $SIG.dts +/dts-v1/; +/plugin/; + +&{/} { + signature { + capsule-key = /incbin/("ESL"); + }; +}; +EOF + +sed -in "s/ESL/$ESL/" $SIG.dts + +dtc -@ -I dts -O dtb -o $SIG.dtbo $SIG.dts +fdtoverlay -i $DTB -o $NEW_DTB -v $SIG.dtbo +mv $NEW_DTB $DTB + +rm $SIG.dts $SIG.dtbo $NEW_DTB diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c index 34ff1bdd82eb..97ce68ec83ee 100644 --- a/tools/mkeficapsule.c +++ b/tools/mkeficapsule.c @@ -4,17 +4,13 @@ * Author: AKASHI Takahiro */ -#include #include #include #include #include #include #include -#include #include - -#include #include #include @@ -28,8 +24,6 @@ #include #endif -#include - typedef __u8 u8; typedef __u16 u16; typedef __u32 u32; @@ -39,9 +33,6 @@ typedef __s32 s32; #define aligned_u64 __aligned_u64 -#define SIGNATURE_NODENAME "signature" -#define OVERLAY_NODENAME "__overlay__" - #ifndef __packed #define __packed __attribute__((packed)) #endif @@ -59,9 +50,9 @@ efi_guid_t efi_guid_image_type_uboot_raw = efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID; #if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) -static const char *opts_short = "f:r:i:I:v:D:K:P:C:m:dOh"; +static const char *opts_short = "f:r:i:I:v:P:C:m:dh"; #else -static const char *opts_short = "f:r:i:I:v:D:K:Oh"; +static const char *opts_short = "f:r:i:I:v:h"; #endif static struct option options[] = { @@ -69,15 +60,12 @@ static struct option options[] = { {"raw", required_argument, NULL, 'r'}, {"index", required_argument, NULL, 'i'}, {"instance", required_argument, NULL, 'I'}, - {"dtb", required_argument, NULL, 'D'}, - {"public key", required_argument, NULL, 'K'}, #if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) {"private-key", required_argument, NULL, 'P'}, {"certificate", required_argument, NULL, 'C'}, {"monotonic-count", required_argument, NULL, 'm'}, {"dump-sig", no_argument, NULL, 'd'}, #endif - {"overlay", no_argument, NULL, 'O'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0}, }; @@ -104,180 +92,6 @@ static void print_usage(void) tool_name); } -static int fdt_add_pub_key_data(void *sptr, void *dptr, size_t key_size, - bool overlay) -{ - int parent; - int ov_node; - int frag_node; - int ret = 0; - - if (overlay) { - /* - * The signature would be stored in the - * first fragment node of the overlay - */ - frag_node = fdt_first_subnode(dptr, 0); - if (frag_node == -FDT_ERR_NOTFOUND) { - fprintf(stderr, - "Couldn't find the fragment node: %s\n", - fdt_strerror(frag_node)); - goto done; - } - - ov_node = fdt_subnode_offset(dptr, frag_node, OVERLAY_NODENAME); - if (ov_node == -FDT_ERR_NOTFOUND) { - fprintf(stderr, - "Couldn't find the __overlay__ node: %s\n", - fdt_strerror(ov_node)); - goto done; - } - } else { - ov_node = 0; - } - - parent = fdt_subnode_offset(dptr, ov_node, SIGNATURE_NODENAME); - if (parent == -FDT_ERR_NOTFOUND) { - parent = fdt_add_subnode(dptr, ov_node, SIGNATURE_NODENAME); - if (parent < 0) { - ret = parent; - if (ret != -FDT_ERR_NOSPACE) { - fprintf(stderr, - "Couldn't create signature node: %s\n", - fdt_strerror(parent)); - } - } - } - if (ret) - goto done; - - /* Write the key to the FDT node */ - ret = fdt_setprop(dptr, parent, "capsule-key", - sptr, key_size); - -done: - if (ret) - ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; - - return ret; -} - -static int add_public_key(const char *pkey_file, const char *dtb_file, - bool overlay) -{ - int ret; - int srcfd = -1; - int destfd = -1; - void *sptr = NULL; - void *dptr = NULL; - off_t src_size; - struct stat pub_key; - struct stat dtb; - - /* Find out the size of the public key */ - srcfd = open(pkey_file, O_RDONLY); - if (srcfd == -1) { - fprintf(stderr, "%s: Can't open %s: %s\n", - __func__, pkey_file, strerror(errno)); - ret = -1; - goto err; - } - - ret = fstat(srcfd, &pub_key); - if (ret == -1) { - fprintf(stderr, "%s: Can't stat %s: %s\n", - __func__, pkey_file, strerror(errno)); - ret = -1; - goto err; - } - - src_size = pub_key.st_size; - - /* mmap the public key esl file */ - sptr = mmap(0, src_size, PROT_READ, MAP_SHARED, srcfd, 0); - if (sptr == MAP_FAILED) { - fprintf(stderr, "%s: Failed to mmap %s:%s\n", - __func__, pkey_file, strerror(errno)); - ret = -1; - goto err; - } - - /* Open the dest FDT */ - destfd = open(dtb_file, O_RDWR); - if (destfd == -1) { - fprintf(stderr, "%s: Can't open %s: %s\n", - __func__, dtb_file, strerror(errno)); - ret = -1; - goto err; - } - - ret = fstat(destfd, &dtb); - if (ret == -1) { - fprintf(stderr, "%s: Can't stat %s: %s\n", - __func__, dtb_file, strerror(errno)); - goto err; - } - - dtb.st_size += src_size + 0x30; - if (ftruncate(destfd, dtb.st_size)) { - fprintf(stderr, "%s: Can't expand %s: %s\n", - __func__, dtb_file, strerror(errno)); - ret = -1; - goto err; - } - - errno = 0; - /* mmap the dtb file */ - dptr = mmap(0, dtb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, - destfd, 0); - if (dptr == MAP_FAILED) { - fprintf(stderr, "%s: Failed to mmap %s:%s\n", - __func__, dtb_file, strerror(errno)); - ret = -1; - goto err; - } - - if (fdt_check_header(dptr)) { - fprintf(stderr, "%s: Invalid FDT header\n", __func__); - ret = -1; - goto err; - } - - ret = fdt_open_into(dptr, dptr, dtb.st_size); - if (ret) { - fprintf(stderr, "%s: Cannot expand FDT: %s\n", - __func__, fdt_strerror(ret)); - ret = -1; - goto err; - } - - /* Copy the esl file to the expanded FDT */ - ret = fdt_add_pub_key_data(sptr, dptr, src_size, overlay); - if (ret < 0) { - fprintf(stderr, "%s: Unable to add public key to the FDT\n", - __func__); - ret = -1; - goto err; - } - - ret = 0; - -err: - if (sptr) - munmap(sptr, src_size); - - if (dptr) - munmap(dptr, dtb.st_size); - - if (srcfd != -1) - close(srcfd); - - if (destfd != -1) - close(destfd); - - return ret; -} - struct auth_context { char *key_file; char *cert_file; @@ -608,19 +422,13 @@ err_1: int main(int argc, char **argv) { char *file; - char *pkey_file; - char *dtb_file; efi_guid_t *guid; unsigned long index, instance; uint64_t mcount; char *privkey_file, *cert_file; int c, idx; - int ret; - bool overlay = false; file = NULL; - pkey_file = NULL; - dtb_file = NULL; guid = NULL; index = 0; instance = 0; @@ -656,20 +464,6 @@ int main(int argc, char **argv) case 'I': instance = strtoul(optarg, NULL, 0); break; - case 'K': - if (pkey_file) { - printf("Public Key already specified\n"); - return -1; - } - pkey_file = optarg; - break; - case 'D': - if (dtb_file) { - printf("DTB file already specified\n"); - return -1; - } - dtb_file = optarg; - break; #if IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) case 'P': if (privkey_file) { @@ -692,9 +486,6 @@ int main(int argc, char **argv) dump_sig = 1; break; #endif - case 'O': - overlay = true; - break; case 'h': print_usage(); return 0; @@ -702,27 +493,15 @@ int main(int argc, char **argv) } /* check necessary parameters */ - if ((file && (!(optind < argc) || - (privkey_file && !cert_file) || - (!privkey_file && cert_file))) || - ((pkey_file && !dtb_file) || - (!pkey_file && dtb_file))) { + if (file && (!(optind < argc) || + (privkey_file && !cert_file) || + (!privkey_file && cert_file))) { print_usage(); exit(EXIT_FAILURE); } - if (pkey_file && dtb_file) { - ret = add_public_key(pkey_file, dtb_file, overlay); - if (ret == -1) { - printf("Adding public key to the dtb failed\n"); - exit(EXIT_FAILURE); - } - } - - if (optind < argc && - create_fwbin(argv[optind], file, guid, index, instance, - mcount, privkey_file, cert_file) - < 0) { + if (create_fwbin(argv[optind], file, guid, index, instance, + mcount, privkey_file, cert_file) < 0) { printf("Creating firmware capsule failed\n"); exit(EXIT_FAILURE); } From patchwork Wed May 12 04:57:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 435450 Delivered-To: patch@linaro.org Received: by 2002:a02:c901:0:0:0:0:0 with SMTP id t1csp4489026jao; Tue, 11 May 2021 21:59:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpoKXd/buLf6e5pLoqbWkO+pJUB3DWhvTRfmUU5FyLYOrAok/Ip4o/adXNiYofACV/BBJU X-Received: by 2002:a05:6402:da:: with SMTP id i26mr4373086edu.379.1620795542019; Tue, 11 May 2021 21:59:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620795542; cv=none; d=google.com; s=arc-20160816; b=DHauL6/T8fFIZH/DBr4IH6OD06eUJVq+iojqCOc4ZXr/n84FxNVnGNZ2e6F6zeIdjJ sQcpY47dK8Y/YPnUwfLCbFoK4g74GMKGxI2rB4syFcuL0QLLKV5AitX/RTXjvknW08jv wGz0fndVjKSBQSTGW/aoK6m1+J3VipP7hfz1xLX0i9VO7HcyycO0vnOXvLB5YEMOz5tV cRgcEI5huZSXvXQs3AUpRtLixlSasHxE+oNw5wy1ZdPKOSZ2wYitAr0WLLUeT3BMdA1G FQQwt9Me5V2F8IwtJlXRfEkOKMF+ph4YZW2f7Q2c70aAOU0mc59gGfGNdHV84nzOhjnt DdxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UQYEX7vW52TUyN2ge2xNNYO6nwda2CZvK4qAzKxmWVM=; b=St3+jiOF5qiWvq8zUo3+a4p0K3WdilxhbUZcdCIMkDSNWBD8AK2Qomrh6YQvglwMtj 6SPRe5/G1qMiSI6OvvK4F5VTp8hNxfEyBGYbhJN5rgiJ0m4Ix9RVjpxsKxrY4u4HIDnX JdGy2Zo7oI6uW+hhZnGTygWzCKnEo1dQoD8WlwQg62ACVWKa0xkG23DeuLXqpXp/qVPY voYej+JmC2tw4zLU/LPInH7zsFe+o1D9hJtwikyh3YluZFL56OaIb7O1gx2NKFyEOtAn fkhgp5Ak+JDRCNqmNwQvVRMP9R48tSqTo6L8ltKv0Jp0eMvv7wUukkNNuXUXWWVMrYgD fPNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AJCNXiec; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id mp39si17836041ejc.182.2021.05.11.21.59.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 21:59:02 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AJCNXiec; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B372482D47; Wed, 12 May 2021 06:58:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org 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=linaro.org header.i=@linaro.org header.b="AJCNXiec"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B5AD082D4B; Wed, 12 May 2021 06:58:35 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) (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 A2A5582AF0 for ; Wed, 12 May 2021 06:58:31 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pf1-x42e.google.com with SMTP id k19so17681130pfu.5 for ; Tue, 11 May 2021 21:58:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UQYEX7vW52TUyN2ge2xNNYO6nwda2CZvK4qAzKxmWVM=; b=AJCNXieceWtEbdvihRVUHKwAThaJL51Db8H3MzlPh4jTf1hT6Dy11ewhVNrl9eSI7n AknqITcMkYzCmgnC1Skx/futKGX3kba8cSIGEBbElBGxZJS0Q+spwvMkz3KsKbd/RTPc d2YAhUsOYfMyAJvX6YB+zQcRgf/LKnScqxlnmzs8sW5bYH7+KsgQ52lGLkkJNLXSTOIF OCV53M81Wzzr1tTDGoEl/TLeSIfYktH+VYXjER1KNo36jMIGBSuasSZ2QjfcWptv/gFG VO8EMiBl3Yghb9rOhDWabvem8TesaXng11nI+K6z+XHb9doKivbwrBv732Ft0qxJ2xKN DZWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UQYEX7vW52TUyN2ge2xNNYO6nwda2CZvK4qAzKxmWVM=; b=IAT/3MKtKHpSiFpyuQs37GcjJKd0vl9PaPCqIE+5t05uaBJHAaCzJAW7i5Uw6iwE4H ZLKtwi6623CPb6mrO2+jv/d4xpH/pmYbsjZvm2lW1zgEep7lQTO2CarrbN+JGPAyv2T2 N0usY2BMK4jMX+3PJ4VM6VEfGMQS4rpRRfyhNMokQQWHvYZB5dgQbQ3funM2f5Physe3 zDj1bJGmCv3W6Opbht5I7ELnQgTWjvRHTZUfH/OC1EfYzIPI43pUzbAxKPmpF88Ta0RK wkvlZrjKMgQ8sLiXXsimCg+5DnVLUF841dcCXjfhRzIhfbrYvUqRPetBK3z9PNPEmgRi 83yQ== X-Gm-Message-State: AOAM532qGMVoxDwDUpiZK2rKY9vDWAKw0EZgJ17Bj1R00d10iubo7rdr vhf4fpSvT4WkBtJWyQYcs36UMQ== X-Received: by 2002:a63:4f50:: with SMTP id p16mr33847860pgl.245.1620795509916; Tue, 11 May 2021 21:58:29 -0700 (PDT) Received: from localhost.localdomain (p3dd30534.tkyea130.ap.so-net.ne.jp. [61.211.5.52]) by smtp.gmail.com with ESMTPSA id q194sm15188202pfc.62.2021.05.11.21.58.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 21:58:29 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, masami.hiramatsu@linaro.org, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH 3/4] tools: add fdtsig command Date: Wed, 12 May 2021 13:57:52 +0900 Message-Id: <20210512045753.62288-4-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210512045753.62288-1-takahiro.akashi@linaro.org> References: <20210512045753.62288-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.4 at phobos.denx.de X-Virus-Status: Clean This command allows us to add a certificate (or public key) to dtb blob: { signature { capsule-key = "..."; }; } The value is actually a signature list in terms of UEFI specificaion, and used in verifying UEFI capsules. The code was originally developed by Sughosh and derived from mkeficapsule.c. Signed-off-by: AKASHI Takahiro --- Makefile | 2 +- tools/Makefile | 2 + tools/fdtsig.c | 274 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 tools/fdtsig.c -- 2.31.0 diff --git a/Makefile b/Makefile index 9806464357e0..8b40987234a0 100644 --- a/Makefile +++ b/Makefile @@ -1016,7 +1016,7 @@ quiet_cmd_lzma = LZMA $@ cmd_lzma = lzma -c -z -k -9 $< > $@ quiet_cmd_fdtsig = FDTSIG $@ -cmd_fdtsig = $(srctree)/tools/fdtsig.sh $(CONFIG_EFI_PKEY_FILE) $@ +cmd_fdtsig = $(objtree)/tools/fdtsig -K $(CONFIG_EFI_PKEY_FILE) -D $@ cfg: u-boot.cfg diff --git a/tools/Makefile b/tools/Makefile index 71a52719620c..e6fd1dbade19 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -234,6 +234,8 @@ HOSTCFLAGS_asn1_compiler.o = -idirafter $(srctree)/include ifneq ($(CONFIG_EFI_CAPSULE_AUTHENTICATE),) HOSTLDLIBS_mkeficapsule += \ $(shell pkg-config --libs libssl libcrypto 2> /dev/null || echo "-lssl -lcrypto") + fdtsig-objs := fdtsig.o $(LIBFDT_OBJS) + hostprogs-y += fdtsig endif hostprogs-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += mkeficapsule diff --git a/tools/fdtsig.c b/tools/fdtsig.c new file mode 100644 index 000000000000..daa1e63c3b33 --- /dev/null +++ b/tools/fdtsig.c @@ -0,0 +1,274 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2021 Linaro Limited + * The code in this file was extracted from mkeficapsule.c + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#define SIGNATURE_NODENAME "signature" +#define OVERLAY_NODENAME "__overlay__" + +static const char *tool_name = "fdtsig"; + +static const char *opts_short = "D:K:Oh"; + +static struct option options[] = { + {"dtb", required_argument, NULL, 'D'}, + {"public key", required_argument, NULL, 'K'}, + {"overlay", no_argument, NULL, 'O'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0}, +}; + +static void print_usage(void) +{ + printf("Usage: %s [options]\n" + "Options:\n" + + "\t-K, --public-key public key esl file\n" + "\t-D, --dtb dtb file\n" + "\t-O, --overlay the dtb file is an overlay\n" + "\t-h, --help print a help message\n", + tool_name); +} + +static int fdt_add_pub_key_data(void *sptr, void *dptr, size_t key_size, + bool overlay) +{ + int parent; + int ov_node; + int frag_node; + int ret = 0; + + if (overlay) { + /* + * The signature would be stored in the + * first fragment node of the overlay + */ + frag_node = fdt_first_subnode(dptr, 0); + if (frag_node == -FDT_ERR_NOTFOUND) { + fprintf(stderr, + "Couldn't find the fragment node: %s\n", + fdt_strerror(frag_node)); + goto done; + } + + ov_node = fdt_subnode_offset(dptr, frag_node, OVERLAY_NODENAME); + if (ov_node == -FDT_ERR_NOTFOUND) { + fprintf(stderr, + "Couldn't find the __overlay__ node: %s\n", + fdt_strerror(ov_node)); + goto done; + } + } else { + ov_node = 0; + } + + parent = fdt_subnode_offset(dptr, ov_node, SIGNATURE_NODENAME); + if (parent == -FDT_ERR_NOTFOUND) { + parent = fdt_add_subnode(dptr, ov_node, SIGNATURE_NODENAME); + if (parent < 0) { + ret = parent; + if (ret != -FDT_ERR_NOSPACE) { + fprintf(stderr, + "Couldn't create signature node: %s\n", + fdt_strerror(parent)); + } + } + } + if (ret) + goto done; + + /* Write the key to the FDT node */ + ret = fdt_setprop(dptr, parent, "capsule-key", + sptr, key_size); + +done: + if (ret) + ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; + + return ret; +} + +static int add_public_key(const char *pkey_file, const char *dtb_file, + bool overlay) +{ + int ret; + int srcfd = -1; + int destfd = -1; + void *sptr = NULL; + void *dptr = NULL; + off_t src_size; + struct stat pub_key; + struct stat dtb; + + /* Find out the size of the public key */ + srcfd = open(pkey_file, O_RDONLY); + if (srcfd == -1) { + fprintf(stderr, "%s: Can't open %s: %s\n", + __func__, pkey_file, strerror(errno)); + ret = -1; + goto err; + } + + ret = fstat(srcfd, &pub_key); + if (ret == -1) { + fprintf(stderr, "%s: Can't stat %s: %s\n", + __func__, pkey_file, strerror(errno)); + ret = -1; + goto err; + } + + src_size = pub_key.st_size; + + /* mmap the public key esl file */ + sptr = mmap(0, src_size, PROT_READ, MAP_SHARED, srcfd, 0); + if (sptr == MAP_FAILED) { + fprintf(stderr, "%s: Failed to mmap %s:%s\n", + __func__, pkey_file, strerror(errno)); + ret = -1; + goto err; + } + + /* Open the dest FDT */ + destfd = open(dtb_file, O_RDWR); + if (destfd == -1) { + fprintf(stderr, "%s: Can't open %s: %s\n", + __func__, dtb_file, strerror(errno)); + ret = -1; + goto err; + } + + ret = fstat(destfd, &dtb); + if (ret == -1) { + fprintf(stderr, "%s: Can't stat %s: %s\n", + __func__, dtb_file, strerror(errno)); + goto err; + } + + dtb.st_size += src_size + 0x30; + if (ftruncate(destfd, dtb.st_size)) { + fprintf(stderr, "%s: Can't expand %s: %s\n", + __func__, dtb_file, strerror(errno)); + ret = -1; + goto err; + } + + errno = 0; + /* mmap the dtb file */ + dptr = mmap(0, dtb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, + destfd, 0); + if (dptr == MAP_FAILED) { + fprintf(stderr, "%s: Failed to mmap %s:%s\n", + __func__, dtb_file, strerror(errno)); + ret = -1; + goto err; + } + + if (fdt_check_header(dptr)) { + fprintf(stderr, "%s: Invalid FDT header\n", __func__); + ret = -1; + goto err; + } + + ret = fdt_open_into(dptr, dptr, dtb.st_size); + if (ret) { + fprintf(stderr, "%s: Cannot expand FDT: %s\n", + __func__, fdt_strerror(ret)); + ret = -1; + goto err; + } + + /* Copy the esl file to the expanded FDT */ + ret = fdt_add_pub_key_data(sptr, dptr, src_size, overlay); + if (ret < 0) { + fprintf(stderr, "%s: Unable to add public key to the FDT\n", + __func__); + ret = -1; + goto err; + } + + ret = 0; + +err: + if (sptr) + munmap(sptr, src_size); + + if (dptr) + munmap(dptr, dtb.st_size); + + if (srcfd != -1) + close(srcfd); + + if (destfd != -1) + close(destfd); + + return ret; +} + +int main(int argc, char **argv) +{ + char *pkey_file; + char *dtb_file; + bool overlay; + int c, idx, ret; + + pkey_file = NULL; + dtb_file = NULL; + overlay = false; + + for (;;) { + c = getopt_long(argc, argv, opts_short, options, &idx); + if (c == -1) + break; + + switch (c) { + case 'K': + if (pkey_file) { + printf("Public Key already specified\n"); + return -1; + } + pkey_file = optarg; + break; + case 'D': + if (dtb_file) { + printf("DTB file already specified\n"); + return -1; + } + dtb_file = optarg; + break; + case 'O': + overlay = true; + break; + case 'h': + print_usage(); + return 0; + } + } + + /* check necessary parameters */ + if (!pkey_file || !dtb_file) { + print_usage(); + exit(EXIT_FAILURE); + } + + ret = add_public_key(pkey_file, dtb_file, overlay); + if (ret == -1) { + printf("Adding public key to the dtb failed\n"); + exit(EXIT_FAILURE); + } + + exit(EXIT_SUCCESS); +} From patchwork Wed May 12 04:57:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 435451 Delivered-To: patch@linaro.org Received: by 2002:a02:c901:0:0:0:0:0 with SMTP id t1csp4489118jao; Tue, 11 May 2021 21:59:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxh209/FIUlH2FkNxsLVoYG0BPN+oXFCkv3q5GXO9smxOzGI/o/jYCwplUMXQucTCxHr4tj X-Received: by 2002:a05:6402:1c0d:: with SMTP id ck13mr40974749edb.170.1620795555389; Tue, 11 May 2021 21:59:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620795555; cv=none; d=google.com; s=arc-20160816; b=J2DH7MAlU6GisTxXHG/drJftock0ItXYfa/xNXyAWPaXN5ZlcXIxOM7y4SuuWk2NJB mILoDZ+Psjeu2affu3UCS7jcekarOY2Hh6XSaw8EM2qwh8aCtJntZDt1yG9Z3oCNImJH wmV+RK/1ktJQQAQMC75aDEEMJPysc8RCSuzgv6JFeO9cmugm4xdHrROB8WSDXIOX87RA tx6HSABRhAIeDsHC+kemrIi3xp0238q4g+KDf1cpaL9GTMLMX2mocj3zXtVcvomRS9QH AFX+TAnojkffV8EVgh89ZVa7jED61NZ9n9xRn8I2HVx8o/wqH5FerqzRZC+D3BsJltVT gyZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=VpniOXo+yH3hQGWs7bzGS30HwuuD96CupOmTi0A/DOo=; b=ekQwJYOoWJeew2rozIBo9IMkfEyXWT+Z/36CGXjDtxeMYqkjsTFzPEUjO8Lsfp8fIw 57e0TwgHEWFxQ0bCXTsxIvoXNQk7/4kk/BvozpT7Pn2W9cQfQmxoal2KUS15qiGQoO4H JY+Rc6y8p8ietTxSatVeMpBZ0HWD2K4fivBSzC/g36nZrHYlZpEMz1hDM1TE9pz4aVxt Y7YX5uhitvORevno2o969rAyEHLIVNw5KTSiTG3SdHig8g233MvVBAju6m1qUTfEC7u5 M3ESe5Z0tb4Vs4mlAxU2nUgynABHFa+WT1WxCDPfbeRyAyt2x0/owF657InAaD7LFRSI v7pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G2A7cFaI; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id t16si19235980eji.432.2021.05.11.21.59.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 21:59:15 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G2A7cFaI; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A563E82D52; Wed, 12 May 2021 06:58:42 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org 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=linaro.org header.i=@linaro.org header.b="G2A7cFaI"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AC7F782D51; Wed, 12 May 2021 06:58:40 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) (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 711A382D13 for ; Wed, 12 May 2021 06:58:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pf1-x434.google.com with SMTP id b21so10563113pft.10 for ; Tue, 11 May 2021 21:58:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VpniOXo+yH3hQGWs7bzGS30HwuuD96CupOmTi0A/DOo=; b=G2A7cFaIsJO8m/KpoxLs8y6mIJUEgFSvVu7jmO1naFGZymmQNP0t3OJCE6vv2jZ8AT 7H2nQ7LjkhLqNrm7bcp5N7rxmCL56S5jWbUP4gpfa/AK5m28e/axKXF6MVSaXoPt1l7/ PmMUG5SxC44ziv3H6O93VEPAb7aB9+BO4nUU8zTyJ7fNCs2wg0V4SDq9Yb1GntU54Xpf kejniaMr2O6WA9hCEtp0LLbHZfrB95vJJ/DgrrPLfzyrNmI7MJRBc4Vl2HACXm69SaUr HrucLduWcDLtITWpGzfX+0tX1Csqyf5Lo0aYnrQznea7zhpY+GK+xIr7v1fmESCwWzhI HQVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VpniOXo+yH3hQGWs7bzGS30HwuuD96CupOmTi0A/DOo=; b=TPiJuQ2L0sphDd+TCS6o4ZmTGS8OJJ9vXLLc11lUjHRMXRpK3Q4XWstuS+cMpUR+se dQImN44AUa0SmoTbo4D3yyr79wjP4VHFSla2ZOkKl64rgIKOxmvw0yUs0xDq389LtVI2 wJr1Fdl7chDUNPEtN/Ch7O8BvbVQ/F+WL0aRR5VEN9PaM0Z/NNQw3lUpxk6wfscFZOpO OFMaJkncSthtBvrpdYCl7RuEb8SvKRvvLo3Qi6rt169NfIQFemNnAbD8rx4HFAMy1FKX 9oPwWfYGuBaK6ozbxQb2dwDkyrl3b4QKxEzFHNMaPiACb2F/Lpjege3e8eXx1SLjMF3n qeGg== X-Gm-Message-State: AOAM531n1x7DMw6MdBKEemsl7f6wDs7MnVtRDb3Q4XXwB0tkSt6Zjm6a wjV40RLn5hXQg+2YmSR3BQ+CXQ== X-Received: by 2002:a63:8c52:: with SMTP id q18mr31734741pgn.356.1620795512775; Tue, 11 May 2021 21:58:32 -0700 (PDT) Received: from localhost.localdomain (p3dd30534.tkyea130.ap.so-net.ne.jp. [61.211.5.52]) by smtp.gmail.com with ESMTPSA id q194sm15188202pfc.62.2021.05.11.21.58.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 21:58:32 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, masami.hiramatsu@linaro.org, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH 4/4] test/py: efi_capsule: add image authentication test Date: Wed, 12 May 2021 13:57:53 +0900 Message-Id: <20210512045753.62288-5-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210512045753.62288-1-takahiro.akashi@linaro.org> References: <20210512045753.62288-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.4 at phobos.denx.de X-Virus-Status: Clean Add a couple of test cases against dealing with a signed capsule file. Signed-off-by: AKASHI Takahiro --- .../py/tests/test_efi_capsule/capsule_defs.py | 5 + test/py/tests/test_efi_capsule/conftest.py | 35 ++- test/py/tests/test_efi_capsule/signature.dts | 8 + .../test_capsule_firmware_signed.py | 234 ++++++++++++++++++ 4 files changed, 279 insertions(+), 3 deletions(-) create mode 100644 test/py/tests/test_efi_capsule/signature.dts create mode 100644 test/py/tests/test_efi_capsule/test_capsule_firmware_signed.py -- 2.31.0 diff --git a/test/py/tests/test_efi_capsule/capsule_defs.py b/test/py/tests/test_efi_capsule/capsule_defs.py index 4fd6353c2040..aa9bf5eee3aa 100644 --- a/test/py/tests/test_efi_capsule/capsule_defs.py +++ b/test/py/tests/test_efi_capsule/capsule_defs.py @@ -3,3 +3,8 @@ # Directories CAPSULE_DATA_DIR = '/EFI/CapsuleTestData' CAPSULE_INSTALL_DIR = '/EFI/UpdateCapsule' + +# v1.5.1 or earlier of efitools has a bug in sha256 calculation, and +# you need build a newer version on your own. +# The path must terminate with '/'. +EFITOOLS_PATH = '' diff --git a/test/py/tests/test_efi_capsule/conftest.py b/test/py/tests/test_efi_capsule/conftest.py index 6ad5608cd71c..bd9544bc056f 100644 --- a/test/py/tests/test_efi_capsule/conftest.py +++ b/test/py/tests/test_efi_capsule/conftest.py @@ -10,13 +10,13 @@ import pytest from capsule_defs import * # -# Fixture for UEFI secure boot test +# Fixture for UEFI capsule test # - @pytest.fixture(scope='session') def efi_capsule_data(request, u_boot_config): - """Set up a file system to be used in UEFI capsule test. + """Set up a file system to be used in UEFI capsule and + authenticaion test. Args: request: Pytest request object. @@ -40,6 +40,26 @@ def efi_capsule_data(request, u_boot_config): check_call('mkdir -p %s' % data_dir, shell=True) check_call('mkdir -p %s' % install_dir, shell=True) + capsule_auth_enabled = u_boot_config.buildconfig.get( + 'config_efi_capsule_authenticate') + if capsule_auth_enabled: + # Create private key (SIGNER.key) and certificate (SIGNER.crt) + check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_SIGNER/ -keyout SIGNER.key -out SIGNER.crt -nodes -days 365' + % data_dir, shell=True) + check_call('cd %s; %scert-to-efi-sig-list SIGNER.crt SIGNER.esl' + % (data_dir, EFITOOLS_PATH), shell=True) + + # Update dtb adding capsule certificate + check_call('cd %s; cp %s/test/py/tests/test_efi_capsule/signature.dts .' + % (data_dir, u_boot_config.source_dir), shell=True) + check_call('cd %s; dtc -@ -I dts -O dtb -o signature.dtbo signature.dts; fdtoverlay -i %s/arch/sandbox/dts/test.dtb -o test_sig.dtb signature.dtbo' + % (data_dir, u_boot_config.build_dir), shell=True) + + # Create *malicious* private key (SIGNER2.key) and certificate + # (SIGNER2.crt) + check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_SIGNER/ -keyout SIGNER2.key -out SIGNER2.crt -nodes -days 365' + % data_dir, shell=True) + # Create capsule files # two regions: one for u-boot.bin and the other for u-boot.env check_call('cd %s; echo -n u-boot:Old > u-boot.bin.old; echo -n u-boot:New > u-boot.bin.new; echo -n u-boot-env:Old -> u-boot.env.old; echo -n u-boot-env:New > u-boot.env.new' % data_dir, @@ -56,6 +76,15 @@ def efi_capsule_data(request, u_boot_config): check_call('cd %s; %s/tools/mkeficapsule --raw u-boot.bin.new --index 1 Test02' % (data_dir, u_boot_config.build_dir), shell=True) + if capsule_auth_enabled: + # firmware signed with proper key + check_call('cd %s; %s/tools/mkeficapsule --raw u-boot.bin.new --index 1 --monotonic-count 1 --private-key SIGNER.key --certificate SIGNER.crt Test03' % + (data_dir, u_boot_config.build_dir), + shell=True) + # firmware signed with *mal* key + check_call('cd %s; %s/tools/mkeficapsule --raw u-boot.bin.new --index 1 --monotonic-count 1 --private-key SIGNER2.key --certificate SIGNER2.crt Test04' % + (data_dir, u_boot_config.build_dir), + shell=True) # Create a disk image with EFI system partition check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat %s %s' % diff --git a/test/py/tests/test_efi_capsule/signature.dts b/test/py/tests/test_efi_capsule/signature.dts new file mode 100644 index 000000000000..55c15432979a --- /dev/null +++ b/test/py/tests/test_efi_capsule/signature.dts @@ -0,0 +1,8 @@ +/dts-v1/; +/plugin/; + +&{/} { + signature { + capsule-key = /incbin/("SIGNER.esl"); + }; +}; diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed.py new file mode 100644 index 000000000000..58a45ddf57a3 --- /dev/null +++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed.py @@ -0,0 +1,234 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright (c) 2021, Linaro Limited +# Author: AKASHI Takahiro +# +# U-Boot UEFI: Firmware Update (Signed capsule) Test + +""" +This test verifies capsule-on-disk firmware update +with signed capsule files +""" + +from subprocess import check_call, check_output, CalledProcessError +import pytest +from capsule_defs import * + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('efi_capsule_firmware_raw') +@pytest.mark.buildconfigspec('efi_capsule_authenticate') +@pytest.mark.buildconfigspec('dfu') +@pytest.mark.buildconfigspec('dfu_sf') +@pytest.mark.buildconfigspec('cmd_efidebug') +@pytest.mark.buildconfigspec('cmd_fat') +@pytest.mark.buildconfigspec('cmd_memory') +@pytest.mark.buildconfigspec('cmd_nvedit_efi') +@pytest.mark.buildconfigspec('cmd_sf') +@pytest.mark.slow +class TestEfiCapsuleFirmwareSigned(object): + def test_efi_capsule_auth1( + self, u_boot_config, u_boot_console, efi_capsule_data): + """ + Test Case 1 - Update U-Boot on SPI Flash, raw image format + 0x100000-0x150000: U-Boot binary (but dummy) + + If the capsule is properly signed, the authentication + should pass and the firmware be updated. + """ + disk_img = efi_capsule_data + with u_boot_console.log.section('Test Case 1-a, before reboot'): + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi', + 'efidebug boot order 1', + 'env set -e -nv -bs -rt OsIndications =0x0000000000000004', + 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"', + 'env save']) + + # initialize content + output = u_boot_console.run_command_list([ + 'sf probe 0:0', + 'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR, + 'sf write 4000000 100000 10', + 'sf read 5000000 100000 10', + 'md.b 5000000 10']) + assert 'Old' in ''.join(output) + + # place a capsule file + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 %s/Test03' % CAPSULE_DATA_DIR, + 'fatwrite host 0:1 4000000 %s/Test03 $filesize' % CAPSULE_INSTALL_DIR, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test03' in ''.join(output) + + # reboot + mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule' + u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR + '/test_sig.dtb' + u_boot_console.restart_uboot() + + capsule_early = u_boot_config.buildconfig.get( + 'config_efi_capsule_on_disk_early') + with u_boot_console.log.section('Test Case 1-b, after reboot'): + if not capsule_early: + # make sure that dfu_alt_info exists even persistent variables + # are not available. + output = u_boot_console.run_command_list([ + 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"', + 'host bind 0 %s' % disk_img, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test03' in ''.join(output) + + # need to run uefi command to initiate capsule handling + output = u_boot_console.run_command( + 'env print -e Capsule0000') + + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test03' not in ''.join(output) + + output = u_boot_console.run_command_list([ + 'sf probe 0:0', + 'sf read 4000000 100000 10', + 'md.b 4000000 10']) + assert 'u-boot:New' in ''.join(output) + + def test_efi_capsule_auth2( + self, u_boot_config, u_boot_console, efi_capsule_data): + """ + Test Case 2 - Update U-Boot on SPI Flash, raw image format + 0x100000-0x150000: U-Boot binary (but dummy) + + If the capsule is signed but with an invalid key, + the authentication should fail and the firmware + not be updated. + """ + disk_img = efi_capsule_data + with u_boot_console.log.section('Test Case 2-a, before reboot'): + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi', + 'efidebug boot order 1', + 'env set -e -nv -bs -rt OsIndications =0x0000000000000004', + 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"', + 'env save']) + + # initialize content + output = u_boot_console.run_command_list([ + 'sf probe 0:0', + 'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR, + 'sf write 4000000 100000 10', + 'sf read 5000000 100000 10', + 'md.b 5000000 10']) + assert 'Old' in ''.join(output) + + # place a capsule file + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 %s/Test04' % CAPSULE_DATA_DIR, + 'fatwrite host 0:1 4000000 %s/Test04 $filesize' % CAPSULE_INSTALL_DIR, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test04' in ''.join(output) + + # reboot + mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule' + u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR + '/test_sig.dtb' + u_boot_console.restart_uboot() + + capsule_early = u_boot_config.buildconfig.get( + 'config_efi_capsule_on_disk_early') + with u_boot_console.log.section('Test Case 2-b, after reboot'): + if not capsule_early: + # make sure that dfu_alt_info exists even persistent variables + # are not available. + output = u_boot_console.run_command_list([ + 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"', + 'host bind 0 %s' % disk_img, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test04' in ''.join(output) + + # need to run uefi command to initiate capsule handling + output = u_boot_console.run_command( + 'env print -e Capsule0000') + + # deleted any way + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test04' not in ''.join(output) + + # TODO: check CapsuleStatus in CapsuleXXXX + + output = u_boot_console.run_command_list([ + 'sf probe 0:0', + 'sf read 4000000 100000 10', + 'md.b 4000000 10']) + assert 'u-boot:Old' in ''.join(output) + + def test_efi_capsule_auth3( + self, u_boot_config, u_boot_console, efi_capsule_data): + """ + Test Case 3 - Update U-Boot on SPI Flash, raw image format + 0x100000-0x150000: U-Boot binary (but dummy) + + If the capsule is not signed, the authentication + should fail and the firmware not be updated. + """ + disk_img = efi_capsule_data + with u_boot_console.log.section('Test Case 3-a, before reboot'): + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi', + 'efidebug boot order 1', + 'env set -e -nv -bs -rt OsIndications =0x0000000000000004', + 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"', + 'env save']) + + # initialize content + output = u_boot_console.run_command_list([ + 'sf probe 0:0', + 'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR, + 'sf write 4000000 100000 10', + 'sf read 5000000 100000 10', + 'md.b 5000000 10']) + assert 'Old' in ''.join(output) + + # place a capsule file + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR, + 'fatwrite host 0:1 4000000 %s/Test02 $filesize' % CAPSULE_INSTALL_DIR, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test02' in ''.join(output) + + # reboot + mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule' + u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR + '/test_sig.dtb' + u_boot_console.restart_uboot() + + capsule_early = u_boot_config.buildconfig.get( + 'config_efi_capsule_on_disk_early') + with u_boot_console.log.section('Test Case 3-b, after reboot'): + if not capsule_early: + # make sure that dfu_alt_info exists even persistent variables + # are not available. + output = u_boot_console.run_command_list([ + 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"', + 'host bind 0 %s' % disk_img, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test02' in ''.join(output) + + # need to run uefi command to initiate capsule handling + output = u_boot_console.run_command( + 'env print -e Capsule0000') + + # deleted any way + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR]) + assert 'Test02' not in ''.join(output) + + # TODO: check CapsuleStatus in CapsuleXXXX + + output = u_boot_console.run_command_list([ + 'sf probe 0:0', + 'sf read 4000000 100000 10', + 'md.b 4000000 10']) + assert 'u-boot:Old' in ''.join(output)