aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/tls/gnutls_x509.c
blob: 3ec47e94ae8fad89a7af3ee31812a0cc90b58eee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
/*
 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */

#include <gnutls_int.h>
#include "gnutls_auth_int.h"
#include "gnutls_errors.h"
#include <gnutls_cert.h>
#include <auth_cert.h>
#include "gnutls_dh.h"
#include "gnutls_num.h"
#include "gnutls_datum.h"
#include <gnutls_pk.h>
#include <gnutls_algorithms.h>
#include <gnutls_global.h>
#include <gnutls_record.h>
#include <gnutls_sig.h>
#include <gnutls_state.h>
#include <gnutls_pk.h>
#include <gnutls_str.h>
#include <debug.h>
#include <x509_b64.h>
#include <gnutls_x509.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/* x509 */
#include "common.h"
#include "x509.h"
#include "verify.h"
#include "mpi.h"
#include "privkey.h"


/*
 * some x509 certificate parsing functions.
 */

/* Check if the number of bits of the key in the certificate
 * is unacceptable.
  */
inline static int
check_bits (MHD_gnutls_x509_crt_t crt, unsigned int max_bits)
{
  int ret;
  unsigned int bits;

  ret = MHD_gnutls_x509_crt_get_pk_algorithm (crt, &bits);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  if (bits > max_bits && max_bits > 0)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_CONSTRAINT_ERROR;
    }

  return 0;
}


#define CLEAR_CERTS for(x=0;x<peer_certificate_list_size;x++) { \
	if (peer_certificate_list[x]) \
		MHD_gnutls_x509_crt_deinit(peer_certificate_list[x]); \
	} \
	MHD_gnutls_free( peer_certificate_list)

/*-
  * MHD__gnutls_x509_cert_verify_peers - This function returns the peer's certificate status
  * @session: is a gnutls session
  *
  * This function will try to verify the peer's certificate and return its status (TRUSTED, REVOKED etc.).
  * The return value (status) should be one of the MHD_gnutls_certificate_status_t enumerated elements.
  * However you must also check the peer's name in order to check if the verified certificate belongs to the
  * actual peer. Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
  *
  -*/
int
MHD__gnutls_x509_cert_verify_peers (MHD_gtls_session_t session,
                                    unsigned int *status)
{
  cert_auth_info_t info;
  MHD_gtls_cert_credentials_t cred;
  MHD_gnutls_x509_crt_t *peer_certificate_list;
  int peer_certificate_list_size, i, x, ret;

  CHECK_AUTH (MHD_GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);

  info = MHD_gtls_get_auth_info (session);
  if (info == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_INVALID_REQUEST;
    }

  cred = (MHD_gtls_cert_credentials_t)
    MHD_gtls_get_cred (session->key, MHD_GNUTLS_CRD_CERTIFICATE, NULL);
  if (cred == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
    }

  if (info->raw_certificate_list == NULL || info->ncerts == 0)
    return GNUTLS_E_NO_CERTIFICATE_FOUND;

  if (info->ncerts > cred->verify_depth && cred->verify_depth > 0)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_CONSTRAINT_ERROR;
    }

  /* generate a list of MHD_gnutls_certs based on the auth info
   * raw certs.
   */
  peer_certificate_list_size = info->ncerts;
  peer_certificate_list =
    MHD_gnutls_calloc (peer_certificate_list_size,
                       sizeof (MHD_gnutls_x509_crt_t));
  if (peer_certificate_list == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }

  for (i = 0; i < peer_certificate_list_size; i++)
    {
      ret = MHD_gnutls_x509_crt_init (&peer_certificate_list[i]);
      if (ret < 0)
        {
          MHD_gnutls_assert ();
          CLEAR_CERTS;
          return ret;
        }

      ret =
        MHD_gnutls_x509_crt_import (peer_certificate_list[i],
                                    &info->raw_certificate_list[i],
                                    GNUTLS_X509_FMT_DER);
      if (ret < 0)
        {
          MHD_gnutls_assert ();
          CLEAR_CERTS;
          return ret;
        }

      ret = check_bits (peer_certificate_list[i], cred->verify_bits);
      if (ret < 0)
        {
          MHD_gnutls_assert ();
          CLEAR_CERTS;
          return ret;
        }

    }

  /* Verify certificate
   */
  ret =
    MHD_gnutls_x509_crt_list_verify (peer_certificate_list,
                                     peer_certificate_list_size,
                                     cred->x509_ca_list, cred->x509_ncas,
                                     cred->x509_crl_list, cred->x509_ncrls,
                                     cred->verify_flags, status);

  CLEAR_CERTS;

  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  return 0;
}

