aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/tls/gnutls_algorithms.c
blob: c3daa08b628d20d548a6e9054045c2508e6d09be (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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
/*
 * Copyright (C) 2000, 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_algorithms.h"
#include "gnutls_errors.h"
#include "gnutls_cert.h"
/* x509 */
#include "common.h"

/* Cred type mappings to KX algorithms
 * FIXME: The mappings are not 1-1. Some KX such as SRP_RSA require
 * more than one credentials type.
 */
typedef struct
{
  enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm;
  enum MHD_GNUTLS_CredentialsType client_type;
  enum MHD_GNUTLS_CredentialsType server_type;  /* The type of credentials a server
                                                 * needs to set */
} gnutls_cred_map;

static const gnutls_cred_map mhd_gtls_cred_mappings[] = {
  {MHD_GNUTLS_KX_ANON_DH,
   MHD_GNUTLS_CRD_ANON,
   MHD_GNUTLS_CRD_ANON},
  {MHD_GNUTLS_KX_RSA,
   MHD_GNUTLS_CRD_CERTIFICATE,
   MHD_GNUTLS_CRD_CERTIFICATE},
  {MHD_GNUTLS_KX_RSA_EXPORT,
   MHD_GNUTLS_CRD_CERTIFICATE,
   MHD_GNUTLS_CRD_CERTIFICATE},
  {MHD_GNUTLS_KX_DHE_DSS,
   MHD_GNUTLS_CRD_CERTIFICATE,
   MHD_GNUTLS_CRD_CERTIFICATE},
  {MHD_GNUTLS_KX_DHE_RSA,
   MHD_GNUTLS_CRD_CERTIFICATE,
   MHD_GNUTLS_CRD_CERTIFICATE},
  {MHD_GNUTLS_KX_SRP,
   MHD_GNUTLS_CRD_SRP,
   MHD_GNUTLS_CRD_SRP},
  {MHD_GNUTLS_KX_SRP_RSA,
   MHD_GNUTLS_CRD_SRP,
   MHD_GNUTLS_CRD_CERTIFICATE},
  {MHD_GNUTLS_KX_SRP_DSS,
   MHD_GNUTLS_CRD_SRP,
   MHD_GNUTLS_CRD_CERTIFICATE},
  {0,
   0,
   0}
};

#define GNUTLS_KX_MAP_LOOP(b) \
        const gnutls_cred_map *p; \
                for(p = mhd_gtls_cred_mappings; p->algorithm != 0; p++) { b ; }

#define GNUTLS_KX_MAP_ALG_LOOP_SERVER(a) \
                        GNUTLS_KX_MAP_LOOP( if(p->server_type == type) { a; break; })

#define GNUTLS_KX_MAP_ALG_LOOP_CLIENT(a) \
                        GNUTLS_KX_MAP_LOOP( if(p->client_type == type) { a; break; })

/* KX mappings to PK algorithms */
typedef struct
{
  enum MHD_GNUTLS_KeyExchangeAlgorithm kx_algorithm;
  enum MHD_GNUTLS_PublicKeyAlgorithm pk_algorithm;
  enum encipher_type encipher_type;     /* CIPHER_ENCRYPT if this algorithm is to be used
                                         * for encryption, CIPHER_SIGN if signature only,
                                         * CIPHER_IGN if this does not apply at all.
                                         *
                                         * This is useful to certificate cipher suites, which check
                                         * against the certificate key usage bits.
                                         */
} gnutls_pk_map;

/* This table maps the Key exchange algorithms to
 * the certificate algorithms. Eg. if we have
 * RSA algorithm in the certificate then we can
 * use GNUTLS_KX_RSA or GNUTLS_KX_DHE_RSA.
 */
static const gnutls_pk_map mhd_gtls_pk_mappings[] = {
  {MHD_GNUTLS_KX_RSA,
   MHD_GNUTLS_PK_RSA,
   CIPHER_ENCRYPT},
  {MHD_GNUTLS_KX_RSA_EXPORT,
   MHD_GNUTLS_PK_RSA,
   CIPHER_SIGN},
  {MHD_GNUTLS_KX_DHE_RSA,
   MHD_GNUTLS_PK_RSA,
   CIPHER_SIGN},
  {MHD_GNUTLS_KX_SRP_RSA,
   MHD_GNUTLS_PK_RSA,
   CIPHER_SIGN},
  {0,
   0,
   0}
};

#define GNUTLS_PK_MAP_LOOP(b) \
        const gnutls_pk_map *p; \
                for(p = mhd_gtls_pk_mappings; p->kx_algorithm != 0; p++) { b }

#define GNUTLS_PK_MAP_ALG_LOOP(a) \
                        GNUTLS_PK_MAP_LOOP( if(p->kx_algorithm == kx_algorithm) { a; break; })

/* TLS Versions */

typedef struct
{
  const char *name;
  enum MHD_GNUTLS_Protocol id;  /* gnutls internal version number */
  int major;                    /* defined by the protocol */
  int minor;                    /* defined by the protocol */
  int supported;                /* 0 not supported, > 0 is supported */
} gnutls_version_entry;

static const gnutls_version_entry mhd_gtls_sup_versions[] = {
  {"SSL3.0",
   MHD_GNUTLS_PROTOCOL_SSL3,
   3,
   0,
   1},
  {"TLS1.0",
   MHD_GNUTLS_PROTOCOL_TLS1_0,
   3,
   1,
   1},
  {"TLS1.1",
   MHD_GNUTLS_PROTOCOL_TLS1_1,
   3,
   2,
   1},
  {"TLS1.2",
   MHD_GNUTLS_PROTOCOL_TLS1_2,
   3,
   3,
   1},
  {0,
   0,
   0,
   0,
   0}
};

/* Keep the contents of this struct the same as the previous one. */
static const enum MHD_GNUTLS_Protocol mhd_gtls_supported_protocols[] =
{ MHD_GNUTLS_PROTOCOL_SSL3,
  MHD_GNUTLS_PROTOCOL_TLS1_0,
  MHD_GNUTLS_PROTOCOL_TLS1_1,
  MHD_GNUTLS_PROTOCOL_TLS1_2,
  0
};

#define GNUTLS_VERSION_LOOP(b) \
        const gnutls_version_entry *p; \
                for(p = mhd_gtls_sup_versions; p->name != NULL; p++) { b ; }

#define GNUTLS_VERSION_ALG_LOOP(a) \
                        GNUTLS_VERSION_LOOP( if(p->id == version) { a; break; })

struct gnutls_cipher_entry
{
  const char *name;
  enum MHD_GNUTLS_CipherAlgorithm id;
  uint16_t blocksize;
  uint16_t keysize;
  cipher_type_t block;
  uint16_t iv;
  int export_flag;              /* 0 non export */
};
typedef struct gnutls_cipher_entry gnutls_cipher_entry;

/* Note that all algorithms are in CBC or STREAM modes.
 * Do not add any algorithms in other modes (avoid modified algorithms).
 * View first: "The order of encryption and authentication for
 * protecting communications" by Hugo Krawczyk - CRYPTO 2001
 */
static const gnutls_cipher_entry mhd_gtls_algorithms[] = {
  {"AES-256-CBC",
   MHD_GNUTLS_CIPHER_AES_256_CBC,
   16,
   32,
   CIPHER_BLOCK,
   16,
   0},
  {"AES-128-CBC",
   MHD_GNUTLS_CIPHER_AES_128_CBC,
   16,
   16,
   CIPHER_BLOCK,
   16,
   0},
  {"3DES-CBC",
   MHD_GNUTLS_CIPHER_3DES_CBC,
   8,
   24,
   CIPHER_BLOCK,
   8,
   0},
  {"DES-CBC",
   MHD_GNUTLS_CIPHER_DES_CBC,
   8,
   8,
   CIPHER_BLOCK,
   8,
   0},
  {"ARCFOUR-128",
   MHD_GNUTLS_CIPHER_ARCFOUR_128,
   1,
   16,
   CIPHER_STREAM,
   0,
   0},
  {"ARCFOUR-40",
   MHD_GNUTLS_CIPHER_ARCFOUR_40,
   1,
   5,
   CIPHER_STREAM,
   0,
   1},
  {"RC2-40",
   MHD_GNUTLS_CIPHER_RC2_40_CBC,
   8,
   5,
   CIPHER_BLOCK,
   8,
   1},
#ifdef	ENABLE_CAMELLIA
  {"CAMELLIA-256-CBC", MHD_GNUTLS_CIPHER_CAMELLIA_256_CBC, 16, 32,
   CIPHER_BLOCK,
   16, 0},
  {"CAMELLIA-128-CBC", MHD_GNUTLS_CIPHER_CAMELLIA_128_CBC, 16, 16,
   CIPHER_BLOCK,
   16, 0},
#endif
  {"NULL",
   MHD_GNUTLS_CIPHER_NULL,
   1,
   0,
   CIPHER_STREAM,
   0,
   0},
  {0,
   0,
   0,
   0,
   0,
   0,
   0}
};

/* Keep the contents of this struct the same as the previous one. */
static const enum MHD_GNUTLS_CipherAlgorithm mhd_gtls_supported_ciphers[] =
{ MHD_GNUTLS_CIPHER_AES_256_CBC,
  MHD_GNUTLS_CIPHER_AES_128_CBC,
  MHD_GNUTLS_CIPHER_3DES_CBC,
  MHD_GNUTLS_CIPHER_DES_CBC,
  MHD_GNUTLS_CIPHER_ARCFOUR_128,
  MHD_GNUTLS_CIPHER_ARCFOUR_40,
  MHD_GNUTLS_CIPHER_RC2_40_CBC,
#ifdef	ENABLE_CAMELLIA
  MHD_GNUTLS_CIPHER_CAMELLIA_256_CBC,
  MHD_GNUTLS_CIPHER_CAMELLIA_128_CBC,
#endif
  MHD_GNUTLS_CIPHER_NULL,
  0
};

#define GNUTLS_LOOP(b) \
        const gnutls_cipher_entry *p; \
                for(p = mhd_gtls_algorithms; p->name != NULL; p++) { b ; }

#define GNUTLS_ALG_LOOP(a) \
                        GNUTLS_LOOP( if(p->id == algorithm) { a; break; } )

struct gnutls_hash_entry
{
  const char *name;
  const char *oid;
  enum MHD_GNUTLS_HashAlgorithm id;
  size_t key_size;              /* in case of mac */
};
typedef struct gnutls_hash_entry gnutls_hash_entry;

static const gnutls_hash_entry mhd_gtls_hash_algorithms[] = {
  {"SHA1",
   HASH_OID_SHA1,
   MHD_GNUTLS_MAC_SHA1,
   20},
  {"MD5",
   HASH_OID_MD5,
   MHD_GNUTLS_MAC_MD5,
   16},
  {"SHA256",
   HASH_OID_SHA256,
   MHD_GNUTLS_MAC_SHA256,
   32},
  {"NULL",
   NULL,
   MHD_GNUTLS_MAC_NULL,
   0},
  {0,
   0,
   0,
   0}
};

/* Keep the contents of this struct the same as the previous one. */
static const enum MHD_GNUTLS_HashAlgorithm mhd_gtls_supported_macs[] =
{ MHD_GNUTLS_MAC_SHA1,
  MHD_GNUTLS_MAC_MD5,
  MHD_GNUTLS_MAC_SHA256,
  MHD_GNUTLS_MAC_NULL,
  0
};

#define GNUTLS_HASH_LOOP(b) \
        const gnutls_hash_entry *p; \
                for(p = mhd_gtls_hash_algorithms; p->name != NULL; p++) { b ; }

#define GNUTLS_HASH_ALG_LOOP(a) \
                        GNUTLS_HASH_LOOP( if(p->id == algorithm) { a; break; } )

/* Compression Section */
#define GNUTLS_COMPRESSION_ENTRY(name, id, wb, ml, cl) \
	{ #name, name, id, wb, ml, cl}

#define MAX_COMP_METHODS 5
const int _gnutls_comp_algorithms_size = MAX_COMP_METHODS;

/* the compression entry is defined in gnutls_algorithms.h */

gnutls_compression_entry _gnutls_compression_algorithms[MAX_COMP_METHODS] =
  { GNUTLS_COMPRESSION_ENTRY (MHD_GNUTLS_COMP_NULL, 0x00, 0, 0, 0),
#ifdef HAVE_LIBZ
  /* draft-ietf-tls-compression-02 */
  GNUTLS_COMPRESSION_ENTRY (MHD_GNUTLS_COMP_DEFLATE, 0x01, 15, 8, 3),
#endif
  {0,
   0,
   0,
   0,
   0,
   0}
};

static const enum MHD_GNUTLS_CompressionMethod
  mhd_gtls_supported_compressions[] =
{
#ifdef HAVE_LIBZ
  MHD_GNUTLS_COMP_DEFLATE,
#endif
  MHD_GNUTLS_COMP_NULL,
  0
};

#define GNUTLS_COMPRESSION_LOOP(b) \
        const gnutls_compression_entry *p; \
                for(p = _gnutls_compression_algorithms; p->name != NULL; p++) { b ; }
#define GNUTLS_COMPRESSION_ALG_LOOP(a) \
                        GNUTLS_COMPRESSION_LOOP( if(p->id == algorithm) { a; break; } )
#define GNUTLS_COMPRESSION_ALG_LOOP_NUM(a) \
                        GNUTLS_COMPRESSION_LOOP( if(p->num == num) { a; break; } )

/* Key Exchange Section */
extern mhd_gtls_mod_auth_st mhd_gtls_rsa_auth_struct;
extern mhd_gtls_mod_auth_st rsa_export_auth_struct;
extern mhd_gtls_mod_auth_st mhd_gtls_dhe_rsa_auth_struct;
extern mhd_gtls_mod_auth_st mhd_gtls_dhe_dss_auth_struct;
extern mhd_gtls_mod_auth_st mhd_gtls_anon_auth_struct;
extern mhd_gtls_mod_auth_st srp_auth_struct;
extern mhd_gtls_mod_auth_st psk_auth_struct;
extern mhd_gtls_mod_auth_st dhe_psk_auth_struct;
extern mhd_gtls_mod_auth_st srp_rsa_auth_struct;
extern mhd_gtls_mod_auth_st srp_dss_auth_struct;

typedef struct mhd_gtls_kx_algo_entry
{
  const char *name;
  enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm;
  mhd_gtls_mod_auth_st *auth_struct;
  int needs_dh_params;
  int needs_rsa_params;
} mhd_gtls_kx_algo_entry_t;

static const mhd_gtls_kx_algo_entry_t mhd_gtls_kx_algorithms[] = {
#ifdef ENABLE_ANON
  {"ANON-DH", MHD_GNUTLS_KX_ANON_DH, &mhd_gtls_anon_auth_struct, 1, 0},
#endif
  {"RSA",
   MHD_GNUTLS_KX_RSA,
   &mhd_gtls_rsa_auth_struct,
   0,
   0},
  {"RSA-EXPORT",
   MHD_GNUTLS_KX_RSA_EXPORT,
   &rsa_export_auth_struct,
   0,
   1 /* needs RSA params */ },
  {"DHE-RSA",
   MHD_GNUTLS_KX_DHE_RSA,
   &mhd_gtls_dhe_rsa_auth_struct,
   1,
   0},
  {"DHE-DSS",
   MHD_GNUTLS_KX_DHE_DSS,
   &mhd_gtls_dhe_dss_auth_struct,
   1,
   0},

#ifdef ENABLE_SRP
  {"SRP-DSS", MHD_GNUTLS_KX_SRP_DSS, &srp_dss_auth_struct, 0, 0},
  {"SRP-RSA", MHD_GNUTLS_KX_SRP_RSA, &srp_rsa_auth_struct, 0, 0},
  {"SRP", MHD_GNUTLS_KX_SRP, &srp_auth_struct, 0, 0},
#endif
#ifdef ENABLE_PSK
  {"PSK", GNUTLS_KX_PSK, &psk_auth_struct, 0, 0},
  {"DHE-PSK", GNUTLS_KX_DHE_PSK, &dhe_psk_auth_struct,
   1 /* needs DHE params */ , 0},
#endif
  {0,
   0,
   0,
   0,
   0}
};

/* Keep the contents of this struct the same as the previous one. */
static const enum MHD_GNUTLS_KeyExchangeAlgorithm mhd_gtls_supported_kxs[] =
{
#ifdef ENABLE_ANON
  MHD_GNUTLS_KX_ANON_DH,
#endif
  MHD_GNUTLS_KX_RSA,
  MHD_GNUTLS_KX_RSA_EXPORT,
  MHD_GNUTLS_KX_DHE_RSA,
  MHD_GNUTLS_KX_DHE_DSS,
#ifdef ENABLE_SRP
  MHD_GNUTLS_KX_SRP_DSS,
  MHD_GNUTLS_KX_SRP_RSA,
  MHD_GNUTLS_KX_SRP,
#endif
#ifdef ENABLE_PSK
  GNUTLS_KX_PSK,
  GNUTLS_KX_DHE_PSK,
#endif
  0
};

#define GNUTLS_KX_LOOP(b) \
        const mhd_gtls_kx_algo_entry_t *p; \
                for(p = mhd_gtls_kx_algorithms; p->name != NULL; p++) { b ; }

#define GNUTLS_KX_ALG_LOOP(a) \
                        GNUTLS_KX_LOOP( if(p->algorithm == algorithm) { a; break; } )

/* Cipher SUITES */
#define GNUTLS_CIPHER_SUITE_ENTRY( name, block_algorithm, kx_algorithm, mac_algorithm, version ) \
	{ #name, {name}, block_algorithm, kx_algorithm, mac_algorithm, version }

typedef struct
{
  const char *name;
  cipher_suite_st id;
  enum MHD_GNUTLS_CipherAlgorithm block_algorithm;
  enum MHD_GNUTLS_KeyExchangeAlgorithm kx_algorithm;
  enum MHD_GNUTLS_HashAlgorithm mac_algorithm;
  enum MHD_GNUTLS_Protocol version;     /* this cipher suite is supported
                                         * from 'version' and above;
                                         */
} mhd_gtls_cipher_suite_entry;

/* RSA with NULL cipher and MD5 MAC
 * for test purposes.
 */
#define GNUTLS_RSA_NULL_MD5 { 0x00, 0x01 }

/* ANONymous cipher suites.
 */

#define GNUTLS_ANON_DH_3DES_EDE_CBC_SHA1 { 0x00, 0x1B }
#define GNUTLS_ANON_DH_ARCFOUR_MD5 { 0x00, 0x18 }

/* rfc3268: */
#define GNUTLS_ANON_DH_AES_128_CBC_SHA1 { 0x00, 0x34 }
#define GNUTLS_ANON_DH_AES_256_CBC_SHA1 { 0x00, 0x3A }

/* rfc4132 */
#define GNUTLS_ANON_DH_CAMELLIA_128_CBC_SHA1 { 0x00,0x46 }
#define GNUTLS_ANON_DH_CAMELLIA_256_CBC_SHA1 { 0x00,0x89 }

/* PSK (not in TLS 1.0)
 * draft-ietf-tls-psk:
 */
#define GNUTLS_PSK_SHA_ARCFOUR_SHA1 { 0x00, 0x8A }
#define GNUTLS_PSK_SHA_3DES_EDE_CBC_SHA1 { 0x00, 0x8B }
#define GNUTLS_PSK_SHA_AES_128_CBC_SHA1 { 0x00, 0x8C }
#define GNUTLS_PSK_SHA_AES_256_CBC_SHA1 { 0x00, 0x8D }

#define GNUTLS_DHE_PSK_SHA_ARCFOUR_SHA1 { 0x00, 0x8E }
#define GNUTLS_DHE_PSK_SHA_3DES_EDE_CBC_SHA1 { 0x00, 0x8F }
#define GNUTLS_DHE_PSK_SHA_AES_128_CBC_SHA1 { 0x00, 0x90 }
#define GNUTLS_DHE_PSK_SHA_AES_256_CBC_SHA1 { 0x00, 0x91 }

/* SRP (rfc5054)
 */
#define GNUTLS_SRP_SHA_3DES_EDE_CBC_SHA1 { 0xC0, 0x1A }
#define GNUTLS_SRP_SHA_RSA_3DES_EDE_CBC_SHA1 { 0xC0, 0x1B }
#define GNUTLS_SRP_SHA_DSS_3DES_EDE_CBC_SHA1 { 0xC0, 0x1C }

#define GNUTLS_SRP_SHA_AES_128_CBC_SHA1 { 0xC0, 0x1D }
#define GNUTLS_SRP_SHA_RSA_AES_128_CBC_SHA1 { 0xC0, 0x1E }
#define GNUTLS_SRP_SHA_DSS_AES_128_CBC_SHA1 { 0xC0, 0x1F }

#define GNUTLS_SRP_SHA_AES_256_CBC_SHA1 { 0xC0, 0x20 }
#define GNUTLS_SRP_SHA_RSA_AES_256_CBC_SHA1 { 0xC0, 0x21 }
#define GNUTLS_SRP_SHA_DSS_AES_256_CBC_SHA1 { 0xC0, 0x22 }

/* RSA
 */
#define GNUTLS_RSA_ARCFOUR_SHA1 { 0x00, 0x05 }
#define GNUTLS_RSA_ARCFOUR_MD5 { 0x00, 0x04 }
#define GNUTLS_RSA_3DES_EDE_CBC_SHA1 { 0x00, 0x0A }

#define GNUTLS_RSA_EXPORT_ARCFOUR_40_MD5 { 0x00, 0x03 }

/* rfc3268:
 */
#define GNUTLS_RSA_AES_128_CBC_SHA1 { 0x00, 0x2F }
#define GNUTLS_RSA_AES_256_CBC_SHA1 { 0x00, 0x35 }

/* rfc4132 */
#define GNUTLS_RSA_CAMELLIA_128_CBC_SHA1 { 0x00,0x41 }
#define GNUTLS_RSA_CAMELLIA_256_CBC_SHA1 { 0x00,0x84 }

/* DHE DSS
 */

#define GNUTLS_DHE_DSS_3DES_EDE_CBC_SHA1 { 0x00, 0x13 }

/* draft-ietf-tls-56-bit-ciphersuites-01:
 */
#define GNUTLS_DHE_DSS_ARCFOUR_SHA1 { 0x00, 0x66 }

/* rfc3268:
 */
#define GNUTLS_DHE_DSS_AES_256_CBC_SHA1 { 0x00, 0x38 }
#define GNUTLS_DHE_DSS_AES_128_CBC_SHA1 { 0x00, 0x32 }

/* rfc4132 */
#define GNUTLS_DHE_DSS_CAMELLIA_128_CBC_SHA1 { 0x00,0x44 }
#define GNUTLS_DHE_DSS_CAMELLIA_256_CBC_SHA1 { 0x00,0x87 }

/* DHE RSA
 */
#define GNUTLS_DHE_RSA_3DES_EDE_CBC_SHA1 { 0x00, 0x16 }

/* rfc3268:
 */
#define GNUTLS_DHE_RSA_AES_128_CBC_SHA1 { 0x00, 0x33 }
#define GNUTLS_DHE_RSA_AES_256_CBC_SHA1 { 0x00, 0x39 }

/* rfc4132 */
#define GNUTLS_DHE_RSA_CAMELLIA_128_CBC_SHA1 { 0x00,0x45 }
#define GNUTLS_DHE_RSA_CAMELLIA_256_CBC_SHA1 { 0x00,0x88 }

#define CIPHER_SUITES_COUNT sizeof(mhd_gtls_cs_algorithms)/sizeof(mhd_gtls_cipher_suite_entry)-1

static const mhd_gtls_cipher_suite_entry mhd_gtls_cs_algorithms[] = {
  /* ANON_DH */
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_ARCFOUR_MD5,
                             MHD_GNUTLS_CIPHER_ARCFOUR_128,
                             MHD_GNUTLS_KX_ANON_DH, MHD_GNUTLS_MAC_MD5,
                             MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_3DES_EDE_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_3DES_CBC,
                             MHD_GNUTLS_KX_ANON_DH,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_AES_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_128_CBC,
                             MHD_GNUTLS_KX_ANON_DH,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_AES_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_256_CBC,
                             MHD_GNUTLS_KX_ANON_DH,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
#ifdef	ENABLE_CAMELLIA
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_CAMELLIA_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_CAMELLIA_128_CBC,
                             MHD_GNUTLS_KX_ANON_DH,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_CAMELLIA_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_CAMELLIA_256_CBC,
                             MHD_GNUTLS_KX_ANON_DH,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
#endif

  /* SRP */
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_3DES_EDE_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_3DES_CBC, MHD_GNUTLS_KX_SRP,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_AES_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_128_CBC, MHD_GNUTLS_KX_SRP,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_AES_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_256_CBC, MHD_GNUTLS_KX_SRP,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),

  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_DSS_3DES_EDE_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_3DES_CBC,
                             MHD_GNUTLS_KX_SRP_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),

  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_RSA_3DES_EDE_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_3DES_CBC,
                             MHD_GNUTLS_KX_SRP_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),

  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_DSS_AES_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_128_CBC,
                             MHD_GNUTLS_KX_SRP_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),

  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_RSA_AES_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_128_CBC,
                             MHD_GNUTLS_KX_SRP_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),

  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_DSS_AES_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_256_CBC,
                             MHD_GNUTLS_KX_SRP_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),

  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_RSA_AES_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_256_CBC,
                             MHD_GNUTLS_KX_SRP_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),

  /* DHE_DSS */
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_ARCFOUR_SHA1,
                             MHD_GNUTLS_CIPHER_ARCFOUR_128,
                             MHD_GNUTLS_KX_DHE_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_3DES_EDE_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_3DES_CBC,
                             MHD_GNUTLS_KX_DHE_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_AES_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_128_CBC,
                             MHD_GNUTLS_KX_DHE_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_AES_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_256_CBC,
                             MHD_GNUTLS_KX_DHE_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
