2 * keyid.c - Routines to calculate key IDs.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
13 #include "keystructs.h"
20 * get_keyid - Given a public key returns the keyid.
21 * @publickey: The key to calculate the id for.
23 uint64_t get_keyid(struct openpgp_publickey *publickey)
25 return (get_packetid(publickey->publickey));
29 * get_fingerprint - Given a public key returns the fingerprint.
30 * @publickey: The key to calculate the id for.
31 * @fingerprint: The fingerprint (must be at least 20 bytes of space).
32 * @len: The length of the returned fingerprint.
34 * This function returns the fingerprint for a given public key. As Type 3
35 * fingerprints are 16 bytes and Type 4 are 20 the len field indicates
36 * which we've returned.
38 unsigned char *get_fingerprint(struct openpgp_packet *packet,
39 unsigned char *fingerprint,
43 struct md5_ctx md5_context;
45 size_t modlen, explen;
47 assert(fingerprint != NULL);
52 switch (packet->data[0]) {
55 md5_init_ctx(&md5_context);
58 * MD5 the modulus and exponent.
60 modlen = ((packet->data[8] << 8) +
61 packet->data[9] + 7) >> 3;
62 md5_process_bytes(&packet->data[10], modlen, &md5_context);
64 explen = ((packet->data[10+modlen] << 8) +
65 packet->data[11+modlen] + 7) >> 3;
66 md5_process_bytes(&packet->data[12 + modlen], explen,
69 md5_finish_ctx(&md5_context, fingerprint);
77 * TODO: Can this be 0x99? Are all public key packets old
78 * format with 2 bytes of length data?
81 SHA1Update(&sha_ctx, &c, sizeof(c));
82 c = packet->length >> 8;
83 SHA1Update(&sha_ctx, &c, sizeof(c));
84 c = packet->length & 0xFF;
85 SHA1Update(&sha_ctx, &c, sizeof(c));
86 SHA1Update(&sha_ctx, packet->data,
88 SHA1Final(fingerprint, &sha_ctx);
93 logthing(LOGTHING_ERROR, "Unknown key type: %d",
102 * get_packetid - Given a PGP packet returns the keyid.
103 * @packet: The packet to calculate the id for.
105 uint64_t get_packetid(struct openpgp_packet *packet)
111 unsigned char buff[20];
113 assert(packet != NULL);
115 switch (packet->data[0]) {
119 * For a type 2 or 3 key the keyid is the last 64 bits of the
120 * public modulus n, which is stored as an MPI from offset 8
123 offset = (packet->data[8] << 8) +
125 offset = ((offset + 7) / 8) + 2;
127 for (keyid = 0, i = 0; i < 8; i++) {
129 keyid += packet->data[offset++];
132 * Check for an RSA key; if not then log but accept anyway.
134 * 2 == RSA Encrypt-Only
137 if (packet->data[7] < 1 || packet->data[7] > 3) {
138 logthing(LOGTHING_NOTICE,
139 "Type 2 or 3 key, but not RSA: %llx (type %d)",
145 get_fingerprint(packet, buff, &length);
147 for (keyid = 0, i = 12; i < 20; i++) {
154 logthing(LOGTHING_ERROR, "Unknown key type: %d",