DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
/*
 * ssltap.c
 *
 * Version 1.0 : Frederick Roeber : 11 June 1997
 * Version 2.0 : Steve Parkinson  : 13 November 1997
 * Version 3.0 : Nelson Bolyard   : 22 July 1998
 * Version 3.0 : Nelson Bolyard   : 22 July 1998
 * Version 3.1 : Nelson Bolyard   : 24 May  1999
 *
 * changes in version 2.0:
 *  Uses NSPR20
 *  Shows structure of SSL negotiation, if enabled.
 *  Shows structure of SSL negotiation, if enabled.
 *
 * This "proxies" a socket connection (like a socks tunnel), but displays the
 * data is it flies by.
 *
 * In the code, the 'client' socket is the one on the client side of the
 * proxy, and the server socket is on the server side.
 *
 */

#include "nspr.h"
#include "nspr.h"
#include "plstr.h"
#include "secutil.h"
#include <memory.h> /* for memcpy, etc. */
#include <string.h>
#include <time.h>

#include "plgetopt.h"
#include "nss.h"
#include "cert.h"
#include "cert.h"
#include "sslproto.h"
#include "ocsp.h"
#include "ocspti.h" /* internals for pretty-printing routines *only* */

struct _DataBufferList;
struct _DataBufferList;
struct _DataBuffer;

typedef struct _DataBufferList {
    struct _DataBuffer *first, *last;
    unsigned int size;
    unsigned int size;
    int isEncrypted;
    unsigned char *msgBuf;
    unsigned int msgBufOffset;
    unsigned int msgBufSize;
    unsigned int hMACsize;
} DataBufferList;


typedef struct _DataBuffer {
    unsigned char *buffer;
    int length;
    int offset; /* offset of first good byte */
    struct _DataBuffer *next;
    struct _DataBuffer *next;
} DataBuffer;

struct sslhandshake {
    PRUint8 type;
    PRUint32 length;
    PRUint32 length;
};

typedef struct _SSLRecord {
    PRUint8 type;
    PRUint8 ver_maj, ver_min;
    PRUint8 ver_maj, ver_min;

    PRUint8 length[2];
} SSLRecord;

typedef struct _ClientHelloV2 {
typedef struct _ClientHelloV2 {
    PRUint8 length[2];
    PRUint8 type;
    PRUint8 version[2];
    PRUint8 cslength[2];
    PRUint8 sidlength[2];
    PRUint8 rndlength[2];
    PRUint8 csuites[1];
    PRUint8 csuites[1];
} ClientHelloV2;

typedef struct _ServerHelloV2 {
    PRUint8 length[2];
    PRUint8 type;
    PRUint8 type;
    PRUint8 sidhit;
    PRUint8 certtype;
    PRUint8 version[2];
    PRUint8 certlength[2];
    PRUint8 cslength[2];
    PRUint8 cslength[2];
    PRUint8 cidlength[2];
} ServerHelloV2;

typedef struct _ClientMasterKeyV2 {
    PRUint8 length[2];
    PRUint8 length[2];
    PRUint8 type;

    PRUint8 cipherkind[3];
    PRUint8 cipherkind[3];
    PRUint8 clearkey[2];
    PRUint8 clearkey[2];
    PRUint8 secretkey[2];

} ClientMasterKeyV2;
} ClientMasterKeyV2;

/* forward declaration */
void showErr(const char *msg);

#define TAPBUFSIZ 16384

#define DEFPORT 1924
#include <ctype.h>


const char *progName;
int hexparse = 0;
int sslparse = 0;
int sslhexparse = 0;
int looparound = 0;
int looparound = 0;
int fancy = 0;
int isV2Session = 0;
int currentcipher = 0;
DataBufferList clientstream, serverstream;


#define PR_FPUTS(x) PR_fprintf(PR_STDOUT, x)

#define GET_SHORT(x) ((PRUint16)(((PRUint16)((PRUint8 *)x)[0]) << 8) + ((PRUint16)((PRUint8 *)x)[1]))
#define GET_24(x) ((PRUint32)(              \
    (((PRUint32)((PRUint8 *)x)[0]) << 16) + \
    (((PRUint32)((PRUint8 *)x)[0]) << 16) + \
    (((PRUint32)((PRUint8 *)x)[1]) << 8) +  \
    (((PRUint32)((PRUint8 *)x)[2]) << 0)))
#define GET_32(x) ((PRUint32)(              \
    (((PRUint32)((PRUint8 *)x)[0]) << 24) + \
    (((PRUint32)((PRUint8 *)x)[1]) << 16) + \
    (((PRUint32)((PRUint8 *)x)[1]) << 16) + \
    (((PRUint32)((PRUint8 *)x)[2]) << 8) +  \
    (((PRUint32)((PRUint8 *)x)[3]) << 0)))


void print_hex(int amt, unsigned char *buf);
void print_hex(int amt, unsigned char *buf);
void read_stream_bytes(unsigned char *d, DataBufferList *db, int length);

void
void
myhalt(int dblsize, int collectedsize)
{

    PR_fprintf(PR_STDERR, "HALTED\n");
    PR_ASSERT(dblsize == collectedsize);
    PR_ASSERT(dblsize == collectedsize);
    exit(13);
}

const char *
get_error_text(int error)
get_error_text(int error)
{
    switch (error) {
        case PR_IO_TIMEOUT_ERROR:
            return "Timeout";
            break;
            break;
        case PR_CONNECT_REFUSED_ERROR:
            return "Connection refused";
            break;
        case PR_NETWORK_UNREACHABLE_ERROR:
            return "Network unreachable";
            break;
        case PR_BAD_ADDRESS_ERROR:
        case PR_BAD_ADDRESS_ERROR:
            return "Bad address";
            break;
        case PR_CONNECT_RESET_ERROR:
            return "Connection reset";
            break;
            break;
        case PR_PIPE_ERROR:
            return "Pipe error";
            break;
    }


    return "";
}

void
void
check_integrity(DataBufferList *dbl)
{
    DataBuffer *db;
    int i;


    db = dbl->first;
    i = 0;
    while (db) {
        i += db->length - db->offset;
        db = db->next;
        db = db->next;
    }
    if (i != dbl->size) {
        myhalt(dbl->size, i);
    }
}
}

/* Free's the DataBuffer at the head of the list and returns the pointer
 * to the new head of the list.
 */
DataBuffer *
DataBuffer *
free_head(DataBufferList *dbl)
{
    DataBuffer *db = dbl->first;
    PR_ASSERT(db->offset >= db->length);
    if (db->offset >= db->length) {
        dbl->first = db->next;
        if (dbl->first == NULL) {
        if (dbl->first == NULL) {
            dbl->last = NULL;
        }
        PORT_Free(db->buffer);
        PORT_Free(db);
        db = dbl->first;
        db = dbl->first;
    }
    return db;
}

void
void
read_stream_bytes(unsigned char *d, DataBufferList *dbl, int length)
{
    int copied = 0;
    int copied = 0;
    DataBuffer *db = dbl->first;

    if (!db) {
        PR_fprintf(PR_STDERR, "assert failed - dbl->first is null\n");
        exit(8);
        exit(8);
    }
    while (length) {
        int toCopy;
        /* find the number of bytes to copy from the head buffer */
        /* if there's too many in this buffer, then only copy 'length' */
        /* if there's too many in this buffer, then only copy 'length' */
        toCopy = PR_MIN(db->length - db->offset, length);

        memcpy(d + copied, db->buffer + db->offset, toCopy);
        copied += toCopy;
        db->offset += toCopy;
        db->offset += toCopy;
        length -= toCopy;
        dbl->size -= toCopy;

        /* if we emptied the head buffer */
        if (db->offset >= db->length) {
        if (db->offset >= db->length) {
            db = free_head(dbl);
        }
    }

    check_integrity(dbl);
    check_integrity(dbl);
}


void
flush_stream(DataBufferList *dbl)
{
    DataBuffer *db = dbl->first;
    DataBuffer *db = dbl->first;
    check_integrity(dbl);
    while (db) {
        db->offset = db->length;
        db = free_head(dbl);
    }
    }
    dbl->size = 0;
    check_integrity(dbl);
    if (dbl->msgBuf) {
        PORT_Free(dbl->msgBuf);
        dbl->msgBuf = NULL;
        dbl->msgBuf = NULL;
    }
    dbl->msgBufOffset = 0;
    dbl->msgBufSize = 0;
    dbl->hMACsize = 0;
}
}

