DbCrypt.cpp 5.7 KB

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