]> the.earth.li Git - onak.git/blob - cleankey.c
Bump debhelper compat level to 13
[onak.git] / cleankey.c
1 /*
2  * cleankey.c - Routines to look for common key problems and clean them up.
3  *
4  * Copyright 2004,2012 Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
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/>.
17  */
18
19 #include <stdbool.h>
20 #include <stdlib.h>
21
22 #include "build-config.h"
23 #include "cleankey.h"
24 #include "decodekey.h"
25 #include "keyid.h"
26 #include "keystructs.h"
27 #include "log.h"
28 #include "mem.h"
29 #include "merge.h"
30 #include "openpgp.h"
31 #include "sigcheck.h"
32
33 /**
34  *      dedupuids - Merge duplicate uids on a key.
35  *      @key: The key to de-dup uids on.
36  *
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.
39  */
40 int dedupuids(struct openpgp_publickey *key)
41 {
42         struct openpgp_signedpacket_list *curuid = NULL;
43         struct openpgp_signedpacket_list *dup = NULL;
44         struct openpgp_signedpacket_list *tmp = NULL;
45         int                               merged = 0;
46
47         log_assert(key != NULL);
48         curuid = key->uids;
49         while (curuid != NULL) {
50                 dup = find_signed_packet(curuid->next, curuid->packet);
51                 while (dup != NULL) {
52                         logthing(LOGTHING_INFO, "Found duplicate uid: %.*s",
53                                         curuid->packet->length,
54                                         curuid->packet->data);
55                         merged++;
56                         merge_packet_sigs(curuid, dup);
57                         /*
58                          * Remove the duplicate uid.
59                          */
60                         tmp = curuid;
61                         while (tmp != NULL && tmp->next != dup) {
62                                 tmp = tmp->next;
63                         }
64                         log_assert(tmp != NULL);
65                         tmp->next = dup->next;
66                         dup->next = NULL;
67                         free_signedpacket_list(dup);
68
69                         dup = find_signed_packet(curuid->next, curuid->packet);
70                 }
71                 curuid = curuid->next;
72         }
73
74         return merged;
75 }
76
77 /**
78  *      dedupsubkeys - Merge duplicate subkeys on a key.
79  *      @key: The key to de-dup subkeys on.
80  *
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.
83  */
84 int dedupsubkeys(struct openpgp_publickey *key)
85 {
86         struct openpgp_signedpacket_list *cursubkey = NULL;
87         struct openpgp_signedpacket_list *dup = NULL;
88         struct openpgp_signedpacket_list *tmp = NULL;
89         int                               merged = 0;
90         uint64_t                          subkeyid;
91
92         log_assert(key != NULL);
93         cursubkey = key->subkeys;
94         while (cursubkey != NULL) {
95                 dup = find_signed_packet(cursubkey->next, cursubkey->packet);
96                 while (dup != NULL) {
97                         get_packetid(cursubkey->packet, &subkeyid);
98                         logthing(LOGTHING_INFO,
99                                 "Found duplicate subkey: 0x%016" PRIX64,
100                                 subkeyid);
101                         merged++;
102                         merge_packet_sigs(cursubkey, dup);
103                         /*
104                          * Remove the duplicate uid.
105                          */
106                         tmp = cursubkey;
107                         while (tmp != NULL && tmp->next != dup) {
108                                 tmp = tmp->next;
109                         }
110                         log_assert(tmp != NULL);
111                         tmp->next = dup->next;
112                         dup->next = NULL;
113                         free_signedpacket_list(dup);
114
115                         dup = find_signed_packet(cursubkey->next,
116                                 cursubkey->packet);
117                 }
118                 cursubkey = cursubkey->next;
119         }
120
121         return merged;
122 }
123
124 /**
125  *      check_sighashes - Check that sig hashes are correct.
126  *      @key - the check to check the sig hashes of.
127  *
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.
135  */
136 int clean_sighashes(struct onak_dbctx *dbctx,
137                 struct openpgp_publickey *key,
138                 struct openpgp_packet *sigdata,
139                 struct openpgp_packet_list **sigs,
140                 bool fullverify,
141                 bool *selfsig, bool *othersig)
142 {
143         struct openpgp_packet_list *tmpsig;
144         struct openpgp_publickey *sigkeys = NULL, *curkey;
145         onak_status_t ret;
146         uint8_t hashtype;
147         uint8_t hash[64];
148         uint8_t *sighash;
149         int removed = 0;
150         uint64_t keyid, sigid;
151         bool remove;
152
153         get_keyid(key, &keyid);
154         if (selfsig != NULL) {
155                 *selfsig = false;
156         }
157         while (*sigs != NULL) {
158                 remove = false;
159                 ret = calculate_packet_sighash(key, sigdata, (*sigs)->packet,
160                                 &hashtype, hash, &sighash);
161
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"
166                                 PRIX64,
167                                 hashtype,
168                                 keyid);
169                         if (fullverify) {
170                                 remove = true;
171                         }
172                 } else if (ret != ONAK_E_OK || (!fullverify &&
173                                 !(hash[0] == sighash[0] &&
174                                         hash[1] == sighash[1]))) {
175                         remove = true;
176                 }
177
178 #if HAVE_CRYPTO
179                 if (fullverify && !remove) {
180                         sig_info((*sigs)->packet, &sigid, NULL);
181
182                         /* Start by assuming it's a bad sig */
183
184                         remove = true;
185                         if (sigid == keyid) {
186                                 ret = onak_check_hash_sig(key, (*sigs)->packet,
187                                                 hash, hashtype);
188
189                                 /* We have a valid self signature */
190                                 if (ret == ONAK_E_OK) {
191                                         remove = false;
192                                         if (selfsig != NULL) {
193                                                 *selfsig = true;
194                                         }
195                                 }
196                         }
197
198                         if (remove) {
199                                 dbctx->fetch_key_id(dbctx, sigid,
200                                                 &sigkeys, false);
201                         }
202
203                         /*
204                          * A 64 bit collision is probably a sign of something
205                          * sneaky happening, but if the signature verifies we
206                          * should keep it.
207                          */
208                         for (curkey = sigkeys; curkey != NULL;
209                                         curkey = curkey->next) {
210
211                                 ret = onak_check_hash_sig(curkey,
212                                                 (*sigs)->packet,
213                                                 hash, hashtype);
214
215                                 /* Got a valid signature */
216                                 if (ret == ONAK_E_OK) {
217                                         remove = false;
218                                         if (othersig != NULL) {
219                                                 *othersig = true;
220                                         }
221                                         break;
222                                 }
223                         }
224
225                         free_publickey(sigkeys);
226                         sigkeys = NULL;
227                 }
228 #endif
229
230                 if (remove) {
231                         tmpsig = *sigs;
232                         *sigs = (*sigs)->next;
233                         tmpsig->next = NULL;
234                         free_packet_list(tmpsig);
235                         removed++;
236                 } else {
237                         sigs = &(*sigs)->next;
238                 }
239         }
240
241         return removed;
242 }
243
244 int clean_list_sighashes(struct onak_dbctx *dbctx,
245                         struct openpgp_publickey *key,
246                         struct openpgp_signedpacket_list **siglist,
247                         bool fullverify, bool needother)
248 {
249         struct openpgp_signedpacket_list **orig, *tmp = NULL;
250         bool selfsig, othersig;
251         int removed = 0;
252
253         othersig = false;
254         orig = siglist;
255         while (siglist != NULL && *siglist != NULL) {
256                 selfsig = false;
257
258                 removed += clean_sighashes(dbctx, key, (*siglist)->packet,
259                         &(*siglist)->sigs, fullverify, &selfsig, &othersig);
260
261                 if (fullverify && !selfsig) {
262                         /* Remove the UID/subkey if there's no selfsig */
263                         tmp = *siglist;
264                         *siglist = (*siglist)->next;
265                         tmp->next = NULL;
266                         free_signedpacket_list(tmp);
267                 } else {
268                         siglist = &(*siglist)->next;
269                 }
270         }
271
272         /*
273          * We need at least one UID to have a signature from another key,
274          * otherwise we remove all of them if needother is set.
275          */
276         if (needother && fullverify && !othersig) {
277                 siglist = orig;
278                 while (siglist != NULL && *siglist != NULL) {
279                         tmp = *siglist;
280                         *siglist = (*siglist)->next;
281                         tmp->next = NULL;
282                         free_signedpacket_list(tmp);
283                 }
284         }
285
286         return removed;
287 }
288
289 int clean_key_signatures(struct onak_dbctx *dbctx,
290                 struct openpgp_publickey *key, bool fullverify, bool needother)
291 {
292         int removed;
293
294         removed = clean_sighashes(dbctx, key, NULL, &key->sigs, fullverify,
295                         NULL, NULL);
296         removed += clean_list_sighashes(dbctx, key, &key->uids, fullverify,
297                         needother);
298         removed += clean_list_sighashes(dbctx, key, &key->subkeys, fullverify,
299                         false);
300
301         return removed;
302 }
303
304 #define UAT_LIMIT       0xFFFF
305 #define UID_LIMIT       1024
306 #define PACKET_LIMIT    8383            /* Fits in 2 byte packet length */
307 int clean_large_packets(struct openpgp_publickey *key)
308 {
309         struct openpgp_signedpacket_list **curuid = NULL;
310         struct openpgp_signedpacket_list *tmp = NULL;
311         bool                              drop;
312         int                               dropped = 0;
313
314         log_assert(key != NULL);
315         curuid = &key->uids;
316         while (*curuid != NULL) {
317                 drop = false;
318                 switch ((*curuid)->packet->tag) {
319                 case OPENPGP_PACKET_UID:
320                         if ((*curuid)->packet->length > UID_LIMIT)
321                                 drop = true;
322                         break;
323                 case OPENPGP_PACKET_UAT:
324                         if ((*curuid)->packet->length > UAT_LIMIT)
325                                 drop = true;
326                         break;
327                 default:
328                         if ((*curuid)->packet->length > PACKET_LIMIT)
329                                 drop = true;
330                         break;
331                 }
332
333                 if (drop) {
334                         logthing(LOGTHING_INFO,
335                                         "Dropping large (%d) packet, type %d",
336                                         (*curuid)->packet->length,
337                                         (*curuid)->packet->tag);
338                         /* Remove the entire large signed packet list */
339                         tmp = *curuid;
340                         *curuid = (*curuid)->next;
341                         tmp->next = NULL;
342                         free_signedpacket_list(tmp);
343                         dropped++;
344                 } else {
345                         curuid = &(*curuid)->next;
346                 }
347         }
348
349         return dropped;
350 }
351
352 /**
353  *      cleankeys - Apply all available cleaning options on a list of keys.
354  *      @policies: The cleaning policies to apply.
355  *
356  *      Applies the requested cleaning policies to a list of keys. These are
357  *      specified from the ONAK_CLEAN_* set of flags, or ONAK_CLEAN_ALL to
358  *      apply all available cleaning options. Returns 0 if no changes were
359  *      made, otherwise the number of keys cleaned. Note that some options
360  *      may result in keys being removed entirely from the list.
361  */
362 int cleankeys(struct onak_dbctx *dbctx, struct openpgp_publickey **keys,
363                 uint64_t policies)
364 {
365         struct openpgp_publickey **curkey, *tmp;
366         struct openpgp_fingerprint fp;
367         int changed = 0, count = 0;
368         bool needother;
369
370         if (keys == NULL)
371                 return 0;
372
373         curkey = keys;
374         while (*curkey != NULL) {
375                 if (policies & ONAK_CLEAN_DROP_V3_KEYS) {
376                         if ((*curkey)->publickey->data[0] < 4) {
377                                 /* Remove the key from the list if it's < v4 */
378                                 tmp = *curkey;
379                                 *curkey = tmp->next;
380                                 tmp->next = NULL;
381                                 free_publickey(tmp);
382                                 changed++;
383                                 continue;
384                         }
385                 }
386                 if (policies & ONAK_CLEAN_LARGE_PACKETS) {
387                         count += clean_large_packets(*curkey);
388                 }
389                 count += dedupuids(*curkey);
390                 count += dedupsubkeys(*curkey);
391                 if (policies & (ONAK_CLEAN_CHECK_SIGHASH |
392                                         ONAK_CLEAN_VERIFY_SIGNATURES)) {
393
394                         needother = policies & ONAK_CLEAN_NEED_OTHER_SIG;
395                         if (needother) {
396                                 /*
397                                  * Check if we already have the key; if we do
398                                  * then we can skip the check to make sure we
399                                  * have signatures from other keys.
400                                  */
401                                 get_fingerprint((*curkey)->publickey, &fp);
402                                 tmp = NULL;
403                                 needother = dbctx->fetch_key(dbctx, &fp,
404                                                 &tmp, false) == 0;
405                                 free_publickey(tmp);
406                         }
407
408                         count += clean_key_signatures(dbctx, *curkey,
409                                 policies & ONAK_CLEAN_VERIFY_SIGNATURES,
410                                 needother);
411                 }
412                 if (count > 0) {
413                         changed++;
414                 }
415                 if ((*curkey)->uids == NULL) {
416                         /* No valid UIDS so remove the key from the list */
417                         tmp = *curkey;
418                         *curkey = tmp->next;
419                         tmp->next = NULL;
420                         free_publickey(tmp);
421                 } else {
422                         curkey = &(*curkey)->next;
423                 }
424         }
425
426         return changed;
427 }