~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-security

« back to all changes in this revision

Viewing changes to sm/verify.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* verify.c - Verify a messages signature
2
 
 *      Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#include <config.h>
48
47
 
49
48
 
50
49
 
51
 
/* Hash the data for a detached signature */
52
 
static void
 
50
/* Hash the data for a detached signature.  Returns 0 on success.  */
 
51
static gpg_error_t
53
52
hash_data (int fd, gcry_md_hd_t md)
54
53
{
 
54
  gpg_error_t err = 0;
55
55
  FILE *fp;
56
56
  char buffer[4096];
57
57
  int nread;
59
59
  fp = fdopen ( dup (fd), "rb");
60
60
  if (!fp)
61
61
    {
62
 
      log_error ("fdopen(%d) failed: %s\n", fd, strerror (errno));
63
 
      return;
 
62
      err = gpg_error_from_syserror ();
 
63
      log_error ("fdopen(%d) failed: %s\n", fd, gpg_strerror (err));
 
64
      return err;
64
65
    }
65
66
 
66
67
  do 
70
71
    }
71
72
  while (nread);
72
73
  if (ferror (fp))
73
 
      log_error ("read error on fd %d: %s\n", fd, strerror (errno));
 
74
    {
 
75
      err = gpg_error_from_syserror ();
 
76
      log_error ("read error on fd %d: %s\n", fd, gpg_strerror (err));
 
77
    }
74
78
  fclose (fp);
 
79
  return err;
75
80
}
76
81
 
77
82
 
81
86
   must be different than -1.  With OUT_FP given and a non-detached
82
87
   signature, the signed material is written to that stream. */
83
88
int
84
 
gpgsm_verify (CTRL ctrl, int in_fd, int data_fd, FILE *out_fp)
 
