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