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>
37
/* Remove this if libgcrypt 1.3.0 is required. */
38
#define MY_GCRY_PK_ECDSA 301
41
/* Return the number of bits of the Q parameter from the DSA key
44
get_dsa_qbits (gcry_sexp_t key)
50
l1 = gcry_sexp_find_token (key, "public-key", 0);
52
return 0; /* Does not contain a key object. */
53
l2 = gcry_sexp_cadr (l1);
54
gcry_sexp_release (l1);
55
l1 = gcry_sexp_find_token (l2, "q", 1);
56
gcry_sexp_release (l2);
58
return 0; /* Invalid object. */
59
q = gcry_sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG);
60
gcry_sexp_release (l1);
62
return 0; /* Missing value. */
63
nbits = gcry_mpi_get_nbits (q);
39
71
do_encode_md (gcry_md_hd_t md, int algo, int pkalgo, unsigned int nbits,
72
gcry_sexp_t pkey, gcry_mpi_t *r_val)
44
76
unsigned char *frame;
46
if (pkalgo == GCRY_PK_DSA)
78
if (pkalgo == GCRY_PK_DSA || pkalgo == MY_GCRY_PK_ECDSA)
82
if ( pkalgo == MY_GCRY_PK_ECDSA )
83
qbits = gcry_pk_get_nbits (pkey);
85
qbits = get_dsa_qbits (pkey);
89
log_error(_("DSA requires the hash length to be a"
90
" multiple of 8 bits\n"));
91
return gpg_error (GPG_ERR_INTERNAL);
94
/* Don't allow any Q smaller than 160 bits. We don't want
95
someone to issue signatures from a key with a 16-bit Q or
96
something like that, which would look correct but allow
97
trivial forgeries. Yes, I know this rules out using MD5 with
101
log_error (_("%s key uses an unsafe (%u bit) hash\n"),
102
gcry_pk_algo_name (pkalgo), qbits);
103
return gpg_error (GPG_ERR_INTERNAL);
106
/* Check if we're too short. Too long is safe as we'll
107
automatically left-truncate. */
48
108
nframe = gcry_md_get_algo_dlen (algo);
109
if (nframe < qbits/8)
51
log_error (_("DSA requires the use of a 160 bit hash algorithm\n"));
52
return gpg_error (GPG_ERR_INTERNAL);
111
log_error (_("a %u bit hash is not valid for a %u bit %s key\n"),
112
(unsigned int)nframe*8,
113
gcry_pk_get_nbits (pkey),
114
gcry_pk_algo_name (pkalgo));
115
/* FIXME: we need to check the requirements for ECDSA. */
116
if (nframe < 20 || pkalgo == GCRY_PK_DSA )
117
return gpg_error (GPG_ERR_INTERNAL);
54
120
frame = xtrymalloc (nframe);
56
return OUT_OF_CORE (errno);
122
return out_of_core ();
57
123
memcpy (frame, gcry_md_read (md, algo), nframe);
140
211
algo = GCRY_PK_RSA;
141
212
else if (n==3 && !memcmp (name, "dsa", 3))
142
213
algo = GCRY_PK_DSA;
214
/* Because this function is called only for verification we can
215
assume that ECC actually means ECDSA. */
216
else if (n==3 && !memcmp (name, "ecc", 3))
217
algo = MY_GCRY_PK_ECDSA;
143
218
else if (n==13 && !memcmp (name, "ambiguous-rsa", 13))
144
219
algo = GCRY_PK_RSA;
153
Check the signature on CERT using the ISSUER-CERT. This function
154
does only test the cryptographic signature and nothing else. It is
155
assumed that the ISSUER_CERT is valid. */
227
/* Check the signature on CERT using the ISSUER-CERT. This function
228
does only test the cryptographic signature and nothing else. It is
229
assumed that the ISSUER_CERT is valid. */
157
231
gpgsm_check_cert_sig (ksba_cert_t issuer_cert, ksba_cert_t cert)
271
345
gpgsm_check_cms_signature (ksba_cert_t cert, ksba_const_sexp_t sigval,
272
gcry_md_hd_t md, int algo)
346
gcry_md_hd_t md, int mdalgo, int *r_pkalgo)
276
350
gcry_mpi_t frame;
277
351
gcry_sexp_t s_sig, s_hash, s_pkey;
280
358
n = gcry_sexp_canon_len (sigval, 0, NULL, NULL);
315
rc = do_encode_md (md, algo, pk_algo_from_sexp (s_pkey),
316
gcry_pk_get_nbits (s_pkey), &frame);
392
pkalgo = pk_algo_from_sexp (s_pkey);
395
rc = do_encode_md (md, mdalgo, pkalgo,
396
gcry_pk_get_nbits (s_pkey), s_pkey, &frame);
319
399
gcry_sexp_release (s_sig);