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