#ifdef	ENABLE_CAMELLIA
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_CAMELLIA_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_CAMELLIA_128_CBC,
                             MHD_GNUTLS_KX_DHE_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_CAMELLIA_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_CAMELLIA_256_CBC,
                             MHD_GNUTLS_KX_DHE_DSS,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
#endif
  /* DHE_RSA */
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_3DES_EDE_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_3DES_CBC,
                             MHD_GNUTLS_KX_DHE_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_AES_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_128_CBC,
                             MHD_GNUTLS_KX_DHE_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_AES_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_256_CBC,
                             MHD_GNUTLS_KX_DHE_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
#ifdef	ENABLE_CAMELLIA
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_CAMELLIA_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_CAMELLIA_128_CBC,
                             MHD_GNUTLS_KX_DHE_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_CAMELLIA_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_CAMELLIA_256_CBC,
                             MHD_GNUTLS_KX_DHE_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
#endif
  /* RSA */
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_NULL_MD5,
                             MHD_GNUTLS_CIPHER_NULL,
                             MHD_GNUTLS_KX_RSA, MHD_GNUTLS_MAC_MD5,
                             MHD_GNUTLS_PROTOCOL_SSL3),

  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_EXPORT_ARCFOUR_40_MD5,
                             MHD_GNUTLS_CIPHER_ARCFOUR_40,
                             MHD_GNUTLS_KX_RSA_EXPORT, MHD_GNUTLS_MAC_MD5,
                             MHD_GNUTLS_PROTOCOL_SSL3),

  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_ARCFOUR_SHA1,
                             MHD_GNUTLS_CIPHER_ARCFOUR_128,
                             MHD_GNUTLS_KX_RSA, MHD_GNUTLS_MAC_SHA1,
                             MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_ARCFOUR_MD5,
                             MHD_GNUTLS_CIPHER_ARCFOUR_128,
                             MHD_GNUTLS_KX_RSA, MHD_GNUTLS_MAC_MD5,
                             MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_3DES_EDE_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_3DES_CBC,
                             MHD_GNUTLS_KX_RSA, MHD_GNUTLS_MAC_SHA1,
                             MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_AES_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_128_CBC, MHD_GNUTLS_KX_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_AES_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_AES_256_CBC, MHD_GNUTLS_KX_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_SSL3),
