* keyserver.c (path_makes_direct): New. (keyserver_spawn): Used here
[gnupg.git] / g10 / sig-check.c
blob15703346c7ce541830c454584ffabf8a152da4c2
1 /* sig-check.c - Check a signature
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 * 2004 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include "util.h"
29 #include "packet.h"
30 #include "memory.h"
31 #include "mpi.h"
32 #include "keydb.h"
33 #include "cipher.h"
34 #include "main.h"
35 #include "status.h"
36 #include "i18n.h"
37 #include "options.h"
39 struct cmp_help_context_s {
40 PKT_signature *sig;
41 MD_HANDLE md;
44 static int do_check( PKT_public_key *pk, PKT_signature *sig, MD_HANDLE digest,
45 int *r_expired, int *r_revoked, PKT_public_key *ret_pk);
47 /****************
48 * Check the signature which is contained in SIG.
49 * The MD_HANDLE should be currently open, so that this function
50 * is able to append some data, before finalizing the digest.
52 int
53 signature_check( PKT_signature *sig, MD_HANDLE digest )
55 return signature_check2( sig, digest, NULL, NULL, NULL, NULL );
58 int
59 signature_check2( PKT_signature *sig, MD_HANDLE digest, u32 *r_expiredate,
60 int *r_expired, int *r_revoked, PKT_public_key *ret_pk )
62 PKT_public_key *pk = xmalloc_clear( sizeof *pk );
63 int rc=0;
65 if( (rc=check_digest_algo(sig->digest_algo)) )
66 ; /* we don't have this digest */
67 else if((rc=check_pubkey_algo(sig->pubkey_algo)))
68 ; /* we don't have this pubkey algo */
69 else if(!md_algo_present(digest,sig->digest_algo))
71 /* Sanity check that the md has a context for the hash that the
72 sig is expecting. This can happen if a onepass sig header does
73 not match the actual sig, and also if the clearsign "Hash:"
74 header is missing or does not match the actual sig. */
76 log_info(_("WARNING: signature digest conflict in message\n"));
77 rc=G10ERR_GENERAL;
79 else if( get_pubkey( pk, sig->keyid ) )
80 rc = G10ERR_NO_PUBKEY;
81 else if(!pk->is_valid && !pk->is_primary)
82 rc=G10ERR_BAD_PUBKEY; /* you cannot have a good sig from an
83 invalid subkey */
84 else
86 if(r_expiredate)
87 *r_expiredate = pk->expiredate;
89 rc = do_check( pk, sig, digest, r_expired, r_revoked, ret_pk );
91 /* Check the backsig. This is a 0x19 signature from the
92 subkey on the primary key. The idea here is that it should
93 not be possible for someone to "steal" subkeys and claim
94 them as their own. The attacker couldn't actually use the
95 subkey, but they could try and claim ownership of any
96 signaures issued by it. */
97 if(rc==0 && !pk->is_primary && pk->backsig<2)
99 if(pk->backsig==0)
101 log_info(_("WARNING: signing subkey %s is not"
102 " cross-certified\n"),keystr_from_pk(pk));
103 log_info(_("please see %s for more information\n"),
104 "http://www.gnupg.org/faq/subkey-cross-certify.html");
105 /* --require-cross-certification makes this warning an
106 error. TODO: change the default to require this
107 after more keys have backsigs. */
108 if(opt.flags.require_cross_cert)
109 rc=G10ERR_GENERAL;
111 else if(pk->backsig==1)
113 log_info(_("WARNING: signing subkey %s has an invalid"
114 " cross-certification\n"),keystr_from_pk(pk));
115 rc=G10ERR_GENERAL;
120 free_public_key( pk );
122 if( !rc && sig->sig_class < 2 && is_status_enabled() ) {
123 /* This signature id works best with DLP algorithms because
124 * they use a random parameter for every signature. Instead of
125 * this sig-id we could have also used the hash of the document
126 * and the timestamp, but the drawback of this is, that it is
127 * not possible to sign more than one identical document within
128 * one second. Some remote batch processing applications might
129 * like this feature here */
130 MD_HANDLE md;
131 u32 a = sig->timestamp;
132 int i, nsig = pubkey_get_nsig( sig->pubkey_algo );
133 byte *p, *buffer;
135 md = md_open( DIGEST_ALGO_RMD160, 0);
136 md_putc( digest, sig->pubkey_algo );
137 md_putc( digest, sig->digest_algo );
138 md_putc( digest, (a >> 24) & 0xff );
139 md_putc( digest, (a >> 16) & 0xff );
140 md_putc( digest, (a >> 8) & 0xff );
141 md_putc( digest, a & 0xff );
142 for(i=0; i < nsig; i++ ) {
143 unsigned n = mpi_get_nbits( sig->data[i]);
145 md_putc( md, n>>8);
146 md_putc( md, n );
147 p = mpi_get_buffer( sig->data[i], &n, NULL );
148 md_write( md, p, n );
149 xfree(p);
151 md_final( md );
152 p = make_radix64_string( md_read( md, 0 ), 20 );
153 buffer = xmalloc( strlen(p) + 60 );
154 sprintf( buffer, "%s %s %lu",
155 p, strtimestamp( sig->timestamp ), (ulong)sig->timestamp );
156 write_status_text( STATUS_SIG_ID, buffer );
157 xfree(buffer);
158 xfree(p);
159 md_close(md);
162 return rc;
166 static int
167 do_check_messages( PKT_public_key *pk, PKT_signature *sig,
168 int *r_expired, int *r_revoked )
170 u32 cur_time;
172 if(r_expired)
173 *r_expired = 0;
174 if(r_revoked)
175 *r_revoked = 0;
177 if( pk->timestamp > sig->timestamp )
179 ulong d = pk->timestamp - sig->timestamp;
180 log_info(d==1
181 ?_("public key %s is %lu second newer than the signature\n")
182 :_("public key %s is %lu seconds newer than the signature\n"),
183 keystr_from_pk(pk),d );
184 if( !opt.ignore_time_conflict )
185 return G10ERR_TIME_CONFLICT; /* pubkey newer than signature */
188 cur_time = make_timestamp();
189 if( pk->timestamp > cur_time )
191 ulong d = pk->timestamp - cur_time;
192 log_info( d==1
193 ? _("key %s was created %lu second"
194 " in the future (time warp or clock problem)\n")
195 : _("key %s was created %lu seconds"
196 " in the future (time warp or clock problem)\n"),
197 keystr_from_pk(pk),d );
198 if( !opt.ignore_time_conflict )
199 return G10ERR_TIME_CONFLICT;
202 if( pk->expiredate && pk->expiredate < cur_time ) {
203 char buf[11];
204 if (opt.verbose)
205 log_info(_("NOTE: signature key %s expired %s\n"),
206 keystr_from_pk(pk), asctimestamp( pk->expiredate ) );
207 /* SIGEXPIRED is deprecated. Use KEYEXPIRED. */
208 sprintf(buf,"%lu",(ulong)pk->expiredate);
209 write_status_text(STATUS_KEYEXPIRED,buf);
210 write_status(STATUS_SIGEXPIRED);
211 if(r_expired)
212 *r_expired = 1;
215 if(pk->is_revoked && r_revoked)
216 *r_revoked=1;
218 return 0;
222 static int
223 do_check( PKT_public_key *pk, PKT_signature *sig, MD_HANDLE digest,
224 int *r_expired, int *r_revoked, PKT_public_key *ret_pk )
226 MPI result = NULL;
227 int rc=0;
228 struct cmp_help_context_s ctx;
230 if( (rc=do_check_messages(pk,sig,r_expired,r_revoked)) )
231 return rc;
233 /* make sure the digest algo is enabled (in case of a detached signature)*/
234 md_enable( digest, sig->digest_algo );
236 /* complete the digest */
237 if( sig->version >= 4 )
238 md_putc( digest, sig->version );
239 md_putc( digest, sig->sig_class );
240 if( sig->version < 4 ) {
241 u32 a = sig->timestamp;
242 md_putc( digest, (a >> 24) & 0xff );
243 md_putc( digest, (a >> 16) & 0xff );
244 md_putc( digest, (a >> 8) & 0xff );
245 md_putc( digest, a & 0xff );
247 else {
248 byte buf[6];
249 size_t n;
250 md_putc( digest, sig->pubkey_algo );
251 md_putc( digest, sig->digest_algo );
252 if( sig->hashed ) {
253 n = sig->hashed->len;
254 md_putc (digest, (n >> 8) );
255 md_putc (digest, n );
256 md_write (digest, sig->hashed->data, n);
257 n += 6;
259 else {
260 /* Two octets for the (empty) length of the hashed
261 section. */
262 md_putc (digest, 0);
263 md_putc (digest, 0);
264 n = 6;
266 /* add some magic */
267 buf[0] = sig->version;
268 buf[1] = 0xff;
269 buf[2] = n >> 24;
270 buf[3] = n >> 16;
271 buf[4] = n >> 8;
272 buf[5] = n;
273 md_write( digest, buf, 6 );
275 md_final( digest );
277 result = encode_md_value( pk, NULL, digest, sig->digest_algo );
278 if (!result)
279 return G10ERR_GENERAL;
280 ctx.sig = sig;
281 ctx.md = digest;
282 rc = pubkey_verify( pk->pubkey_algo, result, sig->data, pk->pkey );
283 mpi_free( result );
285 if( !rc && sig->flags.unknown_critical )
287 log_info(_("assuming bad signature from key %s"
288 " due to an unknown critical bit\n"),keystr_from_pk(pk));
289 rc = G10ERR_BAD_SIGN;
292 if(!rc && ret_pk)
293 copy_public_key(ret_pk,pk);
295 return rc;
299 static void
300 hash_uid_node( KBNODE unode, MD_HANDLE md, PKT_signature *sig )
302 PKT_user_id *uid = unode->pkt->pkt.user_id;
304 assert( unode->pkt->pkttype == PKT_USER_ID );
305 if( uid->attrib_data ) {
306 if( sig->version >=4 ) {
307 byte buf[5];
308 buf[0] = 0xd1; /* packet of type 17 */
309 buf[1] = uid->attrib_len >> 24; /* always use 4 length bytes */
310 buf[2] = uid->attrib_len >> 16;
311 buf[3] = uid->attrib_len >> 8;
312 buf[4] = uid->attrib_len;
313 md_write( md, buf, 5 );
315 md_write( md, uid->attrib_data, uid->attrib_len );
317 else {
318 if( sig->version >=4 ) {
319 byte buf[5];
320 buf[0] = 0xb4; /* indicates a userid packet */
321 buf[1] = uid->len >> 24; /* always use 4 length bytes */
322 buf[2] = uid->len >> 16;
323 buf[3] = uid->len >> 8;
324 buf[4] = uid->len;
325 md_write( md, buf, 5 );
327 md_write( md, uid->name, uid->len );
331 static void
332 cache_sig_result ( PKT_signature *sig, int result )
334 if ( !result ) {
335 sig->flags.checked = 1;
336 sig->flags.valid = 1;
338 else if ( result == G10ERR_BAD_SIGN ) {
339 sig->flags.checked = 1;
340 sig->flags.valid = 0;
342 else {
343 sig->flags.checked = 0;
344 sig->flags.valid = 0;
348 /* Check the revocation keys to see if any of them have revoked our
349 pk. sig is the revocation sig. pk is the key it is on. This code
350 will need to be modified if gpg ever becomes multi-threaded. Note
351 that this guarantees that a designated revocation sig will never be
352 considered valid unless it is actually valid, as well as being
353 issued by a revocation key in a valid direct signature. Note also
354 that this is written so that a revoked revoker can still issue
355 revocations: i.e. If A revokes B, but A is revoked, B is still
356 revoked. I'm not completely convinced this is the proper behavior,
357 but it matches how PGP does it. -dms */
359 /* Returns 0 if sig is valid (i.e. pk is revoked), non-0 if not
360 revoked. It is important that G10ERR_NO_PUBKEY is only returned
361 when a revocation signature is from a valid revocation key
362 designated in a revkey subpacket, but the revocation key itself
363 isn't present. */
365 check_revocation_keys(PKT_public_key *pk,PKT_signature *sig)
367 static int busy=0;
368 int i,rc=G10ERR_GENERAL;
370 assert(IS_KEY_REV(sig));
371 assert((sig->keyid[0]!=pk->keyid[0]) || (sig->keyid[0]!=pk->keyid[1]));
373 if(busy)
375 /* return an error (i.e. not revoked), but mark the pk as
376 uncacheable as we don't really know its revocation status
377 until it is checked directly. */
379 pk->dont_cache=1;
380 return rc;
383 busy=1;
385 /* printf("looking at %08lX with a sig from %08lX\n",(ulong)pk->keyid[1],
386 (ulong)sig->keyid[1]); */
388 /* is the issuer of the sig one of our revokers? */
389 if( !pk->revkey && pk->numrevkeys )
390 BUG();
391 else
392 for(i=0;i<pk->numrevkeys;i++)
394 u32 keyid[2];
396 keyid_from_fingerprint(pk->revkey[i].fpr,MAX_FINGERPRINT_LEN,keyid);
398 if(keyid[0]==sig->keyid[0] && keyid[1]==sig->keyid[1])
400 MD_HANDLE md;
402 md=md_open(sig->digest_algo,0);
403 hash_public_key(md,pk);
404 rc=signature_check(sig,md);
405 cache_sig_result(sig,rc);
406 break;
410 busy=0;
412 return rc;
415 /* Backsigs (0x19) have the same format as binding sigs (0x18), but
416 this function is simpler than check_key_signature in a few ways.
417 For example, there is no support for expiring backsigs since it is
418 questionable what such a thing actually means. Note also that the
419 sig cache check here, unlike other sig caches in GnuPG, is not
420 persistent. */
422 check_backsig(PKT_public_key *main_pk,PKT_public_key *sub_pk,
423 PKT_signature *backsig)
425 MD_HANDLE md;
426 int rc;
428 if(!opt.no_sig_cache && backsig->flags.checked)
430 if((rc=check_digest_algo(backsig->digest_algo)))
431 return rc;
433 return backsig->flags.valid? 0 : G10ERR_BAD_SIGN;
436 md=md_open(backsig->digest_algo,0);
437 hash_public_key(md,main_pk);
438 hash_public_key(md,sub_pk);
439 rc=do_check(sub_pk,backsig,md,NULL,NULL,NULL);
440 cache_sig_result(backsig,rc);
441 md_close(md);
443 return rc;
447 /****************
448 * check the signature pointed to by NODE. This is a key signature.
449 * If the function detects a self-signature, it uses the PK from
450 * ROOT and does not read any public key.
453 check_key_signature( KBNODE root, KBNODE node, int *is_selfsig )
455 return check_key_signature2(root, node, NULL, NULL, is_selfsig, NULL, NULL );
458 /* If check_pk is set, then use it to check the signature in node
459 rather than getting it from root or the keydb. If ret_pk is set,
460 fill in the public key that was used to verify the signature.
461 ret_pk is only meaningful when the verification was successful. */
462 /* TODO: add r_revoked here as well. It has the same problems as
463 r_expiredate and r_expired and the cache. */
465 check_key_signature2( KBNODE root, KBNODE node, PKT_public_key *check_pk,
466 PKT_public_key *ret_pk, int *is_selfsig,
467 u32 *r_expiredate, int *r_expired )
469 MD_HANDLE md;
470 PKT_public_key *pk;
471 PKT_signature *sig;
472 int algo;
473 int rc;
475 if( is_selfsig )
476 *is_selfsig = 0;
477 if( r_expiredate )
478 *r_expiredate = 0;
479 if( r_expired )
480 *r_expired = 0;
481 assert( node->pkt->pkttype == PKT_SIGNATURE );
482 assert( root->pkt->pkttype == PKT_PUBLIC_KEY );
484 pk = root->pkt->pkt.public_key;
485 sig = node->pkt->pkt.signature;
486 algo = sig->digest_algo;
488 /* Check whether we have cached the result of a previous signature
489 check. Note that we may no longer have the pubkey or hash
490 needed to verify a sig, but can still use the cached value. A
491 cache refresh detects and clears these cases. */
492 if ( !opt.no_sig_cache ) {
493 if (sig->flags.checked) { /*cached status available*/
494 if( is_selfsig ) {
495 u32 keyid[2];
497 keyid_from_pk( pk, keyid );
498 if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] )
499 *is_selfsig = 1;
501 /* BUG: This is wrong for non-self-sigs.. needs to be the
502 actual pk */
503 if((rc=do_check_messages(pk,sig,r_expired,NULL)))
504 return rc;
505 return sig->flags.valid? 0 : G10ERR_BAD_SIGN;
509 if( (rc=check_pubkey_algo(sig->pubkey_algo)) )
510 return rc;
511 if( (rc=check_digest_algo(algo)) )
512 return rc;
514 if( sig->sig_class == 0x20 ) { /* key revocation */
515 u32 keyid[2];
516 keyid_from_pk( pk, keyid );
518 /* is it a designated revoker? */
519 if(keyid[0]!=sig->keyid[0] || keyid[1]!=sig->keyid[1])
520 rc=check_revocation_keys(pk,sig);
521 else
523 md = md_open( algo, 0 );
524 hash_public_key( md, pk );
525 rc = do_check( pk, sig, md, r_expired, NULL, ret_pk );
526 cache_sig_result ( sig, rc );
527 md_close(md);
530 else if( sig->sig_class == 0x28 ) { /* subkey revocation */
531 KBNODE snode = find_prev_kbnode( root, node, PKT_PUBLIC_SUBKEY );
533 if( snode ) {
534 md = md_open( algo, 0 );
535 hash_public_key( md, pk );
536 hash_public_key( md, snode->pkt->pkt.public_key );
537 rc = do_check( pk, sig, md, r_expired, NULL, ret_pk );
538 cache_sig_result ( sig, rc );
539 md_close(md);
541 else
543 if (opt.verbose)
544 log_info (_("key %s: no subkey for subkey"
545 " revocation signature\n"),keystr_from_pk(pk));
546 rc = G10ERR_SIG_CLASS;
549 else if( sig->sig_class == 0x18 ) { /* key binding */
550 KBNODE snode = find_prev_kbnode( root, node, PKT_PUBLIC_SUBKEY );
552 if( snode ) {
553 if( is_selfsig ) { /* does this make sense????? */
554 u32 keyid[2]; /* it should always be a selfsig */
556 keyid_from_pk( pk, keyid );
557 if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] )
558 *is_selfsig = 1;
560 md = md_open( algo, 0 );
561 hash_public_key( md, pk );
562 hash_public_key( md, snode->pkt->pkt.public_key );
563 rc = do_check( pk, sig, md, r_expired, NULL, ret_pk );
564 cache_sig_result ( sig, rc );
565 md_close(md);
567 else
569 if (opt.verbose)
570 log_info(_("key %s: no subkey for subkey"
571 " binding signature\n"),keystr_from_pk(pk));
572 rc = G10ERR_SIG_CLASS;
575 else if( sig->sig_class == 0x1f ) { /* direct key signature */
576 md = md_open( algo, 0 );
577 hash_public_key( md, pk );
578 rc = do_check( pk, sig, md, r_expired, NULL, ret_pk );
579 cache_sig_result ( sig, rc );
580 md_close(md);
582 else { /* all other classes */
583 KBNODE unode = find_prev_kbnode( root, node, PKT_USER_ID );
585 if( unode ) {
586 u32 keyid[2];
588 keyid_from_pk( pk, keyid );
589 md = md_open( algo, 0 );
590 hash_public_key( md, pk );
591 hash_uid_node( unode, md, sig );
592 if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] )
594 if( is_selfsig )
595 *is_selfsig = 1;
596 rc = do_check( pk, sig, md, r_expired, NULL, ret_pk );
598 else if (check_pk)
599 rc=do_check(check_pk,sig,md,r_expired,NULL,ret_pk);
600 else
601 rc=signature_check2(sig,md,r_expiredate,r_expired,NULL,ret_pk);
603 cache_sig_result ( sig, rc );
604 md_close(md);
606 else
608 if (!opt.quiet)
609 log_info ("key %s: no user ID for key signature packet"
610 " of class %02x\n",keystr_from_pk(pk),sig->sig_class);
611 rc = G10ERR_SIG_CLASS;
615 return rc;