|
@@ -25,6 +25,38 @@
|
|
|
#endif
|
|
|
|
|
|
#include <wolfssl/wolfcrypt/settings.h>
|
|
|
+#include <wolfssl/wolfcrypt/wc_port.h>
|
|
|
+
|
|
|
+/* xctime - should it move to wc_port.h? */
|
|
|
+#ifndef XCTIME
|
|
|
+ #define XCTIME ctime
|
|
|
+#endif
|
|
|
+
|
|
|
+/* should next ifdef move to wc_port.h? */
|
|
|
+#ifdef FUSION_RTOS
|
|
|
+ #include <fcl_network.h>
|
|
|
+ #define XINET_NTOA FNS_INET_NTOA
|
|
|
+ #define XINET_ATON FNS_INET_ATON
|
|
|
+ #define XINET_PTON(a,b,c,d) FNS_INET_PTON((a),(b),(c),(d),NULL)
|
|
|
+ #define XINET_NTOP(a,b,c,d) FNS_INET_NTOP((a),(b),(c),(d),NULL)
|
|
|
+ #define XINET_ADDR FNS_INET_ADDR
|
|
|
+ #define XHTONS FNS_HTONS
|
|
|
+ #define XNTOHS FNS_NTOHS
|
|
|
+ #define XHTONL FNS_HTONL
|
|
|
+ #define XNTOHL FNS_NTOHL
|
|
|
+ #define XINADDR_NONE FNS_INADDR_NONE
|
|
|
+#else
|
|
|
+ #define XINET_NTOA inet_ntoa
|
|
|
+ #define XINET_ATON inet_aton
|
|
|
+ #define XINET_PTON(a,b,c) inet_pton((a),(b),(c))
|
|
|
+ #define XINET_NTOP inet_ntop
|
|
|
+ #define XINET_ADDR inet_addr
|
|
|
+ #define XHTONS htons
|
|
|
+ #define XNTOHS ntohs
|
|
|
+ #define XHTONL htonl
|
|
|
+ #define XNTOHL ntohl
|
|
|
+ #define XINADDR_NONE INADDR_NONE
|
|
|
+#endif
|
|
|
|
|
|
#if !defined(WOLFCRYPT_ONLY) && !defined(NO_FILESYSTEM)
|
|
|
#ifdef WOLFSSL_SNIFFER
|
|
@@ -32,10 +64,17 @@
|
|
|
#include <assert.h>
|
|
|
#include <time.h>
|
|
|
|
|
|
-#ifndef _WIN32
|
|
|
- #include <arpa/inet.h>
|
|
|
+#ifdef FUSION_RTOS
|
|
|
+ #include <fns_inet.h>
|
|
|
+ #ifdef TCP_PROTOCOL
|
|
|
+ #undef TCP_PROTOCOL
|
|
|
+ #endif
|
|
|
#else
|
|
|
- #include <WS2tcpip.h>
|
|
|
+ #ifndef _WIN32
|
|
|
+ #include <arpa/inet.h>
|
|
|
+ #else
|
|
|
+ #include <WS2tcpip.h>
|
|
|
+ #endif
|
|
|
#endif
|
|
|
|
|
|
#ifdef _WIN32
|
|
@@ -95,7 +134,7 @@ enum {
|
|
|
EXT_TYPE_SZ = 2, /* Extension type length */
|
|
|
MAX_INPUT_SZ = MAX_RECORD_SIZE + COMP_EXTRA + MAX_MSG_EXTRA +
|
|
|
MTU_EXTRA, /* Max input sz of reassembly */
|
|
|
-
|
|
|
+
|
|
|
/* TLS Extensions */
|
|
|
EXT_SERVER_NAME = 0x0000, /* a.k.a. SNI */
|
|
|
EXT_MAX_FRAGMENT_LENGTH = 0x0001,
|
|
@@ -160,7 +199,7 @@ BOOL APIENTRY DllMain( HMODULE hModule,
|
|
|
|
|
|
|
|
|
static WOLFSSL_GLOBAL int TraceOn = 0; /* Trace is off by default */
|
|
|
-static WOLFSSL_GLOBAL FILE* TraceFile = 0;
|
|
|
+static WOLFSSL_GLOBAL XFILE TraceFile = 0;
|
|
|
|
|
|
|
|
|
/* windows uses .rc table for this */
|
|
@@ -716,7 +755,7 @@ void ssl_FreeSniffer(void)
|
|
|
|
|
|
if (TraceFile) {
|
|
|
TraceOn = 0;
|
|
|
- fclose(TraceFile);
|
|
|
+ XFCLOSE(TraceFile);
|
|
|
TraceFile = NULL;
|
|
|
}
|
|
|
|
|
@@ -955,9 +994,9 @@ static void Trace(int idx)
|
|
|
if (TraceOn) {
|
|
|
char myBuffer[MAX_ERROR_LEN];
|
|
|
GetError(idx, myBuffer);
|
|
|
- fprintf(TraceFile, "\t%s\n", myBuffer);
|
|
|
+ XFPRINTF(TraceFile, "\t%s\n", myBuffer);
|
|
|
#ifdef DEBUG_SNIFFER
|
|
|
- fprintf(stderr, "\t%s\n", myBuffer);
|
|
|
+ XFPRINTF(stderr, "\t%s\n", myBuffer);
|
|
|
#endif
|
|
|
}
|
|
|
}
|
|
@@ -967,8 +1006,8 @@ static void Trace(int idx)
|
|
|
static void TraceHeader(void)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- time_t ticks = time(NULL);
|
|
|
- fprintf(TraceFile, "\n%s", ctime(&ticks));
|
|
|
+ time_t ticks = XTIME(NULL);
|
|
|
+ XFPRINTF(TraceFile, "\n%s", XCTIME(&ticks));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -977,8 +1016,8 @@ static void TraceHeader(void)
|
|
|
static void TraceSetServer(const char* srv, int port, const char* keyFile)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tTrying to install a new Sniffer Server with\n");
|
|
|
- fprintf(TraceFile, "\tserver: %s, port: %d, keyFile: %s\n", srv, port,
|
|
|
+ XFPRINTF(TraceFile, "\tTrying to install a new Sniffer Server with\n");
|
|
|
+ XFPRINTF(TraceFile, "\tserver: %s, port: %d, keyFile: %s\n", srv, port,
|
|
|
keyFile);
|
|
|
}
|
|
|
}
|
|
@@ -991,8 +1030,8 @@ static void TraceSetNamedServer(const char* name,
|
|
|
const char* srv, int port, const char* keyFile)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tTrying to install a new Sniffer Server with\n");
|
|
|
- fprintf(TraceFile, "\tname: %s, server: %s, port: %d, keyFile: %s\n",
|
|
|
+ XFPRINTF(TraceFile, "\tTrying to install a new Sniffer Server with\n");
|
|
|
+ XFPRINTF(TraceFile, "\tname: %s, server: %s, port: %d, keyFile: %s\n",
|
|
|
name ? name : "",
|
|
|
srv ? srv : "",
|
|
|
port,
|
|
@@ -1008,7 +1047,7 @@ static void TracePacket(void)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
static word32 packetNumber = 0;
|
|
|
- fprintf(TraceFile, "\tGot a Packet to decode, packet %u\n",
|
|
|
+ XFPRINTF(TraceFile, "\tGot a Packet to decode, packet %u\n",
|
|
|
++packetNumber);
|
|
|
}
|
|
|
}
|
|
@@ -1017,7 +1056,7 @@ static void TracePacket(void)
|
|
|
/* Convert network byte order address into human readable */
|
|
|
static const char* IpToS(int version, void* src, char* dst)
|
|
|
{
|
|
|
- return inet_ntop(version, src, dst, TRACE_MSG_SZ);
|
|
|
+ return XINET_NTOP(version, src, dst, TRACE_MSG_SZ);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -1027,7 +1066,7 @@ static void TraceIP(IpHdr* iphdr)
|
|
|
if (TraceOn) {
|
|
|
char src[TRACE_MSG_SZ];
|
|
|
char dst[TRACE_MSG_SZ];
|
|
|
- fprintf(TraceFile, "\tdst:%s src:%s\n",
|
|
|
+ XFPRINTF(TraceFile, "\tdst:%s src:%s\n",
|
|
|
IpToS(AF_INET, &iphdr->dst, dst),
|
|
|
IpToS(AF_INET, &iphdr->src, src));
|
|
|
}
|
|
@@ -1040,7 +1079,7 @@ static void TraceIP6(Ip6Hdr* iphdr)
|
|
|
if (TraceOn) {
|
|
|
char src[TRACE_MSG_SZ];
|
|
|
char dst[TRACE_MSG_SZ];
|
|
|
- fprintf(TraceFile, "\tdst: %s src: %s\n",
|
|
|
+ XFPRINTF(TraceFile, "\tdst: %s src: %s\n",
|
|
|
IpToS(AF_INET6, iphdr->dst, dst),
|
|
|
IpToS(AF_INET6, iphdr->src, src));
|
|
|
}
|
|
@@ -1051,8 +1090,8 @@ static void TraceIP6(Ip6Hdr* iphdr)
|
|
|
static void TraceTcp(TcpHdr* tcphdr)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tdstPort:%u srcPort:%u\n", ntohs(tcphdr->dstPort),
|
|
|
- ntohs(tcphdr->srcPort));
|
|
|
+ XFPRINTF(TraceFile, "\tdstPort:%u srcPort:%u\n", XNTOHS(tcphdr->dstPort),
|
|
|
+ XNTOHS(tcphdr->srcPort));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1061,7 +1100,7 @@ static void TraceTcp(TcpHdr* tcphdr)
|
|
|
static void TraceSequence(word32 seq, int len)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tSequence:%u, payload length:%d\n", seq, len);
|
|
|
+ XFPRINTF(TraceFile, "\tSequence:%u, payload length:%d\n", seq, len);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1070,7 +1109,7 @@ static void TraceSequence(word32 seq, int len)
|
|
|
static void TraceAck(word32 ack, word32 expected)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tAck:%u Expected:%u\n", ack, expected);
|
|
|
+ XFPRINTF(TraceFile, "\tAck:%u Expected:%u\n", ack, expected);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1079,7 +1118,7 @@ static void TraceAck(word32 ack, word32 expected)
|
|
|
static void TraceRelativeSequence(word32 expected, word32 got)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tExpected sequence:%u, received sequence:%u\n",
|
|
|
+ XFPRINTF(TraceFile, "\tExpected sequence:%u, received sequence:%u\n",
|
|
|
expected, got);
|
|
|
}
|
|
|
}
|
|
@@ -1089,7 +1128,7 @@ static void TraceRelativeSequence(word32 expected, word32 got)
|
|
|
static void TraceServerSyn(word32 seq)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tServer SYN, Sequence Start:%u\n", seq);
|
|
|
+ XFPRINTF(TraceFile, "\tServer SYN, Sequence Start:%u\n", seq);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1098,7 +1137,7 @@ static void TraceServerSyn(word32 seq)
|
|
|
static void TraceClientSyn(word32 seq)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tClient SYN, Sequence Start:%u\n", seq);
|
|
|
+ XFPRINTF(TraceFile, "\tClient SYN, Sequence Start:%u\n", seq);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1107,7 +1146,7 @@ static void TraceClientSyn(word32 seq)
|
|
|
static void TraceClientFin(word32 finSeq, word32 relSeq)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tClient FIN capture:%u, current SEQ:%u\n",
|
|
|
+ XFPRINTF(TraceFile, "\tClient FIN capture:%u, current SEQ:%u\n",
|
|
|
finSeq, relSeq);
|
|
|
}
|
|
|
}
|
|
@@ -1117,7 +1156,7 @@ static void TraceClientFin(word32 finSeq, word32 relSeq)
|
|
|
static void TraceServerFin(word32 finSeq, word32 relSeq)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tServer FIN capture:%u, current SEQ:%u\n",
|
|
|
+ XFPRINTF(TraceFile, "\tServer FIN capture:%u, current SEQ:%u\n",
|
|
|
finSeq, relSeq);
|
|
|
}
|
|
|
}
|
|
@@ -1127,7 +1166,7 @@ static void TraceServerFin(word32 finSeq, word32 relSeq)
|
|
|
static void TraceGotData(int bytes)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\t%d bytes of SSL App data processed\n", bytes);
|
|
|
+ XFPRINTF(TraceFile, "\t%d bytes of SSL App data processed\n", bytes);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1136,7 +1175,7 @@ static void TraceGotData(int bytes)
|
|
|
static void TraceAddedData(int newBytes, int existingBytes)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile,
|
|
|
+ XFPRINTF(TraceFile,
|
|
|
"\t%d bytes added to %d existing bytes in User Buffer\n",
|
|
|
newBytes, existingBytes);
|
|
|
}
|
|
@@ -1147,7 +1186,7 @@ static void TraceAddedData(int newBytes, int existingBytes)
|
|
|
static void TraceStaleSession(void)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tFound a stale session\n");
|
|
|
+ XFPRINTF(TraceFile, "\tFound a stale session\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1156,7 +1195,7 @@ static void TraceStaleSession(void)
|
|
|
static void TraceFindingStale(void)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tTrying to find Stale Sessions\n");
|
|
|
+ XFPRINTF(TraceFile, "\tTrying to find Stale Sessions\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1165,7 +1204,7 @@ static void TraceFindingStale(void)
|
|
|
static void TraceRemovedSession(void)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tRemoved it\n");
|
|
|
+ XFPRINTF(TraceFile, "\tRemoved it\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1175,7 +1214,7 @@ static void TraceSessionInfo(SSLInfo* sslInfo)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
if (sslInfo != NULL && sslInfo->isValid) {
|
|
|
- fprintf(TraceFile,
|
|
|
+ XFPRINTF(TraceFile,
|
|
|
"\tver:(%u %u) suiteId:(%02x %02x) suiteName:(%s) "
|
|
|
#ifdef HAVE_SNI
|
|
|
"sni:(%s) "
|
|
@@ -1201,7 +1240,7 @@ static void TraceSessionInfo(SSLInfo* sslInfo)
|
|
|
static void TraceStat(const char* name, int add)
|
|
|
{
|
|
|
if (TraceOn) {
|
|
|
- fprintf(TraceFile, "\tAdding %d to %s\n", add, name);
|
|
|
+ XFPRINTF(TraceFile, "\tAdding %d to %s\n", add, name);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1368,7 +1407,7 @@ static word32 SessionHash(IpInfo* ipInfo, TcpInfo* tcpInfo)
|
|
|
static SnifferSession* GetSnifferSession(IpInfo* ipInfo, TcpInfo* tcpInfo)
|
|
|
{
|
|
|
SnifferSession* session;
|
|
|
- time_t currTime = time(NULL);
|
|
|
+ time_t currTime = XTIME(NULL);
|
|
|
word32 row = SessionHash(ipInfo, tcpInfo);
|
|
|
|
|
|
assert(row <= HASH_SIZE);
|
|
@@ -1581,9 +1620,14 @@ static int SetNamedPrivateKey(const char* name, const char* address, int port,
|
|
|
#endif
|
|
|
|
|
|
serverIp.version = IPV4;
|
|
|
- serverIp.ip4 = inet_addr(address);
|
|
|
- if (serverIp.ip4 == INADDR_NONE) {
|
|
|
- if (inet_pton(AF_INET6, address, serverIp.ip6) == 1) {
|
|
|
+ serverIp.ip4 = XINET_ADDR(address);
|
|
|
+ if (serverIp.ip4 == XINADDR_NONE) {
|
|
|
+ #ifdef FUSION_RTOS
|
|
|
+ if (XINET_PTON(AF_INET6, address, serverIp.ip6,
|
|
|
+ sizeof(serverIp.ip4)) == 1) {
|
|
|
+ #else
|
|
|
+ if (XINET_PTON(AF_INET6, address, serverIp.ip6) == 1) {
|
|
|
+ #endif
|
|
|
serverIp.version = IPV6;
|
|
|
}
|
|
|
}
|
|
@@ -1896,7 +1940,7 @@ static int CheckIp6Hdr(Ip6Hdr* iphdr, IpInfo* info, int length, char* error)
|
|
|
#endif
|
|
|
|
|
|
info->length = exthdrsz;
|
|
|
- info->total = ntohs(iphdr->length) + info->length;
|
|
|
+ info->total = XNTOHS(iphdr->length) + info->length;
|
|
|
/* IPv6 doesn't include its own header size in the length like v4. */
|
|
|
info->src.version = IPV6;
|
|
|
XMEMCPY(info->src.ip6, iphdr->src, sizeof(info->src.ip6));
|
|
@@ -1938,7 +1982,7 @@ static int CheckIpHdr(IpHdr* iphdr, IpInfo* info, int length, char* error)
|
|
|
#endif
|
|
|
|
|
|
info->length = IP_HL(iphdr);
|
|
|
- info->total = ntohs(iphdr->length);
|
|
|
+ info->total = XNTOHS(iphdr->length);
|
|
|
info->src.version = IPV4;
|
|
|
info->src.ip4 = iphdr->src;
|
|
|
info->dst.version = IPV4;
|
|
@@ -1957,16 +2001,16 @@ static int CheckTcpHdr(TcpHdr* tcphdr, TcpInfo* info, char* error)
|
|
|
{
|
|
|
TraceTcp(tcphdr);
|
|
|
Trace(TCP_CHECK_STR);
|
|
|
- info->srcPort = ntohs(tcphdr->srcPort);
|
|
|
- info->dstPort = ntohs(tcphdr->dstPort);
|
|
|
+ info->srcPort = XNTOHS(tcphdr->srcPort);
|
|
|
+ info->dstPort = XNTOHS(tcphdr->dstPort);
|
|
|
info->length = TCP_LEN(tcphdr);
|
|
|
- info->sequence = ntohl(tcphdr->sequence);
|
|
|
+ info->sequence = XNTOHL(tcphdr->sequence);
|
|
|
info->fin = tcphdr->flags & TCP_FIN;
|
|
|
info->rst = tcphdr->flags & TCP_RST;
|
|
|
info->syn = tcphdr->flags & TCP_SYN;
|
|
|
info->ack = tcphdr->flags & TCP_ACK;
|
|
|
if (info->ack)
|
|
|
- info->ackNumber = ntohl(tcphdr->ack);
|
|
|
+ info->ackNumber = XNTOHL(tcphdr->ack);
|
|
|
|
|
|
#ifndef WOLFSSL_SNIFFER_WATCH
|
|
|
if (!IsPortRegistered(info->srcPort) && !IsPortRegistered(info->dstPort)) {
|
|
@@ -4010,7 +4054,7 @@ static void RemoveStaleSessions(void)
|
|
|
session = SessionTable[i];
|
|
|
while (session) {
|
|
|
SnifferSession* next = session->next;
|
|
|
- if (time(NULL) >= session->lastUsed + WOLFSSL_SNIFFER_TIMEOUT) {
|
|
|
+ if (XTIME(NULL) >= session->lastUsed + WOLFSSL_SNIFFER_TIMEOUT) {
|
|
|
TraceStaleSession();
|
|
|
RemoveSession(session, NULL, NULL, i);
|
|
|
}
|
|
@@ -4059,7 +4103,7 @@ static SnifferSession* CreateSession(IpInfo* ipInfo, TcpInfo* tcpInfo,
|
|
|
session->cliPort = (word16)tcpInfo->srcPort;
|
|
|
session->cliSeqStart = tcpInfo->sequence;
|
|
|
session->cliExpected = 1; /* relative */
|
|
|
- session->lastUsed= time(NULL);
|
|
|
+ session->lastUsed= XTIME(NULL);
|
|
|
session->keySz = 0;
|
|
|
#ifdef HAVE_SNI
|
|
|
session->sni = NULL;
|