text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64a.c
  Label Definition File: CWE319_Cleartext_Tx_Sensitive_Info__w32.label.xml
  Template File: sources-sinks-64a.tmpl.c
  */
  /*
   * @description
   * CWE: 319 Cleartext Transmission of Sensitive Information
   * BadSource: connect_socket Read the password using a connect socket (client side)
   * GoodSource: Use a hardcoded password (one that was not sent over the network)
   * Sinks:
   *    GoodSink: Decrypt the password before using it in an authentication API call to show that it was transferred as ciphertext
   *    BadSink : Use the password directly from the source in an authentication API call to show that it was transferred as plaintext
   * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #include <winsock2.h>
  #include <windows.h>
  #include <direct.h>
  #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
  
  #define TCP_PORT 27015
  #define IP_ADDRESS "127.0.0.1"
  
  #pragma comment(lib, "advapi32.lib")
  
  #define HASH_INPUT "ABCDEFG123456" /* INCIDENTAL: Hardcoded crypto */
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64b_badSink(void * passwordVoidPtr);
  
  void CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64_bad()
  {
      char * password;
      char passwordBuffer[100] = "";
      password = passwordBuffer;
      {
          WSADATA wsaData;
          int wsaDataInit = 0;
          int recvResult;
          struct sockaddr_in service;
          char *replace;
          SOCKET connectSocket = INVALID_SOCKET;
          size_t passwordLen = strlen(password);
          do
          {
              if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
              {
                  break;
              }
              wsaDataInit = 1;
              connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
              if (connectSocket == INVALID_SOCKET)
              {
                  break;
              }
              memset(&service, 0, sizeof(service));
              service.sin_family = AF_INET;
              service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
              service.sin_port = htons(TCP_PORT);
              if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
              {
                  break;
              }
              /* Abort on error or the connection was closed, make sure to recv one
               * less char than is in the recv_buf in order to append a terminator */
              /* POTENTIAL FLAW: Reading sensitive data from the network */
              recvResult = recv(connectSocket, (char*)(password + passwordLen), (100 - passwordLen - 1) * sizeof(char), 0);
              if (recvResult == SOCKET_ERROR || recvResult == 0)
              {
                  break;
              }
              /* Append null terminator */
              password[passwordLen + recvResult / sizeof(char)] = '\0';
              /* Eliminate CRLF */
              replace = strchr(password, '\r');
              if (replace)
              {
                  *replace = '\0';
              }
              replace = strchr(password, '\n');
              if (replace)
              {
                  *replace = '\0';
              }
          }
          while (0);
          if (connectSocket != INVALID_SOCKET)
          {
              closesocket(connectSocket);
          }
          if (wsaDataInit)
          {
              WSACleanup();
          }
      }
      CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64b_badSink(&password);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64b_goodG2BSink(void * passwordVoidPtr);
  
  static void goodG2B()
  {
      char * password;
      char passwordBuffer[100] = "";
      password = passwordBuffer;
      /* FIX: Use a hardcoded password (it was not sent over the network)
      * INCIDENTAL FLAW: CWE-259 Hard Coded Password */
      strcpy(password, "Password1234!");
      CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64b_goodG2BSink(&password);
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64b_goodB2GSink(void * passwordVoidPtr);
  
  static void goodB2G()
  {
      char * password;
      char passwordBuffer[100] = "";
      password = passwordBuffer;
      {
          WSADATA wsaData;
          int wsaDataInit = 0;
          int recvResult;
          struct sockaddr_in service;
          char *replace;
          SOCKET connectSocket = INVALID_SOCKET;
          size_t passwordLen = strlen(password);
          do
          {
              if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
              {
                  break;
              }
              wsaDataInit = 1;
              connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
              if (connectSocket == INVALID_SOCKET)
              {
                  break;
              }
              memset(&service, 0, sizeof(service));
              service.sin_family = AF_INET;
              service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
              service.sin_port = htons(TCP_PORT);
              if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
              {
                  break;
              }
              /* Abort on error or the connection was closed, make sure to recv one
               * less char than is in the recv_buf in order to append a terminator */
              /* POTENTIAL FLAW: Reading sensitive data from the network */
              recvResult = recv(connectSocket, (char*)(password + passwordLen), (100 - passwordLen - 1) * sizeof(char), 0);
              if (recvResult == SOCKET_ERROR || recvResult == 0)
              {
                  break;
              }
              /* Append null terminator */
              password[passwordLen + recvResult / sizeof(char)] = '\0';
              /* Eliminate CRLF */
              replace = strchr(password, '\r');
              if (replace)
              {
                  *replace = '\0';
              }
              replace = strchr(password, '\n');
              if (replace)
              {
                  *replace = '\0';
              }
          }
          while (0);
          if (connectSocket != INVALID_SOCKET)
          {
              closesocket(connectSocket);
          }
          if (wsaDataInit)
          {
              WSACleanup();
          }
      }
      CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64b_goodB2GSink(&password);
  }
  
  void CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64_good()
  {
      goodG2B();
      goodB2G();
  }
  
  #endif /* OMITGOOD */
  
  /* Below is the main(). It is only used when building this testcase on
     its own for testing or for building a binary to use in testing binary
     analysis tools. It is not used when compiling all the testcases as one
     application, which is how source code analysis tools are tested. */
  
  #ifdef INCLUDEMAIN
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE319_Cleartext_Tx_Sensitive_Info__w32_char_connect_socket_64_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  