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