]> git.sommitrealweird.co.uk Git - onak.git/blob - parsekey.c
cscvs to tla changeset 55
[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  *      Returns a count of how many keys we parsed.
44  */
45 int parse_keys(struct openpgp_packet_list *packets,
46                 struct openpgp_publickey **keys)
47 {
48         struct openpgp_publickey *curkey = NULL;
49         int count;
50
51         count = 0;
52
53         /*
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.
56          */
57         for (curkey = *keys; curkey != NULL && curkey->next != NULL;
58                         curkey = curkey->next) ;
59
60         while (packets != NULL) {
61                 switch (packets->packet->tag) {
62                 case 2:
63                         /*
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.
67                          */
68                         assert(curkey != NULL);
69                         if (curkey->subkeys != NULL) {
70                                 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
71                                         sig,
72                                         packet_dup(packets->packet));
73                         } else if (curkey->uids != NULL) {
74                                 ADD_PACKET_TO_LIST_END(curkey->last_uid,
75                                         sig,
76                                         packet_dup(packets->packet));
77                         } else {
78                                 ADD_PACKET_TO_LIST_END(curkey,
79                                         revocation,
80                                         packet_dup(packets->packet));
81                         }
82                         break;
83                 case 6:
84                         /*
85                          * It's a public key packet, so start a new key in our
86                          * list.
87                          */
88                         if (curkey != NULL) {
89                                 curkey->next = malloc(sizeof (*curkey));
90                                 curkey = curkey->next;
91                         } else {
92                                 *keys = curkey =
93                                         malloc(sizeof (*curkey));
94                         }
95                         memset(curkey, 0, sizeof(*curkey));
96                         curkey->publickey = packet_dup(packets->packet);
97                         count++;
98                         break;
99                 case 13:
100                 case 17:
101                         /*
102                          * It's a UID packet (or a photo id, which is similar).
103                          */
104                         assert(curkey != NULL);
105                         assert(curkey->subkeys == NULL);
106                         ADD_PACKET_TO_LIST_END(curkey,
107                                 uid,
108                                 packet_dup(packets->packet));
109                         break;
110                 case 14:
111                         /*
112                          * It's a subkey packet.
113                          */
114                         assert(curkey != NULL);
115                         ADD_PACKET_TO_LIST_END(curkey,
116                                 subkey,
117                                 packet_dup(packets->packet));
118                         break;
119                 case 12:
120                 case 61:
121                         /*
122                          * One of:
123                          *
124                          * Trust packet. Ignore.
125                          * Comment packet. Ignore.
126                          */
127                         break;
128                 default:
129                         fprintf(stderr, "Unsupported packet type: %d\n",
130                                         packets->packet->tag);
131                 }
132                 packets = packets->next;
133         }
134
135         return count;
136 }
137
138 /**
139  *      debug_packet - Print debug info about a packet
140  *      @packet: The packet to display.
141  *
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.
145  */
146 int debug_packet(struct openpgp_packet *packet)
147 {
148         printf("\tNew format: %d, Tag: %d, Length: %d\n",
149                         packet->newformat,
150                         packet->tag,
151                         packet->length);
152
153         return 0;
154 }
155
156 /**
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.
161  *
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.
165  */
166 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
167                                 unsigned char *c),
168                                 void *ctx,
169                                 struct openpgp_packet_list **packets)
170 {
171         unsigned char                    curchar = 0;
172         unsigned long                    count = 0;
173         struct openpgp_packet_list      *curpacket = NULL;
174         int                              rc = 0;
175         bool                             inpacket = false;
176
177         assert(packets != NULL);
178
179         while (!rc && !getchar_func(ctx, 1, &curchar)) {
180                 if (!inpacket && (curchar & 0x80)) {
181                         /*
182                          * New packet. Record the fact we're in a packet and
183                          * allocate memory for it.
184                          */
185                         inpacket = true;
186                         count = 0;
187                         if (curpacket != NULL) {
188                                 curpacket->next = malloc(sizeof (*curpacket));
189                                 curpacket = curpacket->next;
190                         } else {
191                                 *packets = curpacket =
192                                         malloc(sizeof (*curpacket));
193                         }
194                         memset(curpacket, 0, sizeof(*curpacket));
195                         curpacket->packet =
196                                 malloc(sizeof (*curpacket->packet));
197                         memset(curpacket->packet, 0,
198                                         sizeof(*curpacket->packet));
199
200                         curpacket->packet->newformat = (curchar & 0x40);
201
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) {
218                                         /*
219                                          * 5 byte length; ie 255 followed by 3
220                                          * bytes of MSB length.
221                                          */
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;
233                                 }
234                         } else {
235                                 curpacket->packet->tag = (curchar & 0x3C) >> 2;
236                                 switch (curchar & 3) {
237                                 case 0:
238                                         rc = getchar_func(ctx, 1, &curchar);
239                                         curpacket->packet->length = curchar;
240                                         break;
241                                 case 1:
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;
247                                         break;
248                                 case 2:
249                                         rc = getchar_func(ctx, 1, &curchar);
250                                         curpacket->packet->length = 
251                                                 (curchar << 24);
252                                         rc = getchar_func(ctx, 1, &curchar);
253                                         curpacket->packet->length +=
254                                                 (curchar << 16);
255                                         rc = getchar_func(ctx, 1, &curchar);
256                                         curpacket->packet->length +=
257                                                 (curchar << 8);
258                                         rc = getchar_func(ctx, 1, &curchar);
259                                         curpacket->packet->length += curchar;
260                                         break;
261                                 case 3:
262                                         fprintf(stderr, "Unsupported length type 3.\n");
263                                         break;
264                                 }
265                         }
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);
271                         inpacket = false;
272                 } else {
273                         fprintf(stderr, "Unexpected character: 0x%X\n",
274                                 curchar);
275                 }
276         }
277
278         return (rc);
279 }
280
281 /**
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.
286  *
287  *      This function uses putchar_func to write characters to an OpenPGP
288  *      packet stream from a linked list of packets.
289  */
290 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
291                                                 unsigned char *c),
292                                 void *ctx,
293                                 struct openpgp_packet_list *packets)
294 {
295         unsigned char   curchar = 0;
296
297         while (packets != NULL) {
298                 curchar = 0x80;
299                 if (packets->packet->newformat) {
300                         curchar |= 0x40;
301                         curchar |= packets->packet->tag;
302                         putchar_func(ctx, 1, &curchar);
303
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) &
310                                          0xFF00) >> 8) + 192;
311                                 putchar_func(ctx, 1, &curchar);
312
313                                 curchar = (packets->packet->length - 192) &
314                                          0xFF;
315                                 putchar_func(ctx, 1, &curchar);
316                         } else {
317                                 fputs("Unsupported new format length.\n", stderr);
318                         }
319                 } else {
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) {
326                                 curchar |= 1;
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);
332                         } else {
333                                 curchar |= 2;
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);
343                         }
344                 }
345
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]);
349 //              }
350                 packets = packets->next;
351         }
352         return 0;
353 }
354
355 /**
356  *      flatten_publickey - Convert a publickey to an OpenPGP packet list.
357  *      @key: The public key.
358  *      @packets: The outputted packet list.
359  *
360  *      This function converts public key structure to a linked list of OpenPGP
361  *      packets ready for outputing or storage.
362  */
363 int flatten_publickey(struct openpgp_publickey *key,
364                         struct openpgp_packet_list **packets,
365                         struct openpgp_packet_list **list_end)
366 {
367         struct openpgp_signedpacket_list        *tmpsignedlist = NULL;
368         struct openpgp_packet_list              *tmplist = NULL;
369
370         while (key != NULL) {
371                 /*
372                  * First write the public key packet out.
373                  */
374                 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
375                 if (*packets == NULL) {
376                         *packets = *list_end;
377                 }
378
379                 /*
380                  * Now do any revocation signatures on the main key.
381                  */
382                 for (tmplist = key->revocations; tmplist != NULL;
383                                 tmplist = tmplist->next) {
384                         ADD_PACKET_TO_LIST((*list_end),
385                                         packet_dup(tmplist->packet));
386                 }
387
388                 /*
389                  * Output any UIDs along with their signatures.
390                  */
391                 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
392                                 tmpsignedlist = tmpsignedlist->next) {
393
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));
400                         }
401                 }
402
403                 /*
404                  * Output any subkeys along with their signatures.
405                  */
406                 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
407                                 tmpsignedlist = tmpsignedlist->next) {
408
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));
415                         }
416                 }
417                 key = key->next;
418         }
419         return 0;
420 }