keys: X.509 public key issuer lookup without AKID
Subsystems (3)
PATCH v2 1/1 keys: X.509 public key issuer lookup without AKID
There are non-root X.509 v3 certificates in use there that contain no Authority Key Identifier extension (RFC5280 section 4.2.1.1). The asymmetric key's issuer key IDs 1 and 2 for those certificates are generated from the AKID data so current code has no way to look up the issuer certificate for verification. Add a third ID blob to the arrays in both asymmetric_key_ids (for certficate subject) and in the public_keys_signature's auth_ids (for issuer lookup), using just raw subject and issuer DNs from the certificate. There's no other place this data is currently saved that could be used in find_asymmetric_key. Adapt asymmetric_key_ids() and the callers to use the third ID for lookups when none of the other two are available. Attempt to keep the logic intact when they are, to minimise behaviour changes. Adapt the restrict functions' NULL-checks to include that ID too. The lookup logic in pkcs7_verify.c is not modified, the AKID extensions are still required there.
I believe this implements what (2) in the struct asymmetric_key_id comment (include/keys/asymmetric-type.h) talks about already, so that comment isn't modified. It's also how "openssl verify" looks up issuer certificates without the AKID available.
Internally the search specifier string generated for the key lookup in find_asymmetric_key() uses a new prefix that tells asymmetric_key_match_preparse to only match the data against the raw Distinguished Name in the third ID and shouldn't conflict with search specifiers already in use.
Lookups are unambiguous only provided that the CAs respect the condition in RFC5280 4.2.1.1 that the AKID may only be omitted if the CA uses a single signing key.
---
v2:
- focus the commit message on the implementation
- shorten the prefix used in find_asymmetric_key
- clarify find_asymmetric_key comment
crypto/asymmetric_keys/asymmetric_type.c | 52 +++++++++++++++++------ crypto/asymmetric_keys/pkcs7_trust.c | 6 +-- crypto/asymmetric_keys/restrict.c | 48 ++++++++++++--------- crypto/asymmetric_keys/x509_cert_parser.c | 10 +++++ crypto/asymmetric_keys/x509_public_key.c | 10 +++++ include/crypto/public_key.h | 2 +- include/keys/asymmetric-type.h | 3 +- 7 files changed, 95 insertions(+), 36 deletions(-)
crypto/asymmetric_keys/asymmetric_type.c +40 -12
@@ -36,8 +36,11 @@ static DECLARE_RWSEM(asymmetric_key_parsers_sem);* find_asymmetric_key - Find a key by ID. * @keyring: The keys to search. * @id_0: The first ID to look for or NULL. -* @id_1: The second ID to look for or NULL. -* @partial: Use partial match if true, exact if false. +* @id_1: The second ID to look for or NULL, matched together with @id_0 +* against @keyring keys' id[0] and id[1]. +* @id_2: The fallback ID to match against @keyring keys' id[2] if both of the +* other IDs are NULL. +* @partial: Use partial match for @id_0 and @id_1 if true, exact if false. * * Find a key in the given keyring by identifier. The preferred identifier is * the id_0 and the fallback identifier is the id_1. If both are given, the
@@ -46,6 +49,7 @@ static DECLARE_RWSEM(asymmetric_key_parsers_sem);struct key *find_asymmetric_key (struct key *keyring,const struct asymmetric_key_id *id_0,const struct asymmetric_key_id *id_1, +const struct asymmetric_key_id *id_2,bool partial) {struct key *key;
@@ -54,14 +58,17 @@ struct key *find_asymmetric_key(struct key *keyring,char *req, *p;int len; -BUG_ON (!id_0 && !id_1); +BUG_ON (!id_0 && !id_1 && !id_2);if (id_0) { lookup = id_0->data; len = id_0->len; - }else { + }else if (id_1) { lookup = id_1->data; len = id_1->len; + }else { + lookup = id_2->data; + len = id_2->len; }/* Construct an identifier "id:<keyid>". */
@@ -69,7 +76,10 @@ struct key *find_asymmetric_key(struct key *keyring,if (!req)return ERR_PTR (-ENOMEM ); -if (partial) { +if (!id_0 && !id_1) { + *p++ ='d' ; + *p++ ='n' ; + }else if (partial) { *p++ ='i' ; *p++ ='d' ; }else {
@@ -183,8 +193,8 @@ bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,EXPORT_SYMBOL_GPL (asymmetric_key_id_partial);/** -* asymmetric_match_key_ids - Search asymmetric key IDs -* @kids: The list of key IDs to check +* asymmetric_match_key_ids - Search asymmetric key IDs 1 & 2 +* @kids: The pair of key IDs to check * @match_id: The key ID we're looking for * @match: The match function to use */
@@ -198,7 +208,7 @@ static bool asymmetric_match_key_ids(if (!kids || !match_id)return false; -for (i =0 ; i <ARRAY_SIZE (kids->id); i++) +for (i =0 ; i <2 ; i++)if (match (kids->id[i], match_id))return true;return false;
@@ -242,7 +252,7 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id) }/* -* Match asymmetric keys by an exact match on an ID. +* Match asymmetric keys by an exact match on one of the first two IDs. */ static bool asymmetric_key_cmp (const struct key *key,const struct key_match_data *match_data)
@@ -255,7 +265,7 @@ static bool asymmetric_key_cmp(const struct key *key, }/* -* Match asymmetric keys by a partial match on an IDs. +* Match asymmetric keys by a partial match on one of the first two IDs. */ static bool asymmetric_key_cmp_partial (const struct key *key,const struct key_match_data *match_data)
@@ -267,6 +277,18 @@ static bool asymmetric_key_cmp_partial(const struct key *key, asymmetric_key_id_partial); } +/* +* Match asymmetric keys by an exact match on the third IDs. +*/ +static bool asymmetric_key_cmp_name (const struct key *key, +const struct key_match_data *match_data) +{ +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *match_id = match_data->preparsed; + +return kids &&asymmetric_key_id_same (kids->id[2 ], match_id); +} +/* * Preparse the match criterion. If we don't set lookup_type and cmp, * the default will be an exact match on the key description.
@@ -274,8 +296,9 @@ static bool asymmetric_key_cmp_partial(const struct key *key,* There are some specifiers for matching key IDs rather than by the key * description: * -* "id:<id>" - find a key by partial match on any available ID -* "ex:<id>" - find a key by exact match on any available ID +* "id:<id>" - find a key by partial match on one of the first two IDs +* "ex:<id>" - find a key by exact match on one of the first two IDs +* "dn:<id>" - find a key by exact match on the third ID * * These have to be searched by iteration rather than by direct lookup because * the key is hashed according to its description.
@@ -299,6 +322,11 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data) spec[1] == 'x' && spec[2] == ':') { id = spec +3 ; + } else if (spec[0] == 'd' && + spec[1] == 'n' && + spec[2] == ':') { + id = spec +3 ; + cmp = asymmetric_key_cmp_name; } else { goto default_match; }
crypto/asymmetric_keys/pkcs7_trust.c +3 -3
@@ -48,7 +48,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* keys. */ key =find_asymmetric_key (trust_keyring, - x509->id, x509->skid, false); + x509->id, x509->skid,NULL , false);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,7 +82,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7, key =find_asymmetric_key (trust_keyring, last->sig->auth_ids[0 ], last->sig->auth_ids[1 ], - false); +NULL , false);if (!IS_ERR (key)) { x509 = last; pr_devel("sinfo %u: Root cert %u signer is key %x\n",
@@ -97,7 +97,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* the signed info directly. */ key =find_asymmetric_key (trust_keyring, - sinfo->sig->auth_ids[0 ],NULL , false); + sinfo->sig->auth_ids[0 ],NULL ,NULL , false);if (!IS_ERR (key)) {pr_devel ("sinfo %u: Direct signer is key %x\n" , sinfo->index,key_serial (key));
crypto/asymmetric_keys/restrict.c +29 -19
103 lines not on this page · raw message
crypto/asymmetric_keys/x509_cert_parser.c +10 -0
@@ -415,8 +415,18 @@ int x509_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; +struct asymmetric_key_id *kid; + ctx->cert->raw_issuer = value; ctx->cert->raw_issuer_size = vlen; + +if (!ctx->cert->sig->auth_ids[2 ]) { + kid =asymmetric_key_generate_id (value, vlen,"" ,0 ); +if (IS_ERR (kid)) +return PTR_ERR (kid); + ctx->cert->sig->auth_ids[2 ] = kid; + } +return x509_fabricate_name (ctx, hdrlen, tag, &ctx->cert->issuer, vlen); }
crypto/asymmetric_keys/x509_public_key.c +10 -0
@@ -221,6 +221,13 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) goto error_free_desc; kids->id[0 ] = cert->id; kids->id[1 ] = cert->skid; + kids->id[2 ] =asymmetric_key_generate_id (cert->raw_subject, + cert->raw_subject_size, +"" ,0 ); +if (IS_ERR (kids->id[2 ])) { + ret =PTR_ERR (kids->id[2 ]); + goto error_free_kids; + }/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner);
@@ -237,8 +244,11 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) cert->skid =NULL ; cert->sig =NULL ; desc =NULL ; + kids =NULL ; ret =0 ; +error_free_kids: +kfree (kids); error_free_desc:kfree (desc); error_free_cert:
include/crypto/public_key.h +1 -1
@@ -37,7 +37,7 @@ extern void public_key_free(struct public_key *key);* Public key cryptography signature data */ struct public_key_signature { -struct asymmetric_key_id *auth_ids[2 ]; +struct asymmetric_key_id *auth_ids[3 ];u8 *s;/* Signature */ u32 s_size;/* Number of bytes in signature */ u8 *digest;
include/keys/asymmetric-type.h +2 -1
@@ -53,7 +53,7 @@ struct asymmetric_key_id { };struct asymmetric_key_ids { -void *id[2 ]; +void *id[3 ]; }; extern bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
@@ -75,6 +75,7 @@ const struct asymmetric_key_ids *asymmetric_key_ids(const struct key *key)extern struct key *find_asymmetric_key (struct key *keyring,const struct asymmetric_key_id *id_0,const struct asymmetric_key_id *id_1, +const struct asymmetric_key_id *id_2,bool partial);/*
Signature · 1 line
2.27.0
Jarkko Sakkinen
maintainerQuoted 16 lines · Andrew Zaborowski
There are non-root X.509 v3 certificates in use there that contain no Authority Key Identifier extension (RFC5280 section 4.2.1.1). The asymmetric key's issuer key IDs 1 and 2 for those certificates are generated from the AKID data so current code has no way to look up the issuer certificate for verification. Add a third ID blob to the arrays in both
asymmetric_key_ids(for certficate subject) and in the public_keys_signature'sauth_ids(for issuer lookup), using just raw subject and issuer DNs from the certificate. There's no other place this data is currently saved that could be used infind_asymmetric_key. Adaptasymmetric_key_ids()and the callers to use the third ID for lookups when none of the other two are available. Attempt to keep the logic intact when they are, to minimise behaviour changes. Adapt the restrict functions' NULL-checks to include that ID too. The lookup logic inpkcs7_verify.cis not modified, the AKID extensions are still required there.
Quoted 4 lines
I believe this implements what (2) in the struct
asymmetric_key_idcomment (include/keys/asymmetric-type.h) talks about already, so that comment isn't modified. It's also how "openssl verify" looks up issuer certificates without the AKID available.
Preferably no language in persons.
Quoted 96 lines
Internally the search specifier string generated for the key lookup in
find_asymmetric_key()uses a new prefix that tellsasymmetric_key_match_preparseto only match the data against the raw Distinguished Name in the third ID and shouldn't conflict with search specifiers already in use.Lookups are unambiguous only provided that the CAs respect the condition in RFC5280 4.2.1.1 that the AKID may only be omitted if the CA uses a single signing key.
Signed-off-by Andrew Zaborowski <[email protected]>---
v2:
- focus the commit message on the implementation
- shorten the prefix used infind_asymmetric_key
- clarifyfind_asymmetric_keycommentcrypto/asymmetric_keys/asymmetric_type.c | 52 +++++++++++++++++------ crypto/asymmetric_keys/pkcs7_trust.c | 6 +-- crypto/asymmetric_keys/restrict.c | 48 ++++++++++++--------- crypto/asymmetric_keys/x509_cert_parser.c | 10 +++++ crypto/asymmetric_keys/x509_public_key.c | 10 +++++ include/crypto/public_key.h | 2 +- include/keys/asymmetric-type.h | 3 +- 7 files changed, 95 insertions(+), 36 deletions(-)crypto/asymmetric_keys/asymmetric_type.c +17
-7@@ -36,8 +36,11 @@ static DECLARE_RWSEM(asymmetric_key_parsers_sem);* find_asymmetric_key - Find a key by ID. * @keyring: The keys to search. * @id_0: The first ID to look for or NULL. -* @id_1: The second ID to look for or NULL. -* @partial: Use partial match if true, exact if false. +* @id_1: The second ID to look for or NULL, matched together with @id_0 +* against @keyring keys' id[0] and id[1]. +* @id_2: The fallback ID to match against @keyring keys' id[2] if both of the +* other IDs are NULL. +* @partial: Use partial match for @id_0 and @id_1 if true, exact if false. * * Find a key in the given keyring by identifier. The preferred identifier is * the id_0 and the fallback identifier is the id_1. If both are given, the @@ -46,6 +49,7 @@ static DECLARE_RWSEM(asymmetric_key_parsers_sem);struct key *find_asymmetric_key (struct key *keyring,const struct asymmetric_key_id *id_0,const struct asymmetric_key_id *id_1, +const struct asymmetric_key_id *id_2,bool partial) {struct key *key;@@ -54,14 +58,17 @@ struct key *find_asymmetric_key(struct key *keyring,char *req, *p;int len; -BUG_ON (!id_0 && !id_1); +BUG_ON (!id_0 && !id_1 && !id_2);if (id_0) { lookup = id_0->data; len = id_0->len; - }else { + }else if (id_1) { lookup = id_1->data; len = id_1->len; + }else { + lookup = id_2->data; + len = id_2->len; }/* Construct an identifier "id:<keyid>". */ @@ -69,7 +76,10 @@ struct key *find_asymmetric_key(struct key *keyring,if (!req)return ERR_PTR (-ENOMEM ); -if (partial) { +if (!id_0 && !id_1) { + *p++ ='d' ; + *p++ ='n' ; + }else if (partial) { *p++ ='i' ; *p++ ='d' ; }else {@@ -183,8 +193,8 @@ bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,EXPORT_SYMBOL_GPL (asymmetric_key_id_partial);/** -* asymmetric_match_key_ids - Search asymmetric key IDs -* @kids: The list of key IDs to check +* asymmetric_match_key_ids - Search asymmetric key IDs 1 & 2 +* @kids: The pair of key IDs to check * @match_id: The key ID we're looking for * @match: The match function to use */
crypto/asymmetric_keys/asymmetric_type.c static bool asymmetric_match_key_ids( · line 214
Quoted 8 lines
crypto/asymmetric_keys/asymmetric_type.c +1
-1@@ -198,7 +208,7 @@ static bool asymmetric_match_key_ids(if (!kids || !match_id)return false; -for (i =0 ; i <ARRAY_SIZE (kids->id); i++) +for (i =0 ; i <2 ; i++)if (match (kids->id[i], match_id))return true;return false;
Why are key ID 2 and key ID 3 handled differently? They are both optional.
Quoted 296 lines
crypto/asymmetric_keys/asymmetric_type.c +22
-4@@ -242,7 +252,7 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id) }/* -* Match asymmetric keys by an exact match on an ID. +* Match asymmetric keys by an exact match on one of the first two IDs. */ static bool asymmetric_key_cmp (const struct key *key,const struct key_match_data *match_data)@@ -255,7 +265,7 @@ static bool asymmetric_key_cmp(const struct key *key, }/* -* Match asymmetric keys by a partial match on an IDs. +* Match asymmetric keys by a partial match on one of the first two IDs. */ static bool asymmetric_key_cmp_partial (const struct key *key,const struct key_match_data *match_data)@@ -267,6 +277,18 @@ static bool asymmetric_key_cmp_partial(const struct key *key, asymmetric_key_id_partial); } +/* +* Match asymmetric keys by an exact match on the third IDs. +*/ +static bool asymmetric_key_cmp_name (const struct key *key, +const struct key_match_data *match_data) +{ +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *match_id = match_data->preparsed; + +return kids &&asymmetric_key_id_same (kids->id[2 ], match_id); +} +/* * Preparse the match criterion. If we don't set lookup_type and cmp, * the default will be an exact match on the key description. @@ -274,8 +296,9 @@ static bool asymmetric_key_cmp_partial(const struct key *key,* There are some specifiers for matching key IDs rather than by the key * description: * -* "id:<id>" - find a key by partial match on any available ID -* "ex:<id>" - find a key by exact match on any available ID +* "id:<id>" - find a key by partial match on one of the first two IDs +* "ex:<id>" - find a key by exact match on one of the first two IDs +* "dn:<id>" - find a key by exact match on the third ID * * These have to be searched by iteration rather than by direct lookup because * the key is hashed according to its description. @@ -299,6 +322,11 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data) spec[1] == 'x' && spec[2] == ':') { id = spec +3 ; + } else if (spec[0] == 'd' && + spec[1] == 'n' && + spec[2] == ':') { + id = spec +3 ; + cmp = asymmetric_key_cmp_name; } else { goto default_match; }crypto/asymmetric_keys/pkcs7_trust.c +3
-3@@ -48,7 +48,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* keys. */ key =find_asymmetric_key (trust_keyring, - x509->id, x509->skid, false); + x509->id, x509->skid,NULL , false);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,7 +82,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7, key =find_asymmetric_key (trust_keyring, last->sig->auth_ids[0 ], last->sig->auth_ids[1 ], - false); +NULL , false);if (!IS_ERR (key)) { x509 = last; pr_devel("sinfo %u: Root cert %u signer is key %x\n",@@ -97,7 +97,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* the signed info directly. */ key =find_asymmetric_key (trust_keyring, - sinfo->sig->auth_ids[0 ],NULL , false); + sinfo->sig->auth_ids[0 ],NULL ,NULL , false);if (!IS_ERR (key)) {pr_devel ("sinfo %u: Direct signer is key %x\n" , sinfo->index,key_serial (key));crypto/asymmetric_keys/restrict.c +29
-19@@ -87,7 +87,7 @@ int restrict_link_by_signature(struct key *dest_keyring, sig = payload->data[asym_auth];if (!sig)return -ENOPKG ; -if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ] && !sig->auth_ids[2 ])return -ENOKEY ;if (ca_keyid && !asymmetric_key_id_partial (sig->auth_ids[1 ], ca_keyid))@@ -96,7 +96,7 @@ int restrict_link_by_signature(struct key *dest_keyring,/* See if we have a key that signed this one. */ key =find_asymmetric_key (trust_keyring, sig->auth_ids[0 ], sig->auth_ids[1 ], - false); + sig->auth_ids[2 ], false);if (IS_ERR (key))return -ENOKEY ;@@ -108,11 +108,11 @@ int restrict_link_by_signature(struct key *dest_keyring,return ret; } -static bool match_either_id (const struct asymmetric_key_ids *pair, +static bool match_either_id (const struct asymmetric_key_id **pair,const struct asymmetric_key_id *single) { -return (asymmetric_key_id_same (pair->id[0 ], single) || -asymmetric_key_id_same (pair->id[1 ], single)); +return (asymmetric_key_id_same (pair[0 ], single) || +asymmetric_key_id_same (pair[1 ], single)); } static int key_or_keyring_common(struct key *dest_keyring,@@ -140,20 +140,22 @@ static int key_or_keyring_common(struct key *dest_keyring, sig = payload->data[asym_auth];if (!sig)return -ENOPKG ; -if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ] && !sig->auth_ids[2 ])return -ENOKEY ;if (trusted) {if (trusted->type == &key_type_keyring) {/* See if we have a key that signed this one. */ key =find_asymmetric_key (trusted, sig->auth_ids[0 ], - sig->auth_ids[1 ], false); + sig->auth_ids[1 ], + sig->auth_ids[2 ], false);if (IS_ERR (key)) key =NULL ; }else if (trusted->type == &key_type_asymmetric) { -const struct asymmetric_key_ids *signer_ids; +const struct asymmetric_key_id **signer_ids; - signer_ids =asymmetric_key_ids (trusted); + signer_ids = (const struct asymmetric_key_id **) +asymmetric_key_ids (trusted)->id;/* * The auth_ids come from the candidate key (the @@ -164,22 +166,29 @@ static int key_or_keyring_common(struct key *dest_keyring,* The signer_ids are identifiers for the * signing key specified for dest_keyring. * -* The first auth_id is the preferred id, and -* the second is the fallback. If only one -* auth_id is present, it may match against -* either signer_id. If two auth_ids are -* present, the first auth_id must match one -* signer_id and the second auth_id must match -* the second signer_id. +* The first auth_id is the preferred id, 2nd and +* 3rd are the fallbacks. If excatly one of +* auth_ids[0] and auth_ids[1] is present, it may +* match either signer_ids[0] or signed_ids[1]. +* If both are present the first one may match +* either signed_id but the second one must match +* the second signer_id. If neither of them is +* available, auth_ids[2] is matched against +* signer_ids[2] as a fallback. */ -if (!sig->auth_ids[0 ] || !sig->auth_ids[1 ]) { +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) { +if (asymmetric_key_id_same (signer_ids[2 ], + sig->auth_ids[2 ])) + key =__key_get (trusted); + + }else if (!sig->auth_ids[0 ] || !sig->auth_ids[1 ]) {const struct asymmetric_key_id *auth_id; auth_id = sig->auth_ids[0 ] ?: sig->auth_ids[1 ];if (match_either_id (signer_ids, auth_id)) key =__key_get (trusted); - }else if (asymmetric_key_id_same (signer_ids->id[1 ], + }else if (asymmetric_key_id_same (signer_ids[1 ], sig->auth_ids[1 ]) &&match_either_id (signer_ids, sig->auth_ids[0 ])) {@@ -193,7 +202,8 @@ static int key_or_keyring_common(struct key *dest_keyring,if (check_dest && !key) {/* See if the destination has a key that signed this one. */ key =find_asymmetric_key (dest_keyring, sig->auth_ids[0 ], - sig->auth_ids[1 ], false); + sig->auth_ids[1 ], sig->auth_ids[2 ], + false);if (IS_ERR (key)) key =NULL ; }crypto/asymmetric_keys/x509_cert_parser.c +10
-0@@ -415,8 +415,18 @@ int x509_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; +struct asymmetric_key_id *kid; + ctx->cert->raw_issuer = value; ctx->cert->raw_issuer_size = vlen; + +if (!ctx->cert->sig->auth_ids[2 ]) { + kid =asymmetric_key_generate_id (value, vlen,"" ,0 ); +if (IS_ERR (kid)) +return PTR_ERR (kid); + ctx->cert->sig->auth_ids[2 ] = kid; + } +return x509_fabricate_name (ctx, hdrlen, tag, &ctx->cert->issuer, vlen); }crypto/asymmetric_keys/x509_public_key.c +10
-0@@ -221,6 +221,13 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) goto error_free_desc; kids->id[0 ] = cert->id; kids->id[1 ] = cert->skid; + kids->id[2 ] =asymmetric_key_generate_id (cert->raw_subject, + cert->raw_subject_size, +"" ,0 ); +if (IS_ERR (kids->id[2 ])) { + ret =PTR_ERR (kids->id[2 ]); + goto error_free_kids; + }/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner);@@ -237,8 +244,11 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) cert->skid =NULL ; cert->sig =NULL ; desc =NULL ; + kids =NULL ; ret =0 ; +error_free_kids: +kfree (kids); error_free_desc:kfree (desc); error_free_cert:include/crypto/public_key.h +1
-1@@ -37,7 +37,7 @@ extern void public_key_free(struct public_key *key);* Public key cryptography signature data */ struct public_key_signature { -struct asymmetric_key_id *auth_ids[2 ]; +struct asymmetric_key_id *auth_ids[3 ];u8 *s;/* Signature */ u32 s_size;/* Number of bytes in signature */ u8 *digest;include/keys/asymmetric-type.h +2
-1@@ -53,7 +53,7 @@ struct asymmetric_key_id { };struct asymmetric_key_ids { -void *id[2 ]; +void *id[3 ]; }; extern bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,@@ -75,6 +75,7 @@ const struct asymmetric_key_ids *asymmetric_key_ids(const struct key *key)extern struct key *find_asymmetric_key (struct key *keyring,const struct asymmetric_key_id *id_0,const struct asymmetric_key_id *id_1, +const struct asymmetric_key_id *id_2,bool partial);/* Signature · 1 line
2.27.0
This gives checkpatch error but is not due to this patch. I sent a bug fix (and also cc'd you to that). I'll apply that to my tree as soon as I get it ack'd first.
Overally, I don't understand differianting the semantics. Emphasis on that I haven't been involved with asymmetric keys implementation, so it is entirely possible that there are good reasons to do this but the commit message did not shed any light on this.
/Jarkko
Jarkko Sakkinen
maintainerQuoted 433 lines · Jarkko Sakkinen
Quoted 16 lines · Andrew Zaborowski
There are non-root X.509 v3 certificates in use there that contain no Authority Key Identifier extension (RFC5280 section 4.2.1.1). The asymmetric key's issuer key IDs 1 and 2 for those certificates are generated from the AKID data so current code has no way to look up the issuer certificate for verification. Add a third ID blob to the arrays in both
asymmetric_key_ids(for certficate subject) and in the public_keys_signature'sauth_ids(for issuer lookup), using just raw subject and issuer DNs from the certificate. There's no other place this data is currently saved that could be used infind_asymmetric_key. Adaptasymmetric_key_ids()and the callers to use the third ID for lookups when none of the other two are available. Attempt to keep the logic intact when they are, to minimise behaviour changes. Adapt the restrict functions' NULL-checks to include that ID too. The lookup logic inpkcs7_verify.cis not modified, the AKID extensions are still required there.Quoted 4 lines
I believe this implements what (2) in the struct
asymmetric_key_idcomment (include/keys/asymmetric-type.h) talks about already, so that comment isn't modified. It's also how "openssl verify" looks up issuer certificates without the AKID available.Preferably no language in persons.
Quoted 96 lines
Internally the search specifier string generated for the key lookup in
find_asymmetric_key()uses a new prefix that tellsasymmetric_key_match_preparseto only match the data against the raw Distinguished Name in the third ID and shouldn't conflict with search specifiers already in use.Lookups are unambiguous only provided that the CAs respect the condition in RFC5280 4.2.1.1 that the AKID may only be omitted if the CA uses a single signing key.
Signed-off-by Andrew Zaborowski <[email protected]>---
v2:
- focus the commit message on the implementation
- shorten the prefix used infind_asymmetric_key
- clarifyfind_asymmetric_keycommentcrypto/asymmetric_keys/asymmetric_type.c | 52 +++++++++++++++++------ crypto/asymmetric_keys/pkcs7_trust.c | 6 +-- crypto/asymmetric_keys/restrict.c | 48 ++++++++++++--------- crypto/asymmetric_keys/x509_cert_parser.c | 10 +++++ crypto/asymmetric_keys/x509_public_key.c | 10 +++++ include/crypto/public_key.h | 2 +- include/keys/asymmetric-type.h | 3 +- 7 files changed, 95 insertions(+), 36 deletions(-)crypto/asymmetric_keys/asymmetric_type.c +17
-7@@ -36,8 +36,11 @@ static DECLARE_RWSEM(asymmetric_key_parsers_sem);* find_asymmetric_key - Find a key by ID. * @keyring: The keys to search. * @id_0: The first ID to look for or NULL. -* @id_1: The second ID to look for or NULL. -* @partial: Use partial match if true, exact if false. +* @id_1: The second ID to look for or NULL, matched together with @id_0 +* against @keyring keys' id[0] and id[1]. +* @id_2: The fallback ID to match against @keyring keys' id[2] if both of the +* other IDs are NULL. +* @partial: Use partial match for @id_0 and @id_1 if true, exact if false. * * Find a key in the given keyring by identifier. The preferred identifier is * the id_0 and the fallback identifier is the id_1. If both are given, the @@ -46,6 +49,7 @@ static DECLARE_RWSEM(asymmetric_key_parsers_sem);struct key *find_asymmetric_key (struct key *keyring,const struct asymmetric_key_id *id_0,const struct asymmetric_key_id *id_1, +const struct asymmetric_key_id *id_2,bool partial) {struct key *key;@@ -54,14 +58,17 @@ struct key *find_asymmetric_key(struct key *keyring,char *req, *p;int len; -BUG_ON (!id_0 && !id_1); +BUG_ON (!id_0 && !id_1 && !id_2);if (id_0) { lookup = id_0->data; len = id_0->len; - }else { + }else if (id_1) { lookup = id_1->data; len = id_1->len; + }else { + lookup = id_2->data; + len = id_2->len; }/* Construct an identifier "id:<keyid>". */ @@ -69,7 +76,10 @@ struct key *find_asymmetric_key(struct key *keyring,if (!req)return ERR_PTR (-ENOMEM ); -if (partial) { +if (!id_0 && !id_1) { + *p++ ='d' ; + *p++ ='n' ; + }else if (partial) { *p++ ='i' ; *p++ ='d' ; }else {@@ -183,8 +193,8 @@ bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,EXPORT_SYMBOL_GPL (asymmetric_key_id_partial);/** -* asymmetric_match_key_ids - Search asymmetric key IDs -* @kids: The list of key IDs to check +* asymmetric_match_key_ids - Search asymmetric key IDs 1 & 2 +* @kids: The pair of key IDs to check * @match_id: The key ID we're looking for * @match: The match function to use */ crypto/asymmetric_keys/asymmetric_type.c static bool asymmetric_match_key_ids( · line 214
Quoted 8 lines
crypto/asymmetric_keys/asymmetric_type.c +1
-1@@ -198,7 +208,7 @@ static bool asymmetric_match_key_ids(if (!kids || !match_id)return false; -for (i =0 ; i <ARRAY_SIZE (kids->id); i++) +for (i =0 ; i <2 ; i++)if (match (kids->id[i], match_id))return true;return false;Why are key ID 2 and key ID 3 handled differently? They are both optional.
Quoted 296 lines
crypto/asymmetric_keys/asymmetric_type.c +22
-4@@ -242,7 +252,7 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id) }/* -* Match asymmetric keys by an exact match on an ID. +* Match asymmetric keys by an exact match on one of the first two IDs. */ static bool asymmetric_key_cmp (const struct key *key,const struct key_match_data *match_data)@@ -255,7 +265,7 @@ static bool asymmetric_key_cmp(const struct key *key, }/* -* Match asymmetric keys by a partial match on an IDs. +* Match asymmetric keys by a partial match on one of the first two IDs. */ static bool asymmetric_key_cmp_partial (const struct key *key,const struct key_match_data *match_data)@@ -267,6 +277,18 @@ static bool asymmetric_key_cmp_partial(const struct key *key, asymmetric_key_id_partial); } +/* +* Match asymmetric keys by an exact match on the third IDs. +*/ +static bool asymmetric_key_cmp_name (const struct key *key, +const struct key_match_data *match_data) +{ +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *match_id = match_data->preparsed; + +return kids &&asymmetric_key_id_same (kids->id[2 ], match_id); +} +/* * Preparse the match criterion. If we don't set lookup_type and cmp, * the default will be an exact match on the key description. @@ -274,8 +296,9 @@ static bool asymmetric_key_cmp_partial(const struct key *key,* There are some specifiers for matching key IDs rather than by the key * description: * -* "id:<id>" - find a key by partial match on any available ID -* "ex:<id>" - find a key by exact match on any available ID +* "id:<id>" - find a key by partial match on one of the first two IDs +* "ex:<id>" - find a key by exact match on one of the first two IDs +* "dn:<id>" - find a key by exact match on the third ID * * These have to be searched by iteration rather than by direct lookup because * the key is hashed according to its description. @@ -299,6 +322,11 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data) spec[1] == 'x' && spec[2] == ':') { id = spec +3 ; + } else if (spec[0] == 'd' && + spec[1] == 'n' && + spec[2] == ':') { + id = spec +3 ; + cmp = asymmetric_key_cmp_name; } else { goto default_match; }crypto/asymmetric_keys/pkcs7_trust.c +3
-3@@ -48,7 +48,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* keys. */ key =find_asymmetric_key (trust_keyring, - x509->id, x509->skid, false); + x509->id, x509->skid,NULL , false);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,7 +82,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7, key =find_asymmetric_key (trust_keyring, last->sig->auth_ids[0 ], last->sig->auth_ids[1 ], - false); +NULL , false);if (!IS_ERR (key)) { x509 = last; pr_devel("sinfo %u: Root cert %u signer is key %x\n",@@ -97,7 +97,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* the signed info directly. */ key =find_asymmetric_key (trust_keyring, - sinfo->sig->auth_ids[0 ],NULL , false); + sinfo->sig->auth_ids[0 ],NULL ,NULL , false);if (!IS_ERR (key)) {pr_devel ("sinfo %u: Direct signer is key %x\n" , sinfo->index,key_serial (key));crypto/asymmetric_keys/restrict.c +29
-19@@ -87,7 +87,7 @@ int restrict_link_by_signature(struct key *dest_keyring, sig = payload->data[asym_auth];if (!sig)return -ENOPKG ; -if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ] && !sig->auth_ids[2 ])return -ENOKEY ;if (ca_keyid && !asymmetric_key_id_partial (sig->auth_ids[1 ], ca_keyid))@@ -96,7 +96,7 @@ int restrict_link_by_signature(struct key *dest_keyring,/* See if we have a key that signed this one. */ key =find_asymmetric_key (trust_keyring, sig->auth_ids[0 ], sig->auth_ids[1 ], - false); + sig->auth_ids[2 ], false);if (IS_ERR (key))return -ENOKEY ;@@ -108,11 +108,11 @@ int restrict_link_by_signature(struct key *dest_keyring,return ret; } -static bool match_either_id (const struct asymmetric_key_ids *pair, +static bool match_either_id (const struct asymmetric_key_id **pair,const struct asymmetric_key_id *single) { -return (asymmetric_key_id_same (pair->id[0 ], single) || -asymmetric_key_id_same (pair->id[1 ], single)); +return (asymmetric_key_id_same (pair[0 ], single) || +asymmetric_key_id_same (pair[1 ], single)); } static int key_or_keyring_common(struct key *dest_keyring,@@ -140,20 +140,22 @@ static int key_or_keyring_common(struct key *dest_keyring, sig = payload->data[asym_auth];if (!sig)return -ENOPKG ; -if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ] && !sig->auth_ids[2 ])return -ENOKEY ;if (trusted) {if (trusted->type == &key_type_keyring) {/* See if we have a key that signed this one. */ key =find_asymmetric_key (trusted, sig->auth_ids[0 ], - sig->auth_ids[1 ], false); + sig->auth_ids[1 ], + sig->auth_ids[2 ], false);if (IS_ERR (key)) key =NULL ; }else if (trusted->type == &key_type_asymmetric) { -const struct asymmetric_key_ids *signer_ids; +const struct asymmetric_key_id **signer_ids; - signer_ids =asymmetric_key_ids (trusted); + signer_ids = (const struct asymmetric_key_id **) +asymmetric_key_ids (trusted)->id;/* * The auth_ids come from the candidate key (the @@ -164,22 +166,29 @@ static int key_or_keyring_common(struct key *dest_keyring,* The signer_ids are identifiers for the * signing key specified for dest_keyring. * -* The first auth_id is the preferred id, and -* the second is the fallback. If only one -* auth_id is present, it may match against -* either signer_id. If two auth_ids are -* present, the first auth_id must match one -* signer_id and the second auth_id must match -* the second signer_id. +* The first auth_id is the preferred id, 2nd and +* 3rd are the fallbacks. If excatly one of +* auth_ids[0] and auth_ids[1] is present, it may +* match either signer_ids[0] or signed_ids[1]. +* If both are present the first one may match +* either signed_id but the second one must match +* the second signer_id. If neither of them is +* available, auth_ids[2] is matched against +* signer_ids[2] as a fallback. */ -if (!sig->auth_ids[0 ] || !sig->auth_ids[1 ]) { +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) { +if (asymmetric_key_id_same (signer_ids[2 ], + sig->auth_ids[2 ])) + key =__key_get (trusted); + + }else if (!sig->auth_ids[0 ] || !sig->auth_ids[1 ]) {const struct asymmetric_key_id *auth_id; auth_id = sig->auth_ids[0 ] ?: sig->auth_ids[1 ];if (match_either_id (signer_ids, auth_id)) key =__key_get (trusted); - }else if (asymmetric_key_id_same (signer_ids->id[1 ], + }else if (asymmetric_key_id_same (signer_ids[1 ], sig->auth_ids[1 ]) &&match_either_id (signer_ids, sig->auth_ids[0 ])) {@@ -193,7 +202,8 @@ static int key_or_keyring_common(struct key *dest_keyring,if (check_dest && !key) {/* See if the destination has a key that signed this one. */ key =find_asymmetric_key (dest_keyring, sig->auth_ids[0 ], - sig->auth_ids[1 ], false); + sig->auth_ids[1 ], sig->auth_ids[2 ], + false);if (IS_ERR (key)) key =NULL ; }crypto/asymmetric_keys/x509_cert_parser.c +10
-0@@ -415,8 +415,18 @@ int x509_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; +struct asymmetric_key_id *kid; + ctx->cert->raw_issuer = value; ctx->cert->raw_issuer_size = vlen; + +if (!ctx->cert->sig->auth_ids[2 ]) { + kid =asymmetric_key_generate_id (value, vlen,"" ,0 ); +if (IS_ERR (kid)) +return PTR_ERR (kid); + ctx->cert->sig->auth_ids[2 ] = kid; + } +return x509_fabricate_name (ctx, hdrlen, tag, &ctx->cert->issuer, vlen); }crypto/asymmetric_keys/x509_public_key.c +10
-0@@ -221,6 +221,13 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) goto error_free_desc; kids->id[0 ] = cert->id; kids->id[1 ] = cert->skid; + kids->id[2 ] =asymmetric_key_generate_id (cert->raw_subject, + cert->raw_subject_size, +"" ,0 ); +if (IS_ERR (kids->id[2 ])) { + ret =PTR_ERR (kids->id[2 ]); + goto error_free_kids; + }/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner);@@ -237,8 +244,11 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) cert->skid =NULL ; cert->sig =NULL ; desc =NULL ; + kids =NULL ; ret =0 ; +error_free_kids: +kfree (kids); error_free_desc:kfree (desc); error_free_cert:include/crypto/public_key.h +1
-1@@ -37,7 +37,7 @@ extern void public_key_free(struct public_key *key);* Public key cryptography signature data */ struct public_key_signature { -struct asymmetric_key_id *auth_ids[2 ]; +struct asymmetric_key_id *auth_ids[3 ];u8 *s;/* Signature */ u32 s_size;/* Number of bytes in signature */ u8 *digest;include/keys/asymmetric-type.h +2
-1@@ -53,7 +53,7 @@ struct asymmetric_key_id { };struct asymmetric_key_ids { -void *id[2 ]; +void *id[3 ]; }; extern bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,@@ -75,6 +75,7 @@ const struct asymmetric_key_ids *asymmetric_key_ids(const struct key *key)extern struct key *find_asymmetric_key (struct key *keyring,const struct asymmetric_key_id *id_0,const struct asymmetric_key_id *id_1, +const struct asymmetric_key_id *id_2,bool partial);/* Signature · 1 line
2.27.0This gives checkpatch error but is not due to this patch. I sent a bug fix (and also cc'd you to that). I'll apply that to my tree as soon as I get it ack'd first.
Quoted 4 lines
Overally, I don't understand differianting the semantics. Emphasis on that I haven't been involved with asymmetric keys implementation, so it is entirely possible that there are good reasons to do this but the commit message did not shed any light on this.
Also, a small usage example of the feature would be a great addition to the commit message, as this extends the API. It's nice for better understanding, and also later on for backtracking.
/Jarkko
Quoted 14 lines · Jarkko Sakkinen
Quoted 13 lines · Andrew Zaborowski
crypto/asymmetric_keys/asymmetric_type.c +2
-2@@ -183,8 +193,8 @@ bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,EXPORT_SYMBOL_GPL (asymmetric_key_id_partial);/** -* asymmetric_match_key_ids - Search asymmetric key IDs -* @kids: The list of key IDs to check +* asymmetric_match_key_ids - Search asymmetric key IDs 1 & 2 +* @kids: The pair of key IDs to check * @match_id: The key ID we're looking for * @match: The match function to use */ @@ -198,7 +208,7 @@ static bool asymmetric_match_key_ids(
Quoted 10 lines
crypto/asymmetric_keys/asymmetric_type.c static bool asymmetric_match_key_ids( · line 214
Quoted 7 lines
crypto/asymmetric_keys/asymmetric_type.c +1
-1if (!kids || !match_id)return false; -for (i =0 ; i <ARRAY_SIZE (kids->id); i++) +for (i =0 ; i <2 ; i++)if (match (kids->id[i], match_id))return true;return false;Why are key ID 2 and key ID 3 handled differently? They are both optional.
This is to minimise the impact of having a new ID added. I guess the danger is that it could add ambiguity in the lookup, i.e. a different key could be returned for an existing search query.
There's a specific logic in how ID 1 and 2 interact documented as follows in restrict.c:
* The first auth_id is the preferred id, and
* the second is the fallback. If only one
* auth_id is present, it may match against
* either signer_id. If two auth_ids are
* present, the first auth_id must match one
* signer_id and the second auth_id must match
* the second signer_id.
I'm not sure what the use case motivates this. For the x509_public_key subtype you'd expect that ID 1 in the signature matches subject ID 1 of the issuer and ID 2 matches ID 2. Most of the time both will be present for a CA certificate.
I imagine restrict.c only tries to mirror the logic that was already implemented in find_asymmetric_key when the restrict functions were added.
For ID 2, only ever filled in by the x509_public_key subtype (right now), we only have any use for it being matched against the issuer's ID 2.
Note: asymmetric_match_key_ids can be used as part of a generic key API query, or as part of a find_asymmetric_key call (only used in crypto/asymmetric_keys/ for trust verification and similar) but find_asymmetric_key will then perform an extra check. In any case without more background I think it's preferable to minimize the matching logic changes, and even assuming that the logic could be improved it may be best to keep it as is because existing tools may rely on it.
Quoted 292 lines
Quoted 292 lines
crypto/asymmetric_keys/asymmetric_type.c +22
-4@@ -242,7 +252,7 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id) }/* -* Match asymmetric keys by an exact match on an ID. +* Match asymmetric keys by an exact match on one of the first two IDs. */ static bool asymmetric_key_cmp (const struct key *key,const struct key_match_data *match_data)@@ -255,7 +265,7 @@ static bool asymmetric_key_cmp(const struct key *key, }/* -* Match asymmetric keys by a partial match on an IDs. +* Match asymmetric keys by a partial match on one of the first two IDs. */ static bool asymmetric_key_cmp_partial (const struct key *key,const struct key_match_data *match_data)@@ -267,6 +277,18 @@ static bool asymmetric_key_cmp_partial(const struct key *key, asymmetric_key_id_partial); } +/* +* Match asymmetric keys by an exact match on the third IDs. +*/ +static bool asymmetric_key_cmp_name (const struct key *key, +const struct key_match_data *match_data) +{ +const struct asymmetric_key_ids *kids =asymmetric_key_ids (key); +const struct asymmetric_key_id *match_id = match_data->preparsed; + +return kids &&asymmetric_key_id_same (kids->id[2 ], match_id); +} +/* * Preparse the match criterion. If we don't set lookup_type and cmp, * the default will be an exact match on the key description. @@ -274,8 +296,9 @@ static bool asymmetric_key_cmp_partial(const struct key *key,* There are some specifiers for matching key IDs rather than by the key * description: * -* "id:<id>" - find a key by partial match on any available ID -* "ex:<id>" - find a key by exact match on any available ID +* "id:<id>" - find a key by partial match on one of the first two IDs +* "ex:<id>" - find a key by exact match on one of the first two IDs +* "dn:<id>" - find a key by exact match on the third ID * * These have to be searched by iteration rather than by direct lookup because * the key is hashed according to its description. @@ -299,6 +322,11 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data) spec[1] == 'x' && spec[2] == ':') { id = spec +3 ; + } else if (spec[0] == 'd' && + spec[1] == 'n' && + spec[2] == ':') { + id = spec +3 ; + cmp = asymmetric_key_cmp_name; } else { goto default_match; }crypto/asymmetric_keys/pkcs7_trust.c +3
-3@@ -48,7 +48,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* keys. */ key =find_asymmetric_key (trust_keyring, - x509->id, x509->skid, false); + x509->id, x509->skid,NULL , false);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,7 +82,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7, key =find_asymmetric_key (trust_keyring, last->sig->auth_ids[0 ], last->sig->auth_ids[1 ], - false); +NULL , false);if (!IS_ERR (key)) { x509 = last; pr_devel("sinfo %u: Root cert %u signer is key %x\n",@@ -97,7 +97,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,* the signed info directly. */ key =find_asymmetric_key (trust_keyring, - sinfo->sig->auth_ids[0 ],NULL , false); + sinfo->sig->auth_ids[0 ],NULL ,NULL , false);if (!IS_ERR (key)) {pr_devel ("sinfo %u: Direct signer is key %x\n" , sinfo->index,key_serial (key));crypto/asymmetric_keys/restrict.c +29
-19@@ -87,7 +87,7 @@ int restrict_link_by_signature(struct key *dest_keyring, sig = payload->data[asym_auth];if (!sig)return -ENOPKG ; -if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ] && !sig->auth_ids[2 ])return -ENOKEY ;if (ca_keyid && !asymmetric_key_id_partial (sig->auth_ids[1 ], ca_keyid))@@ -96,7 +96,7 @@ int restrict_link_by_signature(struct key *dest_keyring,/* See if we have a key that signed this one. */ key =find_asymmetric_key (trust_keyring, sig->auth_ids[0 ], sig->auth_ids[1 ], - false); + sig->auth_ids[2 ], false);if (IS_ERR (key))return -ENOKEY ;@@ -108,11 +108,11 @@ int restrict_link_by_signature(struct key *dest_keyring,return ret; } -static bool match_either_id (const struct asymmetric_key_ids *pair, +static bool match_either_id (const struct asymmetric_key_id **pair,const struct asymmetric_key_id *single) { -return (asymmetric_key_id_same (pair->id[0 ], single) || -asymmetric_key_id_same (pair->id[1 ], single)); +return (asymmetric_key_id_same (pair[0 ], single) || +asymmetric_key_id_same (pair[1 ], single)); } static int key_or_keyring_common(struct key *dest_keyring,@@ -140,20 +140,22 @@ static int key_or_keyring_common(struct key *dest_keyring, sig = payload->data[asym_auth];if (!sig)return -ENOPKG ; -if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ] && !sig->auth_ids[2 ])return -ENOKEY ;if (trusted) {if (trusted->type == &key_type_keyring) {/* See if we have a key that signed this one. */ key =find_asymmetric_key (trusted, sig->auth_ids[0 ], - sig->auth_ids[1 ], false); + sig->auth_ids[1 ], + sig->auth_ids[2 ], false);if (IS_ERR (key)) key =NULL ; }else if (trusted->type == &key_type_asymmetric) { -const struct asymmetric_key_ids *signer_ids; +const struct asymmetric_key_id **signer_ids; - signer_ids =asymmetric_key_ids (trusted); + signer_ids = (const struct asymmetric_key_id **) +asymmetric_key_ids (trusted)->id;/* * The auth_ids come from the candidate key (the @@ -164,22 +166,29 @@ static int key_or_keyring_common(struct key *dest_keyring,* The signer_ids are identifiers for the * signing key specified for dest_keyring. * -* The first auth_id is the preferred id, and -* the second is the fallback. If only one -* auth_id is present, it may match against -* either signer_id. If two auth_ids are -* present, the first auth_id must match one -* signer_id and the second auth_id must match -* the second signer_id. +* The first auth_id is the preferred id, 2nd and +* 3rd are the fallbacks. If excatly one of +* auth_ids[0] and auth_ids[1] is present, it may +* match either signer_ids[0] or signed_ids[1]. +* If both are present the first one may match +* either signed_id but the second one must match +* the second signer_id. If neither of them is +* available, auth_ids[2] is matched against +* signer_ids[2] as a fallback. */ -if (!sig->auth_ids[0 ] || !sig->auth_ids[1 ]) { +if (!sig->auth_ids[0 ] && !sig->auth_ids[1 ]) { +if (asymmetric_key_id_same (signer_ids[2 ], + sig->auth_ids[2 ])) + key =__key_get (trusted); + + }else if (!sig->auth_ids[0 ] || !sig->auth_ids[1 ]) {const struct asymmetric_key_id *auth_id; auth_id = sig->auth_ids[0 ] ?: sig->auth_ids[1 ];if (match_either_id (signer_ids, auth_id)) key =__key_get (trusted); - }else if (asymmetric_key_id_same (signer_ids->id[1 ], + }else if (asymmetric_key_id_same (signer_ids[1 ], sig->auth_ids[1 ]) &&match_either_id (signer_ids, sig->auth_ids[0 ])) {@@ -193,7 +202,8 @@ static int key_or_keyring_common(struct key *dest_keyring,if (check_dest && !key) {/* See if the destination has a key that signed this one. */ key =find_asymmetric_key (dest_keyring, sig->auth_ids[0 ], - sig->auth_ids[1 ], false); + sig->auth_ids[1 ], sig->auth_ids[2 ], + false);if (IS_ERR (key)) key =NULL ; }crypto/asymmetric_keys/x509_cert_parser.c +10
-0@@ -415,8 +415,18 @@ int x509_note_issuer(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; +struct asymmetric_key_id *kid; + ctx->cert->raw_issuer = value; ctx->cert->raw_issuer_size = vlen; + +if (!ctx->cert->sig->auth_ids[2 ]) { + kid =asymmetric_key_generate_id (value, vlen,"" ,0 ); +if (IS_ERR (kid)) +return PTR_ERR (kid); + ctx->cert->sig->auth_ids[2 ] = kid; + } +return x509_fabricate_name (ctx, hdrlen, tag, &ctx->cert->issuer, vlen); }crypto/asymmetric_keys/x509_public_key.c +10
-0@@ -221,6 +221,13 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) goto error_free_desc; kids->id[0 ] = cert->id; kids->id[1 ] = cert->skid; + kids->id[2 ] =asymmetric_key_generate_id (cert->raw_subject, + cert->raw_subject_size, +"" ,0 ); +if (IS_ERR (kids->id[2 ])) { + ret =PTR_ERR (kids->id[2 ]); + goto error_free_kids; + }/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner);@@ -237,8 +244,11 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) cert->skid =NULL ; cert->sig =NULL ; desc =NULL ; + kids =NULL ; ret =0 ; +error_free_kids: +kfree (kids); error_free_desc:kfree (desc); error_free_cert:include/crypto/public_key.h +1
-1@@ -37,7 +37,7 @@ extern void public_key_free(struct public_key *key);* Public key cryptography signature data */ struct public_key_signature { -struct asymmetric_key_id *auth_ids[2 ]; +struct asymmetric_key_id *auth_ids[3 ];u8 *s;/* Signature */ u32 s_size;/* Number of bytes in signature */ u8 *digest;include/keys/asymmetric-type.h +2
-1@@ -53,7 +53,7 @@ struct asymmetric_key_id { };struct asymmetric_key_ids { -void *id[2 ]; +void *id[3 ]; }; extern bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,@@ -75,6 +75,7 @@ const struct asymmetric_key_ids *asymmetric_key_ids(const struct key *key)extern struct key *find_asymmetric_key (struct key *keyring,const struct asymmetric_key_id *id_0,const struct asymmetric_key_id *id_1, +const struct asymmetric_key_id *id_2,bool partial);
Quoted 9 lines
Quoted 5 lines
include/keys/asymmetric-type.h +0
-1/* -- 2.27.0
This gives checkpatch error but is not due to this patch. I sent a bug fix (and also cc'd you to that). I'll apply that to my tree as soon as I get it ack'd first.
Ok, great. I'll run checkpatch next time I'm sending a version of this patch.
Best regards
Quoted 23 lines · Andrew Zaborowski
Quoted 2 lines
Why are key ID 2 and key ID 3 handled differently? They are both optional.
This is to minimise the impact of having a new ID added. I guess the danger is that it could add ambiguity in the lookup, i.e. a different key could be returned for an existing search query.
There's a specific logic in how ID 1 and 2 interact documented as follows in
restrict.c:* The first
auth_idis the preferred id, and
* the second is the fallback. If only one
*auth_idis present, it may match against
* eithersigner_id. If twoauth_idsare
* present, the firstauth_idmust match one
*signer_idand the secondauth_idmust match
* the secondsigner_id.I'm not sure what the use case motivates this. For the
x509_public_keysubtype you'd expect that ID 1 in the signature matches subject ID 1 of the issuer and ID 2 matches ID 2. Most of the time both will be present for a CA certificate.
Quoted 7 lines
I imagine
restrict.conly tries to mirror the logic that was already implemented infind_asymmetric_keywhen the restrict functions were added.For ID 2, only ever filled in by the
x509_public_keysubtype (right now), we only have any use for it being matched against the issuer's ID 2.
Sorry, I meant the new ID 3 here (ID 1 meaning auth_ids[0], and so on...)
Best regardds
Jarkko Sakkinen
maintainerQuoted 26 lines · Andrew Zaborowski
Quoted 14 lines · Jarkko Sakkinen
Quoted 13 lines · Andrew Zaborowski
crypto/asymmetric_keys/asymmetric_type.c +2
-2@@ -183,8 +193,8 @@ bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,EXPORT_SYMBOL_GPL (asymmetric_key_id_partial);/** -* asymmetric_match_key_ids - Search asymmetric key IDs -* @kids: The list of key IDs to check +* asymmetric_match_key_ids - Search asymmetric key IDs 1 & 2 +* @kids: The pair of key IDs to check * @match_id: The key ID we're looking for * @match: The match function to use */ @@ -198,7 +208,7 @@ static bool asymmetric_match_key_ids(Quoted 10 lines
crypto/asymmetric_keys/asymmetric_type.c static bool asymmetric_match_key_ids( · line 214
Quoted 7 lines
crypto/asymmetric_keys/asymmetric_type.c +1
-1if (!kids || !match_id)return false; -for (i =0 ; i <ARRAY_SIZE (kids->id); i++) +for (i =0 ; i <2 ; i++)if (match (kids->id[i], match_id))return true;return false;Why are key ID 2 and key ID 3 handled differently? They are both optional.
Quoted 3 lines
This is to minimise the impact of having a new ID added. I guess the danger is that it could add ambiguity in the lookup, i.e. a different key could be returned for an existing search query.
Right I do get that. It could potentially break some scripts.
Quoted 25 lines
There's a specific logic in how ID 1 and 2 interact documented as follows in
restrict.c:* The first
auth_idis the preferred id, and
* the second is the fallback. If only one
*auth_idis present, it may match against
* eithersigner_id. If twoauth_idsare
* present, the firstauth_idmust match one
*signer_idand the secondauth_idmust match
* the secondsigner_id.I'm not sure what the use case motivates this. For the
x509_public_keysubtype you'd expect that ID 1 in the signature matches subject ID 1 of the issuer and ID 2 matches ID 2. Most of the time both will be present for a CA certificate.I imagine
restrict.conly tries to mirror the logic that was already implemented infind_asymmetric_keywhen the restrict functions were added.For ID 2, only ever filled in by the
x509_public_keysubtype (right now), we only have any use for it being matched against the issuer's ID 2.
Quoted 8 lines
Note:
asymmetric_match_key_idscan be used as part of a generic key API query, or as part of afind_asymmetric_keycall (only used incrypto/asymmetric_keys/for trust verification and similar) butfind_asymmetric_keywill then perform an extra check. In any case without more background I think it's preferable to minimize the matching logic changes, and even assuming that the logic could be improved it may be best to keep it as is because existing tools may rely on it.
You could give a couple of usage examples, by using this cert
https://letsencrypt.org/docs/certificates-for-localhost/
That is good information to store in the commit log for future and also works as a tested-by criteria.
/Jarkko
Quoted 10 lines · Jarkko Sakkinen
Quoted 8 lines · Andrew Zaborowski
Note:
asymmetric_match_key_idscan be used as part of a generic key API query, or as part of afind_asymmetric_keycall (only used incrypto/asymmetric_keys/for trust verification and similar) butfind_asymmetric_keywill then perform an extra check. In any case without more background I think it's preferable to minimize the matching logic changes, and even assuming that the logic could be improved it may be best to keep it as is because existing tools may rely on it.
Quoted 6 lines
You could give a couple of usage examples, by using this cert
https://letsencrypt.org/docs/certificates-for-localhost/
That is good information to store in the commit log for future and also works as a tested-by criteria.
Ok, resending the patch rebased on top of the BUG_ON fix (applied locally) and adding the command line example.
Best regards
Jarkko Sakkinen
maintainerQuoted 15 lines · Andrew Zaborowski
Quoted 14 lines · Jarkko Sakkinen
Quoted 8 lines · Andrew Zaborowski
Note:
asymmetric_match_key_idscan be used as part of a generic key API query, or as part of afind_asymmetric_keycall (only used incrypto/asymmetric_keys/for trust verification and similar) butfind_asymmetric_keywill then perform an extra check. In any case without more background I think it's preferable to minimize the matching logic changes, and even assuming that the logic could be improved it may be best to keep it as is because existing tools may rely on it.You could give a couple of usage examples, by using this cert
Quoted 7 lines
Quoted 2 lines
That is good information to store in the commit log for future and also works as a tested-by criteria.
Ok, resending the patch rebased on top of the
BUG_ONfix (applied locally) and adding the command line example.Best regards
Please make it a patch set then. Locally applied patches do not count.
/Jarkko