From patchwork Tue Jul 21 10:35:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 245586 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp2790965ilg; Tue, 21 Jul 2020 03:36:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJym/SJds3dWb6ETm0ZAH+fEPSmvfa/0BbfFQZeqavWCdMN88s5ox2Q+qIpHebhcIbtIpTyf X-Received: by 2002:a17:907:100a:: with SMTP id ox10mr24037062ejb.351.1595327796111; Tue, 21 Jul 2020 03:36:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595327796; cv=none; d=google.com; s=arc-20160816; b=ZQMSqz7eujGHT16K8q6FUCKQ+DrRvnSjfSfnXRhmTg/MmRcCNppQdExhZ6/RLsJjOE vBHjXOAYNeC0sKWwo0a93opPmGWqOvkvmC4pVj+nxPtcqIu76g5S9f7gXQRKmJ64rVFT agKbL7kSWMmrZ7YfBSRSOYQUmBdf+1bcN7YPAmwSLCpbG1+rEz7mEThF/MZLCbxDRVpW 63y6uSVrDhY961kwWxAd3NuRdXmcRYDUgh9ZzOzClz77wIZC9JxyVzXwbLvdaTU2pAKa g8uI8eM05uIXrMcIrU55EawtP1m3A9VXduX7zl6yiaig/nybGfgW34e3nri5jdLTq25E Gu6A== 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=Tpdnx5bIdGcwnCIjGjz4WYatygGo+K57tYmZJaT5ZfYkNN9ugzd7KJ7C89peaIWqlI /Vj6BH6sIdQQklo1l+iCp7kC3Sxje0V3SziL1yZzWv9ZQz1slNeVkb4dcg/Ztjvu696K 9Ye9wi1poW/Yh+mov7qo/L6yUFSsjBM9+BoGBD7tyQrwG0iT3KG5HPKiBg0nehOAsTXp pTViIjAtlS9TYatMxBDtxp/Ruz67lWcvpebbHvDcanGvevmWaR3ETeuwCQa068zXZazs KeL3W8tVdb7VaEaIbzC+wxEkes0xCvcsqvWvfhO3022o2QfGkffjLFvMbofzPUV7d4+t AY5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Xx3QePpD; 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 r12si11588742ejs.370.2020.07.21.03.36.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2020 03:36:36 -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=Xx3QePpD; 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 DC50981C3E; Tue, 21 Jul 2020 12:36: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=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="Xx3QePpD"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0C9B681C40; Tue, 21 Jul 2020 12:36:17 +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-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) (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 3C39781C1A for ; Tue, 21 Jul 2020 12:36:11 +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-x543.google.com with SMTP id o13so11771245pgf.0 for ; Tue, 21 Jul 2020 03:36:11 -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=Xx3QePpDUC3z9um/Rnx1Zcy9XnBcRjcAQZhNmc1LX0yIQOZgridxYkcMIxWzHhUf69 aqspXOFGoNsDcmT/fo4HFXim5yMY0u3DaioJbRW/MlzZG2c5ZcWicloQeazMnpfQvFfD +vMGI0bg9ckNNGNutnRBOx6fAF7s1BM1zkcHJl6IzTZJ3vd8ID+dQ2ZPVItI3+lqB2fG zaZ4hw8vofT0W7gpuInPV3NMXakIjlE0UHnbZplLSjdVUaf6aqSr5t9rhRwmW1kumgJ6 2zDK9zhjIRLMQRjFuXHIAmaodX9q7hx8d8/g4STOjSdbnqbXhGpfKRm6g5b9wXkZm0c8 pm4A== 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=UZsdTVCezOvYD4doSt/zV/xG5mhgX9Vsq0/lbJcEhFQtMuBS3/J/5OZvD1ngjN2tzc SkWbxofkVyzUIYH8XxLZSP5pgYGw1rzpuWKOdnJMrUvPIUNTNE+itksJGi2HxODpxT33 d5us782taIbLBFPtup6wngu0bW4eMQiLnU9EGCX0snUt2eCpnBk/yV67L3prlUisGH+I nKOgFiELJETxGneAheJsR+JR5t4sa9nhZ2XgYW3PGmQoDHTWUWyFpjSH9bIvDsfhQ0TU H7sOTCqC5nkN53DI2PpgfF9exSNmKu7ybdQbb+vSpjdcO/UOnUIAlp31NLhaJ0M04Zye uIvg== X-Gm-Message-State: AOAM532pWKo4EZWzmrhe/91HdN4V2v7QWPtwmBPBvg9Nf3GdAB7vuTK/ JiEwp+OhnJmLhSfnX2WShLxS/w== X-Received: by 2002:a05:6a00:14c1:: with SMTP id w1mr23250534pfu.92.1595327769443; Tue, 21 Jul 2020 03:36:09 -0700 (PDT) Received: from localhost.localdomain (p6e424d9a.tkyea130.ap.so-net.ne.jp. [110.66.77.154]) by smtp.gmail.com with ESMTPSA id q20sm19838276pfn.111.2020.07.21.03.36.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2020 03:36:08 -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 v5 2/8] lib: crypto: enable x509_check_for_self_signed() Date: Tue, 21 Jul 2020 19:35:18 +0900 Message-Id: <20200721103524.5956-3-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200721103524.5956-1-takahiro.akashi@linaro.org> References: <20200721103524.5956-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. */