MODSIGN: Use PKCS#7 for module signatures [ver #3]
Subsystems (4)
David Howells
maintainercover MODSIGN: Use PKCS#7 for module signatures [ver #3]
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
and are tagged with:
modsign-pkcs7-20150206
These patches are based on the security tree's next branch.
Changes:
(*) Fixed a comment on x509_certificate::id to show the order of construction
correctly [thanks to Vivek Goyal].
(*) The 'id' argument to x509_request_asymmetric_key() can be NULL so needs to
be checked [thanks to Mimi Zohar].
(*) pkcs7_supply_detached_data() doesn't need exporting [thanks to Mimi
Zohar].
(*) Fixed "make install_modules" to not try to run sign-file under perl
[thanks to Dmitry Kasatkin].
(*) Fixed sign-file to handle binary X.509 certs as well as PEM-encoded X.509
certs.
(*) Changed the x509_certificate struct members 'auth_id' and 'auth_skid' to
be 'akid_id' and akid_skid' as they come from the AKID if it is present.
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
Makefile | 2 crypto/asymmetric_keys/Makefile | 8 - crypto/asymmetric_keys/pkcs7_trust.c | 10 - crypto/asymmetric_keys/pkcs7_verify.c | 80 ++++-- crypto/asymmetric_keys/x509_akid.asn1 | 35 ++ crypto/asymmetric_keys/x509_cert_parser.c | 142 ++++++---- crypto/asymmetric_keys/x509_parser.h | 5 crypto/asymmetric_keys/x509_public_key.c | 86 ++++-- 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 | 205 ++++++++++++++ 15 files changed, 523 insertions(+), 701 deletions(-) create mode 100644 crypto/asymmetric_keys/x509_akid.asn1 delete mode 100755 scripts/sign-file create mode 100755 scripts/sign-file.c
David Howells
maintainerPATCH 1/5 X.509: Extract both parts of the AuthorityKeyIdentifier [ver #3]
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 | 5 + crypto/asymmetric_keys/x509_public_key.c | 8 +- 7 files changed, 145 insertions(+), 69 deletions(-) create mode 100644 crypto/asymmetric_keys/x509_akid.asn1
crypto/asymmetric_keys/Makefile +7 -1
@@ -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
@@ -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->akid_skid) { + key =x509_request_asymmetric_key (trust_keyring, last->akid_skid, false);if (!IS_ERR (key)) { x509 = last;
crypto/asymmetric_keys/pkcs7_verify.c +5 -7
@@ -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->akid_skid)pr_debug ("- authkeyid %*phN\n" , - x509->authority->len, x509->authority->data); + x509->akid_skid->len, x509->akid_skid->data); -if (!x509->authority || +if (!x509->akid_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->akid_skid->len, x509->akid_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->akid_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
@@ -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 +3 -2
@@ -19,9 +19,10 @@ struct x509_certificate {struct public_key_signature sig;/* Signature parameters */ char *issuer;/* Name of certificate issuer */ char *subject;/* Name of certificate subject */ -struct asymmetric_key_id *id;/* Serial number + issuer */ +struct asymmetric_key_id *id;/* Issuer + Serial number */ struct asymmetric_key_id *skid;/* Subject + subjectKeyId (optional) */ -struct asymmetric_key_id *authority;/* Authority key identifier (optional) */ +struct asymmetric_key_id *akid_id;/* CA AuthKeyId matching ->id (optional) */ +struct asymmetric_key_id *akid_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
@@ -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->akid_skid, ca_keyid))return -EPERM ; - key =x509_request_asymmetric_key (trust_keyring, cert->authority, + key =x509_request_asymmetric_key (trust_keyring, cert->akid_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->akid_skid || +asymmetric_key_id_same (cert->skid, cert->akid_skid)) { ret =x509_check_signature (cert->pub, cert);/* self-signed */ if (ret <0 ) goto error_free_cert;
David Howells
maintainerPATCH 2/5 X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier [ver #3]
In reply to David HowellsIf 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 | 84 +++++++++++++++++++++--------- include/crypto/public_key.h | 3 + 4 files changed, 103 insertions(+), 41 deletions(-)
crypto/asymmetric_keys/pkcs7_trust.c +7 -3
@@ -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->akid_skid) { - key =x509_request_asymmetric_key (trust_keyring, last->akid_skid, +if (last && (last->akid_id || last->akid_skid)) { + key =x509_request_asymmetric_key (trust_keyring, + last->akid_id, + last->akid_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 +58 -26
128 lines not on this page · raw message
include/crypto/public_key.h +2 -1
@@ -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 */
David Howells
maintainerPATCH 3/5 PKCS#7: Allow detached data to be supplied for signature checking purposes [ver #3]
In reply to David HowellsIt 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.
This is used in a subsequent patch to supply the data to module signing where the signature is in the form of a PKCS#7 message with detached data, whereby the detached data is the module content that is signed.
---
crypto/asymmetric_keys/pkcs7_verify.c | 25 +++++++++++++++++++++++++ include/crypto/pkcs7.h | 3 +++ 2 files changed, 28 insertions(+)
crypto/asymmetric_keys/pkcs7_verify.c +25 -0
@@ -382,3 +382,28 @@ 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 ; +}
include/crypto/pkcs7.h +3 -0
@@ -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);
David Howells
maintainerPATCH 4/5 MODSIGN: Provide a utility to append a PKCS#7 signature to a module [ver #3]
In reply to David HowellsProvide 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 | 205 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100755 scripts/sign-file.c
include/crypto/public_key.h +1 -0
@@ -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 +205 -0
205 lines not on this page · raw message
Re: [PATCH 4/5] MODSIGN: Provide a utility to append a PKCS#7 signature to a module [ver #3]
scripts/sign-file.c line 126
Quoted 11 lines · David Howells
scripts/sign-file.c new file +11
-0+if (argc ==5 ) { + dest_name = argv[4 ]; + replace_orig = false; + }else { +ERR (asprintf (&dest_name,"%s.~signed~" , module_name) <0 , +"asprintf" ); + replace_orig = true; + } + +ERR_load_crypto_strings (); +ERR_clear_error ();
The error queue initialization should be done before the first use of the ERR() macro. Or the asprintf check should be open-coded, because it has nothing to do with openssl.
Michal
David Howells
maintainerRe: [PATCH 4/5] MODSIGN: Provide a utility to append a PKCS#7 signature to a module [ver #3]
Quoted 11 lines · Michal Marek
scripts/sign-file.c line 126
Quoted 7 lines
scripts/sign-file.c new file +7
-0+ERR (asprintf (&dest_name,"%s.~signed~" , module_name) <0 , +"asprintf" ); + replace_orig = true; + } + +ERR_load_crypto_strings (); +ERR_clear_error ();The error queue initialization should be done before the first use of the
ERR()macro. Or the asprintf check should be open-coded, because it has nothing to do with openssl.
Good point.
David
David Howells
maintainerPATCH 5/5 MODSIGN: Use PKCS#7 messages as module signatures [ver #3]
In reply to David HowellsMove 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.
---
Makefile | 2 init/Kconfig | 1 kernel/module_signing.c | 220 +++++-------------------- scripts/Makefile | 2 scripts/sign-file | 421 ----------------------------------------------- 5 files changed, 48 insertions(+), 598 deletions(-) delete mode 100755 scripts/sign-file
Makefile +1 -1
@@ -871,7 +871,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
init/Kconfig +1 -0
@@ -1859,6 +1859,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
@@ -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
Quoted 4 lines · David Howells
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.
It would be convenient to have it, since pesign also has --import-raw-signature. But it can be added back later.
Michal