gnoga_2.1.2_5f127c56/deps/simple_components/gnutls.adb

   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
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
--                                                                    --
--  package GNUTLS                  Copyright (c)  Dmitry A. Kazakov  --
--  Implementation                                 Luebeck            --
--                                                 Winter, 2015       --
--                                                                    --
--                                Last revision :  18:40 23 Oct 2021  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either Version  2  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  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.IO_Exceptions;     use Ada.IO_Exceptions;
with Interfaces.C.Strings;  use Interfaces.C.Strings;
with System_Errno;          use System_Errno;

with Ada.Text_IO;
with Ada.Unchecked_Conversion;
with Interfaces.C.Pointers;
with System.Address_To_Access_Conversions;
with System.Storage_Elements;

package body GNUTLS is
   use System;

   type ssize_t is new ptrdiff_t;
   function To_Unsigned is new Ada.Unchecked_Conversion (int, unsigned);
   function To_Address is
      new Ada.Unchecked_Conversion (chars_ptr, Address);

   package Certificate_Type_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => Certificate_Type,
             Element_Array      => Certificate_Type_Array,
             Default_Terminator => CRT_Unknown
          );
   package Cipher_Algorithm_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => Cipher_Algorithm,
             Element_Array      => Cipher_Algorithm_Array,
             Default_Terminator => Cipher_Unknown
          );
   package Compression_Method_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => Compression_Method,
             Element_Array      => Compression_Method_Array,
             Default_Terminator => Comp_Unknown
          );
   package Datum_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => Datum,
             Element_Array      => Datum_Array,
             Default_Terminator => (Null_Address, 0)
          );
   package Digest_Algorithm_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => Digest_Algorithm,
             Element_Array      => Digest_Algorithm_Array,
             Default_Terminator => Dig_Unknown
          );
   package ECC_Curve_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => ECC_Curve,
             Element_Array      => ECC_Curve_Array,
             Default_Terminator => ECC_Curve_Invalid
          );
   package KX_Algorithm_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => KX_Algorithm,
             Element_Array      => KX_Algorithm_Array,
             Default_Terminator => KX_Unknown
          );
   package MAC_Algorithm_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => MAC_Algorithm,
             Element_Array      => MAC_Algorithm_Array,
             Default_Terminator => MAC_Unknown
          );
   package PK_Algorithm_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => PK_Algorithm,
             Element_Array      => PK_Algorithm_Array,
             Default_Terminator => PK_Unknown
          );
   package Sign_Algorithm_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => Sign_Algorithm,
             Element_Array      => Sign_Algorithm_Array,
             Default_Terminator => Sign_Unknown
          );
   package Protocol_Pointers is
      new Interfaces.C.Pointers
          (  Index              => Positive,
             Element            => Protocol,
             Element_Array      => Protocol_Array,
             Default_Terminator => No_Protocol
          );

   procedure Adjust (Data : in out DH_Params) is
      function Internal (Destination, Source : Address) return int;
      pragma Import (C, Internal, "gnutls_dh_params_cpy");
      Source : Address := Data.Handle;
   begin
      Initialize (Data);
      Check (Internal (Data.Handle, Source));
   end Adjust;

   procedure Adjust (Data : in out DH_Params_Ref) is
   begin
      null;
   end Adjust;

   function Alert_Get (Session : Session_Type)
      return Alert_Description is
      function Internal (Session : Address) return Alert_Description;
      pragma Import (C, Internal, "gnutls_alert_get");
   begin
      return Internal (Session.Holder.Handle);
   end Alert_Get;

   function Alert_Get_Name (Alert : Alert_Description) return String is
      function Internal (Alert : Alert_Description) return chars_ptr;
      pragma Import
             (  C,
                Internal,
                "gnutls_alert_get_name"
             );
      Result : chars_ptr := Internal (Alert);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No alert matched");
      else
         return Value (Result);
      end if;
   end Alert_Get_Name;

   function Alert_Get_StrName
            (  Alert : Alert_Description
            )  return String is
      function Internal (Alert : Alert_Description) return chars_ptr;
      pragma Import (C, Internal, "gnutls_alert_get_strname");
   begin
      return Value (Internal (Alert));
   end Alert_Get_StrName;

   procedure Alert_Send
             (  Session : in out Session_Type;
                Level   : Alert_level;
                Desc    : Alert_Description
             )  is
      function Internal
               (  Session : Address;
                  Level   : Alert_level;
                  Desc    : Alert_Description
               )  return int;
      pragma Import (C, Internal, "gnutls_alert_send");
   begin
      Check (Internal (Session.Holder.Handle, Level, Desc));
   end Alert_Send;

   procedure Alert_Send_Appropriate
             (  Session : in out Session_Type;
                Level   : Alert_level;
                Desc    : Alert_Description
             )  is
      function Internal
               (  Session : Address;
                  Level   : Alert_level;
                  Desc    : Alert_Description
               )  return int;
      pragma Import (C, Internal, "gnutls_alert_send_appropriate");
   begin
      Check (Internal (Session.Holder.Handle, Level, Desc));
   end Alert_Send_Appropriate;

   function Alpn_Get_Selected_Protocol
            (  Session : Session_Type
            )  return Stream_Element_Array is
      function Internal
               (  Session  : Address;
                  Protocol : access Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_alpn_get_selected_protocol");
      Protocol : aliased Datum;
   begin
      Check (Internal (Session.Holder.Handle, Protocol'Access));
      return To_Stream_Element_Array (Protocol);
   end Alpn_Get_Selected_Protocol;

   procedure Alpn_Set_Selected_Protocol
             (  Session   : in out Session_Type;
                Protocols : Datum_Array;
                Flags     : int
             )  is
      function Internal
               (  Session   : Address;
                  Protocols : access constant Datum;
                  Size      : unsigned;
                  Flags     : int
               )  return int;
      pragma Import (C, Internal, "gnutls_alpn_set_protocols");
   begin
      Check
      (  Internal
         (  Session.Holder.Handle,
            Protocols (Protocols'First)'Access,
            Protocols'Length,
            Flags
      )  );
   end Alpn_Set_Selected_Protocol;

   package body Anon_Params_Function is
      type Params_Function is access function
           (  Session    : Address;
              Type_Of    : Params_Type;
              Parameters : Params_Data
           )  return int;
      pragma Convention (C, Params_Function);
      procedure Internal
                (  Credentials : Address;
                   Func        : Params_Function
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_anon_set_params_function"
             );
      function Func
               (  Session    : Address;
                  Type_Of    : Params_Type;
                  Parameters : Params_Data
               )  return int;
      pragma Convention (C, Func);

      function Func
               (  Session    : Address;
                  Type_Of    : Params_Type;
                  Parameters : Params_Data
               )  return int is
         Parent : Session_Type (0);
      begin
         Parent.Holder.Handle := Session;
         Handle (Parent, Create (Type_Of, Parameters));
         return E_Success;
      exception
         when Error : others =>
            return Get_Error_Code (Error);
      end Func;

      procedure Set
                (  Credentials : in out Anon_Server_Credentials'Class
                )  is
      begin
         Internal (Credentials.Handle, Func'Access);
      end Set;
   end Anon_Params_Function;

   package body Anon_Server_Params_Function is
      type Params_Function is access function
           (  Session    : Address;
              Type_Of    : Params_Type;
              Parameters : Params_Data
           )  return int;
      pragma Convention (C, Params_Function);
      procedure Internal
                (  Credentials : Address;
                   Func        : Params_Function
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_anon_set_server_params_function"
             );
      function Func
               (  Session    : Address;
                  Type_Of    : Params_Type;
                  Parameters : Params_Data
               )  return int;
      pragma Convention (C, Func);

      function Func
               (  Session    : Address;
                  Type_Of    : Params_Type;
                  Parameters : Params_Data
               )  return int is
         Parent : Session_Type (0);
      begin
         Parent.Holder.Handle := Session;
         Handle (Parent, Create (Type_Of, Parameters));
         return E_Success;
      exception
         when Error : others =>
            return Get_Error_Code (Error);
      end Func;

      procedure Set
                (  Credentials : in out Anon_Server_Credentials'Class
                )  is
      begin
         Internal (Credentials.Handle, Func'Access);
      end Set;
   end Anon_Server_Params_Function;

   procedure Anon_Set_Server_DH_Params
             (  Credentials : in out Anon_Server_Credentials;
                Params      : DH_Params'Class
             )  is
      procedure Internal
                (  Credentials : Address;
                   Params      : Address
                );
      pragma Import (C, Internal, "gnutls_anon_set_server_dh_params");
   begin
      Internal (Credentials.Handle, Params.Handle);
   end Anon_Set_Server_DH_Params;

   function Auth_Client_Get_Type
            (  Session : Session_Type
            )  return Credentials_Type is
      function Internal (Session : Address) return Credentials_Type;
      pragma Import (C, Internal, "gnutls_auth_client_get_type");
   begin
      return Internal (Session.Holder.Handle);
   end Auth_Client_Get_Type;

   function Auth_Get_Type
            (  Session : Session_Type
            )  return Credentials_Type is
      function Internal (Session : Address) return Credentials_Type;
      pragma Import (C, Internal, "gnutls_auth_get_type");
   begin
      return Internal (Session.Holder.Handle);
   end Auth_Get_Type;

   procedure Audit_Log_Function
             (  Session : in out Session_Type;
                Text    : String
             )  is
   begin
      null;
   end Audit_Log_Function;

   function Auth_Server_Get_Type
            (  Session : Session_Type
            )  return Credentials_Type is
      function Internal (Session : Address) return Credentials_Type;
      pragma Import (C, Internal, "gnutls_auth_server_get_type");
   begin
      return Internal (Session.Holder.Handle);
   end Auth_Server_Get_Type;

   function Bits_Are_Curve (Bits : unsigned) return Boolean is
   begin
      return 0 /= (16#80000000# and Bits);
   end Bits_Are_Curve;

   function Bits_To_Curve (Bits : unsigned) return ECC_Curve is
   begin
      return ECC_Curve'Val (16#7FFFFFFF# and Bits);
   end Bits_To_Curve;

   procedure Bye
             (  Session : in out Session_Type;
                How     : Close_Request
             )  is
      function Internal
               (  Session : Address;
                  How     : Close_Request
               )  return int;
      pragma Import (C, Internal, "gnutls_bye");
   begin
      Check (Internal (Session.Holder.Handle, How));
   end Bye;

   function Certificate_Activation_Time_Peers
            (  Session : Session_Type
            )  return Time is
      function Internal (Session : Address) return time_t;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_activation_time_peers"
             );
   begin
      return To_Time (Internal (Session.Holder.Handle));
   end Certificate_Activation_Time_Peers;

   function Certificate_Client_Get_Request_Status
            (  Session : Session_Type
            )  return Boolean is
      function Internal (Session : Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_client_get_request_status"
             );
   begin
      return Internal (Session.Holder.Handle) /= 0;
   end Certificate_Client_Get_Request_Status;

   function Certificate_Expiration_Time_Peers
            (  Session : Session_Type
            )  return Time is
      function Internal (Session : Address) return time_t;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_expiration_time_peers"
             );
   begin
      return To_Time (Internal (Session.Holder.Handle));
   end Certificate_Expiration_Time_Peers;

   procedure Certificate_Free_CA_Names
             (  Credentials : in out Certificate_Credentials
             )  is
      procedure Internal (Credentials : Address);
      pragma Import (C, Internal, "gnutls_certificate_free_ca_names");
   begin
      Internal (Credentials.Handle);
   end Certificate_Free_CA_Names;

   procedure Certificate_Free_CAs
             (  Credentials : in out Certificate_Credentials
             )  is
      procedure Internal (Credentials : Address);
      pragma Import (C, Internal, "gnutls_certificate_free_cas");
   begin
      Internal (Credentials.Handle);
   end Certificate_Free_CAs;

   procedure Certificate_Free_CRLs
             (  Credentials : in out Certificate_Credentials
             )  is
      procedure Internal (Credentials : Address);
      pragma Import (C, Internal, "gnutls_certificate_free_crls");
   begin
      Internal (Credentials.Handle);
   end Certificate_Free_CRLs;

   procedure Certificate_Free_Keys
             (  Credentials : in out Certificate_Credentials
             )  is
      procedure Internal (Credentials : Address);
      pragma Import (C, Internal, "gnutls_certificate_free_keys");
   begin
      Internal (Credentials.Handle);
   end Certificate_Free_Keys;

   function Certificate_Get_Crt_Raw
            (  Credentials : Certificate_Credentials;
               Idx1        : unsigned;
               Idx2        : unsigned
            )  return Stream_Element_Array is
      function Internal
               (  Credentials : Address;
                  Idx1        : unsigned;
                  Idx2        : unsigned;
                  Result      : access Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_certificate_get_crt_raw");
      Result : aliased Datum;
   begin
      Check (Internal (Credentials.Handle, Idx1, Idx2, Result'Access));
      return To_Stream_Element_Array (Result);
   end Certificate_Get_Crt_Raw;

   function Certificate_Get_Issuer
            (  Credentials : Certificate_Credentials;
               Certificate : X509_Crt
            )  return X509_Crt is
      function Internal
               (  Credentials : Address;
                  Certificate : X509_Crt;
                  Result      : access X509_Crt;
                  Flags       : unsigned
               )  return int;
      pragma Import (C, Internal, "gnutls_certificate_get_issuer");
      Result : aliased X509_Crt;
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            Certificate,
            Result'Access,
            0
      )  );
      return Result;
   end Certificate_Get_Issuer;

   function Certificate_Get_Ours
            (  Session : Session_Type
            )  return Stream_Element_Array is
      function Internal (Credentials : Address) return access Datum;
      pragma Import (C, Internal, "gnutls_certificate_get_ours");
   begin
      return To_Stream_Element_Array
             (  Internal (Session.Holder.Handle).all
             );
   end Certificate_Get_Ours;

   function Certificate_Get_Peers
            (  Session : Session_Type
            )  return Datum_Array is
      use Datum_Pointers;
      function Internal
               (  Session : Address;
                  Size    : access unsigned
               )  return Pointer;
      pragma Import (C, Internal, "gnutls_certificate_get_peers");
      Size   : aliased unsigned;
      List   : Pointer := Internal (Session.Holder.Handle, Size'Access);
      Result : Datum_Array (1..Positive (Size));
   begin
      for Index in Result'Range loop
         Result (Index) := List.all;
         List := List + 1;
      end loop;
      return Result;
   end Certificate_Get_Peers;

   function Certificate_Get_Peers_Subkey_ID
            (  Session : Session_Type
            )  return String is
      function Internal
               (  Credentials : Address;
                  ID          : access Datum
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_get_peers_subkey_id"
             );
      Result : aliased Datum;
   begin
      Check (Internal (Session.Holder.Handle, Result'Access));
      return To_String (Result);
   end Certificate_Get_Peers_Subkey_ID;

   procedure Certificate_Send_X509_RDN_Sequence
             (  Session : in out Session_Type;
                Status  : int
             )  is
      procedure Internal (Session : Address; Status : int);
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_send_x509_rdn_sequence"
             );
   begin
      Internal (Session.Holder.Handle, Status);
   end Certificate_Send_X509_RDN_Sequence;

   procedure Certificate_Server_Set_Request
             (  Session : in out Session_Type;
                Request : Certificate_Request
             )  is
      procedure Internal
                (  Session : Address;
                   Request : Certificate_Request
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_server_set_request"
             );
   begin
      Internal (Session.Holder.Handle, Request);
   end Certificate_Server_Set_Request;

   procedure Certificate_Server_Set_DH_Params
             (  Credentials : in out Certificate_Credentials;
                Parameters  : DH_Params'Class
             )  is
      procedure Internal (Credentials : Address; Parameters : Address);
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_dh_params"
             );
   begin
      Internal (Credentials.Handle, Parameters.Handle);
   end Certificate_Server_Set_DH_Params;

   procedure Certificate_Set_DH_Params
             (  Credentials : in out Certificate_Credentials;
                Parameters  : DH_Params'Class
             )  is
      procedure Internal (Credentials : Address; Parameters : Address);
      pragma Import (C, Internal, "gnutls_certificate_set_dh_params");
   begin
      Internal (Credentials.Handle, Parameters.Handle);
   end Certificate_Set_DH_Params;

   procedure Certificate_Set_OCSP_Status_Request_File
             (  Credentials   : in out Certificate_Credentials;
                Response_File : String;
                Flags         : int := 0
             )  is
      function Internal
               (  Credentials   : Address;
                  Response_File : char_array;
                  Flags         : int
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_ocsp_status_request_file"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_C (Response_File),
            Flags
      )  );
   end Certificate_Set_OCSP_Status_Request_File;

   procedure Certificate_Server_Set_OpenPGP_Key
             (  Credentials : in out Certificate_Credentials;
                Public_Key  : OpenPGP_Crt;
                Private_Key : OpenPGP_Privkey
             )  is
      function Internal
               (  Credentials : Address;
                  Crt         : OpenPGP_Crt;
                  Key         : OpenPGP_Privkey
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_openpgp_key"
             );
   begin
      Check (Internal (Credentials.Handle, Public_Key, Private_Key));
   end Certificate_Server_Set_OpenPGP_Key;

   procedure Certificate_Server_Set_OpenPGP_Key_File
             (  Credentials  : in out Certificate_Credentials;
                Public_Keys  : String;
                Private_Keys : String;
                Format       : OpenPGP_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Crt         : char_array;
                  Key         : char_array;
                  Format      : OpenPGP_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_openpgp_key_file"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_C (Public_Keys),
            To_C (Private_Keys),
            Format
      )  );
   end Certificate_Server_Set_OpenPGP_Key_File;

   procedure Certificate_Server_Set_OpenPGP_Key_File
             (  Credentials  : in out Certificate_Credentials;
                Public_Keys  : String;
                Private_Keys : String;
                Subkey_ID    : String;
                Format       : OpenPGP_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Crt         : char_array;
                  Key         : char_array;
                  Subkey_ID   : char_array;
                  Format      : OpenPGP_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_openpgp_key_file2"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_C (Public_Keys),
            To_C (Private_Keys),
            To_C (Subkey_ID),
            Format
      )  );
   end Certificate_Server_Set_OpenPGP_Key_File;

   procedure Certificate_Server_Set_OpenPGP_Key_Mem
             (  Credentials  : in out Certificate_Credentials;
                Public_Keys  : Stream_Element_Array;
                Private_Keys : Stream_Element_Array;
                Format       : OpenPGP_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Crt         : Datum;
                  Key         : Datum;
                  Format      : OpenPGP_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_openpgp_key_mem"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_Datum (Public_Keys),
            To_Datum (Private_Keys),
            Format
      )  );
   end Certificate_Server_Set_OpenPGP_Key_Mem;

   procedure Certificate_Server_Set_OpenPGP_Key_Mem
             (  Credentials  : in out Certificate_Credentials;
                Public_Keys  : Stream_Element_Array;
                Private_Keys : Stream_Element_Array;
                Subkey_ID    : String;
                Format       : OpenPGP_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Crt         : Datum;
                  Key         : Datum;
                  Subkey_ID   : char_array;
                  Format      : OpenPGP_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_openpgp_key_mem2"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_Datum (Public_Keys),
            To_Datum (Private_Keys),
            To_C (Subkey_ID),
            Format
      )  );
   end Certificate_Server_Set_OpenPGP_Key_Mem;

   procedure Certificate_Server_Set_OpenPGP_Keyring_File
             (  Credentials : in out Certificate_Credentials;
                File        : String;
                Format      : OpenPGP_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  File        : char_array;
                  Format      : OpenPGP_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_openpgp_keyring_file"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_C (File),
            Format
      )  );
   end Certificate_Server_Set_OpenPGP_Keyring_File;

   procedure Certificate_Server_Set_OpenPGP_Mem
             (  Credentials : in out Certificate_Credentials;
                Data        : Stream_Element_Array;
                Format      : OpenPGP_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Data        : Address;
                  Length      : size_t;
                  Format      : OpenPGP_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_openpgp_keyring_mem"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            Data (Data'First)'Address,
            Data'Length,
            Format
      )  );
   end Certificate_Server_Set_OpenPGP_Mem;

   package body Certificate_Set_Params_Function is
      type Params_Function is access function
           (  Session    : Address;
              Type_Of    : Params_Type;
              Parameters : Params_Data
           )  return int;
      pragma Convention (C, Params_Function);
      procedure Internal
                (  Credentials : Address;
                   Func        : Params_Function
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_params_function"
             );
      function Func
               (  Session    : Address;
                  Type_Of    : Params_Type;
                  Parameters : Params_Data
               )  return int;
      pragma Convention (C, Func);

      function Func
               (  Session    : Address;
                  Type_Of    : Params_Type;
                  Parameters : Params_Data
               )  return int is
         Parent : Session_Type (0);
      begin
         Parent.Holder.Handle := Session;
         Handle (Parent, Create (Type_Of, Parameters));
         return E_Success;
      exception
         when Error : others =>
            return Get_Error_Code (Error);
      end Func;

      procedure Set
                (  Credentials : in out Certificate_Credentials'Class
                )  is
      begin
         Internal (Credentials.Handle, Func'Access);
      end Set;
   end Certificate_Set_Params_Function;

   type Status_Request_OSCP_Func_Ptr is access function
        (  Session  : Address;
           Ptr      : Address;
           Response : access Datum
        )  return int;
   pragma Convention (C, Status_Request_OSCP_Func_Ptr);
   function Status_Request_OSCP_Func
            (  Session  : Address;
               Ptr      : Address;
               Response : access Datum
            )  return int;
   pragma Convention (C, Status_Request_OSCP_Func);

   function Status_Request_OSCP_Func
            (  Session  : Address;
               Ptr      : Address;
               Response : access Datum
            )  return int is
      package Conversions is
         new Address_To_Access_Conversions (OSCP_Status_Request'Class);
      use Conversions;
      Data : Object_Pointer := To_Pointer (Ptr);
   begin
      if Data = null then
         return E_Received_Illegal_Parameter;
      else
         declare
            Parent  : Session_Type (0);
            Request : OSCP_Status_Request'Class renames Data.all;
         begin
            Parent.Holder.Handle := Session;
            Handle (Request, Parent, Response.all);
            return E_Success;
         exception
            when Error : others =>
               return Get_Error_Code (Error);
         end;
      end if;
   end Status_Request_OSCP_Func;

   procedure Certificate_Set_OCSP_Status_Request_Function
             (  Credentials : in out Certificate_Credentials;
                Request     : in out OSCP_Status_Request'Class
             )  is
      procedure Internal
                (  Credentials : Address;
                   Func        : Status_Request_OSCP_Func_Ptr;
                   Data        : Address
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_ocsp_status_request_function"
             );
   begin
      Internal
      (  Credentials.Handle,
         Status_Request_OSCP_Func'Access,
         Request'Address
      );
   end Certificate_Set_OCSP_Status_Request_Function;

   type PIN_Request_Func_Ptr is access function
        (  User_Data   : Address;
           Attempt     : int;
           Token_URL   : chars_ptr;
           Token_Label : chars_ptr;
           Flags       : int;
           PIN         : chars_ptr;
           PIN_Max     : size_t
        )  return int;
   pragma Convention (C, PIN_Request_Func_Ptr);
   function PIN_Request_Func
            (  User_Data   : Address;
               Attempt     : int;
               Token_URL   : chars_ptr;
               Token_Label : chars_ptr;
               Flags       : int;
               PIN         : chars_ptr;
               PIN_Max     : size_t
            )  return int;
   pragma Convention (C, PIN_Request_Func);

   function PIN_Request_Func
            (  User_Data   : Address;
               Attempt     : int;
               Token_URL   : chars_ptr;
               Token_Label : chars_ptr;
               Flags       : int;
               PIN         : chars_ptr;
               PIN_Max     : size_t
            )  return int is
      package Conversions is
         new Address_To_Access_Conversions (PIN_Request'Class);
      use Conversions;
      Data : Object_Pointer := To_Pointer (User_Data);
   begin
      if Data = null then
         return E_Received_Illegal_Parameter;
      else
         declare
            Result : String := Handle
                               (  Data.all'Access,
                                  Attempt,
                                  Value (Token_URL),
                                  Value (Token_Label),
                                  Flags
                               );
         begin
            if Result'Length >= PIN_Max then
               return E_Short_Memory_Buffer;
            else
               Update (PIN, 0, Result & Character'Val (0), False);
               return E_Success;
            end if;
         exception
            when Error : TLS_Error =>
               return Get_Error_Code (Error);
            when others =>
               return E_Internal_Error;
         end;
      end if;
   end PIN_Request_Func;

   procedure Certificate_Set_PIN_Function
             (  Credentials : in out Certificate_Credentials;
                PIN         : in out PIN_Request'Class
             )  is
      procedure Internal
                (  Credentials : Address;
                   Func        : PIN_Request_Func_Ptr;
                   Data        : Address
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_pin_function"
             );
   begin
      Internal
      (  Credentials.Handle,
         PIN_Request_Func'Access,
         PIN'Address
      );
   end Certificate_Set_PIN_Function;

   package body Certificate_Set_Retrieve_Function is
      package PK_Algorithm_Pointers is
         new Interfaces.C.Pointers
             (  Positive,
                PK_Algorithm,
                PK_Algorithm_Array,
                PK_Unknown
             );
      use PK_Algorithm_Pointers;
      type Retrieve_Function is access function
           (  Session           : Address;
              Req_CA_RDN        : Datum;
              NReqs             : int;
              Algorithms        : Pointer;
              Algorithms_Length : int;
              Retrieve          : access Retr2
           )  return int;
      pragma Convention (C, Retrieve_Function);
      procedure Internal
                (  Credentials : Address;
                   Func        : Retrieve_Function
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_retrieve_function"
             );
      function Func
               (  Session           : Address;
                  Req_CA_RDN        : Datum;
                  NReqs             : int;
                  Algorithms        : Pointer;
                  Algorithms_Length : int;
                  Retrieve          : access Retr2
               )  return int;
      pragma Convention (C, Func);

      function Func
               (  Session           : Address;
                  Req_CA_RDN        : Datum;
                  NReqs             : int;
                  Algorithms        : Pointer;
                  Algorithms_Length : int;
                  Retrieve          : access Retr2
               )  return int is
         Parent : Session_Type (0);
      begin
         Parent.Holder.Handle := Session;
         if Algorithms_Length  = 0 then
            Handle
            (  Parent,
               To_Stream_Element_Array (Req_CA_RDN),
               NReqs,
               (1..0 => PK_Unknown),
               Retrieve.all
            );
         else
            Handle
            (  Parent,
               To_Stream_Element_Array (Req_CA_RDN),
               NReqs,
               Value (Algorithms, ptrdiff_t (Algorithms_Length)),
               Retrieve.all
            );
         end if;
         return E_Success;
      exception
         when Error : others =>
            return Get_Error_Code (Error);
      end Func;

      procedure Set
                (  Credentials : in out Certificate_Credentials'Class
                )  is
      begin
         Internal (Credentials.Handle, Func'Access);
      end Set;
   end Certificate_Set_Retrieve_Function;

   procedure Certificate_Set_Verify_Flags
             (  Credentials : in out Certificate_Credentials;
                Flags       : unsigned
             )  is
      procedure Internal (Credentials : Address; Flags : unsigned);
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_verify_flags"
             );
   begin
      Internal (Credentials.Handle, Flags);
   end Certificate_Set_Verify_Flags;

   package body Certificate_Set_Verify_Function is
      type Verify_Function is access function
           (  Session : Address
           )  return int;
      pragma Convention (C, Verify_Function);
      procedure Internal
                (  Credentials : Address;
                   Func        : Verify_Function
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_verify_function"
             );
      function Func (Session : Address) return int;
      pragma Convention (C, Func);

      function Func (Session : Address) return int is
         Parent : Session_Type (0);
      begin
         Parent.Holder.Handle := Session;
         Handle (Parent);
         return E_Success;
      exception
         when Error : others =>
            return Get_Error_Code (Error);
      end Func;

      procedure Set
                (  Credentials : in out Certificate_Credentials'Class
                )  is
      begin
         Internal (Credentials.Handle, Func'Access);
      end Set;
   end Certificate_Set_Verify_Function;

   procedure Certificate_Set_Verify_Limits
             (  Credentials : in out Certificate_Credentials;
                Max_Bits    : unsigned := 8200;
                Max_Depth   : unsigned := 5
             )  is
      procedure Internal
                (  Credentials : Address;
                   Max_Bits    : unsigned;
                   Max_Depth   : unsigned
                );
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_verify_limits"
             );
   begin
      Internal (Credentials.Handle, Max_Bits, Max_Depth);
   end Certificate_Set_Verify_Limits;

   procedure Certificate_Set_X509_CRL
             (  Credentials : in out Certificate_Credentials;
                List        : X509_Crl_Array
             )  is
      function Internal
               (  Credentials : Address;
                  List        : Address;
                  Size        : int
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_crl"
             );
      Result : int;
   begin
      Result :=
         Internal
         (  Credentials.Handle,
            List (List'First)'Address,
            List'Length
         );
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
   end Certificate_Set_X509_CRL;

   procedure Certificate_Set_X509_CRL_File
             (  Credentials : in out Certificate_Credentials;
                File        : String;
                Format      : X509_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  File        : char_array;
                  Format      : X509_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_crl_file"
             );
      Result : int;
   begin
      Result := Internal (Credentials.Handle, To_C (File), Format);
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
   end Certificate_Set_X509_CRL_File;

   procedure Certificate_Set_X509_CRL_Mem
             (  Credentials : in out Certificate_Credentials;
                List        : Datum_Array;
                Format      : X509_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  List        : Address;
                  Size        : int;
                  Format      : X509_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_crl_mem"
             );
      Result : int;
   begin
      Result :=
         Internal
         (  Credentials.Handle,
            List (List'First)'Address,
            List'Length,
            Format
         );
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
   end Certificate_Set_X509_CRL_Mem;

   procedure Certificate_Set_X509_CRL_Mem
             (  Credentials : in out Certificate_Credentials;
                Certificate : Stream_Element_Array;
                Format      : X509_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  List        : Address;
                  Size        : int;
                  Format      : X509_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_crl_mem"
             );
      List   : Datum_Array (1..1) := (1 => To_Datum (Certificate));
      Result : int;
   begin
      Result :=
         Internal
         (  Credentials.Handle,
            List (List'First)'Address,
            List'Length,
            Format
         );
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
   end Certificate_Set_X509_CRL_Mem;

   procedure Certificate_Set_X509_Key
             (  Credentials : in out Certificate_Credentials;
                List        : X509_CRL_Array;
                Key         : X509_Privkey
             )  is
      function Internal
               (  Credentials : Address;
                  List        : Address;
                  Size        : int;
                  Key         : X509_Privkey
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_key"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            List (List'First)'Address,
            List'Length,
            Key
      )  );
   end Certificate_Set_X509_Key;

   procedure Certificate_Set_X509_Key_File
             (  Credentials : in out Certificate_Credentials;
                Cert_File   : String;
                Key_File    : String;
                Format      : X509_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Cert_File   : char_array;
                  Key_File    : char_array;
                  Format      : X509_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_key_file"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_C (Cert_File),
            To_C (Key_File),
            Format
      )  );
   end Certificate_Set_X509_Key_File;

   procedure Certificate_Set_X509_Key_File
             (  Credentials : in out Certificate_Credentials;
                Cert_File   : String;
                Key_File    : String;
                Format      : X509_Crt_Fmt;
                Password    : String;
                Flags       : PKCS_Encrypt_Flags
             )  is
      function Internal
               (  Credentials : Address;
                  Cert_File   : char_array;
                  Key_File    : char_array;
                  Format      : X509_Crt_Fmt;
                  Password    : char_array;
                  Flags       : PKCS_Encrypt_Flags
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_key_file2"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_C (Cert_File),
            To_C (Key_File),
            Format,
            To_C (Password),
            Flags
      )  );
   end Certificate_Set_X509_Key_File;

   procedure Certificate_Set_X509_Key_Mem
             (  Credentials : in out Certificate_Credentials;
                Cert        : Stream_Element_Array;
                Key         : Stream_Element_Array;
                Format      : X509_Crt_Fmt;
                Password    : String;
                Flags       : PKCS_Encrypt_Flags
             )  is
      function Internal
               (  Credentials : Address;
                  Cert        : Datum;
                  Key         : Datum;
                  Format      : X509_Crt_Fmt;
                  Password    : char_array;
                  Flags       : PKCS_Encrypt_Flags
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_key_mem2"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_Datum (Cert),
            To_Datum (Key),
            Format,
            To_C (Password),
            Flags
      )  );
   end Certificate_Set_X509_Key_Mem;

   procedure Certificate_Set_X509_Key_Mem
             (  Credentials : in out Certificate_Credentials;
                Cert        : Stream_Element_Array;
                Format      : X509_Crt_Fmt;
                Password    : String;
                Flags       : PKCS_Encrypt_Flags
             )  is
      function Internal
               (  Credentials : Address;
                  Cert        : Datum;
                  Key         : Address;
                  Format      : X509_Crt_Fmt;
                  Password    : char_array;
                  Flags       : PKCS_Encrypt_Flags
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_key_mem2"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_Datum (Cert),
            Null_Address,
            Format,
            To_C (Password),
            Flags
      )  );
   end Certificate_Set_X509_Key_Mem;

   procedure Certificate_Set_X509_Key_Mem
             (  Credentials : in out Certificate_Credentials;
                Cert        : Stream_Element_Array;
                Key         : Stream_Element_Array;
                Format      : X509_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Cert        : Datum;
                  Key         : Datum;
                  Format      : X509_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_key_mem"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_Datum (Cert),
            To_Datum (Key),
            Format
      )  );
   end Certificate_Set_X509_Key_Mem;

   procedure Certificate_Set_X509_Key_Mem
             (  Credentials : in out Certificate_Credentials;
                Cert        : Stream_Element_Array;
                Format      : X509_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Cert        : Datum;
                  Key         : Address;
                  Format      : X509_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_key_mem"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_Datum (Cert),
            Null_Address,
            Format
      )  );
   end Certificate_Set_X509_Key_Mem;

   procedure Certificate_Set_X509_Simple_PKCS12_File
             (  Credentials : in out Certificate_Credentials;
                PKCS12_File : String;
                Format      : X509_Crt_Fmt;
                Password    : String
             )  is
      function Internal
               (  Credentials : Address;
                  PKCS12_File : char_array;
                  Format      : X509_Crt_Fmt;
                  Password    : char_array
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_simple_pkcs12_file"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_C (PKCS12_File),
            Format,
            To_C (Password)
      )  );
   end Certificate_Set_X509_Simple_PKCS12_File;

   procedure Certificate_Set_X509_Simple_PKCS12_Mem
             (  Credentials : in out Certificate_Credentials;
                P12blob     : Stream_Element_Array;
                Format      : X509_Crt_Fmt;
                Password    : String
             )  is
      function Internal
               (  Credentials : Address;
                  P12blob     : Datum;
                  Format      : X509_Crt_Fmt;
                  Password    : char_array
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_simple_pkcs12_mem"
             );
   begin
      Check
      (  Internal
         (  Credentials.Handle,
            To_Datum (P12blob),
            Format,
            To_C (Password)
      )  );
   end Certificate_Set_X509_Simple_PKCS12_Mem;

   procedure Certificate_Set_X509_System_Trust
             (  Credentials : in out Certificate_Credentials
             )  is
      function Internal (Credentials : Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_system_trust"
             );
   begin
      Check (Internal (Credentials.Handle));
   end Certificate_Set_X509_System_Trust;

   procedure Certificate_Set_X509_Trust
             (  Credentials : in out Certificate_Credentials;
                List        : X509_Crt_Array
             )  is
      function Internal
               (  Credentials : Address;
                  List        : Address;
                  Size        : int
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_trust"
             );
      Result : int;
   begin
      Result :=
         Internal
         (  Credentials.Handle,
            List (List'First)'Address,
            List'Length
         );
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
   end Certificate_Set_X509_Trust;

--     procedure Certificate_Set_X509_Trust_Dir
--               (  Credentials : in out Certificate_Credentials;
--                  Directory   : String;
--                  Format      : X509_Crt_Fmt
--               )  is
--        function Internal
--                 (  Credentials : Address;
--                    Directory   : char_array;
--                    Format      : X509_Crt_Fmt
--                 )  return int;
--        pragma Import
--               (  C,
--                  Internal,
--                  "gnutls_certificate_set_x509_trust_dir"
--               );
--        Result : int;
--     begin
--        Result :=
--           Internal
--           (  Credentials.Handle,
--              To_C (Directory),
--              Format
--           );
--        if Result < 0 then
--           Raise_TLS_Error (Result);
--        end if;
--     end Certificate_Set_X509_Trust_Dir;

   procedure Certificate_Set_X509_Trust_File
             (  Credentials : in out Certificate_Credentials;
                File        : String;
                Format      : X509_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Files       : char_array;
                  Format      : X509_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_trust_file"
             );
      Result : int;
   begin
      Result :=
         Internal
         (  Credentials.Handle,
            To_C (File),
            Format
         );
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
   end Certificate_Set_X509_Trust_File;

   procedure Certificate_Set_X509_Trust_Mem
             (  Credentials : in out Certificate_Credentials;
                Certificate : Stream_Element_Array;
                Format      : X509_Crt_Fmt
             )  is
      function Internal
               (  Credentials : Address;
                  Certificate : Datum;
                  Format      : X509_Crt_Fmt
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_set_x509_trust_mem"
             );
      Result : int;
   begin
      Result :=
         Internal
         (  Credentials.Handle,
            To_Datum (Certificate),
            Format
         );
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
   end Certificate_Set_X509_Trust_Mem;

   function Certificate_Type_Get
            (  Session : Session_Type
            )  return Certificate_Type is
      function Internal (Session : Address) return Certificate_Type;
      pragma Import (C, Internal, "gnutls_certificate_type_get");
   begin
      return Internal (Session.Holder.Handle);
   end Certificate_Type_Get;

   function Certificate_Type_Get_ID
            (  Name : String
            )  return Certificate_Type is
      function Internal
               (  Name : char_array
               )  return Certificate_Type;
      pragma Import (C, Internal, "gnutls_certificate_type_get_id");
   begin
      return Internal (To_C (Name));
   end Certificate_Type_Get_ID;

   function Certificate_Type_Get_Name
            (  Type_Of : Certificate_Type
            )  return String is
      function Internal
               (  Type_Of : Certificate_Type
               )  return chars_ptr;
      pragma Import (C, Internal, "gnutls_certificate_type_get_name");
      Result : chars_ptr := Internal (Type_Of);
   begin
      if Result = Null_Ptr then
         Raise_Exception
         (  End_Error'Identity,
            "No certificate type matched"
         );
      else
         return Value (Result);
      end if;
   end Certificate_Type_Get_Name;

   function Certificate_Type_List return Certificate_Type_Array is
      use Certificate_Type_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_certificate_type_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => CRT_Unknown);
      else
         return Value (Result);
      end if;
   end Certificate_Type_List;

   function Certificate_Verification_Status_Print
            (  Status  : Certificate_Status;
               Type_Of : Certificate_Type
            )  return String is
      function Internal
               (  Status  : Certificate_Status;
                  Type_Of : Certificate_Type;
                  Output  : Datum;
                  Flags   : unsigned := 0
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_verification_status_print"
             );
      Output : Datum_Holder;
   begin
      Check (Internal (Status, Type_Of, Output.Data));
      return To_String (Output.Data);
   end Certificate_Verification_Status_Print;

   function Certificate_Verify_Peers
            (  Session   : Session_Type;
               Host_Name : String
            )  return Certificate_Status is
      function Internal
               (  Session   : Address;
                  Host_Name : char_array;
                  Status    : access unsigned
               )  return int;
      pragma Import (C, Internal, "gnutls_certificate_verify_peers3");
      Status : aliased unsigned;
   begin
      Check
      (  Internal
         (  Session.Holder.Handle,
            To_C (Host_Name),
            Status'Access
      )  );
      return Certificate_Status (Status);
   end Certificate_Verify_Peers;

   function Certificate_Verify_Peers (Session : Session_Type)
      return Certificate_Status is
      function Internal
               (  Session : Address;
                  Status  : access unsigned
               )  return int;
      pragma Import (C, Internal, "gnutls_certificate_verify_peers2");
      Status : aliased unsigned;
   begin
      Check (Internal (Session.Holder.Handle, Status'Access));
      return Certificate_Status (Status);
   end Certificate_Verify_Peers;

   function Check_Version (Version : String) return String is
      function Internal (Version : char_array) return chars_ptr;
      pragma Import (C, Internal, "gnutls_check_version");
      Result : chars_ptr := Internal (To_C (Version));
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No version matched");
      else
         return Value (Result);
      end if;
   end Check_Version;

   procedure Check (Error : int) is
   begin
      if Error < 0 then
         Raise_Exception
         (  TLS_Error'Identity,
            StrError (Error) & '[' & int'Image (Error) & ']'
         );
      end if;
   end Check;

   function Cipher_Get (Session : Session_Type)
      return Cipher_Algorithm is
      function Internal
               (  Session : Address
               )  return Cipher_Algorithm;
      pragma Import (C, Internal, "gnutls_cipher_get");
   begin
      return Internal (Session.Holder.Handle);
   end Cipher_Get;

   function Cipher_Get_ID (Name : String) return Cipher_Algorithm is
      function Internal
               (  Name : char_array
               )  return Cipher_Algorithm;
      pragma Import (C, Internal, "gnutls_cipher_get_id");
   begin
      return Internal (To_C (Name));
   end Cipher_Get_ID;

   function Cipher_Get_Name
            (  Algorithm : Cipher_Algorithm
            )  return String is
      function Internal
               (  Algorithm : Cipher_Algorithm
               )  return chars_ptr;
      pragma Import (C, Internal, "gnutls_cipher_get_name");
      Result : chars_ptr := Internal (Algorithm);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No cipher matched");
      else
         return Value (Result);
      end if;
   end Cipher_Get_Name;

   function Cipher_List return Cipher_Algorithm_Array is
      use Cipher_Algorithm_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_cipher_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => Cipher_Unknown);
      else
         return Value (Result);
      end if;
   end Cipher_List;

   function Cipher_Suite_Get_Name
            (  KX     : KX_Algorithm;
               Cipher : Cipher_Algorithm;
               MAC    : MAC_Algorithm
            )  return String is
      function Internal
               (  KX     : KX_Algorithm;
                  Cipher : Cipher_Algorithm;
                  MAC    : MAC_Algorithm
               )  return chars_ptr;
      pragma Import (C, Internal, "gnutls_cipher_suite_get_name");
      Result : chars_ptr := Internal (KX, Cipher, MAC);
   begin
      if Result = Null_Ptr then
         Raise_Exception
         (  End_Error'Identity,
            "No cipher suite matched"
         );
      else
         return Value (Result);
      end if;
   end Cipher_Suite_Get_Name;

   function Compression_Get
            (  Session : Session_Type
            )  return Compression_Method is
      function Internal (Session : Address) return Compression_Method;
      pragma Import (C, Internal, "gnutls_compression_get");
   begin
      return Internal (Session.Holder.Handle);
   end Compression_Get;

   function Compression_Get_ID
            (  Name : String
            )  return Compression_Method is
      function Internal (Name : char_array) return Compression_Method;
      pragma Import (C, Internal, "gnutls_compression_get_id");
   begin
      return Internal (To_C (Name));
   end Compression_Get_ID;

   function Compression_Get_Name
            (  Algorithm : Compression_Method
            )  return String is
      function Internal
               (  Algorithm : Compression_Method
               )  return chars_ptr;
      pragma Import (C, Internal, "gnutls_compression_get_name");
      Result : chars_ptr := Internal (Algorithm);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No cipher matched");
      else
         return Value (Result);
      end if;
   end Compression_Get_Name;

   function Compression_List return Compression_Method_Array is
      use Compression_Method_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_compression_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => Comp_Unknown);
      else
         return Value (Result);
      end if;
   end Compression_List;

   function Create
            (  Type_Of    : Params_Type;
               Parameters : Params_Data
            )  return Abstract_Params'Class is
   begin
      case Type_Of is
         when Params_RSA_Export =>
            return RSA_Params'
                   (  Ada.Finalization.Controlled with Parameters.Data
                   );
         when Params_DH =>
            if Parameters.Deinit = 0 then
               return DH_Params_Ref'
                      (  Ada.Finalization.Controlled
                      with
                         Parameters.Data
                      );
            else
               return DH_Params'
                      (  Ada.Finalization.Controlled
                      with
                         Parameters.Data
                      );
            end if;
         when Params_ECDH =>
            return ECDH_Params'
                   (  Ada.Finalization.Controlled with Parameters.Data
                   );
      end case;
   end Create;

   procedure Credentials_Clear (Session : in out Session_Type) is
      procedure Internal (Session : Address);
      pragma Import (C, Internal, "gnutls_credentials_clear");
   begin
      Internal (Session.Holder.Handle);
   end Credentials_Clear;

