2 * merge.c - Routines to merge OpenPGP public keys.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
15 #include "keystructs.h"
21 * compare_packets - Check to see if 2 OpenPGP packets are the same.
22 * @a: The first key to compare.
23 * @b: The second key to compare.
25 * Takes 2 keys and returns true if they are the same and false otherwise.
27 bool compare_packets(struct openpgp_packet *a, struct openpgp_packet *b)
29 return (a->tag == b->tag && a->length == b->length &&
30 !memcmp(a->data, b->data, b->length));
34 * find_packet - Checks to see if an OpenPGP packet exists in a list.
35 * @packet_list: The list of packets to look in.
36 * @packet: The packet to look for.
38 * Walks through the packet_list checking to see if the packet given is
39 * present in it. Returns true if it is.
41 bool find_packet(struct openpgp_packet_list *packet_list,
42 struct openpgp_packet *packet)
46 while (!found && packet_list != NULL) {
47 if (compare_packets(packet_list->packet, packet)) {
50 packet_list = packet_list -> next;
57 * get_signed_packet - Gets a signed packet from a list.
58 * @packet_list: The list of packets to look in.
59 * @packet: The packet to look for.
61 * Walks through the signedpacket_list looking for the supplied packet and
62 * returns it if found. Otherwise returns NULL.
64 struct openpgp_signedpacket_list *find_signed_packet(
65 struct openpgp_signedpacket_list *packet_list,
66 struct openpgp_packet *packet)
68 struct openpgp_signedpacket_list *found = NULL;
70 while (found == NULL && packet_list != NULL) {
71 if (compare_packets(packet_list->packet, packet)) {
74 packet_list = packet_list -> next;
81 * remove_signed_packet - Removes a signed packet from a list.
82 * @packet_list: The list of packets to look in.
83 * @packet: The packet to remove.
85 * Walks through the signedpacket_list looking for the supplied packet and
86 * removes it if found. Assumes the packet can only exist a maximum of
89 bool remove_signed_packet(struct openpgp_signedpacket_list **packet_list,
90 struct openpgp_signedpacket_list **list_end,
91 struct openpgp_packet *packet)
93 struct openpgp_signedpacket_list *cur = NULL;
94 struct openpgp_signedpacket_list *prev = NULL;
97 for (cur = *packet_list; !found && (cur != NULL); cur = cur->next) {
98 if (compare_packets(cur->packet, packet)) {
101 *packet_list = cur->next;
103 prev->next = cur->next;
105 if (cur->next == NULL) {
108 // TODO: Free the removed signed packet...
117 * merge_packet_sigs - Takes 2 signed packets and merges their sigs.
118 * @old: The old signed packet.
119 * @new: The new signed packet.
121 * Takes 2 signed packet list structures and the sigs of the packets on
122 * the head of these structures. These packets must both be the same and
123 * the fully merged structure is returned in old and the minimal
124 * difference to get from old to new in new.
126 int merge_packet_sigs(struct openpgp_signedpacket_list *old,
127 struct openpgp_signedpacket_list *new)
129 struct openpgp_packet_list *lastpacket = NULL;
130 struct openpgp_packet_list *curpacket = NULL;
131 struct openpgp_packet_list *nextpacket = NULL;
133 assert(compare_packets(old->packet, new->packet));
135 curpacket = new->sigs;
136 while (curpacket != NULL) {
137 nextpacket = curpacket->next;
138 if (find_packet(old->sigs, curpacket->packet)) {
140 * We already have this sig, remove it from the
141 * difference list and free the memory allocated for
144 if (lastpacket != NULL) {
145 lastpacket->next = curpacket->next;
147 assert(curpacket == new->sigs);
148 new->sigs = curpacket->next;
150 curpacket->next = NULL;
151 free_packet_list(curpacket);
153 lastpacket = curpacket;
155 curpacket = nextpacket;
157 new->last_sig = lastpacket;
160 * What's left on new->sigs now are the new signatures, so add them to
163 packet_list_add(&old->sigs, &old->last_sig, new->sigs);
169 * merge_signed_packets - Takes 2 lists of signed packets and merges them.
170 * @old: The old signed packet list.
171 * @new: The new signed packet list.
173 * Takes 2 lists of signed packets and merges them. The complete list of
174 * signed packets & sigs is returned in old and the minimal set of
175 * differences required to get from old to new in new.
177 int merge_signed_packets(struct openpgp_signedpacket_list **old,
178 struct openpgp_signedpacket_list **old_end,
179 struct openpgp_signedpacket_list **new,
180 struct openpgp_signedpacket_list **new_end)
182 struct openpgp_signedpacket_list *curelem = NULL;
183 struct openpgp_signedpacket_list *newelem = NULL;
185 for (curelem = *old; curelem != NULL; curelem = curelem->next) {
186 newelem = find_signed_packet(*new, curelem->packet);
187 if (newelem != NULL) {
188 merge_packet_sigs(curelem, newelem);
191 * If there are no sigs left on the new signed packet
192 * then remove it from the list.
194 if (newelem->sigs == NULL) {
195 remove_signed_packet(new,
203 * If *new != NULL now then there might be UIDs on the new key that
204 * weren't on the old key. Walk through them, checking if the UID is
205 * on the old key and if not adding them to it.
207 for (curelem = *new; curelem != NULL;
208 curelem = curelem->next) {
210 if (find_signed_packet(*old, curelem->packet) == NULL) {
211 ADD_PACKET_TO_LIST((*old_end),
212 packet_dup(curelem->packet));
216 packet_list_add(&(*old_end)->sigs,
217 &(*old_end)->last_sig,
226 * merge_keys - Takes 2 public keys and merges them.
227 * @a: The old key. The merged key is returned in this structure.
228 * @b: The new key. The changed from old to new keys are returned in this
231 * This function takes 2 keys and merges them. It then returns the merged
232 * key in a and the difference between this new key and the original a
233 * in b (ie newb contains the minimum amount of detail necessary to
234 * convert olda to newa). The intention is that olda is provided from
235 * internal storage and oldb from the remote user. newa is then stored in
236 * internal storage and newb is sent to all our keysync peers.
238 int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b)
240 int rc = 0; /* Return code */
241 struct openpgp_packet_list *curpacket = NULL;
242 struct openpgp_packet_list *lastpacket = NULL;
243 struct openpgp_packet_list *nextpacket = NULL;
245 if (a == NULL || b == NULL) {
250 } else if (get_keyid(a) != get_keyid(b)) {
252 * Key IDs are different.
257 * Key IDs are the same, so I guess we have to merge them.
259 curpacket = b->revocations;
260 while (curpacket != NULL) {
261 nextpacket = curpacket->next;
262 if (find_packet(a->revocations, curpacket->packet)) {
264 * We already have this revocation, remove it
265 * from the difference list and free the memory
269 if (lastpacket != NULL) {
270 lastpacket->next = curpacket->next;
272 assert(curpacket == b->revocations);
273 b->revocations = curpacket->next;
275 curpacket->next = NULL;
276 free_packet_list(curpacket);
279 lastpacket = curpacket;
281 curpacket = nextpacket;
283 b->last_revocation = lastpacket;
286 * Anything left on b->revocations doesn't exist on
287 * a->revocations, so add them to the list.
289 packet_list_add(&a->revocations,
294 * Merge uids (signed list).
295 * Merge subkeys (signed list).
297 merge_signed_packets(&a->uids, &a->last_uid,
298 &b->uids, &b->last_uid);
299 merge_signed_packets(&a->subkeys, &a->last_subkey,
300 &b->subkeys, &b->last_subkey);
308 * update_keys - Takes a list of public keys and updates them in the DB.
309 * @keys: The keys to update in the DB.
310 * @verbose: Should we output more information as we add keys?
312 * Takes a list of keys and adds them to the database, merging them with
313 * the key in the database if it's already present there. The key list is
314 * update to contain the minimum set of updates required to get from what
315 * we had before to what we have now (ie the set of data that was added to
316 * the DB). Returns the number of entirely new keys added.
318 int update_keys(struct openpgp_publickey **keys, bool verbose)
320 struct openpgp_publickey *curkey = NULL;
321 struct openpgp_publickey *oldkey = NULL;
322 struct openpgp_publickey *prev = NULL;
326 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
327 intrans = starttrans();
329 fprintf(stderr, "Fetching key 0x%llX, result: %d\n",
331 fetch_key(get_keyid(curkey), &oldkey, intrans));
333 fetch_key(get_keyid(curkey), &oldkey, intrans);
337 * If we already have the key stored in the DB then merge it
338 * with the new one that's been supplied. Otherwise the key
339 * we've just got is the one that goes in the DB and also the
340 * one that we send out.
342 if (oldkey != NULL) {
343 merge_keys(oldkey, curkey);
344 if (curkey->revocations == NULL &&
345 curkey->uids == NULL &&
346 curkey->subkeys == NULL) {
348 *keys = curkey->next;
350 prev->next = curkey->next;
356 fprintf(stderr, "Merged key; storing updated key.\n");
358 store_key(oldkey, intrans, true);
360 free_publickey(oldkey);
364 fprintf(stderr, "Storing completely new key.\n");
366 store_key(curkey, intrans, false);