2012-05-31 18:53:42 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: JRD access method
|
|
|
|
* MODULE: CryptoManager.h
|
|
|
|
* DESCRIPTION: Database encryption
|
|
|
|
*
|
|
|
|
* 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): ______________________________________.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef JRD_CRYPTO_MANAGER
|
|
|
|
#define JRD_CRYPTO_MANAGER
|
|
|
|
|
|
|
|
#include "../common/classes/alloc.h"
|
|
|
|
#include "../common/classes/fb_atomic.h"
|
|
|
|
#include "../common/classes/SyncObject.h"
|
|
|
|
#include "../common/classes/fb_string.h"
|
|
|
|
#include "../common/classes/objects_array.h"
|
|
|
|
#include "../common/classes/stack.h"
|
2012-06-01 09:09:42 +02:00
|
|
|
#include "../common/ThreadStart.h"
|
2012-06-05 12:06:31 +02:00
|
|
|
#include "../jrd/ods.h"
|
2015-03-20 19:02:30 +01:00
|
|
|
#include "../jrd/status.h"
|
2014-09-29 13:03:47 +02:00
|
|
|
#include "firebird/Interface.h"
|
2012-05-31 18:53:42 +02:00
|
|
|
|
|
|
|
// forward
|
|
|
|
|
|
|
|
class Config;
|
|
|
|
|
|
|
|
namespace Ods {
|
|
|
|
struct pag;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Jrd {
|
|
|
|
|
|
|
|
class Database;
|
|
|
|
class Attachment;
|
|
|
|
class jrd_file;
|
|
|
|
class BufferDesc;
|
|
|
|
class thread_db;
|
|
|
|
class Lock;
|
|
|
|
|
|
|
|
class CryptoManager : public Firebird::PermanentStorage
|
|
|
|
{
|
|
|
|
public:
|
2012-08-08 04:20:30 +02:00
|
|
|
explicit CryptoManager(thread_db* tdbb);
|
2012-05-31 18:53:42 +02:00
|
|
|
~CryptoManager();
|
|
|
|
|
2012-06-03 05:00:24 +02:00
|
|
|
void shutdown(thread_db* tdbb);
|
|
|
|
|
2012-05-31 18:53:42 +02:00
|
|
|
void changeCryptState(thread_db* tdbb, const Firebird::string& plugName);
|
|
|
|
void attach(thread_db* tdbb, Attachment* att);
|
|
|
|
void detach(thread_db* tdbb, Attachment* att);
|
|
|
|
|
|
|
|
void startCryptThread(thread_db* tdbb);
|
|
|
|
void terminateCryptThread(thread_db* tdbb);
|
|
|
|
|
2015-03-20 19:02:30 +01:00
|
|
|
bool decrypt(FbStatusVector* sv, Ods::pag* page);
|
|
|
|
Ods::pag* encrypt(FbStatusVector* sv, Ods::pag* from, Ods::pag* to);
|
2012-05-31 18:53:42 +02:00
|
|
|
|
|
|
|
void cryptThread();
|
|
|
|
|
|
|
|
ULONG getCurrentPage();
|
|
|
|
|
2012-06-05 12:06:31 +02:00
|
|
|
class Buffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
operator Ods::pag*()
|
|
|
|
{
|
2015-09-01 09:24:10 +02:00
|
|
|
return reinterpret_cast<Ods::pag*>(FB_ALIGN(buf, PAGE_ALIGNMENT));
|
2012-06-05 12:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-01 09:24:10 +02:00
|
|
|
char buf[MAX_PAGE_SIZE + PAGE_ALIGNMENT - 1];
|
2012-06-05 12:06:31 +02:00
|
|
|
};
|
|
|
|
|
2012-05-31 18:53:42 +02:00
|
|
|
private:
|
|
|
|
class HolderAttachments
|
|
|
|
{
|
|
|
|
public:
|
2012-08-08 04:20:30 +02:00
|
|
|
explicit HolderAttachments(Firebird::MemoryPool& p);
|
2012-05-31 18:53:42 +02:00
|
|
|
~HolderAttachments();
|
|
|
|
|
|
|
|
void registerAttachment(Attachment* att);
|
2012-06-25 15:11:11 +02:00
|
|
|
bool unregisterAttachment(Attachment* att);
|
2012-05-31 18:53:42 +02:00
|
|
|
|
|
|
|
void setPlugin(Firebird::IKeyHolderPlugin* kh);
|
|
|
|
Firebird::IKeyHolderPlugin* getPlugin() const
|
|
|
|
{
|
|
|
|
return keyHolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(Firebird::IKeyHolderPlugin* kh) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Firebird::IKeyHolderPlugin* keyHolder;
|
|
|
|
Firebird::HalfStaticArray<Attachment*, 32> attachments;
|
|
|
|
};
|
|
|
|
|
|
|
|
class KeyHolderPlugins
|
|
|
|
{
|
|
|
|
public:
|
2012-08-08 04:20:30 +02:00
|
|
|
explicit KeyHolderPlugins(Firebird::MemoryPool& p)
|
2012-05-31 18:53:42 +02:00
|
|
|
: knownHolders(p)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void attach(Attachment* att, Config* config);
|
|
|
|
void detach(Attachment* att);
|
|
|
|
void init(Firebird::IDbCryptPlugin* crypt);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Firebird::Mutex holdersMutex;
|
|
|
|
Firebird::ObjectsArray<HolderAttachments> knownHolders;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int blockingAstChangeCryptState(void*);
|
|
|
|
void blockingAstChangeCryptState();
|
|
|
|
|
|
|
|
void takeStateLock(thread_db* tdbb);
|
|
|
|
void loadPlugin(const char* pluginName);
|
|
|
|
ULONG getLastPage(thread_db* tdbb);
|
|
|
|
void writeDbHeader(thread_db* tdbb, ULONG runpage, Firebird::Stack<ULONG>& pages);
|
|
|
|
|
|
|
|
Firebird::AtomicCounter currentPage;
|
2013-09-06 15:09:07 +02:00
|
|
|
Firebird::Mutex pluginLoadMtx, cryptThreadMtx;
|
2012-05-31 18:53:42 +02:00
|
|
|
KeyHolderPlugins keyHolderPlugins;
|
2014-08-15 16:59:38 +02:00
|
|
|
Thread::Handle cryptThreadId;
|
2012-05-31 18:53:42 +02:00
|
|
|
Firebird::IDbCryptPlugin* cryptPlugin;
|
|
|
|
Database& dbb;
|
|
|
|
Lock* stateLock;
|
|
|
|
Lock* threadLock;
|
|
|
|
bool needLock, crypt, process, down;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Jrd
|
|
|
|
|
|
|
|
|
|
|
|
#endif // JRD_CRYPTO_MANAGER
|