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.
4
4
* This file is part of GnuPG.
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.
11
11
* GnuPG is distributed in the hope that it will be useful,
14
14
* GNU General Public License for more details.
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/>.
21
20
#include <config.h>
59
59
fp = fdopen ( dup (fd), "rb");
62
log_error ("fdopen(%d) failed: %s\n", fd, strerror (errno));
62
err = gpg_error_from_syserror ();
63
log_error ("fdopen(%d) failed: %s\n", fd, gpg_strerror (err));
73
log_error ("read error on fd %d: %s\n", fd, strerror (errno));
75
err = gpg_error_from_syserror ();
76
log_error ("read error on fd %d: %s\n", fd, gpg_strerror (err));
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. */
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)
87
92
Base64Context b64reader = NULL;
129
rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, &writer);
136
rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, NULL, &writer);
132
139
log_error ("can't create writer: %s\n", gpg_strerror (rc));
168
177
if (stopreason == KSBA_SR_NEED_HASH)
180
audit_log (ctrl->audit, AUDIT_DETACHED_SIGNATURE);
172
182
log_info ("detached signature\n");
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 */
188
audit_log (ctrl->audit, AUDIT_GOT_DATA);
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++)
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);
191
gcry_md_enable (data_md, algo);
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);
213
if (opt.extra_digest_algo)
216
log_debug ("enabling extra hash algorithm %d\n",
217
opt.extra_digest_algo);
218
gcry_md_enable (data_md, opt.extra_digest_algo);
195
222
if (data_fd == -1)
196
log_info ("detached signature w/o data "
197
"- assuming certs-only\n");
224
log_info ("detached signature w/o data "
225
"- assuming certs-only\n");
226
audit_log (ctrl->audit, AUDIT_CERT_ONLY_SIG);
199
hash_data (data_fd, data_md);
229
audit_log_ok (ctrl->audit, AUDIT_DATA_HASHING,
230
hash_data (data_fd, data_md));
233
266
certificate first before entering it into the DB. This way
234
267
we would avoid cluttering the DB with invalid
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);
247
281
char *msgdigest = NULL;
248
282
size_t msgdigestlen;
284
int sigval_hash_algo;
286
unsigned int verifyflags;
251
288
rc = ksba_cms_get_issuer_serial (cms, signer, &issuer, &serial);
252
289
if (!signer && gpg_err_code (rc) == GPG_ERR_NO_DATA
273
311
gpgsm_dump_serial (serial);
274
312
log_printf ("\n");
316
char *tmpstr = gpgsm_format_sn_issuer (serial, issuer);
317
audit_log_s (ctrl->audit, AUDIT_SIG_NAME, tmpstr);
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)
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;
312
358
else /* real error */
360
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
315
364
rc = ksba_cms_get_sigattr_oids (cms, signer,
316
365
"1.2.840.113549.1.9.3", &ctattr);
349
400
log_error ("no signature value available\n");
401
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
350
402
goto next_signer;
404
sigval_hash_algo = hash_algo_from_sigval (sigval);
353
log_debug ("signer %d - signature available", signer);
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)); */
412
if (!sigval_hash_algo)
413
sigval_hash_algo = algo; /* Fallback used e.g. with old libksba. */
355
415
/* Find the certificate of the signer */
356
416
keydb_search_reset (kh);
372
432
gpgsm_status2 (ctrl, STATUS_ERROR, "verify.findkey",
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;
389
449
gpgsm_dump_time (sigtime);
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));
399
459
unsigned char *s;
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
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);
475
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
415
476
goto next_signer;
418
rc = gcry_md_open (&md, algo, 0);
479
rc = gcry_md_open (&md, sigval_hash_algo, 0);
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;
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;
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);
441
rc = gpgsm_check_cms_signature (cert, sigval, data_md, algo);
505
rc = gpgsm_check_cms_signature (cert, sigval, data_md,
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);
517
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
452
518
goto next_signer;
454
520
rc = gpgsm_cert_use_verify_p (cert); /*(this displays an info message)*/
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)
467
gpgsm_status (ctrl, STATUS_EXPKEYSIG, NULL);
471
gpgsm_status (ctrl, STATUS_GOODSIG, NULL);
530
audit_log (ctrl->audit, AUDIT_VALIDATE_CHAIN);
531
rc = gpgsm_validate_chain (ctrl, cert,
532
*sigtime? sigtime : "19700101T000000",
534
NULL, 0, &verifyflags);
474
char *buf, *fpr, *tstr;
536
char *fpr, *buf, *tstr;
538
fpr = gpgsm_fpr_and_name_for_status (cert);
539
if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED)
541
gpgsm_status (ctrl, STATUS_EXPKEYSIG, fpr);
545
gpgsm_status (ctrl, STATUS_GOODSIG, fpr);
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",
484
557
gpgsm_status (ctrl, STATUS_VALIDSIG, buf);
561
audit_log_ok (ctrl->audit, AUDIT_CHAIN_STATUS, rc);
488
562
if (rc) /* of validate_chain */
490
564
log_error ("invalid certification chain: %s\n", gpg_strerror (rc));
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;
578
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "good");
503
580
for (i=0; (p = ksba_cert_get_subject (cert, i)); i++)
505
582
log_info (!i? _("Good signature from")
513
gpgsm_status (ctrl, STATUS_TRUST_FULLY, NULL);
590
/* Print a note if this is a qualified signature. */
595
rc = ksba_cert_get_user_data (cert, "is_qualified", &qualbuffer,
596
sizeof (qualbuffer), &qualbuflen);
597
if (!rc && qualbuflen)
601
log_info (_("This is a qualified signature\n"));
602
if (!opt.qualsig_approval)
604
(_("Note, that this software is not officially approved "
605
"to create or verify such signatures.\n"));
608
else if (gpg_err_code (rc) != GPG_ERR_NOT_FOUND)
609
log_error ("get_user_data(is_qualified) failed: %s\n",
613
gpgsm_status (ctrl, STATUS_TRUST_FULLY,
614
(verifyflags & VALIDATE_FLAG_CHAIN_MODEL)?
615
"0 chain": "0 shell");