Skip to contentSkip to conversation

MODSIGN: Use PKCS#7 for module signatures

PATCH In review David Howells 4 participants 32 messages Nov 20, 2014 – Dec 4, 2014

Subsystems (4)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

cover MODSIGN: Use PKCS#7 for module signatures

Here's a set of patches that does the following:

(1) Extracts both parts of an X.509 AuthorityKeyIdentifier (AKID) extension. We already extract the bit that can match the subjectKeyIdentifier (SKID) of the parent X.509 cert, but we currently ignore the bits that can match the issuer and serialNumber.

Looks up an X.509 cert by issuer and serialNumber if those are provided in the AKID. If the keyIdentifier is also provided, checks that the subjectKeyIdentifier of the cert found matches that also.

If no issuer and serialNumber are provided in the AKID, looks up an X.509 cert by SKID using the AKID keyIdentifier.

This allows module signing to be done with certificates that don't have an SKID by which they can be looked up.

(2) Makes use of the PKCS#7 facility to provide module signatures.

sign-file is replaced with a program that generates a PKCS#7 message that has no X.509 certs embedded and that has detached data (the module content) and adds it onto the message with magic string and descriptor.

(3) The PKCS#7 message (and matching X.509 cert) supply all the information that is needed to select the X.509 cert to be used to verify the signature by standard means (including selection of digest algorithm and public key algorithm). No kernel-specific magic values are required.

Note that the revised sign-file program no longer supports the "-s <signature>" option as I'm not sure what the best way to deal with this is. Do we generate a PKCS#7 cert from the signature given, or do we get given a PKCS#7 cert? I lean towards the latter.

They can be found here also:

http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=modsign-pkcs7

These patches are based on the security tree's next branch.

David
---
David Howells (5):
X.509: Extract both parts of the AuthorityKeyIdentifier X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier PKCS#7: Allow detached data to be supplied for signature checking purposes
MODSIGN: Provide a utility to append a PKCS#7 signature to a module
MODSIGN: Use PKCS#7 messages as module signatures

 crypto/asymmetric_keys/Makefile           |    8 -
 crypto/asymmetric_keys/pkcs7_trust.c      |   10 -
 crypto/asymmetric_keys/pkcs7_verify.c     |   81 ++++--
 crypto/asymmetric_keys/x509_akid.asn1     |   35 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  142 ++++++----
 crypto/asymmetric_keys/x509_parser.h      |    3 
 crypto/asymmetric_keys/x509_public_key.c  |   85 ++++--
 include/crypto/pkcs7.h                    |    3 
 include/crypto/public_key.h               |    4 
 init/Kconfig                              |    1 
 kernel/module_signing.c                   |  220 +++------------
 scripts/Makefile                          |    2 
 scripts/sign-file                         |  421 -----------------------------
 scripts/sign-file.c                       |  189 +++++++++++++
 14 files changed, 505 insertions(+), 699 deletions(-)
 create mode 100644 crypto/asymmetric_keys/x509_akid.asn1
 delete mode 100755 scripts/sign-file
 create mode 100755 scripts/sign-file.c
author maintainer
#1 lore raw

PATCH 1/5 X.509: Extract both parts of the AuthorityKeyIdentifier

Extract both parts of the AuthorityKeyIdentifier, not just the keyIdentifier, as the second part can be used to match X.509 certificates by issuer and serialNumber.

---

 crypto/asymmetric_keys/Makefile           |    8 +-
 crypto/asymmetric_keys/pkcs7_trust.c      |    4 -
 crypto/asymmetric_keys/pkcs7_verify.c     |   12 +-
 crypto/asymmetric_keys/x509_akid.asn1     |   35 +++++++
 crypto/asymmetric_keys/x509_cert_parser.c |  142 ++++++++++++++++++-----------
 crypto/asymmetric_keys/x509_parser.h      |    3 -
 crypto/asymmetric_keys/x509_public_key.c  |    8 +-
 7 files changed, 144 insertions(+), 68 deletions(-)
 create mode 100644 crypto/asymmetric_keys/x509_akid.asn1
crypto/asymmetric_keys/Makefile +7 -1
index e47fcd9ac5e8..cd1406f9b14a 100644
@@ -15,15 +15,21 @@ obj-$(CONFIG_PUBLIC_KEY_ALGO_RSA) += rsa.o
 obj-$(CONFIG_X509_CERTIFICATE_PARSER) += x509_key_parser.o
 x509_key_parser-y := \
 	x509-asn1.o \
+	x509_akid-asn1.o \
 	x509_rsakey-asn1.o \
 	x509_cert_parser.o \
 	x509_public_key.o
 
-$(obj)/x509_cert_parser.o: $(obj)/x509-asn1.h $(obj)/x509_rsakey-asn1.h
+$(obj)/x509_cert_parser.o: \
+	$(obj)/x509-asn1.h \
+	$(obj)/x509_akid-asn1.h \
+	$(obj)/x509_rsakey-asn1.h
 $(obj)/x509-asn1.o: $(obj)/x509-asn1.c $(obj)/x509-asn1.h
+$(obj)/x509_akid-asn1.o: $(obj)/x509_akid-asn1.c $(obj)/x509_akid-asn1.h
 $(obj)/x509_rsakey-asn1.o: $(obj)/x509_rsakey-asn1.c $(obj)/x509_rsakey-asn1.h
 
 clean-files	+= x509-asn1.c x509-asn1.h
+clean-files	+= x509_akid-asn1.c x509_akid-asn1.h
 clean-files	+= x509_rsakey-asn1.c x509_rsakey-asn1.h
 
 #
crypto/asymmetric_keys/pkcs7_trust.c +2 -2
index 1d29376072da..f802cf118053 100644
@@ -85,8 +85,8 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
 	/* No match - see if the root certificate has a signer amongst the
 	 * trusted keys.
 	 */
