Update of /cvsroot/com0com/hub4com/plugins/crypt
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv9319
Added Files:
.cvsignore filter.cpp precomp.cpp crypt.vcproj precomp.h
Log Message:
Initial revision
--- NEW FILE: precomp.cpp ---
/*
* $Id: precomp.cpp,v 1.1 2008/12/05 14:27:02 vfrolov Exp $
*
* Copyright (c) 2007 Vyacheslav Frolov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
///////////////////////////////////////////////////////////////
#include "precomp.h"
///////////////////////////////////////////////////////////////
--- NEW FILE: precomp.h ---
/*
* $Id: precomp.h,v 1.1 2008/12/05 14:27:02 vfrolov Exp $
*
* Copyright (c) 2008 Vyacheslav Frolov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* $Log: precomp.h,v $
* Revision 1.1 2008/12/05 14:27:02 vfrolov
* Initial revision
*
*/
#ifndef _PRECOMP_H_
#define _PRECOMP_H_
#include <windows.h>
#include <wincrypt.h>
#include <crtdbg.h>
#include <map>
#include <iostream>
using namespace std;
#pragma warning(disable:4512) // assignment operator could not be generated
#endif /* _PRECOMP_H_ */
--- NEW FILE: crypt.vcproj ---
<?xml version="1.0" encoding="windows-1251"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8,00"
Name="filter-crypt"
ProjectGUID="{FEB9BD18-B0EA-460F-8404-C97BD2623227}"
RootNamespace="hub4com"
Keyword="Win32Proj"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
IntermediateDirectory="Debug"
ConfigurationType="2"
UseOfMFC="0"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
UsePrecompiledHeader="2"
PrecompiledHeaderThrough="precomp.h"
PrecompiledHeaderFile="$(IntDir)\precomp.pch"
WarningLevel="4"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="..\..\$(OutDir)\plugins\$(ProjectName).dll"
LinkIncremental="2"
ModuleDefinitionFile="..\plugins.def"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
IntermediateDirectory="Release"
ConfigurationType="2"
UseOfMFC="0"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE"
RuntimeLibrary="0"
UsePrecompiledHeader="2"
PrecompiledHeaderThrough="precomp.h"
PrecompiledHeaderFile="$(IntDir)\precomp.pch"
WarningLevel="4"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="..\..\$(OutDir)\plugins\$(ProjectName).dll"
LinkIncremental="2"
ModuleDefinitionFile="..\plugins.def"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\plugins_api.h"
>
</File>
<File
RelativePath=".\precomp.h"
>
</File>
</Filter>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\filter.cpp"
>
</File>
<File
RelativePath="..\plugins.def"
>
</File>
<File
RelativePath=".\precomp.cpp"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>
--- NEW FILE: filter.cpp ---
/*
* $Id: filter.cpp,v 1.1 2008/12/05 14:27:02 vfrolov Exp $
*
* Copyright (c) 2008 Vyacheslav Frolov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* $Log: filter.cpp,v $
* Revision 1.1 2008/12/05 14:27:02 vfrolov
* Initial revision
*
*/
#include "precomp.h"
#include "../plugins_api.h"
///////////////////////////////////////////////////////////////
namespace FilterCrypt {
///////////////////////////////////////////////////////////////
static ROUTINE_BUF_APPEND *pBufAppend;
static ROUTINE_MSG_REPLACE_BUF *pmsgreplacebuf;
///////////////////////////////////////////////////////////////
#ifndef _DEBUG
#define DEBUG_PARAM(par)
#else /* _DEBUG */
#define DEBUG_PARAM(par) par
#endif /* _DEBUG */
///////////////////////////////////////////////////////////////
static const char *GetParam(const char *pArg, const char *pPattern)
{
size_t lenPattern = strlen(pPattern);
if (_strnicmp(pArg, pPattern, lenPattern) != 0)
return NULL;
return pArg + lenPattern;
}
///////////////////////////////////////////////////////////////
class Valid {
public:
Valid() : isValid(TRUE) {}
void Invalidate() { isValid = FALSE; }
void Validate() { isValid = TRUE; }
BOOL IsValid() const { return isValid; }
private:
BOOL isValid;
};
///////////////////////////////////////////////////////////////
class State : public Valid {
public:
State() { Invalidate(); }
HCRYPTKEY hKeyIn;
HCRYPTKEY hKeyOut;
};
///////////////////////////////////////////////////////////////
class Filter : public Valid {
public:
Filter(int argc, const char *const argv[]);
State *GetState(HMASTERPORT hPort);
void Open(State *pState);
void Close(State *pState);
private:
HCRYPTPROV hProv;
HCRYPTHASH hHash;
typedef map<HMASTERPORT, State*> PortsMap;
typedef pair<HMASTERPORT, State*> PortPair;
PortsMap portsMap;
};
Filter::Filter(int argc, const char *const argv[])
{
BOOL noSecret = TRUE;
for (const char *const *pArgs = &argv[1] ; argc > 1 ; pArgs++, argc--) {
const char *pArg = GetParam(*pArgs, "--");
if (!pArg) {
cerr << "Unknown option " << *pArgs << endl;
Invalidate();
continue;
}
const char *pParam;
if ((pParam = GetParam(pArg, "secret=")) != NULL) {
if (!*pParam)
cerr << "WARNING: The secret is empty" << endl;
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0)) {
DWORD err = GetLastError();
cerr << "CryptAcquireContext() - error=" << err << endl;
Invalidate();
continue;
}
if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
DWORD err = GetLastError();
cerr << "CryptCreateHash() - error=" << err << endl;
Invalidate();
continue;
}
if (!CryptHashData(hHash, (const BYTE *)pParam, (DWORD)strlen(pParam), 0)) {
DWORD err = GetLastError();
cerr << "CryptHashData() - error=" << err << endl;
Invalidate();
continue;
}
//if (!CryptDestroyHash(hHash)) {
// DWORD err = GetLastError();
// cerr << "CryptDestroyHash() - error=" << err << endl;
//}
//if (CryptReleaseContext(hProv, 0)) {
// DWORD err = GetLastError();
// cerr << "CryptReleaseContext() - error=" << err << endl;
//}
noSecret = FALSE;
}
else {
cerr << "Unknown option " << pArg << endl;
Invalidate();
}
}
if (noSecret) {
cerr << "The secret was not set" << endl;
Invalidate();
}
}
State *Filter::GetState(HMASTERPORT hPort)
{
PortsMap::iterator iPair = portsMap.find(hPort);
if (iPair == portsMap.end()) {
portsMap.insert(PortPair(hPort, NULL));
iPair = portsMap.find(hPort);
if (iPair == portsMap.end())
return NULL;
}
if (!iPair->second)
iPair->second = new State();
return iPair->second;
}
void Filter::Open(State *pState)
{
_ASSERTE(!pState->IsValid());
static const DWORD flags = (((DWORD)128) << 16);
if (!CryptDeriveKey(hProv, CALG_RC4, hHash, flags, &pState->hKeyIn)) {
DWORD err = GetLastError();
cerr << "CryptDeriveKey() - error=" << err << endl;
return;
}
if (!CryptDeriveKey(hProv, CALG_RC4, hHash, flags, &pState->hKeyOut)) {
DWORD err = GetLastError();
cerr << "CryptDeriveKey() - error=" << err << endl;
if (!CryptDestroyKey(pState->hKeyIn)) {
DWORD err = GetLastError();
cerr << "CryptDestroyKey() - error=" << err << endl;
}
return;
}
pState->Validate();
}
void Filter::Close(State *pState)
{
if (!pState->IsValid())
return;
pState->Invalidate();
if (!CryptDestroyKey(pState->hKeyIn) || !CryptDestroyKey(pState->hKeyOut)) {
DWORD err = GetLastError();
cerr << "CryptDestroyKey() - error=" << err << endl;
}
}
///////////////////////////////////////////////////////////////
static PLUGIN_TYPE CALLBACK GetPluginType()
{
return PLUGIN_TYPE_FILTER;
}
///////////////////////////////////////////////////////////////
static const PLUGIN_ABOUT_A about = {
sizeof(PLUGIN_ABOUT_A),
"crypt",
"Copyright (c) 2008 Vyacheslav Frolov",
"GNU General Public License",
"Encrypting/decrypting filter",
};
static const PLUGIN_ABOUT_A * CALLBACK GetPluginAbout()
{
return &about;
}
///////////////////////////////////////////////////////////////
static void CALLBACK Help(const char *pProgPath)
{
cerr
<< "Usage:" << endl
<< " " << pProgPath << " ... --create-filter=" << GetPluginAbout()->pName << "[,<FID>][:<options>] ... --add-filters=<ports>:[...,]<FID>[,...] ..." << endl
<< endl
<< "Options:" << endl
<< " --secret=<secret> - set secret (mandatory)." << endl
<< endl
<< "IN method input data stream description:" << endl
<< " LINE_DATA - encrypted data." << endl
<< endl
<< "IN method output data stream description:" << endl
<< " LINE_DATA - decrypted data." << endl
<< endl
<< "OUT method input data stream description:" << endl
<< " LINE_DATA - raw (not encrypted) data." << endl
<< endl
<< "OUT method output data stream description:" << endl
<< " LINE_DATA - encrypted data." << endl
<< endl
<< "Examples:" << endl
;
}
///////////////////////////////////////////////////////////////
static HFILTER CALLBACK Create(
HCONFIG /*hConfig*/,
int argc,
const char *const argv[])
{
Filter *pFilter = new Filter(argc, argv);
if (!pFilter)
return NULL;
if (!pFilter->IsValid()) {
delete pFilter;
return NULL;
}
return (HFILTER)pFilter;
}
///////////////////////////////////////////////////////////////
static BOOL CALLBACK InMethod(
HFILTER hFilter,
HMASTERPORT DEBUG_PARAM(hFromPort),
HUB_MSG *pInMsg,
HUB_MSG **DEBUG_PARAM(ppEchoMsg))
{
_ASSERTE(hFilter != NULL);
_ASSERTE(hFromPort != NULL);
_ASSERTE(pInMsg != NULL);
_ASSERTE(ppEchoMsg != NULL);
_ASSERTE(*ppEchoMsg == NULL);
switch (pInMsg->type) {
case HUB_MSG_TYPE_LINE_DATA: {
_ASSERTE(pInMsg->u.buf.pBuf != NULL || pInMsg->u.buf.size == 0);
DWORD len = pInMsg->u.buf.size;
if (len == 0)
break;
State *pState = ((Filter *)hFilter)->GetState(hFromPort);
if (!pState)
return FALSE;
if (!CryptDecrypt(pState->hKeyIn, 0, FALSE, 0, pInMsg->u.buf.pBuf, &pInMsg->u.buf.size)) {
DWORD err = GetLastError();
cerr << "CryptDecrypt() - error=" << err << endl;
return FALSE;
}
_ASSERTE(pInMsg->u.buf.size == len);
break;
}
case HUB_MSG_TYPE_CONNECT: {
State *pState = ((Filter *)hFilter)->GetState(hFromPort);
if (!pState)
return FALSE;
if (pInMsg->u.val)
((Filter *)hFilter)->Open(pState);
else
((Filter *)hFilter)->Close(pState);
break;
}
}
return TRUE;
}
///////////////////////////////////////////////////////////////
static BOOL CALLBACK OutMethod(
HFILTER hFilter,
HMASTERPORT DEBUG_PARAM(hFromPort),
HMASTERPORT DEBUG_PARAM(hToPort),
HUB_MSG *pOutMsg)
{
_ASSERTE(hFilter != NULL);
_ASSERTE(hFromPort != NULL);
_ASSERTE(hToPort != NULL);
_ASSERTE(pOutMsg != NULL);
switch (pOutMsg->type) {
case HUB_MSG_TYPE_LINE_DATA: {
_ASSERTE(pOutMsg->u.buf.pBuf != NULL || pOutMsg->u.buf.size == 0);
DWORD len = pOutMsg->u.buf.size;
if (len == 0)
break;
State *pState = ((Filter *)hFilter)->GetState(hToPort);
if (!pState)
return FALSE;
if (!CryptEncrypt(pState->hKeyOut, 0, FALSE, 0, pOutMsg->u.buf.pBuf, &pOutMsg->u.buf.size, len)) {
DWORD err = GetLastError();
cerr << "CryptEncrypt() - error=" << err << endl;
return FALSE;
}
_ASSERTE(pOutMsg->u.buf.size == len);
break;
}
}
return pOutMsg != NULL;
}
///////////////////////////////////////////////////////////////
static const FILTER_ROUTINES_A routines = {
sizeof(FILTER_ROUTINES_A),
GetPluginType,
GetPluginAbout,
Help,
NULL, // ConfigStart
NULL, // Config
NULL, // ConfigStop
Create,
NULL, // Init
InMethod,
OutMethod,
};
static const PLUGIN_ROUTINES_A *const plugins[] = {
(const PLUGIN_ROUTINES_A *)&routines,
NULL
};
///////////////////////////////////////////////////////////////
PLUGIN_INIT_A InitA;
const PLUGIN_ROUTINES_A *const * CALLBACK InitA(
const HUB_ROUTINES_A * pHubRoutines)
{
if (!ROUTINE_IS_VALID(pHubRoutines, pBufAppend))
return NULL;
pBufAppend = pHubRoutines->pBufAppend;
return plugins;
}
///////////////////////////////////////////////////////////////
} // end namespace
///////////////////////////////////////////////////////////////
--- NEW FILE: .cvsignore ---
*.user
Release
Debug
|