MODSIGN: Use PKCS#7 for module signatures
Subsystems (11)
David Howells
maintainercover MODSIGN: Use PKCS#7 for module signatures
Here's a set of patches that does the following:
(1) Improves asymmetric keys identification.
Keys derived from X.509 certs now get labelled with IDs derived from their issuer and certificate number (required to match PKCS#7) and from their SKID and subject (required to match X.509).
IDs are now binary and match criterion preparsing is provided so that criteria can be turned into binary blobs to make matching faster.
(2) Improves PKCS#7 message handling to permit PKCS#7 messages without X.509 cert lists to be matched to trusted keys, thereby allowing minimally sized PKCS#7 certs to be used.
(3) Improves PKCS#7 message handling to better handle certificate chains that are broken due to unsupported crypto that can otherwise by used to intersect a trust keyring.
(4) 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.
(5) 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.
The following need to be considered also:
(1) How to support externally generated signatures (sign-file -s). Ideally, externally generated signatures would be provided as PKCS#7 certificates.
(2) How to handle the old signature format: do we change the magic number and just pretend they don't exist (which would allow us to get rid of most of the descriptor), do we give an error (which I've chosen to do) or do we have to support them still?
(3) Do I really need to make one of the X.509-derived IDs out of the subjKeyId and the subject, or can I just use the subjKeyId by itself? (And likewise for auth + issuer)
They can be found here also:
http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=modsign-pkcs7
David
---
David Howells (13):
Provide a binary to hex conversion utility
KEYS: Preparse match data
KEYS: Remove key_type::def_lookup_type
KEYS: Remove key_type::match in favour of overriding default by match_preparse
KEYS: Make the key matching functions return bool
KEYS: Implement binary asymmetric key ID handling
PKCS#7: Clean up the signed info freeing and fix the parser cleanup
KEYS: Overhaul key identification when searching for asymmetric keys PKCS#7: Better handling of unsupported crypto
PKCS#7: Handle PKCS#7 messages that contain no X.509 certs 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/asymmetric_keys.h | 8 - crypto/asymmetric_keys/asymmetric_type.c | 213 ++++++++++----- crypto/asymmetric_keys/pkcs7_key_type.c | 2 crypto/asymmetric_keys/pkcs7_parser.c | 66 +++-- crypto/asymmetric_keys/pkcs7_parser.h | 7 crypto/asymmetric_keys/pkcs7_trust.c | 72 +++-- crypto/asymmetric_keys/pkcs7_verify.c | 131 +++++++-- crypto/asymmetric_keys/x509_cert_parser.c | 55 ++-- crypto/asymmetric_keys/x509_parser.h | 6 crypto/asymmetric_keys/x509_public_key.c | 102 ++++--- fs/cifs/cifs_spnego.c | 1 fs/cifs/cifsacl.c | 1 fs/nfs/idmap.c | 2 include/crypto/pkcs7.h | 3 include/crypto/public_key.h | 6 include/keys/asymmetric-type.h | 38 +++ include/keys/user-type.h | 1 include/linux/kernel.h | 1 include/linux/key-type.h | 34 ++ init/Kconfig | 1 kernel/module_signing.c | 220 +++------------ lib/hexdump.c | 18 + net/dns_resolver/dns_key.c | 18 + net/rxrpc/ar-key.c | 2 scripts/Makefile | 2 scripts/sign-file | 421 ----------------------------- scripts/sign-file.c | 189 +++++++++++++ security/keys/big_key.c | 2 security/keys/encrypted-keys/encrypted.c | 1 security/keys/internal.h | 10 - security/keys/key.c | 2 security/keys/keyring.c | 59 +++- security/keys/proc.c | 8 - security/keys/process_keys.c | 13 - security/keys/request_key.c | 21 + security/keys/request_key_auth.c | 6 security/keys/trusted.c | 1 security/keys/user_defined.c | 14 - 38 files changed, 869 insertions(+), 888 deletions(-) delete mode 100755 scripts/sign-file create mode 100755 scripts/sign-file.c
David Howells
maintainerPATCH 1/13 Provide a binary to hex conversion utility
Provide a utility to convert a buffer of binary data into an unterminated ascii hex string representation of that data.
---
include/linux/kernel.h | 1 + lib/hexdump.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+)
include/linux/kernel.h +1 -0
@@ -496,6 +496,7 @@ static inline char *hex_byte_pack_upper(char *buf, u8 byte)extern int hex_to_bin (char ch);extern int __must_check hex2bin (u8 *dst,const char *src,size_t count); +extern char *bin2hex (char *dst,const void *src,size_t count);bool mac_pton (const char *s,u8 *mac);
lib/hexdump.c +18 -0
@@ -59,6 +59,24 @@ int hex2bin(u8 *dst, const char *src, size_t count)EXPORT_SYMBOL (hex2bin);/** +* bin2hex - convert binary data to an ascii hexadecimal string +* @dst: ascii hexadecimal result +* @src: binary data +* @count: binary data length +*/ +char *bin2hex (char *dst,const void *src,size_t count) +{ +while (count--) { +unsigned char ch = *(const unsigned char *)src; + *dst++ = hex_asc[ch >>4 ]; + *dst++ = hex_asc[ch &0xf ]; + src++; + } +return dst; +} +EXPORT_SYMBOL (bin2hex); + +/** * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory * @buf: data blob to dump * @len: number of bytes in the @buf
David Howells
maintainerPATCH 2/13 KEYS: Preparse match data
In reply to David HowellsPreparse the match data. This provides several advantages:
(1) The preparser can reject invalid criteria up front.
(2) The preparser can convert the criteria to binary data if necessary (the asymmetric key type really wants to do binary comparison of the key IDs).
(3) The preparser can set the type of search to be performed. This means that it's not then a one-off setting in the key type.
(4) The preparser can set an appropriate comparator function.
---
crypto/asymmetric_keys/asymmetric_type.c | 31 ++++++++++++++++++- include/keys/user-type.h | 4 ++ include/linux/key-type.h | 31 +++++++++++++++++-- net/dns_resolver/dns_key.c | 5 ++- security/keys/internal.h | 8 ++--- security/keys/keyring.c | 49 ++++++++++++++++++------------ security/keys/proc.c | 8 ++--- security/keys/process_keys.c | 13 ++++---- security/keys/request_key.c | 21 ++++++++++--- security/keys/request_key_auth.c | 6 ++-- security/keys/user_defined.c | 4 +- 11 files changed, 129 insertions(+), 51 deletions(-)
crypto/asymmetric_keys/asymmetric_type.c +30 -1
@@ -59,9 +59,11 @@ EXPORT_SYMBOL_GPL(asymmetric_keyid_match);* "id:<id>" - request a key matching the ID * "<subtype>:<id>" - request a key of a subtype */ -static int asymmetric_key_match (const struct key *key,const void *description) +static int asymmetric_key_match (const struct key *key, +const struct key_match_data *match_data) {const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); +const char *description = match_data->raw_data;const char *spec = description;const char *id;ptrdiff_t speclen;
@@ -94,6 +96,31 @@ static int asymmetric_key_match(const struct key *key, const void *description) }/* +* Preparse the match criterion. If we don't set lookup_type and cmp, +* the default will be an exact match on the key description. +* +* There are some specifiers for matching key IDs rather than by the key +* description: +* +* "id:<id>" - request a key by any available ID +* +* These have to be searched by iteration rather than by direct lookup because +* the key is hashed according to its description. +*/ +static int asymmetric_key_match_preparse (struct key_match_data *match_data) +{ + match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +return 0 ; +} + +/* +* Free the preparsed the match criterion. +*/ +static void asymmetric_key_match_free (struct key_match_data *match_data) +{ +} + +/* * Describe the asymmetric key */ static void asymmetric_key_describe (const struct key *key,struct seq_file *m)
@@ -196,7 +223,9 @@ struct key_type key_type_asymmetric = { .preparse = asymmetric_key_preparse, .free_preparse = asymmetric_key_free_preparse, .instantiate = generic_key_instantiate, + .match_preparse = asymmetric_key_match_preparse, .match = asymmetric_key_match, + .match_free = asymmetric_key_match_free, .destroy = asymmetric_key_destroy, .describe = asymmetric_key_describe, .def_lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE,
include/keys/user-type.h +3 -1
@@ -36,11 +36,13 @@ extern struct key_type key_type_user;extern struct key_type key_type_logon;struct key_preparsed_payload ; +struct key_match_data ;extern int user_preparse (struct key_preparsed_payload *prep);extern void user_free_preparse (struct key_preparsed_payload *prep);extern int user_update (struct key *key,struct key_preparsed_payload *prep); -extern int user_match (const struct key *key,const void *criterion); +extern int user_match (const struct key *key, +const struct key_match_data *match_data);extern void user_revoke (struct key *key);extern void user_destroy (struct key *key);extern void user_describe (const struct key *user,struct seq_file *m);
include/linux/key-type.h +28 -3
@@ -53,6 +53,22 @@ typedef int (*request_key_actor_t)(struct key_construction *key, const char *op, void *aux);/* +* Preparsed matching criterion. +*/ +struct key_match_data { +/* Comparison function, defaults to type->match, but can be replaced by +* type->match_preparse(). */ +int (*cmp)(const struct key *key, +const struct key_match_data *match_data); + +const void *raw_data;/* Raw match data */ +void *preparsed;/* For ->match_preparse() to stash stuff */ +unsigned lookup_type;/* Type of lookup for this search. */ +#define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000/* Direct lookup by description. */ +#define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001/* Iterative search. */ +}; + +/* * kernel managed key type definition */ struct key_type {
@@ -67,8 +83,6 @@ struct key_type {/* Default key search algorithm. */ unsigned def_lookup_type; -#define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000/* Direct lookup by description. */ -#define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001/* Iterative search. */ /* vet a description */ int (*vet_description)(const char *description);
@@ -96,8 +110,19 @@ struct key_type {*/ int (*update)(struct key *key,struct key_preparsed_payload *prep); +/* Preparse the data supplied to ->match() (optional). The +* data to be preparsed can be found in match_data->raw_data. +* The lookup type can also be set by this function. +*/ +int (*match_preparse)(struct key_match_data *match_data); +/* match a key against a description */ -int (*match)(const struct key *key,const void *desc); +int (*match)(const struct key *key, +const struct key_match_data *match_data); + +/* Free preparsed match data (optional). This should be supplied it +* ->match_preparse() is supplied. */ +void (*match_free)(struct key_match_data *match_data);/* clear some of the data from a key on revokation (optional) * - the key's semaphore will be write-locked by the caller
net/dns_resolver/dns_key.c +3 -2
@@ -177,10 +177,11 @@ static void dns_resolver_free_preparse(struct key_preparsed_payload *prep)* should end with a period). The domain name is case-independent. */ static int -dns_resolver_match (const struct key *key,const void *description) +dns_resolver_match (const struct key *key, +const struct key_match_data *match_data) {int slen, dlen, ret =0 ; -const char *src = key->description, *dsp = description; +const char *src = key->description, *dsp = match_data->raw_data;kenter ("%s,%s" , src, dsp);
security/keys/internal.h +3 -5
@@ -107,13 +107,10 @@ extern int iterate_over_keyring(const struct key *keyring,int (*func)(const struct key *key,void *data), void *data); -typedef int (*key_match_func_t )(const struct key *,const void *); -struct keyring_search_context {struct keyring_index_key index_key;const struct cred *cred; -key_match_func_t match; -const void *match_data; +struct key_match_data match_data;unsigned flags;#define KEYRING_SEARCH_LOOKUP_TYPE 0x0001/* [as type->def_lookup_type] */ #define KEYRING_SEARCH_NO_STATE_CHECK 0x0002/* Skip state checks */
@@ -152,7 +149,8 @@ extern struct key *request_key_and_link(struct key_type *type,struct key *dest_keyring, unsigned long flags); -extern int lookup_user_key_possessed (const struct key *key,const void *target); +extern int lookup_user_key_possessed (const struct key *key, +const struct key_match_data *match_data);extern key_ref_t lookup_user_key (key_serial_t id,unsigned long flags,key_perm_t perm);#define KEY_LOOKUP_CREATE 0x01
security/keys/keyring.c +30 -19
89 lines not on this page · raw message
security/keys/proc.c +4 -4
@@ -194,10 +194,10 @@ static int proc_keys_show(struct seq_file *m, void *v) .index_key.type = key->type, .index_key.description = key->description, .cred = current_cred(), - .match = lookup_user_key_possessed, - .match_data = key, - .flags = (KEYRING_SEARCH_NO_STATE_CHECK | - KEYRING_SEARCH_LOOKUP_DIRECT), + .match_data.cmp = lookup_user_key_possessed, + .match_data.raw_data = key, + .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, + .flags = KEYRING_SEARCH_NO_STATE_CHECK, }; key_ref =make_key_ref (key,0 );
security/keys/process_keys.c +7 -6
@@ -489,9 +489,10 @@ found:/* * See if the key we're looking at is the target key. */ -int lookup_user_key_possessed (const struct key *key,const void *target) +int lookup_user_key_possessed (const struct key *key, +const struct key_match_data *match_data) { -return key == target; +return key == match_data->raw_data; }/*
@@ -516,9 +517,9 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, key_perm_t perm) {struct keyring_search_context ctx = { - .match = lookup_user_key_possessed, - .flags = (KEYRING_SEARCH_NO_STATE_CHECK | -KEYRING_SEARCH_LOOKUP_DIRECT ), + .match_data.cmp = lookup_user_key_possessed, + .match_data.lookup_type =KEYRING_SEARCH_LOOKUP_DIRECT , + .flags =KEYRING_SEARCH_NO_STATE_CHECK , };struct request_key_auth *rka;struct key *key;
@@ -673,7 +674,7 @@ try_again: ctx.index_key.type = key->type; ctx.index_key.description = key->description; ctx.index_key.desc_len =strlen (key->description); - ctx.match_data = key; + ctx.match_data.raw_data = key;kdebug ("check possessed" ); skey_ref =search_process_keyrings (&ctx);kdebug ("possessed=%p" , skey_ref);
security/keys/request_key.c +16 -5
@@ -513,9 +513,9 @@ struct key *request_key_and_link(struct key_type *type, .index_key.type = type, .index_key.description = description, .cred = current_cred(), - .match = type->match, - .match_data = description, - .flags = KEYRING_SEARCH_LOOKUP_DIRECT, + .match_data.cmp = type->match, + .match_data.raw_data = description, + .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, };struct key *key;key_ref_t key_ref;
@@ -525,6 +525,14 @@ struct key *request_key_and_link(struct key_type *type, ctx.index_key.type->name, ctx.index_key.description, callout_info, callout_len, aux, dest_keyring, flags); +if (type->match_preparse) { + ret = type->match_preparse (&ctx.match_data); +if (ret <0 ) { + key =ERR_PTR (ret); + goto error; + } + } +/* search all the process keyrings for a key */ key_ref =search_process_keyrings (&ctx);
@@ -537,7 +545,7 @@ struct key *request_key_and_link(struct key_type *type,if (ret <0 ) {key_put (key); key =ERR_PTR (ret); - goto error; + goto error_free; } } }else if (PTR_ERR (key_ref) != -EAGAIN ) {
@@ -547,12 +555,15 @@ struct key *request_key_and_link(struct key_type *type,* should consult userspace if we can */ key =ERR_PTR (-ENOKEY );if (!callout_info) - goto error; + goto error_free; key =construct_key_and_link (&ctx, callout_info, callout_len, aux, dest_keyring, flags); } +error_free: +if (type->match_free) + type->match_free (&ctx.match_data); error:kleave (" = %p" , key);return key;
security/keys/request_key_auth.c +3 -3
@@ -246,9 +246,9 @@ struct key *key_get_instantiation_authkey(key_serial_t target_id) .index_key.type = &key_type_request_key_auth, .index_key.description = description, .cred = current_cred(), - .match = user_match, - .match_data = description, - .flags = KEYRING_SEARCH_LOOKUP_DIRECT, + .match_data.cmp = user_match, + .match_data.raw_data = description, + .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, };struct key *authkey;key_ref_t authkey_ref;
security/keys/user_defined.c +2 -2
@@ -141,9 +141,9 @@ EXPORT_SYMBOL_GPL(user_update);/* * match users on their name */ -int user_match (const struct key *key,const void *description) +int user_match (const struct key *key,const struct key_match_data *match_data) { -return strcmp (key->description, description) ==0 ; +return strcmp (key->description, match_data->raw_data) ==0 ; }EXPORT_SYMBOL_GPL (user_match);
David Howells
maintainerPATCH 3/13 KEYS: Remove key_type::def_lookup_type
In reply to David HowellsRemove key_type::def_lookup_type as it's no longer used. The information now defaults to KEYRING_SEARCH_LOOKUP_DIRECT but may be overridden by type->match_preparse().
---
crypto/asymmetric_keys/asymmetric_type.c | 1 - crypto/asymmetric_keys/pkcs7_key_type.c | 1 - include/linux/key-type.h | 3 --- security/keys/big_key.c | 1 - security/keys/user_defined.c | 2 -- 5 files changed, 8 deletions(-)
crypto/asymmetric_keys/asymmetric_type.c +0 -1
@@ -228,7 +228,6 @@ struct key_type key_type_asymmetric = { .match_free = asymmetric_key_match_free, .destroy = asymmetric_key_destroy, .describe = asymmetric_key_describe, - .def_lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE, };EXPORT_SYMBOL_GPL (key_type_asymmetric);
crypto/asymmetric_keys/pkcs7_key_type.c +0 -1
@@ -72,7 +72,6 @@ error:*/ static struct key_type key_type_pkcs7 = { .name ="pkcs7_test" , - .def_lookup_type =KEYRING_SEARCH_LOOKUP_DIRECT , .preparse = pkcs7_preparse, .free_preparse = user_free_preparse, .instantiate = generic_key_instantiate,
include/linux/key-type.h +0 -3
@@ -81,9 +81,6 @@ struct key_type {*/ size_t def_datalen; -/* Default key search algorithm. */ -unsigned def_lookup_type; -/* vet a description */ int (*vet_description)(const char *description);
security/keys/big_key.c +0 -1
@@ -33,7 +33,6 @@ MODULE_LICENSE("GPL");*/ struct key_type key_type_big_key = { .name ="big_key" , - .def_lookup_type =KEYRING_SEARCH_LOOKUP_DIRECT , .preparse = big_key_preparse, .free_preparse = big_key_free_preparse, .instantiate = generic_key_instantiate,
security/keys/user_defined.c +0 -2
@@ -26,7 +26,6 @@ static int logon_vet_description(const char *desc);*/ struct key_type key_type_user = { .name ="user" , - .def_lookup_type =KEYRING_SEARCH_LOOKUP_DIRECT , .preparse = user_preparse, .free_preparse = user_free_preparse, .instantiate = generic_key_instantiate,
@@ -48,7 +47,6 @@ EXPORT_SYMBOL_GPL(key_type_user);*/ struct key_type key_type_logon = { .name ="logon" , - .def_lookup_type =KEYRING_SEARCH_LOOKUP_DIRECT , .preparse = user_preparse, .free_preparse = user_free_preparse, .instantiate = generic_key_instantiate,
David Howells
maintainerPATCH 4/13 KEYS: Remove key_type::match in favour of overriding default by match_preparse
In reply to David HowellsA previous patch added a ->match_preparse() method to the key type. This is allowed to override the function called by the iteration algorithm. Therefore, we can just set a default that simply checks for an exact match of the key description with the original criterion data and allow match_preparse to override it as needed.
The key_type::match op is then redundant and can be removed, as can the user_match() function.
---
crypto/asymmetric_keys/asymmetric_type.c | 6 +++--- crypto/asymmetric_keys/pkcs7_key_type.c | 1 - fs/cifs/cifs_spnego.c | 1 - fs/cifs/cifsacl.c | 1 - fs/nfs/idmap.c | 2 -- include/keys/user-type.h | 3 --- include/linux/key-type.h | 4 ---- net/dns_resolver/dns_key.c | 17 +++++++++++++---- net/rxrpc/ar-key.c | 2 -- security/keys/big_key.c | 1 - security/keys/encrypted-keys/encrypted.c | 1 - security/keys/internal.h | 2 ++ security/keys/key.c | 2 +- security/keys/keyring.c | 12 ++++++++++-- security/keys/request_key.c | 2 +- security/keys/request_key_auth.c | 2 +- security/keys/trusted.c | 1 - security/keys/user_defined.c | 12 ------------ 18 files changed, 31 insertions(+), 41 deletions(-)
crypto/asymmetric_keys/asymmetric_type.c +3 -3
@@ -59,8 +59,8 @@ EXPORT_SYMBOL_GPL(asymmetric_keyid_match);* "id:<id>" - request a key matching the ID * "<subtype>:<id>" - request a key of a subtype */ -static int asymmetric_key_match (const struct key *key, -const struct key_match_data *match_data) +static int asymmetric_key_cmp (const struct key *key, +const struct key_match_data *match_data) {const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key);const char *description = match_data->raw_data;
@@ -110,6 +110,7 @@ static int asymmetric_key_match(const struct key *key,static int asymmetric_key_match_preparse (struct key_match_data *match_data) { match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; + match_data->cmp = asymmetric_key_cmp;return 0 ; }
@@ -224,7 +225,6 @@ struct key_type key_type_asymmetric = { .free_preparse = asymmetric_key_free_preparse, .instantiate = generic_key_instantiate, .match_preparse = asymmetric_key_match_preparse, - .match = asymmetric_key_match, .match_free = asymmetric_key_match_free, .destroy = asymmetric_key_destroy, .describe = asymmetric_key_describe,
crypto/asymmetric_keys/pkcs7_key_type.c +0 -1
@@ -75,7 +75,6 @@ static struct key_type key_type_pkcs7 = { .preparse = pkcs7_preparse, .free_preparse = user_free_preparse, .instantiate = generic_key_instantiate, - .match = user_match, .revoke = user_revoke, .destroy = user_destroy, .describe = user_describe,
fs/cifs/cifs_spnego.c +0 -1
@@ -62,7 +62,6 @@ cifs_spnego_key_destroy(struct key *key)struct key_type cifs_spnego_key_type = { .name ="cifs.spnego" , .instantiate = cifs_spnego_key_instantiate, - .match = user_match, .destroy = cifs_spnego_key_destroy, .describe = user_describe, };
fs/cifs/cifsacl.c +0 -1
@@ -84,7 +84,6 @@ static struct key_type cifs_idmap_key_type = { .instantiate = cifs_idmap_key_instantiate, .destroy = cifs_idmap_key_destroy, .describe = user_describe, - .match = user_match, }; static char *
fs/nfs/idmap.c +0 -2
@@ -177,7 +177,6 @@ static struct key_type key_type_id_resolver = { .preparse = user_preparse, .free_preparse = user_free_preparse, .instantiate = generic_key_instantiate, - .match = user_match, .revoke = user_revoke, .destroy = user_destroy, .describe = user_describe,
@@ -401,7 +400,6 @@ static struct key_type key_type_id_resolver_legacy = { .preparse = user_preparse, .free_preparse = user_free_preparse, .instantiate = generic_key_instantiate, - .match = user_match, .revoke = user_revoke, .destroy = user_destroy, .describe = user_describe,
include/keys/user-type.h +0 -3
@@ -36,13 +36,10 @@ extern struct key_type key_type_user;extern struct key_type key_type_logon;struct key_preparsed_payload ; -struct key_match_data ;extern int user_preparse (struct key_preparsed_payload *prep);extern void user_free_preparse (struct key_preparsed_payload *prep);extern int user_update (struct key *key,struct key_preparsed_payload *prep); -extern int user_match (const struct key *key, -const struct key_match_data *match_data);extern void user_revoke (struct key *key);extern void user_destroy (struct key *key);extern void user_describe (const struct key *user,struct seq_file *m);
include/linux/key-type.h +0 -4
@@ -113,10 +113,6 @@ struct key_type {*/ int (*match_preparse)(struct key_match_data *match_data); -/* match a key against a description */ -int (*match)(const struct key *key, -const struct key_match_data *match_data); -/* Free preparsed match data (optional). This should be supplied it * ->match_preparse() is supplied. */ void (*match_free)(struct key_match_data *match_data);
net/dns_resolver/dns_key.c +13 -4
@@ -176,9 +176,8 @@ static void dns_resolver_free_preparse(struct key_preparsed_payload *prep)* The domain name may be a simple name or an absolute domain name (which * should end with a period). The domain name is case-independent. */ -static int -dns_resolver_match (const struct key *key, -const struct key_match_data *match_data) +static int dns_resolver_cmp (const struct key *key, +const struct key_match_data *match_data) {int slen, dlen, ret =0 ;const char *src = key->description, *dsp = match_data->raw_data;
@@ -210,6 +209,16 @@ no_match: }/* +* Preparse the match criterion. +*/ +static int dns_resolver_match_preparse (struct key_match_data *match_data) +{ + match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; + match_data->cmp = dns_resolver_cmp; +return 0 ; +} + +/* * Describe a DNS key */ static void dns_resolver_describe (const struct key *key,struct seq_file *m)
@@ -243,7 +252,7 @@ struct key_type key_type_dns_resolver = { .preparse = dns_resolver_preparse, .free_preparse = dns_resolver_free_preparse, .instantiate = generic_key_instantiate, - .match = dns_resolver_match, + .match_preparse = dns_resolver_match_preparse, .revoke = user_revoke, .destroy = user_destroy, .describe = dns_resolver_describe,
net/rxrpc/ar-key.c +0 -2
@@ -44,7 +44,6 @@ struct key_type key_type_rxrpc = { .preparse = rxrpc_preparse, .free_preparse = rxrpc_free_preparse, .instantiate = generic_key_instantiate, - .match = user_match, .destroy = rxrpc_destroy, .describe = rxrpc_describe, .read = rxrpc_read,
@@ -61,7 +60,6 @@ struct key_type key_type_rxrpc_s = { .preparse = rxrpc_preparse_s, .free_preparse = rxrpc_free_preparse_s, .instantiate = generic_key_instantiate, - .match = user_match, .destroy = rxrpc_destroy_s, .describe = rxrpc_describe, };
security/keys/big_key.c +0 -1
@@ -36,7 +36,6 @@ struct key_type key_type_big_key = { .preparse = big_key_preparse, .free_preparse = big_key_free_preparse, .instantiate = generic_key_instantiate, - .match = user_match, .revoke = big_key_revoke, .destroy = big_key_destroy, .describe = big_key_describe,
security/keys/encrypted-keys/encrypted.c +0 -1
@@ -970,7 +970,6 @@ struct key_type key_type_encrypted = { .name = "encrypted", .instantiate = encrypted_instantiate, .update = encrypted_update, - .match = user_match, .destroy = encrypted_destroy, .describe = user_describe, .read = encrypted_read,
security/keys/internal.h +2 -0
@@ -128,6 +128,8 @@ struct keyring_search_context {struct timespec now; }; +extern int key_default_cmp (const struct key *key, +const struct key_match_data *match_data);extern key_ref_t keyring_search_aux (key_ref_t keyring_ref,struct keyring_search_context *ctx);
security/keys/key.c +1 -1
@@ -799,7 +799,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, } key_ref =ERR_PTR (-EINVAL ); -if (!index_key.type->match || !index_key.type->instantiate || +if (!index_key.type->instantiate || (!index_key.description && !index_key.type->preparse)) goto error_put_type;
security/keys/keyring.c +10 -2
@@ -89,7 +89,6 @@ struct key_type key_type_keyring = { .preparse = keyring_preparse, .free_preparse = keyring_free_preparse, .instantiate = keyring_instantiate, - .match = user_match, .revoke = keyring_revoke, .destroy = keyring_destroy, .describe = keyring_describe,
@@ -512,6 +511,15 @@ struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,EXPORT_SYMBOL (keyring_alloc);/* +* By default, we keys found by getting an exact match on their descriptions. +*/ +int key_default_cmp (const struct key *key, +const struct key_match_data *match_data) +{ +return strcmp (key->description, match_data->raw_data) ==0 ; +} + +/* * Iteration function to consider each key found. */ static int keyring_search_iterator (const void *object,void *iterator_data)
@@ -884,7 +892,7 @@ key_ref_t keyring_search(key_ref_t keyring, .index_key.type = type, .index_key.description = description, .cred = current_cred(), - .match_data.cmp = type->match, + .match_data.cmp = key_default_cmp, .match_data.raw_data = description, .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, .flags = KEYRING_SEARCH_DO_STATE_CHECK,
security/keys/request_key.c +1 -1
@@ -513,7 +513,7 @@ struct key *request_key_and_link(struct key_type *type, .index_key.type = type, .index_key.description = description, .cred = current_cred(), - .match_data.cmp = type->match, + .match_data.cmp = key_default_cmp, .match_data.raw_data = description, .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, };
security/keys/request_key_auth.c +1 -1
@@ -246,7 +246,7 @@ struct key *key_get_instantiation_authkey(key_serial_t target_id) .index_key.type = &key_type_request_key_auth, .index_key.description = description, .cred = current_cred(), - .match_data.cmp = user_match, + .match_data.cmp = key_default_cmp, .match_data.raw_data = description, .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, };
security/keys/trusted.c +0 -1
@@ -1096,7 +1096,6 @@ struct key_type key_type_trusted = { .name = "trusted", .instantiate = trusted_instantiate, .update = trusted_update, - .match = user_match, .destroy = trusted_destroy, .describe = user_describe, .read = trusted_read,
security/keys/user_defined.c +0 -12
@@ -30,7 +30,6 @@ struct key_type key_type_user = { .free_preparse = user_free_preparse, .instantiate = generic_key_instantiate, .update = user_update, - .match = user_match, .revoke = user_revoke, .destroy = user_destroy, .describe = user_describe,
@@ -51,7 +50,6 @@ struct key_type key_type_logon = { .free_preparse = user_free_preparse, .instantiate = generic_key_instantiate, .update = user_update, - .match = user_match, .revoke = user_revoke, .destroy = user_destroy, .describe = user_describe,
@@ -137,16 +135,6 @@ error:EXPORT_SYMBOL_GPL (user_update);/* -* match users on their name -*/ -int user_match (const struct key *key,const struct key_match_data *match_data) -{ -return strcmp (key->description, match_data->raw_data) ==0 ; -} - -EXPORT_SYMBOL_GPL (user_match); - -/* * dispose of the links from a revoked keyring * - called with the key sem write-locked */
David Howells
maintainerPATCH 5/13 KEYS: Make the key matching functions return bool
In reply to David HowellsMake the key matching functions pointed to by key_match_data::cmp return bool rather than int.
---
crypto/asymmetric_keys/asymmetric_type.c | 4 ++-- include/linux/key-type.h | 10 ++++++---- net/dns_resolver/dns_key.c | 4 ++-- security/keys/internal.h | 8 ++++---- security/keys/keyring.c | 4 ++-- security/keys/process_keys.c | 4 ++-- 6 files changed, 18 insertions(+), 16 deletions(-)
crypto/asymmetric_keys/asymmetric_type.c +2 -2
@@ -59,8 +59,8 @@ EXPORT_SYMBOL_GPL(asymmetric_keyid_match);* "id:<id>" - request a key matching the ID * "<subtype>:<id>" - request a key of a subtype */ -static int asymmetric_key_cmp (const struct key *key, -const struct key_match_data *match_data) +static bool asymmetric_key_cmp (const struct key *key, +const struct key_match_data *match_data) {const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key);const char *description = match_data->raw_data;
include/linux/key-type.h +6 -4
@@ -56,10 +56,12 @@ typedef int (*request_key_actor_t)(struct key_construction *key,* Preparsed matching criterion. */ struct key_match_data { -/* Comparison function, defaults to type->match, but can be replaced by -* type->match_preparse(). */ -int (*cmp)(const struct key *key, -const struct key_match_data *match_data); +/* Comparison function, defaults to exact description match, but can be +* overridden by type->match_preparse(). Should return true if a match +* is found and false if not. +*/ +bool (*cmp)(const struct key *key, +const struct key_match_data *match_data);const void *raw_data;/* Raw match data */ void *preparsed;/* For ->match_preparse() to stash stuff */
net/dns_resolver/dns_key.c +2 -2
@@ -176,8 +176,8 @@ static void dns_resolver_free_preparse(struct key_preparsed_payload *prep)* The domain name may be a simple name or an absolute domain name (which * should end with a period). The domain name is case-independent. */ -static int dns_resolver_cmp (const struct key *key, -const struct key_match_data *match_data) +static bool dns_resolver_cmp (const struct key *key, +const struct key_match_data *match_data) {int slen, dlen, ret =0 ;const char *src = key->description, *dsp = match_data->raw_data;
security/keys/internal.h +4 -4
@@ -128,8 +128,8 @@ struct keyring_search_context {struct timespec now; }; -extern int key_default_cmp (const struct key *key, -const struct key_match_data *match_data); +extern bool key_default_cmp (const struct key *key, +const struct key_match_data *match_data);extern key_ref_t keyring_search_aux (key_ref_t keyring_ref,struct keyring_search_context *ctx);
@@ -151,8 +151,8 @@ extern struct key *request_key_and_link(struct key_type *type,struct key *dest_keyring, unsigned long flags); -extern int lookup_user_key_possessed (const struct key *key, -const struct key_match_data *match_data); +extern bool lookup_user_key_possessed (const struct key *key, +const struct key_match_data *match_data);extern key_ref_t lookup_user_key (key_serial_t id,unsigned long flags,key_perm_t perm);#define KEY_LOOKUP_CREATE 0x01
security/keys/keyring.c +2 -2
@@ -513,8 +513,8 @@ EXPORT_SYMBOL(keyring_alloc);/* * By default, we keys found by getting an exact match on their descriptions. */ -int key_default_cmp (const struct key *key, -const struct key_match_data *match_data) +bool key_default_cmp (const struct key *key, +const struct key_match_data *match_data) {return strcmp (key->description, match_data->raw_data) ==0 ; }
security/keys/process_keys.c +2 -2
@@ -489,8 +489,8 @@ found:/* * See if the key we're looking at is the target key. */ -int lookup_user_key_possessed (const struct key *key, -const struct key_match_data *match_data) +bool lookup_user_key_possessed (const struct key *key, +const struct key_match_data *match_data) {return key == match_data->raw_data; }
David Howells
maintainerPATCH 6/13 KEYS: Implement binary asymmetric key ID handling
In reply to David HowellsImplement the first step in using binary key IDs for asymmetric keys rather than hex string keys.
The previously added match data preparsing will be able to convert hex criterion strings into binary which can then be compared more rapidly.
Further, we actually want more then one ID string per public key. The problem is that X.509 certs refer to other X.509 certs by matching Issuer + AuthKeyId to Subject + SubjKeyId, but PKCS#7 messages match against X.509 Issuer + SerialNumber.
This patch just provides facilities for a later patch to make use of.
---
crypto/asymmetric_keys/asymmetric_keys.h | 4 + crypto/asymmetric_keys/asymmetric_type.c | 82 ++++++++++++++++++++++++++++++ include/keys/asymmetric-type.h | 38 ++++++++++++++ 3 files changed, 124 insertions(+)
crypto/asymmetric_keys/asymmetric_keys.h +4 -0
@@ -10,6 +10,10 @@*/ int asymmetric_keyid_match (const char *kid,const char *id); +extern bool asymmetric_match_key_ids (const struct asymmetric_key_ids *kids, +const struct asymmetric_key_id *match_id); + +extern struct asymmetric_key_id *asymmetric_key_hex_to_key_id (const char *id);static inline const char *asymmetric_key_id (const struct key *key) {
crypto/asymmetric_keys/asymmetric_type.c +82 -0
94 lines not on this page · raw message
include/keys/asymmetric-type.h +38 -0
@@ -19,6 +19,44 @@extern struct key_type key_type_asymmetric;/* +* Identifiers for an asymmetric key ID. We have three ways of looking up a +* key derived from an X.509 certificate: +* +* (1) Serial Number & Issuer. Non-optional. This is the only valid way to +* map a PKCS#7 signature to an X.509 certificate. +* +* (2) Issuer & Subject Unique IDs. Optional. These were the original way to +* match X.509 certificates, but have fallen into disuse in favour of (3). +* +* (3) Auth & Subject Key Identifiers. Optional. SKIDs are only provided on +* CA keys that are intended to sign other keys, so don't appear in end +* user certificates unless forced. +* +* We could also support an PGP key identifier, which is just a SHA1 sum of the +* public key and certain parameters, but since we don't support PGP keys at +* the moment, we shall ignore those. +* +* What we actually do is provide a place where binary identifiers can be +* stashed and then compare against them when checking for an id match. +*/ +struct asymmetric_key_id { +unsigned short len; +unsigned char data[]; +}; + +struct asymmetric_key_ids { +void *id[2 ]; +}; + +extern bool asymmetric_key_id_same (const struct asymmetric_key_id *kid1, +const struct asymmetric_key_id *kid2); + +extern struct asymmetric_key_id *asymmetric_key_generate_id (const void *val_1, +size_t len_1, +const void *val_2, +size_t len_2); + +/* * The payload is at the discretion of the subtype. */
David Howells
maintainerPATCH 7/13 PKCS#7: Clean up the signed info freeing and fix the parser cleanup
In reply to David HowellsProvide a function to free pkcs7_signed_info structs and use it in the parser and pkcs7_message struct freeing routines.
This also exposes a missing pkcs7_message free in the parser cleanup which is also dealt with.
---
crypto/asymmetric_keys/pkcs7_parser.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
crypto/asymmetric_keys/pkcs7_parser.c +19 -9
@@ -31,6 +31,18 @@ struct pkcs7_parse_context {unsigned sinfo_index; }; +/* +* Free a signed information block. +*/ +static void pkcs7_free_signed_info (struct pkcs7_signed_info *sinfo) +{ +if (sinfo) { +mpi_free (sinfo->sig.mpi[0 ]); +kfree (sinfo->sig.digest); +kfree (sinfo); + } +} +/** * pkcs7_free_message - Free a PKCS#7 message * @pkcs7: The PKCS#7 message to free
@@ -54,9 +66,7 @@ void pkcs7_free_message(struct pkcs7_message *pkcs7)while (pkcs7->signed_infos) { sinfo = pkcs7->signed_infos; pkcs7->signed_infos = sinfo->next; -mpi_free (sinfo->sig.mpi[0 ]); -kfree (sinfo->sig.digest); -kfree (sinfo); +pkcs7_free_signed_info (sinfo); }kfree (pkcs7); }
@@ -95,21 +105,21 @@ struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)if (ret <0 ) goto error_decode; +out:while (ctx->certs) {struct x509_certificate *cert = ctx->certs; ctx->certs = cert->next;x509_free_certificate (cert); } -mpi_free (ctx->sinfo->sig.mpi[0 ]); -kfree (ctx->sinfo->sig.digest); -kfree (ctx->sinfo); +pkcs7_free_signed_info (ctx->sinfo);kfree (ctx);return msg; error_decode: -mpi_free (ctx->sinfo->sig.mpi[0 ]); -kfree (ctx->sinfo->sig.digest); -kfree (ctx->sinfo); +pkcs7_free_message (msg); + msg =ERR_PTR (ret); + goto out; + error_no_sinfo:kfree (ctx); error_no_ctx:
David Howells
maintainerPATCH 8/13 KEYS: Overhaul key identification when searching for asymmetric keys
In reply to David HowellsMake use of the new match string preparsing to overhaul key identification when searching for asymmetric keys. The following changes are made:
(1) Use the previously created asymmetric_key_id struct to hold the following key IDs derived from the X.509 certificate or PKCS#7 message:
id: serial number + issuer
skid: subjKeyId + subject
authority: authKeyId + issuer
(2) Replace the hex fingerprint attached to key->type_data[1] with an asymmetric_key_ids struct containing the id and the skid (if present).
(3) Make the asymmetric_type match data preparse select one of two searches:
(a) An iterative search for the key ID given if prefixed with "id:". The prefix is expected to be followed by a hex string giving the ID to search for. The criterion key ID is checked against all key IDs recorded on the key.
(b) A direct search if the key ID is not prefixed with "id:". This will look for an exact match on the key description.
(4) Make x509_request_asymmetric_key() take a key ID. This is then converted into "id:<hex>" and passed into keyring_search() where match preparsing will turn it back into a binary ID.
(5) X.509 certificate verification then takes the authority key ID and looks up a key that matches it to find the public key for the certificate signature.
(6) PKCS#7 certificate verification then takes the id key ID and looks up a key that matches it to find the public key for the signed information block signature.
Additional changes:
(1) Multiple subjKeyId and authKeyId values on an X.509 certificate cause the cert to be rejected with -EBADMSG.
(2) The 'fingerprint' ID is gone. This was primarily intended to convey PGP public key fingerprints. If PGP is supported in future, this should generate a key ID that carries the fingerprint.
(3) Th ca_keyid= kernel command line option is now converted to a key ID and used to match the authority key ID. Possibly this should only match the actual authKeyId part and not the issuer as well.
---
crypto/asymmetric_keys/asymmetric_keys.h | 4 - crypto/asymmetric_keys/asymmetric_type.c | 133 ++++++++++++----------------- crypto/asymmetric_keys/pkcs7_parser.c | 38 ++++++-- crypto/asymmetric_keys/pkcs7_parser.h | 5 - crypto/asymmetric_keys/pkcs7_trust.c | 6 - crypto/asymmetric_keys/pkcs7_verify.c | 44 ++++------ crypto/asymmetric_keys/x509_cert_parser.c | 55 +++++++----- crypto/asymmetric_keys/x509_parser.h | 5 + crypto/asymmetric_keys/x509_public_key.c | 89 +++++++++++-------- include/crypto/public_key.h | 5 + 10 files changed, 198 insertions(+), 186 deletions(-)
crypto/asymmetric_keys/asymmetric_keys.h +2 -2
@@ -9,13 +9,13 @@* 2 of the Licence, or (at your option) any later version. */ -int asymmetric_keyid_match (const char *kid,const char *id);extern bool asymmetric_match_key_ids (const struct asymmetric_key_ids *kids,const struct asymmetric_key_id *match_id);extern struct asymmetric_key_id *asymmetric_key_hex_to_key_id (const char *id); -static inline const char *asymmetric_key_id (const struct key *key) +static inline +const struct asymmetric_key_ids *asymmetric_key_ids (const struct key *key) {return key->type_data.p[1 ]; }
crypto/asymmetric_keys/asymmetric_type.c +56 -77
193 lines not on this page · raw message
crypto/asymmetric_keys/pkcs7_parser.c +27 -11
@@ -29,6 +29,10 @@ struct pkcs7_parse_context {enum OID last_oid;/* Last OID encountered */ unsigned x509_index;unsigned sinfo_index; +const void *raw_serial; +unsigned raw_serial_size; +unsigned raw_issuer_size; +const void *raw_issuer; };/*
@@ -39,6 +43,7 @@ static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)if (sinfo) {mpi_free (sinfo->sig.mpi[0 ]);kfree (sinfo->sig.digest); +kfree (sinfo->signing_cert_id);kfree (sinfo); } }
@@ -256,10 +261,10 @@ int pkcs7_extract_cert(void *context, size_t hdrlen,if (IS_ERR (x509))return PTR_ERR (x509); -pr_debug ("Got cert for %s\n" , x509->subject); -pr_debug ("- fingerprint %s\n" , x509->fingerprint); - x509->index = ++ctx->x509_index; +pr_debug ("Got cert %u for %s\n" , x509->index, x509->subject); +pr_debug ("- fingerprint %*phN\n" , x509->id->len, x509->id->data); + *ctx->ppcerts = x509; ctx->ppcerts = &x509->next;return 0 ;
@@ -348,8 +353,8 @@ int pkcs7_sig_note_serial(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_serial = value; - ctx->sinfo->raw_serial_size = vlen; + ctx->raw_serial = value; + ctx->raw_serial_size = vlen;return 0 ; }
@@ -361,8 +366,8 @@ int pkcs7_sig_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_issuer = value; - ctx->sinfo->raw_issuer_size = vlen; + ctx->raw_issuer = value; + ctx->raw_issuer_size = vlen;return 0 ; }
@@ -395,10 +400,21 @@ int pkcs7_note_signed_info(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - - ctx->sinfo->index = ++ctx->sinfo_index; - *ctx->ppsinfo = ctx->sinfo; - ctx->ppsinfo = &ctx->sinfo->next; +struct pkcs7_signed_info *sinfo = ctx->sinfo; +struct asymmetric_key_id *kid; + +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (ctx->raw_serial, + ctx->raw_serial_size, + ctx->raw_issuer, + ctx->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + + sinfo->signing_cert_id = kid; + sinfo->index = ++ctx->sinfo_index; + *ctx->ppsinfo = sinfo; + ctx->ppsinfo = &sinfo->next; ctx->sinfo =kzalloc (sizeof (struct pkcs7_signed_info ),GFP_KERNEL );if (!ctx->sinfo)return -ENOMEM ;
crypto/asymmetric_keys/pkcs7_parser.h +1 -4
@@ -33,10 +33,7 @@ struct pkcs7_signed_info {const void *authattrs;/* Issuing cert serial number and issuer's name */ -const void *raw_serial; -unsigned raw_serial_size; -unsigned raw_issuer_size; -const void *raw_issuer; +struct asymmetric_key_id *signing_cert_id;/* Message signature. *
crypto/asymmetric_keys/pkcs7_trust.c +2 -4
@@ -49,8 +49,7 @@ 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->subject, - x509->fingerprint); + key =x509_request_asymmetric_key (trust_keyring, x509->id);if (!IS_ERR (key))/* One of the X.509 certificates in the PKCS#7 message * is apparently the same as one we already trust.
@@ -82,8 +81,7 @@ int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,return -ENOKEY ; } - key =x509_request_asymmetric_key (trust_keyring, last->issuer, - last->authority); + key =x509_request_asymmetric_key (trust_keyring, last->authority);if (IS_ERR (key))return PTR_ERR (key) == -ENOMEM ? -ENOMEM : -ENOKEY ; x509 = last;
crypto/asymmetric_keys/pkcs7_verify.c +20 -24
98 lines not on this page · raw message
crypto/asymmetric_keys/x509_cert_parser.c +33 -22
112 lines not on this page · raw message
crypto/asymmetric_keys/x509_parser.h +3 -2
@@ -19,8 +19,9 @@ struct x509_certificate {struct public_key_signature sig;/* Signature parameters */ char *issuer;/* Name of certificate issuer */ char *subject;/* Name of certificate subject */ -char *fingerprint;/* Key fingerprint as hex */ -char *authority;/* Authority key fingerprint as hex */ +struct asymmetric_key_id *id;/* Issuer + serial number */ +struct asymmetric_key_id *skid;/* Subject key identifier */ +struct asymmetric_key_id *authority;/* Authority key identifier */ struct tm valid_from;struct tm valid_to;const void *tbs;/* Signed data */
crypto/asymmetric_keys/x509_public_key.c +51 -38
169 lines not on this page · raw message
include/crypto/public_key.h +3 -2
@@ -15,6 +15,7 @@#define _LINUX_PUBLIC_KEY_H#include <linux/mpi.h> +#include <keys/asymmetric-type.h> #include <crypto/hash_info.h> enum pkey_algo {
@@ -98,8 +99,8 @@ struct key;extern int verify_signature (const struct key *key,const struct public_key_signature *sig); +struct asymmetric_key_id ;extern struct key *x509_request_asymmetric_key (struct key *keyring, -const char *issuer, -const char *key_id); +const struct asymmetric_key_id *kid);#endif /* _LINUX_PUBLIC_KEY_H */
Re: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
Hi David,
I just took latest #next branch from James's security tree which includes latest KEYs patches and noticed following:
[ 9.812332] Request for unknown module key 'Magrathea: Glacier
signing key: 926305d6dda66f47139eb4e3cb25a6adef527f08' err -11
Also I noticed that output of 'keyctl show' and 'cat /proc/keys' output also has changed in respect of certificate ids..
Those ids does not look any close to my kernel X509 X509v3 Subject Key Identifier, which is:
92:63:05:D6:DD:A6:6F:47:13:9E:B4:E3:CB:25:A6:AD:EF:52:7F:08
proc/keys shows
symmetri Magrathea: Glacier signing key: d9e2e4c6951f1e83: X509.RSA 6865612e68326732 []
Very different ids..
How could I match certificate now?
Module verification code cannot find needed key..
- Dmitry
Quoted 870 lines · David Howells
Make use of the new match string preparsing to overhaul key identification when searching for asymmetric keys. The following changes are made:
(1) Use the previously created
asymmetric_key_idstruct to hold the following key IDs derived from the X.509 certificate or PKCS#7 message:id: serial number + issuer
skid: subjKeyId + subject
authority: authKeyId + issuer(2) Replace the hex fingerprint attached to key->type_data[1] with an
asymmetric_key_idsstruct containing the id and the skid (if present).(3) Make the
asymmetric_typematch data preparse select one of two searches:(a) An iterative search for the key ID given if prefixed with "id:". The prefix is expected to be followed by a hex string giving the ID to search for. The criterion key ID is checked against all key IDs recorded on the key.
(b) A direct search if the key ID is not prefixed with "id:". This will look for an exact match on the key description.
(4) Make
x509_request_asymmetric_key()take a key ID. This is then converted into "id:<hex>" and passed intokeyring_search()where match preparsing will turn it back into a binary ID.(5) X.509 certificate verification then takes the authority key ID and looks up a key that matches it to find the public key for the certificate signature.
(6) PKCS#7 certificate verification then takes the id key ID and looks up a key that matches it to find the public key for the signed information block signature.
Additional changes:
(1) Multiple subjKeyId and authKeyId values on an X.509 certificate cause the cert to be rejected with
-EBADMSG.(2) The 'fingerprint' ID is gone. This was primarily intended to convey PGP public key fingerprints. If PGP is supported in future, this should generate a key ID that carries the fingerprint.
(3) Th ca_keyid= kernel command line option is now converted to a key ID and used to match the authority key ID. Possibly this should only match the actual authKeyId part and not the issuer as well.
Signed-off-by David Howells <[email protected]>---
crypto/asymmetric_keys/asymmetric_keys.h | 4 - crypto/asymmetric_keys/asymmetric_type.c | 133 ++++++++++++----------------- crypto/asymmetric_keys/pkcs7_parser.c | 38 ++++++-- crypto/asymmetric_keys/pkcs7_parser.h | 5 - crypto/asymmetric_keys/pkcs7_trust.c | 6 - crypto/asymmetric_keys/pkcs7_verify.c | 44 ++++------ crypto/asymmetric_keys/x509_cert_parser.c | 55 +++++++----- crypto/asymmetric_keys/x509_parser.h | 5 + crypto/asymmetric_keys/x509_public_key.c | 89 +++++++++++-------- include/crypto/public_key.h | 5 + 10 files changed, 198 insertions(+), 186 deletions(-)crypto/asymmetric_keys/asymmetric_keys.h +2
-2@@ -9,13 +9,13 @@* 2 of the Licence, or (at your option) any later version. */ -int asymmetric_keyid_match (const char *kid,const char *id);extern bool asymmetric_match_key_ids (const struct asymmetric_key_ids *kids,const struct asymmetric_key_id *match_id);extern struct asymmetric_key_id *asymmetric_key_hex_to_key_id (const char *id); -static inline const char *asymmetric_key_id (const struct key *key) +static inline +const struct asymmetric_key_ids *asymmetric_key_ids (const struct key *key) {return key->type_data.p[1 ]; }crypto/asymmetric_keys/asymmetric_type.c +56
-77@@ -105,76 +105,15 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id) }/* -* Match asymmetric key id with partial match -* @id: key id to match in a form "id:<id>" -*/ -int asymmetric_keyid_match (const char *kid,const char *id) -{ -size_t idlen, kidlen; - -if (!kid || !id) -return 0 ; - -/* make it possible to use id as in the request: "id:<id>" */ -if (strncmp (id,"id:" ,3 ) ==0 ) - id +=3 ; - -/* Anything after here requires a partial match on the ID string */ - idlen =strlen (id); - kidlen =strlen (kid); -if (idlen > kidlen) -return 0 ; - - kid += kidlen - idlen; -if (strcasecmp (id, kid) !=0 ) -return 0 ; - -return 1 ; -} -EXPORT_SYMBOL_GPL (asymmetric_keyid_match); - -/* -* Match asymmetric keys on (part of) their name -* We have some shorthand methods for matching keys. We allow: -* -* "<desc>" - request a key by description -* "id:<id>" - request a key matching the ID -* "<subtype>:<id>" - request a key of a subtype +* Match asymmetric keys by ID. */ static bool asymmetric_key_cmp (const struct key *key,const struct key_match_data *match_data) { -const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); -const char *description = match_data->raw_data; -const char *spec = description; -const char *id; -ptrdiff_t speclen; - -if (!subtype || !spec || !*spec) -return 0 ; - -/* See if the full key description matches as is */ -if (key->description &&strcmp (key->description, description) ==0 ) -return 1 ; - -/* All tests from here on break the criterion description into a -* specifier, a colon and then an identifier. -*/ - id =strchr (spec,':' ); -if (!id) -return 0 ; - - speclen = id - spec; - id++; - -if (speclen ==2 &&memcmp (spec,"id" ,2 ) ==0 ) -return asymmetric_keyid_match (asymmetric_key_id (key), id); +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *match_id = match_data->preparsed; -if (speclen == subtype->name_len && -memcmp (spec, subtype->name, speclen) ==0 ) -return 1 ; - -return 0 ; +return asymmetric_match_key_ids (kids, match_id); }/* @@ -191,8 +130,30 @@ static bool asymmetric_key_cmp(const struct key *key,*/ static int asymmetric_key_match_preparse (struct key_match_data *match_data) { - match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +struct asymmetric_key_id *match_id; +const char *spec = match_data->raw_data; +const char *id; + +if (!spec || !*spec) +return -EINVAL ; +if (spec[0 ] =='i' && + spec[1 ] =='d' && + spec[2 ] ==':' ) { + id = spec +3 ; + }else { + goto default_match; + } + + match_id =asymmetric_key_hex_to_key_id (id); +if (!match_id) +return -ENOMEM ; + + match_data->preparsed = match_id; match_data->cmp = asymmetric_key_cmp; + match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +return 0 ; + +default_match:return 0 ; }@@ -201,6 +162,7 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data)*/ static void asymmetric_key_match_free (struct key_match_data *match_data) { +kfree (match_data->preparsed); }/* @@ -209,8 +171,10 @@ static void asymmetric_key_match_free(struct key_match_data *match_data)static void asymmetric_key_describe (const struct key *key,struct seq_file *m) {const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); -const char *kid =asymmetric_key_id (key); -size_t n; +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *kid; +const unsigned char *p; +int n;seq_puts (m, key->description);@@ -218,13 +182,16 @@ static void asymmetric_key_describe(const struct key *key, struct seq_file *m)seq_puts (m,": " ); subtype->describe (key, m); -if (kid) { +if (kids && kids->id[0 ]) { + kid = kids->id[0 ];seq_putc (m,' ' ); - n =strlen (kid); -if (n <=8 ) -seq_puts (m, kid); -else -seq_puts (m, kid + n -8 ); + n = kid->len; + p = kid->data; +if (n >8 ) { + p += n -8 ; + n =8 ; + } +seq_printf (m,"%*phN" , n, p); }seq_puts (m," [" );@@ -275,6 +242,7 @@ static int asymmetric_key_preparse(struct key_preparsed_payload *prep)static void asymmetric_key_free_preparse (struct key_preparsed_payload *prep) {struct asymmetric_key_subtype *subtype = prep->type_data[0 ]; +struct asymmetric_key_ids *kids = prep->type_data[1 ];pr_devel ("==>%s()\n" , __func__);@@ -282,7 +250,11 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep) subtype->destroy (prep->payload[0 ]);module_put (subtype->owner); } -kfree (prep->type_data[1 ]); +if (kids) { +kfree (kids->id[0 ]); +kfree (kids->id[1 ]); +kfree (kids); + }kfree (prep->description); }@@ -292,13 +264,20 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)static void asymmetric_key_destroy (struct key *key) {struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); +struct asymmetric_key_ids *kids = key->type_data.p[1 ]; +if (subtype) { subtype->destroy (key->payload.data);module_put (subtype->owner); key->type_data.p[0 ] =NULL ; } -kfree (key->type_data.p[1 ]); - key->type_data.p[1 ] =NULL ; + +if (kids) { +kfree (kids->id[0 ]); +kfree (kids->id[1 ]); +kfree (kids); + key->type_data.p[1 ] =NULL ; + } }struct key_type key_type_asymmetric = {crypto/asymmetric_keys/pkcs7_parser.c +27
-11@@ -29,6 +29,10 @@ struct pkcs7_parse_context {enum OID last_oid;/* Last OID encountered */ unsigned x509_index;unsigned sinfo_index; +const void *raw_serial; +unsigned raw_serial_size; +unsigned raw_issuer_size; +const void *raw_issuer; };/* @@ -39,6 +43,7 @@ static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)if (sinfo) {mpi_free (sinfo->sig.mpi[0 ]);kfree (sinfo->sig.digest); +kfree (sinfo->signing_cert_id);kfree (sinfo); } }@@ -256,10 +261,10 @@ int pkcs7_extract_cert(void *context, size_t hdrlen,if (IS_ERR (x509))return PTR_ERR (x509); -pr_debug ("Got cert for %s\n" , x509->subject); -pr_debug ("- fingerprint %s\n" , x509->fingerprint); - x509->index = ++ctx->x509_index; +pr_debug ("Got cert %u for %s\n" , x509->index, x509->subject); +pr_debug ("- fingerprint %*phN\n" , x509->id->len, x509->id->data); + *ctx->ppcerts = x509; ctx->ppcerts = &x509->next;return 0 ;@@ -348,8 +353,8 @@ int pkcs7_sig_note_serial(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_serial = value; - ctx->sinfo->raw_serial_size = vlen; + ctx->raw_serial = value; + ctx->raw_serial_size = vlen;return 0 ; }@@ -361,8 +366,8 @@ int pkcs7_sig_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_issuer = value; - ctx->sinfo->raw_issuer_size = vlen; + ctx->raw_issuer = value; + ctx->raw_issuer_size = vlen;return 0 ; }@@ -395,10 +400,21 @@ int pkcs7_note_signed_info(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - - ctx->sinfo->index = ++ctx->sinfo_index; - *ctx->ppsinfo = ctx->sinfo; - ctx->ppsinfo = &ctx->sinfo->next; +struct pkcs7_signed_info *sinfo = ctx->sinfo; +struct asymmetric_key_id *kid; + +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (ctx->raw_serial, + ctx->raw_serial_size, + ctx->raw_issuer, + ctx->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + + sinfo->signing_cert_id = kid; + sinfo->index = ++ctx->sinfo_index; + *ctx->ppsinfo = sinfo; + ctx->ppsinfo = &sinfo->next; ctx->sinfo =kzalloc (sizeof (struct pkcs7_signed_info ),GFP_KERNEL );if (!ctx->sinfo)return -ENOMEM ;crypto/asymmetric_keys/pkcs7_parser.h +1
-4@@ -33,10 +33,7 @@ struct pkcs7_signed_info {const void *authattrs;/* Issuing cert serial number and issuer's name */ -const void *raw_serial; -unsigned raw_serial_size; -unsigned raw_issuer_size; -const void *raw_issuer; +struct asymmetric_key_id *signing_cert_id;/* Message signature. * crypto/asymmetric_keys/pkcs7_trust.c +2
-4@@ -49,8 +49,7 @@ 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->subject, - x509->fingerprint); + key =x509_request_asymmetric_key (trust_keyring, x509->id);if (!IS_ERR (key))/* One of the X.509 certificates in the PKCS#7 message * is apparently the same as one we already trust. @@ -82,8 +81,7 @@ int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,return -ENOKEY ; } - key =x509_request_asymmetric_key (trust_keyring, last->issuer, - last->authority); + key =x509_request_asymmetric_key (trust_keyring, last->authority);if (IS_ERR (key))return PTR_ERR (key) == -ENOMEM ? -ENOMEM : -ENOKEY ; x509 = last;crypto/asymmetric_keys/pkcs7_verify.c +20
-24@@ -131,8 +131,7 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,struct x509_certificate *x509;unsigned certix =1 ; -kenter ("%u,%u,%u" , - sinfo->index, sinfo->raw_serial_size, sinfo->raw_issuer_size); +kenter ("%u" , sinfo->index);for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {/* I'm _assuming_ that the generator of the PKCS#7 message will @@ -140,21 +139,11 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,* PKCS#7 message - but I can't be 100% sure of that. It's * possible this will need element-by-element comparison. */ -if (x509->raw_serial_size != sinfo->raw_serial_size || -memcmp (x509->raw_serial, sinfo->raw_serial, - sinfo->raw_serial_size) !=0 ) +if (!asymmetric_key_id_same (x509->id, sinfo->signing_cert_id))continue ;pr_devel ("Sig %u: Found cert serial match X.509[%u]\n" , sinfo->index, certix); -if (x509->raw_issuer_size != sinfo->raw_issuer_size || -memcmp (x509->raw_issuer, sinfo->raw_issuer, - sinfo->raw_issuer_size) !=0 ) { -pr_warn ("Sig %u: X.509 subject and PKCS#7 issuer don't match\n" , - sinfo->index); -continue ; - } -if (x509->pub->pkey_algo != sinfo->sig.pkey_algo) {pr_warn ("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n" , sinfo->index);@@ -164,8 +153,10 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7, sinfo->signer = x509;return 0 ; } +pr_warn ("Sig %u: Issuing X.509 cert not found (#%*ph)\n" , - sinfo->index, sinfo->raw_serial_size, sinfo->raw_serial); + sinfo->index, + sinfo->signing_cert_id->len, sinfo->signing_cert_id->data);return -ENOKEY ; }@@ -184,7 +175,9 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, p->seen = false;for (;;) { -pr_debug ("verify %s: %s\n" , x509->subject, x509->fingerprint); +pr_debug ("verify %s: %*phN\n" , + x509->subject, + x509->raw_serial_size, x509->raw_serial); x509->seen = true; ret =x509_get_sig_params (x509);if (ret <0 )@@ -192,7 +185,8 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,pr_debug ("- issuer %s\n" , x509->issuer);if (x509->authority) -pr_debug ("- authkeyid %s\n" , x509->authority); +pr_debug ("- authkeyid %*phN\n" , + x509->authority->len, x509->authority->data);if (!x509->authority ||strcmp (x509->subject, x509->issuer) ==0 ) {@@ -218,13 +212,14 @@ 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 %s\n" , x509->authority); +pr_debug ("- want %*phN\n" , + x509->authority->len, x509->authority->data);for (p = pkcs7->certs; p; p = p->next) { -pr_debug ("- cmp [%u] %s\n" , p->index, p->fingerprint); -if (p->raw_subject_size == x509->raw_issuer_size && -strcmp (p->fingerprint, x509->authority) ==0 && -memcmp (p->raw_subject, x509->raw_issuer, - x509->raw_issuer_size) ==0 ) +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)) goto found_issuer; }@@ -233,7 +228,7 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,return 0 ; found_issuer: -pr_debug ("- issuer %s\n" , p->subject); +pr_debug ("- subject %s\n" , p->subject);if (p->seen) {pr_warn ("Sig %u: X.509 chain contains loop\n" , sinfo->index);@@ -304,7 +299,8 @@ int pkcs7_verify(struct pkcs7_message *pkcs7) ret =x509_get_sig_params (x509);if (ret <0 )return ret; -pr_debug ("X.509[%u] %s\n" , n, x509->authority); +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_cert_parser.c +33
-22@@ -46,7 +46,8 @@ void x509_free_certificate(struct x509_certificate *cert)public_key_destroy (cert->pub);kfree (cert->issuer);kfree (cert->subject); -kfree (cert->fingerprint); +kfree (cert->id); +kfree (cert->skid);kfree (cert->authority);kfree (cert->sig.digest);mpi_free (cert->sig.rsa.s);@@ -62,6 +63,7 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) {struct x509_certificate *cert;struct x509_parse_context *ctx; +struct asymmetric_key_id *kid;long ret; ret = -ENOMEM ;@@ -89,6 +91,17 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)if (ret <0 ) goto error_decode; +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (cert->raw_serial, + cert->raw_serial_size, + cert->raw_issuer, + cert->raw_issuer_size); +if (IS_ERR (kid)) { + ret =PTR_ERR (kid); + goto error_decode; + } + cert->id = kid; +kfree (ctx);return cert;@@ -407,36 +420,34 @@ int x509_process_extension(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; +struct asymmetric_key_id *kid;const unsigned char *v = value; -char *f;int i;pr_debug ("Extension: %u\n" , ctx->last_oid);if (ctx->last_oid == OID_subjectKeyIdentifier) {/* Get hold of the key fingerprint */ -if (vlen <3 ) +if (ctx->cert->skid || vlen <3 )return -EBADMSG ;if (v[0 ] !=ASN1_OTS || v[1 ] != vlen -2 )return -EBADMSG ; v +=2 ; vlen -=2 ; - f =kmalloc (vlen *2 +1 ,GFP_KERNEL ); -if (!f) -return -ENOMEM ; -for (i =0 ; i < vlen; i++) -sprintf (f + i *2 ,"%02x" , v[i]); -pr_debug ("fingerprint %s\n" , f); - ctx->cert->fingerprint = f; + kid =asymmetric_key_generate_id (v, vlen, + ctx->cert->raw_subject, + ctx->cert->raw_subject_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + ctx->cert->skid = kid; +pr_debug ("subjkeyid %*phN\n" , kid->len, kid->data);return 0 ; }if (ctx->last_oid == OID_authorityKeyIdentifier) { -size_t key_len; -/* Get hold of the CA key fingerprint */ -if (vlen <5 ) +if (ctx->cert->authority || vlen <5 )return -EBADMSG ;/* Authority Key Identifier must be a Constructed SEQUENCE */ @@ -454,7 +465,7 @@ int x509_process_extension(void *context, size_t hdrlen, v[3] > vlen - 4)return -EBADMSG ; - key_len = v[3 ]; + vlen = v[3 ]; v +=4 ; }else {/* Long Form length */ @@ -476,17 +487,17 @@ int x509_process_extension(void *context, size_t hdrlen, v[sub + 1] > vlen - 4 - sub)return -EBADMSG ; - key_len = v[sub +1 ]; + vlen = v[sub +1 ]; v += (sub +2 ); } - f =kmalloc (key_len *2 +1 ,GFP_KERNEL ); -if (!f) -return -ENOMEM ; -for (i =0 ; i < key_len; i++) -sprintf (f + i *2 ,"%02x" , v[i]); -pr_debug ("authority %s\n" , f); - ctx->cert->authority = f; + kid =asymmetric_key_generate_id (v, vlen, + ctx->cert->raw_issuer, + ctx->cert->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); +pr_debug ("authkeyid %*phN\n" , kid->len, kid->data); + ctx->cert->authority = kid;return 0 ; }crypto/asymmetric_keys/x509_parser.h +3
-2@@ -19,8 +19,9 @@ struct x509_certificate {struct public_key_signature sig;/* Signature parameters */ char *issuer;/* Name of certificate issuer */ char *subject;/* Name of certificate subject */ -char *fingerprint;/* Key fingerprint as hex */ -char *authority;/* Authority key fingerprint as hex */ +struct asymmetric_key_id *id;/* Issuer + serial number */ +struct asymmetric_key_id *skid;/* Subject key identifier */ +struct asymmetric_key_id *authority;/* Authority key identifier */ struct tm valid_from;struct tm valid_to;const void *tbs;/* Signed data */ crypto/asymmetric_keys/x509_public_key.c +51
-38@@ -25,7 +25,7 @@#include "x509_parser.h" static bool use_builtin_keys; -static char *ca_keyid; +static struct asymmetric_key_id *ca_keyid;#ifndef MODULE static int __init ca_keys_setup (char *str)@@ -33,10 +33,16 @@ static int __init ca_keys_setup(char *str)if (!str)/* default system keyring */ return 1 ; -if (strncmp (str,"id:" ,3 ) ==0 ) - ca_keyid = str;/* owner key 'id:xxxxxx' */ -else if (strcmp (str,"builtin" ) ==0 ) +if (strncmp (str,"id:" ,3 ) ==0 ) { +struct asymmetric_key_id *p; + p =asymmetric_key_hex_to_key_id (str); +if (p ==ERR_PTR (-EINVAL )) +pr_err ("Unparsable hex string in ca_keys\n" ); +else if (!IS_ERR (p)) + ca_keyid = p;/* owner key 'id:xxxxxx' */ + }else if (strcmp (str,"builtin" ) ==0 ) { use_builtin_keys = true; + }return 1 ; }@@ -46,31 +52,28 @@ __setup("ca_keys=", ca_keys_setup);/** * x509_request_asymmetric_key - Request a key by X.509 certificate params. * @keyring: The keys to search. -* @subject: The name of the subject to whom the key belongs. -* @key_id: The subject key ID as a hex string. +* @kid: The key ID. * * Find a key in the given keyring by subject name and key ID. These might, * for instance, be the issuer name and the authority key ID of an X.509 * certificate that needs to be verified. */ struct key *x509_request_asymmetric_key (struct key *keyring, -const char *subject, -const char *key_id) +const struct asymmetric_key_id *kid) {key_ref_t key; -size_t subject_len =strlen (subject), key_id_len =strlen (key_id); -char *id; +char *id, *p; -/* Construct an identifier "<subjname>:<keyid>". */ - id =kmalloc (subject_len +2 + key_id_len +1 ,GFP_KERNEL ); +/* Construct an identifier "id:<keyid>". */ + p = id =kmalloc (2 +1 + kid->len *2 +1 ,GFP_KERNEL );if (!id)return ERR_PTR (-ENOMEM ); -memcpy (id, subject, subject_len); - id[subject_len +0 ] =':' ; - id[subject_len +1 ] =' ' ; -memcpy (id + subject_len +2 , key_id, key_id_len); - id[subject_len +2 + key_id_len] =0 ; + *p++ ='i' ; + *p++ ='d' ; + *p++ =':' ; + p =bin2hex (p, kid->data, kid->len); + *p =0 ;pr_debug ("Look up: \"%s\"\n" , id);@@ -195,11 +198,10 @@ static int x509_validate_trust(struct x509_certificate *cert,if (!trust_keyring)return -EOPNOTSUPP ; -if (ca_keyid && !asymmetric_keyid_match (cert->authority, ca_keyid)) +if (ca_keyid && !asymmetric_key_id_same (cert->authority, ca_keyid))return -EPERM ; - key =x509_request_asymmetric_key (trust_keyring, - cert->issuer, cert->authority); + key =x509_request_asymmetric_key (trust_keyring, cert->authority);if (!IS_ERR (key)) {if (!use_builtin_keys ||test_bit (KEY_FLAG_BUILTIN , &key->flags))@@ -214,9 +216,11 @@ static int x509_validate_trust(struct x509_certificate *cert,*/ static int x509_key_preparse (struct key_preparsed_payload *prep) { +struct asymmetric_key_ids *kids;struct x509_certificate *cert; +const char *q;size_t srlen, sulen; -char *desc =NULL ; +char *desc =NULL , *p;int ret; cert =x509_cert_parse (prep->data, prep->datalen);@@ -249,19 +253,12 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) pkey_algo_name[cert->sig.pkey_algo], hash_algo_name[cert->sig.pkey_hash_algo]); -if (!cert->fingerprint) { -pr_warn ("Cert for '%s' must have a SubjKeyId extension\n" , - cert->subject); - ret = -EKEYREJECTED ; - goto error_free_cert; - } - cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; cert->pub->id_type =PKEY_ID_X509 ;/* Check the signature on the key if it appears to be self-signed */ if (!cert->authority || -strcmp (cert->fingerprint, cert->authority) ==0 ) { +asymmetric_key_id_same (cert->skid, cert->authority)) { ret =x509_check_signature (cert->pub, cert);/* self-signed */ if (ret <0 ) goto error_free_cert;@@ -273,31 +270,47 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)/* Propose a description */ sulen =strlen (cert->subject); - srlen =strlen (cert->fingerprint); + srlen = cert->raw_serial_size; + q = cert->raw_serial; +if (srlen >1 && *q ==0 ) { + srlen--; + q++; + } + ret = -ENOMEM ; - desc =kmalloc (sulen +2 + srlen +1 ,GFP_KERNEL ); + desc =kmalloc (sulen +2 + srlen *2 +1 ,GFP_KERNEL );if (!desc) goto error_free_cert; -memcpy (desc, cert->subject, sulen); - desc[sulen] =':' ; - desc[sulen +1 ] =' ' ; -memcpy (desc + sulen +2 , cert->fingerprint, srlen); - desc[sulen +2 + srlen] =0 ; + p =memcpy (desc, cert->subject, sulen); + p += sulen; + *p++ =':' ; + *p++ =' ' ; + p =bin2hex (p, q, srlen); + *p =0 ; + + kids =kmalloc (sizeof (struct asymmetric_key_ids ),GFP_KERNEL ); +if (!kids) + goto error_free_desc; + kids->id[0 ] = cert->id; + kids->id[1 ] = cert->skid;/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner); prep->type_data[0 ] = &public_key_subtype; - prep->type_data[1 ] = cert->fingerprint; + prep->type_data[1 ] = kids; prep->payload[0 ] = cert->pub; prep->description = desc; prep->quotalen =100 ;/* We've finished with the certificate */ cert->pub =NULL ; - cert->fingerprint =NULL ; + cert->id =NULL ; + cert->skid =NULL ; desc =NULL ; ret =0 ; +error_free_desc: +kfree (desc); error_free_cert:x509_free_certificate (cert);return ret;include/crypto/public_key.h +3
-3@@ -15,6 +15,7 @@#define _LINUX_PUBLIC_KEY_H#include <linux/mpi.h> +#include <keys/asymmetric-type.h> #include <crypto/hash_info.h> enum pkey_algo {@@ -98,8 +99,8 @@ struct key;extern int verify_signature (const struct key *key,const struct public_key_signature *sig); +struct asymmetric_key_id ;extern struct key *x509_request_asymmetric_key (struct key *keyring, -const char *issuer, -const char *key_id); +const struct asymmetric_key_id *kid);#endif /* _LINUX_PUBLIC_KEY_H */ --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
Re: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
Quoted 20 lines · Dmitry Kasatkin
Hi David,
I just took latest #next branch from James's security tree which includes latest KEYs patches and noticed following:
[ 9.812332] Request for unknown module key 'Magrathea: Glaciersigning key:
926305d6dda66f47139eb4e3cb25a6adef527f08' err -11Also I noticed that output of 'keyctl show' and 'cat
/proc/keys' output also has changed in respect of certificate ids..Those ids does not look any close to my kernel X509 X509v3 Subject Key Identifier, which is:
92:63:05:D6:DD:A6:6F:47:13:9E:B4:E3:CB:25:A6:AD:EF:52:7F:08proc/keys shows
symmetri Magrathea: Glacier signing key:
d9e2e4c6951f1e83: X509.RSA6865612e68326732[]
Quoted 6 lines
Very different ids..
How could I match certificate now?
Module verification code cannot find needed key..- Dmitry
Hehe. Also now I get kernel Oops in asymmetric_key_id_same...
-------------------------
[ 132.816522] BUG: unable to handle kernel paging request at ffffffffffffffea [ 132.819902] IP: [<ffffffff812bfc20>] asymmetric_key_id_same+0x14/0x36 [ 132.820302] PGD 1a12067 PUD 1a14067 PMD 0 [ 132.820302] Oops: 0000 [#1] SMP [ 132.820302] Modules linked in: bridge(E) stp(E) llc(E) evdev(E) serio_raw(E) i2c_piix4(E) button(E) fuse(E) [ 132.820302] CPU: 0 PID: 2993 Comm: cat Tainted: G E 3.16.0-kds+ #2847 [ 132.820302] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011 [ 132.820302] task: ffff88004249a430 ti: ffff880056640000 task.ti: ffff880056640000 [ 132.820302] RIP: 0010:[<ffffffff812bfc20>] [<ffffffff812bfc20>] asymmetric_key_id_same+0x14/0x36 [ 132.820302] RSP: 0018:ffff880056643930 EFLAGS: 00010246 [ 132.820302] RAX: 0000000000000000 RBX: ffffffffffffffea RCX: ffff880056643ae0 [ 132.820302] RDX: 000000000000005e RSI: ffffffffffffffea RDI: ffff88005bac9300 [ 132.820302] RBP: ffff880056643948 R08: 0000000000000003 R09: 00000007504aa01a [ 132.820302] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88005d68ca40 [ 132.820302] R13: 0000000000000101 R14: 0000000000000000 R15: ffff88005bac5280 [ 132.820302] FS: 00007f67a153c740(0000) GS:ffff88005da00000(0000) knlGS:0000000000000000 [ 132.820302] CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b [ 132.820302] CR2: ffffffffffffffea CR3: 000000002e663000 CR4: 00000000000006f0 [ 132.820302] Stack: [ 132.820302] ffffffff812bfc66 ffff880056643ae0 ffff88005bac5280 ffff880056643958 [ 132.820302] ffffffff812bfc9d ffff880056643980 ffffffff812971d9 ffff88005ce930c1 [ 132.820302] ffff88005ce930c0 0000000000000000 ffff8800566439c8 ffffffff812fb753 [ 132.820302] Call Trace: [ 132.820302] [<ffffffff812bfc66>] ? asymmetric_match_key_ids+0x24/0x42 [ 132.820302] [<ffffffff812bfc9d>] asymmetric_key_cmp+0x19/0x1b [ 132.820302] [<ffffffff812971d9>] keyring_search_iterator+0x74/0xd7 [ 132.820302] [<ffffffff812fb753>] assoc_array_subtree_iterate+0x67/0xd2 [ 132.820302] [<ffffffff81297165>] ? key_default_cmp+0x20/0x20 [ 132.820302] [<ffffffff812fbaa1>] assoc_array_iterate+0x19/0x1e [ 132.820302] [<ffffffff81297332>] search_nested_keyrings+0xf6/0x2b6 [ 132.820302] [<ffffffff810728da>] ? sched_clock_cpu+0x91/0xa2 [ 132.820302] [<ffffffff810860d2>] ? mark_held_locks+0x58/0x6e [ 132.820302] [<ffffffff810a137d>] ? current_kernel_time+0x77/0xb8 [ 132.820302] [<ffffffff81297871>] keyring_search_aux+0xe1/0x14c [ 132.820302] [<ffffffff812977fc>] ? keyring_search_aux+0x6c/0x14c [ 132.820302] [<ffffffff8129796b>] keyring_search+0x8f/0xb6 [ 132.820302] [<ffffffff812bfc84>] ? asymmetric_match_key_ids+0x42/0x42 [ 132.820302] [<ffffffff81297165>] ? key_default_cmp+0x20/0x20 [ 132.820302] [<ffffffff812ab9e3>] asymmetric_verify+0xa4/0x214 [ 132.820302] [<ffffffff812ab90e>] integrity_digsig_verify+0xb1/0xe2 [ 132.820302] [<ffffffff812abe41>] ? evm_verifyxattr+0x6a/0x7a [ 132.820302] [<ffffffff812b0390>] ima_appraise_measurement+0x160/0x370 [ 132.820302] [<ffffffff81161db2>] ? d_absolute_path+0x5b/0x7a [ 132.820302] [<ffffffff812ada30>] process_measurement+0x322/0x404
Quoted 876 lines · David Howells
Quoted 866 lines
Make use of the new match string preparsing to overhaul key identification when searching for asymmetric keys. The following changes are made:
(1) Use the previously created
asymmetric_key_idstruct to hold the following key IDs derived from the X.509 certificate or PKCS#7 message:id: serial number + issuer
skid: subjKeyId + subject
authority: authKeyId + issuer(2) Replace the hex fingerprint attached to key->type_data[1] with an
asymmetric_key_idsstruct containing the id and the skid (if present).(3) Make the
asymmetric_typematch data preparse select one of two searches:(a) An iterative search for the key ID given if prefixed with "id:". The prefix is expected to be followed by a hex string giving the ID to search for. The criterion key ID is checked against all key IDs recorded on the key.
(b) A direct search if the key ID is not prefixed with "id:". This will look for an exact match on the key description.
(4) Make
x509_request_asymmetric_key()take a key ID. This is then converted into "id:<hex>" and passed intokeyring_search()where match preparsing will turn it back into a binary ID.(5) X.509 certificate verification then takes the authority key ID and looks up a key that matches it to find the public key for the certificate signature.
(6) PKCS#7 certificate verification then takes the id key ID and looks up a key that matches it to find the public key for the signed information block signature.
Additional changes:
(1) Multiple subjKeyId and authKeyId values on an X.509 certificate cause the cert to be rejected with
-EBADMSG.(2) The 'fingerprint' ID is gone. This was primarily intended to convey PGP public key fingerprints. If PGP is supported in future, this should generate a key ID that carries the fingerprint.
(3) Th ca_keyid= kernel command line option is now converted to a key ID and used to match the authority key ID. Possibly this should only match the actual authKeyId part and not the issuer as well.
Signed-off-by David Howells <[email protected]>---
crypto/asymmetric_keys/asymmetric_keys.h | 4 - crypto/asymmetric_keys/asymmetric_type.c | 133 ++++++++++++----------------- crypto/asymmetric_keys/pkcs7_parser.c | 38 ++++++-- crypto/asymmetric_keys/pkcs7_parser.h | 5 - crypto/asymmetric_keys/pkcs7_trust.c | 6 - crypto/asymmetric_keys/pkcs7_verify.c | 44 ++++------ crypto/asymmetric_keys/x509_cert_parser.c | 55 +++++++----- crypto/asymmetric_keys/x509_parser.h | 5 + crypto/asymmetric_keys/x509_public_key.c | 89 +++++++++++-------- include/crypto/public_key.h | 5 + 10 files changed, 198 insertions(+), 186 deletions(-)crypto/asymmetric_keys/asymmetric_keys.h +2
-2@@ -9,13 +9,13 @@* 2 of the Licence, or (at your option) any later version. */ -int asymmetric_keyid_match (const char *kid,const char *id);extern bool asymmetric_match_key_ids (const struct asymmetric_key_ids *kids,const struct asymmetric_key_id *match_id);extern struct asymmetric_key_id *asymmetric_key_hex_to_key_id (const char *id); -static inline const char *asymmetric_key_id (const struct key *key) +static inline +const struct asymmetric_key_ids *asymmetric_key_ids (const struct key *key) {return key->type_data.p[1 ]; }crypto/asymmetric_keys/asymmetric_type.c +56
-77@@ -105,76 +105,15 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id) }/* -* Match asymmetric key id with partial match -* @id: key id to match in a form "id:<id>" -*/ -int asymmetric_keyid_match (const char *kid,const char *id) -{ -size_t idlen, kidlen; - -if (!kid || !id) -return 0 ; - -/* make it possible to use id as in the request: "id:<id>" */ -if (strncmp (id,"id:" ,3 ) ==0 ) - id +=3 ; - -/* Anything after here requires a partial match on the ID string */ - idlen =strlen (id); - kidlen =strlen (kid); -if (idlen > kidlen) -return 0 ; - - kid += kidlen - idlen; -if (strcasecmp (id, kid) !=0 ) -return 0 ; - -return 1 ; -} -EXPORT_SYMBOL_GPL (asymmetric_keyid_match); - -/* -* Match asymmetric keys on (part of) their name -* We have some shorthand methods for matching keys. We allow: -* -* "<desc>" - request a key by description -* "id:<id>" - request a key matching the ID -* "<subtype>:<id>" - request a key of a subtype +* Match asymmetric keys by ID. */ static bool asymmetric_key_cmp (const struct key *key,const struct key_match_data *match_data) { -const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); -const char *description = match_data->raw_data; -const char *spec = description; -const char *id; -ptrdiff_t speclen; - -if (!subtype || !spec || !*spec) -return 0 ; - -/* See if the full key description matches as is */ -if (key->description &&strcmp (key->description, description) ==0 ) -return 1 ; - -/* All tests from here on break the criterion description into a -* specifier, a colon and then an identifier. -*/ - id =strchr (spec,':' ); -if (!id) -return 0 ; - - speclen = id - spec; - id++; - -if (speclen ==2 &&memcmp (spec,"id" ,2 ) ==0 ) -return asymmetric_keyid_match (asymmetric_key_id (key), id); +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *match_id = match_data->preparsed; -if (speclen == subtype->name_len && -memcmp (spec, subtype->name, speclen) ==0 ) -return 1 ; - -return 0 ; +return asymmetric_match_key_ids (kids, match_id); }/* @@ -191,8 +130,30 @@ static bool asymmetric_key_cmp(const struct key *key,*/ static int asymmetric_key_match_preparse (struct key_match_data *match_data) { - match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +struct asymmetric_key_id *match_id; +const char *spec = match_data->raw_data; +const char *id; + +if (!spec || !*spec) +return -EINVAL ; +if (spec[0 ] =='i' && + spec[1 ] =='d' && + spec[2 ] ==':' ) { + id = spec +3 ; + }else { + goto default_match; + } + + match_id =asymmetric_key_hex_to_key_id (id); +if (!match_id) +return -ENOMEM ; + + match_data->preparsed = match_id; match_data->cmp = asymmetric_key_cmp; + match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +return 0 ; + +default_match:return 0 ; }@@ -201,6 +162,7 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data)*/ static void asymmetric_key_match_free (struct key_match_data *match_data) { +kfree (match_data->preparsed); }/* @@ -209,8 +171,10 @@ static void asymmetric_key_match_free(struct key_match_data *match_data)static void asymmetric_key_describe (const struct key *key,struct seq_file *m) {const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); -const char *kid =asymmetric_key_id (key); -size_t n; +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *kid; +const unsigned char *p; +int n;seq_puts (m, key->description);@@ -218,13 +182,16 @@ static void asymmetric_key_describe(const struct key *key, struct seq_file *m)seq_puts (m,": " ); subtype->describe (key, m); -if (kid) { +if (kids && kids->id[0 ]) { + kid = kids->id[0 ];seq_putc (m,' ' ); - n =strlen (kid); -if (n <=8 ) -seq_puts (m, kid); -else -seq_puts (m, kid + n -8 ); + n = kid->len; + p = kid->data; +if (n >8 ) { + p += n -8 ; + n =8 ; + } +seq_printf (m,"%*phN" , n, p); }seq_puts (m," [" );@@ -275,6 +242,7 @@ static int asymmetric_key_preparse(struct key_preparsed_payload *prep)static void asymmetric_key_free_preparse (struct key_preparsed_payload *prep) {struct asymmetric_key_subtype *subtype = prep->type_data[0 ]; +struct asymmetric_key_ids *kids = prep->type_data[1 ];pr_devel ("==>%s()\n" , __func__);@@ -282,7 +250,11 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep) subtype->destroy (prep->payload[0 ]);module_put (subtype->owner); } -kfree (prep->type_data[1 ]); +if (kids) { +kfree (kids->id[0 ]); +kfree (kids->id[1 ]); +kfree (kids); + }kfree (prep->description); }@@ -292,13 +264,20 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)static void asymmetric_key_destroy (struct key *key) {struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); +struct asymmetric_key_ids *kids = key->type_data.p[1 ]; +if (subtype) { subtype->destroy (key->payload.data);module_put (subtype->owner); key->type_data.p[0 ] =NULL ; } -kfree (key->type_data.p[1 ]); - key->type_data.p[1 ] =NULL ; + +if (kids) { +kfree (kids->id[0 ]); +kfree (kids->id[1 ]); +kfree (kids); + key->type_data.p[1 ] =NULL ; + } }struct key_type key_type_asymmetric = {crypto/asymmetric_keys/pkcs7_parser.c +27
-11@@ -29,6 +29,10 @@ struct pkcs7_parse_context {enum OID last_oid;/* Last OID encountered */ unsigned x509_index;unsigned sinfo_index; +const void *raw_serial; +unsigned raw_serial_size; +unsigned raw_issuer_size; +const void *raw_issuer; };/* @@ -39,6 +43,7 @@ static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)if (sinfo) {mpi_free (sinfo->sig.mpi[0 ]);kfree (sinfo->sig.digest); +kfree (sinfo->signing_cert_id);kfree (sinfo); } }@@ -256,10 +261,10 @@ int pkcs7_extract_cert(void *context, size_t hdrlen,if (IS_ERR (x509))return PTR_ERR (x509); -pr_debug ("Got cert for %s\n" , x509->subject); -pr_debug ("- fingerprint %s\n" , x509->fingerprint); - x509->index = ++ctx->x509_index; +pr_debug ("Got cert %u for %s\n" , x509->index, x509->subject); +pr_debug ("- fingerprint %*phN\n" , x509->id->len, x509->id->data); + *ctx->ppcerts = x509; ctx->ppcerts = &x509->next;return 0 ;@@ -348,8 +353,8 @@ int pkcs7_sig_note_serial(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_serial = value; - ctx->sinfo->raw_serial_size = vlen; + ctx->raw_serial = value; + ctx->raw_serial_size = vlen;return 0 ; }@@ -361,8 +366,8 @@ int pkcs7_sig_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_issuer = value; - ctx->sinfo->raw_issuer_size = vlen; + ctx->raw_issuer = value; + ctx->raw_issuer_size = vlen;return 0 ; }@@ -395,10 +400,21 @@ int pkcs7_note_signed_info(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - - ctx->sinfo->index = ++ctx->sinfo_index; - *ctx->ppsinfo = ctx->sinfo; - ctx->ppsinfo = &ctx->sinfo->next; +struct pkcs7_signed_info *sinfo = ctx->sinfo; +struct asymmetric_key_id *kid; + +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (ctx->raw_serial, + ctx->raw_serial_size, + ctx->raw_issuer, + ctx->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + + sinfo->signing_cert_id = kid; + sinfo->index = ++ctx->sinfo_index; + *ctx->ppsinfo = sinfo; + ctx->ppsinfo = &sinfo->next; ctx->sinfo =kzalloc (sizeof (struct pkcs7_signed_info ),GFP_KERNEL );if (!ctx->sinfo)return -ENOMEM ;crypto/asymmetric_keys/pkcs7_parser.h +1
-4@@ -33,10 +33,7 @@ struct pkcs7_signed_info {const void *authattrs;/* Issuing cert serial number and issuer's name */ -const void *raw_serial; -unsigned raw_serial_size; -unsigned raw_issuer_size; -const void *raw_issuer; +struct asymmetric_key_id *signing_cert_id;/* Message signature. * crypto/asymmetric_keys/pkcs7_trust.c +2
-4@@ -49,8 +49,7 @@ 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->subject, - x509->fingerprint); + key =x509_request_asymmetric_key (trust_keyring, x509->id);if (!IS_ERR (key))/* One of the X.509 certificates in the PKCS#7 message * is apparently the same as one we already trust. @@ -82,8 +81,7 @@ int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,return -ENOKEY ; } - key =x509_request_asymmetric_key (trust_keyring, last->issuer, - last->authority); + key =x509_request_asymmetric_key (trust_keyring, last->authority);if (IS_ERR (key))return PTR_ERR (key) == -ENOMEM ? -ENOMEM : -ENOKEY ; x509 = last;crypto/asymmetric_keys/pkcs7_verify.c +20
-24@@ -131,8 +131,7 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,struct x509_certificate *x509;unsigned certix =1 ; -kenter ("%u,%u,%u" , - sinfo->index, sinfo->raw_serial_size, sinfo->raw_issuer_size); +kenter ("%u" , sinfo->index);for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {/* I'm _assuming_ that the generator of the PKCS#7 message will @@ -140,21 +139,11 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,* PKCS#7 message - but I can't be 100% sure of that. It's * possible this will need element-by-element comparison. */ -if (x509->raw_serial_size != sinfo->raw_serial_size || -memcmp (x509->raw_serial, sinfo->raw_serial, - sinfo->raw_serial_size) !=0 ) +if (!asymmetric_key_id_same (x509->id, sinfo->signing_cert_id))continue ;pr_devel ("Sig %u: Found cert serial match X.509[%u]\n" , sinfo->index, certix); -if (x509->raw_issuer_size != sinfo->raw_issuer_size || -memcmp (x509->raw_issuer, sinfo->raw_issuer, - sinfo->raw_issuer_size) !=0 ) { -pr_warn ("Sig %u: X.509 subject and PKCS#7 issuer don't match\n" , - sinfo->index); -continue ; - } -if (x509->pub->pkey_algo != sinfo->sig.pkey_algo) {pr_warn ("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n" , sinfo->index);@@ -164,8 +153,10 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7, sinfo->signer = x509;return 0 ; } +pr_warn ("Sig %u: Issuing X.509 cert not found (#%*ph)\n" , - sinfo->index, sinfo->raw_serial_size, sinfo->raw_serial); + sinfo->index, + sinfo->signing_cert_id->len, sinfo->signing_cert_id->data);return -ENOKEY ; }@@ -184,7 +175,9 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, p->seen = false;for (;;) { -pr_debug ("verify %s: %s\n" , x509->subject, x509->fingerprint); +pr_debug ("verify %s: %*phN\n" , + x509->subject, + x509->raw_serial_size, x509->raw_serial); x509->seen = true; ret =x509_get_sig_params (x509);if (ret <0 )@@ -192,7 +185,8 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,pr_debug ("- issuer %s\n" , x509->issuer);if (x509->authority) -pr_debug ("- authkeyid %s\n" , x509->authority); +pr_debug ("- authkeyid %*phN\n" , + x509->authority->len, x509->authority->data);if (!x509->authority ||strcmp (x509->subject, x509->issuer) ==0 ) {@@ -218,13 +212,14 @@ 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 %s\n" , x509->authority); +pr_debug ("- want %*phN\n" , + x509->authority->len, x509->authority->data);for (p = pkcs7->certs; p; p = p->next) { -pr_debug ("- cmp [%u] %s\n" , p->index, p->fingerprint); -if (p->raw_subject_size == x509->raw_issuer_size && -strcmp (p->fingerprint, x509->authority) ==0 && -memcmp (p->raw_subject, x509->raw_issuer, - x509->raw_issuer_size) ==0 ) +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)) goto found_issuer; }@@ -233,7 +228,7 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,return 0 ; found_issuer: -pr_debug ("- issuer %s\n" , p->subject); +pr_debug ("- subject %s\n" , p->subject);if (p->seen) {pr_warn ("Sig %u: X.509 chain contains loop\n" , sinfo->index);@@ -304,7 +299,8 @@ int pkcs7_verify(struct pkcs7_message *pkcs7) ret =x509_get_sig_params (x509);if (ret <0 )return ret; -pr_debug ("X.509[%u] %s\n" , n, x509->authority); +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_cert_parser.c +33
-22@@ -46,7 +46,8 @@ void x509_free_certificate(struct x509_certificate *cert)public_key_destroy (cert->pub);kfree (cert->issuer);kfree (cert->subject); -kfree (cert->fingerprint); +kfree (cert->id); +kfree (cert->skid);kfree (cert->authority);kfree (cert->sig.digest);mpi_free (cert->sig.rsa.s);@@ -62,6 +63,7 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) {struct x509_certificate *cert;struct x509_parse_context *ctx; +struct asymmetric_key_id *kid;long ret; ret = -ENOMEM ;@@ -89,6 +91,17 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)if (ret <0 ) goto error_decode; +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (cert->raw_serial, + cert->raw_serial_size, + cert->raw_issuer, + cert->raw_issuer_size); +if (IS_ERR (kid)) { + ret =PTR_ERR (kid); + goto error_decode; + } + cert->id = kid; +kfree (ctx);return cert;@@ -407,36 +420,34 @@ int x509_process_extension(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; +struct asymmetric_key_id *kid;const unsigned char *v = value; -char *f;int i;pr_debug ("Extension: %u\n" , ctx->last_oid);if (ctx->last_oid == OID_subjectKeyIdentifier) {/* Get hold of the key fingerprint */ -if (vlen <3 ) +if (ctx->cert->skid || vlen <3 )return -EBADMSG ;if (v[0 ] !=ASN1_OTS || v[1 ] != vlen -2 )return -EBADMSG ; v +=2 ; vlen -=2 ; - f =kmalloc (vlen *2 +1 ,GFP_KERNEL ); -if (!f) -return -ENOMEM ; -for (i =0 ; i < vlen; i++) -sprintf (f + i *2 ,"%02x" , v[i]); -pr_debug ("fingerprint %s\n" , f); - ctx->cert->fingerprint = f; + kid =asymmetric_key_generate_id (v, vlen, + ctx->cert->raw_subject, + ctx->cert->raw_subject_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + ctx->cert->skid = kid; +pr_debug ("subjkeyid %*phN\n" , kid->len, kid->data);return 0 ; }if (ctx->last_oid == OID_authorityKeyIdentifier) { -size_t key_len; -/* Get hold of the CA key fingerprint */ -if (vlen <5 ) +if (ctx->cert->authority || vlen <5 )return -EBADMSG ;/* Authority Key Identifier must be a Constructed SEQUENCE */ @@ -454,7 +465,7 @@ int x509_process_extension(void *context, size_t hdrlen, v[3] > vlen - 4)return -EBADMSG ; - key_len = v[3 ]; + vlen = v[3 ]; v +=4 ; }else {/* Long Form length */ @@ -476,17 +487,17 @@ int x509_process_extension(void *context, size_t hdrlen, v[sub + 1] > vlen - 4 - sub)return -EBADMSG ; - key_len = v[sub +1 ]; + vlen = v[sub +1 ]; v += (sub +2 ); } - f =kmalloc (key_len *2 +1 ,GFP_KERNEL ); -if (!f) -return -ENOMEM ; -for (i =0 ; i < key_len; i++) -sprintf (f + i *2 ,"%02x" , v[i]); -pr_debug ("authority %s\n" , f); - ctx->cert->authority = f; + kid =asymmetric_key_generate_id (v, vlen, + ctx->cert->raw_issuer, + ctx->cert->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); +pr_debug ("authkeyid %*phN\n" , kid->len, kid->data); + ctx->cert->authority = kid;return 0 ; }crypto/asymmetric_keys/x509_parser.h +3
-2@@ -19,8 +19,9 @@ struct x509_certificate {struct public_key_signature sig;/* Signature parameters */ char *issuer;/* Name of certificate issuer */ char *subject;/* Name of certificate subject */ -char *fingerprint;/* Key fingerprint as hex */ -char *authority;/* Authority key fingerprint as hex */ +struct asymmetric_key_id *id;/* Issuer + serial number */ +struct asymmetric_key_id *skid;/* Subject key identifier */ +struct asymmetric_key_id *authority;/* Authority key identifier */ struct tm valid_from;struct tm valid_to;const void *tbs;/* Signed data */ crypto/asymmetric_keys/x509_public_key.c +51
-38@@ -25,7 +25,7 @@#include "x509_parser.h" static bool use_builtin_keys; -static char *ca_keyid; +static struct asymmetric_key_id *ca_keyid;#ifndef MODULE static int __init ca_keys_setup (char *str)@@ -33,10 +33,16 @@ static int __init ca_keys_setup(char *str)if (!str)/* default system keyring */ return 1 ; -if (strncmp (str,"id:" ,3 ) ==0 ) - ca_keyid = str;/* owner key 'id:xxxxxx' */ -else if (strcmp (str,"builtin" ) ==0 ) +if (strncmp (str,"id:" ,3 ) ==0 ) { +struct asymmetric_key_id *p; + p =asymmetric_key_hex_to_key_id (str); +if (p ==ERR_PTR (-EINVAL )) +pr_err ("Unparsable hex string in ca_keys\n" ); +else if (!IS_ERR (p)) + ca_keyid = p;/* owner key 'id:xxxxxx' */ + }else if (strcmp (str,"builtin" ) ==0 ) { use_builtin_keys = true; + }return 1 ; }@@ -46,31 +52,28 @@ __setup("ca_keys=", ca_keys_setup);/** * x509_request_asymmetric_key - Request a key by X.509 certificate params. * @keyring: The keys to search. -* @subject: The name of the subject to whom the key belongs. -* @key_id: The subject key ID as a hex string. +* @kid: The key ID. * * Find a key in the given keyring by subject name and key ID. These might, * for instance, be the issuer name and the authority key ID of an X.509 * certificate that needs to be verified. */ struct key *x509_request_asymmetric_key (struct key *keyring, -const char *subject, -const char *key_id) +const struct asymmetric_key_id *kid) {key_ref_t key; -size_t subject_len =strlen (subject), key_id_len =strlen (key_id); -char *id; +char *id, *p; -/* Construct an identifier "<subjname>:<keyid>". */ - id =kmalloc (subject_len +2 + key_id_len +1 ,GFP_KERNEL ); +/* Construct an identifier "id:<keyid>". */ + p = id =kmalloc (2 +1 + kid->len *2 +1 ,GFP_KERNEL );if (!id)return ERR_PTR (-ENOMEM ); -memcpy (id, subject, subject_len); - id[subject_len +0 ] =':' ; - id[subject_len +1 ] =' ' ; -memcpy (id + subject_len +2 , key_id, key_id_len); - id[subject_len +2 + key_id_len] =0 ; + *p++ ='i' ; + *p++ ='d' ; + *p++ =':' ; + p =bin2hex (p, kid->data, kid->len); + *p =0 ;pr_debug ("Look up: \"%s\"\n" , id);@@ -195,11 +198,10 @@ static int x509_validate_trust(struct x509_certificate *cert,if (!trust_keyring)return -EOPNOTSUPP ; -if (ca_keyid && !asymmetric_keyid_match (cert->authority, ca_keyid)) +if (ca_keyid && !asymmetric_key_id_same (cert->authority, ca_keyid))return -EPERM ; - key =x509_request_asymmetric_key (trust_keyring, - cert->issuer, cert->authority); + key =x509_request_asymmetric_key (trust_keyring, cert->authority);if (!IS_ERR (key)) {if (!use_builtin_keys ||test_bit (KEY_FLAG_BUILTIN , &key->flags))@@ -214,9 +216,11 @@ static int x509_validate_trust(struct x509_certificate *cert,*/ static int x509_key_preparse (struct key_preparsed_payload *prep) { +struct asymmetric_key_ids *kids;struct x509_certificate *cert; +const char *q;size_t srlen, sulen; -char *desc =NULL ; +char *desc =NULL , *p;int ret; cert =x509_cert_parse (prep->data, prep->datalen);@@ -249,19 +253,12 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) pkey_algo_name[cert->sig.pkey_algo], hash_algo_name[cert->sig.pkey_hash_algo]); -if (!cert->fingerprint) { -pr_warn ("Cert for '%s' must have a SubjKeyId extension\n" , - cert->subject); - ret = -EKEYREJECTED ; - goto error_free_cert; - } - cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; cert->pub->id_type =PKEY_ID_X509 ;/* Check the signature on the key if it appears to be self-signed */ if (!cert->authority || -strcmp (cert->fingerprint, cert->authority) ==0 ) { +asymmetric_key_id_same (cert->skid, cert->authority)) { ret =x509_check_signature (cert->pub, cert);/* self-signed */ if (ret <0 ) goto error_free_cert;@@ -273,31 +270,47 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)/* Propose a description */ sulen =strlen (cert->subject); - srlen =strlen (cert->fingerprint); + srlen = cert->raw_serial_size; + q = cert->raw_serial; +if (srlen >1 && *q ==0 ) { + srlen--; + q++; + } + ret = -ENOMEM ; - desc =kmalloc (sulen +2 + srlen +1 ,GFP_KERNEL ); + desc =kmalloc (sulen +2 + srlen *2 +1 ,GFP_KERNEL );if (!desc) goto error_free_cert; -memcpy (desc, cert->subject, sulen); - desc[sulen] =':' ; - desc[sulen +1 ] =' ' ; -memcpy (desc + sulen +2 , cert->fingerprint, srlen); - desc[sulen +2 + srlen] =0 ; + p =memcpy (desc, cert->subject, sulen); + p += sulen; + *p++ =':' ; + *p++ =' ' ; + p =bin2hex (p, q, srlen); + *p =0 ; + + kids =kmalloc (sizeof (struct asymmetric_key_ids ),GFP_KERNEL ); +if (!kids) + goto error_free_desc; + kids->id[0 ] = cert->id; + kids->id[1 ] = cert->skid;/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner); prep->type_data[0 ] = &public_key_subtype; - prep->type_data[1 ] = cert->fingerprint; + prep->type_data[1 ] = kids; prep->payload[0 ] = cert->pub; prep->description = desc; prep->quotalen =100 ;/* We've finished with the certificate */ cert->pub =NULL ; - cert->fingerprint =NULL ; + cert->id =NULL ; + cert->skid =NULL ; desc =NULL ; ret =0 ; +error_free_desc: +kfree (desc); error_free_cert:x509_free_certificate (cert);return ret;include/crypto/public_key.h +3
-3@@ -15,6 +15,7 @@#define _LINUX_PUBLIC_KEY_H#include <linux/mpi.h> +#include <keys/asymmetric-type.h> #include <crypto/hash_info.h> enum pkey_algo {@@ -98,8 +99,8 @@ struct key;extern int verify_signature (const struct key *key,const struct public_key_signature *sig); +struct asymmetric_key_id ;extern struct key *x509_request_asymmetric_key (struct key *keyring, -const char *issuer, -const char *key_id); +const struct asymmetric_key_id *kid);#endif /* _LINUX_PUBLIC_KEY_H */ --Quoted 4 lines
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--
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
Re: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
Quoted 16 lines · Dmitry Kasatkin
Quoted 15 lines · Dmitry Kasatkin
Hi David,
I just took latest #next branch from James's security tree which includes latest KEYs patches and noticed following:
[ 9.812332] Request for unknown module key 'Magrathea: Glaciersigning key:
926305d6dda66f47139eb4e3cb25a6adef527f08' err -11Also I noticed that output of 'keyctl show' and 'cat
/proc/keys' output also has changed in respect of certificate ids..Those ids does not look any close to my kernel X509 X509v3 Subject Key Identifier, which is:
92:63:05:D6:DD:A6:6F:47:13:9E:B4:E3:CB:25:A6:AD:EF:52:7F:08
Quoted 5 lines
Quoted 5 lines
proc/keys shows
symmetri Magrathea: Glacier signing key:
d9e2e4c6951f1e83: X509.RSA6865612e68326732[]
Ok.. d9e2e4c6951f1e83 is serial number.
What is this 6865612e68326732?
Does it still support searching for the key by partially matching Subject Key Identifier?
- Dmitry
Quoted 953 lines
Quoted 6 lines
Very different ids..
How could I match certificate now?
Module verification code cannot find needed key..- Dmitry
Hehe. Also now I get kernel Oops in
asymmetric_key_id_same...-------------------------
[ 132.816522] BUG: unable to handle kernel paging request at ffffffffffffffea [ 132.819902] IP: [<ffffffff812bfc20>] asymmetric_key_id_same+0x14/0x36 [ 132.820302] PGD 1a12067 PUD 1a14067 PMD 0 [ 132.820302] Oops: 0000 [#1] SMP [ 132.820302] Modules linked in: bridge(E) stp(E) llc(E) evdev(E) serio_raw(E) i2c_piix4(E) button(E) fuse(E) [ 132.820302] CPU: 0 PID: 2993 Comm: cat Tainted: G E 3.16.0-kds+ #2847 [ 132.820302] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011 [ 132.820302] task: ffff88004249a430 ti: ffff880056640000 task.ti: ffff880056640000 [ 132.820302] RIP: 0010:[<ffffffff812bfc20>] [<ffffffff812bfc20>] asymmetric_key_id_same+0x14/0x36 [ 132.820302] RSP: 0018:ffff880056643930 EFLAGS: 00010246 [ 132.820302] RAX: 0000000000000000 RBX: ffffffffffffffea RCX: ffff880056643ae0 [ 132.820302] RDX: 000000000000005e RSI: ffffffffffffffea RDI: ffff88005bac9300 [ 132.820302] RBP: ffff880056643948 R08: 0000000000000003 R09: 00000007504aa01a [ 132.820302] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88005d68ca40 [ 132.820302] R13: 0000000000000101 R14: 0000000000000000 R15: ffff88005bac5280 [ 132.820302] FS: 00007f67a153c740(0000) GS:ffff88005da00000(0000) knlGS:0000000000000000 [ 132.820302] CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b [ 132.820302] CR2: ffffffffffffffea CR3: 000000002e663000 CR4: 00000000000006f0 [ 132.820302] Stack: [ 132.820302] ffffffff812bfc66 ffff880056643ae0 ffff88005bac5280 ffff880056643958 [ 132.820302] ffffffff812bfc9d ffff880056643980 ffffffff812971d9 ffff88005ce930c1 [ 132.820302] ffff88005ce930c0 0000000000000000 ffff8800566439c8 ffffffff812fb753 [ 132.820302] Call Trace: [ 132.820302] [<ffffffff812bfc66>] ? asymmetric_match_key_ids+0x24/0x42 [ 132.820302] [<ffffffff812bfc9d>] asymmetric_key_cmp+0x19/0x1b [ 132.820302] [<ffffffff812971d9>] keyring_search_iterator+0x74/0xd7 [ 132.820302] [<ffffffff812fb753>] assoc_array_subtree_iterate+0x67/0xd2 [ 132.820302] [<ffffffff81297165>] ? key_default_cmp+0x20/0x20 [ 132.820302] [<ffffffff812fbaa1>] assoc_array_iterate+0x19/0x1e [ 132.820302] [<ffffffff81297332>] search_nested_keyrings+0xf6/0x2b6 [ 132.820302] [<ffffffff810728da>] ? sched_clock_cpu+0x91/0xa2 [ 132.820302] [<ffffffff810860d2>] ? mark_held_locks+0x58/0x6e [ 132.820302] [<ffffffff810a137d>] ? current_kernel_time+0x77/0xb8 [ 132.820302] [<ffffffff81297871>] keyring_search_aux+0xe1/0x14c [ 132.820302] [<ffffffff812977fc>] ? keyring_search_aux+0x6c/0x14c [ 132.820302] [<ffffffff8129796b>] keyring_search+0x8f/0xb6 [ 132.820302] [<ffffffff812bfc84>] ? asymmetric_match_key_ids+0x42/0x42 [ 132.820302] [<ffffffff81297165>] ? key_default_cmp+0x20/0x20 [ 132.820302] [<ffffffff812ab9e3>] asymmetric_verify+0xa4/0x214 [ 132.820302] [<ffffffff812ab90e>] integrity_digsig_verify+0xb1/0xe2 [ 132.820302] [<ffffffff812abe41>] ? evm_verifyxattr+0x6a/0x7a [ 132.820302] [<ffffffff812b0390>] ima_appraise_measurement+0x160/0x370 [ 132.820302] [<ffffffff81161db2>] ? d_absolute_path+0x5b/0x7a [ 132.820302] [<ffffffff812ada30>] process_measurement+0x322/0x404Quoted 866 lines · David Howells
Quoted 865 lines
Make use of the new match string preparsing to overhaul key identification when searching for asymmetric keys. The following changes are made:
(1) Use the previously created
asymmetric_key_idstruct to hold the following key IDs derived from the X.509 certificate or PKCS#7 message:id: serial number + issuer
skid: subjKeyId + subject
authority: authKeyId + issuer(2) Replace the hex fingerprint attached to key->type_data[1] with an
asymmetric_key_idsstruct containing the id and the skid (if present).(3) Make the
asymmetric_typematch data preparse select one of two searches:(a) An iterative search for the key ID given if prefixed with "id:". The prefix is expected to be followed by a hex string giving the ID to search for. The criterion key ID is checked against all key IDs recorded on the key.
(b) A direct search if the key ID is not prefixed with "id:". This will look for an exact match on the key description.
(4) Make
x509_request_asymmetric_key()take a key ID. This is then converted into "id:<hex>" and passed intokeyring_search()where match preparsing will turn it back into a binary ID.(5) X.509 certificate verification then takes the authority key ID and looks up a key that matches it to find the public key for the certificate signature.
(6) PKCS#7 certificate verification then takes the id key ID and looks up a key that matches it to find the public key for the signed information block signature.
Additional changes:
(1) Multiple subjKeyId and authKeyId values on an X.509 certificate cause the cert to be rejected with
-EBADMSG.(2) The 'fingerprint' ID is gone. This was primarily intended to convey PGP public key fingerprints. If PGP is supported in future, this should generate a key ID that carries the fingerprint.
(3) Th ca_keyid= kernel command line option is now converted to a key ID and used to match the authority key ID. Possibly this should only match the actual authKeyId part and not the issuer as well.
Signed-off-by David Howells <[email protected]>---
crypto/asymmetric_keys/asymmetric_keys.h | 4 - crypto/asymmetric_keys/asymmetric_type.c | 133 ++++++++++++----------------- crypto/asymmetric_keys/pkcs7_parser.c | 38 ++++++-- crypto/asymmetric_keys/pkcs7_parser.h | 5 - crypto/asymmetric_keys/pkcs7_trust.c | 6 - crypto/asymmetric_keys/pkcs7_verify.c | 44 ++++------ crypto/asymmetric_keys/x509_cert_parser.c | 55 +++++++----- crypto/asymmetric_keys/x509_parser.h | 5 + crypto/asymmetric_keys/x509_public_key.c | 89 +++++++++++-------- include/crypto/public_key.h | 5 + 10 files changed, 198 insertions(+), 186 deletions(-)crypto/asymmetric_keys/asymmetric_keys.h +2
-2@@ -9,13 +9,13 @@* 2 of the Licence, or (at your option) any later version. */ -int asymmetric_keyid_match (const char *kid,const char *id);extern bool asymmetric_match_key_ids (const struct asymmetric_key_ids *kids,const struct asymmetric_key_id *match_id);extern struct asymmetric_key_id *asymmetric_key_hex_to_key_id (const char *id); -static inline const char *asymmetric_key_id (const struct key *key) +static inline +const struct asymmetric_key_ids *asymmetric_key_ids (const struct key *key) {return key->type_data.p[1 ]; }crypto/asymmetric_keys/asymmetric_type.c +56
-77@@ -105,76 +105,15 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id) }/* -* Match asymmetric key id with partial match -* @id: key id to match in a form "id:<id>" -*/ -int asymmetric_keyid_match (const char *kid,const char *id) -{ -size_t idlen, kidlen; - -if (!kid || !id) -return 0 ; - -/* make it possible to use id as in the request: "id:<id>" */ -if (strncmp (id,"id:" ,3 ) ==0 ) - id +=3 ; - -/* Anything after here requires a partial match on the ID string */ - idlen =strlen (id); - kidlen =strlen (kid); -if (idlen > kidlen) -return 0 ; - - kid += kidlen - idlen; -if (strcasecmp (id, kid) !=0 ) -return 0 ; - -return 1 ; -} -EXPORT_SYMBOL_GPL (asymmetric_keyid_match); - -/* -* Match asymmetric keys on (part of) their name -* We have some shorthand methods for matching keys. We allow: -* -* "<desc>" - request a key by description -* "id:<id>" - request a key matching the ID -* "<subtype>:<id>" - request a key of a subtype +* Match asymmetric keys by ID. */ static bool asymmetric_key_cmp (const struct key *key,const struct key_match_data *match_data) { -const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); -const char *description = match_data->raw_data; -const char *spec = description; -const char *id; -ptrdiff_t speclen; - -if (!subtype || !spec || !*spec) -return 0 ; - -/* See if the full key description matches as is */ -if (key->description &&strcmp (key->description, description) ==0 ) -return 1 ; - -/* All tests from here on break the criterion description into a -* specifier, a colon and then an identifier. -*/ - id =strchr (spec,':' ); -if (!id) -return 0 ; - - speclen = id - spec; - id++; - -if (speclen ==2 &&memcmp (spec,"id" ,2 ) ==0 ) -return asymmetric_keyid_match (asymmetric_key_id (key), id); +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *match_id = match_data->preparsed; -if (speclen == subtype->name_len && -memcmp (spec, subtype->name, speclen) ==0 ) -return 1 ; - -return 0 ; +return asymmetric_match_key_ids (kids, match_id); }/* @@ -191,8 +130,30 @@ static bool asymmetric_key_cmp(const struct key *key,*/ static int asymmetric_key_match_preparse (struct key_match_data *match_data) { - match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +struct asymmetric_key_id *match_id; +const char *spec = match_data->raw_data; +const char *id; + +if (!spec || !*spec) +return -EINVAL ; +if (spec[0 ] =='i' && + spec[1 ] =='d' && + spec[2 ] ==':' ) { + id = spec +3 ; + }else { + goto default_match; + } + + match_id =asymmetric_key_hex_to_key_id (id); +if (!match_id) +return -ENOMEM ; + + match_data->preparsed = match_id; match_data->cmp = asymmetric_key_cmp; + match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +return 0 ; + +default_match:return 0 ; }@@ -201,6 +162,7 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data)*/ static void asymmetric_key_match_free (struct key_match_data *match_data) { +kfree (match_data->preparsed); }/* @@ -209,8 +171,10 @@ static void asymmetric_key_match_free(struct key_match_data *match_data)static void asymmetric_key_describe (const struct key *key,struct seq_file *m) {const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); -const char *kid =asymmetric_key_id (key); -size_t n; +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *kid; +const unsigned char *p; +int n;seq_puts (m, key->description);@@ -218,13 +182,16 @@ static void asymmetric_key_describe(const struct key *key, struct seq_file *m)seq_puts (m,": " ); subtype->describe (key, m); -if (kid) { +if (kids && kids->id[0 ]) { + kid = kids->id[0 ];seq_putc (m,' ' ); - n =strlen (kid); -if (n <=8 ) -seq_puts (m, kid); -else -seq_puts (m, kid + n -8 ); + n = kid->len; + p = kid->data; +if (n >8 ) { + p += n -8 ; + n =8 ; + } +seq_printf (m,"%*phN" , n, p); }seq_puts (m," [" );@@ -275,6 +242,7 @@ static int asymmetric_key_preparse(struct key_preparsed_payload *prep)static void asymmetric_key_free_preparse (struct key_preparsed_payload *prep) {struct asymmetric_key_subtype *subtype = prep->type_data[0 ]; +struct asymmetric_key_ids *kids = prep->type_data[1 ];pr_devel ("==>%s()\n" , __func__);@@ -282,7 +250,11 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep) subtype->destroy (prep->payload[0 ]);module_put (subtype->owner); } -kfree (prep->type_data[1 ]); +if (kids) { +kfree (kids->id[0 ]); +kfree (kids->id[1 ]); +kfree (kids); + }kfree (prep->description); }@@ -292,13 +264,20 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)static void asymmetric_key_destroy (struct key *key) {struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); +struct asymmetric_key_ids *kids = key->type_data.p[1 ]; +if (subtype) { subtype->destroy (key->payload.data);module_put (subtype->owner); key->type_data.p[0 ] =NULL ; } -kfree (key->type_data.p[1 ]); - key->type_data.p[1 ] =NULL ; + +if (kids) { +kfree (kids->id[0 ]); +kfree (kids->id[1 ]); +kfree (kids); + key->type_data.p[1 ] =NULL ; + } }struct key_type key_type_asymmetric = {crypto/asymmetric_keys/pkcs7_parser.c +27
-11@@ -29,6 +29,10 @@ struct pkcs7_parse_context {enum OID last_oid;/* Last OID encountered */ unsigned x509_index;unsigned sinfo_index; +const void *raw_serial; +unsigned raw_serial_size; +unsigned raw_issuer_size; +const void *raw_issuer; };/* @@ -39,6 +43,7 @@ static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)if (sinfo) {mpi_free (sinfo->sig.mpi[0 ]);kfree (sinfo->sig.digest); +kfree (sinfo->signing_cert_id);kfree (sinfo); } }@@ -256,10 +261,10 @@ int pkcs7_extract_cert(void *context, size_t hdrlen,if (IS_ERR (x509))return PTR_ERR (x509); -pr_debug ("Got cert for %s\n" , x509->subject); -pr_debug ("- fingerprint %s\n" , x509->fingerprint); - x509->index = ++ctx->x509_index; +pr_debug ("Got cert %u for %s\n" , x509->index, x509->subject); +pr_debug ("- fingerprint %*phN\n" , x509->id->len, x509->id->data); + *ctx->ppcerts = x509; ctx->ppcerts = &x509->next;return 0 ;@@ -348,8 +353,8 @@ int pkcs7_sig_note_serial(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_serial = value; - ctx->sinfo->raw_serial_size = vlen; + ctx->raw_serial = value; + ctx->raw_serial_size = vlen;return 0 ; }@@ -361,8 +366,8 @@ int pkcs7_sig_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_issuer = value; - ctx->sinfo->raw_issuer_size = vlen; + ctx->raw_issuer = value; + ctx->raw_issuer_size = vlen;return 0 ; }@@ -395,10 +400,21 @@ int pkcs7_note_signed_info(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - - ctx->sinfo->index = ++ctx->sinfo_index; - *ctx->ppsinfo = ctx->sinfo; - ctx->ppsinfo = &ctx->sinfo->next; +struct pkcs7_signed_info *sinfo = ctx->sinfo; +struct asymmetric_key_id *kid; + +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (ctx->raw_serial, + ctx->raw_serial_size, + ctx->raw_issuer, + ctx->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + + sinfo->signing_cert_id = kid; + sinfo->index = ++ctx->sinfo_index; + *ctx->ppsinfo = sinfo; + ctx->ppsinfo = &sinfo->next; ctx->sinfo =kzalloc (sizeof (struct pkcs7_signed_info ),GFP_KERNEL );if (!ctx->sinfo)return -ENOMEM ;crypto/asymmetric_keys/pkcs7_parser.h +1
-4@@ -33,10 +33,7 @@ struct pkcs7_signed_info {const void *authattrs;/* Issuing cert serial number and issuer's name */ -const void *raw_serial; -unsigned raw_serial_size; -unsigned raw_issuer_size; -const void *raw_issuer; +struct asymmetric_key_id *signing_cert_id;/* Message signature. * crypto/asymmetric_keys/pkcs7_trust.c +2
-4@@ -49,8 +49,7 @@ 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->subject, - x509->fingerprint); + key =x509_request_asymmetric_key (trust_keyring, x509->id);if (!IS_ERR (key))/* One of the X.509 certificates in the PKCS#7 message * is apparently the same as one we already trust. @@ -82,8 +81,7 @@ int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,return -ENOKEY ; } - key =x509_request_asymmetric_key (trust_keyring, last->issuer, - last->authority); + key =x509_request_asymmetric_key (trust_keyring, last->authority);if (IS_ERR (key))return PTR_ERR (key) == -ENOMEM ? -ENOMEM : -ENOKEY ; x509 = last;crypto/asymmetric_keys/pkcs7_verify.c +20
-24@@ -131,8 +131,7 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,struct x509_certificate *x509;unsigned certix =1 ; -kenter ("%u,%u,%u" , - sinfo->index, sinfo->raw_serial_size, sinfo->raw_issuer_size); +kenter ("%u" , sinfo->index);for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {/* I'm _assuming_ that the generator of the PKCS#7 message will @@ -140,21 +139,11 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,* PKCS#7 message - but I can't be 100% sure of that. It's * possible this will need element-by-element comparison. */ -if (x509->raw_serial_size != sinfo->raw_serial_size || -memcmp (x509->raw_serial, sinfo->raw_serial, - sinfo->raw_serial_size) !=0 ) +if (!asymmetric_key_id_same (x509->id, sinfo->signing_cert_id))continue ;pr_devel ("Sig %u: Found cert serial match X.509[%u]\n" , sinfo->index, certix); -if (x509->raw_issuer_size != sinfo->raw_issuer_size || -memcmp (x509->raw_issuer, sinfo->raw_issuer, - sinfo->raw_issuer_size) !=0 ) { -pr_warn ("Sig %u: X.509 subject and PKCS#7 issuer don't match\n" , - sinfo->index); -continue ; - } -if (x509->pub->pkey_algo != sinfo->sig.pkey_algo) {pr_warn ("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n" , sinfo->index);@@ -164,8 +153,10 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7, sinfo->signer = x509;return 0 ; } +pr_warn ("Sig %u: Issuing X.509 cert not found (#%*ph)\n" , - sinfo->index, sinfo->raw_serial_size, sinfo->raw_serial); + sinfo->index, + sinfo->signing_cert_id->len, sinfo->signing_cert_id->data);return -ENOKEY ; }@@ -184,7 +175,9 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, p->seen = false;for (;;) { -pr_debug ("verify %s: %s\n" , x509->subject, x509->fingerprint); +pr_debug ("verify %s: %*phN\n" , + x509->subject, + x509->raw_serial_size, x509->raw_serial); x509->seen = true; ret =x509_get_sig_params (x509);if (ret <0 )@@ -192,7 +185,8 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,pr_debug ("- issuer %s\n" , x509->issuer);if (x509->authority) -pr_debug ("- authkeyid %s\n" , x509->authority); +pr_debug ("- authkeyid %*phN\n" , + x509->authority->len, x509->authority->data);if (!x509->authority ||strcmp (x509->subject, x509->issuer) ==0 ) {@@ -218,13 +212,14 @@ 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 %s\n" , x509->authority); +pr_debug ("- want %*phN\n" , + x509->authority->len, x509->authority->data);for (p = pkcs7->certs; p; p = p->next) { -pr_debug ("- cmp [%u] %s\n" , p->index, p->fingerprint); -if (p->raw_subject_size == x509->raw_issuer_size && -strcmp (p->fingerprint, x509->authority) ==0 && -memcmp (p->raw_subject, x509->raw_issuer, - x509->raw_issuer_size) ==0 ) +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)) goto found_issuer; }@@ -233,7 +228,7 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,return 0 ; found_issuer: -pr_debug ("- issuer %s\n" , p->subject); +pr_debug ("- subject %s\n" , p->subject);if (p->seen) {pr_warn ("Sig %u: X.509 chain contains loop\n" , sinfo->index);@@ -304,7 +299,8 @@ int pkcs7_verify(struct pkcs7_message *pkcs7) ret =x509_get_sig_params (x509);if (ret <0 )return ret; -pr_debug ("X.509[%u] %s\n" , n, x509->authority); +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_cert_parser.c +33
-22@@ -46,7 +46,8 @@ void x509_free_certificate(struct x509_certificate *cert)public_key_destroy (cert->pub);kfree (cert->issuer);kfree (cert->subject); -kfree (cert->fingerprint); +kfree (cert->id); +kfree (cert->skid);kfree (cert->authority);kfree (cert->sig.digest);mpi_free (cert->sig.rsa.s);@@ -62,6 +63,7 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) {struct x509_certificate *cert;struct x509_parse_context *ctx; +struct asymmetric_key_id *kid;long ret; ret = -ENOMEM ;@@ -89,6 +91,17 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)if (ret <0 ) goto error_decode; +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (cert->raw_serial, + cert->raw_serial_size, + cert->raw_issuer, + cert->raw_issuer_size); +if (IS_ERR (kid)) { + ret =PTR_ERR (kid); + goto error_decode; + } + cert->id = kid; +kfree (ctx);return cert;@@ -407,36 +420,34 @@ int x509_process_extension(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; +struct asymmetric_key_id *kid;const unsigned char *v = value; -char *f;int i;pr_debug ("Extension: %u\n" , ctx->last_oid);if (ctx->last_oid == OID_subjectKeyIdentifier) {/* Get hold of the key fingerprint */ -if (vlen <3 ) +if (ctx->cert->skid || vlen <3 )return -EBADMSG ;if (v[0 ] !=ASN1_OTS || v[1 ] != vlen -2 )return -EBADMSG ; v +=2 ; vlen -=2 ; - f =kmalloc (vlen *2 +1 ,GFP_KERNEL ); -if (!f) -return -ENOMEM ; -for (i =0 ; i < vlen; i++) -sprintf (f + i *2 ,"%02x" , v[i]); -pr_debug ("fingerprint %s\n" , f); - ctx->cert->fingerprint = f; + kid =asymmetric_key_generate_id (v, vlen, + ctx->cert->raw_subject, + ctx->cert->raw_subject_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + ctx->cert->skid = kid; +pr_debug ("subjkeyid %*phN\n" , kid->len, kid->data);return 0 ; }if (ctx->last_oid == OID_authorityKeyIdentifier) { -size_t key_len; -/* Get hold of the CA key fingerprint */ -if (vlen <5 ) +if (ctx->cert->authority || vlen <5 )return -EBADMSG ;/* Authority Key Identifier must be a Constructed SEQUENCE */ @@ -454,7 +465,7 @@ int x509_process_extension(void *context, size_t hdrlen, v[3] > vlen - 4)return -EBADMSG ; - key_len = v[3 ]; + vlen = v[3 ]; v +=4 ; }else {/* Long Form length */ @@ -476,17 +487,17 @@ int x509_process_extension(void *context, size_t hdrlen, v[sub + 1] > vlen - 4 - sub)return -EBADMSG ; - key_len = v[sub +1 ]; + vlen = v[sub +1 ]; v += (sub +2 ); } - f =kmalloc (key_len *2 +1 ,GFP_KERNEL ); -if (!f) -return -ENOMEM ; -for (i =0 ; i < key_len; i++) -sprintf (f + i *2 ,"%02x" , v[i]); -pr_debug ("authority %s\n" , f); - ctx->cert->authority = f; + kid =asymmetric_key_generate_id (v, vlen, + ctx->cert->raw_issuer, + ctx->cert->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); +pr_debug ("authkeyid %*phN\n" , kid->len, kid->data); + ctx->cert->authority = kid;return 0 ; }crypto/asymmetric_keys/x509_parser.h +3
-2@@ -19,8 +19,9 @@ struct x509_certificate {struct public_key_signature sig;/* Signature parameters */ char *issuer;/* Name of certificate issuer */ char *subject;/* Name of certificate subject */ -char *fingerprint;/* Key fingerprint as hex */ -char *authority;/* Authority key fingerprint as hex */ +struct asymmetric_key_id *id;/* Issuer + serial number */ +struct asymmetric_key_id *skid;/* Subject key identifier */ +struct asymmetric_key_id *authority;/* Authority key identifier */ struct tm valid_from;struct tm valid_to;const void *tbs;/* Signed data */ crypto/asymmetric_keys/x509_public_key.c +51
-38@@ -25,7 +25,7 @@#include "x509_parser.h" static bool use_builtin_keys; -static char *ca_keyid; +static struct asymmetric_key_id *ca_keyid;#ifndef MODULE static int __init ca_keys_setup (char *str)@@ -33,10 +33,16 @@ static int __init ca_keys_setup(char *str)if (!str)/* default system keyring */ return 1 ; -if (strncmp (str,"id:" ,3 ) ==0 ) - ca_keyid = str;/* owner key 'id:xxxxxx' */ -else if (strcmp (str,"builtin" ) ==0 ) +if (strncmp (str,"id:" ,3 ) ==0 ) { +struct asymmetric_key_id *p; + p =asymmetric_key_hex_to_key_id (str); +if (p ==ERR_PTR (-EINVAL )) +pr_err ("Unparsable hex string in ca_keys\n" ); +else if (!IS_ERR (p)) + ca_keyid = p;/* owner key 'id:xxxxxx' */ + }else if (strcmp (str,"builtin" ) ==0 ) { use_builtin_keys = true; + }return 1 ; }@@ -46,31 +52,28 @@ __setup("ca_keys=", ca_keys_setup);/** * x509_request_asymmetric_key - Request a key by X.509 certificate params. * @keyring: The keys to search. -* @subject: The name of the subject to whom the key belongs. -* @key_id: The subject key ID as a hex string. +* @kid: The key ID. * * Find a key in the given keyring by subject name and key ID. These might, * for instance, be the issuer name and the authority key ID of an X.509 * certificate that needs to be verified. */ struct key *x509_request_asymmetric_key (struct key *keyring, -const char *subject, -const char *key_id) +const struct asymmetric_key_id *kid) {key_ref_t key; -size_t subject_len =strlen (subject), key_id_len =strlen (key_id); -char *id; +char *id, *p; -/* Construct an identifier "<subjname>:<keyid>". */ - id =kmalloc (subject_len +2 + key_id_len +1 ,GFP_KERNEL ); +/* Construct an identifier "id:<keyid>". */ + p = id =kmalloc (2 +1 + kid->len *2 +1 ,GFP_KERNEL );if (!id)return ERR_PTR (-ENOMEM ); -memcpy (id, subject, subject_len); - id[subject_len +0 ] =':' ; - id[subject_len +1 ] =' ' ; -memcpy (id + subject_len +2 , key_id, key_id_len); - id[subject_len +2 + key_id_len] =0 ; + *p++ ='i' ; + *p++ ='d' ; + *p++ =':' ; + p =bin2hex (p, kid->data, kid->len); + *p =0 ;pr_debug ("Look up: \"%s\"\n" , id);@@ -195,11 +198,10 @@ static int x509_validate_trust(struct x509_certificate *cert,if (!trust_keyring)return -EOPNOTSUPP ; -if (ca_keyid && !asymmetric_keyid_match (cert->authority, ca_keyid)) +if (ca_keyid && !asymmetric_key_id_same (cert->authority, ca_keyid))return -EPERM ; - key =x509_request_asymmetric_key (trust_keyring, - cert->issuer, cert->authority); + key =x509_request_asymmetric_key (trust_keyring, cert->authority);if (!IS_ERR (key)) {if (!use_builtin_keys ||test_bit (KEY_FLAG_BUILTIN , &key->flags))@@ -214,9 +216,11 @@ static int x509_validate_trust(struct x509_certificate *cert,*/ static int x509_key_preparse (struct key_preparsed_payload *prep) { +struct asymmetric_key_ids *kids;struct x509_certificate *cert; +const char *q;size_t srlen, sulen; -char *desc =NULL ; +char *desc =NULL , *p;int ret; cert =x509_cert_parse (prep->data, prep->datalen);@@ -249,19 +253,12 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) pkey_algo_name[cert->sig.pkey_algo], hash_algo_name[cert->sig.pkey_hash_algo]); -if (!cert->fingerprint) { -pr_warn ("Cert for '%s' must have a SubjKeyId extension\n" , - cert->subject); - ret = -EKEYREJECTED ; - goto error_free_cert; - } - cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; cert->pub->id_type =PKEY_ID_X509 ;/* Check the signature on the key if it appears to be self-signed */ if (!cert->authority || -strcmp (cert->fingerprint, cert->authority) ==0 ) { +asymmetric_key_id_same (cert->skid, cert->authority)) { ret =x509_check_signature (cert->pub, cert);/* self-signed */ if (ret <0 ) goto error_free_cert;@@ -273,31 +270,47 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)/* Propose a description */ sulen =strlen (cert->subject); - srlen =strlen (cert->fingerprint); + srlen = cert->raw_serial_size; + q = cert->raw_serial; +if (srlen >1 && *q ==0 ) { + srlen--; + q++; + } + ret = -ENOMEM ; - desc =kmalloc (sulen +2 + srlen +1 ,GFP_KERNEL ); + desc =kmalloc (sulen +2 + srlen *2 +1 ,GFP_KERNEL );if (!desc) goto error_free_cert; -memcpy (desc, cert->subject, sulen); - desc[sulen] =':' ; - desc[sulen +1 ] =' ' ; -memcpy (desc + sulen +2 , cert->fingerprint, srlen); - desc[sulen +2 + srlen] =0 ; + p =memcpy (desc, cert->subject, sulen); + p += sulen; + *p++ =':' ; + *p++ =' ' ; + p =bin2hex (p, q, srlen); + *p =0 ; + + kids =kmalloc (sizeof (struct asymmetric_key_ids ),GFP_KERNEL ); +if (!kids) + goto error_free_desc; + kids->id[0 ] = cert->id; + kids->id[1 ] = cert->skid;/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner); prep->type_data[0 ] = &public_key_subtype; - prep->type_data[1 ] = cert->fingerprint; + prep->type_data[1 ] = kids; prep->payload[0 ] = cert->pub; prep->description = desc; prep->quotalen =100 ;/* We've finished with the certificate */ cert->pub =NULL ; - cert->fingerprint =NULL ; + cert->id =NULL ; + cert->skid =NULL ; desc =NULL ; ret =0 ; +error_free_desc: +kfree (desc); error_free_cert:x509_free_certificate (cert);return ret;include/crypto/public_key.h +3
-2@@ -15,6 +15,7 @@#define _LINUX_PUBLIC_KEY_H#include <linux/mpi.h> +#include <keys/asymmetric-type.h> #include <crypto/hash_info.h> enum pkey_algo {@@ -98,8 +99,8 @@ struct key;extern int verify_signature (const struct key *key,const struct public_key_signature *sig); +struct asymmetric_key_id ;extern struct key *x509_request_asymmetric_key (struct key *keyring, -const char *issuer, -const char *key_id); +const struct asymmetric_key_id *kid);#endif /* _LINUX_PUBLIC_KEY_H */ Quoted 10 lines
Quoted 5 lines
--
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--
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--
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
Signature · 2 lines
Thanks, Dmitry
David Howells
maintainerRe: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
Quoted 1 line · Dmitry Kasatkin
What is this
6865612e68326732?
It's the tail of the first ID attached to the key (there may be more than one). As this is derived from an X.509 cert, that will be the serial number plus the ASN.1 encoded issuer name.
To quote from the description in the patch:
(1) Use the previously created asymmetric_key_id struct to hold the following key IDs derived from the X.509 certificate or PKCS#7 message:
id: serial number + issuer
skid: subjKeyId + subject
authority: authKeyId + issuer
(2) Replace the hex fingerprint attached to key->type_data[1] with an asymmetric_key_ids struct containing the id and the skid (if present).
If you turn the hex into chars, you will see "hea.h2g2"
I'm open to suggestions about the best way to represent the auxiliary IDs in /proc/keys - but don't forget there can be more than one.
David
David Howells
maintainerRe: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
In reply to Dmitry KasatkinQuoted 2 lines · Dmitry Kasatkin
Does it still support searching for the key by partially matching Subject Key Identifier?
No, not at this time. Adding partial matching support wouldn't be hard, and given that the subjKeyId is the first part of the second key ID, that would then work.
David
Re: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
In reply to Dmitry KasatkinQuoted 23 lines · Dmitry Kasatkin
Quoted 22 lines · Dmitry Kasatkin
Hi David,
I just took latest #next branch from James's security tree which includes latest KEYs patches and noticed following:
[ 9.812332] Request for unknown module key 'Magrathea: Glaciersigning key:
926305d6dda66f47139eb4e3cb25a6adef527f08' err -11Also I noticed that output of 'keyctl show' and 'cat
/proc/keys' output also has changed in respect of certificate ids..Those ids does not look any close to my kernel X509 X509v3 Subject Key Identifier, which is:
92:63:05:D6:DD:A6:6F:47:13:9E:B4:E3:CB:25:A6:AD:EF:52:7F:08proc/keys shows
symmetri Magrathea: Glacier signing key:
d9e2e4c6951f1e83: X509.RSA6865612e68326732[]Very different ids..
Quoted 7 lines
Quoted 4 lines
How could I match certificate now?
Module verification code cannot find needed key..- Dmitry
Hehe. Also now I get kernel Oops in
asymmetric_key_id_same...
Confirmed
Mimi
Quoted 944 lines
-------------------------
[ 132.816522] BUG: unable to handle kernel paging request at ffffffffffffffea [ 132.819902] IP: [<ffffffff812bfc20>] asymmetric_key_id_same+0x14/0x36 [ 132.820302] PGD 1a12067 PUD 1a14067 PMD 0 [ 132.820302] Oops: 0000 [#1] SMP [ 132.820302] Modules linked in: bridge(E) stp(E) llc(E) evdev(E) serio_raw(E) i2c_piix4(E) button(E) fuse(E) [ 132.820302] CPU: 0 PID: 2993 Comm: cat Tainted: G E 3.16.0-kds+ #2847 [ 132.820302] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011 [ 132.820302] task: ffff88004249a430 ti: ffff880056640000 task.ti: ffff880056640000 [ 132.820302] RIP: 0010:[<ffffffff812bfc20>] [<ffffffff812bfc20>] asymmetric_key_id_same+0x14/0x36 [ 132.820302] RSP: 0018:ffff880056643930 EFLAGS: 00010246 [ 132.820302] RAX: 0000000000000000 RBX: ffffffffffffffea RCX: ffff880056643ae0 [ 132.820302] RDX: 000000000000005e RSI: ffffffffffffffea RDI: ffff88005bac9300 [ 132.820302] RBP: ffff880056643948 R08: 0000000000000003 R09: 00000007504aa01a [ 132.820302] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88005d68ca40 [ 132.820302] R13: 0000000000000101 R14: 0000000000000000 R15: ffff88005bac5280 [ 132.820302] FS: 00007f67a153c740(0000) GS:ffff88005da00000(0000) knlGS:0000000000000000 [ 132.820302] CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b [ 132.820302] CR2: ffffffffffffffea CR3: 000000002e663000 CR4: 00000000000006f0 [ 132.820302] Stack: [ 132.820302] ffffffff812bfc66 ffff880056643ae0 ffff88005bac5280 ffff880056643958 [ 132.820302] ffffffff812bfc9d ffff880056643980 ffffffff812971d9 ffff88005ce930c1 [ 132.820302] ffff88005ce930c0 0000000000000000 ffff8800566439c8 ffffffff812fb753 [ 132.820302] Call Trace: [ 132.820302] [<ffffffff812bfc66>] ? asymmetric_match_key_ids+0x24/0x42 [ 132.820302] [<ffffffff812bfc9d>] asymmetric_key_cmp+0x19/0x1b [ 132.820302] [<ffffffff812971d9>] keyring_search_iterator+0x74/0xd7 [ 132.820302] [<ffffffff812fb753>] assoc_array_subtree_iterate+0x67/0xd2 [ 132.820302] [<ffffffff81297165>] ? key_default_cmp+0x20/0x20 [ 132.820302] [<ffffffff812fbaa1>] assoc_array_iterate+0x19/0x1e [ 132.820302] [<ffffffff81297332>] search_nested_keyrings+0xf6/0x2b6 [ 132.820302] [<ffffffff810728da>] ? sched_clock_cpu+0x91/0xa2 [ 132.820302] [<ffffffff810860d2>] ? mark_held_locks+0x58/0x6e [ 132.820302] [<ffffffff810a137d>] ? current_kernel_time+0x77/0xb8 [ 132.820302] [<ffffffff81297871>] keyring_search_aux+0xe1/0x14c [ 132.820302] [<ffffffff812977fc>] ? keyring_search_aux+0x6c/0x14c [ 132.820302] [<ffffffff8129796b>] keyring_search+0x8f/0xb6 [ 132.820302] [<ffffffff812bfc84>] ? asymmetric_match_key_ids+0x42/0x42 [ 132.820302] [<ffffffff81297165>] ? key_default_cmp+0x20/0x20 [ 132.820302] [<ffffffff812ab9e3>] asymmetric_verify+0xa4/0x214 [ 132.820302] [<ffffffff812ab90e>] integrity_digsig_verify+0xb1/0xe2 [ 132.820302] [<ffffffff812abe41>] ? evm_verifyxattr+0x6a/0x7a [ 132.820302] [<ffffffff812b0390>] ima_appraise_measurement+0x160/0x370 [ 132.820302] [<ffffffff81161db2>] ? d_absolute_path+0x5b/0x7a [ 132.820302] [<ffffffff812ada30>] process_measurement+0x322/0x404Quoted 866 lines · David Howells
Quoted 865 lines
Make use of the new match string preparsing to overhaul key identification when searching for asymmetric keys. The following changes are made:
(1) Use the previously created
asymmetric_key_idstruct to hold the following key IDs derived from the X.509 certificate or PKCS#7 message:id: serial number + issuer
skid: subjKeyId + subject
authority: authKeyId + issuer(2) Replace the hex fingerprint attached to key->type_data[1] with an
asymmetric_key_idsstruct containing the id and the skid (if present).(3) Make the
asymmetric_typematch data preparse select one of two searches:(a) An iterative search for the key ID given if prefixed with "id:". The prefix is expected to be followed by a hex string giving the ID to search for. The criterion key ID is checked against all key IDs recorded on the key.
(b) A direct search if the key ID is not prefixed with "id:". This will look for an exact match on the key description.
(4) Make
x509_request_asymmetric_key()take a key ID. This is then converted into "id:<hex>" and passed intokeyring_search()where match preparsing will turn it back into a binary ID.(5) X.509 certificate verification then takes the authority key ID and looks up a key that matches it to find the public key for the certificate signature.
(6) PKCS#7 certificate verification then takes the id key ID and looks up a key that matches it to find the public key for the signed information block signature.
Additional changes:
(1) Multiple subjKeyId and authKeyId values on an X.509 certificate cause the cert to be rejected with
-EBADMSG.(2) The 'fingerprint' ID is gone. This was primarily intended to convey PGP public key fingerprints. If PGP is supported in future, this should generate a key ID that carries the fingerprint.
(3) Th ca_keyid= kernel command line option is now converted to a key ID and used to match the authority key ID. Possibly this should only match the actual authKeyId part and not the issuer as well.
Signed-off-by David Howells <[email protected]>---
crypto/asymmetric_keys/asymmetric_keys.h | 4 - crypto/asymmetric_keys/asymmetric_type.c | 133 ++++++++++++----------------- crypto/asymmetric_keys/pkcs7_parser.c | 38 ++++++-- crypto/asymmetric_keys/pkcs7_parser.h | 5 - crypto/asymmetric_keys/pkcs7_trust.c | 6 - crypto/asymmetric_keys/pkcs7_verify.c | 44 ++++------ crypto/asymmetric_keys/x509_cert_parser.c | 55 +++++++----- crypto/asymmetric_keys/x509_parser.h | 5 + crypto/asymmetric_keys/x509_public_key.c | 89 +++++++++++-------- include/crypto/public_key.h | 5 + 10 files changed, 198 insertions(+), 186 deletions(-)crypto/asymmetric_keys/asymmetric_keys.h +2
-2@@ -9,13 +9,13 @@* 2 of the Licence, or (at your option) any later version. */ -int asymmetric_keyid_match (const char *kid,const char *id);extern bool asymmetric_match_key_ids (const struct asymmetric_key_ids *kids,const struct asymmetric_key_id *match_id);extern struct asymmetric_key_id *asymmetric_key_hex_to_key_id (const char *id); -static inline const char *asymmetric_key_id (const struct key *key) +static inline +const struct asymmetric_key_ids *asymmetric_key_ids (const struct key *key) {return key->type_data.p[1 ]; }crypto/asymmetric_keys/asymmetric_type.c +56
-77@@ -105,76 +105,15 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id) }/* -* Match asymmetric key id with partial match -* @id: key id to match in a form "id:<id>" -*/ -int asymmetric_keyid_match (const char *kid,const char *id) -{ -size_t idlen, kidlen; - -if (!kid || !id) -return 0 ; - -/* make it possible to use id as in the request: "id:<id>" */ -if (strncmp (id,"id:" ,3 ) ==0 ) - id +=3 ; - -/* Anything after here requires a partial match on the ID string */ - idlen =strlen (id); - kidlen =strlen (kid); -if (idlen > kidlen) -return 0 ; - - kid += kidlen - idlen; -if (strcasecmp (id, kid) !=0 ) -return 0 ; - -return 1 ; -} -EXPORT_SYMBOL_GPL (asymmetric_keyid_match); - -/* -* Match asymmetric keys on (part of) their name -* We have some shorthand methods for matching keys. We allow: -* -* "<desc>" - request a key by description -* "id:<id>" - request a key matching the ID -* "<subtype>:<id>" - request a key of a subtype +* Match asymmetric keys by ID. */ static bool asymmetric_key_cmp (const struct key *key,const struct key_match_data *match_data) { -const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); -const char *description = match_data->raw_data; -const char *spec = description; -const char *id; -ptrdiff_t speclen; - -if (!subtype || !spec || !*spec) -return 0 ; - -/* See if the full key description matches as is */ -if (key->description &&strcmp (key->description, description) ==0 ) -return 1 ; - -/* All tests from here on break the criterion description into a -* specifier, a colon and then an identifier. -*/ - id =strchr (spec,':' ); -if (!id) -return 0 ; - - speclen = id - spec; - id++; - -if (speclen ==2 &&memcmp (spec,"id" ,2 ) ==0 ) -return asymmetric_keyid_match (asymmetric_key_id (key), id); +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *match_id = match_data->preparsed; -if (speclen == subtype->name_len && -memcmp (spec, subtype->name, speclen) ==0 ) -return 1 ; - -return 0 ; +return asymmetric_match_key_ids (kids, match_id); }/* @@ -191,8 +130,30 @@ static bool asymmetric_key_cmp(const struct key *key,*/ static int asymmetric_key_match_preparse (struct key_match_data *match_data) { - match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +struct asymmetric_key_id *match_id; +const char *spec = match_data->raw_data; +const char *id; + +if (!spec || !*spec) +return -EINVAL ; +if (spec[0 ] =='i' && + spec[1 ] =='d' && + spec[2 ] ==':' ) { + id = spec +3 ; + }else { + goto default_match; + } + + match_id =asymmetric_key_hex_to_key_id (id); +if (!match_id) +return -ENOMEM ; + + match_data->preparsed = match_id; match_data->cmp = asymmetric_key_cmp; + match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; +return 0 ; + +default_match:return 0 ; }@@ -201,6 +162,7 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data)*/ static void asymmetric_key_match_free (struct key_match_data *match_data) { +kfree (match_data->preparsed); }/* @@ -209,8 +171,10 @@ static void asymmetric_key_match_free(struct key_match_data *match_data)static void asymmetric_key_describe (const struct key *key,struct seq_file *m) {const struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); -const char *kid =asymmetric_key_id (key); -size_t n; +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *kid; +const unsigned char *p; +int n;seq_puts (m, key->description);@@ -218,13 +182,16 @@ static void asymmetric_key_describe(const struct key *key, struct seq_file *m)seq_puts (m,": " ); subtype->describe (key, m); -if (kid) { +if (kids && kids->id[0 ]) { + kid = kids->id[0 ];seq_putc (m,' ' ); - n =strlen (kid); -if (n <=8 ) -seq_puts (m, kid); -else -seq_puts (m, kid + n -8 ); + n = kid->len; + p = kid->data; +if (n >8 ) { + p += n -8 ; + n =8 ; + } +seq_printf (m,"%*phN" , n, p); }seq_puts (m," [" );@@ -275,6 +242,7 @@ static int asymmetric_key_preparse(struct key_preparsed_payload *prep)static void asymmetric_key_free_preparse (struct key_preparsed_payload *prep) {struct asymmetric_key_subtype *subtype = prep->type_data[0 ]; +struct asymmetric_key_ids *kids = prep->type_data[1 ];pr_devel ("==>%s()\n" , __func__);@@ -282,7 +250,11 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep) subtype->destroy (prep->payload[0 ]);module_put (subtype->owner); } -kfree (prep->type_data[1 ]); +if (kids) { +kfree (kids->id[0 ]); +kfree (kids->id[1 ]); +kfree (kids); + }kfree (prep->description); }@@ -292,13 +264,20 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)static void asymmetric_key_destroy (struct key *key) {struct asymmetric_key_subtype *subtype =asymmetric_key_subtype (key); +struct asymmetric_key_ids *kids = key->type_data.p[1 ]; +if (subtype) { subtype->destroy (key->payload.data);module_put (subtype->owner); key->type_data.p[0 ] =NULL ; } -kfree (key->type_data.p[1 ]); - key->type_data.p[1 ] =NULL ; + +if (kids) { +kfree (kids->id[0 ]); +kfree (kids->id[1 ]); +kfree (kids); + key->type_data.p[1 ] =NULL ; + } }struct key_type key_type_asymmetric = {crypto/asymmetric_keys/pkcs7_parser.c +27
-11@@ -29,6 +29,10 @@ struct pkcs7_parse_context {enum OID last_oid;/* Last OID encountered */ unsigned x509_index;unsigned sinfo_index; +const void *raw_serial; +unsigned raw_serial_size; +unsigned raw_issuer_size; +const void *raw_issuer; };/* @@ -39,6 +43,7 @@ static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)if (sinfo) {mpi_free (sinfo->sig.mpi[0 ]);kfree (sinfo->sig.digest); +kfree (sinfo->signing_cert_id);kfree (sinfo); } }@@ -256,10 +261,10 @@ int pkcs7_extract_cert(void *context, size_t hdrlen,if (IS_ERR (x509))return PTR_ERR (x509); -pr_debug ("Got cert for %s\n" , x509->subject); -pr_debug ("- fingerprint %s\n" , x509->fingerprint); - x509->index = ++ctx->x509_index; +pr_debug ("Got cert %u for %s\n" , x509->index, x509->subject); +pr_debug ("- fingerprint %*phN\n" , x509->id->len, x509->id->data); + *ctx->ppcerts = x509; ctx->ppcerts = &x509->next;return 0 ;@@ -348,8 +353,8 @@ int pkcs7_sig_note_serial(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_serial = value; - ctx->sinfo->raw_serial_size = vlen; + ctx->raw_serial = value; + ctx->raw_serial_size = vlen;return 0 ; }@@ -361,8 +366,8 @@ int pkcs7_sig_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - ctx->sinfo->raw_issuer = value; - ctx->sinfo->raw_issuer_size = vlen; + ctx->raw_issuer = value; + ctx->raw_issuer_size = vlen;return 0 ; }@@ -395,10 +400,21 @@ int pkcs7_note_signed_info(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; - - ctx->sinfo->index = ++ctx->sinfo_index; - *ctx->ppsinfo = ctx->sinfo; - ctx->ppsinfo = &ctx->sinfo->next; +struct pkcs7_signed_info *sinfo = ctx->sinfo; +struct asymmetric_key_id *kid; + +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (ctx->raw_serial, + ctx->raw_serial_size, + ctx->raw_issuer, + ctx->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + + sinfo->signing_cert_id = kid; + sinfo->index = ++ctx->sinfo_index; + *ctx->ppsinfo = sinfo; + ctx->ppsinfo = &sinfo->next; ctx->sinfo =kzalloc (sizeof (struct pkcs7_signed_info ),GFP_KERNEL );if (!ctx->sinfo)return -ENOMEM ;crypto/asymmetric_keys/pkcs7_parser.h +1
-4@@ -33,10 +33,7 @@ struct pkcs7_signed_info {const void *authattrs;/* Issuing cert serial number and issuer's name */ -const void *raw_serial; -unsigned raw_serial_size; -unsigned raw_issuer_size; -const void *raw_issuer; +struct asymmetric_key_id *signing_cert_id;/* Message signature. * crypto/asymmetric_keys/pkcs7_trust.c +2
-4@@ -49,8 +49,7 @@ 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->subject, - x509->fingerprint); + key =x509_request_asymmetric_key (trust_keyring, x509->id);if (!IS_ERR (key))/* One of the X.509 certificates in the PKCS#7 message * is apparently the same as one we already trust. @@ -82,8 +81,7 @@ int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,return -ENOKEY ; } - key =x509_request_asymmetric_key (trust_keyring, last->issuer, - last->authority); + key =x509_request_asymmetric_key (trust_keyring, last->authority);if (IS_ERR (key))return PTR_ERR (key) == -ENOMEM ? -ENOMEM : -ENOKEY ; x509 = last;crypto/asymmetric_keys/pkcs7_verify.c +20
-24@@ -131,8 +131,7 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,struct x509_certificate *x509;unsigned certix =1 ; -kenter ("%u,%u,%u" , - sinfo->index, sinfo->raw_serial_size, sinfo->raw_issuer_size); +kenter ("%u" , sinfo->index);for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {/* I'm _assuming_ that the generator of the PKCS#7 message will @@ -140,21 +139,11 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,* PKCS#7 message - but I can't be 100% sure of that. It's * possible this will need element-by-element comparison. */ -if (x509->raw_serial_size != sinfo->raw_serial_size || -memcmp (x509->raw_serial, sinfo->raw_serial, - sinfo->raw_serial_size) !=0 ) +if (!asymmetric_key_id_same (x509->id, sinfo->signing_cert_id))continue ;pr_devel ("Sig %u: Found cert serial match X.509[%u]\n" , sinfo->index, certix); -if (x509->raw_issuer_size != sinfo->raw_issuer_size || -memcmp (x509->raw_issuer, sinfo->raw_issuer, - sinfo->raw_issuer_size) !=0 ) { -pr_warn ("Sig %u: X.509 subject and PKCS#7 issuer don't match\n" , - sinfo->index); -continue ; - } -if (x509->pub->pkey_algo != sinfo->sig.pkey_algo) {pr_warn ("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n" , sinfo->index);@@ -164,8 +153,10 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7, sinfo->signer = x509;return 0 ; } +pr_warn ("Sig %u: Issuing X.509 cert not found (#%*ph)\n" , - sinfo->index, sinfo->raw_serial_size, sinfo->raw_serial); + sinfo->index, + sinfo->signing_cert_id->len, sinfo->signing_cert_id->data);return -ENOKEY ; }@@ -184,7 +175,9 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, p->seen = false;for (;;) { -pr_debug ("verify %s: %s\n" , x509->subject, x509->fingerprint); +pr_debug ("verify %s: %*phN\n" , + x509->subject, + x509->raw_serial_size, x509->raw_serial); x509->seen = true; ret =x509_get_sig_params (x509);if (ret <0 )@@ -192,7 +185,8 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,pr_debug ("- issuer %s\n" , x509->issuer);if (x509->authority) -pr_debug ("- authkeyid %s\n" , x509->authority); +pr_debug ("- authkeyid %*phN\n" , + x509->authority->len, x509->authority->data);if (!x509->authority ||strcmp (x509->subject, x509->issuer) ==0 ) {@@ -218,13 +212,14 @@ 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 %s\n" , x509->authority); +pr_debug ("- want %*phN\n" , + x509->authority->len, x509->authority->data);for (p = pkcs7->certs; p; p = p->next) { -pr_debug ("- cmp [%u] %s\n" , p->index, p->fingerprint); -if (p->raw_subject_size == x509->raw_issuer_size && -strcmp (p->fingerprint, x509->authority) ==0 && -memcmp (p->raw_subject, x509->raw_issuer, - x509->raw_issuer_size) ==0 ) +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)) goto found_issuer; }@@ -233,7 +228,7 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,return 0 ; found_issuer: -pr_debug ("- issuer %s\n" , p->subject); +pr_debug ("- subject %s\n" , p->subject);if (p->seen) {pr_warn ("Sig %u: X.509 chain contains loop\n" , sinfo->index);@@ -304,7 +299,8 @@ int pkcs7_verify(struct pkcs7_message *pkcs7) ret =x509_get_sig_params (x509);if (ret <0 )return ret; -pr_debug ("X.509[%u] %s\n" , n, x509->authority); +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_cert_parser.c +33
-22@@ -46,7 +46,8 @@ void x509_free_certificate(struct x509_certificate *cert)public_key_destroy (cert->pub);kfree (cert->issuer);kfree (cert->subject); -kfree (cert->fingerprint); +kfree (cert->id); +kfree (cert->skid);kfree (cert->authority);kfree (cert->sig.digest);mpi_free (cert->sig.rsa.s);@@ -62,6 +63,7 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) {struct x509_certificate *cert;struct x509_parse_context *ctx; +struct asymmetric_key_id *kid;long ret; ret = -ENOMEM ;@@ -89,6 +91,17 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)if (ret <0 ) goto error_decode; +/* Generate cert issuer + serial number key ID */ + kid =asymmetric_key_generate_id (cert->raw_serial, + cert->raw_serial_size, + cert->raw_issuer, + cert->raw_issuer_size); +if (IS_ERR (kid)) { + ret =PTR_ERR (kid); + goto error_decode; + } + cert->id = kid; +kfree (ctx);return cert;@@ -407,36 +420,34 @@ int x509_process_extension(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; +struct asymmetric_key_id *kid;const unsigned char *v = value; -char *f;int i;pr_debug ("Extension: %u\n" , ctx->last_oid);if (ctx->last_oid == OID_subjectKeyIdentifier) {/* Get hold of the key fingerprint */ -if (vlen <3 ) +if (ctx->cert->skid || vlen <3 )return -EBADMSG ;if (v[0 ] !=ASN1_OTS || v[1 ] != vlen -2 )return -EBADMSG ; v +=2 ; vlen -=2 ; - f =kmalloc (vlen *2 +1 ,GFP_KERNEL ); -if (!f) -return -ENOMEM ; -for (i =0 ; i < vlen; i++) -sprintf (f + i *2 ,"%02x" , v[i]); -pr_debug ("fingerprint %s\n" , f); - ctx->cert->fingerprint = f; + kid =asymmetric_key_generate_id (v, vlen, + ctx->cert->raw_subject, + ctx->cert->raw_subject_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); + ctx->cert->skid = kid; +pr_debug ("subjkeyid %*phN\n" , kid->len, kid->data);return 0 ; }if (ctx->last_oid == OID_authorityKeyIdentifier) { -size_t key_len; -/* Get hold of the CA key fingerprint */ -if (vlen <5 ) +if (ctx->cert->authority || vlen <5 )return -EBADMSG ;/* Authority Key Identifier must be a Constructed SEQUENCE */ @@ -454,7 +465,7 @@ int x509_process_extension(void *context, size_t hdrlen, v[3] > vlen - 4)return -EBADMSG ; - key_len = v[3 ]; + vlen = v[3 ]; v +=4 ; }else {/* Long Form length */ @@ -476,17 +487,17 @@ int x509_process_extension(void *context, size_t hdrlen, v[sub + 1] > vlen - 4 - sub)return -EBADMSG ; - key_len = v[sub +1 ]; + vlen = v[sub +1 ]; v += (sub +2 ); } - f =kmalloc (key_len *2 +1 ,GFP_KERNEL ); -if (!f) -return -ENOMEM ; -for (i =0 ; i < key_len; i++) -sprintf (f + i *2 ,"%02x" , v[i]); -pr_debug ("authority %s\n" , f); - ctx->cert->authority = f; + kid =asymmetric_key_generate_id (v, vlen, + ctx->cert->raw_issuer, + ctx->cert->raw_issuer_size); +if (IS_ERR (kid)) +return PTR_ERR (kid); +pr_debug ("authkeyid %*phN\n" , kid->len, kid->data); + ctx->cert->authority = kid;return 0 ; }crypto/asymmetric_keys/x509_parser.h +3
-2@@ -19,8 +19,9 @@ struct x509_certificate {struct public_key_signature sig;/* Signature parameters */ char *issuer;/* Name of certificate issuer */ char *subject;/* Name of certificate subject */ -char *fingerprint;/* Key fingerprint as hex */ -char *authority;/* Authority key fingerprint as hex */ +struct asymmetric_key_id *id;/* Issuer + serial number */ +struct asymmetric_key_id *skid;/* Subject key identifier */ +struct asymmetric_key_id *authority;/* Authority key identifier */ struct tm valid_from;struct tm valid_to;const void *tbs;/* Signed data */ crypto/asymmetric_keys/x509_public_key.c +51
-38@@ -25,7 +25,7 @@#include "x509_parser.h" static bool use_builtin_keys; -static char *ca_keyid; +static struct asymmetric_key_id *ca_keyid;#ifndef MODULE static int __init ca_keys_setup (char *str)@@ -33,10 +33,16 @@ static int __init ca_keys_setup(char *str)if (!str)/* default system keyring */ return 1 ; -if (strncmp (str,"id:" ,3 ) ==0 ) - ca_keyid = str;/* owner key 'id:xxxxxx' */ -else if (strcmp (str,"builtin" ) ==0 ) +if (strncmp (str,"id:" ,3 ) ==0 ) { +struct asymmetric_key_id *p; + p =asymmetric_key_hex_to_key_id (str); +if (p ==ERR_PTR (-EINVAL )) +pr_err ("Unparsable hex string in ca_keys\n" ); +else if (!IS_ERR (p)) + ca_keyid = p;/* owner key 'id:xxxxxx' */ + }else if (strcmp (str,"builtin" ) ==0 ) { use_builtin_keys = true; + }return 1 ; }@@ -46,31 +52,28 @@ __setup("ca_keys=", ca_keys_setup);/** * x509_request_asymmetric_key - Request a key by X.509 certificate params. * @keyring: The keys to search. -* @subject: The name of the subject to whom the key belongs. -* @key_id: The subject key ID as a hex string. +* @kid: The key ID. * * Find a key in the given keyring by subject name and key ID. These might, * for instance, be the issuer name and the authority key ID of an X.509 * certificate that needs to be verified. */ struct key *x509_request_asymmetric_key (struct key *keyring, -const char *subject, -const char *key_id) +const struct asymmetric_key_id *kid) {key_ref_t key; -size_t subject_len =strlen (subject), key_id_len =strlen (key_id); -char *id; +char *id, *p; -/* Construct an identifier "<subjname>:<keyid>". */ - id =kmalloc (subject_len +2 + key_id_len +1 ,GFP_KERNEL ); +/* Construct an identifier "id:<keyid>". */ + p = id =kmalloc (2 +1 + kid->len *2 +1 ,GFP_KERNEL );if (!id)return ERR_PTR (-ENOMEM ); -memcpy (id, subject, subject_len); - id[subject_len +0 ] =':' ; - id[subject_len +1 ] =' ' ; -memcpy (id + subject_len +2 , key_id, key_id_len); - id[subject_len +2 + key_id_len] =0 ; + *p++ ='i' ; + *p++ ='d' ; + *p++ =':' ; + p =bin2hex (p, kid->data, kid->len); + *p =0 ;pr_debug ("Look up: \"%s\"\n" , id);@@ -195,11 +198,10 @@ static int x509_validate_trust(struct x509_certificate *cert,if (!trust_keyring)return -EOPNOTSUPP ; -if (ca_keyid && !asymmetric_keyid_match (cert->authority, ca_keyid)) +if (ca_keyid && !asymmetric_key_id_same (cert->authority, ca_keyid))return -EPERM ; - key =x509_request_asymmetric_key (trust_keyring, - cert->issuer, cert->authority); + key =x509_request_asymmetric_key (trust_keyring, cert->authority);if (!IS_ERR (key)) {if (!use_builtin_keys ||test_bit (KEY_FLAG_BUILTIN , &key->flags))@@ -214,9 +216,11 @@ static int x509_validate_trust(struct x509_certificate *cert,*/ static int x509_key_preparse (struct key_preparsed_payload *prep) { +struct asymmetric_key_ids *kids;struct x509_certificate *cert; +const char *q;size_t srlen, sulen; -char *desc =NULL ; +char *desc =NULL , *p;int ret; cert =x509_cert_parse (prep->data, prep->datalen);@@ -249,19 +253,12 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) pkey_algo_name[cert->sig.pkey_algo], hash_algo_name[cert->sig.pkey_hash_algo]); -if (!cert->fingerprint) { -pr_warn ("Cert for '%s' must have a SubjKeyId extension\n" , - cert->subject); - ret = -EKEYREJECTED ; - goto error_free_cert; - } - cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; cert->pub->id_type =PKEY_ID_X509 ;/* Check the signature on the key if it appears to be self-signed */ if (!cert->authority || -strcmp (cert->fingerprint, cert->authority) ==0 ) { +asymmetric_key_id_same (cert->skid, cert->authority)) { ret =x509_check_signature (cert->pub, cert);/* self-signed */ if (ret <0 ) goto error_free_cert;@@ -273,31 +270,47 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)/* Propose a description */ sulen =strlen (cert->subject); - srlen =strlen (cert->fingerprint); + srlen = cert->raw_serial_size; + q = cert->raw_serial; +if (srlen >1 && *q ==0 ) { + srlen--; + q++; + } + ret = -ENOMEM ; - desc =kmalloc (sulen +2 + srlen +1 ,GFP_KERNEL ); + desc =kmalloc (sulen +2 + srlen *2 +1 ,GFP_KERNEL );if (!desc) goto error_free_cert; -memcpy (desc, cert->subject, sulen); - desc[sulen] =':' ; - desc[sulen +1 ] =' ' ; -memcpy (desc + sulen +2 , cert->fingerprint, srlen); - desc[sulen +2 + srlen] =0 ; + p =memcpy (desc, cert->subject, sulen); + p += sulen; + *p++ =':' ; + *p++ =' ' ; + p =bin2hex (p, q, srlen); + *p =0 ; + + kids =kmalloc (sizeof (struct asymmetric_key_ids ),GFP_KERNEL ); +if (!kids) + goto error_free_desc; + kids->id[0 ] = cert->id; + kids->id[1 ] = cert->skid;/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner); prep->type_data[0 ] = &public_key_subtype; - prep->type_data[1 ] = cert->fingerprint; + prep->type_data[1 ] = kids; prep->payload[0 ] = cert->pub; prep->description = desc; prep->quotalen =100 ;/* We've finished with the certificate */ cert->pub =NULL ; - cert->fingerprint =NULL ; + cert->id =NULL ; + cert->skid =NULL ; desc =NULL ; ret =0 ; +error_free_desc: +kfree (desc); error_free_cert:x509_free_certificate (cert);return ret;include/crypto/public_key.h +3
-2@@ -15,6 +15,7 @@#define _LINUX_PUBLIC_KEY_H#include <linux/mpi.h> +#include <keys/asymmetric-type.h> #include <crypto/hash_info.h> enum pkey_algo {@@ -98,8 +99,8 @@ struct key;extern int verify_signature (const struct key *key,const struct public_key_signature *sig); +struct asymmetric_key_id ;extern struct key *x509_request_asymmetric_key (struct key *keyring, -const char *issuer, -const char *key_id); +const struct asymmetric_key_id *kid);#endif /* _LINUX_PUBLIC_KEY_H */ Quoted 10 lines
Quoted 5 lines
--
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--
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--
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
David Howells
maintainerRe: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
In reply to Dmitry KasatkinQuoted 1 line · Dmitry Kasatkin
[ 132.820302] CR2: ffffffffffffffea
Looks like I leaked an error number somewhere.
David
Re: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
Quoted 7 lines · David Howells
Quoted 1 line · Dmitry Kasatkin
[ 132.820302] CR2: ffffffffffffffeaLooks like I leaked an error number somewhere.
David
Yes... I found..
Check patches I posted.
Also here
http://git.kernel.org/cgit/linux/kernel/git/kasatkin/linux-digsig.git/log/?h=keys-fixes
- Dmitry
David Howells
maintainerRe: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
In reply to Dmitry KasatkinQuoted 7 lines · Dmitry Kasatkin
Also I noticed that output of 'keyctl show' and 'cat
/proc/keys' output also has changed in respect of certificate ids..Those ids does not look any close to my kernel X509 X509v3 Subject Key Identifier, which is:
92:63:05:D6:DD:A6:6F:47:13:9E:B4:E3:CB:25:A6:AD:EF:52:7F:08
Quoted 8 lines
proc/keys shows
symmetri Magrathea: Glacier signing key:
d9e2e4c6951f1e83: X509.RSA6865612e68326732[]Very different ids..
How could I match certificate now?
There are two IDs available:
id: serial number + issuer
skid: subjKeyId + subject
You can use either of them and their content is somewhat negotiable. Note that they are both compound IDs at this point.
We have to move away from using subjKeyId for module signatures because we have to be able to deal with keys that don't have one. Blech, but the PKCS specs suck somewhat.
This is why I want to move to using detached-data PKCS#7 certs as the signature. We have the PKCS#7 handling in the kernel now for doing kexec.
David
Re: [PATCH 08/13] KEYS: Overhaul key identification when searching for asymmetric keys
Quoted 25 lines · David Howells
Quoted 7 lines · Dmitry Kasatkin
Also I noticed that output of 'keyctl show' and 'cat
/proc/keys' output also has changed in respect of certificate ids..Those ids does not look any close to my kernel X509 X509v3 Subject Key Identifier, which is:
92:63:05:D6:DD:A6:6F:47:13:9E:B4:E3:CB:25:A6:AD:EF:52:7F:08Quoted 8 lines
proc/keys shows
symmetri Magrathea: Glacier signing key:
d9e2e4c6951f1e83: X509.RSA6865612e68326732[]Very different ids..
How could I match certificate now?
There are two IDs available:
id: serial number + issuer
skid: subjKeyId + subjectYou can use either of them and their content is somewhat negotiable. Note that they are both compound IDs at this point.
Quoted 6 lines
We have to move away from using subjKeyId for module signatures because we have to be able to deal with keys that don't have one. Blech, but the PKCS specs suck somewhat.
This is why I want to move to using detached-data PKCS#7 certs as the signature. We have the PKCS#7 handling in the kernel now for doing kexec.
I looked to the code and understood...
See my patches please.
- Dmitry
Quoted 6 lines
David
--
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
David Howells
maintainerPATCH 9/13 PKCS#7: Better handling of unsupported crypto
In reply to David HowellsProvide better handling of unsupported crypto when verifying a PKCS#7 message. If we can't bridge the gap between a pair of X.509 certs or between a signed info block and an X.509 cert because it involves some crypto we don't support, that's not necessarily the end of the world as there may be other ways points at which we can intersect with a ring of trusted keys.
Instead, only produce ENOPKG immediately if all the signed info blocks in a PKCS#7 message require unsupported crypto to bridge to the first X.509 cert. Otherwise, we defer the generation of ENOPKG until we get ENOKEY during trust validation.
---
crypto/asymmetric_keys/pkcs7_parser.h | 2 + crypto/asymmetric_keys/pkcs7_trust.c | 20 +++++++----- crypto/asymmetric_keys/pkcs7_verify.c | 49 ++++++++++++++++++++++++++++-- crypto/asymmetric_keys/x509_parser.h | 1 + crypto/asymmetric_keys/x509_public_key.c | 13 +++++++- 5 files changed, 70 insertions(+), 15 deletions(-)
crypto/asymmetric_keys/pkcs7_parser.h +2 -0
@@ -23,6 +23,7 @@ struct pkcs7_signed_info {struct x509_certificate *signer;/* Signing certificate (in msg->certs) */ unsigned index;bool trusted; +bool unsupported_crypto;/* T if not usable due to missing crypto */ /* Message digest - the digest of the Content Data (or NULL) */ const void *msgdigest;
@@ -49,6 +50,7 @@ struct pkcs7_message {struct x509_certificate *certs;/* Certificate list */ struct x509_certificate *crl;/* Revocation list */ struct pkcs7_signed_info *signed_infos; +bool unsupported_crypto;/* T if some part of the chains had missing crypto */ /* Content Data (or NULL) */ enum OID data_type;/* Type of Data */
crypto/asymmetric_keys/pkcs7_trust.c +11 -9
@@ -35,6 +35,11 @@ int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,kenter (",%u," , sinfo->index); +if (sinfo->unsupported_crypto) { +kleave (" = -ENOPKG [cached]" ); +return -ENOPKG ; + } +for (x509 = sinfo->signer; x509; x509 = x509->signer) {if (x509->seen) {if (x509->verified) {
@@ -139,7 +144,8 @@ int pkcs7_validate_trust(struct pkcs7_message *pkcs7, {struct pkcs7_signed_info *sinfo;struct x509_certificate *p; -int cached_ret =0 , ret; +int cached_ret = pkcs7->unsupported_crypto ? -ENOPKG : -ENOKEY ; +int ret;for (p = pkcs7->certs; p; p = p->next) p->seen = false;
@@ -147,16 +153,12 @@ int pkcs7_validate_trust(struct pkcs7_message *pkcs7,for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) { ret =pkcs7_validate_trust_one (pkcs7, sinfo, trust_keyring);if (ret <0 ) { -if (ret == -ENOPKG ) { - cached_ret = -ENOPKG ; - }else if (ret == -ENOKEY ) { -if (cached_ret ==0 ) - cached_ret = -ENOKEY ; - }else { -return ret; - } +if (ret == -ENOPKG || ret == -ENOKEY ) +continue ; +return ret; } *_trusted |= sinfo->trusted; + cached_ret =0 ; }return cached_ret;
crypto/asymmetric_keys/pkcs7_verify.c +45 -4
88 lines not on this page · raw message
crypto/asymmetric_keys/x509_parser.h +1 -0
@@ -38,6 +38,7 @@ struct x509_certificate {bool seen;/* Infinite recursion prevention */ bool verified;bool trusted; +bool unsupported_crypto;/* T if can't be verified due to missing crypto */ };/*
crypto/asymmetric_keys/x509_public_key.c +11 -2
@@ -115,6 +115,8 @@ int x509_get_sig_params(struct x509_certificate *cert)pr_devel ("==>%s()\n" , __func__); +if (cert->unsupported_crypto) +return -ENOPKG ;if (cert->sig.rsa.s)return 0 ;
@@ -127,8 +129,13 @@ int x509_get_sig_params(struct x509_certificate *cert)* big the hash operational data will be. */ tfm =crypto_alloc_shash (hash_algo_name[cert->sig.pkey_hash_algo],0 ,0 ); -if (IS_ERR (tfm)) -return (PTR_ERR (tfm) == -ENOENT ) ? -ENOPKG :PTR_ERR (tfm); +if (IS_ERR (tfm)) { +if (PTR_ERR (tfm) == -ENOENT ) { + cert->unsupported_crypto = true; +return -ENOPKG ; + } +return PTR_ERR (tfm); + } desc_size =crypto_shash_descsize (tfm) +sizeof (*desc); digest_size =crypto_shash_digestsize (tfm);
@@ -175,6 +182,8 @@ int x509_check_signature(const struct public_key *pub,return ret; ret =public_key_verify_signature (pub, &cert->sig); +if (ret == -ENOPKG ) + cert->unsupported_crypto = true;pr_debug ("Cert Verification: %d\n" , ret);return ret; }
David Howells
maintainerPATCH 10/13 PKCS#7: Handle PKCS#7 messages that contain no X.509 certs
In reply to David HowellsThe X.509 certificate list in a PKCS#7 message is optional. To save space, we can omit the inclusion of any X.509 certificates if we are sure that we can look the relevant public key up by the serial number and issuer given in a signed info block.
This also supports use of a signed info block for which we don't have a matching X.509 cert giving in a populated certificate list.
---
crypto/asymmetric_keys/pkcs7_trust.c | 48 +++++++++++++++++++++++++-------- crypto/asymmetric_keys/pkcs7_verify.c | 16 ++++++++--- 2 files changed, 47 insertions(+), 17 deletions(-)
crypto/asymmetric_keys/pkcs7_trust.c +36 -12
@@ -55,13 +55,16 @@ int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* keys. */ key =x509_request_asymmetric_key (trust_keyring, x509->id); -if (!IS_ERR (key)) +if (!IS_ERR (key)) {/* One of the X.509 certificates in the PKCS#7 message * is apparently the same as one we already trust. * Verify that the trusted variant can also validate * the signature on the descendant. */ +pr_devel ("sinfo %u: Cert %u as key %x\n" , + sinfo->index, x509->index,key_serial (key)); goto matched; + }if (key ==ERR_PTR (-ENOMEM ))return -ENOMEM ;
@@ -81,15 +84,34 @@ 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->issuer || !last->authority) { -kleave (" = -ENOKEY [no backref]" ); -return -ENOKEY ; +if (last && last->authority) { + key =x509_request_asymmetric_key (trust_keyring, last->authority); +if (!IS_ERR (key)) { +pr_devel ("sinfo %u: Root cert %u signer is key %x\n" , + sinfo->index, x509->index,key_serial (key)); + x509 = last; + goto matched; + } +if (PTR_ERR (key) != -ENOKEY ) +return PTR_ERR (key); } - key =x509_request_asymmetric_key (trust_keyring, last->authority); -if (IS_ERR (key)) -return PTR_ERR (key) == -ENOMEM ? -ENOMEM : -ENOKEY ; - x509 = last; +/* As a last resort, see if we have a trusted public key that matches +* the signed info directly. +*/ + key =x509_request_asymmetric_key (trust_keyring, + sinfo->signing_cert_id); +if (!IS_ERR (key)) { +pr_devel ("sinfo %u: Direct signer is key %x\n" , + sinfo->index,key_serial (key)); + x509 =NULL ; + goto matched; + } +if (PTR_ERR (key) != -ENOKEY ) +return PTR_ERR (key); + +kleave (" = -ENOKEY [no backref]" ); +return -ENOKEY ; matched: ret =verify_signature (key, sig);
@@ -103,10 +125,12 @@ matched: } verified: - x509->verified = true; -for (p = sinfo->signer; p != x509; p = p->signer) { - p->verified = true; - p->trusted = trusted; +if (x509) { + x509->verified = true; +for (p = sinfo->signer; p != x509; p = p->signer) { + p->verified = true; + p->trusted = trusted; + } } sinfo->trusted = trusted;kleave (" = 0" );
crypto/asymmetric_keys/pkcs7_verify.c +11 -5
@@ -154,10 +154,13 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7,return 0 ; } -pr_warn ("Sig %u: Issuing X.509 cert not found (#%*ph)\n" , - sinfo->index, - sinfo->signing_cert_id->len, sinfo->signing_cert_id->data); -return -ENOKEY ; +/* The relevant X.509 cert isn't found here, but it might be found in +* the trust keyring. +*/ +pr_debug ("Sig %u: Issuing X.509 cert not found (#%*phN)\n" , + sinfo->index, + sinfo->signing_cert_id->len, sinfo->signing_cert_id->data); +return 0 ; }/*
@@ -277,11 +280,14 @@ static int pkcs7_verify_one(struct pkcs7_message *pkcs7,if (ret <0 )return ret; -/* Find the key for the signature */ +/* Find the key for the signature if there is one */ ret =pkcs7_find_key (pkcs7, sinfo);if (ret <0 )return ret; +if (!sinfo->signer) +return 0 ; +pr_devel ("Using X.509[%u] for sig %u\n" , sinfo->signer->index, sinfo->index);
David Howells
maintainerPATCH 11/13 PKCS#7: Allow detached data to be supplied for signature checking purposes
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.
---
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
@@ -362,3 +362,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
@@ -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 12/13 MODSIGN: Provide a utility to append a PKCS#7 signature to a module
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 | 189 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 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 +189 -0
189 lines not on this page · raw message
David Howells
maintainerPATCH 13/13 MODSIGN: Use PKCS#7 messages as module signatures
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.
---
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
@@ -1837,6 +1837,7 @@ config MODULE_SIG select ASN1 select OID_REGISTRY select X509_CERTIFICATE_PARSER + select CONFIG_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 +HOSTCFLAGS_sign-file.o = -lcrypto always := $(hostprogs-y) $(hostprogs-m)
scripts/sign-file deleted +0 -421
421 lines not on this page · raw message