const char *
V2CipherString(int cs_int)
{
    char *cs_str;
    char *cs_str;
    cs_str = NULL;
    switch (cs_int) {

        case 0x010080:
            cs_str = "SSL2/RSA/RC4-128/MD5";
            break;
            break;
        case 0x020080:
            cs_str = "SSL2/RSA/RC4-40/MD5";
            break;
        case 0x030080:
            cs_str = "SSL2/RSA/RC2CBC128/MD5";
            cs_str = "SSL2/RSA/RC2CBC128/MD5";
            break;
        case 0x040080:
            cs_str = "SSL2/RSA/RC2CBC40/MD5";
            break;
        case 0x050080:
        case 0x050080:
            cs_str = "SSL2/RSA/IDEA128CBC/MD5";
            break;
        case 0x060040:
            cs_str = "SSL2/RSA/DES56-CBC/MD5";
            cs_str = "SSL2/RSA/DES56-CBC/MD5";
            break;
        case 0x0700C0:
            cs_str = "SSL2/RSA/3DES192EDE-CBC/MD5";
            break;


        case 0x000001:
            cs_str = "SSL3/RSA/NULL/MD5";
            break;
        case 0x000002:
            cs_str = "SSL3/RSA/NULL/SHA";
            cs_str = "SSL3/RSA/NULL/SHA";
            break;
        case 0x000003:
            cs_str = "SSL3/RSA/RC4-40/MD5";
            break;
        case 0x000004:
        case 0x000004:
            cs_str = "SSL3/RSA/RC4-128/MD5";
            break;
        case 0x000005:
            cs_str = "SSL3/RSA/RC4-128/SHA";
            break;
            break;
        case 0x000006:
            cs_str = "SSL3/RSA/RC2CBC40/MD5";
            break;
        case 0x000007:
            cs_str = "SSL3/RSA/IDEA128CBC/SHA";
            cs_str = "SSL3/RSA/IDEA128CBC/SHA";
            break;
        case 0x000008:
            cs_str = "SSL3/RSA/DES40-CBC/SHA";
            break;
        case 0x000009:
        case 0x000009:
            cs_str = "SSL3/RSA/DES56-CBC/SHA";
            break;
        case 0x00000A:
        case 0x00000A:
            cs_str = "SSL3/RSA/3DES192EDE-CBC/SHA";
            break;

        case 0x00000B:
            cs_str = "SSL3/DH-DSS/DES40-CBC/SHA";
            cs_str = "SSL3/DH-DSS/DES40-CBC/SHA";
            break;
        case 0x00000C:
            cs_str = "SSL3/DH-DSS/DES56-CBC/SHA";
            break;
        case 0x00000D:
        case 0x00000D:
            cs_str = "SSL3/DH-DSS/DES192EDE3CBC/SHA";
            break;
        case 0x00000E:
            cs_str = "SSL3/DH-RSA/DES40-CBC/SHA";
            break;
            break;
        case 0x00000F:
            cs_str = "SSL3/DH-RSA/DES56-CBC/SHA";
            break;
        case 0x000010:
            cs_str = "SSL3/DH-RSA/3DES192EDE-CBC/SHA";
            cs_str = "SSL3/DH-RSA/3DES192EDE-CBC/SHA";
            break;

        case 0x000011:
            cs_str = "SSL3/DHE-DSS/DES40-CBC/SHA";
            break;
        case 0x000012:
            cs_str = "SSL3/DHE-DSS/DES56-CBC/SHA";
            break;
            break;
        case 0x000013:
            cs_str = "SSL3/DHE-DSS/DES192EDE3CBC/SHA";
            break;
        case 0x000014:
            cs_str = "SSL3/DHE-RSA/DES40-CBC/SHA";
            cs_str = "SSL3/DHE-RSA/DES40-CBC/SHA";
            break;
        case 0x000015:
            cs_str = "SSL3/DHE-RSA/DES56-CBC/SHA";
            break;
        case 0x000016:
        case 0x000016:
            cs_str = "SSL3/DHE-RSA/3DES192EDE-CBC/SHA";
            break;

        case 0x000017:
            cs_str = "SSL3/DH-anon/RC4-40/MD5";
            cs_str = "SSL3/DH-anon/RC4-40/MD5";
            break;
        case 0x000018:
            cs_str = "SSL3/DH-anon/RC4-128/MD5";
            cs_str = "SSL3/DH-anon/RC4-128/MD5";
            break;
        case 0x000019:
            cs_str = "SSL3/DH-anon/DES40-CBC/SHA";
            break;
        case 0x00001A:
        case 0x00001A:
            cs_str = "SSL3/DH-anon/DES56-CBC/SHA";
            break;
        case 0x00001B:
            cs_str = "SSL3/DH-anon/3DES192EDE-CBC/SHA";
            cs_str = "SSL3/DH-anon/3DES192EDE-CBC/SHA";
            break;

        case 0x00001C:
            cs_str = "SSL3/FORTEZZA-DMS/NULL/SHA";
            break;
            break;
        case 0x00001D:
            cs_str = "SSL3/FORTEZZA-DMS/FORTEZZA-CBC/SHA";
            break;
        case 0x00001E:
            cs_str = "SSL3/FORTEZZA-DMS/RC4-128/SHA";
            cs_str = "SSL3/FORTEZZA-DMS/RC4-128/SHA";
            break;

        case 0x00002F:
            cs_str = "TLS/RSA/AES128-CBC/SHA";
            break;
        case 0x000030:
        case 0x000030:
            cs_str = "TLS/DH-DSS/AES128-CBC/SHA";
            break;
        case 0x000031:
            cs_str = "TLS/DH-RSA/AES128-CBC/SHA";
            break;
            break;
        case 0x000032:
            cs_str = "TLS/DHE-DSS/AES128-CBC/SHA";
            break;
        case 0x000033:
            cs_str = "TLS/DHE-RSA/AES128-CBC/SHA";
            cs_str = "TLS/DHE-RSA/AES128-CBC/SHA";
            break;
        case 0x000034:
            cs_str = "TLS/DH-ANON/AES128-CBC/SHA";
            break;


        case 0x000035:
            cs_str = "TLS/RSA/AES256-CBC/SHA";
            break;
        case 0x000036:
            cs_str = "TLS/DH-DSS/AES256-CBC/SHA";
            cs_str = "TLS/DH-DSS/AES256-CBC/SHA";
            break;
        case 0x000037:
            cs_str = "TLS/DH-RSA/AES256-CBC/SHA";
            break;
        case 0x000038:
            cs_str = "TLS/DHE-DSS/AES256-CBC/SHA";
            break;
        case 0x000039:
        case 0x000039:
            cs_str = "TLS/DHE-RSA/AES256-CBC/SHA";
            break;
        case 0x00003A:
            cs_str = "TLS/DH-ANON/AES256-CBC/SHA";
            break;
            break;

        case 0x00003B:
            cs_str = "TLS/RSA/NULL/SHA256";
            break;
        case 0x00003C:
        case 0x00003C:
            cs_str = "TLS/RSA/AES128-CBC/SHA256";
            break;
        case 0x00003D:
            cs_str = "TLS/RSA/AES256-CBC/SHA256";
            break;
            break;
        case 0x00003E:
            cs_str = "TLS/DH-DSS/AES128-CBC/SHA256";
            break;
            break;
        case 0x00003F:
            cs_str = "TLS/DH-RSA/AES128-CBC/SHA256";
            break;
        case 0x000040:
            cs_str = "TLS/DHE-DSS/AES128-CBC/SHA256";
            cs_str = "TLS/DHE-DSS/AES128-CBC/SHA256";
            break;

        case 0x000041:
            cs_str = "TLS/RSA/CAMELLIA128-CBC/SHA";
            cs_str = "TLS/RSA/CAMELLIA128-CBC/SHA";
            break;
        case 0x000042:
            cs_str = "TLS/DH-DSS/CAMELLIA128-CBC/SHA";
            break;
        case 0x000043:
        case 0x000043:
            cs_str = "TLS/DH-RSA/CAMELLIA128-CBC/SHA";
            break;
        case 0x000044:
            cs_str = "TLS/DHE-DSS/CAMELLIA128-CBC/SHA";
            break;
            break;
        case 0x000045:
            cs_str = "TLS/DHE-RSA/CAMELLIA128-CBC/SHA";
            break;
        case 0x000046:
            cs_str = "TLS/DH-ANON/CAMELLIA128-CBC/SHA";
            break;

        case 0x000060:
            cs_str = "TLS/RSA-EXPORT1024/RC4-56/MD5";
            break;
            break;
        case 0x000061:
            cs_str = "TLS/RSA-EXPORT1024/RC2CBC56/MD5";
            break;
        case 0x000062:
            cs_str = "TLS/RSA-EXPORT1024/DES56-CBC/SHA";
            cs_str = "TLS/RSA-EXPORT1024/DES56-CBC/SHA";
            break;
        case 0x000064:
            cs_str = "TLS/RSA-EXPORT1024/RC4-56/SHA";
            break;
        case 0x000063:
        case 0x000063:
            cs_str = "TLS/DHE-DSS_EXPORT1024/DES56-CBC/SHA";
            break;
        case 0x000065:
            cs_str = "TLS/DHE-DSS_EXPORT1024/RC4-56/SHA";
            break;
            break;
        case 0x000066:
            cs_str = "TLS/DHE-DSS/RC4-128/SHA";
            break;


        case 0x000067:
            cs_str = "TLS/DHE-RSA/AES128-CBC/SHA256";
            break;
        case 0x000068:
            cs_str = "TLS/DH-DSS/AES256-CBC/SHA256";
            cs_str = "TLS/DH-DSS/AES256-CBC/SHA256";
            break;
        case 0x000069:
            cs_str = "TLS/DH-RSA/AES256-CBC/SHA256";
            break;
        case 0x00006A:
        case 0x00006A:
            cs_str = "TLS/DHE-DSS/AES256-CBC/SHA256";
            break;
        case 0x00006B:
            cs_str = "TLS/DHE-RSA/AES256-CBC/SHA256";
            break;
            break;

        case 0x000072:
            cs_str = "TLS/DHE-DSS/3DESEDE-CBC/RMD160";
            break;
        case 0x000073:
        case 0x000073:
            cs_str = "TLS/DHE-DSS/AES128-CBC/RMD160";
            break;
        case 0x000074:
            cs_str = "TLS/DHE-DSS/AES256-CBC/RMD160";
            break;

        case 0x000079:
        case 0x000079:
            cs_str = "TLS/DHE-RSA/AES256-CBC/RMD160";
            break;

        case 0x00007C:
            cs_str = "TLS/RSA/3DESEDE-CBC/RMD160";
            cs_str = "TLS/RSA/3DESEDE-CBC/RMD160";
            break;
        case 0x00007D:
            cs_str = "TLS/RSA/AES128-CBC/RMD160";
            break;
        case 0x00007E:
        case 0x00007E:
            cs_str = "TLS/RSA/AES256-CBC/RMD160";
            break;

        case 0x000080:
            cs_str = "TLS/GOST341094/GOST28147-OFB/GOST28147";
            cs_str = "TLS/GOST341094/GOST28147-OFB/GOST28147";
            break;
        case 0x000081:
            cs_str = "TLS/GOST34102001/GOST28147-OFB/GOST28147";
            break;
        case 0x000082:
            cs_str = "TLS/GOST341094/NULL/GOSTR3411";
            break;
        case 0x000083:
            cs_str = "TLS/GOST34102001/NULL/GOSTR3411";
            cs_str = "TLS/GOST34102001/NULL/GOSTR3411";
            break;

        case 0x000084:
            cs_str = "TLS/RSA/CAMELLIA256-CBC/SHA";
            break;
            break;
        case 0x000085:
            cs_str = "TLS/DH-DSS/CAMELLIA256-CBC/SHA";
            break;
        case 0x000086:
        case 0x000086:
            cs_str = "TLS/DH-RSA/CAMELLIA256-CBC/SHA";
            break;
        case 0x000087:
            cs_str = "TLS/DHE-DSS/CAMELLIA256-CBC/SHA";
            break;
            break;
        case 0x000088:
            cs_str = "TLS/DHE-RSA/CAMELLIA256-CBC/SHA";
            break;
        case 0x000089:
            cs_str = "TLS/DH-ANON/CAMELLIA256-CBC/SHA";
            break;
        case 0x00008A:
            cs_str = "TLS/PSK/RC4-128/SHA";
            break;
            break;
        case 0x00008B:
            cs_str = "TLS/PSK/3DES-EDE-CBC/SHA";
            break;
        case 0x00008C:
            cs_str = "TLS/PSK/AES128-CBC/SHA";
            cs_str = "TLS/PSK/AES128-CBC/SHA";
            break;
        case 0x00008D:
            cs_str = "TLS/PSK/AES256-CBC/SHA";
            break;
        case 0x00008E:
        case 0x00008E:
            cs_str = "TLS/DHE-PSK/RC4-128/SHA";
            break;
        case 0x00008F:
            cs_str = "TLS/DHE-PSK/3DES-EDE-CBC/SHA";
            break;
            break;
        case 0x000090:
            cs_str = "TLS/DHE-PSK/AES128-CBC/SHA";
            break;
        case 0x000091:
            cs_str = "TLS/DHE-PSK/AES256-CBC/SHA";
            break;
            break;
        case 0x000092:
            cs_str = "TLS/RSA-PSK/RC4-128/SHA";
            break;
        case 0x000093:
            cs_str = "TLS/RSA-PSK/3DES-EDE-CBC/SHA";
            cs_str = "TLS/RSA-PSK/3DES-EDE-CBC/SHA";
            break;
        case 0x000094:
            cs_str = "TLS/RSA-PSK/AES128-CBC/SHA";
            break;
        case 0x000095:
        case 0x000095:
            cs_str = "TLS/RSA-PSK/AES256-CBC/SHA";
            break;
        case 0x000096:
            cs_str = "TLS/RSA/SEED-CBC/SHA";
            cs_str = "TLS/RSA/SEED-CBC/SHA";
            break;
        case 0x000097:
            cs_str = "TLS/DH-DSS/SEED-CBC/SHA";
            break;
        case 0x000098:
        case 0x000098:
            cs_str = "TLS/DH-RSA/SEED-CBC/SHA";
            break;
        case 0x000099:
            cs_str = "TLS/DHE-DSS/SEED-CBC/SHA";
            break;
            break;
        case 0x00009A:
            cs_str = "TLS/DHE-RSA/SEED-CBC/SHA";
            break;
        case 0x00009B:
            cs_str = "TLS/DH-ANON/SEED-CBC/SHA";
            cs_str = "TLS/DH-ANON/SEED-CBC/SHA";
            break;
        case 0x00009C:
            cs_str = "TLS/RSA/AES128-GCM/SHA256";
            break;
        case 0x00009E:
        case 0x00009E:
            cs_str = "TLS/DHE-RSA/AES128-GCM/SHA256";
            break;

        case 0x0000FF:
            cs_str = "TLS_EMPTY_RENEGOTIATION_INFO_SCSV";
            cs_str = "TLS_EMPTY_RENEGOTIATION_INFO_SCSV";
            break;
        case 0x005600:
            cs_str = "TLS_FALLBACK_SCSV";
            break;


        case 0x00C001:
            cs_str = "TLS/ECDH-ECDSA/NULL/SHA";
            cs_str = "TLS/ECDH-ECDSA/NULL/SHA";
            break;
        case 0x00C002:
            cs_str = "TLS/ECDH-ECDSA/RC4-128/SHA";
            break;
        case 0x00C003:
        case 0x00C003:
            cs_str = "TLS/ECDH-ECDSA/3DES-EDE-CBC/SHA";
            break;
        case 0x00C004:
            cs_str = "TLS/ECDH-ECDSA/AES128-CBC/SHA";
            break;
        case 0x00C005:
            cs_str = "TLS/ECDH-ECDSA/AES256-CBC/SHA";
            cs_str = "TLS/ECDH-ECDSA/AES256-CBC/SHA";
            break;
        case 0x00C006:
            cs_str = "TLS/ECDHE-ECDSA/NULL/SHA";
            break;
        case 0x00C007:
        case 0x00C007:
            cs_str = "TLS/ECDHE-ECDSA/RC4-128/SHA";
            break;
        case 0x00C008:
            cs_str = "TLS/ECDHE-ECDSA/3DES-EDE-CBC/SHA";
            break;
        case 0x00C009:
            cs_str = "TLS/ECDHE-ECDSA/AES128-CBC/SHA";
            break;
        case 0x00C00A:
        case 0x00C00A:
            cs_str = "TLS/ECDHE-ECDSA/AES256-CBC/SHA";
            break;
        case 0x00C00B:
            cs_str = "TLS/ECDH-RSA/NULL/SHA";
            break;
            break;
        case 0x00C00C:
            cs_str = "TLS/ECDH-RSA/RC4-128/SHA";
            break;
        case 0x00C00D:
            cs_str = "TLS/ECDH-RSA/3DES-EDE-CBC/SHA";
            cs_str = "TLS/ECDH-RSA/3DES-EDE-CBC/SHA";
            break;
        case 0x00C00E:
            cs_str = "TLS/ECDH-RSA/AES128-CBC/SHA";
            break;
        case 0x00C00F:
        case 0x00C00F:
            cs_str = "TLS/ECDH-RSA/AES256-CBC/SHA";
            break;
        case 0x00C010:
            cs_str = "TLS/ECDHE-RSA/NULL/SHA";
            break;
            break;
        case 0x00C011:
            cs_str = "TLS/ECDHE-RSA/RC4-128/SHA";
            break;
        case 0x00C012:
            cs_str = "TLS/ECDHE-RSA/3DES-EDE-CBC/SHA";
            break;
        case 0x00C013:
        case 0x00C013:
            cs_str = "TLS/ECDHE-RSA/AES128-CBC/SHA";
            break;
        case 0x00C014:
            cs_str = "TLS/ECDHE-RSA/AES256-CBC/SHA";
            break;
            break;
        case 0x00C015:
            cs_str = "TLS/ECDH-anon/NULL/SHA";
            break;
        case 0x00C016:
            cs_str = "TLS/ECDH-anon/RC4-128/SHA";
            cs_str = "TLS/ECDH-anon/RC4-128/SHA";
            break;
        case 0x00C017:
            cs_str = "TLS/ECDH-anon/3DES-EDE-CBC/SHA";
            break;
        case 0x00C018:
        case 0x00C018:
            cs_str = "TLS/ECDH-anon/AES128-CBC/SHA";
            break;
        case 0x00C019:
            cs_str = "TLS/ECDH-anon/AES256-CBC/SHA";
            break;
            break;

        case 0x00C023:
            cs_str = "TLS/ECDHE-ECDSA/AES128-CBC/SHA256";
            break;
        case 0x00C024:
        case 0x00C024:
            cs_str = "TLS/ECDHE-ECDSA/AES256-CBC/SHA384";
            break;
        case 0x00C025:
            cs_str = "TLS/ECDH-ECDSA/AES128-CBC/SHA256";
            break;
            break;
        case 0x00C026:
            cs_str = "TLS/ECDH-ECDSA/AES256-CBC/SHA384";
            break;
        case 0x00C027:
            cs_str = "TLS/ECDHE-RSA/AES128-CBC/SHA256";
            cs_str = "TLS/ECDHE-RSA/AES128-CBC/SHA256";
            break;
        case 0x00C028:
            cs_str = "TLS/ECDHE-RSA/AES256-CBC/SHA384";
            break;
        case 0x00C029:
        case 0x00C029:
            cs_str = "TLS/ECDH-RSA/AES128-CBC/SHA256";
            break;
        case 0x00C02A:
            cs_str = "TLS/ECDH-RSA/AES256-CBC/SHA384";
            break;
            break;
        case 0x00C02B:
            cs_str = "TLS/ECDHE-ECDSA/AES128-GCM/SHA256";
            break;
        case 0x00C02C:
            cs_str = "TLS/ECDHE-ECDSA/AES256-GCM/SHA384";
            cs_str = "TLS/ECDHE-ECDSA/AES256-GCM/SHA384";
            break;
        case 0x00C02F:
            cs_str = "TLS/ECDHE-RSA/AES128-GCM/SHA256";
            break;


        case 0x00CCA8:
            cs_str = "TLS/ECDHE-RSA/CHACHA20-POLY1305/SHA256";
            break;
        case 0x00CCA9:
        case 0x00CCA9:
            cs_str = "TLS/ECDHE-ECDSA/CHACHA20-POLY1305/SHA256";
            break;
        case 0x00CCAA:
        case 0x00CCAA:
            cs_str = "TLS/DHE-RSA/CHACHA20-POLY1305/SHA256";
            break;

        case 0x00FEFF:
            cs_str = "SSL3/RSA-FIPS/3DESEDE-CBC/SHA";
            cs_str = "SSL3/RSA-FIPS/3DESEDE-CBC/SHA";
            break;
        case 0x00FEFE:
            cs_str = "SSL3/RSA-FIPS/DES-CBC/SHA";
            break;
        case 0x00FFE1:
        case 0x00FFE1:
            cs_str = "SSL3/RSA-FIPS/DES56-CBC/SHA";
            break;
            break;
        case 0x00FFE0:
            cs_str = "SSL3/RSA-FIPS/3DES192EDE-CBC/SHA";
            break;

        /* the string literal is broken up to avoid trigraphs */
        /* the string literal is broken up to avoid trigraphs */
        default:
            cs_str = "????"
                     "/????????"
                     "/?????????"
                     "/???";
            break;
            break;
    }

    return cs_str;
}