/*
 * Read certificates and private keys, from memory etc.
 */

/* returns error if the certificate has different algorithm than
 * the given key parameters.
 */
static int
MHD__gnutls_check_key_cert_match (MHD_gtls_cert_credentials_t res)
{
  MHD_gnutls_datum_t cid;
  MHD_gnutls_datum_t kid;
  unsigned pk = res->cert_list[res->ncerts - 1][0].subject_pk_algorithm;

  if (res->pkey[res->ncerts - 1].pk_algorithm != pk)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
    }

  MHD__gnutls_x509_write_rsa_params (res->pkey[res->ncerts - 1].params,
                                     res->pkey[res->ncerts -
                                               1].params_size, &kid);


  MHD__gnutls_x509_write_rsa_params (res->cert_list[res->ncerts - 1][0].
                                     params,
                                     res->cert_list[res->ncerts -
                                                    1][0].params_size, &cid);

  if (cid.size != kid.size)
    {
      MHD_gnutls_assert ();
      MHD__gnutls_free_datum (&kid);
      MHD__gnutls_free_datum (&cid);
      return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
    }

  if (memcmp (kid.data, cid.data, kid.size) != 0)
    {
      MHD_gnutls_assert ();
      MHD__gnutls_free_datum (&kid);
      MHD__gnutls_free_datum (&cid);
      return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
    }

  MHD__gnutls_free_datum (&kid);
  MHD__gnutls_free_datum (&cid);
  return 0;
}

/* Reads a DER encoded certificate list from memory and stores it to
 * a MHD_gnutls_cert structure.
 * Returns the number of certificates parsed.
 */
static int
parse_crt_mem (MHD_gnutls_cert ** cert_list, unsigned *ncerts,
               MHD_gnutls_x509_crt_t cert)
{
  int i;
  int ret;

  i = *ncerts + 1;

  *cert_list =
    (MHD_gnutls_cert *) MHD_gtls_realloc_fast (*cert_list,
                                               i * sizeof (MHD_gnutls_cert));

  if (*cert_list == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }

  ret = MHD_gtls_x509_crt_to_gcert (&cert_list[0][i - 1], cert, 0);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  *ncerts = i;

  return 1;                     /* one certificate parsed */
}

/* Reads a DER encoded certificate list from memory and stores it to
 * a MHD_gnutls_cert structure.
 * Returns the number of certificates parsed.
 */
static int
parse_der_cert_mem (MHD_gnutls_cert ** cert_list, unsigned *ncerts,
                    const void *input_cert, int input_cert_size)
{
  MHD_gnutls_datum_t tmp;
  MHD_gnutls_x509_crt_t cert;
  int ret;

  ret = MHD_gnutls_x509_crt_init (&cert);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  tmp.data = (opaque *) input_cert;
  tmp.size = input_cert_size;

  ret = MHD_gnutls_x509_crt_import (cert, &tmp, GNUTLS_X509_FMT_DER);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      MHD_gnutls_x509_crt_deinit (cert);
      return ret;
    }

  ret = parse_crt_mem (cert_list, ncerts, cert);
  MHD_gnutls_x509_crt_deinit (cert);

  return ret;
}

/* Reads a base64 encoded certificate list from memory and stores it to
 * a MHD_gnutls_cert structure. Returns the number of certificate parsed.
 */
