]> the.earth.li Git - onak.git/blob - cleankey.c
Remove --with-systemd option to dh
[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 (othersig != NULL) {
155                 *othersig = false;
156         }
157         if (selfsig != NULL) {
158                 *selfsig = false;
159         }
160         while (*sigs != NULL) {
161                 remove = false;
162                 ret = calculate_packet_sighash(key, sigdata, (*sigs)->packet,
163                                 &hashtype, hash, &sighash);
164
165                 if (ret == ONAK_E_UNSUPPORTED_FEATURE) {
166                         get_keyid(key, &keyid);
167                         logthing(LOGTHING_ERROR,
168                                 "Unsupported signature hash type %d on 0x%016"
169                                 PRIX64,
170                                 hashtype,
171                                 keyid);
172                         if (fullverify) {
173                                 remove = true;
174                         }
175                 } else if (ret != ONAK_E_OK || (!fullverify &&
176                                 !(hash[0] == sighash[0] &&
177                                         hash[1] == sighash[1]))) {
178                         remove = true;
179                 }
180
181 #if HAVE_CRYPTO
182                 if (fullverify && !remove) {
183                         sig_info((*sigs)->packet, &sigid, NULL);
184
185                         /* Start by assuming it's a bad sig */
186
187                         remove = true;
188                         if (sigid == keyid) {
189                                 ret = onak_check_hash_sig(key, (*sigs)->packet,
190                                                 hash, hashtype);
191
192                                 /* We have a valid self signature */
193                                 if (ret == ONAK_E_OK) {
194                                         remove = false;
195                                         if (selfsig != NULL) {
196                                                 *selfsig = true;
197                                         }
198                                 }
199                         }
200
201                         if (remove) {
202                                 dbctx->fetch_key_id(dbctx, sigid,
203                                                 &sigkeys, false);
204                         }
205
206                         /*
207                          * A 64 bit collision is probably a sign of something
208                          * sneaky happening, but if the signature verifies we
209                          * should keep it.
210                          */
211                         for (curkey = sigkeys; curkey != NULL;
212                                         curkey = curkey->next) {
213
214                                 ret = onak_check_hash_sig(curkey,
215                                                 (*sigs)->packet,
216                                                 hash, hashtype);
217
218                                 /* Got a valid signature */
219                                 if (ret == ONAK_E_OK) {
220                                         remove = false;
221                                         if (othersig != NULL) {
222                                                 *othersig = true;
223                                         }
224                                         break;
225                                 }
226                         }
227
228                         free_publickey(sigkeys);
229                         sigkeys = NULL;
230                 }
231 #endif
232
233                 if (remove) {
234                         tmpsig = *sigs;
235                         *sigs = (*sigs)->next;
236                         tmpsig->next = NULL;
237                         free_packet_list(tmpsig);
238                         removed++;
239                 } else {
240                         sigs = &(*sigs)->next;
241                 }
242         }
243
244         return removed;
245 }
246
247 int clean_list_sighashes(struct onak_dbctx *dbctx,
248                         struct openpgp_publickey *key,
249                         struct openpgp_signedpacket_list **siglist,
250                         bool fullverify, bool needother)
251 {
252         struct openpgp_signedpacket_list *tmp = NULL;
253         bool selfsig, othersig;
254         int removed = 0;
255
256         while (siglist != NULL && *siglist != NULL) {
257                 selfsig = othersig = false;
258
259                 removed += clean_sighashes(dbctx, key, (*siglist)->packet,
260                         &(*siglist)->sigs, fullverify, &selfsig, &othersig);
261
262                 if (fullverify && (!selfsig || (needother && !othersig))) {
263                         /* Remove the UID/subkey if there's no selfsig */
264                         tmp = *siglist;
265                         *siglist = (*siglist)->next;
266                         tmp->next = NULL;
267                         free_signedpacket_list(tmp);
268                 } else {
269                         siglist = &(*siglist)->next;
270                 }
271         }
272
273         return removed;
274 }
275
276 int clean_key_signatures(struct onak_dbctx *dbctx,
277                 struct openpgp_publickey *key, bool fullverify, bool needother)
278 {
279         int removed;
280
281         removed = clean_sighashes(dbctx, key, NULL, &key->sigs, fullverify,
282                         NULL, NULL);
283         removed += clean_list_sighashes(dbctx, key, &key->uids, fullverify,
284                         needother);
285         removed += clean_list_sighashes(dbctx, key, &key->subkeys, fullverify,
286                         false);
287
288         return removed;
289 }
290
291 #define UAT_LIMIT       0xFFFF
292 #define UID_LIMIT       1024
293 #define PACKET_LIMIT    8383            /* Fits in 2 byte packet length */
294 int clean_large_packets(struct openpgp_publickey *key)
295 {
296         struct openpgp_signedpacket_list **curuid = NULL;
297         struct openpgp_signedpacket_list *tmp = NULL;
298         bool                              drop;
299         int                               dropped = 0;
300
301         log_assert(key != NULL);
302         curuid = &key->uids;
303         while (*curuid != NULL) {
304                 drop = false;
305                 switch ((*curuid)->packet->tag) {
306                 case OPENPGP_PACKET_UID:
307                         if ((*curuid)->packet->length > UID_LIMIT)
308                                 drop = true;
309                         break;
310                 case OPENPGP_PACKET_UAT:
311                         if ((*curuid)->packet->length > UAT_LIMIT)
312                                 drop = true;
313                         break;
314                 default:
315                         if ((*curuid)->packet->length > PACKET_LIMIT)
316                                 drop = true;
317                         break;
318                 }
319
320                 if (drop) {
321                         logthing(LOGTHING_INFO,
322                                         "Dropping large (%d) packet, type %d",
323                                         (*curuid)->packet->length,
324                                         (*curuid)->packet->tag);
325                         /* Remove the entire large signed packet list */
326                         tmp = *curuid;
327                         *curuid = (*curuid)->next;
328                         tmp->next = NULL;
329                         free_signedpacket_list(tmp);
330                         dropped++;
331                 } else {
332                         curuid = &(*curuid)->next;
333                 }
334         }
335
336         return dropped;
337 }
338
339 /**
340  *      cleankeys - Apply all available cleaning options on a list of keys.
341  *      @policies: The cleaning policies to apply.
342  *
343  *      Applies the requested cleaning policies to a list of keys. These are
344  *      specified from the ONAK_CLEAN_* set of flags, or ONAK_CLEAN_ALL to
345  *      apply all available cleaning options. Returns 0 if no changes were
346  *      made, otherwise the number of keys cleaned. Note that some options
347  *      may result in keys being removed entirely from the list.
348  */
349 int cleankeys(struct onak_dbctx *dbctx, struct openpgp_publickey **keys,
350                 uint64_t policies)
351 {
352         struct openpgp_publickey **curkey, *tmp;
353         int changed = 0, count = 0;
354
355         if (keys == NULL)
356                 return 0;
357
358         curkey = keys;
359         while (*curkey != NULL) {
360                 if (policies & ONAK_CLEAN_DROP_V3_KEYS) {
361                         if ((*curkey)->publickey->data[0] < 4) {
362                                 /* Remove the key from the list if it's < v4 */
363                                 tmp = *curkey;
364                                 *curkey = tmp->next;
365                                 tmp->next = NULL;
366                                 free_publickey(tmp);
367                                 changed++;
368                                 continue;
369                         }
370                 }
371                 if (policies & ONAK_CLEAN_LARGE_PACKETS) {
372                         count += clean_large_packets(*curkey);
373                 }
374                 count += dedupuids(*curkey);
375                 count += dedupsubkeys(*curkey);
376                 if (policies & (ONAK_CLEAN_CHECK_SIGHASH |
377                                         ONAK_CLEAN_VERIFY_SIGNATURES)) {
378                         count += clean_key_signatures(dbctx, *curkey,
379                                 policies & ONAK_CLEAN_VERIFY_SIGNATURES,
380                                 policies & ONAK_CLEAN_NEED_OTHER_SIG);
381                 }
382                 if (count > 0) {
383                         changed++;
384                 }
385                 if ((*curkey)->uids == NULL) {
386                         /* No valid UIDS so remove the key from the list */
387                         tmp = *curkey;
388                         *curkey = tmp->next;
389                         tmp->next = NULL;
390                         free_publickey(tmp);
391                 } else {
392                         curkey = &(*curkey)->next;
393                 }
394         }
395
396         return changed;
397 }