const char *
CompressionMethodString(int cm_int)
{
    char *cm_str;
    char *cm_str;
    cm_str = NULL;
    switch (cm_int) {
        case 0:
            cm_str = "NULL";
            break;
            break;
        case 1:
            cm_str = "DEFLATE";
            break; /* RFC 3749 */
        case 64:
            cm_str = "LZS";
            cm_str = "LZS";
            break; /* RFC 3943 */
        default:
            cm_str = "???";
            break;
    }

    return cm_str;
    return cm_str;
}

const char *
helloExtensionNameString(int ex_num)
{
{
    const char *ex_name = NULL;
    static char buf[10];

    switch (ex_num) {
        case 0:
        case 0:
            ex_name = "server_name";
            break;
        case 1:
            ex_name = "max_fragment_length";
            break;
            break;
        case 2:
            ex_name = "client_certificate_url";
            break;
        case 3:
            ex_name = "trusted_ca_keys";
            ex_name = "trusted_ca_keys";
            break;
        case 4:
            ex_name = "truncated_hmac";
            break;
        case 5:
        case 5:
            ex_name = "status_request";
            break;
        case 10:
            ex_name = "elliptic_curves";
            break;
            break;
        case 11:
            ex_name = "ec_point_formats";
            break;
        case 13:
            ex_name = "signature_algorithms";
            ex_name = "signature_algorithms";
            break;
        case 35:
            ex_name = "session_ticket";
            break;
        case 0xff01:
        case 0xff01:
            ex_name = "renegotiation_info";
            break;
        default:
            sprintf(buf, "%d", ex_num);
            ex_name = (const char *)buf;
            break;
            break;
    }

    return ex_name;
}

static int
isNULLmac(int cs_int)
isNULLmac(int cs_int)
{
    return (cs_int == TLS_NULL_WITH_NULL_NULL);
}

static int
static int
isNULLcipher(int cs_int)
{
    return ((cs_int == TLS_RSA_WITH_NULL_MD5) ||
            (cs_int == TLS_RSA_WITH_NULL_SHA) ||
            (cs_int == SSL_FORTEZZA_DMS_WITH_NULL_SHA) ||
            (cs_int == SSL_FORTEZZA_DMS_WITH_NULL_SHA) ||
            (cs_int == TLS_ECDH_ECDSA_WITH_NULL_SHA) ||
            (cs_int == TLS_ECDHE_ECDSA_WITH_NULL_SHA) ||
            (cs_int == TLS_ECDH_RSA_WITH_NULL_SHA) ||
            (cs_int == TLS_ECDHE_RSA_WITH_NULL_SHA));
}
}

void
partial_packet(int thispacket, int size, int needed)
{
    PR_fprintf(PR_STDOUT, "(%u bytes", thispacket);
    PR_fprintf(PR_STDOUT, "(%u bytes", thispacket);
    if (thispacket < needed) {
    if (thispacket < needed) {
        PR_fprintf(PR_STDOUT, ", making %u", size);
    }
    PR_fprintf(PR_STDOUT, " of %u", needed);
    if (size > needed) {
        PR_fprintf(PR_STDOUT, ", with %u left over", size - needed);
        PR_fprintf(PR_STDOUT, ", with %u left over", size - needed);
    }
    PR_fprintf(PR_STDOUT, ")\n");
}
}