#ifdef	ENABLE_CAMELLIA
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_CAMELLIA_128_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_CAMELLIA_128_CBC,
                             MHD_GNUTLS_KX_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
  GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_CAMELLIA_256_CBC_SHA1,
                             MHD_GNUTLS_CIPHER_CAMELLIA_256_CBC,
                             MHD_GNUTLS_KX_RSA,
                             MHD_GNUTLS_MAC_SHA1, MHD_GNUTLS_PROTOCOL_TLS1_0),
#endif
  {0,
   {
    {0,
     0}},
   0,
   0,
   0,
   0}
};

#define GNUTLS_CIPHER_SUITE_LOOP(b) \
        const mhd_gtls_cipher_suite_entry *p; \
                for(p = mhd_gtls_cs_algorithms; p->name != NULL; p++) { b ; }

#define GNUTLS_CIPHER_SUITE_ALG_LOOP(a) \
                        GNUTLS_CIPHER_SUITE_LOOP( if( (p->id.suite[0] == suite->suite[0]) && (p->id.suite[1] == suite->suite[1])) { a; break; } )

/* Generic Functions */

int
mhd_gtls_mac_priority (mhd_gtls_session_t session,
                       enum MHD_GNUTLS_HashAlgorithm algorithm)
{                               /* actually returns the priority */
  unsigned int i;
  for (i = 0; i < session->internals.priorities.mac.num_algorithms; i++)
    {
      if (session->internals.priorities.mac.priority[i] == algorithm)
        return i;
    }
  return -1;
}

