]> git.sommitrealweird.co.uk Git - onak.git/blob - parsekey.c
4ec7ef3338a43d2d7a02502bbe02ab6f5d7d39cd
[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                                         curpacket->packet->length = 0;
264                                         curpacket->packet->data = NULL;
265                                         rc = -1;
266                                         break;
267                                 }
268                         }
269
270                         if (rc == 0) {
271                                 curpacket->packet->data =
272                                         malloc(curpacket->packet->length *
273                                         sizeof(unsigned char));
274                                 rc = getchar_func(ctx,
275                                         curpacket->packet->length,
276                                         curpacket->packet->data);
277                         }
278                         inpacket = false;
279                 } else {
280                         fprintf(stderr, "Unexpected character: 0x%X\n",
281                                 curchar);
282                 }
283         }
284
285         return (rc);
286 }
287
288 /**
289  *      write_openpgp_stream - Reads a stream of OpenPGP packets.
290  *      @putchar_func: The function to put the next character to the stream.
291  *      @ctx: A pointer to the context structure for putchar_func.
292  *      @packets: The list of packets.
293  *
294  *      This function uses putchar_func to write characters to an OpenPGP
295  *      packet stream from a linked list of packets.
296  */
297 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
298                                                 unsigned char *c),
299                                 void *ctx,
300                                 struct openpgp_packet_list *packets)
301 {
302         unsigned char   curchar = 0;
303
304         while (packets != NULL) {
305                 curchar = 0x80;
306                 if (packets->packet->newformat) {
307                         curchar |= 0x40;
308                         curchar |= packets->packet->tag;
309                         putchar_func(ctx, 1, &curchar);
310
311                         if (packets->packet->length < 192) {
312                                 curchar = packets->packet->length;
313                                 putchar_func(ctx, 1, &curchar);
314                         } else if (packets->packet->length > 191 &&
315                                 packets->packet->length < 8383) {
316                                 curchar = (((packets->packet->length - 192) &
317                                          0xFF00) >> 8) + 192;
318                                 putchar_func(ctx, 1, &curchar);
319
320                                 curchar = (packets->packet->length - 192) &
321                                          0xFF;
322                                 putchar_func(ctx, 1, &curchar);
323                         } else {
324                                 fputs("Unsupported new format length.\n", stderr);
325                         }
326                 } else {
327                         curchar |= (packets->packet->tag << 2);
328                         if (packets->packet->length < 256) {
329                                 putchar_func(ctx, 1, &curchar);
330                                 curchar = packets->packet->length;
331                                 putchar_func(ctx, 1, &curchar);
332                         } else if (packets->packet->length < 0x10000) {
333                                 curchar |= 1;
334                                 putchar_func(ctx, 1, &curchar);
335                                 curchar = packets->packet->length >> 8;
336                                 putchar_func(ctx, 1, &curchar);
337                                 curchar = packets->packet->length & 0xFF;
338                                 putchar_func(ctx, 1, &curchar);
339                         } else {
340                                 curchar |= 2;
341                                 putchar_func(ctx, 1, &curchar);
342                                 curchar = packets->packet->length >> 24;
343                                 putchar_func(ctx, 1, &curchar);
344                                 curchar = (packets->packet->length >> 16) & 0xFF;
345                                 putchar_func(ctx, 1, &curchar);
346                                 curchar = (packets->packet->length >> 8) & 0xFF;
347                                 putchar_func(ctx, 1, &curchar);
348                                 curchar = packets->packet->length & 0xFF;
349                                 putchar_func(ctx, 1, &curchar);
350                         }
351                 }
352
353                 putchar_func(ctx, packets->packet->length, packets->packet->data);
354 //              for (i = 0; i < packets->packet->length; i++) {
355 //                      putchar_func(ctx, packets->packet->data[i]);
356 //              }
357                 packets = packets->next;
358         }
359         return 0;
360 }
361
362 /**
363  *      flatten_publickey - Convert a publickey to an OpenPGP packet list.
364  *      @key: The public key.
365  *      @packets: The outputted packet list.
366  *
367  *      This function converts public key structure to a linked list of OpenPGP
368  *      packets ready for outputing or storage.
369  */
370 int flatten_publickey(struct openpgp_publickey *key,
371                         struct openpgp_packet_list **packets,
372                         struct openpgp_packet_list **list_end)
373 {
374         struct openpgp_signedpacket_list        *tmpsignedlist = NULL;
375         struct openpgp_packet_list              *tmplist = NULL;
376
377         while (key != NULL) {
378                 /*
379                  * First write the public key packet out.
380                  */
381                 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
382                 if (*packets == NULL) {
383                         *packets = *list_end;
384                 }
385
386                 /*
387                  * Now do any revocation signatures on the main key.
388                  */
389                 for (tmplist = key->revocations; tmplist != NULL;
390                                 tmplist = tmplist->next) {
391                         ADD_PACKET_TO_LIST((*list_end),
392                                         packet_dup(tmplist->packet));
393                 }
394
395                 /*
396                  * Output any UIDs along with their signatures.
397                  */
398                 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
399                                 tmpsignedlist = tmpsignedlist->next) {
400
401                         ADD_PACKET_TO_LIST((*list_end),
402                                 packet_dup(tmpsignedlist->packet));
403                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
404                                         tmplist = tmplist->next) {
405                                 ADD_PACKET_TO_LIST((*list_end), 
406                                         packet_dup(tmplist->packet));
407                         }
408                 }
409
410                 /*
411                  * Output any subkeys along with their signatures.
412                  */
413                 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
414                                 tmpsignedlist = tmpsignedlist->next) {
415
416                         ADD_PACKET_TO_LIST((*list_end),
417                                 packet_dup(tmpsignedlist->packet));
418                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
419                                         tmplist = tmplist->next) {
420                                 ADD_PACKET_TO_LIST((*list_end), 
421                                         packet_dup(tmplist->packet));
422                         }
423                 }
424                 key = key->next;
425         }
426         return 0;
427 }