Add support for calculating SKS style key hashes
[onak.git] / keyid.c
1 /*
2  * keyid.c - Routines to calculate key IDs.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <sys/types.h>
10 #include <arpa/inet.h>
11
12 #include "keyid.h"
13 #include "keystructs.h"
14 #include "log.h"
15 #include "parsekey.h"
16 #include "md5.h"
17 #include "mem.h"
18 #include "merge.h"
19 #include "sha1.h"
20
21
22 /**
23  *      get_keyid - Given a public key returns the keyid.
24  *      @publickey: The key to calculate the id for.
25  */
26 uint64_t get_keyid(struct openpgp_publickey *publickey)
27 {
28         return (get_packetid(publickey->publickey));
29 }
30
31 /**
32  *      get_fingerprint - Given a public key returns the fingerprint.
33  *      @publickey: The key to calculate the id for.
34  *      @fingerprint: The fingerprint (must be at least 20 bytes of space). 
35  *      @len: The length of the returned fingerprint.
36  *
37  *      This function returns the fingerprint for a given public key. As Type 3
38  *      fingerprints are 16 bytes and Type 4 are 20 the len field indicates
39  *      which we've returned.
40  */
41 unsigned char *get_fingerprint(struct openpgp_packet *packet,
42         unsigned char *fingerprint,
43         size_t *len)
44 {
45         SHA1_CTX sha_ctx;
46         struct md5_ctx md5_context;
47         unsigned char c;
48         size_t         modlen, explen;
49
50         log_assert(fingerprint != NULL);
51         log_assert(len != NULL);
52
53         *len = 0;
54
55         switch (packet->data[0]) {
56         case 2:
57         case 3:
58                 md5_init_ctx(&md5_context);
59
60                 /*
61                  * MD5 the modulus and exponent.
62                  */
63                 modlen = ((packet->data[8] << 8) +
64                          packet->data[9] + 7) >> 3;
65                 md5_process_bytes(&packet->data[10], modlen, &md5_context);
66
67                 explen = ((packet->data[10+modlen] << 8) +
68                          packet->data[11+modlen] + 7) >> 3;
69                 md5_process_bytes(&packet->data[12 + modlen], explen,
70                                 &md5_context);
71
72                 md5_finish_ctx(&md5_context, fingerprint);
73                 *len = 16;
74
75                 break;
76
77         case 4:
78                 SHA1Init(&sha_ctx);
79                 /*
80                  * TODO: Can this be 0x99? Are all public key packets old
81                  * format with 2 bytes of length data?
82                  */
83                 c = 0x99;
84                 SHA1Update(&sha_ctx, &c, sizeof(c));
85                 c = packet->length >> 8;
86                 SHA1Update(&sha_ctx, &c, sizeof(c));
87                 c = packet->length & 0xFF;
88                 SHA1Update(&sha_ctx, &c, sizeof(c));
89                 SHA1Update(&sha_ctx, packet->data,
90                         packet->length);
91                 SHA1Final(fingerprint, &sha_ctx);
92                 *len = 20;
93
94                 break;
95         default:
96                 logthing(LOGTHING_ERROR, "Unknown key type: %d",
97                                 packet->data[0]);
98         }
99
100         return fingerprint;
101 }
102
103
104 /**
105  *      get_packetid - Given a PGP packet returns the keyid.
106  *      @packet: The packet to calculate the id for.
107  */
108 uint64_t get_packetid(struct openpgp_packet *packet)
109 {
110         uint64_t        keyid = 0;
111         int             offset = 0;
112         int             i = 0;
113         size_t          length = 0;
114         unsigned char   buff[20];
115
116         log_assert(packet != NULL);
117
118         switch (packet->data[0]) {
119         case 2:
120         case 3:
121                 /*
122                  * For a type 2 or 3 key the keyid is the last 64 bits of the
123                  * public modulus n, which is stored as an MPI from offset 8
124                  * onwards.
125                  */
126                 offset = (packet->data[8] << 8) +
127                         packet->data[9];
128                 offset = ((offset + 7) / 8) + 2;
129
130                 for (keyid = 0, i = 0; i < 8; i++) {
131                         keyid <<= 8;
132                         keyid += packet->data[offset++];
133                 }
134                 /*
135                  * Check for an RSA key; if not then log but accept anyway.
136                  * 1 == RSA
137                  * 2 == RSA Encrypt-Only
138                  * 3 == RSA Sign-Only
139                  */
140                 if (packet->data[7] < 1 || packet->data[7] > 3) {
141                         logthing(LOGTHING_NOTICE,
142                                 "Type 2 or 3 key, but not RSA: %llx (type %d)",
143                                 keyid,
144                                 packet->data[7]);
145                 }
146                 break;
147         case 4:
148                 get_fingerprint(packet, buff, &length);
149                 
150                 for (keyid = 0, i = 12; i < 20; i++) {
151                         keyid <<= 8;
152                         keyid += buff[i];
153                 }
154
155                 break;
156         default:
157                 logthing(LOGTHING_ERROR, "Unknown key type: %d",
158                                 packet->data[0]);
159         }
160
161         return keyid;
162 }
163
164 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
165                                                         *packets)
166 {
167         struct openpgp_packet_list *sorted, **cur, *next;
168
169         sorted = NULL;
170         while (packets != NULL) {
171                 cur = &sorted;
172                 while (*cur != NULL && compare_packets((*cur)->packet,
173                                 packets->packet) < 0) {
174                         cur = &((*cur)->next);
175                 }
176                 next = *cur;
177                 *cur = packets;
178                 packets = packets->next;
179                 (*cur)->next = next;
180         }
181
182         return sorted;
183 }
184
185 void get_skshash(struct openpgp_publickey *key, struct skshash *hash)
186 {
187         struct openpgp_packet_list *packets = NULL, *list_end = NULL;
188         struct openpgp_packet_list *curpacket;
189         struct md5_ctx md5_context;
190         struct openpgp_publickey *next;
191         uint32_t tmp;
192
193         /*
194          * We only want a single key, so clear any link to the next
195          * one for the period during the flatten.
196          */
197         next = key->next;
198         key->next = NULL;
199         flatten_publickey(key, &packets, &list_end);
200         key->next = next;
201         packets = sortpackets(packets);
202
203         md5_init_ctx(&md5_context);
204
205         for (curpacket = packets; curpacket != NULL;
206                         curpacket = curpacket->next) {
207                 tmp = htonl(curpacket->packet->tag);
208                 md5_process_bytes(&tmp, sizeof(tmp), &md5_context);
209                 tmp = htonl(curpacket->packet->length);
210                 md5_process_bytes(&tmp, sizeof(tmp), &md5_context);
211                 md5_process_bytes(curpacket->packet->data,
212                                 curpacket->packet->length,
213                                 &md5_context);
214         }
215
216         md5_finish_ctx(&md5_context, &hash->hash);
217         free_packet_list(packets);
218 }