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