]> the.earth.li Git - onak.git/blob - parsekey.c
Add subkey support to keydb_fs backend.
[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  *      parse_keys - Process a stream of packets for public keys + sigs.
24  *      @packets: The packet list to parse.
25  *      @keys: The returned list of public keys.
26  *
27  *      This function takes an list of OpenPGP packets and attempts to parse it
28  *      into a list of public keys with signatures and subkeys.
29  *
30  *      Returns a count of how many keys we parsed.
31  */
32 int parse_keys(struct openpgp_packet_list *packets,
33                 struct openpgp_publickey **keys)
34 {
35         struct openpgp_publickey *curkey = NULL;
36         int count;
37
38         count = 0;
39
40         /*
41          * If keys already has some keys in it then set curkey to the last one
42          * so we add to the end of the list.
43          */
44         for (curkey = *keys; curkey != NULL && curkey->next != NULL;
45                         curkey = curkey->next) ;
46
47         while (packets != NULL) {
48                 switch (packets->packet->tag) {
49                 case 2:
50                         /*
51                          * It's a signature packet. Add it to either the public
52                          * key (it should be a revocation), to the current UID
53                          * or the current subkey.
54                          */
55                         assert(curkey != NULL);
56                         if (curkey->subkeys != NULL) {
57                                 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
58                                         sig,
59                                         packet_dup(packets->packet));
60                         } else if (curkey->uids != NULL) {
61                                 ADD_PACKET_TO_LIST_END(curkey->last_uid,
62                                         sig,
63                                         packet_dup(packets->packet));
64                         } else {
65                                 ADD_PACKET_TO_LIST_END(curkey,
66                                         revocation,
67                                         packet_dup(packets->packet));
68                         }
69                         break;
70                 case 6:
71                         /*
72                          * It's a public key packet, so start a new key in our
73                          * list.
74                          */
75                         if (curkey != NULL) {
76                                 curkey->next = malloc(sizeof (*curkey));
77                                 curkey = curkey->next;
78                         } else {
79                                 *keys = curkey =
80                                         malloc(sizeof (*curkey));
81                         }
82                         memset(curkey, 0, sizeof(*curkey));
83                         curkey->publickey = packet_dup(packets->packet);
84                         count++;
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                 case 12:
107                 case 61:
108                         /*
109                          * One of:
110                          *
111                          * Trust packet. Ignore.
112                          * Comment packet. Ignore.
113                          */
114                         break;
115                 default:
116                         logthing(LOGTHING_ERROR,
117                                         "Unsupported packet type: %d",
118                                         packets->packet->tag);
119                 }
120                 packets = packets->next;
121         }
122
123         return count;
124 }
125
126 /**
127  *      debug_packet - Print debug info about a packet
128  *      @packet: The packet to display.
129  *
130  *      This function takes an OpenPGP packet and displays some information
131  *      about it to stdout. Useful for debugging purposes or curiousity about
132  *      an OpenPGP packet stream.
133  */
134 int debug_packet(struct openpgp_packet *packet)
135 {
136         printf("\tNew format: %d, Tag: %u, Length: %d\n",
137                         packet->newformat,
138                         packet->tag,
139                         packet->length);
140
141         return 0;
142 }
143
144 /**
145  *      read_openpgp_stream - Reads a stream of OpenPGP packets.
146  *      @getchar_func: The function to get the next character from the stream.
147  *      @ctx: A pointer to the context structure for getchar_func.
148  *      @packets: The outputted list of packets.
149  *      @maxnum: The maximum number of keys to read. 0 means unlimited.
150  *
151  *      This function uses getchar_func to read characters from an OpenPGP
152  *      packet stream and reads the packets into a linked list of packets
153  *      ready for parsing as a public key or whatever.
154  */
155 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
156                                 unsigned char *c),
157                                 void *ctx,
158                                 struct openpgp_packet_list **packets,
159                                 int maxnum)
160 {
161         unsigned char                    curchar = 0;
162         struct openpgp_packet_list      *curpacket = NULL;
163         int                              rc = 0;
164         int                              keys = 0;
165         bool                             inpacket = false;
166
167         assert(packets != NULL);
168         curpacket = *packets;
169         if (curpacket != NULL) {
170                 while (curpacket->next != NULL) {
171                         curpacket = curpacket->next;
172                 }
173         }
174
175         while (!rc && (maxnum == 0 || keys < maxnum) &&
176                         !getchar_func(ctx, 1, &curchar)) {
177                 if (!inpacket && (curchar & 0x80)) {
178                         /*
179                          * New packet. Record the fact we're in a packet and
180                          * allocate memory for it.
181                          */
182                         inpacket = true;
183                         if (curpacket != NULL) {
184                                 curpacket->next = malloc(sizeof (*curpacket));
185                                 curpacket = curpacket->next;
186                         } else {
187                                 *packets = curpacket =
188                                         malloc(sizeof (*curpacket));
189                         }
190                         memset(curpacket, 0, sizeof(*curpacket));
191                         curpacket->packet =
192                                 malloc(sizeof (*curpacket->packet));
193                         memset(curpacket->packet, 0,
194                                         sizeof(*curpacket->packet));
195
196                         curpacket->packet->newformat = (curchar & 0x40);
197
198                         /*
199                          * TODO: Better error checking on getchar_func.
200                          */
201                         if (curpacket->packet->newformat) {
202                                 curpacket->packet->tag = (curchar & 0x3F);
203                                 rc = getchar_func(ctx, 1, &curchar);
204                                 curpacket->packet->length = curchar;
205                                 if (curpacket->packet->length > 191 &&
206                                         curpacket->packet->length < 224) {
207                                         rc = getchar_func(ctx, 1, &curchar);
208                                         curpacket->packet->length -= 192;
209                                         curpacket->packet->length <<= 8;
210                                         curpacket->packet->length += curchar;
211                                         curpacket->packet->length += 192;
212                                 } else if (curpacket->packet->length > 223 &&
213                                         curpacket->packet->length < 255) {
214                                         logthing(LOGTHING_NOTICE,
215                                                 "Partial length;"
216                                                 " not supported.");
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                                         logthing(LOGTHING_ERROR,
263                                                 "Unsupported length type 3.");
264                                         curpacket->packet->length = 0;
265                                         curpacket->packet->data = NULL;
266                                         rc = -1;
267                                         break;
268                                 }
269                         }
270
271                         if (rc == 0) {
272                                 if (curpacket->packet->tag == 6) {
273                                         keys++;
274                                 }
275                                 curpacket->packet->data =
276                                         malloc(curpacket->packet->length *
277                                         sizeof(unsigned char));
278                                 if (curpacket->packet->data == NULL) {
279                                         logthing(LOGTHING_ERROR, 
280                                                 "Can't allocate memory for "
281                                                 "packet!");
282                                         rc = -1;
283                                 } else {
284                                         rc = getchar_func(ctx,
285                                                 curpacket->packet->length,
286                                                 curpacket->packet->data);
287                                 }
288                         }
289                         inpacket = false;
290                 } else {
291                         logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
292                                 curchar);
293                         rc = 1;
294                 }
295         }
296
297         return (rc);
298 }
299
300 /**
301  *      write_openpgp_stream - Reads a stream of OpenPGP packets.
302  *      @putchar_func: The function to put the next character to the stream.
303  *      @ctx: A pointer to the context structure for putchar_func.
304  *      @packets: The list of packets.
305  *
306  *      This function uses putchar_func to write characters to an OpenPGP
307  *      packet stream from a linked list of packets.
308  */
309 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
310                                                 unsigned char *c),
311                                 void *ctx,
312                                 struct openpgp_packet_list *packets)
313 {
314         unsigned char   curchar = 0;
315
316         while (packets != NULL) {
317                 curchar = 0x80;
318                 if (packets->packet->newformat) {
319                         curchar |= 0x40;
320                         curchar |= packets->packet->tag;
321                         putchar_func(ctx, 1, &curchar);
322
323                         if (packets->packet->length < 192) {
324                                 curchar = packets->packet->length;
325                                 putchar_func(ctx, 1, &curchar);
326                         } else if (packets->packet->length > 191 &&
327                                 packets->packet->length < 8383) {
328                                 curchar = (((packets->packet->length - 192) &
329                                          0xFF00) >> 8) + 192;
330                                 putchar_func(ctx, 1, &curchar);
331
332                                 curchar = (packets->packet->length - 192) &
333                                          0xFF;
334                                 putchar_func(ctx, 1, &curchar);
335                         } else if (packets->packet->length > 8382 &&
336                                 packets->packet->length < 0xFFFFFFFF) {
337                                 logthing(LOGTHING_DEBUG,
338                                         "Writing 5 byte length");
339                                 curchar = 255;
340                                 putchar_func(ctx, 1, &curchar);
341                                 
342                                 curchar = (packets->packet->length >> 24);
343                                 curchar &= 0xFF;
344                                 putchar_func(ctx, 1, &curchar);
345                                 
346                                 curchar = (packets->packet->length >> 16);
347                                 curchar &= 0xFF;
348                                 putchar_func(ctx, 1, &curchar);
349                                 
350                                 curchar = (packets->packet->length >> 8);
351                                 curchar &= 0xFF;
352                                 putchar_func(ctx, 1, &curchar);
353                                 
354                                 curchar = packets->packet->length;
355                                 curchar &= 0xFF;
356                                 putchar_func(ctx, 1, &curchar);
357                         } else {
358                                 logthing(LOGTHING_ERROR,
359                                         "Unsupported new format length.");
360                         }
361                 } else {
362                         curchar |= (packets->packet->tag << 2);
363                         if (packets->packet->length < 256) {
364                                 putchar_func(ctx, 1, &curchar);
365                                 curchar = packets->packet->length;
366                                 putchar_func(ctx, 1, &curchar);
367                         } else if (packets->packet->length < 0x10000) {
368                                 curchar |= 1;
369                                 putchar_func(ctx, 1, &curchar);
370                                 curchar = packets->packet->length >> 8;
371                                 putchar_func(ctx, 1, &curchar);
372                                 curchar = packets->packet->length & 0xFF;
373                                 putchar_func(ctx, 1, &curchar);
374                         } else {
375                                 curchar |= 2;
376                                 putchar_func(ctx, 1, &curchar);
377                                 curchar = packets->packet->length >> 24;
378                                 putchar_func(ctx, 1, &curchar);
379                                 curchar = (packets->packet->length >> 16) & 0xFF;
380                                 putchar_func(ctx, 1, &curchar);
381                                 curchar = (packets->packet->length >> 8) & 0xFF;
382                                 putchar_func(ctx, 1, &curchar);
383                                 curchar = packets->packet->length & 0xFF;
384                                 putchar_func(ctx, 1, &curchar);
385                         }
386                 }
387
388                 putchar_func(ctx, packets->packet->length,
389                                 packets->packet->data);
390                 packets = packets->next;
391         }
392         return 0;
393 }
394
395 /**
396  *      flatten_publickey - Convert a publickey to an OpenPGP packet list.
397  *      @key: The public key.
398  *      @packets: The outputted packet list.
399  *
400  *      This function converts public key structure to a linked list of OpenPGP
401  *      packets ready for outputing or storage.
402  */
403 int flatten_publickey(struct openpgp_publickey *key,
404                         struct openpgp_packet_list **packets,
405                         struct openpgp_packet_list **list_end)
406 {
407         struct openpgp_signedpacket_list        *tmpsignedlist = NULL;
408         struct openpgp_packet_list              *tmplist = NULL;
409
410         while (key != NULL) {
411                 /*
412                  * First write the public key packet out.
413                  */
414                 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
415                 if (*packets == NULL) {
416                         *packets = *list_end;
417                 }
418
419                 /*
420                  * Now do any revocation signatures on the main key.
421                  */
422                 for (tmplist = key->revocations; tmplist != NULL;
423                                 tmplist = tmplist->next) {
424                         ADD_PACKET_TO_LIST((*list_end),
425                                         packet_dup(tmplist->packet));
426                 }
427
428                 /*
429                  * Output any UIDs along with their signatures.
430                  */
431                 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
432                                 tmpsignedlist = tmpsignedlist->next) {
433
434                         ADD_PACKET_TO_LIST((*list_end),
435                                 packet_dup(tmpsignedlist->packet));
436                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
437                                         tmplist = tmplist->next) {
438                                 ADD_PACKET_TO_LIST((*list_end), 
439                                         packet_dup(tmplist->packet));
440                         }
441                 }
442
443                 /*
444                  * Output any subkeys along with their signatures.
445                  */
446                 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
447                                 tmpsignedlist = tmpsignedlist->next) {
448
449                         ADD_PACKET_TO_LIST((*list_end),
450                                 packet_dup(tmpsignedlist->packet));
451                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
452                                         tmplist = tmplist->next) {
453                                 ADD_PACKET_TO_LIST((*list_end), 
454                                         packet_dup(tmplist->packet));
455                         }
456                 }
457                 key = key->next;
458         }
459         return 0;
460 }