char *
get_time_string(void)
{
{
    char *cp;
    char *eol;
    time_t tt;

    time(&tt);
    time(&tt);
    cp = ctime(&tt);
    eol = strchr(cp, '\n');
    if (eol)
        *eol = 0;
    return cp;
    return cp;
}

void
print_sslv2(DataBufferList *s, unsigned char *recordBuf, unsigned int recordLen)
{
    ClientHelloV2 *chv2;
    ClientHelloV2 *chv2;
    ServerHelloV2 *shv2;
    unsigned char *pos;
    unsigned int p;
    unsigned int q;
    PRUint32 len;
    PRUint32 len;

    chv2 = (ClientHelloV2 *)recordBuf;
    shv2 = (ServerHelloV2 *)recordBuf;
    if (s->isEncrypted) {
        PR_fprintf(PR_STDOUT, " [ssl2]  Encrypted {...}\n");
        PR_fprintf(PR_STDOUT, " [ssl2]  Encrypted {...}\n");
        return;
    }
    PR_fprintf(PR_STDOUT, " [%s]", get_time_string());
    switch (chv2->type) {
    switch (chv2->type) {
        case 1:
            PR_fprintf(PR_STDOUT, " [ssl2]  ClientHelloV2 {\n");
            PR_fprintf(PR_STDOUT, "           version = {0x%02x, 0x%02x}\n",
                       (PRUint32)chv2->version[0], (PRUint32)chv2->version[1]);
            PR_fprintf(PR_STDOUT, "           cipher-specs-length = %d (0x%02x)\n",
            PR_fprintf(PR_STDOUT, "           cipher-specs-length = %d (0x%02x)\n",
                       (PRUint32)(GET_SHORT((chv2->cslength))),
                       (PRUint32)(GET_SHORT((chv2->cslength))));
            PR_fprintf(PR_STDOUT, "           sid-length = %d (0x%02x)\n",
                       (PRUint32)(GET_SHORT((chv2->sidlength))),
                       (PRUint32)(GET_SHORT((chv2->sidlength))));
            PR_fprintf(PR_STDOUT, "           challenge-length = %d (0x%02x)\n",
            PR_fprintf(PR_STDOUT, "           challenge-length = %d (0x%02x)\n",
                       (PRUint32)(GET_SHORT((chv2->rndlength))),
                       (PRUint32)(GET_SHORT((chv2->rndlength))));
            PR_fprintf(PR_STDOUT, "           cipher-suites = { \n");
            for (p =
                     0;
                     0;
                 p < (PRUint32)GET_SHORT((chv2->cslength)); p += 3) {
                PRUint32 cs_int = GET_24((&chv2->csuites[p]));
                const char *cs_str =
                    V2CipherString(cs_int);


                PR_fprintf(PR_STDOUT, "                (0x%06x) %s\n",
                           cs_int, cs_str);
            }
            q = p;
            PR_fprintf(PR_STDOUT, "                }\n");
            PR_fprintf(PR_STDOUT, "                }\n");
            if (GET_SHORT((chv2->sidlength))) {
                PR_fprintf(PR_STDOUT, "           session-id = { ");
                for (p = 0;
                     p < (PRUint32)GET_SHORT((chv2->sidlength)); p += 2) {
                    PR_fprintf(PR_STDOUT, "0x%04x ", (PRUint32)(GET_SHORT((&chv2->csuites[p + q]))));
                    PR_fprintf(PR_STDOUT, "0x%04x ", (PRUint32)(GET_SHORT((&chv2->csuites[p + q]))));
                }
            }
            q += p;
            PR_fprintf(PR_STDOUT, "}\n");
            if (GET_SHORT((chv2->rndlength))) {
            if (GET_SHORT((chv2->rndlength))) {
                PR_fprintf(PR_STDOUT, "           challenge = { ");
                for (p = 0;
                     p < (PRUint32)GET_SHORT((chv2->rndlength)); p += 2) {
                    PR_fprintf(PR_STDOUT, "0x%04x ", (PRUint32)(GET_SHORT((&chv2->csuites[p + q]))));
                }
                }
                PR_fprintf(PR_STDOUT, "}\n");
            }
            PR_fprintf(PR_STDOUT, "}\n");
            break;
        /* end of V2 CLientHello Parsing */
        /* end of V2 CLientHello Parsing */

        case 2: /* Client Master Key  */
        {
            const char *cs_str =
                NULL;
            PRUint32 cs_int =
                0;
            ClientMasterKeyV2 *cmkv2;
            cmkv2 = (ClientMasterKeyV2 *)chv2;
            cmkv2 = (ClientMasterKeyV2 *)chv2;
            isV2Session = 1;


            PR_fprintf(PR_STDOUT, " [ssl2]  ClientMasterKeyV2 { \n");

            cs_int = GET_24(&cmkv2->cipherkind[0]);
            cs_str = V2CipherString(cs_int);
            PR_fprintf(PR_STDOUT, "         cipher-spec-chosen = (0x%06x) %s\n",
            PR_fprintf(PR_STDOUT, "         cipher-spec-chosen = (0x%06x) %s\n",
                       cs_int, cs_str);

            PR_fprintf(PR_STDOUT, "         clear-portion = %d bits\n",
                       8 *
                           (PRUint32)(GET_SHORT((cmkv2->clearkey))));
                           (PRUint32)(GET_SHORT((cmkv2->clearkey))));

            PR_fprintf(PR_STDOUT, "      }\n");
            clientstream.isEncrypted = 1;
            serverstream.isEncrypted = 1;
        } break;
        } break;

        case 3:
            PR_fprintf(PR_STDOUT, " [ssl2]  Client Finished V2 {...}\n");
            isV2Session = 1;
            break;
            break;

        case 4: /* V2 Server Hello */
            isV2Session = 1;


            PR_fprintf(PR_STDOUT, " [ssl2]  ServerHelloV2 {\n");
            PR_fprintf(PR_STDOUT, "           sid hit = {0x%02x}\n",
            PR_fprintf(PR_STDOUT, "           sid hit = {0x%02x}\n",
                       (PRUintn)shv2->sidhit);
            PR_fprintf(PR_STDOUT, "           version = {0x%02x, 0x%02x}\n",
                       (PRUint32)shv2->version[0], (PRUint32)shv2->version[1]);
            PR_fprintf(PR_STDOUT, "           cipher-specs-length = %d (0x%02x)\n",
                       (PRUint32)(GET_SHORT((shv2->cslength))),
                       (PRUint32)(GET_SHORT((shv2->cslength))),
                       (PRUint32)(GET_SHORT((shv2->cslength))));
            PR_fprintf(PR_STDOUT, "           sid-length = %d (0x%02x)\n",
                       (PRUint32)(GET_SHORT((shv2->cidlength))),
                       (PRUint32)(GET_SHORT((shv2->cidlength))));


            pos = (unsigned char *)shv2;
            pos += 2;  /* skip length header */
            pos += 11; /* position pointer to Certificate data area */
            q = GET_SHORT(&shv2->certlength);
            if (q > recordLen) {
            if (q > recordLen) {
                goto eosh;
            }
            pos += q; /* skip certificate */


            PR_fprintf(PR_STDOUT, "           cipher-suites = { ");
            len = GET_SHORT((shv2->cslength));
            for (p = 0; p < len; p += 3) {
                PRUint32 cs_int = GET_24((pos + p));
                PRUint32 cs_int = GET_24((pos + p));
                const char *cs_str =
                    V2CipherString(cs_int);
                PR_fprintf(PR_STDOUT, "\n              ");
                PR_fprintf(PR_STDOUT, "(0x%06x) %s", cs_int, cs_str);
            }
            pos += len;
            PR_fprintf(PR_STDOUT, "   }\n"); /* End of cipher suites */
            PR_fprintf(PR_STDOUT, "   }\n"); /* End of cipher suites */
            len = (PRUint32)GET_SHORT((shv2->cidlength));
            if (len) {
                PR_fprintf(PR_STDOUT, "           connection-id = { ");
                for (p =
                         0;
                         0;
                     p < len; p += 2) {
                    PR_fprintf(PR_STDOUT, "0x%04x ", (PRUint32)(GET_SHORT((pos +
                                                                           p))));
                }
                PR_fprintf(PR_STDOUT, "   }\n"); /* End of connection id */
                PR_fprintf(PR_STDOUT, "   }\n"); /* End of connection id */
            }
        eosh:
            PR_fprintf(PR_STDOUT, "\n              }\n"); /* end of ServerHelloV2 */
            if (shv2->sidhit) {
                clientstream.isEncrypted =
                    1;
                    1;
                serverstream.isEncrypted =
                    1;
            }
            break;


        case 5:
            PR_fprintf(PR_STDOUT, " [ssl2]  Server Verify V2 {...}\n");
            isV2Session = 1;
            break;
            break;

        case 6:
            PR_fprintf(PR_STDOUT, " [ssl2]  Server Finished V2 {...}\n");
            isV2Session = 1;
            break;
            break;

        case 7:
            PR_fprintf(PR_STDOUT, " [ssl2]  Request Certificate V2 {...}\n");
            isV2Session = 1;
            break;
            break;

        case 8:
            PR_fprintf(PR_STDOUT, " [ssl2]  Client Certificate V2 {...}\n");
            isV2Session = 1;
            break;
            break;

        default:
        default:
            PR_fprintf(PR_STDOUT, " [ssl2]  UnknownType 0x%02x {...}\n",
                       (PRUint32)chv2->type);
            break;
    }
}
}

unsigned int
print_hello_extension(unsigned char *hsdata,
                      unsigned int length,
                      unsigned int pos)
                      unsigned int pos)
{
    /* pretty print extensions, if any */
    if (pos < length) {
        int exListLen = GET_SHORT((hsdata + pos));
        pos += 2;
        pos += 2;
        PR_fprintf(PR_STDOUT,
                   "            extensions[%d] = {\n", exListLen);
        while (exListLen > 0 && pos < length) {
            int exLen;
            int exType = GET_SHORT((hsdata + pos));
            int exType = GET_SHORT((hsdata + pos));
            pos += 2;
            exLen = GET_SHORT((hsdata + pos));
            pos += 2;
            /* dump the extension */
            PR_fprintf(PR_STDOUT,
                       "              extension type %s, length [%d]",
                       helloExtensionNameString(exType), exLen);
            if (exLen > 0) {
            if (exLen > 0) {
                PR_fprintf(PR_STDOUT, " = {\n");
                print_hex(exLen, hsdata + pos);
                PR_fprintf(PR_STDOUT, "              }\n");
            } else {
                PR_fprintf(PR_STDOUT, "\n");
            }
            pos += exLen;
            pos += exLen;
            exListLen -= 2 + exLen;
        }
        PR_fprintf(PR_STDOUT, "            }\n");
    }
    return pos;
    return pos;
}

/*
 * Note this must match (exactly) the enumeration ocspResponseStatus.
 */
 */
static char *responseStatusNames[] = {
    "successful (Response has valid confirmations)",
    "malformedRequest (Illegal confirmation request)",
    "internalError (Internal error in issuer)",
    "tryLater (Try again later)",
    "unused ((4) is not used)",
    "sigRequired (Must sign the request)",
    "sigRequired (Must sign the request)",
    "unauthorized (Request unauthorized)",
};

static void
print_ocsp_cert_id(FILE *out_file, CERTOCSPCertID *cert_id, int level)
print_ocsp_cert_id(FILE *out_file, CERTOCSPCertID *cert_id, int level)
{
    SECU_Indent(out_file, level);
    fprintf(out_file, "Cert ID:\n");
    level++;
    /*
    SECU_PrintAlgorithmID (out_file, &(cert_id->hashAlgorithm),
                           "Hash Algorithm", level);
                           "Hash Algorithm", level);
    SECU_PrintAsHex (out_file, &(cert_id->issuerNameHash),
                     "Issuer Name Hash", level);
    SECU_PrintAsHex (out_file, &(cert_id->issuerKeyHash),
                     "Issuer Key Hash", level);
*/
*/
    SECU_PrintInteger(out_file, &(cert_id->serialNumber),
                      "Serial Number", level);
    /* XXX lookup the cert; if found, print something nice (nickname?) */
}


static void
print_ocsp_version(FILE *out_file, SECItem *version, int level)
{
    if (version->len > 0) {
        SECU_PrintInteger(out_file, version, "Version", level);
        SECU_PrintInteger(out_file, version, "Version", level);
    } else {
        SECU_Indent(out_file, level);
        fprintf(out_file, "Version: DEFAULT\n");
    }
}

static void
static void
print_responder_id(FILE *out_file, ocspResponderID *responderID, int level)
{
    SECU_Indent(out_file, level);
    fprintf(out_file, "Responder ID ");


    switch (responderID->responderIDType) {
        case ocspResponderID_byName:
            fprintf(out_file, "(byName):\n");
            SECU_PrintName(out_file, &(responderID->responderIDValue.name),
                           "Name", level + 1);
                           "Name", level + 1);
            break;
            break;
        case ocspResponderID_byKey:
            fprintf(out_file, "(byKey):\n");
            SECU_PrintAsHex(out_file, &(responderID->responderIDValue.keyHash),
                            "Key Hash", level + 1);
            break;
            break;
        default:
            fprintf(out_file, "Unrecognized Responder ID Type\n");
            break;
    }
}

static void
static void
print_ocsp_extensions(FILE *out_file, CERTCertExtension **extensions,
                      char *msg, int level)
{
    if (extensions) {
        SECU_PrintExtensions(out_file, extensions, msg, level);
        SECU_PrintExtensions(out_file, extensions, msg, level);
    } else {
        SECU_Indent(out_file, level);
        fprintf(out_file, "No %s\n", msg);
    }
}
}

static void
print_revoked_info(FILE *out_file, ocspRevokedInfo *revoked_info, int level)
print_revoked_info(FILE *out_file, ocspRevokedInfo *revoked_info, int level)
{
    SECU_PrintGeneralizedTime(out_file, &(revoked_info->revocationTime),
                              "Revocation Time", level);

    if (revoked_info->revocationReason != NULL) {
    if (revoked_info->revocationReason != NULL) {
        SECU_PrintAsHex(out_file, revoked_info->revocationReason,
                        "Revocation Reason", level);
    } else {
        SECU_Indent(out_file, level);
        fprintf(out_file, "No Revocation Reason.\n");
    }
}
}

static void
print_cert_status(FILE *out_file, ocspCertStatus *status, int level)
{
    SECU_Indent(out_file, level);
    SECU_Indent(out_file, level);
    fprintf(out_file, "Status: ");

    switch (status->certStatusType) {
        case ocspCertStatus_good:
            fprintf(out_file, "Cert is good.\n");
            fprintf(out_file, "Cert is good.\n");
            break;
        case ocspCertStatus_revoked:
            fprintf(out_file, "Cert has been revoked.\n");
            print_revoked_info(out_file, status->certStatusInfo.revokedInfo,
                               level + 1);
            break;
            break;
        case ocspCertStatus_unknown:
            fprintf(out_file, "Cert is unknown to responder.\n");
            break;
        default:
            fprintf(out_file, "Unrecognized status.\n");
            fprintf(out_file, "Unrecognized status.\n");
            break;
    }
}

static void
static void
print_single_response(FILE *out_file, CERTOCSPSingleResponse *single,
                      int level)
{
    print_ocsp_cert_id(out_file, single->certID, level);
    print_ocsp_cert_id(out_file, single->certID, level);

    print_cert_status(out_file, single->certStatus, level);

    SECU_PrintGeneralizedTime(out_file, &(single->thisUpdate),
                              "This Update", level);
                              "This Update", level);

    if (single->nextUpdate != NULL) {
        SECU_PrintGeneralizedTime(out_file, single->nextUpdate,
                                  "Next Update", level);
    } else {
        SECU_Indent(out_file, level);
        SECU_Indent(out_file, level);
        fprintf(out_file, "No Next Update\n");
    }

    print_ocsp_extensions(out_file, single->singleExtensions,
                          "Single Response Extensions", level);
                          "Single Response Extensions", level);
}

static void
print_response_data(FILE *out_file, ocspResponseData *responseData, int level)
{
{
    SECU_Indent(out_file, level);
    fprintf(out_file, "Response Data:\n");
    level++;

    print_ocsp_version(out_file, &(responseData->version), level);
    print_ocsp_version(out_file, &(responseData->version), level);

    print_responder_id(out_file, responseData->responderID, level);

    SECU_PrintGeneralizedTime(out_file, &(responseData->producedAt),
                              "Produced At", level);
                              "Produced At", level);

    if (responseData->responses != NULL) {
        int i;

        for (i = 0; responseData->responses[i] != NULL; i++) {
        for (i = 0; responseData->responses[i] != NULL; i++) {
            SECU_Indent(out_file, level);
            fprintf(out_file, "Response %d:\n", i);
            print_single_response(out_file, responseData->responses[i],
                                  level + 1);
        }
        }
    } else {
        fprintf(out_file, "Response list is empty.\n");
    }

    print_ocsp_extensions(out_file, responseData->responseExtensions,
    print_ocsp_extensions(out_file, responseData->responseExtensions,
                          "Response Extensions", level);
}

static void
print_basic_response(FILE *out_file, ocspBasicOCSPResponse *basic, int level)
print_basic_response(FILE *out_file, ocspBasicOCSPResponse *basic, int level)
{
    SECU_Indent(out_file, level);
    fprintf(out_file, "Basic OCSP Response:\n");
    level++;

    print_response_data(out_file, basic->tbsResponseData, level);
}
}

