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