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, see <https://www.gnu.org/licenses/>.
21 #include "build-config.h"
22 #include "decodekey.h"
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];
75 keyheader[1] = key->publickey->length >> 8;
76 keyheader[2] = key->publickey->length & 0xFF;
77 hashdata[0] = keyheader;
79 hashdata[1] = key->publickey->data;
80 hashlen[1] = key->publickey->length;
83 switch (sig->data[0]) {
86 hashtype = sig->data[16];
89 if (packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
90 packetheader[0] = 0x99;
91 packetheader[1] = packet->length >> 8;
92 packetheader[2] = packet->length & 0xFF;
93 hashdata[chunks] = packetheader;
98 // TODO: Things other than UIDS/subkeys?
99 hashdata[chunks] = packet->data;
100 hashlen[chunks] = packet->length;
104 hashdata[chunks] = &sig->data[2];
107 sighash = &sig->data[17];
110 hashtype = sig->data[3];
112 /* Check to see if this is an X509 based signature */
113 if (sig->data[2] == 0 || sig->data[2] == 100) {
117 res = parse_subpackets(&sig->data[4],
118 sig->length - 4, &len,
120 if (res != ONAK_E_OK) {
121 /* If it parses badly, reject it */
125 /* No unhashed data */
126 sig->data[4 + len] == 0 &&
127 sig->data[5 + len] == 0 &&
128 /* Dummy 0 checksum */
129 sig->data[6 + len] == 0 &&
130 sig->data[7 + len] == 0 &&
132 sig->data[8 + len] == 0 &&
133 sig->data[9 + len] == 1 &&
134 sig->data[10 + len] == 1) {
135 get_keyid(key, &keyid);
136 logthing(LOGTHING_DEBUG,
137 "Skipping X509 signature on 0x%016"
144 if (packet != NULL) {
145 if (packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
146 packetheader[0] = 0x99;
147 packetheader[1] = packet->length >> 8;
148 packetheader[2] = packet->length & 0xFF;
149 hashdata[chunks] = packetheader;
152 } else if (packet->tag == OPENPGP_PACKET_UID ||
153 packet->tag == OPENPGP_PACKET_UAT) {
154 packetheader[0] = (packet->tag ==
155 OPENPGP_PACKET_UID) ? 0xB4 : 0xD1;
156 packetheader[1] = packet->length >> 24;
157 packetheader[2] = (packet->length >> 16) & 0xFF;
158 packetheader[3] = (packet->length >> 8) & 0xFF;
159 packetheader[4] = packet->length & 0xFF;
160 hashdata[chunks] = packetheader;
164 hashdata[chunks] = packet->data;
165 hashlen[chunks] = packet->length;
169 hashdata[chunks] = sig->data;
170 hashlen[chunks] = siglen = (sig->data[4] << 8) +
172 if (siglen > sig->length) {
173 /* Signature data exceed packet length, bogus */
180 v4trailer[2] = siglen >> 24;
181 v4trailer[3] = (siglen >> 16) & 0xFF;
182 v4trailer[4] = (siglen >> 8) & 0xFF;
183 v4trailer[5] = siglen & 0xFF;
184 hashdata[chunks] = v4trailer;
188 unhashedlen = (sig->data[siglen] << 8) +
189 sig->data[siglen + 1];
190 sighash = &sig->data[siglen + unhashedlen + 2];
193 get_keyid(key, &keyid);
194 logthing(LOGTHING_ERROR,
195 "Unknown signature version %d on 0x%016" PRIX64,
196 sig->data[0], keyid);
201 case OPENPGP_HASH_MD5:
202 md5_init(&md5_context);
203 for (i = 0; i < chunks; i++) {
204 md5_update(&md5_context, hashlen[i], hashdata[i]);
206 md5_digest(&md5_context, 16, hash);
208 case OPENPGP_HASH_SHA1:
209 sha1_init(&sha1_context);
210 for (i = 0; i < chunks; i++) {
211 sha1_update(&sha1_context, hashlen[i], hashdata[i]);
213 sha1_digest(&sha1_context, 20, hash);
215 case OPENPGP_HASH_RIPEMD160:
216 #ifdef NETTLE_WITH_RIPEMD160
217 ripemd160_init(&ripemd160_context);
218 for (i = 0; i < chunks; i++) {
219 ripemd160_update(&ripemd160_context, hashlen[i],
222 ripemd160_digest(&ripemd160_context, RIPEMD160_DIGEST_SIZE,
226 logthing(LOGTHING_INFO, "RIPEMD160 support not available.");
229 case OPENPGP_HASH_SHA1X:
230 sha1x_init(&sha1x_context);
231 for (i = 0; i < chunks; i++) {
232 sha1x_update(&sha1x_context, hashlen[i], hashdata[i]);
234 sha1x_digest(&sha1x_context, 20, hash);
236 case OPENPGP_HASH_SHA224:
237 #ifdef NETTLE_WITH_SHA224
238 sha224_init(&sha224_context);
239 for (i = 0; i < chunks; i++) {
240 sha224_update(&sha224_context, hashlen[i],
243 sha224_digest(&sha224_context, SHA224_DIGEST_SIZE, hash);
246 logthing(LOGTHING_INFO, "SHA224 support not available.");
249 case OPENPGP_HASH_SHA256:
250 #ifdef NETTLE_WITH_SHA256
251 sha256_init(&sha256_context);
252 for (i = 0; i < chunks; i++) {
253 sha256_update(&sha256_context, hashlen[i],
256 sha256_digest(&sha256_context, SHA256_DIGEST_SIZE, hash);
259 logthing(LOGTHING_INFO, "SHA256 support not available.");
262 case OPENPGP_HASH_SHA384:
263 #ifdef NETTLE_WITH_SHA384
264 sha384_init(&sha384_context);
265 for (i = 0; i < chunks; i++) {
266 sha384_update(&sha384_context, hashlen[i],
269 sha384_digest(&sha384_context, SHA384_DIGEST_SIZE, hash);
272 logthing(LOGTHING_INFO, "SHA384 support not available.");
275 case OPENPGP_HASH_SHA512:
276 #ifdef NETTLE_WITH_SHA512
277 sha512_init(&sha512_context);
278 for (i = 0; i < chunks; i++) {
279 sha512_update(&sha512_context, hashlen[i],
282 sha512_digest(&sha512_context, SHA512_DIGEST_SIZE, hash);
285 logthing(LOGTHING_INFO, "SHA512 support not available.");
289 get_keyid(key, &keyid);
290 logthing(LOGTHING_ERROR,
291 "Unsupported signature hash type %d on 0x%016" PRIX64,
297 logthing(LOGTHING_DEBUG, "Hash type: %d, %d chunks, "
298 "calculated: %02X%02X / actual: %02X%02X",
300 hash[0], hash[1], sighash[0], sighash[1]);
302 return (hash[0] == sighash[0] && hash[1] == sighash[1]);