--     procedure Credentials_Get
--               (  Credentials : in out Anon_Client_Credentials_Ref;
--                  Session     : Session_Type
--               )  is
--        function Internal
--                 (  Session : Address;
--                    Type_Of : Credentials_Type;
--                    Cred    : access Address
--                 )  return int;
--        pragma Import (C, Internal, "gnutls_credentials_get");
--     begin
--        if 0 /= (Session.Flags and Init_Client) then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "Session is not a client"
--           );
--        end if;
--        Check
--        (  Internal
--           (  Session.Holder.Handle,
--              Crd_Anon,
--              Credentials.Handle'Access
--        )  );
--        if Credentials.Handle /= Null_Address then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "No anon credentials set"
--           );
--        end if;
--     end Credentials_Get;
--
--     procedure Credentials_Get
--               (  Credentials : in out Anon_Server_Credentials_Ref;
--                  Session     : Session_Type
--               )  is
--        function Internal
--                 (  Session : Address;
--                    Type_Of : Credentials_Type;
--                    Cred    : access Address
--                 )  return int;
--        pragma Import (C, Internal, "gnutls_credentials_get");
--     begin
--        if 0 /= (Session.Flags and Init_Server) then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "Session is not a server"
--           );
--        end if;
--        Check
--        (  Internal
--           (  Session.Holder.Handle,
--              Crd_Anon,
--              Credentials.Handle'Access
--        )  );
--        if Credentials.Handle /= Null_Address then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "No anon credentials set"
--           );
--        end if;
--     end Credentials_Get;
--
--     procedure Credentials_Get
--               (  Credentials : in out Certificate_Credentials_Ref;
--                  Session     : Session_Type
--               )  is
--        function Internal
--                 (  Session : Address;
--                    Type_Of : Credentials_Type;
--                    Cred    : access Address
--                 )  return int;
--        pragma Import (C, Internal, "gnutls_credentials_get");
--     begin
--        Check
--        (  Internal
--           (  Session.Holder.Handle,
--              Crd_Certificate,
--              Credentials.Handle'Access
--        )  );
--        if Credentials.Handle /= Null_Address then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "No certificate credentials set"
--           );
--        end if;
--     end Credentials_Get;
--
--     procedure Credentials_Get
--               (  Credentials : in out SRP_Client_Credentials_Ref;
--                  Session     : Session_Type
--               )  is
--        function Internal
--                 (  Session : Address;
--                    Type_Of : Credentials_Type;
--                    Cred    : access Address
--                 )  return int;
--        pragma Import (C, Internal, "gnutls_credentials_get");
--     begin
--        if 0 /= (Session.Flags and Init_Client) then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "Session is not a client"
--           );
--        end if;
--        Check
--        (  Internal
--           (  Session.Holder.Handle,
--              Crd_SRP,
--              Credentials.Handle'Access
--        )  );
--        if Credentials.Handle /= Null_Address then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "No SRP credentials set"
--           );
--        end if;
--     end Credentials_Get;
--
--     procedure Credentials_Get
--               (  Credentials : in out SRP_Server_Credentials_Ref;
--                  Session     : Session_Type
--               )  is
--        function Internal
--                 (  Session : Address;
--                    Type_Of : Credentials_Type;
--                    Cred    : access Address
--                 )  return int;
--        pragma Import (C, Internal, "gnutls_credentials_get");
--     begin
--        if 0 /= (Session.Flags and Init_Server) then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "Session SRP is not a server"
--           );
--        end if;
--        Check
--        (  Internal
--           (  Session.Holder.Handle,
--              Crd_SRP,
--              Credentials.Handle'Access
--        )  );
--        if Credentials.Handle /= Null_Address then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "No credentials set"
--           );
--        end if;
--     end Credentials_Get;

   procedure Credentials_Set
             (  Session     : in out Session_Type;
                Credentials : Anon_Client_Credentials
             )  is
      function Internal
               (  Session : Address;
                  Type_Of : Credentials_Type;
                  Cred    : Address
               )  return int;
      pragma Import (C, Internal, "gnutls_credentials_set");
   begin
      if Credentials.Handle = Null_Address then
         Raise_Exception
         (  Constraint_Error'Identity,
            "Invalid credentials"
         );
      end if;
      Check (Internal (Session.Holder.Handle, Crd_Anon, Credentials.Handle));
   end Credentials_Set;

   procedure Credentials_Set
             (  Session     : in out Session_Type;
                Credentials : Anon_Server_Credentials
             )  is
      function Internal
               (  Session : Address;
                  Type_Of : Credentials_Type;
                  Cred    : Address
               )  return int;
      pragma Import (C, Internal, "gnutls_credentials_set");
   begin
      if Credentials.Handle = Null_Address then
         Raise_Exception
         (  Constraint_Error'Identity,
            "Invalid credentials"
         );
      end if;
      Check
      (  Internal
         (  Session.Holder.Handle,
            Crd_Anon,
            Credentials.Handle
      )  );
   end Credentials_Set;

   procedure Credentials_Set
             (  Session     : in out Session_Type;
                Credentials : Certificate_Credentials
             )  is
      function Internal
               (  Session : Address;
                  Type_Of : Credentials_Type;
                  Cred    : Address
               )  return int;
      pragma Import (C, Internal, "gnutls_credentials_set");
   begin
      if Credentials.Handle = Null_Address then
         Raise_Exception
         (  Constraint_Error'Identity,
            "Invalid credentials"
         );
      end if;
      Check
      (  Internal
         (  Session.Holder.Handle,
            Crd_Certificate,
            Credentials.Handle
      )  );
   end Credentials_Set;

