cscvs to tla changeset 101
[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.14 2003/09/30 21:59:33 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  *      @maxnum: The maximum number of keys to read. 0 means unlimited.
166  *
167  *      This function uses getchar_func to read characters from an OpenPGP
168  *      packet stream and reads the packets into a linked list of packets
169  *      ready for parsing as a public key or whatever.
170  */
171 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
172                                 unsigned char *c),
173                                 void *ctx,
174                                 struct openpgp_packet_list **packets,
175                                 int maxnum)
176 {
177         unsigned char                    curchar = 0;
178         unsigned long                    count = 0;
179         struct openpgp_packet_list      *curpacket = NULL;
180         int                              rc = 0;
181         int                              keys = 0;
182         bool                             inpacket = false;
183
184         assert(packets != NULL);
185         curpacket = *packets;
186         if (curpacket != NULL) {
187                 while (curpacket->next != NULL) {
188                         curpacket = curpacket->next;
189                 }
190         }
191
192         while (!rc && !getchar_func(ctx, 1, &curchar) &&
193                         (maxnum == 0 || keys < maxnum)) {
194                 if (!inpacket && (curchar & 0x80)) {
195                         /*
196                          * New packet. Record the fact we're in a packet and
197                          * allocate memory for it.
198                          */
199                         inpacket = true;
200                         count = 0;
201                         if (curpacket != NULL) {
202                                 curpacket->next = malloc(sizeof (*curpacket));
203                                 curpacket = curpacket->next;
204                         } else {
205                                 *packets = curpacket =
206                                         malloc(sizeof (*curpacket));
207                         }
208                         memset(curpacket, 0, sizeof(*curpacket));
209                         curpacket->packet =
210                                 malloc(sizeof (*curpacket->packet));
211                         memset(curpacket->packet, 0,
212                                         sizeof(*curpacket->packet));
213
214                         curpacket->packet->newformat = (curchar & 0x40);
215
216                         /*
217                          * TODO: Better error checking on getchar_func.
218                          */
219                         if (curpacket->packet->newformat) {
220                                 curpacket->packet->tag = (curchar & 0x3F);
221                                 rc = getchar_func(ctx, 1, &curchar);
222                                 curpacket->packet->length = curchar;
223                                 if (curpacket->packet->length > 191 &&
224                                         curpacket->packet->length < 224) {
225                                         rc = getchar_func(ctx, 1, &curchar);
226                                         curpacket->packet->length -= 192;
227                                         curpacket->packet->length <<= 8;
228                                         curpacket->packet->length += curchar;
229                                         curpacket->packet->length += 192;
230                                 } else if (curpacket->packet->length > 223 &&
231                                         curpacket->packet->length < 255) {
232                                         logthing(LOGTHING_NOTICE,
233                                                 "Partial length;"
234                                                 " not supported.");
235                                 } else if (curpacket->packet->length == 255) {
236                                         /*
237                                          * 5 byte length; ie 255 followed by 3
238                                          * bytes of MSB length.
239                                          */
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                                         curpacket->packet->length <<= 8;
246                                         rc = getchar_func(ctx, 1, &curchar);
247                                         curpacket->packet->length = curchar;
248                                         curpacket->packet->length <<= 8;
249                                         rc = getchar_func(ctx, 1, &curchar);
250                                         curpacket->packet->length = curchar;
251                                 }
252                         } else {
253                                 curpacket->packet->tag = (curchar & 0x3C) >> 2;
254                                 switch (curchar & 3) {
255                                 case 0:
256                                         rc = getchar_func(ctx, 1, &curchar);
257                                         curpacket->packet->length = curchar;
258                                         break;
259                                 case 1:
260                                         rc = getchar_func(ctx, 1, &curchar);
261                                         curpacket->packet->length = curchar;
262                                         curpacket->packet->length <<= 8;
263                                         rc = getchar_func(ctx, 1, &curchar);
264                                         curpacket->packet->length += curchar;
265                                         break;
266                                 case 2:
267                                         rc = getchar_func(ctx, 1, &curchar);
268                                         curpacket->packet->length = 
269                                                 (curchar << 24);
270                                         rc = getchar_func(ctx, 1, &curchar);
271                                         curpacket->packet->length +=
272                                                 (curchar << 16);
273                                         rc = getchar_func(ctx, 1, &curchar);
274                                         curpacket->packet->length +=
275                                                 (curchar << 8);
276                                         rc = getchar_func(ctx, 1, &curchar);
277                                         curpacket->packet->length += curchar;
278                                         break;
279                                 case 3:
280                                         logthing(LOGTHING_ERROR,
281                                                 "Unsupported length type 3.");
282                                         curpacket->packet->length = 0;
283                                         curpacket->packet->data = NULL;
284                                         rc = -1;
285                                         break;
286                                 }
287                         }
288
289                         if (rc == 0) {
290                                 if (curpacket->packet->tag == 6) {
291                                         keys++;
292                                 }
293                                 curpacket->packet->data =
294                                         malloc(curpacket->packet->length *
295                                         sizeof(unsigned char));
296                                 if (curpacket->packet->data == NULL) {
297                                         logthing(LOGTHING_ERROR, 
298                                                 "Can't allocate memory for "
299                                                 "packet!");
300                                         rc = -1;
301                                 } else {
302                                         rc = getchar_func(ctx,
303                                                 curpacket->packet->length,
304                                                 curpacket->packet->data);
305                                 }
306                         }
307                         inpacket = false;
308                 } else {
309                         logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
310                                 curchar);
311                 }
312         }
313
314         return (rc);
315 }
316
317 /**
318  *      write_openpgp_stream - Reads a stream of OpenPGP packets.
319  *      @putchar_func: The function to put the next character to the stream.
320  *      @ctx: A pointer to the context structure for putchar_func.
321  *      @packets: The list of packets.
322  *
323  *      This function uses putchar_func to write characters to an OpenPGP
324  *      packet stream from a linked list of packets.
325  */
326 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
327                                                 unsigned char *c),
328                                 void *ctx,
329                                 struct openpgp_packet_list *packets)
330 {
331         unsigned char   curchar = 0;
332
333         while (packets != NULL) {
334                 curchar = 0x80;
335                 if (packets->packet->newformat) {
336                         curchar |= 0x40;
337                         curchar |= packets->packet->tag;
338                         putchar_func(ctx, 1, &curchar);
339
340                         if (packets->packet->length < 192) {
341                                 curchar = packets->packet->length;
342                                 putchar_func(ctx, 1, &curchar);
343                         } else if (packets->packet->length > 191 &&
344                                 packets->packet->length < 8383) {
345                                 curchar = (((packets->packet->length - 192) &
346                                          0xFF00) >> 8) + 192;
347                                 putchar_func(ctx, 1, &curchar);
348
349                                 curchar = (packets->packet->length - 192) &
350                                          0xFF;
351                                 putchar_func(ctx, 1, &curchar);
352                         } else {
353                                 logthing(LOGTHING_ERROR,
354                                         "Unsupported new format length.");
355                         }
356                 } else {
357                         curchar |= (packets->packet->tag << 2);
358                         if (packets->packet->length < 256) {
359                                 putchar_func(ctx, 1, &curchar);
360                                 curchar = packets->packet->length;
361                                 putchar_func(ctx, 1, &curchar);
362                         } else if (packets->packet->length < 0x10000) {
363                                 curchar |= 1;
364                                 putchar_func(ctx, 1, &curchar);
365                                 curchar = packets->packet->length >> 8;
366                                 putchar_func(ctx, 1, &curchar);
367                                 curchar = packets->packet->length & 0xFF;
368                                 putchar_func(ctx, 1, &curchar);
369                         } else {
370                                 curchar |= 2;
371                                 putchar_func(ctx, 1, &curchar);
372                                 curchar = packets->packet->length >> 24;
373                                 putchar_func(ctx, 1, &curchar);
374                                 curchar = (packets->packet->length >> 16) & 0xFF;
375                                 putchar_func(ctx, 1, &curchar);
376                                 curchar = (packets->packet->length >> 8) & 0xFF;
377                                 putchar_func(ctx, 1, &curchar);
378                                 curchar = packets->packet->length & 0xFF;
379                                 putchar_func(ctx, 1, &curchar);
380                         }
381                 }
382
383                 putchar_func(ctx, packets->packet->length,
384                                 packets->packet->data);
385                 packets = packets->next;
386         }
387         return 0;
388 }
389
390 /**
391  *      flatten_publickey - Convert a publickey to an OpenPGP packet list.
392  *      @key: The public key.
393  *      @packets: The outputted packet list.
394  *
395  *      This function converts public key structure to a linked list of OpenPGP
396  *      packets ready for outputing or storage.
397  */
398 int flatten_publickey(struct openpgp_publickey *key,
399                         struct openpgp_packet_list **packets,
400                         struct openpgp_packet_list **list_end)
401 {
402         struct openpgp_signedpacket_list        *tmpsignedlist = NULL;
403         struct openpgp_packet_list              *tmplist = NULL;
404
405         while (key != NULL) {
406                 /*
407                  * First write the public key packet out.
408                  */
409                 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
410                 if (*packets == NULL) {
411                         *packets = *list_end;
412                 }
413
414                 /*
415                  * Now do any revocation signatures on the main key.
416                  */
417                 for (tmplist = key->revocations; tmplist != NULL;
418                                 tmplist = tmplist->next) {
419                         ADD_PACKET_TO_LIST((*list_end),
420                                         packet_dup(tmplist->packet));
421                 }
422
423                 /*
424                  * Output any UIDs along with their signatures.
425                  */
426                 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
427                                 tmpsignedlist = tmpsignedlist->next) {
428
429                         ADD_PACKET_TO_LIST((*list_end),
430                                 packet_dup(tmpsignedlist->packet));
431                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
432                                         tmplist = tmplist->next) {
433                                 ADD_PACKET_TO_LIST((*list_end), 
434                                         packet_dup(tmplist->packet));
435                         }
436                 }
437
438                 /*
439                  * Output any subkeys along with their signatures.
440                  */
441                 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
442                                 tmpsignedlist = tmpsignedlist->next) {
443
444                         ADD_PACKET_TO_LIST((*list_end),
445                                 packet_dup(tmpsignedlist->packet));
446                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
447                                         tmplist = tmplist->next) {
448                                 ADD_PACKET_TO_LIST((*list_end), 
449                                         packet_dup(tmplist->packet));
450                         }
451                 }
452                 key = key->next;
453         }
454         return 0;
455 }