static int
parse_pem_cert_mem (MHD_gnutls_cert ** cert_list, unsigned *ncerts,
                    const char *input_cert, int input_cert_size)
{
  int size, siz2, i;
  const char *ptr;
  opaque *ptr2;
  MHD_gnutls_datum_t tmp;
  int ret, count;

  /* move to the certificate
   */
  ptr = memmem (input_cert, input_cert_size,
                PEM_CERT_SEP, sizeof (PEM_CERT_SEP) - 1);
  if (ptr == NULL)
    ptr = memmem (input_cert, input_cert_size,
                  PEM_CERT_SEP2, sizeof (PEM_CERT_SEP2) - 1);

  if (ptr == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_BASE64_DECODING_ERROR;
    }
  size = input_cert_size - (ptr - input_cert);

  i = *ncerts + 1;
  count = 0;

  do
    {

      siz2 =
        MHD__gnutls_fbase64_decode (NULL, (const unsigned char *) ptr, size,
                                    &ptr2);

      if (siz2 < 0)
        {
          MHD_gnutls_assert ();
          return GNUTLS_E_BASE64_DECODING_ERROR;
        }

      *cert_list =
        (MHD_gnutls_cert *) MHD_gtls_realloc_fast (*cert_list,
                                                   i *
                                                   sizeof (MHD_gnutls_cert));

      if (*cert_list == NULL)
        {
          MHD_gnutls_assert ();
          return GNUTLS_E_MEMORY_ERROR;
        }

      tmp.data = ptr2;
      tmp.size = siz2;

      ret = MHD_gtls_x509_raw_cert_to_gcert (&cert_list[0][i - 1], &tmp, 0);
      if (ret < 0)
        {
          MHD_gnutls_assert ();
          return ret;
        }
      MHD__gnutls_free_datum (&tmp);    /* free ptr2 */

      /* now we move ptr after the pem header
       */
      ptr++;
      /* find the next certificate (if any)
       */
      size = input_cert_size - (ptr - input_cert);

      if (size > 0)
        {
          char *ptr3;

          ptr3 = memmem (ptr, size, PEM_CERT_SEP, sizeof (PEM_CERT_SEP) - 1);
          if (ptr3 == NULL)
            ptr3 = memmem (ptr, size, PEM_CERT_SEP2,
                           sizeof (PEM_CERT_SEP2) - 1);

          ptr = ptr3;
        }
      else
        ptr = NULL;

      i++;
      count++;

    }
  while (ptr != NULL);

  *ncerts = i - 1;

  return count;
}



/* Reads a DER or PEM certificate from memory
 */
static int
read_cert_mem (MHD_gtls_cert_credentials_t res, const void *cert,
               int cert_size, MHD_gnutls_x509_crt_fmt_t type)
{
  int ret;

  /* allocate space for the certificate to add
   */
  res->cert_list = MHD_gtls_realloc_fast (res->cert_list,
                                          (1 +
                                           res->ncerts) *
                                          sizeof (MHD_gnutls_cert *));
  if (res->cert_list == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }

  res->cert_list_length = MHD_gtls_realloc_fast (res->cert_list_length,
                                                 (1 +
                                                  res->ncerts) *
                                                 sizeof (int));
  if (res->cert_list_length == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }

  res->cert_list[res->ncerts] = NULL;   /* for realloc */
  res->cert_list_length[res->ncerts] = 0;

  if (type == GNUTLS_X509_FMT_DER)
    ret = parse_der_cert_mem (&res->cert_list[res->ncerts],
                              &res->cert_list_length[res->ncerts],
                              cert, cert_size);
  else
    ret =
      parse_pem_cert_mem (&res->cert_list[res->ncerts],
                          &res->cert_list_length[res->ncerts], cert,
                          cert_size);

  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  return ret;
}


int
MHD__gnutls_x509_privkey_to_gkey (MHD_gnutls_privkey * dest,
                                  MHD_gnutls_x509_privkey_t src)
{
  int i, ret;

  memset (dest, 0, sizeof (MHD_gnutls_privkey));

  for (i = 0; i < src->params_size; i++)
    {
      dest->params[i] = MHD__gnutls_mpi_copy (src->params[i]);
      if (dest->params[i] == NULL)
        {
          MHD_gnutls_assert ();
          ret = GNUTLS_E_MEMORY_ERROR;
          goto cleanup;
        }
    }

  dest->pk_algorithm = src->pk_algorithm;
  dest->params_size = src->params_size;

  return 0;

cleanup:

  for (i = 0; i < src->params_size; i++)
    {
      MHD_gtls_mpi_release (&dest->params[i]);
    }
  return ret;
}