static void
print_status_response(SECItem *data)
{
{
    int level = 2;
    CERTOCSPResponse *response;
    response = CERT_DecodeOCSPResponse(data);
    if (!response) {
        SECU_Indent(stdout, level);
        SECU_Indent(stdout, level);
        fprintf(stdout, "unable to decode certificate_status\n");
        return;
    }

    SECU_Indent(stdout, level);
    SECU_Indent(stdout, level);
    if (response->statusValue >= ocspResponse_min &&
        response->statusValue <= ocspResponse_max) {
        fprintf(stdout, "Response Status: %s\n",
                responseStatusNames[response->statusValue]);
    } else {
    } else {
        fprintf(stdout,
                "Response Status: other (Status value %d out of defined range)\n",
                (int)response->statusValue);
    }


    if (response->statusValue == ocspResponse_successful) {
        ocspResponseBytes *responseBytes = response->responseBytes;
        PORT_Assert(responseBytes != NULL);

        level++;
        SECU_PrintObjectID(stdout, &(responseBytes->responseType),
                           "Response Type", level);
                           "Response Type", level);
        switch (response->responseBytes->responseTypeTag) {
            case SEC_OID_PKIX_OCSP_BASIC_RESPONSE:
                print_basic_response(stdout,
                                     responseBytes->decodedResponse.basic,
                                     level);
                                     level);
                break;
            default:
                SECU_Indent(stdout, level);
                fprintf(stdout, "Unknown response syntax\n");
                break;
                break;
        }
    } else {
        SECU_Indent(stdout, level);
        fprintf(stdout, "Unsuccessful response, no more information.\n");
    }
    }

    CERT_DestroyOCSPResponse(response);
}

/* In the case of renegotiation, handshakes that occur in an already MAC'ed
/* In the case of renegotiation, handshakes that occur in an already MAC'ed
 * channel, by the time of this call, the caller has already removed the MAC
 * from input recordLen. The only MAC'ed record that will get here with its
 * MAC intact (not removed) is the first Finished message on the connection.
 */
