2 * merge.c - Routines to merge OpenPGP public keys.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
16 #include "keystructs.h"
22 * compare_packets - Check to see if 2 OpenPGP packets are the same.
23 * @a: The first packet to compare.
24 * @b: The second packet to compare.
26 * Takes 2 packets and returns true if they are the same and false
29 bool compare_packets(struct openpgp_packet *a, struct openpgp_packet *b)
31 return (a->tag == b->tag && a->length == b->length &&
32 !memcmp(a->data, b->data, b->length));
36 * compare_signatures - Check to see if 2 OpenPGP signatures are the same.
37 * @a: The first signature to compare.
38 * @b: The second signature to compare.
40 * Takes 2 signature packets and returns true if they are the same and
43 bool compare_signatures(struct openpgp_packet *a, struct openpgp_packet *b)
45 return (sig_keyid(a) == sig_keyid(b));
49 * find_packet - Checks to see if an OpenPGP packet exists in a list.
50 * @packet_list: The list of packets to look in.
51 * @packet: The packet to look for.
53 * Walks through the packet_list checking to see if the packet given is
54 * present in it. Returns true if it is.
56 bool find_packet(struct openpgp_packet_list *packet_list,
57 struct openpgp_packet *packet)
61 while (!found && packet_list != NULL) {
62 if (compare_packets(packet_list->packet, packet)) {
65 packet_list = packet_list -> next;
72 * find_signature - Checks to see if an OpenPGP signature exists in a list.
73 * @packet_list: The list of packets to look in.
74 * @packet: The signature to look for.
76 * Walks through the packet_list checking to see if the signature given is
77 * present in it. Returns a pointer to it if it is, NULL otherwise.
80 struct openpgp_packet_list *find_signature(
81 struct openpgp_packet_list *packet_list,
82 struct openpgp_packet *packet)
84 struct openpgp_packet_list *found = NULL;
86 while (!found && packet_list != NULL) {
87 if (compare_signatures(packet_list->packet, packet)) {
90 packet_list = packet_list -> next;
97 * get_signed_packet - Gets a signed packet from a list.
98 * @packet_list: The list of packets to look in.
99 * @packet: The packet to look for.
101 * Walks through the signedpacket_list looking for the supplied packet and
102 * returns it if found. Otherwise returns NULL.
104 struct openpgp_signedpacket_list *find_signed_packet(
105 struct openpgp_signedpacket_list *packet_list,
106 struct openpgp_packet *packet)
108 struct openpgp_signedpacket_list *found = NULL;
110 while (found == NULL && packet_list != NULL) {
111 if (compare_packets(packet_list->packet, packet)) {
114 packet_list = packet_list -> next;
121 * remove_signed_packet - Removes a signed packet from a list.
122 * @packet_list: The list of packets to look in.
123 * @packet: The packet to remove.
125 * Walks through the signedpacket_list looking for the supplied packet and
126 * removes it if found. Assumes the packet can only exist a maximum of
129 bool remove_signed_packet(struct openpgp_signedpacket_list **packet_list,
130 struct openpgp_signedpacket_list **list_end,
131 struct openpgp_packet *packet)
133 struct openpgp_signedpacket_list *cur = NULL;
134 struct openpgp_signedpacket_list *prev = NULL;
137 for (cur = *packet_list; !found && (cur != NULL); cur = cur->next) {
138 if (compare_packets(cur->packet, packet)) {
141 *packet_list = cur->next;
143 prev->next = cur->next;
145 if (cur->next == NULL) {
148 // TODO: Free the removed signed packet...
157 * merge_packet_sigs - Takes 2 signed packets and merges their sigs.
158 * @old: The old signed packet.
159 * @new: The new signed packet.
161 * Takes 2 signed packet list structures and the sigs of the packets on
162 * the head of these structures. These packets must both be the same and
163 * the fully merged structure is returned in old and the minimal
164 * difference to get from old to new in new.
166 int merge_packet_sigs(struct openpgp_signedpacket_list *old,
167 struct openpgp_signedpacket_list *new)
169 struct openpgp_packet_list *lastpacket = NULL;
170 struct openpgp_packet_list *curpacket = NULL;
171 struct openpgp_packet_list *nextpacket = NULL;
173 assert(compare_packets(old->packet, new->packet));
175 curpacket = new->sigs;
176 while (curpacket != NULL) {
177 nextpacket = curpacket->next;
179 * TODO: We should be checking the signature and then
180 * potentially merging/replacing it depending on the subpackets
181 * really. For now this stops us adding the same one twice
184 if (find_signature(old->sigs, curpacket->packet)) {
186 * We already have this sig, remove it from the
187 * difference list and free the memory allocated for
190 if (lastpacket != NULL) {
191 lastpacket->next = curpacket->next;
193 assert(curpacket == new->sigs);
194 new->sigs = curpacket->next;
196 curpacket->next = NULL;
197 free_packet_list(curpacket);
199 lastpacket = curpacket;
201 curpacket = nextpacket;
203 new->last_sig = lastpacket;
206 * What's left on new->sigs now are the new signatures, so add them to
209 packet_list_add(&old->sigs, &old->last_sig, new->sigs);
215 * merge_signed_packets - Takes 2 lists of signed packets and merges them.
216 * @old: The old signed packet list.
217 * @new: The new signed packet list.
219 * Takes 2 lists of signed packets and merges them. The complete list of
220 * signed packets & sigs is returned in old and the minimal set of
221 * differences required to get from old to new in new.
223 int merge_signed_packets(struct openpgp_signedpacket_list **old,
224 struct openpgp_signedpacket_list **old_end,
225 struct openpgp_signedpacket_list **new,
226 struct openpgp_signedpacket_list **new_end)
228 struct openpgp_signedpacket_list *curelem = NULL;
229 struct openpgp_signedpacket_list *newelem = NULL;
231 for (curelem = *old; curelem != NULL; curelem = curelem->next) {
232 newelem = find_signed_packet(*new, curelem->packet);
233 if (newelem != NULL) {
234 merge_packet_sigs(curelem, newelem);
237 * If there are no sigs left on the new signed packet
238 * then remove it from the list.
240 if (newelem->sigs == NULL) {
241 remove_signed_packet(new,
249 * If *new != NULL now then there might be UIDs on the new key that
250 * weren't on the old key. Walk through them, checking if the UID is
251 * on the old key and if not adding them to it.
253 for (curelem = *new; curelem != NULL;
254 curelem = curelem->next) {
256 if (find_signed_packet(*old, curelem->packet) == NULL) {
257 ADD_PACKET_TO_LIST((*old_end),
258 packet_dup(curelem->packet));
262 packet_list_add(&(*old_end)->sigs,
263 &(*old_end)->last_sig,
272 * merge_keys - Takes 2 public keys and merges them.
273 * @a: The old key. The merged key is returned in this structure.
274 * @b: The new key. The changed from old to new keys are returned in this
277 * This function takes 2 keys and merges them. It then returns the merged
278 * key in a and the difference between this new key and the original a
279 * in b (ie newb contains the minimum amount of detail necessary to
280 * convert olda to newa). The intention is that olda is provided from
281 * internal storage and oldb from the remote user. newa is then stored in
282 * internal storage and newb is sent to all our keysync peers.
284 int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b)
286 int rc = 0; /* Return code */
287 struct openpgp_packet_list *curpacket = NULL;
288 struct openpgp_packet_list *lastpacket = NULL;
289 struct openpgp_packet_list *nextpacket = NULL;
291 if (a == NULL || b == NULL) {
296 } else if (get_keyid(a) != get_keyid(b)) {
298 * Key IDs are different.
303 * Key IDs are the same, so I guess we have to merge them.
305 curpacket = b->revocations;
306 while (curpacket != NULL) {
307 nextpacket = curpacket->next;
308 if (find_packet(a->revocations, curpacket->packet)) {
310 * We already have this revocation, remove it
311 * from the difference list and free the memory
315 if (lastpacket != NULL) {
316 lastpacket->next = curpacket->next;
318 assert(curpacket == b->revocations);
319 b->revocations = curpacket->next;
321 curpacket->next = NULL;
322 free_packet_list(curpacket);
325 lastpacket = curpacket;
327 curpacket = nextpacket;
329 b->last_revocation = lastpacket;
332 * Anything left on b->revocations doesn't exist on
333 * a->revocations, so add them to the list.
335 packet_list_add(&a->revocations,
340 * Merge uids (signed list).
341 * Merge subkeys (signed list).
343 merge_signed_packets(&a->uids, &a->last_uid,
344 &b->uids, &b->last_uid);
345 merge_signed_packets(&a->subkeys, &a->last_subkey,
346 &b->subkeys, &b->last_subkey);
354 * update_keys - Takes a list of public keys and updates them in the DB.
355 * @keys: The keys to update in the DB.
356 * @verbose: Should we output more information as we add keys?
358 * Takes a list of keys and adds them to the database, merging them with
359 * the key in the database if it's already present there. The key list is
360 * update to contain the minimum set of updates required to get from what
361 * we had before to what we have now (ie the set of data that was added to
362 * the DB). Returns the number of entirely new keys added.
364 int update_keys(struct openpgp_publickey **keys, bool verbose)
366 struct openpgp_publickey *curkey = NULL;
367 struct openpgp_publickey *oldkey = NULL;
368 struct openpgp_publickey *prev = NULL;
372 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
373 intrans = starttrans();
375 fprintf(stderr, "Fetching key 0x%llX, result: %d\n",
377 fetch_key(get_keyid(curkey), &oldkey, intrans));
379 fetch_key(get_keyid(curkey), &oldkey, intrans);
383 * If we already have the key stored in the DB then merge it
384 * with the new one that's been supplied. Otherwise the key
385 * we've just got is the one that goes in the DB and also the
386 * one that we send out.
388 if (oldkey != NULL) {
389 merge_keys(oldkey, curkey);
390 if (curkey->revocations == NULL &&
391 curkey->uids == NULL &&
392 curkey->subkeys == NULL) {
394 *keys = curkey->next;
396 prev->next = curkey->next;
402 fprintf(stderr, "Merged key; storing updated key.\n");
404 store_key(oldkey, intrans, true);
406 free_publickey(oldkey);
410 fprintf(stderr, "Storing completely new key.\n");
412 store_key(curkey, intrans, false);