Coverage Report

Created: 2026-10-08 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/sig-check.c
Line
Count
Source
1
/* sig-check.c -  Check a signature
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3
 *               2004, 2006 Free Software Foundation, Inc.
4
 * Copyright (C) 2015, 2016 g10 Code GmbH
5
 *
6
 * This file is part of GnuPG.
7
 *
8
 * GnuPG is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * GnuPG is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <config.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include "gpg.h"
28
#include "../common/util.h"
29
#include "packet.h"
30
#include "keydb.h"
31
#include "main.h"
32
#include "../common/status.h"
33
#include "../common/i18n.h"
34
#include "options.h"
35
#include "pkglue.h"
36
#include "../common/compliance.h"
37
38
static int check_signature_end (PKT_public_key *pk, PKT_signature *sig,
39
        gcry_md_hd_t digest,
40
                                const void *extrahash, size_t extrahashlen,
41
        int *r_expired, int *r_revoked,
42
        PKT_public_key *ret_pk);
43
44
static int check_signature_end_simple (PKT_public_key *pk, PKT_signature *sig,
45
                                       gcry_md_hd_t digest,
46
                                       const void *extrahash,
47
                                       size_t extrahashlen);
48
49
50
/* Statistics for signature verification.  */
51
struct
52
{
53
  unsigned int total;  /* Total number of verifications.  */
54
  unsigned int cached; /* Number of seen cache entries.  */
55
  unsigned int goodsig;/* Number of good verifications from the cache.  */
56
  unsigned int badsig; /* Number of bad verifications from the cache.  */
57
} cache_stats;
58
59
60
/* Dump verification stats.  */
61
void
62
sig_check_dump_stats (void)
63
0
{
64
0
  log_info ("sig_cache: total=%u cached=%u good=%u bad=%u\n",
65
0
            cache_stats.total, cache_stats.cached,
66
0
            cache_stats.goodsig, cache_stats.badsig);
67
0
}
68
69
70
static gpg_error_t
71
check_key_verify_compliance (PKT_public_key *pk)
72
0
{
73
0
  gpg_error_t err = 0;
74
75
0
  if (!gnupg_pk_is_allowed (opt.compliance, PK_USE_VERIFICATION,
76
0
                            pk->pubkey_algo, 0, pk->pkey,
77
0
                            nbits_from_pk (pk),
78
0
                            NULL))
79
0
    {
80
      /* Compliance failure.  */
81
0
      log_error (_("key %s may not be used for signing in %s mode\n"),
82
0
                 keystr_from_pk (pk),
83
0
                 gnupg_compliance_option_string (opt.compliance));
84
0
      err = gpg_error (GPG_ERR_PUBKEY_ALGO);
85
0
    }
86
87
0
  return err;
88
0
}
89
90
91
/* Check a signature.
92
 *
93
 * Looks up the public key that created the signature (SIG->KEYID)
94
 * from the key db.  Makes sure that the signature is valid (it was
95
 * not created prior to the key, the public key was created in the
96
 * past, and the signature does not include any unsupported critical
97
 * features), finishes computing the hash of the signature data, and
98
 * checks that the signature verifies the digest.  If the key that
99
 * generated the signature is a subkey, this function also verifies
100
 * that there is a valid backsig from the subkey to the primary key.
101
 * Finally, if status fd is enabled and the signature class is 0x00 or
102
 * 0x01, then a STATUS_SIG_ID is emitted on the status fd.
103
 *
104
 * SIG is the signature to check.
105
 *
106
 * DIGEST contains a valid hash context that already includes the
107
 * signed data.  This function adds the relevant meta-data from the
108
 * signature packet to compute the final hash.  (See Section 5.2 of
109
 * RFC 4880: "The concatenation of the data being signed and the
110
 * signature data from the version number through the hashed subpacket
111
 * data (inclusive) is hashed.")
112
 *
113
 * EXTRAHASH and EXTRAHASHLEN is additional data which is hashed with
114
 * v5 signatures.  They may be NULL to use the default.
115
 *
116
 * If FORCED_PK is not NULL this public key is used to verify the
117
 * signature and no other public key is looked up.  This is used to
118
 * verify against a key included in the signature.
119
 *
120
 * If R_EXPIREDATE is not NULL, R_EXPIREDATE is set to the key's
121
 * expiry.
122
 *
123
 * If R_EXPIRED is not NULL, *R_EXPIRED is set to 1 if PK has expired
124
 * (0 otherwise).  Note: PK being expired does not cause this function
125
 * to fail.
126
 *
127
 * If R_REVOKED is not NULL, *R_REVOKED is set to 1 if PK has been
128
 * revoked (0 otherwise).  Note: PK being revoked does not cause this
129
 * function to fail.
130
 *
131
 * If R_PK is not NULL, the public key is stored at that address if it
132
 * was found; other wise NULL is stored.
133
 *
134
 * If R_KEYBLOCK is not NULL, the entire keyblock used to verify the
135
 * signature is stored at that address.  If no key was found or on
136
 * some other errors NULL is stored there.  The callers needs to
137
 * release the keyblock using release_kbnode (kb).
138
 *
139
 * Returns 0 on success.  An error code otherwise.  */
140
gpg_error_t
141
check_signature (ctrl_t ctrl,
142
                 PKT_signature *sig, gcry_md_hd_t digest,
143
                 const void *extrahash, size_t extrahashlen,
144
                 PKT_public_key *forced_pk,
145
                 u32 *r_expiredate, int *r_expired, int *r_revoked,
146
                 PKT_public_key **r_pk, kbnode_t *r_keyblock)
