Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1814800rdb; Tue, 3 Oct 2023 01:31:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG0gZAXi74M+gO11Gmp4fpNNJ/xLNKmcjrcoixbYt2QMHcY/4QNXe4aEfabhNkOne2m97L/ X-Received: by 2002:aca:1b0b:0:b0:3a7:4987:d44 with SMTP id b11-20020aca1b0b000000b003a749870d44mr13050735oib.20.1696321918436; Tue, 03 Oct 2023 01:31:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696321918; cv=none; d=google.com; s=arc-20160816; b=gVqrkL/0ZP3FK6vWmup5GU49r2h0uoMEgMXCtCGy0OTfbsss4+3yMsrdTO7BzYdRUG 6VFZ/1LF3Zu7UPGdQGwQrnhOUPaZ3UJDA1xrg4qu6+PYEZShyDpu+6c5l6F4BsyjHCtG 9VlAmiVLiQdwaJ3ercGATpWkGfYgkjpVni85Kz1yVJkDuSZU0grbahFJWT3rsZtJTN11 rJ170G8KCNi/GLAcav6JBOjPgjlmVoA5GJ2voFDTFIFVF6vPKH7sSduP5M2xXrl2Q8/V ORpucTWuVy06fmuJcVvTK2S3hb4qdgbzE2yiAdcTEEkNdW+mvyLg0Z505f2ePubSYyku cXzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date:dkim-signature; bh=v0PJoQbf7nihFZrP2YJQGefAwPFppnbeLxMvd8/Q114=; fh=g62NcDT+ivL79B1aZ0R8nTeuoXyNDXdlohxxgMsnpNA=; b=K5ff0oLnEUpdmNmS2baAsIp/p3QJw9cT1UWUMwOtwtherRlpSjaUIcY9QF+WBLtQLy otxq583vD0tEFX+P2TR72gdEVeCjkFIR8z93sVQYiRmHQdEneMjMeiGrzVQFs3BkxUtW TfMhYTelcdI7JTLFGZx//ZxZAbHorJslTzOTUpUPxY/sgzOgNuNw0X1wwNh4vgE5M1Uc Ta4InePo7ZViQ8qNDGg4P9p6YmK0tmg8UNyFxC+5difdvjDAZCERLY1GXvKpp+cNg40x mKBEmiQ0aEMk/c4fivui89B0GxQJGHoh/tWOD/Slvi9NO6btgGcrh2+2nHajdRACW38O pBSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=T6uvfCTC; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id fj2-20020a056a003a0200b0068e3f550763si1037376pfb.101.2023.10.03.01.31.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 01:31:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=T6uvfCTC; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 73094809B9DB; Tue, 3 Oct 2023 01:31:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230425AbjJCIbl (ORCPT + 99 others); Tue, 3 Oct 2023 04:31:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239298AbjJCIbj (ORCPT ); Tue, 3 Oct 2023 04:31:39 -0400 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77CB6AD; Tue, 3 Oct 2023 01:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696321891; x=1727857891; h=date:from:to:cc:subject:in-reply-to:message-id: references:mime-version; bh=xvRTEJgiLM4R8J7f3Dw6MhEmiMJyQfp7liKkGIGPBHQ=; b=T6uvfCTCZvES4Eaz0/0jVBpbBZaJ/m8GAvwFHXlZ+TWNPCo1PI0D9Rw0 2vzKUfccromOazCcTIHb1c1JswynfKwYP6yozSiDWYvbOeJCiIJr91IPm oOoacJaSmsZY9+r1CBp+3xbUYKkGLzy10BZT0CmzInWnMthVDzUxqRrTJ /XPpnFb9TNRe7n6Db/KgAyDvA6fzIpL2jjYTlFMQvMb18YNcbmGlvLN9c SY6YNqdLKLYR51p7dLWMYs87QMnTjimDLRz14x9yhGA4f0zRQVGBdnRtq c1W2YoNZZZccixaOvPlBWkfugDR8L3QFOKE9L63NAgPjVt6MUEWuWLAtm Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10851"; a="1413573" X-IronPort-AV: E=Sophos;i="6.03,196,1694761200"; d="scan'208";a="1413573" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Oct 2023 01:31:29 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10851"; a="874639937" X-IronPort-AV: E=Sophos;i="6.03,196,1694761200"; d="scan'208";a="874639937" Received: from tciutacu-mobl.ger.corp.intel.com (HELO rrabie-mobl.amr.corp.intel.com) ([10.252.40.114]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Oct 2023 01:31:22 -0700 Date: Tue, 3 Oct 2023 11:31:20 +0300 (EEST) From: =?ISO-8859-15?Q?Ilpo_J=E4rvinen?= To: Lukas Wunner cc: Bjorn Helgaas , David Howells , David Woodhouse , Herbert Xu , "David S. Miller" , Alex Williamson , linux-pci@vger.kernel.org, linux-cxl@vger.kernel.org, linux-coco@lists.linux.dev, keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, kvm@vger.kernel.org, Jonathan Cameron , linuxarm@huawei.com, David Box , Dan Williams , Dave Jiang , "Li, Ming" , Zhi Wang , Alistair Francis , Wilfred Mallawa , Alexey Kardashevskiy , Tom Lendacky , Sean Christopherson , Alexander Graf Subject: Re: [PATCH 03/12] X.509: Move certificate length retrieval into new helper In-Reply-To: <16c06528d13b2c0081229a45cacd4b1b9cdff738.1695921657.git.lukas@wunner.de> Message-ID: <76259143-d07e-e042-73a1-677094211361@linux.intel.com> References: <16c06528d13b2c0081229a45cacd4b1b9cdff738.1695921657.git.lukas@wunner.de> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 03 Oct 2023 01:31:55 -0700 (PDT) On Thu, 28 Sep 2023, Lukas Wunner wrote: > The upcoming in-kernel SPDM library (Security Protocol and Data Model, > https://www.dmtf.org/dsp/DSP0274) needs to retrieve the length from > ASN.1 DER-encoded X.509 certificates. > > Such code already exists in x509_load_certificate_list(), so move it > into a new helper for reuse by SPDM. > > No functional change intended. > > Signed-off-by: Lukas Wunner > --- > crypto/asymmetric_keys/x509_loader.c | 38 +++++++++++++++++++--------- > include/keys/asymmetric-type.h | 2 ++ > 2 files changed, 28 insertions(+), 12 deletions(-) > > diff --git a/crypto/asymmetric_keys/x509_loader.c b/crypto/asymmetric_keys/x509_loader.c > index a41741326998..121460a0de46 100644 > --- a/crypto/asymmetric_keys/x509_loader.c > +++ b/crypto/asymmetric_keys/x509_loader.c > @@ -4,28 +4,42 @@ > #include > #include > > +int x509_get_certificate_length(const u8 *p, unsigned long buflen) Make the return type ssize_t. unsigned long -> size_t buflen (or perhaps ssize_t if you want to compare below to have the same signedness). > +{ > + int plen; ssize_t > + > + /* Each cert begins with an ASN.1 SEQUENCE tag and must be more > + * than 256 bytes in size. > + */ > + if (buflen < 4) > + return -EINVAL; > + > + if (p[0] != 0x30 && > + p[1] != 0x82) > + return -EINVAL; > + > + plen = (p[2] << 8) | p[3]; > + plen += 4; > + if (plen > buflen) > + return -EINVAL; > + > + return plen; > +} > +EXPORT_SYMBOL_GPL(x509_get_certificate_length); > + > int x509_load_certificate_list(const u8 cert_list[], > const unsigned long list_size, > const struct key *keyring) > { > key_ref_t key; > const u8 *p, *end; > - size_t plen; > + int plen; ssize_t plen. -- i. > > p = cert_list; > end = p + list_size; > while (p < end) { > - /* Each cert begins with an ASN.1 SEQUENCE tag and must be more > - * than 256 bytes in size. > - */ > - if (end - p < 4) > - goto dodgy_cert; > - if (p[0] != 0x30 && > - p[1] != 0x82) > - goto dodgy_cert; > - plen = (p[2] << 8) | p[3]; > - plen += 4; > - if (plen > end - p) > + plen = x509_get_certificate_length(p, end - p); > + if (plen < 0) > goto dodgy_cert; > > key = key_create_or_update(make_key_ref(keyring, 1), > diff --git a/include/keys/asymmetric-type.h b/include/keys/asymmetric-type.h > index 69a13e1e5b2e..6705cfde25b9 100644 > --- a/include/keys/asymmetric-type.h > +++ b/include/keys/asymmetric-type.h > @@ -84,6 +84,8 @@ extern struct key *find_asymmetric_key(struct key *keyring, > const struct asymmetric_key_id *id_2, > bool partial); > > +int x509_get_certificate_length(const u8 *p, unsigned long buflen); > + > int x509_load_certificate_list(const u8 cert_list[], const unsigned long list_size, > const struct key *keyring); > >