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) {
115 * merge_packet_sigs - Takes 2 signed packets and merges their sigs.
116 * @old: The old signed packet.
117 * @new: The new signed packet.
119 * Takes 2 signed packet list structures and the sigs of the packets on
120 * the head of these structures. These packets must both be the same and
121 * the fully merged structure is returned in old and the minimal
122 * difference to get from old to new in new.
124 int merge_packet_sigs(struct openpgp_signedpacket_list *old,
125 struct openpgp_signedpacket_list *new)
127 struct openpgp_packet_list *lastpacket = NULL;
128 struct openpgp_packet_list *curpacket = NULL;
129 struct openpgp_packet_list *nextpacket = NULL;
131 assert(compare_packets(old->packet, new->packet));
133 curpacket = new->sigs;
134 while (curpacket != NULL) {
135 nextpacket = curpacket->next;
136 if (find_packet(old->sigs, curpacket->packet)) {
138 * We already have this sig, remove it from the
139 * difference list and free the memory allocated for
142 if (lastpacket != NULL) {
143 lastpacket->next = curpacket->next;
145 assert(curpacket == new->sigs);
146 new->sigs = curpacket->next;
148 curpacket->next = NULL;
149 free_packet_list(curpacket);
151 lastpacket = curpacket;
153 curpacket = nextpacket;
155 new->last_sig = lastpacket;
158 * What's left on new->sigs now are the new signatures, so add them to
161 packet_list_add(&old->sigs, &old->last_sig, new->sigs);
167 * merge_signed_packets - Takes 2 lists of signed packets and merges them.
168 * @old: The old signed packet list.
169 * @new: The new signed packet list.
171 * Takes 2 lists of signed packets and merges them. The complete list of
172 * signed packets & sigs is returned in old and the minimal set of
173 * differences required to get from old to new in new.
175 int merge_signed_packets(struct openpgp_signedpacket_list **old,
176 struct openpgp_signedpacket_list **old_end,
177 struct openpgp_signedpacket_list **new,
178 struct openpgp_signedpacket_list **new_end)
180 struct openpgp_signedpacket_list *curelem = NULL;
181 struct openpgp_signedpacket_list *newelem = NULL;
183 for (curelem = *old; curelem != NULL; curelem = curelem->next) {
184 newelem = find_signed_packet(*new, curelem->packet);
185 if (newelem != NULL) {
186 merge_packet_sigs(curelem, newelem);
189 * If there are no sigs left on the new signed packet
190 * then remove it from the list.
192 if (newelem->sigs == NULL) {
193 remove_signed_packet(new,
201 * If *new != NULL now then there are UIDs on the new key that weren't
202 * on the old key. Add them.
204 for (curelem = *new; curelem != NULL;
205 curelem = curelem->next) {
206 ADD_PACKET_TO_LIST((*old_end),
207 packet_dup(curelem->packet));
211 packet_list_add(&(*old_end)->sigs,
212 &(*old_end)->last_sig,
220 * merge_keys - Takes 2 public keys and merges them.
221 * @a: The old key. The merged key is returned in this structure.
222 * @b: The new key. The changed from old to new keys are returned in this
225 * This function takes 2 keys and merges them. It then returns the merged
226 * key in a and the difference between this new key and the original a
227 * in b (ie newb contains the minimum amount of detail necessary to
228 * convert olda to newa). The intention is that olda is provided from
229 * internal storage and oldb from the remote user. newa is then stored in
230 * internal storage and newb is sent to all our keysync peers.
232 int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b)
234 int rc = 0; /* Return code */
235 struct openpgp_packet_list *curpacket = NULL;
236 struct openpgp_packet_list *lastpacket = NULL;
237 struct openpgp_packet_list *nextpacket = NULL;
239 if (a == NULL || b == NULL) {
244 } else if (get_keyid(a) != get_keyid(b)) {
246 * Key IDs are different.
251 * Key IDs are the same, so I guess we have to merge them.
253 curpacket = b->revocations;
254 while (curpacket != NULL) {
255 nextpacket = curpacket->next;
256 if (find_packet(a->revocations, curpacket->packet)) {
258 * We already have this revocation, remove it
259 * from the difference list and free the memory
263 if (lastpacket != NULL) {
264 lastpacket->next = curpacket->next;
266 assert(curpacket == b->revocations);
267 b->revocations = curpacket->next;
269 curpacket->next = NULL;
270 free_packet_list(curpacket);
273 lastpacket = curpacket;
275 curpacket = nextpacket;
277 b->last_revocation = lastpacket;
280 * Anything left on b->revocations doesn't exist on
281 * a->revocations, so add them to the list.
283 packet_list_add(&a->revocations,
288 * Merge uids (signed list).
289 * Merge subkeys (signed list).
291 merge_signed_packets(&a->uids, &a->last_uid,
292 &b->uids, &b->last_uid);
293 merge_signed_packets(&a->subkeys, &a->last_uid,
294 &b->subkeys, &b->last_subkey);
302 * update_keys - Takes a list of public keys and updates them in the DB.
303 * @keys: The keys to update in the DB.
305 * Takes a list of keys and adds them to the database, merging them with
306 * the key in the database if it's already present there. The key list is
307 * update to contain the minimum set of updates required to get from what
308 * we had before to what we have now (ie the set of data that was added to
309 * the DB). Returns the number of entirely new keys added.
311 int update_keys(struct openpgp_publickey **keys)
313 struct openpgp_publickey *curkey = NULL;
314 struct openpgp_publickey *oldkey = NULL;
315 struct openpgp_publickey *prev = NULL;
318 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
319 fetch_key(get_keyid(curkey), &oldkey);
322 * If we already have the key stored in the DB then merge it
323 * with the new one that's been supplied. Otherwise the key
324 * we've just got is the one that goes in the DB and also the
325 * one that we send out.
327 if (oldkey != NULL) {
328 merge_keys(oldkey, curkey);
329 if (curkey->revocations == NULL &&
330 curkey->uids == NULL &&
331 curkey->subkeys == NULL) {
333 *keys = curkey->next;
335 prev->next = curkey->next;
342 free_publickey(oldkey);