CryptKeyHolder.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. IMaster* master = NULL;
  30. class PluginModule : public IPluginModuleImpl<PluginModule, CheckStatusWrapper>
  31. {
  32. public:
  33. PluginModule()
  34. : pluginManager(NULL)
  35. { }
  36. ~PluginModule()
  37. {
  38. if (pluginManager)
  39. {
  40. pluginManager->unregisterModule(this);
  41. doClean();
  42. }
  43. }
  44. void registerMe(IPluginManager* m)
  45. {
  46. pluginManager = m;
  47. pluginManager->registerModule(this);
  48. }
  49. void doClean()
  50. {
  51. pluginManager = NULL;
  52. }
  53. void threadDetach() {}
  54. private:
  55. IPluginManager* pluginManager;
  56. };
  57. class CryptKeyHolder : public IKeyHolderPluginImpl<CryptKeyHolder, CheckStatusWrapper>
  58. {
  59. public:
  60. explicit CryptKeyHolder(IPluginConfig* cnf) throw()
  61. : callbackInterface(this), named(NULL), tempStatus(master->getStatus()),
  62. config(cnf), key(0), owner(NULL)
  63. {
  64. config->addRef();
  65. }
  66. ~CryptKeyHolder()
  67. {
  68. config->release();
  69. tempStatus.dispose();
  70. }
  71. // IKeyHolderPlugin implementation
  72. int keyCallback(CheckStatusWrapper* status, ICryptKeyCallback* callback);
  73. ICryptKeyCallback* keyHandle(CheckStatusWrapper* status, const char* keyName);
  74. ICryptKeyCallback* chainHandle(CheckStatusWrapper* status);
  75. int release()
  76. {
  77. if (--refCounter == 0)
  78. {
  79. delete this;
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. void addRef()
  85. {
  86. ++refCounter;
  87. }
  88. void setOwner(Firebird::IReferenceCounted* o)
  89. {
  90. owner = o;
  91. }
  92. IReferenceCounted* getOwner()
  93. {
  94. return owner;
  95. }
  96. ISC_UCHAR getKey()
  97. {
  98. if (!key)
  99. keyCallback(&tempStatus, NULL);
  100. return key;
  101. }
  102. FB_BOOLEAN useOnlyOwnKeys(CheckStatusWrapper* status)
  103. {
  104. IConfigEntry* e = getEntry(status, "OnlyOwnKey");
  105. if (!e)
  106. return FB_TRUE; // safe default
  107. FB_BOOLEAN rc = e->getBoolValue();
  108. e->release();
  109. return rc;
  110. }
  111. private:
  112. class CallbackInterface : public ICryptKeyCallbackImpl<CallbackInterface, CheckStatusWrapper>
  113. {
  114. public:
  115. explicit CallbackInterface(CryptKeyHolder* p)
  116. : holder(p)
  117. { }
  118. unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
  119. {
  120. ISC_UCHAR k = holder->getKey();
  121. if (!k)
  122. {
  123. return 0;
  124. }
  125. if (length > 0 && buffer)
  126. {
  127. memcpy(buffer, &k, 1);
  128. }
  129. return 1;
  130. }
  131. private:
  132. CryptKeyHolder* holder;
  133. };
  134. class NamedCallback : public ICryptKeyCallbackImpl<NamedCallback, CheckStatusWrapper>
  135. {
  136. public:
  137. NamedCallback(NamedCallback* n, const char* nm, ISC_UCHAR k)
  138. : next(n), key(k)
  139. {
  140. strncpy(name, nm, sizeof(name));
  141. name[sizeof(name) - 1] = 0;
  142. }
  143. unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
  144. {
  145. memcpy(buffer, &key, 1);
  146. return 1;
  147. }
  148. ~NamedCallback()
  149. {
  150. delete next;
  151. }
  152. char name[32];
  153. NamedCallback* next;
  154. ISC_UCHAR key;
  155. };
  156. CallbackInterface callbackInterface;
  157. NamedCallback *named;
  158. CheckStatusWrapper tempStatus;
  159. IPluginConfig* config;
  160. ISC_UCHAR key;
  161. FbSampleAtomic refCounter;
  162. IReferenceCounted* owner;
  163. IConfigEntry* getEntry(CheckStatusWrapper* status, const char* entryName);
  164. };
  165. IConfigEntry* CryptKeyHolder::getEntry(CheckStatusWrapper* status, const char* entryName)
  166. {
  167. IConfig* def = config->getDefaultConfig(status);
  168. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  169. return NULL;
  170. IConfigEntry* confEntry = def->find(status, entryName);
  171. def->release();
  172. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  173. return NULL;
  174. return confEntry;
  175. }
  176. int CryptKeyHolder::keyCallback(CheckStatusWrapper* status, ICryptKeyCallback* callback)
  177. {
  178. if (key != 0)
  179. return 1;
  180. IConfigEntry* confEntry = getEntry(status, "Auto");
  181. if (confEntry)
  182. {
  183. FB_BOOLEAN b = confEntry->getBoolValue();
  184. confEntry->release();
  185. if (b)
  186. {
  187. confEntry = getEntry(status, "Key");
  188. if (confEntry)
  189. {
  190. key = confEntry->getIntValue();
  191. confEntry->release();
  192. }
  193. else
  194. key = 0x5a;
  195. return 1;
  196. }
  197. }
  198. if (callback && callback->callback(0, NULL, 1, &key) != 1)
  199. {
  200. key = 0;
  201. return 0;
  202. }
  203. return 1;
  204. }
  205. ICryptKeyCallback* CryptKeyHolder::keyHandle(CheckStatusWrapper* status, const char* keyName)
  206. {
  207. if (keyName[0] == 0)
  208. return &callbackInterface;
  209. for (NamedCallback* n = named; n; n = n->next)
  210. {
  211. if (strcmp(keyName, n->name) == 0)
  212. return n;
  213. }
  214. char kn[40];
  215. strcpy(kn, "Key");
  216. strncat(kn, keyName, sizeof(kn) - 3 - 1);
  217. kn[sizeof(kn) - 1] = 0;
  218. IConfigEntry* confEntry = getEntry(status, kn);
  219. if (confEntry)
  220. {
  221. int k = confEntry->getIntValue();
  222. confEntry->release();
  223. if (k > 0 && k < 256)
  224. {
  225. named = new NamedCallback(named, keyName, static_cast<ISC_UCHAR>(k));
  226. return named;
  227. }
  228. }
  229. return NULL;
  230. }
  231. ICryptKeyCallback* CryptKeyHolder::chainHandle(CheckStatusWrapper* status)
  232. {
  233. return &callbackInterface;
  234. }
  235. class Factory : public IPluginFactoryImpl<Factory, CheckStatusWrapper>
  236. {
  237. public:
  238. IPluginBase* createPlugin(CheckStatusWrapper* status, IPluginConfig* factoryParameter)
  239. {
  240. CryptKeyHolder* p = new CryptKeyHolder(factoryParameter);
  241. p->addRef();
  242. return p;
  243. }
  244. };
  245. PluginModule module;
  246. Factory factory;
  247. } // anonymous namespace
  248. extern "C" FB_DLL_EXPORT void FB_PLUGIN_ENTRY_POINT(IMaster* m)
  249. {
  250. master = m;
  251. IPluginManager* pluginManager = master->getPluginManager();
  252. module.registerMe(pluginManager);
  253. pluginManager->registerPluginFactory(IPluginManager::TYPE_KEY_HOLDER, "fbSampleKeyHolder",
  254. &factory);
  255. }