]> git.sommitrealweird.co.uk Git - onak.git/blob - decodekey.c
cscvs to tla changeset 103
[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.4 2003/09/28 21:07:50 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 23:
88                         /*
89                          * Key server preferences. Including no-modify.
90                          */
91                         break;
92                 case 25:
93                         /*
94                          * Primary UID.
95                          */
96                         break;
97                 default:
98                         /*
99                          * We don't care about unrecognized packets unless bit
100                          * 7 is set in which case we log a major error.
101                          */
102                         if (data[offset] & 0x80) {
103                                 logthing(LOGTHING_CRITICAL,
104                                 "Critical subpacket type not parsed: 0x%X",
105                                         data[offset]);
106                         }
107                                 
108                 }
109                 offset += packetlen;
110         }
111
112         return length;
113 }
114
115 /**
116  *      keysigs - Return the sigs on a given OpenPGP signature list.
117  *      @curll: The current linked list. Can be NULL to create a new list.
118  *      @sigs: The signature list we want the sigs on.
119  *
120  *      Returns a linked list of stats_key elements containing the sigs on the
121  *      supplied OpenPGP packet list.
122  */
123 struct ll *keysigs(struct ll *curll,
124                 struct openpgp_packet_list *sigs)
125 {
126         uint64_t keyid = 0;
127         
128         while (sigs != NULL) {
129                 keyid = sig_keyid(sigs->packet);
130                 sigs = sigs->next;
131                 curll = lladd(curll, createandaddtohash(keyid));
132         }
133
134         return curll;
135 }
136
137 /**
138  *      sig_keyid - Return the keyid for a given OpenPGP signature packet.
139  *      @packet: The signature packet.
140  *
141  *      Returns the keyid for the supplied signature packet.
142  */
143 uint64_t sig_keyid(struct openpgp_packet *packet)
144 {
145         int length = 0;
146         uint64_t keyid = 0;
147         
148         if (packet != NULL) {
149                 keyid = 0;
150                 switch (packet->data[0]) {
151                 case 2:
152                 case 3:
153                         keyid = packet->data[7];
154                         keyid <<= 8;
155                         keyid += packet->data[8];
156                         keyid <<= 8;
157                         keyid += packet->data[9];
158                         keyid <<= 8;
159                         keyid += packet->data[10];
160                         keyid <<= 8;
161                         keyid += packet->data[11];
162                         keyid <<= 8;
163                         keyid += packet->data[12];
164                         keyid <<= 8;
165                         keyid += packet->data[13];
166                         keyid <<= 8;
167                         keyid += packet->data[14];
168                         break;
169                 case 4:
170                         length = parse_subpackets(&packet->data[4],
171                                         &keyid);
172                         parse_subpackets(&packet->data[length + 4],
173                                         &keyid);
174                         /*
175                          * Don't bother to look at the unsigned packets.
176                          */
177                         break;
178                 default:
179                         break;
180                 }
181         }
182
183         return keyid;
184 }
185
186 /*
187  * TODO: Abstract out; all our linked lists should be generic and then we can
188  * llsize them.
189  */
190 int spsize(struct openpgp_signedpacket_list *list)
191 {
192         int size = 0;
193         struct openpgp_signedpacket_list *cur;
194
195         for (cur = list; cur != NULL; cur = cur->next, size++) ;
196
197         return size;
198 }
199
200 /**
201  *      keyuids - Takes a key and returns an array of its UIDs
202  *      @key: The key to get the uids of.
203  *      @primary: A pointer to store the primary UID in.
204  *
205  *      keyuids takes a public key structure and builds an array of the UIDs 
206  *      on the key. It also attempts to work out the primary UID and returns a
207  *      separate pointer to that particular element of the array.
208  */
209 char **keyuids(struct openpgp_publickey *key, char **primary)
210 {
211         struct openpgp_signedpacket_list *curuid = NULL;
212         char buf[1024];
213         char **uids = NULL;
214         int count = 0;
215
216         *primary = NULL;
217         if (key != NULL && key->uids != NULL) {
218                 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
219         
220                 curuid = key->uids;
221                 while (curuid != NULL) {
222                         buf[0] = 0;
223                         if (curuid->packet->tag == 13) {
224                                 snprintf(buf, 1023, "%.*s",
225                                                 (int) curuid->packet->length,
226                                                 curuid->packet->data);
227                                 uids[count++] = strdup(buf);
228                         }
229                         curuid = curuid -> next;
230                 }
231                 uids[count] = NULL;
232
233                 /*
234                  * TODO: Parse subpackets for real primary ID (v4 keys)
235                  */
236                 if (primary != NULL) {
237                         *primary = uids[0];
238                 }
239         }
240
241         return uids;
242 }