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.10 2003/09/29 07:35:26 noodles Exp $
18 #include "keystructs.h"
25 * add_key - Takes a key and adds it to the keyserver.
26 * @key: The public key to add.
28 * This function takes a public key and adds it to the keyserver.
29 * It first of all sees if we already have the key locally. If we do then
30 * we retrieve it and merge the two keys. We then store the resulting key
31 * (or just the original we received if we don't already have it). We then
32 * send out the appropriate updates to our keyserver peers.
34 int add_key(struct openpgp_publickey *key) {
39 * parse_keys - Process a stream of packets for public keys + sigs.
40 * @packets: The packet list to parse.
41 * @keys: The returned list of public keys.
43 * This function takes an list of OpenPGP packets and attempts to parse it
44 * into a list of public keys with signatures and subkeys.
46 * Returns a count of how many keys we parsed.
48 int parse_keys(struct openpgp_packet_list *packets,
49 struct openpgp_publickey **keys)
51 struct openpgp_publickey *curkey = NULL;
57 * If keys already has some keys in it then set curkey to the last one
58 * so we add to the end of the list.
60 for (curkey = *keys; curkey != NULL && curkey->next != NULL;
61 curkey = curkey->next) ;
63 while (packets != NULL) {
64 switch (packets->packet->tag) {
67 * It's a signature packet. Add it to either the public
68 * key (it should be a revocation), to the current UID
69 * or the current subkey.
71 assert(curkey != NULL);
72 if (curkey->subkeys != NULL) {
73 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
75 packet_dup(packets->packet));
76 } else if (curkey->uids != NULL) {
77 ADD_PACKET_TO_LIST_END(curkey->last_uid,
79 packet_dup(packets->packet));
81 ADD_PACKET_TO_LIST_END(curkey,
83 packet_dup(packets->packet));
88 * It's a public key packet, so start a new key in our
92 curkey->next = malloc(sizeof (*curkey));
93 curkey = curkey->next;
96 malloc(sizeof (*curkey));
98 memset(curkey, 0, sizeof(*curkey));
99 curkey->publickey = packet_dup(packets->packet);
105 * It's a UID packet (or a photo id, which is similar).
107 assert(curkey != NULL);
108 assert(curkey->subkeys == NULL);
109 ADD_PACKET_TO_LIST_END(curkey,
111 packet_dup(packets->packet));
115 * It's a subkey packet.
117 assert(curkey != NULL);
118 ADD_PACKET_TO_LIST_END(curkey,
120 packet_dup(packets->packet));
127 * Trust packet. Ignore.
128 * Comment packet. Ignore.
132 logthing(LOGTHING_ERROR,
133 "Unsupported packet type: %d",
134 packets->packet->tag);
136 packets = packets->next;
143 * debug_packet - Print debug info about a packet
144 * @packet: The packet to display.
146 * This function takes an OpenPGP packet and displays some information
147 * about it to stdout. Useful for debugging purposes or curiousity about
148 * an OpenPGP packet stream.
150 int debug_packet(struct openpgp_packet *packet)
152 printf("\tNew format: %d, Tag: %d, Length: %d\n",
161 * read_openpgp_stream - Reads a stream of OpenPGP packets.
162 * @getchar_func: The function to get the next character from the stream.
163 * @ctx: A pointer to the context structure for getchar_func.
164 * @packets: The outputted list of packets.
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)
175 unsigned char curchar = 0;
176 unsigned long count = 0;
177 struct openpgp_packet_list *curpacket = NULL;
179 bool inpacket = false;
181 assert(packets != NULL);
183 while (!rc && !getchar_func(ctx, 1, &curchar)) {
184 if (!inpacket && (curchar & 0x80)) {
186 * New packet. Record the fact we're in a packet and
187 * allocate memory for it.
191 if (curpacket != NULL) {
192 curpacket->next = malloc(sizeof (*curpacket));
193 curpacket = curpacket->next;
195 *packets = curpacket =
196 malloc(sizeof (*curpacket));
198 memset(curpacket, 0, sizeof(*curpacket));
200 malloc(sizeof (*curpacket->packet));
201 memset(curpacket->packet, 0,
202 sizeof(*curpacket->packet));
204 curpacket->packet->newformat = (curchar & 0x40);
207 * TODO: Better error checking on getchar_func.
209 if (curpacket->packet->newformat) {
210 curpacket->packet->tag = (curchar & 0x3F);
211 rc = getchar_func(ctx, 1, &curchar);
212 curpacket->packet->length = curchar;
213 if (curpacket->packet->length > 191 &&
214 curpacket->packet->length < 224) {
215 rc = getchar_func(ctx, 1, &curchar);
216 curpacket->packet->length -= 192;
217 curpacket->packet->length <<= 8;
218 curpacket->packet->length += curchar;
219 curpacket->packet->length += 192;
220 } else if (curpacket->packet->length > 223 &&
221 curpacket->packet->length < 255) {
222 logthing(LOGTHING_NOTICE,
224 " not supported.\n");
225 } else if (curpacket->packet->length == 255) {
227 * 5 byte length; ie 255 followed by 3
228 * bytes of MSB length.
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;
235 curpacket->packet->length <<= 8;
236 rc = getchar_func(ctx, 1, &curchar);
237 curpacket->packet->length = curchar;
238 curpacket->packet->length <<= 8;
239 rc = getchar_func(ctx, 1, &curchar);
240 curpacket->packet->length = curchar;
243 curpacket->packet->tag = (curchar & 0x3C) >> 2;
244 switch (curchar & 3) {
246 rc = getchar_func(ctx, 1, &curchar);
247 curpacket->packet->length = curchar;
250 rc = getchar_func(ctx, 1, &curchar);
251 curpacket->packet->length = curchar;
252 curpacket->packet->length <<= 8;
253 rc = getchar_func(ctx, 1, &curchar);
254 curpacket->packet->length += curchar;
257 rc = getchar_func(ctx, 1, &curchar);
258 curpacket->packet->length =
260 rc = getchar_func(ctx, 1, &curchar);
261 curpacket->packet->length +=
263 rc = getchar_func(ctx, 1, &curchar);
264 curpacket->packet->length +=
266 rc = getchar_func(ctx, 1, &curchar);
267 curpacket->packet->length += curchar;
270 logthing(LOGTHING_ERROR,
271 "Unsupported length type 3.");
272 curpacket->packet->length = 0;
273 curpacket->packet->data = NULL;
280 curpacket->packet->data =
281 malloc(curpacket->packet->length *
282 sizeof(unsigned char));
283 if (curpacket->packet->data == NULL) {
284 logthing(LOGTHING_ERROR,
285 "Can't allocate memory for "
289 rc = getchar_func(ctx,
290 curpacket->packet->length,
291 curpacket->packet->data);
296 logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
305 * write_openpgp_stream - Reads a stream of OpenPGP packets.
306 * @putchar_func: The function to put the next character to the stream.
307 * @ctx: A pointer to the context structure for putchar_func.
308 * @packets: The list of packets.
310 * This function uses putchar_func to write characters to an OpenPGP
311 * packet stream from a linked list of packets.
313 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
316 struct openpgp_packet_list *packets)
318 unsigned char curchar = 0;
320 while (packets != NULL) {
322 if (packets->packet->newformat) {
324 curchar |= packets->packet->tag;
325 putchar_func(ctx, 1, &curchar);
327 if (packets->packet->length < 192) {
328 curchar = packets->packet->length;
329 putchar_func(ctx, 1, &curchar);
330 } else if (packets->packet->length > 191 &&
331 packets->packet->length < 8383) {
332 curchar = (((packets->packet->length - 192) &
334 putchar_func(ctx, 1, &curchar);
336 curchar = (packets->packet->length - 192) &
338 putchar_func(ctx, 1, &curchar);
340 logthing(LOGTHING_ERROR,
341 "Unsupported new format length.");
344 curchar |= (packets->packet->tag << 2);
345 if (packets->packet->length < 256) {
346 putchar_func(ctx, 1, &curchar);
347 curchar = packets->packet->length;
348 putchar_func(ctx, 1, &curchar);
349 } else if (packets->packet->length < 0x10000) {
351 putchar_func(ctx, 1, &curchar);
352 curchar = packets->packet->length >> 8;
353 putchar_func(ctx, 1, &curchar);
354 curchar = packets->packet->length & 0xFF;
355 putchar_func(ctx, 1, &curchar);
358 putchar_func(ctx, 1, &curchar);
359 curchar = packets->packet->length >> 24;
360 putchar_func(ctx, 1, &curchar);
361 curchar = (packets->packet->length >> 16) & 0xFF;
362 putchar_func(ctx, 1, &curchar);
363 curchar = (packets->packet->length >> 8) & 0xFF;
364 putchar_func(ctx, 1, &curchar);
365 curchar = packets->packet->length & 0xFF;
366 putchar_func(ctx, 1, &curchar);
370 putchar_func(ctx, packets->packet->length,
371 packets->packet->data);
372 packets = packets->next;
378 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
379 * @key: The public key.
380 * @packets: The outputted packet list.
382 * This function converts public key structure to a linked list of OpenPGP
383 * packets ready for outputing or storage.
385 int flatten_publickey(struct openpgp_publickey *key,
386 struct openpgp_packet_list **packets,
387 struct openpgp_packet_list **list_end)
389 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
390 struct openpgp_packet_list *tmplist = NULL;
392 while (key != NULL) {
394 * First write the public key packet out.
396 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
397 if (*packets == NULL) {
398 *packets = *list_end;
402 * Now do any revocation signatures on the main key.
404 for (tmplist = key->revocations; tmplist != NULL;
405 tmplist = tmplist->next) {
406 ADD_PACKET_TO_LIST((*list_end),
407 packet_dup(tmplist->packet));
411 * Output any UIDs along with their signatures.
413 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
414 tmpsignedlist = tmpsignedlist->next) {
416 ADD_PACKET_TO_LIST((*list_end),
417 packet_dup(tmpsignedlist->packet));
418 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
419 tmplist = tmplist->next) {
420 ADD_PACKET_TO_LIST((*list_end),
421 packet_dup(tmplist->packet));
426 * Output any subkeys along with their signatures.
428 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
429 tmpsignedlist = tmpsignedlist->next) {
431 ADD_PACKET_TO_LIST((*list_end),
432 packet_dup(tmpsignedlist->packet));
433 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
434 tmplist = tmplist->next) {
435 ADD_PACKET_TO_LIST((*list_end),
436 packet_dup(tmplist->packet));