Remove CVS Id tags.
[onak.git] / keyid.c
1 /*
2  * keyid.c - Routines to calculate key IDs.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <sys/types.h>
10
11 #include "assert.h"
12 #include "keyid.h"
13 #include "keystructs.h"
14 #include "log.h"
15 #include "md5.h"
16 #include "sha1.h"
17
18
19 /**
20  *      get_keyid - Given a public key returns the keyid.
21  *      @publickey: The key to calculate the id for.
22  */
23 uint64_t get_keyid(struct openpgp_publickey *publickey)
24 {
25         return (get_packetid(publickey->publickey));
26 }
27
28 /**
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.
33  *
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.
37  */
38 unsigned char *get_fingerprint(struct openpgp_packet *packet,
39         unsigned char *fingerprint,
40         size_t *len)
41 {
42         SHA1_CTX sha_ctx;
43         struct md5_ctx md5_context;
44         unsigned char c;
45         size_t         modlen, explen;
46
47         assert(fingerprint != NULL);
48         assert(len != NULL);
49
50         *len = 0;
51
52         switch (packet->data[0]) {
53         case 2:
54         case 3:
55                 md5_init_ctx(&md5_context);
56
57                 /*
58                  * MD5 the modulus and exponent.
59                  */
60                 modlen = ((packet->data[8] << 8) +
61                          packet->data[9] + 7) >> 3;
62                 md5_process_bytes(&packet->data[10], modlen, &md5_context);
63
64                 explen = ((packet->data[10+modlen] << 8) +
65                          packet->data[11+modlen] + 7) >> 3;
66                 md5_process_bytes(&packet->data[12 + modlen], explen,
67                                 &md5_context);
68
69                 md5_finish_ctx(&md5_context, fingerprint);
70                 *len = 16;
71
72                 break;
73
74         case 4:
75                 SHA1Init(&sha_ctx);
76                 /*
77                  * TODO: Can this be 0x99? Are all public key packets old
78                  * format with 2 bytes of length data?
79                  */
80                 c = 0x99;
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,
87                         packet->length);
88                 SHA1Final(fingerprint, &sha_ctx);
89                 *len = 20;
90
91                 break;
92         default:
93                 logthing(LOGTHING_ERROR, "Unknown key type: %d",
94                                 packet->data[0]);
95         }
96
97         return fingerprint;
98 }
99
100
101 /**
102  *      get_packetid - Given a PGP packet returns the keyid.
103  *      @packet: The packet to calculate the id for.
104  */
105 uint64_t get_packetid(struct openpgp_packet *packet)
106 {
107         uint64_t        keyid = 0;
108         int             offset = 0;
109         int             i = 0;
110         size_t          length = 0;
111         unsigned char   buff[20];
112
113         assert(packet != NULL);
114
115         switch (packet->data[0]) {
116         case 2:
117         case 3:
118                 /*
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
121                  * onwards.
122                  */
123                 offset = (packet->data[8] << 8) +
124                         packet->data[9];
125                 offset = ((offset + 7) / 8) + 2;
126
127                 for (keyid = 0, i = 0; i < 8; i++) {
128                         keyid <<= 8;
129                         keyid += packet->data[offset++];
130                 }
131                 /*
132                  * Check for an RSA key; if not then log but accept anyway.
133                  * 1 == RSA
134                  * 2 == RSA Encrypt-Only
135                  * 3 == RSA Sign-Only
136                  */
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)",
140                                 keyid,
141                                 packet->data[7]);
142                 }
143                 break;
144         case 4:
145                 get_fingerprint(packet, buff, &length);
146                 
147                 for (keyid = 0, i = 12; i < 20; i++) {
148                         keyid <<= 8;
149                         keyid += buff[i];
150                 }
151
152                 break;
153         default:
154                 logthing(LOGTHING_ERROR, "Unknown key type: %d",
155                                 packet->data[0]);
156         }
157
158         return keyid;
159 }