]> git.sommitrealweird.co.uk Git - onak.git/blob - decodekey.c
cscvs to tla changeset 67
[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
9 #include <assert.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15
16 #include "decodekey.h"
17 #include "hash.h"
18 #include "keyid.h"
19 #include "keystructs.h"
20 #include "ll.h"
21
22 /*
23  *      parse_subpackets - Parse the subpackets of a Type 4 signature.
24  *      @data: The subpacket data.
25  *      @keyid: A pointer to where we should return the keyid.
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.
30  */
31 int parse_subpackets(unsigned char *data, uint64_t *keyid)
32 {
33         int offset = 0;
34         int length = 0;
35         int packetlen = 0;
36
37         assert(data != NULL);
38
39         length = (data[0] << 8) + data[1] + 2;
40
41         offset = 2;
42         while (offset < length) {
43                 packetlen = data[offset++];
44                 if (packetlen > 191 && packetlen < 255) {
45                         packetlen = ((packetlen - 192) << 8) +
46                                         data[offset++] + 192;
47                 } else if (packetlen == 255) {
48                         packetlen = data[offset++];
49                         packetlen <<= 8;
50                         packetlen = data[offset++];
51                         packetlen <<= 8;
52                         packetlen = data[offset++];
53                         packetlen <<= 8;
54                         packetlen = data[offset++];
55                 }
56                 switch (data[offset]) {
57                 case 2:
58                         /*
59                          * Signature creation time. Might want to output this?
60                          */
61                         break;
62                 case 0x83:
63                         /*
64                          * Signature expiration time. Might want to output this?
65                          */
66                         break;
67                 case 16:
68                         *keyid = data[offset+packetlen - 8];
69                         *keyid <<= 8;
70                         *keyid += data[offset+packetlen - 7];
71                         *keyid <<= 8;
72                         *keyid += data[offset+packetlen - 6];
73                         *keyid <<= 8;
74                         *keyid += data[offset+packetlen - 5];
75                         *keyid <<= 8;
76                         *keyid += data[offset+packetlen - 4];
77                         *keyid <<= 8;
78                         *keyid += data[offset+packetlen - 3];
79                         *keyid <<= 8;
80                         *keyid += data[offset+packetlen - 2];
81                         *keyid <<= 8;
82                         *keyid += data[offset+packetlen - 1];
83                         break;
84                 case 23:
85                         /*
86                          * Key server preferences. Including no-modify.
87                          */
88                         break;
89                 case 25:
90                         /*
91                          * Primary UID.
92                          */
93                         break;
94                 default:
95                         /*
96                          * We don't care about unrecognized packets unless bit
97                          * 7 is set in which case we prefer an error than
98                          * ignoring it.
99                          */
100                         assert(!(data[offset] & 0x80));
101                 }
102                 offset += packetlen;
103         }
104
105         return length;
106 }
107
108 /**
109  *      keysigs - Return the sigs on a given OpenPGP signature list.
110  *      @curll: The current linked list. Can be NULL to create a new list.
111  *      @sigs: The signature list we want the sigs on.
112  *
113  *      Returns a linked list of stats_key elements containing the sigs on the
114  *      supplied OpenPGP packet list.
115  */
116 struct ll *keysigs(struct ll *curll,
117                 struct openpgp_packet_list *sigs)
118 {
119         uint64_t keyid = 0;
120         
121         while (sigs != NULL) {
122                 keyid = sig_keyid(sigs->packet);
123                 sigs = sigs->next;
124                 curll = lladd(curll, createandaddtohash(keyid));
125         }
126
127         return curll;
128 }
129
130 /**
131  *      sig_keyid - Return the keyid for a given OpenPGP signature packet.
132  *      @packet: The signature packet.
133  *
134  *      Returns the keyid for the supplied signature packet.
135  */
136 uint64_t sig_keyid(struct openpgp_packet *packet)
137 {
138         int length = 0;
139         uint64_t keyid = 0;
140         
141         if (packet != NULL) {
142                 keyid = 0;
143                 switch (packet->data[0]) {
144                 case 2:
145                 case 3:
146                         keyid = packet->data[7];
147                         keyid <<= 8;
148                         keyid += packet->data[8];
149                         keyid <<= 8;
150                         keyid += packet->data[9];
151                         keyid <<= 8;
152                         keyid += packet->data[10];
153                         keyid <<= 8;
154                         keyid += packet->data[11];
155                         keyid <<= 8;
156                         keyid += packet->data[12];
157                         keyid <<= 8;
158                         keyid += packet->data[13];
159                         keyid <<= 8;
160                         keyid += packet->data[14];
161                         break;
162                 case 4:
163                         length = parse_subpackets(&packet->data[4],
164                                         &keyid);
165                         parse_subpackets(&packet->data[length + 4],
166                                         &keyid);
167                         /*
168                          * Don't bother to look at the unsigned packets.
169                          */
170                         break;
171                 default:
172                         break;
173                 }
174         }
175
176         return keyid;
177 }
178
179 /*
180  * TODO: Abstract out; all our linked lists should be generic and then we can
181  * llsize them.
182  */
183 int spsize(struct openpgp_signedpacket_list *list)
184 {
185         int size = 0;
186         struct openpgp_signedpacket_list *cur;
187
188         for (cur = list; cur != NULL; cur = cur->next, size++) ;
189
190         return size;
191 }
192
193 /**
194  *      keyuids - Takes a key and returns an array of its UIDs
195  *      @key: The key to get the uids of.
196  *      @primary: A pointer to store the primary UID in.
197  *
198  *      keyuids takes a public key structure and builds an array of the UIDs 
199  *      on the key. It also attempts to work out the primary UID and returns a
200  *      separate pointer to that particular element of the array.
201  */
202 char **keyuids(struct openpgp_publickey *key, char **primary)
203 {
204         struct openpgp_signedpacket_list *curuid = NULL;
205         char buf[1024];
206         char **uids = NULL;
207         int count = 0;
208
209         if (key != NULL && key->uids != NULL) {
210                 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
211         
212                 curuid = key->uids;
213                 while (curuid != NULL) {
214                         buf[0] = 0;
215                         if (curuid->packet->tag == 13) {
216                                 snprintf(buf, 1023, "%.*s",
217                                                 (int) curuid->packet->length,
218                                                 curuid->packet->data);
219                                 uids[count++] = strdup(buf);
220                         }
221                         curuid = curuid -> next;
222                 }
223                 uids[count] = NULL;
224         }
225         /*
226          * TODO: Parse subpackets for real primary ID (v4 keys)
227          */
228         if (primary != NULL) {
229                 *primary = uids[0];
230         }
231
232         return uids;
233 }