text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE591_Sensitive_Data_Storage_in_Improperly_Locked_Memory__w32_wchar_t_66b.c
  Label Definition File: CWE591_Sensitive_Data_Storage_in_Improperly_Locked_Memory__w32.label.xml
  Template File: sources-sink-66b.tmpl.c
  */
  /*
   * @description
   * CWE: 591 Sensitive Data Storage in Improperly Locked Memory
   * BadSource:  Allocate memory for sensitive data and use VirtualLock() to lock the buffer into memory
   * GoodSource: Allocate memory for sensitive data and use VirtualLock() to lock the buffer into memory
   * Sinks:
   *    BadSink : Authenticate the user using LogonUserW()
   * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  #ifdef _WIN32
  #include <windows.h>
  #endif
  
  #ifdef _WIN32
  #pragma comment(lib, "advapi32.lib")
  #endif
  
  #ifndef OMITBAD
  
  void CWE591_Sensitive_Data_Storage_in_Improperly_Locked_Memory__w32_wchar_t_66b_bad_sink(wchar_t * password_array[])
  {
      /* copy password out of password_array */
      wchar_t * password = password_array[2];
      {
  #ifdef _WIN32 /* this is WIN32 specific */
          HANDLE pHandle;
          wchar_t * username = L"User";
          wchar_t * domain = L"Domain";
          /* Use the password in LogonUser() to establish that it is "sensitive" */
          if (LogonUserW(
                      username,
                      domain,
                      password,
                      LOGON32_LOGON_NETWORK,
                      LOGON32_PROVIDER_DEFAULT,
                      &pHandle) != 0)
          {
              printLine("User logged in successfully.");
              CloseHandle(pHandle);
          }
          else
          {
              printLine("Unable to login.");
          }
          /* POTENTIAL FLAW: Sensitive data possibly improperly locked */
  #endif
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE591_Sensitive_Data_Storage_in_Improperly_Locked_Memory__w32_wchar_t_66b_goodG2B_sink(wchar_t * password_array[])
  {
      wchar_t * password = password_array[2];
      {
  #ifdef _WIN32 /* this is WIN32 specific */
          HANDLE pHandle;
          wchar_t * username = L"User";
          wchar_t * domain = L"Domain";
          /* Use the password in LogonUser() to establish that it is "sensitive" */
          if (LogonUserW(
                      username,
                      domain,
                      password,
                      LOGON32_LOGON_NETWORK,
                      LOGON32_PROVIDER_DEFAULT,
                      &pHandle) != 0)
          {
              printLine("User logged in successfully.");
              CloseHandle(pHandle);
          }
          else
          {
              printLine("Unable to login.");
          }
          /* POTENTIAL FLAW: Sensitive data possibly improperly locked */
  #endif
      }
  }
  
  #endif /* OMITGOOD */
  