Inital subkey searching support for db3 backend.
[onak.git] / decodekey.c
1 /*
2  * decodekey.c - Routines to further decode an OpenPGP key.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  *
8  * $Id: decodekey.c,v 1.6 2004/05/27 03:24:01 noodles Exp $
9  */
10
11 #include <assert.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17
18 #include "decodekey.h"
19 #include "hash.h"
20 #include "keyid.h"
21 #include "keystructs.h"
22 #include "ll.h"
23 #include "log.h"
24
25 /*
26  *      parse_subpackets - Parse the subpackets of a Type 4 signature.
27  *      @data: The subpacket data.
28  *      @keyid: A pointer to where we should return the keyid.
29  *
30  *      This function parses the subkey data of a Type 4 signature and fills
31  *      in the supplied variables. It also returns the length of the data
32  *      processed.
33  */
34 int parse_subpackets(unsigned char *data, uint64_t *keyid)
35 {
36         int offset = 0;
37         int length = 0;
38         int packetlen = 0;
39
40         assert(data != NULL);
41
42         length = (data[0] << 8) + data[1] + 2;
43
44         offset = 2;
45         while (offset < length) {
46                 packetlen = data[offset++];
47                 if (packetlen > 191 && packetlen < 255) {
48                         packetlen = ((packetlen - 192) << 8) +
49                                         data[offset++] + 192;
50                 } else if (packetlen == 255) {
51                         packetlen = data[offset++];
52                         packetlen <<= 8;
53                         packetlen = data[offset++];
54                         packetlen <<= 8;
55                         packetlen = data[offset++];
56                         packetlen <<= 8;
57                         packetlen = data[offset++];
58                 }
59                 switch (data[offset] & 0x7F) {
60                 case 2:
61                         /*
62                          * Signature creation time. Might want to output this?
63                          */
64                         break;
65                 case 3:
66                         /*
67                          * Signature expiration time. Might want to output this?
68                          */
69                         break;
70                 case 16:
71                         *keyid = data[offset+packetlen - 8];
72                         *keyid <<= 8;
73                         *keyid += data[offset+packetlen - 7];
74                         *keyid <<= 8;
75                         *keyid += data[offset+packetlen - 6];
76                         *keyid <<= 8;
77                         *keyid += data[offset+packetlen - 5];
78                         *keyid <<= 8;
79                         *keyid += data[offset+packetlen - 4];
80                         *keyid <<= 8;
81                         *keyid += data[offset+packetlen - 3];
82                         *keyid <<= 8;
83                         *keyid += data[offset+packetlen - 2];
84                         *keyid <<= 8;
85                         *keyid += data[offset+packetlen - 1];
86                         break;
87                 case 20:
88                         /*
89                          * Annotation data.
90                          */
91                         break;
92
93                 case 23:
94                         /*
95                          * Key server preferences. Including no-modify.
96                          */
97                         break;
98                 case 25:
99                         /*
100                          * Primary UID.
101                          */
102                         break;
103                 default:
104                         /*
105                          * We don't care about unrecognized packets unless bit
106                          * 7 is set in which case we log a major error.
107                          */
108                         if (data[offset] & 0x80) {
109                                 logthing(LOGTHING_CRITICAL,
110                                 "Critical subpacket type not parsed: 0x%X",
111                                         data[offset]);
112                         }
113                                 
114                 }
115                 offset += packetlen;
116         }
117
118         return length;
119 }
120
121 /**
122  *      keysigs - Return the sigs on a given OpenPGP signature list.
123  *      @curll: The current linked list. Can be NULL to create a new list.
124  *      @sigs: The signature list we want the sigs on.
125  *
126  *      Returns a linked list of stats_key elements containing the sigs on the
127  *      supplied OpenPGP packet list.
128  */
129 struct ll *keysigs(struct ll *curll,
130                 struct openpgp_packet_list *sigs)
131 {
132         uint64_t keyid = 0;
133         
134         while (sigs != NULL) {
135                 keyid = sig_keyid(sigs->packet);
136                 sigs = sigs->next;
137                 curll = lladd(curll, createandaddtohash(keyid));
138         }
139
140         return curll;
141 }
142
143 /**
144  *      sig_keyid - Return the keyid for a given OpenPGP signature packet.
145  *      @packet: The signature packet.
146  *
147  *      Returns the keyid for the supplied signature packet.
148  */
149 uint64_t sig_keyid(struct openpgp_packet *packet)
150 {
151         int length = 0;
152         uint64_t keyid = 0;
153         
154         if (packet != NULL) {
155                 keyid = 0;
156                 switch (packet->data[0]) {
157                 case 2:
158                 case 3:
159                         keyid = packet->data[7];
160                         keyid <<= 8;
161                         keyid += packet->data[8];
162                         keyid <<= 8;
163                         keyid += packet->data[9];
164                         keyid <<= 8;
165                         keyid += packet->data[10];
166                         keyid <<= 8;
167                         keyid += packet->data[11];
168                         keyid <<= 8;
169                         keyid += packet->data[12];
170                         keyid <<= 8;
171                         keyid += packet->data[13];
172                         keyid <<= 8;
173                         keyid += packet->data[14];
174                         break;
175                 case 4:
176                         length = parse_subpackets(&packet->data[4],
177                                         &keyid);
178                         parse_subpackets(&packet->data[length + 4],
179                                         &keyid);
180                         /*
181                          * Don't bother to look at the unsigned packets.
182                          */
183                         break;
184                 default:
185                         break;
186                 }
187         }
188
189         return keyid;
190 }
191
192 /*
193  * TODO: Abstract out; all our linked lists should be generic and then we can
194  * llsize them.
195  */
196 int spsize(struct openpgp_signedpacket_list *list)
197 {
198         int size = 0;
199         struct openpgp_signedpacket_list *cur;
200
201         for (cur = list; cur != NULL; cur = cur->next, size++) ;
202
203         return size;
204 }
205
206 /**
207  *      keyuids - Takes a key and returns an array of its UIDs
208  *      @key: The key to get the uids of.
209  *      @primary: A pointer to store the primary UID in.
210  *
211  *      keyuids takes a public key structure and builds an array of the UIDs 
212  *      on the key. It also attempts to work out the primary UID and returns a
213  *      separate pointer to that particular element of the array.
214  */
215 char **keyuids(struct openpgp_publickey *key, char **primary)
216 {
217         struct openpgp_signedpacket_list *curuid = NULL;
218         char buf[1024];
219         char **uids = NULL;
220         int count = 0;
221         
222         if (primary != NULL) {
223                 *primary = NULL;
224         }
225
226         if (key != NULL && key->uids != NULL) {
227                 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
228         
229                 curuid = key->uids;
230                 while (curuid != NULL) {
231                         buf[0] = 0;
232                         if (curuid->packet->tag == 13) {
233                                 snprintf(buf, 1023, "%.*s",
234                                                 (int) curuid->packet->length,
235                                                 curuid->packet->data);
236                                 uids[count++] = strdup(buf);
237                         }
238                         curuid = curuid -> next;
239                 }
240                 uids[count] = NULL;
241
242                 /*
243                  * TODO: Parse subpackets for real primary ID (v4 keys)
244                  */
245                 if (primary != NULL) {
246                         *primary = uids[0];
247                 }
248         }
249
250         return uids;
251 }
252
253 /**
254  *      keysubkeys - Takes a key and returns an array of its subkey keyids.
255  *      @key: The key to get the subkeys of.
256  *
257  *      keysubkeys takes a public key structure and returns an array of the
258  *      subkey keyids for that key.
259  */
260 uint64_t *keysubkeys(struct openpgp_publickey *key)
261 {
262         struct openpgp_signedpacket_list *cursubkey = NULL;
263         uint64_t                         *subkeys = NULL;
264         int                               count = 0;
265         
266         if (key != NULL && key->subkeys != NULL) {
267                 subkeys = malloc((spsize(key->subkeys) + 1) *
268                                 sizeof (uint64_t));
269                 cursubkey = key->subkeys;
270                 while (cursubkey != NULL) {
271                         subkeys[count++] = get_packetid(cursubkey->packet);
272                         cursubkey = cursubkey -> next;
273                 }
274                 subkeys[count] = NULL;
275         }
276
277         return subkeys;
278 }