Skip to content

Commit d3e4f41

Browse files
committed
Merge tag 'keys-pkcs7-20140916' into keys-next
Changes for next to improve the matching of asymmetric keys and to improve the handling of PKCS#7 certificates: (1) Provide a method to preparse the data supplied for matching a key. This permits they key type to extract out the bits it needs for matching once only. Further, the type of search (direct lookup or iterative) can be set and the function used to actually check the match can be set by preparse rather than being hard coded for the type. (2) 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. (3) 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. (4) 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. These must go on top of the PKCS#7 parser cleanup fixes. Signed-off-by: David Howells <dhowells@redhat.com>
2 parents 1c9c115 + 757932e commit d3e4f41

34 files changed

Lines changed: 649 additions & 305 deletions

‎Documentation/security/keys.txt‎

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,11 @@ payload contents" for more information.
888888
const char *callout_info);
889889

890890
This is used to request a key or keyring with a description that matches
891-
the description specified according to the key type's match function. This
892-
permits approximate matching to occur. If callout_string is not NULL, then
893-
/sbin/request-key will be invoked in an attempt to obtain the key from
894-
userspace. In that case, callout_string will be passed as an argument to
895-
the program.
891+
the description specified according to the key type's match_preparse()
892+
method. This permits approximate matching to occur. If callout_string is
893+
not NULL, then /sbin/request-key will be invoked in an attempt to obtain
894+
the key from userspace. In that case, callout_string will be passed as an
895+
argument to the program.
896896

897897
Should the function fail error ENOKEY, EKEYEXPIRED or EKEYREVOKED will be
898898
returned.
@@ -1170,7 +1170,7 @@ The structure has a number of fields, some of which are mandatory:
11701170
The method should return 0 if successful or a negative error code
11711171
otherwise.
11721172

1173-
1173+
11741174
(*) void (*free_preparse)(struct key_preparsed_payload *prep);
11751175

11761176
This method is only required if the preparse() method is provided,
@@ -1225,16 +1225,55 @@ The structure has a number of fields, some of which are mandatory:
12251225
It is safe to sleep in this method.
12261226

12271227

1228-
(*) int (*match)(const struct key *key, const void *desc);
1228+
(*) int (*match_preparse)(struct key_match_data *match_data);
1229+
1230+
This method is optional. It is called when a key search is about to be
1231+
performed. It is given the following structure:
12291232

1230-
This method is called to match a key against a description. It should
1231-
return non-zero if the two match, zero if they don't.
1233+
struct key_match_data {
1234+
bool (*cmp)(const struct key *key,
1235+
const struct key_match_data *match_data);
1236+
const void *raw_data;
1237+
void *preparsed;
1238+
unsigned lookup_type;
1239+
};
12321240

1233-
This method should not need to lock the key in any way. The type and
1234-
description can be considered invariant, and the payload should not be
1235-
accessed (the key may not yet be instantiated).
1241+
On entry, raw_data will be pointing to the criteria to be used in matching
1242+
a key by the caller and should not be modified. (*cmp)() will be pointing
1243+
to the default matcher function (which does an exact description match
1244+
against raw_data) and lookup_type will be set to indicate a direct lookup.
12361245

1237-
It is not safe to sleep in this method; the caller may hold spinlocks.
1246+
The following lookup_type values are available:
1247+
1248+
[*] KEYRING_SEARCH_LOOKUP_DIRECT - A direct lookup hashes the type and
1249+
description to narrow down the search to a small number of keys.
1250+
1251+
[*] KEYRING_SEARCH_LOOKUP_ITERATE - An iterative lookup walks all the
1252+
keys in the keyring until one is matched. This must be used for any
1253+
search that's not doing a simple direct match on the key description.
1254+
1255+
The method may set cmp to point to a function of its choice that does some
1256+
other form of match, may set lookup_type to KEYRING_SEARCH_LOOKUP_ITERATE
1257+
and may attach something to the preparsed pointer for use by (*cmp)().
1258+
(*cmp)() should return true if a key matches and false otherwise.
1259+
1260+
If preparsed is set, it may be necessary to use the match_free() method to
1261+
clean it up.
1262+
1263+
The method should return 0 if successful or a negative error code
1264+
otherwise.
1265+
1266+
It is permitted to sleep in this method, but (*cmp)() may not sleep as
1267+
locks will be held over it.
1268+
1269+
If match_preparse() is not provided, keys of this type will be matched
1270+
exactly by their description.
1271+
1272+
1273+
(*) void (*match_free)(struct key_match_data *match_data);
1274+
1275+
This method is optional. If given, it called to clean up
1276+
match_data->preparsed after a successful call to match_preparse().
12381277

12391278

12401279
(*) void (*revoke)(struct key *key);

‎crypto/asymmetric_keys/asymmetric_keys.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
* 2 of the Licence, or (at your option) any later version.
1010
*/
1111

12-
int asymmetric_keyid_match(const char *kid, const char *id);
12+
extern bool asymmetric_match_key_ids(const struct asymmetric_key_ids *kids,
13+
const struct asymmetric_key_id *match_id);
1314

14-
static inline const char *asymmetric_key_id(const struct key *key)
15+
extern struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id);
16+
17+
static inline
18+
const struct asymmetric_key_ids *asymmetric_key_ids(const struct key *key)
1519
{
1620
return key->type_data.p[1];
1721
}

0 commit comments

Comments
 (0)