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