~ubuntu-branches/ubuntu/trusty/gnutls26/trusty

« back to all changes in this revision

Viewing changes to lib/x509/verify.c

  • Committer: Package Import Robot
  • Author(s): Andreas Metzler
  • Date: 2011-10-01 15:28:13 UTC
  • mfrom: (12.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20111001152813-yygm1c4cxonfxhzy
* New upstream version.
  + Allow CA importing of 0 certificates to succeed. Closes: #640639
* Add libp11-kit-dev to libgnutls-dev dependencies. (see #643811)
* [20_guiledocstring.diff] guile: Fix docstring extraction with CPP 4.5+.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <gnutls_cert.h>
33
33
#include <libtasn1.h>
34
34
#include <gnutls_global.h>
35
 
#include <gnutls_num.h>         /* MAX */
 
35
#include <gnutls_num.h>         /* MAX */
36
36
#include <gnutls_sig.h>
37
37
#include <gnutls_str.h>
38
38
#include <gnutls_datum.h>
40
40
#include <common.h>
41
41
 
42
42
static int _gnutls_verify_certificate2 (gnutls_x509_crt_t cert,
43
 
                                        const gnutls_x509_crt_t * trusted_cas,
44
 
                                        int tcas_size, unsigned int flags,
45
 
                                        unsigned int *output,
46
 
                                        gnutls_x509_crt_t * issuer);
 
43
                                        const gnutls_x509_crt_t * trusted_cas,
 
44
                                        int tcas_size, unsigned int flags,
 
45
                                        unsigned int *output,
 
46
                                        gnutls_x509_crt_t * issuer);
47
47
 
48
48
static int is_crl_issuer (gnutls_x509_crl_t crl,
49
 
                          gnutls_x509_crt_t issuer_cert);
 
49
                          gnutls_x509_crt_t issuer_cert);
50
50
 
51
51
static int _gnutls_verify_crl2 (gnutls_x509_crl_t crl,
52
 
                                const gnutls_x509_crt_t * trusted_cas,
53
 
                                int tcas_size, unsigned int flags,
54
 
                                unsigned int *output);
 
52
                                const gnutls_x509_crt_t * trusted_cas,
 
53
                                int tcas_size, unsigned int flags,
 
54
                                unsigned int *output);
55
55
 
56
 
/* Checks if two certs are identical.  Return 0 onn match. */
 