89
gpgsm_verify (ctrl_t ctrl, int in_fd, int data_fd, FILE *out_fp)
85
90
{
86
91
  int i, rc;
87
92
  Base64Context b64reader = NULL;
100
105
  FILE *fp = NULL;
101
106
  char *p;
102
107
 
 
108
  audit_set_type (ctrl->audit, AUDIT_TYPE_VERIFY);
 
109
 
103
110
  kh = keydb_new (0);
104
111
  if (!kh)
105
112
    {
126
133
 
127
134
  if (out_fp)
128
135
    {
129
 
      rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, &writer);
 
136
      rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, NULL, &writer);
130
137
      if (rc)
131
138
        {
132
139
          log_error ("can't create writer: %s\n", gpg_strerror (rc));
155
162
  if (DBG_HASHING)
156
163
    gcry_md_start_debug (data_md, "vrfy.data");
157
164
 
 
165
  audit_log (ctrl->audit, AUDIT_SETUP_READY);
 
166
 
158
167
  is_detached = 0;
159
168
  do 
160
169
    {
168
177
      if (stopreason == KSBA_SR_NEED_HASH)
169
178
        {
170
179
          is_detached = 1;
 
180
          audit_log (ctrl->audit, AUDIT_DETACHED_SIGNATURE);
171
181
          if (opt.verbose)
172
182
            log_info ("detached signature\n");
173
183
        }
174
184
 
175
185
      if (stopreason == KSBA_SR_NEED_HASH
176
186
          || stopreason == KSBA_SR_BEGIN_DATA)
177
 
        { /* We are now able to enable the hash algorithms */
 
187
        { 
 
188
          audit_log (ctrl->audit, AUDIT_GOT_DATA);
 
189
 
 
190
          /* We are now able to enable the hash algorithms */
178
191
          for (i=0; (algoid=ksba_cms_get_digest_algo_list (cms, i)); i++)
179
192
            {
180
193
              algo = gcry_md_map_name (algoid);
186
199
                      && (  !strcmp (algoid, "1.2.840.113549.1.1.2")
187
200
                          ||!strcmp (algoid, "1.2.840.113549.2.2")))
188
201
                    log_info (_("(this is the MD2 algorithm)\n"));
 
202
                  audit_log_s (ctrl->audit, AUDIT_BAD_DATA_HASH_ALGO, algoid);
189
203
                }
190
204
              else
191
 
                gcry_md_enable (data_md, algo);
 
205
                {
 
206
                  if (DBG_X509)
 
207
                    log_debug ("enabling hash algorithm %d (%s)\n",
 
208
                               algo, algoid? algoid:"");
 
209
                  gcry_md_enable (data_md, algo);
 
210
                  audit_log_i (ctrl->audit, AUDIT_DATA_HASH_ALGO, algo);
 
211
                }
 
212
            }
 
213
          if (opt.extra_digest_algo)
 
214
            {
 
215
              if (DBG_X509)
 
216
                log_debug ("enabling extra hash algorithm %d\n", 
 
217
                           opt.extra_digest_algo);
 
218
              gcry_md_enable (data_md, opt.extra_digest_algo);
192
219
            }
193
220
          if (is_detached)
194
221
            {
195
222
              if (data_fd == -1)
196
 
                log_info ("detached signature w/o data "
197
 
                          "- assuming certs-only\n");
 
223
                {
 
224
                  log_info ("detached signature w/o data "
 
225
                            "- assuming certs-only\n");
 
226
                  audit_log (ctrl->audit, AUDIT_CERT_ONLY_SIG);
 
227
                }
198
228
              else
199
 
                hash_data (data_fd, data_md);  
 
229
                audit_log_ok (ctrl->audit, AUDIT_DATA_HASHING,
 
230
                              hash_data (data_fd, data_md));
200
231
            }
201
232
          else
202
233
            {
216
247
      if (rc) 
217
248
        {
218
249
          log_error ("write failed: %s\n", gpg_strerror (rc));
 
250
          audit_log_ok (ctrl->audit, AUDIT_WRITE_ERROR, rc);
219
251
          goto leave;
220
252
        }
221
253
    }
224
256
    {
225
257
      log_error ("data given for a non-detached signature\n");
226
258
      rc = gpg_error (GPG_ERR_CONFLICT);
 
259
      audit_log (ctrl->audit, AUDIT_USAGE_ERROR);
227
260
      goto leave;
228
261
    }
229
262
 
233
266
         certificate first before entering it into the DB.  This way
234
267
         we would avoid cluttering the DB with invalid
235
268
         certificates. */
236
 
      keydb_store_cert (cert, 0, NULL);
 
269
      audit_log_cert (ctrl->audit, AUDIT_SAVE_CERT, cert, 
 
270
                      keydb_store_cert (cert, 0, NULL));
237
271
      ksba_cert_release (cert);
238
272
    }
239
273
 
247
281
      char *msgdigest = NULL;
248
282
      size_t msgdigestlen;
249
283
      char *ctattr;
 
284
      int sigval_hash_algo;
 
285
      int info_pkalgo;
 
286
      unsigned int verifyflags;
250
287
 
251
288
      rc = ksba_cms_get_issuer_serial (cms, signer, &issuer, &serial);
252
289
      if (!signer && gpg_err_code (rc) == GPG_ERR_NO_DATA
264
301
        }
265
302
 
266
303
      gpgsm_status (ctrl, STATUS_NEWSIG, NULL);
 
304
      audit_log_i (ctrl->audit, AUDIT_NEW_SIG, signer);
267
305
 
268
306
      if (DBG_X509)
269
307
        {
273
311
          gpgsm_dump_serial (serial);
274
312
          log_printf ("\n");
275
313
        }
 
314
      if (ctrl->audit)
 
315
        {
 
316
          char *tmpstr = gpgsm_format_sn_issuer (serial, issuer);
 
317
          audit_log_s (ctrl->audit, AUDIT_SIG_NAME, tmpstr);
 
318
          xfree (tmpstr);
 
319
        }
276
320
 
277
321
      rc = ksba_cms_get_signing_time (cms, signer, sigtime);
278
322
      if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
298
342
                             &algo, &is_enabled)
299
343
               || !is_enabled)
300
344
            {
301
 
              log_error ("digest algo %d has not been enabled\n", algo);
 
345
              log_error ("digest algo %d (%s) has not been enabled\n", 
 
346
                         algo, algoid?algoid:"");
 
347
              audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "unsupported");
302
348
              goto next_signer;
303
349
            }
304
350
        }
310
356
          algo = 0; 
311
357
        }
312
358
      else /* real error */
313
 
        break;
 
359
        {
 
360
          audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
 
361
          break;
 
362
        }
314
363
 
315
364
      rc = ksba_cms_get_sigattr_oids (cms, signer,
316
365
                                      "1.2.840.113549.1.9.3", &ctattr);
329
378
                         "actual content-type\n");
330
379
              ksba_free (ctattr);
331
380
              ctattr = NULL;
 
381
              audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
332
382
              goto next_signer;
333
383
            }
334
384
          ksba_free (ctattr);
338
388
        {
339
389
          log_error ("error getting content-type attribute: %s\n",
340
390
                     gpg_strerror (rc));
 
391
          audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
341
392
          goto next_signer;
342
393
        }