/**
 * MHD_gnutls_mac_get_name - Returns a string with the name of the specified mac algorithm
 * @algorithm: is a MAC algorithm
 *
 * Returns: a string that contains the name of the specified MAC
 * algorithm, or %NULL.
 **/
const char *
MHD_gnutls_mac_get_name (enum MHD_GNUTLS_HashAlgorithm algorithm)
{
  const char *ret = NULL;

  /* avoid prefix */
  GNUTLS_HASH_ALG_LOOP (ret = p->name);

  return ret;
}

/**
 * MHD_gtls_mac_get_id - Returns the gnutls id of the specified in string algorithm
 * @algorithm: is a MAC algorithm name
 *
 * Returns: an %enum MHD_GNUTLS_HashAlgorithmid of the specified in a string
 * MAC algorithm, or %GNUTLS_MAC_UNKNOWN on failures.  The names are
 * compared in a case insensitive way.
 **/
enum MHD_GNUTLS_HashAlgorithm
MHD_gtls_mac_get_id (const char *name)
{
  enum MHD_GNUTLS_HashAlgorithm ret = MHD_GNUTLS_MAC_UNKNOWN;

  GNUTLS_HASH_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->id)
    ;

  return ret;
}

/**
 * MHD_gnutls_mac_get_key_size - Returns the length of the MAC's key size
 * @algorithm: is an encryption algorithm
 *
 * Returns: length (in bytes) of the given MAC key size, or 0 if the
 * given MAC algorithm is invalid.
 *
 **/
size_t
MHD_gnutls_mac_get_key_size (enum MHD_GNUTLS_HashAlgorithm algorithm)
{
  size_t ret = 0;

  /* avoid prefix */
  GNUTLS_HASH_ALG_LOOP (ret = p->key_size);

  return ret;
}

/**
 * MHD_gtls_mac_list:
 *
 * Get a list of hash algorithms for use as MACs.  Note that not
 * necessarily all MACs are supported in TLS cipher suites.  For
 * example, MD2 is not supported as a cipher suite, but is supported
 * for other purposes (e.g., X.509 signature verification or similar).
 *
 * Returns: Return a zero-terminated list of %enum MHD_GNUTLS_HashAlgorithm
 * integers indicating the available MACs.
 **/
const enum MHD_GNUTLS_HashAlgorithm *
MHD_gtls_mac_list (void)
{
  return mhd_gtls_supported_macs;
}

const char *
mhd_gtls_x509_mac_to_oid (enum MHD_GNUTLS_HashAlgorithm algorithm)
{
  const char *ret = NULL;

  /* avoid prefix */
  GNUTLS_HASH_ALG_LOOP (ret = p->oid);

  return ret;
}

enum MHD_GNUTLS_HashAlgorithm
mhd_gtls_x509_oid2mac_algorithm (const char *oid)
{
  enum MHD_GNUTLS_HashAlgorithm ret = 0;

  GNUTLS_HASH_LOOP (if (p->oid && strcmp (oid, p->oid) == 0)
                    {
                    ret = p->id; break;}
  )
    ;

  if (ret == 0)
    return MHD_GNUTLS_MAC_UNKNOWN;
  return ret;
}

int
mhd_gnutls_mac_is_ok (enum MHD_GNUTLS_HashAlgorithm algorithm)
{
  ssize_t ret = -1;
  GNUTLS_HASH_ALG_LOOP (ret = p->id);
  if (ret >= 0)
    ret = 0;
  else
    ret = 1;
  return ret;
}

/* Compression Functions */
int
mhd_gtls_compression_priority (mhd_gtls_session_t session,
                               enum MHD_GNUTLS_CompressionMethod algorithm)
{                               /* actually returns the priority */
  unsigned int i;
  for (i = 0; i < session->internals.priorities.compression.num_algorithms;
       i++)
    {
      if (session->internals.priorities.compression.priority[i] == algorithm)
        return i;
    }
  return -1;
}

/**
 * MHD_gnutls_compression_get_name - Returns a string with the name of the specified compression algorithm
 * @algorithm: is a Compression algorithm
 *
 * Returns: a pointer to a string that contains the name of the
 * specified compression algorithm, or %NULL.
 **/
const char *
MHD_gtls_compression_get_name (enum MHD_GNUTLS_CompressionMethod algorithm)
{
  const char *ret = NULL;

  /* avoid prefix */
  GNUTLS_COMPRESSION_ALG_LOOP (ret = p->name + sizeof ("GNUTLS_COMP_") - 1);

  return ret;
}

/**
 * MHD_gtls_compression_get_id - Returns the gnutls id of the specified in string algorithm
 * @algorithm: is a compression method name
 *
 * The names are compared in a case insensitive way.
 *
 * Returns: an id of the specified in a string compression method, or
 * %GNUTLS_COMP_UNKNOWN on error.
 *
 **/
enum MHD_GNUTLS_CompressionMethod
MHD_gtls_compression_get_id (const char *name)
{
  enum MHD_GNUTLS_CompressionMethod ret = MHD_GNUTLS_COMP_UNKNOWN;

  GNUTLS_COMPRESSION_LOOP (if
                           (strcasecmp
                            (p->name + sizeof ("GNUTLS_COMP_") - 1,
                             name) == 0) ret = p->id)
    ;

  return ret;
}

/**
 * MHD_gtls_compression_list:
 *
 * Get a list of compression methods.  Note that to be able to use LZO
 * compression, you must link to libgnutls-extra and call
 * gnutls_global_init_extra().
 *
 * Returns: a zero-terminated list of %enum MHD_GNUTLS_CompressionMethod
 * integers indicating the available compression methods.
 **/
const enum MHD_GNUTLS_CompressionMethod *
MHD_gtls_compression_list (void)
{
  return mhd_gtls_supported_compressions;
}

/* return the tls number of the specified algorithm */
int
mhd_gtls_compression_get_num (enum MHD_GNUTLS_CompressionMethod algorithm)
{
  int ret = -1;

  /* avoid prefix */
  GNUTLS_COMPRESSION_ALG_LOOP (ret = p->num);

  return ret;
}

int
mhd_gtls_compression_get_wbits (enum MHD_GNUTLS_CompressionMethod algorithm)
{
  int ret = -1;
  /* avoid prefix */
  GNUTLS_COMPRESSION_ALG_LOOP (ret = p->window_bits);
  return ret;
}

int
mhd_gtls_compression_get_mem_level (enum MHD_GNUTLS_CompressionMethod
                                    algorithm)
{
  int ret = -1;
  /* avoid prefix */
  GNUTLS_COMPRESSION_ALG_LOOP (ret = p->mem_level);
  return ret;
}

int
mhd_gtls_compression_get_comp_level (enum MHD_GNUTLS_CompressionMethod
                                     algorithm)
{
  int ret = -1;
  /* avoid prefix */
  GNUTLS_COMPRESSION_ALG_LOOP (ret = p->comp_level);
  return ret;
}

/* returns the gnutls internal ID of the TLS compression
 * method num
 */
enum MHD_GNUTLS_CompressionMethod
mhd_gtls_compression_get_id (int num)
{
  enum MHD_GNUTLS_CompressionMethod ret = -1;

  /* avoid prefix */
  GNUTLS_COMPRESSION_ALG_LOOP_NUM (ret = p->id);

  return ret;
}

int
mhd_gtls_compression_is_ok (enum MHD_GNUTLS_CompressionMethod algorithm)
{
  ssize_t ret = -1;
  GNUTLS_COMPRESSION_ALG_LOOP (ret = p->id);
  if (ret >= 0)
    ret = 0;
  else
    ret = 1;
  return ret;
}

/* CIPHER functions */
int
mhd_gtls_cipher_get_block_size (enum MHD_GNUTLS_CipherAlgorithm algorithm)
{
  size_t ret = 0;
  GNUTLS_ALG_LOOP (ret = p->blocksize);
  return ret;

}

/* returns the priority */
int
mhd_gtls_cipher_priority (mhd_gtls_session_t session,
                          enum MHD_GNUTLS_CipherAlgorithm algorithm)
{
  unsigned int i;
  for (i = 0; i < session->internals.priorities.cipher.num_algorithms; i++)
    {
      if (session->internals.priorities.cipher.priority[i] == algorithm)
        return i;
    }
  return -1;
}

int
mhd_gtls_cipher_is_block (enum MHD_GNUTLS_CipherAlgorithm algorithm)
{
  size_t ret = 0;

  GNUTLS_ALG_LOOP (ret = p->block);
  return ret;

}

/**
 * MHD_gnutls_cipher_get_key_size - Returns the length of the cipher's key size
 * @algorithm: is an encryption algorithm
 *
 * Returns: length (in bytes) of the given cipher's key size, o 0 if
 *   the given cipher is invalid.
 **/
size_t
MHD_gnutls_cipher_get_key_size (enum MHD_GNUTLS_CipherAlgorithm algorithm)
{                               /* In bytes */
  size_t ret = 0;
  GNUTLS_ALG_LOOP (ret = p->keysize);
  return ret;

}

int
mhd_gtls_cipher_get_iv_size (enum MHD_GNUTLS_CipherAlgorithm algorithm)
{                               /* In bytes */
  size_t ret = 0;
  GNUTLS_ALG_LOOP (ret = p->iv);
  return ret;

}

int
mhd_gtls_cipher_get_export_flag (enum MHD_GNUTLS_CipherAlgorithm algorithm)
{                               /* In bytes */
  size_t ret = 0;
  GNUTLS_ALG_LOOP (ret = p->export_flag);
  return ret;

}

/**
 * MHD_gnutls_cipher_get_name - Returns a string with the name of the specified cipher algorithm
 * @algorithm: is an encryption algorithm
 *
 * Returns: a pointer to a string that contains the name of the
 *   specified cipher, or %NULL.
 **/
const char *
MHD_gnutls_cipher_get_name (enum MHD_GNUTLS_CipherAlgorithm algorithm)
{
  const char *ret = NULL;

  /* avoid prefix */
  GNUTLS_ALG_LOOP (ret = p->name);

  return ret;
}

/**
 * MHD_gtls_cipher_get_id - Returns the gnutls id of the specified in string algorithm
 * @algorithm: is a MAC algorithm name
 *
 * The names are compared in a case insensitive way.
 *
 * Returns: an id of the specified cipher, or %GNUTLS_CIPHER_UNKNOWN
 * on error.
 *
 **/
enum MHD_GNUTLS_CipherAlgorithm
MHD_gtls_cipher_get_id (const char *name)
{
  enum MHD_GNUTLS_CipherAlgorithm ret = MHD_GNUTLS_CIPHER_UNKNOWN;

  GNUTLS_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->id)
    ;

  return ret;
}

/**
 * MHD_gtls_cipher_list:
 *
 * Get a list of supported cipher algorithms.  Note that not
 * necessarily all ciphers are supported as TLS cipher suites.  For
 * example, DES is not supported as a cipher suite, but is supported
 * for other purposes (e.g., PKCS#8 or similar).
 *
 * Returns: a zero-terminated list of %enum MHD_GNUTLS_CipherAlgorithm
 * integers indicating the available ciphers.
 *
 **/
const enum MHD_GNUTLS_CipherAlgorithm *
MHD_gtls_cipher_list (void)
{
  return mhd_gtls_supported_ciphers;
}

int
mhd_gtls_cipher_is_ok (enum MHD_GNUTLS_CipherAlgorithm algorithm)
{
  ssize_t ret = -1;
  GNUTLS_ALG_LOOP (ret = p->id);
  if (ret >= 0)
    ret = 0;
  else
    ret = 1;
  return ret;
}

/* Key EXCHANGE functions */
mhd_gtls_mod_auth_st *
mhd_gtls_kx_auth_struct (enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm)
{
  mhd_gtls_mod_auth_st *ret = NULL;
  GNUTLS_KX_ALG_LOOP (ret = p->auth_struct);
  return ret;

}

int
mhd_gtls_kx_priority (mhd_gtls_session_t session,
                      enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm)
{
  unsigned int i;
  for (i = 0; i < session->internals.priorities.kx.num_algorithms; i++)
    {
      if (session->internals.priorities.kx.priority[i] == algorithm)
        return i;
    }
  return -1;
}

/**
 * MHD_gnutls_kx_get_name - Returns a string with the name of the specified key exchange algorithm
 * @algorithm: is a key exchange algorithm
 *
 * Returns: a pointer to a string that contains the name of the
 * specified key exchange algorithm, or %NULL.
 **/
const char *
MHD_gnutls_kx_get_name (enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm)
{
  const char *ret = NULL;

  /* avoid prefix */
  GNUTLS_KX_ALG_LOOP (ret = p->name);

  return ret;
}

/**
 * MHD_gtls_kx_get_id - Returns the gnutls id of the specified in string algorithm
 * @algorithm: is a KX name
 *
 * The names are compared in a case insensitive way.
 *
 * Returns: an id of the specified KX algorithm, or
 * %GNUTLS_KX_UNKNOWN on error.
 **/
enum MHD_GNUTLS_KeyExchangeAlgorithm
MHD_gtls_kx_get_id (const char *name)
{
  enum MHD_GNUTLS_CipherAlgorithm ret = MHD_GNUTLS_KX_UNKNOWN;

  GNUTLS_KX_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->algorithm)
    ;

  return ret;
}

/**
 * MHD_gtls_kx_list:
 *
 * Get a list of supported key exchange algorithms.
 *
 * Returns: a zero-terminated list of %enum MHD_GNUTLS_KeyExchangeAlgorithm integers
 * indicating the available key exchange algorithms.
 **/
const enum MHD_GNUTLS_KeyExchangeAlgorithm *
MHD_gtls_kx_list (void)
{
  return mhd_gtls_supported_kxs;
}

int
mhd_gtls_kx_is_ok (enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm)
{
  ssize_t ret = -1;
  GNUTLS_KX_ALG_LOOP (ret = p->algorithm);
  if (ret >= 0)
    ret = 0;
  else
    ret = 1;
  return ret;
}

int
mhd_gtls_kx_needs_rsa_params (enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm)
{
  ssize_t ret = 0;
  GNUTLS_KX_ALG_LOOP (ret = p->needs_rsa_params);
  return ret;
}

int
mhd_gtls_kx_needs_dh_params (enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm)
{
  ssize_t ret = 0;
  GNUTLS_KX_ALG_LOOP (ret = p->needs_dh_params);
  return ret;
}

/* Version */
int
mhd_gtls_version_priority (mhd_gtls_session_t session,
                           enum MHD_GNUTLS_Protocol version)
{                               /* actually returns the priority */
  unsigned int i;

  if (session->internals.priorities.protocol.priority == NULL)
    {
      gnutls_assert ();
      return -1;
    }

  for (i = 0; i < session->internals.priorities.protocol.num_algorithms; i++)
    {
      if (session->internals.priorities.protocol.priority[i] == version)
        return i;
    }
  return -1;
}

enum MHD_GNUTLS_Protocol
mhd_gtls_version_lowest (mhd_gtls_session_t session)
{                               /* returns the lowest version supported */
  unsigned int i, min = 0xff;

  if (session->internals.priorities.protocol.priority == NULL)
    {
      return MHD_GNUTLS_PROTOCOL_VERSION_UNKNOWN;
    }
  else
    for (i = 0; i < session->internals.priorities.protocol.num_algorithms;
         i++)
      {
        if (session->internals.priorities.protocol.priority[i] < min)
          min = session->internals.priorities.protocol.priority[i];
      }

  if (min == 0xff)
    return MHD_GNUTLS_PROTOCOL_VERSION_UNKNOWN;  /* unknown version */

  return min;
}

enum MHD_GNUTLS_Protocol
mhd_gtls_version_max (mhd_gtls_session_t session)
{                               /* returns the maximum version supported */
  unsigned int i, max = 0x00;

  if (session->internals.priorities.protocol.priority == NULL)
    {
      return MHD_GNUTLS_PROTOCOL_VERSION_UNKNOWN;
    }
  else
    for (i = 0; i < session->internals.priorities.protocol.num_algorithms;
         i++)
      {
        if (session->internals.priorities.protocol.priority[i] > max)
          max = session->internals.priorities.protocol.priority[i];
      }

  if (max == 0x00)
    return MHD_GNUTLS_PROTOCOL_VERSION_UNKNOWN;  /* unknown version */

  return max;
}

/**
 * MHD_gnutls_protocol_get_name - Returns a string with the name of the specified SSL/TLS version
 * @version: is a (gnutls) version number
 *
 * Returns: a string that contains the name of the specified TLS
 * version (e.g., "TLS 1.0"), or %NULL.
 **/
const char *
MHD_gnutls_protocol_get_name (enum MHD_GNUTLS_Protocol version)
{
  const char *ret = NULL;

  /* avoid prefix */
  GNUTLS_VERSION_ALG_LOOP (ret = p->name);
  return ret;
}

/**
 * MHD_gtls_protocol_get_id - Returns the gnutls id of the specified in string protocol
 * @algorithm: is a protocol name
 *
 * The names are compared in a case insensitive way.
 *
 * Returns: an id of the specified protocol, or
 * %GNUTLS_VERSION_UNKNOWN on error.
 **/
enum MHD_GNUTLS_Protocol
MHD_gtls_protocol_get_id (const char *name)
{
  enum MHD_GNUTLS_Protocol ret = MHD_GNUTLS_PROTOCOL_VERSION_UNKNOWN;

  GNUTLS_VERSION_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->id)
    ;

  return ret;
}

/**
 * MHD_gtls_protocol_list:
 *
 * Get a list of supported protocols, e.g. SSL 3.0, TLS 1.0 etc.
 *
 * Returns: a zero-terminated list of %enum MHD_GNUTLS_Protocol integers
 * indicating the available protocols.
 *
 **/
const enum MHD_GNUTLS_Protocol *
MHD_gtls_protocol_list (void)
{
  return mhd_gtls_supported_protocols;
}

int
mhd_gtls_version_get_minor (enum MHD_GNUTLS_Protocol version)
{
  int ret = -1;

  GNUTLS_VERSION_ALG_LOOP (ret = p->minor);
  return ret;
}

enum MHD_GNUTLS_Protocol
mhd_gtls_version_get (int major, int minor)
{
  int ret = -1;

  GNUTLS_VERSION_LOOP (if ((p->major == major) && (p->minor == minor))
                       ret = p->id)
    ;
  return ret;
}

int
mhd_gtls_version_get_major (enum MHD_GNUTLS_Protocol version)
{
  int ret = -1;

  GNUTLS_VERSION_ALG_LOOP (ret = p->major);
  return ret;
}

/* Version Functions */

int
mhd_gtls_version_is_supported (mhd_gtls_session_t session,
                               const enum MHD_GNUTLS_Protocol version)
{
  int ret = 0;

  GNUTLS_VERSION_ALG_LOOP (ret = p->supported);
  if (ret == 0)
    return 0;

  if (mhd_gtls_version_priority (session, version) < 0)
    return 0;                   /* disabled by the user */
  else
    return 1;
}

/* Type to KX mappings */
enum MHD_GNUTLS_KeyExchangeAlgorithm
mhd_gtls_map_kx_get_kx (enum MHD_GNUTLS_CredentialsType type, int server)
{
  enum MHD_GNUTLS_KeyExchangeAlgorithm ret = -1;

  if (server)
    {
      GNUTLS_KX_MAP_ALG_LOOP_SERVER (ret = p->algorithm);
    }
  else
    {
      GNUTLS_KX_MAP_ALG_LOOP_SERVER (ret = p->algorithm);
    }
  return ret;
}

enum MHD_GNUTLS_CredentialsType
mhd_gtls_map_kx_get_cred (enum MHD_GNUTLS_KeyExchangeAlgorithm algorithm,
                          int server)
{
  enum MHD_GNUTLS_CredentialsType ret = -1;
  if (server)
    {
      GNUTLS_KX_MAP_LOOP (if (p->algorithm == algorithm) ret = p->server_type)
        ;
    }
  else
    {
      GNUTLS_KX_MAP_LOOP (if (p->algorithm == algorithm) ret = p->client_type)
        ;
    }

  return ret;
}

/* Cipher Suite's functions */
enum MHD_GNUTLS_CipherAlgorithm
mhd_gtls_cipher_suite_get_cipher_algo (const cipher_suite_st * suite)
{
  int ret = 0;
  GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->block_algorithm);
  return ret;
}

enum MHD_GNUTLS_Protocol
mhd_gtls_cipher_suite_get_version (const cipher_suite_st * suite)
{
  int ret = 0;
  GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->version);
  return ret;
}

enum MHD_GNUTLS_KeyExchangeAlgorithm
mhd_gtls_cipher_suite_get_kx_algo (const cipher_suite_st * suite)
{
  int ret = 0;

  GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->kx_algorithm);
  return ret;

}

enum MHD_GNUTLS_HashAlgorithm
mhd_gtls_cipher_suite_get_mac_algo (const cipher_suite_st * suite)
{                               /* In bytes */
  int ret = 0;
  GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->mac_algorithm);
  return ret;

}

const char *
mhd_gtls_cipher_suite_get_name (cipher_suite_st * suite)
{
  const char *ret = NULL;

  /* avoid prefix */
  GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->name + sizeof ("GNUTLS_") - 1);

  return ret;
}

static inline int
_gnutls_cipher_suite_is_ok (cipher_suite_st * suite)
{
  size_t ret;
  const char *name = NULL;

  GNUTLS_CIPHER_SUITE_ALG_LOOP (name = p->name);
  if (name != NULL)
    ret = 0;
  else
    ret = 1;
  return ret;

}

#define SWAP(x, y) memcpy(tmp,x,size); \
		   memcpy(x,y,size); \
		   memcpy(y,tmp,size);

#define MAX_ELEM_SIZE 4
static inline int
_gnutls_partition (mhd_gtls_session_t session,
                   void *_base,
                   size_t nmemb,
                   size_t size,
                   int (*compar) (mhd_gtls_session_t,
                                  const void *, const void *))
{
  uint8_t *base = _base;
  uint8_t tmp[MAX_ELEM_SIZE];
  uint8_t ptmp[MAX_ELEM_SIZE];
  unsigned int pivot;
  unsigned int i, j;
  unsigned int full;

  i = pivot = 0;
  j = full = (nmemb - 1) * size;

  memcpy (ptmp, &base[0], size);        /* set pivot item */

  while (i < j)
    {
      while ((compar (session, &base[i], ptmp) <= 0) && (i < full))
        {
          i += size;
        }
      while ((compar (session, &base[j], ptmp) >= 0) && (j > 0))
        j -= size;

      if (i < j)
        {
          SWAP (&base[j], &base[i]);
        }
    }

  if (j > pivot)
    {
      SWAP (&base[pivot], &base[j]);
      pivot = j;
    }
  else if (i < pivot)
    {
      SWAP (&base[pivot], &base[i]);
      pivot = i;
    }
  return pivot / size;
}

static void
_gnutls_qsort (mhd_gtls_session_t session,
               void *_base,
               size_t nmemb,
               size_t size,
               int (*compar) (mhd_gtls_session_t, const void *, const void *))
{
  unsigned int pivot;
  char *base = _base;
  size_t snmemb = nmemb;

#ifdef DEBUG
  if (size > MAX_ELEM_SIZE)
    {
      gnutls_assert ();
      _gnutls_debug_log ("QSORT BUG\n");
      exit (1);
    }
#endif

  if (snmemb <= 1)
    return;
  pivot = _gnutls_partition (session, _base, nmemb, size, compar);

  _gnutls_qsort (session, base, pivot < nmemb ? pivot + 1
                 : pivot, size, compar);
  _gnutls_qsort (session, &base[(pivot + 1) * size], nmemb - pivot - 1, size,
                 compar);
}

/* a compare function for KX algorithms (using priorities).
 * For use with qsort
 */
static int
_gnutls_compare_algo (mhd_gtls_session_t session,
                      const void *i_A1, const void *i_A2)
{
  enum MHD_GNUTLS_KeyExchangeAlgorithm kA1 =
    mhd_gtls_cipher_suite_get_kx_algo ((const cipher_suite_st *) i_A1);
  enum MHD_GNUTLS_KeyExchangeAlgorithm kA2 =
    mhd_gtls_cipher_suite_get_kx_algo ((const cipher_suite_st *) i_A2);
  enum MHD_GNUTLS_CipherAlgorithm cA1 =
    mhd_gtls_cipher_suite_get_cipher_algo ((const cipher_suite_st *) i_A1);
  enum MHD_GNUTLS_CipherAlgorithm cA2 =
    mhd_gtls_cipher_suite_get_cipher_algo ((const cipher_suite_st *) i_A2);
  enum MHD_GNUTLS_HashAlgorithm mA1 =
    mhd_gtls_cipher_suite_get_mac_algo ((const cipher_suite_st *) i_A1);
  enum MHD_GNUTLS_HashAlgorithm mA2 =
    mhd_gtls_cipher_suite_get_mac_algo ((const cipher_suite_st *) i_A2);

  int p1 = (mhd_gtls_kx_priority (session, kA1) + 1) * 64;
  int p2 = (mhd_gtls_kx_priority (session, kA2) + 1) * 64;
  p1 += (mhd_gtls_cipher_priority (session, cA1) + 1) * 8;
  p2 += (mhd_gtls_cipher_priority (session, cA2) + 1) * 8;
  p1 += mhd_gtls_mac_priority (session, mA1);
  p2 += mhd_gtls_mac_priority (session, mA2);

  if (p1 > p2)
    {
      return 1;
    }
  else
    {
      if (p1 == p2)
        {
          return 0;
        }
      return -1;
    }
}

#ifdef SORT_DEBUG
static void
_gnutls_bsort (mhd_gtls_session_t session, void *_base, size_t nmemb,
               size_t size, int (*compar) (mhd_gtls_session_t, const void *,
                                           const void *))
{
  unsigned int i, j;
  int full = nmemb * size;
  char *base = _base;
  char tmp[MAX_ELEM_SIZE];

  for (i = 0; i < full; i += size)
    {
      for (j = 0; j < full; j += size)
        {
          if (compar (session, &base[i], &base[j]) < 0)
            {
              SWAP (&base[j], &base[i]);
            }
        }
    }

}
#endif

int
mhd_gtls_supported_ciphersuites_sorted (mhd_gtls_session_t session,
                                        cipher_suite_st ** ciphers)
{

#ifdef SORT_DEBUG
  unsigned int i;
#endif
  int count;

  count = mhd_gtls_supported_ciphersuites (session, ciphers);
  if (count <= 0)
    {
      gnutls_assert ();
      return count;
    }
#ifdef SORT_DEBUG
  _gnutls_debug_log ("Unsorted: \n");
  for (i = 0; i < count; i++)
    _gnutls_debug_log ("\t%d: %s\n", i,
                       mhd_gtls_cipher_suite_get_name ((*ciphers)[i]));
#endif

  _gnutls_qsort (session, *ciphers, count, sizeof (cipher_suite_st),
                 _gnutls_compare_algo);

#ifdef SORT_DEBUG
  _gnutls_debug_log ("Sorted: \n");
  for (i = 0; i < count; i++)
    _gnutls_debug_log ("\t%d: %s\n", i,
                       mhd_gtls_cipher_suite_get_name ((*ciphers)[i]));
#endif

  return count;
}

int
mhd_gtls_supported_ciphersuites (mhd_gtls_session_t session,
                                 cipher_suite_st ** _ciphers)
{

  unsigned int i, ret_count, j;
  unsigned int count = CIPHER_SUITES_COUNT;
  cipher_suite_st *tmp_ciphers;
  cipher_suite_st *ciphers;
  enum MHD_GNUTLS_Protocol version;

  if (count == 0)
    {
      return 0;
    }

  tmp_ciphers = gnutls_alloca (count * sizeof (cipher_suite_st));
  if (tmp_ciphers == NULL)
    return GNUTLS_E_MEMORY_ERROR;

  ciphers = gnutls_malloc (count * sizeof (cipher_suite_st));
  if (ciphers == NULL)
    {
      gnutls_afree (tmp_ciphers);
      return GNUTLS_E_MEMORY_ERROR;
    }

  version = MHD_gnutls_protocol_get_version (session);

  for (i = 0; i < count; i++)
    {
      memcpy (&tmp_ciphers[i], &mhd_gtls_cs_algorithms[i].id,
              sizeof (cipher_suite_st));
    }

  for (i = j = 0; i < count; i++)
    {
      /* remove private cipher suites, if requested.
       */
      if (tmp_ciphers[i].suite[0] == 0xFF && session->internals.enable_private
          == 0)
        continue;

      /* remove cipher suites which do not support the
       * protocol version used.
       */
      if (mhd_gtls_cipher_suite_get_version (&tmp_ciphers[i]) > version)
        continue;

      if (mhd_gtls_kx_priority (session,
                                mhd_gtls_cipher_suite_get_kx_algo
                                (&tmp_ciphers[i])) < 0)
        continue;
      if (mhd_gtls_mac_priority (session,
                                 mhd_gtls_cipher_suite_get_mac_algo
                                 (&tmp_ciphers[i])) < 0)
        continue;
      if (mhd_gtls_cipher_priority (session,
                                    mhd_gtls_cipher_suite_get_cipher_algo
                                    (&tmp_ciphers[i])) < 0)
        continue;

      memcpy (&ciphers[j], &tmp_ciphers[i], sizeof (cipher_suite_st));
      j++;
    }

  ret_count = j;

#if 0                           /* expensive */
  if (ret_count > 0 && ret_count != count)
    {
      ciphers =
        mhd_gtls_realloc_fast (ciphers, ret_count * sizeof (cipher_suite_st));
    }
  else
    {
      if (ret_count != count)
        {
          gnutls_free (ciphers);
          ciphers = NULL;
        }
    }
#endif

  gnutls_afree (tmp_ciphers);

  /* This function can no longer return 0 cipher suites.
   * It returns an error code instead.
   */
  if (ret_count == 0)
    {
      gnutls_assert ();
      gnutls_free (ciphers);
      return GNUTLS_E_NO_CIPHER_SUITES;
    }
  *_ciphers = ciphers;
  return ret_count;
}

/* For compression  */

#define MIN_PRIVATE_COMP_ALGO 0xEF

/* returns the TLS numbers of the compression methods we support
 */
