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>
25 #include "keystructs.h"
35 * get_keyid - Given a public key returns the keyid.
36 * @publickey: The key to calculate the id for.
38 uint64_t get_keyid(struct openpgp_publickey *publickey)
40 return (get_packetid(publickey->publickey));
44 * get_fingerprint - Given a public key returns the fingerprint.
45 * @publickey: The key to calculate the id for.
46 * @fingerprint: The fingerprint (must be at least 20 bytes of space).
47 * @len: The length of the returned fingerprint.
49 * This function returns the fingerprint for a given public key. As Type 3
50 * fingerprints are 16 bytes and Type 4 are 20 the len field indicates
51 * which we've returned.
53 unsigned char *get_fingerprint(struct openpgp_packet *packet,
54 unsigned char *fingerprint,
58 struct md5_ctx md5_context;
60 size_t modlen, explen;
62 log_assert(fingerprint != NULL);
63 log_assert(len != NULL);
67 switch (packet->data[0]) {
70 md5_init_ctx(&md5_context);
73 * MD5 the modulus and exponent.
75 modlen = ((packet->data[8] << 8) +
76 packet->data[9] + 7) >> 3;
77 md5_process_bytes(&packet->data[10], modlen, &md5_context);
79 explen = ((packet->data[10+modlen] << 8) +
80 packet->data[11+modlen] + 7) >> 3;
81 md5_process_bytes(&packet->data[12 + modlen], explen,
84 md5_finish_ctx(&md5_context, fingerprint);
92 * TODO: Can this be 0x99? Are all public key packets old
93 * format with 2 bytes of length data?
96 SHA1Update(&sha_ctx, &c, sizeof(c));
97 c = packet->length >> 8;
98 SHA1Update(&sha_ctx, &c, sizeof(c));
99 c = packet->length & 0xFF;
100 SHA1Update(&sha_ctx, &c, sizeof(c));
101 SHA1Update(&sha_ctx, packet->data,
103 SHA1Final(fingerprint, &sha_ctx);
108 logthing(LOGTHING_ERROR, "Unknown key type: %d",
117 * get_packetid - Given a PGP packet returns the keyid.
118 * @packet: The packet to calculate the id for.
120 uint64_t get_packetid(struct openpgp_packet *packet)
126 unsigned char buff[20];
128 log_assert(packet != NULL);
130 switch (packet->data[0]) {
134 * For a type 2 or 3 key the keyid is the last 64 bits of the
135 * public modulus n, which is stored as an MPI from offset 8
138 offset = (packet->data[8] << 8) +
140 offset = ((offset + 7) / 8) + 2;
142 for (keyid = 0, i = 0; i < 8; i++) {
144 keyid += packet->data[offset++];
147 * Check for an RSA key; if not then log but accept anyway.
149 * 2 == RSA Encrypt-Only
152 if (packet->data[7] < 1 || packet->data[7] > 3) {
153 logthing(LOGTHING_NOTICE,
154 "Type 2 or 3 key, but not RSA: %llx (type %d)",
160 get_fingerprint(packet, buff, &length);
162 for (keyid = 0, i = 12; i < 20; i++) {
169 logthing(LOGTHING_ERROR, "Unknown key type: %d",
176 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
179 struct openpgp_packet_list *sorted, **cur, *next;
182 while (packets != NULL) {
184 while (*cur != NULL && compare_packets((*cur)->packet,
185 packets->packet) < 0) {
186 cur = &((*cur)->next);
190 packets = packets->next;
197 void get_skshash(struct openpgp_publickey *key, struct skshash *hash)
199 struct openpgp_packet_list *packets = NULL, *list_end = NULL;
200 struct openpgp_packet_list *curpacket;
201 struct md5_ctx md5_context;
202 struct openpgp_publickey *next;
206 * We only want a single key, so clear any link to the next
207 * one for the period during the flatten.
211 flatten_publickey(key, &packets, &list_end);
213 packets = sortpackets(packets);
215 md5_init_ctx(&md5_context);
217 for (curpacket = packets; curpacket != NULL;
218 curpacket = curpacket->next) {
219 tmp = htonl(curpacket->packet->tag);
220 md5_process_bytes(&tmp, sizeof(tmp), &md5_context);
221 tmp = htonl(curpacket->packet->length);
222 md5_process_bytes(&tmp, sizeof(tmp), &md5_context);
223 md5_process_bytes(curpacket->packet->data,
224 curpacket->packet->length,
228 md5_finish_ctx(&md5_context, &hash->hash);
229 free_packet_list(packets);
232 uint8_t hexdigit(char c)
234 if (c >= '0' && c <= '9')
236 else if (c >= 'a' && c <= 'f')
238 else if (c >= 'A' && c <= 'F')
244 int parse_skshash(char *search, struct skshash *hash)
248 len = strlen(search);
253 for (i = 0; i < len; i += 2) {
254 hash->hash[i >> 1] = (hexdigit(search[i]) << 4) +
255 hexdigit(search[i + 1]);