2001-11-13 12:50:14 +00:00
|
|
|
/* certcheck.c - check one certificate
|
2003-06-03 19:55:50 +00:00
|
|
|
* Copyright (C) 2001, 2003 Free Software Foundation, Inc.
|
2001-11-13 12:50:14 +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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2003-06-27 20:53:09 +00:00
|
|
|
#include "gpgsm.h"
|
2001-11-13 12:50:14 +00:00
|
|
|
#include <gcrypt.h>
|
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
#include "keydb.h"
|
|
|
|
#include "i18n.h"
|
|
|
|
|
2001-11-16 17:56:23 +00:00
|
|
|
|
2001-11-13 12:50:14 +00:00
|
|
|
static int
|
2003-06-27 20:53:09 +00:00
|
|
|
do_encode_md (gcry_md_hd_t md, int algo, unsigned int nbits,
|
|
|
|
gcry_mpi_t *r_val)
|
2001-11-13 12:50:14 +00:00
|
|
|
{
|
|
|
|
int nframe = (nbits+7) / 8;
|
|
|
|
byte *frame;
|
|
|
|
int i, n;
|
2001-11-16 17:56:23 +00:00
|
|
|
byte asn[100];
|
|
|
|
size_t asnlen;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
asnlen = DIM(asn);
|
|
|
|
if (gcry_md_algo_info (algo, GCRYCTL_GET_ASNOID, asn, &asnlen))
|
|
|
|
{
|
|
|
|
log_error ("No object identifier for algo %d\n", algo);
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
2001-11-16 17:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = gcry_md_get_algo_dlen (algo);
|
2001-11-13 12:50:14 +00:00
|
|
|
|
|
|
|
if ( len + asnlen + 4 > nframe )
|
|
|
|
{
|
|
|
|
log_error ("can't encode a %d bit MD into a %d bits frame\n",
|
|
|
|
(int)(len*8), (int)nbits);
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
2001-11-13 12:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We encode the MD in this way:
|
|
|
|
*
|
|
|
|
* 0 A PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
|
|
|
|
*
|
|
|
|
* PAD consists of FF bytes.
|
|
|
|
*/
|
|
|
|
frame = xtrymalloc (nframe);
|
|
|
|
if (!frame)
|
2003-06-03 19:55:50 +00:00
|
|
|
return OUT_OF_CORE (errno);
|
2001-11-13 12:50:14 +00:00
|
|
|
n = 0;
|
|
|
|
frame[n++] = 0;
|
|
|
|
frame[n++] = 1; /* block type */
|
|
|
|
i = nframe - len - asnlen -3 ;
|
|
|
|
assert ( i > 1 );
|
|
|
|
memset ( frame+n, 0xff, i ); n += i;
|
|
|
|
frame[n++] = 0;
|
|
|
|
memcpy ( frame+n, asn, asnlen ); n += asnlen;
|
|
|
|
memcpy ( frame+n, gcry_md_read(md, algo), len ); n += len;
|
|
|
|
assert ( n == nframe );
|
2001-11-16 17:56:23 +00:00
|
|
|
if (DBG_X509)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
log_debug ("encoded hash:");
|
|
|
|
for (j=0; j < nframe; j++)
|
|
|
|
log_printf (" %02X", frame[j]);
|
|
|
|
log_printf ("\n");
|
|
|
|
}
|
|
|
|
|
2003-07-28 08:59:18 +00:00
|
|
|
gcry_mpi_scan (r_val, GCRYMPI_FMT_USG, frame, n, &nframe);
|
2001-11-13 12:50:14 +00:00
|
|
|
xfree (frame);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check the signature on CERT using the ISSUER-CERT. This function
|
|
|
|
does only test the cryptographic signature and nothing else. It is
|
|
|
|
assumed that the ISSUER_CERT is valid. */
|
|
|
|
int
|
|
|
|
gpgsm_check_cert_sig (KsbaCert issuer_cert, KsbaCert cert)
|
|
|
|
{
|
2001-11-20 18:28:53 +00:00
|
|
|
const char *algoid;
|
2003-06-27 20:53:09 +00:00
|
|
|
gcry_md_hd_t md;
|
2001-11-13 12:50:14 +00:00
|
|
|
int rc, algo;
|
2003-06-27 20:53:09 +00:00
|
|
|
gcry_mpi_t frame;
|
2001-12-18 17:37:48 +00:00
|
|
|
KsbaSexp p;
|
|
|
|
size_t n;
|
2003-06-27 20:53:09 +00:00
|
|
|
gcry_sexp_t s_sig, s_hash, s_pkey;
|
2001-11-13 12:50:14 +00:00
|
|
|
|
2001-11-20 18:28:53 +00:00
|
|
|
algo = gcry_md_map_name ( (algoid=ksba_cert_get_digest_algo (cert)));
|
|
|
|
if (!algo)
|
|
|
|
{
|
|
|
|
log_error ("unknown hash algorithm `%s'\n", algoid? algoid:"?");
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
2001-11-20 18:28:53 +00:00
|
|
|
}
|
2003-06-27 20:53:09 +00:00
|
|
|
rc = gcry_md_open (&md, algo, 0);
|
|
|
|
if (rc)
|
2001-11-13 12:50:14 +00:00
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
log_error ("md_open failed: %s\n", gpg_strerror (rc));
|
|
|
|
return rc;
|
2001-11-13 12:50:14 +00:00
|
|
|
}
|
2002-09-19 12:59:57 +00:00
|
|
|
if (DBG_HASHING)
|
|
|
|
gcry_md_start_debug (md, "hash.cert");
|
2001-11-13 12:50:14 +00:00
|
|
|
|
|
|
|
rc = ksba_cert_hash (cert, 1, HASH_FNC, md);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("ksba_cert_hash failed: %s\n", ksba_strerror (rc));
|
|
|
|
gcry_md_close (md);
|
|
|
|
return map_ksba_err (rc);
|
|
|
|
}
|
|
|
|
gcry_md_final (md);
|
|
|
|
|
2001-12-18 17:37:48 +00:00
|
|
|
p = ksba_cert_get_sig_val (cert);
|
|
|
|
n = gcry_sexp_canon_len (p, 0, NULL, NULL);
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
log_error ("libksba did not return a proper S-Exp\n");
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_md_close (md);
|
|
|
|
ksba_free (p);
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_BUG);
|
2001-12-18 17:37:48 +00:00
|
|
|
}
|
2002-09-19 12:59:57 +00:00
|
|
|
if (DBG_X509)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
log_debug ("signature value:");
|
|
|
|
for (j=0; j < n; j++)
|
|
|
|
log_printf (" %02X", p[j]);
|
|
|
|
log_printf ("\n");
|
|
|
|
}
|
|
|
|
|
2001-12-18 17:37:48 +00:00
|
|
|
rc = gcry_sexp_sscan ( &s_sig, NULL, p, n);
|
2002-08-09 18:12:22 +00:00
|
|
|
ksba_free (p);
|
2001-11-13 12:50:14 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc));
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_md_close (md);
|
2003-06-27 20:53:09 +00:00
|
|
|
return rc;
|
2001-11-13 12:50:14 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 17:56:23 +00:00
|
|
|
p = ksba_cert_get_public_key (issuer_cert);
|
2001-12-18 17:37:48 +00:00
|
|
|
n = gcry_sexp_canon_len (p, 0, NULL, NULL);
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
log_error ("libksba did not return a proper S-Exp\n");
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_md_close (md);
|
|
|
|
ksba_free (p);
|
|
|
|
gcry_sexp_release (s_sig);
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_BUG);
|
2001-12-18 17:37:48 +00:00
|
|
|
}
|
|
|
|
rc = gcry_sexp_sscan ( &s_pkey, NULL, p, n);
|
2002-08-09 18:12:22 +00:00
|
|
|
ksba_free (p);
|
2001-11-16 17:56:23 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc));
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_md_close (md);
|
|
|
|
gcry_sexp_release (s_sig);
|
2003-06-27 20:53:09 +00:00
|
|
|
return rc;
|
2001-11-16 17:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = do_encode_md (md, algo, gcry_pk_get_nbits (s_pkey), &frame);
|
2001-11-13 12:50:14 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_md_close (md);
|
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
gcry_sexp_release (s_pkey);
|
2001-11-13 12:50:14 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2002-08-09 18:12:22 +00:00
|
|
|
|
2001-11-13 12:50:14 +00:00
|
|
|
/* put hash into the S-Exp s_hash */
|
|
|
|
if ( gcry_sexp_build (&s_hash, NULL, "%m", frame) )
|
|
|
|
BUG ();
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_mpi_release (frame);
|
2002-09-19 12:59:57 +00:00
|
|
|
|
2001-11-16 17:56:23 +00:00
|
|
|
|
|
|
|
rc = gcry_pk_verify (s_sig, s_hash, s_pkey);
|
|
|
|
if (DBG_CRYPTO)
|
2003-06-27 20:53:09 +00:00
|
|
|
log_debug ("gcry_pk_verify: %s\n", gpg_strerror (rc));
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_md_close (md);
|
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
gcry_sexp_release (s_hash);
|
|
|
|
gcry_sexp_release (s_pkey);
|
2003-06-27 20:53:09 +00:00
|
|
|
return rc;
|
2001-11-16 17:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2001-12-18 17:37:48 +00:00
|
|
|
gpgsm_check_cms_signature (KsbaCert cert, KsbaConstSexp sigval,
|
2003-06-27 20:53:09 +00:00
|
|
|
gcry_md_hd_t md, int algo)
|
2001-11-16 17:56:23 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2001-12-18 17:37:48 +00:00
|
|
|
KsbaSexp p;
|
2003-06-27 20:53:09 +00:00
|
|
|
gcry_mpi_t frame;
|
|
|
|
gcry_sexp_t s_sig, s_hash, s_pkey;
|
2001-12-18 17:37:48 +00:00
|
|
|
size_t n;
|
2001-11-16 17:56:23 +00:00
|
|
|
|
2001-12-18 17:37:48 +00:00
|
|
|
n = gcry_sexp_canon_len (sigval, 0, NULL, NULL);
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
log_error ("libksba did not return a proper S-Exp\n");
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_BUG);
|
2001-12-18 17:37:48 +00:00
|
|
|
}
|
|
|
|
rc = gcry_sexp_sscan (&s_sig, NULL, sigval, n);
|
2001-11-16 17:56:23 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc));
|
|
|
|
return rc;
|
2001-11-16 17:56:23 +00:00
|
|
|
}
|
|
|
|
|
2001-12-18 17:37:48 +00:00
|
|
|
p = ksba_cert_get_public_key (cert);
|
|
|
|
n = gcry_sexp_canon_len (p, 0, NULL, NULL);
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
log_error ("libksba did not return a proper S-Exp\n");
|
2002-08-09 18:12:22 +00:00
|
|
|
ksba_free (p);
|
|
|
|
gcry_sexp_release (s_sig);
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_BUG);
|
2001-12-18 17:37:48 +00:00
|
|
|
}
|
2002-03-05 15:56:46 +00:00
|
|
|
if (DBG_X509)
|
|
|
|
log_printhex ("public key: ", p, n);
|
|
|
|
|
2001-12-18 17:37:48 +00:00
|
|
|
rc = gcry_sexp_sscan ( &s_pkey, NULL, p, n);
|
2002-08-09 18:12:22 +00:00
|
|
|
ksba_free (p);
|
2001-11-13 12:50:14 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc));
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_sexp_release (s_sig);
|
2003-06-27 20:53:09 +00:00
|
|
|
return rc;
|
2001-11-13 12:50:14 +00:00
|
|
|
}
|
2001-11-24 14:26:27 +00:00
|
|
|
|
2001-11-16 17:56:23 +00:00
|
|
|
|
|
|
|
rc = do_encode_md (md, algo, gcry_pk_get_nbits (s_pkey), &frame);
|
|
|
|
if (rc)
|
|
|
|
{
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
gcry_sexp_release (s_pkey);
|
2001-11-16 17:56:23 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/* put hash into the S-Exp s_hash */
|
|
|
|
if ( gcry_sexp_build (&s_hash, NULL, "%m", frame) )
|
|
|
|
BUG ();
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_mpi_release (frame);
|
|
|
|
|
2001-11-13 12:50:14 +00:00
|
|
|
rc = gcry_pk_verify (s_sig, s_hash, s_pkey);
|
|
|
|
if (DBG_CRYPTO)
|
2003-06-27 20:53:09 +00:00
|
|
|
log_debug ("gcry_pk_verify: %s\n", gpg_strerror (rc));
|
2002-08-09 18:12:22 +00:00
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
gcry_sexp_release (s_hash);
|
2003-01-09 12:59:25 +00:00
|
|
|
gcry_sexp_release (s_pkey);
|
2003-06-27 20:53:09 +00:00
|
|
|
return rc;
|
2001-11-13 12:50:14 +00:00
|
|
|
}
|
|
|
|
|
2001-11-24 14:26:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2003-06-27 20:53:09 +00:00
|
|
|
gpgsm_create_cms_signature (KsbaCert cert, gcry_md_hd_t md, int mdalgo,
|
2001-11-24 14:26:27 +00:00
|
|
|
char **r_sigval)
|
|
|
|
{
|
2001-11-25 18:23:06 +00:00
|
|
|
int rc;
|
|
|
|
char *grip;
|
|
|
|
size_t siglen;
|
|
|
|
|
|
|
|
grip = gpgsm_get_keygrip_hexstring (cert);
|
|
|
|
if (!grip)
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_BAD_CERT);
|
2001-11-25 18:23:06 +00:00
|
|
|
|
|
|
|
rc = gpgsm_agent_pksign (grip, gcry_md_read(md, mdalgo),
|
|
|
|
gcry_md_get_algo_dlen (mdalgo), mdalgo,
|
|
|
|
r_sigval, &siglen);
|
|
|
|
xfree (grip);
|
|
|
|
return rc;
|
2001-11-24 14:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-25 18:18:40 +00:00
|
|
|
|