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/sha.h>
42 * get_keyid - Given a public key returns the keyid.
43 * @publickey: The key to calculate the id for.
45 onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid)
47 return (get_packetid(publickey->publickey, keyid));
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.
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.
60 onak_status_t get_fingerprint(struct openpgp_packet *packet,
61 unsigned char *fingerprint,
64 struct sha1_ctx sha_ctx;
65 struct md5_ctx md5_context;
67 size_t modlen, explen;
69 if (fingerprint == NULL)
70 return ONAK_E_INVALID_PARAM;
72 return ONAK_E_INVALID_PARAM;
76 switch (packet->data[0]) {
79 md5_init(&md5_context);
82 * MD5 the modulus and exponent.
84 modlen = ((packet->data[8] << 8) +
85 packet->data[9] + 7) >> 3;
86 md5_update(&md5_context, modlen, &packet->data[10]);
88 explen = ((packet->data[10+modlen] << 8) +
89 packet->data[11+modlen] + 7) >> 3;
90 md5_update(&md5_context, explen, &packet->data[12 + modlen]);
93 md5_digest(&md5_context, *len, fingerprint);
100 * TODO: Can this be 0x99? Are all public key packets old
101 * format with 2 bytes of length data?
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,
112 sha1_digest(&sha_ctx, *len, fingerprint);
116 return ONAK_E_UNKNOWN_VER;
124 * get_packetid - Given a PGP packet returns the keyid.
125 * @packet: The packet to calculate the id for.
127 onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid)
132 unsigned char buff[20];
135 return ONAK_E_INVALID_PARAM;
137 switch (packet->data[0]) {
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
145 offset = (packet->data[8] << 8) +
147 offset = ((offset + 7) / 8) + 2;
149 for (*keyid = 0, i = 0; i < 8; i++) {
151 *keyid += packet->data[offset++];
154 * Check for an RSA key; if not return an error.
156 * 2 == RSA Encrypt-Only
159 if (packet->data[7] < 1 || packet->data[7] > 3) {
160 return ONAK_E_INVALID_PKT;
164 get_fingerprint(packet, buff, &length);
166 for (*keyid = 0, i = 12; i < 20; i++) {
173 return ONAK_E_UNKNOWN_VER;
179 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
182 struct openpgp_packet_list *sorted, **cur, *next;
185 while (packets != NULL) {
187 while (*cur != NULL && compare_packets((*cur)->packet,
188 packets->packet) < 0) {
189 cur = &((*cur)->next);
193 packets = packets->next;
200 onak_status_t get_skshash(struct openpgp_publickey *key, struct skshash *hash)
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;
209 * We only want a single key, so clear any link to the next
210 * one for the period during the flatten.
214 flatten_publickey(key, &packets, &list_end);
216 packets = sortpackets(packets);
218 md5_init(&md5_context);
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);
231 md5_digest(&md5_context, 16, (uint8_t *) &hash->hash);
232 free_packet_list(packets);
237 uint8_t hexdigit(char c)
239 if (c >= '0' && c <= '9')
241 else if (c >= 'a' && c <= 'f')
243 else if (c >= 'A' && c <= 'F')
249 int parse_skshash(char *search, struct skshash *hash)
253 len = strlen(search);
258 for (i = 0; i < len; i += 2) {
259 hash->hash[i >> 1] = (hexdigit(search[i]) << 4) +
260 hexdigit(search[i + 1]);