From patchwork Fri Jul 17 07:16:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 235677 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp1457684ilg; Fri, 17 Jul 2020 00:17:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEkSERnKU40CYxUiJYM2wlL5Eiax8qxOcitbrVqBhbkuBoCrslbjXvRR0xDraavmScKHTu X-Received: by 2002:a17:906:492:: with SMTP id f18mr7353129eja.279.1594970241159; Fri, 17 Jul 2020 00:17:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594970241; cv=none; d=google.com; s=arc-20160816; b=H/vUgJwFhFjP17A6smuXggrZuNyTt12htfQ3zfuD5za4KM6Wr75OeVcq8A1cyOZd1s NpTV1F++jDTJV2I63JQbrzX3JQjlrwp7vCOTJzCAfR51mrvTg5rcOTTPZbTfrRW9gR6n kAghARBpctlYRJoZgrtXG7rY9H4WX9BJ731zGpvyfYuh4/6A8AQIL8o+HiW2sA95o8BJ lFPKTKhEXqWu9TTZVncQmk5VZdltI1eIxjaScIcCpog6EuCtdxKkh5mGudT2F5IZT559 OB3lScAENeHt6e2Qpr57SInqb3cz+fEyEIaDmXW/08wYXIWBg6GwEi7lflCvCt679j5I gGwA== 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=Jek9/eG6Fs92n3eYHCSbRPN+HnT8Ko+6vUqZusoqtVE=; b=EQl5/pg69IcoEcOuV8u7a2PisSs6jufoKy+kaV6C5hHJhazffO1JAxYdwHNKLEhesx Dam6BtYTOrPhBABA+XukxQOAnBOJDumvtg42SJd5cg+mAXzCHD0G0ZH8W0PFMHKc8ZAa 10KHr45HC6zb6h/UknkonH8+vY9ex6W3qhx3rLyGYzDMcOLcDHTnrdBMUDhEeuVYXFG8 tSRjUHhvT12YdCAH9FGRtH3bfBg6sdjWtyKXsNJcatI8fJIau2xX55i1Fy9ua93p/Rie qxxezBQgzFWzCju3ic9GWMm7uMIw6kg/P3Wu47TEdpn+75mRvwqToOwXPJ0WnQr5lj4I bXkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ITNqigEY; 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 bx8si4506077edb.170.2020.07.17.00.17.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 00:17:21 -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=ITNqigEY; 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 9E40781C35; Fri, 17 Jul 2020 09:17:15 +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="ITNqigEY"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 84E4481C2A; Fri, 17 Jul 2020 09:17:06 +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,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) (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 6395081C2A for ; Fri, 17 Jul 2020 09:17:00 +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-x442.google.com with SMTP id u5so4973250pfn.7 for ; Fri, 17 Jul 2020 00:17:00 -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=Jek9/eG6Fs92n3eYHCSbRPN+HnT8Ko+6vUqZusoqtVE=; b=ITNqigEYSOY/PMutQhivxmVfoEy2gWiEMDXwSdjp9CuDOsvs1k79HNMxI16yyUfaz3 uLj+3x+CrMPy/NL06H0RNB7gsF5lo+MJRK180GSWq1lWC3/QTyIyqQXZ85n8MlrZHWdo 8qTlaLju/7gpwm97bdliVBX3UrdhTDNKFBwzdU/Z3QudnQf9pdX+rXvSvEccdqYXcUWh f3ZUZxj6G6GWaeSB8ErqcKl1M61giuiEXUf80QzHeHmZ3YE8b8NIpbYjBi1P/F5JCFfL qlBgLI3zi5uPhdN2emqbu5z7pscSasT6amElB0dZwY340fTW1RSS//AMRZ0p7zYYUfQK JMng== 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=Jek9/eG6Fs92n3eYHCSbRPN+HnT8Ko+6vUqZusoqtVE=; b=Anmznrh0aelYEthAreQurEfUmZ1zSF7fsj+M/yohvX0iLQNQpTfFT88ioFYbKMBqdG CeLEBUq2ybq2FJMIrBoEI5rxBYqb8warvZa4FSJg3/pLm8Xr/NBPcnBPPM9V+YaEomgQ AHClv2kj+UEB9+RZGHkCdE2LoozWLL8krPlngyoVEIEC3WyGcSoHePyIC/LPRDkc0J1D idqCkL4eb/WqdqQ6LsM5zwwav5tT5oZ3MWMFV+1k6OecsWduCY9kkDumAS20grfZJFYH FjtvLzK4R2JU+ZRa4u9cMkGcxoYuYDzHvXy8Jx6lN+O83E/tOhFhf102bftbF+th6SiP pDkg== X-Gm-Message-State: AOAM5328owlegxGM6kPemkkbU8Z7v9vyr4zSHv+fGu9Rew54was3s17x 9aquOI4BAUJQUzg5b2zeg5obkw== X-Received: by 2002:a63:7f17:: with SMTP id a23mr7466456pgd.3.1594970218616; Fri, 17 Jul 2020 00:16:58 -0700 (PDT) Received: from localhost.localdomain (p6e424d9a.tkyea130.ap.so-net.ne.jp. [110.66.77.154]) by smtp.gmail.com with ESMTPSA id g12sm6749388pfb.190.2020.07.17.00.16.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 00:16:58 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, mail@patrick-wildt.de, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH v4 2/7] lib: crypto: enable x509_check_for_self_signed() Date: Fri, 17 Jul 2020 16:16:25 +0900 Message-Id: <20200717071630.7363-3-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200717071630.7363-1-takahiro.akashi@linaro.org> References: <20200717071630.7363-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.3 at phobos.denx.de X-Virus-Status: Clean When the file, x509_public_key.c, was imported from linux code in commit b4adf627d5b7 ("lib: crypto: add x509 parser"), x509_check_for_self_signed() was commented out for simplicity. Now it need be enabled in order to make pkcs7_verify_one(), which will be imported in a later patch, functional. Signed-off-by: AKASHI Takahiro --- lib/crypto/x509_cert_parser.c | 2 -- lib/crypto/x509_public_key.c | 33 +++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) -- 2.27.0 diff --git a/lib/crypto/x509_cert_parser.c b/lib/crypto/x509_cert_parser.c index 5f984b9dfdae..eb24349460c2 100644 --- a/lib/crypto/x509_cert_parser.c +++ b/lib/crypto/x509_cert_parser.c @@ -142,12 +142,10 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) } cert->id = kid; -#ifndef __UBOOT__ /* Detect self-signed certificates */ ret = x509_check_for_self_signed(cert); if (ret < 0) goto error_decode; -#endif kfree(ctx); return cert; diff --git a/lib/crypto/x509_public_key.c b/lib/crypto/x509_public_key.c index 571af9a0adf9..91810a864049 100644 --- a/lib/crypto/x509_public_key.c +++ b/lib/crypto/x509_public_key.c @@ -8,6 +8,7 @@ #define pr_fmt(fmt) "X.509: "fmt #ifdef __UBOOT__ #include +#include #include #include #include @@ -18,6 +19,7 @@ #include #ifdef __UBOOT__ #include +#include #else #include #include @@ -35,7 +37,9 @@ int x509_get_sig_params(struct x509_certificate *cert) { struct public_key_signature *sig = cert->sig; -#ifndef __UBOOT__ +#ifdef __UBOOT__ + struct image_region region; +#else struct crypto_shash *tfm; struct shash_desc *desc; size_t desc_size; @@ -63,12 +67,25 @@ int x509_get_sig_params(struct x509_certificate *cert) sig->s_size = cert->raw_sig_size; #ifdef __UBOOT__ - /* - * Note: - * This part (filling sig->digest) should be implemented if - * x509_check_for_self_signed() is enabled x509_cert_parse(). - * Currently, this check won't affect UEFI secure boot. - */ + if (!sig->hash_algo) + return -ENOPKG; + if (!strcmp(sig->hash_algo, "sha256")) + sig->digest_size = SHA256_SUM_LEN; + else if (!strcmp(sig->hash_algo, "sha1")) + sig->digest_size = SHA1_SUM_LEN; + else + return -ENOPKG; + + sig->digest = calloc(1, sig->digest_size); + if (!sig->digest) + return -ENOMEM; + + region.data = cert->tbs; + region.size = cert->tbs_size; + hash_calculate(sig->hash_algo, ®ion, 1, sig->digest); + + /* TODO: is_hash_blacklisted()? */ + ret = 0; #else /* Allocate the hashing algorithm we're going to need and find out how @@ -118,7 +135,6 @@ error: return ret; } -#ifndef __UBOOT__ /* * Check for self-signedness in an X.509 cert and if found, check the signature * immediately if we can. @@ -175,6 +191,7 @@ not_self_signed: return 0; } +#ifndef __UBOOT__ /* * Attempt to parse a data blob for a key as an X509 certificate. */