2 * keyid.c - Routines to calculate key IDs.
4 * Copyright 2002,2011 Jonathan McDowell <noodles@earth.li>
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.
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
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.
21 #include <sys/types.h>
22 #include <arpa/inet.h>
26 #include "keystructs.h"
33 #include <nettle/md5.h>
34 #include <nettle/ripemd160.h>
35 #include <nettle/sha.h>
43 * get_keyid - Given a public key returns the keyid.
44 * @publickey: The key to calculate the id for.
46 onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid)
48 return (get_packetid(publickey->publickey, keyid));
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.
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.
61 onak_status_t get_fingerprint(struct openpgp_packet *packet,
62 unsigned char *fingerprint,
65 struct sha1_ctx sha_ctx;
66 struct md5_ctx md5_context;
68 size_t modlen, explen;
70 if (fingerprint == NULL)
71 return ONAK_E_INVALID_PARAM;
73 return ONAK_E_INVALID_PARAM;
77 switch (packet->data[0]) {
80 md5_init(&md5_context);
83 * MD5 the modulus and exponent.
85 modlen = ((packet->data[8] << 8) +
86 packet->data[9] + 7) >> 3;
87 md5_update(&md5_context, modlen, &packet->data[10]);
89 explen = ((packet->data[10+modlen] << 8) +
90 packet->data[11+modlen] + 7) >> 3;
91 md5_update(&md5_context, explen, &packet->data[12 + modlen]);
94 md5_digest(&md5_context, *len, fingerprint);
101 * TODO: Can this be 0x99? Are all public key packets old
102 * format with 2 bytes of length data?
105 sha1_update(&sha_ctx, sizeof(c), &c);
106 c = packet->length >> 8;
107 sha1_update(&sha_ctx, sizeof(c), &c);
108 c = packet->length & 0xFF;
109 sha1_update(&sha_ctx, sizeof(c), &c);
110 sha1_update(&sha_ctx, packet->length,
113 sha1_digest(&sha_ctx, *len, fingerprint);
117 return ONAK_E_UNKNOWN_VER;
125 * get_packetid - Given a PGP packet returns the keyid.
126 * @packet: The packet to calculate the id for.
128 onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid)
133 unsigned char buff[20];
134 #ifdef NETTLE_WITH_RIPEMD160
135 struct ripemd160_ctx ripemd160_context;
140 return ONAK_E_INVALID_PARAM;
142 switch (packet->data[0]) {
146 * Old versions of GnuPG would put Elgamal keys inside
147 * a V3 key structure, then generate the keyid using
150 #ifdef NETTLE_WITH_RIPEMD160
151 if (packet->data[7] == 16) {
152 ripemd160_init(&ripemd160_context);
154 ripemd160_update(&ripemd160_context, 1, &data);
155 data = packet->length >> 8;
156 ripemd160_update(&ripemd160_context, 1, &data);
157 data = packet->length & 0xFF;
158 ripemd160_update(&ripemd160_context, 1, &data);
159 ripemd160_update(&ripemd160_context,
163 ripemd160_digest(&ripemd160_context,
164 RIPEMD160_DIGEST_SIZE,
167 for (*keyid = 0, i = 12; i < 20; i++) {
176 * Check for an RSA key; if not return an error.
178 * 2 == RSA Encrypt-Only
181 if (packet->data[7] < 1 || packet->data[7] > 3) {
182 return ONAK_E_INVALID_PKT;
186 * For a type 2 or 3 key the keyid is the last 64 bits of the
187 * public modulus n, which is stored as an MPI from offset 8
190 offset = (packet->data[8] << 8) +
192 offset = ((offset + 7) / 8) + 2;
194 for (*keyid = 0, i = 0; i < 8; i++) {
196 *keyid += packet->data[offset++];
200 get_fingerprint(packet, buff, &length);
202 for (*keyid = 0, i = 12; i < 20; i++) {
209 return ONAK_E_UNKNOWN_VER;
215 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
218 struct openpgp_packet_list *sorted, **cur, *next;
221 while (packets != NULL) {
223 while (*cur != NULL && compare_packets((*cur)->packet,
224 packets->packet) < 0) {
225 cur = &((*cur)->next);
229 packets = packets->next;
236 onak_status_t get_skshash(struct openpgp_publickey *key, struct skshash *hash)
238 struct openpgp_packet_list *packets = NULL, *list_end = NULL;
239 struct openpgp_packet_list *curpacket;
240 struct md5_ctx md5_context;
241 struct openpgp_publickey *next;
245 * We only want a single key, so clear any link to the next
246 * one for the period during the flatten.
250 flatten_publickey(key, &packets, &list_end);
252 packets = sortpackets(packets);
254 md5_init(&md5_context);
256 for (curpacket = packets; curpacket != NULL;
257 curpacket = curpacket->next) {
258 tmp = htonl(curpacket->packet->tag);
259 md5_update(&md5_context, sizeof(tmp), (void *) &tmp);
260 tmp = htonl(curpacket->packet->length);
261 md5_update(&md5_context, sizeof(tmp), (void *) &tmp);
262 md5_update(&md5_context,
263 curpacket->packet->length,
264 curpacket->packet->data);
267 md5_digest(&md5_context, 16, (uint8_t *) &hash->hash);
268 free_packet_list(packets);
273 uint8_t hexdigit(char c)
275 if (c >= '0' && c <= '9')
277 else if (c >= 'a' && c <= 'f')
279 else if (c >= 'A' && c <= 'F')
285 int parse_skshash(char *search, struct skshash *hash)
289 len = strlen(search);
294 for (i = 0; i < len; i += 2) {
295 hash->hash[i >> 1] = (hexdigit(search[i]) << 4) +
296 hexdigit(search[i + 1]);