2 * parsekey.c - Routines to parse an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
16 #include "keystructs.h"
22 * add_key - Takes a key and adds it to the keyserver.
23 * @key: The public key to add.
25 * This function takes a public key and adds it to the keyserver.
26 * It first of all sees if we already have the key locally. If we do then
27 * we retrieve it and merge the two keys. We then store the resulting key
28 * (or just the original we received if we don't already have it). We then
29 * send out the appropriate updates to our keyserver peers.
31 int add_key(struct openpgp_publickey *key) {
36 * parse_keys - Process a stream of packets for public keys + sigs.
37 * @packets: The packet list to parse.
38 * @keys: The returned list of public keys.
40 * This function takes an list of OpenPGP packets and attempts to parse it
41 * into a list of public keys with signatures and subkeys.
43 int parse_keys(struct openpgp_packet_list *packets,
44 struct openpgp_publickey **keys)
46 struct openpgp_publickey *curkey = NULL;
48 while (packets != NULL) {
49 switch (packets->packet->tag) {
52 * It's a signature packet. Add it to either the public
53 * key (it should be a revocation), to the current UID
54 * or the current subkey.
56 assert(curkey != NULL);
57 if (curkey->subkeys != NULL) {
58 ADD_PACKET_TO_LIST_END(curkey->last_subkey,
60 packet_dup(packets->packet));
61 } else if (curkey->uids != NULL) {
62 ADD_PACKET_TO_LIST_END(curkey->last_uid,
64 packet_dup(packets->packet));
66 ADD_PACKET_TO_LIST_END(curkey,
68 packet_dup(packets->packet));
73 * It's a public key packet, so start a new key in our
77 curkey->next = malloc(sizeof (*curkey));
78 curkey = curkey->next;
81 malloc(sizeof (*curkey));
83 memset(curkey, 0, sizeof(*curkey));
84 curkey->publickey = packet_dup(packets->packet);
89 * It's a UID packet (or a photo id, which is similar).
91 assert(curkey != NULL);
92 assert(curkey->subkeys == NULL);
93 ADD_PACKET_TO_LIST_END(curkey,
95 packet_dup(packets->packet));
99 * It's a subkey packet.
101 assert(curkey != NULL);
102 ADD_PACKET_TO_LIST_END(curkey,
104 packet_dup(packets->packet));
107 printf("Unsupported packet type: %d\n",
108 packets->packet->tag);
110 packets = packets->next;
117 * debug_packet - Print debug info about a packet
118 * @packet: The packet to display.
120 * This function takes an OpenPGP packet and displays some information
121 * about it to stdout. Useful for debugging purposes or curiousity about
122 * an OpenPGP packet stream.
124 int debug_packet(struct openpgp_packet *packet)
126 printf("\tNew format: %d, Tag: %d, Length: %d\n",
135 * read_openpgp_stream - Reads a stream of OpenPGP packets.
136 * @getchar_func: The function to get the next character from the stream.
137 * @ctx: A pointer to the context structure for getchar_func.
138 * @packets: The outputted list of packets.
140 * This function uses getchar_func to read characters from an OpenPGP
141 * packet stream and reads the packets into a linked list of packets
142 * ready for parsing as a public key or whatever.
144 int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
147 struct openpgp_packet_list **packets)
149 unsigned char curchar = 0;
150 unsigned long count = 0;
151 struct openpgp_packet_list *curpacket = NULL;
153 bool inpacket = false;
155 assert(packets != NULL);
157 while (!rc && !getchar_func(ctx, 1, &curchar)) {
158 if (!inpacket && (curchar & 0x80)) {
160 * New packet. Record the fact we're in a packet and
161 * allocate memory for it.
165 if (curpacket != NULL) {
166 curpacket->next = malloc(sizeof (*curpacket));
167 curpacket = curpacket->next;
169 *packets = curpacket =
170 malloc(sizeof (*curpacket));
172 memset(curpacket, 0, sizeof(*curpacket));
174 malloc(sizeof (*curpacket->packet));
175 memset(curpacket->packet, 0,
176 sizeof(*curpacket->packet));
178 curpacket->packet->newformat = (curchar & 0x40);
180 // TODO: Better error checking on getchar_func.
181 if (curpacket->packet->newformat) {
182 curpacket->packet->tag = (curchar & 0x3F);
183 rc = getchar_func(ctx, 1, &curchar);
184 curpacket->packet->length = curchar;
185 if (curpacket->packet->length > 191 &&
186 curpacket->packet->length < 224) {
187 rc = getchar_func(ctx, 1, &curchar);
188 curpacket->packet->length -= 192;
189 curpacket->packet->length <<= 8;
190 curpacket->packet->length += curchar;
191 curpacket->packet->length += 192;
192 } else if (curpacket->packet->length > 223 &&
193 curpacket->packet->length < 255) {
194 printf("Partial length; not supported.\n");
197 * 5 byte length; ie 255 followed by 3
198 * bytes of MSB length.
200 rc = getchar_func(ctx, 1, &curchar);
201 curpacket->packet->length = curchar;
202 curpacket->packet->length <<= 8;
203 rc = getchar_func(ctx, 1, &curchar);
204 curpacket->packet->length = curchar;
205 curpacket->packet->length <<= 8;
206 rc = getchar_func(ctx, 1, &curchar);
207 curpacket->packet->length = curchar;
208 curpacket->packet->length <<= 8;
209 rc = getchar_func(ctx, 1, &curchar);
210 curpacket->packet->length = curchar;
214 curpacket->packet->tag = (curchar & 0x3C) >> 2;
215 switch (curchar & 3) {
217 rc = getchar_func(ctx, 1, &curchar);
218 curpacket->packet->length = curchar;
221 rc = getchar_func(ctx, 1, &curchar);
222 curpacket->packet->length = curchar;
223 curpacket->packet->length <<= 8;
224 rc = getchar_func(ctx, 1, &curchar);
225 curpacket->packet->length += curchar;
228 printf("Unsupported length type 2.\n");
231 printf("Unsupported length type 3.\n");
235 curpacket->packet->data =
236 malloc(curpacket->packet->length *
237 sizeof(unsigned char));
238 rc = getchar_func(ctx, curpacket->packet->length,
239 curpacket->packet->data);
242 fprintf(stderr, "Unexpected character: 0x%X\n",
251 * write_openpgp_stream - Reads a stream of OpenPGP packets.
252 * @putchar_func: The function to put the next character to the stream.
253 * @ctx: A pointer to the context structure for putchar_func.
254 * @packets: The list of packets.
256 * This function uses putchar_func to write characters to an OpenPGP
257 * packet stream from a linked list of packets.
259 int write_openpgp_stream(int (*putchar_func)(void *ctx, unsigned char c),
261 struct openpgp_packet_list *packets)
263 unsigned char curchar = 0;
266 while (packets != NULL) {
268 if (packets->packet->newformat) {
270 curchar |= packets->packet->tag;
271 putchar_func(ctx, curchar);
273 if (packets->packet->length < 192) {
274 putchar_func(ctx, packets->packet->length);
275 } else if (packets->packet->length > 191 &&
276 packets->packet->length < 8383) {
277 // fputs("Potentially dodgy code here.\n", stderr);
279 (((packets->packet->length - 192) &
280 0xFF00) >> 8) + 192);
283 (packets->packet->length - 192) &
287 fputs("Unsupported new format length.\n", stderr);
290 curchar |= (packets->packet->tag << 2);
291 if (packets->packet->length < 256) {
292 putchar_func(ctx, curchar);
293 putchar_func(ctx, packets->packet->length);
294 } else if (packets->packet->length < 0x10000) {
296 putchar_func(ctx, curchar);
297 putchar_func(ctx, packets->packet->length >> 8);
299 packets->packet->length & 0xFF);
302 putchar_func(ctx, curchar);
304 packets->packet->length >> 24);
306 (packets->packet->length >> 16) & 0xFF);
308 (packets->packet->length >> 8) & 0xFF);
310 packets->packet->length & 0xFF);
314 for (i = 0; i < packets->packet->length; i++) {
315 putchar_func(ctx, packets->packet->data[i]);
317 packets = packets->next;
323 * flatten_publickey - Convert a publickey to an OpenPGP packet list.
324 * @key: The public key.
325 * @packets: The outputted packet list.
327 * This function converts public key structure to a linked list of OpenPGP
328 * packets ready for outputing or storage.
330 int flatten_publickey(struct openpgp_publickey *key,
331 struct openpgp_packet_list **packets,
332 struct openpgp_packet_list **list_end)
334 struct openpgp_signedpacket_list *tmpsignedlist = NULL;
335 struct openpgp_packet_list *tmplist = NULL;
337 while (key != NULL) {
339 * First write the public key packet out.
341 ADD_PACKET_TO_LIST((*list_end), packet_dup(key->publickey));
342 if (*packets == NULL) {
343 *packets = *list_end;
347 * Now do any revocation signatures on the main key.
349 for (tmplist = key->revocations; tmplist != NULL;
350 tmplist = tmplist->next) {
351 ADD_PACKET_TO_LIST((*list_end),
352 packet_dup(tmplist->packet));
356 * Output any UIDs along with their signatures.
358 for (tmpsignedlist = key->uids; tmpsignedlist != NULL;
359 tmpsignedlist = tmpsignedlist->next) {
361 ADD_PACKET_TO_LIST((*list_end),
362 packet_dup(tmpsignedlist->packet));
363 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
364 tmplist = tmplist->next) {
365 ADD_PACKET_TO_LIST((*list_end),
366 packet_dup(tmplist->packet));
371 * Output any subkeys along with their signatures.
373 for (tmpsignedlist = key->subkeys; tmpsignedlist != NULL;
374 tmpsignedlist = tmpsignedlist->next) {
376 ADD_PACKET_TO_LIST((*list_end),
377 packet_dup(tmpsignedlist->packet));
378 for (tmplist = tmpsignedlist->sigs; tmplist != NULL;
379 tmplist = tmplist->next) {
380 ADD_PACKET_TO_LIST((*list_end),
381 packet_dup(tmplist->packet));