2 * parsekey.c - Routines to parse an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
8 * $Id: parsekey.c,v 1.17 2003/10/11 21:52:18 noodles Exp $
18 #include "keystructs.h"
25 * parse_keys - Process a stream of packets for public keys + sigs.
26 * @packets: The packet list to parse.
27 * @keys: The returned list of public keys.
29 * This function takes an list of OpenPGP packets and attempts to parse it
30 * into a list of public keys with signatures and subkeys.
32 * Returns a count of how many keys we parsed.
34 int parse_keys(struct openpgp_packet_list *packets,
35 struct openpgp_publickey **keys)
37 struct openpgp_publickey *curkey = NULL;
43 * If keys already has some keys in it then set curkey to the last one
44 * so we add to the end of the list.
46 for (curkey = *keys; curkey != NULL && curkey->next != NULL;
47 curkey = curkey->next) ;
49 while (packets != NULL) {
50 switch (packets->packet->tag) {
53 * It's a signature packet. Add it to either the public
54 * key (it should be a revocation), to the current UID
55 * or the current subkey.
57 assert(curkey != NULL);
58 if (curkey->subkeys != NULL) {
59 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
61 packet_dup(packets->packet));
62 } else if (curkey->uids != NULL) {
63 ADD_PACKET_TO_LIST_END(curkey->last_uid,
65 packet_dup(packets->packet));
67 ADD_PACKET_TO_LIST_END(curkey,
69 packet_dup(packets->packet));
74 * It's a public key packet, so start a new key in our
78 curkey->next = malloc(sizeof (*curkey));
79 curkey = curkey->next;
82 malloc(sizeof (*curkey));
84 memset(curkey, 0, sizeof(*curkey));
85 curkey->publickey = packet_dup(packets->packet);
91 * It's a UID packet (or a photo id, which is similar).
93 assert(curkey != NULL);
94 assert(curkey->subkeys == NULL);
95 ADD_PACKET_TO_LIST_END(curkey,
97 packet_dup(packets->packet));
101 * It's a subkey packet.
103 assert(curkey != NULL);
104 ADD_PACKET_TO_LIST_END(curkey,
106 packet_dup(packets->packet));
113 * Trust packet. Ignore.
114 * Comment packet. Ignore.
118 logthing(LOGTHING_ERROR,
119 "Unsupported packet type: %d",
120 packets->packet->tag);
122 packets = packets->next;
129 * debug_packet - Print debug info about a packet
130 * @packet: The packet to display.
132 * This function takes an OpenPGP packet and displays some information
133 * about it to stdout. Useful for debugging purposes or curiousity about
134 * an OpenPGP packet stream.
136 int debug_packet(struct openpgp_packet *packet)
138 printf("\tNew format: %d, Tag: %u, Length: %d\n",
147 * read_openpgp_stream - Reads a stream of OpenPGP packets.
148 * @getchar_func: The function to get the next character from the stream.
149 * @ctx: A pointer to the context structure for getchar_func.
150 * @packets: The outputted list of packets.
151 * @maxnum: The maximum number of keys to read. 0 means unlimited.
153 * This function uses getchar_func to read characters from an OpenPGP
154 * packet stream and reads the packets into a linked list of packets
155 * ready for parsing as a public key or whatever.
157 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
160 struct openpgp_packet_list **packets,
163 unsigned char curchar = 0;
164 struct openpgp_packet_list *curpacket = NULL;
167 bool inpacket = false;
169 assert(packets != NULL);
170 curpacket = *packets;
171 if (curpacket != NULL) {
172 while (curpacket->next != NULL) {
173 curpacket = curpacket->next;
177 while (!rc && (maxnum == 0 || keys < maxnum) &&
178 !getchar_func(ctx, 1, &curchar)) {
179 if (!inpacket && (curchar & 0x80)) {
181 * New packet. Record the fact we're in a packet and
182 * allocate memory for it.
185 if (curpacket != NULL) {
186 curpacket->next = malloc(sizeof (*curpacket));
187 curpacket = curpacket->next;
189 *packets = curpacket =
190 malloc(sizeof (*curpacket));
192 memset(curpacket, 0, sizeof(*curpacket));
194 malloc(sizeof (*curpacket->packet));
195 memset(curpacket->packet, 0,
196 sizeof(*curpacket->packet));
198 curpacket->packet->newformat = (curchar & 0x40);
201 * TODO: Better error checking on getchar_func.
203 if (curpacket->packet->newformat) {
204 curpacket->packet->tag = (curchar & 0x3F);
205 rc = getchar_func(ctx, 1, &curchar);
206 curpacket->packet->length = curchar;
207 if (curpacket->packet->length > 191 &&
208 curpacket->packet->length < 224) {
209 rc = getchar_func(ctx, 1, &curchar);
210 curpacket->packet->length -= 192;
211 curpacket->packet->length <<= 8;
212 curpacket->packet->length += curchar;
213 curpacket->packet->length += 192;
214 } else if (curpacket->packet->length > 223 &&
215 curpacket->packet->length < 255) {
216 logthing(LOGTHING_NOTICE,
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 if (curpacket->packet->tag == 6) {
277 curpacket->packet->data =
278 malloc(curpacket->packet->length *
279 sizeof(unsigned char));
280 if (curpacket->packet->data == NULL) {
281 logthing(LOGTHING_ERROR,
282 "Can't allocate memory for "
286 rc = getchar_func(ctx,
287 curpacket->packet->length,
288 curpacket->packet->data);
293 logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
302 * write_openpgp_stream - Reads a stream of OpenPGP packets.
303 * @putchar_func: The function to put the next character to the stream.
304 * @ctx: A pointer to the context structure for putchar_func.
305 * @packets: The list of packets.
307 * This function uses putchar_func to write characters to an OpenPGP
308 * packet stream from a linked list of packets.
310 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
313 struct openpgp_packet_list *packets)
315 unsigned char curchar = 0;
317 while (packets != NULL) {
319 if (packets->packet->newformat) {
321 curchar |= packets->packet->tag;
322 putchar_func(ctx, 1, &curchar);
324 if (packets->packet->length < 192) {
325 curchar = packets->packet->length;
326 putchar_func(ctx, 1, &curchar);
327 } else if (packets->packet->length > 191 &&
328 packets->packet->length < 8383) {
329 curchar = (((packets->packet->length - 192) &
331 putchar_func(ctx, 1, &curchar);
333 curchar = (packets->packet->length - 192) &
335 putchar_func(ctx, 1, &curchar);
337 logthing(LOGTHING_ERROR,
338 "Unsupported new format length.");
341 curchar |= (packets->packet->tag << 2);
342 if (packets->packet->length < 256) {
343 putchar_func(ctx, 1, &curchar);
344 curchar = packets->packet->length;
345 putchar_func(ctx, 1, &curchar);
346 } else if (packets->packet->length < 0x10000) {
348 putchar_func(ctx, 1, &curchar);
349 curchar = packets->packet->length >> 8;
350 putchar_func(ctx, 1, &curchar);
351 curchar = packets->packet->length & 0xFF;
352 putchar_func(ctx, 1, &curchar);
355 putchar_func(ctx, 1, &curchar);
356 curchar = packets->packet->length >> 24;
357 putchar_func(ctx, 1, &curchar);
358 curchar = (packets->packet->length >> 16) & 0xFF;
359 putchar_func(ctx, 1, &curchar);
360 curchar = (packets->packet->length >> 8) & 0xFF;
361 putchar_func(ctx, 1, &curchar);
362 curchar = packets->packet->length & 0xFF;
363 putchar_func(ctx, 1, &curchar);
367 putchar_func(ctx, packets->packet->length,
368 packets->packet->data);
369 packets = packets->next;
375 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
376 * @key: The public key.
377 * @packets: The outputted packet list.
379 * This function converts public key structure to a linked list of OpenPGP
380 * packets ready for outputing or storage.
382 int flatten_publickey(struct openpgp_publickey *key,
383 struct openpgp_packet_list **packets,
384 struct openpgp_packet_list **list_end)
386 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
387 struct openpgp_packet_list *tmplist = NULL;
389 while (key != NULL) {
391 * First write the public key packet out.
393 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
394 if (*packets == NULL) {
395 *packets = *list_end;
399 * Now do any revocation signatures on the main key.
401 for (tmplist = key->revocations; tmplist != NULL;
402 tmplist = tmplist->next) {
403 ADD_PACKET_TO_LIST((*list_end),
404 packet_dup(tmplist->packet));
408 * Output any UIDs along with their signatures.
410 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
411 tmpsignedlist = tmpsignedlist->next) {
413 ADD_PACKET_TO_LIST((*list_end),
414 packet_dup(tmpsignedlist->packet));
415 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
416 tmplist = tmplist->next) {
417 ADD_PACKET_TO_LIST((*list_end),
418 packet_dup(tmplist->packet));
423 * Output any subkeys along with their signatures.
425 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
426 tmpsignedlist = tmpsignedlist->next) {
428 ADD_PACKET_TO_LIST((*list_end),
429 packet_dup(tmpsignedlist->packet));
430 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
431 tmplist = tmplist->next) {
432 ADD_PACKET_TO_LIST((*list_end),
433 packet_dup(tmplist->packet));