-	if (last && last->authority) {
-		key = x509_request_asymmetric_key(trust_keyring, last->authority,
+	if (last && last->auth_skid) {
+		key = x509_request_asymmetric_key(trust_keyring, last->auth_skid,
 						  false);
 		if (!IS_ERR(key)) {
 			x509 = last;
crypto/asymmetric_keys/pkcs7_verify.c +5 -7
index cd455450b069..5e956c5b9071 100644
@@ -187,11 +187,11 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
 			goto maybe_missing_crypto_in_x509;
 
 		pr_debug("- issuer %s\n", x509->issuer);
-		if (x509->authority)
+		if (x509->auth_skid)
 			pr_debug("- authkeyid %*phN\n",
-				 x509->authority->len, x509->authority->data);
+				 x509->auth_skid->len, x509->auth_skid->data);
 
-		if (!x509->authority ||
+		if (!x509->auth_skid ||
 		    strcmp(x509->subject, x509->issuer) == 0) {
 			/* If there's no authority certificate specified, then
 			 * the certificate must be self-signed and is the root
@@ -216,13 +216,13 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
 		 * list to see if the next one is there.
 		 */
 		pr_debug("- want %*phN\n",
-			 x509->authority->len, x509->authority->data);
+			 x509->auth_skid->len, x509->auth_skid->data);
 		for (p = pkcs7->certs; p; p = p->next) {
 			if (!p->skid)
 				continue;
 			pr_debug("- cmp [%u] %*phN\n",
 				 p->index, p->skid->len, p->skid->data);
-			if (asymmetric_key_id_same(p->skid, x509->authority))
+			if (asymmetric_key_id_same(p->skid, x509->auth_skid))
 				goto found_issuer;
 		}
 
@@ -338,8 +338,6 @@ int pkcs7_verify(struct pkcs7_message *pkcs7)
 		ret = x509_get_sig_params(x509);
 		if (ret < 0)
 			return ret;
-		pr_debug("X.509[%u] %*phN\n",
-			 n, x509->authority->len, x509->authority->data);
 	}
 
 	for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
crypto/asymmetric_keys/x509_akid.asn1 new file +35 -0
new file mode 100644
index 000000000000..1a33231a75a8
@@ -0,0 +1,35 @@
+-- X.509 AuthorityKeyIdentifier
+-- rfc5280 section 4.2.1.1
+
+AuthorityKeyIdentifier ::= SEQUENCE {
+	keyIdentifier			[0] IMPLICIT KeyIdentifier		OPTIONAL,
+	authorityCertIssuer		[1] IMPLICIT GeneralNames		OPTIONAL,
+	authorityCertSerialNumber	[2] IMPLICIT CertificateSerialNumber	OPTIONAL
+	}
+
+KeyIdentifier ::= OCTET STRING ({ x509_akid_note_kid })
+
+CertificateSerialNumber ::= INTEGER ({ x509_akid_note_serial })
+
+GeneralNames ::= SEQUENCE OF GeneralName
+
+GeneralName ::= CHOICE {
+	otherName			[0] ANY,
+	rfc822Name			[1] IA5String,
+	dNSName				[2] IA5String,
+	x400Address			[3] ANY,
+	directoryName			[4] Name ({ x509_akid_note_name }),
+	ediPartyName			[5] ANY,
+	uniformResourceIdentifier	[6] IA5String,
+	iPAddress			[7] OCTET STRING,
+	registeredID			[8] OBJECT IDENTIFIER
+	}
+
+Name ::= SEQUENCE OF RelativeDistinguishedName
+
+RelativeDistinguishedName ::= SET OF AttributeValueAssertion
+
+AttributeValueAssertion ::= SEQUENCE {
+	attributeType		OBJECT IDENTIFIER ({ x509_note_OID }),
+	attributeValue		ANY ({ x509_extract_name_segment })
+	}
crypto/asymmetric_keys/x509_cert_parser.c +89 -53

181 lines not on this page · raw message

crypto/asymmetric_keys/x509_parser.h +2 -1
index 3dfe6b5d6f0b..223b72344060 100644
@@ -21,7 +21,8 @@ struct x509_certificate {
 	char		*subject;		/* Name of certificate subject */
 	struct asymmetric_key_id *id;		/* Serial number + issuer */
 	struct asymmetric_key_id *skid;		/* Subject + subjectKeyId (optional) */
-	struct asymmetric_key_id *authority;	/* Authority key identifier (optional) */
+	struct asymmetric_key_id *auth_id;	/* CA AuthKeyId matching ->id (optional) */
+	struct asymmetric_key_id *auth_skid;	/* CA AuthKeyId matching ->skid (optional) */
 	struct tm	valid_from;
 	struct tm	valid_to;
 	const void	*tbs;			/* Signed data */
crypto/asymmetric_keys/x509_public_key.c +4 -4
index a6c42031628e..a3d9ba999da5 100644
@@ -214,10 +214,10 @@ static int x509_validate_trust(struct x509_certificate *cert,
 	if (!trust_keyring)
 		return -EOPNOTSUPP;
 
-	if (ca_keyid && !asymmetric_key_id_partial(cert->authority, ca_keyid))
+	if (ca_keyid && !asymmetric_key_id_partial(cert->auth_skid, ca_keyid))
 		return -EPERM;
 
-	key = x509_request_asymmetric_key(trust_keyring, cert->authority,
+	key = x509_request_asymmetric_key(trust_keyring, cert->auth_skid,
 					  false);
 	if (!IS_ERR(key))  {
 		if (!use_builtin_keys
@@ -274,8 +274,8 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
 	cert->pub->id_type = PKEY_ID_X509;
 
 	/* Check the signature on the key if it appears to be self-signed */
-	if (!cert->authority ||
-	    asymmetric_key_id_same(cert->skid, cert->authority)) {
+	if (!cert->auth_skid ||
+	    asymmetric_key_id_same(cert->skid, cert->auth_skid)) {
 		ret = x509_check_signature(cert->pub, cert); /* self-signed */
 		if (ret < 0)
 			goto error_free_cert;
#2 lore raw

Re: [PATCH 1/5] X.509: Extract both parts of the AuthorityKeyIdentifier

On Thu, Nov 20, 2014 at 04:54:03PM +0000, David Howells wrote:

[..]

crypto/asymmetric_keys/x509_parser.h struct x509_certificate { · line 25

Quoted 11 lines
crypto/asymmetric_keys/x509_parser.h +2 -1
index 3dfe6b5d6f0b..223b72344060 100644
@@ -21,7 +21,8 @@ struct x509_certificate {
 	char		*subject;		/* Name of certificate subject */
 	struct asymmetric_key_id *id;		/* Serial number + issuer */
 	struct asymmetric_key_id *skid;		/* Subject + subjectKeyId (optional) */
-	struct asymmetric_key_id *authority;	/* Authority key identifier (optional) */
+	struct asymmetric_key_id *auth_id;	/* CA AuthKeyId matching ->id (optional) */
+	struct asymmetric_key_id *auth_skid;	/* CA AuthKeyId matching ->skid (optional) */

A very minor nit. It might help if we put additional comment to explain what auth_id and auth_skid are composed of (like other key ids).

auth_id /* akid issuer + akid serial */
auth_skid /* issuer + akid keyid */

Thanks
Vivek

author maintainer
#3 lore raw

Re: [PATCH 1/5] X.509: Extract both parts of the AuthorityKeyIdentifier

Quoted 2 lines · Vivek Goyal

A very minor nit. It might help if we put additional comment to explain what auth_id and auth_skid are composed of (like other key ids).

I thought it better to show what they match - ie. auth_id matches id and auth_skid matches skid from the same structure. The id and skid members show their composition (and I should fix the comment on id so that the bits are the right way round).

David

#4 lore raw

Re: [PATCH 1/5] X.509: Extract both parts of the AuthorityKeyIdentifier

In reply to Vivek Goyal
Quoted 17 lines · Vivek Goyal

On Thu, Nov 20, 2014 at 04:54:03PM +0000, David Howells wrote:

[..]

crypto/asymmetric_keys/x509_parser.h struct x509_certificate { · line 25

Quoted 11 lines
crypto/asymmetric_keys/x509_parser.h +2 -1
index 3dfe6b5d6f0b..223b72344060 100644
@@ -21,7 +21,8 @@ struct x509_certificate {
 	char		*subject;		/* Name of certificate subject */
 	struct asymmetric_key_id *id;		/* Serial number + issuer */
 	struct asymmetric_key_id *skid;		/* Subject + subjectKeyId (optional) */
-	struct asymmetric_key_id *authority;	/* Authority key identifier (optional) */
+	struct asymmetric_key_id *auth_id;	/* CA AuthKeyId matching ->id (optional) */
+	struct asymmetric_key_id *auth_skid;	/* CA AuthKeyId matching ->skid (optional) */

A very minor nit. It might help if we put additional comment to explain what auth_id and auth_skid are composed of (like other key ids).

Quoted 6 lines

auth_id /* akid issuer + akid serial */
auth_skid /* issuer + akid keyid */

Thanks
Vivek

Right,

David did not address this in his v2 patchset...

- Dmitry

author maintainer
#5 lore raw

Re: [PATCH 1/5] X.509: Extract both parts of the AuthorityKeyIdentifier

Quoted 9 lines · Dmitry Kasatkin
Quoted 9 lines

crypto/asymmetric_keys/x509_parser.h struct x509_certificate { · line 25

Quoted 3 lines
crypto/asymmetric_keys/x509_parser.h +2 -1
-	struct asymmetric_key_id *authority;	/* Authority key identifier (optional) */
+	struct asymmetric_key_id *auth_id;	/* CA AuthKeyId matching ->id (optional) */
+	struct asymmetric_key_id *auth_skid;	/* CA AuthKeyId matching ->skid (optional) */

A very minor nit. It might help if we put additional comment to explain what auth_id and auth_skid are composed of (like other key ids).

auth_id /* akid issuer + akid serial */
auth_skid /* issuer + akid keyid */

Quoted 7 lines
Quoted 3 lines

Thanks
Vivek

Right,

David did not address this in his v2 patchset...

I decided against changing them on the basis that I'd prefer to show what they match over the way they are fabricated. The id and skid members do show how they are fabricated. If you really want, I can show both - but my thought is that if you look at how AuthorityKeyIdentifier is constructed, you can work it out reasonably easily.

David

author maintainer
#6 lore raw

PATCH 2/5 X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

In reply to David Howells

If an X.509 certificate has an AuthorityKeyIdentifier extension that provides an issuer and serialNumber, then make it so that these are used in preference to the keyIdentifier field also held therein for searching for the signing certificate.

If both the issuer+serialNumber and the keyIdentifier are supplied, then the certificate is looked up by the former but the latter is checked as well. If the latter doesn't match the subjectKeyIdentifier of the parent certificate, EKEYREJECTED is returned.

This makes it possible to chain X.509 certificates based on the issuer and serialNumber fields rather than on subjectKeyIdentifier. This is necessary as we are having to deal with keys that are represented by X.509 certificates that lack a subjectKeyIdentifier.

---

 crypto/asymmetric_keys/pkcs7_trust.c     |   10 +++-
 crypto/asymmetric_keys/pkcs7_verify.c    |   47 +++++++++++++----
 crypto/asymmetric_keys/x509_public_key.c |   83 +++++++++++++++++++++---------
 include/crypto/public_key.h              |    3 +
 4 files changed, 102 insertions(+), 41 deletions(-)
crypto/asymmetric_keys/pkcs7_trust.c +7 -3
index f802cf118053..11f8ca752e2a 100644
@@ -54,7 +54,8 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
 		/* Look to see if this certificate is present in the trusted
 		 * keys.
 		 */
-		key = x509_request_asymmetric_key(trust_keyring, x509->id,
+		key = x509_request_asymmetric_key(trust_keyring,
+						  x509->id, x509->skid,
 						  false);
 		if (!IS_ERR(key)) {
 			/* One of the X.509 certificates in the PKCS#7 message
@@ -85,8 +86,10 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
 	/* No match - see if the root certificate has a signer amongst the
 	 * trusted keys.
 	 */
-	if (last && last->auth_skid) {
-		key = x509_request_asymmetric_key(trust_keyring, last->auth_skid,
+	if (last && (last->auth_id || last->auth_skid)) {
+		key = x509_request_asymmetric_key(trust_keyring,
+						  last->auth_id,
+						  last->auth_skid,
 						  false);
 		if (!IS_ERR(key)) {
 			x509 = last;
@@ -103,6 +106,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
 	 */
 	key = x509_request_asymmetric_key(trust_keyring,
 					  sinfo->signing_cert_id,
+					  NULL,
 					  false);
 	if (!IS_ERR(key)) {
 		pr_devel("sinfo %u: Direct signer is key %x\n",
crypto/asymmetric_keys/pkcs7_verify.c +36 -11

74 lines not on this page · raw message

crypto/asymmetric_keys/x509_public_key.c +57 -26

127 lines not on this page · raw message

include/crypto/public_key.h +2 -1
index 54add2069901..b6f27a240856 100644
@@ -101,7 +101,8 @@ extern int verify_signature(const struct key *key,
 
 struct asymmetric_key_id;
 extern struct key *x509_request_asymmetric_key(struct key *keyring,
-					       const struct asymmetric_key_id *kid,
+					       const struct asymmetric_key_id *id,
+					       const struct asymmetric_key_id *skid,
 					       bool partial);
 
 #endif /* _LINUX_PUBLIC_KEY_H */
#7 lore raw

Re: [PATCH 2/5] X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

On Thu, Nov 20, 2014 at 04:54:14PM +0000, David Howells wrote:

[..]

Quoted 39 lines
crypto/asymmetric_keys/pkcs7_verify.c +20 -9
@@ -215,21 +219,42 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
 		/* Look through the X.509 certificates in the PKCS#7 message's
 		 * list to see if the next one is there.
 		 */
-		pr_debug("- want %*phN\n",
-			 x509->auth_skid->len, x509->auth_skid->data);
-		for (p = pkcs7->certs; p; p = p->next) {
-			if (!p->skid)
-				continue;
-			pr_debug("- cmp [%u] %*phN\n",
-				 p->index, p->skid->len, p->skid->data);
-			if (asymmetric_key_id_same(p->skid, x509->auth_skid))
-				goto found_issuer;
+		auth = x509->auth_id;
+		if (auth) {
+			pr_debug("- want %*phN\n", auth->len, auth->data);
+			for (p = pkcs7->certs; p; p = p->next) {
+				pr_debug("- cmp [%u] %*phN\n",
+					 p->index, p->id->len, p->id->data);
+				if (asymmetric_key_id_same(p->id, auth))
+					goto found_issuer_check_skid;
+			}
+		} else {
+			auth = x509->auth_skid;
+			pr_debug("- want %*phN\n", auth->len, auth->data);
+			for (p = pkcs7->certs; p; p = p->next) {
+				if (!p->skid)
+					continue;
+				pr_debug("- cmp [%u] %*phN\n",
+					 p->index, p->skid->len, p->skid->data);
+				if (asymmetric_key_id_same(p->skid, auth))
+					goto found_issuer;
+			}
 		}
 
 		/* We didn't find the root of this chain */
 		pr_debug("- top\n");
 		return 0;
 

crypto/asymmetric_keys/pkcs7_verify.c static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, · line 257

Quoted 10 lines
crypto/asymmetric_keys/pkcs7_verify.c +10 -0
+	found_issuer_check_skid:
+		/* We matched issuer + serialNumber, but if there's an
+		 * authKeyId.keyId, that must match the CA subjKeyId also.
+		 */
+		if (x509->auth_skid &&
+		    !asymmetric_key_id_same(p->skid, x509->auth_skid)) {
+			pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
+				sinfo->index, x509->index, p->index);
+			return -EKEYREJECTED;
+		}

Hi David,

A minor nit.

pkcs7_verify_sig_chain() is getting big with multiple goto labels. Will it make sense to introduce a helper function to see if cert B is authority cert of cert A or not. And then we should be able to get rid of labels like found_issuer_check_skid() and some of the inline code also go away.

Thanks
Vivek

#8 lore raw

Re: [PATCH 2/5] X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

In reply to David Howells

Don't assume that the issuer & serialNumber are specified.

---
crypto/asymmetric_keys/x509_public_key.c | 11 +++++++----

1 file changed, 7 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/crypto/asymmetric_keys/x509_public_key.c
index 9c1a3d6..510269f 100644

crypto/asymmetric_keys/x509_public_key.c +1 -0
@@ -9,6 +9,7 @@
  * 2 of the Licence, or (at your option) any later version.
  */
 
+#define DEBUG 1

#define pr_fmt(fmt) "X.509: "fmt
#include <linux/module.h>
#include <linux/kernel.h>
@@ -68,12 +69,14 @@ struct key *x509_request_asymmetric_key(struct key *keyring,
{
struct key *key;
key_ref_t ref;
- const char *lookup;
+ const char *lookup = NULL;
char *req, *p;
- int len;
+ int len = 0;

- lookup = id->data;
- len = id->len;
+ if (id) {
+ lookup = id->data;
+ len = id->len;
+ }
if (!lookup) {
lookup = skid->data;
len = skid->len;

Signature · 1 line
1.8.1.4
#9 lore raw

Re: [Keyrings] [PATCH 2/5] X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

Previous post was mangled. Reposting ...
---

Don't assume that the issuer & serialNumber are specified.

---
 crypto/asymmetric_keys/x509_public_key.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
crypto/asymmetric_keys/x509_public_key.c +7 -4
index 9c1a3d6..510269f 100644
@@ -9,6 +9,7 @@
  * 2 of the Licence, or (at your option) any later version.
  */
 
+#define DEBUG 1
 #define pr_fmt(fmt) "X.509: "fmt
 #include <linux/module.h>
 #include <linux/kernel.h>
@@ -68,12 +69,14 @@ struct key *x509_request_asymmetric_key(struct key *keyring,
 {
 	struct key *key;
 	key_ref_t ref;
-	const char *lookup;
+	const char *lookup = NULL;
 	char *req, *p;
-	int len;
+	int len = 0;
 
-	lookup = id->data;
-	len = id->len;
+        if (id) {
+		lookup = id->data;
+		len = id->len;
+	}
 	if (!lookup) {
 		lookup = skid->data;
 		len = skid->len;
Signature · 1 line
1.8.1.4
author maintainer
#10 lore raw

Re: [Keyrings] [PATCH 2/5] X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

Quoted 1 line · Mimi Zohar

Don't assume that the issuer & serialNumber are specified.

Bah... I didn't assume, I just did it wrong.

Quoted 1 line

+#define DEBUG 1

Shouldn't be in the patch.

Quoted 1 line

+ if (id) {

Tab, not spaces.

I'll fix these up and merge your patch into mine to avoid the issues coming out in bisection.

David

#11 lore raw

Re: [Keyrings] [PATCH 2/5] X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

Quoted 8 lines · David Howells
Quoted 1 line · Mimi Zohar

Don't assume that the issuer & serialNumber are specified.

Bah... I didn't assume, I just did it wrong.

Quoted 1 line

+#define DEBUG 1

Quoted 8 lines

Shouldn't be in the patch.

Quoted 1 line

+ if (id) {

Tab, not spaces.

I'll fix these up and merge your patch into mine to avoid the issues coming out in bisection.

Thanks!

Mimi

author maintainer
#12 lore raw

Re: [Keyrings] [PATCH 2/5] X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

In reply to Mimi Zohar

Mimi Zohar <[email protected]> wrote:

crypto/asymmetric_keys/x509_public_key.c struct key *x509_request_asymmetric_key(struct key *keyring, · line 80

Quoted 5 lines · Mimi Zohar
crypto/asymmetric_keys/x509_public_key.c +4 -0
+        if (id) {
+		lookup = id->data;
+		len = id->len;
+	}
 	if (!lookup) {

The last line here can then just be replaced by an else-clause.

David

#13 lore raw

Re: [Keyrings] [PATCH 2/5] X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

Quoted 11 lines · David Howells

Mimi Zohar <[email protected]> wrote:

crypto/asymmetric_keys/x509_public_key.c struct key *x509_request_asymmetric_key(struct key *keyring, · line 80

Quoted 5 lines · Mimi Zohar
crypto/asymmetric_keys/x509_public_key.c +4 -0
+        if (id) {
+		lookup = id->data;
+		len = id->len;
+	}
 	if (!lookup) {

The last line here can then just be replaced by an else-clause.

I would assume so, but the original code looked like "id->data" could have been null.

Mimi

author maintainer
#14 lore raw

Re: [Keyrings] [PATCH 2/5] X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier

Quoted 2 lines · Mimi Zohar

I would assume so, but the original code looked like "id->data" could have been null.

It can't be - it's an embedded array.

David

author maintainer
#15 lore raw

PATCH 3/5 PKCS#7: Allow detached data to be supplied for signature checking purposes

In reply to David Howells

It is possible for a PKCS#7 message to have detached data. However, to verify the signatures on a PKCS#7 message, we have to be able to digest the data. Provide a function to supply that data. An error is given if the PKCS#7 message included embedded data.

---

 crypto/asymmetric_keys/pkcs7_verify.c |   26 ++++++++++++++++++++++++++
 include/crypto/pkcs7.h                |    3 +++
 2 files changed, 29 insertions(+)
crypto/asymmetric_keys/pkcs7_verify.c +26 -0
index 667064daf66b..c6ee41bfda6e 100644
@@ -382,3 +382,29 @@ int pkcs7_verify(struct pkcs7_message *pkcs7)
 	return enopkg;
 }
 EXPORT_SYMBOL_GPL(pkcs7_verify);
+
+/**
+ * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
+ * @pkcs7: The PKCS#7 message
+ * @data: The data to be verified
+ * @datalen: The amount of data
+ *
+ * Supply the detached data needed to verify a PKCS#7 message.  Note that no
+ * attempt to retain/pin the data is made.  That is left to the caller.  The
+ * data will not be modified by pkcs7_verify() and will not be freed when the
+ * PKCS#7 message is freed.
+ *
+ * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
+ */
+int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
+			       const void *data, size_t datalen)
+{
+	if (pkcs7->data) {
+		pr_debug("Data already supplied\n");
+		return -EINVAL;
+	}
+	pkcs7->data = data;
+	pkcs7->data_len = datalen;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pkcs7_supply_detached_data);
include/crypto/pkcs7.h +3 -0
index 691c79172a26..e235ab4957ee 100644
@@ -34,3 +34,6 @@ extern int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
  * pkcs7_verify.c
  */
 extern int pkcs7_verify(struct pkcs7_message *pkcs7);
+
+extern int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
+				      const void *data, size_t datalen);
#16 lore raw

Re: [PATCH 3/5] PKCS#7: Allow detached data to be supplied for signature checking purposes

Quoted 6 lines · David Howells

It is possible for a PKCS#7 message to have detached data. However, to verify the signatures on a PKCS#7 message, we have to be able to digest the data. Provide a function to supply that data. An error is given if the PKCS#7 message included embedded data.

Ok, PCKS#7 supports detached data. I assume this is not needed for kernel modules. What is the motivation for adding this support to the kernel?

Mimi

Quoted 57 lines

---

 crypto/asymmetric_keys/pkcs7_verify.c |   26 ++++++++++++++++++++++++++
 include/crypto/pkcs7.h                |    3 +++
 2 files changed, 29 insertions(+)
crypto/asymmetric_keys/pkcs7_verify.c +26 -0
index 667064daf66b..c6ee41bfda6e 100644
@@ -382,3 +382,29 @@ int pkcs7_verify(struct pkcs7_message *pkcs7)
 	return enopkg;
 }
 EXPORT_SYMBOL_GPL(pkcs7_verify);
+
+/**
+ * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
+ * @pkcs7: The PKCS#7 message
+ * @data: The data to be verified
+ * @datalen: The amount of data
+ *
+ * Supply the detached data needed to verify a PKCS#7 message.  Note that no
+ * attempt to retain/pin the data is made.  That is left to the caller.  The
+ * data will not be modified by pkcs7_verify() and will not be freed when the
+ * PKCS#7 message is freed.
+ *
+ * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
+ */
+int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
+			       const void *data, size_t datalen)
+{
+	if (pkcs7->data) {
+		pr_debug("Data already supplied\n");
+		return -EINVAL;
+	}
+	pkcs7->data = data;
+	pkcs7->data_len = datalen;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pkcs7_supply_detached_data);
include/crypto/pkcs7.h +3 -1
index 691c79172a26..e235ab4957ee 100644
@@ -34,3 +34,6 @@ extern int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
  * pkcs7_verify.c
  */
 extern int pkcs7_verify(struct pkcs7_message *pkcs7);
+
+extern int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
+				      const void *data, size_t datalen);
 
--

To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#17 lore raw

Re: [PATCH 3/5] PKCS#7: Allow detached data to be supplied for signature checking purposes

Quoted 3 lines · Mimi Zohar

Ok, PCKS#7 supports detached data. I assume this is not needed for kernel modules. What is the motivation for adding this support to the kernel?

See patch #5. I should probably note that in the commit message.

David

#18 lore raw

Re: [PATCH 3/5] PKCS#7: Allow detached data to be supplied for signature checking purposes

Quoted 7 lines · David Howells
Quoted 3 lines · Mimi Zohar

Ok, PCKS#7 supports detached data. I assume this is not needed for kernel modules. What is the motivation for adding this support to the kernel?

See patch #5. I should probably note that in the commit message.

This patch set does not change the syscall. The signature is still appended to the kernel module. In fact, the call from kernel/module_signing.c: mod_verify_pkcs7() calls pkcs7_supply_detached_data() with a pointer to the module and the module length to set the data field. pkcs7_supply_detached_data() would not be defined or exported, unless it was going to be called elsewhere. How else are you planning on using pkcs7_supply_detached_data()?

Mimi

author maintainer
#19 lore raw

Re: [PATCH 3/5] PKCS#7: Allow detached data to be supplied for signature checking purposes

Quoted 8 lines · Mimi Zohar
Quoted 5 lines
Quoted 3 lines

Ok, PCKS#7 supports detached data. I assume this is not needed for kernel modules. What is the motivation for adding this support to the kernel?

See patch #5. I should probably note that in the commit message.

This patch set does not change the syscall. The signature is still appended to the kernel module.

Ummm, yes, so?

Quoted 5 lines

In fact, the call from kernel/module_signing.c: mod_verify_pkcs7() calls pkcs7_supply_detached_data() with a pointer to the module and the module length to set the data field. pkcs7_supply_detached_data() would not be defined or exported, unless it was going to be called elsewhere. How else are you planning on using pkcs7_supply_detached_data()?

Is your point that I exported it unnecessarily? That I have now fixed.

David

#20 lore raw

Re: [PATCH 3/5] PKCS#7: Allow detached data to be supplied for signature checking purposes

Quoted 13 lines · David Howells
Quoted 8 lines · Mimi Zohar
Quoted 5 lines
Quoted 3 lines

Ok, PCKS#7 supports detached data. I assume this is not needed for kernel modules. What is the motivation for adding this support to the kernel?

See patch #5. I should probably note that in the commit message.

This patch set does not change the syscall. The signature is still appended to the kernel module.

Ummm, yes, so?

Quoted 7 lines
Quoted 5 lines

In fact, the call from kernel/module_signing.c: mod_verify_pkcs7() calls pkcs7_supply_detached_data() with a pointer to the module and the module length to set the data field. pkcs7_supply_detached_data() would not be defined or exported, unless it was going to be called elsewhere. How else are you planning on using pkcs7_supply_detached_data()?

Is your point that I exported it unnecessarily? That I have now fixed.

No, it was more a comment on the function name pkcs7_supply_detached_data() being a bit weird, as the signature is appended to the data.

Mimi

author maintainer
#21 lore raw

Re: [PATCH 3/5] PKCS#7: Allow detached data to be supplied for signature checking purposes

Quoted 5 lines · Mimi Zohar

No, it was more a comment on the function name pkcs7_supply_detached_data() being a bit weird, as the signature is appended to the data.

From the point of view of the PKCS#7 message it's detached.

David

author maintainer
#22 lore raw

PATCH 4/5 MODSIGN: Provide a utility to append a PKCS#7 signature to a module

In reply to David Howells

Provide a utility that:

(1) Digests a module using the specified hash algorithm (typically sha256).

[The digest can be dumped into a file by passing the '-d' flag]

(2) Generates a PKCS#7 message that:

(a) Has detached data (ie. the module content).

(b) Is signed with the specified private key.

(c) Refers to the specified X.509 certificate.

(d) Has an empty X.509 certificate list.

[The PKCS#7 message can be dumped into a file by passing the '-p' flag]

(3) Generates a signed module by concatenating the old module, the PKCS#7 message, a descriptor and a magic string. The descriptor contains the size of the PKCS#7 message and indicates the id_type as PKEY_ID_PKCS7.

(4) Either writes the signed module to the specified destination or renames it over the source module.

This allows module signing to reuse the PKCS#7 handling code that was added for PE file parsing for signed kexec.

Note that the utility is written in C and must be linked against the OpenSSL crypto library.

Note further that I have temporarily dropped support for handling externally created signatures until we can work out the best way to do those. Hopefully, whoever creates the signature can give me a PKCS#7 certificate.

---

 include/crypto/public_key.h |    1 
 scripts/sign-file.c         |  189 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)
 create mode 100755 scripts/sign-file.c
include/crypto/public_key.h +1 -0
index b6f27a240856..fda097e079a4 100644
@@ -33,6 +33,7 @@ extern const struct public_key_algorithm *pkey_algo[PKEY_ALGO__LAST];
 enum pkey_id_type {
 	PKEY_ID_PGP,		/* OpenPGP generated key ID */
 	PKEY_ID_X509,		/* X.509 arbitrary subjectKeyIdentifier */
+	PKEY_ID_PKCS7,		/* Signature in PKCS#7 message */
 	PKEY_ID_TYPE__LAST
 };
 
scripts/sign-file.c new file +189 -0

189 lines not on this page · raw message

author maintainer
#23 lore raw

PATCH 5/5 MODSIGN: Use PKCS#7 messages as module signatures

In reply to David Howells

Move to using PKCS#7 messages as module signatures because:

(1) We have to be able to support the use of X.509 certificates that don't have a subjKeyId set. We're currently relying on this to look up the X.509 certificate in the trusted keyring list.

(2) PKCS#7 message signed information blocks have a field that supplies the data required to match with the X.509 certificate that signed it.

(3) The PKCS#7 certificate carries fields that specify the digest algorithm used to generate the signature in a standardised way and the X.509 certificates specify the public key algorithm in a standardised way - so we don't need our own methods of specifying these.

(4) We now have PKCS#7 message support in the kernel for signed kexec purposes and we can make use of this.

To make this work, the old sign-file script has been replaced with a program that needs compiling in a previous patch. The rules to build it are added here.

---

 init/Kconfig            |    1 
 kernel/module_signing.c |  220 +++++--------------------
 scripts/Makefile        |    2 
 scripts/sign-file       |  421 -----------------------------------------------
 4 files changed, 47 insertions(+), 597 deletions(-)
 delete mode 100755 scripts/sign-file
init/Kconfig +1 -0
index 80a6907f91c5..e6f418b97bdd 100644
@@ -1840,6 +1840,7 @@ config MODULE_SIG
 	select ASN1
 	select OID_REGISTRY
 	select X509_CERTIFICATE_PARSER
+	select PKCS7_MESSAGE_PARSER
 	help
 	  Check modules for valid signatures upon load: the signature
 	  is simply appended to the module. For more information see
kernel/module_signing.c +44 -176

269 lines not on this page · raw message

scripts/Makefile +2 -0
index 72902b5f2721..719311b7bd46 100644
@@ -16,9 +16,11 @@ hostprogs-$(CONFIG_VT)           += conmakehash
 hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount
 hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable
 hostprogs-$(CONFIG_ASN1)	 += asn1_compiler
+hostprogs-$(CONFIG_MODULE_SIG)	 += sign-file
 
 HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include
 HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
+HOSTLOADLIBES_sign-file = -lcrypto
 
 always		:= $(hostprogs-y) $(hostprogs-m)
 
scripts/sign-file deleted +0 -421

421 lines not on this page · raw message

#24 lore raw

Re: [PATCH 5/5] MODSIGN: Use PKCS#7 messages as module signatures

Quoted 26 lines · David Howells

/*

kernel/module_signing.c +0 -10
@@ -186,12 +81,8 @@ static struct key *request_asymmetric_key(const char *signer, size_t signer_len,
  */
 int mod_verify_sig(const void *mod, unsigned long *_modlen)
 {
-	struct public_key_signature *pks;
 	struct module_signature ms;
-	struct key *key;
-	const void *sig;
 	size_t modlen = *_modlen, sig_len;
-	int ret;
 
 	pr_devel("==>%s(,%zu)\n", __func__, modlen);
 
@@ -205,46 +96,23 @@ int mod_verify_sig(const void *mod, unsigned long *_modlen)
 	if (sig_len >= modlen)
 		return -EBADMSG;
 	modlen -= sig_len;
-	if ((size_t)ms.signer_len + ms.key_id_len >= modlen)
-		return -EBADMSG;
-	modlen -= (size_t)ms.signer_len + ms.key_id_len;
-
 	*_modlen = modlen;
-	sig = mod + modlen;
-

kernel/module_signing.c int mod_verify_sig(const void *mod, unsigned long *_modlen) · line 103

Quoted 10 lines
kernel/module_signing.c +2 -6
-	/* For the moment, only support RSA and X.509 identifiers */
-	if (ms.algo != PKEY_ALGO_RSA ||
-	    ms.id_type != PKEY_ID_X509)
-		return -ENOPKG;
 
-	if (ms.hash >= PKEY_HASH__LAST ||
-	    !hash_algo_name[ms.hash])
+	if (ms.id_type != PKEY_ID_PKCS7) {
+		pr_err("Module is not signed with expected PKCS#7 message\n");
 		return -ENOPKG;

Perhaps because modules are resigned with each kernel build, it is acceptable to totally replace one signature format with another like this, and fail the old method.

Mimi

#25 lore raw
In reply to David Howells

Hi David,

Before I go into reviewing the patches just want to let you know that Integrity stuff seems to work fine with these changes.

Thanks,
Dmitry

Quoted 69 lines · David Howells

Here's a set of patches that does the following:

(1) Extracts both parts of an X.509 AuthorityKeyIdentifier (AKID) extension. We already extract the bit that can match the subjectKeyIdentifier (SKID) of the parent X.509 cert, but we currently ignore the bits that can match the issuer and serialNumber.

Looks up an X.509 cert by issuer and serialNumber if those are provided in the AKID. If the keyIdentifier is also provided, checks that the subjectKeyIdentifier of the cert found matches that also.

If no issuer and serialNumber are provided in the AKID, looks up an X.509 cert by SKID using the AKID keyIdentifier.

This allows module signing to be done with certificates that don't have an SKID by which they can be looked up.

(2) Makes use of the PKCS#7 facility to provide module signatures.

sign-file is replaced with a program that generates a PKCS#7 message that has no X.509 certs embedded and that has detached data (the module content) and adds it onto the message with magic string and descriptor.

(3) The PKCS#7 message (and matching X.509 cert) supply all the information that is needed to select the X.509 cert to be used to verify the signature by standard means (including selection of digest algorithm and public key algorithm). No kernel-specific magic values are required.

Note that the revised sign-file program no longer supports the "-s <signature>" option as I'm not sure what the best way to deal with this is. Do we generate a PKCS#7 cert from the signature given, or do we get given a PKCS#7 cert? I lean towards the latter.

They can be found here also:

http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=modsign-pkcs7

These patches are based on the security tree's next branch.

David
---
David Howells (5):
X.509: Extract both parts of the AuthorityKeyIdentifier X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier PKCS#7: Allow detached data to be supplied for signature checking purposes
MODSIGN: Provide a utility to append a PKCS#7 signature to a module
MODSIGN: Use PKCS#7 messages as module signatures

 crypto/asymmetric_keys/Makefile           |    8 -
 crypto/asymmetric_keys/pkcs7_trust.c      |   10 -
 crypto/asymmetric_keys/pkcs7_verify.c     |   81 ++++--
 crypto/asymmetric_keys/x509_akid.asn1     |   35 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  142 ++++++----
 crypto/asymmetric_keys/x509_parser.h      |    3 
 crypto/asymmetric_keys/x509_public_key.c  |   85 ++++--
 include/crypto/pkcs7.h                    |    3 
 include/crypto/public_key.h               |    4 
 init/Kconfig                              |    1 
 kernel/module_signing.c                   |  220 +++------------
 scripts/Makefile                          |    2 
 scripts/sign-file                         |  421 -----------------------------
 scripts/sign-file.c                       |  189 +++++++++++++
 14 files changed, 505 insertions(+), 699 deletions(-)
 create mode 100644 crypto/asymmetric_keys/x509_akid.asn1
 delete mode 100755 scripts/sign-file
 create mode 100755 scripts/sign-file.c
#26 lore raw
Quoted 4 lines · Dmitry Kasatkin

Hi David,

Before I go into reviewing the patches just want to let you know that Integrity stuff seems to work fine with these changes.

Actually after cleaning the tree and re-signing the modules, I get following

Unrecognized character \x7F; marked by <-- HERE after <-- HERE near column 1 at ./scripts/sign-file line 1.
make[1]: *** [arch/x86/crypto/aes-x86_64.ko] Error 255

- Dmitry

Quoted 78 lines

Thanks,
Dmitry

Quoted 59 lines · David Howells

Here's a set of patches that does the following:

(1) Extracts both parts of an X.509 AuthorityKeyIdentifier (AKID) extension. We already extract the bit that can match the subjectKeyIdentifier (SKID) of the parent X.509 cert, but we currently ignore the bits that can match the issuer and serialNumber.

Looks up an X.509 cert by issuer and serialNumber if those are provided in the AKID. If the keyIdentifier is also provided, checks that the subjectKeyIdentifier of the cert found matches that also.

If no issuer and serialNumber are provided in the AKID, looks up an X.509 cert by SKID using the AKID keyIdentifier.

This allows module signing to be done with certificates that don't have an SKID by which they can be looked up.

(2) Makes use of the PKCS#7 facility to provide module signatures.

sign-file is replaced with a program that generates a PKCS#7 message that has no X.509 certs embedded and that has detached data (the module content) and adds it onto the message with magic string and descriptor.

(3) The PKCS#7 message (and matching X.509 cert) supply all the information that is needed to select the X.509 cert to be used to verify the signature by standard means (including selection of digest algorithm and public key algorithm). No kernel-specific magic values are required.

Note that the revised sign-file program no longer supports the "-s <signature>" option as I'm not sure what the best way to deal with this is. Do we generate a PKCS#7 cert from the signature given, or do we get given a PKCS#7 cert? I lean towards the latter.

They can be found here also:

http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=modsign-pkcs7

These patches are based on the security tree's next branch.

David
---
David Howells (5):
X.509: Extract both parts of the AuthorityKeyIdentifier X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier PKCS#7: Allow detached data to be supplied for signature checking purposes
MODSIGN: Provide a utility to append a PKCS#7 signature to a module
MODSIGN: Use PKCS#7 messages as module signatures

 crypto/asymmetric_keys/Makefile           |    8 -
 crypto/asymmetric_keys/pkcs7_trust.c      |   10 -
 crypto/asymmetric_keys/pkcs7_verify.c     |   81 ++++--
 crypto/asymmetric_keys/x509_akid.asn1     |   35 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  142 ++++++----
 crypto/asymmetric_keys/x509_parser.h      |    3 
 crypto/asymmetric_keys/x509_public_key.c  |   85 ++++--
 include/crypto/pkcs7.h                    |    3 
 include/crypto/public_key.h               |    4 
 init/Kconfig                              |    1 
Quoted 10 lines
 kernel/module_signing.c                   |  220 +++------------
 scripts/Makefile                          |    2 
 scripts/sign-file                         |  421 -----------------------------
 scripts/sign-file.c                       |  189 +++++++++++++
 14 files changed, 505 insertions(+), 699 deletions(-)
 create mode 100644 crypto/asymmetric_keys/x509_akid.asn1
 delete mode 100755 scripts/sign-file
 create mode 100755 scripts/sign-file.c

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#27 lore raw
Quoted 5 lines · Dmitry Kasatkin

Actually after cleaning the tree and re-signing the modules, I get following

Unrecognized character \x7F; marked by <-- HERE after <-- HERE near column 1 at ./scripts/sign-file line 1.
make[1]: *** [arch/x86/crypto/aes-x86_64.ko] Error 255

warthog>grep -r sign-file Makefile
mod_sign_cmd = perl $(srctree)/scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY)

Because of that. I need to remove the 'perl' bit.

David

author maintainer
#28 lore raw
Quoted 10 lines · David Howells
Quoted 5 lines

Actually after cleaning the tree and re-signing the modules, I get following

Unrecognized character \x7F; marked by <-- HERE after <-- HERE near column 1 at ./scripts/sign-file line 1.
make[1]: *** [arch/x86/crypto/aes-x86_64.ko] Error 255

warthog>grep -r sign-file Makefile
mod_sign_cmd = perl $(srctree)/scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY)

Because of that. I need to remove the 'perl' bit.

It's a little more involved than that. The X.509 cert being passed to the program is binary, whereas the one I've been testing with is PEM encoded - and libssl has separate routines that don't work out for themselves which encoding is in force. Proposed changes below.

David
---

Makefile +1 -1
index b77de27e58fc..8d5624bf96db 100644
@@ -859,7 +859,7 @@ ifdef CONFIG_MODULE_SIG_ALL
 MODSECKEY = ./signing_key.priv
 MODPUBKEY = ./signing_key.x509
 export MODPUBKEY
-mod_sign_cmd = perl $(srctree)/scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY)
+mod_sign_cmd = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY)
 else
 mod_sign_cmd = true
 endif
scripts/sign-file.c +25 -8
index 3f9bedbd185f..ff5e78348de0 100755
@@ -61,14 +61,24 @@ static void display_openssl_errors(int l)
 	}
 }
 
+static void drain_openssl_errors(void)
+{
+	const char *file;
+	int line;
+
+	if (ERR_peek_error() == 0)
+		return;
+	while (ERR_get_error_line(&file, &line)) {}
+}
 
-#define ERR(cond, ...)				  \
-	do {					  \
-		bool __cond = (cond);		  \
-		display_openssl_errors(__LINE__); \
-		if (__cond) {			  \
-			err(1, ## __VA_ARGS__);	  \
-		}				  \
+
+#define ERR(cond, ...)					\
+	do {						\
+		bool __cond = (cond);			\
+		display_openssl_errors(__LINE__);	\
+		if (__cond) {				\
+			err(1, ## __VA_ARGS__);		\
+		}					\
 	} while(0)
 
 int main(int argc, char **argv)
@@ -126,8 +136,15 @@ int main(int argc, char **argv)
 
 	b = BIO_new_file(x509_name, "rb");
 	ERR(!b, "%s", x509_name);
-        x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
+	x509 = d2i_X509_bio(b, NULL); /* Binary encoded X.509 */
+	if (!x509) {
+		BIO_reset(b);
+		x509 = PEM_read_bio_X509(b, NULL, NULL, NULL); /* PEM encoded X.509 */
+		if (x509)
+			drain_openssl_errors();
+	}
 	BIO_free(b);
+	ERR(!x509, "%s", x509_name);
 
 	/* Open the destination file now so that we can shovel the module data
 	 * across as we read it.
#29 lore raw
Quoted 11 lines · David Howells
Quoted 9 lines · David Howells
Quoted 5 lines

Actually after cleaning the tree and re-signing the modules, I get following

Unrecognized character \x7F; marked by <-- HERE after <-- HERE near column 1 at ./scripts/sign-file line 1.
make[1]: *** [arch/x86/crypto/aes-x86_64.ko] Error 255

warthog>grep -r sign-file Makefile
mod_sign_cmd = perl $(srctree)/scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY)

Quoted 6 lines
Quoted 1 line

Because of that. I need to remove the 'perl' bit.

It's a little more involved than that. The X.509 cert being passed to the program is binary, whereas the one I've been testing with is PEM encoded - and libssl has separate routines that don't work out for themselves which encoding is in force. Proposed changes below.

With this patch, I'm now able to install and boot the new kernel and modules.

Mimi

#30 lore raw
In reply to David Howells
Quoted 14 lines · David Howells

Here's a set of patches that does the following:

(1) Extracts both parts of an X.509 AuthorityKeyIdentifier (AKID) extension. We already extract the bit that can match the subjectKeyIdentifier (SKID) of the parent X.509 cert, but we currently ignore the bits that can match the issuer and serialNumber.

Looks up an X.509 cert by issuer and serialNumber if those are provided in the AKID. If the keyIdentifier is also provided, checks that the subjectKeyIdentifier of the cert found matches that also.

If no issuer and serialNumber are provided in the AKID, looks up an X.509 cert by SKID using the AKID keyIdentifier.

Quoted 8 lines

This allows module signing to be done with certificates that don't have an SKID by which they can be looked up.

(2) Makes use of the PKCS#7 facility to provide module signatures.

sign-file is replaced with a program that generates a PKCS#7 message that has no X.509 certs embedded and that has detached data (the module content) and adds it onto the message with magic string and descriptor.

Why do you highlight that X509 is not embedded?
Current module signing does not embed X509 also.

- Dmitry

Quoted 50 lines

(3) The PKCS#7 message (and matching X.509 cert) supply all the information that is needed to select the X.509 cert to be used to verify the signature by standard means (including selection of digest algorithm and public key algorithm). No kernel-specific magic values are required.

Note that the revised sign-file program no longer supports the "-s <signature>" option as I'm not sure what the best way to deal with this is. Do we generate a PKCS#7 cert from the signature given, or do we get given a PKCS#7 cert? I lean towards the latter.

They can be found here also:

http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=modsign-pkcs7

These patches are based on the security tree's next branch.

David
---
David Howells (5):
X.509: Extract both parts of the AuthorityKeyIdentifier X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier PKCS#7: Allow detached data to be supplied for signature checking purposes
MODSIGN: Provide a utility to append a PKCS#7 signature to a module
MODSIGN: Use PKCS#7 messages as module signatures

 crypto/asymmetric_keys/Makefile           |    8 -
 crypto/asymmetric_keys/pkcs7_trust.c      |   10 -
 crypto/asymmetric_keys/pkcs7_verify.c     |   81 ++++--
 crypto/asymmetric_keys/x509_akid.asn1     |   35 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  142 ++++++----
 crypto/asymmetric_keys/x509_parser.h      |    3 
 crypto/asymmetric_keys/x509_public_key.c  |   85 ++++--
 include/crypto/pkcs7.h                    |    3 
 include/crypto/public_key.h               |    4 
 init/Kconfig                              |    1 
 kernel/module_signing.c                   |  220 +++------------
 scripts/Makefile                          |    2 
 scripts/sign-file                         |  421 -----------------------------
 scripts/sign-file.c                       |  189 +++++++++++++
 14 files changed, 505 insertions(+), 699 deletions(-)
 create mode 100644 crypto/asymmetric_keys/x509_akid.asn1
 delete mode 100755 scripts/sign-file
 create mode 100755 scripts/sign-file.c

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#31 lore raw
Quoted 9 lines · Dmitry Kasatkin
Quoted 6 lines

(2) Makes use of the PKCS#7 facility to provide module signatures.

sign-file is replaced with a program that generates a PKCS#7 message that has no X.509 certs embedded and that has detached data (the module content) and adds it onto the message with magic string and descriptor.

Why do you highlight that X509 is not embedded?
Current module signing does not embed X509 also.

A PKCS#7 message can have X.509 certs embedded within it - but it's optional within the spec. Given that we expect to have the appropriate cert available to verify the signature on the PKCS#7 message directly, there's no need to actually embed the X.509 cert therein.

Unfortunately, it doesn't appear that you can do this with the openssl command line utility - hence why I moved to C.

David