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
|
/* wolfSSL.cs
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL 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 3 of the License, or
* (at your option) any later version.
*
* wolfSSL 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 program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace wolfSSL.CSharp
{
public class wolfssl
{
private const string wolfssl_dll = "wolfssl.dll";
/* wait for 6 seconds default on TCP socket state poll if timeout not set */
private const int WC_WAIT = 6000000;
/********************************
* Utility String Conversion functions
*/
#if WindowsCE
/// <summary>
/// Convert MBCS (8-bit single/multi byte) to Wide Char/Unicode (16-bit) character set
/// </summary>
public static string MultiByteToWideChar(string msg)
{
if (msg == null)
return null;
/* Convert to Byte Array */
byte[] bytes = Encoding.Unicode.GetBytes((string)msg.ToString());
/* Convert to String */
string ret = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
/* Remove possible extra null terminator */
int len = 0;
while (len < ret.Length && ret[len] != 0) len++;
return ret.Substring(0, len);
}
/// <summary>
/// Convert Unicode/Wide Char (16-bit) to MBCS (8-bit single/multi byte) character set
/// </summary>
public static string WideCharToMultiByte(string msg)
{
if (msg == null)
return null;
/* Get length and round up to even for multibyte / unicode */
int msgLen = msg.Length;
msgLen = ((msgLen + 1) & ~1);
byte[] bytes = new byte[msgLen];
/* Convert to Byte Array */
byte[] msgBytes = Encoding.ASCII.GetBytes((string)msg.ToString());
msgBytes.CopyTo(bytes, 0);
/* Convert to String */
return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
}
/// <summary>
/// WinCE version of Marshal for Multi-byte pointer to ASCII string
/// Similar conversion used in MultiByteToWideChar, but input is IntPtr
/// </summary>
public static string PtrToStringAnsi(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
return null;
/* Estimate string length */
int len = 0;
while (Marshal.ReadByte(ptr, len) != 0) len++;
if (len == 0)
return string.Empty;
byte[] buffer = new byte[len];
Marshal.Copy(ptr, buffer, 0, len);
return Encoding.ASCII.GetString(buffer, 0, len);
}
#else
public static string PtrToStringAnsi(IntPtr ptr)
{
return Marshal.PtrToStringAnsi(ptr);
}
#endif
/********************************
* Class for DTLS connections
*/
/// <summary>
/// Contains information regarding a DTLS connection having UdpClient udp and IPEndPoint ep.
/// Used to keep memory alive.
/// </summary>
public class DTLS_con
{
public UdpClient udp;
public IPEndPoint ep;
}
/********************************
* Class for keeping ctx handles alive
*/
[StructLayout(LayoutKind.Sequential)]
private class ctx_handle
{
private GCHandle rec_cb;
private GCHandle snd_cb;
private GCHandle psk_cb;
private GCHandle sni_cb;
private GCHandle sni_arg;
private GCHandle vrf_cb;
private IntPtr ctx;
public void set_receive(GCHandle input)
{
this.rec_cb = input;
}
public GCHandle get_receive()
{
return this.rec_cb;
}
public void set_send(GCHandle input)
{
this.snd_cb = input;
}
public GCHandle get_send()
{
return this.snd_cb;
}
public void set_psk(GCHandle input)
{
this.psk_cb = input;
}
public GCHandle get_psk()
{
return this.psk_cb;
}
public void set_sni(GCHandle input) {
this.sni_cb = input;
}
public GCHandle get_sni(GCHandle input) {
return this.sni_cb;
}
public void set_arg(GCHandle input) {
this.sni_arg= input;
}
public GCHandle get_arg(GCHandle input) {
return this.sni_arg;
}
public void set_vrf(GCHandle input)
{
if (!Object.Equals(this.vrf_cb, default(GCHandle)))
{
this.vrf_cb.Free();
}
this.vrf_cb = input;
}
public GCHandle get_vrf()
{
return this.vrf_cb;
}
public void set_ctx(IntPtr input)
{
this.ctx = input;
}
public IntPtr get_ctx()
{
return this.ctx;
}
/// <summary>
/// Called to free the pointers keeping handles alive
/// </summary>
public void free()
{
log(INFO_LOG, "freeing ctx handle");
if (!Object.Equals(this.rec_cb, default(GCHandle)))
{
this.rec_cb.Free();
}
if (!Object.Equals(this.snd_cb, default(GCHandle)))
{
this.snd_cb.Free();
}
if (!Object.Equals(this.psk_cb, default(GCHandle)))
{
this.psk_cb.Free();
}
if (!Object.Equals(this.sni_cb, default(GCHandle)))
{
this.sni_cb.Free();
}
if (!Object.Equals(this.vrf_cb, default(GCHandle)))
{
this.vrf_cb.Free();
}
}
}
/********************************
* Class for keeping ssl handle alive
*/
[StructLayout(LayoutKind.Sequential)]
private class ssl_handle
{
private GCHandle fd_pin;
private GCHandle psk_cb;
private GCHandle sni_cb;
private GCHandle vrf_cb;
private IntPtr ssl;
public void set_fd(GCHandle input)
{
this.fd_pin = input;
}
public GCHandle get_fd()
{
return this.fd_pin;
}
public void set_psk(GCHandle input)
{
this.psk_cb = input;
}
public GCHandle get_psk()
{
return this.psk_cb;
}
public void set_vrf(GCHandle input)
{
if (!Object.Equals(this.vrf_cb, default(GCHandle)))
{
this.vrf_cb.Free();
}
this.vrf_cb = input;
}
public GCHandle get_vrf()
{
return this.vrf_cb;
}
public void set_ssl(IntPtr input)
{
this.ssl = input;
}
public IntPtr get_ssl()
{
return this.ssl;
}
public void free()
{
log(INFO_LOG, "freeing ssl handle");
if (!Object.Equals(this.fd_pin, default(GCHandle)))
{
this.fd_pin.Free();
}
if (!Object.Equals(this.psk_cb, default(GCHandle)))
{
this.psk_cb.Free();
}
if (!Object.Equals(this.sni_cb, default(GCHandle)))
{
this.sni_cb.Free();
}
if (!Object.Equals(this.vrf_cb, default(GCHandle)))
{
this.vrf_cb.Free();
}
}
}
/********************************
* Init wolfSSL library
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_Init();
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_Cleanup();
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_Init();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_Cleanup();
#endif
/********************************
* Methods of connection
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfTLSv1_2_server_method();
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfTLSv1_3_server_method();
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSLv23_server_method();
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfTLSv1_2_client_method();
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfTLSv1_3_client_method();
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSLv23_client_method();
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfDTLSv1_2_server_method();
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfDTLSv1_2_client_method();
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfTLSv1_2_server_method();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfTLSv1_3_server_method();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSLv23_server_method();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfTLSv1_2_client_method();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfTLSv1_3_client_method();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSLv23_client_method();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfDTLSv1_2_server_method();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfDTLSv1_2_client_method();
#endif
/********************************
* Callbacks
*/
#if WindowsCE
public delegate int CallbackIORecv_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_SetIORecv(IntPtr ctx, CallbackIORecv_delegate recv);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_SetIOReadCtx(IntPtr ssl, IntPtr rctx);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_GetIOReadCtx(IntPtr ssl);
public delegate int CallbackIOSend_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_SetIOSend(IntPtr ctx, CallbackIOSend_delegate send);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_SetIOWriteCtx(IntPtr ssl, IntPtr wctx);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_GetIOWriteCtx(IntPtr ssl);
#else
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int CallbackIORecv_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_SetIORecv(IntPtr ctx, CallbackIORecv_delegate recv);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_SetIOReadCtx(IntPtr ssl, IntPtr rctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_GetIOReadCtx(IntPtr ssl);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int CallbackIOSend_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_SetIOSend(IntPtr ctx, CallbackIOSend_delegate send);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_SetIOWriteCtx(IntPtr ssl, IntPtr wctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_GetIOWriteCtx(IntPtr ssl);
#endif
/********************************
* CTX structure
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_CTX_new(IntPtr method);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_use_certificate_file(IntPtr ctx, string file, int type);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_load_verify_locations(IntPtr ctx, string file, string path);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_load_verify_locations_ex(IntPtr ctx, string file, string path, int flags);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_use_PrivateKey_file(IntPtr ctx, string file, int type);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_CTX_free(IntPtr ctx);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_CTX_new(IntPtr method);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_use_certificate_file(IntPtr ctx, string file, int type);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_load_verify_locations(IntPtr ctx, string file, string path);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_load_verify_locations_ex(IntPtr ctx, string file, string path, int flags);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_use_PrivateKey_file(IntPtr ctx, string file, int type);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_CTX_free(IntPtr ctx);
#endif
/********************************
* PSK
*/
#if WindowsCE
public delegate uint psk_delegate(IntPtr ssl, string identity, IntPtr key, uint max_sz);
public delegate uint psk_client_delegate(IntPtr ssl, string hint, IntPtr identity, uint id_max_len, IntPtr key, uint max_sz);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_set_psk_server_callback(IntPtr ssl, psk_delegate psk_cb);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_CTX_set_psk_server_callback(IntPtr ctx, psk_delegate psk_cb);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_CTX_set_psk_client_callback(IntPtr ctx, psk_client_delegate psk_cb);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_use_psk_identity_hint(IntPtr ctx, string identity);
#else
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate uint psk_delegate(IntPtr ssl, string identity, IntPtr key, uint max_sz);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate uint psk_client_delegate(IntPtr ssl, string hint, IntPtr identity, uint id_max_len, IntPtr key, uint max_sz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_set_psk_server_callback(IntPtr ssl, psk_delegate psk_cb);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_CTX_set_psk_server_callback(IntPtr ctx, psk_delegate psk_cb);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_CTX_set_psk_client_callback(IntPtr ctx, psk_client_delegate psk_cb);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_use_psk_identity_hint(IntPtr ctx, StringBuilder identity);
#endif
/********************************
* SNI
*/
#if WindowsCE
public delegate int sni_delegate(IntPtr ssl, IntPtr ret, IntPtr exArg);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_CTX_set_servername_callback(IntPtr ctx, sni_delegate sni_cb);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_set_servername_arg(IntPtr ctx, IntPtr arg);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size);
[DllImport(wolfssl_dll)]
private extern static ushort wolfSSL_SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_SNI_GetFromBuffer(byte[] clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz);
#else
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int sni_delegate(IntPtr ssl, IntPtr ret, IntPtr exArg);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_CTX_set_servername_callback(IntPtr ctx, sni_delegate sni_cb);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_set_servername_arg(IntPtr ctx, IntPtr arg);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static ushort wolfSSL_SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_SNI_GetFromBuffer(byte[] clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz);
#endif
/********************************
* SSL Structure
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_new(IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_accept(IntPtr ssl);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_connect(IntPtr ssl);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_read(IntPtr ssl, IntPtr buf, int sz);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_write(IntPtr ssl, IntPtr buf, int sz);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_shutdown(IntPtr ssl);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_free(IntPtr ssl);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_new(IntPtr ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_accept(IntPtr ssl);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_connect(IntPtr ssl);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_read(IntPtr ssl, IntPtr buf, int sz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_write(IntPtr ssl, IntPtr buf, int sz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_shutdown(IntPtr ssl);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_free(IntPtr ssl);
#endif
/********************************
* Cipher lists
*/
#if WindowsCE
/* only supports full name from cipher_name[] delimited by : */
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_set_cipher_list(IntPtr ctx, string ciphers);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_set_cipher_list(IntPtr ssl, string ciphers);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_get_ciphers(string ciphers, int sz);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_get_cipher(IntPtr ssl);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_CIPHER_get_name(IntPtr cipher);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_get_current_cipher(IntPtr ssl);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_get_version(IntPtr ssl);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_get_cipher_list(IntPtr ssl);
#else
/* only supports full name from cipher_name[] delimited by : */
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_set_cipher_list(IntPtr ctx, StringBuilder ciphers);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_set_cipher_list(IntPtr ssl, StringBuilder ciphers);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_get_ciphers(StringBuilder ciphers, int sz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_get_cipher(IntPtr ssl);
[DllImport(wolfssl_dll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_CIPHER_get_name(IntPtr cipher);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_get_current_cipher(IntPtr ssl);
[DllImport(wolfssl_dll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_get_version(IntPtr ssl);
[DllImport(wolfssl_dll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_get_cipher_list(IntPtr ssl);
#endif
/********************************
* Error logging
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_ERR_reason_error_string(uint err);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_get_error(IntPtr ssl, int err);
public delegate void loggingCb(int lvl, string msg);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_Debugging_ON();
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_Debugging_OFF();
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_SetLoggingCb(loggingCb vc);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private extern static IntPtr wolfSSL_ERR_reason_error_string(uint err);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_get_error(IntPtr ssl, int err);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void loggingCb(int lvl, StringBuilder msg);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_Debugging_ON();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_Debugging_OFF();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_SetLoggingCb(loggingCb vc);
#endif
private static loggingCb internal_log;
/********************************
* DH
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_SetMinDhKey_Sz(IntPtr ctx, short size);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_SetTmpDH_file(IntPtr ssl, string dhParam, int type);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_CTX_SetTmpDH_file(IntPtr ctx, string dhParam, int type);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_SetMinDhKey_Sz(IntPtr ctx, short size);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_SetTmpDH_file(IntPtr ssl, StringBuilder dhParam, int type);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_CTX_SetTmpDH_file(IntPtr ctx, StringBuilder dhParam, int type);
#endif
/********************************
* Verify Callback
*/
#if WindowsCE
public delegate int CallbackVerify_delegate(int ret, IntPtr x509_ctx);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_CTX_set_verify(IntPtr ctx, int mode, CallbackVerify_delegate vc);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_set_verify(IntPtr ssl, int mode, CallbackVerify_delegate vc);
#else
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int CallbackVerify_delegate(int ret, IntPtr x509_ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_CTX_set_verify(IntPtr ctx, int mode, CallbackVerify_delegate vc);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_set_verify(IntPtr ssl, int mode, CallbackVerify_delegate vc);
#endif
/********************************
* X509 Store
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_X509_STORE_CTX_get_current_cert(IntPtr x509Ctx);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_X509_STORE_CTX_get_error(IntPtr sk);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_X509_STORE_GetCerts(IntPtr x509Ctx);
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_sk_X509_num(IntPtr sk);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_sk_X509_free(IntPtr sk);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_sk_X509_pop(IntPtr sk);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_X509_STORE_CTX_get_current_cert(IntPtr x509Ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_X509_STORE_CTX_get_error(IntPtr sk);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_X509_STORE_GetCerts(IntPtr x509Ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_sk_X509_num(IntPtr sk);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wolfSSL_sk_X509_free(IntPtr sk);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_sk_X509_pop(IntPtr sk);
#endif
/********************************
* Enum types from wolfSSL library
*/
public static readonly int SSL_FILETYPE_PEM = 1;
public static readonly int SSL_FILETYPE_ASN1 = 2;
public static readonly int SSL_FILETYPE_RAW = 3;
public static readonly int SSL_VERIFY_NONE = 0;
public static readonly int SSL_VERIFY_PEER = 1;
public static readonly int SSL_VERIFY_FAIL_IF_NO_PEER_CERT = 2;
public static readonly int SSL_VERIFY_CLIENT_ONCE = 4;
public static readonly int SSL_VERIFY_POST_HANDSHAKE = 8;
public static readonly int SSL_VERIFY_FAIL_EXCEPT_PSK = 16;
public static readonly int CBIO_ERR_GENERAL = -1;
public static readonly int CBIO_ERR_WANT_READ = -2;
public static readonly int CBIO_ERR_WANT_WRITE = -2;
public static readonly int CBIO_ERR_CONN_RST = -3;
public static readonly int CBIO_ERR_ISR = -4;
public static readonly int CBIO_ERR_CONN_CLOSE = -5;
public static readonly int CBIO_ERR_TIMEOUT = -6;
public static readonly int ERROR_LOG = 0;
public static readonly int INFO_LOG = 1;
public static readonly int ENTER_LOG = 2;
public static readonly int LEAVE_LOG = 3;
public static readonly int OTHER_LOG = 4;
public static readonly int SUCCESS = 1;
public static readonly int FAILURE = 0;
public static readonly int WOLFSSL_SNI_HOST_NAME = 0;
public static readonly int WOLFSSL_LOAD_FLAG_NONE = 0x00000000;
public static readonly int WOLFSSL_LOAD_FLAG_IGNORE_ERR = 0x00000001;
public static readonly int WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY = 0x00000002;
public static readonly int WOLFSSL_LOAD_FLAG_PEM_CA_ONLY = 0x00000004;
public static readonly int WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR = 0x00000008;
public static readonly int WOLFSSL_LOAD_FLAG_IGNORE_ZEROFILE = 0x00000010;
public static readonly int WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS = WOLFSSL_LOAD_FLAG_NONE;
private static IntPtr unwrap_ctx(IntPtr ctx)
{
try {
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
return handles.get_ctx();
} catch (Exception e)
{
log(ERROR_LOG, "wolfssl ctx pointer is incorrect " + e);
return IntPtr.Zero;
}
}
private static IntPtr unwrap_ssl(IntPtr ssl)
{
try {
#if WindowsCE
GCHandle gch = (GCHandle)ssl;
#else
GCHandle gch = GCHandle.FromIntPtr(ssl);
#endif
ssl_handle handles = (ssl_handle)gch.Target;
return handles.get_ssl();
} catch (Exception e)
{
log(ERROR_LOG, "wolfssl pointer is incorrect " + e);
return IntPtr.Zero;
}
}
/// <summary>
/// Utility function used to access the certificates
/// based on the platform.
/// <returns>return the platform specific path to the certificate</returns>
/// </summary>
public static string setPath(string file) {
PlatformID platform = Environment.OSVersion.Platform;
#if !WindowsCE
if (platform == PlatformID.Unix ||
platform == PlatformID.MacOSX)
{
Console.WriteLine("Linux - " + file);
return @"../../certs/" + file;
}
else
#endif
if (platform == PlatformID.Win32NT ||
platform == PlatformID.Win32Windows ||
platform == PlatformID.Win32S ||
platform == PlatformID.WinCE)
{
Console.WriteLine("Windows - " + file);
return @"../../../../certs/" + file;
} else
{
return "";
}
}
/// <summary>
/// Call back to allow receiving TLS information
/// </summary>
/// <param name="ssl">structure of ssl passed in</param>
/// <param name="buf">buffer to contain received msg</param>
/// <param name="sz">size of buffer</param>
/// <param name="ctx">optional information passed in</param>
/// <returns>size of message received</returns>
private static int wolfSSLCbIORecv(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx)
{
if (sz <= 0)
{
log(ERROR_LOG, "wolfssl receive error, size less than 0");
return wolfssl.CBIO_ERR_GENERAL;
}
int amtRecv = 0;
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
Socket con = (System.Net.Sockets.Socket)gch.Target;
Byte[] msg = new Byte[sz];
amtRecv = con.Receive(msg, msg.Length, 0);
if (amtRecv == 0)
{
/* No data received so check for a response to see if connection is still open */
#if WindowsCE
if (con.Poll(WC_WAIT, SelectMode.SelectRead))
#else
if (con.Poll((con.ReceiveTimeout > 0) ? con.ReceiveTimeout : WC_WAIT, SelectMode.SelectRead))
#endif
{
log(ERROR_LOG, "socket connection issue, suspected connection termination.");
return wolfssl.CBIO_ERR_CONN_CLOSE;
}
}
Marshal.Copy(msg, 0, buf, sz);
}
catch (Exception e)
{
log(ERROR_LOG, "Error in receive " + e.ToString());
return wolfssl.CBIO_ERR_CONN_CLOSE;
}
return amtRecv;
}
/// <summary>
/// Call back used for sending TLS information
/// </summary>
/// <param name="ssl">pointer to ssl struct</param>
/// <param name="buf">buffer containing information to send</param>
/// <param name="sz">size of buffer to send</param>
/// <param name="ctx">optional information</param>
/// <returns>amount of information sent</returns>
private static int wolfSSLCbIOSend(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx)
{
if (sz <= 0)
{
log(ERROR_LOG, "wolfssl send error, size less than 0");
return wolfssl.CBIO_ERR_GENERAL;
}
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
Socket con = (System.Net.Sockets.Socket)gch.Target;
Byte[] msg = new Byte[sz];
Marshal.Copy(buf, msg, 0, sz);
if (con.Send(msg, 0, msg.Length, SocketFlags.None) == 0 && sz != 0)
{
/* no data sent and msg size is larger then 0, check for lost connection */
#if WindowsCE
if (con.Poll(WC_WAIT, SelectMode.SelectWrite))
#else
if (con.Poll((con.SendTimeout > 0) ? con.SendTimeout : WC_WAIT, SelectMode.SelectWrite))
#endif
{
log(ERROR_LOG, "socket connection issue, suspect connection termination");
return wolfssl.CBIO_ERR_CONN_CLOSE;
}
}
return sz;
}
catch (Exception e)
{
log(ERROR_LOG, "socket connection issue " + e.ToString());
return wolfssl.CBIO_ERR_CONN_CLOSE;
}
}
/// <summary>
/// Call back used for sending DTLS information
/// </summary>
/// <param name="ssl">pointer to ssl struct</param>
/// <param name="buf">buffer containing information to send</param>
/// <param name="sz">size of buffer to send</param>
/// <param name="ctx">optional information</param>
/// <returns>amount of information sent</returns>
private static int wolfSSL_dtlsCbIOSend(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx)
{
if (sz <= 0)
{
log(ERROR_LOG, "wolfssl dtls send error, size less than 0");
return wolfssl.CBIO_ERR_GENERAL;
}
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
DTLS_con con = (DTLS_con)gch.Target;
Byte[] msg = new Byte[sz];
Marshal.Copy(buf, msg, 0, sz);
con.udp.Send(msg, msg.Length, con.ep);
return msg.Length;
}
catch (Exception e)
{
log(ERROR_LOG, "socket connection issue " + e.ToString());
return wolfssl.CBIO_ERR_CONN_CLOSE;
}
}
/// <summary>
/// Call back to allow receiving DTLS information
/// </summary>
/// <param name="ssl">structure of ssl passed in</param>
/// <param name="buf">buffer to contain received msg</param>
/// <param name="sz">size of buffer</param>
/// <param name="ctx">optional information passed in</param>
/// <returns>size of message received</returns>
private static int wolfSSL_dtlsCbIORecv(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx)
{
if (sz <= 0)
{
log(ERROR_LOG, "wolfssl dtls receive error, size less than 0");
return wolfssl.CBIO_ERR_GENERAL;
}
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
DTLS_con con = (DTLS_con)gch.Target;
Byte[] msg = con.udp.Receive(ref con.ep);
if (msg.Length > sz)
{
log(ERROR_LOG, "wolfssl DTLS packet received was larger than buffer");
return wolfssl.CBIO_ERR_GENERAL;
}
Marshal.Copy(msg, 0, buf, msg.Length);
return msg.Length;
}
catch (Exception e)
{
/* issue with receive or size of buffer */
log(ERROR_LOG, "socket read issue " + e.ToString());
return wolfssl.CBIO_ERR_CONN_CLOSE;
}
}
/// <summary>
/// Create a new ssl structure
/// </summary>
/// <param name="ctx">structure to create ssl structure from</param>
/// <returns>pointer to ssl structure</returns>
public static IntPtr new_ssl(IntPtr ctx)
{
if (ctx == IntPtr.Zero)
return IntPtr.Zero;
try
{
ssl_handle io;
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "new_ssl ctx unwrap error");
return IntPtr.Zero;
}
io = new ssl_handle();
io.set_ssl(wolfSSL_new(local_ctx));
/* check if null */
if (io.get_ssl() == IntPtr.Zero)
{
return IntPtr.Zero;
}
/* keep memory pinned to be able to reference by address */
#if WindowsCE
return (IntPtr)GCHandle.Alloc(io, GCHandleType.Pinned);
#else
return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned));
#endif
}
catch (Exception e)
{
log(ERROR_LOG, e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Used for a server to accept a connection
/// </summary>
/// <param name="ssl">structure containing info for connection</param>
/// <returns>1 on success</returns>
public static int accept(IntPtr ssl)
{
if (ssl == IntPtr.Zero)
return FAILURE;
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "accept ssl unwrap error");
return FAILURE;
}
return wolfSSL_accept(sslCtx);
}
catch (Exception e)
{
log(ERROR_LOG, "accept error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Used for a client to connect
/// </summary>
/// <param name="ssl">structure containing connection info</param>
/// <returns>1 on success</returns>
public static int connect(IntPtr ssl)
{
if (ssl == IntPtr.Zero)
return FAILURE;
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "connect ssl unwrap error");
return FAILURE;
}
return wolfSSL_connect(sslCtx);
}
catch (Exception e)
{
log(ERROR_LOG, "connect error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Read message from secure connection
/// </summary>
/// <param name="ssl">structure containing info about connection</param>
/// <param name="buf">object to hold incoming message (Unicode format)</param>
/// <param name="sz">size of available memory in buf</param>
/// <returns>amount of data read on success</returns>
public static int read(IntPtr ssl, StringBuilder buf, int sz)
{
if (ssl == IntPtr.Zero)
return FAILURE;
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
IntPtr data;
int ret;
byte[] msg;
/* Clear incoming buffer */
buf.Length = 0;
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "read ssl unwrap error");
return FAILURE;
}
data = Marshal.AllocHGlobal(sz);
ret = wolfSSL_read(sslCtx, data, sz);
if (ret >= 0)
{
/* Get data that was sent across and store it using a literal read of
* the conversion from bytes to character. Takes care of if
* a null terminator is part of the message read.
*/
msg = new byte[ret];
Marshal.Copy(data, msg, 0, ret);
for (int i = 0; i < ret; i++)
{
buf.Append(@Convert.ToChar(msg[i]));
}
}
Marshal.FreeHGlobal(data);
return ret;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl read error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Read message from secure connection using a byte array
/// </summary>
/// <param name="ssl">structure containing info about connection</param>
/// <param name="buf">object to hold incoming message (raw bytes)</param>
/// <param name="sz">size of available memory in buf</param>
/// <returns>amount of data read on success</returns>
public static int read(IntPtr ssl, byte[] buf, int sz)
{
if (ssl == IntPtr.Zero)
return FAILURE;
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
IntPtr data;
int ret;
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "read ssl unwrap error");
return FAILURE;
}
data = Marshal.AllocHGlobal(sz);
ret = wolfSSL_read(sslCtx, data, sz);
if (ret >= 0)
{
Marshal.Copy(data, buf, 0, ret);
}
Marshal.FreeHGlobal(data);
return ret;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl read error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Write message to secure connection
/// </summary>
/// <param name="ssl">structure containing connection info</param>
/// <param name="buf">message to send</param>
/// <param name="sz">size of the message</param>
/// <returns>amount sent on success</returns>
public static int write(IntPtr ssl, StringBuilder buf, int sz)
{
if (ssl == IntPtr.Zero)
return FAILURE;
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
IntPtr data;
int ret;
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "write ssl unwrap error");
return FAILURE;
}
data = Marshal.AllocHGlobal(sz);
Marshal.Copy(System.Text.Encoding.Default.GetBytes(buf.ToString()), 0,
data, System.Text.Encoding.Default.GetByteCount(buf.ToString()));
ret = wolfSSL_write(sslCtx, data, sz);
Marshal.FreeHGlobal(data);
return ret;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl write error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Write message to secure connection
/// </summary>
/// <param name="ssl">structure containing connection info</param>
/// <param name="buf">message to send</param>
/// <param name="sz">size of the message</param>
/// <returns>amount sent on success</returns>
public static int write(IntPtr ssl, byte[] buf, int sz)
{
if (ssl == IntPtr.Zero)
return FAILURE;
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
IntPtr data;
int ret;
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "write ssl unwrap error");
return FAILURE;
}
data = Marshal.AllocHGlobal(sz);
Marshal.Copy(buf, 0, data, sz);
ret = wolfSSL_write(sslCtx, data, sz);
Marshal.FreeHGlobal(data);
return ret;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl write error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Free information stored in ssl struct
/// </summary>
/// <param name="ssl">pointer to ssl struct to free</param>
public static void free(IntPtr ssl)
{
try
{
IntPtr sslCtx;
#if WindowsCE
GCHandle gch = (GCHandle)ssl;
#else
GCHandle gch = GCHandle.FromIntPtr(ssl);
#endif
ssl_handle handles = (ssl_handle)gch.Target;
sslCtx = handles.get_ssl();
wolfSSL_free(sslCtx);
handles.free();
gch.Free();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl free error " + e.ToString());
}
}
/// <summary>
/// Shutdown a connection
/// </summary>
/// <param name="ssl">pointer to ssl struct to close connection of</param>
/// <returns>1 on success</returns>
public static int shutdown(IntPtr ssl)
{
if (ssl == IntPtr.Zero)
return FAILURE;
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "shutdown ssl unwrap error");
return FAILURE;
}
return wolfSSL_shutdown(sslCtx);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl shutdwon error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Optional, can be used to set a custom receive function
/// </summary>
/// <param name="ctx">structure to set receive function in</param>
/// <param name="func">function to use when reading socket</param>
public static void SetIORecv(IntPtr ctx, CallbackIORecv_delegate func)
{
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
/* check if already stored handle needs freed */
gch = handles.get_receive();
if (!Object.Equals(gch, default(GCHandle)))
{
gch.Free();
}
/* keep new function alive */
handles.set_receive(GCHandle.Alloc(func));
wolfSSL_CTX_SetIORecv(handles.get_ctx(), func);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl setIORecv error " + e.ToString());
}
}
/// <summary>
/// Optional, can be used to set a custom send function
/// </summary>
/// <param name="ctx">structure to set function in</param>
/// <param name="func">function to use when sending data</param>
public static void SetIOSend(IntPtr ctx, CallbackIOSend_delegate func)
{
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
/* check if already stored handle needs freed */
gch = handles.get_send();
if (!Object.Equals(gch, default(GCHandle)))
{
gch.Free();
}
/* keep new function alive */
handles.set_send(GCHandle.Alloc(func));
wolfSSL_CTX_SetIOSend(handles.get_ctx(), func);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl setIOSend error " + e.ToString());
}
}
/// <summary>
/// Create a new CTX structure
/// </summary>
/// <param name="method">method to use such as TLSv1.2</param>
/// <returns>pointer to CTX structure</returns>
public static IntPtr CTX_new(IntPtr method)
{
try
{
IntPtr ctx = wolfSSL_CTX_new(method);
if (ctx == IntPtr.Zero)
return ctx;
ctx_handle io = new ctx_handle();
io.set_ctx(ctx);
CallbackIORecv_delegate recv = new CallbackIORecv_delegate(wolfssl.wolfSSLCbIORecv);
io.set_receive(GCHandle.Alloc(recv));
wolfSSL_CTX_SetIORecv(ctx, recv);
CallbackIOSend_delegate send = new CallbackIOSend_delegate(wolfssl.wolfSSLCbIOSend);
io.set_send(GCHandle.Alloc(send));
wolfSSL_CTX_SetIOSend(ctx, send);
/* keep memory pinned */
#if WindowsCE
return (IntPtr)GCHandle.Alloc(io, GCHandleType.Pinned);
#else
return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned));
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "ctx_new error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Create a new CTX structure for a DTLS connection
/// </summary>
/// <param name="method">Method to use in connection ie DTLSv1.2</param>
/// <returns></returns>
public static IntPtr CTX_dtls_new(IntPtr method)
{
try
{
IntPtr ctx = wolfSSL_CTX_new(method);
if (ctx == IntPtr.Zero)
return ctx;
ctx_handle io = new ctx_handle();
io.set_ctx(ctx);
CallbackIORecv_delegate recv = new CallbackIORecv_delegate(wolfssl.wolfSSL_dtlsCbIORecv);
io.set_receive(GCHandle.Alloc(recv));
wolfSSL_CTX_SetIORecv(ctx, recv);
CallbackIOSend_delegate send = new CallbackIOSend_delegate(wolfssl.wolfSSL_dtlsCbIOSend);
io.set_send(GCHandle.Alloc(send));
wolfSSL_CTX_SetIOSend(ctx, send);
/* keep memory pinned */
#if WindowsCE
return (IntPtr)GCHandle.Alloc(io, GCHandleType.Pinned);
#else
return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned));
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "ctx_dtls_new error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Free information used in CTX structure
/// </summary>
/* <param name="ctx">structure to free</param>
*/
public static void CTX_free(IntPtr ctx)
{
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
wolfSSL_CTX_free(handles.get_ctx());
handles.free();
gch.Free();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl ctx free error " + e.ToString());
}
}
public static void CTX_set_servername_callback(IntPtr ctx, sni_delegate sni_cb)
{
try {
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
handles.set_sni(GCHandle.Alloc(sni_cb));
wolfSSL_CTX_set_servername_callback(handles.get_ctx(), sni_cb);
} catch (Exception e) {
log(ERROR_LOG, "wolfssl servername callback error: " + e.ToString());
}
}
public static int CTX_set_servername_arg(IntPtr ctx, IntPtr arg)
{
try {
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
handles.set_arg(GCHandle.Alloc(arg));
return wolfSSL_CTX_set_servername_arg(handles.get_ctx(), arg);
} catch (Exception e) {
log(ERROR_LOG, "wolfssl arg servername callback error: " + e.ToString());
return FAILURE;
}
}
public static int CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size)
{
try {
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
return wolfSSL_CTX_UseSNI(handles.get_ctx(), type, data, size);
} catch (Exception e) {
log(ERROR_LOG, "wolfssl ctx use sni error: " + e.ToString());
return FAILURE;
}
}
public static int UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size)
{
try {
#if WindowsCE
GCHandle gch = (GCHandle)ssl;
#else
GCHandle gch = GCHandle.FromIntPtr(ssl);
#endif
ssl_handle handles = (ssl_handle)gch.Target;
return wolfSSL_UseSNI(handles.get_ssl(), type, data, size);
} catch (Exception e) {
log(ERROR_LOG, "wolfssl use sni error: " + e.ToString());
return FAILURE;
}
}
public static ushort SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data)
{
try {
#if WindowsCE
GCHandle gch = (GCHandle)ssl;
#else
GCHandle gch = GCHandle.FromIntPtr(ssl);
#endif
ssl_handle handles = (ssl_handle)gch.Target;
return wolfSSL_SNI_GetRequest(handles.get_ssl(), type, ref data);
} catch (Exception e) {
log(ERROR_LOG, "wolfssl sni get request error: " + e.ToString());
return ushort.MaxValue;
}
}
public static int SNI_GetFromBuffer(byte []clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz)
{
try {
return wolfSSL_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
} catch(Exception e) {
log(ERROR_LOG, "wolfssl sni get from buffer error: " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set identity hint to use
/// </summary>
/// <param name="ctx">pointer to structure of ctx to set hint in</param>
/// <param name="hint">hint to use</param>
/// <returns>1 on success</returns>
#if WindowsCE
public static int CTX_use_psk_identity_hint(IntPtr ctx, string hint)
#else
public static int CTX_use_psk_identity_hint(IntPtr ctx, StringBuilder hint)
#endif
{
try
{
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "CTX use psk identity hint unwrap error");
return FAILURE;
}
#if WindowsCE
return wolfSSL_CTX_use_psk_identity_hint(local_ctx, wolfssl.WideCharToMultiByte(hint));
#else
return wolfSSL_CTX_use_psk_identity_hint(local_ctx, hint);
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl psk identity hint error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set the function to use for PSK connections
/// </summary>
/// <param name="ctx">pointer to CTX that the function is set in</param>
/// <param name="psk_cb">PSK function to use</param>
public static void CTX_set_psk_server_callback(IntPtr ctx, psk_delegate psk_cb)
{
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
handles.set_psk(GCHandle.Alloc(psk_cb));
wolfSSL_CTX_set_psk_server_callback(handles.get_ctx(), psk_cb);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl psk server callback error " + e.ToString());
}
}
/// <summary>
/// Set the function to use for PSK connections
/// </summary>
/// <param name="ctx">pointer to CTX that the function is set in</param>
/// <param name="psk_cb">PSK function to use</param>
public static void CTX_set_psk_client_callback(IntPtr ctx, psk_client_delegate psk_cb)
{
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ctx;
#else
GCHandle gch = GCHandle.FromIntPtr(ctx);
#endif
ctx_handle handles = (ctx_handle)gch.Target;
handles.set_psk(GCHandle.Alloc(psk_cb));
wolfSSL_CTX_set_psk_client_callback(handles.get_ctx(), psk_cb);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl psk client callback error " + e.ToString());
}
}
/// <summary>
/// Set the function to use for PSK connections on a single TLS/DTLS connection
/// </summary>
/// <param name="ctx">pointer to SSL that the function is set in</param>
/// <param name="psk_cb">PSK function to use</param>
public static void set_psk_server_callback(IntPtr ssl, psk_delegate psk_cb)
{
try
{
#if WindowsCE
GCHandle gch = (GCHandle)ssl;
#else
GCHandle gch = GCHandle.FromIntPtr(ssl);
#endif
ssl_handle handles = (ssl_handle)gch.Target;
handles.set_psk(GCHandle.Alloc(psk_cb));
wolfSSL_set_psk_server_callback(handles.get_ssl(), psk_cb);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl psk server callback error " + e.ToString());
}
}
/// <summary>
/// Set Socket for TLS connection
/// </summary>
/// <param name="ssl">structure to set Socket in</param>
/// <param name="fd">Socket to use</param>
/// <returns>1 on success</returns>
public static int set_fd(IntPtr ssl, Socket fd)
{
/* sanity check on inputs */
if (ssl == IntPtr.Zero)
{
return FAILURE;
}
try
{
if (!fd.Equals(null))
{
#if WindowsCE
GCHandle gch = (GCHandle)ssl;
#else
GCHandle gch = GCHandle.FromIntPtr(ssl);
#endif
ssl_handle handles = (ssl_handle)gch.Target;
IntPtr sslCtx = handles.get_ssl();
IntPtr ptr;
GCHandle fd_pin = GCHandle.Alloc(fd); // NOTE: Pinned only needed for buffers, not simple refs
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "wolfssl error setting up fd!!");
return FAILURE;
}
handles.set_fd(fd_pin);
#if WindowsCE
ptr = (IntPtr)fd_pin;
#else
ptr = GCHandle.ToIntPtr(fd_pin);
#endif
wolfSSL_SetIOWriteCtx(sslCtx, ptr); //pass along the socket for writing to
wolfSSL_SetIOReadCtx(sslCtx, ptr); //pass along the socket for reading from
return SUCCESS;
}
return FAILURE;
}
catch (Exception e)
{
log(ERROR_LOG, "Error setting up fd!! " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Get socket of a TLS connection
/// </summary>
/// <param name="ssl">structure to get socket from</param>
/// <returns>Socket object used for connection</returns>
public static Socket get_fd(IntPtr ssl)
{
try
{
IntPtr ptr;
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "wolfssl get_fd error");
return null;
}
ptr = wolfSSL_GetIOReadCtx(sslCtx);
if (ptr != IntPtr.Zero)
{
#if WindowsCE
GCHandle gch = (GCHandle)ptr;
#else
GCHandle gch = GCHandle.FromIntPtr(ptr);
#endif
return (System.Net.Sockets.Socket)gch.Target;
}
return null;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl get_fd error " + e.ToString());
return null;
}
}
/// <summary>
/// Set information needed to send and receive a DTLS connection
/// </summary>
/// <param name="ssl">structure to set information in</param>
/// <param name="udp">UDP object to send and receive</param>
/// <param name="ep">End point of connection</param>
/// <returns>1 on success</returns>
public static int set_dtls_fd(IntPtr ssl, UdpClient udp, IPEndPoint ep)
{
/* sanity check on inputs */
if (ssl == IntPtr.Zero)
{
return FAILURE;
}
try
{
if (!udp.Equals(null) && !ep.Equals(null))
{
IntPtr ptr;
DTLS_con con;
#if WindowsCE
GCHandle gch = (GCHandle)ssl;
#else
GCHandle gch = GCHandle.FromIntPtr(ssl);
#endif
ssl_handle handles = (ssl_handle)gch.Target;
GCHandle fd_pin;
con = new DTLS_con();
con.udp = udp;
con.ep = ep;
fd_pin = GCHandle.Alloc(con);
handles.set_fd(fd_pin);
#if WindowsCE
ptr = (IntPtr)fd_pin;
#else
ptr = GCHandle.ToIntPtr(fd_pin);
#endif
wolfSSL_SetIOWriteCtx(handles.get_ssl(), ptr); //pass along the socket for writing to
wolfSSL_SetIOReadCtx(handles.get_ssl(), ptr); //pass along the socket for reading from
return SUCCESS;
}
return FAILURE;
}
catch (Exception e)
{
log(ERROR_LOG, "Error setting up fd!! " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Get the pointer to DTLS_con class used for connection
/// </summary>
/// <param name="ssl">structure to get connection from</param>
/// <returns>DTLS_con object</returns>
public static DTLS_con get_dtls_fd(IntPtr ssl)
{
try
{
IntPtr ptr;
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "wolfssl get_dtls_fd error");
return null;
}
ptr = wolfSSL_GetIOReadCtx(sslCtx);
if (ptr != IntPtr.Zero)
{
#if WindowsCE
GCHandle gch = (GCHandle)ptr;
#else
GCHandle gch = GCHandle.FromIntPtr(ptr);
#endif
return (DTLS_con)gch.Target;
}
return null;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl get_dtls_fd error " + e.ToString());
return null;
}
}
public static string get_ciphers()
{
try
{
#if WindowsCE
string ciphers = new string(' ', 4096);
#else
StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
#endif
int ret = wolfSSL_get_ciphers(ciphers, ciphers.Length);
if (ret != SUCCESS)
return null;
#if WindowsCE
return wolfssl.MultiByteToWideChar(ciphers);
#else
return ciphers.ToString();
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl get_ciphers error " + e.ToString());
return null;
}
}
/// <summary>
/// Get available cipher suites
/// </summary>
/// <param name="list">list to fill with cipher suite names</param>
/// <param name="sz">size of list available to fill</param>
/// <returns>1 on success</returns>
#if WindowsCE
public static int get_ciphers(string list, int sz)
#else
public static int get_ciphers(StringBuilder list, int sz)
#endif
{
try
{
return wolfSSL_get_ciphers(list, sz);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl get_ciphers error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Initialize wolfSSL library
/// </summary>
/// <returns>1 on success</returns>
public static int Init()
{
try
{
return wolfSSL_Init();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl init error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Clean up wolfSSL library memory
/// </summary>
/// <returns>1 on success</returns>
public static int Cleanup()
{
try
{
return wolfSSL_Cleanup();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl cleanup error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set up TLS version 1.2 method
/// </summary>
/// <returns>pointer to TLSv1.2 method</returns>
public static IntPtr useTLSv1_2_server()
{
try
{
return wolfTLSv1_2_server_method();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Set up TLS version 1.3 method
/// </summary>
/// <returns>pointer to TLSv1.3 method</returns>
public static IntPtr useTLSv1_3_server()
{
try
{
return wolfTLSv1_3_server_method();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Use any TLS version
/// </summary>
/// <returns>pointer to method</returns>
public static IntPtr usev23_server()
{
try
{
return wolfSSLv23_server_method();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Set up TLS version 1.2 method
/// </summary>
/// <returns>pointer to TLSv1.2 method</returns>
public static IntPtr useTLSv1_2_client()
{
try
{
return wolfTLSv1_2_client_method();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Set up TLS version 1.3 method
/// </summary>
/// <returns>pointer to TLSv1.3 method</returns>
public static IntPtr useTLSv1_3_client()
{
try
{
return wolfTLSv1_3_client_method();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Use any TLS version
/// </summary>
/// <returns>pointer to method</returns>
public static IntPtr usev23_client()
{
try
{
return wolfSSLv23_client_method();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Set up DTLS version 1.2
/// </summary>
/// <returns>pointer to DTLSv1.2 method</returns>
public static IntPtr useDTLSv1_2_server()
{
try
{
return wolfDTLSv1_2_server_method();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Set up DTLS version 1.2
/// </summary>
/// <returns>pointer to DTLSv1.2 method</returns>
public static IntPtr useDTLSv1_2_client()
{
try
{
return wolfDTLSv1_2_client_method();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl error " + e.ToString());
return IntPtr.Zero;
}
}
/// <summary>
/// Gets the current cipher suite being used in connection
/// </summary>
/// <param name="ssl">SSL struct to get cipher suite from</param>
/// <returns>string containing current cipher suite</returns>
public static string get_current_cipher(IntPtr ssl)
{
if (ssl == IntPtr.Zero)
return null;
try
{
IntPtr ssl_cipher;
IntPtr ssl_cipher_ptr;
string ssl_cipher_str;
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "wolfssl get_current_cipher error");
return null;
}
ssl_cipher = wolfSSL_get_current_cipher(sslCtx);
ssl_cipher_ptr = wolfSSL_CIPHER_get_name(ssl_cipher);
ssl_cipher_str = wolfssl.PtrToStringAnsi(ssl_cipher_ptr);
return ssl_cipher_str;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl get current cipher error " + e.ToString());
return null;
}
}
/// <summary>
/// Set available cipher suites for all ssl structs created from ctx
/// </summary>
/// <param name="ctx">CTX structure to set</param>
/// <param name="list">List full of ciphers suites</param>
/// <returns>1 on success</returns>
#if WindowsCE
public static int CTX_set_cipher_list(IntPtr ctx, string list)
#else
public static int CTX_set_cipher_list(IntPtr ctx, StringBuilder list)
#endif
{
try
{
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "CTX set cipher list error");
return FAILURE;
}
#if WindowsCE
return wolfSSL_CTX_set_cipher_list(local_ctx, wolfssl.WideCharToMultiByte(list));
#else
return wolfSSL_CTX_set_cipher_list(local_ctx, list);
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl ctx set cipher list error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set available cipher suite in local connection
/// </summary>
/// <param name="ssl">Structure to set cipher suite in</param>
/// <param name="list">List of cipher suites</param>
/// <returns>1 on success</returns>
#if WindowsCE
public static int set_cipher_list(IntPtr ssl, string list)
#else
public static int set_cipher_list(IntPtr ssl, StringBuilder list)
#endif
{
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "wolfssl set_cipher_list error");
return FAILURE;
}
#if WindowsCE
return wolfSSL_set_cipher_list(sslCtx, wolfssl.WideCharToMultiByte(list));
#else
return wolfSSL_set_cipher_list(sslCtx, list);
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl set cipher error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Gets the version of the connection made ie TLSv1.2
/// </summary>
/// <param name="ssl">SSL struct to get version of</param>
/// <returns>string containing version</returns>
public static string get_version(IntPtr ssl)
{
if (ssl == IntPtr.Zero)
return null;
try
{
IntPtr version_ptr;
string version;
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "wolfssl get_version error");
return null;
}
version_ptr = wolfSSL_get_version(sslCtx);
version = wolfssl.PtrToStringAnsi(version_ptr);
return version;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl get version error " + e.ToString());
return null;
}
}
/// <summary>
/// Get a string containing error value and reason
/// </summary>
/// <param name="ssl">SSL struct that had error</param>
/// <returns>String containing error value and reason</returns>
public static string get_error(IntPtr ssl)
{
if (ssl == IntPtr.Zero)
return null;
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "wolfssl get_error error");
return null;
}
int err = wolfSSL_get_error(sslCtx, 0);
IntPtr err_ptr = wolfSSL_ERR_reason_error_string((uint)err);
string err_str = wolfssl.PtrToStringAnsi(err_ptr);
return "Error " + err + " " + err_str;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl get error, error " + e.ToString());
return null;
}
}
/// <summary>
/// Used to load in the certificate file
/// </summary>
/// <param name="ctx">CTX structure for TLS/SSL connections</param>
/// <param name="fileCert">Name of the file to load including absolute path</param>
/// <param name="type">Type of file ie PEM or DER</param>
/// <returns>1 on success</returns>
public static int CTX_use_certificate_file(IntPtr ctx, string fileCert, int type)
{
try
{
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "CTX use certificate file error");
return FAILURE;
}
#if WindowsCE
return wolfSSL_CTX_use_certificate_file(local_ctx, wolfssl.WideCharToMultiByte(fileCert), type);
#else
return wolfSSL_CTX_use_certificate_file(local_ctx, fileCert, type);
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl ctx use cert file error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Used to load in the peer trusted root file
/// </summary>
/// <param name="ctx">CTX structure for TLS/SSL connections</param>
/// <param name="fileCert">Name of the file to load including absolute path</param>
/// <param name="type">path to multiple certificates (try to load all in path) </param>
/// <returns>1 on success</returns>
public static int CTX_load_verify_locations(IntPtr ctx, string fileCert, string path)
{
try
{
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "CTX load verify locations certificate file error");
return FAILURE;
}
#if WindowsCE
return wolfSSL_CTX_load_verify_locations(local_ctx, wolfssl.WideCharToMultiByte(fileCert), wolfssl.WideCharToMultiByte(path));
#else
return wolfSSL_CTX_load_verify_locations(local_ctx, fileCert, path);
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl ctx load verify locations file error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Used to load in the private key from a file
/// </summary>
/// <param name="ctx">CTX structure for TLS/SSL connections </param>
/// <param name="fileKey">Name of the file, including absolute directory</param>
/// <param name="type">Type of file ie PEM or DER</param>
/// <returns>1 on success</returns>
public static int CTX_use_PrivateKey_file(IntPtr ctx, string fileKey, int type)
{
try
{
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "CTX use PrivateKey file error");
return FAILURE;
}
#if WindowsCE
return wolfSSL_CTX_use_PrivateKey_file(local_ctx, wolfssl.WideCharToMultiByte(fileKey), type);
#else
return wolfSSL_CTX_use_PrivateKey_file(local_ctx, fileKey, type);
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl ctx use key file error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set temporary DH parameters
/// </summary>
/// <param name="ssl">Structure to set in</param>
/// <param name="dhparam">file name</param>
/// <param name="file_type">type of file ie PEM</param>
/// <returns>1 on success</returns>
#if WindowsCE
public static int SetTmpDH_file(IntPtr ssl, string dhparam, int file_type)
#else
public static int SetTmpDH_file(IntPtr ssl, StringBuilder dhparam, int file_type)
#endif
{
try
{
IntPtr sslCtx = unwrap_ssl(ssl);
if (sslCtx == IntPtr.Zero)
{
log(ERROR_LOG, "SetTmpDH_file ssl unwrap error");
return FAILURE;
}
#if WindowsCE
return wolfSSL_SetTmpDH_file(sslCtx, wolfssl.WideCharToMultiByte(dhparam), file_type);
#else
return wolfSSL_SetTmpDH_file(sslCtx, dhparam, file_type);
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "SetTmpDH_file error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set temporary DH parameters
/// </summary>
/// <param name="ctx">Structure to set in</param>
/// <param name="dhparam">file name</param>
/// <param name="file_type">type of file ie PEM</param>
/// <returns>1 on success</returns>
#if WindowsCE
public static int CTX_SetTmpDH_file(IntPtr ctx, string dhparam, int file_type)
#else
public static int CTX_SetTmpDH_file(IntPtr ctx, StringBuilder dhparam, int file_type)
#endif
{
try
{
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "CTX_SetTmpDH_file ctx unwrap error");
return FAILURE;
}
#if WindowsCE
return wolfSSL_CTX_SetTmpDH_file(local_ctx, wolfssl.WideCharToMultiByte(dhparam), file_type);
#else
return wolfSSL_CTX_SetTmpDH_file(local_ctx, dhparam, file_type);
#endif
}
catch (Exception e)
{
log(ERROR_LOG, "CTX_SetTmpDH_file error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Used to set the minimum size of DH key
/// </summary>
/// <param name="ctx">Structure to store key size</param>
/// <param name="minDhKey">Min key size </param>
/// <returns>1 on success</returns>
public static int CTX_SetMinDhKey_Sz(IntPtr ctx, short minDhKey)
{
try
{
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "CTX SetMinDhKey_Sz error");
return FAILURE;
}
return wolfSSL_CTX_SetMinDhKey_Sz(local_ctx, minDhKey);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl ctx set min dh key error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set the certificate verification mode and optional callback function
/// </summary>
/// <param name="ctx">pointer to CTX that the function is set in</param>
/// <param name="mode">See SSL_VERIFY options</param>
/// <param name="vc">Optional verify callback function to use</param>
public static int CTX_set_verify(IntPtr ctx, int mode, CallbackVerify_delegate vc)
{
try
{
GCHandle gch;
ctx_handle handles;
IntPtr local_ctx = unwrap_ctx(ctx);
if (local_ctx == IntPtr.Zero)
{
log(ERROR_LOG, "CTX set_verify error");
return FAILURE;
}
/* pin the verify callback to protect from garbage collection */
if (!vc.Equals(null)) {
#if WindowsCE
gch = (GCHandle)ctx;
#else
gch = GCHandle.FromIntPtr(ctx);
#endif
handles = (ctx_handle)gch.Target;
handles.set_vrf(GCHandle.Alloc(vc));
}
wolfSSL_CTX_set_verify(local_ctx, mode, vc);
return SUCCESS;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl ctx set verify error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set the certificate verification mode and optional callback function
/// </summary>
/// <param name="ctx">pointer to SSL object that the function is set in</param>
/// <param name="mode">See SSL_VERIFY options</param>
/// <param name="vc">Optional verify callback function to use</param>
public static int set_verify(IntPtr ssl, int mode, CallbackVerify_delegate vc)
{
try
{
GCHandle gch;
ssl_handle handles;
IntPtr local_ssl = unwrap_ssl(ssl);
if (local_ssl == IntPtr.Zero)
{
log(ERROR_LOG, "set_verify error");
return FAILURE;
}
/* pin the verify callback to protect from garbage collection */
if (!vc.Equals(null)) {
#if WindowsCE
gch = (GCHandle)ssl;
#else
gch = GCHandle.FromIntPtr(ssl);
#endif
handles = (ssl_handle)gch.Target;
handles.set_vrf(GCHandle.Alloc(vc));
}
wolfSSL_set_verify(local_ssl, mode, vc);
return SUCCESS;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl set verify error " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set the certificate verification mode and optional callback function
/// </summary>
/// <param name="ctx">pointer to SSL object that the function is set in</param>
/// <param name="mode">See SSL_VERIFY options</param>
/// <param name="vc">Optional verify callback function to use</param>
public static X509 X509_STORE_CTX_get_current_cert(IntPtr x509Ctx)
{
X509 ret = null;
try
{
if (x509Ctx == IntPtr.Zero)
{
log(ERROR_LOG, "pointer passed in was not set");
return ret;
}
IntPtr x509 = wolfSSL_X509_STORE_CTX_get_current_cert(x509Ctx);
if (x509 != IntPtr.Zero) {
return new X509(x509, false);
}
return ret;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl WOLFSSL_X509_STORE_CTX error " + e.ToString());
return ret;
}
}
/// <summary>
/// Gets all of the certificates from store
/// </summary>
/// <param name="x509Ctx">pointer to store to get certificates from</param>
public static X509[] X509_STORE_CTX_get_certs(IntPtr x509Ctx)
{
X509[] ret = null;
try
{
if (x509Ctx == IntPtr.Zero)
{
log(ERROR_LOG, "pointer passed in was not set");
return ret;
}
IntPtr sk = wolfSSL_X509_STORE_GetCerts(x509Ctx);
if (sk != IntPtr.Zero) {
int i;
int numCerts = wolfSSL_sk_X509_num(sk);
ret = new X509[numCerts];
for (i = 0; i < numCerts; i++) {
IntPtr current = wolfSSL_sk_X509_pop(sk);
if (current != IntPtr.Zero)
{
ret[i] = new X509(current, true);
}
}
wolfSSL_sk_X509_free(sk);
}
return ret;
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl WOLFSSL_X509_STORE_CTX error " + e.ToString());
return ret;
}
}
/// <summary>
/// Get the current WOLFSSL_X509_STORE_CTX error value
/// </summary>
/// <param name="x509Ctx">pointer to store to get error from</param>
public static int X509_STORE_CTX_get_error(IntPtr x509Ctx)
{
try
{
if (x509Ctx == IntPtr.Zero)
{
log(ERROR_LOG, "pointer passed in was not set");
return -1;
}
return wolfSSL_X509_STORE_CTX_get_error(x509Ctx);
}
catch (Exception e)
{
log(ERROR_LOG, "wolfssl WOLFSSL_X509_STORE_CTX error " + e.ToString());
return -1;
}
}
/// <summary>
/// Print low level C library debug messages to stdout when compiled with macro DEBUG_WOLFSSL
/// </summary>
public static void Debugging_ON()
{
wolfSSL_Debugging_ON();
}
/// <summary>
/// Turn off low level C debug messages
/// </summary>
public static void Debugging_OFF()
{
wolfSSL_Debugging_OFF();
}
/// <summary>
/// Set the function to use for logging
/// </summary>
/// <param name="input">Function that conforms as to loggingCb</param>
/// <returns>1 on success</returns>
public static int SetLogging(loggingCb input)
{
internal_log = input;
wolfSSL_SetLoggingCb(input);
return SUCCESS;
}
/// <summary>
/// Log a message to set logging function
/// </summary>
/// <param name="lvl">Level of log message</param>
/// <param name="msg">Message to log</param>
public static void log(int lvl, string msg)
{
/* if log is not set then print nothing */
if (internal_log == null)
return;
#if WindowsCE
internal_log(lvl, msg);
#else
StringBuilder msg_sb = new StringBuilder(msg);
internal_log(lvl, msg_sb);
#endif
}
}
}
|