2 * parsekey.c - Routines to parse an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
15 #include "keystructs.h"
23 * parse_keys - Process a stream of packets for public keys + sigs.
24 * @packets: The packet list to parse.
25 * @keys: The returned list of public keys.
27 * This function takes an list of OpenPGP packets and attempts to parse it
28 * into a list of public keys with signatures and subkeys.
30 * Returns a count of how many keys we parsed.
32 int parse_keys(struct openpgp_packet_list *packets,
33 struct openpgp_publickey **keys)
35 struct openpgp_publickey *curkey = NULL;
41 * If keys already has some keys in it then set curkey to the last one
42 * so we add to the end of the list.
44 for (curkey = *keys; curkey != NULL && curkey->next != NULL;
45 curkey = curkey->next) ;
47 while (packets != NULL) {
48 switch (packets->packet->tag) {
49 case OPENPGP_PACKET_SIGNATURE:
51 * It's a signature packet. Add it to either the public
52 * key, to the current UID or the current subkey.
54 log_assert(curkey != NULL);
55 if (curkey->subkeys != NULL) {
56 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
58 packet_dup(packets->packet));
59 } else if (curkey->uids != NULL) {
60 ADD_PACKET_TO_LIST_END(curkey->last_uid,
62 packet_dup(packets->packet));
64 ADD_PACKET_TO_LIST_END(curkey,
66 packet_dup(packets->packet));
68 * This is a signature on the public key; check
69 * if it's a revocation.
71 if (packets->packet->data[0] == 3 &&
72 packets->packet->data[2] ==
73 OPENPGP_SIGTYPE_KEY_REV) {
75 * Type 3 key, 0x20 == revocation
77 curkey->revoked = true;
78 } else if (packets->packet->data[0] == 4 &&
79 packets->packet->data[1] ==
80 OPENPGP_SIGTYPE_KEY_REV) {
82 * Type 4 key, 0x20 == revocation
84 curkey->revoked = true;
88 case OPENPGP_PACKET_PUBLICKEY:
90 * It's a public key packet, so start a new key in our
94 curkey->next = malloc(sizeof (*curkey));
95 curkey = curkey->next;
98 malloc(sizeof (*curkey));
100 memset(curkey, 0, sizeof(*curkey));
101 curkey->publickey = packet_dup(packets->packet);
104 case OPENPGP_PACKET_UID:
105 case OPENPGP_PACKET_UAT:
107 * It's a UID packet (or a photo id, which is similar).
109 log_assert(curkey != NULL);
110 log_assert(curkey->subkeys == NULL);
111 ADD_PACKET_TO_LIST_END(curkey,
113 packet_dup(packets->packet));
115 case OPENPGP_PACKET_PUBLICSUBKEY:
117 * It's a subkey packet.
119 log_assert(curkey != NULL);
120 ADD_PACKET_TO_LIST_END(curkey,
122 packet_dup(packets->packet));
124 case OPENPGP_PACKET_TRUST:
129 * Trust packet. Ignore.
130 * Comment packet. Ignore.
134 logthing(LOGTHING_ERROR,
135 "Unsupported packet type: %d",
136 packets->packet->tag);
138 packets = packets->next;
145 * debug_packet - Print debug info about a packet
146 * @packet: The packet to display.
148 * This function takes an OpenPGP packet and displays some information
149 * about it to stdout. Useful for debugging purposes or curiousity about
150 * an OpenPGP packet stream.
152 int debug_packet(struct openpgp_packet *packet)
154 printf("\tNew format: %d, Tag: %u, Length: %zd\n",
163 * read_openpgp_stream - Reads a stream of OpenPGP packets.
164 * @getchar_func: The function to get the next character from the stream.
165 * @ctx: A pointer to the context structure for getchar_func.
166 * @packets: The outputted list of packets.
167 * @maxnum: The maximum number of keys to read. 0 means unlimited.
169 * This function uses getchar_func to read characters from an OpenPGP
170 * packet stream and reads the packets into a linked list of packets
171 * ready for parsing as a public key or whatever.
173 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
176 struct openpgp_packet_list **packets,
179 unsigned char curchar = 0;
180 struct openpgp_packet_list *curpacket = NULL;
183 bool inpacket = false;
185 log_assert(packets != NULL);
186 curpacket = *packets;
187 if (curpacket != NULL) {
188 while (curpacket->next != NULL) {
189 curpacket = curpacket->next;
193 while (!rc && (maxnum == 0 || keys < maxnum) &&
194 !getchar_func(ctx, 1, &curchar)) {
195 if (!inpacket && (curchar & 0x80)) {
197 * New packet. Record the fact we're in a packet and
198 * allocate memory for it.
201 if (curpacket != NULL) {
202 curpacket->next = malloc(sizeof (*curpacket));
203 curpacket = curpacket->next;
205 *packets = curpacket =
206 malloc(sizeof (*curpacket));
208 memset(curpacket, 0, sizeof(*curpacket));
210 malloc(sizeof (*curpacket->packet));
211 memset(curpacket->packet, 0,
212 sizeof(*curpacket->packet));
214 curpacket->packet->newformat = (curchar & 0x40);
217 * TODO: Better error checking on getchar_func.
219 if (curpacket->packet->newformat) {
220 curpacket->packet->tag = (curchar & 0x3F);
221 rc = getchar_func(ctx, 1, &curchar);
222 curpacket->packet->length = curchar;
223 if (curpacket->packet->length > 191 &&
224 curpacket->packet->length < 224) {
225 rc = getchar_func(ctx, 1, &curchar);
226 curpacket->packet->length -= 192;
227 curpacket->packet->length <<= 8;
228 curpacket->packet->length += curchar;
229 curpacket->packet->length += 192;
230 } else if (curpacket->packet->length > 223 &&
231 curpacket->packet->length < 255) {
232 logthing(LOGTHING_NOTICE,
235 } else if (curpacket->packet->length == 255) {
237 * 5 byte length; ie 255 followed by 3
238 * bytes of MSB length.
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;
248 curpacket->packet->length <<= 8;
249 rc = getchar_func(ctx, 1, &curchar);
250 curpacket->packet->length += curchar;
253 curpacket->packet->tag = (curchar & 0x3C) >> 2;
254 switch (curchar & 3) {
256 rc = getchar_func(ctx, 1, &curchar);
257 curpacket->packet->length = curchar;
260 rc = getchar_func(ctx, 1, &curchar);
261 curpacket->packet->length = curchar;
262 curpacket->packet->length <<= 8;
263 rc = getchar_func(ctx, 1, &curchar);
264 curpacket->packet->length += curchar;
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 +=
276 rc = getchar_func(ctx, 1, &curchar);
277 curpacket->packet->length += curchar;
280 logthing(LOGTHING_ERROR,
281 "Unsupported length type 3.");
282 curpacket->packet->length = 0;
283 curpacket->packet->data = NULL;
290 if (curpacket->packet->tag ==
291 OPENPGP_PACKET_PUBLICKEY) {
294 curpacket->packet->data =
295 malloc(curpacket->packet->length *
296 sizeof(unsigned char));
297 if (curpacket->packet->data == NULL) {
298 logthing(LOGTHING_ERROR,
299 "Can't allocate memory for "
303 rc = getchar_func(ctx,
304 curpacket->packet->length,
305 curpacket->packet->data);
310 logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
320 * write_openpgp_stream - Reads a stream of OpenPGP packets.
321 * @putchar_func: The function to put the next character to the stream.
322 * @ctx: A pointer to the context structure for putchar_func.
323 * @packets: The list of packets.
325 * This function uses putchar_func to write characters to an OpenPGP
326 * packet stream from a linked list of packets.
328 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
331 struct openpgp_packet_list *packets)
333 unsigned char curchar = 0;
335 while (packets != NULL) {
337 if (packets->packet->newformat) {
339 curchar |= packets->packet->tag;
340 putchar_func(ctx, 1, &curchar);
342 if (packets->packet->length < 192) {
343 curchar = packets->packet->length;
344 putchar_func(ctx, 1, &curchar);
345 } else if (packets->packet->length > 191 &&
346 packets->packet->length < 8383) {
347 curchar = (((packets->packet->length - 192) &
349 putchar_func(ctx, 1, &curchar);
351 curchar = (packets->packet->length - 192) &
353 putchar_func(ctx, 1, &curchar);
354 } else if (packets->packet->length > 8382 &&
355 packets->packet->length < 0xFFFFFFFF) {
356 logthing(LOGTHING_DEBUG,
357 "Writing 5 byte length");
359 putchar_func(ctx, 1, &curchar);
361 curchar = (packets->packet->length >> 24);
363 putchar_func(ctx, 1, &curchar);
365 curchar = (packets->packet->length >> 16);
367 putchar_func(ctx, 1, &curchar);
369 curchar = (packets->packet->length >> 8);
371 putchar_func(ctx, 1, &curchar);
373 curchar = packets->packet->length;
375 putchar_func(ctx, 1, &curchar);
377 logthing(LOGTHING_ERROR,
378 "Unsupported new format length.");
381 curchar |= (packets->packet->tag << 2);
382 if (packets->packet->length < 256) {
383 putchar_func(ctx, 1, &curchar);
384 curchar = packets->packet->length;
385 putchar_func(ctx, 1, &curchar);
386 } else if (packets->packet->length < 0x10000) {
388 putchar_func(ctx, 1, &curchar);
389 curchar = packets->packet->length >> 8;
390 putchar_func(ctx, 1, &curchar);
391 curchar = packets->packet->length & 0xFF;
392 putchar_func(ctx, 1, &curchar);
395 putchar_func(ctx, 1, &curchar);
396 curchar = packets->packet->length >> 24;
397 putchar_func(ctx, 1, &curchar);
398 curchar = (packets->packet->length >> 16) & 0xFF;
399 putchar_func(ctx, 1, &curchar);
400 curchar = (packets->packet->length >> 8) & 0xFF;
401 putchar_func(ctx, 1, &curchar);
402 curchar = packets->packet->length & 0xFF;
403 putchar_func(ctx, 1, &curchar);
407 putchar_func(ctx, packets->packet->length,
408 packets->packet->data);
409 packets = packets->next;
415 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
416 * @key: The public key.
417 * @packets: The outputted packet list.
419 * This function converts public key structure to a linked list of OpenPGP
420 * packets ready for outputing or storage.
422 int flatten_publickey(struct openpgp_publickey *key,
423 struct openpgp_packet_list **packets,
424 struct openpgp_packet_list **list_end)
426 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
427 struct openpgp_packet_list *tmplist = NULL;
429 while (key != NULL) {
431 * First write the public key packet out.
433 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
434 if (*packets == NULL) {
435 *packets = *list_end;
439 * Now do any signatures on the main key.
441 for (tmplist = key->sigs; tmplist != NULL;
442 tmplist = tmplist->next) {
443 ADD_PACKET_TO_LIST((*list_end),
444 packet_dup(tmplist->packet));
448 * Output any UIDs along with their signatures.
450 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
451 tmpsignedlist = tmpsignedlist->next) {
453 ADD_PACKET_TO_LIST((*list_end),
454 packet_dup(tmpsignedlist->packet));
455 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
456 tmplist = tmplist->next) {
457 ADD_PACKET_TO_LIST((*list_end),
458 packet_dup(tmplist->packet));
463 * Output any subkeys along with their signatures.
465 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
466 tmpsignedlist = tmpsignedlist->next) {
468 ADD_PACKET_TO_LIST((*list_end),
469 packet_dup(tmpsignedlist->packet));
470 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
471 tmplist = tmplist->next) {
472 ADD_PACKET_TO_LIST((*list_end),
473 packet_dup(tmplist->packet));