56
/* Checks if two certs are identical.  Return 0 on match. */
57
57
static int
58
58
check_if_same_cert (gnutls_x509_crt_t cert1, gnutls_x509_crt_t cert2)
59
59
{
61
61
  {
62
62
  NULL, 0};
63
63
  int result;
64
 
 
 
64
  opaque serial1[128], serial2[128];
 
65
  size_t serial1_size, serial2_size;
 
66
 
 
67
  serial1_size = sizeof (serial1);
 
68
  result = gnutls_x509_crt_get_serial (cert1, serial1, &serial1_size);
 
69
  if (result < 0)
 
70
    {
 
71
      gnutls_assert ();
 
72
      goto cmp;
 
73
    }
 
74
 
 
75
  serial2_size = sizeof (serial2);
 
76
  result = gnutls_x509_crt_get_serial (cert2, serial2, &serial2_size);
 
77
  if (result < 0)
 
78
    {
 
79
      gnutls_assert ();
 
80
      goto cmp;
 
81
    }
 
82
 
 
83
  if (serial2_size != serial1_size
 
84
      || memcmp (serial1, serial2, serial1_size) != 0)
 
85
    {
 
86
      return 1;
 
87
    }
 
88
 
 
89
cmp:
65
90
  result = _gnutls_x509_der_encode (cert1->cert, "", &cert1bin, 0);
66
91
  if (result < 0)
67
92
    {
97
122
 */
98
123
static int
99
124
check_if_ca (gnutls_x509_crt_t cert, gnutls_x509_crt_t issuer,
100
 
             unsigned int flags)
 
125
             unsigned int flags)
101
126
{
102
127
  gnutls_datum_t cert_signed_data = { NULL, 0 };
103
128
  gnutls_datum_t issuer_signed_data = { NULL, 0 };
112
137
 
113
138
  result =
114
139
    _gnutls_x509_get_signed_data (issuer->cert, "tbsCertificate",
115
 
                                  &issuer_signed_data);
 
140
                                  &issuer_signed_data);
116
141
  if (result < 0)
117
142
    {
118
143
      gnutls_assert ();
121
146
 
122
147
  result =
123
148
    _gnutls_x509_get_signed_data (cert->cert, "tbsCertificate",
124
 
                                  &cert_signed_data);
 
149
                                  &cert_signed_data);
125
150
  if (result < 0)
126
151
    {
127
152
      gnutls_assert ();
150
175
  if (!(flags & GNUTLS_VERIFY_DO_NOT_ALLOW_SAME))
151
176
    if (cert_signed_data.size == issuer_signed_data.size)
152
177
      {
153
 
        if ((memcmp (cert_signed_data.data, issuer_signed_data.data,
154
 
                     cert_signed_data.size) == 0) &&
155
 
            (cert_signature.size == issuer_signature.size) &&
156
 
            (memcmp (cert_signature.data, issuer_signature.data,
157
 
                     cert_signature.size) == 0))
158
 
          {
159
 
            result = 1;
160
 
            goto cleanup;
161
 
          }
 
178
        if ((memcmp (cert_signed_data.data, issuer_signed_data.data,
 
179
                     cert_signed_data.size) == 0) &&
 
180
            (cert_signature.size == issuer_signature.size) &&
 
181
            (memcmp (cert_signature.data, issuer_signature.data,
 
182
                     cert_signature.size) == 0))
 
183
          {
 
184
            result = 1;
 
185
            goto cleanup;
 
186
          }
162
187
      }
163
188
 
164
189
  result = gnutls_x509_crt_get_ca_status (issuer, NULL);
170
195
  /* Handle V1 CAs that do not have a basicConstraint, but accept
171
196
     these certs only if the appropriate flags are set. */
172
197
  else if ((result == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) &&
173
 
           ((flags & GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT) ||
174
 
            (!(flags & GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT) &&
175
 
             (gnutls_x509_crt_check_issuer (issuer, issuer) == 1))))
 
198
           ((flags & GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT) ||
 
199
            (!(flags & GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT) &&
 
200
             (gnutls_x509_crt_check_issuer (issuer, issuer) == 1))))
176
201
    {
177
202
      gnutls_assert ();
178
203
      result = 1;
233
258
 
234
259
static inline gnutls_x509_crt_t
235
260
find_issuer (gnutls_x509_crt_t cert,
236
 
             const gnutls_x509_crt_t * trusted_cas, int tcas_size)
 
261
             const gnutls_x509_crt_t * trusted_cas, int tcas_size)
237
262
{
238
263
  int i;
239
264
 
243
268
  for (i = 0; i < tcas_size; i++)
244
269
    {
245
270
      if (is_issuer (cert, trusted_cas[i]) == 1)
246
 
        return trusted_cas[i];
 
271
        return trusted_cas[i];
247
272
    }
248
273
 
249
274
  gnutls_assert ();
266
291
 */
267
292
static int
268
293
_gnutls_verify_certificate2 (gnutls_x509_crt_t cert,
269
 
                             const gnutls_x509_crt_t * trusted_cas,
270
 
                             int tcas_size, unsigned int flags,
271
 
                             unsigned int *output,
272
 
                             gnutls_x509_crt_t * _issuer)
 
294
                             const gnutls_x509_crt_t * trusted_cas,
 
295
                             int tcas_size, unsigned int flags,
 
296
                             unsigned int *output,
 
297
                             gnutls_x509_crt_t * _issuer)
273
298
{
274
299
  gnutls_datum_t cert_signed_data = { NULL, 0 };
275
300
  gnutls_datum_t cert_signature = { NULL, 0 };
276
301
  gnutls_x509_crt_t issuer = NULL;
277
 
  int ret, issuer_version, result;
 
302
  int issuer_version, result;
278
303
 
279
304
  if (output)
280
305
    *output = 0;
285
310
    {
286
311
      gnutls_assert ();
287
312
      if (output)
288
 
        *output |= GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_INVALID;
 
313
        *output |= GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_INVALID;
289
314
      return 0;
290
315
    }
291
316
 
295
320
  if (issuer == NULL)
296
321
    {
297
322
      if (output)
298
 
        *output |= GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_INVALID;
 
323
        *output |= GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_INVALID;
299
324
      gnutls_assert ();
300
325
      return 0;
301
326
    }
311
336
    }
312
337
 
313
338
  if (!(flags & GNUTLS_VERIFY_DISABLE_CA_SIGN) &&
314
 
      ((flags & GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT) || issuer_version != 1))
 
339
      ((flags & GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT)
 
340
       || issuer_version != 1))
315
341
    {
316
342
      if (check_if_ca (cert, issuer, flags) == 0)
317
 
        {
318
 
          gnutls_assert ();
319
 
          if (output)
320
 
            *output |= GNUTLS_CERT_SIGNER_NOT_CA | GNUTLS_CERT_INVALID;
321
 
          return 0;
322
 
        }
 
343
        {
 
344
          gnutls_assert ();
 
345
          if (output)
 
346
            *output |= GNUTLS_CERT_SIGNER_NOT_CA | GNUTLS_CERT_INVALID;
 
347
          return 0;
 
348
        }
323
349
    }
324
350
 
325
351
  result =
326
352
    _gnutls_x509_get_signed_data (cert->cert, "tbsCertificate",
327
 
                                  &cert_signed_data);
 
353
                                  &cert_signed_data);
328
354
  if (result < 0)
329
355
    {
330
356
      gnutls_assert ();
339
365
      goto cleanup;
340
366
    }
341
367
 
342
 
  ret =
 
368
  result =
343
369
    _gnutls_x509_verify_signature (&cert_signed_data, NULL, &cert_signature,
344
 
                                   issuer);
345
 
  if (ret < 0)
346
 
    {
347
 
      gnutls_assert ();
348
 
    }
349
 
  else if (ret == 0)
 
370
                                   issuer);
 
371
  if (result == GNUTLS_E_PK_SIG_VERIFY_FAILED)
350
372
    {
351
373
      gnutls_assert ();
352
374
      /* error. ignore it */
353
375
      if (output)
354
 
        *output |= GNUTLS_CERT_INVALID;
355
 
      ret = 0;
 
376
        *output |= GNUTLS_CERT_INVALID;
 
377
      result = 0;
 
378
    }
 
379
  else if (result < 0)
 
380
    {
 
381
      gnutls_assert();
 
382
      goto cleanup;
356
383
    }
357
384
 
