2003-08-05 17:11:04 +00:00
|
|
|
|
/* verify.c - Verify a messages signature
|
2011-02-04 12:57:53 +01:00
|
|
|
|
* Copyright (C) 2001, 2002, 2003, 2007,
|
2010-03-08 12:22:18 +00:00
|
|
|
|
* 2010 Free Software Foundation, Inc.
|
2003-08-05 17:11:04 +00:00
|
|
|
|
*
|
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 17:11:04 +00:00
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-04 19:49:40 +00:00
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-08-05 17:11:04 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
2011-02-04 12:57:53 +01:00
|
|
|
|
#include <unistd.h>
|
2003-08-05 17:11:04 +00:00
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#include "gpgsm.h"
|
|
|
|
|
#include <gcrypt.h>
|
|
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
|
|
#include "keydb.h"
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
static char *
|
2003-10-31 12:12:47 +00:00
|
|
|
|
strtimestamp_r (ksba_isotime_t atime)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
char *buffer = xmalloc (15);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-10-31 12:12:47 +00:00
|
|
|
|
if (!atime || !*atime)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
strcpy (buffer, "none");
|
|
|
|
|
else
|
2003-10-31 12:12:47 +00:00
|
|
|
|
sprintf (buffer, "%.4s-%.2s-%.2s", atime, atime+4, atime+6);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
|
/* Hash the data for a detached signature. Returns 0 on success. */
|
|
|
|
|
static gpg_error_t
|
2003-08-05 17:11:04 +00:00
|
|
|
|
hash_data (int fd, gcry_md_hd_t md)
|
|
|
|
|
{
|
2007-11-19 16:03:50 +00:00
|
|
|
|
gpg_error_t err = 0;
|
2010-03-08 12:22:18 +00:00
|
|
|
|
estream_t fp;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
char buffer[4096];
|
|
|
|
|
int nread;
|
|
|
|
|
|
2010-03-08 12:22:18 +00:00
|
|
|
|
fp = es_fdopen_nc (fd, "rb");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (!fp)
|
|
|
|
|
{
|
2007-11-19 16:03:50 +00:00
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("fdopen(%d) failed: %s\n", fd, gpg_strerror (err));
|
|
|
|
|
return err;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
do
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2010-03-08 12:22:18 +00:00
|
|
|
|
nread = es_fread (buffer, 1, DIM(buffer), fp);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
gcry_md_write (md, buffer, nread);
|
|
|
|
|
}
|
|
|
|
|
while (nread);
|
2010-03-08 12:22:18 +00:00
|
|
|
|
if (es_ferror (fp))
|
2007-11-19 16:03:50 +00:00
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("read error on fd %d: %s\n", fd, gpg_strerror (err));
|
|
|
|
|
}
|
2010-03-08 12:22:18 +00:00
|
|
|
|
es_fclose (fp);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
return err;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-08 12:22:18 +00:00
|
|
|
|
/* Perform a verify operation. To verify detached signatures, DATA_FD
|
2003-08-05 17:11:04 +00:00
|
|
|
|
must be different than -1. With OUT_FP given and a non-detached
|
2010-03-08 12:22:18 +00:00
|
|
|
|
signature, the signed material is written to that stream. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int
|
2010-03-08 12:22:18 +00:00
|
|
|
|
gpgsm_verify (ctrl_t ctrl, int in_fd, int data_fd, estream_t out_fp)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int i, rc;
|
|
|
|
|
Base64Context b64reader = NULL;
|
|
|
|
|
Base64Context b64writer = NULL;
|
2003-12-17 12:28:24 +00:00
|
|
|
|
ksba_reader_t reader;
|
|
|
|
|
ksba_writer_t writer = NULL;
|
|
|
|
|
ksba_cms_t cms = NULL;
|
|
|
|
|
ksba_stop_reason_t stopreason;
|
|
|
|
|
ksba_cert_t cert;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
KEYDB_HANDLE kh;
|
|
|
|
|
gcry_md_hd_t data_md = NULL;
|
|
|
|
|
int signer;
|
|
|
|
|
const char *algoid;
|
|
|
|
|
int algo;
|
|
|
|
|
int is_detached;
|
2010-03-08 12:22:18 +00:00
|
|
|
|
estream_t in_fp = NULL;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
char *p;
|
|
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_set_type (ctrl->audit, AUDIT_TYPE_VERIFY);
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
kh = keydb_new (0);
|
|
|
|
|
if (!kh)
|
|
|
|
|
{
|
|
|
|
|
log_error (_("failed to allocated keyDB handle\n"));
|
|
|
|
|
rc = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-03-08 12:22:18 +00:00
|
|
|
|
in_fp = es_fdopen_nc (in_fd, "rb");
|
|
|
|
|
if (!in_fp)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2010-03-08 12:22:18 +00:00
|
|
|
|
rc = gpg_error_from_syserror ();
|
2003-08-05 17:11:04 +00:00
|
|
|
|
log_error ("fdopen() failed: %s\n", strerror (errno));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 12:22:18 +00:00
|
|
|
|
rc = gpgsm_create_reader (&b64reader, ctrl, in_fp, 0, &reader);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("can't create reader: %s\n", gpg_strerror (rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (out_fp)
|
|
|
|
|
{
|
2010-11-26 09:42:56 +00:00
|
|
|
|
rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, &writer);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("can't create writer: %s\n", gpg_strerror (rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = ksba_cms_new (&cms);
|
|
|
|
|
if (rc)
|
|
|
|
|
goto leave;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = ksba_cms_set_reader_writer (cms, reader, writer);
|
|
|
|
|
if (rc)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
log_error ("ksba_cms_set_reader_writer failed: %s\n",
|
2004-06-06 13:00:59 +00:00
|
|
|
|
gpg_strerror (rc));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = gcry_md_open (&data_md, 0, 0);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("md_open failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (DBG_HASHING)
|
2011-09-20 09:54:27 +02:00
|
|
|
|
gcry_md_debug (data_md, "vrfy.data");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log (ctrl->audit, AUDIT_SETUP_READY);
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
is_detached = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
do
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = ksba_cms_parse (cms, &stopreason);
|
|
|
|
|
if (rc)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-06-06 13:00:59 +00:00
|
|
|
|
log_error ("ksba_cms_parse failed: %s\n", gpg_strerror (rc));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stopreason == KSBA_SR_NEED_HASH)
|
|
|
|
|
{
|
|
|
|
|
is_detached = 1;
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log (ctrl->audit, AUDIT_DETACHED_SIGNATURE);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("detached signature\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stopreason == KSBA_SR_NEED_HASH
|
|
|
|
|
|| stopreason == KSBA_SR_BEGIN_DATA)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
{
|
2007-12-06 15:55:03 +00:00
|
|
|
|
audit_log (ctrl->audit, AUDIT_GOT_DATA);
|
|
|
|
|
|
|
|
|
|
/* We are now able to enable the hash algorithms */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
for (i=0; (algoid=ksba_cms_get_digest_algo_list (cms, i)); i++)
|
|
|
|
|
{
|
|
|
|
|
algo = gcry_md_map_name (algoid);
|
|
|
|
|
if (!algo)
|
2005-11-13 19:07:06 +00:00
|
|
|
|
{
|
|
|
|
|
log_error ("unknown hash algorithm `%s'\n",
|
|
|
|
|
algoid? algoid:"?");
|
|
|
|
|
if (algoid
|
|
|
|
|
&& ( !strcmp (algoid, "1.2.840.113549.1.1.2")
|
|
|
|
|
||!strcmp (algoid, "1.2.840.113549.2.2")))
|
|
|
|
|
log_info (_("(this is the MD2 algorithm)\n"));
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_BAD_DATA_HASH_ALGO, algoid);
|
2005-11-13 19:07:06 +00:00
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
else
|
2007-11-19 16:03:50 +00:00
|
|
|
|
{
|
2007-12-13 15:45:40 +00:00
|
|
|
|
if (DBG_X509)
|
|
|
|
|
log_debug ("enabling hash algorithm %d (%s)\n",
|
|
|
|
|
algo, algoid? algoid:"");
|
2007-11-19 16:03:50 +00:00
|
|
|
|
gcry_md_enable (data_md, algo);
|
|
|
|
|
audit_log_i (ctrl->audit, AUDIT_DATA_HASH_ALGO, algo);
|
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
2007-12-13 15:45:40 +00:00
|
|
|
|
if (opt.extra_digest_algo)
|
|
|
|
|
{
|
|
|
|
|
if (DBG_X509)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
log_debug ("enabling extra hash algorithm %d\n",
|
2007-12-13 15:45:40 +00:00
|
|
|
|
opt.extra_digest_algo);
|
|
|
|
|
gcry_md_enable (data_md, opt.extra_digest_algo);
|
2009-12-02 18:33:59 +00:00
|
|
|
|
audit_log_i (ctrl->audit, AUDIT_DATA_HASH_ALGO,
|
|
|
|
|
opt.extra_digest_algo);
|
2007-12-13 15:45:40 +00:00
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (is_detached)
|
|
|
|
|
{
|
|
|
|
|
if (data_fd == -1)
|
2007-11-19 16:03:50 +00:00
|
|
|
|
{
|
|
|
|
|
log_info ("detached signature w/o data "
|
|
|
|
|
"- assuming certs-only\n");
|
|
|
|
|
audit_log (ctrl->audit, AUDIT_CERT_ONLY_SIG);
|
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
else
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_ok (ctrl->audit, AUDIT_DATA_HASHING,
|
|
|
|
|
hash_data (data_fd, data_md));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ksba_cms_set_hash_function (cms, HASH_FNC, data_md);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (stopreason == KSBA_SR_END_DATA)
|
|
|
|
|
{ /* The data bas been hashed */
|
2009-12-02 18:33:59 +00:00
|
|
|
|
audit_log_ok (ctrl->audit, AUDIT_DATA_HASHING, 0);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
while (stopreason != KSBA_SR_READY);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
if (b64writer)
|
|
|
|
|
{
|
|
|
|
|
rc = gpgsm_finish_writer (b64writer);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
if (rc)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
log_error ("write failed: %s\n", gpg_strerror (rc));
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_ok (ctrl->audit, AUDIT_WRITE_ERROR, rc);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data_fd != -1 && !is_detached)
|
|
|
|
|
{
|
|
|
|
|
log_error ("data given for a non-detached signature\n");
|
|
|
|
|
rc = gpg_error (GPG_ERR_CONFLICT);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log (ctrl->audit, AUDIT_USAGE_ERROR);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i=0; (cert=ksba_cms_get_cert (cms, i)); i++)
|
|
|
|
|
{
|
|
|
|
|
/* Fixme: it might be better to check the validity of the
|
|
|
|
|
certificate first before entering it into the DB. This way
|
|
|
|
|
we would avoid cluttering the DB with invalid
|
|
|
|
|
certificates. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
audit_log_cert (ctrl->audit, AUDIT_SAVE_CERT, cert,
|
2007-11-19 16:03:50 +00:00
|
|
|
|
keydb_store_cert (cert, 0, NULL));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
ksba_cert_release (cert);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cert = NULL;
|
|
|
|
|
for (signer=0; ; signer++)
|
|
|
|
|
{
|
|
|
|
|
char *issuer = NULL;
|
2003-12-17 12:28:24 +00:00
|
|
|
|
ksba_sexp_t sigval = NULL;
|
2003-10-31 12:12:47 +00:00
|
|
|
|
ksba_isotime_t sigtime, keyexptime;
|
2003-12-17 12:28:24 +00:00
|
|
|
|
ksba_sexp_t serial;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
char *msgdigest = NULL;
|
|
|
|
|
size_t msgdigestlen;
|
|
|
|
|
char *ctattr;
|
2007-12-13 15:45:40 +00:00
|
|
|
|
int sigval_hash_algo;
|
2007-08-10 16:52:05 +00:00
|
|
|
|
int info_pkalgo;
|
|
|
|
|
unsigned int verifyflags;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = ksba_cms_get_issuer_serial (cms, signer, &issuer, &serial);
|
|
|
|
|
if (!signer && gpg_err_code (rc) == GPG_ERR_NO_DATA
|
2003-11-12 15:17:44 +00:00
|
|
|
|
&& data_fd == -1 && is_detached)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
log_info ("certs-only message accepted\n");
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = 0;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2004-06-06 13:00:59 +00:00
|
|
|
|
if (rc)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-06-06 13:00:59 +00:00
|
|
|
|
if (signer && rc == -1)
|
|
|
|
|
rc = 0;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2004-04-05 17:25:21 +00:00
|
|
|
|
|
|
|
|
|
gpgsm_status (ctrl, STATUS_NEWSIG, NULL);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_i (ctrl->audit, AUDIT_NEW_SIG, signer);
|
2004-04-05 17:25:21 +00:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (DBG_X509)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("signer %d - issuer: `%s'\n",
|
|
|
|
|
signer, issuer? issuer:"[NONE]");
|
|
|
|
|
log_debug ("signer %d - serial: ", signer);
|
|
|
|
|
gpgsm_dump_serial (serial);
|
|
|
|
|
log_printf ("\n");
|
|
|
|
|
}
|
2007-11-19 16:03:50 +00:00
|
|
|
|
if (ctrl->audit)
|
|
|
|
|
{
|
|
|
|
|
char *tmpstr = gpgsm_format_sn_issuer (serial, issuer);
|
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_NAME, tmpstr);
|
|
|
|
|
xfree (tmpstr);
|
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = ksba_cms_get_signing_time (cms, signer, sigtime);
|
|
|
|
|
if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
|
2003-10-31 12:12:47 +00:00
|
|
|
|
*sigtime = 0;
|
2004-06-06 13:00:59 +00:00
|
|
|
|
else if (rc)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-06-06 13:00:59 +00:00
|
|
|
|
log_error ("error getting signing time: %s\n", gpg_strerror (rc));
|
|
|
|
|
*sigtime = 0; /* (we can't encode an error in the time string.) */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = ksba_cms_get_message_digest (cms, signer,
|
|
|
|
|
&msgdigest, &msgdigestlen);
|
|
|
|
|
if (!rc)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2003-11-18 17:29:51 +00:00
|
|
|
|
size_t is_enabled;
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
algoid = ksba_cms_get_digest_algo (cms, signer);
|
|
|
|
|
algo = gcry_md_map_name (algoid);
|
|
|
|
|
if (DBG_X509)
|
|
|
|
|
log_debug ("signer %d - digest algo: %d\n", signer, algo);
|
2003-11-18 17:29:51 +00:00
|
|
|
|
is_enabled = sizeof algo;
|
|
|
|
|
if ( gcry_md_info (data_md, GCRYCTL_IS_ALGO_ENABLED,
|
|
|
|
|
&algo, &is_enabled)
|
|
|
|
|
|| !is_enabled)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
|
log_error ("digest algo %d (%s) has not been enabled\n",
|
2007-12-13 15:45:40 +00:00
|
|
|
|
algo, algoid?algoid:"");
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "unsupported");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-06-06 13:00:59 +00:00
|
|
|
|
else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
assert (!msgdigest);
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = 0;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
algoid = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
algo = 0;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
else /* real error */
|
2007-11-19 16:03:50 +00:00
|
|
|
|
{
|
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = ksba_cms_get_sigattr_oids (cms, signer,
|
|
|
|
|
"1.2.840.113549.1.9.3", &ctattr);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
if (!rc)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
|
|
if (DBG_X509)
|
2004-06-06 13:00:59 +00:00
|
|
|
|
log_debug ("signer %d - content-type attribute: %s",
|
|
|
|
|
signer, ctattr);
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
s = ksba_cms_get_content_oid (cms, 1);
|
|
|
|
|
if (!s || strcmp (ctattr, s))
|
|
|
|
|
{
|
|
|
|
|
log_error ("content-type attribute does not match "
|
|
|
|
|
"actual content-type\n");
|
|
|
|
|
ksba_free (ctattr);
|
|
|
|
|
ctattr = NULL;
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
|
|
|
|
ksba_free (ctattr);
|
|
|
|
|
ctattr = NULL;
|
|
|
|
|
}
|
2004-06-06 13:00:59 +00:00
|
|
|
|
else if (rc != -1)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
log_error ("error getting content-type attribute: %s\n",
|
2004-06-06 13:00:59 +00:00
|
|
|
|
gpg_strerror (rc));
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
2004-06-06 13:00:59 +00:00
|
|
|
|
rc = 0;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sigval = ksba_cms_get_sig_val (cms, signer);
|
|
|
|
|
if (!sigval)
|
|
|
|
|
{
|
|
|
|
|
log_error ("no signature value available\n");
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
2007-12-13 15:45:40 +00:00
|
|
|
|
sigval_hash_algo = hash_algo_from_sigval (sigval);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (DBG_X509)
|
2007-12-13 15:45:40 +00:00
|
|
|
|
{
|
|
|
|
|
log_debug ("signer %d - signature available (sigval hash=%d)",
|
|
|
|
|
signer, sigval_hash_algo);
|
|
|
|
|
/* log_printhex ("sigval ", sigval, */
|
|
|
|
|
/* gcry_sexp_canon_len (sigval, 0, NULL, NULL)); */
|
|
|
|
|
}
|
|
|
|
|
if (!sigval_hash_algo)
|
|
|
|
|
sigval_hash_algo = algo; /* Fallback used e.g. with old libksba. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
/* Find the certificate of the signer */
|
|
|
|
|
keydb_search_reset (kh);
|
|
|
|
|
rc = keydb_search_issuer_sn (kh, issuer, serial);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
if (rc == -1)
|
|
|
|
|
{
|
|
|
|
|
log_error ("certificate not found\n");
|
|
|
|
|
rc = gpg_error (GPG_ERR_NO_PUBKEY);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
log_error ("failed to find the certificate: %s\n",
|
|
|
|
|
gpg_strerror(rc));
|
|
|
|
|
{
|
|
|
|
|
char numbuf[50];
|
|
|
|
|
sprintf (numbuf, "%d", rc);
|
|
|
|
|
|
|
|
|
|
gpgsm_status2 (ctrl, STATUS_ERROR, "verify.findkey",
|
|
|
|
|
numbuf, NULL);
|
|
|
|
|
}
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "no-cert");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = keydb_get_cert (kh, &cert);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("failed to get cert: %s\n", gpg_strerror (rc));
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_info (_("Signature made "));
|
2003-10-31 12:12:47 +00:00
|
|
|
|
if (*sigtime)
|
2009-03-16 09:44:44 +00:00
|
|
|
|
dump_isotime (sigtime);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
else
|
|
|
|
|
log_printf (_("[date not given]"));
|
2007-08-10 16:52:05 +00:00
|
|
|
|
log_printf (_(" using certificate ID 0x%08lX\n"),
|
2009-03-25 16:05:16 +00:00
|
|
|
|
gpgsm_get_short_fingerprint (cert, NULL));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2009-12-02 18:33:59 +00:00
|
|
|
|
audit_log_i (ctrl->audit, AUDIT_DATA_HASH_ALGO, algo);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
if (msgdigest)
|
|
|
|
|
{ /* Signed attributes are available. */
|
|
|
|
|
gcry_md_hd_t md;
|
|
|
|
|
unsigned char *s;
|
|
|
|
|
|
2007-12-13 15:45:40 +00:00
|
|
|
|
/* Check that the message digest in the signed attributes
|
|
|
|
|
matches the one we calculated on the data. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
s = gcry_md_read (data_md, algo);
|
|
|
|
|
if ( !s || !msgdigestlen
|
|
|
|
|
|| gcry_md_get_algo_dlen (algo) != msgdigestlen
|
|
|
|
|
|| !s || memcmp (s, msgdigest, msgdigestlen) )
|
|
|
|
|
{
|
|
|
|
|
char *fpr;
|
|
|
|
|
|
2008-04-18 09:20:25 +00:00
|
|
|
|
log_error (_("invalid signature: message digest attribute "
|
|
|
|
|
"does not match computed one\n"));
|
|
|
|
|
if (DBG_X509)
|
|
|
|
|
{
|
|
|
|
|
if (msgdigest)
|
|
|
|
|
log_printhex ("message: ", msgdigest, msgdigestlen);
|
|
|
|
|
if (s)
|
|
|
|
|
log_printhex ("computed: ",
|
|
|
|
|
s, gcry_md_get_algo_dlen (algo));
|
|
|
|
|
}
|
2006-11-14 10:23:21 +00:00
|
|
|
|
fpr = gpgsm_fpr_and_name_for_status (cert);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
gpgsm_status (ctrl, STATUS_BADSIG, fpr);
|
|
|
|
|
xfree (fpr);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
goto next_signer;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2009-12-02 18:33:59 +00:00
|
|
|
|
audit_log_i (ctrl->audit, AUDIT_ATTR_HASH_ALGO, sigval_hash_algo);
|
2007-12-13 15:45:40 +00:00
|
|
|
|
rc = gcry_md_open (&md, sigval_hash_algo, 0);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("md_open failed: %s\n", gpg_strerror (rc));
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
|
|
|
|
if (DBG_HASHING)
|
2011-09-20 09:54:27 +02:00
|
|
|
|
gcry_md_debug (md, "vrfy.attr");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
ksba_cms_set_hash_function (cms, HASH_FNC, md);
|
|
|
|
|
rc = ksba_cms_hash_signed_attrs (cms, signer);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("hashing signed attrs failed: %s\n",
|
2003-11-12 15:17:44 +00:00
|
|
|
|
gpg_strerror (rc));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
gcry_md_close (md);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "error");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
rc = gpgsm_check_cms_signature (cert, sigval, md,
|
2007-12-13 15:45:40 +00:00
|
|
|
|
sigval_hash_algo, &info_pkalgo);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
gcry_md_close (md);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
|
rc = gpgsm_check_cms_signature (cert, sigval, data_md,
|
2007-12-13 15:45:40 +00:00
|
|
|
|
algo, &info_pkalgo);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
char *fpr;
|
|
|
|
|
|
|
|
|
|
log_error ("invalid signature: %s\n", gpg_strerror (rc));
|
2006-11-14 10:23:21 +00:00
|
|
|
|
fpr = gpgsm_fpr_and_name_for_status (cert);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
gpgsm_status (ctrl, STATUS_BADSIG, fpr);
|
|
|
|
|
xfree (fpr);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
|
|
|
|
rc = gpgsm_cert_use_verify_p (cert); /*(this displays an info message)*/
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
gpgsm_status_with_err_code (ctrl, STATUS_ERROR, "verify.keyusage",
|
|
|
|
|
gpg_err_code (rc));
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DBG_X509)
|
|
|
|
|
log_debug ("signature okay - checking certs\n");
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log (ctrl->audit, AUDIT_VALIDATE_CHAIN);
|
2007-08-10 16:52:05 +00:00
|
|
|
|
rc = gpgsm_validate_chain (ctrl, cert,
|
|
|
|
|
*sigtime? sigtime : "19700101T000000",
|
2011-02-04 12:57:53 +01:00
|
|
|
|
keyexptime, 0,
|
2007-08-10 16:52:05 +00:00
|
|
|
|
NULL, 0, &verifyflags);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2006-11-14 10:23:21 +00:00
|
|
|
|
char *fpr, *buf, *tstr;
|
|
|
|
|
|
|
|
|
|
fpr = gpgsm_fpr_and_name_for_status (cert);
|
|
|
|
|
if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED)
|
|
|
|
|
{
|
|
|
|
|
gpgsm_status (ctrl, STATUS_EXPKEYSIG, fpr);
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
gpgsm_status (ctrl, STATUS_GOODSIG, fpr);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2006-11-14 10:23:21 +00:00
|
|
|
|
xfree (fpr);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
|
|
|
|
|
tstr = strtimestamp_r (sigtime);
|
2007-08-10 16:52:05 +00:00
|
|
|
|
buf = xasprintf ("%s %s %s %s 0 0 %d %d 00", fpr, tstr,
|
|
|
|
|
*sigtime? sigtime : "0",
|
|
|
|
|
*keyexptime? keyexptime : "0",
|
|
|
|
|
info_pkalgo, algo);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
xfree (tstr);
|
|
|
|
|
xfree (fpr);
|
|
|
|
|
gpgsm_status (ctrl, STATUS_VALIDSIG, buf);
|
|
|
|
|
xfree (buf);
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-06 15:55:03 +00:00
|
|
|
|
audit_log_ok (ctrl->audit, AUDIT_CHAIN_STATUS, rc);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc) /* of validate_chain */
|
|
|
|
|
{
|
|
|
|
|
log_error ("invalid certification chain: %s\n", gpg_strerror (rc));
|
|
|
|
|
if (gpg_err_code (rc) == GPG_ERR_BAD_CERT_CHAIN
|
|
|
|
|
|| gpg_err_code (rc) == GPG_ERR_BAD_CERT
|
|
|
|
|
|| gpg_err_code (rc) == GPG_ERR_BAD_CA_CERT
|
|
|
|
|
|| gpg_err_code (rc) == GPG_ERR_CERT_REVOKED)
|
|
|
|
|
gpgsm_status_with_err_code (ctrl, STATUS_TRUST_NEVER, NULL,
|
|
|
|
|
gpg_err_code (rc));
|
|
|
|
|
else
|
2011-02-04 12:57:53 +01:00
|
|
|
|
gpgsm_status_with_err_code (ctrl, STATUS_TRUST_UNDEFINED, NULL,
|
2003-08-05 17:11:04 +00:00
|
|
|
|
gpg_err_code (rc));
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "bad");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
goto next_signer;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
|
audit_log_s (ctrl->audit, AUDIT_SIG_STATUS, "good");
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
for (i=0; (p = ksba_cert_get_subject (cert, i)); i++)
|
|
|
|
|
{
|
|
|
|
|
log_info (!i? _("Good signature from")
|
|
|
|
|
: _(" aka"));
|
|
|
|
|
log_printf (" \"");
|
2010-03-15 11:15:45 +00:00
|
|
|
|
gpgsm_es_print_name (log_get_stream (), p);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
log_printf ("\"\n");
|
|
|
|
|
ksba_free (p);
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-10 16:52:05 +00:00
|
|
|
|
/* Print a note if this is a qualified signature. */
|
|
|
|
|
{
|
|
|
|
|
size_t qualbuflen;
|
|
|
|
|
char qualbuffer[1];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2007-08-10 16:52:05 +00:00
|
|
|
|
rc = ksba_cert_get_user_data (cert, "is_qualified", &qualbuffer,
|
|
|
|
|
sizeof (qualbuffer), &qualbuflen);
|
|
|
|
|
if (!rc && qualbuflen)
|
|
|
|
|
{
|
|
|
|
|
if (*qualbuffer)
|
|
|
|
|
{
|
|
|
|
|
log_info (_("This is a qualified signature\n"));
|
|
|
|
|
if (!opt.qualsig_approval)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
log_info
|
2007-08-10 16:52:05 +00:00
|
|
|
|
(_("Note, that this software is not officially approved "
|
|
|
|
|
"to create or verify such signatures.\n"));
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
}
|
2007-08-10 16:52:05 +00:00
|
|
|
|
else if (gpg_err_code (rc) != GPG_ERR_NOT_FOUND)
|
|
|
|
|
log_error ("get_user_data(is_qualified) failed: %s\n",
|
2011-02-04 12:57:53 +01:00
|
|
|
|
gpg_strerror (rc));
|
2007-08-10 16:52:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
gpgsm_status (ctrl, STATUS_TRUST_FULLY,
|
2011-12-07 16:15:15 +01:00
|
|
|
|
(verifyflags & VALIDATE_FLAG_STEED)?
|
|
|
|
|
"0 steed":
|
2007-08-10 16:52:05 +00:00
|
|
|
|
(verifyflags & VALIDATE_FLAG_CHAIN_MODEL)?
|
|
|
|
|
"0 chain": "0 shell");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
next_signer:
|
|
|
|
|
rc = 0;
|
|
|
|
|
xfree (issuer);
|
|
|
|
|
xfree (serial);
|
|
|
|
|
xfree (sigval);
|
|
|
|
|
xfree (msgdigest);
|
|
|
|
|
ksba_cert_release (cert);
|
|
|
|
|
cert = NULL;
|
|
|
|
|
}
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
ksba_cms_release (cms);
|
|
|
|
|
gpgsm_destroy_reader (b64reader);
|
|
|
|
|
gpgsm_destroy_writer (b64writer);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
keydb_release (kh);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
gcry_md_close (data_md);
|
2010-03-08 12:22:18 +00:00
|
|
|
|
es_fclose (in_fp);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
char numbuf[50];
|
|
|
|
|
sprintf (numbuf, "%d", rc );
|
|
|
|
|
gpgsm_status2 (ctrl, STATUS_ERROR, "verify.leave",
|
|
|
|
|
numbuf, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|