Skip to content

Commit f1b731d

Browse files
Dmitry Kasatkindhowells
authored andcommitted
KEYS: Restore partial ID matching functionality for asymmetric keys
Bring back the functionality whereby an asymmetric key can be matched with a partial match on one of its IDs. Whilst we're at it, allow for the possibility of having an increased number of IDs. Reported-by: Dmitry Kasatkin <d.kasatkin@samsung.com> Signed-off-by: Dmitry Kasatkin <d.kasatkin@samsung.com> Signed-off-by: David Howells <dhowells@redhat.com>
1 parent dd2f6c4 commit f1b731d

6 files changed

Lines changed: 81 additions & 25 deletions

File tree

‎crypto/asymmetric_keys/asymmetric_keys.h‎

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

12-
extern bool asymmetric_match_key_ids(const struct asymmetric_key_ids *kids,
13-
const struct asymmetric_key_id *match_id);
14-
1512
extern struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id);
1613

1714
static inline

‎crypto/asymmetric_keys/asymmetric_type.c‎

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,44 @@ bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
6565
}
6666
EXPORT_SYMBOL_GPL(asymmetric_key_id_same);
6767

68+
/**
69+
* asymmetric_key_id_partial - Return true if two asymmetric keys IDs
70+
* partially match
71+
* @kid_1, @kid_2: The key IDs to compare
72+
*/
73+
bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,
74+
const struct asymmetric_key_id *kid2)
75+
{
76+
if (!kid1 || !kid2)
77+
return false;
78+
if (kid1->len < kid2->len)
79+
return false;
80+
return memcmp(kid1->data + (kid1->len - kid2->len),
81+
kid2->data, kid2->len) == 0;
82+
}
83+
EXPORT_SYMBOL_GPL(asymmetric_key_id_partial);
84+
6885
/**
6986
* asymmetric_match_key_ids - Search asymmetric key IDs
7087
* @kids: The list of key IDs to check
7188
* @match_id: The key ID we're looking for
89+
* @match: The match function to use
7290
*/
73-
bool asymmetric_match_key_ids(const struct asymmetric_key_ids *kids,
74-
const struct asymmetric_key_id *match_id)
91+
static bool asymmetric_match_key_ids(
92+
const struct asymmetric_key_ids *kids,
93+
const struct asymmetric_key_id *match_id,
94+
bool (*match)(const struct asymmetric_key_id *kid1,
95+
const struct asymmetric_key_id *kid2))
7596
{
97+
int i;
98+
7699
if (!kids || !match_id)
77100
return false;
78-
if (asymmetric_key_id_same(kids->id[0], match_id))
79-
return true;
80-
if (asymmetric_key_id_same(kids->id[1], match_id))
81-
return true;
101+
for (i = 0; i < ARRAY_SIZE(kids->id); i++)
102+
if (match(kids->id[i], match_id))
103+
return true;
82104
return false;
83105
}
84-
EXPORT_SYMBOL_GPL(asymmetric_match_key_ids);
85106

86107
/**
87108
* asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
@@ -113,15 +134,29 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
113134
}
114135

115136
/*
116-
* Match asymmetric keys by ID.
137+
* Match asymmetric keys by an exact match on an ID.
117138
*/
118139
static bool asymmetric_key_cmp(const struct key *key,
119140
const struct key_match_data *match_data)
120141
{
121142
const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
122143
const struct asymmetric_key_id *match_id = match_data->preparsed;
123144

124-
return asymmetric_match_key_ids(kids, match_id);
145+
return asymmetric_match_key_ids(kids, match_id,
146+
asymmetric_key_id_same);
147+
}
148+
149+
/*
150+
* Match asymmetric keys by a partial match on an IDs.
151+
*/
152+
static bool asymmetric_key_cmp_partial(const struct key *key,
153+
const struct key_match_data *match_data)
154+
{
155+
const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
156+
const struct asymmetric_key_id *match_id = match_data->preparsed;
157+
158+
return asymmetric_match_key_ids(kids, match_id,
159+
asymmetric_key_id_partial);
125160
}
126161