358
385
  /* If the certificate is not self signed check if the algorithms
366
393
      sigalg = gnutls_x509_crt_get_signature_algorithm (cert);
367
394
 
368
395
      if (((sigalg == GNUTLS_SIGN_RSA_MD2) &&
369
 
           !(flags & GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2)) ||
370
 
          ((sigalg == GNUTLS_SIGN_RSA_MD5) &&
371
 
           !(flags & GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5)))
372
 
        {
373
 
          if (output)
374
 
            *output |= GNUTLS_CERT_INSECURE_ALGORITHM | GNUTLS_CERT_INVALID;
375
 
          ret = 0;
376
 
        }
 
396
           !(flags & GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2)) ||
 
397
          ((sigalg == GNUTLS_SIGN_RSA_MD5) &&
 
398
           !(flags & GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5)))
 
399
        {
 
400
          if (output)
 
401
            *output |= GNUTLS_CERT_INSECURE_ALGORITHM | GNUTLS_CERT_INVALID;
 
402
          result = 0;
 
403
        }
377
404
    }
378
405
 
379
 
  result = ret;
380
 
 
381
406
cleanup:
382
407
  _gnutls_free_datum (&cert_signed_data);
383
408
  _gnutls_free_datum (&cert_signature);
399
424
 **/
400
425
int
401
426
gnutls_x509_crt_check_issuer (gnutls_x509_crt_t cert,
402
 
                              gnutls_x509_crt_t issuer)
 
427
                              gnutls_x509_crt_t issuer)
403
428
{
404
429
  return is_issuer (cert, issuer);
405
430
}
438
463
 */
439
464
static unsigned int
440
465
_gnutls_x509_verify_certificate (const gnutls_x509_crt_t * certificate_list,
441
 
                                 int clist_size,
442
 
                                 const gnutls_x509_crt_t * trusted_cas,
443
 
                                 int tcas_size,
444
 
                                 const gnutls_x509_crl_t * CRLs,
445
 
                                 int crls_size, unsigned int flags)
 
466
                                 int clist_size,
 
467
                                 const gnutls_x509_crt_t * trusted_cas,
 
468
                                 int tcas_size,
 
469
                                 const gnutls_x509_crl_t * CRLs,
 
470
                                 int crls_size, unsigned int flags)
446
471
{
447
472
  int i = 0, ret;
448
473
  unsigned int status = 0, output;
449
 
  time_t now = time (0);
 
474
  time_t now = gnutls_time (0);
450
475
  gnutls_x509_crt_t issuer = NULL;
451
476
 
452
477
  if (clist_size > 1)
461
486
       * MD2 algorithm.
462
487
       */
463
488
      if (gnutls_x509_crt_check_issuer (certificate_list[clist_size - 1],
464
 
                                        certificate_list[clist_size - 1]) > 0)
465
 
        {
466
 
          clist_size--;
467
 
        }
 
489
                                        certificate_list[clist_size - 1]) > 0)
 
490
        {
 
491
          clist_size--;
 
492
        }
468
493
    }
469
494
 
470
495
  /* We want to shorten the chain by removing the cert that matches
473
498
   * self-signed E but already removed above), and we trust B, remove
474
499
   * B, C and D. */
475
500
  if (!(flags & GNUTLS_VERIFY_DO_NOT_ALLOW_SAME))
 
501
    i = 0;                      /* also replace the first one */
 
502
  else
 
503
    i = 1;                      /* do not replace the first one */
 
504
 
 
505
  for (; i < clist_size; i++)
476
506
    {
477
 
      for (i = 0; i < clist_size; i++)
478
 
        {
479
 
          int j;
 
507
      int j;
480
508
 
481
 
          for (j = 0; j < tcas_size; j++)
482
 
            {
483
 
              if (check_if_same_cert (certificate_list[i],
484
 
                                      trusted_cas[j]) == 0)
485
 
                {
486
 
                  /* explicity time check for trusted CA that we remove from
487
 
                   * list. GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS
488
 
                   */
489
 
                  if (!(flags & GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS)
490
 
                      && !(flags & GNUTLS_VERIFY_DISABLE_TIME_CHECKS))
491
 
                    {
492
 
                      status |= check_time (trusted_cas[j], now);
493
 
                      if (status != 0)
494
 
                        {
495
 
                          return status;
496
 
                        }
497
 
                    }
498
 
                  clist_size = i;
499
 
                  break;
500
 
                }
501
 
            }
502
 
          /* clist_size may have been changed which gets out of loop */
503
 
        }
 
509
      for (j = 0; j < tcas_size; j++)
 
510
        {
 
511
          if (check_if_same_cert (certificate_list[i], trusted_cas[j]) == 0)
 
512
            {
 
513
              /* explicity time check for trusted CA that we remove from
 
514
               * list. GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS
 
515
               */
 
516
              if (!(flags & GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS)
 
517
                  && !(flags & GNUTLS_VERIFY_DISABLE_TIME_CHECKS))
 
518
                {
 
519
                  status |= check_time (trusted_cas[j], now);
 
520
                  if (status != 0)
 
521
                    {
 
522
                      return status;
 
523
                    }
 
524
                }
 
525
              clist_size = i;
 
526
              break;
 
527
            }
 
528
        }
 
529
      /* clist_size may have been changed which gets out of loop */
504
530
    }
505
531
 
506
532
  if (clist_size == 0)
515
541
   * in self signed etc certificates.
516
542
   */
517
543
  ret = _gnutls_verify_certificate2 (certificate_list[clist_size - 1],
518
 
                                     trusted_cas, tcas_size, flags, &output,
519
 
                                     &issuer);
 
544
                                     trusted_cas, tcas_size, flags, &output,
 
545
                                     &issuer);
520
546
  if (ret == 0)
