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