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