147
0
{
148
0
  int rc=0;
149
0
  PKT_public_key *pk;
150
151
0
  if (r_expiredate)
152
0
    *r_expiredate = 0;
153
0
  if (r_expired)
154
0
    *r_expired = 0;
155
0
  if (r_revoked)
156
0
    *r_revoked = 0;
157
0
  if (r_pk)
158
0
    *r_pk = NULL;
159
0
  if (r_keyblock)
160
0
    *r_keyblock = NULL;
161
162
0
  pk = xtrycalloc (1, sizeof *pk);
163
0
  if (!pk)
164
0
    return gpg_error_from_syserror ();
165
166
0
  if  ((rc=openpgp_md_test_algo(sig->digest_algo)))
167
0
    {
168
      /* We don't have this digest. */
169
0
    }
170
0
  else if (!gnupg_digest_is_allowed (opt.compliance, 0, sig->digest_algo))
171
0
    {
172
      /* Compliance failure.  */
173
0
      log_info (_("digest algorithm '%s' may not be used in %s mode\n"),
174
0
                gcry_md_algo_name (sig->digest_algo),
175
0
                gnupg_compliance_option_string (opt.compliance));
176
0
      rc = gpg_error (GPG_ERR_DIGEST_ALGO);
177
0
    }
178
0
  else if ((rc=openpgp_pk_test_algo(sig->pubkey_algo)))
179
0
    {
180
      /* We don't have this pubkey algo. */
181
0
    }
182
0
  else if (!gcry_md_is_enabled (digest,sig->digest_algo))
183
0
    {
184
      /* Sanity check that the md has a context for the hash that the
185
       * sig is expecting.  This can happen if a onepass sig header
186
       * does not match the actual sig, and also if the clearsign
187
       * "Hash:" header is missing or does not match the actual sig. */
188
0
      log_info(_("WARNING: signature digest conflict in message\n"));
189
0
      rc = gpg_error (GPG_ERR_GENERAL);
190
0
    }
191
0
  else if (get_pubkey_for_sig (ctrl, pk, sig, forced_pk, r_keyblock))
192
0
    rc = gpg_error (GPG_ERR_NO_PUBKEY);
193
0
  else if ((rc = check_key_verify_compliance (pk)))
194
0
    ;/* Compliance failure.  */
195
0
  else if (!pk->flags.valid)
196
0
    {
197
      /* You cannot have a good sig from an invalid key.  */
198
0
      rc = gpg_error (GPG_ERR_BAD_PUBKEY);
199
0
    }
200
0
  else
201
0
    {
202
0
      if (r_expiredate)
203
0
        *r_expiredate = pk->expiredate;
204
205
0
      rc = check_signature_end (pk, sig, digest, extrahash, extrahashlen,
206
0
                                r_expired, r_revoked, NULL);
207
208
      /* Check the backsig.  This is a back signature (0x19) from
209
       * the subkey on the primary key.  The idea here is that it
210
       * should not be possible for someone to "steal" subkeys and
211
       * claim them as their own.  The attacker couldn't actually
212
       * use the subkey, but they could try and claim ownership of
213
       * any signatures issued by it.  */
214
0
      if (!rc && !pk->flags.primary && pk->flags.backsig < 2)
215
0
        {
216
0
          if (!pk->flags.backsig)
217
0
            {
218
0
              log_info (_("WARNING: signing subkey %s is not"
219
0
                          " cross-certified\n"),keystr_from_pk(pk));
220
0
              log_info (_("please see %s for more information\n"),
221
0
                        "https://gnupg.org/faq/subkey-cross-certify.html");
222
              /* The default option --require-cross-certification
223
               * makes this warning an error.  */
224
0
              if (opt.flags.require_cross_cert)
225
0
                rc = gpg_error (GPG_ERR_GENERAL);
226
0
            }
227
0
          else if(pk->flags.backsig == 1)
228
0
            {
229
0
              log_info (_("WARNING: signing subkey %s has an invalid"
230
0
                          " cross-certification\n"), keystr_from_pk(pk));
231
0
              rc = gpg_error (GPG_ERR_GENERAL);
232
0
            }
233
0
        }
234
235
0
    }
236
237
0
    if( !rc && sig->sig_class < 2 && is_status_enabled() ) {
238
  /* This signature id works best with DLP algorithms because
239
   * they use a random parameter for every signature.  Instead of
240
   * this sig-id we could have also used the hash of the document
241
   * and the timestamp, but the drawback of this is, that it is
242
   * not possible to sign more than one identical document within
243
   * one second.  Some remote batch processing applications might
244
   * like this feature here.
245
         *
246
         * Note that before 2.0.10, we used RIPE-MD160 for the hash
247
         * and accidentally didn't include the timestamp and algorithm
248
         * information in the hash.  Given that this feature is not
249
         * commonly used and that a replay attacks detection should
250
         * not solely be based on this feature (because it does not
251
         * work with RSA), we take the freedom and switch to SHA-1
252
         * with 2.0.10 to take advantage of hardware supported SHA-1
253
         * implementations.  We also include the missing information
254
         * in the hash.  Note also the SIG_ID as computed by gpg 1.x
255
         * and gpg 2.x didn't matched either because 2.x used to print
256
         * MPIs not in PGP format.  */
257
0
  u32 a = sig->timestamp;
258
0
  int nsig = pubkey_get_nsig( sig->pubkey_algo );
259
0
  unsigned char *p, *buffer;
260
0
        size_t n, nbytes;
261
0
        int i;
262
0
        char hashbuf[20];  /* We use SHA-1 here.  */
263
264
0
      nbytes = 6;
265
0
      for (i=0; i < nsig; i++ )
266
0
        {
267
0
          if (gcry_mpi_get_flag (sig->data[i], GCRYMPI_FLAG_OPAQUE))
268
0
            {
269
0
              unsigned int nbits;
270
271
0
              gcry_mpi_get_opaque (sig->data[i], &nbits);
272
0
              n = (nbits+7)/8 + 2;
273
0
            }
274
0
          else if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &n, sig->data[i]))
275
0
            BUG();
276
0
          nbytes += n;
277
0
        }
278
279
      /* Make buffer large enough to be later used as output buffer.  */
280
0
      if (nbytes < 100)
281
0
        nbytes = 100;
282
0
      nbytes += 10;  /* Safety margin.  */
283
284
      /* Fill and hash buffer.  */
285
0
      buffer = p = xmalloc (nbytes);
286
0
      *p++ = sig->pubkey_algo;
287
0
      *p++ = sig->digest_algo;
288
0
      *p++ = (a >> 24) & 0xff;
289
0
      *p++ = (a >> 16) & 0xff;
290
0
      *p++ = (a >>  8) & 0xff;
291
0
      *p++ =  a & 0xff;
292
0
      nbytes -= 6;
293
0
      for (i=0; i < nsig; i++ )
294
0
        {
295
0
          if (gcry_mpi_get_flag (sig->data[i], GCRYMPI_FLAG_OPAQUE))
296
0
            {
297
0
              const byte *sigdata;
298
0
              unsigned int nbits;
299
300
0
              sigdata = gcry_mpi_get_opaque (sig->data[i], &nbits);
301
0
              n = (nbits+7)/8;
302
0
              p[0] = nbits >> 8;
303
0
              p[1] = (nbits & 0xff);
304
0
              memcpy (p+2, sigdata, n);
305
0
              n += 2;
306
0
            }
307
0
          else if (gcry_mpi_print (GCRYMPI_FMT_PGP, p, nbytes, &n, sig->data[i]))
308
0
            BUG();
309
0
          p += n;
310
0
          nbytes -= n;
311
0
        }
312
0
      gcry_md_hash_buffer (GCRY_MD_SHA1, hashbuf, buffer, p-buffer);
313
314
0
      p = make_radix64_string (hashbuf, 20);
315
0
      sprintf (buffer, "%s %s %lu",
316
0
               p, strtimestamp (sig->timestamp), (ulong)sig->timestamp);
317
0
      xfree (p);
318
0
      write_status_text (STATUS_SIG_ID, buffer);
319
0
      xfree (buffer);
320
0
    }
321
322
0
  if (r_pk)
323
0
    *r_pk = pk;
324
0
  else
325
0
    {
326
0
      release_public_key_parts (pk);
327
0
      xfree (pk);
328
0
    }
329
330
0
  return rc;
331
0
}
332
333
334
/* The signature SIG was generated with the public key PK.  Check
335
 * whether the signature is valid in the following sense:
336
 *
337
 *   - Make sure the public key was created before the signature was
338
 *     generated.
339
 *
340
 *   - Make sure the public key was created in the past
341
 *
342
 *   - Check whether PK has expired (set *R_EXPIRED to 1 if so and 0
343
 *     otherwise)
344
 *
345
 *   - Check whether PK has been revoked (set *R_REVOKED to 1 if so
346
 *     and 0 otherwise).
347
 *
348
 * If either of the first two tests fail, returns an error code.
349
 * Otherwise returns 0.  (Thus, this function doesn't fail if the
350
 * public key is expired or revoked.)  */
351
static int
352
check_signature_metadata_validity (PKT_public_key *pk, PKT_signature *sig,
353
           int *r_expired, int *r_revoked)
