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_packetid - Given a PGP packet returns the keyid.
28 * @packet: The packet to calculate the id for.
30 uint64_t get_packetid(struct openpgp_packet *packet)
37 unsigned char *buff = NULL;
39 assert(packet != NULL);
41 switch (packet->data[0]) {
45 * For a type 2 or 3 key the keyid is the last 64 bits of the
46 * public modulus n, which is stored as an MPI from offset 8
49 * We need to ensure it's an RSA key.
51 if (packet->data[7] == 1) {
52 offset = (packet->data[8] << 8) +
54 offset = ((offset + 7) / 8) + 2;
56 for (keyid = 0, i = 0; i < 8; i++) {
58 keyid += packet->data[offset++];
61 fputs("Type 2 or 3 key, but not RSA.\n", stderr);
66 * For a type 4 key the keyid is the last 64 bits of the
67 * fingerprint, which is the 160 bit SHA-1 hash of the packet
68 * tag, 2 octet packet length and the public key packet
69 * including version field.
73 * TODO: Can this be 0x99? Are all public key packets old
74 * format with 2 bytes of length data?
77 sha1_write(&sha_ctx, &c, sizeof(c));
78 c = packet->length >> 8;
79 sha1_write(&sha_ctx, &c, sizeof(c));
80 c = packet->length & 0xFF;
81 sha1_write(&sha_ctx, &c, sizeof(c));
82 sha1_write(&sha_ctx, packet->data,
85 buff = sha1_read(&sha_ctx);
89 for (keyid = 0, i = 12; i < 20; i++) {
96 fprintf(stderr, "Unknown key type: %d\n",