2 * parsekey.c - Routines to parse an OpenPGP key.
4 * Copyright 2002-2004,2007-2008,2011 Jonathan McDowell <noodles@earth.li>
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.
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
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.
26 #include "keystructs.h"
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.
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.
41 * Returns a count of how many keys we parsed.
43 int parse_keys(struct openpgp_packet_list *packets,
44 struct openpgp_publickey **keys)
46 struct openpgp_publickey *curkey = NULL;
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.
55 for (curkey = *keys; curkey != NULL && curkey->next != NULL;
56 curkey = curkey->next) ;
58 while (packets != NULL) {
59 switch (packets->packet->tag) {
60 case OPENPGP_PACKET_SIGNATURE:
62 * It's a signature packet. Add it to either the public
63 * key, to the current UID or the current subkey.
66 return ONAK_E_INVALID_PARAM;
67 if (curkey->subkeys != NULL) {
68 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
70 packet_dup(packets->packet));
71 } else if (curkey->uids != NULL) {
72 ADD_PACKET_TO_LIST_END(curkey->last_uid,
74 packet_dup(packets->packet));
76 ADD_PACKET_TO_LIST_END(curkey,
78 packet_dup(packets->packet));
80 * This is a signature on the public key; check
81 * if it's a revocation.
83 if (packets->packet->data[0] == 3 &&
84 packets->packet->data[2] ==
85 OPENPGP_SIGTYPE_KEY_REV) {
87 * Type 3 key, 0x20 == revocation
89 curkey->revoked = true;
90 } else if (packets->packet->data[0] == 4 &&
91 packets->packet->data[1] ==
92 OPENPGP_SIGTYPE_KEY_REV) {
94 * Type 4 key, 0x20 == revocation
96 curkey->revoked = true;
100 case OPENPGP_PACKET_PUBLICKEY:
102 * It's a public key packet, so start a new key in our
105 if (curkey != NULL) {
106 curkey->next = malloc(sizeof (*curkey));
107 curkey = curkey->next;
110 malloc(sizeof (*curkey));
112 memset(curkey, 0, sizeof(*curkey));
113 curkey->publickey = packet_dup(packets->packet);
116 case OPENPGP_PACKET_UID:
117 case OPENPGP_PACKET_UAT:
119 * It's a UID packet (or a photo id, which is similar).
122 return ONAK_E_INVALID_PARAM;
123 if (curkey->subkeys != NULL)
124 return ONAK_E_INVALID_PARAM;
125 ADD_PACKET_TO_LIST_END(curkey,
127 packet_dup(packets->packet));
129 case OPENPGP_PACKET_PUBLICSUBKEY:
131 * It's a subkey packet.
134 return ONAK_E_INVALID_PARAM;
135 ADD_PACKET_TO_LIST_END(curkey,
137 packet_dup(packets->packet));
139 case OPENPGP_PACKET_TRUST:
144 * Trust packet. Ignore.
145 * Comment packet. Ignore.
149 /* Unsupported packet. Do what? Ignore for now. */
152 packets = packets->next;
159 * debug_packet - Print debug info about a packet
160 * @packet: The packet to display.
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.
166 int debug_packet(struct openpgp_packet *packet)
168 printf("\tNew format: %d, Tag: %u, Length: %zd\n",
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.
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.
187 onak_status_t read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
190 struct openpgp_packet_list **packets,
193 unsigned char curchar = 0;
194 struct openpgp_packet_list *curpacket = NULL;
195 onak_status_t rc = ONAK_E_OK;
197 bool inpacket = false;
200 return ONAK_E_INVALID_PARAM;
202 curpacket = *packets;
203 if (curpacket != NULL) {
204 while (curpacket->next != NULL) {
205 curpacket = curpacket->next;
209 while (!rc && (maxnum == 0 || keys < maxnum) &&
210 !getchar_func(ctx, 1, &curchar)) {
211 if (!inpacket && (curchar & 0x80)) {
213 * New packet. Record the fact we're in a packet and
214 * allocate memory for it.
217 if (curpacket != NULL) {
218 curpacket->next = malloc(sizeof (*curpacket));
219 curpacket = curpacket->next;
221 *packets = curpacket =
222 malloc(sizeof (*curpacket));
224 memset(curpacket, 0, sizeof(*curpacket));
226 malloc(sizeof (*curpacket->packet));
227 memset(curpacket->packet, 0,
228 sizeof(*curpacket->packet));
230 curpacket->packet->newformat = (curchar & 0x40);
233 * TODO: Better error checking on getchar_func.
235 if (curpacket->packet->newformat) {
236 curpacket->packet->tag = (curchar & 0x3F);
237 rc = getchar_func(ctx, 1, &curchar);
238 curpacket->packet->length = curchar;
239 if (curpacket->packet->length > 191 &&
240 curpacket->packet->length < 224) {
241 rc = getchar_func(ctx, 1, &curchar);
242 curpacket->packet->length -= 192;
243 curpacket->packet->length <<= 8;
244 curpacket->packet->length += curchar;
245 curpacket->packet->length += 192;
246 } else if (curpacket->packet->length > 223 &&
247 curpacket->packet->length < 255) {
248 return ONAK_E_UNSUPPORTED_FEATURE;
249 } else if (curpacket->packet->length == 255) {
251 * 5 byte length; ie 255 followed by 3
252 * bytes of MSB length.
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 curpacket->packet->length <<= 8;
263 rc = getchar_func(ctx, 1, &curchar);
264 curpacket->packet->length += curchar;
267 curpacket->packet->tag = (curchar & 0x3C) >> 2;
268 switch (curchar & 3) {
270 rc = getchar_func(ctx, 1, &curchar);
271 curpacket->packet->length = curchar;
274 rc = getchar_func(ctx, 1, &curchar);
275 curpacket->packet->length = curchar;
276 curpacket->packet->length <<= 8;
277 rc = getchar_func(ctx, 1, &curchar);
278 curpacket->packet->length += curchar;
281 rc = getchar_func(ctx, 1, &curchar);
282 curpacket->packet->length =
284 rc = getchar_func(ctx, 1, &curchar);
285 curpacket->packet->length +=
287 rc = getchar_func(ctx, 1, &curchar);
288 curpacket->packet->length +=
290 rc = getchar_func(ctx, 1, &curchar);
291 curpacket->packet->length += curchar;
294 rc = ONAK_E_UNSUPPORTED_FEATURE;
295 curpacket->packet->length = 0;
296 curpacket->packet->data = NULL;
302 if (curpacket->packet->tag ==
303 OPENPGP_PACKET_PUBLICKEY) {
306 curpacket->packet->data =
307 malloc(curpacket->packet->length *
308 sizeof(unsigned char));
309 if (curpacket->packet->data == NULL) {
312 rc = getchar_func(ctx,
313 curpacket->packet->length,
314 curpacket->packet->data);
319 rc = ONAK_E_INVALID_PKT;
327 * write_openpgp_stream - Reads a stream of OpenPGP packets.
328 * @putchar_func: The function to put the next character to the stream.
329 * @ctx: A pointer to the context structure for putchar_func.
330 * @packets: The list of packets.
332 * This function uses putchar_func to write characters to an OpenPGP
333 * packet stream from a linked list of packets.
335 onak_status_t write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
338 struct openpgp_packet_list *packets)
340 unsigned char curchar = 0;
342 while (packets != NULL) {
344 if (packets->packet->newformat) {
346 curchar |= packets->packet->tag;
347 putchar_func(ctx, 1, &curchar);
349 if (packets->packet->length < 192) {
350 curchar = packets->packet->length;
351 putchar_func(ctx, 1, &curchar);
352 } else if (packets->packet->length > 191 &&
353 packets->packet->length < 8383) {
354 curchar = (((packets->packet->length - 192) &
356 putchar_func(ctx, 1, &curchar);
358 curchar = (packets->packet->length - 192) &
360 putchar_func(ctx, 1, &curchar);
361 } else if (packets->packet->length > 8382 &&
362 packets->packet->length < 0xFFFFFFFF) {
364 putchar_func(ctx, 1, &curchar);
366 curchar = (packets->packet->length >> 24);
368 putchar_func(ctx, 1, &curchar);
370 curchar = (packets->packet->length >> 16);
372 putchar_func(ctx, 1, &curchar);
374 curchar = (packets->packet->length >> 8);
376 putchar_func(ctx, 1, &curchar);
378 curchar = packets->packet->length;
380 putchar_func(ctx, 1, &curchar);
382 return ONAK_E_UNSUPPORTED_FEATURE;
385 curchar |= (packets->packet->tag << 2);
386 if (packets->packet->length < 256) {
387 putchar_func(ctx, 1, &curchar);
388 curchar = packets->packet->length;
389 putchar_func(ctx, 1, &curchar);
390 } else if (packets->packet->length < 0x10000) {
392 putchar_func(ctx, 1, &curchar);
393 curchar = packets->packet->length >> 8;
394 putchar_func(ctx, 1, &curchar);
395 curchar = packets->packet->length & 0xFF;
396 putchar_func(ctx, 1, &curchar);
399 putchar_func(ctx, 1, &curchar);
400 curchar = packets->packet->length >> 24;
401 putchar_func(ctx, 1, &curchar);
402 curchar = (packets->packet->length >> 16) & 0xFF;
403 putchar_func(ctx, 1, &curchar);
404 curchar = (packets->packet->length >> 8) & 0xFF;
405 putchar_func(ctx, 1, &curchar);
406 curchar = packets->packet->length & 0xFF;
407 putchar_func(ctx, 1, &curchar);
411 putchar_func(ctx, packets->packet->length,
412 packets->packet->data);
413 packets = packets->next;
420 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
421 * @key: The public key.
422 * @packets: The outputted packet list.
424 * This function converts public key structure to a linked list of OpenPGP
425 * packets ready for outputing or storage.
427 int flatten_publickey(struct openpgp_publickey *key,
428 struct openpgp_packet_list **packets,
429 struct openpgp_packet_list **list_end)
431 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
432 struct openpgp_packet_list *tmplist = NULL;
434 while (key != NULL) {
436 * First write the public key packet out.
438 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
439 if (*packets == NULL) {
440 *packets = *list_end;
444 * Now do any signatures on the main key.
446 for (tmplist = key->sigs; tmplist != NULL;
447 tmplist = tmplist->next) {
448 ADD_PACKET_TO_LIST((*list_end),
449 packet_dup(tmplist->packet));
453 * Output any UIDs along with their signatures.
455 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
456 tmpsignedlist = tmpsignedlist->next) {
458 ADD_PACKET_TO_LIST((*list_end),
459 packet_dup(tmpsignedlist->packet));
460 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
461 tmplist = tmplist->next) {
462 ADD_PACKET_TO_LIST((*list_end),
463 packet_dup(tmplist->packet));
468 * Output any subkeys along with their signatures.
470 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
471 tmpsignedlist = tmpsignedlist->next) {
473 ADD_PACKET_TO_LIST((*list_end),
474 packet_dup(tmpsignedlist->packet));
475 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
476 tmplist = tmplist->next) {
477 ADD_PACKET_TO_LIST((*list_end),
478 packet_dup(tmplist->packet));