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");
266 curpacket->packet->data =
267 malloc(curpacket->packet->length *
268 sizeof(unsigned char));
269 rc = getchar_func(ctx, curpacket->packet->length,
270 curpacket->packet->data);
273 fprintf(stderr, "Unexpected character: 0x%X\n",
282 * write_openpgp_stream - Reads a stream of OpenPGP packets.
283 * @putchar_func: The function to put the next character to the stream.
284 * @ctx: A pointer to the context structure for putchar_func.
285 * @packets: The list of packets.
287 * This function uses putchar_func to write characters to an OpenPGP
288 * packet stream from a linked list of packets.
290 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
293 struct openpgp_packet_list *packets)
295 unsigned char curchar = 0;
297 while (packets != NULL) {
299 if (packets->packet->newformat) {
301 curchar |= packets->packet->tag;
302 putchar_func(ctx, 1, &curchar);
304 if (packets->packet->length < 192) {
305 curchar = packets->packet->length;
306 putchar_func(ctx, 1, &curchar);
307 } else if (packets->packet->length > 191 &&
308 packets->packet->length < 8383) {
309 curchar = (((packets->packet->length - 192) &
311 putchar_func(ctx, 1, &curchar);
313 curchar = (packets->packet->length - 192) &
315 putchar_func(ctx, 1, &curchar);
317 fputs("Unsupported new format length.\n", stderr);
320 curchar |= (packets->packet->tag << 2);
321 if (packets->packet->length < 256) {
322 putchar_func(ctx, 1, &curchar);
323 curchar = packets->packet->length;
324 putchar_func(ctx, 1, &curchar);
325 } else if (packets->packet->length < 0x10000) {
327 putchar_func(ctx, 1, &curchar);
328 curchar = packets->packet->length >> 8;
329 putchar_func(ctx, 1, &curchar);
330 curchar = packets->packet->length & 0xFF;
331 putchar_func(ctx, 1, &curchar);
334 putchar_func(ctx, 1, &curchar);
335 curchar = packets->packet->length >> 24;
336 putchar_func(ctx, 1, &curchar);
337 curchar = (packets->packet->length >> 16) & 0xFF;
338 putchar_func(ctx, 1, &curchar);
339 curchar = (packets->packet->length >> 8) & 0xFF;
340 putchar_func(ctx, 1, &curchar);
341 curchar = packets->packet->length & 0xFF;
342 putchar_func(ctx, 1, &curchar);
346 putchar_func(ctx, packets->packet->length, packets->packet->data);
347 // for (i = 0; i < packets->packet->length; i++) {
348 // putchar_func(ctx, packets->packet->data[i]);
350 packets = packets->next;
356 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
357 * @key: The public key.
358 * @packets: The outputted packet list.
360 * This function converts public key structure to a linked list of OpenPGP
361 * packets ready for outputing or storage.
363 int flatten_publickey(struct openpgp_publickey *key,
364 struct openpgp_packet_list **packets,
365 struct openpgp_packet_list **list_end)
367 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
368 struct openpgp_packet_list *tmplist = NULL;
370 while (key != NULL) {
372 * First write the public key packet out.
374 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
375 if (*packets == NULL) {
376 *packets = *list_end;
380 * Now do any revocation signatures on the main key.
382 for (tmplist = key->revocations; tmplist != NULL;
383 tmplist = tmplist->next) {
384 ADD_PACKET_TO_LIST((*list_end),
385 packet_dup(tmplist->packet));
389 * Output any UIDs along with their signatures.
391 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
392 tmpsignedlist = tmpsignedlist->next) {
394 ADD_PACKET_TO_LIST((*list_end),
395 packet_dup(tmpsignedlist->packet));
396 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
397 tmplist = tmplist->next) {
398 ADD_PACKET_TO_LIST((*list_end),
399 packet_dup(tmplist->packet));
404 * Output any subkeys along with their signatures.
406 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
407 tmpsignedlist = tmpsignedlist->next) {
409 ADD_PACKET_TO_LIST((*list_end),
410 packet_dup(tmpsignedlist->packet));
411 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
412 tmplist = tmplist->next) {
413 ADD_PACKET_TO_LIST((*list_end),
414 packet_dup(tmplist->packet));