void
MHD_gtls_gkey_deinit (MHD_gnutls_privkey * key)
{
  int i;
  if (key == NULL)
    return;

  for (i = 0; i < key->params_size; i++)
    {
      MHD_gtls_mpi_release (&key->params[i]);
    }
}

int
MHD__gnutls_x509_raw_privkey_to_gkey (MHD_gnutls_privkey * privkey,
                                      const MHD_gnutls_datum_t * raw_key,
                                      MHD_gnutls_x509_crt_fmt_t type)
{
  MHD_gnutls_x509_privkey_t tmpkey;
  int ret;

  ret = MHD_gnutls_x509_privkey_init (&tmpkey);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  ret = MHD_gnutls_x509_privkey_import (tmpkey, raw_key, type);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      MHD_gnutls_x509_privkey_deinit (tmpkey);
      return ret;
    }

  ret = MHD__gnutls_x509_privkey_to_gkey (privkey, tmpkey);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      MHD_gnutls_x509_privkey_deinit (tmpkey);
      return ret;
    }

  MHD_gnutls_x509_privkey_deinit (tmpkey);

  return 0;
}

/* Reads a PEM encoded PKCS-1 RSA/DSA private key from memory.  Type
 * indicates the certificate format.  KEY can be NULL, to indicate
 * that GnuTLS doesn't know the private key.
 */
static int
read_key_mem (MHD_gtls_cert_credentials_t res,
              const void *key, int key_size, MHD_gnutls_x509_crt_fmt_t type)
{
  int ret;
  MHD_gnutls_datum_t tmp;

  /* allocate space for the pkey list
   */
  res->pkey =
    MHD_gtls_realloc_fast (res->pkey,
                           (res->ncerts + 1) * sizeof (MHD_gnutls_privkey));
  if (res->pkey == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }

  if (key)
    {
      tmp.data = (opaque *) key;
      tmp.size = key_size;

      ret =
        MHD__gnutls_x509_raw_privkey_to_gkey (&res->pkey[res->ncerts], &tmp,
                                              type);
      if (ret < 0)
        {
          MHD_gnutls_assert ();
          return ret;
        }
    }
  else
    memset (&res->pkey[res->ncerts], 0, sizeof (MHD_gnutls_privkey));

  return 0;
}

/**
  * MHD__gnutls_certificate_set_x509_key_mem - Used to set keys in a MHD_gtls_cert_credentials_t structure
  * @res: is an #MHD_gtls_cert_credentials_t structure.
  * @cert: contains a certificate list (path) for the specified private key
  * @key: is the private key, or %NULL
  * @type: is PEM or DER
  *
  * This function sets a certificate/private key pair in the
  * MHD_gtls_cert_credentials_t structure. This function may be called
  * more than once (in case multiple keys/certificates exist for the
  * server).
  *
  * Currently are supported: RSA PKCS-1 encoded private keys,
  * DSA private keys.
  *
  * DSA private keys are encoded the OpenSSL way, which is an ASN.1
  * DER sequence of 6 INTEGERs - version, p, q, g, pub, priv.
  *
  * Note that the keyUsage (2.5.29.15) PKIX extension in X.509 certificates
  * is supported. This means that certificates intended for signing cannot
  * be used for ciphersuites that require encryption.
  *
  * If the certificate and the private key are given in PEM encoding
  * then the strings that hold their values must be null terminated.
  *
  * The @key may be %NULL if you are using a sign callback, see
  * MHD_gtls_sign_callback_set().
  *
  * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
  **/
int
MHD__gnutls_certificate_set_x509_key_mem (MHD_gtls_cert_credentials_t
                                          res,
                                          const MHD_gnutls_datum_t * cert,
                                          const MHD_gnutls_datum_t * key,
                                          MHD_gnutls_x509_crt_fmt_t type)
{
  int ret;

  /* this should be first
   */
  if ((ret = read_key_mem (res, key ? key->data : NULL,
                           key ? key->size : 0, type)) < 0)
    return ret;

  if ((ret = read_cert_mem (res, cert->data, cert->size, type)) < 0)
    return ret;

  res->ncerts++;

  if (key && (ret = MHD__gnutls_check_key_cert_match (res)) < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  return 0;
}

static int
generate_rdn_seq (MHD_gtls_cert_credentials_t res)
{
  MHD_gnutls_datum_t tmp;
  int ret;
  unsigned size, i;
  opaque *pdata;

  /* Generate the RDN sequence
   * This will be sent to clients when a certificate
   * request message is sent.
   */

  /* FIXME: in case of a client it is not needed
   * to do that. This would save time and memory.
   * However we don't have that information available
   * here.
   */

  size = 0;
  for (i = 0; i < res->x509_ncas; i++)
    {
      if ((ret =
           MHD_gnutls_x509_crt_get_raw_dn (res->x509_ca_list[i], &tmp)) < 0)
        {
          MHD_gnutls_assert ();
          return ret;
        }
      size += (2 + tmp.size);
      MHD__gnutls_free_datum (&tmp);
    }

  if (res->x509_rdn_sequence.data != NULL)
    MHD_gnutls_free (res->x509_rdn_sequence.data);

  res->x509_rdn_sequence.data = MHD_gnutls_malloc (size);
  if (res->x509_rdn_sequence.data == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }
  res->x509_rdn_sequence.size = size;

  pdata = res->x509_rdn_sequence.data;

  for (i = 0; i < res->x509_ncas; i++)
    {
      if ((ret =
           MHD_gnutls_x509_crt_get_raw_dn (res->x509_ca_list[i], &tmp)) < 0)
        {
          MHD__gnutls_free_datum (&res->x509_rdn_sequence);
          MHD_gnutls_assert ();
          return ret;
        }

      MHD_gtls_write_datum16 (pdata, tmp);
      pdata += (2 + tmp.size);
      MHD__gnutls_free_datum (&tmp);
    }

  return 0;
}

/* Returns 0 if it's ok to use the enum MHD_GNUTLS_KeyExchangeAlgorithm with this
 * certificate (uses the KeyUsage field).
 */
int
MHD__gnutls_check_key_usage (const MHD_gnutls_cert * cert,
                             enum MHD_GNUTLS_KeyExchangeAlgorithm alg)
{
  unsigned int key_usage = 0;
  int encipher_type;

  if (cert == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_INTERNAL_ERROR;
    }

  if (MHD_gtls_map_kx_get_cred (alg, 1) == MHD_GNUTLS_CRD_CERTIFICATE ||
      MHD_gtls_map_kx_get_cred (alg, 0) == MHD_GNUTLS_CRD_CERTIFICATE)
    {

      key_usage = cert->key_usage;

      encipher_type = MHD_gtls_kx_encipher_type (alg);

      if (key_usage != 0 && encipher_type != CIPHER_IGN)
        {
          /* If key_usage has been set in the certificate
           */

          if (encipher_type == CIPHER_ENCRYPT)
            {
              /* If the key exchange method requires an encipher
               * type algorithm, and key's usage does not permit
               * encipherment, then fail.
               */
              if (!(key_usage & KEY_KEY_ENCIPHERMENT))
                {
                  MHD_gnutls_assert ();
                  return GNUTLS_E_KEY_USAGE_VIOLATION;
                }
            }

          if (encipher_type == CIPHER_SIGN)
            {
              /* The same as above, but for sign only keys
               */
              if (!(key_usage & KEY_DIGITAL_SIGNATURE))
                {
                  MHD_gnutls_assert ();
                  return GNUTLS_E_KEY_USAGE_VIOLATION;
                }
            }
        }
    }
  return 0;
}



static int
parse_pem_ca_mem (MHD_gnutls_x509_crt_t ** cert_list, unsigned *ncerts,
                  const opaque * input_cert, int input_cert_size)
{
  int i, size;
  const opaque *ptr;
  MHD_gnutls_datum_t tmp;
  int ret, count;

  /* move to the certificate
   */
  ptr = memmem (input_cert, input_cert_size,
                PEM_CERT_SEP, sizeof (PEM_CERT_SEP) - 1);
  if (ptr == NULL)
    ptr = memmem (input_cert, input_cert_size,
                  PEM_CERT_SEP2, sizeof (PEM_CERT_SEP2) - 1);

  if (ptr == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_BASE64_DECODING_ERROR;
    }
  size = input_cert_size - (ptr - input_cert);

  i = *ncerts + 1;
  count = 0;

  do
    {

      *cert_list =
        (MHD_gnutls_x509_crt_t *) MHD_gtls_realloc_fast (*cert_list,
                                                         i *
                                                         sizeof
                                                         (MHD_gnutls_x509_crt_t));

      if (*cert_list == NULL)
        {
          MHD_gnutls_assert ();
          return GNUTLS_E_MEMORY_ERROR;
        }

      ret = MHD_gnutls_x509_crt_init (&cert_list[0][i - 1]);
      if (ret < 0)
        {
          MHD_gnutls_assert ();
          return ret;
        }

      tmp.data = (opaque *) ptr;
      tmp.size = size;

      ret =
        MHD_gnutls_x509_crt_import (cert_list[0][i - 1],
                                    &tmp, GNUTLS_X509_FMT_PEM);
      if (ret < 0)
        {
          MHD_gnutls_assert ();
          return ret;
        }

      /* now we move ptr after the pem header
       */
      ptr++;
      size--;
      /* find the next certificate (if any)
       */

      if (size > 0)
        {
          char *ptr3;

          ptr3 = memmem (ptr, size, PEM_CERT_SEP, sizeof (PEM_CERT_SEP) - 1);
          if (ptr3 == NULL)
            ptr3 = memmem (ptr, size,
                           PEM_CERT_SEP2, sizeof (PEM_CERT_SEP2) - 1);

          ptr = (const opaque *) ptr3;
          size = input_cert_size - (ptr - input_cert);
        }
      else
        ptr = NULL;

      i++;
      count++;

    }
  while (ptr != NULL);

  *ncerts = i - 1;

  return count;
}

/* Reads a DER encoded certificate list from memory and stores it to
 * a MHD_gnutls_cert structure.
 * returns the number of certificates parsed.
 */
static int
parse_der_ca_mem (MHD_gnutls_x509_crt_t ** cert_list, unsigned *ncerts,
                  const void *input_cert, int input_cert_size)
{
  int i;
  MHD_gnutls_datum_t tmp;
  int ret;

  i = *ncerts + 1;

  *cert_list =
    (MHD_gnutls_x509_crt_t *) MHD_gtls_realloc_fast (*cert_list,
                                                     i *
                                                     sizeof
                                                     (MHD_gnutls_x509_crt_t));

  if (*cert_list == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }

  tmp.data = (opaque *) input_cert;
  tmp.size = input_cert_size;

  ret = MHD_gnutls_x509_crt_init (&cert_list[0][i - 1]);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  ret =
    MHD_gnutls_x509_crt_import (cert_list[0][i - 1], &tmp,
                                GNUTLS_X509_FMT_DER);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  *ncerts = i;

  return 1;                     /* one certificate parsed */
}

/**
  * MHD__gnutls_certificate_set_x509_trust_mem - Used to add trusted CAs in a MHD_gtls_cert_credentials_t structure
  * @res: is an #MHD_gtls_cert_credentials_t structure.
  * @ca: is a list of trusted CAs or a DER certificate
  * @type: is DER or PEM
  *
  * This function adds the trusted CAs in order to verify client or
  * server certificates. In case of a client this is not required to
  * be called if the certificates are not verified using
  * MHD_gtls_certificate_verify_peers2().  This function may be called
  * multiple times.
  *
  * In case of a server the CAs set here will be sent to the client if
  * a certificate request is sent. This can be disabled using
  * MHD__gnutls_certificate_send_x509_rdn_sequence().
  *
  * Returns: the number of certificates processed or a negative value
  * on error.
  **/
int
MHD__gnutls_certificate_set_x509_trust_mem (MHD_gtls_cert_credentials_t
                                            res,
                                            const MHD_gnutls_datum_t * ca,
                                            MHD_gnutls_x509_crt_fmt_t type)
{
  int ret, ret2;

  if (type == GNUTLS_X509_FMT_DER)
    ret = parse_der_ca_mem (&res->x509_ca_list, &res->x509_ncas,
                            ca->data, ca->size);
  else
    ret = parse_pem_ca_mem (&res->x509_ca_list, &res->x509_ncas,
                            ca->data, ca->size);

  if ((ret2 = generate_rdn_seq (res)) < 0)
    return ret2;

  return ret;
}