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