521
547
    {
522
548
      /* if the last certificate in the certificate
535
561
  for (i = 0; i < clist_size; i++)
536
562
    {
537
563
      ret = gnutls_x509_crt_check_revocation (certificate_list[i],
538
 
                                              CRLs, crls_size);
 
564
                                              CRLs, crls_size);
539
565
      if (ret == 1)
540
 
        {                       /* revoked */
541
 
          status |= GNUTLS_CERT_REVOKED;
542
 
          status |= GNUTLS_CERT_INVALID;
543
 
          return status;
544
 
        }
 
566
        {                       /* revoked */
 
567
          status |= GNUTLS_CERT_REVOKED;
 
568
          status |= GNUTLS_CERT_INVALID;
 
569
          return status;
 
570
        }
545
571
    }
546
572
#endif
547
573
 
552
578
    {
553
579
      /* check the time of the issuer first */
554
580
      if (!(flags & GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS))
555
 
        {
556
 
          if (issuer == NULL)
557
 
            {
558
 
              gnutls_assert ();
559
 
              return GNUTLS_E_INTERNAL_ERROR;
560
 
            }
 
581
        {
 
582
          if (issuer == NULL)
 
583
            {
 
584
              gnutls_assert ();
 
585
              return GNUTLS_E_INTERNAL_ERROR;
 
586
            }
561
587
 
562
 
          status |= check_time (issuer, now);
563
 
          if (status != 0)
564
 
            {
565
 
              return status;
566
 
            }
567
 
        }
 
588
          status |= check_time (issuer, now);
 
589
          if (status != 0)
 
590
            {
 
591
              return status;
 
592
            }
 
593
        }
568
594
 
569
595
      for (i = 0; i < clist_size; i++)
570
 
        {
571
 
          status |= check_time (certificate_list[i], now);
572
 
          if (status != 0)
573
 
            {
574
 
              return status;
575
 
            }
576
 
        }
 
596
        {
 
597
          status |= check_time (certificate_list[i], now);
 
598
          if (status != 0)
 
599
            {
 
600
              return status;
 
601
            }
 
602
        }
577
603
    }
578
604
 
579
605
  /* Verify the certificate path (chain)
581
607
  for (i = clist_size - 1; i > 0; i--)
582
608
    {
583
609
      if (i - 1 < 0)
584
 
        break;
 
610
        break;
585
611
 
586
612
      /* note that here we disable this V1 CA flag. So that no version 1
587
613
       * certificates can exist in a supplied chain.
588
614
       */
589
615
      if (!(flags & GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT))
590
 
        flags &= ~(GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
 
616
        flags &= ~(GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
591
617
      if ((ret =
592
 
           _gnutls_verify_certificate2 (certificate_list[i - 1],
593
 
                                        &certificate_list[i], 1, flags,
594
 
                                        NULL, NULL)) == 0)
595
 
        {
596
 
          status |= GNUTLS_CERT_INVALID;
597
 
          return status;
598
 
        }
 
618
           _gnutls_verify_certificate2 (certificate_list[i - 1],
 
619
                                        &certificate_list[i], 1, flags,
 
620
                                        NULL, NULL)) == 0)
 
621
        {
 
622
          status |= GNUTLS_CERT_INVALID;
 
623
          return status;
 
624
        }
599
625
    }
600
626
 
601
627
  return 0;
608
634
 */
609
635
static int
610
636
decode_ber_digest_info (const gnutls_datum_t * info,
611
 
                        gnutls_mac_algorithm_t * hash,
612
 
                        opaque * digest, int *digest_size)
 
637
                        gnutls_mac_algorithm_t * hash,
 
638
                        opaque * digest, int *digest_size)
613
639
{
614
640
  ASN1_TYPE dinfo = ASN1_TYPE_EMPTY;
615
641
  int result;
617
643
  int len;
618
644
 
619
645
  if ((result = asn1_create_element (_gnutls_get_gnutls_asn (),
620
 
                                     "GNUTLS.DigestInfo",
621
 
                                     &dinfo)) != ASN1_SUCCESS)
 
646
                                     "GNUTLS.DigestInfo",
 
647
                                     &dinfo)) != ASN1_SUCCESS)
622
648
    {
623
649
      gnutls_assert ();
624
650
      return _gnutls_asn2err (result);
658
684
  /* To avoid permitting garbage in the parameters field, either the
659
685
     parameters field is not present, or it contains 0x05 0x00. */
660
686
  if (!(result == ASN1_ELEMENT_NOT_FOUND ||
661
 
        (result == ASN1_SUCCESS && len == 2 &&
662
 
         str[0] == 0x05 && str[1] == 0x00)))
 
687
        (result == ASN1_SUCCESS && len == ASN1_NULL_SIZE &&
 
688
         memcmp (str, ASN1_NULL, ASN1_NULL_SIZE) == 0)))
663
689
    {
664
690
      gnutls_assert ();
665
691
      asn1_delete_structure (&dinfo);
686
712
 */
687
713
static int
688
714
_pkcs1_rsa_verify_sig (const gnutls_datum_t * text,
689
 
                       const gnutls_datum_t * prehash,
690
 
                       const gnutls_datum_t * signature, bigint_t * params,
691
 
                       int params_len)
 
715
                       const gnutls_datum_t * prehash,
 
716
                       const gnutls_datum_t * signature, bigint_t * params,
 
717
                       int params_len)
