File: CryptKeyHolder.cpp

package info (click to toggle)
firebird3.0 3.0.13.ds7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,632 kB
  • sloc: ansic: 374,403; cpp: 319,973; sql: 14,691; pascal: 14,532; yacc: 7,557; fortran: 5,645; sh: 5,336; makefile: 1,041; perl: 194; sed: 83; awk: 76; xml: 19; csh: 15
file content (292 lines) | stat: -rw-r--r-- 5,902 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 *	PROGRAM:		Firebird samples.
 *	MODULE:			CryptKeyHolder.cpp
 *	DESCRIPTION:	Sample of how key holder 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): ______________________________________.
 */

#include "../interfaces/ifaceExamples.h"

namespace
{

class PluginModule : public IPluginModuleImpl<PluginModule, CheckStatusWrapper>
{
public:
	PluginModule()
		: pluginManager(NULL)
	{ }

	~PluginModule()
	{
		if (pluginManager)
		{
			pluginManager->unregisterModule(this);
			doClean();
		}
	}

	void registerMe(IPluginManager* m)
	{
		pluginManager = m;
		pluginManager->registerModule(this);
	}

	void doClean()
	{
		pluginManager = NULL;
	}

	void threadDetach() {};

private:
	IPluginManager* pluginManager;
};

class CryptKeyHolder : public IKeyHolderPluginImpl<CryptKeyHolder, CheckStatusWrapper>
{
public:
	explicit CryptKeyHolder(IPluginConfig* cnf) throw()
		: callbackInterface(this), named(NULL), config(cnf), key(0), owner(NULL)
	{
		config->addRef();
	}

	~CryptKeyHolder()
	{
		config->release();
	}

	// IKeyHolderPlugin implementation
	int keyCallback(CheckStatusWrapper* status, ICryptKeyCallback* callback);
	ICryptKeyCallback* keyHandle(CheckStatusWrapper* status, const char* keyName);
	ICryptKeyCallback* chainHandle(CheckStatusWrapper* status);

	int release()
	{
		if (--refCounter == 0)
		{
			delete this;
			return 0;
		}
		return 1;
	}

	void addRef()
	{
		++refCounter;
	}

	void setOwner(Firebird::IReferenceCounted* o)
	{
		owner = o;
	}

	IReferenceCounted* getOwner()
	{
		return owner;
	}

	ISC_UCHAR getKey()
	{
		return key;
	}

	FB_BOOLEAN useOnlyOwnKeys(CheckStatusWrapper* status)
	{
		IConfigEntry* e = getEntry(status, "OnlyOwnKey");
		if (!e)
			return FB_TRUE;	// safe default

		FB_BOOLEAN rc = e->getBoolValue();
		e->release();
		return rc;
	}

private:
	class CallbackInterface : public ICryptKeyCallbackImpl<CallbackInterface, CheckStatusWrapper>
	{
	public:
		explicit CallbackInterface(CryptKeyHolder* p)
			: holder(p)
		{ }

		unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
		{
			ISC_UCHAR k = holder->getKey();
			if (!k)
			{
				return 0;
			}

			if (length > 0 && buffer)
			{
				memcpy(buffer, &k, 1);
			}
			return 1;
		}

	private:
		CryptKeyHolder* holder;
	};

	class NamedCallback : public ICryptKeyCallbackImpl<NamedCallback, CheckStatusWrapper>
	{
	public:
		NamedCallback(NamedCallback* n, const char* nm, ISC_UCHAR k)
			: next(n), key(k)
		{
			strncpy(name, nm, sizeof(name));
			name[sizeof(name) - 1] = 0;
		}

		unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
		{
			memcpy(buffer, &key, 1);
			return 1;
		}

		~NamedCallback()
		{
			delete next;
		}

		char name[32];
		NamedCallback* next;
		ISC_UCHAR key;
	};

	CallbackInterface callbackInterface;
	NamedCallback *named;

	IPluginConfig* config;
	ISC_UCHAR key;

	FbSampleAtomic refCounter;
	IReferenceCounted* owner;

	IConfigEntry* getEntry(CheckStatusWrapper* status, const char* entryName);
};

IConfigEntry* CryptKeyHolder::getEntry(CheckStatusWrapper* status, const char* entryName)
{
	IConfig* def = config->getDefaultConfig(status);
	if (status->getState() & Firebird::IStatus::STATE_ERRORS)
		return NULL;

	IConfigEntry* confEntry = def->find(status, entryName);
	def->release();
	if (status->getState() & Firebird::IStatus::STATE_ERRORS)
		return NULL;

	return confEntry;
}

int CryptKeyHolder::keyCallback(CheckStatusWrapper* status, ICryptKeyCallback* callback)
{
	if (key != 0)
		return 1;

	IConfigEntry* confEntry = getEntry(status, "Auto");

	if (confEntry)
	{
		FB_BOOLEAN b = confEntry->getBoolValue();
		confEntry->release();
		if (b)
		{
			key = 0x5a;
			return 1;
		}
	}

	if (callback && callback->callback(0, NULL, 1, &key) != 1)
	{
		key = 0;
		return 0;
	}

	return 1;
}

ICryptKeyCallback* CryptKeyHolder::keyHandle(CheckStatusWrapper* status, const char* keyName)
{
	if (keyName[0] == 0)
		return &callbackInterface;

	for (NamedCallback* n = named; n; n = n->next)
	{
		if (strcmp(keyName, n->name) == 0)
			return n;
	}

	char kn[40];
	strcpy(kn, "Key");
	strncat(kn, keyName, sizeof(kn) - 3 - 1);
	kn[sizeof(kn) - 1] = 0;

	IConfigEntry* confEntry = getEntry(status, kn);
	if (confEntry)
	{
		int k = confEntry->getIntValue();
		confEntry->release();
		if (k > 0 && k < 256)
		{
			named = new NamedCallback(named, keyName, static_cast<ISC_UCHAR>(k));
			return named;
		}
	}

	return NULL;
}

ICryptKeyCallback* CryptKeyHolder::chainHandle(CheckStatusWrapper* status)
{
	return &callbackInterface;
}


class Factory : public IPluginFactoryImpl<Factory, CheckStatusWrapper>
{
public:
	IPluginBase* createPlugin(CheckStatusWrapper* status, IPluginConfig* factoryParameter)
	{
		CryptKeyHolder* p = new CryptKeyHolder(factoryParameter);
		p->addRef();
		return p;
	}
};

PluginModule module;
Factory factory;

} // anonymous namespace

extern "C" FB_DLL_EXPORT void FB_PLUGIN_ENTRY_POINT(IMaster* master)
{
	IPluginManager* pluginManager = master->getPluginManager();

	module.registerMe(pluginManager);
	pluginManager->registerPluginFactory(IPluginManager::TYPE_KEY_HOLDER, "CryptKeyHolder_example",
		&factory);
}