Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp515741pxb; Thu, 25 Feb 2021 08:11:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJwOBw7HZ7Sioudq0pxnXgAn9aCaXqtLMEZLROcsBOCtSHC92Vc7Owxn3VMhljFmehO9OV1W X-Received: by 2002:a17:906:28d6:: with SMTP id p22mr3370402ejd.365.1614269477517; Thu, 25 Feb 2021 08:11:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614269477; cv=none; d=google.com; s=arc-20160816; b=aXwtWh+g3gxx9tHM44UZ66+9j8VMqaintES12/NhYS3QFQsGSymcXUs2jKaFZgwbZA U5wleedFrq9+r7aoDkdG1EwCM/e9HOqr1xVZJprQ12K+gN/FJNNc2WKHp019TW5fyTHz GPlrHQzKZntNj69Uzh7PTHDMWnPzJAaX6DK5j0RG0F2iUuaeukJNhDBWSpvbMJP7KbtT gXG6pcZwVOYHSAEza+CbNZo5936m15vHJHtWMJcjb3geAzIqdhSj2qLMXzZtdNSzXTsd Jn2K8jKUaocStEvl+ZED6+jon+HC70Ain4hG6bnUBU43sKmiPE3YcJqW8i+8/t3iJnmn IFxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=83Ty5NeWlE8hxGAqq+K0aQUl2aB9eYYBeVV6ziV1fRQ=; b=pNmZWsgynbjQoy7yx+22NNTIOQX+Bu3WXGDmFMh3S0uVXi4ilebxv2iLuxZYAcsXwN DRGCUoIt8O5x3FogzzDRJCtSPWoUwIGNj4dONfAgZloY0hCW2FDJ6mvgoT/TWuCKWILP Wkv/3CtbL0hrxrfGIkKvZTGTvAksfREOtjodWkcV/qLyyme/kx+hWKVtgDvCk1gSA344 u3vf9hY5ftgO+aRp0ziTqqLrUwWAh5IxpxPA7VfaXz2/SIVgXk9lESXkSc/+DdLmGcec aFLW8HGryCsT6IlZHJ37vLErpvjIh3IQheO5z/ZX4aNqOxJp8wLD4djRJX0gXxvgP9dl Ph1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=pnLwxnCu; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i7si3394371edg.300.2021.02.25.08.10.11; Thu, 25 Feb 2021 08:11:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=pnLwxnCu; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233662AbhBYQJW (ORCPT + 99 others); Thu, 25 Feb 2021 11:09:22 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:31414 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S233607AbhBYQJN (ORCPT ); Thu, 25 Feb 2021 11:09:13 -0500 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 11PG3rpf087976; Thu, 25 Feb 2021 11:08:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=83Ty5NeWlE8hxGAqq+K0aQUl2aB9eYYBeVV6ziV1fRQ=; b=pnLwxnCuamTNNJ8Nh7FgvWDRDxnvpC7c+5vf/3P5FSAS8/XX59UsyVj9iKoRjAOycQya k8cLVsUkWpHnv0ZtCFMxTlfbDxEG5ksTTbMbOkmdA8gA6TFv66UdifiRvw3yTXH46FWA 400nxppl5fNQJDvW4I0KLm+V/UAenSTS8vwL3dlR4qEPy0fHcLMJccikHE7hsWj4wnIB 5W7bK9ED5WNEDr4A8MZeRVqp+RMcwsoUASuIjNQEwHpZRE/XUOAyqtF0cdK9HFKzcYx9 fxC6zPj67MK1v3cVmt6GZOErthX6PhrL9g424m66OeUUCaxL55cTLxfp7OjYwfmkTA8e Gg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 36xe4e2wgw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Feb 2021 11:08:22 -0500 Received: from m0098416.ppops.net (m0098416.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 11PG4IZF091220; Thu, 25 Feb 2021 11:08:21 -0500 Received: from ppma03wdc.us.ibm.com (ba.79.3fa9.ip4.static.sl-reverse.com [169.63.121.186]) by mx0b-001b2d01.pphosted.com with ESMTP id 36xe4e2wg0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Feb 2021 11:08:21 -0500 Received: from pps.filterd (ppma03wdc.us.ibm.com [127.0.0.1]) by ppma03wdc.us.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 11PG25jZ025245; Thu, 25 Feb 2021 16:08:20 GMT Received: from b03cxnp08027.gho.boulder.ibm.com (b03cxnp08027.gho.boulder.ibm.com [9.17.130.19]) by ppma03wdc.us.ibm.com with ESMTP id 36tt29fw0q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Feb 2021 16:08:20 +0000 Received: from b03ledav001.gho.boulder.ibm.com (b03ledav001.gho.boulder.ibm.com [9.17.130.232]) by b03cxnp08027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 11PG8JqP12845414 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 Feb 2021 16:08:19 GMT Received: from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 68E806E052; Thu, 25 Feb 2021 16:08:19 +0000 (GMT) Received: from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CCC766E04C; Thu, 25 Feb 2021 16:08:18 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by b03ledav001.gho.boulder.ibm.com (Postfix) with ESMTP; Thu, 25 Feb 2021 16:08:18 +0000 (GMT) From: Stefan Berger To: keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, davem@davemloft.net, herbert@gondor.apana.org.au, dhowells@redhat.com, zohar@linux.ibm.com Cc: linux-kernel@vger.kernel.org, patrick@puiterwijk.org, linux-integrity@vger.kernel.org, Stefan Berger , Tianjia Zhang Subject: [PATCH v9 2/9] x509: Detect sm2 keys by their parameters OID Date: Thu, 25 Feb 2021 11:07:55 -0500 Message-Id: <20210225160802.2478700-3-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210225160802.2478700-1-stefanb@linux.vnet.ibm.com> References: <20210225160802.2478700-1-stefanb@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-02-25_09:2021-02-24,2021-02-25 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 mlxlogscore=999 malwarescore=0 spamscore=0 priorityscore=1501 mlxscore=0 adultscore=0 clxscore=1011 impostorscore=0 suspectscore=0 phishscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102250127 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Stefan Berger Detect whether a key is an sm2 type of key by its OID in the parameters array rather than assuming that everything under OID_id_ecPublicKey is sm2, which is not the case. Cc: David Howells Cc: keyrings@vger.kernel.org Signed-off-by: Stefan Berger Reviewed-by: Tianjia Zhang --- crypto/asymmetric_keys/x509_cert_parser.c | 12 +++++++++++- include/linux/oid_registry.h | 1 + lib/oid_registry.c | 13 +++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c index 52c9b455fc7d..1621ceaf5c95 100644 --- a/crypto/asymmetric_keys/x509_cert_parser.c +++ b/crypto/asymmetric_keys/x509_cert_parser.c @@ -459,6 +459,7 @@ int x509_extract_key_data(void *context, size_t hdrlen, const void *value, size_t vlen) { struct x509_parse_context *ctx = context; + enum OID oid; ctx->key_algo = ctx->last_oid; switch (ctx->last_oid) { @@ -470,7 +471,16 @@ int x509_extract_key_data(void *context, size_t hdrlen, ctx->cert->pub->pkey_algo = "ecrdsa"; break; case OID_id_ecPublicKey: - ctx->cert->pub->pkey_algo = "sm2"; + if (parse_OID(ctx->params, ctx->params_size, &oid) != 0) + return -EBADMSG; + + switch (oid) { + case OID_sm2: + ctx->cert->pub->pkey_algo = "sm2"; + break; + default: + return -ENOPKG; + } break; default: return -ENOPKG; diff --git a/include/linux/oid_registry.h b/include/linux/oid_registry.h index b504e2f36b25..f32d91895e4d 100644 --- a/include/linux/oid_registry.h +++ b/include/linux/oid_registry.h @@ -121,6 +121,7 @@ enum OID { }; extern enum OID look_up_OID(const void *data, size_t datasize); +extern int parse_OID(const void *data, size_t datasize, enum OID *oid); extern int sprint_oid(const void *, size_t, char *, size_t); extern int sprint_OID(enum OID, char *, size_t); diff --git a/lib/oid_registry.c b/lib/oid_registry.c index f7ad43f28579..508e0b34b5f0 100644 --- a/lib/oid_registry.c +++ b/lib/oid_registry.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "oid_registry_data.c" MODULE_DESCRIPTION("OID Registry"); @@ -92,6 +93,18 @@ enum OID look_up_OID(const void *data, size_t datasize) } EXPORT_SYMBOL_GPL(look_up_OID); +int parse_OID(const void *data, size_t datasize, enum OID *oid) +{ + const unsigned char *v = data; + + if (datasize < 2 || v[0] != ASN1_OID || v[1] != datasize - 2) + return -EBADMSG; + + *oid = look_up_OID(data + 2, datasize - 2); + return 0; +} +EXPORT_SYMBOL_GPL(parse_OID); + /* * sprint_OID - Print an Object Identifier into a buffer * @data: The encoded OID to print -- 2.29.2