--     procedure Credentials_Set
--               (  Session     : in out Session_Type;
--                  Credentials : SRP_Client_Credentials
--               )  is
--        function Internal
--                 (  Session : Address;
--                    Type_Of : Credentials_Type;
--                    Cred    : Address
--                 )  return int;
--        pragma Import (C, Internal, "gnutls_credentials_set");
--     begin
--        if Credentials.Handle = Null_Address then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "Invalid credentials"
--           );
--        end if;
--        Check (Internal (Session.Holder.Handle, Crd_SRP, Credentials.Handle));
--     end Credentials_Set;
--
--     procedure Credentials_Set
--               (  Session     : in out Session_Type;
--                  Credentials : SRP_Server_Credentials
--               )  is
--        function Internal
--                 (  Session : Address;
--                    Type_Of : Credentials_Type;
--                    Cred    : Address
--                 )  return int;
--        pragma Import (C, Internal, "gnutls_credentials_set");
--     begin
--        if Credentials.Handle = Null_Address then
--           Raise_Exception
--           (  Constraint_Error'Identity,
--              "Invalid credentials"
--           );
--        end if;
--        Check (Internal (Session.Holder.Handle, Crd_SRP, Credentials.Handle));
--     end Credentials_Set;

   function Curve_To_Bits (Curve : ECC_Curve) return unsigned is
   begin
      return 2**31 or ECC_Curve'Pos (Curve);
   end Curve_To_Bits;

   function DB_Check_Entry
            (  Session       : Session_Type;
               Session_Entry : Stream_Element_Array
            )  return Boolean is
      function Internal
               (  Session       : Address;
                  Session_Entry : Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_db_check_entry");
      Result : int := Internal
                      (  Session.Holder.Handle,
                         To_Datum (Session_Entry)
                      );
   begin
      case Result is
         when E_Success =>
            return True;
         when E_Expired =>
            return False;
         when others =>
            Raise_TLS_Error (Result);
            return False;
      end case;
   end DB_Check_Entry;

   function DB_Check_Entry_Time
            (  DB_Entry : Stream_Element_Array
            )  return Time is
      function Internal (DB_Entry : Datum) return time_t;
      pragma Import (C, Internal, "gnutls_db_check_entry_time");
      Result : time_t := Internal (To_Datum (DB_Entry));
   begin
      if Result = 0 then
         Raise_Exception
         (  Time_Error'Identity,
            "Error database entry time"
         );
      end if;
      return To_Time (Result);
   end DB_Check_Entry_Time;

   function DB_Get_Default_Cache_Expiration return Duration is
      function Internal return unsigned;
      pragma Import
             (  C,
                Internal,
                "gnutls_db_get_default_cache_expiration"
             );
   begin
      return Duration (Internal);
   end DB_Get_Default_Cache_Expiration;

   procedure DB_Remove_Session (Session : in out Session_Type) is
      procedure Internal (Session : Address);
      pragma Import (C, Internal, "gnutls_db_remove_session");
   begin
      Internal (Session.Holder.Handle);
   end DB_Remove_Session;

   procedure DB_Set_Cache_Expiration
             (  Session : in out Session_Type;
                Timeout : Duration
             )  is
      procedure Internal (Session : Address; Seconds : unsigned);
      pragma Import (C, Internal, "gnutls_db_set_cache_expiration");
   begin
      if Timeout < 1.0 then
         Raise_Exception
         (  Time_Error'Identity,
            "Timeout is too small"
         );
      end if;
      Internal (Session.Holder.Handle, unsigned (Timeout));
   exception
      when Constraint_Error =>
         Raise_Exception
         (  Time_Error'Identity,
            "Timeout is too large"
         );
   end DB_Set_Cache_Expiration;

   package DB_Conversions is
      new Address_To_Access_Conversions (DB_Request'Class);

   type DB_Remove_Func_Ptr is access function
        (  Request : Address;
           Key     : Datum
        )  return int;
   pragma Convention (C, DB_Remove_Func_Ptr);

   function DB_Remove_Func
            (  Request : Address;
               Key     : Datum
            )  return int;
   pragma Convention (C, DB_Remove_Func);

   function DB_Remove_Func
            (  Request : Address;
               Key     : Datum
            )  return int is
      use DB_Conversions;
      Data : Object_Pointer := To_Pointer (Request);
   begin
      if Data = null then
         return E_Internal_Error;
      else
         Remove (Data.all, To_Stream_Element_Array (Key));
         return E_Success;
      end if;
   exception
      when Error : others =>
         return Get_Error_Code (Error);
   end DB_Remove_Func;

   type DB_Retrieve_Func_Ptr is access function
        (  Request : Address;
           Key     : Datum
        )  return Datum;
   pragma Convention (C, DB_Retrieve_Func_Ptr);

   function DB_Retrieve_Func
            (  Request : Address;
               Key     : Datum
            )  return Datum;
   pragma Convention (C, DB_Retrieve_Func);

   function DB_Retrieve_Func
            (  Request : Address;
               Key     : Datum
            )  return Datum is
      use DB_Conversions;
      Data : Object_Pointer := To_Pointer (Request);
   begin
      if Data = null then
         return (Null_Address, 0);
      else
         return New_Datum
                (  Retrieve (Data, To_Stream_Element_Array (Key))
                );
      end if;
   exception
      when Error : others =>
         return (Null_Address, 0);
   end DB_Retrieve_Func;

   type DB_Store_Func_Ptr is access function
        (  Request : Address;
           Key     : Datum
        )  return int;
   pragma Convention (C, DB_Store_Func_Ptr);

   function DB_Store_Func
            (  Request : Address;
               Key     : Datum
            )  return int;
   pragma Convention (C, DB_Store_Func);

   function DB_Store_Func
            (  Request : Address;
               Key     : Datum
            )  return int is
      use DB_Conversions;
      Data : Object_Pointer := To_Pointer (Request);
   begin
      if Data = null then
         return E_Internal_Error;
      else
         Store (Data.all, To_Stream_Element_Array (Key));
         return E_Success;
      end if;
   exception
      when Error : others =>
         return Get_Error_Code (Error);
   end DB_Store_Func;

   procedure DB_Set_Functions
             (  Session : in out Session_Type;
                Request : access DB_Request'Class
             )  is
      procedure Set_Remove
                (  Session : Address;
                   Ptr     : DB_Remove_Func_Ptr
                );
      pragma Import (C, Set_Remove, "gnutls_db_set_remove_function");

      procedure Set_Retrieve
                (  Session : Address;
                   Ptr     : DB_Retrieve_Func_Ptr
                );
      pragma Import
             (  C,
                Set_Retrieve,
                "gnutls_db_set_retrieve_function"
             );

      procedure Set_Ptr (Session : Address; Ptr : Address);
      pragma Import (C, Set_Ptr, "gnutls_db_set_ptr");

      procedure Set_Store (Session : Address; Ptr : DB_Store_Func_Ptr);
      pragma Import (C, Set_Store, "gnutls_db_set_store_function");
   begin
      Set_Ptr      (Session.Holder.Handle, Request.all'Address);
      Set_Remove   (Session.Holder.Handle, DB_Remove_Func'Access);
      Set_Retrieve (Session.Holder.Handle, DB_Retrieve_Func'Access);
      Set_Store    (Session.Holder.Handle, DB_Store_Func'Access);
   end DB_Set_Functions;

   procedure DB_Get_Group
             (  Session   : Session_Type;
                Raw_Gen   : in out Datum_Holder'Class;
                Raw_Prime : in out Datum_Holder'Class
             )  is
      function Internal
               (  Session   : Address;
                  Raw_Gen   : access Datum;
                  Raw_Prime : access Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_dh_get_group");
   begin
      Finalize (Raw_Gen);
      Finalize (Raw_Prime);
      Check
      (  Internal
         (  Session.Holder.Handle,
            Raw_Gen.Data'Access,
            Raw_Prime.Data'Access
      )  );
   end DB_Get_Group;

   function DB_Get_Peers_Public_Bits
            (  Session : Session_Type
            )  return Natural is
      function Internal (Session : Address) return int;
      pragma Import (C, Internal, "gnutls_dh_get_peers_public_bits");
      Result : int := Internal (Session.Holder.Handle);
   begin
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
      return Natural (Result);
   end DB_Get_Peers_Public_Bits;

   function DB_Get_Prime_Bits (Session : Session_Type) return Natural is
      function Internal (Session : Address) return int;
      pragma Import (C, Internal, "gnutls_dh_get_prime_bits");
      Result : int := Internal (Session.Holder.Handle);
   begin
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
      return Natural (Result);
   end DB_Get_Prime_Bits;

   procedure DB_Get_Pubkey
             (  Session : Session_Type;
                Raw_Key : in out Datum_Holder'Class
             )  is
      function Internal
               (  Session : Address;
                  Raw_Key : access Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_dh_get_pubkey");
   begin
      Finalize (Raw_Key);
      Check (Internal (Session.Holder.Handle, Raw_Key.Data'Access));
   end DB_Get_Pubkey;

   function DB_Get_Secret_Bits (Session : Session_Type)
      return Natural is
      function Internal (Session : Address) return int;
      pragma Import (C, Internal, "gnutls_dh_get_secret_bits");
      Result : int := Internal (Session.Holder.Handle);
   begin
      if Result < 0 then
         Raise_TLS_Error (Result);
      end if;
      return Natural (Result);
   end DB_Get_Secret_Bits;

   procedure DH_Params_Export2_PKCS3
             (  Params : DH_Params;
                Format : X509_Crt_Fmt;
                Result : in out Datum_Holder'Class
             )  is
      function Internal
               (  Params : Address;
                  Format : X509_Crt_Fmt;
                  Result : access Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_dh_params_export2_pkcs3");
   begin
      Finalize (Result);
      Check (Internal (Params.Handle, Format, Result.Data'Access));
   end DH_Params_Export2_PKCS3;

   procedure DH_Params_Export_PKCS3
             (  Params  : DH_Params;
                Format  : X509_Crt_Fmt;
                Buffer  : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
      function Internal
               (  Params : Address;
                  Format : X509_Crt_Fmt;
                  Buffer : Address;
                  Size   : access size_t
               )  return int;
      pragma Import (C, Internal, "gnutls_dh_params_export_pkcs3");
      Size   : aliased size_t;
      Result : int;
   begin
      if Pointer not in Buffer'First..Buffer'Last then
         Raise_Exception
         (  Layout_Error'Identity,
            "Illegal pointer, out of the buffer"
         );
      end if;
      Size := size_t (Buffer'Last - Pointer + 1);
      Result :=
         Internal
         (  Params.Handle,
            Format,
            Buffer (Pointer)'Address,
            Size'Access
         );
      if Result >= 0 then
         Pointer := Pointer + Stream_Element_Count (Size - 1);
      elsif Result = E_Short_Memory_Buffer then
         Raise_Exception
         (  Layout_Error'Identity,
            "No room for output"
         );
      else
         Raise_TLS_Error (Result);
      end if;
   end DH_Params_Export_PKCS3;

   procedure DH_Params_Export_Raw
             (  Params    : DH_Params;
                Prime     : in out Datum_Holder'Class;
                Generator : in out Datum_Holder'Class
             )  is
      Bits : Natural;
   begin
      DH_Params_Export_Raw (Params, Prime, Generator, Bits);
   end DH_Params_Export_Raw;

   procedure DH_Params_Export_Raw
             (  Params    : DH_Params;
                Prime     : in out Datum_Holder'Class;
                Generator : in out Datum_Holder'Class;
                Bits      : out Natural
             )  is
      function Internal
               (  Params    : Address;
                  Prime     : Datum;
                  Generator : Datum;
                  Bits      : access unsigned
               )  return int;
      pragma Import (C, Internal, "gnutls_dh_params_export_raw");
      Size : aliased unsigned;
   begin
      Check
      (  Internal
         (  Params.Handle,
            Prime.Data,
            Generator.Data,
            Size'Access
      )  );
      Bits := Natural (Size);
   end DH_Params_Export_Raw;

   procedure DH_Params_Generate2
             (  Params : in out DH_Params;
                Bits   : Positive
             )  is
      function Internal (Params : Address; Bits : unsigned) return int;
      pragma Import (C, Internal, "gnutls_dh_params_generate2");
   begin
      Check (Internal (Params.Handle,  unsigned (Bits)));
   end DH_Params_Generate2;

   procedure DH_Params_Import_PKCS3
             (  Params       : in out DH_Params;
                PKCS3_Params : Stream_Element_Array;
                Format       : X509_Crt_Fmt
             )  is
      function Internal
               (  Params       : Address;
                  PKCS3_Params : Datum;
                  Format       : X509_Crt_Fmt
               )  return int;
      pragma Import (C, Internal, "gnutls_dh_params_import_pkcs3");
   begin
      Check (Internal (Params.Handle, To_Datum (PKCS3_Params), Format));
   end DH_Params_Import_PKCS3;

   procedure DH_Params_Import_Raw
             (  Params    : in out DH_Params;
                Prime     : Stream_Element_Array;
                Generator : Stream_Element_Array
             )  is
      function Internal
               (  Params    : Address;
                  Prime     : Datum;
                  Generator : Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_dh_params_import_raw");
   begin
      Check
      (  Internal
         (  Params.Handle,
            To_Datum (Prime),
            To_Datum (Generator)
      )  );
   end DH_Params_Import_Raw;

   procedure DB_Set_Prime_Bits
             (  Session : in out Session_Type;
                Bits    : Positive
             )  is
      procedure Internal (Session : Address; Bits : unsigned);
      pragma Import (C, Internal, "gnutls_dh_set_prime_bits");
   begin
      Internal (Session.Holder.Handle, unsigned (Bits));
   end DB_Set_Prime_Bits;

   function Digest_Get_ID (Name : String) return Digest_Algorithm is
      function Internal (Name : char_array) return Digest_Algorithm;
      pragma Import (C, Internal, "gnutls_digest_get_id");
   begin
      return Internal (To_C (Name));
   end Digest_Get_ID;

   function Digest_Get_Name
            (  Algorithm : Digest_Algorithm
            )  return String is
      function Internal (Algorithm : Digest_Algorithm) return chars_ptr;
      pragma Import (C, Internal, "gnutls_digest_get_name");
      Result : chars_ptr := Internal (Algorithm);
   begin
      if Result = Null_Ptr then
         Raise_Exception
         (  End_Error'Identity,
            "No digest algorithm matched"
         );
      end if;
      return Value (Result);
   end Digest_Get_Name;

   function Digest_List return Digest_Algorithm_Array is
      use Digest_Algorithm_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_digest_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => Dig_Unknown);
      else
         return Value (Result);
      end if;
   end Digest_List;

   function ECC_Curve_Get (Session : Session_Type) return ECC_Curve is
      function Internal (Session : Address) return ECC_Curve;
      pragma Import (C, Internal, "gnutls_ecc_curve_get");
   begin
      return Internal (Session.Holder.Handle);
   end ECC_Curve_Get;

   function ECC_Curve_Get_Name (Curve : ECC_Curve) return String is
      function Internal (Curve : ECC_Curve) return chars_ptr;
      pragma Import (C, Internal, "gnutls_ecc_curve_get_name");
      Result : chars_ptr := Internal (Curve);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No ECC curve matched");
      end if;
      return Value (Result);
   end ECC_Curve_Get_Name;

   function ECC_Curve_Get_Size (Curve : ECC_Curve) return Natural is
      function Internal (Curve : ECC_Curve) return int;
      pragma Import (C, Internal, "gnutls_ecc_curve_get_size");
   begin
      return Natural (Internal (Curve));
   end ECC_Curve_Get_Size;

   function ECC_Curve_List return ECC_Curve_Array is
      use ECC_Curve_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_ecc_curve_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => ECC_Curve_Invalid);
      else
         return Value (Result);
      end if;
   end ECC_Curve_List;

   function Error_Is_Fatal (Error : int) return Boolean is
      function Internal (Error : int) return int;
      pragma Import (C, Internal, "gnutls_error_is_fatal");
   begin
      return Internal (Error) /= 0;
   end Error_Is_Fatal;

   procedure Finalize (Data : in out Datum_Holder) is
      procedure Internal (Ptr : Address);
      pragma Import (C, Internal, "gnutls_free");
   begin
      if Data.Data.Data /= Null_Address then
         Internal (Data.Data.Data);
         Data.Data.Data := Null_Address;
      end if;
      Data.Data.Size := 0;
   end Finalize;

   procedure Finalize (Credentials : in out Anon_Client_Credentials) is
      function Internal (Ptr : Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_anon_free_client_credentials"
             );
   begin
      if Credentials.Handle /= Null_Address then
         Check (Internal (Credentials.Handle));
         Credentials.Handle := Null_Address;
      end if;
   end Finalize;

   procedure Finalize
             (  Credentials : in out Anon_Client_Credentials_Ref
             )  is
   begin
      null;
   end Finalize;

   procedure Finalize (Credentials : in out Anon_Server_Credentials) is
      function Internal (Ptr : Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_anon_free_server_credentials"
             );
   begin
      if Credentials.Handle /= Null_Address then
         Check (Internal (Credentials.Handle));
         Credentials.Handle := Null_Address;
      end if;
   end Finalize;

   procedure Finalize
             (  Credentials : in out Anon_Server_Credentials_Ref
             )  is
   begin
      null;
   end Finalize;

   procedure Finalize (Session : in out Session_Holder) is
      procedure Internal (Ptr : Address);
      pragma Import (C, Internal, "gnutls_deinit");
   begin
      if Session.Handle /= Null_Address and then Session.Flags /= 0 then
         Internal (Session.Handle);
         Session.Handle := Null_Address;
      end if;
   end Finalize;

   procedure Finalize (Credentials : in out Certificate_Credentials) is
      function Internal (Ptr : Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_free_credentials"
             );
   begin
     if Credentials.Handle /= Null_Address then
        Check (Internal (Credentials.Handle));
        Credentials.Handle := Null_Address;
     end if;
   end Finalize;

   procedure Finalize
             (  Credentials : in out Certificate_Credentials_Ref
             )  is
   begin
      null;
   end Finalize;

   procedure Finalize (Data : in out DH_Params) is
      procedure Internal (Ptr : Address);
      pragma Import (C, Internal, "gnutls_dh_params_deinit");
   begin
      if Data.Handle /= Null_Address then
         Internal (Data.Handle);
         Data.Handle := Null_Address;
      end if;
   end Finalize;

   procedure Finalize (Data : in out DH_Params_Ref) is
   begin
      null;
   end Finalize;

   procedure Finalize (Data : in out ECDH_Params) is
   begin
      null;
   end Finalize;

   procedure Finalize (Data : in out RSA_Params) is
   begin
      null;
   end Finalize;

--     procedure Finalize (Credentials : in out SRP_Client_Credentials) is
--        function Internal (Ptr : Address) return int;
--        pragma Import
--               (  C,
--                  Internal,
--                  "gnutls_srp_free_client_credentials"
--               );
--     begin
--        if Credentials.Handle /= Null_Address then
--           Check (Internal (Credentials.Handle));
--           Credentials.Handle := Null_Address;
--        end if;
--     end Finalize;
--
--     procedure Finalize
--               (  Credentials : in out SRP_Client_Credentials_Ref
--               )  is
--     begin
--        null;
--     end Finalize;
--
--     procedure Finalize (Credentials : in out SRP_Server_Credentials) is
--        function Internal (Ptr : Address) return int;
--        pragma Import
--               (  C,
--                  Internal,
--                  "gnutls_srp_free_server_credentials"
--               );
--     begin
--        if Credentials.Handle /= Null_Address then
--           Check (Internal (Credentials.Handle));
--           Credentials.Handle := Null_Address;
--        end if;
--     end Finalize;
--
--     procedure Finalize
--               (  Credentials : in out SRP_Server_Credentials_Ref
--               )  is
--     begin
--        null;
--     end Finalize;

   procedure Finalize (Cache : in out Priority) is
      procedure Internal (Cache : Address);
      pragma Import (C, Internal, "gnutls_priority_deinit");
   begin
      if Cache.Handle /= Null_Address then
         Internal (Cache.Handle);
         Cache.Handle := Null_Address;
      end if;
   end Finalize;

   function Fingerprint
            (  Algorithm : Digest_Algorithm;
               Data      : Stream_Element_Array
            )  return Stream_Element_Array is
      function Internal
               (  Algorithm : Digest_Algorithm;
                  Data      : Datum;
                  Result    : access Datum;
                  Size      : access size_t
               )  return int;
      pragma Import (C, Internal, "gnutls_fingerprint");
      Result   : aliased Datum_Holder;
      Size     : aliased size_t;
   begin
      Check
      (  Internal
         (  Algorithm,
            To_Datum (Data),
            Result.Data'Access,
            Size'Access
      )  );
      return To_Stream_Element_Array (Result.Data);
   end Fingerprint;

   function Get_Error_Code (Error : Exception_Occurrence) return int is
   begin
      if Exception_Identity (Error) = TLS_Error'Identity then
         declare
            Text : String := Exception_Message (Error);
         begin
            for Index in reverse Text'Range loop
               if Text (Index) = '[' then
                  return int'Value (Text (Index + 1..Text'Last));
               end if;
            end loop;
         exception
            when others =>
               null;
         end;
      end if;
      return E_Internal_Error;
   end Get_Error_Code;

   package body Global_Set_Audit_Log_Function is
      type Audit_Log_Func_Ptr is access procedure
           (  Session : Address;
              Text    : chars_ptr
           );
      pragma Convention (C, Audit_Log_Func_Ptr);

      procedure Audit_Log_Func (Session : Address; Text : chars_ptr);
      pragma Convention (C, Audit_Log_Func);

      procedure Audit_Log_Func (Session : Address; Text : chars_ptr) is
      begin
         if Session = Null_Address then
            Log (Value (Text));
         else
            declare
               Current : Session_Type (0);
            begin
               Current.Holder.Handle := Session;
               Log (Current, Value (Text));
            end;
         end if;
      end Audit_Log_Func;

      procedure Set is
         procedure Internal (Func : Audit_Log_Func_Ptr);
         pragma Import
                (  C,
                   Internal,
                   "gnutls_global_set_audit_log_function"
                );
      begin
         Internal (Audit_Log_Func'Access);
      end Set;
   end Global_Set_Audit_Log_Function;

   package body Global_Set_Log_Function is
      type Log_Func_Ptr is access procedure
           (  Level : int;
              Text  : chars_ptr
           );
      pragma Convention (C, Log_Func_Ptr);

      procedure Log_Func (Level : int; Text : chars_ptr);
      pragma Convention (C, Log_Func);

      procedure Log_Func (Level : int; Text : chars_ptr) is
      begin
         Log (Level, Value (Text));
      end Log_Func;

      procedure Set (Level : int) is
         procedure Set_Func (Func : Log_Func_Ptr);
         pragma Import
                (  C,
                   Set_Func,
                   "gnutls_global_set_log_function"
                );
         procedure Set_Level (Level : int);
         pragma Import
                (  C,
                   Set_Level,
                   "gnutls_global_set_log_level"
                );
      begin
         Set_Level (Level);
         Set_Func  (Log_Func'Access);
      end Set;
   end Global_Set_Log_Function;

   function Handshake (Session : Session_Type) return Boolean is
      function Internal (Session : Address) return int;
      pragma Import (C, Internal, "gnutls_handshake");
      Code : int := Internal (Session.Holder.Handle);
   begin
      case Code is
         when 0..int'Last =>
            return False;
         when E_Again |
              E_Got_Application_Data |
              E_Interrupted |
              E_Warning_Alert_Received =>
            return True;
         when others =>
            Raise_TLS_Error (Code);
            return True;
      end case;
   end Handshake;

   function Handshake_Description_Get_Name
            (  Description : Handshake_Description
            )  return String is
      function Internal
               (  Description : Handshake_Description
               )  return chars_ptr;
      pragma Import
             (  C,
                Internal,
                "gnutls_handshake_description_get_name"
             );
      Result : chars_ptr := Internal (Description);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No description matched");
      else
         return Value (Result);
      end if;
   end Handshake_Description_Get_Name;

   function Handshake_Get_Last_In
            (  Session : Session_Type
            )  return Handshake_Description is
      function Internal (Session : Address)
         return Handshake_Description;
      pragma Import (C, Internal, "gnutls_handshake_get_last_in");
   begin
      return Internal (Session.Holder.Handle);
   end Handshake_Get_Last_In;

   function Handshake_Get_Last_Out
            (  Session : Session_Type
            )  return Handshake_Description is
      function Internal (Session : Address)
         return Handshake_Description;
      pragma Import (C, Internal, "gnutls_handshake_get_last_out");
   begin
      return Internal (Session.Holder.Handle);
   end Handshake_Get_Last_Out;

   package body Handshake_Set_Hook_Function is
      type Handshake_Hook_Ptr is access function
           (  Session  : Address;
              Message  : Handshake_Description;
              Post     : Hook_Type;
              Incoming : unsigned
           )  return int;
      pragma Convention (C, Handshake_Hook_Ptr);

      function Handshake_Hook
               (  Session  : Address;
                  Message  : Handshake_Description;
                  Post     : Hook_Type;
                  Incoming : unsigned
               )  return int;
      pragma Convention (C, Handshake_Hook);

      function Handshake_Hook
               (  Session  : Address;
                  Message  : Handshake_Description;
                  Post     : Hook_Type;
                  Incoming : unsigned
               )  return int is
         Current : Session_Type (0);
      begin
         Current.Holder.Handle := Session;
         Hook (Current, Message, Post, Incoming);
         return E_Success;
      exception
         when Error : others =>
            return Get_Error_Code (Error);
      end Handshake_Hook;

      procedure Set
                (  Session : in out Session_Type;
                   Message : Handshake_Description;
                   Post    : Hook_Type
                )  is
         procedure Internal
                   (  Session : Address;
                      Message : Handshake_Description;
                      Post    : Hook_Type;
                      Hook    : Handshake_Hook_Ptr
                   );
         pragma Import
                (  C,
                   Internal,
                   "gnutls_handshake_set_hook_function"
                );
      begin
         Internal
         (  Session.Holder.Handle,
            Message,
            Post,
            Handshake_Hook'Access
         );
      end Set;
   end Handshake_Set_Hook_Function;

   procedure Handshake_Set_Max_Packet_Length
             (  Session : in out Session_Type;
                Max     : size_t
             )  is
      procedure Internal (Session : Address; Max : size_t);
      pragma Import
             (  C,
                Internal,
                "gnutls_handshake_set_max_packet_length"
             );
   begin
      Internal (Session.Holder.Handle, Max);
   end Handshake_Set_Max_Packet_Length;

   package body Handshake_Set_Post_Client_Hello_Function is
      type Handshake_Hook_Ptr is access function
           (  Session : Address
           )  return int;
      pragma Convention (C, Handshake_Hook_Ptr);

      function Handshake_Hook (Session : Address) return int;
      pragma Convention (C, Handshake_Hook);

      function Handshake_Hook (Session : Address) return int is
         Current : Session_Type (0);
         Hold    : Boolean;
      begin
         Current.Holder.Handle := Session;
         Hello (Current, Hold);
         if Hold then
            return E_Interrupted;
         else
            return E_Success;
         end if;
      exception
         when Error : others =>
            return Get_Error_Code (Error);
      end Handshake_Hook;

      procedure Set (Session : in out Session_Type) is
         procedure Internal
                   (  Session : Address;
                      Hook    : Handshake_Hook_Ptr
                   );
         pragma Import
                (  C,
                   Internal,
                   "gnutls_handshake_set_post_client_hello_function"
                );
      begin
         Internal (Session.Holder.Handle, Handshake_Hook'Access);
      end Set;
   end Handshake_Set_Post_Client_Hello_Function;

   procedure Handshake_Set_Random
             (  Session : in out Session_Type;
                Random  : Random_Value
             )  is
      function Internal (Session : Address; Random : Datum) return int;
      pragma Import (C, Internal, "gnutls_handshake_set_random");
   begin
      Check (Internal (Session.Holder.Handle, To_Datum (Random)));
   end Handshake_Set_Random;

   procedure Handshake_Set_Timeout
             (  Session : Session_Type;
                Timeout : Duration := Duration'First
             )  is
      procedure Internal (Session : Address; Timeout : unsigned);
      pragma Import (C, Internal, "gnutls_handshake_set_timeout");
   begin
      if Timeout < 0.0 then
         Internal (Session.Holder.Handle, To_unsigned (-1));
      else
         Internal (Session.Holder.Handle, unsigned (Timeout * 1_000.0));
      end if;
   exception
      when Constraint_Error =>
         Raise_Exception
         (  Time_Error'Identity,
            "Illegal timeout value"
         );
   end Handshake_Set_Timeout;

   procedure Initialize
             (  Credentials : in out Anon_Client_Credentials
             )  is
      function Internal (Credentials : access Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_anon_allocate_client_credentials"
             );
   begin
      Check (Internal (Credentials.Handle'Access));
   end Initialize;

   procedure Initialize
             (  Credentials : in out Anon_Client_Credentials_Ref
             )  is
   begin
      null;
   end Initialize;

   procedure Initialize
             (  Credentials : in out Anon_Server_Credentials
             )  is
      function Internal (Credentials : access Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_anon_allocate_server_credentials"
             );
   begin
      Check (Internal (Credentials.Handle'Access));
   end Initialize;

   procedure Initialize
             (  Credentials : in out Anon_Server_Credentials_Ref
             )  is
   begin
      null;
   end Initialize;

   procedure Initialize (Data : in out DH_Params) is
      function Internal (Data : access Address) return int;
      pragma Import (C, Internal, "gnutls_dh_params_init");
   begin
      Check (Internal (Data.Handle'Access));
   end Initialize;

   procedure Initialize (Data : in out DH_Params_Ref) is
   begin
      null;
   end Initialize;

   procedure Initialize (Data : in out ECDH_Params) is
   begin
      null;
   end Initialize;

   procedure Initialize (Data : in out RSA_Params) is
   begin
      null;
   end Initialize;

   procedure Initialize (Session : in out Session_Holder) is
      function Internal
               (  Session : access Address;
                  Flags   : unsigned
               )  return int;
      pragma Import (C, Internal, "gnutls_init");
   begin
      if Session.Handle = Null_Address and then Session.Flags /= 0 then
         Check
         (  Internal
            (  Session.Handle'Access,
               Init_Flags'Pos (Session.Flags)
         )  );
      end if;
   end Initialize;

   procedure Initialize
             (  Credentials : in out Certificate_Credentials
             )  is
      function Internal (Credentials : access Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_certificate_allocate_credentials"
             );
   begin
      Check (Internal (Credentials.Handle'Access));
   end Initialize;

   procedure Initialize
             (  Credentials : in out Certificate_Credentials_Ref
             )  is
   begin
      null;
   end Initialize;

--     procedure Initialize
--               (  Credentials : in out SRP_Client_Credentials
--               )  is
--        function Internal (Credentials : access Address) return int;
--        pragma Import
--               (  C,
--                  Internal,
--                  "gnutls_srp_allocate_client_credentials"
--               );
--     begin
--        Check (Internal (Credentials.Handle'Access));
--     end Initialize;
--
--     procedure Initialize
--               (  Credentials : in out SRP_Client_Credentials_Ref
--               )  is
--     begin
--        null;
--     end Initialize;
--
--     procedure Initialize
--               (  Credentials : in out SRP_Server_Credentials
--               )  is
--        function Internal (Credentials : access Address) return int;
--        pragma Import
--               (  C,
--                  Internal,
--                  "gnutls_srp_allocate_server_credentials"
--               );
--     begin
--        Check (Internal (Credentials.Handle'Access));
--     end Initialize;
--
--     procedure Initialize
--               (  Credentials : in out SRP_Server_Credentials_Ref
--               )  is
--     begin
--        null;
--     end Initialize;

   procedure Initialize
             (  Cache      : in out Priority;
                Parameters : String := "PERFORMANCE:%SERVER_PRECEDENCE"
             )  is
      use System.Storage_Elements;
      function Internal
               (  Cache      : access Address;
                  Priorities : char_array;
                  Err_Pos    : access Address
               )  return int;
      pragma Import (C, Internal, "gnutls_priority_init");
      Value  : char_array := To_C (Parameters);
      Error  : aliased Address;
      Result : int;
   begin
      if Cache.Handle /= Null_Address then
         Raise_Exception
         (  Constraint_Error'Identity,
            "Invalid priority cache (alread initialized)"
         );
      end if;
      Result := Internal (Cache.Handle'Access, Value, Error'Access);
      if Result < 0 then
         Raise_Exception
         (  TLS_Error'Identity,
            (  StrError (Result)
            &  '['
            &  int'Image (Result)
            &  "] at"
            &  Integer'Image
               (  Parameters'First
               +  Integer (Error - Value'Address)
         )  )  );
      end if;
   end Initialize;

   function Is_Initialized (Cache : Priority) return Boolean is
   begin
      return Cache.Handle /= Null_Address;
   end Is_Initialized;

   function KX_Get (Session : Session_Type) return KX_Algorithm is
      function Internal (Session : Address) return KX_Algorithm;
      pragma Import (C, Internal, "gnutls_kx_get");
   begin
      return Internal (Session.Holder.Handle);
   end KX_Get;

   function KX_Get_ID (Name : String) return KX_Algorithm is
      function Internal (Name : char_array) return KX_Algorithm;
      pragma Import (C, Internal, "gnutls_kx_get_id");
   begin
      return Internal (To_C (Name));
   end KX_Get_ID;

   function KX_Get_Name (Algorithm : KX_Algorithm) return String is
      function Internal (Algorithm : KX_Algorithm) return chars_ptr;
      pragma Import (C, Internal, "gnutls_kx_get_name");
      Result : chars_ptr := Internal (Algorithm);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No algorithm matched");
      else
         return Value (Result);
      end if;
   end KX_Get_Name;

   function KX_List return KX_Algorithm_Array is
      use KX_Algorithm_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_kx_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => KX_Unknown);
      else
         return Value (Result);
      end if;
   end KX_List;

   function MAC_Get (Session : Session_Type) return MAC_Algorithm is
      function Internal (Session : Address) return MAC_Algorithm;
      pragma Import (C, Internal, "gnutls_mac_get");
   begin
      return Internal (Session.Holder.Handle);
   end MAC_Get;

   function MAC_Get_ID (Name : String) return MAC_Algorithm is
      function Internal (Name : char_array) return MAC_Algorithm;
      pragma Import (C, Internal, "gnutls_mac_get_id");
   begin
      return Internal (To_C (Name));
   end MAC_Get_ID;

   function MAC_Get_Name (Algorithm : MAC_Algorithm) return String is
      function Internal (Algorithm : MAC_Algorithm) return chars_ptr;
      pragma Import (C, Internal, "gnutls_mac_get_name");
      Result : chars_ptr := Internal (Algorithm);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No algorithm matched");
      else
         return Value (Result);
      end if;
   end MAC_Get_Name;

   function MAC_List return MAC_Algorithm_Array is
      use MAC_Algorithm_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_mac_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => MAC_Unknown);
      else
         return Value (Result);
      end if;
   end MAC_List;

   function New_Datum (Data : Stream_Element_Array) return Datum is
      Result : Datum;
   begin
      if Data'Length > 0 then
         declare
            function Internal (Size : size_t) return Address;
            pragma Import (C, Internal, "gnutls_malloc");
            subtype Copy_Array is Stream_Element_Array (Data'Range);
            Copy : Copy_Array;
            for Copy'Address use Internal (Data'Length);
            pragma Import (Ada, Copy);
         begin
            Result.Size := Data'Length;
            Result.Data := Copy'Address;
            Copy := Data;
         end;
      end if;
      return Result;
   end New_Datum;

   function OCSP_Status_Request_Is_Checked
            (  Session : Session_Type
            )  return Boolean is
      function Internal (Session : Address; Flags : unsigned)
         return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_ocsp_status_request_is_checked"
             );
   begin
      return Internal (Session.Holder.Handle, 0) /= 0;
   end OCSP_Status_Request_Is_Checked;

   procedure OpenPGP_Send_Cert
             (  Session : Session_Type;
                Status  : OpenPGP_Crt_Status
             )  is
      procedure Internal
                (  Session : Address;
                   Status  : OpenPGP_Crt_Status
                );
      pragma Import (C, Internal, "gnutls_openpgp_send_cert");
   begin
      Internal (Session.Holder.Handle, Status);
   end OpenPGP_Send_Cert;

   procedure PEM_Base64_Decode
             (  Header  : String;
                Data    : Stream_Element_Array;
                Result  : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
      function Internal
               (  Header : char_array;
                  Data   : Datum;
                  Result : Address;
                  Size   : access size_t
               )  return int;
      pragma Import (C, Internal, "gnutls_pem_base64_decode");
      Size : aliased size_t := Result'Length;
   begin
      if Pointer < Result'First or else Pointer > Result'Last then
         Raise_Exception
         (  Layout_Error'Identity,
            "Invalid pointer is out of buffer range"
         );
      end if;
      Check
      (  Internal
         (  To_C (Header),
            To_Datum (Data),
            Result (Pointer)'Address,
            Size'Access
      )  );
      Pointer := Pointer + (Stream_Element_Offset (Size) - 1);
   exception
      when Error : TLS_Error =>
         if Get_Error_Code (Error) = E_Short_Memory_Buffer then
            Raise_Exception
            (  Layout_Error'Identity,
               "No room in the buffer"
            );
         else
            raise;
         end if;
   end PEM_Base64_Decode;

   procedure PEM_Base64_Decode
             (  Header  : String;
                Data    : String;
                Result  : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
      function Internal
               (  Header : char_array;
                  Data   : Datum;
                  Result : Address;
                  Size   : access size_t
               )  return int;
      pragma Import (C, Internal, "gnutls_pem_base64_decode");
      Size : aliased size_t := Result'Length;
      Copy : char_array := To_C (Data);
   begin
      if Pointer < Result'First or else Pointer > Result'Last then
         Raise_Exception
         (  Layout_Error'Identity,
            "Invalid pointer is out of buffer range"
         );
      end if;
      Check
      (  Internal
         (  To_C (Header),
            To_Datum (Copy),
            Result (Pointer)'Address,
            Size'Access
      )  );
      Pointer := Pointer + (Stream_Element_Offset (Size) - 1);
   exception
      when Error : TLS_Error =>
         if Get_Error_Code (Error) = E_Short_Memory_Buffer then
            Raise_Exception
            (  Layout_Error'Identity,
               "No room in the buffer"
            );
         else
            raise;
         end if;
   end PEM_Base64_Decode;

   procedure PEM_Base64_Decode
             (  Data    : Stream_Element_Array;
                Result  : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
      function Internal
               (  Header : chars_ptr;
                  Data   : Datum;
                  Result : Address;
                  Size   : access size_t
               )  return int;
      pragma Import (C, Internal, "gnutls_pem_base64_decode");
      Size : aliased size_t := Result'Length;
   begin
      if Pointer < Result'First or else Pointer > Result'Last then
         Raise_Exception
         (  Layout_Error'Identity,
            "Invalid pointer is out of buffer range"
         );
      end if;
      Check
      (  Internal
         (  Null_Ptr,
            To_Datum (Data),
            Result (Pointer)'Address,
            Size'Access
      )  );
      Pointer := Pointer + (Stream_Element_Offset (Size) - 1);
   exception
      when Error : TLS_Error =>
         if Get_Error_Code (Error) = E_Short_Memory_Buffer then
            Raise_Exception
            (  Layout_Error'Identity,
               "No room in the buffer"
            );
         else
            raise;
         end if;
   end PEM_Base64_Decode;

   procedure PEM_Base64_Decode
             (  Data    : String;
                Result  : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
      function Internal
               (  Header : chars_ptr;
                  Data   : Datum;
                  Result : Address;
                  Size   : access size_t
               )  return int;
      pragma Import (C, Internal, "gnutls_pem_base64_decode");
      Size : aliased size_t := Result'Length;
      Copy : char_array := To_C (Data);
   begin
      if Pointer < Result'First or else Pointer > Result'Last then
         Raise_Exception
         (  Layout_Error'Identity,
            "Invalid pointer is out of buffer range"
         );
      end if;
      Check
      (  Internal
         (  Null_Ptr,
            To_Datum (Copy),
            Result (Pointer)'Address,
            Size'Access
      )  );
      Pointer := Pointer + (Stream_Element_Offset (Size) - 1);
   exception
      when Error : TLS_Error =>
         if Get_Error_Code (Error) = E_Short_Memory_Buffer then
            Raise_Exception
            (  Layout_Error'Identity,
               "No room in the buffer"
            );
         else
            raise;
         end if;
   end PEM_Base64_Decode;

   procedure PEM_Base64_Encode
             (  Data    : Stream_Element_Array;
                Result  : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
      function Internal
               (  Header : chars_ptr;
                  Data   : Datum;
                  Result : Address;
                  Size   : access size_t
               )  return int;
      pragma Import (C, Internal, "gnutls_pem_base64_encode");
      Size : aliased size_t := Result'Length;
   begin
      if Pointer < Result'First or else Pointer > Result'Last then
         Raise_Exception
         (  Layout_Error'Identity,
            "Invalid pointer is out of buffer range"
         );
      end if;
      Check
      (  Internal
         (  Null_Ptr,
            To_Datum (Data),
            Result (Pointer)'Address,
            Size'Access
      )  );
      Pointer := Pointer + (Stream_Element_Offset (Size) - 1);
   exception
      when Error : TLS_Error =>
         if Get_Error_Code (Error) = E_Short_Memory_Buffer then
            Raise_Exception
            (  Layout_Error'Identity,
               "No room in the buffer"
            );
         else
            raise;
         end if;
   end PEM_Base64_Encode;

   function PEM_Base64_Encode
            (  Data : Stream_Element_Array
            )  return Stream_Element_Array is
      Result  : Stream_Element_Array (1..Data'Length * 4 / 3 + 4);
      Pointer : Stream_Element_Offset := Result'First;
   begin
      PEM_Base64_Encode (Data, Result, Pointer);
      return Result (Result'First..Pointer - 1);
   end PEM_Base64_Encode;

   function PK_Algorithm_Get_Name
            (  Algorithm : PK_Algorithm
            )  return String is
      function Internal (Algorithm : PK_Algorithm) return chars_ptr;
      pragma Import (C, Internal, "gnutls_pk_algorithm_get_name");
      Result : chars_ptr := Internal (Algorithm);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No algorithm matched");
      else
         return Value (Result);
      end if;
   end PK_Algorithm_Get_Name;

   function PK_Bits_To_Sec_Param
            (  Algorithm : PK_Algorithm;
               Bits      : Positive
            )  return Sec_Param is
      function Internal
               (  Algorithm : PK_Algorithm;
                  Bits      : unsigned
               )  return Sec_Param;
      pragma Import
             (  C,
                Internal,
                "gnutls_pk_bits_to_sec_param"
             );
   begin
      return Internal (Algorithm, unsigned (Bits));
   end PK_Bits_To_Sec_Param;

   function PK_Get_ID (Name : String) return PK_Algorithm is
      function Internal (Name : char_array) return PK_Algorithm;
      pragma Import (C, Internal, "gnutls_pk_get_id");
   begin
      return Internal (To_C (Name));
   end PK_Get_ID;

   function PK_Get_Name (Algorithm : PK_Algorithm) return String is
      function Internal (Algorithm : PK_Algorithm) return chars_ptr;
      pragma Import (C, Internal, "gnutls_pk_get_name");
      Result : chars_ptr := Internal (Algorithm);
   begin
      if Result = Null_Ptr then
         Raise_Exception (End_Error'Identity, "No algorithm matched");
      else
         return Value (Result);
      end if;
   end PK_Get_Name;

   function PK_List return PK_Algorithm_Array is
      use PK_Algorithm_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_pk_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => PK_Unknown);
      else
         return Value (Result);
      end if;
   end PK_List;

   procedure Priority_Set
             (  Session : in out Session_Type;
                Cache   : in out Priority
             )  is
      function Internal
               (  Session : Address;
                  Cache   : Address
               )  return int;
      pragma Import (C, Internal, "gnutls_priority_set");
   begin
      if Cache.Handle = Null_Address then
         Raise_Exception
         (  Constraint_Error'Identity,
            "Invalid priority cache (not yet initialized)"
         );
      end if;
      Check (Internal (Session.Holder.Handle, Cache.Handle));
   end Priority_Set;

   procedure Priority_Set_Direct
             (  Session    : in out Session_Type;
                Priorities : String
             )  is
      use System.Storage_Elements;
      function Internal
               (  Session    : Address;
                  Priorities : char_array;
                  Err_Pos    : access Address
               )  return int;
      pragma Import (C, Internal, "gnutls_priority_set_direct");
      Value  : char_array := To_C (Priorities);
      Error  : aliased Address;
      Result : int;
   begin
      Result := Internal (Session.Holder.Handle, Value, Error'Access);
      if Result < 0 then
         Raise_Exception
         (  TLS_Error'Identity,
            (  StrError (Result)
            &  '['
            &  int'Image (Result)
            &  "] at"
            &  Integer'Image
               (  Priorities'First
               +  Integer (Error - Value'Address)
         )  )  );
      end if;
   end Priority_Set_Direct;

   procedure Raise_TLS_Error (Error : int) is
   begin
      Raise_Exception
      (  TLS_Error'Identity,
         StrError (Error) & '[' & int'Image (Error) & ']'
      );
   end Raise_TLS_Error;

   procedure Read
             (  Session : in out Session_Type;
                Data    : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
   begin
      Pointer := Data'First - 1;
   end Read;

   function Protocol_Get_ID (Name : String) return Protocol is
      function Internal (Name : char_array) return Protocol;
      pragma Import (C, Internal, "gnutls_protocol_get_id");
      Result : Protocol := Internal (To_C (Name));
   begin
      if Result = Version_Unknown then
         Raise_Exception
         (  End_Error'Identity,
            "No protocol matched"
         );
      end if;
      return Result;
   end Protocol_Get_ID;

   function Protocol_Get_Name (Version : Protocol) return String is
      function Internal (Version : Protocol) return chars_ptr;
      pragma Import (C, Internal, "gnutls_protocol_get_name");
      Result : chars_ptr := Internal (Version);
   begin
      if Result = Null_Ptr then
         Raise_Exception
         (  End_Error'Identity,
            "No protocol version matched"
         );
      end if;
      return Value (Result);
   end Protocol_Get_Name;

   function Protocol_Get_Version
            (  Session : Session_Type
            )  return Protocol is
      function Internal (Session : Address) return Protocol;
      pragma Import (C, Internal, "gnutls_protocol_get_version");
   begin
      return Internal (Session.Holder.Handle);
   end Protocol_Get_Version;

   function Protocol_List return Protocol_Array is
      use Protocol_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_protocol_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => No_Protocol);
      else
         return Value (Result);
      end if;
   end Protocol_List;

   function Record_Check_Corked (Session : Session_Type)
      return size_t is
      function Internal (Session : Address) return size_t;
      pragma Import (C, Internal, "gnutls_record_check_corked");
   begin
      return Internal (Session.Holder.Handle);
   end Record_Check_Corked;

   function Record_Check_Pending (Session : Session_Type)
      return size_t is
      function Internal (Session : Address) return size_t;
      pragma Import (C, Internal, "gnutls_record_check_pending");
   begin
      return Internal (Session.Holder.Handle);
   end Record_Check_Pending;

   procedure Record_Cork (Session : in out Session_Type) is
      procedure Internal (Session : Address);
      pragma Import (C, Internal, "gnutls_record_cork");
   begin
      Internal (Session.Holder.Handle);
   end Record_Cork;

   procedure Record_Disable_Padding (Session : in out Session_Type) is
      procedure Internal (Session : Address);
      pragma Import (C, Internal, "gnutls_record_disable_padding");
   begin
      Internal (Session.Holder.Handle);
   end Record_Disable_Padding;

   function Record_Get_Direction (Session : Session_Type)
      return Direction is
      function Internal (Session : Address) return int;
      pragma Import (C, Internal, "gnutls_record_get_direction");
   begin
      if Internal (Session.Holder.Handle) = 0 then
         return Reading;
      else
         return Writing;
      end if;
   end Record_Get_Direction;

   function Record_Get_Max_Size (Session : Session_Type)
      return size_t is
      function Internal (Session : Address) return size_t;
      pragma Import (C, Internal, "gnutls_record_get_max_size");
   begin
      return Internal (Session.Holder.Handle);
   end Record_Get_Max_Size;

   function Record_Overhead_Size (Session : Session_Type)
      return size_t is
      function Internal (Session : Address) return size_t;
      pragma Import (C, Internal, "gnutls_record_overhead_size");
   begin
      return Internal (Session.Holder.Handle);
   end Record_Overhead_Size;

   procedure Record_Recv
             (  Session : in out Session_Type;
                Buffer  : out Stream_Element_Array;
                Last    : out Stream_Element_Offset
             )  is
      function Internal
               (  Session : Address;
                  Buffer  : Address;
                  Size    : size_t
               )  return ptrdiff_t;
      pragma Import (C, Internal, "gnutls_record_recv");
      Result : ptrdiff_t;
   begin
      if Buffer'Length > 0 then
         Result :=
            Internal
            (  Session.Holder.Handle,
               Buffer (Buffer'First)'Address,
               Buffer'Length
            );
         if Result > 0 then
            Last := Buffer'First + Stream_Element_Offset (Result) - 1;
         elsif Result = 0 then
            Raise_Exception
            (  End_Error'Identity,
               "Connection closed by peer"
            );
         elsif Result = E_Again or else Result = E_Interrupted then
            Last := Buffer'First - 1;
         else
            Raise_TLS_Error (int (Result));
         end if;
      else
         Last := Buffer'First - 1;
      end if;
   end Record_Recv;

   procedure Record_Send
             (  Session : in out Session_Type;
                Buffer  : Stream_Element_Array;
                Last    : out Stream_Element_Offset
             )  is
      function Internal
               (  Session : Address;
                  Buffer  : Address;
                  Size    : size_t
               )  return ptrdiff_t;
      pragma Import (C, Internal, "gnutls_record_send");
      Result : ptrdiff_t;
   begin
      if Buffer'Length > 0 then
         Result :=
            Internal
            (  Session.Holder.Handle,
               Buffer (Buffer'First)'Address,
               Buffer'Length
            );
         if Result >= 0 then
            Last := Buffer'First + Stream_Element_Offset (Result) - 1;
         elsif Result = E_Again or else Result = E_Interrupted then
            Last := Buffer'First - 1;
         else
            Raise_TLS_Error (int (Result));
         end if;
      else
         Last := Buffer'First - 1;
      end if;
   end Record_Send;
--
-- *** Removed ***
--
--     procedure Record_Set_Max_Empty_Records
--               (  Session : in out Session_Type;
--                  Count   : Positive
--               )  is
--        procedure Internal (Session : Address; Count : int);
--        pragma Import
--               (  C,
--                  Internal,
--                  "gnutls_record_set_max_empty_records"
--               );
--     begin
--        Internal (Session.Holder.Handle, int (Count));
--     end Record_Set_Max_Empty_Records;

   procedure Record_Set_Max_Size
             (  Session : in out Session_Type;
                Size    : size_t
             )  is
      function Internal (Session : Address; Size : size_t)
         return ssize_t;
      pragma Import (C, Internal, "gnutls_record_set_max_size");
      Result : ssize_t := Internal (Session.Holder.Handle, Size);
   begin
      if Result < 0 then
         Raise_TLS_Error (int (Result));
      end if;
   end Record_Set_Max_Size;

   function Record_Uncork
            (  Session : Session_Type;
               Wait    : Boolean
            )  return Boolean is
      function Internal (Session : Address; Wait : int) return int;
      pragma Import (C, Internal, "gnutls_record_uncork");
      Result : int;
   begin
      if Wait then
         Result := Internal (Session.Holder.Handle, 1);
      else
         Result := Internal (Session.Holder.Handle, 0);
      end if;
      case Result is
         when 0..int'Last =>
            return True;
         when E_Again | E_Interrupted =>
            return False;
         when others =>
            Raise_TLS_Error (Result);
            return False;
      end case;
   end Record_Uncork;

   procedure Rehandshake (Session : in out Session_Type) is
      function Internal (Session : Address) return int;
      pragma Import (C, Internal, "gnutls_rehandshake");
   begin
      Check (Internal (Session.Holder.Handle));
   end Rehandshake;

   function Safe_Renegotiation_Status
            (  Session : Session_Type
            )  return Boolean is
      function Internal (Session : Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_safe_renegotiation_status"
             );
   begin
      return Internal (Session.Holder.Handle) /= 0;
   end Safe_Renegotiation_Status;

   function Sec_Param_Get_Name (Param : Sec_Param) return String is
      function Internal (Param : Sec_Param) return chars_ptr;
      pragma Import (C, Internal, "gnutls_sec_param_get_name");
      Result : chars_ptr := Internal (Param);
   begin
      if Result = Null_Ptr then
         Raise_Exception
         (  End_Error'Identity,
            "No security parameter matched"
         );
      else
         return Value (Result);
      end if;
   end Sec_Param_Get_Name;

   function Sec_Param_To_PK_Bits
            (  Algorithm : PK_Algorithm;
               Parameter : Sec_Param
            )  return Natural is
      function Internal
               (  Algorithm : PK_Algorithm;
                  Parameter : Sec_Param
               )  return unsigned;
      pragma Import
             (  C,
                Internal,
                "gnutls_sec_param_to_pk_bits"
             );
   begin
      return Natural (Internal (Algorithm, Parameter));
   end Sec_Param_To_PK_Bits;

   procedure Session_Channel_Binding
             (  Session      : in out Session_Type;
                Binding_Type : Channel_Binding;
                Binding_Data : in out Datum
             )  is
      function Internal
               (  Session      : Address;
                  Binding_Type : Channel_Binding;
                  Binding_Data : access Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_session_channel_binding");
      Result : aliased Datum := Binding_Data;
   begin
      Check
      (  Internal
         (  Session.Holder.Handle,
            Binding_Type,
            Result'Access
      )  );
      Binding_Data := Result;
   end Session_Channel_Binding;

   procedure Session_Enable_Compatibility_Mode
             (  Session : in out Session_Type
             )  is
      procedure Internal (Session : Address);
      pragma Import
             (  C,
                Internal,
                "gnutls_session_enable_compatibility_mode"
             );
   begin
      Internal (Session.Holder.Handle);
   end Session_Enable_Compatibility_Mode;

   procedure Session_Force_Valid (Session : in out Session_Type) is
      procedure Internal (Session : Address);
      pragma Import (C, Internal, "gnutls_session_force_valid");
   begin
      Internal (Session.Holder.Handle);
   end Session_Force_Valid;

   procedure Session_Get_Data
             (  Session : Session_Type;
                Data    : in out Datum_Holder'Class
             )  is
      function Internal (Session : Address; Data : access Datum)
         return int;
      pragma Import (C, Internal, "gnutls_session_get_data2");
   begin
      Finalize (Data);
      Check (Internal (Session.Holder.Handle, Data.Data'Access));
   end Session_Get_Data;

   function Session_Get_Desc (Session : Session_Type) return String is
      function Internal (Session : Address) return chars_ptr;
      pragma Import (C, Internal, "gnutls_session_get_desc");
      Ptr : chars_ptr := Internal (Session.Holder.Handle);
   begin
      if Ptr = Null_Ptr then
         return "";
      else
         declare
            Result : String := Value (Ptr);
         begin
            Free (Ptr);
            return Result;
         end;
      end if;
   end Session_Get_Desc;

   function Session_Get_ID
            (  Session : Session_Type
            )  return Stream_Element_Array is
      function Internal
               (  Session : Address;
                  Data    : access Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_session_get_id2");
      Data : aliased Datum;
   begin
      Check (Internal (Session.Holder.Handle, Data'Access));
      return To_Stream_Element_Array (Data);
   end Session_Get_ID;

   function Session_Get_Ptr (Session : Session_Type) return Address is
      function Internal (Session : Address) return Address;
      pragma Import (C, Internal, "gnutls_session_get_ptr");
   begin
      return Internal (Session.Holder.Handle);
   end Session_Get_Ptr;

   function Session_Is_Resumed (Session : Session_Type)
      return Boolean is
      function Internal (Session : Address) return int;
      pragma Import (C, Internal, "gnutls_session_is_resumed");
   begin
      return Internal (Session.Holder.Handle) /= 0;
   end Session_Is_Resumed;

   function Session_Resumption_Requested (Session : Session_Type)
      return Boolean is
      function Internal (Session : Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_session_resumption_requested"
             );
   begin
      return Internal (Session.Holder.Handle) /= 0;
   end Session_Resumption_Requested;

   procedure Session_Set_Data
             (  Session : in out Session_Type;
                Data    : Stream_Element_Array
             )  is
      function Internal (Session : Address; Data : Datum) return int;
      pragma Import (C, Internal, "gnutls_session_set_data");
   begin
      Check (Internal (Session.Holder.Handle, To_Datum (Data)));
   end Session_Set_Data;

   procedure Session_Set_Ptr
             (  Session : in out Session_Type;
                Pointer : Address
             )  is
      procedure Internal (Session : Address; Pointer : Address);
      pragma Import (C, Internal, "gnutls_session_set_ptr");
   begin
      Internal (Session.Holder.Handle, Pointer);
   end Session_Set_Ptr;

   procedure Session_Ticket_Enable_Client
             (  Session : in out Session_Type
             )  is
      function Internal (Session : Address) return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_session_ticket_enable_client"
             );
   begin
      Check (Internal (Session.Holder.Handle));
   end Session_Ticket_Enable_Client;

   procedure Session_Ticket_Enable_Server
             (  Session : in out Session_Type;
                Key     : Stream_Element_Array
             )  is
      function Internal (Session : Address; Key : Datum) return int;
      pragma Import (C, Internal, "gnutls_session_ticket_enable_server");
   begin
      Check (Internal (Session.Holder.Handle, To_Datum (Key)));
   end Session_Ticket_Enable_Server;

   procedure Session_Ticket_Key_Generate
             (  Session : in out Session_Type;
                Key     : in out Datum_Holder'Class
             )  is
      function Internal
               (  Session : Address;
                  Key     : access Datum
               )  return int;
      pragma Import (C, Internal, "gnutls_session_ticket_key_generate");
   begin
      Finalize (Key);
      Check (Internal (Session.Holder.Handle, Key.Data'Access));
   end Session_Ticket_Key_Generate;

   procedure Set_Default_Priority (Session : in out Session_Type) is
      function Internal (Session : Address) return int;
      pragma Import (C, Internal, "gnutls_set_default_priority");
   begin
      Check (Internal (Session.Holder.Handle));
   end Set_Default_Priority;

   function Sign_Algorithm_Get
            (  Session : Session_Type
            )  return Sign_Algorithm is
      function Internal
               (  Session : Address
               )  return int;
      pragma Import (C, Internal, "gnutls_sign_algorithm_get");
   begin
      return Sign_Algorithm'Val (Internal (Session.Holder.Handle));
   end Sign_Algorithm_Get;

   function Sign_Algorithm_Get_Client
            (  Session : Session_Type
            )  return Sign_Algorithm is
      function Internal
               (  Session : Address
               )  return int;
      pragma Import (C, Internal, "gnutls_sign_algorithm_get_client");
   begin
      return Sign_Algorithm'Val (Internal (Session.Holder.Handle));
   end Sign_Algorithm_Get_Client;

   function Sign_Algorithm_Get_Requested
            (  Session : Session_Type;
               Index   : Positive
            )  return Sign_Algorithm is
      function Internal
               (  Session   : Address;
                  Index     : size_t;
                  Algorithm : access Sign_Algorithm
               )  return int;
      pragma Import
             (  C,
                Internal,
                "gnutls_sign_algorithm_get_requested"
             );
      Result : aliased Sign_Algorithm;
   begin
      Check
      (  Internal
         (  Session.Holder.Handle,
            size_t (Index) - 1,
            Result'Access
      )  );
      return Result;
   end Sign_Algorithm_Get_Requested;

   function Sign_Get_ID (Name : String) return Sign_Algorithm is
      function Internal (Name : char_array) return Sign_Algorithm;
      pragma Import (C, Internal, "gnutls_sign_get_id");
   begin
      return Internal (To_C (Name));
   end Sign_Get_ID;

   function Sign_Get_Name (Algorithm : Sign_Algorithm) return String is
      function Internal (Algorithm : Sign_Algorithm) return chars_ptr;
      pragma Import (C, Internal, "gnutls_sign_get_name");
      Result : chars_ptr := Internal (Algorithm);
   begin
      if Result = Null_Ptr then
         Raise_Exception
         (  End_Error'Identity,
            "No signature algorithm matched"
         );
      else
         return Value (Result);
      end if;
   end Sign_Get_Name;

   function Sign_Is_Secure (Algorithm : Sign_Algorithm)
      return Boolean is
      function Internal (Algorithm : Sign_Algorithm) return int;
      pragma Import (C, Internal, "gnutls_sign_is_secure");
   begin
      return Internal (Algorithm) /= 0;
   end Sign_Is_Secure;

   function Sign_List return Sign_Algorithm_Array is
      use Sign_Algorithm_Pointers;
      function Internal return Pointer;
      pragma Import (C, Internal, "gnutls_sign_list");
      Result : Pointer := Internal;
   begin
      if Result = null then
         return (1..0 => Sign_Unknown);
      else
         return Value (Result);
      end if;
   end Sign_List;

   function StrError (Error : int) return String is
      function Internal (Error : int) return chars_ptr;
      pragma Import (C, Internal, "gnutls_strerror");
   begin
      return Value (Internal (Error));
   end StrError;

   function To_Stream_Element_Array (Data : Datum)
      return Stream_Element_Array is
   begin
      if Data.Size = 0 or else Data.Data = Null_Address then
         return (1..0 => 0);
      else
         declare
            subtype Buffer_Type is Stream_Element_Array
                                   (  1
                                   .. Stream_Element_Offset (Data.Size)
                                   );
            Buffer : Buffer_Type;
            for Buffer'Address use Data.Data;
            pragma Import (Ada, Buffer);
         begin
            return Buffer;
         end;
      end if;
   end To_Stream_Element_Array;

   function To_Stream_Element_Array (Data : String)
      return Stream_Element_Array is
   begin
      if Data'Length = 0 then
         return (1..0 => 0);
      else
         declare
            Result  : Stream_Element_Array (1..Data'Length);
            Pointer : Stream_Element_Offset := Result'First;
         begin
            for Index in Data'Range loop
               Result (Pointer) := Character'Pos (Data (Index));
               Pointer := Pointer + 1;
            end loop;
            return Result;
         end;
      end if;
   end To_Stream_Element_Array;

   function To_String (Data : Datum) return String is
   begin
      if Data.Size = 0 or else Data.Data = Null_Address then
         return "";
      else
         declare
            subtype Buffer_Type is char_array (1..size_t (Data.Size));
            Buffer : Buffer_Type;
            for Buffer'Address use Data.Data;
            pragma Import (Ada, Buffer);
         begin
            return To_Ada (Buffer, False);
         end;
      end if;
   end To_String;

   procedure Transport_Set_Error_No
             (  Session : in out Session_Type;
                Error   : int
             )  is
      procedure Internal (Session : Address; Error : int);
      pragma Import (C, Internal, "gnutls_transport_set_errno");
   begin
      Internal (Session.Holder.Handle, Error);
   end Transport_Set_Error_No;

   package body Transport_Set_Pull_Function is
      package Conversions is
         new Address_To_Access_Conversions (Transport_Type);
      use Conversions;

      type Pull_Func_Ptr is access function
           (  Transport : Address;
              Data      : Address;
              Size      : size_t
           )  return ssize_t;
      pragma Convention (C, Pull_Func_Ptr);
      function Pull_Func
               (  Transport : Address;
                  Data      : Address;
                  Size      : size_t
               )  return ssize_t;
      pragma Convention (C, Pull_Func);
      function Pull_Func
               (  Transport : Address;
                  Data      : Address;
                  Size      : size_t
               )  return ssize_t is
         Object : Object_Pointer;
      begin
         if Transport = Null_Address then
            return -1;
         end if;
         Object := To_Pointer (Transport);
         if Object = null then
            return -1;
         end if;
         declare
            subtype Data_Buffer is Stream_Element_Array
                                   (  1
                                   .. Stream_Element_Offset (Size)
                                   );
            Buffer : Data_Buffer;
            for Buffer'Address use Data;
            Pointer : Stream_Element_Offset := Buffer'First;
         begin
            Read (Object.all, Buffer, Pointer);
            if Pointer <= Buffer'First then
               Transport_Set_Error_No
               (  Get_Session (Object.all).all,
                  EAGAIN
               );
               return -1;
            elsif Pointer > Buffer'Last then
               return Buffer'Length;
            else
               return ssize_t (Pointer - Buffer'First);
            end if;
         exception
            when End_Error =>
               return 0;
            when Error : others =>
               Transport_Set_Error_No
               (  Get_Session (Object.all).all,
                  EIO
               );
               return -1;
         end;
      exception
         when others =>
            return -1;
      end Pull_Func;

      procedure Set
                (  Session   : in out Session_Type;
                   Transport : access Transport_Type
                )  is
         procedure Internal (Session : Address; Func : Pull_Func_Ptr);
         pragma Import
                (  C,
                   Internal,
                   "gnutls_transport_set_pull_function"
                );
         Receive : Address;
         Send    : Address;
      begin
         Transport_Get_Ptr2 (Session, Receive, Send);
         Transport_Set_Ptr2 (Session, Transport.all'Address, Send);
         Internal (Session.Holder.Handle, Pull_Func'Access);
      end Set;
   end Transport_Set_Pull_Function;

   function Transport_Get_Int (Session : Session_Type) return int is
      function Internal (Session : Address) return  int;
      pragma Import (C, Internal, "gnutls_transport_get_int");
   begin
      return Internal (Session.Holder.Handle);
   end Transport_Get_Int;

   procedure Transport_Get_Int2
             (  Session : Session_Type;
                Receive : out int;
                Send    : out int
             )  is
      procedure Internal
                (  Session : Address;
                   Receive : access int;
                   Send    : access int
                );
      pragma Import (C, Internal, "gnutls_transport_get_int2");
      Receive_Int : aliased int;
      Send_Int    : aliased int;
   begin
      Internal
      (  Session.Holder.Handle,
         Receive_Int'Access,
         Send_Int'Access
      );
      Receive := Receive_Int;
      Send    := Send_Int;
   end Transport_Get_Int2;

   function Transport_Get_Ptr (Session : Session_Type) return Address is
      function Internal (Session : Address) return  Address;
      pragma Import (C, Internal, "gnutls_transport_get_ptr");
   begin
      return Internal (Session.Holder.Handle);
   end Transport_Get_Ptr;

   procedure Transport_Get_Ptr2
             (  Session : Session_Type;
                Receive : out Address;
                Send    : out Address
             )  is
      procedure Internal
                (  Session : Address;
                   Receive : access Address;
                   Send    : access Address
                );
      pragma Import (C, Internal, "gnutls_transport_get_ptr2");
      Receive_Ptr : aliased Address;
      Send_Ptr    : aliased Address;
   begin
      Internal
      (  Session.Holder.Handle,
         Receive_Ptr'Access,
         Send_Ptr'Access
      );
      Receive := Receive_Ptr;
      Send    := Send_Ptr;
   end Transport_Get_Ptr2;

   procedure Transport_Set_Int
             (  Session : in out Session_Type;
                Value   : int
             )  is
      procedure Internal (Session : Address; Receive, Send : int);
      pragma Import (C, Internal, "gnutls_transport_set_int2");
   begin
      Internal (Session.Holder.Handle, Value, Value);
   end Transport_Set_Int;

   procedure Transport_Set_Int2
             (  Session : in out Session_Type;
                Receive : int;
                Send    : int
             )  is
      procedure Internal
                (  Session : Address;
                   Receive : int;
                   Send    : int
                );
      pragma Import (C, Internal, "gnutls_transport_set_int2");
   begin
      Internal (Session.Holder.Handle, Receive, Send);
   end Transport_Set_Int2;

   procedure Transport_Set_Ptr
             (  Session : in out Session_Type;
                Pointer : Address
             )  is
      procedure Internal (Session : Address; Pointer : Address);
      pragma Import (C, Internal, "gnutls_transport_set_ptr");
   begin
      Internal (Session.Holder.Handle, Pointer);
   end Transport_Set_Ptr;

   procedure Transport_Set_Ptr2
             (  Session : in out Session_Type;
                Receive : Address;
                Send    : Address
             )  is
      procedure Internal
                (  Session : Address;
                   Receive : Address;
                   Send    : Address
                );
      pragma Import (C, Internal, "gnutls_transport_set_ptr2");
   begin
      Internal (Session.Holder.Handle, Receive, Send);
   end Transport_Set_Ptr2;

   package body Transport_Set_Push_Function is
      package Conversions is
         new Address_To_Access_Conversions (Transport_Type);
      use Conversions;

      type Push_Func_Ptr is access function
           (  Transport : Address;
              Data      : Address;
              Size      : size_t
           )  return ssize_t;
      pragma Convention (C, Push_Func_Ptr);
      function Push_Func
               (  Transport : Address;
                  Data      : Address;
                  Size      : size_t
               )  return ssize_t;
      pragma Convention (C, Push_Func);
      function Push_Func
               (  Transport : Address;
                  Data      : Address;
                  Size      : size_t
               )  return ssize_t is
         Object : Object_Pointer;
      begin
         if Transport = Null_Address then
            return -1;
         end if;
         Object := To_Pointer (Transport);
         if Object = null then
            return -1;
         end if;
         declare
            subtype Data_Buffer is Stream_Element_Array
                                   (  1
                                   .. Stream_Element_Offset (Size)
                                   );
            Buffer : Data_Buffer;
            for Buffer'Address use Data;
            pragma Import (Ada, Buffer);
            Pointer : Stream_Element_Offset := Buffer'First;
         begin
            Write (Object.all, Buffer, Pointer);
            if Pointer <= Buffer'First then
               Transport_Set_Error_No
               (  Get_Session (Object.all).all,
                  EAGAIN
               );
               return -1;
            elsif Pointer > Buffer'Last then
               return Buffer'Length;
            else
               return ssize_t (Buffer'Last - Pointer + 1);
            end if;
         exception
            when End_Error =>
               return 0;
            when Error : others =>
               Transport_Set_Error_No
               (  Get_Session (Object.all).all,
                  EIO
               );
               return -1;
         end;
      exception
         when others =>
            return -1;
      end Push_Func;

      procedure Set
                (  Session   : in out Session_Type;
                   Transport : access Transport_Type
                )  is
         procedure Internal (Session : Address; Func : Push_Func_Ptr);
         pragma Import
                (  C,
                   Internal,
                   "gnutls_transport_set_push_function"
                );
         Receive : Address;
         Send    : Address;
      begin
         Transport_Get_Ptr2 (Session, Receive, Send);
         Transport_Set_Ptr2 (Session, Receive, Transport.all'Address);
         Internal (Session.Holder.Handle, Push_Func'Access);
      end Set;
   end Transport_Set_Push_Function;

   Epoch : constant Time := Time_Of (1970, 1, 1);

   function To_Time (Stamp : time_t) return Time is
   begin
      if Stamp = -1 then
         Raise_Exception (Time_Error'Identity, "Wrong POSIX time");
      end if;
      return Epoch + Duration (Stamp);
   end To_Time;

   Debug_To_File : Boolean := False;
   Debug_File    : Ada.Text_IO.File_Type;

   procedure Debug_Trace (Level : int; Text : String) is
      use Ada.Text_IO;
   begin
      if Debug_To_File then
         if Level in 0..99 then
            Put (Debug_File, "GNUTLS" & int'Image (Level) & ": ");
         else
            Put (Debug_File, "GNUTLS audit: ");
         end if;
         Put (Debug_File, Text);
      else
         if Level in 0..99 then
            Put ("GNUTLS" & int'Image (Level) & ": ");
         else
            Put ("GNUTLS audit: ");
         end if;
         Put (Text);
      end if;
   end Debug_Trace;

   procedure Log_Trace (Text : String) is
   begin
      Debug_Trace (-1, Text);
   end Log_Trace;

   procedure Log_Trace (Session : in out Session_Type; Text : String) is
   begin
      Debug_Trace (-1, Text);
   end Log_Trace;

   package Debug_Log is
      new Global_Set_Log_Function (Debug_Trace);

   package Audit_Log is
      new Global_Set_Audit_Log_Function (Log_Trace, Log_Trace);

   procedure Set_TLS_Debug (Level : int) is
      use Ada.Text_IO;
   begin
      if Debug_To_File then
         Close (Debug_File);
         Debug_To_File := False;
      end if;
      Audit_Log.Set;
      Debug_Log.Set (Level);
   end Set_TLS_Debug;

   procedure Set_TLS_Debug (Level : int; File : String) is
      use Ada.Text_IO;
   begin
      if Debug_To_File then
         Close (Debug_File);
         Debug_To_File := False;
      end if;
      Create (Debug_File, Out_File, File);
      Debug_To_File := True;
      Audit_Log.Set;
      Debug_Log.Set (Level);
   end Set_TLS_Debug;

   function To_Datum (Data : Stream_Element_Array) return Datum is
      Result : Datum;
   begin
      if Data'Length > 0 then
         Result.Size := Data'Length;
         Result.Data := Data (Data'First)'Address;
      end if;
      return Result;
   end To_Datum;

   function To_Datum (Data : char_array) return Datum is
      Result : Datum;
   begin
      if Data'Length > 0 then
         Result.Size := Data'Length;
         Result.Data := Data (Data'First)'Address;
      end if;
      return Result;
   end To_Datum;

   function URL_Is_Supported (URL : String) return Boolean is
      function Internal (URL : char_array) return int;
      pragma Import (C, Internal, "gnutls_url_is_supported");
   begin
      return Internal (To_C (URL)) /= 0;
   end URL_Is_Supported;

begin
   declare
      procedure Internal;
      pragma Import (C, Internal, "gnutls_global_init");
   begin
      Internal;
   end;
end GNUTLS;