Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 649F3C432BE for ; Wed, 28 Jul 2021 18:33:38 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 953A36103C for ; Wed, 28 Jul 2021 18:33:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 953A36103C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=konsulko.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9C03D82C84; Wed, 28 Jul 2021 20:33:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=konsulko.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=konsulko.com header.i=@konsulko.com header.b="Fyr0DCrZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CA26C82C88; Wed, 28 Jul 2021 20:33:32 +0200 (CEST) Received: from mail-qt1-x832.google.com (mail-qt1-x832.google.com [IPv6:2607:f8b0:4864:20::832]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 664A582314 for ; Wed, 28 Jul 2021 20:33:29 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=konsulko.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=trini@konsulko.com Received: by mail-qt1-x832.google.com with SMTP id d9so2036276qty.12 for ; Wed, 28 Jul 2021 11:33:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=jFdOiYjV24uftSxT72OMfBKHlOig04ga/OcnCivzcrg=; b=Fyr0DCrZfDIzhs6kqWq9t2b7nwy+DgfFOVhRb+DHAnv7ZnEH8O7GzdE1hxse8JIMGC mI1D4t3JKZzm8mGssHAl5EdvzaL/8Wxja0cUXLGXRRgzLSpys83OBicebBGyIBqASy8q oNefigfFjHWwdc6Rn9z8K27WmAus5TBg3BQok= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=jFdOiYjV24uftSxT72OMfBKHlOig04ga/OcnCivzcrg=; b=MpLIR0Mydm+rg4mxmK1Y8XMWhim8q5r/UtvFRuv4DhsR0bgyuluFpv9HujkUE4Nhgr 7+WYuMrF+c90woHg/oUZFQWQA91sB4ApmBhL6FzVoKor0KOUshqBS3xRauuIzrFz89gT 0Hv53S+IdYZ8ACNJK/pKhCxxCyo1OJRyFhpGhmJMeuk5aNDfi+Ra8WFILhEkSWtTBYIB JM8RlrKMUdqcWqHXHkkQZUa8EXg3xjmx3QTN5qVQ6s8LrGWFeaCYM0Cr1zLN9jBf5FxA R3JtLRpI/p8QUIywn0er8gkdqz8vznHGIXq8VlKAicZBBkoI+VzR9lRuO1pzRlXa2Azx mD3w== X-Gm-Message-State: AOAM532IEl9xlf9+yYb0oUtmPR+2NLmhf4XZqWGwWdaL4To4SMnjI8kr xmIvXI68CZu4Ae8J3e1X4hYxhQ== X-Google-Smtp-Source: ABdhPJwet+ftAzK1UnNlOU3rM3GUhKK8a0cvxeOs/WQm57zvtFoFitFeuQn1CqzfDTUYVirSb8OkXA== X-Received: by 2002:ac8:5502:: with SMTP id j2mr870446qtq.116.1627497208191; Wed, 28 Jul 2021 11:33:28 -0700 (PDT) Received: from bill-the-cat (2603-6081-7b01-cbda-3037-3a3d-435c-0707.res6.spectrum.com. [2603:6081:7b01:cbda:3037:3a3d:435c:707]) by smtp.gmail.com with ESMTPSA id c2sm283009qtw.30.2021.07.28.11.33.27 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 28 Jul 2021 11:33:27 -0700 (PDT) Date: Wed, 28 Jul 2021 14:33:25 -0400 From: Tom Rini To: "Chan, Donald" Cc: u-boot@lists.denx.de, Alexandru Gagniuc , Simon Glass Subject: Re: [PATCH] lib: rsa: Extract public key from private key if keyfile argument is used Message-ID: <20210728183325.GW9379@bill-the-cat> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="yHz7pC0TR255sgha" Content-Disposition: inline In-Reply-To: X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.4 (2018-02-28) X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean --yHz7pC0TR255sgha Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Jul 18, 2021 at 09:52:03AM -0700, Chan, Donald wrote: > If the 'keyfile' (-G) argument is used, there is little value to require > 'keydir' (-k) argument since the public key can also be extracted from the > private key itself. >=20 > Signed-off-by: Donald Chan > --- > lib/rsa/rsa-sign.c | 28 +++++++++++++++++++++------- > 1 file changed, 21 insertions(+), 7 deletions(-) >=20 > diff --git a/lib/rsa/rsa-sign.c b/lib/rsa/rsa-sign.c > index f4ed11e74a..f70f352311 100644 > --- a/lib/rsa/rsa-sign.c > +++ b/lib/rsa/rsa-sign.c > @@ -49,16 +49,16 @@ static int rsa_err(const char *msg) > } >=20 > /** > - * rsa_pem_get_pub_key() - read a public key from a .crt file > + * rsa_pem_get_pub_key() - read a public key from a private key file or > .crt file > * > - * @keydir: Directory containins the key > - * @name Name of key file (will have a .crt extension) > + * @keydir: Directory containing the key, can be NULL > + * @name Name of key file (will apply a .crt extension if keydir is not > NULL) > * @evpp Returns EVP_PKEY object, or NULL on failure > * @return 0 if ok, -ve on error (in which case *evpp will be set to NUL= L) > */ > static int rsa_pem_get_pub_key(const char *keydir, const char *name, > EVP_PKEY **evpp) > { > - char path[1024]; > + char path[1024] =3D {0}; > EVP_PKEY *key =3D NULL; > X509 *cert; > FILE *f; > @@ -68,7 +68,10 @@ static int rsa_pem_get_pub_key(const char *keydir, con= st > char *name, EVP_PKEY ** > return -EINVAL; >=20 > *evpp =3D NULL; > - snprintf(path, sizeof(path), "%s/%s.crt", keydir, name); > + if (keydir && name) > + snprintf(path, sizeof(path), "%s/%s.crt", keydir, name); > + else if (name) > + snprintf(path, sizeof(path), "%s", name); > f =3D fopen(path, "r"); > if (!f) { > fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", > @@ -76,7 +79,13 @@ static int rsa_pem_get_pub_key(const char *keydir, con= st > char *name, EVP_PKEY ** > return -EACCES; > } >=20 > - /* Read the certificate */ > + /* See if it contains a PEM private key? */ > + if (PEM_read_PrivateKey(f, evpp, NULL, path)) { > + fclose(f); > + return 0; > + } > + > + /* Not a PEM private key, read the certificate */ > cert =3D NULL; > if (!PEM_read_X509(f, &cert, NULL, NULL)) { > rsa_err("Couldn't read certificate"); > @@ -672,7 +681,12 @@ int rsa_add_verify_data(struct image_sign_info *info, > void *keydest) > if (ret) > return ret; > } > - ret =3D rsa_get_pub_key(info->keydir, info->keyname, e, &pkey); > + if (info->keydir && info->keyname) > + ret =3D rsa_get_pub_key(info->keydir, info->keyname, e, &pkey); > + else if (info->keyfile) > + ret =3D rsa_get_pub_key(NULL, info->keyfile, e, &pkey); > + else > + ret =3D -EINVAL; > if (ret) > goto err_get_pub_key; > #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ This seems reasonable, but the formatting of the patch was destroyed somewhere along the way, can you please resend? Thanks. --=20 Tom --yHz7pC0TR255sgha Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEEGjx/cOCPqxcHgJu/FHw5/5Y0tywFAmEBou4ACgkQFHw5/5Y0 tywInQwAsZj6HrOidbY6wOR3zAA/6IOfZOznInQesCUGTcRsNv4IFSa5+JB4U6k0 RhqDQspo7yT2PSCATj7mRXxbXzaLOztzumiAGjoHo09a/HSfFWUqoaUvLFmt6cud 07hvNAtlLd5gS9B7/ktonCWSjEM55csrY0m4737OToP8Pg99ushbbRcOsL2aELu5 bMY8pBFGxEKNhaxsmxLoE3OO+xCDalZaSBFSVuNSoXaK9uxWwxQXMqI0C1bAgA3c aHHq/JPtej0dN8g4q7hRMYA5awrwo25bvssE4z03Qww/JQYWArKSxt7fPK/uSmKS a9pRWTBmQzE38qg6q0K4KVgaJOKZIt5Fzey8/OtnGLk067KcwNcqn1mVUgz6Nirs jcbi7h7Sghrdk2VkurmdQywOmACNq2DSdWN1trAEZzFTW/Q/2gTigTJlOTlbBYnU lem0sco7cpBa9Lbyhq3PZoS4QHPYa1SCH7jrQLLc5Ce4Fk15/xHPf8rlCCryIFhP lDVWqMXP =VuJn -----END PGP SIGNATURE----- --yHz7pC0TR255sgha--