Update Debian Vcs-* fields to point to git repository
[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, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "cleankey.h"
25 #include "keystructs.h"
26 #include "log.h"
27 #include "mem.h"
28 #include "merge.h"
29 #include "onak-conf.h"
30 #include "sigcheck.h"
31
32 /**
33  *      dedupuids - Merge duplicate uids on a key.
34  *      @key: The key to de-dup uids on.
35  *
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.
38  */
39 int dedupuids(struct openpgp_publickey *key)
40 {
41         struct openpgp_signedpacket_list *curuid = NULL;
42         struct openpgp_signedpacket_list *dup = NULL;
43         struct openpgp_signedpacket_list *tmp = NULL;
44         int                               merged = 0;
45
46         log_assert(key != NULL);
47         curuid = key->uids;
48         while (curuid != NULL) {
49                 dup = find_signed_packet(curuid->next, curuid->packet);
50                 while (dup != NULL) {
51                         logthing(LOGTHING_INFO, "Found duplicate uid: %.*s",
52                                         curuid->packet->length,
53                                         curuid->packet->data);
54                         merged++;
55                         merge_packet_sigs(curuid, dup);
56                         /*
57                          * Remove the duplicate uid.
58                          */
59                         tmp = curuid;
60                         while (tmp != NULL && tmp->next != dup) {
61                                 tmp = tmp->next;
62                         }
63                         log_assert(tmp != NULL);
64                         tmp->next = dup->next;
65                         dup->next = NULL;
66                         free_signedpacket_list(dup);
67
68                         dup = find_signed_packet(curuid->next, curuid->packet);
69                 }
70                 curuid = curuid->next;
71         }
72
73         return merged;
74 }
75
76 /**
77  *      check_sighashes - Check that sig hashes are correct.
78  *      @key - the check to check the sig hashes of.
79  *
80  *      Given an OpenPGP key confirm that all of the sigs on it have the
81  *      appropriate 2 octet hash beginning, as stored as part of the sig.
82  *      This is a simple way to remove junk sigs and, for example, catches
83  *      subkey sig corruption as produced by old pksd implementations.
84  *      Any sig that has an incorrect hash is removed from the key. If the
85  *      hash cannot be checked (eg we don't support that hash type) we err
86  *      on the side of caution and keep it.
87  */
88 int clean_sighashes(struct openpgp_publickey *key,
89                 struct openpgp_packet *sigdata,
90                 struct openpgp_packet_list **sigs)
91 {
92         struct openpgp_packet_list *tmpsig;
93         int removed = 0;
94
95         while (*sigs != NULL) {
96                 if (check_packet_sighash(key, sigdata, (*sigs)->packet) == 0) {
97                         tmpsig = *sigs;
98                         *sigs = (*sigs)->next;
99                         tmpsig->next = NULL;
100                         free_packet_list(tmpsig);
101                         removed++;
102                 } else {
103                         sigs = &(*sigs)->next;
104                 }
105         }
106
107         return removed;
108 }
109
110 int clean_list_sighashes(struct openpgp_publickey *key,
111                         struct openpgp_signedpacket_list *siglist)
112 {
113         int removed = 0;
114
115         while (siglist != NULL) {
116                 removed += clean_sighashes(key, siglist->packet,
117                         &siglist->sigs);
118                 siglist = siglist->next;
119         }
120
121         return removed;
122 }
123
124 int clean_key_sighashes(struct openpgp_publickey *key)
125 {
126         int removed;
127
128         removed = clean_sighashes(key, NULL, &key->sigs);
129         removed += clean_list_sighashes(key, key->uids);
130         removed += clean_list_sighashes(key, key->subkeys);
131
132         return removed;
133 }
134
135 /**
136  *      cleankeys - Apply all available cleaning options on a list of keys.
137  *      @keys: The list of keys to clean.
138  *
139  *      Applies all the cleaning options we can (eg duplicate key ids) to a
140  *      list of keys. Returns 0 if no changes were made, otherwise the number
141  *      of keys cleaned.
142  */
143 int cleankeys(struct openpgp_publickey *keys)
144 {
145         int changed = 0, count;
146
147         while (keys != NULL) {
148                 count = dedupuids(keys);
149                 if (config.check_sighash) {
150                         count += clean_key_sighashes(keys);
151                 }
152                 if (count > 0) {
153                         changed++;
154                 }
155                 keys = keys->next;
156         }
157
158         return changed;
159 }