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