343
394
      rc = 0;
347
398
      if (!sigval)
348
399
        {
349
400
          log_error ("no signature value available\n");
 
401
          audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
350
402
          goto next_signer;
351
403
        }
 
404
      sigval_hash_algo = hash_algo_from_sigval (sigval);
352
405
      if (DBG_X509)
353
 
        log_debug ("signer %d - signature available", signer);
 
406
        {
 
407
          log_debug ("signer %d - signature available (sigval hash=%d)",
 
408
                     signer, sigval_hash_algo);
 
409
/*           log_printhex ("sigval    ", sigval, */
 
410
/*                         gcry_sexp_canon_len (sigval, 0, NULL, NULL)); */
 
411
        }
 
412
      if (!sigval_hash_algo)
 
413
        sigval_hash_algo = algo; /* Fallback used e.g. with old libksba. */
354
414
 
355
415
      /* Find the certificate of the signer */
356
416
      keydb_search_reset (kh);
372
432
            gpgsm_status2 (ctrl, STATUS_ERROR, "verify.findkey",
373
433
                           numbuf, NULL);
374
434
          }
375
 
          /* fixme: we might want to append the issuer and serial
376
 
             using our standard notation */
 
435
          audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "no-cert");
377
436
          goto next_signer;
378
437
        }
379
438
 
381
440
      if (rc)
382
441
        {
383
442
          log_error ("failed to get cert: %s\n", gpg_strerror (rc));
 
443
          audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
384
444
          goto next_signer;
385
445
        }
386
446
 
389
449
        gpgsm_dump_time (sigtime);
390
450
      else
391
451
        log_printf (_("[date not given]"));
392
 
      log_printf (_(" using certificate ID %08lX\n"),
 
452
      log_printf (_(" using certificate ID 0x%08lX\n"),
393
453
                  gpgsm_get_short_fingerprint (cert));
394
454
 
395
455
 
398
458
          gcry_md_hd_t md;
399
459
          unsigned char *s;
400
460
 
401
 
          /* check that the message digest in the signed attributes
402
 
             matches the one we calculated on the data */
 
461
          /* Check that the message digest in the signed attributes
 
462
             matches the one we calculated on the data.  */
403
463
          s = gcry_md_read (data_md, algo);
404
464
          if ( !s || !msgdigestlen
405
465
               || gcry_md_get_algo_dlen (algo) != msgdigestlen
409
469
 
410
470
              log_error ("invalid signature: message digest attribute "
411
471
                         "does not match calculated one\n");
412
 
              fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
 
472
              fpr = gpgsm_fpr_and_name_for_status (cert);
413
473
              gpgsm_status (ctrl, STATUS_BADSIG, fpr);
414
474
              xfree (fpr);
 
475
              audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
415
476
              goto next_signer; 
416
477
            }
417
478
            
418
 
          rc = gcry_md_open (&md, algo, 0);
 
479
          rc = gcry_md_open (&md, sigval_hash_algo, 0);
419
480
          if (rc)
420
481
            {
421
482
              log_error ("md_open failed: %s\n", gpg_strerror (rc));
 
483
              audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
422
484
              goto next_signer;
423
485
            }
424
486
          if (DBG_HASHING)
431
493
              log_error ("hashing signed attrs failed: %s\n",
432
494
                         gpg_strerror (rc));
433
495
              gcry_md_close (md);
 
496
              audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
434
497
              goto next_signer;
435
498
            }
436
 
          rc = gpgsm_check_cms_signature (cert, sigval, md, algo);
 
499
          rc = gpgsm_check_cms_signature (cert, sigval, md, 
 
500
                                          sigval_hash_algo, &info_pkalgo);
437
501
          gcry_md_close (md);
438
502
        }
439
503
      else
440
504
        {
441
 
          rc = gpgsm_check_cms_signature (cert, sigval, data_md, algo);
 
505
          rc = gpgsm_check_cms_signature (cert, sigval, data_md, 
 
506
                                          algo, &info_pkalgo);
442
507
        }
443
508
 
444
509
      if (rc)
446
511
          char *fpr;
447
512
 
448
513
          log_error ("invalid signature: %s\n", gpg_strerror (rc));
449
 
          fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
 
514
          fpr = gpgsm_fpr_and_name_for_status (cert);
450
515
          gpgsm_status (ctrl, STATUS_BADSIG, fpr);
451
516
          xfree (fpr);
 
517
          audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
452
518
          goto next_signer;
453
519
        }
454
520
      rc = gpgsm_cert_use_verify_p (cert); /*(this displays an info message)*/
