2 * parsekey.c - Routines to parse an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
16 #include "keystructs.h"
22 * add_key - Takes a key and adds it to the keyserver.
23 * @key: The public key to add.
25 * This function takes a public key and adds it to the keyserver.
26 * It first of all sees if we already have the key locally. If we do then
27 * we retrieve it and merge the two keys. We then store the resulting key
28 * (or just the original we received if we don't already have it). We then
29 * send out the appropriate updates to our keyserver peers.
31 int add_key(struct openpgp_publickey *key) {
36 * parse_keys - Process a stream of packets for public keys + sigs.
37 * @packets: The packet list to parse.
38 * @keys: The returned list of public keys.
40 * This function takes an list of OpenPGP packets and attempts to parse it
41 * into a list of public keys with signatures and subkeys.
43 * Returns a count of how many keys we parsed.
45 int parse_keys(struct openpgp_packet_list *packets,
46 struct openpgp_publickey **keys)
48 struct openpgp_publickey *curkey = NULL;
54 * If keys already has some keys in it then set curkey to the last one
55 * so we add to the end of the list.
57 for (curkey = *keys; curkey != NULL && curkey->next != NULL;
58 curkey = curkey->next) ;
60 while (packets != NULL) {
61 switch (packets->packet->tag) {
64 * It's a signature packet. Add it to either the public
65 * key (it should be a revocation), to the current UID
66 * or the current subkey.
68 assert(curkey != NULL);
69 if (curkey->subkeys != NULL) {
70 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
72 packet_dup(packets->packet));
73 } else if (curkey->uids != NULL) {
74 ADD_PACKET_TO_LIST_END(curkey->last_uid,
76 packet_dup(packets->packet));
78 ADD_PACKET_TO_LIST_END(curkey,
80 packet_dup(packets->packet));
85 * It's a public key packet, so start a new key in our
89 curkey->next = malloc(sizeof (*curkey));
90 curkey = curkey->next;
93 malloc(sizeof (*curkey));
95 memset(curkey, 0, sizeof(*curkey));
96 curkey->publickey = packet_dup(packets->packet);
102 * It's a UID packet (or a photo id, which is similar).
104 assert(curkey != NULL);
105 assert(curkey->subkeys == NULL);
106 ADD_PACKET_TO_LIST_END(curkey,
108 packet_dup(packets->packet));
112 * It's a subkey packet.
114 assert(curkey != NULL);
115 ADD_PACKET_TO_LIST_END(curkey,
117 packet_dup(packets->packet));
124 * Trust packet. Ignore.
125 * Comment packet. Ignore.
129 fprintf(stderr, "Unsupported packet type: %d\n",
130 packets->packet->tag);
132 packets = packets->next;
139 * debug_packet - Print debug info about a packet
140 * @packet: The packet to display.
142 * This function takes an OpenPGP packet and displays some information
143 * about it to stdout. Useful for debugging purposes or curiousity about
144 * an OpenPGP packet stream.
146 int debug_packet(struct openpgp_packet *packet)
148 printf("\tNew format: %d, Tag: %d, Length: %d\n",
157 * read_openpgp_stream - Reads a stream of OpenPGP packets.
158 * @getchar_func: The function to get the next character from the stream.
159 * @ctx: A pointer to the context structure for getchar_func.
160 * @packets: The outputted list of packets.
162 * This function uses getchar_func to read characters from an OpenPGP
163 * packet stream and reads the packets into a linked list of packets
164 * ready for parsing as a public key or whatever.
166 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
169 struct openpgp_packet_list **packets)
171 unsigned char curchar = 0;
172 unsigned long count = 0;
173 struct openpgp_packet_list *curpacket = NULL;
175 bool inpacket = false;
177 assert(packets != NULL);
179 while (!rc && !getchar_func(ctx, 1, &curchar)) {
180 if (!inpacket && (curchar & 0x80)) {
182 * New packet. Record the fact we're in a packet and
183 * allocate memory for it.
187 if (curpacket != NULL) {
188 curpacket->next = malloc(sizeof (*curpacket));
189 curpacket = curpacket->next;
191 *packets = curpacket =
192 malloc(sizeof (*curpacket));
194 memset(curpacket, 0, sizeof(*curpacket));
196 malloc(sizeof (*curpacket->packet));
197 memset(curpacket->packet, 0,
198 sizeof(*curpacket->packet));
200 curpacket->packet->newformat = (curchar & 0x40);
202 // 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 printf("Partial length; not supported.\n");
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 fprintf(stderr, "Unsupported length type 3.\n");
263 curpacket->packet->length = 0;
264 curpacket->packet->data = NULL;
271 curpacket->packet->data =
272 malloc(curpacket->packet->length *
273 sizeof(unsigned char));
274 rc = getchar_func(ctx,
275 curpacket->packet->length,
276 curpacket->packet->data);
280 fprintf(stderr, "Unexpected character: 0x%X\n",
289 * write_openpgp_stream - Reads a stream of OpenPGP packets.
290 * @putchar_func: The function to put the next character to the stream.
291 * @ctx: A pointer to the context structure for putchar_func.
292 * @packets: The list of packets.
294 * This function uses putchar_func to write characters to an OpenPGP
295 * packet stream from a linked list of packets.
297 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
300 struct openpgp_packet_list *packets)
302 unsigned char curchar = 0;
304 while (packets != NULL) {
306 if (packets->packet->newformat) {
308 curchar |= packets->packet->tag;
309 putchar_func(ctx, 1, &curchar);
311 if (packets->packet->length < 192) {
312 curchar = packets->packet->length;
313 putchar_func(ctx, 1, &curchar);
314 } else if (packets->packet->length > 191 &&
315 packets->packet->length < 8383) {
316 curchar = (((packets->packet->length - 192) &
318 putchar_func(ctx, 1, &curchar);
320 curchar = (packets->packet->length - 192) &
322 putchar_func(ctx, 1, &curchar);
324 fputs("Unsupported new format length.\n", stderr);
327 curchar |= (packets->packet->tag << 2);
328 if (packets->packet->length < 256) {
329 putchar_func(ctx, 1, &curchar);
330 curchar = packets->packet->length;
331 putchar_func(ctx, 1, &curchar);
332 } else if (packets->packet->length < 0x10000) {
334 putchar_func(ctx, 1, &curchar);
335 curchar = packets->packet->length >> 8;
336 putchar_func(ctx, 1, &curchar);
337 curchar = packets->packet->length & 0xFF;
338 putchar_func(ctx, 1, &curchar);
341 putchar_func(ctx, 1, &curchar);
342 curchar = packets->packet->length >> 24;
343 putchar_func(ctx, 1, &curchar);
344 curchar = (packets->packet->length >> 16) & 0xFF;
345 putchar_func(ctx, 1, &curchar);
346 curchar = (packets->packet->length >> 8) & 0xFF;
347 putchar_func(ctx, 1, &curchar);
348 curchar = packets->packet->length & 0xFF;
349 putchar_func(ctx, 1, &curchar);
353 putchar_func(ctx, packets->packet->length, packets->packet->data);
354 // for (i = 0; i < packets->packet->length; i++) {
355 // putchar_func(ctx, packets->packet->data[i]);
357 packets = packets->next;
363 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
364 * @key: The public key.
365 * @packets: The outputted packet list.
367 * This function converts public key structure to a linked list of OpenPGP
368 * packets ready for outputing or storage.
370 int flatten_publickey(struct openpgp_publickey *key,
371 struct openpgp_packet_list **packets,
372 struct openpgp_packet_list **list_end)
374 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
375 struct openpgp_packet_list *tmplist = NULL;
377 while (key != NULL) {
379 * First write the public key packet out.
381 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
382 if (*packets == NULL) {
383 *packets = *list_end;
387 * Now do any revocation signatures on the main key.
389 for (tmplist = key->revocations; tmplist != NULL;
390 tmplist = tmplist->next) {
391 ADD_PACKET_TO_LIST((*list_end),
392 packet_dup(tmplist->packet));
396 * Output any UIDs along with their signatures.
398 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
399 tmpsignedlist = tmpsignedlist->next) {
401 ADD_PACKET_TO_LIST((*list_end),
402 packet_dup(tmpsignedlist->packet));
403 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
404 tmplist = tmplist->next) {
405 ADD_PACKET_TO_LIST((*list_end),
406 packet_dup(tmplist->packet));
411 * Output any subkeys along with their signatures.
413 for (tmpsignedlist = key->subkeys; 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));