2
0

CryptKeyHolder.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * PROGRAM: Firebird samples.
  3. * MODULE: CryptKeyHolder.cpp
  4. * DESCRIPTION: Sample of how key holder may be written.
  5. *
  6. * The contents of this file are subject to the Initial
  7. * Developer's Public License Version 1.0 (the "License");
  8. * you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  11. *
  12. * Software distributed under the License is distributed AS IS,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing rights
  15. * and limitations under the License.
  16. *
  17. * The Original Code was created by Alex Peshkov
  18. * for the Firebird Open Source RDBMS project.
  19. *
  20. * Copyright (c) 2012 Alex Peshkov <peshkoff at mail.ru>
  21. * and all contributors signed below.
  22. *
  23. * All Rights Reserved.
  24. * Contributor(s): ______________________________________.
  25. */
  26. #include "../interfaces/ifaceExamples.h"
  27. namespace
  28. {
  29. class PluginModule : public IPluginModuleImpl<PluginModule, CheckStatusWrapper>
  30. {
  31. public:
  32. PluginModule()
  33. : pluginManager(NULL)
  34. { }
  35. ~PluginModule()
  36. {
  37. if (pluginManager)
  38. {
  39. pluginManager->unregisterModule(this);
  40. doClean();
  41. }
  42. }
  43. void registerMe(IPluginManager* m)
  44. {
  45. pluginManager = m;
  46. pluginManager->registerModule(this);
  47. }
  48. void doClean()
  49. {
  50. pluginManager = NULL;
  51. }
  52. void threadDetach() {};
  53. private:
  54. IPluginManager* pluginManager;
  55. };
  56. class CryptKeyHolder : public IKeyHolderPluginImpl<CryptKeyHolder, CheckStatusWrapper>
  57. {
  58. public:
  59. explicit CryptKeyHolder(IPluginConfig* cnf) throw()
  60. : callbackInterface(this), named(NULL), config(cnf), key(0), owner(NULL)
  61. {
  62. config->addRef();
  63. }
  64. ~CryptKeyHolder()
  65. {
  66. config->release();
  67. }
  68. // IKeyHolderPlugin implementation
  69. int keyCallback(CheckStatusWrapper* status, ICryptKeyCallback* callback);
  70. ICryptKeyCallback* keyHandle(CheckStatusWrapper* status, const char* keyName);
  71. ICryptKeyCallback* chainHandle(CheckStatusWrapper* status);
  72. int release()
  73. {
  74. if (--refCounter == 0)
  75. {
  76. delete this;
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. void addRef()
  82. {
  83. ++refCounter;
  84. }
  85. void setOwner(Firebird::IReferenceCounted* o)
  86. {
  87. owner = o;
  88. }
  89. IReferenceCounted* getOwner()
  90. {
  91. return owner;
  92. }
  93. ISC_UCHAR getKey()
  94. {
  95. return key;
  96. }
  97. FB_BOOLEAN useOnlyOwnKeys(CheckStatusWrapper* status)
  98. {
  99. IConfigEntry* e = getEntry(status, "OnlyOwnKey");
  100. if (!e)
  101. return FB_TRUE; // safe default
  102. FB_BOOLEAN rc = e->getBoolValue();
  103. e->release();
  104. return rc;
  105. }
  106. private:
  107. class CallbackInterface : public ICryptKeyCallbackImpl<CallbackInterface, CheckStatusWrapper>
  108. {
  109. public:
  110. explicit CallbackInterface(CryptKeyHolder* p)
  111. : holder(p)
  112. { }
  113. unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
  114. {
  115. ISC_UCHAR k = holder->getKey();
  116. if (!k)
  117. {
  118. return 0;
  119. }
  120. if (length > 0 && buffer)
  121. {
  122. memcpy(buffer, &k, 1);
  123. }
  124. return 1;
  125. }
  126. private:
  127. CryptKeyHolder* holder;
  128. };
  129. class NamedCallback : public ICryptKeyCallbackImpl<NamedCallback, CheckStatusWrapper>
  130. {
  131. public:
  132. NamedCallback(NamedCallback* n, const char* nm, ISC_UCHAR k)
  133. : next(n), key(k)
  134. {
  135. strncpy(name, nm, sizeof(name));
  136. name[sizeof(name) - 1] = 0;
  137. }
  138. unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
  139. {
  140. memcpy(buffer, &key, 1);
  141. return 1;
  142. }
  143. ~NamedCallback()
  144. {
  145. delete next;
  146. }
  147. char name[32];
  148. NamedCallback* next;
  149. ISC_UCHAR key;
  150. };
  151. CallbackInterface callbackInterface;
  152. NamedCallback *named;
  153. IPluginConfig* config;
  154. ISC_UCHAR key;
  155. FbSampleAtomic refCounter;
  156. IReferenceCounted* owner;
  157. IConfigEntry* getEntry(CheckStatusWrapper* status, const char* entryName);
  158. };
  159. IConfigEntry* CryptKeyHolder::getEntry(CheckStatusWrapper* status, const char* entryName)
  160. {
  161. IConfig* def = config->getDefaultConfig(status);
  162. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  163. return NULL;
  164. IConfigEntry* confEntry = def->find(status, entryName);
  165. def->release();
  166. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  167. return NULL;
  168. return confEntry;
  169. }
  170. int CryptKeyHolder::keyCallback(CheckStatusWrapper* status, ICryptKeyCallback* callback)
  171. {
  172. if (key != 0)
  173. return 1;
  174. IConfigEntry* confEntry = getEntry(status, "Auto");
  175. if (confEntry)
  176. {
  177. FB_BOOLEAN b = confEntry->getBoolValue();
  178. confEntry->release();
  179. if (b)
  180. {
  181. key = 0x5a;
  182. return 1;
  183. }
  184. }
  185. if (callback && callback->callback(0, NULL, 1, &key) != 1)
  186. {
  187. key = 0;
  188. return 0;
  189. }
  190. return 1;
  191. }
  192. ICryptKeyCallback* CryptKeyHolder::keyHandle(CheckStatusWrapper* status, const char* keyName)
  193. {
  194. if (keyName[0] == 0)
  195. return &callbackInterface;
  196. for (NamedCallback* n = named; n; n = n->next)
  197. {
  198. if (strcmp(keyName, n->name) == 0)
  199. return n;
  200. }
  201. char kn[40];
  202. strcpy(kn, "Key");
  203. strncat(kn, keyName, sizeof(kn) - 3 - 1);
  204. kn[sizeof(kn) - 1] = 0;
  205. IConfigEntry* confEntry = getEntry(status, kn);
  206. if (confEntry)
  207. {
  208. int k = confEntry->getIntValue();
  209. confEntry->release();
  210. if (k > 0 && k < 256)
  211. {
  212. named = new NamedCallback(named, keyName, static_cast<ISC_UCHAR>(k));
  213. return named;
  214. }
  215. }
  216. return NULL;
  217. }
  218. ICryptKeyCallback* CryptKeyHolder::chainHandle(CheckStatusWrapper* status)
  219. {
  220. return &callbackInterface;
  221. }
  222. class Factory : public IPluginFactoryImpl<Factory, CheckStatusWrapper>
  223. {
  224. public:
  225. IPluginBase* createPlugin(CheckStatusWrapper* status, IPluginConfig* factoryParameter)
  226. {
  227. CryptKeyHolder* p = new CryptKeyHolder(factoryParameter);
  228. p->addRef();
  229. return p;
  230. }
  231. };
  232. PluginModule module;
  233. Factory factory;
  234. } // anonymous namespace
  235. extern "C" void FB_DLL_EXPORT FB_PLUGIN_ENTRY_POINT(IMaster* master)
  236. {
  237. IPluginManager* pluginManager = master->getPluginManager();
  238. module.registerMe(pluginManager);
  239. pluginManager->registerPluginFactory(IPluginManager::TYPE_KEY_HOLDER, "CryptKeyHolder_example",
  240. &factory);
  241. }