461
527
 
462
528
      if (DBG_X509)
463
529
        log_debug ("signature okay - checking certs\n");
464
 
      rc = gpgsm_validate_chain (ctrl, cert, keyexptime, 0, NULL, 0);
465
 
      if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED)
466
 
        {
467
 
          gpgsm_status (ctrl, STATUS_EXPKEYSIG, NULL);
468
 
          rc = 0;
469
 
        }
470
 
      else
471
 
        gpgsm_status (ctrl, STATUS_GOODSIG, NULL);
472
 
      
 
530
      audit_log (ctrl->audit, AUDIT_VALIDATE_CHAIN);
 
531
      rc = gpgsm_validate_chain (ctrl, cert,
 
532
                                 *sigtime? sigtime : "19700101T000000",
 
533
                                 keyexptime, 0, 
 
534
                                 NULL, 0, &verifyflags);
473
535
      {
474
 
        char *buf, *fpr, *tstr;
 
536
        char *fpr, *buf, *tstr;
 
537
 
 
538
        fpr = gpgsm_fpr_and_name_for_status (cert);
 
539
        if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED)
 
540
          {
 
541
            gpgsm_status (ctrl, STATUS_EXPKEYSIG, fpr);
 
542
            rc = 0;
 
543
          }
 
544
        else
 
545
          gpgsm_status (ctrl, STATUS_GOODSIG, fpr);
 
546
        
 
547
        xfree (fpr);
475
548
 
476
549
        fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
477
550
        tstr = strtimestamp_r (sigtime);
478
 
        buf = xmalloc ( strlen(fpr) + strlen (tstr) + 120);
479
 
        sprintf (buf, "%s %s %s %s", fpr, tstr,
480
 
                 *sigtime? sigtime : "0",
481
 
                 *keyexptime? keyexptime : "0" );
 
551
        buf = xasprintf ("%s %s %s %s 0 0 %d %d 00", fpr, tstr,
 
552
                         *sigtime? sigtime : "0",
 
553
                         *keyexptime? keyexptime : "0",
 
554
                         info_pkalgo, algo);
482
555
        xfree (tstr);
483
556
        xfree (fpr);
484
557
        gpgsm_status (ctrl, STATUS_VALIDSIG, buf);
485
558
        xfree (buf);
486
559
      }
487
560
 
 
561
      audit_log_ok (ctrl->audit, AUDIT_CHAIN_STATUS, rc);
488
562
      if (rc) /* of validate_chain */
489
563
        {
490
564
          log_error ("invalid certification chain: %s\n", gpg_strerror (rc));
497
571
          else
498
572
            gpgsm_status_with_err_code (ctrl, STATUS_TRUST_UNDEFINED, NULL, 
499
573
                                        gpg_err_code (rc));
 
574
          audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
500
575
          goto next_signer;
501
576
        }
502
577
 
 
578
      audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "good");
 
579
 
503
580
      for (i=0; (p = ksba_cert_get_subject (cert, i)); i++)
504
581
        {
505
582
          log_info (!i? _("Good signature from")
510
587
          ksba_free (p);
511
588
        }
512
589
 
513
 
      gpgsm_status (ctrl, STATUS_TRUST_FULLY, NULL);
 
590
      /* Print a note if this is a qualified signature.  */
 
591
      {
 
592
        size_t qualbuflen;
 
593
        char qualbuffer[1];
 
594
        
 
595
        rc = ksba_cert_get_user_data (cert, "is_qualified", &qualbuffer,
 
596
                                      sizeof (qualbuffer), &qualbuflen);
 
597
        if (!rc && qualbuflen)
 
598
          {
 
599
            if (*qualbuffer)
 
600
              {
 
601
                log_info (_("This is a qualified signature\n"));
 
602
                if (!opt.qualsig_approval)
 
603
                  log_info 
 
604
                    (_("Note, that this software is not officially approved "
 
605
                       "to create or verify such signatures.\n"));
 
606
              }
 
607
          }    
 
608
        else if (gpg_err_code (rc) != GPG_ERR_NOT_FOUND)
 
609
          log_error ("get_user_data(is_qualified) failed: %s\n",
 
610
                     gpg_strerror (rc)); 
 
611
      }
 
612
 
 
613
      gpgsm_status (ctrl, STATUS_TRUST_FULLY, 
 
614
                    (verifyflags & VALIDATE_FLAG_CHAIN_MODEL)?
 
615
                    "0 chain": "0 shell");
514
616
          
515
617
 
516
618
    next_signer: