8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-22 21:23:04 +01:00
firebird-mirror/examples/dbcrypt/DbCrypt.cpp

297 lines
5.6 KiB
C++
Raw Normal View History

/*
* PROGRAM: Firebird samples.
* MODULE: DbCrypt.cpp
* DESCRIPTION: Sample of how diskcrypt may be written.
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Alex Peshkov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2012 Alex Peshkov <peshkoff at mail.ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
2014-10-16 12:36:23 +02:00
#include <stdint.h>
#include "ibase.h"
2014-09-29 13:03:47 +02:00
#include "firebird/Interface.h"
2014-09-29 13:03:47 +02:00
#include "firebird.h" // Needed for atomic support
#include "../common/classes/fb_atomic.h"
using namespace Firebird;
namespace
{
IMaster* master = NULL;
IPluginManager* pluginManager = NULL;
class PluginModule : public IPluginModuleImpl<PluginModule, CheckStatusWrapper>
{
public:
PluginModule()
: flag(false)
{ }
void registerMe()
{
pluginManager->registerModule(this);
flag = true;
}
~PluginModule()
{
if (flag)
{
pluginManager->unregisterModule(this);
doClean();
}
}
2014-09-29 13:03:47 +02:00
IPluginModule* getModule()
{
return this;
}
2014-09-29 13:03:47 +02:00
void doClean()
{
flag = false;
}
private:
bool flag;
};
PluginModule module;
class DbCrypt : public IDbCryptPluginImpl<DbCrypt, CheckStatusWrapper>
{
public:
2014-09-29 13:03:47 +02:00
explicit DbCrypt(IPluginConfig* cnf) throw()
: config(cnf), key(0), owner(NULL)
{
config->addRef();
}
~DbCrypt()
{
config->release();
}
// ICryptPlugin implementation
void encrypt(CheckStatusWrapper* status, unsigned int length, const void* from, void* to);
void decrypt(CheckStatusWrapper* status, unsigned int length, const void* from, void* to);
void setKey(CheckStatusWrapper* status, unsigned int length, IKeyHolderPlugin** sources,
const char* keyName);
2014-09-29 13:03:47 +02:00
int release()
{
if (--refCounter == 0)
{
delete this;
return 0;
}
return 1;
}
2014-09-29 13:03:47 +02:00
void addRef()
{
++refCounter;
}
2014-09-29 13:03:47 +02:00
IPluginModule* getModule()
{
return &module;
}
2014-09-29 13:03:47 +02:00
void setOwner(IReferenceCounted* o)
{
owner = o;
}
2014-09-29 13:03:47 +02:00
IReferenceCounted* getOwner()
{
return owner;
}
private:
IPluginConfig* config;
char savedKeyName[32];
UCHAR key;
AtomicCounter refCounter;
2014-09-29 13:03:47 +02:00
IReferenceCounted* owner;
void noKeyError(CheckStatusWrapper* status);
};
void DbCrypt::noKeyError(CheckStatusWrapper* status)
{
char msg[100];
strcpy(msg, "Crypt key ");
if (savedKeyName[0])
{
strcat(msg, savedKeyName);
strcat(msg, " ");
}
strcat(msg, "not set");
ISC_STATUS_ARRAY vector;
vector[0] = isc_arg_gds;
vector[1] = isc_random;
vector[2] = isc_arg_string;
2016-02-15 03:53:24 +01:00
vector[3] = (ISC_STATUS) msg;
vector[4] = isc_arg_end;
status->setErrors(vector);
}
void DbCrypt::encrypt(CheckStatusWrapper* status, unsigned int length, const void* from, void* to)
{
status->init();
if (!key)
{
noKeyError(status);
return;
}
const UCHAR* f = static_cast<const UCHAR*>(from);
UCHAR* t = static_cast<UCHAR*>(to);
2012-06-03 05:00:24 +02:00
while (length--)
{
*t++ = (*f++) ^ key;
}
}
void DbCrypt::decrypt(CheckStatusWrapper* status, unsigned int length, const void* from, void* to)
{
status->init();
if (!key)
{
noKeyError(status);
return;
}
const UCHAR* f = static_cast<const UCHAR*>(from);
UCHAR* t = static_cast<UCHAR*>(to);
2012-06-03 05:00:24 +02:00
while (length--)
{
*t++ = (*f++) ^ key;
}
}
void DbCrypt::setKey(CheckStatusWrapper* status, unsigned int length, IKeyHolderPlugin** sources,
const char* keyName)
{
status->init();
if (key != 0)
return;
2016-02-15 03:53:24 +01:00
strncpy(savedKeyName, (keyName ? keyName : ""), sizeof(savedKeyName));
savedKeyName[sizeof(savedKeyName) - 1] = 0;
IConfig* def = config->getDefaultConfig(status);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
return;
IConfigEntry* confEntry = def->find(status, "Auto");
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
{
def->release();
return;
}
if (confEntry)
{
char v = *(confEntry->getValue());
confEntry->release();
if (v == '1' || v == 'y' || v == 'Y' || v == 't' || v == 'T')
{
confEntry = def->find(status, "Value");
def->release();
if (confEntry)
{
v = confEntry->getIntValue();
confEntry->release();
if (v)
{
key = v;
return;
}
}
key = 0x5a;
return;
}
def->release();
}
for (unsigned n = 0; n < length; ++n)
{
ICryptKeyCallback* callback = sources[n]->keyHandle(status, savedKeyName);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
return;
if (callback && callback->callback(0, NULL, 1, &key) == 1)
return;
}
key = 0;
noKeyError(status);
}
class Factory : public IPluginFactoryImpl<Factory, CheckStatusWrapper>
{
public:
2014-09-29 13:03:47 +02:00
IPluginModule* getModule()
{
return &module;
}
IPluginBase* createPlugin(CheckStatusWrapper* status, IPluginConfig* factoryParameter)
{
try
{
DbCrypt* p = new DbCrypt(factoryParameter);
p->addRef();
return p;
}
2014-09-29 13:03:47 +02:00
catch (...)
{
2014-09-29 13:03:47 +02:00
ISC_STATUS st[3] = {isc_arg_gds, isc_virmemexh, isc_arg_end};
status->setErrors(st);
}
return NULL;
}
};
Factory factory;
} // anonymous namespace
2015-11-06 17:43:49 +01:00
extern "C" void FB_DLL_EXPORT FB_PLUGIN_ENTRY_POINT(IMaster* m)
{
master = m;
pluginManager = master->getPluginManager();
module.registerMe();
pluginManager->registerPluginFactory(IPluginManager::TYPE_DB_CRYPT, "DbCrypt_example", &factory);
}