]> the.earth.li Git - onak.git/blob - keyid.c
6a9b5580c34fa1c6a9df2100f565fd117ce382dc
[onak.git] / keyid.c
1 /*
2  * keyid.c - Routines to calculate key IDs.
3  *
4  * Copyright 2002,2011 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, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <string.h>
21 #include <sys/types.h>
22 #include <arpa/inet.h>
23
24 #include "config.h"
25 #include "keyid.h"
26 #include "keystructs.h"
27 #include "onak.h"
28 #include "parsekey.h"
29 #include "mem.h"
30 #include "merge.h"
31
32 #ifdef HAVE_NETTLE
33 #include <nettle/md5.h>
34 #include <nettle/ripemd160.h>
35 #include <nettle/sha.h>
36 #else
37 #include "md5.h"
38 #include "sha1.h"
39 #endif
40
41
42 /**
43  *      get_keyid - Given a public key returns the keyid.
44  *      @publickey: The key to calculate the id for.
45  */
46 onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid)
47 {
48         return (get_packetid(publickey->publickey, keyid));
49 }
50
51 /**
52  *      get_fingerprint - Given a public key returns the fingerprint.
53  *      @publickey: The key to calculate the id for.
54  *      @fingerprint: The fingerprint (must be at least 20 bytes of space).
55  *      @len: The length of the returned fingerprint.
56  *
57  *      This function returns the fingerprint for a given public key. As Type 3
58  *      fingerprints are 16 bytes and Type 4 are 20 the len field indicates
59  *      which we've returned.
60  */
61 onak_status_t get_fingerprint(struct openpgp_packet *packet,
62         struct openpgp_fingerprint *fingerprint)
63 {
64         struct sha1_ctx sha_ctx;
65         struct md5_ctx md5_context;
66         unsigned char c;
67         size_t         modlen, explen;
68
69         if (fingerprint == NULL)
70                 return ONAK_E_INVALID_PARAM;
71
72         fingerprint->length = 0;
73
74         switch (packet->data[0]) {
75         case 2:
76         case 3:
77                 md5_init(&md5_context);
78
79                 /*
80                  * MD5 the modulus and exponent.
81                  */
82                 modlen = ((packet->data[8] << 8) +
83                          packet->data[9] + 7) >> 3;
84                 md5_update(&md5_context, modlen, &packet->data[10]);
85
86                 explen = ((packet->data[10+modlen] << 8) +
87                          packet->data[11+modlen] + 7) >> 3;
88                 md5_update(&md5_context, explen, &packet->data[12 + modlen]);
89
90                 fingerprint->length = 16;
91                 md5_digest(&md5_context, fingerprint->length, fingerprint->fp);
92
93                 break;
94
95         case 4:
96                 sha1_init(&sha_ctx);
97                 /*
98                  * TODO: Can this be 0x99? Are all public key packets old
99                  * format with 2 bytes of length data?
100                  */
101                 c = 0x99;
102                 sha1_update(&sha_ctx, sizeof(c), &c);
103                 c = packet->length >> 8;
104                 sha1_update(&sha_ctx, sizeof(c), &c);
105                 c = packet->length & 0xFF;
106                 sha1_update(&sha_ctx, sizeof(c), &c);
107                 sha1_update(&sha_ctx, packet->length,
108                         packet->data);
109                 fingerprint->length = 20;
110                 sha1_digest(&sha_ctx, fingerprint->length, fingerprint->fp);
111
112                 break;
113         default:
114                 return ONAK_E_UNKNOWN_VER;
115         }
116
117         return ONAK_E_OK;
118 }
119
120
121 /**
122  *      get_packetid - Given a PGP packet returns the keyid.
123  *      @packet: The packet to calculate the id for.
124  */
125 onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid)
126 {
127         int             offset = 0;
128         int             i = 0;
129         struct openpgp_fingerprint fingerprint;
130 #ifdef NETTLE_WITH_RIPEMD160
131         struct ripemd160_ctx ripemd160_context;
132         uint8_t         data;
133 #endif
134
135         if (packet == NULL)
136                 return ONAK_E_INVALID_PARAM;
137
138         switch (packet->data[0]) {
139         case 2:
140         case 3:
141                 /*
142                  * Old versions of GnuPG would put Elgamal keys inside
143                  * a V3 key structure, then generate the keyid using
144                  * RIPED160.
145                  */
146 #ifdef NETTLE_WITH_RIPEMD160
147                 if (packet->data[7] == 16) {
148                         ripemd160_init(&ripemd160_context);
149                         data = 0x99;
150                         ripemd160_update(&ripemd160_context, 1, &data);
151                         data = packet->length >> 8;
152                         ripemd160_update(&ripemd160_context, 1, &data);
153                         data = packet->length & 0xFF;
154                         ripemd160_update(&ripemd160_context, 1, &data);
155                         ripemd160_update(&ripemd160_context,
156                                 packet->length,
157                                 packet->data);
158
159                         ripemd160_digest(&ripemd160_context,
160                                 RIPEMD160_DIGEST_SIZE,
161                                 fingerprint.fp);
162
163                         for (*keyid = 0, i = 12; i < 20; i++) {
164                                 *keyid <<= 8;
165                                 *keyid += fingerprint.fp[i];
166                         }
167
168                         return ONAK_E_OK;
169                 }
170 #endif
171                 /*
172                  * Check for an RSA key; if not return an error.
173                  * 1 == RSA
174                  * 2 == RSA Encrypt-Only
175                  * 3 == RSA Sign-Only
176                  */
177                 if (packet->data[7] < 1 || packet->data[7] > 3) {
178                         return ONAK_E_INVALID_PKT;
179                 }
180
181                 /*
182                  * For a type 2 or 3 key the keyid is the last 64 bits of the
183                  * public modulus n, which is stored as an MPI from offset 8
184                  * onwards.
185                  */
186                 offset = (packet->data[8] << 8) +
187                         packet->data[9];
188                 offset = ((offset + 7) / 8) + 2;
189
190                 for (*keyid = 0, i = 0; i < 8; i++) {
191                         *keyid <<= 8;
192                         *keyid += packet->data[offset++];
193                 }
194                 break;
195         case 4:
196                 get_fingerprint(packet, &fingerprint);
197                 
198                 for (*keyid = 0, i = 12; i < 20; i++) {
199                         *keyid <<= 8;
200                         *keyid += fingerprint.fp[i];
201                 }
202
203                 break;
204         default:
205                 return ONAK_E_UNKNOWN_VER;
206         }
207
208         return ONAK_E_OK;
209 }
210
211 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
212                                                         *packets)
213 {
214         struct openpgp_packet_list *sorted, **cur, *next;
215
216         sorted = NULL;
217         while (packets != NULL) {
218                 cur = &sorted;
219                 while (*cur != NULL && compare_packets((*cur)->packet,
220                                 packets->packet) < 0) {
221                         cur = &((*cur)->next);
222                 }
223                 next = *cur;
224                 *cur = packets;
225                 packets = packets->next;
226                 (*cur)->next = next;
227         }
228
229         return sorted;
230 }
231
232 onak_status_t get_skshash(struct openpgp_publickey *key, struct skshash *hash)
233 {
234         struct openpgp_packet_list *packets = NULL, *list_end = NULL;
235         struct openpgp_packet_list *curpacket;
236         struct md5_ctx md5_context;
237         struct openpgp_publickey *next;
238         uint32_t tmp;
239
240         /*
241          * We only want a single key, so clear any link to the next
242          * one for the period during the flatten.
243          */
244         next = key->next;
245         key->next = NULL;
246         flatten_publickey(key, &packets, &list_end);
247         key->next = next;
248         packets = sortpackets(packets);
249
250         md5_init(&md5_context);
251
252         for (curpacket = packets; curpacket != NULL;
253                         curpacket = curpacket->next) {
254                 tmp = htonl(curpacket->packet->tag);
255                 md5_update(&md5_context, sizeof(tmp), (void *) &tmp);
256                 tmp = htonl(curpacket->packet->length);
257                 md5_update(&md5_context, sizeof(tmp), (void *) &tmp);
258                 md5_update(&md5_context,
259                                 curpacket->packet->length,
260                                 curpacket->packet->data);
261         }
262
263         md5_digest(&md5_context, 16, (uint8_t *) &hash->hash);
264         free_packet_list(packets);
265
266         return ONAK_E_OK;
267 }
268
269 uint8_t hexdigit(char c)
270 {
271         if (c >= '0' && c <= '9')
272                 return c - '0';
273         else if (c >= 'a' && c <= 'f')
274                 return c - 'a' + 10;
275         else if (c >= 'A' && c <= 'F')
276                 return c - 'A' + 10;
277         else
278                 return 0;
279 }
280
281 int parse_skshash(char *search, struct skshash *hash)
282 {
283         int i, len;
284
285         len = strlen(search);
286         if (len > 32) {
287                 return 0;
288         }
289
290         for (i = 0; i < len; i += 2) {
291                 hash->hash[i >> 1] = (hexdigit(search[i]) << 4) +
292                                 hexdigit(search[i + 1]);
293         }
294
295         return 1;
296 }