cscvs to tla changeset 96
[onak.git] / parsekey.c
1 /*
2  * parsekey.c - Routines to parse an OpenPGP key.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  *
8  * $Id: parsekey.c,v 1.11 2003/09/30 16:58:04 noodles Exp $
9  */
10
11 #include <assert.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "keyid.h"
18 #include "keystructs.h"
19 #include "ll.h"
20 #include "log.h"
21 #include "mem.h"
22 #include "parsekey.h"
23
24 /**
25  *      add_key - Takes a key and adds it to the keyserver.
26  *      @key: The public key to add.
27  *
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.
33  */
34 int add_key(struct openpgp_publickey *key) {
35         return 0;
36 }
37
38 /**
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.
42  *
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.
45  *
46  *      Returns a count of how many keys we parsed.
47  */
48 int parse_keys(struct openpgp_packet_list *packets,
49                 struct openpgp_publickey **keys)
50 {
51         struct openpgp_publickey *curkey = NULL;
52         int count;
53
54         count = 0;
55
56         /*
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.
59          */
60         for (curkey = *keys; curkey != NULL && curkey->next != NULL;
61                         curkey = curkey->next) ;
62
63         while (packets != NULL) {
64                 switch (packets->packet->tag) {
65                 case 2:
66                         /*
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.
70                          */
71                         assert(curkey != NULL);
72                         if (curkey->subkeys != NULL) {
73                                 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
74                                         sig,
75                                         packet_dup(packets->packet));
76                         } else if (curkey->uids != NULL) {
77                                 ADD_PACKET_TO_LIST_END(curkey->last_uid,
78                                         sig,
79                                         packet_dup(packets->packet));
80                         } else {
81                                 ADD_PACKET_TO_LIST_END(curkey,
82                                         revocation,
83                                         packet_dup(packets->packet));
84                         }
85                         break;
86                 case 6:
87                         /*
88                          * It's a public key packet, so start a new key in our
89                          * list.
90                          */
91                         if (curkey != NULL) {
92                                 curkey->next = malloc(sizeof (*curkey));
93                                 curkey = curkey->next;
94                         } else {
95                                 *keys = curkey =
96                                         malloc(sizeof (*curkey));
97                         }
98                         memset(curkey, 0, sizeof(*curkey));
99                         curkey->publickey = packet_dup(packets->packet);
100                         count++;
101                         break;
102                 case 13:
103                 case 17:
104                         /*
105                          * It's a UID packet (or a photo id, which is similar).
106                          */
107                         assert(curkey != NULL);
108                         assert(curkey->subkeys == NULL);
109                         ADD_PACKET_TO_LIST_END(curkey,
110                                 uid,
111                                 packet_dup(packets->packet));
112                         break;
113                 case 14:
114                         /*
115                          * It's a subkey packet.
116                          */
117                         assert(curkey != NULL);
118                         ADD_PACKET_TO_LIST_END(curkey,
119                                 subkey,
120                                 packet_dup(packets->packet));
121                         break;
122                 case 12:
123                 case 61:
124                         /*
125                          * One of:
126                          *
127                          * Trust packet. Ignore.
128                          * Comment packet. Ignore.
129                          */
130                         break;
131                 default:
132                         logthing(LOGTHING_ERROR,
133                                         "Unsupported packet type: %d",
134                                         packets->packet->tag);
135                 }
136                 packets = packets->next;
137         }
138
139         return count;
140 }
141
142 /**
143  *      debug_packet - Print debug info about a packet
144  *      @packet: The packet to display.
145  *
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.
149  */
150 int debug_packet(struct openpgp_packet *packet)
151 {
152         printf("\tNew format: %d, Tag: %d, Length: %d\n",
153                         packet->newformat,
154                         packet->tag,
155                         packet->length);
156
157         return 0;
158 }
159
160 /**
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.
165  *
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.
169  */
170 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
171                                 unsigned char *c),
172                                 void *ctx,
173                                 struct openpgp_packet_list **packets)
174 {
175         unsigned char                    curchar = 0;
176         unsigned long                    count = 0;
177         struct openpgp_packet_list      *curpacket = NULL;
178         int                              rc = 0;
179         bool                             inpacket = false;
180
181         assert(packets != NULL);
182         curpacket = *packets;
183         while (curpacket->next != NULL) {
184                 curpacket = curpacket->next;
185         }
186
187         while (!rc && !getchar_func(ctx, 1, &curchar)) {
188                 if (!inpacket && (curchar & 0x80)) {
189                         /*
190                          * New packet. Record the fact we're in a packet and
191                          * allocate memory for it.
192                          */
193                         inpacket = true;
194                         count = 0;
195                         if (curpacket != NULL) {
196                                 curpacket->next = malloc(sizeof (*curpacket));
197                                 curpacket = curpacket->next;
198                         } else {
199                                 *packets = curpacket =
200                                         malloc(sizeof (*curpacket));
201                         }
202                         memset(curpacket, 0, sizeof(*curpacket));
203                         curpacket->packet =
204                                 malloc(sizeof (*curpacket->packet));
205                         memset(curpacket->packet, 0,
206                                         sizeof(*curpacket->packet));
207
208                         curpacket->packet->newformat = (curchar & 0x40);
209
210                         /*
211                          * TODO: Better error checking on getchar_func.
212                          */
213                         if (curpacket->packet->newformat) {
214                                 curpacket->packet->tag = (curchar & 0x3F);
215                                 rc = getchar_func(ctx, 1, &curchar);
216                                 curpacket->packet->length = curchar;
217                                 if (curpacket->packet->length > 191 &&
218                                         curpacket->packet->length < 224) {
219                                         rc = getchar_func(ctx, 1, &curchar);
220                                         curpacket->packet->length -= 192;
221                                         curpacket->packet->length <<= 8;
222                                         curpacket->packet->length += curchar;
223                                         curpacket->packet->length += 192;
224                                 } else if (curpacket->packet->length > 223 &&
225                                         curpacket->packet->length < 255) {
226                                         logthing(LOGTHING_NOTICE,
227                                                 "Partial length;"
228                                                 " not supported.\n");
229                                 } else if (curpacket->packet->length == 255) {
230                                         /*
231                                          * 5 byte length; ie 255 followed by 3
232                                          * bytes of MSB length.
233                                          */
234                                         rc = getchar_func(ctx, 1, &curchar);
235                                         curpacket->packet->length = curchar;
236                                         curpacket->packet->length <<= 8;
237                                         rc = getchar_func(ctx, 1, &curchar);
238                                         curpacket->packet->length = curchar;
239                                         curpacket->packet->length <<= 8;
240                                         rc = getchar_func(ctx, 1, &curchar);
241                                         curpacket->packet->length = curchar;
242                                         curpacket->packet->length <<= 8;
243                                         rc = getchar_func(ctx, 1, &curchar);
244                                         curpacket->packet->length = curchar;
245                                 }
246                         } else {
247                                 curpacket->packet->tag = (curchar & 0x3C) >> 2;
248                                 switch (curchar & 3) {
249                                 case 0:
250                                         rc = getchar_func(ctx, 1, &curchar);
251                                         curpacket->packet->length = curchar;
252                                         break;
253                                 case 1:
254                                         rc = getchar_func(ctx, 1, &curchar);
255                                         curpacket->packet->length = curchar;
256                                         curpacket->packet->length <<= 8;
257                                         rc = getchar_func(ctx, 1, &curchar);
258                                         curpacket->packet->length += curchar;
259                                         break;
260                                 case 2:
261                                         rc = getchar_func(ctx, 1, &curchar);
262                                         curpacket->packet->length = 
263                                                 (curchar << 24);
264                                         rc = getchar_func(ctx, 1, &curchar);
265                                         curpacket->packet->length +=
266                                                 (curchar << 16);
267                                         rc = getchar_func(ctx, 1, &curchar);
268                                         curpacket->packet->length +=
269                                                 (curchar << 8);
270                                         rc = getchar_func(ctx, 1, &curchar);
271                                         curpacket->packet->length += curchar;
272                                         break;
273                                 case 3:
274                                         logthing(LOGTHING_ERROR,
275                                                 "Unsupported length type 3.");
276                                         curpacket->packet->length = 0;
277                                         curpacket->packet->data = NULL;
278                                         rc = -1;
279                                         break;
280                                 }
281                         }
282
283                         if (rc == 0) {
284                                 curpacket->packet->data =
285                                         malloc(curpacket->packet->length *
286                                         sizeof(unsigned char));
287                                 if (curpacket->packet->data == NULL) {
288                                         logthing(LOGTHING_ERROR, 
289                                                 "Can't allocate memory for "
290                                                 "packet!");
291                                         rc = -1;
292                                 } else {
293                                         rc = getchar_func(ctx,
294                                                 curpacket->packet->length,
295                                                 curpacket->packet->data);
296                                 }
297                         }
298                         inpacket = false;
299                 } else {
300                         logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
301                                 curchar);
302                 }
303         }
304
305         return (rc);
306 }
307
308 /**
309  *      write_openpgp_stream - Reads a stream of OpenPGP packets.
310  *      @putchar_func: The function to put the next character to the stream.
311  *      @ctx: A pointer to the context structure for putchar_func.
312  *      @packets: The list of packets.
313  *
314  *      This function uses putchar_func to write characters to an OpenPGP
315  *      packet stream from a linked list of packets.
316  */
317 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
318                                                 unsigned char *c),
319                                 void *ctx,
320                                 struct openpgp_packet_list *packets)
321 {
322         unsigned char   curchar = 0;
323
324         while (packets != NULL) {
325                 curchar = 0x80;
326                 if (packets->packet->newformat) {
327                         curchar |= 0x40;
328                         curchar |= packets->packet->tag;
329                         putchar_func(ctx, 1, &curchar);
330
331                         if (packets->packet->length < 192) {
332                                 curchar = packets->packet->length;
333                                 putchar_func(ctx, 1, &curchar);
334                         } else if (packets->packet->length > 191 &&
335                                 packets->packet->length < 8383) {
336                                 curchar = (((packets->packet->length - 192) &
337                                          0xFF00) >> 8) + 192;
338                                 putchar_func(ctx, 1, &curchar);
339
340                                 curchar = (packets->packet->length - 192) &
341                                          0xFF;
342                                 putchar_func(ctx, 1, &curchar);
343                         } else {
344                                 logthing(LOGTHING_ERROR,
345                                         "Unsupported new format length.");
346                         }
347                 } else {
348                         curchar |= (packets->packet->tag << 2);
349                         if (packets->packet->length < 256) {
350                                 putchar_func(ctx, 1, &curchar);
351                                 curchar = packets->packet->length;
352                                 putchar_func(ctx, 1, &curchar);
353                         } else if (packets->packet->length < 0x10000) {
354                                 curchar |= 1;
355                                 putchar_func(ctx, 1, &curchar);
356                                 curchar = packets->packet->length >> 8;
357                                 putchar_func(ctx, 1, &curchar);
358                                 curchar = packets->packet->length & 0xFF;
359                                 putchar_func(ctx, 1, &curchar);
360                         } else {
361                                 curchar |= 2;
362                                 putchar_func(ctx, 1, &curchar);
363                                 curchar = packets->packet->length >> 24;
364                                 putchar_func(ctx, 1, &curchar);
365                                 curchar = (packets->packet->length >> 16) & 0xFF;
366                                 putchar_func(ctx, 1, &curchar);
367                                 curchar = (packets->packet->length >> 8) & 0xFF;
368                                 putchar_func(ctx, 1, &curchar);
369                                 curchar = packets->packet->length & 0xFF;
370                                 putchar_func(ctx, 1, &curchar);
371                         }
372                 }
373
374                 putchar_func(ctx, packets->packet->length,
375                                 packets->packet->data);
376                 packets = packets->next;
377         }
378         return 0;
379 }
380
381 /**
382  *      flatten_publickey - Convert a publickey to an OpenPGP packet list.
383  *      @key: The public key.
384  *      @packets: The outputted packet list.
385  *
386  *      This function converts public key structure to a linked list of OpenPGP
387  *      packets ready for outputing or storage.
388  */
389 int flatten_publickey(struct openpgp_publickey *key,
390                         struct openpgp_packet_list **packets,
391                         struct openpgp_packet_list **list_end)
392 {
393         struct openpgp_signedpacket_list        *tmpsignedlist = NULL;
394         struct openpgp_packet_list              *tmplist = NULL;
395
396         while (key != NULL) {
397                 /*
398                  * First write the public key packet out.
399                  */
400                 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
401                 if (*packets == NULL) {
402                         *packets = *list_end;
403                 }
404
405                 /*
406                  * Now do any revocation signatures on the main key.
407                  */
408                 for (tmplist = key->revocations; tmplist != NULL;
409                                 tmplist = tmplist->next) {
410                         ADD_PACKET_TO_LIST((*list_end),
411                                         packet_dup(tmplist->packet));
412                 }
413
414                 /*
415                  * Output any UIDs along with their signatures.
416                  */
417                 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
418                                 tmpsignedlist = tmpsignedlist->next) {
419
420                         ADD_PACKET_TO_LIST((*list_end),
421                                 packet_dup(tmpsignedlist->packet));
422                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
423                                         tmplist = tmplist->next) {
424                                 ADD_PACKET_TO_LIST((*list_end), 
425                                         packet_dup(tmplist->packet));
426                         }
427                 }
428
429                 /*
430                  * Output any subkeys along with their signatures.
431                  */
432                 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
433                                 tmpsignedlist = tmpsignedlist->next) {
434
435                         ADD_PACKET_TO_LIST((*list_end),
436                                 packet_dup(tmpsignedlist->packet));
437                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
438                                         tmplist = tmplist->next) {
439                                 ADD_PACKET_TO_LIST((*list_end), 
440                                         packet_dup(tmplist->packet));
441                         }
442                 }
443                 key = key->next;
444         }
445         return 0;
446 }