2003-08-05 19:11:04 +02:00
|
|
|
/* certcheck.c - check one certificate
|
2020-04-09 12:18:08 +02:00
|
|
|
* Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
|
|
|
|
* Copyright (C) 2001-2019 Werner Koch
|
|
|
|
* Copyright (C) 2015-2020 g10 Code GmbH
|
2003-08-05 19:11:04 +02: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 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 19:11:04 +02: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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2020-04-09 12:18:08 +02:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2003-08-05 19:11:04 +02: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 19:11:04 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "gpgsm.h"
|
|
|
|
#include <gcrypt.h>
|
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
#include "keydb.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/i18n.h"
|
2020-05-13 21:21:24 +02:00
|
|
|
#include "../common/membuf.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2007-05-07 21:49:12 +02:00
|
|
|
|
2007-04-20 12:54:46 +02:00
|
|
|
/* Return the number of bits of the Q parameter from the DSA key
|
|
|
|
KEY. */
|
|
|
|
static unsigned int
|
|
|
|
get_dsa_qbits (gcry_sexp_t key)
|
|
|
|
{
|
|
|
|
gcry_sexp_t l1, l2;
|
|
|
|
gcry_mpi_t q;
|
|
|
|
unsigned int nbits;
|
|
|
|
|
|
|
|
l1 = gcry_sexp_find_token (key, "public-key", 0);
|
|
|
|
if (!l1)
|
|
|
|
return 0; /* Does not contain a key object. */
|
|
|
|
l2 = gcry_sexp_cadr (l1);
|
|
|
|
gcry_sexp_release (l1);
|
|
|
|
l1 = gcry_sexp_find_token (l2, "q", 1);
|
|
|
|
gcry_sexp_release (l2);
|
|
|
|
if (!l1)
|
|
|
|
return 0; /* Invalid object. */
|
|
|
|
q = gcry_sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG);
|
|
|
|
gcry_sexp_release (l1);
|
|
|
|
if (!q)
|
|
|
|
return 0; /* Missing value. */
|
|
|
|
nbits = gcry_mpi_get_nbits (q);
|
|
|
|
gcry_mpi_release (q);
|
|
|
|
|
|
|
|
return nbits;
|
|
|
|
}
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
static int
|
2004-08-17 17:26:22 +02:00
|
|
|
do_encode_md (gcry_md_hd_t md, int algo, int pkalgo, unsigned int nbits,
|
2007-04-20 12:54:46 +02:00
|
|
|
gcry_sexp_t pkey, gcry_mpi_t *r_val)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2005-06-16 10:12:03 +02:00
|
|
|
int n;
|
|
|
|
size_t nframe;
|
2004-08-17 17:26:22 +02:00
|
|
|
unsigned char *frame;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2020-05-08 14:14:01 +02:00
|
|
|
if (pkalgo == GCRY_PK_DSA || pkalgo == GCRY_PK_ECC)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2020-06-17 14:27:12 +02:00
|
|
|
unsigned int qbits0, qbits;
|
2007-04-20 12:54:46 +02:00
|
|
|
|
2020-05-08 14:14:01 +02:00
|
|
|
if ( pkalgo == GCRY_PK_ECC )
|
2020-06-17 14:27:12 +02:00
|
|
|
{
|
|
|
|
qbits0 = gcry_pk_get_nbits (pkey);
|
2020-06-25 08:57:33 +02:00
|
|
|
qbits = qbits0 == 521? 512 : qbits0;
|
2020-06-17 14:27:12 +02:00
|
|
|
}
|
2007-04-20 12:54:46 +02:00
|
|
|
else
|
2020-06-17 14:27:12 +02:00
|
|
|
qbits0 = qbits = get_dsa_qbits (pkey);
|
2007-04-20 12:54:46 +02:00
|
|
|
|
|
|
|
if ( (qbits%8) )
|
|
|
|
{
|
|
|
|
log_error(_("DSA requires the hash length to be a"
|
|
|
|
" multiple of 8 bits\n"));
|
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't allow any Q smaller than 160 bits. We don't want
|
|
|
|
someone to issue signatures from a key with a 16-bit Q or
|
|
|
|
something like that, which would look correct but allow
|
|
|
|
trivial forgeries. Yes, I know this rules out using MD5 with
|
|
|
|
DSA. ;) */
|
|
|
|
if (qbits < 160)
|
|
|
|
{
|
|
|
|
log_error (_("%s key uses an unsafe (%u bit) hash\n"),
|
2020-06-17 14:27:12 +02:00
|
|
|
gcry_pk_algo_name (pkalgo), qbits0);
|
2007-04-20 12:54:46 +02:00
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we're too short. Too long is safe as we'll
|
|
|
|
automatically left-truncate. */
|
2004-08-17 17:26:22 +02:00
|
|
|
nframe = gcry_md_get_algo_dlen (algo);
|
2007-04-20 12:54:46 +02:00
|
|
|
if (nframe < qbits/8)
|
2004-08-17 17:26:22 +02:00
|
|
|
{
|
2007-04-20 12:54:46 +02:00
|
|
|
log_error (_("a %u bit hash is not valid for a %u bit %s key\n"),
|
|
|
|
(unsigned int)nframe*8,
|
2020-06-17 14:27:12 +02:00
|
|
|
qbits0,
|
2007-04-20 12:54:46 +02:00
|
|
|
gcry_pk_algo_name (pkalgo));
|
|
|
|
/* FIXME: we need to check the requirements for ECDSA. */
|
|
|
|
if (nframe < 20 || pkalgo == GCRY_PK_DSA )
|
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
2004-08-17 17:26:22 +02:00
|
|
|
}
|
2007-04-20 12:54:46 +02:00
|
|
|
|
2004-08-17 17:26:22 +02:00
|
|
|
frame = xtrymalloc (nframe);
|
|
|
|
if (!frame)
|
2006-09-06 18:35:52 +02:00
|
|
|
return out_of_core ();
|
2004-08-17 17:26:22 +02:00
|
|
|
memcpy (frame, gcry_md_read (md, algo), nframe);
|
|
|
|
n = nframe;
|
2007-04-20 12:54:46 +02:00
|
|
|
/* Truncate. */
|
|
|
|
if (n > qbits/8)
|
|
|
|
n = qbits/8;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2004-08-17 17:26:22 +02:00
|
|
|
else
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2004-08-17 17:26:22 +02:00
|
|
|
int i;
|
|
|
|
unsigned char asn[100];
|
|
|
|
size_t asnlen;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
nframe = (nbits+7) / 8;
|
|
|
|
|
|
|
|
asnlen = DIM(asn);
|
2006-10-05 13:06:42 +02:00
|
|
|
if (!algo || gcry_md_test_algo (algo))
|
|
|
|
return gpg_error (GPG_ERR_DIGEST_ALGO);
|
2004-08-17 17:26:22 +02:00
|
|
|
if (gcry_md_algo_info (algo, GCRYCTL_GET_ASNOID, asn, &asnlen))
|
|
|
|
{
|
|
|
|
log_error ("no object identifier for algo %d\n", algo);
|
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-08-17 17:26:22 +02:00
|
|
|
len = gcry_md_get_algo_dlen (algo);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-08-17 17:26:22 +02: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);
|
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-08-17 17:26:22 +02: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)
|
2006-09-06 18:35:52 +02:00
|
|
|
return out_of_core ();
|
2004-08-17 17:26:22 +02:00
|
|
|
n = 0;
|
|
|
|
frame[n++] = 0;
|
|
|
|
frame[n++] = 1; /* block type */
|
|
|
|
i = nframe - len - asnlen -3 ;
|
2020-07-08 14:40:34 +02:00
|
|
|
log_assert ( i > 1 );
|
2004-08-17 17:26:22 +02:00
|
|
|
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;
|
2020-07-08 14:40:34 +02:00
|
|
|
log_assert ( n == nframe );
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2005-03-17 20:10:37 +01:00
|
|
|
if (DBG_CRYPTO)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
log_debug ("encoded hash:");
|
|
|
|
for (j=0; j < nframe; j++)
|
|
|
|
log_printf (" %02X", frame[j]);
|
|
|
|
log_printf ("\n");
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
gcry_mpi_scan (r_val, GCRYMPI_FMT_USG, frame, n, &nframe);
|
|
|
|
xfree (frame);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-17 17:26:22 +02:00
|
|
|
/* Return the public key algorithm id from the S-expression PKEY.
|
|
|
|
FIXME: libgcrypt should provide such a function. Note that this
|
|
|
|
implementation uses the names as used by libksba. */
|
|
|
|
static int
|
|
|
|
pk_algo_from_sexp (gcry_sexp_t pkey)
|
|
|
|
{
|
|
|
|
gcry_sexp_t l1, l2;
|
|
|
|
const char *name;
|
|
|
|
size_t n;
|
|
|
|
int algo;
|
|
|
|
|
|
|
|
l1 = gcry_sexp_find_token (pkey, "public-key", 0);
|
|
|
|
if (!l1)
|
|
|
|
return 0; /* Not found. */
|
|
|
|
l2 = gcry_sexp_cadr (l1);
|
|
|
|
gcry_sexp_release (l1);
|
|
|
|
|
|
|
|
name = gcry_sexp_nth_data (l2, 0, &n);
|
|
|
|
if (!name)
|
|
|
|
algo = 0; /* Not found. */
|
|
|
|
else if (n==3 && !memcmp (name, "rsa", 3))
|
|
|
|
algo = GCRY_PK_RSA;
|
|
|
|
else if (n==3 && !memcmp (name, "dsa", 3))
|
|
|
|
algo = GCRY_PK_DSA;
|
2007-04-18 15:03:35 +02:00
|
|
|
else if (n==3 && !memcmp (name, "ecc", 3))
|
2020-05-08 14:14:01 +02:00
|
|
|
algo = GCRY_PK_ECC;
|
2004-08-17 17:26:22 +02:00
|
|
|
else if (n==13 && !memcmp (name, "ambiguous-rsa", 13))
|
|
|
|
algo = GCRY_PK_RSA;
|
|
|
|
else
|
|
|
|
algo = 0;
|
|
|
|
gcry_sexp_release (l2);
|
|
|
|
return algo;
|
|
|
|
}
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2020-04-09 12:18:08 +02:00
|
|
|
/* Return the hash algorithm's algo id from its name given in the
|
|
|
|
* non-null termnated string in (buffer,buflen). Returns 0 on failure
|
|
|
|
* or if the algo is not known. */
|
|
|
|
static int
|
|
|
|
hash_algo_from_buffer (const void *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
int algo;
|
|
|
|
|
|
|
|
string = xtrymalloc (buflen + 1);
|
|
|
|
if (!string)
|
|
|
|
{
|
|
|
|
log_error (_("out of core\n"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy (string, buffer, buflen);
|
|
|
|
string[buflen] = 0;
|
|
|
|
algo = gcry_md_map_name (string);
|
|
|
|
if (!algo)
|
|
|
|
log_error ("unknown digest algorithm '%s' used in certificate\n", string);
|
|
|
|
xfree (string);
|
|
|
|
return algo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return an unsigned integer from the non-null termnated string
|
|
|
|
* (buffer,buflen). Returns 0 on failure. */
|
|
|
|
static unsigned int
|
|
|
|
uint_from_buffer (const void *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
string = xtrymalloc (buflen + 1);
|
|
|
|
if (!string)
|
|
|
|
{
|
|
|
|
log_error (_("out of core\n"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy (string, buffer, buflen);
|
|
|
|
string[buflen] = 0;
|
|
|
|
val = strtoul (string, NULL, 10);
|
|
|
|
xfree (string);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-14 15:46:04 +02:00
|
|
|
/* Extract the hash algorithm and the salt length from the sigval. */
|
|
|
|
static gpg_error_t
|
|
|
|
extract_pss_params (gcry_sexp_t s_sig, int *r_algo, unsigned int *r_saltlen)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
gcry_buffer_t ioarray[2] = { {0}, {0} };
|
|
|
|
|
|
|
|
err = gcry_sexp_extract_param (s_sig, "sig-val",
|
|
|
|
"&'hash-algo''salt-length'",
|
|
|
|
ioarray+0, ioarray+1, NULL);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error ("extracting params from PSS failed: %s\n", gpg_strerror (err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*r_algo = hash_algo_from_buffer (ioarray[0].data, ioarray[0].len);
|
|
|
|
*r_saltlen = uint_from_buffer (ioarray[1].data, ioarray[1].len);
|
|
|
|
xfree (ioarray[0].data);
|
|
|
|
xfree (ioarray[1].data);
|
|
|
|
if (*r_saltlen < 20)
|
|
|
|
{
|
|
|
|
log_error ("length of PSS salt too short\n");
|
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
return gpg_error (GPG_ERR_DIGEST_ALGO);
|
|
|
|
}
|
|
|
|
if (!*r_algo)
|
|
|
|
{
|
|
|
|
return gpg_error (GPG_ERR_DIGEST_ALGO);
|
|
|
|
}
|
2020-04-15 11:05:41 +02:00
|
|
|
|
|
|
|
/* PSS has no hash function firewall like PKCS#1 and thus offers
|
|
|
|
* a path for hash algorithm replacement. To avoid this it makes
|
|
|
|
* sense to restrict the allowed hash algorithms and also allow only
|
|
|
|
* matching salt lengths. According to Peter Gutmann:
|
|
|
|
* "Beware of bugs in the above signature scheme;
|
|
|
|
* I have only proved it secure, not implemented it"
|
|
|
|
* - Apologies to Donald Knuth.
|
|
|
|
* https://www.metzdowd.com/pipermail/cryptography/2019-November/035449.html
|
|
|
|
*
|
|
|
|
* Given the set of supported algorithms currently available in
|
|
|
|
* Libgcrypt and the extra hash checks we have in some compliance
|
|
|
|
* modes, it would be hard to trick gpgsm to verify a forged
|
|
|
|
* signature. However, if eventually someone adds the xor256 hash
|
|
|
|
* algorithm (1.3.6.1.4.1.3029.3.2) to Libgcrypt we would be doomed.
|
|
|
|
*/
|
|
|
|
switch (*r_algo)
|
|
|
|
{
|
|
|
|
case GCRY_MD_SHA1:
|
|
|
|
case GCRY_MD_SHA256:
|
|
|
|
case GCRY_MD_SHA384:
|
|
|
|
case GCRY_MD_SHA512:
|
|
|
|
case GCRY_MD_SHA3_256:
|
|
|
|
case GCRY_MD_SHA3_384:
|
|
|
|
case GCRY_MD_SHA3_512:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_error ("PSS hash algorithm '%s' rejected\n",
|
|
|
|
gcry_md_algo_name (*r_algo));
|
|
|
|
return gpg_error (GPG_ERR_DIGEST_ALGO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gcry_md_get_algo_dlen (*r_algo) != *r_saltlen)
|
|
|
|
{
|
|
|
|
log_error ("PSS hash algorithm '%s' rejected due to salt length %u\n",
|
|
|
|
gcry_md_algo_name (*r_algo), *r_saltlen);
|
|
|
|
return gpg_error (GPG_ERR_DIGEST_ALGO);
|
|
|
|
}
|
|
|
|
|
2020-04-14 15:46:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-20 12:54:46 +02:00
|
|
|
/* 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. */
|
2003-08-05 19:11:04 +02:00
|
|
|
int
|
2003-12-17 13:28:24 +01:00
|
|
|
gpgsm_check_cert_sig (ksba_cert_t issuer_cert, ksba_cert_t cert)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
const char *algoid;
|
2020-05-13 21:21:24 +02:00
|
|
|
gcry_md_hd_t md = NULL;
|
|
|
|
void *certder = NULL;
|
|
|
|
size_t certderlen;
|
2003-08-05 19:11:04 +02:00
|
|
|
int rc, algo;
|
2003-12-17 13:28:24 +01:00
|
|
|
ksba_sexp_t p;
|
2003-08-05 19:11:04 +02:00
|
|
|
size_t n;
|
2020-04-09 12:18:08 +02:00
|
|
|
gcry_sexp_t s_sig, s_data, s_pkey;
|
|
|
|
int use_pss = 0;
|
2020-05-13 21:21:24 +02:00
|
|
|
int use_eddsa = 0;
|
2020-04-09 12:18:08 +02:00
|
|
|
unsigned int saltlen;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2020-05-18 19:32:30 +02:00
|
|
|
/* Note that we map the 4 algos which current Libgcrypt versions are
|
|
|
|
* not aware of the OID. */
|
2003-08-05 19:11:04 +02:00
|
|
|
algo = gcry_md_map_name ( (algoid=ksba_cert_get_digest_algo (cert)));
|
2020-04-09 12:18:08 +02:00
|
|
|
if (!algo && algoid && !strcmp (algoid, "1.2.840.113549.1.1.10"))
|
|
|
|
use_pss = 1;
|
2020-05-13 21:21:24 +02:00
|
|
|
else if (algoid && !strcmp (algoid, "1.3.101.112"))
|
|
|
|
use_eddsa = 1;
|
|
|
|
else if (algoid && !strcmp (algoid, "1.3.101.113"))
|
|
|
|
use_eddsa = 2;
|
2020-05-18 19:32:30 +02:00
|
|
|
else if (!algo && algoid && !strcmp (algoid, "1.2.840.10045.4.3.1"))
|
|
|
|
algo = GCRY_MD_SHA224; /* ecdsa-with-sha224 */
|
|
|
|
else if (!algo && algoid && !strcmp (algoid, "1.2.840.10045.4.3.2"))
|
|
|
|
algo = GCRY_MD_SHA256; /* ecdsa-with-sha256 */
|
|
|
|
else if (!algo && algoid && !strcmp (algoid, "1.2.840.10045.4.3.3"))
|
|
|
|
algo = GCRY_MD_SHA384; /* ecdsa-with-sha384 */
|
|
|
|
else if (!algo && algoid && !strcmp (algoid, "1.2.840.10045.4.3.4"))
|
|
|
|
algo = GCRY_MD_SHA512; /* ecdsa-with-sha512 */
|
2020-04-09 12:18:08 +02:00
|
|
|
else if (!algo)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2020-05-13 21:21:24 +02:00
|
|
|
log_error ("unknown digest algorithm '%s' used in certificate\n",
|
2020-04-09 12:18:08 +02:00
|
|
|
algoid? algoid:"?");
|
2005-11-13 20:07:06 +01:00
|
|
|
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"));
|
2003-08-05 19:11:04 +02:00
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:18:08 +02:00
|
|
|
/* The the signature from the certificate. */
|
2003-08-05 19:11:04 +02: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");
|
|
|
|
ksba_free (p);
|
|
|
|
return gpg_error (GPG_ERR_BUG);
|
|
|
|
}
|
2020-04-09 12:18:08 +02:00
|
|
|
rc = gcry_sexp_sscan ( &s_sig, NULL, (char*)p, n);
|
|
|
|
ksba_free (p);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc));
|
|
|
|
return rc;
|
|
|
|
}
|
2005-03-17 20:10:37 +01:00
|
|
|
if (DBG_CRYPTO)
|
2020-04-09 12:18:08 +02:00
|
|
|
gcry_log_debugsxp ("sigval", s_sig);
|
|
|
|
|
|
|
|
if (use_pss)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2020-04-14 15:46:04 +02:00
|
|
|
rc = extract_pss_params (s_sig, &algo, &saltlen);
|
2020-04-09 12:18:08 +02:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
return rc;
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 12:18:08 +02:00
|
|
|
|
2020-05-13 21:21:24 +02:00
|
|
|
/* Hash the to-be-signed parts of the certificate or but them into a
|
|
|
|
* buffer for the EdDSA algorithms. */
|
|
|
|
if (use_eddsa)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2020-05-13 21:21:24 +02:00
|
|
|
membuf_t mb;
|
2020-04-09 12:18:08 +02:00
|
|
|
|
2020-05-13 21:21:24 +02:00
|
|
|
init_membuf (&mb, 2048);
|
|
|
|
rc = ksba_cert_hash (cert, 1,
|
|
|
|
(void (*)(void *, const void*,size_t))put_membuf,
|
|
|
|
&mb);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("ksba_cert_hash failed: %s\n", gpg_strerror (rc));
|
|
|
|
xfree (get_membuf (&mb, NULL));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
certder = get_membuf (&mb, &certderlen);
|
|
|
|
if (!certder)
|
|
|
|
{
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
log_error ("getting tbsCertificate failed: %s\n", gpg_strerror (rc));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-04-09 12:18:08 +02:00
|
|
|
{
|
2020-05-13 21:21:24 +02:00
|
|
|
rc = gcry_md_open (&md, algo, 0);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("md_open failed: %s\n", gpg_strerror (rc));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if (DBG_HASHING)
|
|
|
|
gcry_md_debug (md, "hash.cert");
|
|
|
|
|
|
|
|
rc = ksba_cert_hash (cert, 1, HASH_FNC, md);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("ksba_cert_hash failed: %s\n", gpg_strerror (rc));
|
|
|
|
gcry_md_close (md);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
gcry_md_final (md);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 12:18:08 +02:00
|
|
|
/* Get the public key from the certificate. */
|
2003-08-05 19:11:04 +02:00
|
|
|
p = ksba_cert_get_public_key (issuer_cert);
|
|
|
|
n = gcry_sexp_canon_len (p, 0, NULL, NULL);
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
log_error ("libksba did not return a proper S-Exp\n");
|
|
|
|
gcry_md_close (md);
|
|
|
|
ksba_free (p);
|
|
|
|
gcry_sexp_release (s_sig);
|
2020-05-13 21:21:24 +02:00
|
|
|
xfree (certder);
|
2003-08-05 19:11:04 +02:00
|
|
|
return gpg_error (GPG_ERR_BUG);
|
|
|
|
}
|
2005-06-16 10:12:03 +02:00
|
|
|
rc = gcry_sexp_sscan ( &s_pkey, NULL, (char*)p, n);
|
2003-08-05 19:11:04 +02:00
|
|
|
ksba_free (p);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc));
|
|
|
|
gcry_md_close (md);
|
|
|
|
gcry_sexp_release (s_sig);
|
2020-05-13 21:21:24 +02:00
|
|
|
xfree (certder);
|
2003-08-05 19:11:04 +02:00
|
|
|
return rc;
|
|
|
|
}
|
2020-04-09 12:18:08 +02:00
|
|
|
if (DBG_CRYPTO)
|
|
|
|
gcry_log_debugsxp ("pubkey:", s_pkey);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2020-04-09 12:18:08 +02:00
|
|
|
if (use_pss)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2020-04-09 12:18:08 +02:00
|
|
|
rc = gcry_sexp_build (&s_data, NULL,
|
|
|
|
"(data (flags pss)"
|
|
|
|
"(hash %s %b)"
|
|
|
|
"(salt-length %u))",
|
|
|
|
hash_algo_to_string (algo),
|
|
|
|
(int)gcry_md_get_algo_dlen (algo),
|
|
|
|
gcry_md_read (md, algo),
|
|
|
|
saltlen);
|
|
|
|
if (rc)
|
|
|
|
BUG ();
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2020-05-13 21:21:24 +02:00
|
|
|
else if (use_eddsa)
|
|
|
|
{
|
|
|
|
rc = gcry_sexp_build (&s_data, NULL,
|
|
|
|
"(data(flags eddsa)(hash-algo %s)(value %b))",
|
|
|
|
use_eddsa == 1? "sha512":"shake256",
|
|
|
|
(int)certderlen, certder);
|
|
|
|
xfree (certder);
|
|
|
|
certder = NULL;
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("building data for eddsa failed: %s\n", gpg_strerror (rc));
|
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 12:18:08 +02:00
|
|
|
else
|
|
|
|
{
|
2020-04-14 15:46:04 +02:00
|
|
|
/* RSA or DSA: Prepare the hash for verification. */
|
2020-04-09 12:18:08 +02:00
|
|
|
gcry_mpi_t frame;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2020-04-09 12:18:08 +02:00
|
|
|
rc = do_encode_md (md, algo, pk_algo_from_sexp (s_pkey),
|
|
|
|
gcry_pk_get_nbits (s_pkey), s_pkey, &frame);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
gcry_md_close (md);
|
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if ( gcry_sexp_build (&s_data, NULL, "%m", frame) )
|
|
|
|
BUG ();
|
|
|
|
gcry_mpi_release (frame);
|
|
|
|
}
|
|
|
|
if (DBG_CRYPTO)
|
|
|
|
gcry_log_debugsxp ("data:", s_data);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2020-04-09 12:18:08 +02:00
|
|
|
/* Verify. */
|
|
|
|
rc = gcry_pk_verify (s_sig, s_data, s_pkey);
|
2005-03-17 20:10:37 +01:00
|
|
|
if (DBG_X509)
|
2020-05-13 21:21:24 +02:00
|
|
|
log_debug ("gcry_pk_verify: %s\n", gpg_strerror (rc));
|
|
|
|
if (use_eddsa && (gpg_err_code (rc) == GPG_ERR_INTERNAL
|
|
|
|
|| gpg_err_code (rc) == GPG_ERR_INV_CURVE))
|
|
|
|
{
|
|
|
|
/* Let's assume that this is a certificate for an ECDH key
|
|
|
|
* signed using EdDSA. This won't work. We should have located
|
|
|
|
* the public key using subjectKeyIdentifier (SKI) to not run
|
|
|
|
* into this problem. However, we don't do this for self-signed
|
|
|
|
* certificates and we don't have a way to search for arbitrary
|
|
|
|
* keys based on the SKI. Note: The sample certificate from
|
|
|
|
* RFC-8410 uses a SHA-1 hash of the public key for the SKI; so
|
|
|
|
* we are not able to verify it.
|
|
|
|
*/
|
|
|
|
ksba_sexp_t ski;
|
|
|
|
const unsigned char *skider;
|
|
|
|
size_t skiderlen;
|
|
|
|
|
|
|
|
if (DBG_X509)
|
|
|
|
log_debug ("retrying using the ski\n");
|
|
|
|
if (!ksba_cert_get_subj_key_id (issuer_cert, NULL, &ski))
|
|
|
|
{
|
|
|
|
skider = gpgsm_get_serial (ski, &skiderlen);
|
|
|
|
if (!skider)
|
|
|
|
;
|
|
|
|
else if (skiderlen == (use_eddsa==1? 32:57))
|
|
|
|
{
|
|
|
|
/* Here we assume that the SKI is actually the public key. */
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
rc = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
"(public-key(ecc(curve%s)(q%b)))",
|
|
|
|
use_eddsa==1? "1.3.101.112":"1.3.101.113",
|
|
|
|
(int)skiderlen, skider);
|
|
|
|
if (rc)
|
|
|
|
log_error ("building pubkey from SKI failed: %s\n",
|
|
|
|
gpg_strerror (rc));
|
|
|
|
else
|
|
|
|
rc = gcry_pk_verify (s_sig, s_data, s_pkey);
|
|
|
|
if (DBG_X509)
|
|
|
|
log_debug ("gcry_pk_verify: %s\n", gpg_strerror (rc));
|
|
|
|
}
|
|
|
|
else if (skiderlen == 20)
|
|
|
|
{
|
|
|
|
log_printhex (skider, skiderlen, "ski might be the SHA-1:");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (DBG_X509)
|
|
|
|
log_debug(skider, skiderlen, "ski is:");
|
|
|
|
}
|
|
|
|
ksba_free (ski);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
gcry_md_close (md);
|
|
|
|
gcry_sexp_release (s_sig);
|
2020-04-09 12:18:08 +02:00
|
|
|
gcry_sexp_release (s_data);
|
2003-08-05 19:11:04 +02:00
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2020-07-03 15:47:55 +02:00
|
|
|
gpgsm_check_cms_signature (ksba_cert_t cert, gcry_sexp_t s_sig,
|
|
|
|
gcry_md_hd_t md, int mdalgo,
|
|
|
|
unsigned int pkalgoflags, int *r_pkalgo)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int rc;
|
2003-12-17 13:28:24 +01:00
|
|
|
ksba_sexp_t p;
|
2020-07-03 15:47:55 +02:00
|
|
|
gcry_sexp_t s_hash, s_pkey;
|
2003-08-05 19:11:04 +02:00
|
|
|
size_t n;
|
2007-08-10 18:52:05 +02:00
|
|
|
int pkalgo;
|
2020-04-14 15:46:04 +02:00
|
|
|
int use_pss;
|
|
|
|
unsigned int saltlen = 0;
|
|
|
|
|
2007-08-10 18:52:05 +02:00
|
|
|
if (r_pkalgo)
|
|
|
|
*r_pkalgo = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2020-07-03 15:47:55 +02:00
|
|
|
/* Check whether rsaPSS is needed. This information is indicated in
|
|
|
|
* the SIG-VAL and already provided to us by the caller so that we
|
|
|
|
* do not need to parse this out. */
|
|
|
|
use_pss = !!(pkalgoflags & PK_ALGO_FLAG_RSAPSS);
|
|
|
|
if (use_pss)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2020-07-03 15:47:55 +02:00
|
|
|
int algo;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2020-07-03 15:47:55 +02:00
|
|
|
rc = extract_pss_params (s_sig, &algo, &saltlen);
|
|
|
|
if (rc)
|
2020-04-14 15:46:04 +02:00
|
|
|
{
|
2020-07-03 15:47:55 +02:00
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if (algo != mdalgo)
|
|
|
|
{
|
|
|
|
log_error ("PSS hash algo mismatch (%d/%d)\n", mdalgo, algo);
|
|
|
|
gcry_sexp_release (s_sig);
|
|
|
|
return gpg_error (GPG_ERR_DIGEST_ALGO);
|
2020-04-14 15:46:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-05 19:11:04 +02: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");
|
|
|
|
ksba_free (p);
|
|
|
|
return gpg_error (GPG_ERR_BUG);
|
|
|
|
}
|
2005-03-17 20:10:37 +01:00
|
|
|
if (DBG_CRYPTO)
|
2017-11-27 15:00:25 +01:00
|
|
|
log_printhex (p, n, "public key: ");
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2005-06-16 10:12:03 +02:00
|
|
|
rc = gcry_sexp_sscan ( &s_pkey, NULL, (char*)p, n);
|
2003-08-05 19:11:04 +02:00
|
|
|
ksba_free (p);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-08-10 18:52:05 +02:00
|
|
|
pkalgo = pk_algo_from_sexp (s_pkey);
|
|
|
|
if (r_pkalgo)
|
|
|
|
*r_pkalgo = pkalgo;
|
2020-04-14 15:46:04 +02:00
|
|
|
|
|
|
|
if (use_pss)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2020-04-14 15:46:04 +02:00
|
|
|
rc = gcry_sexp_build (&s_hash, NULL,
|
|
|
|
"(data (flags pss)"
|
|
|
|
"(hash %s %b)"
|
|
|
|
"(salt-length %u))",
|
|
|
|
hash_algo_to_string (mdalgo),
|
|
|
|
(int)gcry_md_get_algo_dlen (mdalgo),
|
|
|
|
gcry_md_read (md, mdalgo),
|
|
|
|
saltlen);
|
|
|
|
if (rc)
|
|
|
|
BUG ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* RSA or DSA: Prepare the hash for verification. */
|
|
|
|
gcry_mpi_t frame;
|
|
|
|
|
|
|
|
rc = do_encode_md (md, mdalgo, pkalgo,
|
|
|
|
gcry_pk_get_nbits (s_pkey), s_pkey, &frame);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/* put hash into the S-Exp s_hash */
|
|
|
|
if ( gcry_sexp_build (&s_hash, NULL, "%m", frame) )
|
|
|
|
BUG ();
|
|
|
|
gcry_mpi_release (frame);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
rc = gcry_pk_verify (s_sig, s_hash, s_pkey);
|
2005-03-17 20:10:37 +01:00
|
|
|
if (DBG_X509)
|
2020-04-14 15:46:04 +02:00
|
|
|
log_debug ("gcry_pk_verify: %s\n", gpg_strerror (rc));
|
2003-08-05 19:11:04 +02:00
|
|
|
gcry_sexp_release (s_hash);
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2004-04-26 15:29:09 +02:00
|
|
|
gpgsm_create_cms_signature (ctrl_t ctrl, ksba_cert_t cert,
|
2005-06-16 10:12:03 +02:00
|
|
|
gcry_md_hd_t md, int mdalgo,
|
|
|
|
unsigned char **r_sigval)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int rc;
|
2004-02-13 18:06:50 +01:00
|
|
|
char *grip, *desc;
|
2003-08-05 19:11:04 +02:00
|
|
|
size_t siglen;
|
|
|
|
|
|
|
|
grip = gpgsm_get_keygrip_hexstring (cert);
|
|
|
|
if (!grip)
|
|
|
|
return gpg_error (GPG_ERR_BAD_CERT);
|
|
|
|
|
2004-02-13 18:06:50 +01:00
|
|
|
desc = gpgsm_format_keydesc (cert);
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
rc = gpgsm_agent_pksign (ctrl, grip, desc, gcry_md_read(md, mdalgo),
|
2003-08-05 19:11:04 +02:00
|
|
|
gcry_md_get_algo_dlen (mdalgo), mdalgo,
|
|
|
|
r_sigval, &siglen);
|
2004-02-13 18:06:50 +01:00
|
|
|
xfree (desc);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (grip);
|
|
|
|
return rc;
|
|
|
|
}
|