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
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Netscape security libraries.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1994-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* 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.1 : Nelson Bolyard : 24 May 1999
*
* changes in version 2.0:
* Uses NSPR20
* 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 "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"
#define VERSIONSTRING "$Revision: 1.7.2.2 $ ($Date: 2006/07/19 01:18:33 $) $Author: nelson%bolyard.com $"
struct _DataBufferList;
struct _DataBuffer;
typedef struct _DataBufferList {
struct _DataBuffer *first,*last;
int size;
int isEncrypted;
} DataBufferList;
typedef struct _DataBuffer {
unsigned char *buffer;
int length;
int offset; /* offset of first good byte */
struct _DataBuffer *next;
} DataBuffer;
DataBufferList
clientstream = {NULL, NULL, 0, 0},
serverstream = {NULL, NULL, 0, 0};
struct sslhandshake {
PRUint8 type;
PRUint32 length;
};
typedef struct _SSLRecord {
PRUint8 type;
PRUint8 ver_maj,ver_min;
PRUint8 length[2];
} SSLRecord;
typedef struct _ClientHelloV2 {
PRUint8 length[2];
PRUint8 type;
PRUint8 version[2];
PRUint8 cslength[2];
PRUint8 sidlength[2];
PRUint8 rndlength[2];
PRUint8 csuites[1];
} ClientHelloV2;
typedef struct _ServerHelloV2 {
PRUint8 length[2];
PRUint8 type;
PRUint8 sidhit;
PRUint8 certtype;
PRUint8 version[2];
PRUint8 certlength[2];
PRUint8 cslength[2];
PRUint8 cidlength[2];
} ServerHelloV2;
typedef struct _ClientMasterKeyV2 {
PRUint8 length[2];
PRUint8 type;
PRUint8 cipherkind[3];
PRUint8 clearkey[2];
PRUint8 secretkey[2];
} ClientMasterKeyV2;
#define TAPBUFSIZ 16384
#define DEFPORT 1924
#include <ctype.h>
int hexparse=0;
int sslparse=0;
int sslhexparse=0;
int looparound=0;
int fancy=0;
int isV2Session=0;
#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)[1]) << 8) \
+ \
(((PRUint32)((PRUint8*)x)[2]) << 0) \
) )
void print_hex(int amt, unsigned char *buf);
void read_stream_bytes(unsigned char *d, DataBufferList *db, int length);
void myhalt(int dblsize,int collectedsize) {
while(1) ;
}
const char *get_error_text(int error) {
switch (error) {
case PR_IO_TIMEOUT_ERROR:
return "Timeout";
break;
case PR_CONNECT_REFUSED_ERROR:
return "Connection refused";
break;
case PR_NETWORK_UNREACHABLE_ERROR:
return "Network unreachable";
break;
case PR_BAD_ADDRESS_ERROR:
return "Bad address";
break;
case PR_CONNECT_RESET_ERROR:
return "Connection reset";
break;
case PR_PIPE_ERROR:
return "Pipe error";
break;
}
return "";
}
void check_integrity(DataBufferList *dbl) {
DataBuffer *db;
int i;
db = dbl->first;
i =0;
while (db) {
i+= db->length - db->offset;
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 *
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) {
dbl->last = NULL;
}
PR_Free(db->buffer);
PR_Free(db);
db = dbl->first;
}
return db;
}
void
read_stream_bytes(unsigned char *d, DataBufferList *dbl, int length) {
int copied = 0;
DataBuffer *db = dbl->first;
if (!db) {
PR_fprintf(PR_STDERR,"assert failed - dbl->first is null\n");
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' */
toCopy = PR_MIN(db->length - db->offset, length);
memcpy(d + copied, db->buffer + db->offset, toCopy);
copied += toCopy;
db->offset += toCopy;
length -= toCopy;
dbl->size -= toCopy;
/* if we emptied the head buffer */
if (db->offset >= db->length) {
db = free_head(dbl);
}
}
check_integrity(dbl);
}
void
flush_stream(DataBufferList *dbl)
{
DataBuffer *db = dbl->first;
check_integrity(dbl);
while (db) {
db->offset = db->length;
db = free_head(dbl);
}
dbl->size = 0;
check_integrity(dbl);
}
const char * V2CipherString(int cs_int) {
char *cs_str;
cs_str = NULL;
switch (cs_int) {
case 0x010080: cs_str = "SSL2/RSA/RC4-128/MD5"; break;
case 0x020080: cs_str = "SSL2/RSA/RC4-40/MD5"; break;
case 0x030080: cs_str = "SSL2/RSA/RC2CBC128/MD5"; break;
case 0x040080: cs_str = "SSL2/RSA/RC2CBC40/MD5"; break;
case 0x050080: cs_str = "SSL2/RSA/IDEA128CBC/MD5"; break;
case 0x060040: 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"; break;
case 0x000003: cs_str = "SSL3/RSA/RC4-40/MD5"; break;
case 0x000004: cs_str = "SSL3/RSA/RC4-128/MD5"; break;
case 0x000005: cs_str = "SSL3/RSA/RC4-128/SHA"; break;
case 0x000006: cs_str = "SSL3/RSA/RC2CBC40/MD5"; break;
case 0x000007: cs_str = "SSL3/RSA/IDEA128CBC/SHA"; break;
case 0x000008: cs_str = "SSL3/RSA/DES40-CBC/SHA"; break;
case 0x000009: cs_str = "SSL3/RSA/DES56-CBC/SHA"; break;
case 0x00000A: cs_str = "SSL3/RSA/3DES192EDE-CBC/SHA"; break;
case 0x00000B: cs_str = "SSL3/DH-DSS/DES40-CBC/SHA"; break;
case 0x00000C: cs_str = "SSL3/DH-DSS/DES56-CBC/SHA"; break;
case 0x00000D: cs_str = "SSL3/DH-DSS/DES192EDE3CBC/SHA"; break;
case 0x00000E: cs_str = "SSL3/DH-RSA/DES40-CBC/SHA"; break;
case 0x00000F: cs_str = "SSL3/DH-RSA/DES56-CBC/SHA"; break;
case 0x000010: 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;
case 0x000013: cs_str = "SSL3/DHE-DSS/DES192EDE3CBC/SHA"; break;
case 0x000014: cs_str = "SSL3/DHE-RSA/DES40-CBC/SHA"; break;
case 0x000015: cs_str = "SSL3/DHE-RSA/DES56-CBC/SHA"; break;
case 0x000016: cs_str = "SSL3/DHE-RSA/3DES192EDE-CBC/SHA"; break;
case 0x000017: cs_str = "SSL3/DH-anon/RC4-40/MD5"; break;
case 0x000018: cs_str = "SSL3/DH-anon/RC4-128/MD5"; break;
case 0x000019: cs_str = "SSL3/DH-anon/DES40-CBC/SHA"; break;
case 0x00001A: cs_str = "SSL3/DH-anon/DES56-CBC/SHA"; break;
case 0x00001B: cs_str = "SSL3/DH-anon/3DES192EDE-CBC/SHA"; break;
case 0x00001C: cs_str = "SSL3/FORTEZZA-DMS/NULL/SHA"; break;
case 0x00001D: cs_str = "SSL3/FORTEZZA-DMS/FORTEZZA-CBC/SHA"; break;
case 0x00001E: cs_str = "SSL3/FORTEZZA-DMS/RC4-128/SHA"; break;
case 0x00002F: cs_str = "TLS/RSA/AES128-CBC/SHA"; break;
case 0x000030: cs_str = "TLS/DH-DSS/AES128-CBC/SHA"; break;
case 0x000031: cs_str = "TLS/DH-RSA/AES128-CBC/SHA"; break;
case 0x000032: cs_str = "TLS/DHE-DSS/AES128-CBC/SHA"; break;
case 0x000033: 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"; 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: cs_str = "TLS/DHE-RSA/AES256-CBC/SHA"; break;
case 0x00003A: cs_str = "TLS/DH-ANON/AES256-CBC/SHA"; break;
case 0x000041: cs_str = "TLS/RSA/CAMELLIA128-CBC/SHA"; break;
case 0x000042: cs_str = "TLS/DH-DSS/CAMELLIA128-CBC/SHA"; break;
case 0x000043: cs_str = "TLS/DH-RSA/CAMELLIA128-CBC/SHA"; break;
case 0x000044: cs_str = "TLS/DHE-DSS/CAMELLIA128-CBC/SHA"; break;
case 0x000045: cs_str = "TLS/DHE-RSA/CAMELLIA128-CBC/SHA"; break;
case 0x000046: cs_str = "TLS/DH-ANON/CAMELLIA128-CBC/SHA"; break;
case 0x000047: cs_str = "TLS/ECDH-ECDSA/NULL/SHA"; break;
case 0x000048: cs_str = "TLS/ECDH-ECDSA/RC4-128/SHA"; break;
case 0x000049: cs_str = "TLS/ECDH-ECDSA/DES-CBC/SHA"; break;
case 0x00004A: cs_str = "TLS/ECDH-ECDSA/3DESEDE-CBC/SHA"; break;
case 0x00004B: cs_str = "TLS/ECDH-ECDSA/AES128-CBC/SHA"; break;
case 0x00004C: cs_str = "TLS/ECDH-ECDSA/AES256-CBC/SHA"; break;
case 0x00004D: cs_str = "TLS/ECDH-RSA/NULL/SHA"; break;
case 0x00004E: cs_str = "TLS/ECDH-RSA/RC4-128/SHA"; break;
case 0x00004F: cs_str = "TLS/ECDH-RSA/DES-CBC/SHA"; break;
case 0x000050: cs_str = "TLS/ECDH-RSA/3DESEDE-CBC/SHA"; break;
case 0x000051: cs_str = "TLS/ECDH-RSA/AES128-CBC/SHA"; break;
case 0x000052: cs_str = "TLS/ECDH-RSA/AES256-CBC/SHA"; break;
case 0x000060: cs_str = "TLS/RSA-EXPORT1024/RC4-56/MD5"; break;
case 0x000061: cs_str = "TLS/RSA-EXPORT1024/RC2CBC56/MD5"; break;
case 0x000062: cs_str = "TLS/RSA-EXPORT1024/DES56-CBC/SHA"; break;
case 0x000064: cs_str = "TLS/RSA-EXPORT1024/RC4-56/SHA"; break;
case 0x000063: cs_str = "TLS/DHE-DSS_EXPORT1024/DES56-CBC/SHA"; break;
case 0x000065: cs_str = "TLS/DHE-DSS_EXPORT1024/RC4-56/SHA"; break;
case 0x000066: cs_str = "TLS/DHE-DSS/RC4-128/SHA"; break;
case 0x000072: cs_str = "TLS/DHE-DSS/3DESEDE-CBC/RMD160"; break;
case 0x000073: cs_str = "TLS/DHE-DSS/AES128-CBC/RMD160"; break;
case 0x000074: cs_str = "TLS/DHE-DSS/AES256-CBC/RMD160"; break;
case 0x000077: cs_str = "TLS/ECDHE-ECDSA/AES128-CBC/SHA"; break;
case 0x000078: cs_str = "TLS/ECDHE-RSA/AES128-CBC/SHA"; break;
case 0x000079: cs_str = "TLS/DHE-RSA/AES256-CBC/RMD160"; break;
case 0x00007C: cs_str = "TLS/RSA/3DESEDE-CBC/RMD160"; break;
case 0x00007D: cs_str = "TLS/RSA/AES128-CBC/RMD160"; break;
case 0x00007E: cs_str = "TLS/RSA/AES256-CBC/RMD160"; break;
case 0x000080: 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"; break;
case 0x000084: cs_str = "TLS/RSA/CAMELLIA256-CBC/SHA"; break;
case 0x000085: cs_str = "TLS/DH-DSS/CAMELLIA256-CBC/SHA"; break;
case 0x000086: cs_str = "TLS/DH-RSA/CAMELLIA256-CBC/SHA"; break;
case 0x000087: cs_str = "TLS/DHE-DSS/CAMELLIA256-CBC/SHA"; 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;
case 0x00008B: cs_str = "TLS/PSK/3DES-EDE-CBC/SHA"; break;
case 0x00008C: cs_str = "TLS/PSK/AES128-CBC/SHA"; break;
case 0x00008D: cs_str = "TLS/PSK/AES256-CBC/SHA"; break;
case 0x00008E: cs_str = "TLS/DHE-PSK/RC4-128/SHA"; break;
case 0x00008F: cs_str = "TLS/DHE-PSK/3DES-EDE-CBC/SHA"; break;
case 0x000090: cs_str = "TLS/DHE-PSK/AES128-CBC/SHA"; break;
case 0x000091: cs_str = "TLS/DHE-PSK/AES256-CBC/SHA"; break;
case 0x000092: cs_str = "TLS/RSA-PSK/RC4-128/SHA"; break;
case 0x000093: cs_str = "TLS/RSA-PSK/3DES-EDE-CBC/SHA"; break;
case 0x000094: cs_str = "TLS/RSA-PSK/AES128-CBC/SHA"; break;
case 0x000095: cs_str = "TLS/RSA-PSK/AES256-CBC/SHA"; break;
case 0x000096: cs_str = "TLS/RSA/SEED-CBC/SHA"; break;
case 0x000097: cs_str = "TLS/DH-DSS/SEED-CBC/SHA"; break;
case 0x000098: cs_str = "TLS/DH-RSA/SEED-CBC/SHA"; break;
case 0x000099: cs_str = "TLS/DHE-DSS/SEED-CBC/SHA"; break;
case 0x00009A: cs_str = "TLS/DHE-RSA/SEED-CBC/SHA"; break;
case 0x00009B: cs_str = "TLS/DH-ANON/SEED-CBC/SHA"; break;
case 0x00C001: cs_str = "TLS/ECDH-ECDSA/NULL/SHA"; break;
case 0x00C002: cs_str = "TLS/ECDH-ECDSA/RC4-128/SHA"; break;
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"; break;
case 0x00C006: cs_str = "TLS/ECDHE-ECDSA/NULL/SHA"; break;
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: cs_str = "TLS/ECDHE-ECDSA/AES256-CBC/SHA"; break;
case 0x00C00B: cs_str = "TLS/ECDH-RSA/NULL/SHA"; break;
case 0x00C00C: cs_str = "TLS/ECDH-RSA/RC4-128/SHA"; break;
case 0x00C00D: cs_str = "TLS/ECDH-RSA/3DES-EDE-CBC/SHA"; break;
case 0x00C00E: cs_str = "TLS/ECDH-RSA/AES128-CBC/SHA"; break;
case 0x00C00F: cs_str = "TLS/ECDH-RSA/AES256-CBC/SHA"; break;
case 0x00C010: cs_str = "TLS/ECDHE-RSA/NULL/SHA"; 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: cs_str = "TLS/ECDHE-RSA/AES128-CBC/SHA"; break;
case 0x00C014: cs_str = "TLS/ECDHE-RSA/AES256-CBC/SHA"; break;
case 0x00C015: cs_str = "TLS/ECDH-anon/NULL/SHA"; break;
case 0x00C016: cs_str = "TLS/ECDH-anon/RC4-128/SHA"; break;
case 0x00C017: cs_str = "TLS/ECDH-anon/3DES-EDE-CBC/SHA"; break;
case 0x00C018: cs_str = "TLS/ECDH-anon/AES128-CBC/SHA"; break;
case 0x00C019: cs_str = "TLS/ECDH-anon/AES256-CBC/SHA"; break;
case 0x00feff: cs_str = "SSL3/RSA-FIPS/3DESEDE-CBC/SHA"; break;
case 0x00fefe: cs_str = "SSL3/RSA-FIPS/DES-CBC/SHA"; break;
case 0x00ffe1: cs_str = "SSL3/RSA-FIPS/DES56-CBC/SHA"; break;
case 0x00ffe0: cs_str = "SSL3/RSA-FIPS/3DES192EDE-CBC/SHA";break;
/* the string literal is broken up to avoid trigraphs */
default: cs_str = "????" "/????????" "/?????????" "/???"; break;
}
return cs_str;
}
const char * helloExtensionNameString(int ex_num) {
const char *ex_name = NULL;
static char buf[10];
switch (ex_num) {
case 0: ex_name = "server_name"; break;
case 1: ex_name = "max_fragment_length"; break;
case 2: ex_name = "client_certificate_url"; break;
case 3: ex_name = "trusted_ca_keys"; break;
case 4: ex_name = "truncated_hmac"; break;
case 5: ex_name = "status_request"; break;
case 10: ex_name = "elliptic_curves"; break;
case 11: ex_name = "ec_point_formats"; break;
default: sprintf(buf, "%d", ex_num); ex_name = (const char *)buf; break;
}
return ex_name;
}
void partial_packet(int thispacket, int size, int needed)
{
PR_fprintf(PR_STDOUT,"(%u bytes", thispacket);
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,")\n");
}
char * get_time_string(void)
{
char *cp;
char *eol;
time_t tt;
time(&tt);
cp = ctime(&tt);
eol = strchr(cp, '\n');
if (eol)
*eol = 0;
return cp;
}
void print_sslv2(DataBufferList *s, unsigned char *tbuf, unsigned int alloclen)
{
ClientHelloV2 *chv2;
ServerHelloV2 *shv2;
unsigned char *pos;
unsigned int p;
unsigned int q;
PRUint32 len;
chv2 = (ClientHelloV2 *)tbuf;
shv2 = (ServerHelloV2 *)tbuf;
if (s->isEncrypted) {
PR_fprintf(PR_STDOUT," [ssl2] Encrypted {...}\n");
return;
}
PR_fprintf(PR_STDOUT," [%s]", get_time_string() );
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",
(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",
(PRUint32)(GET_SHORT((chv2->rndlength))),
(PRUint32)(GET_SHORT((chv2->rndlength))));
PR_fprintf(PR_STDOUT," cipher-suites = { \n");
for (p=0;p<GET_SHORT((chv2->cslength));p+=3) {
const char *cs_str=NULL;
PRUint32 cs_int=0;
cs_int = GET_24((&chv2->csuites[p]));
cs_str = V2CipherString(cs_int);
PR_fprintf(PR_STDOUT," (0x%06x) %s\n",
cs_int, cs_str);
}
q = p;
PR_fprintf(PR_STDOUT," }\n");
if (chv2->sidlength) {
PR_fprintf(PR_STDOUT," session-id = { ");
for (p=0;p<GET_SHORT((chv2->sidlength));p+=2) {
PR_fprintf(PR_STDOUT,"0x%04x ",(PRUint32)(GET_SHORT((&chv2->csuites[p+q]))));
}
}
q += p;
PR_fprintf(PR_STDOUT,"}\n");
if (chv2->rndlength) {
PR_fprintf(PR_STDOUT," challenge = { ");
for (p=0;p<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 */
case 2: /* Client Master Key */
{
const char *cs_str=NULL;
PRUint32 cs_int=0;
ClientMasterKeyV2 *cmkv2;
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",
cs_int, cs_str);
PR_fprintf(PR_STDOUT," clear-portion = %d bits\n",
8*(PRUint32)(GET_SHORT((cmkv2->clearkey))));
PR_fprintf(PR_STDOUT," }\n");
clientstream.isEncrypted = 1;
serverstream.isEncrypted = 1;
}
break;
case 3:
PR_fprintf(PR_STDOUT," [ssl2] Client Finished V2 {...}\n");
isV2Session = 1;
break;
case 4: /* V2 Server Hello */
isV2Session = 1;
PR_fprintf(PR_STDOUT," [ssl2] ServerHelloV2 {\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))));
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 >alloclen) {
goto eosh;
}
pos += q; /* skip certificate */
PR_fprintf(PR_STDOUT," cipher-suites = { ");
len = GET_SHORT((shv2->cslength));
for (p = 0; p < len; p += 3) {
const char *cs_str=NULL;
PRUint32 cs_int=0;
cs_int = GET_24((pos+p));
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 */
len = (PRUint32)GET_SHORT((shv2->cidlength));
if (len) {
PR_fprintf(PR_STDOUT," connection-id = { ");
for (p = 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 */
}
eosh:
PR_fprintf(PR_STDOUT,"\n }\n"); /* end of ServerHelloV2 */
if (shv2->sidhit) {
clientstream.isEncrypted = 1;
serverstream.isEncrypted = 1;
}
break;
case 5:
PR_fprintf(PR_STDOUT," [ssl2] Server Verify V2 {...}\n");
isV2Session = 1;
break;
case 6:
PR_fprintf(PR_STDOUT," [ssl2] Server Finished V2 {...}\n");
isV2Session = 1;
break;
case 7:
PR_fprintf(PR_STDOUT," [ssl2] Request Certificate V2 {...}\n");
isV2Session = 1;
break;
case 8:
PR_fprintf(PR_STDOUT," [ssl2] Client Certificate V2 {...}\n");
isV2Session = 1;
break;
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)
{
/* pretty print extensions, if any */
if (pos < length) {
int exListLen = GET_SHORT((hsdata+pos)); pos += 2;
PR_fprintf(PR_STDOUT,
" extensions[%d] = {\n", exListLen);
while (exListLen > 0 && pos < length) {
int exLen;
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) {
PR_fprintf(PR_STDOUT, " = {\n");
print_hex(exLen, hsdata + pos);
PR_fprintf(PR_STDOUT, " }\n");
} else {
PR_fprintf(PR_STDOUT, "\n");
}
pos += exLen;
exListLen -= 2 + exLen;
}
PR_fprintf(PR_STDOUT," }\n");
}
return pos;
}
void print_ssl3_handshake(unsigned char *tbuf,
unsigned int alloclen,
SSLRecord * sr)
{
struct sslhandshake sslh;
unsigned char * hsdata;
int offset=0;
PR_fprintf(PR_STDOUT," handshake {\n");
while (offset < alloclen) {
sslh.type = tbuf[offset];
sslh.length = GET_24(tbuf+offset+1);
hsdata= &tbuf[offset+4];
if (sslhexparse) print_hex(4,tbuf+offset);
PR_fprintf(PR_STDOUT," type = %d (",sslh.type);
switch(sslh.type) {
case 0: PR_FPUTS("hello_request)\n" ); break;
case 1: PR_FPUTS("client_hello)\n" ); break;
case 2: PR_FPUTS("server_hello)\n" ); break;
case 11: PR_FPUTS("certificate)\n" ); break;
case 12: PR_FPUTS("server_key_exchange)\n" ); break;
case 13: PR_FPUTS("certificate_request)\n" ); break;
case 14: PR_FPUTS("server_hello_done)\n" ); break;
case 15: PR_FPUTS("certificate_verify)\n" ); break;
case 16: PR_FPUTS("client_key_exchange)\n" ); break;
case 20: PR_FPUTS("finished)\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. */ break;
case 1: /* client hello */
switch (sr->ver_maj) {
case 3: /* ssl version 3 */
{
unsigned int pos;
int w;
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]);
/* pretty print Session ID */
{
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) print_hex(sidlength,&hsdata[2+32+1]);
PR_fprintf(PR_STDOUT," }\n");
pos = 2+32+1+sidlength;
}
/* pretty print cipher suites */
{
int csuitelength = GET_SHORT((hsdata+pos));
PR_fprintf(PR_STDOUT," cipher_suites[%d] = { \n",
csuitelength/2);
if (csuitelength % 2) {
PR_fprintf(PR_STDOUT,
"*error in protocol - csuitelength shouldn't be odd*\n");
}
for (w=0; w<csuitelength; w+=2) {
const char *cs_str=NULL;
PRUint32 cs_int=0;
cs_int = GET_SHORT((hsdata+pos+2+w));
cs_str = V2CipherString(cs_int);
PR_fprintf(PR_STDOUT,
" (0x%04x) %s\n", cs_int, cs_str);
}
pos += 2 + csuitelength;
PR_fprintf(PR_STDOUT," }\n");
}
/* pretty print compression methods */
{
int complength = hsdata[pos];
PR_fprintf(PR_STDOUT," compression[%d] = {",
complength);
for (w=0; w < complength; w++) {
PR_fprintf(PR_STDOUT, " %02x", hsdata[pos+1+w]);
}
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");
} /* end of ssl version 3 */
break;
default:
PR_fprintf(PR_STDOUT," UNDEFINED VERSION %d.%d {...}\n",
sr->ver_maj, sr->ver_min );
if (sslhexparse) print_hex(sslh.length, hsdata);
break;
} /* end of switch sr->ver_maj */
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]);
PR_fprintf(PR_STDOUT," random = {...}\n");
if (sslhexparse) print_hex(32,&hsdata[2]);
PR_fprintf(PR_STDOUT," session ID = {\n");
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]);
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",
cs_int, cs_str);
pos += 2;
}
PR_fprintf(PR_STDOUT, " compression method = %02x\n",
hsdata[pos++]);
/* pretty print extensions, if any */
pos = print_hello_extension(hsdata, sslh.length, pos);
PR_fprintf(PR_STDOUT," }\n");
}
break;
case 11: /* certificate */
{
PRFileDesc *cfd;
int pos;
int certslength;
int certlength;
int certbytesread = 0;
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",
certslength,certslength);
pos = 3;
while (certbytesread < certslength) {
certlength = GET_24((hsdata+pos));
pos += 3;
PR_fprintf(PR_STDOUT," Certificate {\n");
PR_fprintf(PR_STDOUT," size = %d (0x%04x)\n",
certlength,certlength);
PR_snprintf(certFileName, sizeof certFileName, "cert.%03d",
++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+pos), certlength);
PR_fprintf(PR_STDOUT,
" data = { saved in file '%s' }\n",
certFileName);
PR_Close(cfd);
}
PR_fprintf(PR_STDOUT," }\n");
pos += certlength;
certbytesread += certlength+3;
}
PR_fprintf(PR_STDOUT," }\n");
}
break;
case 12: /* server_key_exchange */
if (sslhexparse) print_hex(sslh.length, hsdata);
break;
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];
PR_fprintf(PR_STDOUT," certificate types[%d] = {",
reqLength);
for (w=0; w < reqLength; w++) {
PR_fprintf(PR_STDOUT, " %02x", hsdata[pos+1+w]);
}
pos += 1 + reqLength;
PR_fprintf(PR_STDOUT," }\n");
/* pretty print CA names, if any */
if (pos < sslh.length) {
int exListLen = GET_SHORT((hsdata+pos)); pos += 2;
PR_fprintf(PR_STDOUT,
" certificate_authorities[%d] = {\n",
exListLen);
while (exListLen > 0 && pos < sslh.length) {
char * ca_name;
SECItem it;
int dnLen = GET_SHORT((hsdata+pos)); pos += 2;
/* dump the CA name */
it.type = siBuffer;
it.data = hsdata + pos;
it.len = dnLen;
ca_name = CERT_DerNameToAscii(&it);
if (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 > 0 && sslhexparse) {
PR_fprintf(PR_STDOUT, " = {\n");
print_hex(dnLen, hsdata + pos);
PR_fprintf(PR_STDOUT, " }\n");
} else {
PR_fprintf(PR_STDOUT, "\n");
}
}
pos += dnLen;
exListLen -= 2 + dnLen;
}
PR_fprintf(PR_STDOUT," }\n");
}
PR_fprintf(PR_STDOUT," }\n");
}
break;
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," }\n");
}
break;
case 20: /* finished */
if (sslhexparse) print_hex(sslh.length, hsdata);
break;
default:
{
PR_fprintf(PR_STDOUT," UNKNOWN MESSAGE TYPE %d [%d] {\n",
sslh.type, sslh.length);
if (sslhexparse) print_hex(sslh.length, hsdata);
PR_fprintf(PR_STDOUT," }\n");
}
} /* end of switch sslh.type */
offset += sslh.length + 4; /* +4 because of length (3 bytes) and type (1 byte) */
} /* while */
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;
int i,l;
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;
}
check_integrity(s);
i = 0;
l = length;
db = PR_NEW(struct _DataBuffer);
db->buffer = (unsigned char*)PR_Malloc(length);
db->length = length;
db->offset = 0;
memcpy(db->buffer, buffer, length);
db->next = NULL;
/* now, add it to the stream */
if (s->last != NULL) 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 ) {
unsigned char *tbuf = NULL;
SSLRecord sr;
unsigned alloclen;
unsigned recordsize;
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 (which is all ssltap supports) */
/* 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
*/
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;
}
/* read the first two bytes off the stream. */
read_stream_bytes(lenbuf, s, sizeof(lenbuf));
alloclen = ((unsigned int)(lenbuf[0] & 0x7f) << 8) + lenbuf[1] +
((lenbuf[0] & 0x80) ? 2 : 3);
PR_fprintf(PR_STDOUT, "alloclen = %u bytes\n", alloclen);
/* put 'em back on the head of the stream. */
db = PR_NEW(struct _DataBuffer);
db->length = sizeof lenbuf;
db->buffer = (unsigned char*) PR_Malloc(db->length);
db->offset = 0;
memcpy(db->buffer, lenbuf, sizeof lenbuf);
db->next = s->first;
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 < alloclen) {
check_integrity(s);
partial_packet(length, s->size, alloclen);
return;
}
partial_packet(length, s->size, alloclen);
/* read in the whole record. */
tbuf = PR_Malloc(alloclen);
read_stream_bytes(tbuf, s, alloclen);
print_sslv2(s, tbuf, alloclen);
PR_FREEIF(tbuf);
check_integrity(s);
continue;
}
/***********************************************************/
/* It's SSL v3 */
/***********************************************************/
check_integrity(s);
if (s->size < sizeof(SSLRecord)) {
partial_packet(length, s->size, sizeof(SSLRecord));
return;
}
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*) PR_Malloc(db->length);
db->offset = 0;
memcpy(db->buffer, &sr, sizeof sr);
db->next = s->first;
/* now, add it back on to the head of the stream */
s->first = db;
if (s->last == NULL)
s->last = db;
s->size += db->length;
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;
case 21 :
PR_fprintf(PR_STDOUT,"alert)\n");
break;
case 22 :
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;
}
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));
alloclen = recordsize;
PR_ASSERT(s->size >= alloclen);
if (s->size >= alloclen) {
tbuf = (unsigned char*) PR_Malloc(alloclen);
read_stream_bytes(tbuf, s, alloclen);
if (s->isEncrypted) {
PR_fprintf(PR_STDOUT," < encrypted >\n");
} else
switch(sr.type) {
case 20 : /* change_cipher_spec */
if (sslhexparse) print_hex(alloclen,tbuf);
s->isEncrypted = 1; /* mark to say we can only dump hex form now on */
break;
case 21 : /* alert */
switch(tbuf[0]) {
case 1: PR_fprintf(PR_STDOUT, " warning: "); break;
case 2: PR_fprintf(PR_STDOUT, " fatal: "); break;
default: PR_fprintf(PR_STDOUT, " unknown level %d: ", tbuf[0]); break;
}
switch(tbuf[1]) {
case 0: PR_FPUTS("close_notify\n" ); break;
case 10: PR_FPUTS("unexpected_message\n" ); break;
case 20: PR_FPUTS("bad_record_mac\n" ); break;
case 21: PR_FPUTS("decryption_failed\n" ); break;
case 22: PR_FPUTS("record_overflow\n" ); break;
case 30: PR_FPUTS("decompression_failure\n" ); break;
case 40: PR_FPUTS("handshake_failure\n" ); break;
case 41: PR_FPUTS("no_certificate\n" ); break;
case 42: PR_FPUTS("bad_certificate\n" ); break;
case 43: PR_FPUTS("unsupported_certificate\n" ); break;
case 44: PR_FPUTS("certificate_revoked\n" ); break;
case 45: PR_FPUTS("certificate_expired\n" ); break;
case 46: PR_FPUTS("certificate_unknown\n" ); break;
case 47: PR_FPUTS("illegal_parameter\n" ); break;
case 48: PR_FPUTS("unknown_ca\n" ); break;
case 49: PR_FPUTS("access_denied\n" ); break;
case 50: PR_FPUTS("decode_error\n" ); break;
case 51: 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" ); break;
case 80: PR_FPUTS("internal_error\n" ); break;
case 90: PR_FPUTS("user_canceled\n" ); break;
case 100: PR_FPUTS("no_renegotiation\n" ); break;
case 110: PR_FPUTS("unsupported_extension\n" ); break;
case 111: PR_FPUTS("certificate_unobtainable\n" ); break;
case 112: PR_FPUTS("unrecognized_name\n" ); break;
case 113: PR_FPUTS("bad_certificate_status_response\n" ); break;
case 114: PR_FPUTS("bad_certificate_hash_value\n" ); break;
default: PR_fprintf(PR_STDOUT, "unknown alert %d\n", tbuf[1]); break;
}
if (sslhexparse) print_hex(alloclen,tbuf);
break;
case 22 : /* handshake */
print_ssl3_handshake( tbuf, alloclen, &sr );
break;
case 23 : /* application data */
default:
print_hex(alloclen,tbuf);
break;
}
}
PR_fprintf(PR_STDOUT,"}\n");
PR_FREEIF(tbuf);
check_integrity(s);
}
}
void print_hex(int amt, unsigned char *buf) {
int i,j,k;
char t[20];
static char string[5000];
for(i=0;i<amt;i++) {
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 '<':
strcpy(t,"<");
break;
case '>':
strcpy(t,">");
break;
case '&':
strcpy(t,"&");
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);
}
/* we reached the end of the buffer,*/
/* do we have buffer left over? */
j = i%16;
if (j > 0) {
for (k=0;k<(16-j);k++) PR_fprintf(PR_STDOUT," ");
PR_fprintf(PR_STDOUT," |%s\n",string);
}
}
void Usage(void) {
PR_fprintf(PR_STDERR, "SSLTAP (C) 1997, 1998 Netscape Communications Corporation.\n");
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, " -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, " -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;
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, "Error %d: %s: %s", err, errString, msg);
}
int main(int argc, char *argv[])
{
char *hostname=NULL;
PRUint16 rendport=DEFPORT,port;
PRHostEnt hp;
PRStatus r;
PRNetAddr na_client,na_server,na_rend;
PRFileDesc *s_server,*s_client,*s_rend; /*rendezvous */
char netdbbuf[PR_NETDB_BUF_SIZE];
int c_count=0;
PLOptState *optstate;
PLOptStatus status;
SECStatus rv;
optstate = PL_CreateOptState(argc,argv,"fvxhslp:");
while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
switch (optstate->option) {
case 'f':
fancy++;
break;
case 'h':
hexparse++;
break;
case 'v':
PR_fprintf(PR_STDOUT,"Version: %s\n",VERSIONSTRING);
break;
case 's':
sslparse++;
break;
case 'x':
sslhexparse++;
break;
case 'l':
looparound++;
break;
case 'p':
rendport = atoi(optstate->value);
break;
case '\0':
hostname = PL_strdup(optstate->value);
}
}
if (status == PL_OPT_BAD)
Usage();
if (fancy) {
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);
}
port = atoi(&colon[1]);
*colon = '\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_STDERR,"Looking up \"%s\"...\n", hostname);
r = PR_GetHostByName(hostname,netdbbuf,PR_NETDB_BUF_SIZE,&hp);
if (r) {
showErr("Host Name lookup failed\n");
exit(5);
}
PR_EnumerateHostEnt(0,&hp,0,&na_server);
PR_InitializeNetAddr(PR_IpAddrNull,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) {
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);
}
s_rend = PR_NewTCPSocket();
if (!s_rend) {
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());
exit(-1);
}
if ( PR_Listen(s_rend, 5)) {
showErr("Couldn't listen\n");
exit(-1);
}
PR_fprintf(PR_STDERR,"Proxy socket ready and listening\n");
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");
exit(7);
}
s_server = PR_NewTCPSocket();
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;
}
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());
if (fancy) PR_fprintf(PR_STDOUT,"</H2>");
}
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 */
PRInt32 amt;
PRInt32 wrote;
unsigned char buffer[ TAPBUFSIZ ];
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");
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;
}
if( amt == 0 ) {
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);
if (sslparse) print_ssl(&clientstream,amt,buffer);
if (!hexparse && !sslparse) PR_Write(PR_STDOUT,buffer,amt);
if (fancy) PR_fprintf(PR_STDOUT,"</font>");
PR_fprintf(PR_STDOUT,"]\n");
wrote = PR_Write(s_server, buffer, 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");
}
}
} /* end of read from client 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;
PR_Shutdown(s_client, PR_SHUTDOWN_SEND);
continue;
}
PR_fprintf(PR_STDOUT,"<-- [\n");
if (fancy) PR_fprintf(PR_STDOUT,"<font color=red>");
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);
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;
} 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(&serverstream);
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;
}
|