Define OpenPGP constants and use them rather than magic numbers
[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 <stdbool.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "keyid.h"
15 #include "keystructs.h"
16 #include "ll.h"
17 #include "log.h"
18 #include "mem.h"
19 #include "openpgp.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 OPENPGP_PACKET_SIGNATURE:
50                         /*
51                          * It's a signature packet. Add it to either the public
52                          * key, to the current UID or the current subkey.
53                          */
54                         log_assert(curkey != NULL);
55                         if (curkey->subkeys != NULL) {
56                                 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
57                                         sig,
58                                         packet_dup(packets->packet));
59                         } else if (curkey->uids != NULL) {
60                                 ADD_PACKET_TO_LIST_END(curkey->last_uid,
61                                         sig,
62                                         packet_dup(packets->packet));
63                         } else {
64                                 ADD_PACKET_TO_LIST_END(curkey,
65                                         sig,
66                                         packet_dup(packets->packet));
67                                 /*
68                                  * This is a signature on the public key; check
69                                  * if it's a revocation.
70                                  */
71                                 if (packets->packet->data[0] == 3 &&
72                                         packets->packet->data[2] ==
73                                                 OPENPGP_SIGTYPE_KEY_REV) {
74                                         /*
75                                          * Type 3 key, 0x20 == revocation
76                                          */
77                                         curkey->revoked = true;
78                                 } else if (packets->packet->data[0] == 4 &&
79                                         packets->packet->data[1] ==
80                                                 OPENPGP_SIGTYPE_KEY_REV) {
81                                         /*
82                                          * Type 4 key, 0x20 == revocation
83                                          */
84                                         curkey->revoked = true;
85                                 }
86                         }
87                         break;
88                 case OPENPGP_PACKET_PUBLICKEY:
89                         /*
90                          * It's a public key packet, so start a new key in our
91                          * list.
92                          */
93                         if (curkey != NULL) {
94                                 curkey->next = malloc(sizeof (*curkey));
95                                 curkey = curkey->next;
96                         } else {
97                                 *keys = curkey =
98                                         malloc(sizeof (*curkey));
99                         }
100                         memset(curkey, 0, sizeof(*curkey));
101                         curkey->publickey = packet_dup(packets->packet);
102                         count++;
103                         break;
104                 case OPENPGP_PACKET_UID:
105                 case OPENPGP_PACKET_UAT:
106                         /*
107                          * It's a UID packet (or a photo id, which is similar).
108                          */
109                         log_assert(curkey != NULL);
110                         log_assert(curkey->subkeys == NULL);
111                         ADD_PACKET_TO_LIST_END(curkey,
112                                 uid,
113                                 packet_dup(packets->packet));
114                         break;
115                 case OPENPGP_PACKET_PUBLICSUBKEY:
116                         /*
117                          * It's a subkey packet.
118                          */
119                         log_assert(curkey != NULL);
120                         ADD_PACKET_TO_LIST_END(curkey,
121                                 subkey,
122                                 packet_dup(packets->packet));
123                         break;
124                 case OPENPGP_PACKET_TRUST:
125                 case 61:
126                         /*
127                          * One of:
128                          *
129                          * Trust packet. Ignore.
130                          * Comment packet. Ignore.
131                          */
132                         break;
133                 default:
134                         logthing(LOGTHING_ERROR,
135                                         "Unsupported packet type: %d",
136                                         packets->packet->tag);
137                 }
138                 packets = packets->next;
139         }
140
141         return count;
142 }
143
144 /**
145  *      debug_packet - Print debug info about a packet
146  *      @packet: The packet to display.
147  *
148  *      This function takes an OpenPGP packet and displays some information
149  *      about it to stdout. Useful for debugging purposes or curiousity about
150  *      an OpenPGP packet stream.
151  */
152 int debug_packet(struct openpgp_packet *packet)
153 {
154         printf("\tNew format: %d, Tag: %u, Length: %zd\n",
155                         packet->newformat,
156                         packet->tag,
157                         packet->length);
158
159         return 0;
160 }
161
162 /**
163  *      read_openpgp_stream - Reads a stream of OpenPGP packets.
164  *      @getchar_func: The function to get the next character from the stream.
165  *      @ctx: A pointer to the context structure for getchar_func.
166  *      @packets: The outputted list of packets.
167  *      @maxnum: The maximum number of keys to read. 0 means unlimited.
168  *
169  *      This function uses getchar_func to read characters from an OpenPGP
170  *      packet stream and reads the packets into a linked list of packets
171  *      ready for parsing as a public key or whatever.
172  */
173 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
174                                 void *c),
175                                 void *ctx,
176                                 struct openpgp_packet_list **packets,
177                                 int maxnum)
178 {
179         unsigned char                    curchar = 0;
180         struct openpgp_packet_list      *curpacket = NULL;
181         int                              rc = 0;
182         int                              keys = 0;
183         bool                             inpacket = false;
184
185         log_assert(packets != NULL);
186         curpacket = *packets;
187         if (curpacket != NULL) {
188                 while (curpacket->next != NULL) {
189                         curpacket = curpacket->next;
190                 }
191         }
192
193         while (!rc && (maxnum == 0 || keys < maxnum) &&
194                         !getchar_func(ctx, 1, &curchar)) {
195                 if (!inpacket && (curchar & 0x80)) {
196                         /*
197                          * New packet. Record the fact we're in a packet and
198                          * allocate memory for it.
199                          */
200                         inpacket = true;
201                         if (curpacket != NULL) {
202                                 curpacket->next = malloc(sizeof (*curpacket));
203                                 curpacket = curpacket->next;
204                         } else {
205                                 *packets = curpacket =
206                                         malloc(sizeof (*curpacket));
207                         }
208                         memset(curpacket, 0, sizeof(*curpacket));
209                         curpacket->packet =
210                                 malloc(sizeof (*curpacket->packet));
211                         memset(curpacket->packet, 0,
212                                         sizeof(*curpacket->packet));
213
214                         curpacket->packet->newformat = (curchar & 0x40);
215
216                         /*
217                          * TODO: Better error checking on getchar_func.
218                          */
219                         if (curpacket->packet->newformat) {
220                                 curpacket->packet->tag = (curchar & 0x3F);
221                                 rc = getchar_func(ctx, 1, &curchar);
222                                 curpacket->packet->length = curchar;
223                                 if (curpacket->packet->length > 191 &&
224                                         curpacket->packet->length < 224) {
225                                         rc = getchar_func(ctx, 1, &curchar);
226                                         curpacket->packet->length -= 192;
227                                         curpacket->packet->length <<= 8;
228                                         curpacket->packet->length += curchar;
229                                         curpacket->packet->length += 192;
230                                 } else if (curpacket->packet->length > 223 &&
231                                         curpacket->packet->length < 255) {
232                                         logthing(LOGTHING_NOTICE,
233                                                 "Partial length;"
234                                                 " not supported.");
235                                 } else if (curpacket->packet->length == 255) {
236                                         /*
237                                          * 5 byte length; ie 255 followed by 3
238                                          * bytes of MSB length.
239                                          */
240                                         rc = getchar_func(ctx, 1, &curchar);
241                                         curpacket->packet->length = curchar;
242                                         curpacket->packet->length <<= 8;
243                                         rc = getchar_func(ctx, 1, &curchar);
244                                         curpacket->packet->length += curchar;
245                                         curpacket->packet->length <<= 8;
246                                         rc = getchar_func(ctx, 1, &curchar);
247                                         curpacket->packet->length += curchar;
248                                         curpacket->packet->length <<= 8;
249                                         rc = getchar_func(ctx, 1, &curchar);
250                                         curpacket->packet->length += curchar;
251                                 }
252                         } else {
253                                 curpacket->packet->tag = (curchar & 0x3C) >> 2;
254                                 switch (curchar & 3) {
255                                 case 0:
256                                         rc = getchar_func(ctx, 1, &curchar);
257                                         curpacket->packet->length = curchar;
258                                         break;
259                                 case 1:
260                                         rc = getchar_func(ctx, 1, &curchar);
261                                         curpacket->packet->length = curchar;
262                                         curpacket->packet->length <<= 8;
263                                         rc = getchar_func(ctx, 1, &curchar);
264                                         curpacket->packet->length += curchar;
265                                         break;
266                                 case 2:
267                                         rc = getchar_func(ctx, 1, &curchar);
268                                         curpacket->packet->length = 
269                                                 (curchar << 24);
270                                         rc = getchar_func(ctx, 1, &curchar);
271                                         curpacket->packet->length +=
272                                                 (curchar << 16);
273                                         rc = getchar_func(ctx, 1, &curchar);
274                                         curpacket->packet->length +=
275                                                 (curchar << 8);
276                                         rc = getchar_func(ctx, 1, &curchar);
277                                         curpacket->packet->length += curchar;
278                                         break;
279                                 case 3:
280                                         logthing(LOGTHING_ERROR,
281                                                 "Unsupported length type 3.");
282                                         curpacket->packet->length = 0;
283                                         curpacket->packet->data = NULL;
284                                         rc = -1;
285                                         break;
286                                 }
287                         }
288
289                         if (rc == 0) {
290                                 if (curpacket->packet->tag ==
291                                                 OPENPGP_PACKET_PUBLICKEY) {
292                                         keys++;
293                                 }
294                                 curpacket->packet->data =
295                                         malloc(curpacket->packet->length *
296                                         sizeof(unsigned char));
297                                 if (curpacket->packet->data == NULL) {
298                                         logthing(LOGTHING_ERROR, 
299                                                 "Can't allocate memory for "
300                                                 "packet!");
301                                         rc = -1;
302                                 } else {
303                                         rc = getchar_func(ctx,
304                                                 curpacket->packet->length,
305                                                 curpacket->packet->data);
306                                 }
307                         }
308                         inpacket = false;
309                 } else {
310                         logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
311                                 curchar);
312                         rc = 1;
313                 }
314         }
315
316         return (rc);
317 }
318
319 /**
320  *      write_openpgp_stream - Reads a stream of OpenPGP packets.
321  *      @putchar_func: The function to put the next character to the stream.
322  *      @ctx: A pointer to the context structure for putchar_func.
323  *      @packets: The list of packets.
324  *
325  *      This function uses putchar_func to write characters to an OpenPGP
326  *      packet stream from a linked list of packets.
327  */
328 int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
329                                                 void *c),
330                                 void *ctx,
331                                 struct openpgp_packet_list *packets)
332 {
333         unsigned char   curchar = 0;
334
335         while (packets != NULL) {
336                 curchar = 0x80;
337                 if (packets->packet->newformat) {
338                         curchar |= 0x40;
339                         curchar |= packets->packet->tag;
340                         putchar_func(ctx, 1, &curchar);
341
342                         if (packets->packet->length < 192) {
343                                 curchar = packets->packet->length;
344                                 putchar_func(ctx, 1, &curchar);
345                         } else if (packets->packet->length > 191 &&
346                                 packets->packet->length < 8383) {
347                                 curchar = (((packets->packet->length - 192) &
348                                          0xFF00) >> 8) + 192;
349                                 putchar_func(ctx, 1, &curchar);
350
351                                 curchar = (packets->packet->length - 192) &
352                                          0xFF;
353                                 putchar_func(ctx, 1, &curchar);
354                         } else if (packets->packet->length > 8382 &&
355                                 packets->packet->length < 0xFFFFFFFF) {
356                                 logthing(LOGTHING_DEBUG,
357                                         "Writing 5 byte length");
358                                 curchar = 255;
359                                 putchar_func(ctx, 1, &curchar);
360                                 
361                                 curchar = (packets->packet->length >> 24);
362                                 curchar &= 0xFF;
363                                 putchar_func(ctx, 1, &curchar);
364                                 
365                                 curchar = (packets->packet->length >> 16);
366                                 curchar &= 0xFF;
367                                 putchar_func(ctx, 1, &curchar);
368                                 
369                                 curchar = (packets->packet->length >> 8);
370                                 curchar &= 0xFF;
371                                 putchar_func(ctx, 1, &curchar);
372                                 
373                                 curchar = packets->packet->length;
374                                 curchar &= 0xFF;
375                                 putchar_func(ctx, 1, &curchar);
376                         } else {
377                                 logthing(LOGTHING_ERROR,
378                                         "Unsupported new format length.");
379                         }
380                 } else {
381                         curchar |= (packets->packet->tag << 2);
382                         if (packets->packet->length < 256) {
383                                 putchar_func(ctx, 1, &curchar);
384                                 curchar = packets->packet->length;
385                                 putchar_func(ctx, 1, &curchar);
386                         } else if (packets->packet->length < 0x10000) {
387                                 curchar |= 1;
388                                 putchar_func(ctx, 1, &curchar);
389                                 curchar = packets->packet->length >> 8;
390                                 putchar_func(ctx, 1, &curchar);
391                                 curchar = packets->packet->length & 0xFF;
392                                 putchar_func(ctx, 1, &curchar);
393                         } else {
394                                 curchar |= 2;
395                                 putchar_func(ctx, 1, &curchar);
396                                 curchar = packets->packet->length >> 24;
397                                 putchar_func(ctx, 1, &curchar);
398                                 curchar = (packets->packet->length >> 16) & 0xFF;
399                                 putchar_func(ctx, 1, &curchar);
400                                 curchar = (packets->packet->length >> 8) & 0xFF;
401                                 putchar_func(ctx, 1, &curchar);
402                                 curchar = packets->packet->length & 0xFF;
403                                 putchar_func(ctx, 1, &curchar);
404                         }
405                 }
406
407                 putchar_func(ctx, packets->packet->length,
408                                 packets->packet->data);
409                 packets = packets->next;
410         }
411         return 0;
412 }
413
414 /**
415  *      flatten_publickey - Convert a publickey to an OpenPGP packet list.
416  *      @key: The public key.
417  *      @packets: The outputted packet list.
418  *
419  *      This function converts public key structure to a linked list of OpenPGP
420  *      packets ready for outputing or storage.
421  */
422 int flatten_publickey(struct openpgp_publickey *key,
423                         struct openpgp_packet_list **packets,
424                         struct openpgp_packet_list **list_end)
425 {
426         struct openpgp_signedpacket_list        *tmpsignedlist = NULL;
427         struct openpgp_packet_list              *tmplist = NULL;
428
429         while (key != NULL) {
430                 /*
431                  * First write the public key packet out.
432                  */
433                 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
434                 if (*packets == NULL) {
435                         *packets = *list_end;
436                 }
437
438                 /*
439                  * Now do any signatures on the main key.
440                  */
441                 for (tmplist = key->sigs; tmplist != NULL;
442                                 tmplist = tmplist->next) {
443                         ADD_PACKET_TO_LIST((*list_end),
444                                         packet_dup(tmplist->packet));
445                 }
446
447                 /*
448                  * Output any UIDs along with their signatures.
449                  */
450                 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
451                                 tmpsignedlist = tmpsignedlist->next) {
452
453                         ADD_PACKET_TO_LIST((*list_end),
454                                 packet_dup(tmpsignedlist->packet));
455                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
456                                         tmplist = tmplist->next) {
457                                 ADD_PACKET_TO_LIST((*list_end), 
458                                         packet_dup(tmplist->packet));
459                         }
460                 }
461
462                 /*
463                  * Output any subkeys along with their signatures.
464                  */
465                 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
466                                 tmpsignedlist = tmpsignedlist->next) {
467
468                         ADD_PACKET_TO_LIST((*list_end),
469                                 packet_dup(tmpsignedlist->packet));
470                         for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
471                                         tmplist = tmplist->next) {
472                                 ADD_PACKET_TO_LIST((*list_end), 
473                                         packet_dup(tmplist->packet));
474                         }
475                 }
476                 key = key->next;
477         }
478         return 0;
479 }