354
6.33M
{
355
6.33M
  u32 cur_time;
356
357
6.33M
  if (r_expired)
358
0
    *r_expired = 0;
359
6.33M
  if (r_revoked)
360
0
    *r_revoked = 0;
361
362
6.33M
  if (pk->timestamp > sig->timestamp
363
10.2k
      && !(parse_key_usage (sig) & PUBKEY_USAGE_RENC))
364
10.2k
    {
365
10.2k
      ulong d = pk->timestamp - sig->timestamp;
366
10.2k
      if ( d < 86400 )
367
7.40k
        {
368
7.40k
          log_info (ngettext
369
7.40k
                    ("public key %s is %lu second newer than the signature\n",
370
7.40k
                     "public key %s is %lu seconds newer than the signature\n",
371
7.40k
                     d), keystr_from_pk (pk), d);
372
7.40k
        }
373
2.88k
      else
374
2.88k
        {
375
2.88k
          d /= 86400;
376
2.88k
          log_info (ngettext
377
2.88k
                    ("public key %s is %lu day newer than the signature\n",
378
2.88k
                     "public key %s is %lu days newer than the signature\n",
379
2.88k
                     d), keystr_from_pk (pk), d);
380
2.88k
        }
381
10.2k
      if (!opt.ignore_time_conflict)
382
10.2k
        return GPG_ERR_TIME_CONFLICT; /* pubkey newer than signature.  */
383
10.2k
    }
384
385
6.32M
  cur_time = make_timestamp ();
386
6.32M
  if (pk->timestamp > cur_time)
387
48
    {
388
48
      ulong d = pk->timestamp - cur_time;
389
48
      if (d < 86400)
390
0
        {
391
0
          log_info (ngettext("key %s was created %lu second"
392
0
                             " in the future (time warp or clock problem)\n",
393
0
                             "key %s was created %lu seconds"
394
0
                             " in the future (time warp or clock problem)\n",
395
0
                             d), keystr_from_pk (pk), d);
396
0
        }
397
48
      else
398
48
        {
399
48
          d /= 86400;
400
48
          log_info (ngettext("key %s was created %lu day"
401
48
                             " in the future (time warp or clock problem)\n",
402
48
                             "key %s was created %lu days"
403
48
                             " in the future (time warp or clock problem)\n",
404
48
                             d), keystr_from_pk (pk), d);
405
48
        }
406
48
      if (!opt.ignore_time_conflict)
407
48
        return GPG_ERR_TIME_CONFLICT;
408
48
    }
409
410
  /* Check whether the key has expired.  We check the has_expired
411
   * flag which is set after a full evaluation of the key (getkey.c)
412
   * as well as a simple compare to the current time in case the
413
   * merge has for whatever reasons not been done.  */
414
6.32M
  if (pk->has_expired || (pk->expiredate && pk->expiredate < cur_time))
415
4
    {
416
4
      char buf[11];
417
4
      if (opt.verbose)
418
4
        log_info (_("Note: signature key %s expired %s\n"),
419
0
                  keystr_from_pk(pk), isotimestamp( pk->expiredate ) );
420
4
      snprintf (buf, sizeof buf, "%lu",(ulong)pk->expiredate);
421
4
      write_status_text (STATUS_KEYEXPIRED, buf);
422
4
      if (r_expired)
423
0
        *r_expired = 1;
424
4
    }
425
426
6.32M
  if (pk->flags.revoked)
427
0
    {
428
0
      if (opt.verbose)
429
0
        log_info (_("Note: signature key %s has been revoked\n"),
430
0
                  keystr_from_pk(pk));
431
0
      if (r_revoked)
432
0
        *r_revoked=1;
433
0
    }
434
435
6.32M
  return 0;
436
6.32M
}
437
438
439
/* Finish generating a signature and check it.  Concretely: make sure
440
 * that the signature is valid (it was not created prior to the key,
441
 * the public key was created in the past, and the signature does not
442
 * include any unsupported critical features), finish computing the
443
 * digest by adding the relevant data from the signature packet, and
444
 * check that the signature verifies the digest.
445
 *
446
 * DIGEST contains a hash context, which has already hashed the signed
447
 * data.  This function adds the relevant meta-data from the signature
448
 * packet to compute the final hash.  (See Section 5.2 of RFC 4880:
449
 * "The concatenation of the data being signed and the signature data
450
 * from the version number through the hashed subpacket data
451
 * (inclusive) is hashed.")
452
 *
453
 * SIG is the signature to check.
454
 *
455
 * PK is the public key used to generate the signature.
456
 *
457
 * If R_EXPIRED is not NULL, *R_EXPIRED is set to 1 if PK has expired
458
 * (0 otherwise).  Note: PK being expired does not cause this function
459
 * to fail.
460
 *
461
 * If R_REVOKED is not NULL, *R_REVOKED is set to 1 if PK has been
462
 * revoked (0 otherwise).  Note: PK being revoked does not cause this
463
 * function to fail.
464
 *
465
 * If RET_PK is not NULL, PK is copied into RET_PK on success.
466
 *
467
 * Returns 0 on success.  An error code other.  */
468
static int
469
check_signature_end (PKT_public_key *pk, PKT_signature *sig,
470
         gcry_md_hd_t digest,
471
                     const void *extrahash, size_t extrahashlen,
472
         int *r_expired, int *r_revoked, PKT_public_key *ret_pk)
473
14
{
474
14
  int rc = 0;
475
476
14
  if ((rc = check_signature_metadata_validity (pk, sig,
477
14
                                               r_expired, r_revoked)))
478
0
    return rc;
479
480
14
  if ((rc = check_signature_end_simple (pk, sig, digest,
481
14
                                        extrahash, extrahashlen)))
482
0
    return rc;
483
484
14
  if (!rc && ret_pk)
485
0
    copy_public_key(ret_pk,pk);
486
487
14
  return rc;
488
14
}
489
490
491
/* This function is similar to check_signature_end, but it only checks
492
 * whether the signature was generated by PK.  It does not check
493
 * expiration, revocation, etc.  */
494
static int
495
check_signature_end_simple (PKT_public_key *pk, PKT_signature *sig,
496
                            gcry_md_hd_t digest,
497
                            const void *extrahash, size_t extrahashlen)
498
172k
{
499
172k
  gcry_mpi_t result = NULL;
500
172k
  int rc = 0;
501
502
172k
  if (!opt.flags.allow_weak_digest_algos)
503
172k
    {
504
172k
      if (is_weak_digest (sig->digest_algo))
505
0
        {
506
0
          print_digest_rejected_note (sig->digest_algo);
507
0
          return GPG_ERR_DIGEST_ALGO;
508
0
        }
509
172k
    }
510
511
  /* For key signatures check that the key has a cert usage.  We may
512
   * do this only for subkeys because the primary may always issue key
513
   * signature.  The latter may not be reflected in the pubkey_usage
514
   * field because we need to check the key signatures to extract the
515
   * key usage.  */
516
172k
  if (!pk->flags.primary
517
992
      && IS_CERT (sig) && !(pk->pubkey_usage & PUBKEY_USAGE_CERT))
518
713
    {
519
713
      rc = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
520
713
      if (!opt.quiet)
521
713
        log_info (_("bad key signature from key %s: %s (0x%02x, 0x%x)\n"),
522
713
                  keystr_from_pk (pk), gpg_strerror (rc),
523
713
                  sig->sig_class, pk->pubkey_usage);
524
713
      return rc;
525
713
    }
526
527
  /* For data signatures check that the key has sign usage.  */
528
172k
  if (!IS_BACK_SIG (sig) && IS_SIG (sig)
529
0
      && !(pk->pubkey_usage & PUBKEY_USAGE_SIG))
530
0
    {
531
0
      rc = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
532
0
      if (!opt.quiet)
533
0
        log_info (_("bad data signature from key %s: %s (0x%02x, 0x%x)\n"),
534
0
                  keystr_from_pk (pk), gpg_strerror (rc),
535
0
                  sig->sig_class, pk->pubkey_usage);
536
0
      return rc;
537
0
    }
538
539
  /* Make sure the digest algo is enabled (in case of a detached
540
   * signature).  */
541
172k
  gcry_md_enable (digest, sig->digest_algo);
542
543
  /* Complete the digest. */
544
172k
  if (sig->version >= 4)
545
172k
    gcry_md_putc (digest, sig->version);
546
547
172k
  gcry_md_putc( digest, sig->sig_class );
548
172k
  if (sig->version < 4)
549
149k
    {
550
149k
      u32 a = sig->timestamp;
551
149k
      gcry_md_putc (digest, ((a >> 24) & 0xff));
552
149k
      gcry_md_putc (digest, ((a >> 16) & 0xff));
553
149k
      gcry_md_putc (digest, ((a >>  8) & 0xff));
554
149k
      gcry_md_putc (digest, ( a        & 0xff));
555
149k
    }
556
22.5k
  else
557
22.5k
    {
558
22.5k
      byte buf[10];
559
22.5k
      int i;
560
22.5k
      size_t n;
561
562
22.5k
      gcry_md_putc (digest, sig->pubkey_algo);
563
22.5k
      gcry_md_putc (digest, sig->digest_algo);
564
22.5k
      if (sig->hashed)
565
16.2k
        {
566
16.2k
          n = sig->hashed->len;
567
16.2k
          if (sig->version >= 6) /* (v6 and later use 4 octets) */
568
0
            {
569
0
              gcry_md_putc (digest, (n >> 24));
570
0
              gcry_md_putc (digest, (n >> 16));
571
0
            }
572
16.2k
          gcry_md_putc (digest, (n >> 8) );
573
16.2k
          gcry_md_putc (digest,  n       );
574
16.2k
          gcry_md_write (digest, sig->hashed->data, n);
575
16.2k
          n += 6;
576
16.2k
  }
577
6.33k
      else
578
6.33k
        {
579
    /* Two octets for the (empty) length of the hashed
580
           * section. */
581
6.33k
          if (sig->version >= 6)
582
0
            {
583
0
              gcry_md_putc (digest, 0);
584
0
              gcry_md_putc (digest, 0);
585
0
            }
586
6.33k
          gcry_md_putc (digest, 0);
587
6.33k
    gcry_md_putc (digest, 0);
588
6.33k
          n = 6;
589
6.33k
  }
590
22.5k
      if (sig->version >= 6)
591
0
        n += 2; /* Fixup for the two extra octets hashed.  */
592
593
      /* Hash data from the literal data packet.  */
594
22.5k
      if (sig->version >= 5 && sig->version != 6
595
763
          && (sig->sig_class == 0x00 || sig->sig_class == 0x01))
596
0
        {
597
          /* - One octet content format
598
           * - File name (one octet length followed by the name)
599
           * - Four octet timestamp */
600
0
          if (extrahash && extrahashlen)
601
0
            gcry_md_write (digest, extrahash, extrahashlen);
602
0
          else /* Detached signature. */
603
0
            {
604
0
              memset (buf, 0, 6);
605
0
              gcry_md_write (digest, buf, 6);
606
0
            }
607
0
        }
608
      /* Add some magic per Section 5.2.4 of RFC 4880.  */
609
22.5k
      i = 0;
610
22.5k
      buf[i++] = sig->version;
611
22.5k
      buf[i++] = 0xff;
612
22.5k
      if (sig->version >= 5 && sig->version != 6)
613
763
        {
614
763
#if SIZEOF_SIZE_T > 4
615
763
          buf[i++] = n >> 56;
616
763
          buf[i++] = n >> 48;
617
763
          buf[i++] = n >> 40;
618
763
          buf[i++] = n >> 32;
619
#else
620
          buf[i++] = 0;
621
          buf[i++] = 0;
622
          buf[i++] = 0;
623
          buf[i++] = 0;
624
#endif
625
763
        }
626
22.5k
      buf[i++] = n >> 24;
627
22.5k
      buf[i++] = n >> 16;
628
22.5k
      buf[i++] = n >>  8;
629
22.5k
      buf[i++] = n;
630
22.5k
      gcry_md_write (digest, buf, i);
631
22.5k
    }
632
172k
    gcry_md_final( digest );
633
634
    /* Convert the digest to an MPI.  */
635
172k
    result = encode_md_value (pk, digest, sig->digest_algo );
636
172k
    if (!result)
637
150k
      return GPG_ERR_GENERAL;
638
639
    /* Verify the signature.  */
640
22.0k
    if (DBG_CLOCK && sig->sig_class <= 0x01)
641
22.0k
      log_clock ("enter pk_verify");
642
22.0k
    rc = pk_verify( pk->pubkey_algo, result, sig->data, pk->pkey );
643
22.0k
    if (DBG_CLOCK && sig->sig_class <= 0x01)
644
22.0k
      log_clock ("leave pk_verify");
645
22.0k
    gcry_mpi_release (result);
646
647
22.0k
  if (!rc && sig->flags.unknown_critical)
648
349
    {
649
349
      log_info(_("assuming bad signature from key %s"
650
349
                 " due to an unknown critical bit\n"),keystr_from_pk(pk));
651
349
      rc = GPG_ERR_BAD_SIGNATURE;
652
349
    }
653
654
22.0k
  return rc;
655
172k
}
656
657
658
/* Add a uid node to a hash context.  See section 5.2.4, paragraph 4
659
 * of RFC 4880.  */
