Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp112203yba; Wed, 17 Apr 2019 20:53:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqzZ7yaSfExUxXTrzwkq99jPiuYTSC+DW5CxEdjcHq1GR0WUp8UdtIwj7QpVLO375zkFQeVx X-Received: by 2002:a17:902:110c:: with SMTP id d12mr65061471pla.47.1555559604737; Wed, 17 Apr 2019 20:53:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555559604; cv=none; d=google.com; s=arc-20160816; b=fYUg4f6Xo8dq7h/V13uW7IbshDIcr5Zryp26AT1kzXAiAUEcmlTQPFcyRadQlCWmIm +Z5PAN5Hm7Oq6iIinA5P+CdmrqHA/JkNXHKPkwqD8njFLrVAAucRSQkszZ7tb/1aMq59 AM37rzHoDbXE5pKP/iZl6ttHfnlxYRlum15lZElFF56/kRuhXyO/tXlgfl1BIhU+9F1g SUpOO1mMy8G3XcxG3ABiDrcpHfZW0LW8Q3Gq50lXlLayJyKAUn1ybEQa7uvdstA/dCS+ td3yvM7YiHVRLsNW+n89qNOkY4qyIfyrgKBlh8AZ/0dsGZO/yhdSq3Rqi8q46s7dq0B/ qZ9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:references:in-reply-to:date :subject:cc:to:from; bh=lruMnLaZFk1aaVf8b920rMNOFPwZj0DEup/8bM/nRO4=; b=MXpljlCGXs/y7AlPaMPUBxyAjaywBMX+DqdWGYbseBjPO9cRDcAWcBV/71ElPy411O mUZIJxmyMZV1wkdaJCFR1Agw3URsxnS3jAE+ZxwM/gRcPdm4G2TsSFIbNnXkeTFPi8se c0Z2m/mu0tgCcIqq9UVUHZpR9mIL6Lp8ve6xvuydQXHLwPzu0T50CfG3osnTe3i1Tllv Es1FPKvU4s3morbE+q26fw4GzXUnsscLptrPbw15KP3BJZM9LKRgiLcs7fmmd0XpEQx1 V6FPGNxXW2t9k5n0zOXaCCxzLaPTE1PzAIzgIdnUVZfD80jB3uP1ewmb6+kCABB7h7Qa G7KQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j16si937866pll.291.2019.04.17.20.53.09; Wed, 17 Apr 2019 20:53:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388034AbfDRDwH (ORCPT + 99 others); Wed, 17 Apr 2019 23:52:07 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:49742 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2388023AbfDRDwG (ORCPT ); Wed, 17 Apr 2019 23:52:06 -0400 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x3I3o9H7075644 for ; Wed, 17 Apr 2019 23:52:04 -0400 Received: from e34.co.us.ibm.com (e34.co.us.ibm.com [32.97.110.152]) by mx0b-001b2d01.pphosted.com with ESMTP id 2rxgd7th45-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 17 Apr 2019 23:52:04 -0400 Received: from localhost by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 18 Apr 2019 04:52:03 +0100 Received: from b03cxnp08025.gho.boulder.ibm.com (9.17.130.17) by e34.co.us.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Thu, 18 Apr 2019 04:51:57 +0100 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x3I3pun326083470 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 18 Apr 2019 03:51:56 GMT Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 05D8178067; Thu, 18 Apr 2019 03:51:56 +0000 (GMT) Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9B69E7805C; Thu, 18 Apr 2019 03:51:51 +0000 (GMT) Received: from morokweng.localdomain.com (unknown [9.85.230.182]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP; Thu, 18 Apr 2019 03:51:51 +0000 (GMT) From: Thiago Jung Bauermann To: linux-integrity@vger.kernel.org Cc: linux-security-module@vger.kernel.org, keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Mimi Zohar , Dmitry Kasatkin , James Morris , "Serge E. Hallyn" , David Howells , David Woodhouse , Jessica Yu , Herbert Xu , "David S. Miller" , Jonathan Corbet , "AKASHI, Takahiro" , Thiago Jung Bauermann Subject: [PATCH v10 03/12] PKCS#7: Introduce pkcs7_get_digest() Date: Thu, 18 Apr 2019 00:51:11 -0300 X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190418035120.2354-1-bauerman@linux.ibm.com> References: <20190418035120.2354-1-bauerman@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19041803-0016-0000-0000-000009A21AC0 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00010947; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000284; SDB=6.01190666; UDB=6.00623948; IPR=6.00971462; MB=3.00026492; MTD=3.00000008; XFM=3.00000015; UTC=2019-04-18 03:52:02 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19041803-0017-0000-0000-000042DB3460 Message-Id: <20190418035120.2354-4-bauerman@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-04-18_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1904180023 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org IMA will need to access the digest of the PKCS7 message (as calculated by the kernel) before the signature is verified, so introduce pkcs7_get_digest() for that purpose. Also, modify pkcs7_digest() to detect when the digest was already calculated so that it doesn't have to do redundant work. Verifying that sinfo->sig->digest isn't NULL is sufficient because both places which allocate sinfo->sig (pkcs7_parse_message() and pkcs7_note_signed_info()) use kzalloc() so sig->digest is always initialized to zero. Signed-off-by: Thiago Jung Bauermann Cc: David Howells Cc: Herbert Xu Cc: "David S. Miller" --- crypto/asymmetric_keys/pkcs7_verify.c | 33 +++++++++++++++++++++++++++ include/crypto/pkcs7.h | 4 ++++ 2 files changed, 37 insertions(+) diff --git a/crypto/asymmetric_keys/pkcs7_verify.c b/crypto/asymmetric_keys/pkcs7_verify.c index 97c77f66b20d..5c3de46b0b73 100644 --- a/crypto/asymmetric_keys/pkcs7_verify.c +++ b/crypto/asymmetric_keys/pkcs7_verify.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "pkcs7_parser.h" @@ -33,6 +34,10 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7, kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo); + /* The digest was calculated already. */ + if (sig->digest) + return 0; + if (!sinfo->sig->hash_algo) return -ENOPKG; @@ -122,6 +127,34 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7, return ret; } +int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len, + enum hash_algo *hash_algo) +{ + struct pkcs7_signed_info *sinfo = pkcs7->signed_infos; + int i, ret; + + /* + * This function doesn't support messages with more than one signature. + */ + if (sinfo == NULL || sinfo->next != NULL) + return -EBADMSG; + + ret = pkcs7_digest(pkcs7, sinfo); + if (ret) + return ret; + + *buf = sinfo->sig->digest; + *len = sinfo->sig->digest_size; + + for (i = 0; i < HASH_ALGO__LAST; i++) + if (!strcmp(hash_algo_name[i], sinfo->sig->hash_algo)) { + *hash_algo = i; + break; + } + + return 0; +} + /* * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7 * uses the issuer's name and the issuing certificate serial number for diff --git a/include/crypto/pkcs7.h b/include/crypto/pkcs7.h index 583f199400a3..3bfe6829eaae 100644 --- a/include/crypto/pkcs7.h +++ b/include/crypto/pkcs7.h @@ -13,6 +13,7 @@ #define _CRYPTO_PKCS7_H #include +#include #include struct key; @@ -44,4 +45,7 @@ extern int pkcs7_verify(struct pkcs7_message *pkcs7, extern int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7, const void *data, size_t datalen); +extern int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, + u32 *len, enum hash_algo *hash_algo); + #endif /* _CRYPTO_PKCS7_H */