~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to sm/certcheck.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *
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.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
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/>.
19
18
 */
20
19
 
21
20
#include <config.h>
35
34
#include "i18n.h"
36
35
 
37
36
 
 
37
/* Remove this if libgcrypt 1.3.0 is required. */
 
38
#define MY_GCRY_PK_ECDSA  301
 
39
 
 
40
 
 
41
/* Return the number of bits of the Q parameter from the DSA key
 
42
   KEY.  */
 
43
static unsigned int
 
44
get_dsa_qbits (gcry_sexp_t key)
 
45
{
 
46
  gcry_sexp_t l1, l2;
 
47
  gcry_mpi_t q;
 
48
  unsigned int nbits;
 
49
 
 
50
  l1 = gcry_sexp_find_token (key, "public-key", 0);
 
51
  if (!l1)
 
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);
 
57
  if (!l1)
 
58
    return 0; /* Invalid object.  */
 
59
  q = gcry_sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG);
 
60
  gcry_sexp_release (l1);
 
61
  if (!q)
 
62
    return 0; /* Missing value.  */
 
63
  nbits = gcry_mpi_get_nbits (q);
 
64
  gcry_mpi_release (q);
 
65
 
 
66
  return nbits;
 
67
}
 
68
 
 
69
 
38
70
static int
39
71
do_encode_md (gcry_md_hd_t md, int algo, int pkalgo, unsigned int nbits,
40
 
              gcry_mpi_t *r_val)
 
72
              gcry_sexp_t pkey, gcry_mpi_t *r_val)
41
73
{
42
74
  int n;
43
75
  size_t nframe;
44
76
  unsigned char *frame;
45
77
 
46
 
  if (pkalgo == GCRY_PK_DSA)
 
78
  if (pkalgo == GCRY_PK_DSA || pkalgo == MY_GCRY_PK_ECDSA)
47
79
    {
 
80
      unsigned int qbits;
 
81
 
 
82
      if ( pkalgo == MY_GCRY_PK_ECDSA )
 
83
        qbits = gcry_pk_get_nbits (pkey);
 
84
      else
 
85
        qbits = get_dsa_qbits (pkey);
 
86
 
 
87
      if ( (qbits%8) )
 
88
        {
 
89
          log_error(_("DSA requires the hash length to be a"
 
90
                      " multiple of 8 bits\n"));
 
91
          return gpg_error (GPG_ERR_INTERNAL);
 
92
        }
 
93
 
 
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
 
98
         DSA. ;) */
 
99
      if (qbits < 160)
 
100
        {
 
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);
 
104
        }
 
105
 
 
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);
49
 
      if (nframe != 20)
 
109
      if (nframe < qbits/8)
50
110
        {
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);
53
118
        }
 
119
 
54
120
      frame = xtrymalloc (nframe);
55
121
      if (!frame)
56
 
        return OUT_OF_CORE (errno);
 
122
        return out_of_core ();
57
123
      memcpy (frame, gcry_md_read (md, algo), nframe);
58
124
      n = nframe;
 
125
      /* Truncate.  */
 
126
      if (n > qbits/8)
 
127
        n = qbits/8;
59
128
    }
60
129
  else
61
130
    {
67
136
      nframe = (nbits+7) / 8;
68
137
 
69
138
      asnlen = DIM(asn);
 
139
      if (!algo || gcry_md_test_algo (algo))
 
140
        return gpg_error (GPG_ERR_DIGEST_ALGO);
70
141
      if (gcry_md_algo_info (algo, GCRYCTL_GET_ASNOID, asn, &asnlen))
71
142
        {
72
143
          log_error ("no object identifier for algo %d\n", algo);
90
161
       */
91
162
      frame = xtrymalloc (nframe);
92
163
      if (!frame)
93
 
        return OUT_OF_CORE (errno);
 
164
        return out_of_core ();
94
165
      n = 0;
95
166
      frame[n++] = 0;
96
167
      frame[n++] = 1; /* block type */
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;
145
220
  else
149
224
}
150
225
 
151
226
 
152
 
/*
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. */
156
230
int
157
231
gpgsm_check_cert_sig (ksba_cert_t issuer_cert, ksba_cert_t cert)
158
232
{
240
314
    }
241
315
 
242
316
  rc = do_encode_md (md, algo, pk_algo_from_sexp (s_pkey),
243
 
                     gcry_pk_get_nbits (s_pkey), &frame);
 
317
                     gcry_pk_get_nbits (s_pkey), s_pkey, &frame);
244
318
  if (rc)
245
319
    {
246
320
      gcry_md_close (md);
269
343
 
270
344
int
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)
273
347
{
274
348
  int rc;
275
349
  ksba_sexp_t p;
276
350
  gcry_mpi_t frame;
277
351
  gcry_sexp_t s_sig, s_hash, s_pkey;
278
352
  size_t n;
 
353
  int pkalgo;
 
354
 
 
355
  if (r_pkalgo)
 
356
    *r_pkalgo = 0;
279
357
 
280
358
  n = gcry_sexp_canon_len (sigval, 0, NULL, NULL);
281
359
  if (!n)
311
389
      return rc;
312
390
    }
313
391
 
314
 
 
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);
 
393
  if (r_pkalgo)
 
394
    *r_pkalgo = pkalgo;
 
395
  rc = do_encode_md (md, mdalgo, pkalgo,
 
396
                     gcry_pk_get_nbits (s_pkey), s_pkey, &frame);
317
397
  if (rc)
318
398
    {
319
399
      gcry_sexp_release (s_sig);