660
static void
661
hash_uid_packet (PKT_user_id *uid, gcry_md_hd_t md, PKT_signature *sig )
662
112k
{
663
112k
  if (uid->attrib_data)
664
52.7k
    {
665
52.7k
      if (sig->version >= 4)
666
4.41k
        {
667
4.41k
          byte buf[5];
668
4.41k
          buf[0] = 0xd1;       /* packet of type 17 */
669
4.41k
          buf[1] = uid->attrib_len >> 24;  /* always use 4 length bytes */
670
4.41k
          buf[2] = uid->attrib_len >> 16;
671
4.41k
          buf[3] = uid->attrib_len >>  8;
672
4.41k
          buf[4] = uid->attrib_len;
673
4.41k
          gcry_md_write( md, buf, 5 );
674
4.41k
  }
675
52.7k
      gcry_md_write( md, uid->attrib_data, uid->attrib_len );
676
52.7k
    }
677
59.9k
  else
678
59.9k
    {
679
59.9k
      if (sig->version >= 4)
680
15.5k
        {
681
15.5k
          byte buf[5];
682
15.5k
          buf[0] = 0xb4;        /* indicates a userid packet */
683
15.5k
          buf[1] = uid->len >> 24;    /* always use 4 length bytes */
684
15.5k
          buf[2] = uid->len >> 16;
685
15.5k
          buf[3] = uid->len >>  8;
686
15.5k
          buf[4] = uid->len;
687
15.5k
          gcry_md_write( md, buf, 5 );
688
15.5k
  }
689
59.9k
      gcry_md_write( md, uid->name, uid->len );
690
59.9k
    }
691
112k
}
692
693
static void
694
cache_sig_result ( PKT_signature *sig, int result )
695
10.9k
{
696
10.9k
  if (!result)
697
3.97k
    {
698
3.97k
      sig->flags.checked = 1;
699
3.97k
      sig->flags.valid = 1;
700
3.97k
    }
701
6.96k
  else if  (gpg_err_code (result) == GPG_ERR_BAD_SIGNATURE)
702
1.06k
    {
703
1.06k
      sig->flags.checked = 1;
704
1.06k
      sig->flags.valid = 0;
705
1.06k
    }
706
5.89k
  else
707
5.89k
    {
708
5.89k
      sig->flags.checked = 0;
709
5.89k
      sig->flags.valid = 0;
710
5.89k
    }
711
10.9k
}
712
713
714
/* SIG is a key revocation signature.  Check if this signature was
715
 * generated by any of the public key PK's designated revokers.
716
 *
717
 *   PK is the public key that SIG allegedly revokes.
718
 *
719
 *   SIG is the revocation signature to check.
720
 *
721
 * This function avoids infinite recursion, which can happen if two
722
 * keys are designed revokers for each other and they revoke each
723
 * other.  This is done by observing that if a key A is revoked by key
724
 * B we still consider the revocation to be valid even if B is
725
 * revoked.  Thus, we don't need to determine whether B is revoked to
726
 * determine whether A has been revoked by B, we just need to check
727
 * the signature.
728
 *
729
 * Returns 0 if sig is valid (i.e. pk is revoked), non-0 if not
730
 * revoked.  We are careful to make sure that GPG_ERR_NO_PUBKEY is
731
 * only returned when a revocation signature is from a valid
732
 * revocation key designated in a revkey subpacket, but the revocation
733
 * key itself isn't present.
734
 *
735
 * Note that this guarantees that a designated revocation sig will
736
 * never be considered valid unless it is actually valid, as well as
737
 * being issued by a revocation key in a valid direct signature.  Note
738
 * also that this is written so that a revoked revoker can still issue
739
 * revocations: i.e. If A revokes B, but A is revoked, B is still
740
 * revoked.  I'm not completely convinced this is the proper behavior,
741
 * but it matches how PGP does it. -dms
742
 */
743
gpg_error_t
744
check_revocation_keys (ctrl_t ctrl, PKT_public_key *pk, PKT_signature *sig)
745
4
{
746
4
  int i;
747
4
  gpg_error_t err = gpg_error (GPG_ERR_GENERAL);
748
749
4
  log_assert (IS_KEY_REV(sig));
750
4
  log_assert ((sig->keyid[0]!=pk->keyid[0]) || (sig->keyid[1]!=pk->keyid[1]));
751
752
  /* Avoid infinite recursion.  Consider the following:
753
   *
754
   *   - We want to check if A is revoked.
755
   *
756
   *   - C is a designated revoker for B and has revoked B.
757
   *
758
   *   - B is a designated revoker for A and has revoked A.
759
   *
760
   * When checking if A is revoked (in merge_selfsigs_main), we
761
   * observe that A has a designed revoker.  As such, we call this
762
   * function.  This function sees that there is a valid revocation
763
   * signature, which is signed by B.  It then calls check_signature()
764
   * to verify that the signature is good.  To check the sig, we need
765
   * to lookup B.  Looking up B means calling merge_selfsigs_main,
766
   * which checks whether B is revoked, which calls this function to
767
   * see if B was revoked by some key.
768
   *
769
   * In this case, the added level of indirection doesn't hurt.  It
770
   * just means a bit more work.  However, if C == A, then we'd end up
771
   * in a loop.  But, it doesn't make sense to look up C anyways: even
772
   * if B is revoked, we conservatively consider a valid revocation
773
   * signed by B to revoke A.  Since this is the only place where this
774
   * type of recursion can occur, we simply cause this function to
775
   * fail if it is entered recursively.  */
776
4
  if (ctrl->in_check_revocation_keys)
777
0
    {
778
      /* Return an error (i.e. not revoked), but mark the pk as
779
         uncacheable as we don't really know its revocation status
780
         until it is checked directly.  */
781
0
      pk->flags.dont_cache = 1;
782
0
      return err;
783
0
    }
784
785
4
  ctrl->in_check_revocation_keys = 1;
786
787
  /*  es_printf("looking at %08lX with a sig from %08lX\n",(ulong)pk->keyid[1],
788
      (ulong)sig->keyid[1]); */
789
790
  /* Is the issuer of the sig one of our revokers? */
791
4
  if (!pk->revkey && pk->numrevkeys)
792
0
    BUG();
793
794
8
  for (i=0; i < pk->numrevkeys; i++)
795
4
    {
796
4
      u32 keyid[2]; /* Receives the revoker's keyid.  */
797
798
4
      keyid_from_fingerprint (ctrl, pk->revkey[i].fpr, pk->revkey[i].fprlen,
799
4
                              keyid);
800
801
      /* If the signature was generated by a designated revoker
802
       * verify the signature.  */
803
4
      if (keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1])
804
0
        {
805
0
          gcry_md_hd_t md;
806
807
0
          if (gcry_md_open (&md, sig->digest_algo, 0))
808
0
            BUG ();
809
0
          hash_public_key (md, pk, sig);
810
          /* Note: check_signature only checks that the signature
811
           * is good.  It does not fail if the key is revoked.  */
812
0
          err = check_signature (ctrl, sig, md, NULL, 0, NULL,
813
0
                                 NULL, NULL, NULL, NULL, NULL);
814
0
          cache_sig_result (sig, err);
815
0
          gcry_md_close (md);
816
0
          break;  /* Fixme: We did not check RC.  */
817
0
        }
818
4
    }
819
820
4
  ctrl->in_check_revocation_keys = 0;
821
822
4
  return err;
823
4
}
824
825
826
/* Check that the backsig BACKSIG from the subkey SUB_PK to its
827
 * primary key MAIN_PK is valid.
828
 *
829
 * Backsigs (0x19) have the same format as binding sigs (0x18), but
830
 * this function is simpler than check_key_signature in a few ways.
831
 * For example, there is no support for expiring backsigs since it is
832
 * questionable what such a thing actually means.  Note also that the
833
 * sig cache check here, unlike other sig caches in GnuPG, is not
834
 * persistent.  */
835
int
836
check_backsig (PKT_public_key *main_pk,PKT_public_key *sub_pk,
837
         PKT_signature *backsig)
