2 * parsekey.c - Routines to parse an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
15 #include "keystructs.h"
22 * parse_keys - Process a stream of packets for public keys + sigs.
23 * @packets: The packet list to parse.
24 * @keys: The returned list of public keys.
26 * This function takes an list of OpenPGP packets and attempts to parse it
27 * into a list of public keys with signatures and subkeys.
29 * Returns a count of how many keys we parsed.
31 int parse_keys(struct openpgp_packet_list *packets,
32 struct openpgp_publickey **keys)
34 struct openpgp_publickey *curkey = NULL;
40 * If keys already has some keys in it then set curkey to the last one
41 * so we add to the end of the list.
43 for (curkey = *keys; curkey != NULL && curkey->next != NULL;
44 curkey = curkey->next) ;
46 while (packets != NULL) {
47 switch (packets->packet->tag) {
50 * It's a signature packet. Add it to either the public
51 * key, to the current UID or the current subkey.
53 log_assert(curkey != NULL);
54 if (curkey->subkeys != NULL) {
55 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
57 packet_dup(packets->packet));
58 } else if (curkey->uids != NULL) {
59 ADD_PACKET_TO_LIST_END(curkey->last_uid,
61 packet_dup(packets->packet));
63 ADD_PACKET_TO_LIST_END(curkey,
65 packet_dup(packets->packet));
67 * This is a signature on the public key; check
68 * if it's a revocation.
70 if (packets->packet->data[0] == 3 &&
71 packets->packet->data[2] == 0x20) {
73 * Type 3 key, 0x20 == revocation
75 curkey->revoked = true;
76 } else if (packets->packet->data[0] == 4 &&
77 packets->packet->data[1] == 0x20) {
79 * Type 4 key, 0x20 == revocation
81 curkey->revoked = true;
87 * It's a public key packet, so start a new key in our
91 curkey->next = malloc(sizeof (*curkey));
92 curkey = curkey->next;
95 malloc(sizeof (*curkey));
97 memset(curkey, 0, sizeof(*curkey));
98 curkey->publickey = packet_dup(packets->packet);
104 * It's a UID packet (or a photo id, which is similar).
106 log_assert(curkey != NULL);
107 log_assert(curkey->subkeys == NULL);
108 ADD_PACKET_TO_LIST_END(curkey,
110 packet_dup(packets->packet));
114 * It's a subkey packet.
116 log_assert(curkey != NULL);
117 ADD_PACKET_TO_LIST_END(curkey,
119 packet_dup(packets->packet));
126 * Trust packet. Ignore.
127 * Comment packet. Ignore.
131 logthing(LOGTHING_ERROR,
132 "Unsupported packet type: %d",
133 packets->packet->tag);
135 packets = packets->next;
142 * debug_packet - Print debug info about a packet
143 * @packet: The packet to display.
145 * This function takes an OpenPGP packet and displays some information
146 * about it to stdout. Useful for debugging purposes or curiousity about
147 * an OpenPGP packet stream.
149 int debug_packet(struct openpgp_packet *packet)
151 printf("\tNew format: %d, Tag: %u, Length: %d\n",
160 * read_openpgp_stream - Reads a stream of OpenPGP packets.
161 * @getchar_func: The function to get the next character from the stream.
162 * @ctx: A pointer to the context structure for getchar_func.
163 * @packets: The outputted list of packets.
164 * @maxnum: The maximum number of keys to read. 0 means unlimited.
166 * This function uses getchar_func to read characters from an OpenPGP
167 * packet stream and reads the packets into a linked list of packets
168 * ready for parsing as a public key or whatever.
170 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
173 struct openpgp_packet_list **packets,
176 unsigned char curchar = 0;
177 struct openpgp_packet_list *curpacket = NULL;
180 bool inpacket = false;
182 log_assert(packets != NULL);
183 curpacket = *packets;
184 if (curpacket != NULL) {
185 while (curpacket->next != NULL) {
186 curpacket = curpacket->next;
190 while (!rc && (maxnum == 0 || keys < maxnum) &&
191 !getchar_func(ctx, 1, &curchar)) {
192 if (!inpacket && (curchar & 0x80)) {
194 * New packet. Record the fact we're in a packet and
195 * allocate memory for it.
198 if (curpacket != NULL) {
199 curpacket->next = malloc(sizeof (*curpacket));
200 curpacket = curpacket->next;
202 *packets = curpacket =
203 malloc(sizeof (*curpacket));
205 memset(curpacket, 0, sizeof(*curpacket));
207 malloc(sizeof (*curpacket->packet));
208 memset(curpacket->packet, 0,
209 sizeof(*curpacket->packet));
211 curpacket->packet->newformat = (curchar & 0x40);
214 * TODO: Better error checking on getchar_func.
216 if (curpacket->packet->newformat) {
217 curpacket->packet->tag = (curchar & 0x3F);
218 rc = getchar_func(ctx, 1, &curchar);
219 curpacket->packet->length = curchar;
220 if (curpacket->packet->length > 191 &&
221 curpacket->packet->length < 224) {
222 rc = getchar_func(ctx, 1, &curchar);
223 curpacket->packet->length -= 192;
224 curpacket->packet->length <<= 8;
225 curpacket->packet->length += curchar;
226 curpacket->packet->length += 192;
227 } else if (curpacket->packet->length > 223 &&
228 curpacket->packet->length < 255) {
229 logthing(LOGTHING_NOTICE,
232 } else if (curpacket->packet->length == 255) {
234 * 5 byte length; ie 255 followed by 3
235 * bytes of MSB length.
237 rc = getchar_func(ctx, 1, &curchar);
238 curpacket->packet->length = curchar;
239 curpacket->packet->length <<= 8;
240 rc = getchar_func(ctx, 1, &curchar);
241 curpacket->packet->length += curchar;
242 curpacket->packet->length <<= 8;
243 rc = getchar_func(ctx, 1, &curchar);
244 curpacket->packet->length += curchar;
245 curpacket->packet->length <<= 8;
246 rc = getchar_func(ctx, 1, &curchar);
247 curpacket->packet->length += curchar;
250 curpacket->packet->tag = (curchar & 0x3C) >> 2;
251 switch (curchar & 3) {
253 rc = getchar_func(ctx, 1, &curchar);
254 curpacket->packet->length = curchar;
257 rc = getchar_func(ctx, 1, &curchar);
258 curpacket->packet->length = curchar;
259 curpacket->packet->length <<= 8;
260 rc = getchar_func(ctx, 1, &curchar);
261 curpacket->packet->length += curchar;
264 rc = getchar_func(ctx, 1, &curchar);
265 curpacket->packet->length =
267 rc = getchar_func(ctx, 1, &curchar);
268 curpacket->packet->length +=
270 rc = getchar_func(ctx, 1, &curchar);
271 curpacket->packet->length +=
273 rc = getchar_func(ctx, 1, &curchar);
274 curpacket->packet->length += curchar;
277 logthing(LOGTHING_ERROR,
278 "Unsupported length type 3.");
279 curpacket->packet->length = 0;
280 curpacket->packet->data = NULL;
287 if (curpacket->packet->tag == 6) {
290 curpacket->packet->data =
291 malloc(curpacket->packet->length *
292 sizeof(unsigned char));
293 if (curpacket->packet->data == NULL) {
294 logthing(LOGTHING_ERROR,
295 "Can't allocate memory for "
299 rc = getchar_func(ctx,
300 curpacket->packet->length,
301 curpacket->packet->data);
306 logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
316 * write_openpgp_stream - Reads a stream of OpenPGP packets.
317 * @putchar_func: The function to put the next character to the stream.
318 * @ctx: A pointer to the context structure for putchar_func.
319 * @packets: The list of packets.
321 * This function uses putchar_func to write characters to an OpenPGP
322 * packet stream from a linked list of packets.
324 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
327 struct openpgp_packet_list *packets)
329 unsigned char curchar = 0;
331 while (packets != NULL) {
333 if (packets->packet->newformat) {
335 curchar |= packets->packet->tag;
336 putchar_func(ctx, 1, &curchar);
338 if (packets->packet->length < 192) {
339 curchar = packets->packet->length;
340 putchar_func(ctx, 1, &curchar);
341 } else if (packets->packet->length > 191 &&
342 packets->packet->length < 8383) {
343 curchar = (((packets->packet->length - 192) &
345 putchar_func(ctx, 1, &curchar);
347 curchar = (packets->packet->length - 192) &
349 putchar_func(ctx, 1, &curchar);
350 } else if (packets->packet->length > 8382 &&
351 packets->packet->length < 0xFFFFFFFF) {
352 logthing(LOGTHING_DEBUG,
353 "Writing 5 byte length");
355 putchar_func(ctx, 1, &curchar);
357 curchar = (packets->packet->length >> 24);
359 putchar_func(ctx, 1, &curchar);
361 curchar = (packets->packet->length >> 16);
363 putchar_func(ctx, 1, &curchar);
365 curchar = (packets->packet->length >> 8);
367 putchar_func(ctx, 1, &curchar);
369 curchar = packets->packet->length;
371 putchar_func(ctx, 1, &curchar);
373 logthing(LOGTHING_ERROR,
374 "Unsupported new format length.");
377 curchar |= (packets->packet->tag << 2);
378 if (packets->packet->length < 256) {
379 putchar_func(ctx, 1, &curchar);
380 curchar = packets->packet->length;
381 putchar_func(ctx, 1, &curchar);
382 } else if (packets->packet->length < 0x10000) {
384 putchar_func(ctx, 1, &curchar);
385 curchar = packets->packet->length >> 8;
386 putchar_func(ctx, 1, &curchar);
387 curchar = packets->packet->length & 0xFF;
388 putchar_func(ctx, 1, &curchar);
391 putchar_func(ctx, 1, &curchar);
392 curchar = packets->packet->length >> 24;
393 putchar_func(ctx, 1, &curchar);
394 curchar = (packets->packet->length >> 16) & 0xFF;
395 putchar_func(ctx, 1, &curchar);
396 curchar = (packets->packet->length >> 8) & 0xFF;
397 putchar_func(ctx, 1, &curchar);
398 curchar = packets->packet->length & 0xFF;
399 putchar_func(ctx, 1, &curchar);
403 putchar_func(ctx, packets->packet->length,
404 packets->packet->data);
405 packets = packets->next;
411 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
412 * @key: The public key.
413 * @packets: The outputted packet list.
415 * This function converts public key structure to a linked list of OpenPGP
416 * packets ready for outputing or storage.
418 int flatten_publickey(struct openpgp_publickey *key,
419 struct openpgp_packet_list **packets,
420 struct openpgp_packet_list **list_end)
422 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
423 struct openpgp_packet_list *tmplist = NULL;
425 while (key != NULL) {
427 * First write the public key packet out.
429 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
430 if (*packets == NULL) {
431 *packets = *list_end;
435 * Now do any signatures on the main key.
437 for (tmplist = key->sigs; tmplist != NULL;
438 tmplist = tmplist->next) {
439 ADD_PACKET_TO_LIST((*list_end),
440 packet_dup(tmplist->packet));
444 * Output any UIDs along with their signatures.
446 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
447 tmpsignedlist = tmpsignedlist->next) {
449 ADD_PACKET_TO_LIST((*list_end),
450 packet_dup(tmpsignedlist->packet));
451 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
452 tmplist = tmplist->next) {
453 ADD_PACKET_TO_LIST((*list_end),
454 packet_dup(tmplist->packet));
459 * Output any subkeys along with their signatures.
461 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
462 tmpsignedlist = tmpsignedlist->next) {
464 ADD_PACKET_TO_LIST((*list_end),
465 packet_dup(tmpsignedlist->packet));
466 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
467 tmplist = tmplist->next) {
468 ADD_PACKET_TO_LIST((*list_end),
469 packet_dup(tmplist->packet));