692
718
{
693
719
  gnutls_mac_algorithm_t hash = GNUTLS_MAC_UNKNOWN;
694
720
  int ret;
732
758
  else
733
759
    {
734
760
      if (!text)
735
 
        {
736
 
          gnutls_assert ();
737
 
          return GNUTLS_E_INVALID_REQUEST;
738
 
        }
 
761
        {
 
762
          gnutls_assert ();
 
763
          return GNUTLS_E_INVALID_REQUEST;
 
764
        }
739
765
 
740
766
      ret = _gnutls_hash_init (&hd, hash);
741
767
      if (ret < 0)
742
 
        {
743
 
          gnutls_assert ();
744
 
          return ret;
745
 
        }
 
768
        {
 
769
          gnutls_assert ();
 
770
          return ret;
 
771
        }
746
772
 
747
773
      _gnutls_hash (&hd, text->data, text->size);
748
774
      _gnutls_hash_deinit (&hd, md);
763
789
 */
764
790
static int
765
791
dsa_verify_sig (const gnutls_datum_t * text,
766
 
                const gnutls_datum_t * hash,
767
 
                const gnutls_datum_t * signature, bigint_t * params,
768
 
                int params_len)
 
792
                const gnutls_datum_t * hash,
 
793
                const gnutls_datum_t * signature, bigint_t * params,
 
794
                int params_len)
769
795
{
770
796
  int ret;
771
797
  opaque _digest[MAX_HASH_SIZE];
772
798
  gnutls_datum_t digest;
773
799
  digest_hd_st hd;
 
800
  gnutls_digest_algorithm_t algo;
 
801
  unsigned int hash_len;
774
802
 
775
 
  if (hash && hash->data && hash->size == 20)
 
803
  algo = _gnutls_dsa_q_to_hash (params[1], &hash_len);
 
804
  if (hash)
776
805
    {
 
806
      /* SHA1 or better allowed */
 
807
      if (!hash->data || hash->size < hash_len)
 
808
        {
 
809
          gnutls_assert();
 
810
          _gnutls_debug_log("Hash size (%d) does not correspond to hash %s", (int)hash->size, gnutls_mac_get_name(algo));
 
811
          
 
812
          if (hash->size != 20)
 
813
            return GNUTLS_E_PK_SIG_VERIFY_FAILED;
 
814
        }
777
815
      digest = *hash;
778
816
    }
779
817
  else
780
818
    {
781
 
      ret = _gnutls_hash_init (&hd, GNUTLS_MAC_SHA1);
 
819
 
 
820
      ret = _gnutls_hash_init (&hd, algo);
782
821
      if (ret < 0)
783
 
        {
784
 
          gnutls_assert ();
785
 
          return ret;
786
 
        }
 
822
        {
 
823
          gnutls_assert ();
 
824
          return ret;
 
825
        }
787
826
 
788
827
      _gnutls_hash (&hd, text->data, text->size);
789
828
      _gnutls_hash_deinit (&hd, _digest);
790
829
 
791
830
      digest.data = _digest;
792
 
      digest.size = 20;
 
831
      digest.size = _gnutls_hash_get_algo_len(algo);
793
832
    }
794
833
 
795
834
  ret = _gnutls_dsa_verify (&digest, signature, params, params_len);
797
836
  return ret;
798
837
}
799
838
 
800
 
/* Verifies the signature data, and returns 0 if not verified,
801
 
 * or 1 otherwise.
 
839
/* Verifies the signature data, and returns GNUTLS_E_PK_SIG_VERIFY_FAILED if 
 
840
 * not verified, or 1 otherwise.
802
841
 */
803
 
static int
804
 
verify_sig (const gnutls_datum_t * tbs,
805
 
            const gnutls_datum_t * hash,
806
 
            const gnutls_datum_t * signature,
807
 
            gnutls_pk_algorithm_t pk, bigint_t * issuer_params,
808
 
            int issuer_params_size)
 
842
int
 
843
pubkey_verify_sig (const gnutls_datum_t * tbs,
 
844
                   const gnutls_datum_t * hash,
 
845
                   const gnutls_datum_t * signature,
 
846
                   gnutls_pk_algorithm_t pk, bigint_t * issuer_params,
 
847
                   int issuer_params_size)
809
848
{
810
849
 
811
850
  switch (pk)
813
852
    case GNUTLS_PK_RSA:
814
853
 
815
854
      if (_pkcs1_rsa_verify_sig
816
 
          (tbs, hash, signature, issuer_params, issuer_params_size) != 0)
817
 
        {
818
 
          gnutls_assert ();
819
 
          return 0;
820
 
        }
 
855
          (tbs, hash, signature, issuer_params, issuer_params_size) != 0)
 
856
        {
 
857
          gnutls_assert ();
 
858
          return GNUTLS_E_PK_SIG_VERIFY_FAILED;
 
859
        }
821
860
 
822
861
      return 1;
823
862
      break;
824
863
 
825
864
    case GNUTLS_PK_DSA:
826
865
      if (dsa_verify_sig
827
 
          (tbs, hash, signature, issuer_params, issuer_params_size) != 0)
828
 
        {
829
 
          gnutls_assert ();
830
 
          return 0;
831
 
        }
 
866
          (tbs, hash, signature, issuer_params, issuer_params_size) != 0)
 
867
        {
 
868
          gnutls_assert ();
 
869
          return GNUTLS_E_PK_SIG_VERIFY_FAILED;
 
870
        }
832
871
 
833
872
      return 1;
834
873
      break;
840
879
}
841
880
 
842
881
gnutls_digest_algorithm_t
843
 
_gnutls_dsa_q_to_hash (bigint_t q)
 
