mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: mcgrof@gmail.com
Cc: mjg59@srcf.ucam.org, keyrings@linux-nfs.org,
	gregkh@linuxfoundation.org, kyle@kernel.org,
	linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
	seth.forshee@canonical.com,
	linux-security-module@vger.kernel.org, zohar@linux.vnet.ibm.com,
	dwmw2@infradead.org
Subject: [PATCH 18/20] X.509: Parse the keyUsage extension to detect key-signing keys [ver #5]
Date: Thu, 28 May 2015 16:48:53 +0100	[thread overview]
Message-ID: <20150528154853.1259.65019.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <20150528154605.1259.42518.stgit@warthog.procyon.org.uk>

Parse the keyUsage extension to detect keys for which the purpose is key
signing and to restrict their use only to the verification of signatures on
keys.

Not-yet-signed-off-by: David Howells <dhowells@redhat.com>
---

 crypto/asymmetric_keys/x509_cert_parser.c |   45 ++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
index 4d9e9a7f8fd2..82b869de1759 100644
--- a/crypto/asymmetric_keys/x509_cert_parser.c
+++ b/crypto/asymmetric_keys/x509_cert_parser.c
@@ -457,9 +457,51 @@ int x509_process_extension(void *context, size_t hdrlen,
 	struct x509_parse_context *ctx = context;
 	struct asymmetric_key_id *kid;
 	const unsigned char *v = value;
+	u32 shift, tmp;
 
 	pr_debug("Extension: %u\n", ctx->last_oid);
 
+	if (ctx->last_oid == OID_keyUsage) {
+		/* There's a bitstring inside the content that we want to get
+		 * at.  This bitstring is shuffled up so that the most
+		 * significant set bit is the MSB of the second byte.  The
+		 * first byte tells us how far we have to shift the string back
+		 * to put bit string bit 0 on a byte bit 0.
+		 */
+		if (vlen < 3)
+			return -EBADMSG;
+		if (v[0] != ASN1_BTS || v[1] != vlen - 2)
+			return -EBADMSG;
+		shift = v[2];
+		if (shift > 7)
+			return -EBADMSG;
+		v += 3;
+		vlen -= 3;
+		if (vlen > 2) {
+			/* We only care about the least significant 9 bits, but
+			 * they will be spread over up to two bytes (leading 0s
+			 * aren't stored).
+			 */
+			v += vlen - 2;
+			vlen = 2;
+		}
+
+		tmp = 0;
+		if (vlen == 2) {
+			tmp = (u32)*v << 8;
+			v++;
+		}
+		if (vlen >= 1)
+			tmp |= *v;
+		tmp >>= shift;
+		pr_debug("keyUsage %x\n", tmp);
+		/* check for keyCertSign */
+		if (tmp & (1 << 5))
+			ctx->cert->pub->usage_restriction =
+				PKEY_RESTRICTED_TO_KEY_SIGNING;
+		return 0;
+	}
+
 	if (ctx->last_oid == OID_subjectKeyIdentifier) {
 		/* Get hold of the key fingerprint */
 		if (ctx->cert->skid || vlen < 3)
@@ -492,7 +534,8 @@ int x509_process_extension(void *context, size_t hdrlen,
 		/* Get hold of the extended key usage information */
 		ctx->raw_extusage = v;
 		ctx->raw_extusage_size = vlen;
-		ctx->cert->pub->usage_restriction = PKEY_RESTRICTED_USAGE;
+		if (ctx->cert->pub->usage_restriction == PKEY_USAGE_NOT_SPECIFIED)
+			ctx->cert->pub->usage_restriction = PKEY_RESTRICTED_USAGE;
 		return 0;
 	}
 


  parent reply	other threads:[~2015-05-28 15:50 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-28 15:46 [PATCH 00/20] MODSIGN: Use PKCS#7 for module signatures " David Howells
2015-05-28 15:46 ` [PATCH 01/20] X.509: Extract both parts of the AuthorityKeyIdentifier " David Howells
2015-05-28 15:46 ` [PATCH 02/20] X.509: Support X.509 lookup by Issuer+Serial form " David Howells
2015-05-28 15:46 ` [PATCH 03/20] PKCS#7: Allow detached data to be supplied for signature checking purposes " David Howells
2015-05-28 15:46 ` [PATCH 04/20] MODSIGN: Provide a utility to append a PKCS#7 signature to a module " David Howells
2015-05-28 15:46 ` [PATCH 05/20] MODSIGN: Use PKCS#7 messages as module signatures " David Howells
2015-05-28 15:47 ` [PATCH 06/20] sign-file: Add option to only create signature file " David Howells
2015-05-28 15:47 ` [PATCH 07/20] system_keyring.c doesn't need to #include module-internal.h " David Howells
2015-05-28 15:47 ` [PATCH 08/20] MODSIGN: Extract the blob PKCS#7 signature verifier from module signing " David Howells
2015-05-28 15:47 ` [PATCH 09/20] modsign: Abort modules_install when signing fails " David Howells
2015-05-28 15:47 ` [PATCH 10/20] modsign: Allow password to be specified for signing key " David Howells
2015-05-28 15:47 ` [PATCH 11/20] modsign: Allow signing key to be PKCS#11 " David Howells
2015-05-28 15:47 ` [PATCH 12/20] modsign: Allow external signing key to be specified " David Howells
2015-05-28 15:48 ` [PATCH 13/20] modsign: Extract signing cert from CONFIG_MODULE_SIG_KEY if needed " David Howells
2015-05-28 15:48 ` [PATCH 14/20] modsign: Use single PEM file for autogenerated key " David Howells
2015-05-28 15:48 ` [PATCH 15/20] modsign: Add explicit CONFIG_SYSTEM_TRUSTED_KEYS option " David Howells
2015-05-28 15:48 ` [PATCH 16/20] PKCS#7: Add an optional authenticated attribute to hold firmware name " David Howells
2015-05-29  1:30   ` Andy Lutomirski
2015-05-29 12:40   ` David Howells
2015-05-29 18:38     ` Andy Lutomirski
2015-06-01 15:50     ` David Howells
2015-06-01 17:06       ` Andy Lutomirski
2015-05-28 15:48 ` [PATCH 17/20] X.509: Restrict the usage of a key based on information in X.509 certificate " David Howells
2015-05-28 15:48 ` David Howells [this message]
2015-05-28 15:49 ` [PATCH 19/20] KEYS: Restrict signature verification to keys appropriate to the purpose " David Howells
2015-05-28 15:49 ` [PATCH 20/20] sign-file: use .p7s instead of .pkcs7 file extension " David Howells
2015-05-28 15:52 ` [PATCH 00/20] MODSIGN: Use PKCS#7 for module signatures " David Woodhouse
2015-05-28 15:57 ` David Howells

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=20150528154853.1259.65019.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=dwmw2@infradead.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=keyrings@linux-nfs.org \
    --cc=kyle@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=mcgrof@gmail.com \
    --cc=mjg59@srcf.ucam.org \
    --cc=seth.forshee@canonical.com \
    --cc=zohar@linux.vnet.ibm.com \
    /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

all inboxes | Powered by JetHome®