public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: AKASHI Takahiro <takahiro.akashi@linaro.org>
To: u-boot@lists.denx.de
Subject: [PATCH v2 3/8] lib: crypto: enable x509_check_for_self_signed()
Date: Wed, 8 Jul 2020 15:24:07 +0900	[thread overview]
Message-ID: <20200708062407.GB19234@laputa> (raw)
In-Reply-To: <cfe7d74d-786f-164b-d1a3-783de1cc0dfc@gmx.de>

Heinrich,

On Tue, Jul 07, 2020 at 12:02:51PM +0200, Heinrich Schuchardt wrote:
> On 16.06.20 07:26, AKASHI Takahiro wrote:
> > When the file, x509_public_key.c, was imported from linux code in
> >     commit b4adf627d5b7 ("lib: crypto: add x509 parser"),
> > x509_check_for_self_signed() was commented out for simplicity.
> >
> > Now it need be enabled in order to make pkcs7_verify_one(), which will be
> > imported in a later patch, functional.
> >
> > Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> > ---
> >  lib/crypto/x509_cert_parser.c |  2 --
> >  lib/crypto/x509_public_key.c  | 33 +++++++++++++++++++++++++--------
> >  2 files changed, 25 insertions(+), 10 deletions(-)
> >
> > diff --git a/lib/crypto/x509_cert_parser.c b/lib/crypto/x509_cert_parser.c
> > index 5f984b9dfdae..eb24349460c2 100644
> > --- a/lib/crypto/x509_cert_parser.c
> > +++ b/lib/crypto/x509_cert_parser.c
> > @@ -142,12 +142,10 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
> >  	}
> >  	cert->id = kid;
> >
> > -#ifndef __UBOOT__
> >  	/* Detect self-signed certificates */
> >  	ret = x509_check_for_self_signed(cert);
> >  	if (ret < 0)
> >  		goto error_decode;
> > -#endif
> >
> >  	kfree(ctx);
> >  	return cert;
> > diff --git a/lib/crypto/x509_public_key.c b/lib/crypto/x509_public_key.c
> > index 571af9a0adf9..91810a864049 100644
> > --- a/lib/crypto/x509_public_key.c
> > +++ b/lib/crypto/x509_public_key.c
> > @@ -8,6 +8,7 @@
> >  #define pr_fmt(fmt) "X.509: "fmt
> >  #ifdef __UBOOT__
> >  #include <common.h>
> > +#include <image.h>
> >  #include <dm/devres.h>
> >  #include <linux/compat.h>
> >  #include <linux/err.h>
> > @@ -18,6 +19,7 @@
> >  #include <linux/kernel.h>
> >  #ifdef __UBOOT__
> >  #include <crypto/x509_parser.h>
> > +#include <u-boot/rsa-checksum.h>
> >  #else
> >  #include <linux/slab.h>
> >  #include <keys/asymmetric-subtype.h>
> > @@ -35,7 +37,9 @@
> >  int x509_get_sig_params(struct x509_certificate *cert)
> >  {
> >  	struct public_key_signature *sig = cert->sig;
> > -#ifndef __UBOOT__
> > +#ifdef __UBOOT__
> > +	struct image_region region;
> > +#else
> >  	struct crypto_shash *tfm;
> >  	struct shash_desc *desc;
> >  	size_t desc_size;
> > @@ -63,12 +67,25 @@ int x509_get_sig_params(struct x509_certificate *cert)
> >  	sig->s_size = cert->raw_sig_size;
> >
> >  #ifdef __UBOOT__
> > -	/*
> > -	 * Note:
> > -	 * This part (filling sig->digest) should be implemented if
> > -	 * x509_check_for_self_signed() is enabled x509_cert_parse().
> > -	 * Currently, this check won't affect UEFI secure boot.
> > -	 */
> > +	if (!sig->hash_algo)
> > +		return -ENOPKG;
> > +	if (!strcmp(sig->hash_algo, "sha256"))
> > +		sig->digest_size = SHA256_SUM_LEN;
> > +	else if (!strcmp(sig->hash_algo, "sha1"))
> > +		sig->digest_size = SHA1_SUM_LEN;
> > +	else
> > +		return -ENOPKG;
> 
> It would be preferable to call hash_lookup_algo() instead of hard coding
> hash sizes in multiple places.

Maybe it's a matter of taste, but I don't see any benefit of
calling hash_lookup_algo() here as we name an algo name
explicitly.
It's nothing but an overhead.

-Takahiro Akashi


> Best regards
> 
> Heinrich
> 
> > +
> > +	sig->digest = calloc(1, sig->digest_size);
> > +	if (!sig->digest)
> > +		return -ENOMEM;
> > +
> > +	region.data = cert->tbs;
> > +	region.size = cert->tbs_size;
> > +	hash_calculate(sig->hash_algo, &region, 1, sig->digest);
> > +
> > +	/* TODO: is_hash_blacklisted()? */
> > +
> >  	ret = 0;
> >  #else
> >  	/* Allocate the hashing algorithm we're going to need and find out how
> > @@ -118,7 +135,6 @@ error:
> >  	return ret;
> >  }
> >
> > -#ifndef __UBOOT__
> >  /*
> >   * Check for self-signedness in an X.509 cert and if found, check the signature
> >   * immediately if we can.
> > @@ -175,6 +191,7 @@ not_self_signed:
> >  	return 0;
> >  }
> >
> > +#ifndef __UBOOT__
> >  /*
> >   * Attempt to parse a data blob for a key as an X509 certificate.
> >   */
> >
> 

  reply	other threads:[~2020-07-08  6:24 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-16  5:26 [PATCH v2 0/8] efi_loader: secure boot: support intermediate certificates in signature AKASHI Takahiro
2020-06-16  5:26 ` [PATCH v2 1/8] lib: rsa: export rsa_verify_with_pkey() AKASHI Takahiro
2020-07-07 10:06   ` Heinrich Schuchardt
2020-06-16  5:26 ` [PATCH v2 2/8] lib: crypto: add public_key_verify_signature() AKASHI Takahiro
2020-07-07 10:04   ` Heinrich Schuchardt
2020-07-08  6:21     ` AKASHI Takahiro
2020-06-16  5:26 ` [PATCH v2 3/8] lib: crypto: enable x509_check_for_self_signed() AKASHI Takahiro
2020-07-07 10:02   ` Heinrich Schuchardt
2020-07-08  6:24     ` AKASHI Takahiro [this message]
2020-06-16  5:26 ` [PATCH v2 4/8] lib: crypto: import pkcs7_verify.c from linux AKASHI Takahiro
2020-07-07 10:27   ` Heinrich Schuchardt
2020-07-08  6:30     ` AKASHI Takahiro
2020-06-16  5:26 ` [PATCH v2 5/8] lib: crypto: add pkcs7_digest() AKASHI Takahiro
2020-06-16  5:26 ` [PATCH v2 6/8] lib: crypto: export and enhance pkcs7_verify_one() AKASHI Takahiro
2020-07-07 10:32   ` Heinrich Schuchardt
2020-07-08  6:37     ` AKASHI Takahiro
2020-06-16  5:26 ` [PATCH v2 7/8] efi_loader: signature: rework for intermediate certificates support AKASHI Takahiro
2020-07-07 10:33   ` Heinrich Schuchardt
2020-06-16  5:26 ` [PATCH v2 8/8] test/py: efi_secboot: add test for intermediate certificates AKASHI Takahiro
2020-07-07 10:42   ` Heinrich Schuchardt
2020-07-08  6:39     ` AKASHI Takahiro
2020-07-09  0:58     ` using sudo?, " AKASHI Takahiro
2020-07-09  3:15       ` Tom Rini
2020-07-09  5:33         ` AKASHI Takahiro
2020-07-09 12:34           ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200708062407.GB19234@laputa \
    --to=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox