@@ -109,31 +109,20 @@ int restrict_link_by_signature(struct key *dest_keyring,
109109 return ret ;
110110}
111111
112- /**
113- * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
114- * keys using the restrict_key information stored in the ring.
115- * @dest_keyring: Keyring being linked to.
116- * @type: The type of key being added.
117- * @payload: The payload of the new key.
118- * @trusted: A key or ring of keys that can be used to vouch for the new cert.
119- *
120- * Check the new certificate only against the key or keys passed in the data
121- * parameter. If one of those is the signing key and validates the new
122- * certificate, then mark the new certificate as being ok to link.
123- *
124- * Returns 0 if the new certificate was accepted, -ENOKEY if we
125- * couldn't find a matching parent certificate in the trusted list,
126- * -EKEYREJECTED if the signature check fails, and some other error if
127- * there is a matching certificate but the signature check cannot be
128- * performed.
129- */
130- int restrict_link_by_key_or_keyring (struct key * dest_keyring ,
131- const struct key_type * type ,
132- const union key_payload * payload ,
133- struct key * trusted )
112+ static bool match_either_id (const struct asymmetric_key_ids * pair ,
113+ const struct asymmetric_key_id * single )
114+ {
115+ return (asymmetric_key_id_same (pair -> id [0 ], single ) ||
116+ asymmetric_key_id_same (pair -> id [1 ], single ));
117+ }
118+
119+ static int key_or_keyring_common (struct key * dest_keyring ,
120+ const struct key_type * type ,
121+ const union key_payload * payload ,
122+ struct key * trusted , bool check_dest )
134123{
135124 const struct public_key_signature * sig ;
136- struct key * key ;
125+ struct key * key = NULL ;
137126 int ret ;
138127
139128 pr_devel ("==>%s()\n" , __func__ );
@@ -143,7 +132,7 @@ int restrict_link_by_key_or_keyring(struct key *dest_keyring,
143132 else if (dest_keyring -> type != & key_type_keyring )
144133 return - EOPNOTSUPP ;
145134
146- if (!trusted )
135+ if (!trusted && ! check_dest )
147136 return - ENOKEY ;
148137
149138 if (type != & key_type_asymmetric )
@@ -153,29 +142,122 @@ int restrict_link_by_key_or_keyring(struct key *dest_keyring,
153142 if (!sig -> auth_ids [0 ] && !sig -> auth_ids [1 ])
154143 return - ENOKEY ;
155144
156- if (trusted -> type == & key_type_keyring ) {
157- /* See if we have a key that signed this one. */
158- key = find_asymmetric_key (trusted , sig -> auth_ids [0 ],
159- sig -> auth_ids [1 ], false);
160- if (IS_ERR (key ))
161- return - ENOKEY ;
162- } else if (trusted -> type == & key_type_asymmetric ) {
163- const struct asymmetric_key_ids * kids ;
145+ if (trusted ) {
146+ if (trusted -> type == & key_type_keyring ) {
147+ /* See if we have a key that signed this one. */
148+ key = find_asymmetric_key (trusted , sig -> auth_ids [0 ],
149+ sig -> auth_ids [1 ], false);
150+ if (IS_ERR (key ))
151+ key = NULL ;
152+ } else if (trusted -> type == & key_type_asymmetric ) {
153+ const struct asymmetric_key_ids * signer_ids ;
164154
165- kids = asymmetric_key_ids (trusted );
155+ signer_ids = asymmetric_key_ids (trusted );
166156
167- if (!asymmetric_key_id_same (kids -> id [1 ], sig -> auth_ids [0 ]))
168- return - ENOKEY ;
157+ /*
158+ * The auth_ids come from the candidate key (the
159+ * one that is being considered for addition to
160+ * dest_keyring) and identify the key that was
161+ * used to sign.
162+ *
163+ * The signer_ids are identifiers for the
164+ * signing key specified for dest_keyring.
165+ *
166+ * The first auth_id is the preferred id, and
167+ * the second is the fallback. If only one
168+ * auth_id is present, it may match against
169+ * either signer_id. If two auth_ids are
170+ * present, the first auth_id must match one
171+ * signer_id and the second auth_id must match
172+ * the second signer_id.
173+ */
174+ if (!sig -> auth_ids [0 ] || !sig -> auth_ids [1 ]) {
175+ const struct asymmetric_key_id * auth_id ;
169176
170- key = __key_get (trusted );
171- } else {
172- return - EOPNOTSUPP ;
177+ auth_id = sig -> auth_ids [0 ] ?: sig -> auth_ids [1 ];
178+ if (match_either_id (signer_ids , auth_id ))
179+ key = __key_get (trusted );
180+
181+ } else if (asymmetric_key_id_same (signer_ids -> id [1 ],
182+ sig -> auth_ids [1 ]) &&
183+ match_either_id (signer_ids ,
184+ sig -> auth_ids [0 ])) {
185+ key = __key_get (trusted );
186+ }
187+ } else {
188+ return - EOPNOTSUPP ;
189+ }
173190 }
174191
192+ if (check_dest && !key ) {
193+ /* See if the destination has a key that signed this one. */
194+ key = find_asymmetric_key (dest_keyring , sig -> auth_ids [0 ],
195+ sig -> auth_ids [1 ], false);
196+ if (IS_ERR (key ))
197+ key = NULL ;
198+ }
199+
200+ if (!key )
201+ return - ENOKEY ;
202+
175203 ret = key_validate (key );
176204 if (ret == 0 )
177205 ret = verify_signature (key , sig );
178206
179207 key_put (key );
180208 return ret ;
181209}
210+
211+ /**
212+ * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
213+ * keys using the restrict_key information stored in the ring.
214+ * @dest_keyring: Keyring being linked to.
215+ * @type: The type of key being added.
216+ * @payload: The payload of the new key.
217+ * @trusted: A key or ring of keys that can be used to vouch for the new cert.
218+ *
219+ * Check the new certificate only against the key or keys passed in the data
220+ * parameter. If one of those is the signing key and validates the new
221+ * certificate, then mark the new certificate as being ok to link.
222+ *
223+ * Returns 0 if the new certificate was accepted, -ENOKEY if we
224+ * couldn't find a matching parent certificate in the trusted list,
225+ * -EKEYREJECTED if the signature check fails, and some other error if
226+ * there is a matching certificate but the signature check cannot be
227+ * performed.
228+ */
229+ int restrict_link_by_key_or_keyring (struct key * dest_keyring ,
230+ const struct key_type * type ,
231+ const union key_payload * payload ,
232+ struct key * trusted )
233+ {
234+ return key_or_keyring_common (dest_keyring , type , payload , trusted ,
235+ false);
236+ }
237+
238+ /**
239+ * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
240+ * public keys using the restrict_key information stored in the ring.
241+ * @dest_keyring: Keyring being linked to.
242+ * @type: The type of key being added.
243+ * @payload: The payload of the new key.
244+ * @trusted: A key or ring of keys that can be used to vouch for the new cert.
245+ *
246+ * Check the new certificate only against the key or keys passed in the data
247+ * parameter. If one of those is the signing key and validates the new
248+ * certificate, then mark the new certificate as being ok to link.
249+ *
250+ * Returns 0 if the new certificate was accepted, -ENOKEY if we
251+ * couldn't find a matching parent certificate in the trusted list,
252+ * -EKEYREJECTED if the signature check fails, and some other error if
253+ * there is a matching certificate but the signature check cannot be
254+ * performed.
255+ */
256+ int restrict_link_by_key_or_keyring_chain (struct key * dest_keyring ,
257+ const struct key_type * type ,
258+ const union key_payload * payload ,
259+ struct key * trusted )
260+ {
261+ return key_or_keyring_common (dest_keyring , type , payload , trusted ,
262+ true);
263+ }
0 commit comments