DbCrypt.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * PROGRAM: Firebird samples.
  3. * MODULE: DbCrypt.cpp
  4. * DESCRIPTION: Sample of how diskcrypt 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. #include <atomic>
  28. using namespace Firebird;
  29. namespace
  30. {
  31. class PluginModule : public IPluginModuleImpl<PluginModule, CheckStatusWrapper>
  32. {
  33. public:
  34. PluginModule()
  35. : pluginManager(NULL)
  36. { }
  37. ~PluginModule()
  38. {
  39. if (pluginManager)
  40. {
  41. pluginManager->unregisterModule(this);
  42. doClean();
  43. }
  44. }
  45. void registerMe(IPluginManager* m)
  46. {
  47. pluginManager = m;
  48. pluginManager->registerModule(this);
  49. }
  50. void doClean()
  51. {
  52. pluginManager = NULL;
  53. }
  54. void threadDetach() {}
  55. private:
  56. IPluginManager* pluginManager;
  57. };
  58. class DbCrypt : public IDbCryptPluginImpl<DbCrypt, CheckStatusWrapper>
  59. {
  60. public:
  61. explicit DbCrypt(IPluginConfig* cnf) noexcept
  62. : config(cnf), key(0), refCounter(0), owner(NULL)
  63. {
  64. config->addRef();
  65. }
  66. ~DbCrypt()
  67. {
  68. config->release();
  69. }
  70. // ICryptPlugin implementation
  71. void encrypt(CheckStatusWrapper* status, unsigned int length, const void* from, void* to);
  72. void decrypt(CheckStatusWrapper* status, unsigned int length, const void* from, void* to);
  73. void setKey(CheckStatusWrapper* status, unsigned int length, IKeyHolderPlugin** sources,
  74. const char* keyName);
  75. // One is free to ignore passed info when not needed
  76. void setInfo(CheckStatusWrapper* status, IDbCryptInfo* info)
  77. {
  78. // You may uncomment next line in a case of embedded connection
  79. // fprintf(stderr, "DbInfo: name is %s\n", info->getDatabaseFullPath(status));
  80. }
  81. int release()
  82. {
  83. if (--refCounter == 0)
  84. {
  85. delete this;
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. void addRef()
  91. {
  92. ++refCounter;
  93. }
  94. void setOwner(IReferenceCounted* o)
  95. {
  96. owner = o;
  97. }
  98. IReferenceCounted* getOwner()
  99. {
  100. return owner;
  101. }
  102. private:
  103. IPluginConfig* config;
  104. char savedKeyName[32];
  105. ISC_UCHAR key;
  106. std::atomic_int refCounter;
  107. IReferenceCounted* owner;
  108. void noKeyError(CheckStatusWrapper* status);
  109. };
  110. void DbCrypt::noKeyError(CheckStatusWrapper* status)
  111. {
  112. char msg[100];
  113. strcpy(msg, "Crypt key ");
  114. if (savedKeyName[0])
  115. {
  116. strcat(msg, savedKeyName);
  117. strcat(msg, " ");
  118. }
  119. strcat(msg, "not set");
  120. ISC_STATUS_ARRAY vector;
  121. vector[0] = isc_arg_gds;
  122. vector[1] = isc_random;
  123. vector[2] = isc_arg_string;
  124. vector[3] = (ISC_STATUS) msg;
  125. vector[4] = isc_arg_end;
  126. status->setErrors(vector);
  127. }
  128. void DbCrypt::encrypt(CheckStatusWrapper* status, unsigned int length, const void* from, void* to)
  129. {
  130. status->init();
  131. if (!key)
  132. {
  133. noKeyError(status);
  134. return;
  135. }
  136. const ISC_UCHAR* f = static_cast<const ISC_UCHAR*>(from);
  137. ISC_UCHAR* t = static_cast<ISC_UCHAR*>(to);
  138. while (length--)
  139. {
  140. *t++ = (*f++) ^ key;
  141. }
  142. }
  143. void DbCrypt::decrypt(CheckStatusWrapper* status, unsigned int length, const void* from, void* to)
  144. {
  145. status->init();
  146. if (!key)
  147. {
  148. noKeyError(status);
  149. return;
  150. }
  151. const ISC_UCHAR* f = static_cast<const ISC_UCHAR*>(from);
  152. ISC_UCHAR* t = static_cast<ISC_UCHAR*>(to);
  153. while (length--)
  154. {
  155. *t++ = (*f++) ^ key;
  156. }
  157. }
  158. void DbCrypt::setKey(CheckStatusWrapper* status, unsigned int length, IKeyHolderPlugin** sources,
  159. const char* keyName)
  160. {
  161. status->init();
  162. if (key != 0)
  163. return;
  164. strncpy(savedKeyName, (keyName ? keyName : ""), sizeof(savedKeyName));
  165. savedKeyName[sizeof(savedKeyName) - 1] = 0;
  166. IConfig* def = config->getDefaultConfig(status);
  167. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  168. return;
  169. IConfigEntry* confEntry = def->find(status, "Auto");
  170. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  171. {
  172. def->release();
  173. return;
  174. }
  175. if (confEntry)
  176. {
  177. char v = *(confEntry->getValue());
  178. confEntry->release();
  179. if (v == '1' || v == 'y' || v == 'Y' || v == 't' || v == 'T')
  180. {
  181. confEntry = def->find(status, "Value");
  182. def->release();
  183. if (confEntry)
  184. {
  185. v = confEntry->getIntValue();
  186. confEntry->release();
  187. if (v)
  188. {
  189. key = v;
  190. return;
  191. }
  192. }
  193. key = 0x5a;
  194. return;
  195. }
  196. def->release();
  197. }
  198. for (unsigned n = 0; n < length; ++n)
  199. {
  200. ICryptKeyCallback* callback = sources[n]->keyHandle(status, savedKeyName);
  201. if (status->getState() & Firebird::IStatus::STATE_ERRORS)
  202. return;
  203. if (callback && callback->callback(0, NULL, 1, &key) == 1)
  204. return;
  205. }
  206. key = 0;
  207. noKeyError(status);
  208. }
  209. class Factory : public IPluginFactoryImpl<Factory, CheckStatusWrapper>
  210. {
  211. public:
  212. IPluginBase* createPlugin(CheckStatusWrapper* status, IPluginConfig* factoryParameter)
  213. {
  214. /*
  215. // *** Uncomment this 2 lines to see how plugin creation errors are handled
  216. const ISC_STATUS_ARRAY vector = {isc_arg_gds, isc_virmemexh, isc_arg_end};
  217. throw FbException(status, vector);
  218. */
  219. DbCrypt* p = new DbCrypt(factoryParameter);
  220. p->addRef();
  221. return p;
  222. }
  223. };
  224. PluginModule module;
  225. Factory factory;
  226. } // anonymous namespace
  227. extern "C" FB_DLL_EXPORT void FB_PLUGIN_ENTRY_POINT(IMaster* master)
  228. {
  229. IPluginManager* pluginManager = master->getPluginManager();
  230. module.registerMe(pluginManager);
  231. pluginManager->registerPluginFactory(IPluginManager::TYPE_DB_CRYPT, "fbSampleDbCrypt", &factory);
  232. }