2 * cleankey.c - Routines to look for common key problems and clean them up.
4 * Copyright 2004,2012 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <https://www.gnu.org/licenses/>.
22 #include "build-config.h"
24 #include "decodekey.h"
26 #include "keystructs.h"
34 * dedupuids - Merge duplicate uids on a key.
35 * @key: The key to de-dup uids on.
37 * This function attempts to merge duplicate IDs on a key. It returns 0
38 * if the key is unchanged, otherwise the number of dups merged.
40 int dedupuids(struct openpgp_publickey *key)
42 struct openpgp_signedpacket_list *curuid = NULL;
43 struct openpgp_signedpacket_list *dup = NULL;
44 struct openpgp_signedpacket_list *tmp = NULL;
47 log_assert(key != NULL);
49 while (curuid != NULL) {
50 dup = find_signed_packet(curuid->next, curuid->packet);
52 logthing(LOGTHING_INFO, "Found duplicate uid: %.*s",
53 curuid->packet->length,
54 curuid->packet->data);
56 merge_packet_sigs(curuid, dup);
58 * Remove the duplicate uid.
61 while (tmp != NULL && tmp->next != dup) {
64 log_assert(tmp != NULL);
65 tmp->next = dup->next;
67 free_signedpacket_list(dup);
69 dup = find_signed_packet(curuid->next, curuid->packet);
71 curuid = curuid->next;
78 * dedupsubkeys - Merge duplicate subkeys on a key.
79 * @key: The key to de-dup subkeys on.
81 * This function attempts to merge duplicate subkeys on a key. It returns
82 * 0 if the key is unchanged, otherwise the number of dups merged.
84 int dedupsubkeys(struct openpgp_publickey *key)
86 struct openpgp_signedpacket_list *cursubkey = NULL;
87 struct openpgp_signedpacket_list *dup = NULL;
88 struct openpgp_signedpacket_list *tmp = NULL;
92 log_assert(key != NULL);
93 cursubkey = key->subkeys;
94 while (cursubkey != NULL) {
95 dup = find_signed_packet(cursubkey->next, cursubkey->packet);
97 get_packetid(cursubkey->packet, &subkeyid);
98 logthing(LOGTHING_INFO,
99 "Found duplicate subkey: 0x%016" PRIX64,
102 merge_packet_sigs(cursubkey, dup);
104 * Remove the duplicate uid.
107 while (tmp != NULL && tmp->next != dup) {
110 log_assert(tmp != NULL);
111 tmp->next = dup->next;
113 free_signedpacket_list(dup);
115 dup = find_signed_packet(cursubkey->next,
118 cursubkey = cursubkey->next;
125 * check_sighashes - Check that sig hashes are correct.
126 * @key - the check to check the sig hashes of.
128 * Given an OpenPGP key confirm that all of the sigs on it have the
129 * appropriate 2 octet hash beginning, as stored as part of the sig.
130 * This is a simple way to remove junk sigs and, for example, catches
131 * subkey sig corruption as produced by old pksd implementations.
132 * Any sig that has an incorrect hash is removed from the key. If the
133 * hash cannot be checked (eg we don't support that hash type) we err
134 * on the side of caution and keep it.
136 int clean_sighashes(struct onak_dbctx *dbctx,
137 struct openpgp_publickey *key,
138 struct openpgp_packet *sigdata,
139 struct openpgp_packet_list **sigs,
141 bool *selfsig, bool *othersig)
143 struct openpgp_packet_list *tmpsig;
144 struct openpgp_publickey *sigkeys = NULL, *curkey;
150 uint64_t keyid, sigid;
153 get_keyid(key, &keyid);
154 if (selfsig != NULL) {
157 while (*sigs != NULL) {
159 ret = calculate_packet_sighash(key, sigdata, (*sigs)->packet,
160 &hashtype, hash, &sighash);
162 if (ret == ONAK_E_UNSUPPORTED_FEATURE) {
163 get_keyid(key, &keyid);
164 logthing(LOGTHING_ERROR,
165 "Unsupported signature hash type %d on 0x%016"
172 } else if (ret != ONAK_E_OK || (!fullverify &&
173 !(hash[0] == sighash[0] &&
174 hash[1] == sighash[1]))) {
179 if (fullverify && !remove) {
180 sig_info((*sigs)->packet, &sigid, NULL);
182 /* Start by assuming it's a bad sig */
185 if (sigid == keyid) {
186 ret = onak_check_hash_sig(key->publickey,
190 /* We have a valid self signature */
191 if (ret == ONAK_E_OK) {
193 if (selfsig != NULL) {
200 dbctx->fetch_key_id(dbctx, sigid,
205 * A 64 bit collision is probably a sign of something
206 * sneaky happening, but if the signature verifies we
209 for (curkey = sigkeys; curkey != NULL;
210 curkey = curkey->next) {
212 ret = onak_check_hash_sig(curkey->publickey,
216 /* Got a valid signature */
217 if (ret == ONAK_E_OK) {
219 if (othersig != NULL) {
226 free_publickey(sigkeys);
233 *sigs = (*sigs)->next;
235 free_packet_list(tmpsig);
238 sigs = &(*sigs)->next;
245 int clean_list_sighashes(struct onak_dbctx *dbctx,
246 struct openpgp_publickey *key,
247 struct openpgp_signedpacket_list **siglist,
248 bool fullverify, bool needother)
250 struct openpgp_signedpacket_list **orig, *tmp = NULL;
251 bool selfsig, othersig;
256 while (siglist != NULL && *siglist != NULL) {
259 removed += clean_sighashes(dbctx, key, (*siglist)->packet,
260 &(*siglist)->sigs, fullverify, &selfsig, &othersig);
262 if (fullverify && !selfsig) {
263 /* Remove the UID/subkey if there's no selfsig */
265 *siglist = (*siglist)->next;
267 free_signedpacket_list(tmp);
269 siglist = &(*siglist)->next;
274 * We need at least one UID to have a signature from another key,
275 * otherwise we remove all of them if needother is set.
277 if (needother && fullverify && !othersig) {
279 while (siglist != NULL && *siglist != NULL) {
281 *siglist = (*siglist)->next;
283 free_signedpacket_list(tmp);
290 int clean_key_signatures(struct onak_dbctx *dbctx,
291 struct openpgp_publickey *key, bool fullverify, bool needother)
295 removed = clean_sighashes(dbctx, key, NULL, &key->sigs, fullverify,
297 removed += clean_list_sighashes(dbctx, key, &key->uids, fullverify,
299 removed += clean_list_sighashes(dbctx, key, &key->subkeys, fullverify,
305 #define UAT_LIMIT 0xFFFF
306 #define UID_LIMIT 1024
307 #define PACKET_LIMIT 8383 /* Fits in 2 byte packet length */
308 int clean_large_packets(struct openpgp_publickey *key)
310 struct openpgp_signedpacket_list **curuid = NULL;
311 struct openpgp_signedpacket_list *tmp = NULL;
315 log_assert(key != NULL);
317 while (*curuid != NULL) {
319 switch ((*curuid)->packet->tag) {
320 case OPENPGP_PACKET_UID:
321 if ((*curuid)->packet->length > UID_LIMIT)
324 case OPENPGP_PACKET_UAT:
325 if ((*curuid)->packet->length > UAT_LIMIT)
329 if ((*curuid)->packet->length > PACKET_LIMIT)
335 logthing(LOGTHING_INFO,
336 "Dropping large (%d) packet, type %d",
337 (*curuid)->packet->length,
338 (*curuid)->packet->tag);
339 /* Remove the entire large signed packet list */
341 *curuid = (*curuid)->next;
343 free_signedpacket_list(tmp);
346 curuid = &(*curuid)->next;
354 * cleankeys - Apply all available cleaning options on a list of keys.
355 * @policies: The cleaning policies to apply.
357 * Applies the requested cleaning policies to a list of keys. These are
358 * specified from the ONAK_CLEAN_* set of flags, or ONAK_CLEAN_ALL to
359 * apply all available cleaning options. Returns 0 if no changes were
360 * made, otherwise the number of keys cleaned. Note that some options
361 * may result in keys being removed entirely from the list.
363 int cleankeys(struct onak_dbctx *dbctx, struct openpgp_publickey **keys,
366 struct openpgp_publickey **curkey, *tmp;
367 struct openpgp_fingerprint fp;
368 int changed = 0, count = 0;
375 while (*curkey != NULL) {
376 if (policies & ONAK_CLEAN_DROP_V3_KEYS) {
377 if ((*curkey)->publickey->data[0] < 4) {
378 /* Remove the key from the list if it's < v4 */
387 if (policies & ONAK_CLEAN_LARGE_PACKETS) {
388 count += clean_large_packets(*curkey);
390 count += dedupuids(*curkey);
391 count += dedupsubkeys(*curkey);
392 if (policies & (ONAK_CLEAN_CHECK_SIGHASH |
393 ONAK_CLEAN_VERIFY_SIGNATURES)) {
395 needother = policies & ONAK_CLEAN_NEED_OTHER_SIG;
398 * Check if we already have the key; if we do
399 * then we can skip the check to make sure we
400 * have signatures from other keys.
402 get_fingerprint((*curkey)->publickey, &fp);
404 needother = dbctx->fetch_key(dbctx, &fp,
409 count += clean_key_signatures(dbctx, *curkey,
410 policies & ONAK_CLEAN_VERIFY_SIGNATURES,
416 if ((*curkey)->uids == NULL) {
417 /* No valid UIDS so remove the key from the list */
423 curkey = &(*curkey)->next;