CryptKeyHolder.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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), init(false), 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. const ISC_UCHAR& getKey()
  97. {
  98. if (!init)
  99. {
  100. keyCallback(&tempStatus, NULL);
  101. init = true;
  102. }
  103. return key;
  104. }
  105. FB_BOOLEAN useOnlyOwnKeys(CheckStatusWrapper* status)
  106. {
  107. IConfigEntry* e = getEntry(status, "OnlyOwnKey");
  108. if (!e)
  109. return FB_TRUE; // safe default
  110. FB_BOOLEAN rc = e->getBoolValue();
  111. e->release();
  112. return rc;
  113. }
  114. private:
  115. class CallbackInterface : public ICryptKeyCallbackImpl<CallbackInterface, CheckStatusWrapper>
  116. {
  117. public:
  118. explicit CallbackInterface(CryptKeyHolder* p)
  119. : holder(p)
  120. { }
  121. unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
  122. {
  123. const ISC_UCHAR& k = holder->getKey();
  124. if (!k)
  125. {
  126. return 0;
  127. }
  128. if (length > 0 && buffer)
  129. {
  130. memcpy(buffer, &k, 1);
  131. }
  132. return 1;
  133. }
  134. int getHashLength(Firebird::CheckStatusWrapper* status) override
  135. {
  136. const ISC_UCHAR& k = holder->getKey();
  137. if (!k)
  138. {
  139. ISC_STATUS err[] = {isc_arg_gds, isc_wish_list};
  140. status->setErrors2(2, err);
  141. return -1;
  142. }
  143. return 1;
  144. }
  145. void getHashData(Firebird::CheckStatusWrapper* status, void* h) override
  146. {
  147. // here key value is returned by hash function as is
  148. // do not do it in production - use some hash function
  149. const ISC_UCHAR& k = holder->getKey();
  150. if (!k)
  151. {
  152. ISC_STATUS err[] = {isc_arg_gds, isc_wish_list};
  153. status->setErrors2(2, err);
  154. return;
  155. }
  156. memcpy(h, &k, 1);
  157. }
  158. private:
  159. CryptKeyHolder* holder;
  160. };
  161. class NamedCallback : public ICryptKeyCallbackImpl<NamedCallback, CheckStatusWrapper>
  162. {
  163. public:
  164. NamedCallback(NamedCallback* n, const char* nm, ISC_UCHAR k)
  165. : next(n), key(k)
  166. {
  167. strncpy(name, nm, sizeof(name));
  168. name[sizeof(name) - 1] = 0;
  169. }
  170. unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
  171. {
  172. memcpy(buffer, &key, 1);
  173. return 1;
  174. }
  175. int getHashLength(Firebird::CheckStatusWrapper* status) override
  176. {
  177. return 1;
  178. }
  179. void getHashData(Firebird::CheckStatusWrapper* status, void* h) override
  180. {
  181. // here key value is returned by hash function as is
  182. // do not do it in production - use some hash function
  183. memcpy(h, &key, 1);
  184. }
  185. ~NamedCallback()
  186. {
  187. delete next;
  188. }
  189. char name[32];
  190. NamedCallback* next;
  191. ISC_UCHAR key;
  192. };
  193. CallbackInterface callbackInterface;
  194. NamedCallback *named;
  195. CheckStatusWrapper tempStatus;
  196. IPluginConfig* config;
  197. ISC_UCHAR key;
  198. bool init;
  199. FbSampleAtomic refCounter;
  200. IReferenceCounted* owner;
  201. IConfigEntry* getEntry(CheckStatusWrapper* status, const char* entryName);
  202. };
  203. IConfigEntry* CryptKeyHolder::getEntry(CheckStatusWrapper* status, const char* entryName)
  204. {
  205. IConfig* def = config->getDefaultConfig(status);
  206. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  207. return NULL;
  208. IConfigEntry* confEntry = def->find(status, entryName);
  209. def->release();
  210. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  211. return NULL;
  212. return confEntry;
  213. }
  214. int CryptKeyHolder::keyCallback(CheckStatusWrapper* status, ICryptKeyCallback* callback)
  215. {
  216. if (key != 0)
  217. return 1;
  218. IConfigEntry* confEntry = getEntry(status, "Auto");
  219. if (confEntry)
  220. {
  221. FB_BOOLEAN b = confEntry->getBoolValue();
  222. confEntry->release();
  223. if (b)
  224. {
  225. confEntry = getEntry(status, "Key");
  226. if (confEntry)
  227. {
  228. key = confEntry->getIntValue();
  229. confEntry->release();
  230. }
  231. else
  232. key = 0x5a;
  233. return 1;
  234. }
  235. }
  236. if (callback && callback->callback(0, NULL, 1, &key) != 1)
  237. {
  238. key = 0;
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. ICryptKeyCallback* CryptKeyHolder::keyHandle(CheckStatusWrapper* status, const char* keyName)
  244. {
  245. if (keyName[0] == 0)
  246. return &callbackInterface;
  247. for (NamedCallback* n = named; n; n = n->next)
  248. {
  249. if (strcmp(keyName, n->name) == 0)
  250. return n;
  251. }
  252. char kn[40];
  253. strcpy(kn, "Key");
  254. strncat(kn, keyName, sizeof(kn) - 3 - 1);
  255. kn[sizeof(kn) - 1] = 0;
  256. IConfigEntry* confEntry = getEntry(status, kn);
  257. if (confEntry)
  258. {
  259. int k = confEntry->getIntValue();
  260. confEntry->release();
  261. if (k > 0 && k < 256)
  262. {
  263. named = new NamedCallback(named, keyName, static_cast<ISC_UCHAR>(k));
  264. return named;
  265. }
  266. }
  267. return NULL;
  268. }
  269. ICryptKeyCallback* CryptKeyHolder::chainHandle(CheckStatusWrapper* status)
  270. {
  271. return &callbackInterface;
  272. }
  273. class Factory : public IPluginFactoryImpl<Factory, CheckStatusWrapper>
  274. {
  275. public:
  276. IPluginBase* createPlugin(CheckStatusWrapper* status, IPluginConfig* factoryParameter)
  277. {
  278. CryptKeyHolder* p = new CryptKeyHolder(factoryParameter);
  279. p->addRef();
  280. return p;
  281. }
  282. };
  283. PluginModule module;
  284. Factory factory;
  285. } // anonymous namespace
  286. extern "C" FB_DLL_EXPORT void FB_PLUGIN_ENTRY_POINT(IMaster* m)
  287. {
  288. master = m;
  289. IPluginManager* pluginManager = master->getPluginManager();
  290. module.registerMe(pluginManager);
  291. pluginManager->registerPluginFactory(IPluginManager::TYPE_KEY_HOLDER, "fbSampleKeyHolder",
  292. &factory);
  293. }