void
print_ssl3_handshake(unsigned char *recordBuf,
                     unsigned int recordLen,
                     unsigned int recordLen,
                     SSLRecord *sr,
                     DataBufferList *s)
{
    struct sslhandshake sslh;
    unsigned char *hsdata;
    unsigned char *hsdata;
    unsigned int offset = 0;

    PR_fprintf(PR_STDOUT, "   handshake {\n");

    if (s->msgBufOffset && s->msgBuf) {
        /* append recordBuf to msgBuf, then use msgBuf */
        if (s->msgBufOffset + recordLen > s->msgBufSize) {
            int newSize = s->msgBufOffset + recordLen;
            unsigned char *newBuf = PORT_Realloc(s->msgBuf, newSize);
            unsigned char *newBuf = PORT_Realloc(s->msgBuf, newSize);
            if (!newBuf) {
                PR_ASSERT(newBuf);
                showErr("Realloc failed");
                exit(10);
            }
            }
            s->msgBuf = newBuf;
            s->msgBufSize = newSize;
        }
        memcpy(s->msgBuf + s->msgBufOffset, recordBuf, recordLen);
        s->msgBufOffset += recordLen;
        recordLen = s->msgBufOffset;
        recordLen = s->msgBufOffset;
        recordBuf = s->msgBuf;
    }
    while (offset + 4 <= recordLen) {
        sslh.type = recordBuf[offset];
        sslh.length = GET_24(recordBuf + offset + 1);
        sslh.length = GET_24(recordBuf + offset + 1);
        if (offset + 4 + sslh.length > recordLen)
            break;
        /* finally have a complete message */
        if (sslhexparse)
            print_hex(4, recordBuf + offset);
            print_hex(4, recordBuf + offset);

        hsdata = &recordBuf[offset + 4];

        PR_fprintf(PR_STDOUT, "      type = %d (", sslh.type);
        switch (sslh.type) {
        switch (sslh.type) {
            case 0:
                PR_FPUTS("hello_request)\n");
                break;
            case 1:
                PR_FPUTS("client_hello)\n");
                PR_FPUTS("client_hello)\n");
                break;
            case 2:
                PR_FPUTS("server_hello)\n");
                PR_FPUTS("server_hello)\n");
                break;
            case 4:
                PR_FPUTS("new_session_ticket)\n");
                break;
            case 11:
            case 11:
                PR_FPUTS("certificate)\n");
                break;
            case 12:
                PR_FPUTS("server_key_exchange)\n");
                PR_FPUTS("server_key_exchange)\n");
                break;
            case 13:
                PR_FPUTS("certificate_request)\n");
                break;
            case 14:
            case 14:
                PR_FPUTS("server_hello_done)\n");
                break;
            case 15:
                PR_FPUTS("certificate_verify)\n");
                break;
                break;
            case 16:
                PR_FPUTS("client_key_exchange)\n");
                break;
                break;
            case 20:
                PR_FPUTS("finished)\n");
                break;
            case 22:
                PR_FPUTS("certificate_status)\n");
                PR_FPUTS("certificate_status)\n");
                break;
            default:
                PR_FPUTS("unknown)\n");
                break;
        }
        }

        PR_fprintf(PR_STDOUT, "      length = %d (0x%06x)\n", sslh.length, sslh.length);
        switch (sslh.type) {

            case 0: /* hello_request */ /* not much to show here. */
            case 0: /* hello_request */ /* not much to show here. */
                break;

            case 1: /* client hello */
                switch (sr->ver_maj) {
                    case 3: /* ssl version 3 */
                    case 3: /* ssl version 3 */
                    {
                        unsigned int pos;
                        int w;

                        PR_fprintf(PR_STDOUT, "         ClientHelloV3 {\n");
                        PR_fprintf(PR_STDOUT, "         ClientHelloV3 {\n");
                        PR_fprintf(PR_STDOUT, "            client_version = {%d, %d}\n",
                                   (PRUint8)hsdata[0], (PRUint8)hsdata[1]);
                        PR_fprintf(PR_STDOUT, "            random = {...}\n");
                        if (sslhexparse)
                            print_hex(32, &hsdata[2]);
                            print_hex(32, &hsdata[2]);

                        /* pretty print Session ID */
                        {
                            int sidlength =
                            int sidlength =
                                (int)hsdata[2 + 32];
                            PR_fprintf(PR_STDOUT, "            session ID = {\n");
                            PR_fprintf(PR_STDOUT, "                length = %d\n", sidlength);
                            PR_fprintf(PR_STDOUT, "                contents = {...}\n");
                            if (sslhexparse)
                            if (sslhexparse)
                                print_hex(sidlength, &hsdata[2 + 32 + 1]);
                            PR_fprintf(PR_STDOUT, "            }\n");
                            pos =
                                2 +
                                32 +
                                32 +
                                1 +
                                sidlength;
                        }

                        /* pretty print cipher suites */
                        /* pretty print cipher suites */
                        {
                            int csuitelength =
                                GET_SHORT((hsdata + pos));
                            PR_fprintf(PR_STDOUT, "            cipher_suites[%d] = {\n",
                                       csuitelength /
                                       csuitelength /
                                           2);
                            if (csuitelength %
                                2) {
                                PR_fprintf(PR_STDOUT,
                                           "*error in protocol - csuitelength shouldn't be odd*\n");
                                           "*error in protocol - csuitelength shouldn't be odd*\n");
                            }
                            for (w =
                                     0;
                                 w <
                                 csuitelength;
                                 csuitelength;
                                 w += 2) {
                                PRUint32 cs_int =
                                    GET_SHORT((hsdata + pos + 2 + w));
                                const char *cs_str =
                                    V2CipherString(cs_int);
                                    V2CipherString(cs_int);
                                PR_fprintf(PR_STDOUT,
                                           "                (0x%04x) %s\n", cs_int, cs_str);
                                           "                (0x%04x) %s\n", cs_int, cs_str);
                            }
                            pos +=
                                2 +
                                csuitelength;
                            PR_fprintf(PR_STDOUT, "            }\n");
                            PR_fprintf(PR_STDOUT, "            }\n");
                        }

                        /* pretty print compression methods */
                        {
                            int complength =
                            int complength =
                                hsdata[pos];
                            PR_fprintf(PR_STDOUT, "            compression[%d] = {\n",
                                       complength);
                            for (w =
                                     0;
                                     0;
                                 w <
                                 complength;
                                 w++) {
                                PRUint32 cm_int =
                                    hsdata[pos + 1 + w];
                                const char *cm_str =
                                    CompressionMethodString(cm_int);
                                    CompressionMethodString(cm_int);
                                PR_fprintf(PR_STDOUT,
                                           "                (%02x) %s\n", cm_int, cm_str);
                            }
                            pos +=
                            pos +=
                                1 +
                                complength;
                            PR_fprintf(PR_STDOUT, "            }\n");
                        }


                        /* pretty print extensions, if any */
                        pos =
                            print_hello_extension(hsdata, sslh.length, pos);

                        PR_fprintf(PR_STDOUT, "         }\n");
                        PR_fprintf(PR_STDOUT, "         }\n");
                    } /* end of ssl version 3 */
                    break;
                    default:
                        PR_fprintf(PR_STDOUT, "         UNDEFINED VERSION %d.%d {...}\n",
                                   sr->ver_maj, sr->ver_min);
                                   sr->ver_maj, sr->ver_min);
                        if (sslhexparse)
                            print_hex(sslh.length, hsdata);
                        break;
                } /* end of switch sr->ver_maj */
                break;
                break;

            case 2: /* server hello */
            {
                unsigned int sidlength, pos;


                PR_fprintf(PR_STDOUT, "         ServerHello {\n");

                PR_fprintf(PR_STDOUT, "            server_version = {%d, %d}\n",
                           (PRUint8)hsdata[0], (PRUint8)hsdata[1]);
                           (PRUint8)hsdata[0], (PRUint8)hsdata[1]);
                PR_fprintf(PR_STDOUT, "            random = {...}\n");
                if (sslhexparse)
                    print_hex(32, &hsdata[2]);
                PR_fprintf(PR_STDOUT, "            session ID = {\n");
                sidlength = (int)hsdata[2 +
                sidlength = (int)hsdata[2 +
                                        32];
                PR_fprintf(PR_STDOUT, "                length = %d\n", sidlength);
                PR_fprintf(PR_STDOUT, "                contents = {...}\n");
                if (sslhexparse)
                    print_hex(sidlength, &hsdata[2 + 32 + 1]);
                    print_hex(sidlength, &hsdata[2 + 32 + 1]);
                PR_fprintf(PR_STDOUT, "            }\n");
                pos = 2 +
                      32 + 1 +
                      sidlength;


                /* pretty print chosen cipher suite */
                {
                    PRUint32 cs_int = GET_SHORT((hsdata + pos));
                    const char *cs_str =
                        V2CipherString(cs_int);
                    PR_fprintf(PR_STDOUT, "            cipher_suite = (0x%04x) %s\n",
                    PR_fprintf(PR_STDOUT, "            cipher_suite = (0x%04x) %s\n",
                               cs_int, cs_str);
                    currentcipher =
                        cs_int;
                    pos +=
                        2;
                        2;
                }
                /* pretty print chosen compression method */
                {
                    PRUint32 cm_int = hsdata[pos++];
                    const char *cm_str =
                    const char *cm_str =
                        CompressionMethodString(cm_int);
                    PR_fprintf(PR_STDOUT, "            compression method = (%02x) %s\n",
                               cm_int, cm_str);
                }
                }

                /* pretty print extensions, if any */
                pos = print_hello_extension(hsdata, sslh.length, pos);

                PR_fprintf(PR_STDOUT, "         }\n");
                PR_fprintf(PR_STDOUT, "         }\n");
            } break;

            case 4: /* new session ticket */
            {
                PRUint32 lifetimehint;
                PRUint16 ticketlength;
                char lifetime[32];
                char lifetime[32];
                lifetimehint = GET_32(hsdata);
                if (lifetimehint) {
                    PRExplodedTime et;
                    PRTime t =
                        lifetimehint;
                        lifetimehint;
                    t *=
                        PR_USEC_PER_SEC;
                    PR_ExplodeTime(t, PR_GMTParameters, &et);
                    /* use HTTP Cookie header's date format */
                    PR_FormatTimeUSEnglish(lifetime, sizeof lifetime,
                    PR_FormatTimeUSEnglish(lifetime, sizeof lifetime,
                                           "%a, %d-%b-%Y %H:%M:%S GMT", &et);
                } else {
                    /* 0 means the lifetime of the ticket is unspecified */
                    strcpy(lifetime, "unspecified");
                }
                }
                ticketlength = GET_SHORT((hsdata +
                                          4));
                PR_fprintf(PR_STDOUT, "         NewSessionTicket {\n");
                PR_fprintf(PR_STDOUT, "            ticket_lifetime_hint = %s\n",
                           lifetime);
                           lifetime);
                PR_fprintf(PR_STDOUT, "            ticket = {\n");
                PR_fprintf(PR_STDOUT, "                length = %d\n", ticketlength);
                PR_fprintf(PR_STDOUT, "                contents = {...}\n");
                if (sslhexparse)
                    print_hex(ticketlength, &hsdata[4 + 2]);
                    print_hex(ticketlength, &hsdata[4 + 2]);
                PR_fprintf(PR_STDOUT, "            }\n");
                PR_fprintf(PR_STDOUT, "         }\n");
            } break;

            case 11: /* certificate */
            case 11: /* certificate */
            {
                PRFileDesc *cfd;
                int pos;
                int certslength;
                int certlength;
                int certbytesread = 0;
                static int certFileNumber;
                static int certFileNumber;
                char certFileName[20];

                PR_fprintf(PR_STDOUT, "         CertificateChain {\n");
                certslength = GET_24(hsdata);
                PR_fprintf(PR_STDOUT, "            chainlength = %d (0x%04x)\n",
                PR_fprintf(PR_STDOUT, "            chainlength = %d (0x%04x)\n",
                           certslength, certslength);
                           certslength, certslength);
                pos = 3;
                while (certbytesread < certslength) {
                    certlength =
                        GET_24((hsdata + pos));
                    pos +=
                    pos +=
                        3;
                    PR_fprintf(PR_STDOUT, "            Certificate {\n");
                    PR_fprintf(PR_STDOUT, "               size = %d (0x%04x)\n",
                               certlength, certlength);
                    certbytesread +=
                    certbytesread +=
                        certlength + 3;
                    if (certbytesread <=
                        certslength) {
                        PR_snprintf(certFileName, sizeof certFileName, "cert.%03d",
                                    ++certFileNumber);
                                    ++certFileNumber);
                        cfd =
                            PR_Open(certFileName, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
                                    0664);
                        if (!cfd) {
                            PR_fprintf(PR_STDOUT,
                                       "               data = { couldn't save file '%s' }\n",
                                       certFileName);
                        } else {
                            PR_Write(cfd, (hsdata +
                            PR_Write(cfd, (hsdata +
                                           pos),
                                     certlength);
                            PR_fprintf(PR_STDOUT,
                                       "               data = { saved in file '%s' }\n",
                                       certFileName);
                            PR_Close(cfd);
                            PR_Close(cfd);
                        }
                    }

                    PR_fprintf(PR_STDOUT, "            }\n");
                    pos += certlength;
                    pos += certlength;
                }
                PR_fprintf(PR_STDOUT, "         }\n");
            } break;

            case 12: /* server_key_exchange */
            case 12: /* server_key_exchange */
                if (sslhexparse)
                    print_hex(sslh.length, hsdata);
                break;

            case 13: /* certificate request */
            case 13: /* certificate request */
            {
                unsigned int pos = 0;
                int w, reqLength;

                PR_fprintf(PR_STDOUT, "         CertificateRequest {\n");

                /* pretty print requested certificate types */
                reqLength = hsdata[pos];
                reqLength = hsdata[pos];
                PR_fprintf(PR_STDOUT, "            certificate types[%d] = {",
                           reqLength);
                for (w =
                         0;
                     w < reqLength; w++) {
                    PR_fprintf(PR_STDOUT, " %02x", hsdata[pos +
                                                          1 + w]);
                                                          1 + w]);
                }
                pos += 1 + reqLength;
                PR_fprintf(PR_STDOUT, " }\n");

                /* pretty print CA names, if any */
                /* pretty print CA names, if any */
                if (pos < sslh.length) {
                    int exListLen =
                        GET_SHORT((hsdata + pos));
                    pos += 2;
                    PR_fprintf(PR_STDOUT,
                    PR_fprintf(PR_STDOUT,
                               "            certificate_authorities[%d] = {\n",
                               exListLen);
                    while (exListLen >
                               0 &&
                           pos < sslh.length) {
                           pos < sslh.length) {
                        char *ca_name;
                        SECItem it;
                        int dnLen = GET_SHORT((hsdata +
                                               pos));
                        pos += 2;

                        /* dump the CA name */
                        /* dump the CA name */
                        it.type =
                            siBuffer;
                        it.data =
                            hsdata + pos;
                        it.len =
                        it.len =
                            dnLen;
                        ca_name =
                            CERT_DerNameToAscii(&it);
                        if (ca_name) {
                            PR_fprintf(PR_STDOUT, "   %s\n", ca_name);
                            PR_fprintf(PR_STDOUT, "   %s\n", ca_name);
                            PORT_Free(ca_name);
                        } else {
                            PR_fprintf(PR_STDOUT,
                                       "              distinguished name [%d]", dnLen);
                            if (dnLen >
                            if (dnLen >
                                    0 &&
                                sslhexparse) {
                                PR_fprintf(PR_STDOUT, " = {\n");
                                print_hex(dnLen, hsdata + pos);
                                PR_fprintf(PR_STDOUT, "              }\n");
                            } else {
                                PR_fprintf(PR_STDOUT, "\n");
                                PR_fprintf(PR_STDOUT, "\n");
                            }
                        }
                        pos +=
                            dnLen;
                        exListLen -=
                        exListLen -=
                            2 + dnLen;
                    }
                    PR_fprintf(PR_STDOUT, "            }\n");
                }


                PR_fprintf(PR_STDOUT, "         }\n");
            } break;

            case 14: /* server_hello_done */ /* not much to show here. */
            case 14: /* server_hello_done */ /* not much to show here. */
                break;

            case 15: /* certificate_verify */
                if (sslhexparse)
                    print_hex(sslh.length, hsdata);
                break;


            case 16: /* client key exchange */
            {
                PR_fprintf(PR_STDOUT, "         ClientKeyExchange {\n");
                PR_fprintf(PR_STDOUT, "            message = {...}\n");
                PR_fprintf(PR_STDOUT, "            message = {...}\n");
                PR_fprintf(PR_STDOUT, "         }\n");
            } break;

            case 20: /* finished */
                PR_fprintf(PR_STDOUT, "         Finished {\n");
                PR_fprintf(PR_STDOUT, "         Finished {\n");
                PR_fprintf(PR_STDOUT, "            verify_data = {...}\n");
                if (sslhexparse)
                    print_hex(sslh.length, hsdata);
                PR_fprintf(PR_STDOUT, "         }\n");


                if (!isNULLmac(currentcipher) &&
                    !s->hMACsize) {
                    /* To calculate the size of MAC, we subtract the number of known
                     * bytes of message from the number of remaining bytes in the
                     * record. This assumes that this is the first record on the
                     * record. This assumes that this is the first record on the
                     * connection to have a MAC, and that the sender has not put another
                     * message after the finished message in the handshake record.
                     * This is only correct for the first transition from unMACed to
                     * MACed. If the connection switches from one cipher suite to
                     * another one with a different MAC, this logic will not track that
                     * change correctly.
                     */
                    s->hMACsize =
                    s->hMACsize =
                        recordLen - (sslh.length + 4);
                    sslh.length +=
                        s->hMACsize; /* skip over the MAC data */
                }
                break;
                break;

            case 22: /* certificate_status */
            {
                SECItem data;
                PRFileDesc *ofd;
                static int ocspFileNumber;
                char ocspFileName[20];

                /* skip 4 bytes with handshake numbers, as in ssl3_HandleCertificateStatus */
                data.type = siBuffer;
                data.type = siBuffer;
                data.data = hsdata + 4;
                data.len = sslh.length - 4;
                print_status_response(&data);

                PR_snprintf(ocspFileName, sizeof ocspFileName, "ocsp.%03d",
                PR_snprintf(ocspFileName, sizeof ocspFileName, "ocsp.%03d",
                            ++ocspFileNumber);
                ofd = PR_Open(ocspFileName, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
                              0664);
                              0664);
                if (!ofd) {
                    PR_fprintf(PR_STDOUT,
                               "               data = { couldn't save file '%s' }\n",
                               ocspFileName);
                } else {
                    PR_Write(ofd, data.data, data.len);
                    PR_fprintf(PR_STDOUT,
                               "               data = { saved in file '%s' }\n",
                               ocspFileName);
                    PR_Close(ofd);
                }
                }
            } break;

            default: {
                PR_fprintf(PR_STDOUT, "         UNKNOWN MESSAGE TYPE %d [%d] {\n",
                           sslh.type, sslh.length);
                           sslh.type, sslh.length);
                if (sslhexparse)
                    print_hex(sslh.length, hsdata);
                PR_fprintf(PR_STDOUT, "         }\n");
            }
        } /* end of switch sslh.type */
        } /* end of switch sslh.type */
        offset += sslh.length + 4;
    }                         /* while */
    if (offset < recordLen) { /* stuff left over */
    if (offset < recordLen) { /* stuff left over */
        unsigned int newMsgLen = recordLen - offset;
        if (!s->msgBuf) {
            s->msgBuf = PORT_Alloc(newMsgLen);
            if (!s->msgBuf) {
                PR_ASSERT(s->msgBuf);
                PR_ASSERT(s->msgBuf);
                showErr("Malloc failed");
                exit(11);
            }
            s->msgBufSize = newMsgLen;
            memcpy(s->msgBuf, recordBuf + offset, newMsgLen);
            memcpy(s->msgBuf, recordBuf + offset, newMsgLen);
        } else if (newMsgLen > s->msgBufSize) {
            unsigned char *newBuf = PORT_Realloc(s->msgBuf, newMsgLen);
            if (!newBuf) {
                PR_ASSERT(newBuf);
                showErr("Realloc failed");
                showErr("Realloc failed");
                exit(12);
            }
            s->msgBuf = newBuf;
            s->msgBufSize = newMsgLen;
        } else if (offset || s->msgBuf != recordBuf) {
        } else if (offset || s->msgBuf != recordBuf) {
            memmove(s->msgBuf, recordBuf + offset, newMsgLen);
        }
        s->msgBufOffset = newMsgLen;
        PR_fprintf(PR_STDOUT, "     [incomplete handshake message]\n");
    } else {
    } else {
        s->msgBufOffset = 0;
    }
    PR_fprintf(PR_STDOUT, "   }\n");
}