127162
/*
@@ -131,7 +166,8 @@ static bool asymmetric_key_cmp(const struct key *key,
131166
* There are some specifiers for matching key IDs rather than by the key
132167
* description:
133168
*
134-
* "id:<id>" - request a key by any available ID
169+
* "id:<id>" - find a key by partial match on any available ID
170+
* "ex:<id>" - find a key by exact match on any available ID
135171
*
136172
* These have to be searched by iteration rather than by direct lookup because
137173
* the key is hashed according to its description.
@@ -141,13 +177,20 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data)
141177
struct asymmetric_key_id *match_id;
142178
const char *spec = match_data->raw_data;
143179
const char *id;
180+
bool (*cmp)(const struct key *, const struct key_match_data *) =
181+
asymmetric_key_cmp;
144182

145183
if (!spec || !*spec)
146184
return -EINVAL;
147185
if (spec[0] == 'i' &&
148186
spec[1] == 'd' &&
149187
spec[2] == ':') {
150188
id = spec + 3;
189+
cmp = asymmetric_key_cmp_partial;
190+
} else if (spec[0] == 'e' &&
191+
spec[1] == 'x' &&
192+
spec[2] == ':') {
193+
id = spec + 3;
151194
} else {
152195
goto default_match;
153196
}
@@ -157,7 +200,7 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data)
157200
return PTR_ERR(match_id);
158201

159202
match_data->preparsed = match_id;
160-
match_data->cmp = asymmetric_key_cmp;
203+
match_data->cmp = cmp;
161204
match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
162205
return 0;
163206

@@ -251,6 +294,7 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
251294
{
252295
struct asymmetric_key_subtype *subtype = prep->type_data[0];
253296
struct asymmetric_key_ids *kids = prep->type_data[1];
297+
int i;
254298

255299
pr_devel("==>%s()\n", __func__);
256300

@@ -259,8 +303,8 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
259303
module_put(subtype->owner);
260304
}
261305
if (kids) {
262-
kfree(kids->id[0]);
263-
kfree(kids->id[1]);
306+
for (i = 0; i < ARRAY_SIZE(kids->id); i++)
307+
kfree(kids->id[i]);
264308
kfree(kids);
265309
}
266310
kfree(prep->description);

‎crypto/asymmetric_keys/pkcs7_trust.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
5454
/* Look to see if this certificate is present in the trusted
5555
* keys.
5656
*/
57-
key = x509_request_asymmetric_key(trust_keyring, x509->id);
57+
key = x509_request_asymmetric_key(trust_keyring, x509->id,
58+
false);
5859
if (!IS_ERR(key)) {
5960
/* One of the X.509 certificates in the PKCS#7 message
6061
* is apparently the same as one we already trust.
@@ -85,7 +86,8 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
8586
* trusted keys.
8687
*/
8788
if (last && last->authority) {
88-
key = x509_request_asymmetric_key(trust_keyring, last->authority);
89+
key = x509_request_asymmetric_key(trust_keyring, last->authority,
90+
false);
8991
if (!IS_ERR(key)) {
9092
x509 = last;
9193
pr_devel("sinfo %u: Root cert %u signer is key %x\n",
@@ -100,7 +102,8 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
100102
* the signed info directly.
101103
*/
102104
key = x509_request_asymmetric_key(trust_keyring,
103-
sinfo->signing_cert_id);
105+
sinfo->signing_cert_id,
106+
false);
104107
if (!IS_ERR(key)) {
105108
pr_devel("sinfo %u: Direct signer is key %x\n",
106109
sinfo->index, key_serial(key));

‎crypto/asymmetric_keys/x509_public_key.c‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ __setup("ca_keys=", ca_keys_setup);
5353
* x509_request_asymmetric_key - Request a key by X.509 certificate params.
5454
* @keyring: The keys to search.
5555
* @kid: The key ID.
56+
* @partial: Use partial match if true, exact if false.
5657
*
5758
* Find a key in the given keyring by subject name and key ID. These might,
5859
* for instance, be the issuer name and the authority key ID of an X.509
5960
* certificate that needs to be verified.
6061
*/
6162
struct key *x509_request_asymmetric_key(struct key *keyring,
62-
const struct asymmetric_key_id *kid)
63+
const struct asymmetric_key_id *kid,
64+
bool partial)
6365
{
6466
key_ref_t key;
6567
char *id, *p;
@@ -69,8 +71,13 @@ struct key *x509_request_asymmetric_key(struct key *keyring,
6971
if (!id)
7072
return ERR_PTR(-ENOMEM);
7173

72-
*p++ = 'i';
73-
*p++ = 'd';
74+
if (partial) {
75+
*p++ = 'i';
76+
*p++ = 'd';
77+
} else {
78+
*p++ = 'e';
79+
*p++ = 'x';
80+
}
7481
*p++ = ':';
7582
p = bin2hex(p, kid->data, kid->len);
7683
*p = 0;
@@ -207,10 +214,11 @@ static int x509_validate_trust(struct x509_certificate *cert,
207214
if (!trust_keyring)
208215
return -EOPNOTSUPP;
209216

210-
if (ca_keyid && !asymmetric_key_id_same(cert->authority, ca_keyid))
217+
if (ca_keyid && !asymmetric_key_id_partial(cert->authority, ca_keyid))
211218
return -EPERM;
212219

213-
key = x509_request_asymmetric_key(trust_keyring, cert->authority);
220+
key = x509_request_asymmetric_key(trust_keyring, cert->authority,
221+
false);
214222
if (!IS_ERR(key)) {
215223
if (!use_builtin_keys
216224
|| test_bit(KEY_FLAG_BUILTIN, &key->flags))

‎include/crypto/public_key.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ extern int verify_signature(const struct key *key,
101101

102102
struct asymmetric_key_id;
103103
extern struct key *x509_request_asymmetric_key(struct key *keyring,
104-
const struct asymmetric_key_id *kid);
104+
const struct asymmetric_key_id *kid,
105+
bool partial);
105106

106107
#endif /* _LINUX_PUBLIC_KEY_H */

‎include/keys/asymmetric-type.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ struct asymmetric_key_ids {
5151
extern bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
5252
const struct asymmetric_key_id *kid2);
5353

54+
extern bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,
55+
const struct asymmetric_key_id *kid2);
56+
5457
extern struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
5558
size_t len_1,
5659
const void *val_2,

0 commit comments

Comments
 (0)