#define SUPPORTED_COMPRESSION_METHODS session->internals.priorities.compression.num_algorithms
int
mhd_gtls_supported_compression_methods (mhd_gtls_session_t session,
                                        uint8_t ** comp)
{
  unsigned int i, j;

  *comp = gnutls_malloc (sizeof (uint8_t) * SUPPORTED_COMPRESSION_METHODS);
  if (*comp == NULL)
    return GNUTLS_E_MEMORY_ERROR;

  for (i = j = 0; i < SUPPORTED_COMPRESSION_METHODS; i++)
    {
      int tmp =
        mhd_gtls_compression_get_num (session->internals.priorities.
                                      compression.priority[i]);

      /* remove private compression algorithms, if requested.
       */
      if (tmp == -1 || (tmp >= MIN_PRIVATE_COMP_ALGO
                        && session->internals.enable_private == 0))
        {
          gnutls_assert ();
          continue;
        }

      (*comp)[j] = (uint8_t) tmp;
      j++;
    }

  if (j == 0)
    {
      gnutls_assert ();
      gnutls_free (*comp);
      *comp = NULL;
      return GNUTLS_E_NO_COMPRESSION_ALGORITHMS;
    }
  return j;
}

/**
 * MHD_gnutls_certificate_type_get_name - Returns a string with the name of the specified certificate type
 * @type: is a certificate type
 *
 * Returns: a string (or %NULL) that contains the name of the
 * specified certificate type.
 **/
const char *
MHD_gnutls_certificate_type_get_name (enum MHD_GNUTLS_CertificateType type)
{
  const char *ret = NULL;

  if (type == MHD_GNUTLS_CRT_X509)
    ret = "X.509";
  return ret;
}

/**
 * MHD_gtls_certificate_type_get_id - Returns the gnutls id of the specified in string type
 * @name: is a certificate type name
 *
 * The names are compared in a case insensitive way.
 *
 * Returns: an id of the specified in a string certificate type, or
 * %GNUTLS_CRT_UNKNOWN on error.
 **/
enum MHD_GNUTLS_CertificateType
MHD_gtls_certificate_type_get_id (const char *name)
{
  enum MHD_GNUTLS_CertificateType ret = MHD_GNUTLS_CRT_UNKNOWN;

  if (strcasecmp (name, "X.509") == 0 || strcasecmp (name, "X509") == 0)
    return MHD_GNUTLS_CRT_X509;
  return ret;
}

static const enum MHD_GNUTLS_CertificateType
  mhd_gtls_supported_certificate_types[] =
{ MHD_GNUTLS_CRT_X509,
  0
};

/**
 * MHD_gtls_certificate_type_list:
 *
 * Get a list of certificate types.
 *
 * Returns: a zero-terminated list of %enum MHD_GNUTLS_CertificateType
 * integers indicating the available certificate types.
 *
 **/
const enum MHD_GNUTLS_CertificateType *
MHD_gtls_certificate_type_list (void)
{
  return mhd_gtls_supported_certificate_types;
}

/* returns the enum MHD_GNUTLS_PublicKeyAlgorithm which is compatible with
 * the given enum MHD_GNUTLS_KeyExchangeAlgorithm.
 */
enum MHD_GNUTLS_PublicKeyAlgorithm
mhd_gtls_map_pk_get_pk (enum MHD_GNUTLS_KeyExchangeAlgorithm kx_algorithm)
{
  enum MHD_GNUTLS_PublicKeyAlgorithm ret = -1;

  GNUTLS_PK_MAP_ALG_LOOP (ret = p->pk_algorithm) return ret;
}

/* Returns the encipher type for the given key exchange algorithm.
 * That one of CIPHER_ENCRYPT, CIPHER_SIGN, CIPHER_IGN.
 *
 * ex. GNUTLS_KX_RSA requires a certificate able to encrypt... so returns CIPHER_ENCRYPT.
 */
enum encipher_type
mhd_gtls_kx_encipher_type (enum MHD_GNUTLS_KeyExchangeAlgorithm kx_algorithm)
{
  int ret = CIPHER_IGN;
  GNUTLS_PK_MAP_ALG_LOOP (ret = p->encipher_type) return ret;

}

/* signature algorithms;
 */
struct gnutls_sign_entry
{
  const char *name;
  const char *oid;
  gnutls_sign_algorithm_t id;
  enum MHD_GNUTLS_PublicKeyAlgorithm pk;
  enum MHD_GNUTLS_HashAlgorithm mac;
};
typedef struct gnutls_sign_entry gnutls_sign_entry;

static const gnutls_sign_entry mhd_gtls_sign_algorithms[] = {
  {"RSA-SHA",
   SIG_RSA_SHA1_OID,
   GNUTLS_SIGN_RSA_SHA1,
   MHD_GNUTLS_PK_RSA,
   MHD_GNUTLS_MAC_SHA1},
  {"RSA-SHA256",
   SIG_RSA_SHA256_OID,
   GNUTLS_SIGN_RSA_SHA256,
   MHD_GNUTLS_PK_RSA,
   MHD_GNUTLS_MAC_SHA256},
  {"RSA-MD5",
   SIG_RSA_MD5_OID,
   GNUTLS_SIGN_RSA_MD5,
   MHD_GNUTLS_PK_RSA,
   MHD_GNUTLS_MAC_MD5},
  {"GOST R 34.10-2001",
   SIG_GOST_R3410_2001_OID,
   0,
   0,
   0},
  {"GOST R 34.10-94",
   SIG_GOST_R3410_94_OID,
   0,
   0,
   0},
  {0,
   0,
   0,
   0,
   0}
};

#define GNUTLS_SIGN_LOOP(b) \
  do {								       \
    const gnutls_sign_entry *p;					       \
    for(p = mhd_gtls_sign_algorithms; p->name != NULL; p++) { b ; }	       \
  } while (0)

#define GNUTLS_SIGN_ALG_LOOP(a) \
  GNUTLS_SIGN_LOOP( if(p->id && p->id == sign) { a; break; } )

gnutls_sign_algorithm_t
mhd_gtls_x509_oid2sign_algorithm (const char *oid)
{
  gnutls_sign_algorithm_t ret = 0;

  GNUTLS_SIGN_LOOP (if (strcmp (oid, p->oid) == 0)
                    {
                    ret = p->id; break;}
  );

  if (ret == 0)
    {
      _gnutls_x509_log ("Unknown SIGN OID: '%s'\n", oid);
      return GNUTLS_SIGN_UNKNOWN;
    }
  return ret;
}

gnutls_sign_algorithm_t
mhd_gtls_x509_pk_to_sign (enum MHD_GNUTLS_PublicKeyAlgorithm pk,
                          enum MHD_GNUTLS_HashAlgorithm mac)
{
  gnutls_sign_algorithm_t ret = 0;

  GNUTLS_SIGN_LOOP (if (pk == p->pk && mac == p->mac)
                    {
                    ret = p->id; break;}
  );

  if (ret == 0)
    return GNUTLS_SIGN_UNKNOWN;
  return ret;
}

const char *
mhd_gtls_x509_sign_to_oid (enum MHD_GNUTLS_PublicKeyAlgorithm pk,
                           enum MHD_GNUTLS_HashAlgorithm mac)
{
  gnutls_sign_algorithm_t sign;
  const char *ret = NULL;

  sign = mhd_gtls_x509_pk_to_sign (pk, mac);
  if (sign == GNUTLS_SIGN_UNKNOWN)
    return NULL;

  GNUTLS_SIGN_ALG_LOOP (ret = p->oid);
  return ret;
}

/* pk algorithms;
 */
struct gnutls_pk_entry
{
  const char *name;
  const char *oid;
  enum MHD_GNUTLS_PublicKeyAlgorithm id;
};
typedef struct gnutls_pk_entry gnutls_pk_entry;

static const gnutls_pk_entry mhd_gtls_pk_algorithms[] = {
  {"RSA",
   PK_PKIX1_RSA_OID,
   MHD_GNUTLS_PK_RSA},
  {"GOST R 34.10-2001",
   PK_GOST_R3410_2001_OID,
   0},
  {"GOST R 34.10-94",
   PK_GOST_R3410_94_OID,
   0},
  {0,
   0,
   0}
};

enum MHD_GNUTLS_PublicKeyAlgorithm
mhd_gtls_x509_oid2pk_algorithm (const char *oid)
{
  enum MHD_GNUTLS_PublicKeyAlgorithm ret = MHD_GNUTLS_PK_UNKNOWN;
  const gnutls_pk_entry *p;

  for (p = mhd_gtls_pk_algorithms; p->name != NULL; p++)
    if (strcmp (p->oid, oid) == 0)
      {
        ret = p->id;
        break;
      }

  return ret;
}

const char *
mhd_gtls_x509_pk_to_oid (enum MHD_GNUTLS_PublicKeyAlgorithm algorithm)
{
  const char *ret = NULL;
  const gnutls_pk_entry *p;

  for (p = mhd_gtls_pk_algorithms; p->name != NULL; p++)
    if (p->id == algorithm)
      {
        ret = p->oid;
        break;
      }

  return ret;
}