2 * parsekey.c - Routines to parse an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
16 #include "keystructs.h"
23 * add_key - Takes a key and adds it to the keyserver.
24 * @key: The public key to add.
26 * This function takes a public key and adds it to the keyserver.
27 * It first of all sees if we already have the key locally. If we do then
28 * we retrieve it and merge the two keys. We then store the resulting key
29 * (or just the original we received if we don't already have it). We then
30 * send out the appropriate updates to our keyserver peers.
32 int add_key(struct openpgp_publickey *key) {
37 * parse_keys - Process a stream of packets for public keys + sigs.
38 * @packets: The packet list to parse.
39 * @keys: The returned list of public keys.
41 * This function takes an list of OpenPGP packets and attempts to parse it
42 * into a list of public keys with signatures and subkeys.
44 * Returns a count of how many keys we parsed.
46 int parse_keys(struct openpgp_packet_list *packets,
47 struct openpgp_publickey **keys)
49 struct openpgp_publickey *curkey = NULL;
55 * If keys already has some keys in it then set curkey to the last one
56 * so we add to the end of the list.
58 for (curkey = *keys; curkey != NULL && curkey->next != NULL;
59 curkey = curkey->next) ;
61 while (packets != NULL) {
62 switch (packets->packet->tag) {
65 * It's a signature packet. Add it to either the public
66 * key (it should be a revocation), to the current UID
67 * or the current subkey.
69 assert(curkey != NULL);
70 if (curkey->subkeys != NULL) {
71 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
73 packet_dup(packets->packet));
74 } else if (curkey->uids != NULL) {
75 ADD_PACKET_TO_LIST_END(curkey->last_uid,
77 packet_dup(packets->packet));
79 ADD_PACKET_TO_LIST_END(curkey,
81 packet_dup(packets->packet));
86 * It's a public key packet, so start a new key in our
90 curkey->next = malloc(sizeof (*curkey));
91 curkey = curkey->next;
94 malloc(sizeof (*curkey));
96 memset(curkey, 0, sizeof(*curkey));
97 curkey->publickey = packet_dup(packets->packet);
103 * It's a UID packet (or a photo id, which is similar).
105 assert(curkey != NULL);
106 assert(curkey->subkeys == NULL);
107 ADD_PACKET_TO_LIST_END(curkey,
109 packet_dup(packets->packet));
113 * It's a subkey packet.
115 assert(curkey != NULL);
116 ADD_PACKET_TO_LIST_END(curkey,
118 packet_dup(packets->packet));
125 * Trust packet. Ignore.
126 * Comment packet. Ignore.
130 logthing(LOGTHING_ERROR,
131 "Unsupported packet type: %d",
132 packets->packet->tag);
134 packets = packets->next;
141 * debug_packet - Print debug info about a packet
142 * @packet: The packet to display.
144 * This function takes an OpenPGP packet and displays some information
145 * about it to stdout. Useful for debugging purposes or curiousity about
146 * an OpenPGP packet stream.
148 int debug_packet(struct openpgp_packet *packet)
150 printf("\tNew format: %d, Tag: %d, Length: %d\n",
159 * read_openpgp_stream - Reads a stream of OpenPGP packets.
160 * @getchar_func: The function to get the next character from the stream.
161 * @ctx: A pointer to the context structure for getchar_func.
162 * @packets: The outputted list of packets.
164 * This function uses getchar_func to read characters from an OpenPGP
165 * packet stream and reads the packets into a linked list of packets
166 * ready for parsing as a public key or whatever.
168 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
171 struct openpgp_packet_list **packets)
173 unsigned char curchar = 0;
174 unsigned long count = 0;
175 struct openpgp_packet_list *curpacket = NULL;
177 bool inpacket = false;
179 assert(packets != NULL);
181 while (!rc && !getchar_func(ctx, 1, &curchar)) {
182 if (!inpacket && (curchar & 0x80)) {
184 * New packet. Record the fact we're in a packet and
185 * allocate memory for it.
189 if (curpacket != NULL) {
190 curpacket->next = malloc(sizeof (*curpacket));
191 curpacket = curpacket->next;
193 *packets = curpacket =
194 malloc(sizeof (*curpacket));
196 memset(curpacket, 0, sizeof(*curpacket));
198 malloc(sizeof (*curpacket->packet));
199 memset(curpacket->packet, 0,
200 sizeof(*curpacket->packet));
202 curpacket->packet->newformat = (curchar & 0x40);
204 // TODO: Better error checking on getchar_func.
205 if (curpacket->packet->newformat) {
206 curpacket->packet->tag = (curchar & 0x3F);
207 rc = getchar_func(ctx, 1, &curchar);
208 curpacket->packet->length = curchar;
209 if (curpacket->packet->length > 191 &&
210 curpacket->packet->length < 224) {
211 rc = getchar_func(ctx, 1, &curchar);
212 curpacket->packet->length -= 192;
213 curpacket->packet->length <<= 8;
214 curpacket->packet->length += curchar;
215 curpacket->packet->length += 192;
216 } else if (curpacket->packet->length > 223 &&
217 curpacket->packet->length < 255) {
218 printf("Partial length; not supported.\n");
219 } else if (curpacket->packet->length == 255) {
221 * 5 byte length; ie 255 followed by 3
222 * bytes of MSB length.
224 rc = getchar_func(ctx, 1, &curchar);
225 curpacket->packet->length = curchar;
226 curpacket->packet->length <<= 8;
227 rc = getchar_func(ctx, 1, &curchar);
228 curpacket->packet->length = curchar;
229 curpacket->packet->length <<= 8;
230 rc = getchar_func(ctx, 1, &curchar);
231 curpacket->packet->length = curchar;
232 curpacket->packet->length <<= 8;
233 rc = getchar_func(ctx, 1, &curchar);
234 curpacket->packet->length = curchar;
237 curpacket->packet->tag = (curchar & 0x3C) >> 2;
238 switch (curchar & 3) {
240 rc = getchar_func(ctx, 1, &curchar);
241 curpacket->packet->length = curchar;
244 rc = getchar_func(ctx, 1, &curchar);
245 curpacket->packet->length = curchar;
246 curpacket->packet->length <<= 8;
247 rc = getchar_func(ctx, 1, &curchar);
248 curpacket->packet->length += curchar;
251 rc = getchar_func(ctx, 1, &curchar);
252 curpacket->packet->length =
254 rc = getchar_func(ctx, 1, &curchar);
255 curpacket->packet->length +=
257 rc = getchar_func(ctx, 1, &curchar);
258 curpacket->packet->length +=
260 rc = getchar_func(ctx, 1, &curchar);
261 curpacket->packet->length += curchar;
264 logthing(LOGTHING_ERROR,
265 "Unsupported length type 3.");
266 curpacket->packet->length = 0;
267 curpacket->packet->data = NULL;
274 curpacket->packet->data =
275 malloc(curpacket->packet->length *
276 sizeof(unsigned char));
277 rc = getchar_func(ctx,
278 curpacket->packet->length,
279 curpacket->packet->data);
283 logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
292 * write_openpgp_stream - Reads a stream of OpenPGP packets.
293 * @putchar_func: The function to put the next character to the stream.
294 * @ctx: A pointer to the context structure for putchar_func.
295 * @packets: The list of packets.
297 * This function uses putchar_func to write characters to an OpenPGP
298 * packet stream from a linked list of packets.
300 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
303 struct openpgp_packet_list *packets)
305 unsigned char curchar = 0;
307 while (packets != NULL) {
309 if (packets->packet->newformat) {
311 curchar |= packets->packet->tag;
312 putchar_func(ctx, 1, &curchar);
314 if (packets->packet->length < 192) {
315 curchar = packets->packet->length;
316 putchar_func(ctx, 1, &curchar);
317 } else if (packets->packet->length > 191 &&
318 packets->packet->length < 8383) {
319 curchar = (((packets->packet->length - 192) &
321 putchar_func(ctx, 1, &curchar);
323 curchar = (packets->packet->length - 192) &
325 putchar_func(ctx, 1, &curchar);
327 logthing(LOGTHING_ERROR,
328 "Unsupported new format length.");
331 curchar |= (packets->packet->tag << 2);
332 if (packets->packet->length < 256) {
333 putchar_func(ctx, 1, &curchar);
334 curchar = packets->packet->length;
335 putchar_func(ctx, 1, &curchar);
336 } else if (packets->packet->length < 0x10000) {
338 putchar_func(ctx, 1, &curchar);
339 curchar = packets->packet->length >> 8;
340 putchar_func(ctx, 1, &curchar);
341 curchar = packets->packet->length & 0xFF;
342 putchar_func(ctx, 1, &curchar);
345 putchar_func(ctx, 1, &curchar);
346 curchar = packets->packet->length >> 24;
347 putchar_func(ctx, 1, &curchar);
348 curchar = (packets->packet->length >> 16) & 0xFF;
349 putchar_func(ctx, 1, &curchar);
350 curchar = (packets->packet->length >> 8) & 0xFF;
351 putchar_func(ctx, 1, &curchar);
352 curchar = packets->packet->length & 0xFF;
353 putchar_func(ctx, 1, &curchar);
357 putchar_func(ctx, packets->packet->length, packets->packet->data);
358 // for (i = 0; i < packets->packet->length; i++) {
359 // putchar_func(ctx, packets->packet->data[i]);
361 packets = packets->next;
367 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
368 * @key: The public key.
369 * @packets: The outputted packet list.
371 * This function converts public key structure to a linked list of OpenPGP
372 * packets ready for outputing or storage.
374 int flatten_publickey(struct openpgp_publickey *key,
375 struct openpgp_packet_list **packets,
376 struct openpgp_packet_list **list_end)
378 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
379 struct openpgp_packet_list *tmplist = NULL;
381 while (key != NULL) {
383 * First write the public key packet out.
385 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
386 if (*packets == NULL) {
387 *packets = *list_end;
391 * Now do any revocation signatures on the main key.
393 for (tmplist = key->revocations; tmplist != NULL;
394 tmplist = tmplist->next) {
395 ADD_PACKET_TO_LIST((*list_end),
396 packet_dup(tmplist->packet));
400 * Output any UIDs along with their signatures.
402 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
403 tmpsignedlist = tmpsignedlist->next) {
405 ADD_PACKET_TO_LIST((*list_end),
406 packet_dup(tmpsignedlist->packet));
407 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
408 tmplist = tmplist->next) {
409 ADD_PACKET_TO_LIST((*list_end),
410 packet_dup(tmplist->packet));
415 * Output any subkeys along with their signatures.
417 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
418 tmpsignedlist = tmpsignedlist->next) {
420 ADD_PACKET_TO_LIST((*list_end),
421 packet_dup(tmpsignedlist->packet));
422 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
423 tmplist = tmplist->next) {
424 ADD_PACKET_TO_LIST((*list_end),
425 packet_dup(tmplist->packet));