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 * 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) {
51 * It's a signature packet. Add it to either the public
52 * key (it should be a revocation), to the current UID
53 * or the current subkey.
55 assert(curkey != NULL);
56 if (curkey->subkeys != NULL) {
57 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
59 packet_dup(packets->packet));
60 } else if (curkey->uids != NULL) {
61 ADD_PACKET_TO_LIST_END(curkey->last_uid,
63 packet_dup(packets->packet));
65 ADD_PACKET_TO_LIST_END(curkey,
67 packet_dup(packets->packet));
72 * It's a public key packet, so start a new key in our
76 curkey->next = malloc(sizeof (*curkey));
77 curkey = curkey->next;
80 malloc(sizeof (*curkey));
82 memset(curkey, 0, sizeof(*curkey));
83 curkey->publickey = packet_dup(packets->packet);
89 * It's a UID packet (or a photo id, which is similar).
91 assert(curkey != NULL);
92 assert(curkey->subkeys == NULL);
93 ADD_PACKET_TO_LIST_END(curkey,
95 packet_dup(packets->packet));
99 * It's a subkey packet.
101 assert(curkey != NULL);
102 ADD_PACKET_TO_LIST_END(curkey,
104 packet_dup(packets->packet));
111 * Trust packet. Ignore.
112 * Comment packet. Ignore.
116 logthing(LOGTHING_ERROR,
117 "Unsupported packet type: %d",
118 packets->packet->tag);
120 packets = packets->next;
127 * debug_packet - Print debug info about a packet
128 * @packet: The packet to display.
130 * This function takes an OpenPGP packet and displays some information
131 * about it to stdout. Useful for debugging purposes or curiousity about
132 * an OpenPGP packet stream.
134 int debug_packet(struct openpgp_packet *packet)
136 printf("\tNew format: %d, Tag: %u, Length: %d\n",
145 * read_openpgp_stream - Reads a stream of OpenPGP packets.
146 * @getchar_func: The function to get the next character from the stream.
147 * @ctx: A pointer to the context structure for getchar_func.
148 * @packets: The outputted list of packets.
149 * @maxnum: The maximum number of keys to read. 0 means unlimited.
151 * This function uses getchar_func to read characters from an OpenPGP
152 * packet stream and reads the packets into a linked list of packets
153 * ready for parsing as a public key or whatever.
155 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
158 struct openpgp_packet_list **packets,
161 unsigned char curchar = 0;
162 struct openpgp_packet_list *curpacket = NULL;
165 bool inpacket = false;
167 assert(packets != NULL);
168 curpacket = *packets;
169 if (curpacket != NULL) {
170 while (curpacket->next != NULL) {
171 curpacket = curpacket->next;
175 while (!rc && (maxnum == 0 || keys < maxnum) &&
176 !getchar_func(ctx, 1, &curchar)) {
177 if (!inpacket && (curchar & 0x80)) {
179 * New packet. Record the fact we're in a packet and
180 * allocate memory for it.
183 if (curpacket != NULL) {
184 curpacket->next = malloc(sizeof (*curpacket));
185 curpacket = curpacket->next;
187 *packets = curpacket =
188 malloc(sizeof (*curpacket));
190 memset(curpacket, 0, sizeof(*curpacket));
192 malloc(sizeof (*curpacket->packet));
193 memset(curpacket->packet, 0,
194 sizeof(*curpacket->packet));
196 curpacket->packet->newformat = (curchar & 0x40);
199 * TODO: Better error checking on getchar_func.
201 if (curpacket->packet->newformat) {
202 curpacket->packet->tag = (curchar & 0x3F);
203 rc = getchar_func(ctx, 1, &curchar);
204 curpacket->packet->length = curchar;
205 if (curpacket->packet->length > 191 &&
206 curpacket->packet->length < 224) {
207 rc = getchar_func(ctx, 1, &curchar);
208 curpacket->packet->length -= 192;
209 curpacket->packet->length <<= 8;
210 curpacket->packet->length += curchar;
211 curpacket->packet->length += 192;
212 } else if (curpacket->packet->length > 223 &&
213 curpacket->packet->length < 255) {
214 logthing(LOGTHING_NOTICE,
217 } else if (curpacket->packet->length == 255) {
219 * 5 byte length; ie 255 followed by 3
220 * bytes of MSB length.
222 rc = getchar_func(ctx, 1, &curchar);
223 curpacket->packet->length = curchar;
224 curpacket->packet->length <<= 8;
225 rc = getchar_func(ctx, 1, &curchar);
226 curpacket->packet->length += curchar;
227 curpacket->packet->length <<= 8;
228 rc = getchar_func(ctx, 1, &curchar);
229 curpacket->packet->length += curchar;
230 curpacket->packet->length <<= 8;
231 rc = getchar_func(ctx, 1, &curchar);
232 curpacket->packet->length += curchar;
235 curpacket->packet->tag = (curchar & 0x3C) >> 2;
236 switch (curchar & 3) {
238 rc = getchar_func(ctx, 1, &curchar);
239 curpacket->packet->length = curchar;
242 rc = getchar_func(ctx, 1, &curchar);
243 curpacket->packet->length = curchar;
244 curpacket->packet->length <<= 8;
245 rc = getchar_func(ctx, 1, &curchar);
246 curpacket->packet->length += curchar;
249 rc = getchar_func(ctx, 1, &curchar);
250 curpacket->packet->length =
252 rc = getchar_func(ctx, 1, &curchar);
253 curpacket->packet->length +=
255 rc = getchar_func(ctx, 1, &curchar);
256 curpacket->packet->length +=
258 rc = getchar_func(ctx, 1, &curchar);
259 curpacket->packet->length += curchar;
262 logthing(LOGTHING_ERROR,
263 "Unsupported length type 3.");
264 curpacket->packet->length = 0;
265 curpacket->packet->data = NULL;
272 if (curpacket->packet->tag == 6) {
275 curpacket->packet->data =
276 malloc(curpacket->packet->length *
277 sizeof(unsigned char));
278 if (curpacket->packet->data == NULL) {
279 logthing(LOGTHING_ERROR,
280 "Can't allocate memory for "
284 rc = getchar_func(ctx,
285 curpacket->packet->length,
286 curpacket->packet->data);
291 logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
301 * write_openpgp_stream - Reads a stream of OpenPGP packets.
302 * @putchar_func: The function to put the next character to the stream.
303 * @ctx: A pointer to the context structure for putchar_func.
304 * @packets: The list of packets.
306 * This function uses putchar_func to write characters to an OpenPGP
307 * packet stream from a linked list of packets.
309 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
312 struct openpgp_packet_list *packets)
314 unsigned char curchar = 0;
316 while (packets != NULL) {
318 if (packets->packet->newformat) {
320 curchar |= packets->packet->tag;
321 putchar_func(ctx, 1, &curchar);
323 if (packets->packet->length < 192) {
324 curchar = packets->packet->length;
325 putchar_func(ctx, 1, &curchar);
326 } else if (packets->packet->length > 191 &&
327 packets->packet->length < 8383) {
328 curchar = (((packets->packet->length - 192) &
330 putchar_func(ctx, 1, &curchar);
332 curchar = (packets->packet->length - 192) &
334 putchar_func(ctx, 1, &curchar);
335 } else if (packets->packet->length > 8382 &&
336 packets->packet->length < 0xFFFFFFFF) {
337 logthing(LOGTHING_DEBUG,
338 "Writing 5 byte length");
340 putchar_func(ctx, 1, &curchar);
342 curchar = (packets->packet->length >> 24);
344 putchar_func(ctx, 1, &curchar);
346 curchar = (packets->packet->length >> 16);
348 putchar_func(ctx, 1, &curchar);
350 curchar = (packets->packet->length >> 8);
352 putchar_func(ctx, 1, &curchar);
354 curchar = packets->packet->length;
356 putchar_func(ctx, 1, &curchar);
358 logthing(LOGTHING_ERROR,
359 "Unsupported new format length.");
362 curchar |= (packets->packet->tag << 2);
363 if (packets->packet->length < 256) {
364 putchar_func(ctx, 1, &curchar);
365 curchar = packets->packet->length;
366 putchar_func(ctx, 1, &curchar);
367 } else if (packets->packet->length < 0x10000) {
369 putchar_func(ctx, 1, &curchar);
370 curchar = packets->packet->length >> 8;
371 putchar_func(ctx, 1, &curchar);
372 curchar = packets->packet->length & 0xFF;
373 putchar_func(ctx, 1, &curchar);
376 putchar_func(ctx, 1, &curchar);
377 curchar = packets->packet->length >> 24;
378 putchar_func(ctx, 1, &curchar);
379 curchar = (packets->packet->length >> 16) & 0xFF;
380 putchar_func(ctx, 1, &curchar);
381 curchar = (packets->packet->length >> 8) & 0xFF;
382 putchar_func(ctx, 1, &curchar);
383 curchar = packets->packet->length & 0xFF;
384 putchar_func(ctx, 1, &curchar);
388 putchar_func(ctx, packets->packet->length,
389 packets->packet->data);
390 packets = packets->next;
396 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
397 * @key: The public key.
398 * @packets: The outputted packet list.
400 * This function converts public key structure to a linked list of OpenPGP
401 * packets ready for outputing or storage.
403 int flatten_publickey(struct openpgp_publickey *key,
404 struct openpgp_packet_list **packets,
405 struct openpgp_packet_list **list_end)
407 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
408 struct openpgp_packet_list *tmplist = NULL;
410 while (key != NULL) {
412 * First write the public key packet out.
414 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
415 if (*packets == NULL) {
416 *packets = *list_end;
420 * Now do any revocation signatures on the main key.
422 for (tmplist = key->revocations; tmplist != NULL;
423 tmplist = tmplist->next) {
424 ADD_PACKET_TO_LIST((*list_end),
425 packet_dup(tmplist->packet));
429 * Output any UIDs along with their signatures.
431 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
432 tmpsignedlist = tmpsignedlist->next) {
434 ADD_PACKET_TO_LIST((*list_end),
435 packet_dup(tmpsignedlist->packet));
436 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
437 tmplist = tmplist->next) {
438 ADD_PACKET_TO_LIST((*list_end),
439 packet_dup(tmplist->packet));
444 * Output any subkeys along with their signatures.
446 for (tmpsignedlist = key->subkeys; 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));