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