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