From patchwork Tue Jul 21 10:35:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 245592 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp2791408ilg; Tue, 21 Jul 2020 03:37:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyWajO5qWOEy7HEfo8VLppFhurWLPxtkZg6jmgXKx3j/tKcpLlHn4Nc0XKxWQfxzIx7Wqsc X-Received: by 2002:aa7:c496:: with SMTP id m22mr25583329edq.336.1595327838894; Tue, 21 Jul 2020 03:37:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595327838; cv=none; d=google.com; s=arc-20160816; b=NfWhhCuBL2UeIw8F+xlfoxCHAiFM9a8F72SsqWfeeL6yQgpdcWgVUO8K7mJEIuZVai VEB/WREj7zYroNF5qmPVwdCzl9lH7QL18FnHvbvV/60nfmuqPT2l9zcWFwu7YX8w6eWX 6HNAVjFNVrgvilWfz5pXfiawviUVaTY6OpwWsIPA4W/G4OD75CwWh1PSJGa5dRZnHQJi VJmzSWbucinMOXYi5Oy0cyN7HnKKuIhIrdo3NGkCRYCCFDlAU16F2bSnfSTecSmOP/YO fw3/vGqEs49KjGKDxBGL0FSjixj4T+ypWY3mKUopTis1ow9r1voccpyw4V3qnSJQ40ED REGA== 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=4KqvDsG//1D5lKbjKy8oOrrJubIzFyb61hbHOLE8xzQ=; b=DyyWZuKTWXEoUYuoJYtYu3jpqtRPBilJ51k1xk3XB224gcgHgMMSCTzF0MFn9YRT/9 frHCzkcz7ngChvVLDGYJf5Q4fSuFRaIg0zk+zlcl5kYHRjgwibg8bFdU9zCSYs/gmMH6 IIInpgXEt7BpSNnYpkPeLYRnlxt1NUP4q1iHeSAoRBnh+/SDfCJPe+1L6TrowhuFYZU/ l54GBv5vru+f46HqIgklwFID6ihvY2HQx9IdQNi/Qk4gbqffLU+F00RlR42y2N2xWe48 OQnfNl4Z4FbFNuBlDeCRoBeWf1CyLdL2gWdPs6X24pcyRprfVG2JdERbABtiMWZh6wvD gFYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MisTCtPi; 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 m26si15724061ejn.40.2020.07.21.03.37.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2020 03:37:18 -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=MisTCtPi; 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 590A781C40; Tue, 21 Jul 2020 12:36:46 +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="MisTCtPi"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 54CB481C3C; Tue, 21 Jul 2020 12:36:26 +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-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) (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 6FBFA81C3E for ; Tue, 21 Jul 2020 12:36:17 +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-x1043.google.com with SMTP id b92so1460283pjc.4 for ; Tue, 21 Jul 2020 03:36:17 -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=4KqvDsG//1D5lKbjKy8oOrrJubIzFyb61hbHOLE8xzQ=; b=MisTCtPiSJ3/r1ULjavelLHlW6konV/AmEe0GhzaFUeyFTUoPNajW2aKr+eNJDTUsy YI8XmZcpyqLJBYW8h5MozUr60vA4qG73ydGPk7W1PMVhYm6gqd2Ggvz6wvAxQS6rkLAO Jvo87MRmF3+MvyXYc1tO5srSA7fra41Yw/HYUuq9nTLor9+X3oWNuUcZ/hUKRjD9cJHf Qgm2meq5UECibtDRclxTMtNKBcqKqmSLC+6p8V2IYfft+O+6Xks3NmEYzXPN46TiTTc0 w3Pi8FuizCtvCxy2Fr7EAa6HxdwSApI/bnXA2Rl1l0SNRC1M3665fMG+3Ml3xLIzbX0P 1CZg== 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=4KqvDsG//1D5lKbjKy8oOrrJubIzFyb61hbHOLE8xzQ=; b=nfEONtfta4iTGbVSdfW3p9+74tkx1FQfg4JYie7XmLsIXEzlALAmFcITVp1r4WfTl0 DSwyjLZWq9J+B1oaFMgpNripZI/tiwhD+7b12AsQnG7AckM3N+oDspol6iZNFilDeP6C 90pu394dXOUmBJck1sbK82Oo36MrZtm/gjp+l4aTCPH8q9FyouyfW5Rmytzit147KmpI SwUogxw9dC/2VeD4oU3G0SIgHeqzqyocgAZlHKhSgmIqlNvjC6p4xn87/tcubQdh7ami kPTiwCTVkAMWisNvhryxLn8Kn6a5f6JZQIuRDHViCHHkSItgatneebNygjci+3F4vSpY NmPw== X-Gm-Message-State: AOAM532wltsuukyduMJUNGy90iW1VhYBX5ae2EFgnXVkcyW8f+nbld1K /RCtXK24FeFT1gnjith+cmyejA== X-Received: by 2002:a17:902:9042:: with SMTP id w2mr22444627plz.9.1595327775192; Tue, 21 Jul 2020 03:36:15 -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.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2020 03:36:14 -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 4/8] lib: crypto: add pkcs7_digest() Date: Tue, 21 Jul 2020 19:35:20 +0900 Message-Id: <20200721103524.5956-5-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 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 192e39bae906..706358f09b5e 100644 --- a/lib/crypto/pkcs7_verify.c +++ b/lib/crypto/pkcs7_verify.c @@ -10,10 +10,12 @@ #define pr_fmt(fmt) "PKCS7: "fmt #ifdef __UBOOT__ +#include #include #include #include #include +#include #include #include #else @@ -29,15 +31,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) {