2 * sigcheck.c - routines to check OpenPGP signatures
4 * Copyright 2012 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "keystructs.h"
30 #include <nettle/md5.h>
31 #include <nettle/ripemd160.h>
32 #include <nettle/sha.h>
39 int check_packet_sighash(struct openpgp_publickey *key,
40 struct openpgp_packet *packet,
41 struct openpgp_packet *sig)
45 size_t siglen, unhashedlen;
46 struct sha1_ctx sha1_context;
47 struct sha1x_ctx sha1x_context;
48 struct md5_ctx md5_context;
49 #ifdef NETTLE_WITH_RIPEMD160
50 struct ripemd160_ctx ripemd160_context;
52 #ifdef NETTLE_WITH_SHA224
53 struct sha224_ctx sha224_context;
55 #ifdef NETTLE_WITH_SHA256
56 struct sha256_ctx sha256_context;
58 #ifdef NETTLE_WITH_SHA384
59 struct sha384_ctx sha384_context;
61 #ifdef NETTLE_WITH_SHA512
62 struct sha512_ctx sha512_context;
65 uint8_t packetheader[5];
74 keyheader[1] = key->publickey->length >> 8;
75 keyheader[2] = key->publickey->length & 0xFF;
76 hashdata[0] = keyheader;
78 hashdata[1] = key->publickey->data;
79 hashlen[1] = key->publickey->length;
82 switch (sig->data[0]) {
85 hashtype = sig->data[16];
88 if (packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
89 packetheader[0] = 0x99;
90 packetheader[1] = packet->length >> 8;
91 packetheader[2] = packet->length & 0xFF;
92 hashdata[chunks] = packetheader;
97 // TODO: Things other than UIDS/subkeys?
98 hashdata[chunks] = packet->data;
99 hashlen[chunks] = packet->length;
103 hashdata[chunks] = &sig->data[2];
106 sighash = &sig->data[17];
109 hashtype = sig->data[3];
111 if (packet != NULL) {
112 if (packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
113 packetheader[0] = 0x99;
114 packetheader[1] = packet->length >> 8;
115 packetheader[2] = packet->length & 0xFF;
116 hashdata[chunks] = packetheader;
119 } else if (packet->tag == OPENPGP_PACKET_UID ||
120 packet->tag == OPENPGP_PACKET_UAT) {
121 packetheader[0] = (packet->tag ==
122 OPENPGP_PACKET_UID) ? 0xB4 : 0xD1;
123 packetheader[1] = packet->length >> 24;
124 packetheader[2] = (packet->length >> 16) & 0xFF;
125 packetheader[3] = (packet->length >> 8) & 0xFF;
126 packetheader[4] = packet->length & 0xFF;
127 hashdata[chunks] = packetheader;
131 hashdata[chunks] = packet->data;
132 hashlen[chunks] = packet->length;
136 hashdata[chunks] = sig->data;
137 hashlen[chunks] = siglen = (sig->data[4] << 8) +
143 v4trailer[2] = siglen >> 24;
144 v4trailer[3] = (siglen >> 16) & 0xFF;
145 v4trailer[4] = (siglen >> 8) & 0xFF;
146 v4trailer[5] = siglen & 0xFF;
147 hashdata[chunks] = v4trailer;
151 unhashedlen = (sig->data[siglen] << 8) +
152 sig->data[siglen + 1];
153 sighash = &sig->data[siglen + unhashedlen + 2];
156 get_keyid(key, &keyid);
157 logthing(LOGTHING_ERROR,
158 "Unknown signature version %d on 0x%016" PRIX64,
159 sig->data[0], keyid);
164 case OPENPGP_HASH_MD5:
165 md5_init(&md5_context);
166 for (i = 0; i < chunks; i++) {
167 md5_update(&md5_context, hashlen[i], hashdata[i]);
169 md5_digest(&md5_context, 16, hash);
171 case OPENPGP_HASH_SHA1:
172 sha1_init(&sha1_context);
173 for (i = 0; i < chunks; i++) {
174 sha1_update(&sha1_context, hashlen[i], hashdata[i]);
176 sha1_digest(&sha1_context, 20, hash);
178 case OPENPGP_HASH_RIPEMD160:
179 #ifdef NETTLE_WITH_RIPEMD160
180 ripemd160_init(&ripemd160_context);
181 for (i = 0; i < chunks; i++) {
182 ripemd160_update(&ripemd160_context, hashlen[i],
185 ripemd160_digest(&ripemd160_context, RIPEMD160_DIGEST_SIZE,
189 logthing(LOGTHING_INFO, "RIPEMD160 support not available.");
192 case OPENPGP_HASH_SHA1X:
193 sha1x_init(&sha1x_context);
194 for (i = 0; i < chunks; i++) {
195 sha1x_update(&sha1x_context, hashlen[i], hashdata[i]);
197 sha1x_digest(&sha1x_context, 20, hash);
199 case OPENPGP_HASH_SHA224:
200 #ifdef NETTLE_WITH_SHA224
201 sha224_init(&sha224_context);
202 for (i = 0; i < chunks; i++) {
203 sha224_update(&sha224_context, hashlen[i],
206 sha224_digest(&sha224_context, SHA224_DIGEST_SIZE, hash);
209 logthing(LOGTHING_INFO, "SHA224 support not available.");
212 case OPENPGP_HASH_SHA256:
213 #ifdef NETTLE_WITH_SHA256
214 sha256_init(&sha256_context);
215 for (i = 0; i < chunks; i++) {
216 sha256_update(&sha256_context, hashlen[i],
219 sha256_digest(&sha256_context, SHA256_DIGEST_SIZE, hash);
222 logthing(LOGTHING_INFO, "SHA256 support not available.");
225 case OPENPGP_HASH_SHA384:
226 #ifdef NETTLE_WITH_SHA384
227 sha384_init(&sha384_context);
228 for (i = 0; i < chunks; i++) {
229 sha384_update(&sha384_context, hashlen[i],
232 sha384_digest(&sha384_context, SHA384_DIGEST_SIZE, hash);
235 logthing(LOGTHING_INFO, "SHA384 support not available.");
238 case OPENPGP_HASH_SHA512:
239 #ifdef NETTLE_WITH_SHA512
240 sha512_init(&sha512_context);
241 for (i = 0; i < chunks; i++) {
242 sha512_update(&sha512_context, hashlen[i],
245 sha512_digest(&sha512_context, SHA512_DIGEST_SIZE, hash);
248 logthing(LOGTHING_INFO, "SHA512 support not available.");
252 get_keyid(key, &keyid);
253 logthing(LOGTHING_ERROR,
254 "Unsupported signature hash type %d on 0x%016" PRIX64,
260 logthing(LOGTHING_DEBUG, "Hash type: %d, %d chunks, "
261 "calculated: %02X%02X / actual: %02X%02X",
263 hash[0], hash[1], sighash[0], sighash[1]);
265 return (hash[0] == sighash[0] && hash[1] == sighash[1]);