838
14
{
839
14
  gcry_md_hd_t md;
840
14
  int rc;
841
842
  /* Always check whether the algorithm is available.  Although
843
     gcry_md_open would throw an error, some libgcrypt versions will
844
     print a debug message in that case too. */
845
14
  if ((rc=openpgp_md_test_algo (backsig->digest_algo)))
846
0
    return rc;
847
848
14
  if(!opt.no_sig_cache && backsig->flags.checked)
849
0
    return backsig->flags.valid? 0 : gpg_error (GPG_ERR_BAD_SIGNATURE);
850
851
14
  rc = gcry_md_open (&md, backsig->digest_algo,0);
852
14
  if (!rc)
853
14
    {
854
14
      hash_public_key (md, main_pk, backsig);
855
14
      hash_public_key (md, sub_pk, backsig);
856
14
      rc = check_signature_end (sub_pk, backsig, md, NULL, 0, NULL, NULL, NULL);
857
14
      cache_sig_result(backsig,rc);
858
14
      gcry_md_close(md);
859
14
    }
860
861
14
  return rc;
862
14
}
863
864
865
/* Check that a signature over a key is valid.  This is a
866
 * specialization of check_key_signature2 with the unnamed parameters
867
 * passed as NULL.  See the documentation for that function for more
868
 * details.  */
869
int
870
check_key_signature (ctrl_t ctrl, kbnode_t root, kbnode_t node,
871
                     int *is_selfsig)
872
6.37M
{
873
6.37M
  return check_key_signature2 (ctrl, root, node, NULL, NULL,
874
6.37M
                               is_selfsig, NULL, NULL);
875
6.37M
}
876
877
878
/* Returns whether SIGNER generated the signature SIG over the packet
879
 * PACKET, which is a key, subkey or uid, and comes from the key block
880
 * KB.  (KB is PACKET's corresponding keyblock; we don't assume that
881
 * SIG has been added to the keyblock.)
882
 *
883
 * If SIGNER is set, then checks whether SIGNER generated the
884
 * signature.  Otherwise, uses SIG->KEYID to find the alleged signer.
885
 * This parameter can be used to effectively override the alleged
886
 * signer that is stored in SIG.
887
 *
888
 * KB may be NULL if SIGNER is set.
889
 *
890
 * Unlike check_key_signature, this function ignores any cached
891
 * results!  That is, it does not consider SIG->FLAGS.CHECKED and
892
 * SIG->FLAGS.VALID nor does it set them.
893
 *
894
 * This doesn't check the signature's semantic mean.  Concretely, it
895
 * doesn't check whether a non-self signed revocation signature was
896
 * created by a designated revoker.  In fact, it doesn't return an
897
 * error for a binding generated by a completely different key!
898
 *
899
 * Returns 0 if the signature is valid.  Returns GPG_ERR_SIG_CLASS if
900
 * this signature can't be over PACKET.  Returns GPG_ERR_NOT_FOUND if
901
 * the key that generated the signature (according to SIG) could not
902
 * be found.  Returns GPG_ERR_BAD_SIGNATURE if the signature is bad.
903
 * Other errors codes may be returned if something else goes wrong.
904
 *
905
 * If IS_SELFSIG is not NULL, sets *IS_SELFSIG to 1 if this is a
906
 * self-signature (by the key's primary key) or 0 if not.
907
 *
908
 * If RET_PK is not NULL, returns a copy of the public key that
909
 * generated the signature (i.e., the signer) on success.  This must
910
 * be released by the caller using release_public_key_parts ().  */
911
gpg_error_t
912
check_signature_over_key_or_uid (ctrl_t ctrl, PKT_public_key *signer,
913
                                 PKT_signature *sig, KBNODE kb, PACKET *packet,
914
                                 int *is_selfsig, PKT_public_key *ret_pk)