void
print_ssl(DataBufferList *s, int length, unsigned char *buffer)
{
    /* --------------------------------------------------------  */
    /* first, create a new buffer object for this piece of data. */

    DataBuffer *db;
    DataBuffer *db;

    if (s->size == 0 && length > 0 && buffer[0] >= 32 && buffer[0] < 128) {
        /* Not an SSL record, treat entire buffer as plaintext */
        PR_Write(PR_STDOUT, buffer, length);
        return;
        return;
    }

    check_integrity(s);

    db = PR_NEW(struct _DataBuffer);
    db = PR_NEW(struct _DataBuffer);

    if (!db) {
        return;
    }


    db->buffer = (unsigned char *)PORT_Alloc(length);
    db->length = length;
    db->offset = 0;
    memcpy(db->buffer, buffer, length);
    db->next = NULL;
    db->next = NULL;

    /* now, add it to the stream */

    if (s->last != NULL)
        s->last->next = db;
        s->last->next = db;
    s->last = db;
    s->size += length;
    if (s->first == NULL)
        s->first = db;


    check_integrity(s);

    /*------------------------------------------------------- */
    /* now we look at the stream to see if we have enough data to
     decode  */

    while (s->size > 0) {
    while (s->size > 0) {
        unsigned char *recordBuf = NULL;

        SSLRecord sr;
        unsigned recordLen;
        unsigned recordsize;
        unsigned recordsize;

        check_integrity(s);
        check_integrity(s);

        if (s->first == NULL) {
            PR_fprintf(PR_STDOUT, "ERROR: s->first is null\n");
            exit(9);
        }
        }

        /* in the case of an SSL 2 client-hello  */
        /* will have the high-bit set, whereas an SSL 3 client-hello will not  */
        /* SSL2 can also send records that begin with the high bit clear.
         * This code will incorrectly handle them. XXX
         * This code will incorrectly handle them. XXX
         */
        if (isV2Session || s->first->buffer[s->first->offset] & 0x80) {
            /* it's an SSL 2 packet */
            unsigned char lenbuf[3];


            /* first, we check if there's enough data for it to be an SSL2-type
             * record.  What a pain.*/
            if (s->size < sizeof lenbuf) {
                partial_packet(length, s->size, sizeof lenbuf);
                return;
                return;
            }

            /* read the first two bytes off the stream. */
            read_stream_bytes(lenbuf, s, sizeof(lenbuf));
            recordLen = ((unsigned int)(lenbuf[0] & 0x7f) << 8) + lenbuf[1] +
                        ((lenbuf[0] & 0x80) ? 2 : 3);
            PR_fprintf(PR_STDOUT, "recordLen = %u bytes\n", recordLen);


            /* put 'em back on the head of the stream. */
            db = PR_NEW(struct _DataBuffer);

            db->length = sizeof lenbuf;
            db->buffer = (unsigned char *)PORT_Alloc(db->length);
            db->buffer = (unsigned char *)PORT_Alloc(db->length);
            db->offset = 0;
            memcpy(db->buffer, lenbuf, sizeof lenbuf);

            db->next = s->first;
            s->first = db;
            s->first = db;
            if (s->last == NULL)
                s->last = db;
            s->size += db->length;

            /* if there wasn't enough, go back for more. */
            if (s->size < recordLen) {
                check_integrity(s);
                check_integrity(s);
                partial_packet(length, s->size, recordLen);
                return;
            }
            partial_packet(length, s->size, recordLen);


            /* read in the whole record. */
            recordBuf = PORT_Alloc(recordLen);
            recordBuf = PORT_Alloc(recordLen);
            read_stream_bytes(recordBuf, s, recordLen);

            print_sslv2(s, recordBuf, recordLen);
            PR_FREEIF(recordBuf);
            check_integrity(s);
            check_integrity(s);

            continue;
        }

        /***********************************************************/
        /***********************************************************/
        /* It's SSL v3 */
        /***********************************************************/
        check_integrity(s);

        if (s->size < sizeof sr) {
        if (s->size < sizeof sr) {
            partial_packet(length, s->size, sizeof(SSLRecord));
            return;
        }

        read_stream_bytes((unsigned char *)&sr, s, sizeof sr);
        read_stream_bytes((unsigned char *)&sr, s, sizeof sr);

        /* we have read the stream bytes. Look at the length of
       the ssl record. If we don't have enough data to satisfy this
       request, then put the bytes we just took back at the head
       of the queue */
        recordsize = GET_SHORT(sr.length);


        if (recordsize > s->size) {
            db = PR_NEW(struct _DataBuffer);

            db->length = sizeof sr;
            db->buffer = (unsigned char *)PORT_Alloc(db->length);
            db->offset = 0;
            memcpy(db->buffer, &sr, sizeof sr);
            memcpy(db->buffer, &sr, sizeof sr);
            db->next = s->first;

            /* now, add it back on to the head of the stream */

            s->first = db;
            s->first = db;
            if (s->last == NULL)
                s->last = db;
            s->size += db->length;

            check_integrity(s);
            check_integrity(s);
            partial_packet(length, s->size, recordsize);
            return;
        }
        partial_packet(length, s->size, recordsize);


        PR_fprintf(PR_STDOUT, "SSLRecord { [%s]\n", get_time_string());
        if (sslhexparse) {
            print_hex(5, (unsigned char *)&sr);
        }

        check_integrity(s);


        PR_fprintf(PR_STDOUT, "   type    = %d (", sr.type);
        switch (sr.type) {
            case 20:
                PR_fprintf(PR_STDOUT, "change_cipher_spec)\n");
                break;
                break;
            case 21:
                PR_fprintf(PR_STDOUT, "alert)\n");
                break;
            case 22:
                PR_fprintf(PR_STDOUT, "handshake)\n");
                PR_fprintf(PR_STDOUT, "handshake)\n");
                break;
            case 23:
                PR_fprintf(PR_STDOUT, "application_data)\n");
                break;
            default:
                PR_fprintf(PR_STDOUT, "unknown)\n");
                break;
                break;
        }
        PR_fprintf(PR_STDOUT, "   version = { %d,%d }\n",
                   (PRUint32)sr.ver_maj, (PRUint32)sr.ver_min);
        PR_fprintf(PR_STDOUT, "   length  = %d (0x%x)\n",
                   (PRUint32)GET_SHORT(sr.length), (PRUint32)GET_SHORT(sr.length));
                   (PRUint32)GET_SHORT(sr.length), (PRUint32)GET_SHORT(sr.length));

        recordLen = recordsize;
        PR_ASSERT(s->size >= recordLen);
        if (s->size >= recordLen) {
            recordBuf = (unsigned char *)PORT_Alloc(recordLen);
            recordBuf = (unsigned char *)PORT_Alloc(recordLen);
            read_stream_bytes(recordBuf, s, recordLen);

            if (s->isEncrypted) {
                PR_fprintf(PR_STDOUT, "            < encrypted >\n");
            } else { /* not encrypted */
            } else { /* not encrypted */

                switch (sr.type) {
                    case 20: /* change_cipher_spec */
                        if (sslhexparse)
                            print_hex(recordLen - s->hMACsize, recordBuf);
                            print_hex(recordLen - s->hMACsize, recordBuf);
                        /* mark to say we can only dump hex form now on
                         * if it is not one on a null cipher */
                        s->isEncrypted =
                            isNULLcipher(currentcipher) ? 0 : 1;
                        break;
                        break;

                    case 21: /* alert */
                        switch (recordBuf[0]) {
                            case 1:
                                PR_fprintf(PR_STDOUT, "   warning: ");
                                PR_fprintf(PR_STDOUT, "   warning: ");
                                break;
                            case 2:
                                PR_fprintf(PR_STDOUT, "   fatal: ");
                                break;
                            default:
                            default:
                                PR_fprintf(PR_STDOUT, "   unknown level %d: ", recordBuf[0]);
                                break;
                        }

                        switch (recordBuf[1]) {
                        switch (recordBuf[1]) {
                            case 0:
                                PR_FPUTS("close_notify\n");
                                break;
                            case 10:
                            case 10:
                                PR_FPUTS("unexpected_message\n");
                                break;
                            case 20:
                                PR_FPUTS("bad_record_mac\n");
                                break;
                                break;
                            case 21:
                                PR_FPUTS("decryption_failed\n");
                                PR_FPUTS("decryption_failed\n");
                                break;
                            case 22:
                                PR_FPUTS("record_overflow\n");
                                break;
                            case 30:
                                PR_FPUTS("decompression_failure\n");
                                break;
                                break;
                            case 40:
                                PR_FPUTS("handshake_failure\n");
                                break;
                            case 41:
                                PR_FPUTS("no_certificate\n");
                                PR_FPUTS("no_certificate\n");
                                break;
                            case 42:
                                PR_FPUTS("bad_certificate\n");
                                break;
                            case 43:
                            case 43:
                                PR_FPUTS("unsupported_certificate\n");
                                break;
                            case 44:
                                PR_FPUTS("certificate_revoked\n");
                                break;
                                break;
                            case 45:
                                PR_FPUTS("certificate_expired\n");
                                break;
                            case 46:
                                PR_FPUTS("certificate_unknown\n");
                                PR_FPUTS("certificate_unknown\n");
                                break;
                            case 47:
                                PR_FPUTS("illegal_parameter\n");
                                break;
                            case 48:
                            case 48:
                                PR_FPUTS("unknown_ca\n");
                                break;
                            case 49:
                                PR_FPUTS("access_denied\n");
                                break;
                                break;
                            case 50:
                                PR_FPUTS("decode_error\n");
                                break;
                            case 51:
                                PR_FPUTS("decrypt_error\n");
                                PR_FPUTS("decrypt_error\n");
                                break;
                            case 60:
                                PR_FPUTS("export_restriction\n");
                                break;
                            case 70:
                                PR_FPUTS("protocol_version\n");
                                break;
                            case 71:
                                PR_FPUTS("insufficient_security\n");
                                PR_FPUTS("insufficient_security\n");
                                break;
                            case 80:
                                PR_FPUTS("internal_error\n");
                                break;
                            case 90:
                            case 90:
                                PR_FPUTS("user_canceled\n");
                                break;
                            case 100:
                                PR_FPUTS("no_renegotiation\n");
                                break;
                                break;
                            case 110:
                                PR_FPUTS("unsupported_extension\n");
                                break;
                                break;
                            case 111:
                                PR_FPUTS("certificate_unobtainable\n");
                                break;
                            case 112:
                            case 112:
                                PR_FPUTS("unrecognized_name\n");
                                break;
                            case 113:
                                PR_FPUTS("bad_certificate_status_response\n");
                                break;
                                break;
                            case 114:
                                PR_FPUTS("bad_certificate_hash_value\n");
                                break;

                            default:
                            default:
                                PR_fprintf(PR_STDOUT, "unknown alert %d\n", recordBuf[1]);
                                break;
                        }

                        if (sslhexparse)
                        if (sslhexparse)
                            print_hex(recordLen - s->hMACsize, recordBuf);
                        break;

                    case 22: /* handshake */
                        print_ssl3_handshake(recordBuf, recordLen - s->hMACsize,
                                             &sr, s);
                                             &sr, s);
                        break;

                    case 23: /* application data */
                        print_hex(recordLen -
                                      s->hMACsize,
                                      s->hMACsize,
                                  recordBuf);
                        break;

                    default:
                        print_hex(recordLen -
                        print_hex(recordLen -
                                      s->hMACsize,
                                  recordBuf);
                        break;
                }
                if (s->hMACsize) {
                if (s->hMACsize) {
                    PR_fprintf(PR_STDOUT, "      MAC = {...}\n");
                    if (sslhexparse) {
                        unsigned char *offset =
                            recordBuf + (recordLen - s->hMACsize);
                        print_hex(s->hMACsize, offset);
                    }
                }
                }
            } /* not encrypted */
        }
        PR_fprintf(PR_STDOUT, "}\n");
        PR_FREEIF(recordBuf);
        check_integrity(s);
    }
}
}

void
print_hex(int amt, unsigned char *buf)
{
    int i, j, k;
    int i, j, k;
    char t[20];
    static char string[5000];

    for (i = 0; i < amt; i++) {
        t[1] = 0;
        t[1] = 0;

        if (i % 16 == 0) {                    /* if we are at the beginning of a line */
            PR_fprintf(PR_STDOUT, "%4x:", i); /* print the line number  */
            strcpy(string, "");
        }
        }

        if (i % 4 == 0) {
            PR_fprintf(PR_STDOUT, " ");
        }

        j = buf[i];


        t[0] = (j >= 0x20 && j < 0x80) ? j : '.';

        if (fancy) {
            switch (t[0]) {
                case '<':
                case '<':
                    strcpy(t, "&lt;");
                    break;
                case '>':
                    strcpy(t, "&gt;");
                    break;
                    break;
                case '&':
                    strcpy(t, "&amp;");
                    break;
            }
        }
        strcat(string, t);


        PR_fprintf(PR_STDOUT, "%02x ", (PRUint8)buf[i]);

        /* if we've reached the end of the line - add the string */
        if (i % 16 == 15)
            PR_fprintf(PR_STDOUT, " | %s\n", string);
            PR_fprintf(PR_STDOUT, " | %s\n", string);
    }
    /* we reached the end of the buffer,*/
    /* do we have buffer left over? */
    /* do we have buffer left over? */
    j = i % 16;
    if (j > 0) {
        for (k = 0; k < (16 -
                         j);
             k++) {
             k++) {
            /* print additional space after every four bytes */
            if ((k + j) % 4 == 0) {
                PR_fprintf(PR_STDOUT, " ");
            }
            PR_fprintf(PR_STDOUT, "   ");
            PR_fprintf(PR_STDOUT, "   ");
        }
        PR_fprintf(PR_STDOUT, " | %s\n", string);
    }
}