882
_gnutls_dsa_q_to_hash (bigint_t q, unsigned int* hash_len)
844
883
{
845
884
  int bits = _gnutls_mpi_get_nbits (q);
846
885
 
847
886
  if (bits <= 160)
848
887
    {
 
888
      if (hash_len) *hash_len = 20;
849
889
      return GNUTLS_DIG_SHA1;
850
890
    }
851
891
  else if (bits <= 224)
852
892
    {
853
 
      return GNUTLS_DIG_SHA224;
 
893
      if (hash_len) *hash_len = 28;
 
894
      return GNUTLS_DIG_SHA256;
854
895
    }
855
896
  else
856
897
    {
 
898
      if (hash_len) *hash_len = 32;
857
899
      return GNUTLS_DIG_SHA256;
858
900
    }
859
901
}
860
902
 
 
903
/* This will return the appropriate hash to verify the given signature.
 
904
 * If signature is NULL it will return an (or the) appropriate hash for
 
905
 * the given parameters.
 
906
 */
861
907
int
862
908
_gnutls_x509_verify_algorithm (gnutls_mac_algorithm_t * hash,
863
 
                               const gnutls_datum_t * signature,
864
 
                               const gnutls_x509_crt_t issuer)
 
909
                               const gnutls_datum_t * signature,
 
910
                               gnutls_pk_algorithm pk,
 
911
                               bigint_t * issuer_params,
 
912
                               unsigned int issuer_params_size)
865
913
{
866
 
  bigint_t issuer_params[MAX_PUBLIC_PARAMS_SIZE];
867
914
  opaque digest[MAX_HASH_SIZE];
868
915
  gnutls_datum_t decrypted;
869
 
  int issuer_params_size;
870
916
  int digest_size;
871
 
  int ret, i;
872
 
 
873
 
  issuer_params_size = MAX_PUBLIC_PARAMS_SIZE;
874
 
  ret =
875
 
    _gnutls_x509_crt_get_mpis (issuer, issuer_params, &issuer_params_size);
876
 
  if (ret < 0)
877
 
    {
878
 
      gnutls_assert ();
879
 
      return ret;
880
 
    }
881
 
 
882
 
  switch (gnutls_x509_crt_get_pk_algorithm (issuer, NULL))
 
917
  int ret;
 
918
 
 
919
  switch (pk)
883
920
    {
884
921
    case GNUTLS_PK_DSA:
885
922
 
886
923
      if (hash)
887
 
        *hash = _gnutls_dsa_q_to_hash (issuer_params[1]);
 
924
        *hash = _gnutls_dsa_q_to_hash (issuer_params[1], NULL);
888
925
 
889
926
      ret = 0;
890
927
      break;
891
 
 
892
928
    case GNUTLS_PK_RSA:
 
929
      if (signature == NULL)
 
930
        {                       /* return a sensible algorithm */
 
931
          if (hash)
 
932
            *hash = GNUTLS_DIG_SHA256;
 
933
          return 0;
 
934
        }
893
935
 
894
936
      ret =
895
 
        _gnutls_pkcs1_rsa_decrypt (&decrypted, signature,
896
 
                                   issuer_params, issuer_params_size, 1);
 
937
        _gnutls_pkcs1_rsa_decrypt (&decrypted, signature,
 
938
                                   issuer_params, issuer_params_size, 1);
897
939
 
898
940
 
899
941
      if (ret < 0)
900
 
        {
901
 
          gnutls_assert ();
902
 
          goto cleanup;
903
 
        }
 
942
        {
 
943
          gnutls_assert ();
 
944
          goto cleanup;
 
945
        }
904
946
 
905
947
      digest_size = sizeof (digest);
906
948
      if ((ret =
907
 
           decode_ber_digest_info (&decrypted, hash, digest,
908
 
                                   &digest_size)) != 0)
909
 
        {
910
 
          gnutls_assert ();
911
 
          _gnutls_free_datum (&decrypted);
912
 
          goto cleanup;
913
 
        }
 
949
           decode_ber_digest_info (&decrypted, hash, digest,
 
950
                                   &digest_size)) != 0)
 
951
        {
 
952
          gnutls_assert ();
 
953
          _gnutls_free_datum (&decrypted);
 
954
          goto cleanup;
 
955
        }
914
956
 
915
957
      _gnutls_free_datum (&decrypted);
916
958
      if (digest_size != _gnutls_hash_get_algo_len (*hash))
917
 
        {
918
 
          gnutls_assert ();
919
 
          ret = GNUTLS_E_ASN1_GENERIC_ERROR;
920
 
          goto cleanup;
921
 
        }
 
959
        {
 
960
          gnutls_assert ();
 
961
          ret = GNUTLS_E_ASN1_GENERIC_ERROR;
 
962
          goto cleanup;
 
963
        }
922
964
 
923
965
      ret = 0;
924
966
      break;
929
971
    }
930
972
 
931
973
cleanup:
932
 
  /* release allocated mpis */
933
 
  for (i = 0; i < issuer_params_size; i++)
934
 
    {
935
 
      _gnutls_mpi_release (&issuer_params[i]);
936
 
    }
937
974
 
938
975
  return ret;
939
976
 
940
977
}
941
978
 
942
979
/* verifies if the certificate is properly signed.
943
 
 * returns 0 on failure and 1 on success.
 
980
 * returns GNUTLS_E_PK_VERIFY_SIG_FAILED on failure and 1 on success.
944
981
 * 
945
982
 * 'tbs' is the signed data
946
983
 * 'signature' is the signature!
947
984
 */
948
985
int
949
986
_gnutls_x509_verify_signature (const gnutls_datum_t * tbs,
950
 
                               const gnutls_datum_t * hash,
951
 
                               const gnutls_datum_t * signature,
952
 
                               gnutls_x509_crt_t issuer)
 
