2 * keyid.c - Routines to calculate key IDs.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
12 #include "keystructs.h"
18 * get_keyid - Given a public key returns the keyid.
19 * @publickey: The key to calculate the id for.
21 uint64_t get_keyid(struct openpgp_publickey *publickey)
23 return (get_packetid(publickey->publickey));
27 * get_fingerprint - Given a public key returns the fingerprint.
28 * @publickey: The key to calculate the id for.
29 * @fingerprint: The fingerprint (must be at least 20 bytes of space).
30 * @len: The length of the returned fingerprint.
32 * This function returns the fingerprint for a given public key. As Type 3
33 * fingerprints are 16 bytes and Type 4 are 20 the len field indicates
34 * which we've returned.
36 unsigned char *get_fingerprint(struct openpgp_packet *packet,
37 unsigned char *fingerprint,
43 unsigned char *buff = NULL;
44 size_t modlen, explen;
46 assert(fingerprint != NULL);
51 switch (packet->data[0]) {
57 * MD5 the modulus and exponent.
59 modlen = ((packet->data[8] << 8) +
60 packet->data[9] + 7) >> 3;
61 md5_write(&md5_ctx, &packet->data[10], modlen);
63 explen = ((packet->data[10+modlen] << 8) +
64 packet->data[11+modlen] + 7) >> 3;
65 md5_write(&md5_ctx, &packet->data[12 + modlen], explen);
68 buff = md5_read(&md5_ctx);
71 memcpy(fingerprint, buff, *len);
78 * TODO: Can this be 0x99? Are all public key packets old
79 * format with 2 bytes of length data?
82 sha1_write(&sha_ctx, &c, sizeof(c));
83 c = packet->length >> 8;
84 sha1_write(&sha_ctx, &c, sizeof(c));
85 c = packet->length & 0xFF;
86 sha1_write(&sha_ctx, &c, sizeof(c));
87 sha1_write(&sha_ctx, packet->data,
90 buff = sha1_read(&sha_ctx);
93 memcpy(fingerprint, buff, *len);
96 fprintf(stderr, "Unknown key type: %d\n",
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 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 * We need to ensure it's an RSA key.
128 if (packet->data[7] == 1) {
129 offset = (packet->data[8] << 8) +
131 offset = ((offset + 7) / 8) + 2;
133 for (keyid = 0, i = 0; i < 8; i++) {
135 keyid += packet->data[offset++];
138 fputs("Type 2 or 3 key, but not RSA.\n", stderr);
142 get_fingerprint(packet, buff, &length);
144 for (keyid = 0, i = 12; i < 20; i++) {
151 fprintf(stderr, "Unknown key type: %d\n",