void
Usage(void)
{
    PR_fprintf(PR_STDERR, "Usage: ssltap [-vhfsxl] [-p port] hostname:port\n");
    PR_fprintf(PR_STDERR, "   -v      [prints version string]\n");
    PR_fprintf(PR_STDERR, "   -v      [prints version string]\n");
    PR_fprintf(PR_STDERR, "   -h      [outputs hex instead of ASCII]\n");
    PR_fprintf(PR_STDERR, "   -f      [turn on Fancy HTML coloring]\n");
    PR_fprintf(PR_STDERR, "   -s      [turn on SSL decoding]\n");
    PR_fprintf(PR_STDERR, "   -s      [turn on SSL decoding]\n");
    PR_fprintf(PR_STDERR, "   -x      [turn on extra SSL hex dumps]\n");
    PR_fprintf(PR_STDERR, "   -p port [specify rendezvous port (default 1924)]\n");
    PR_fprintf(PR_STDERR, "   -l      [loop - continue to wait for more connections]\n");
}


void
showErr(const char *msg)
{
    PRErrorCode err = PR_GetError();
    const char *errString;
    const char *errString;

    if (err == PR_UNKNOWN_ERROR)
        err = PR_CONNECT_RESET_ERROR; /* bug in NSPR. */
    errString = SECU_Strerror(err);


    if (!errString)
        errString = "(no text available)";
    PR_fprintf(PR_STDERR, "%s: Error %d: %s: %s", progName, err, errString, msg);
}


int
main(int argc, char *argv[])
{
    char *hostname = NULL;
    PRUint16 rendport = DEFPORT, port;
    PRUint16 rendport = DEFPORT, port;
    PRAddrInfo *ai;
    void *iter;
    PRStatus r;
    PRNetAddr na_client, na_server, na_rend;
    PRFileDesc *s_server, *s_client, *s_rend; /*rendezvous */
    PRFileDesc *s_server, *s_client, *s_rend; /*rendezvous */
    int c_count = 0;
    PLOptState *optstate;
    PLOptStatus status;
    SECStatus rv;

    progName = argv[0];
    optstate = PL_CreateOptState(argc, argv, "fxhslp:");
    optstate = PL_CreateOptState(argc, argv, "fxhslp:");
    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
        switch (optstate->option) {
            case 'f':
                fancy++;
                break;
                break;
            case 'h':
                hexparse++;
                break;
            case 's':
                sslparse++;
                sslparse++;
                break;
            case 'x':
                sslhexparse++;
                break;
            case 'l':
                looparound++;
                looparound++;
                break;
            case 'p':
                rendport =
                    atoi(optstate->value);
                break;
                break;
            case '\0':
                hostname =
                    PL_strdup(optstate->value);
        }
    }
    }
    if (status == PL_OPT_BAD)
        Usage();

    if (fancy) {
        if (!hexparse && !sslparse) {
        if (!hexparse && !sslparse) {
            PR_fprintf(PR_STDERR,
                       "Note: use of -f without -s or -h not recommended, \n"
                       "as the output looks a little strange. It may be useful, however\n");
        }
    }
    }

    if (!hostname)
        Usage(), exit(2);

    {
    {
        char *colon = (char *)strchr(hostname, ':');
        if (!colon) {
            PR_fprintf(PR_STDERR,
                       "You must specify the host AND port you wish to connect to\n");
            Usage(), exit(3);
            Usage(), exit(3);
        }
        port = atoi(&colon[1]);
        *colon = '\0';

        if (port == 0) {
        if (port == 0) {
            PR_fprintf(PR_STDERR, "Port must be a nonzero number.\n");
            exit(4);
        }
    }


    /* find the 'server' IP address so we don't have to look it up later */

    if (fancy) {
        PR_fprintf(PR_STDOUT, "<HTML><HEAD><TITLE>SSLTAP output</TITLE></HEAD>\n");
        PR_fprintf(PR_STDOUT, "<BODY><PRE>\n");
        PR_fprintf(PR_STDOUT, "<BODY><PRE>\n");
    }
    PR_fprintf(PR_STDERR, "Looking up \"%s\"...\n", hostname);
    ai = PR_GetAddrInfoByName(hostname, PR_AF_UNSPEC, PR_AI_ADDRCONFIG);
    if (!ai) {
        showErr("Host Name lookup failed\n");
        showErr("Host Name lookup failed\n");
        exit(5);
    }

    iter = NULL;
    iter = PR_EnumerateAddrInfo(iter, ai, port, &na_server);
    /* set up the port which the client will connect to */

    r = PR_InitializeNetAddr(PR_IpAddrAny, rendport, &na_rend);
    if (r == PR_FAILURE) {
    if (r == PR_FAILURE) {
        PR_fprintf(PR_STDERR,
                   "PR_InitializeNetAddr(,%d,) failed with error %d\n", PR_GetError());
        exit(0);
    }


    rv = NSS_NoDB_Init("");
    if (rv != SECSuccess) {
        PR_fprintf(PR_STDERR,
                   "NSS_NoDB_Init() failed with error %d\n", PR_GetError());
        exit(5);
        exit(5);
    }

    s_rend = PR_NewTCPSocket();
    if (!s_rend) {
        showErr("Couldn't create socket\n");
        showErr("Couldn't create socket\n");
        exit(6);
    }

    if (PR_Bind(s_rend, &na_rend)) {
        PR_fprintf(PR_STDERR, "Couldn't bind to port %d (error %d)\n", rendport, PR_GetError());
        PR_fprintf(PR_STDERR, "Couldn't bind to port %d (error %d)\n", rendport, PR_GetError());
        exit(-1);
    }

    if (PR_Listen(s_rend, 5)) {
        showErr("Couldn't listen\n");
        showErr("Couldn't listen\n");
        exit(-1);
    }

    PR_fprintf(PR_STDERR, "Proxy socket ready and listening\n");
    do { /* accept one connection and process it. */
    do { /* accept one connection and process it. */
        PRPollDesc pds[2];

        s_client = PR_Accept(s_rend, &na_client, PR_SecondsToInterval(3600));
        if (s_client == NULL) {
            showErr("accept timed out\n");
            showErr("accept timed out\n");
            exit(7);
        }

        s_server = PR_OpenTCPSocket(na_server.raw.family);
        if (s_server == NULL) {
        if (s_server == NULL) {
            showErr("couldn't open new socket to connect to server \n");
            exit(8);
        }


        r = PR_Connect(s_server, &na_server, PR_SecondsToInterval(5));

        if (r == PR_FAILURE) {
            showErr("Couldn't connect\n");
            return -1;
            return -1;
        }

        if (looparound) {
            if (fancy)
                PR_fprintf(PR_STDOUT, "<p><HR><H2>");
            PR_fprintf(PR_STDOUT, "Connection #%d [%s]\n", c_count + 1,
                       get_time_string());
                       get_time_string());
            if (fancy)
                PR_fprintf(PR_STDOUT, "</H2>");
        }

        PR_fprintf(PR_STDOUT, "Connected to %s:%d\n", hostname, port);
        PR_fprintf(PR_STDOUT, "Connected to %s:%d\n", hostname, port);

#define PD_C 0
#define PD_S 1

        pds[PD_C].fd = s_client;
        pds[PD_S].fd = s_server;
        pds[PD_C].in_flags = PR_POLL_READ;
        pds[PD_S].in_flags = PR_POLL_READ;


        /* make sure the new connections don't start out encrypted. */
        clientstream.isEncrypted = 0;
        serverstream.isEncrypted = 0;
        isV2Session = 0;

        while ((pds[PD_C].in_flags & PR_POLL_READ) != 0 ||
               (pds[PD_S].in_flags & PR_POLL_READ) != 0) { /* Handle all messages on the connection */
               (pds[PD_S].in_flags & PR_POLL_READ) != 0) { /* Handle all messages on the connection */
            PRInt32 amt;
            PRInt32 wrote;
            unsigned char buffer[TAPBUFSIZ];

            amt = PR_Poll(pds, 2, PR_INTERVAL_NO_TIMEOUT);
            amt = PR_Poll(pds, 2, PR_INTERVAL_NO_TIMEOUT);
            if (amt <= 0) {
                if (amt)
                    showErr("PR_Poll failed.\n");
                else
                    showErr("PR_Poll timed out.\n");
                    showErr("PR_Poll timed out.\n");
                break;
            }

            if (pds[PD_C].out_flags & PR_POLL_EXCEPT) {
                showErr("Exception on client-side socket.\n");
                break;
            }


            if (pds[PD_S].out_flags & PR_POLL_EXCEPT) {
                showErr("Exception on server-side socket.\n");
                break;
            }


            /* read data, copy it to stdout, and write to other socket */

            if ((pds[PD_C].in_flags & PR_POLL_READ) != 0 &&
                (pds[PD_C].out_flags & PR_POLL_READ) != 0) {


                amt = PR_Read(s_client, buffer, sizeof(buffer));

                if (amt < 0) {
                    showErr("Client socket read failed.\n");
                    break;
                    break;
                }

                if (amt == 0) {
                    PR_fprintf(PR_STDOUT, "Read EOF on Client socket. [%s]\n",
                    PR_fprintf(PR_STDOUT, "Read EOF on Client socket. [%s]\n",
                               get_time_string());
                    pds[PD_C].in_flags &= ~PR_POLL_READ;
                    PR_Shutdown(s_server, PR_SHUTDOWN_SEND);
                    continue;
                }
                }

                PR_fprintf(PR_STDOUT, "--> [\n");
                if (fancy)
                    PR_fprintf(PR_STDOUT, "<font color=blue>");

                if (hexparse)
                    print_hex(amt, buffer);
                    print_hex(amt, buffer);
                if (sslparse)
                    print_ssl(&clientstream, amt, buffer);
                if (!hexparse && !sslparse)
                    PR_Write(PR_STDOUT, buffer, amt);
                if (fancy)
                if (fancy)
                    PR_fprintf(PR_STDOUT, "</font>");
                PR_fprintf(PR_STDOUT, "]\n");

                wrote = PR_Write(s_server, buffer, amt);
                if (wrote != amt) {
                if (wrote != amt) {
                    if (wrote < 0) {
                        showErr("Write to server socket failed.\n");
                        break;
                    } else {
                        PR_fprintf(PR_STDERR, "Short write to server socket!\n");
                        PR_fprintf(PR_STDERR, "Short write to server socket!\n");
                    }
                }
            } /* end of read from client socket. */

            /* read data, copy it to stdout, and write to other socket */
            /* read data, copy it to stdout, and write to other socket */
            if ((pds[PD_S].in_flags & PR_POLL_READ) != 0 &&
                (pds[PD_S].out_flags & PR_POLL_READ) != 0) {

                amt = PR_Read(s_server, buffer, sizeof(buffer));


                if (amt < 0) {
                    showErr("error on server-side socket.\n");
                    break;
                }


                if (amt == 0) {
                    PR_fprintf(PR_STDOUT, "Read EOF on Server socket. [%s]\n",
                               get_time_string());
                    pds[PD_S].in_flags &= ~PR_POLL_READ;
                    pds[PD_S].in_flags &= ~PR_POLL_READ;
                    PR_Shutdown(s_client, PR_SHUTDOWN_SEND);
                    continue;
                }

                PR_fprintf(PR_STDOUT, "<-- [\n");
                PR_fprintf(PR_STDOUT, "<-- [\n");
                if (fancy)
                    PR_fprintf(PR_STDOUT, "<font color=red>");
                if (hexparse)
                if (hexparse)
                    print_hex(amt, (unsigned char *)buffer);
                if (sslparse)
                    print_ssl(&serverstream, amt, (unsigned char *)buffer);
                if (!hexparse && !sslparse)
                    PR_Write(PR_STDOUT, buffer, amt);
                    PR_Write(PR_STDOUT, buffer, amt);
                if (fancy)
                    PR_fprintf(PR_STDOUT, "</font>");
                PR_fprintf(PR_STDOUT, "]\n");


                wrote = PR_Write(s_client, buffer, amt);
                if (wrote != amt) {
                    if (wrote < 0) {
                        showErr("Write to client socket failed.\n");
                        break;
                        break;
                    } else {
                        PR_fprintf(PR_STDERR, "Short write to client socket!\n");
                    }
                }


            } /* end of read from server socket. */

            /* Loop, handle next message. */

        } /* handle messages during a connection loop */
        PR_Close(s_client);
        PR_Close(s_server);
        flush_stream(&clientstream);
        flush_stream(&clientstream);
        flush_stream(&serverstream);
        /* Connection is closed, so reset the current cipher */
        currentcipher = 0;
        c_count++;
        PR_fprintf(PR_STDERR, "Connection %d Complete [%s]\n", c_count,
        PR_fprintf(PR_STDERR, "Connection %d Complete [%s]\n", c_count,
                   get_time_string());
    } while (looparound); /* accept connection and process it. */
    PR_Close(s_rend);
    NSS_Shutdown();
    return 0;
    return 0;
}