987
                               const gnutls_datum_t * hash,
 
988
                               const gnutls_datum_t * signature,
 
989
                               gnutls_x509_crt_t issuer)
953
990
{
954
991
  bigint_t issuer_params[MAX_PUBLIC_PARAMS_SIZE];
955
992
  int ret, issuer_params_size, i;
966
1003
    }
967
1004
 
968
1005
  ret =
969
 
    verify_sig (tbs, hash, signature,
970
 
                gnutls_x509_crt_get_pk_algorithm (issuer, NULL),
971
 
                issuer_params, issuer_params_size);
 
1006
    pubkey_verify_sig (tbs, hash, signature,
 
1007
                       gnutls_x509_crt_get_pk_algorithm (issuer, NULL),
 
1008
                       issuer_params, issuer_params_size);
972
1009
  if (ret < 0)
973
1010
    {
974
1011
      gnutls_assert ();
985
1022
}
986
1023
 
987
1024
/* verifies if the certificate is properly signed.
988
 
 * returns 0 on failure and 1 on success.
 
1025
 * returns GNUTLS_E_PK_VERIFY_SIG_FAILED on failure and 1 on success.
989
1026
 * 
990
1027
 * 'tbs' is the signed data
991
1028
 * 'signature' is the signature!
992
1029
 */
993
1030
int
994
1031
_gnutls_x509_privkey_verify_signature (const gnutls_datum_t * tbs,
995
 
                                       const gnutls_datum_t * signature,
996
 
                                       gnutls_x509_privkey_t issuer)
 
1032
                                       const gnutls_datum_t * signature,
 
1033
                                       gnutls_x509_privkey_t issuer)
997
1034
{
998
1035
  int ret;
999
1036
 
1000
 
  ret = verify_sig (tbs, NULL, signature, issuer->pk_algorithm,
1001
 
                    issuer->params, issuer->params_size);
 
1037
  ret = pubkey_verify_sig (tbs, NULL, signature, issuer->pk_algorithm,
 
1038
                           issuer->params, issuer->params_size);
1002
1039
  if (ret < 0)
1003
1040
    {
1004
1041
      gnutls_assert ();
1041
1078
 **/
1042
1079
int
1043
1080
gnutls_x509_crt_list_verify (const gnutls_x509_crt_t * cert_list,
1044
 
                             int cert_list_length,
1045
 
                             const gnutls_x509_crt_t * CA_list,
1046
 
                             int CA_list_length,
1047
 
                             const gnutls_x509_crl_t * CRL_list,
1048
 
                             int CRL_list_length, unsigned int flags,
1049
 
                             unsigned int *verify)
 
1081
                             int cert_list_length,
 
1082
                             const gnutls_x509_crt_t * CA_list,
 
1083
                             int CA_list_length,
 
1084
                             const gnutls_x509_crl_t * CRL_list,
 
1085
                             int CRL_list_length, unsigned int flags,
 
1086
                             unsigned int *verify)
1050
1087
{
1051
1088
  if (cert_list == NULL || cert_list_length == 0)
1052
1089
    return GNUTLS_E_NO_CERTIFICATE_FOUND;
1055
1092
   */
1056
1093
  *verify =
1057
1094
    _gnutls_x509_verify_certificate (cert_list, cert_list_length,
1058
 
                                     CA_list, CA_list_length, CRL_list,
1059
 
                                     CRL_list_length, flags);
 
1095
                                     CA_list, CA_list_length, CRL_list,
 
1096
                                     CRL_list_length, flags);
1060
1097
 
1061
1098
  return 0;
1062
1099
}
1077
1114
 **/
1078
1115
int
1079
1116
gnutls_x509_crt_verify (gnutls_x509_crt_t cert,
1080
 
                        const gnutls_x509_crt_t * CA_list,
1081
 
                        int CA_list_length, unsigned int flags,
1082
 
                        unsigned int *verify)
 
1117
                        const gnutls_x509_crt_t * CA_list,
 
1118
                        int CA_list_length, unsigned int flags,
 
1119
                        unsigned int *verify)
1083
1120
{
1084
1121
  /* Verify certificate 
1085
1122
   */
1086
1123
  *verify =
1087
1124
    _gnutls_x509_verify_certificate (&cert, 1,
1088
 
                                     CA_list, CA_list_length, NULL, 0, flags);
 
1125
                                     CA_list, CA_list_length, NULL, 0, flags);
1089
1126
  return 0;
1090
1127
}
1091
1128
 
1107
1144
 **/
1108
1145
int
1109
1146
gnutls_x509_crl_check_issuer (gnutls_x509_crl_t cert,
1110
 
                              gnutls_x509_crt_t issuer)
 
1147
                              gnutls_x509_crt_t issuer)
1111
1148
{
1112
1149
  return is_crl_issuer (cert, issuer);
1113
1150
}
1129
1166
 **/
1130
1167
int
1131
1168
gnutls_x509_crl_verify (gnutls_x509_crl_t crl,
1132
 
                        const gnutls_x509_crt_t * CA_list,
1133
 
                        int CA_list_length, unsigned int flags,
1134
 
                        unsigned int *verify)
 
1169
                        const gnutls_x509_crt_t * CA_list,
 
1170
                        int CA_list_length, unsigned int flags,
 
1171
                        unsigned int *verify)
