Received: by 10.213.65.68 with SMTP id h4csp1078232imn; Thu, 22 Mar 2018 14:52:00 -0700 (PDT) X-Google-Smtp-Source: AG47ELu9VTY9QcFYWkhMe7QdSEZqC9uqyHpx4NoR2Y4jKxl0jq4pNBpGh1RF7m+v0T6xHcvl8LqE X-Received: by 10.101.71.202 with SMTP id f10mr18605303pgs.366.1521755520045; Thu, 22 Mar 2018 14:52:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521755520; cv=none; d=google.com; s=arc-20160816; b=XY9ejbBTr7DGHVxg4MaulQTtzzvtrynVo1LLnQTIcCK6Nqv5LuzU5RvIVSBKttB9M6 Yqry9CVBx4wnbAiekeD9eUUqsNfx0+licdheBDnQ0Xu77mbNdxGHqSiwqdzMEgTQJ62/ GIN3tdZJNgTyvW5Lmp0gmFxVJ6XobqFarLytsfNs/xSar8C/SEInSXXgHwVQ0C4sTfvr rD4VTYubxKwATim/Yk7HptQVRUbchEpFvnD6NVJwGgYEwS5yH6JJ7f2V6gwP8ARCeQ9O XhBLIS7g0fIvK4IEZrgWkicG9KG3OMRUJQp2b6pPLA6zEfD4wt6TLk9STrHmF+mqc36n 3DqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:content-transfer-encoding :mime-version:references:in-reply-to:date:cc:to:from:subject :arc-authentication-results; bh=YgM5R5GPMk23JA4hQ8a79R4olIJ2PSA/M6+aqs4HK7w=; b=cMRtUA6dxorcqEL8nJR85jyy18yj9FuItqMJ+UzZE8K2QfxCZmO4cmCKRN9wfbSCl3 uuXh7rWzgP3MiURoPlaMk0781Dwp6XuMcrOVaHoqUTjU7RhDBs25tEJZ3cpG8lzWOJRp nlAypbTCcEUn66HV0xTgSHP1HnY3o++/Mp5vGzaPMX7g+0foMLDCznUqmWoDvgB8A1KG KXO1Dvk3ONuQTWFVHYMf9G1CTyLQb/Ps5BifVNfbyMPmQds2PR8+DkQBSKp99UObQz/j 0zdZzGQQ9XE49dai2fwjZczQFVB41/tynkfYNeMW+UF1D6HXwEa6hDGlkudtscuzGWZv cscQ== 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 u191si5003977pgc.725.2018.03.22.14.51.45; Thu, 22 Mar 2018 14:51:59 -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 S1751853AbeCVVtx (ORCPT + 99 others); Thu, 22 Mar 2018 17:49:53 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:32808 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751628AbeCVVtt (ORCPT ); Thu, 22 Mar 2018 17:49:49 -0400 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w2MLn4h7107140 for ; Thu, 22 Mar 2018 17:49:49 -0400 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0a-001b2d01.pphosted.com with ESMTP id 2gvja7e0j7-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Thu, 22 Mar 2018 17:49:48 -0400 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 22 Mar 2018 21:49:45 -0000 Received: from b06cxnps4075.portsmouth.uk.ibm.com (9.149.109.197) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 22 Mar 2018 21:49:42 -0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w2MLnfGE50921504; Thu, 22 Mar 2018 21:49:41 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4DFD752043; Thu, 22 Mar 2018 20:41:03 +0000 (GMT) Received: from localhost.localdomain (unknown [9.80.103.4]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id 5B8725203F; Thu, 22 Mar 2018 20:41:01 +0000 (GMT) Subject: Re: [PATCH v6 02/12] PKCS#7: Introduce pkcs7_get_message_sig() and verify_pkcs7_message_sig() From: Mimi Zohar To: Thiago Jung Bauermann , 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-kernel@vger.kernel.org, Dmitry Kasatkin , James Morris , "Serge E. Hallyn" , David Howells , David Woodhouse , Jessica Yu , Herbert Xu , "David S. Miller" , "AKASHI, Takahiro" Date: Thu, 22 Mar 2018 17:49:38 -0400 In-Reply-To: <20180316203837.10174-3-bauerman@linux.vnet.ibm.com> References: <20180316203837.10174-1-bauerman@linux.vnet.ibm.com> <20180316203837.10174-3-bauerman@linux.vnet.ibm.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.20.5 (3.20.5-1.fc24) Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 x-cbid: 18032221-0012-0000-0000-000005C2555D X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18032221-0013-0000-0000-0000193E7FC6 Message-Id: <1521755378.3848.295.camel@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-03-22_11:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1803220245 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Thiago, On Fri, 2018-03-16 at 17:38 -0300, Thiago Jung Bauermann wrote: > IMA will need to know the key that signed a given PKCS#7 message, so add > pkcs7_get_message_sig(). > > It will also need to verify an already parsed PKCS#7 message. For this > purpose, add verify_pkcs7_message_sig() which takes a struct pkcs7_message > for verification instead of the raw bytes that verify_pkcs7_signature() > takes. The title "PKCS#7: refactor verify_pkcs7_signature()" might be more appropriate.  The patch description would then explain why it needs to be refactored.  In this case, verify_pkcs7_signature() verifies the signature using keys on the builtin and secondary keyrings.  IMA- appraisal needs to verify the signature using keys on its keyring. The patch itself looks good! Reviewed-by: Mimi Zohar > Signed-off-by: Thiago Jung Bauermann > Cc: David Howells > Cc: David Woodhouse > Cc: Herbert Xu > Cc: "David S. Miller" > --- > certs/system_keyring.c | 61 ++++++++++++++++++++++++++--------- > crypto/asymmetric_keys/pkcs7_parser.c | 16 +++++++++ > include/crypto/pkcs7.h | 2 ++ > include/linux/verification.h | 10 ++++++ > 4 files changed, 73 insertions(+), 16 deletions(-) > > diff --git a/certs/system_keyring.c b/certs/system_keyring.c > index 6251d1b27f0c..7ddc8b7a3062 100644 > --- a/certs/system_keyring.c > +++ b/certs/system_keyring.c > @@ -190,33 +190,27 @@ late_initcall(load_system_certificate_list); > #ifdef CONFIG_SYSTEM_DATA_VERIFICATION > > /** > - * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data. > + * verify_pkcs7_message_sig - Verify a PKCS#7-based signature on system data. > * @data: The data to be verified (NULL if expecting internal data). > * @len: Size of @data. > - * @raw_pkcs7: The PKCS#7 message that is the signature. > - * @pkcs7_len: The size of @raw_pkcs7. > + * @pkcs7: The PKCS#7 message that is the signature. > * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only, > * (void *)1UL for all trusted keys). > * @usage: The use to which the key is being put. > * @view_content: Callback to gain access to content. > * @ctx: Context for callback. > */ > -int verify_pkcs7_signature(const void *data, size_t len, > - const void *raw_pkcs7, size_t pkcs7_len, > - struct key *trusted_keys, > - enum key_being_used_for usage, > - int (*view_content)(void *ctx, > - const void *data, size_t len, > - size_t asn1hdrlen), > - void *ctx) > +int verify_pkcs7_message_sig(const void *data, size_t len, > + struct pkcs7_message *pkcs7, > + struct key *trusted_keys, > + enum key_being_used_for usage, > + int (*view_content)(void *ctx, > + const void *data, size_t len, > + size_t asn1hdrlen), > + void *ctx) > { > - struct pkcs7_message *pkcs7; > int ret; > > - pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len); > - if (IS_ERR(pkcs7)) > - return PTR_ERR(pkcs7); > - > /* The data should be detached - so we need to supply it. */ > if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) { > pr_err("PKCS#7 signature with non-detached data\n"); > @@ -258,6 +252,41 @@ int verify_pkcs7_signature(const void *data, size_t len, > } > > error: > + pr_devel("<==%s() = %d\n", __func__, ret); > + return ret; > +} > + > +/** > + * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data. > + * @data: The data to be verified (NULL if expecting internal data). > + * @len: Size of @data. > + * @raw_pkcs7: The PKCS#7 message that is the signature. > + * @pkcs7_len: The size of @raw_pkcs7. > + * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only, > + * (void *)1UL for all trusted keys). > + * @usage: The use to which the key is being put. > + * @view_content: Callback to gain access to content. > + * @ctx: Context for callback. > + */ > +int verify_pkcs7_signature(const void *data, size_t len, > + const void *raw_pkcs7, size_t pkcs7_len, > + struct key *trusted_keys, > + enum key_being_used_for usage, > + int (*view_content)(void *ctx, > + const void *data, size_t len, > + size_t asn1hdrlen), > + void *ctx) > +{ > + struct pkcs7_message *pkcs7; > + int ret; > + > + pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len); > + if (IS_ERR(pkcs7)) > + return PTR_ERR(pkcs7); > + > + ret = verify_pkcs7_message_sig(data, len, pkcs7, trusted_keys, usage, > + view_content, ctx); > + > pkcs7_free_message(pkcs7); > pr_devel("<==%s() = %d\n", __func__, ret); > return ret; > diff --git a/crypto/asymmetric_keys/pkcs7_parser.c b/crypto/asymmetric_keys/pkcs7_parser.c > index a6dcaa659aa8..456b803972b5 100644 > --- a/crypto/asymmetric_keys/pkcs7_parser.c > +++ b/crypto/asymmetric_keys/pkcs7_parser.c > @@ -683,3 +683,19 @@ int pkcs7_note_signed_info(void *context, size_t hdrlen, > return -ENOMEM; > return 0; > } > + > +/** > + * pkcs7_get_message_sig - get signature in @pkcs7 > + */ > +const struct public_key_signature *pkcs7_get_message_sig( > + const struct pkcs7_message *pkcs7) > +{ > + /* > + * This function doesn't support messages with more than one signature, > + * so don't return anything in that case. > + */ > + if (pkcs7->signed_infos == NULL || pkcs7->signed_infos->next != NULL) > + return NULL; > + > + return pkcs7->signed_infos->sig; > +} > diff --git a/include/crypto/pkcs7.h b/include/crypto/pkcs7.h > index 583f199400a3..6f51d0cb6d12 100644 > --- a/include/crypto/pkcs7.h > +++ b/include/crypto/pkcs7.h > @@ -28,6 +28,8 @@ extern void pkcs7_free_message(struct pkcs7_message *pkcs7); > extern int pkcs7_get_content_data(const struct pkcs7_message *pkcs7, > const void **_data, size_t *_datalen, > size_t *_headerlen); > +extern const struct public_key_signature *pkcs7_get_message_sig( > + const struct pkcs7_message *pkcs7); > > /* > * pkcs7_trust.c > diff --git a/include/linux/verification.h b/include/linux/verification.h > index a10549a6c7cd..f04dac2728ec 100644 > --- a/include/linux/verification.h > +++ b/include/linux/verification.h > @@ -29,6 +29,7 @@ extern const char *const key_being_used_for[NR__KEY_BEING_USED_FOR]; > #ifdef CONFIG_SYSTEM_DATA_VERIFICATION > > struct key; > +struct pkcs7_message; > > extern int verify_pkcs7_signature(const void *data, size_t len, > const void *raw_pkcs7, size_t pkcs7_len, > @@ -38,6 +39,15 @@ extern int verify_pkcs7_signature(const void *data, size_t len, > const void *data, size_t len, > size_t asn1hdrlen), > void *ctx); > +extern int verify_pkcs7_message_sig(const void *data, size_t len, > + struct pkcs7_message *pkcs7, > + struct key *trusted_keys, > + enum key_being_used_for usage, > + int (*view_content)(void *ctx, > + const void *data, > + size_t len, > + size_t asn1hdrlen), > + void *ctx); > > #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION > extern int verify_pefile_signature(const void *pebuf, unsigned pelen, >