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/>.
25 #include "keystructs.h"
33 * dedupuids - Merge duplicate uids on a key.
34 * @key: The key to de-dup uids on.
36 * This function attempts to merge duplicate IDs on a key. It returns 0
37 * if the key is unchanged, otherwise the number of dups merged.
39 int dedupuids(struct openpgp_publickey *key)
41 struct openpgp_signedpacket_list *curuid = NULL;
42 struct openpgp_signedpacket_list *dup = NULL;
43 struct openpgp_signedpacket_list *tmp = NULL;
46 log_assert(key != NULL);
48 while (curuid != NULL) {
49 dup = find_signed_packet(curuid->next, curuid->packet);
51 logthing(LOGTHING_INFO, "Found duplicate uid: %.*s",
52 curuid->packet->length,
53 curuid->packet->data);
55 merge_packet_sigs(curuid, dup);
57 * Remove the duplicate uid.
60 while (tmp != NULL && tmp->next != dup) {
63 log_assert(tmp != NULL);
64 tmp->next = dup->next;
66 free_signedpacket_list(dup);
68 dup = find_signed_packet(curuid->next, curuid->packet);
70 curuid = curuid->next;
77 * dedupsubkeys - Merge duplicate subkeys on a key.
78 * @key: The key to de-dup subkeys on.
80 * This function attempts to merge duplicate subkeys on a key. It returns
81 * 0 if the key is unchanged, otherwise the number of dups merged.
83 int dedupsubkeys(struct openpgp_publickey *key)
85 struct openpgp_signedpacket_list *cursubkey = NULL;
86 struct openpgp_signedpacket_list *dup = NULL;
87 struct openpgp_signedpacket_list *tmp = NULL;
91 log_assert(key != NULL);
92 cursubkey = key->subkeys;
93 while (cursubkey != NULL) {
94 dup = find_signed_packet(cursubkey->next, cursubkey->packet);
96 get_packetid(cursubkey->packet, &subkeyid);
97 logthing(LOGTHING_INFO,
98 "Found duplicate subkey: 0x%016" PRIX64,
101 merge_packet_sigs(cursubkey, dup);
103 * Remove the duplicate uid.
106 while (tmp != NULL && tmp->next != dup) {
109 log_assert(tmp != NULL);
110 tmp->next = dup->next;
112 free_signedpacket_list(dup);
114 dup = find_signed_packet(cursubkey->next,
117 cursubkey = cursubkey->next;
124 * check_sighashes - Check that sig hashes are correct.
125 * @key - the check to check the sig hashes of.
127 * Given an OpenPGP key confirm that all of the sigs on it have the
128 * appropriate 2 octet hash beginning, as stored as part of the sig.
129 * This is a simple way to remove junk sigs and, for example, catches
130 * subkey sig corruption as produced by old pksd implementations.
131 * Any sig that has an incorrect hash is removed from the key. If the
132 * hash cannot be checked (eg we don't support that hash type) we err
133 * on the side of caution and keep it.
135 int clean_sighashes(struct openpgp_publickey *key,
136 struct openpgp_packet *sigdata,
137 struct openpgp_packet_list **sigs)
139 struct openpgp_packet_list *tmpsig;
142 while (*sigs != NULL) {
143 if (check_packet_sighash(key, sigdata, (*sigs)->packet) == 0) {
145 *sigs = (*sigs)->next;
147 free_packet_list(tmpsig);
150 sigs = &(*sigs)->next;
157 int clean_list_sighashes(struct openpgp_publickey *key,
158 struct openpgp_signedpacket_list *siglist)
162 while (siglist != NULL) {
163 removed += clean_sighashes(key, siglist->packet,
165 siglist = siglist->next;
171 int clean_key_sighashes(struct openpgp_publickey *key)
175 removed = clean_sighashes(key, NULL, &key->sigs);
176 removed += clean_list_sighashes(key, key->uids);
177 removed += clean_list_sighashes(key, key->subkeys);
182 #define UAT_LIMIT 0xFFFF
183 #define UID_LIMIT 1024
184 #define PACKET_LIMIT 8383 /* Fits in 2 byte packet length */
185 int clean_large_packets(struct openpgp_publickey *key)
187 struct openpgp_signedpacket_list **curuid = NULL;
188 struct openpgp_signedpacket_list *tmp = NULL;
192 log_assert(key != NULL);
194 while (*curuid != NULL) {
196 switch ((*curuid)->packet->tag) {
197 case OPENPGP_PACKET_UID:
198 if ((*curuid)->packet->length > UID_LIMIT)
201 case OPENPGP_PACKET_UAT:
202 if ((*curuid)->packet->length > UAT_LIMIT)
206 if ((*curuid)->packet->length > PACKET_LIMIT)
212 logthing(LOGTHING_INFO,
213 "Dropping large (%d) packet, type %d",
214 (*curuid)->packet->length,
215 (*curuid)->packet->tag);
216 /* Remove the entire large signed packet list */
218 *curuid = (*curuid)->next;
220 free_signedpacket_list(tmp);
223 curuid = &(*curuid)->next;
231 * cleankeys - Apply all available cleaning options on a list of keys.
232 * @policies: The cleaning policies to apply.
234 * Applies the requested cleaning policies to a list of keys. These are
235 * specified from the ONAK_CLEAN_* set of flags, or ONAK_CLEAN_ALL to
236 * apply all available cleaning options. Returns 0 if no changes were
237 * made, otherwise the number of keys cleaned. Note that some options
238 * may result in keys being removed entirely from the list.
240 int cleankeys(struct openpgp_publickey **keys, uint64_t policies)
242 struct openpgp_publickey *curkey;
243 int changed = 0, count = 0;
249 while (curkey != NULL) {
250 if (policies & ONAK_CLEAN_LARGE_PACKETS) {
251 count += clean_large_packets(curkey);
253 count += dedupuids(curkey);
254 count += dedupsubkeys(curkey);
255 if (policies & ONAK_CLEAN_CHECK_SIGHASH) {
256 count += clean_key_sighashes(curkey);
261 curkey = curkey->next;