1135
1172
{
1136
1173
  int ret;
1137
1174
  /* Verify crl 
1157
1194
  NULL, 0};
1158
1195
  int ret;
1159
1196
 
1160
 
  ret = _gnutls_x509_crl_get_raw_issuer_dn (crl, &dn1);
 
1197
  ret = gnutls_x509_crl_get_raw_issuer_dn (crl, &dn1);
1161
1198
  if (ret < 0)
1162
1199
    {
1163
1200
      gnutls_assert ();
1182
1219
 
1183
1220
static inline gnutls_x509_crt_t
1184
1221
find_crl_issuer (gnutls_x509_crl_t crl,
1185
 
                 const gnutls_x509_crt_t * trusted_cas, int tcas_size)
 
1222
                 const gnutls_x509_crt_t * trusted_cas, int tcas_size)
1186
1223
{
1187
1224
  int i;
1188
1225
 
1192
1229
  for (i = 0; i < tcas_size; i++)
1193
1230
    {
1194
1231
      if (is_crl_issuer (crl, trusted_cas[i]) == 1)
1195
 
        return trusted_cas[i];
 
1232
        return trusted_cas[i];
1196
1233
    }
1197
1234
 
1198
1235
  gnutls_assert ();
1210
1247
 */
1211
1248
static int
1212
1249
_gnutls_verify_crl2 (gnutls_x509_crl_t crl,
1213
 
                     const gnutls_x509_crt_t * trusted_cas,
1214
 
                     int tcas_size, unsigned int flags, unsigned int *output)
 
1250
                     const gnutls_x509_crt_t * trusted_cas,
 
1251
                     int tcas_size, unsigned int flags, unsigned int *output)
1215
1252
{
1216
1253
/* CRL is ignored for now */
1217
1254
  gnutls_datum_t crl_signed_data = { NULL, 0 };
1218
1255
  gnutls_datum_t crl_signature = { NULL, 0 };
1219
1256
  gnutls_x509_crt_t issuer;
1220
 
  int ret, result;
 
1257
  int result;
1221
1258
 
1222
1259
  if (output)
1223
1260
    *output = 0;
1228
1265
    {
1229
1266
      gnutls_assert ();
1230
1267
      if (output)
1231
 
        *output |= GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_INVALID;
 
1268
        *output |= GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_INVALID;
1232
1269
      return 0;
1233
1270
    }
1234
1271
 
1239
1276
    {
1240
1277
      gnutls_assert ();
1241
1278
      if (output)
1242
 
        *output |= GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_INVALID;
 
1279
        *output |= GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_INVALID;
1243
1280
      return 0;
1244
1281
    }
1245
1282
 
1246
1283
  if (!(flags & GNUTLS_VERIFY_DISABLE_CA_SIGN))
1247
1284
    {
1248
1285
      if (gnutls_x509_crt_get_ca_status (issuer, NULL) != 1)
1249
 
        {
1250
 
          gnutls_assert ();
1251
 
          if (output)
1252
 
            *output |= GNUTLS_CERT_SIGNER_NOT_CA | GNUTLS_CERT_INVALID;
1253
 
          return 0;
1254
 
        }
 
1286
        {
 
1287
          gnutls_assert ();
 
1288
          if (output)
 
1289
            *output |= GNUTLS_CERT_SIGNER_NOT_CA | GNUTLS_CERT_INVALID;
 
1290
          return 0;
 
1291
        }
1255
1292
    }
1256
1293
 
1257
1294
  result =
1269
1306
      goto cleanup;
1270
1307
    }
1271
1308
 
1272
 
  ret =
 
1309
  result =
1273
1310
    _gnutls_x509_verify_signature (&crl_signed_data, NULL, &crl_signature,
1274
 
                                   issuer);
1275
 
  if (ret < 0)
1276
 
    {
1277
 
      gnutls_assert ();
1278
 
    }
1279
 
  else if (ret == 0)
 
1311
                                   issuer);
 
1312
  if (result == GNUTLS_E_PK_SIG_VERIFY_FAILED)
1280
1313
    {
1281
1314
      gnutls_assert ();
1282
1315
      /* error. ignore it */
1283
1316
      if (output)
1284
 
        *output |= GNUTLS_CERT_INVALID;
1285
 
      ret = 0;
 
1317
        *output |= GNUTLS_CERT_INVALID;
 
1318
      result = 0;
 
1319
    }
 
1320
  else if (result < 0)
 
1321
    {
 
1322
      gnutls_assert ();
 
1323
      goto cleanup;
1286
1324
    }
1287
1325
 
1288
1326
  {
1291
1329
    sigalg = gnutls_x509_crl_get_signature_algorithm (crl);
1292
1330
 
1293
1331
    if (((sigalg == GNUTLS_SIGN_RSA_MD2) &&
1294
 
         !(flags & GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2)) ||
1295
 
        ((sigalg == GNUTLS_SIGN_RSA_MD5) &&
1296
 
         !(flags & GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5)))
 
1332
         !(flags & GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2)) ||
 
1333
        ((sigalg == GNUTLS_SIGN_RSA_MD5) &&
 
1334
         !(flags & GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5)))
1297
1335
      {
1298
 
        if (output)
1299
 
          *output |= GNUTLS_CERT_INSECURE_ALGORITHM | GNUTLS_CERT_INVALID;
1300
 
        ret = 0;
 
1336
        if (output)
 
1337
          *output |= GNUTLS_CERT_INSECURE_ALGORITHM | GNUTLS_CERT_INVALID;
 
1338
        result = 0;
1301
1339
      }
1302
1340
  }
1303
1341
 
1304
 
  result = ret;
1305
 
 
1306
1342
cleanup:
1307
1343
  _gnutls_free_datum (&crl_signed_data);
1308
1344
  _gnutls_free_datum (&crl_signature);