From patchwork Fri Jul 17 07:16:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 235678 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp1457791ilg; Fri, 17 Jul 2020 00:17:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx05Du5PNT0wdYTAwKR0F5k62ES4Y/6/xs6fpWRaNx8FE9DDqi16MBrrCovmWmflEHTyXPS X-Received: by 2002:aa7:d353:: with SMTP id m19mr7736418edr.75.1594970254225; Fri, 17 Jul 2020 00:17:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594970254; cv=none; d=google.com; s=arc-20160816; b=H9sU7TDda6SvWjiRlVpJ/qIjVYcxLpDIVOeVXvmmWY6ykI3qQNJC2NGiwlY1Kr9kwr RSVbKxLY4m4S0YcF6xQd8/x3w8C8Y579uA9voJyRkxEWZYbDc16zRti2dl9KwLjKcy0f sJ4N3fsTmo3vVSGLh2sNKCXKL3ZC4RGvO7BSZn6HJKjmti7oAq5ILywgt6V7x88iU1wi pLWkcmqPdYXhcgWfJXchD0EC0RgMveFoibfNFoFnZ09DmHwBhIomqeMKH515jeTMLaol BuedTnmD4lMX3lJK8KfeupoiSqDtMRMFJtR6z5eRygcs+XsD0V8MMkK8BmI+xskZZ7Cx UzrA== 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=ys5S2L5WKoDlFzQloeJvV9epu2vzumQzDcaTJUisawQ=; b=MxMBBYLF8tf4eTrSrbPU9Yxo26/iMEEqzHR2zp7Qg35trm6n216prZWPuCweQInWp1 ESDJX0gmAQcNGlGo0xSs80gY39KQ0IFfAzJOEH7l/7hZEeaIRMGD90u1pdPJaVKvAAtF SbJV9gk5IWzI41YRD0jym7aH++8t3FlHUJ1nC3gOqwiDR4UIG/YEk65dC7fhnqx2HCRP 3N8Kd3E+665Nnq7La0BLX3MEZfh4Wx7e1/QhXHELWAjpUAPPK3ItfEHobwZs5Bn9hqz2 /tnklh/czfOrt614XrPVvy1NNSsyXfFmBBTCL7N9lzp6XQ7+88yRHx99ASH+iZwo2/kP WO+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ilbbz0i+; 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 du1si5994397ejc.616.2020.07.17.00.17.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 00:17:34 -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=Ilbbz0i+; 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 7712F81C3E; Fri, 17 Jul 2020 09:17:20 +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="Ilbbz0i+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id F068D81C37; Fri, 17 Jul 2020 09:17:12 +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-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) (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 C33CD81C3E for ; Fri, 17 Jul 2020 09:17:05 +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-pj1-x1041.google.com with SMTP id ch3so6098274pjb.5 for ; Fri, 17 Jul 2020 00:17:05 -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=ys5S2L5WKoDlFzQloeJvV9epu2vzumQzDcaTJUisawQ=; b=Ilbbz0i+rs9sBbVDTfN0fJznX9SnLSPXNt6HfntMTPW+Hw1ma3xSjzp7p7dgNhY0Ru pfB8+MZXpshP9nD2tpSlm0dcuQznOQjhGS1gPKzNpN/tDWwDQoJxcqACRjqKJI+9kym2 LezE14cbtWqKk/79MCXzGnYNCpcG2+poqWSLHaZNK7t6PFSv0hfn0W3T0upxbL0dNurV yI2TGBO7cXgree/9LE7wgKTWPhR/in+MK/cwksQ8cxcZ26AGuZLuP53u64pMdOhdUc/x ir2rwu/hPbZxsBLnnZ+fSCQXawH56IT/Eqg10Ul63ql9URgnqbWeLXZ9GSZ/vtX+DUPY rCdg== 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=ys5S2L5WKoDlFzQloeJvV9epu2vzumQzDcaTJUisawQ=; b=Eo8DQolkWu0q636FQ7vpYgeoKnLD26oS1vlk7moSDF9rIdnpiI1xGw7pA5ifKJcAVC 7wXGfGdQ8rT78o7HwAfIXuCTP8I2jGd2Je7Xl/YGLgq1F8Q9NpxkbXtVz4Lee7Lc6j7o gKILO0IoVA0qHXVwcgKVJ8XXLAkqgMcSJujSxqWQcs+lGnIk0YKlitARF/QESSCcByiS QYZ7nIFLVHjvTIIu/EPDXO7iP7u4uiMQ9loaX7MnoUf8FO75HnH8WAt1K7rlHu+wPsWP hkrRc4Q3iWaDdySN/qX1mzBQ0AwQIxr12XPMoNwBC+B3+UQkBUeVzj16cO8hbXTCLmfx ghcg== X-Gm-Message-State: AOAM531G9IY6EiZklRoCNnKUy8PifIcPlIRrTfpYOh9FuBGXctDPg3hm wNuceSlp0MO7fy7DMbYZoKFsiw== X-Received: by 2002:a17:90b:4c91:: with SMTP id my17mr7869792pjb.81.1594970224184; Fri, 17 Jul 2020 00:17:04 -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.17.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 00:17:03 -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 4/7] lib: crypto: add pkcs7_digest() Date: Fri, 17 Jul 2020 16:16:27 +0900 Message-Id: <20200717071630.7363-5-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 This function was nullified when the file, pkcs7_verify.c, was imported because it calls further linux-specific interfaces inside, hence that could lead to more files being imported from linux. We need this function in pkcs7_verify_one() and so simply re-implement it here instead of re-using the code. Signed-off-by: AKASHI Takahiro --- lib/crypto/pkcs7_verify.c | 92 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 3 deletions(-) -- 2.27.0 diff --git a/lib/crypto/pkcs7_verify.c b/lib/crypto/pkcs7_verify.c index a893fa3b586b..a32e841cb22b 100644 --- a/lib/crypto/pkcs7_verify.c +++ b/lib/crypto/pkcs7_verify.c @@ -7,10 +7,12 @@ #define pr_fmt(fmt) "PKCS7: "fmt #ifdef __UBOOT__ +#include #include #include #include #include +#include #include #include #else @@ -26,15 +28,99 @@ #endif /* - * Digest the relevant parts of the PKCS#7 data + * pkcs7_digest - Digest the relevant parts of the PKCS#7 data + * @pkcs7: PKCS7 Signed Data + * @sinfo: PKCS7 Signed Info + * + * Digest the relevant parts of the PKCS#7 data, @pkcs7, using signature + * information in @sinfo. But if there are authentication attributes, + * i.e. signed image case, the digest must be calculated against + * the authentication attributes. + * + * Return: 0 - on success, non-zero error code - otherwise */ #ifdef __UBOOT__ static int pkcs7_digest(struct pkcs7_message *pkcs7, struct pkcs7_signed_info *sinfo) { - return 0; + struct public_key_signature *sig = sinfo->sig; + struct image_region regions[2]; + int ret = 0; + + /* The digest was calculated already. */ + if (sig->digest) + return 0; + + if (!sinfo->sig->hash_algo) + return -ENOPKG; + if (!strcmp(sinfo->sig->hash_algo, "sha256")) + sig->digest_size = SHA256_SUM_LEN; + else if (!strcmp(sinfo->sig->hash_algo, "sha1")) + sig->digest_size = SHA1_SUM_LEN; + else + return -ENOPKG; + + sig->digest = calloc(1, sig->digest_size); + if (!sig->digest) { + pr_warn("Sig %u: Out of memory\n", sinfo->index); + return -ENOMEM; + } + + regions[0].data = pkcs7->data; + regions[0].size = pkcs7->data_len; + + /* Digest the message [RFC2315 9.3] */ + hash_calculate(sinfo->sig->hash_algo, regions, 1, sig->digest); + + /* However, if there are authenticated attributes, there must be a + * message digest attribute amongst them which corresponds to the + * digest we just calculated. + */ + if (sinfo->authattrs) { + u8 tag; + + if (!sinfo->msgdigest) { + pr_warn("Sig %u: No messageDigest\n", sinfo->index); + ret = -EKEYREJECTED; + goto error; + } + + if (sinfo->msgdigest_len != sig->digest_size) { + pr_debug("Sig %u: Invalid digest size (%u)\n", + sinfo->index, sinfo->msgdigest_len); + ret = -EBADMSG; + goto error; + } + + if (memcmp(sig->digest, sinfo->msgdigest, + sinfo->msgdigest_len) != 0) { + pr_debug("Sig %u: Message digest doesn't match\n", + sinfo->index); + ret = -EKEYREJECTED; + goto error; + } + + /* We then calculate anew, using the authenticated attributes + * as the contents of the digest instead. Note that we need to + * convert the attributes from a CONT.0 into a SET before we + * hash it. + */ + memset(sig->digest, 0, sig->digest_size); + + tag = 0x31; + regions[0].data = &tag; + regions[0].size = 1; + regions[1].data = sinfo->authattrs; + regions[1].size = sinfo->authattrs_len; + + hash_calculate(sinfo->sig->hash_algo, regions, 2, sig->digest); + + ret = 0; + } +error: + return ret; } -#else +#else /* !__UBOOT__ */ static int pkcs7_digest(struct pkcs7_message *pkcs7, struct pkcs7_signed_info *sinfo) {