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