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,
39 struct cmp_help_context_s
{
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
);
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.
53 signature_check( PKT_signature
*sig
, MD_HANDLE digest
)
55 return signature_check2( sig
, digest
, NULL
, NULL
, NULL
, NULL
);
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
);
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"));
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
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)
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
)
111 else if(pk
->backsig
==1)
113 log_info(_("WARNING: signing subkey %s has an invalid"
114 " cross-certification\n"),keystr_from_pk(pk
));
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 */
131 u32 a
= sig
->timestamp
;
132 int i
, nsig
= pubkey_get_nsig( sig
->pubkey_algo
);
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
]);
147 p
= mpi_get_buffer( sig
->data
[i
], &n
, NULL
);
148 md_write( md
, p
, n
);
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
);
167 do_check_messages( PKT_public_key
*pk
, PKT_signature
*sig
,
168 int *r_expired
, int *r_revoked
)
177 if( pk
->timestamp
> sig
->timestamp
)
179 ulong d
= pk
->timestamp
- sig
->timestamp
;
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
;
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
) {
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
);
215 if(pk
->is_revoked
&& r_revoked
)
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
)
228 struct cmp_help_context_s ctx
;
230 if( (rc
=do_check_messages(pk
,sig
,r_expired
,r_revoked
)) )
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 );
250 md_putc( digest
, sig
->pubkey_algo
);
251 md_putc( digest
, sig
->digest_algo
);
253 n
= sig
->hashed
->len
;
254 md_putc (digest
, (n
>> 8) );
255 md_putc (digest
, n
);
256 md_write (digest
, sig
->hashed
->data
, n
);
260 /* Two octets for the (empty) length of the hashed
267 buf
[0] = sig
->version
;
273 md_write( digest
, buf
, 6 );
277 result
= encode_md_value( pk
, NULL
, digest
, sig
->digest_algo
);
279 return G10ERR_GENERAL
;
282 rc
= pubkey_verify( pk
->pubkey_algo
, result
, sig
->data
, pk
->pkey
);
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
;
293 copy_public_key(ret_pk
,pk
);
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 ) {
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
);
318 if( sig
->version
>=4 ) {
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;
325 md_write( md
, buf
, 5 );
327 md_write( md
, uid
->name
, uid
->len
);
332 cache_sig_result ( PKT_signature
*sig
, int 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;
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
365 check_revocation_keys(PKT_public_key
*pk
,PKT_signature
*sig
)
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]));
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. */
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
)
392 for(i
=0;i
<pk
->numrevkeys
;i
++)
396 keyid_from_fingerprint(pk
->revkey
[i
].fpr
,MAX_FINGERPRINT_LEN
,keyid
);
398 if(keyid
[0]==sig
->keyid
[0] && keyid
[1]==sig
->keyid
[1])
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
);
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
422 check_backsig(PKT_public_key
*main_pk
,PKT_public_key
*sub_pk
,
423 PKT_signature
*backsig
)
428 if(!opt
.no_sig_cache
&& backsig
->flags
.checked
)
430 if((rc
=check_digest_algo(backsig
->digest_algo
)))
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
);
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
)
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*/
497 keyid_from_pk( pk
, keyid
);
498 if( keyid
[0] == sig
->keyid
[0] && keyid
[1] == sig
->keyid
[1] )
501 /* BUG: This is wrong for non-self-sigs.. needs to be the
503 if((rc
=do_check_messages(pk
,sig
,r_expired
,NULL
)))
505 return sig
->flags
.valid
? 0 : G10ERR_BAD_SIGN
;
509 if( (rc
=check_pubkey_algo(sig
->pubkey_algo
)) )
511 if( (rc
=check_digest_algo(algo
)) )
514 if( sig
->sig_class
== 0x20 ) { /* key revocation */
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
);
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
);
530 else if( sig
->sig_class
== 0x28 ) { /* subkey revocation */
531 KBNODE snode
= find_prev_kbnode( root
, node
, PKT_PUBLIC_SUBKEY
);
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
);
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
);
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] )
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
);
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
);
582 else { /* all other classes */
583 KBNODE unode
= find_prev_kbnode( root
, node
, PKT_USER_ID
);
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] )
596 rc
= do_check( pk
, sig
, md
, r_expired
, NULL
, ret_pk
);
599 rc
=do_check(check_pk
,sig
,md
,r_expired
,NULL
,ret_pk
);
601 rc
=signature_check2(sig
,md
,r_expiredate
,r_expired
,NULL
,ret_pk
);
603 cache_sig_result ( sig
, rc
);
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
;