915
605k
{
916
605k
  int rc;
917
605k
  PKT_public_key *pripk = kb->pkt->pkt.public_key;
918
605k
  gcry_md_hd_t md;
919
605k
  int signer_alloced = 0;
920
605k
  int stub_is_selfsig;
921
922
605k
  if (!is_selfsig)
923
605k
    is_selfsig = &stub_is_selfsig;
924
925
605k
  *is_selfsig = 0; /* Init early to comply with function description. */
926
927
605k
  rc = openpgp_pk_test_algo (sig->pubkey_algo);
928
605k
  if (rc)
929
0
    return rc;
930
605k
  rc = openpgp_md_test_algo (sig->digest_algo);
931
605k
  if (rc)
932
0
    return rc;
933
934
  /* A signature's class indicates the type of packet that it
935
     signs.  */
936
605k
  if (IS_BACK_SIG (sig) || IS_KEY_SIG (sig) || IS_KEY_REV (sig))
937
228k
    {
938
      /* Key revocations can only be over primary keys.  */
939
228k
      if (packet->pkttype != PKT_PUBLIC_KEY)
940
225k
        return gpg_error (GPG_ERR_SIG_CLASS);
941
228k
    }
942
376k
  else if (IS_SUBKEY_SIG (sig) || IS_SUBKEY_REV (sig))
943
185k
    {
944
185k
      if (packet->pkttype != PKT_PUBLIC_SUBKEY)
945
128k
        return gpg_error (GPG_ERR_SIG_CLASS);
946
185k
    }
947
190k
  else if (IS_UID_SIG (sig) || IS_UID_REV (sig))
948
188k
    {
949
188k
      if (packet->pkttype != PKT_USER_ID)
950
76.0k
        return gpg_error (GPG_ERR_SIG_CLASS);
951
188k
    }
952
2.01k
  else
953
2.01k
    return gpg_error (GPG_ERR_SIG_CLASS);
954
955
  /* PACKET is the right type for SIG.  */
956
957
172k
  if (signer)
958
171k
    {
959
171k
      if (signer->keyid[0] == pripk->keyid[0]
960
169k
          && signer->keyid[1] == pripk->keyid[1])
961
169k
        *is_selfsig = 1;
962
1.98k
      else
963
1.98k
        *is_selfsig = 0;
964
171k
    }
965
1.35k
  else
966
1.35k
    {
967
      /* Get the signer.  If possible, avoid a look up.  */
968
1.35k
      if (sig->keyid[0] == pripk->keyid[0]
969
1.35k
          && sig->keyid[1] == pripk->keyid[1])
970
1.35k
        {
971
          /* Issued by the primary key.  */
972
1.35k
          signer = pripk;
973
1.35k
          *is_selfsig = 1;
974
1.35k
        }
975
0
      else
976
0
        {
977
          /* See if one of the subkeys was the signer (although this
978
           * is extremely unlikely).  */
979
0
          kbnode_t ctx = NULL;
980
0
          kbnode_t n;
981
982
0
          while ((n = walk_kbnode (kb, &ctx, 0)))
983
0
            {
984
0
              PKT_public_key *subk;
985
986
0
              if (n->pkt->pkttype != PKT_PUBLIC_SUBKEY)
987
0
                continue;
988
989
0
              subk = n->pkt->pkt.public_key;
990
0
              if (sig->keyid[0] == subk->keyid[0]
991
0
                  && sig->keyid[1] == subk->keyid[1])
992
0
                {
993
                  /* Issued by a subkey.  */
994
0
                  signer = subk;
995
0
                  break;
996
0
                }
997
0
            }
998
999
0
          if (! signer)
1000
0
            {
1001
              /* Signer by some other key.  */
1002
0
              *is_selfsig = 0;
1003
0
              if (ret_pk)
1004
0
                {
1005
0
                  signer = ret_pk;
1006
                  /* FIXME: Using memset here is problematic because it
1007
                   * assumes that there are no allocated fields in
1008
                   * SIGNER.  */
1009
0
                  memset (signer, 0, sizeof (*signer));
1010
0
                  signer_alloced = 1;
1011
0
                }
1012
0
              else
1013
0
                {
1014
0
                  signer = xmalloc_clear (sizeof (*signer));
1015
0
                  signer_alloced = 2;
1016
0
                }
1017
1018
0
              if (IS_CERT (sig))
1019
0
                signer->req_usage = PUBKEY_USAGE_CERT;
1020
1021
0
              rc = get_pubkey_for_sig (ctrl, signer, sig, NULL, NULL);
1022
0
              if (rc)
1023
0
                {
1024
0
                  if (signer_alloced != 1)
1025
0
                    xfree (signer);
1026
0
                  signer = NULL;
1027
0
                  signer_alloced = 0;
1028
0
                  goto leave;
1029
0
                }
1030
0
            }
1031
0
        }
1032
1.35k
    }
1033
1034
  /* We checked above that we supported this algo, so an error here is
1035
   * a bug.  */
1036
172k
  if (gcry_md_open (&md, sig->digest_algo, 0))
1037
0
    BUG ();
1038
172k
  if (DBG_HASHING)
1039
0
    gcry_md_debug (md, "keysig");
1040
1041
  /* Hash the relevant data.  */
1042
1043
172k
  if (IS_KEY_SIG (sig) || IS_KEY_REV (sig))
1044
2.20k
    {
1045
2.20k
      log_assert (packet->pkttype == PKT_PUBLIC_KEY);
1046
2.20k
      hash_public_key (md, packet->pkt.public_key, sig);
1047
2.20k
      rc = check_signature_end_simple (signer, sig, md, NULL, 0);
1048
2.20k
    }
1049
170k
  else if (IS_BACK_SIG (sig))
1050
697
    {
1051
697
      log_assert (packet->pkttype == PKT_PUBLIC_KEY);
1052
697
      hash_public_key (md, packet->pkt.public_key, sig);
1053
697
      hash_public_key (md, signer, sig);
1054
697
      rc = check_signature_end_simple (signer, sig, md, NULL, 0);
1055
697
    }
1056
169k
  else if (IS_SUBKEY_SIG (sig) || IS_SUBKEY_REV (sig))
1057
57.1k
    {
1058
57.1k
      log_assert (packet->pkttype == PKT_PUBLIC_SUBKEY);
1059
57.1k
      hash_public_key (md, pripk, sig);
1060
57.1k
      hash_public_key (md, packet->pkt.public_key, NULL);
1061
57.1k
      rc = check_signature_end_simple (signer, sig, md, NULL, 0);
1062
57.1k
    }
1063
112k
  else if (IS_UID_SIG (sig) || IS_UID_REV (sig))
1064
112k
    {
1065
112k
      log_assert (packet->pkttype == PKT_USER_ID);
1066
112k
      if (sig->digest_algo == DIGEST_ALGO_SHA1 && !*is_selfsig
1067
50
          && !opt.flags.allow_weak_key_signatures)
1068
50
        {
1069
          /* If the signature was created using SHA-1 we consider this
1070
           * signature invalid because it makes it possible to mount a
1071
           * chosen-prefix collision.  We don't do this for
1072
           * self-signatures, though.  */
1073
50
          print_sha1_keysig_rejected_note ();
1074
50
          rc = gpg_error (GPG_ERR_DIGEST_ALGO);
1075
50
        }
1076
112k
      else
1077
112k
        {
1078
112k
          hash_public_key (md, pripk, sig);
1079
112k
          hash_uid_packet (packet->pkt.user_id, md, sig);
1080
112k
          rc = check_signature_end_simple (signer, sig, md, NULL, 0);
1081
112k
        }
1082
112k
    }
1083
0
  else
1084
0
    {
1085
      /* We should never get here.  (The first if above should have
1086
       * already caught this error.)  */
1087
0
      BUG ();
1088
0
    }
1089
1090
172k
  gcry_md_close (md);
1091
1092
172k
 leave:
1093
172k
  if (! rc && ret_pk && ret_pk != signer)
1094
0
    copy_public_key (ret_pk, signer);
1095
1096
172k
  if (signer_alloced)
1097
0
    {
1098
      /* We looked up SIGNER; it is not a pointer into KB.  */
1099
0
      release_public_key_parts (signer);
1100
      /* Free if we also allocated the memory.  */
1101
0
      if (signer_alloced == 2)
1102
0
        xfree (signer);
1103
0
    }
1104
1105
172k
  return rc;
1106
172k
}
1107
1108
1109
/* Check that a signature over a key (e.g., a key revocation, key
1110
 * binding, user id certification, etc.) is valid.  If the function
1111
 * detects a self-signature, it uses the public key from the specified
1112
 * key block and does not bother looking up the key specified in the
1113
 * signature packet.
1114
 *
1115
 * ROOT is a keyblock.
1116
 *
1117
 * NODE references a signature packet that appears in the keyblock
1118
 * that should be verified.
1119
 *
1120
 * If CHECK_PK is set, the specified key is sometimes preferred for
1121
 * verifying signatures.  See the implementation for details.
1122
 *
1123
 * If RET_PK is not NULL, the public key that successfully verified
1124
 * the signature is copied into *RET_PK.
1125
 *
1126
 * If IS_SELFSIG is not NULL, *IS_SELFSIG is set to 1 if NODE is a
1127
 * self-signature.
1128
 *
1129
 * If R_EXPIREDATE is not NULL, *R_EXPIREDATE is set to the expiry
1130
 * date.
1131
 *
1132
 * If R_EXPIRED is not NULL, *R_EXPIRED is set to 1 if PK has been
1133
 * expired (0 otherwise).  Note: PK being revoked does not cause this
1134
 * function to fail.
1135
 *
1136
 *
1137
 * If OPT.NO_SIG_CACHE is not set, this function will first check if
1138
 * the result of a previous verification is already cached in the
1139
 * signature packet's data structure.
1140
 *
1141
 * TODO: add r_revoked here as well.  It has the same problems as
1142
 * r_expiredate and r_expired and the cache [nw].  Which problems [wk]? */
1143
int
1144
check_key_signature2 (ctrl_t ctrl,
1145
                      kbnode_t root, kbnode_t node, PKT_public_key *check_pk,
1146
                      PKT_public_key *ret_pk, int *is_selfsig,
1147
                      u32 *r_expiredate, int *r_expired )
1148
6.37M
{
1149
6.37M
  PKT_public_key *pk;
1150
6.37M
  PKT_signature *sig;
1151
6.37M
  int algo;
1152
6.37M
  int rc;
1153
1154
6.37M
  if (is_selfsig)
1155
0
    *is_selfsig = 0;
1156
6.37M
  if (r_expiredate)
1157
0
    *r_expiredate = 0;
1158
6.37M
  if (r_expired)
1159
0
    *r_expired = 0;
1160
6.37M
  log_assert (node->pkt->pkttype == PKT_SIGNATURE);
1161
6.37M
  log_assert (root->pkt->pkttype == PKT_PUBLIC_KEY);
1162
1163
6.37M
  pk = root->pkt->pkt.public_key;
1164
6.37M
  sig = node->pkt->pkt.signature;
1165
6.37M
  algo = sig->digest_algo;
1166
1167
  /* Check whether we have cached the result of a previous signature
1168
   * check.  Note that we may no longer have the pubkey or hash
1169
   * needed to verify a sig, but can still use the cached value.  A
1170
   * cache refresh detects and clears these cases. */
1171
6.37M
  if ( !opt.no_sig_cache )
1172
6.37M
    {
1173
6.37M
      cache_stats.total++;
1174
6.37M
      if (sig->flags.checked) /* Cached status available.  */
1175
6.32M
        {
1176
6.32M
          cache_stats.cached++;
1177
6.32M
          if (is_selfsig)
1178
0
            {
1179
0
              u32 keyid[2];
1180
1181
0
              keyid_from_pk (pk, keyid);
1182
0
              if (keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1])
1183
0
                *is_selfsig = 1;
1184
0
      }
1185
          /* BUG: This is wrong for non-self-sigs... needs to be the
1186
           * actual pk.  */
1187
6.32M
          rc = check_signature_metadata_validity (pk, sig, r_expired, NULL);
1188
6.32M
          if (rc)
1189
7.30k
            return rc;
1190
6.31M
          if (sig->flags.valid)
1191
5.98M
            {
1192
5.98M
              cache_stats.goodsig++;
1193
5.98M
              return 0;
1194
5.98M
            }
1195
328k
          cache_stats.badsig++;
1196
328k
          return gpg_error (GPG_ERR_BAD_SIGNATURE);
1197
6.31M
        }
1198
6.37M
    }
