Changeset ce57b1b in nscp


Ignore:
Timestamp:
02/11/08 23:15:02 (5 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
7a156f4
Parents:
1a35b3f
Message:

2008-02-11 MickeM

+ Added encryption support for NSCA module (about half of the algorithms are avalible,

if someone wants to use one not available let me know, and I will try to add it)

Files:
13 edited

Legend:

Unmodified
Added
Removed
  • AutoBuild.h

    rc1d545e rce57b1b  
    33// change the FALSE to TRUE for autoincrement of build number 
    44#define INCREMENT_VERSION TRUE 
    5 #define FILEVER        0,3,0,48 
    6 #define PRODUCTVER     0,3,0,48 
    7 #define STRFILEVER     _T("0.3.0.48") 
    8 #define STRPRODUCTVER  _T("0.3.0.48") 
    9 #define STRPRODUCTDATE  _T("2008-02-08") 
     5#define FILEVER        0,3,0,50 
     6#define PRODUCTVER     0,3,0,50 
     7#define STRFILEVER     _T("0.3.0.50") 
     8#define STRPRODUCTVER  _T("0.3.0.50") 
     9#define STRPRODUCTDATE  _T("2008-02-11") 
    1010#endif // AUTOBUILD_H 
  • NSC.dist

    r1a35b3f rce57b1b  
    130130 
    131131[External Alias] 
    132 alias_cpu=checkCPU warn=80 crit=90 time=5m time=1m time=30s 
    133 alias_disk=CheckDriveSize MinWarn=10% MinCrit=5% CheckAll FilterType=FIXED 
    134 alias_service=checkServiceState CheckAll 
    135 alias_mem=checkMem MaxWarn=80% MaxCrit=90% ShowAll type=physical 
     132;alias_cpu=checkCPU warn=80 crit=90 time=5m time=1m time=30s 
     133;alias_disk=CheckDriveSize MinWarn=10% MinCrit=5% CheckAll FilterType=FIXED 
     134;alias_service=checkServiceState CheckAll 
     135;alias_mem=checkMem MaxWarn=80% MaxCrit=90% ShowAll type=physical 
    136136 
    137137 
     
    232232; 0 = None  (Do NOT use this option) 
    233233; 1 = Simple XOR  (No security, just obfuscation, but very fast) 
    234 ; (no MCRYPT yet, but soon...) 
    235 ;encryption_method=1 
     234;   2 = DES 
     235;   3 = 3DES (Triple DES) 
     236; 4 = CAST-128 
     237; 6 = xTEA 
     238; 8 = BLOWFISH 
     239; 9 = TWOFISH 
     240; 11 = RC2 
     241; 14 = RIJNDAEL-128 (AES) 
     242; 20 = SERPENT 
     243;encryption_method=14 
    236244; 
    237245;# ENCRYPTION PASSWORD 
     
    258266; 
    259267[NSCA Commands] 
    260 my_cpu_check=checkCPU warn=80 crit=90 time=20m time=10s time=4 
    261 my_mem_check=checkMem MaxWarn=80% MaxCrit=90% ShowAll type=page 
    262 my_svc_check=checkServiceState CheckAll exclude=wampmysqld exclude=MpfService 
     268;my_cpu_check=checkCPU warn=80 crit=90 time=20m time=10s time=4 
     269;my_mem_check=checkMem MaxWarn=80% MaxCrit=90% ShowAll type=page 
     270;my_svc_check=checkServiceState CheckAll exclude=wampmysqld exclude=MpfService 
  • NSClient++-2005.sln

    rc1d545e rce57b1b  
    33Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NSClient++", "NSClient++-2005.vcproj", "{2286162D-7571-4735-BAC8-4A8D33A4F42D}" 
    44  ProjectSection(ProjectDependencies) = postProject 
     5    {626EB00E-A4D2-4B02-9BF4-4C655CA2B7E4} = {626EB00E-A4D2-4B02-9BF4-4C655CA2B7E4} 
     6    {8F1C3E39-D6C6-4414-AAD2-FE03C9A8655F} = {8F1C3E39-D6C6-4414-AAD2-FE03C9A8655F} 
     7    {2FCAF54B-AAD3-4F59-895A-8F9CEAFDC65D} = {2FCAF54B-AAD3-4F59-895A-8F9CEAFDC65D} 
     8    {08D6246D-1B4A-47A3-965D-296DCC54A4E8} = {08D6246D-1B4A-47A3-965D-296DCC54A4E8} 
     9    {62B685D7-3A2E-4F3E-B2B8-B17F20C0217F} = {62B685D7-3A2E-4F3E-B2B8-B17F20C0217F} 
     10    {2FF60AF6-09AA-49AB-B414-2E8FD01655C6} = {2FF60AF6-09AA-49AB-B414-2E8FD01655C6} 
     11    {05DE66AC-E55C-43B3-849F-7EC695D8B8D0} = {05DE66AC-E55C-43B3-849F-7EC695D8B8D0} 
     12    {BA246C01-063A-4548-8957-32D5CC76171B} = {BA246C01-063A-4548-8957-32D5CC76171B} 
     13    {E6E588AB-EFEF-481C-9AF7-DCDCB95CFF45} = {E6E588AB-EFEF-481C-9AF7-DCDCB95CFF45} 
    514    {BBFF8362-C626-4838-B0A2-F695D638AD24} = {BBFF8362-C626-4838-B0A2-F695D638AD24} 
    6     {E6E588AB-EFEF-481C-9AF7-DCDCB95CFF45} = {E6E588AB-EFEF-481C-9AF7-DCDCB95CFF45} 
    7     {BA246C01-063A-4548-8957-32D5CC76171B} = {BA246C01-063A-4548-8957-32D5CC76171B} 
    8     {05DE66AC-E55C-43B3-849F-7EC695D8B8D0} = {05DE66AC-E55C-43B3-849F-7EC695D8B8D0} 
    9     {2FF60AF6-09AA-49AB-B414-2E8FD01655C6} = {2FF60AF6-09AA-49AB-B414-2E8FD01655C6} 
    10     {62B685D7-3A2E-4F3E-B2B8-B17F20C0217F} = {62B685D7-3A2E-4F3E-B2B8-B17F20C0217F} 
    11     {08D6246D-1B4A-47A3-965D-296DCC54A4E8} = {08D6246D-1B4A-47A3-965D-296DCC54A4E8} 
    12     {2FCAF54B-AAD3-4F59-895A-8F9CEAFDC65D} = {2FCAF54B-AAD3-4F59-895A-8F9CEAFDC65D} 
    13     {8F1C3E39-D6C6-4414-AAD2-FE03C9A8655F} = {8F1C3E39-D6C6-4414-AAD2-FE03C9A8655F} 
    14     {626EB00E-A4D2-4B02-9BF4-4C655CA2B7E4} = {626EB00E-A4D2-4B02-9BF4-4C655CA2B7E4} 
    1515  EndProjectSection 
    1616EndProject 
     
    3838EndProject 
    3939Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NSCAAgent", "modules\NSCAAgent\NSCAAgent-2005.vcproj", "{6F55C9BF-57F6-4A15-A058-C83A52F539EE}" 
     40  ProjectSection(ProjectDependencies) = postProject 
     41    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A} = {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A} 
     42  EndProjectSection 
    4043EndProject 
    4144Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LUAScript", "modules\LUAScript\LUAScript-2005.vcproj", "{26B84883-BE52-40E6-9BEE-55AD056D5751}" 
     
    4750EndProject 
    4851Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CheckExternalScripts", "modules\CheckExternalScripts\CheckExternalScripts-2005.vcproj", "{0BEEC749-0E3E-4FB2-82DA-AC8D4730A129}" 
     52EndProject 
     53Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Crypt++", "..\Crypt++\Crypt++.vcproj", "{630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}" 
    4954EndProject 
    5055Global 
     
    680685    {0BEEC749-0E3E-4FB2-82DA-AC8D4730A129}.Release|x64.ActiveCfg = Release|x64 
    681686    {0BEEC749-0E3E-4FB2-82DA-AC8D4730A129}.Release|x64.Build.0 = Release|x64 
     687    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug|Any CPU.ActiveCfg = Debug|Win32 
     688    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug|Itanium.ActiveCfg = Debug|Win32 
     689    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32 
     690    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug|Mixed Platforms.Build.0 = Debug|Win32 
     691    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug|Win32.ActiveCfg = Debug|Win32 
     692    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug|Win32.Build.0 = Debug|Win32 
     693    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug|x64.ActiveCfg = Debug|Win32 
     694    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug-MemCheck|Any CPU.ActiveCfg = Debug|Win32 
     695    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug-MemCheck|Itanium.ActiveCfg = Debug|Win32 
     696    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug-MemCheck|Mixed Platforms.ActiveCfg = Debug|Win32 
     697    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug-MemCheck|Mixed Platforms.Build.0 = Debug|Win32 
     698    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug-MemCheck|Win32.ActiveCfg = Debug|Win32 
     699    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug-MemCheck|Win32.Build.0 = Debug|Win32 
     700    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Debug-MemCheck|x64.ActiveCfg = Debug|Win32 
     701    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Distribution|Any CPU.ActiveCfg = Debug|Win32 
     702    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Distribution|Itanium.ActiveCfg = Debug|Win32 
     703    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Distribution|Mixed Platforms.ActiveCfg = Debug|Win32 
     704    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Distribution|Mixed Platforms.Build.0 = Debug|Win32 
     705    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Distribution|Win32.ActiveCfg = Debug|Win32 
     706    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Distribution|Win32.Build.0 = Debug|Win32 
     707    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Distribution|x64.ActiveCfg = Debug|Win32 
     708    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Nightly|Any CPU.ActiveCfg = Release|Win32 
     709    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Nightly|Itanium.ActiveCfg = Release|Win32 
     710    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Nightly|Mixed Platforms.ActiveCfg = Release|Win32 
     711    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Nightly|Mixed Platforms.Build.0 = Release|Win32 
     712    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Nightly|Win32.ActiveCfg = Nightly|Win32 
     713    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Nightly|Win32.Build.0 = Nightly|Win32 
     714    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Nightly|x64.ActiveCfg = Release|Win32 
     715    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|Any CPU.ActiveCfg = Release|Win32 
     716    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|Itanium.ActiveCfg = Release|Win32 
     717    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|Mixed Platforms.ActiveCfg = Release|Win32 
     718    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|Mixed Platforms.Build.0 = Release|Win32 
     719    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|Win32.ActiveCfg = Release|Win32 
     720    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|Win32.Build.0 = Release|Win32 
     721    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|x64.ActiveCfg = Release|Win32 
    682722  EndGlobalSection 
    683723  GlobalSection(SolutionProperties) = preSolution 
  • NSClient++.cpp

    rc1d545e rce57b1b  
    2828////////////////////////////////////////////////////////////////////////// 
    2929// Startup code 
     30 
    3031 
    3132/** 
  • changelog

    rc1d545e rce57b1b  
    55 * Add module for relaying events 
    66 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
     7 
     82008-02-11 MickeM 
     9 + Added encryption support for NSCA module (about half of the algorithms are avalible,  
     10     if someone wants to use one not available let me know, and I will try to add it) 
    711 
    8122008-02-09 MickeM 
  • include/Socket.h

    r035c51f rce57b1b  
    5656    } 
    5757    DataBuffer(const char* buffer, unsigned int length) { 
     58      buffer_ = new char[length+2]; 
     59      memcpy(buffer_, buffer, length+1); 
     60      length_ = length; 
     61    } 
     62    DataBuffer(const unsigned char* buffer, unsigned int length) { 
    5863      buffer_ = new char[length+2]; 
    5964      memcpy(buffer_, buffer, length+1); 
  • include/utils.cpp

    rdc65e35 rce57b1b  
    5757} 
    5858 
     59unsigned long calculate_crc32(const unsigned char *buffer, int buffer_size){ 
     60  if (!hascrc32) 
     61    generate_crc32_table(); 
     62  register unsigned long crc; 
     63  int this_char; 
     64  int current_index; 
    5965 
     66  crc=0xFFFFFFFF; 
     67 
     68  for(current_index=0;current_index<buffer_size;current_index++){ 
     69    this_char=(int)buffer[current_index]; 
     70    crc=((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc ^ this_char) & 0xFF]; 
     71  } 
     72 
     73  return (crc ^ 0xFFFFFFFF); 
     74} 
     75 
     76 
  • include/utils.h

    r99e4d8f rce57b1b  
    2626void generate_crc32_table(void); 
    2727unsigned long calculate_crc32(const char *buffer, int buffer_size); 
     28unsigned long calculate_crc32(const unsigned char *buffer, int buffer_size); 
    2829 
    2930 
  • modules/CheckExternalScripts/CheckExternalScripts.cpp

    rc1d545e rce57b1b  
    124124 
    125125    for (;cit2!=arr.end();cit2++,i++) { 
    126       if (NSCModuleHelper::getSettingsInt(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_ALLOW_NASTY_META, EXTSCRIPT_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) { 
     126      if (isAlias || NSCModuleHelper::getSettingsInt(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_ALLOW_NASTY_META, EXTSCRIPT_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) { 
    127127        if ((*cit2).find_first_of(NASTY_METACHARS) != std::wstring::npos) { 
    128128          NSC_LOG_ERROR(_T("Request string contained illegal metachars!")); 
     
    136136    return NSCModuleHelper::InjectSplitAndCommand(cd.command, cd.arguments, ' ', message, perf, true); 
    137137  } else { 
    138     return executeNRPECommand(args, message, perf); 
     138    return executeNRPECommand(cd.command + _T(" ") + args, message, perf); 
    139139    /* 
    140140  } else if (cd.type == script_dir) { 
     
    168168  sec.lpSecurityDescriptor = NULL; 
    169169 
     170  // CreateProcess doesn't work with a const command 
     171  TCHAR *cmd = new TCHAR[command.length()+1]; 
     172  if (cmd == NULL) { 
     173    NSC_LOG_ERROR(_T("Failed to allocate memory for command buffer (") + command + _T(").")); 
     174    return NSCAPI::returnUNKNOWN; 
     175  } 
     176  wcsncpy_s(cmd, command.length()+1, command.c_str(), command.length()); 
     177  cmd[command.length()] = 0; 
     178  std::wstring root = NSCModuleHelper::getBasePath(); 
     179 
    170180  // Create Pipes 
    171   CreatePipe(&hChildInR, &hChildInW, &sec, 0); 
    172   CreatePipe(&hChildOutR, &hChildOutW, &sec, 0); 
     181  if (!CreatePipe(&hChildInR, &hChildInW, &sec, 0)) { 
     182    NSC_LOG_ERROR(_T("Failed to create pipe for (") + command + _T(") return code: ") + error::lookup::last_error()); 
     183    return NSCAPI::returnUNKNOWN; 
     184  } 
     185  if (!CreatePipe(&hChildOutR, &hChildOutW, &sec, 0)) { 
     186    NSC_LOG_ERROR(_T("Failed to create pipe for (") + command + _T(") return code: ") + error::lookup::last_error()); 
     187    return NSCAPI::returnUNKNOWN; 
     188  } 
    173189 
    174190  // Set up members of STARTUPINFO structure.  
     
    181197  si.hStdError = hChildOutW; 
    182198  si.wShowWindow = SW_HIDE; 
    183  
    184  
    185   // CreateProcess doesn't work with a const command 
    186   TCHAR *cmd = new TCHAR[command.length()+1]; 
    187   wcsncpy_s(cmd, command.length()+1, command.c_str(), command.length()); 
    188   cmd[command.length()] = 0; 
    189   std::wstring root = NSCModuleHelper::getBasePath(); 
    190199 
    191200  // Create the child process.  
  • modules/NSCAAgent/NSCAAgent-2005.vcproj

    r035c51f rce57b1b  
    6969      <Tool 
    7070        Name="VCLinkerTool" 
    71         AdditionalDependencies="ws2_32.lib" 
     71        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    7272        OutputFile="../../Debug/modules/$(ProjectName).dll" 
    7373        LinkIncremental="2" 
     74        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    7475        ModuleDefinitionFile="NSCAAgent.def" 
    7576        GenerateDebugInformation="true" 
    7677        ProgramDatabaseFile="$(OutDir)/NSCAAgent.pdb" 
    7778        SubSystem="2" 
    78         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     79        ImportLibrary="$(OutDir)/NRPEListener.lib" 
    7980        TargetMachine="1" 
    8081      /> 
     
    152153      <Tool 
    153154        Name="VCLinkerTool" 
    154         AdditionalDependencies="ws2_32.lib" 
     155        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    155156        OutputFile="$(OutDir)\$(ProjectName).dll" 
    156157        LinkIncremental="2" 
     158        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    157159        ModuleDefinitionFile="NSCAAgent.def" 
    158160        GenerateDebugInformation="true" 
    159161        ProgramDatabaseFile="$(OutDir)/NSCAAgent.pdb" 
    160162        SubSystem="2" 
    161         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     163        ImportLibrary="$(OutDir)/NRPEListener.lib" 
    162164        TargetMachine="17" 
    163165      /> 
     
    231233      <Tool 
    232234        Name="VCLinkerTool" 
    233         AdditionalDependencies="ws2_32.lib" 
     235        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    234236        OutputFile="$(OutDir)\$(ProjectName).dll" 
    235237        LinkIncremental="1" 
     238        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    236239        ModuleDefinitionFile="NSCAAgent.def" 
    237240        GenerateDebugInformation="false" 
     
    239242        OptimizeReferences="2" 
    240243        EnableCOMDATFolding="2" 
    241         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     244        ImportLibrary="$(TargetDir)$(TargetName).lib" 
    242245        TargetMachine="1" 
    243246      /> 
     
    312315      <Tool 
    313316        Name="VCLinkerTool" 
    314         AdditionalDependencies="ws2_32.lib" 
     317        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    315318        OutputFile="$(OutDir)\$(ProjectName).dll" 
    316319        LinkIncremental="1" 
     320        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    317321        ModuleDefinitionFile="NSCAAgent.def" 
    318322        GenerateDebugInformation="false" 
     
    320324        OptimizeReferences="2" 
    321325        EnableCOMDATFolding="2" 
    322         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     326        ImportLibrary="$(TargetDir)$(TargetName).lib" 
    323327        TargetMachine="17" 
    324328      /> 
     
    392396      <Tool 
    393397        Name="VCLinkerTool" 
    394         AdditionalDependencies="ws2_32.lib" 
     398        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    395399        OutputFile="$(OutDir)\$(ProjectName).dll" 
    396400        LinkIncremental="1" 
     401        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    397402        ModuleDefinitionFile="NSCAAgent.def" 
    398403        GenerateDebugInformation="false" 
     
    400405        OptimizeReferences="2" 
    401406        EnableCOMDATFolding="2" 
    402         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     407        ImportLibrary="$(TargetDir)$(TargetName).lib" 
    403408        TargetMachine="1" 
    404409      /> 
     
    473478      <Tool 
    474479        Name="VCLinkerTool" 
    475         AdditionalDependencies="ws2_32.lib" 
     480        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    476481        OutputFile="$(OutDir)\$(ProjectName).dll" 
    477482        LinkIncremental="1" 
     483        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    478484        ModuleDefinitionFile="NSCAAgent.def" 
    479485        GenerateDebugInformation="false" 
     
    481487        OptimizeReferences="2" 
    482488        EnableCOMDATFolding="2" 
    483         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     489        ImportLibrary="$(TargetDir)$(TargetName).lib" 
    484490        TargetMachine="17" 
    485491      /> 
     
    553559      <Tool 
    554560        Name="VCLinkerTool" 
    555         AdditionalDependencies="ws2_32.lib" 
     561        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    556562        OutputFile="$(OutDir)\$(ProjectName).dll" 
    557563        LinkIncremental="1" 
     564        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    558565        ModuleDefinitionFile="NSCAAgent.def" 
    559566        GenerateDebugInformation="false" 
     
    561568        OptimizeReferences="2" 
    562569        EnableCOMDATFolding="2" 
    563         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     570        ImportLibrary="" 
    564571        TargetMachine="1" 
    565572      /> 
     
    634641      <Tool 
    635642        Name="VCLinkerTool" 
    636         AdditionalDependencies="ws2_32.lib" 
     643        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    637644        OutputFile="$(OutDir)\$(ProjectName).dll" 
    638645        LinkIncremental="1" 
     646        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    639647        ModuleDefinitionFile="NSCAAgent.def" 
    640648        GenerateDebugInformation="false" 
     
    642650        OptimizeReferences="2" 
    643651        EnableCOMDATFolding="2" 
    644         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     652        ImportLibrary="$(TargetDir)$(TargetName).lib" 
    645653        TargetMachine="17" 
    646654      /> 
     
    717725      <Tool 
    718726        Name="VCLinkerTool" 
    719         AdditionalDependencies="ws2_32.lib" 
     727        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    720728        OutputFile="$(OutDir)\$(ProjectName).dll" 
    721729        LinkIncremental="2" 
     730        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    722731        ModuleDefinitionFile="NSCAAgent.def" 
    723732        GenerateDebugInformation="true" 
    724733        ProgramDatabaseFile="$(OutDir)/NSCAAgent.pdb" 
    725734        SubSystem="2" 
    726         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     735        ImportLibrary="$(TargetDir)$(TargetName).lib" 
    727736        TargetMachine="1" 
    728737      /> 
     
    800809      <Tool 
    801810        Name="VCLinkerTool" 
    802         AdditionalDependencies="ws2_32.lib" 
     811        AdditionalDependencies="ws2_32.lib Crypt++.lib" 
    803812        OutputFile="$(OutDir)\$(ProjectName).dll" 
    804813        LinkIncremental="2" 
     814        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
    805815        ModuleDefinitionFile="NSCAAgent.def" 
    806816        GenerateDebugInformation="true" 
    807817        ProgramDatabaseFile="$(OutDir)/NSCAAgent.pdb" 
    808818        SubSystem="2" 
    809         ImportLibrary="$(OutDir)/NSCAAgent.lib" 
     819        ImportLibrary="$(TargetDir)$(TargetName).lib" 
    810820        TargetMachine="17" 
    811821      /> 
  • modules/NSCAAgent/NSCAThread.cpp

    r035c51f rce57b1b  
    3333    addCommand(*cit); 
    3434  } 
    35   //std::string s = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CPU_BUFFER_TIME, C_SYSTEM_CPU_BUFFER_TIME_DEFAULT); 
    36   //unsigned int i = strEx::stoui_as_time(s, checkIntervall_*100); 
    3735} 
    3836 
     
    143141    NSCAPacket::init_packet_struct *packet_in = (NSCAPacket::init_packet_struct*) inc.getBuffer(); 
    144142    try { 
    145       crypt_inst.encrypt_init(password_.c_str(),encryption_method_,packet_in->iv); 
    146     } catch (nsca_encrypt::exception &e) { 
     143      crypt_inst.encrypt_init(password_.c_str(),encryption_method_,reinterpret_cast<unsigned char*>(packet_in->iv)); 
     144    } catch (nsca_encrypt::encryption_exception &e) { 
    147145      NSC_LOG_ERROR_STD(_T("<<< Failed to initalize encryption header: ") + e.getMessage()); 
    148146      return; 
     
    154152    try { 
    155153      for (std::list<Command::Result>::const_iterator cit = results.begin(); cit != results.end(); ++cit) { 
    156         //NSC_DEBUG_MSG_STD(_T("Sending : ") + (*cit).toString()); 
    157154        socket.send((*cit).getBuffer(crypt_inst)); 
    158155      } 
    159     } catch (nsca_encrypt::exception &e) { 
     156    } catch (nsca_encrypt::encryption_exception &e) { 
    160157      NSC_LOG_ERROR_STD(_T("<<< Failed to encrypt packet: ") + e.getMessage()); 
    161158      return; 
  • modules/NSCAAgent/NSCAThread.h

    r035c51f rce57b1b  
    159159      std::string h = strEx::wstring_to_string(host); 
    160160 
    161       NSCAPacket::data_packet data; 
    162       data.packet_version=static_cast<NSCAPacket::int16_t>(htons(NSCA_PACKET_VERSION_3)); 
    163       data.timestamp=static_cast<NSCAPacket::u_int32_t>(htonl(time)); 
    164       data.return_code = code; 
    165       data.crc32_value=static_cast<NSCAPacket::u_int32_t>(0L); 
    166  
    167       strncpy_s(data.host_name, NSCA_MAX_HOSTNAME_LENGTH, h.c_str(), h.length()); 
    168       strncpy_s(data.svc_description, NSCA_MAX_DESCRIPTION_LENGTH, s.c_str(), s.length()); 
    169       strncpy_s(data.plugin_output, NSCA_MAX_PLUGINOUTPUT_LENGTH, r.c_str(), r.length()); 
    170  
    171       unsigned int calculated_crc32=calculate_crc32(reinterpret_cast<char*>(&data),sizeof(data)); 
    172       data.crc32_value=static_cast<NSCAPacket::u_int32_t>(htonl(calculated_crc32)); 
    173       char * buffer = reinterpret_cast<char*>(&data); 
    174       crypt_inst.encrypt_buffer(buffer, sizeof(data)); 
    175       return simpleSocket::DataBuffer(buffer,sizeof(data)); 
     161      unsigned int buffer_len = sizeof(NSCAPacket::data_packet); 
     162      unsigned char* buffer = crypt_inst.get_rand_buffer(buffer_len); 
     163      NSCAPacket::data_packet *data = reinterpret_cast<NSCAPacket::data_packet*>(buffer); 
     164      data->packet_version=static_cast<NSCAPacket::int16_t>(htons(NSCA_PACKET_VERSION_3)); 
     165      data->timestamp=static_cast<NSCAPacket::u_int32_t>(htonl(time)); 
     166      data->return_code = code; 
     167      data->crc32_value=static_cast<NSCAPacket::u_int32_t>(0L); 
     168 
     169      strncpy_s(data->host_name, NSCA_MAX_HOSTNAME_LENGTH, h.c_str(), h.length()); 
     170      strncpy_s(data->svc_description, NSCA_MAX_DESCRIPTION_LENGTH, s.c_str(), s.length()); 
     171      strncpy_s(data->plugin_output, NSCA_MAX_PLUGINOUTPUT_LENGTH, r.c_str(), r.length()); 
     172 
     173      unsigned int calculated_crc32=calculate_crc32(buffer,buffer_len); 
     174      data->crc32_value=static_cast<NSCAPacket::u_int32_t>(htonl(calculated_crc32)); 
     175      crypt_inst.encrypt_buffer(buffer, buffer_len); 
     176      simpleSocket::DataBuffer ret(buffer,buffer_len); 
     177      crypt_inst.destroy_random_buffer(buffer); 
     178      return ret; 
    176179    } 
    177180 
  • modules/NSCAAgent/nsca_enrypt.hpp

    r035c51f rce57b1b  
     1#define HAVE_LIBCRYPTOPP 
     2 
     3#ifdef HAVE_LIBCRYPTOPP 
     4#include <crypto++/cryptlib.h> 
     5#include <crypto++/modes.h> 
     6#include <crypto++/des.h> 
     7#include <crypto++/aes.h> 
     8#include <crypto++/cast.h> 
     9#include <crypto++/tea.h> 
     10#include <crypto++/3way.h> 
     11#include <crypto++/blowfish.h> 
     12#include <crypto++/twofish.h> 
     13#include <crypto++/rc2.h> 
     14#include <crypto++/arc4.h> 
     15#include <crypto++/serpent.h> 
     16#include <crypto++/gost.h> 
     17#include <crypto++/filters.h> 
     18#include <crypto++/osrng.h> 
     19#endif 
    120 
    221#define TRANSMITTED_IV_SIZE     128     /* size of IV to transmit - must be as big as largest IV needed for any crypto algorithm */ 
     
    726#define ENCRYPT_XOR             1       /* not really encrypted, just obfuscated */ 
    827 
    9 #ifdef HAVE_LIBMCRYPT 
     28#ifdef HAVE_LIBCRYPTOPP 
    1029#define ENCRYPT_DES             2       /* DES */ 
    1130#define ENCRYPT_3DES            3       /* 3DES or Triple DES */ 
     
    3756 
    3857class nsca_encrypt { 
     58public: 
     59  class encryption_exception { 
     60    std::wstring msg_; 
     61  public: 
     62    encryption_exception() {} 
     63    encryption_exception(std::wstring msg) : msg_(msg) {} 
     64    std::wstring getMessage() const { return msg_; } 
     65 
     66  }; 
     67  class any_encryption { 
     68  public: 
     69    virtual void init(std::string password, unsigned char *transmitted_iv, int iv_size) = 0; 
     70    virtual void encrypt(unsigned char *buffer, int buffer_size) = 0; 
     71    virtual void decrypt(unsigned char *buffer, int buffer_size) = 0; 
     72  }; 
     73  template <class TMethod> 
     74  class cryptopp_encryption : public any_encryption { 
     75  private: 
     76    typedef CryptoPP::CFB_Mode_ExternalCipher::Encryption TEncryption; 
     77    typedef typename TMethod::Encryption TCipher; 
     78    TEncryption crypto_; 
     79    TCipher cipher_; 
     80    int keysize_; 
     81  public: 
     82    cryptopp_encryption() : keysize_(TMethod::DEFAULT_KEYLENGTH) {} 
     83    cryptopp_encryption(int keysize) : keysize_(keysize) {} 
     84    int get_keySize() { 
     85      return keysize_; 
     86    } 
     87    int get_blockSize() { 
     88      return TMethod::BLOCKSIZE; 
     89    } 
     90 
     91    void init(std::string password, unsigned char *transmitted_iv, int iv_size) { 
     92      /* generate an encryption/description key using the password */ 
     93      int keysize=get_keySize(); 
     94      std::cout << "keysize: " << keysize << std::endl; 
     95 
     96      unsigned char *key = new unsigned char[keysize+1]; 
     97      if (key == NULL){ 
     98        throw encryption_exception(_T("Could not allocate memory for encryption/decryption key")); 
     99      } 
     100      ZeroMemory(key,keysize*sizeof(unsigned char)); 
     101      strncpy(reinterpret_cast<char*>(key),password.c_str(),min(keysize,password.length())); 
     102 
     103 
     104      /* determine size of IV buffer for this algorithm */ 
     105      int blocksize = get_blockSize(); 
     106      if(blocksize>iv_size){ 
     107        throw encryption_exception(_T("IV size for crypto algorithm exceeds limits")); 
     108      } 
     109 
     110      /* allocate memory for IV buffer */ 
     111      unsigned char *iv = new unsigned char[blocksize+1]; 
     112      if (iv == NULL){ 
     113        throw encryption_exception(_T("Could not allocate memory for IV buffer")); 
     114      } 
     115 
     116      /* fill IV buffer with first bytes of IV that is going to be used to crypt (determined by server) */ 
     117      memcpy(iv, transmitted_iv, sizeof(unsigned char)*blocksize); 
     118 
     119      try { 
     120        cipher_.SetKey(key, keysize); 
     121        crypto_.SetCipherWithIV(cipher_, iv, 1); 
     122      } catch (...) { 
     123        throw encryption_exception(_T("Unknown exception when trying to setup crypto")); 
     124      } 
     125      delete [] iv; 
     126      delete [] key; 
     127    } 
     128    void encrypt(unsigned char *buffer, int buffer_size) { 
     129      /* encrypt each byte of buffer, one byte at a time (CFB mode) */ 
     130      try { 
     131        for(int x=0;x<buffer_size;x++) 
     132          crypto_.ProcessData(&buffer[x], &buffer[x], 1); 
     133      } catch (...) { 
     134        throw encryption_exception(_T("Unknown exception when trying to setup crypto")); 
     135      } 
     136    } 
     137    void decrypt(unsigned char *buffer, int buffer_size) { 
     138      throw encryption_exception(_T("Decryption not supported")); 
     139    } 
     140 
     141  }; 
     142  class no_encryption : public any_encryption { 
     143  public: 
     144    static int get_keySize() { 
     145      return 0; 
     146    } 
     147    static int get_blockSize() { 
     148      return 1; 
     149    } 
     150    void init(std::string password, unsigned char *transmitted_iv, int iv_size) {} 
     151    void encrypt(unsigned char *buffer, int buffer_size) {} 
     152    void decrypt(unsigned char *buffer, int buffer_size) {} 
     153  }; 
     154  class xor_encryption : public any_encryption { 
     155  private: 
     156    int password_size_; 
     157    int iv_size_; 
     158    unsigned char* transmitted_iv_; 
     159    unsigned char* password_; 
     160  public: 
     161    xor_encryption() : transmitted_iv_(NULL), password_(NULL) {} 
     162    ~xor_encryption() { 
     163      delete [] password_; 
     164      delete [] transmitted_iv_; 
     165    } 
     166    static int get_keySize() { 
     167      return 0; 
     168    } 
     169    static int get_blockSize() { 
     170      return 1; 
     171    } 
     172    void init(std::string password, unsigned char *transmitted_iv, int iv_size) { 
     173      iv_size_ = iv_size; 
     174      delete [] transmitted_iv_; 
     175      transmitted_iv_ = new unsigned char[iv_size_+1]; 
     176      if (transmitted_iv_ == NULL) 
     177        throw encryption_exception(_T("Failed to allocate memory for iv")); 
     178      memcpy(transmitted_iv_, transmitted_iv, sizeof(unsigned char)*iv_size_); 
     179 
     180      password_size_ = password.length(); 
     181      delete [] password_; 
     182      password_ = new unsigned char[password_size_+1]; 
     183      if (password_ == NULL) 
     184        throw encryption_exception(_T("Failed to allocate memory for password")); 
     185      memcpy(password_, password.c_str(), sizeof(unsigned char)*password_size_); 
     186 
     187    } 
     188    void encrypt(unsigned char *buffer, int buffer_size) { 
     189      /* rotate over IV we received from the server... */ 
     190      for (int y=0,x=0;y<buffer_size;y++,x++) { 
     191        /* keep rotating over IV */ 
     192        if (x >= iv_size_) 
     193          x = 0; 
     194        buffer[y] ^= transmitted_iv_[x]; 
     195      } 
     196 
     197      /* rotate over password... */ 
     198      for(int y=0,x=0; y < buffer_size; y++,x++) { 
     199        /* keep rotating over password */ 
     200        if (x >= password_size_) 
     201          x = 0; 
     202        buffer[y] ^= password_[x]; 
     203      } 
     204      return; 
     205    } 
     206    void decrypt(unsigned char *buffer, int buffer_size) { 
     207      throw encryption_exception(_T("Decryption not supported")); 
     208    } 
     209  }; 
     210 
    39211private: 
    40   char transmitted_iv_[TRANSMITTED_IV_SIZE]; 
    41   bool isInialized_; 
    42   std::string password_; 
    43   int encryption_method_; 
    44 #ifdef HAVE_LIBMCRYPT 
    45   MCRYPT td_; 
    46   char *key_; 
    47   char *IV_; 
    48   char block_buffer_; 
    49   int blocksize_; 
    50   int keysize_; 
    51   std::string mcrypt_algorithm_; 
    52   std::string mcrypt_mode_; 
    53 #endif 
     212  any_encryption *core_; 
    54213public: 
    55   class exception { 
    56     std::wstring error_; 
    57   public: 
    58     exception(std::wstring error) : error_(error) {} 
    59     std::wstring getMessage() const { return error_; } 
    60  
    61   }; 
    62  
    63   nsca_encrypt() : isInialized_(false) 
    64 #ifdef HAVE_LIBMCRYPT 
    65     , key_(NULL), IV_(NULL)  
    66 #endif 
    67   {} 
     214 
     215  nsca_encrypt() : core_(NULL) {} 
    68216  ~nsca_encrypt() { 
    69 #ifdef HAVE_LIBMCRYPT 
    70     /* mcrypt cleanup */ 
    71     if(encryption_method!=ENCRYPT_NONE && encryption_method!=ENCRYPT_XOR){ 
    72       mcrypt_generic_end(td); 
    73       delete [] key; 
    74       key=NULL; 
    75       delete [] IV; 
    76       IV=NULL; 
    77     } 
    78 #endif 
     217    delete core_; 
    79218  } 
    80219 
     
    84223      case ENCRYPT_NONE: 
    85224      case ENCRYPT_XOR: 
    86 #ifdef HAVE_LIBMCRYPT 
     225#ifdef HAVE_LIBCRYPTOPP 
    87226      case ENCRYPT_DES: 
    88227      case ENCRYPT_3DES: 
    89228      case ENCRYPT_CAST128: 
    90       case ENCRYPT_CAST256: 
    91229      case ENCRYPT_XTEA: 
    92       case ENCRYPT_3WAY: 
    93230      case ENCRYPT_BLOWFISH: 
    94231      case ENCRYPT_TWOFISH: 
     232      case ENCRYPT_RC2: 
     233      case ENCRYPT_RIJNDAEL128: 
     234      case ENCRYPT_SERPENT: 
     235      case ENCRYPT_GOST: 
     236#endif 
     237        return true; 
     238 
     239// UNdefined 
     240      case ENCRYPT_3WAY: 
     241      case ENCRYPT_ARCFOUR: 
     242      case ENCRYPT_CAST256: 
    95243      case ENCRYPT_LOKI97: 
    96       case ENCRYPT_RC2: 
    97       case ENCRYPT_ARCFOUR: 
    98       case ENCRYPT_RIJNDAEL128: 
     244      case ENCRYPT_WAKE: 
     245      case ENCRYPT_ENIGMA: 
    99246      case ENCRYPT_RIJNDAEL192: 
    100247      case ENCRYPT_RIJNDAEL256: 
    101       case ENCRYPT_WAKE: 
    102       case ENCRYPT_SERPENT: 
    103       case ENCRYPT_ENIGMA: 
    104       case ENCRYPT_GOST: 
    105248      case ENCRYPT_SAFER64: 
    106249      case ENCRYPT_SAFER128: 
    107250      case ENCRYPT_SAFERPLUS: 
    108 #endif 
    109         return true; 
    110251      default: 
    111252        return false; 
     
    113254  } 
    114255 
    115   static void generate_transmitted_iv(char *transmitted_iv){ 
     256 
     257  static any_encryption* get_encryption_core(int encryption_method) { 
     258    switch(encryption_method) { 
     259  case ENCRYPT_NONE: 
     260    return new no_encryption(); 
     261  case ENCRYPT_XOR: 
     262    return new xor_encryption(); 
     263#ifdef HAVE_LIBCRYPTOPP 
     264  case ENCRYPT_DES: 
     265    return new cryptopp_encryption<CryptoPP::DES>(); 
     266  case ENCRYPT_3DES: 
     267    return new cryptopp_encryption<CryptoPP::DES_EDE3>(); 
     268  case ENCRYPT_CAST128: 
     269    return new cryptopp_encryption<CryptoPP::CAST128>(); 
     270  case ENCRYPT_XTEA: 
     271    return new cryptopp_encryption<CryptoPP::XTEA>(); 
     272  case ENCRYPT_3WAY: 
     273    return new cryptopp_encryption<CryptoPP::ThreeWay>(); 
     274  case ENCRYPT_BLOWFISH: 
     275    return new cryptopp_encryption<CryptoPP::Blowfish>(56); 
     276  case ENCRYPT_TWOFISH: 
     277    return new cryptopp_encryption<CryptoPP::Twofish>(32); 
     278  case ENCRYPT_RC2: 
     279    return new cryptopp_encryption<CryptoPP::RC2>(128); 
     280  case ENCRYPT_RIJNDAEL128: 
     281    return new cryptopp_encryption<CryptoPP::AES>(32); 
     282  case ENCRYPT_SERPENT: 
     283    return new cryptopp_encryption<CryptoPP::Serpent>(32); 
     284  case ENCRYPT_GOST: 
     285    return new cryptopp_encryption<CryptoPP::GOST>(); 
     286#endif 
     287  default: 
     288    return NULL; 
     289    } 
     290  } 
     291  static void generate_transmitted_iv(unsigned char *transmitted_iv){ 
    116292    int x; 
    117293    int seed=0; 
     
    121297    /*********************************************************/ 
    122298 
    123     /* else fallback to using the current time as the seed */ 
     299    /* else fall back to using the current time as the seed */ 
    124300    seed=(int)time(NULL); 
    125301 
     
    132308  } 
    133309 
    134  
    135  
    136310  /* initializes encryption routines */ 
    137   void encrypt_init(std::string password, int encryption_method, char *received_iv){ 
    138 #ifdef HAVE_LIBMCRYPT 
    139     int i; 
    140     int iv_size; 
    141 #endif 
    142     if (isInialized_) 
    143       throw exception(_T("already iniatilized!")); 
    144     encryption_method_ = encryption_method; 
    145     password_ = password; 
    146     isInialized_ = true; 
     311  void encrypt_init(std::string password, int encryption_method, unsigned char *received_iv){ 
     312    delete core_; 
     313    core_ = get_encryption_core(encryption_method); 
     314    if (core_ == NULL) 
     315      throw encryption_exception(_T("Failed to get encryption core!")); 
    147316 
    148317    /* server generates IV used for encryption */ 
    149     if(received_iv==NULL) 
    150       generate_transmitted_iv(transmitted_iv_); 
    151  
    152     /* client receives IV from server */ 
    153     else 
    154       memcpy(transmitted_iv_,received_iv,TRANSMITTED_IV_SIZE); 
    155  
    156 #ifdef HAVE_LIBMCRYPT 
    157     blocksize=1;                        /* block size = 1 byte w/ CFB mode */ 
    158     keysize=7;                          /* default to 56 bit key length */ 
    159     mcrypt_mode="cfb";                  /* CFB = 8-bit cipher-feedback mode */ 
    160     mcrypt_algorithm="unknown"; 
    161 #endif 
    162  
    163  
    164  
    165     /* get the name of the mcrypt encryption algorithm to use */ 
    166     switch(encryption_method){ 
    167     /* no encryption */ 
    168     case ENCRYPT_NONE: 
    169       return; 
    170       /* XOR or no encryption */ 
    171     case ENCRYPT_XOR: 
    172       return; 
    173 #ifdef HAVE_LIBMCRYPT 
    174     case ENCRYPT_DES: 
    175       mcrypt_algorithm=MCRYPT_DES; 
    176       break; 
    177     case ENCRYPT_3DES: 
    178       mcrypt_algorithm=MCRYPT_3DES; 
    179       break; 
    180     case ENCRYPT_CAST128: 
    181       mcrypt_algorithm=MCRYPT_CAST_128; 
    182       break; 
    183     case ENCRYPT_CAST256: 
    184       mcrypt_algorithm=MCRYPT_CAST_256; 
    185       break; 
    186     case ENCRYPT_XTEA: 
    187       mcrypt_algorithm=MCRYPT_XTEA; 
    188       break; 
    189     case ENCRYPT_3WAY: 
    190       mcrypt_algorithm=MCRYPT_3WAY; 
    191       break; 
    192     case ENCRYPT_BLOWFISH: 
    193       mcrypt_algorithm=MCRYPT_BLOWFISH; 
    194       break; 
    195     case ENCRYPT_TWOFISH: 
    196       mcrypt_algorithm=MCRYPT_TWOFISH; 
    197       break; 
    198     case ENCRYPT_LOKI97: 
    199       mcrypt_algorithm=MCRYPT_LOKI97; 
    200       break; 
    201     case ENCRYPT_RC2: 
    202       mcrypt_algorithm=MCRYPT_RC2; 
    203       break; 
    204     case ENCRYPT_ARCFOUR: 
    205       mcrypt_algorithm=MCRYPT_ARCFOUR; 
    206       break; 
    207     case ENCRYPT_RIJNDAEL128: 
    208       mcrypt_algorithm=MCRYPT_RIJNDAEL_128; 
    209       break; 
    210     case ENCRYPT_RIJNDAEL192: 
    211       mcrypt_algorithm=MCRYPT_RIJNDAEL_192; 
    212       break; 
    213     case ENCRYPT_RIJNDAEL256: 
    214       mcrypt_algorithm=MCRYPT_RIJNDAEL_256; 
    215       break; 
    216     case ENCRYPT_WAKE: 
    217       mcrypt_algorithm=MCRYPT_WAKE; 
    218       break; 
    219     case ENCRYPT_SERPENT: 
    220       mcrypt_algorithm=MCRYPT_SERPENT; 
    221       break; 
    222     case ENCRYPT_ENIGMA: 
    223       mcrypt_algorithm=MCRYPT_ENIGMA; 
    224       break; 
    225     case ENCRYPT_GOST: 
    226       mcrypt_algorithm=MCRYPT_GOST; 
    227       break; 
    228     case ENCRYPT_SAFER64: 
    229       mcrypt_algorithm=MCRYPT_SAFER_SK64; 
    230       break; 
    231     case ENCRYPT_SAFER128: 
    232       mcrypt_algorithm=MCRYPT_SAFER_SK128; 
    233       break; 
    234     case ENCRYPT_SAFERPLUS: 
    235       mcrypt_algorithm=MCRYPT_SAFERPLUS; 
    236       break; 
    237 #endif 
    238     default: 
    239       throw exception(_T("Invalid encryption algorithm!")); 
    240     } 
    241  
    242 #ifdef HAVE_LIBMCRYPT 
    243     /* open encryption module */ 
    244     if((td=mcrypt_module_open(mcrypt_algorithm,NULL,mcrypt_mode,NULL))==MCRYPT_FAILED){ 
    245       throw exception(_T("Could not open mcrypt algorithm '") + mcrypt_algorithm + _T("' with mode '") + mcrypt_mode + _T("'")); 
    246     } 
    247  
    248     /* determine size of IV buffer for this algorithm */ 
    249     iv_size=mcrypt_enc_get_iv_size(td); 
    250     if(iv_size>TRANSMITTED_IV_SIZE){ 
    251       throw exception(_T("IV size for crypto algorithm exceeds limits")); 
    252     } 
    253  
    254     /* allocate memory for IV buffer */ 
    255     if((IV=new char[iv_size])==NULL){ 
    256       throw exception(_T("Could not allocate memory for IV buffer")); 
    257     } 
    258  
    259     /* fill IV buffer with first bytes of IV that is going to be used to crypt (determined by server) */ 
    260     for(i=0;i<iv_size;i++) 
    261       IV[i]=transmitted_iv[i]; 
    262  
    263     /* get maximum key size for this algorithm */ 
    264     keysize=mcrypt_enc_get_key_size(td); 
    265  
    266     /* generate an encryption/decription key using the password */ 
    267     if((key=new char[keysize])==NULL){ 
    268       throw exception(_T("Could not allocate memory for encryption/decryption key")); 
    269       return ERROR; 
    270     } 
    271     ZeroMemory(key,keysize); 
    272  
    273     if(keysize<password.length()) 
    274       strncpy(key,password.c_str(),keysize); 
    275     else 
    276       strncpy(key,password.c_str(),password.length()); 
    277  
    278     /* initialize encryption buffers */ 
    279     mcrypt_generic_init(td,key,keysize,IV); 
    280 #endif 
     318    if (received_iv==NULL) { 
     319      unsigned char generated_iv[TRANSMITTED_IV_SIZE]; 
     320      generate_transmitted_iv(generated_iv); 
     321      core_->init(password, generated_iv, TRANSMITTED_IV_SIZE); 
     322    } else  /* client receives IV from server */ 
     323      core_->init(password, received_iv, TRANSMITTED_IV_SIZE); 
    281324  } 
    282325 
    283326  /* encrypt a buffer */ 
    284   void encrypt_buffer(char *buffer,int buffer_size){ 
    285     int x; 
    286     int y; 
    287     int password_length; 
    288  
    289     /* no crypt instance */ 
    290     if (!isInialized_) 
    291       throw new exception(_T("Not initialized!")); 
    292  
    293     /* no encryption */ 
    294     if(encryption_method_==ENCRYPT_NONE) 
    295       return; 
    296  
    297     /* simple XOR "encryption" - not meant for any real security, just obfuscates data, but its fast... */ 
    298     else if(encryption_method_==ENCRYPT_XOR){ 
    299  
    300       /* rotate over IV we received from the server... */ 
    301       for(y=0,x=0;y<buffer_size;y++,x++){ 
    302  
    303         /* keep rotating over IV */ 
    304         if(x>=TRANSMITTED_IV_SIZE) 
    305           x=0; 
    306  
    307         buffer[y]^=transmitted_iv_[x]; 
    308       } 
    309  
    310       /* rotate over password... */ 
    311       password_length=password_.length(); 
    312       for(y=0,x=0;y<buffer_size;y++,x++){ 
    313         /* keep rotating over password */ 
    314         if(x>=password_length) 
    315           x=0; 
    316         buffer[y]^=password_[x]; 
    317       } 
    318       return; 
    319     } 
    320  
    321 #ifdef HAVE_LIBMCRYPT 
    322     /* use mcrypt routines */ 
    323     else{ 
    324       /* encrypt each byte of buffer, one byte at a time (CFB mode) */ 
    325       for(x=0;x<buffer_size;x++) 
    326         mcrypt_generic(td,&buffer[x],1); 
    327     } 
    328 #endif 
    329     return; 
     327  void encrypt_buffer(unsigned char *buffer,int buffer_size) { 
     328    if (core_ == NULL) 
     329      throw encryption_exception(_T("No encryption core!")); 
     330    core_->encrypt(buffer, buffer_size); 
     331  } 
     332 
     333  unsigned char* get_rand_buffer(int length) { 
     334    CryptoPP::AutoSeededRandomPool rng; 
     335    unsigned char * buffer = new unsigned char[length+1]; 
     336    rng.GenerateBlock(buffer, length); 
     337    return buffer; 
     338  } 
     339  void destroy_random_buffer(unsigned char* buffer) { 
     340    delete [] buffer; 
    330341  } 
    331342 
Note: See TracChangeset for help on using the changeset viewer.