2 * keyid.c - Routines to calculate key IDs.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
10 #include <sys/types.h>
11 #include <arpa/inet.h>
14 #include "keystructs.h"
24 * get_keyid - Given a public key returns the keyid.
25 * @publickey: The key to calculate the id for.
27 uint64_t get_keyid(struct openpgp_publickey *publickey)
29 return (get_packetid(publickey->publickey));
33 * get_fingerprint - Given a public key returns the fingerprint.
34 * @publickey: The key to calculate the id for.
35 * @fingerprint: The fingerprint (must be at least 20 bytes of space).
36 * @len: The length of the returned fingerprint.
38 * This function returns the fingerprint for a given public key. As Type 3
39 * fingerprints are 16 bytes and Type 4 are 20 the len field indicates
40 * which we've returned.
42 unsigned char *get_fingerprint(struct openpgp_packet *packet,
43 unsigned char *fingerprint,
47 struct md5_ctx md5_context;
49 size_t modlen, explen;
51 log_assert(fingerprint != NULL);
52 log_assert(len != NULL);
56 switch (packet->data[0]) {
59 md5_init_ctx(&md5_context);
62 * MD5 the modulus and exponent.
64 modlen = ((packet->data[8] << 8) +
65 packet->data[9] + 7) >> 3;
66 md5_process_bytes(&packet->data[10], modlen, &md5_context);
68 explen = ((packet->data[10+modlen] << 8) +
69 packet->data[11+modlen] + 7) >> 3;
70 md5_process_bytes(&packet->data[12 + modlen], explen,
73 md5_finish_ctx(&md5_context, fingerprint);
81 * TODO: Can this be 0x99? Are all public key packets old
82 * format with 2 bytes of length data?
85 SHA1Update(&sha_ctx, &c, sizeof(c));
86 c = packet->length >> 8;
87 SHA1Update(&sha_ctx, &c, sizeof(c));
88 c = packet->length & 0xFF;
89 SHA1Update(&sha_ctx, &c, sizeof(c));
90 SHA1Update(&sha_ctx, packet->data,
92 SHA1Final(fingerprint, &sha_ctx);
97 logthing(LOGTHING_ERROR, "Unknown key type: %d",
106 * get_packetid - Given a PGP packet returns the keyid.
107 * @packet: The packet to calculate the id for.
109 uint64_t get_packetid(struct openpgp_packet *packet)
115 unsigned char buff[20];
117 log_assert(packet != NULL);
119 switch (packet->data[0]) {
123 * For a type 2 or 3 key the keyid is the last 64 bits of the
124 * public modulus n, which is stored as an MPI from offset 8
127 offset = (packet->data[8] << 8) +
129 offset = ((offset + 7) / 8) + 2;
131 for (keyid = 0, i = 0; i < 8; i++) {
133 keyid += packet->data[offset++];
136 * Check for an RSA key; if not then log but accept anyway.
138 * 2 == RSA Encrypt-Only
141 if (packet->data[7] < 1 || packet->data[7] > 3) {
142 logthing(LOGTHING_NOTICE,
143 "Type 2 or 3 key, but not RSA: %llx (type %d)",
149 get_fingerprint(packet, buff, &length);
151 for (keyid = 0, i = 12; i < 20; i++) {
158 logthing(LOGTHING_ERROR, "Unknown key type: %d",
165 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
168 struct openpgp_packet_list *sorted, **cur, *next;
171 while (packets != NULL) {
173 while (*cur != NULL && compare_packets((*cur)->packet,
174 packets->packet) < 0) {
175 cur = &((*cur)->next);
179 packets = packets->next;
186 void get_skshash(struct openpgp_publickey *key, struct skshash *hash)
188 struct openpgp_packet_list *packets = NULL, *list_end = NULL;
189 struct openpgp_packet_list *curpacket;
190 struct md5_ctx md5_context;
191 struct openpgp_publickey *next;
195 * We only want a single key, so clear any link to the next
196 * one for the period during the flatten.
200 flatten_publickey(key, &packets, &list_end);
202 packets = sortpackets(packets);
204 md5_init_ctx(&md5_context);
206 for (curpacket = packets; curpacket != NULL;
207 curpacket = curpacket->next) {
208 tmp = htonl(curpacket->packet->tag);
209 md5_process_bytes(&tmp, sizeof(tmp), &md5_context);
210 tmp = htonl(curpacket->packet->length);
211 md5_process_bytes(&tmp, sizeof(tmp), &md5_context);
212 md5_process_bytes(curpacket->packet->data,
213 curpacket->packet->length,
217 md5_finish_ctx(&md5_context, &hash->hash);
218 free_packet_list(packets);
221 uint8_t hexdigit(char c)
223 if (c >= '0' && c <= '9')
225 else if (c >= 'a' && c <= 'f')
227 else if (c >= 'A' && c <= 'F')
233 int parse_skshash(char *search, struct skshash *hash)
237 len = strlen(search);
242 for (i = 0; i < len; i += 2) {
243 hash->hash[i >> 1] = (hexdigit(search[i]) << 4) +
244 hexdigit(search[i + 1]);