1199
1200
54.3k
  rc = openpgp_pk_test_algo(sig->pubkey_algo);
1201
54.3k
  if (rc)
1202
750
    return rc;
1203
53.5k
  rc = openpgp_md_test_algo(algo);
1204
53.5k
  if (rc)
1205
42.6k
    return rc;
1206
1207
10.9k
  if (IS_KEY_REV (sig))
1208
693
    {
1209
693
      u32 keyid[2];
1210
693
      keyid_from_pk( pk, keyid );
1211
1212
      /* Is it a designated revoker? */
1213
693
      if (keyid[0] != sig->keyid[0] || keyid[1] != sig->keyid[1])
1214
0
        {
1215
          /* In order to access flags we have to get the PK from the keyring
1216
           * and cannot use the keyblock. */
1217
0
          PKT_public_key *pk_from_keydb;
1218
0
          pk_from_keydb = xmalloc_clear (sizeof *pk_from_keydb);
1219
0
          rc = get_pubkey (ctrl, pk_from_keydb, keyid);
1220
0
          if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY)
1221
0
            {
1222
0
              log_error (_("key %s: no public key -"
1223
0
                           " can't check revocation signature.\n"),
1224
0
                         keystr (keyid));
1225
0
            }
1226
0
          else if (rc) {
1227
0
            log_error ("key %s: can't find public key of revocation sig"
1228
0
                       "in keyring: %s\n",
1229
0
                       keystr (keyid), gpg_strerror (rc));
1230
0
          }
1231
1232
          /* No need to check an already revoked key. */
1233
0
          if (pk_from_keydb->flags.revoked)
1234
0
            {
1235
0
              rc = GPG_ERR_CERT_REVOKED;
1236
0
            }
1237
0
          else {
1238
0
            rc = check_revocation_keys (ctrl, pk_from_keydb, sig);
1239
0
          }
1240
1241
0
          free_public_key (pk_from_keydb);
1242
0
          pk_from_keydb = NULL;
1243
0
        }
1244
693
      else /* Here: Revocation self-signatures. */
1245
693
        {
1246
693
          rc = check_signature_metadata_validity (pk, sig,
1247
693
                                                  r_expired, NULL);
1248
693
          if (! rc)
1249
561
            rc = check_signature_over_key_or_uid (ctrl, pk, sig,
1250
561
                                                  root, root->pkt,
1251
561
                                                  is_selfsig, ret_pk);
1252
693
        }
1253
693
    }
1254
10.2k
  else if (IS_SUBKEY_REV (sig) || IS_SUBKEY_SIG (sig))
1255
2.72k
    {
1256
2.72k
      kbnode_t snode = find_prev_kbnode (root, node, PKT_PUBLIC_SUBKEY);
1257
1258
2.72k
      if (snode)
1259
2.70k
        {
1260
2.70k
          rc = check_signature_metadata_validity (pk, sig,
1261
2.70k
                                                  r_expired, NULL);
1262
2.70k
          if (! rc)
1263
1.94k
            {
1264
              /* A subkey revocation (0x28) must be a self-sig, but a
1265
               * subkey signature (0x18) needn't be.  */
1266
1.94k
              rc = check_signature_over_key_or_uid (ctrl,
1267
1.94k
                                                    IS_SUBKEY_SIG (sig)
1268
1.94k
                                                    ? NULL : pk,
1269
1.94k
                                                    sig, root, snode->pkt,
1270
1.94k
                                                    is_selfsig, ret_pk);
1271
1.94k
            }
1272
2.70k
        }
1273
20
      else
1274
20
        {
1275
20
          if (opt.verbose)
1276
0
            {
1277
0
              if (IS_SUBKEY_REV (sig))
1278
0
                log_info (_("key %s: no subkey for subkey"
1279
0
                            " revocation signature\n"), keystr_from_pk(pk));
1280
0
              else if (sig->sig_class == 0x18)
1281
0
                log_info(_("key %s: no subkey for subkey"
1282
0
                           " binding signature\n"), keystr_from_pk(pk));
1283
0
            }
1284
20
          rc = GPG_ERR_SIG_CLASS;
1285
20
        }
1286
2.72k
    }
1287
7.49k
  else if (IS_KEY_SIG (sig)) /* direct key signature */
1288
1.33k
    {
1289
1.33k
      rc = check_signature_metadata_validity (pk, sig,
1290
1.33k
                                              r_expired, NULL);
1291
1.33k
      if (! rc)
1292
671
        rc = check_signature_over_key_or_uid (ctrl, pk, sig, root, root->pkt,
1293
671
                                              is_selfsig, ret_pk);
1294
1.33k
    }
1295
6.16k
  else if (IS_UID_SIG (sig) || IS_UID_REV (sig))
1296
5.65k
    {
1297
5.65k
      kbnode_t unode = find_prev_kbnode (root, node, PKT_USER_ID);
1298
1299
5.65k
      if (unode)
1300
5.62k
        {
1301
5.62k
          rc = check_signature_metadata_validity (pk, sig, r_expired, NULL);
1302
5.62k
          if (! rc)
1303
4.15k
            {
1304
              /* If this is a self-sig, ignore check_pk.  */
1305
4.15k
              rc = check_signature_over_key_or_uid
1306
4.15k
                (ctrl,
1307
4.15k
                 keyid_cmp (pk_keyid (pk), sig->keyid) == 0 ? pk : check_pk,
1308
4.15k
                 sig, root, unode->pkt, NULL, ret_pk);
1309
4.15k
            }
1310
5.62k
        }
1311
26
      else
1312
26
        {
1313
26
          if (!opt.quiet)
1314
26
            log_info ("key %s: no user ID for key signature packet"
1315
26
                      " of class %02x\n",keystr_from_pk(pk),sig->sig_class);
1316
26
          rc = GPG_ERR_SIG_CLASS;
1317
26
        }
1318
5.65k
    }
1319
512
  else
1320
512
    {
1321
512
      log_info ("sig issued by %s with class %d (digest: %02x %02x)"
1322
512
                " is not valid over a user id or a key id, ignoring.\n",
1323
512
                keystr (sig->keyid), sig->sig_class,
1324
512
                sig->digest_start[0], sig->digest_start[1]);
1325
512
      rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
1326
512
    }
1327
1328
10.9k
  cache_sig_result  (sig, rc);
1329
1330
10.9k
  return rc;
1331
53.5k
}