2 * keyid.c - Routines to calculate key IDs.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
10 #include <arpa/inet.h>
13 #include "keystructs.h"
23 * get_keyid - Given a public key returns the keyid.
24 * @publickey: The key to calculate the id for.
26 uint64_t get_keyid(struct openpgp_publickey *publickey)
28 return (get_packetid(publickey->publickey));
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.
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.
41 unsigned char *get_fingerprint(struct openpgp_packet *packet,
42 unsigned char *fingerprint,
46 struct md5_ctx md5_context;
48 size_t modlen, explen;
50 log_assert(fingerprint != NULL);
51 log_assert(len != NULL);
55 switch (packet->data[0]) {
58 md5_init_ctx(&md5_context);
61 * MD5 the modulus and exponent.
63 modlen = ((packet->data[8] << 8) +
64 packet->data[9] + 7) >> 3;
65 md5_process_bytes(&packet->data[10], modlen, &md5_context);
67 explen = ((packet->data[10+modlen] << 8) +
68 packet->data[11+modlen] + 7) >> 3;
69 md5_process_bytes(&packet->data[12 + modlen], explen,
72 md5_finish_ctx(&md5_context, fingerprint);
80 * TODO: Can this be 0x99? Are all public key packets old
81 * format with 2 bytes of length data?
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,
91 SHA1Final(fingerprint, &sha_ctx);
96 logthing(LOGTHING_ERROR, "Unknown key type: %d",
105 * get_packetid - Given a PGP packet returns the keyid.
106 * @packet: The packet to calculate the id for.
108 uint64_t get_packetid(struct openpgp_packet *packet)
114 unsigned char buff[20];
116 log_assert(packet != NULL);
118 switch (packet->data[0]) {
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
126 offset = (packet->data[8] << 8) +
128 offset = ((offset + 7) / 8) + 2;
130 for (keyid = 0, i = 0; i < 8; i++) {
132 keyid += packet->data[offset++];
135 * Check for an RSA key; if not then log but accept anyway.
137 * 2 == RSA Encrypt-Only
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)",
148 get_fingerprint(packet, buff, &length);
150 for (keyid = 0, i = 12; i < 20; i++) {
157 logthing(LOGTHING_ERROR, "Unknown key type: %d",
164 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
167 struct openpgp_packet_list *sorted, **cur, *next;
170 while (packets != NULL) {
172 while (*cur != NULL && compare_packets((*cur)->packet,
173 packets->packet) < 0) {
174 cur = &((*cur)->next);
178 packets = packets->next;
185 void get_skshash(struct openpgp_publickey *key, struct skshash *hash)
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;
194 * We only want a single key, so clear any link to the next
195 * one for the period during the flatten.
199 flatten_publickey(key, &packets, &list_end);
201 packets = sortpackets(packets);
203 md5_init_ctx(&md5_context);
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,
216 md5_finish_ctx(&md5_context, &hash->hash);
217 free_packet_list(packets);