cryptDb.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. {
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: cryptDb.pas
  4. * DESCRIPTION: Sample of how diskcrypt may be written using pascal.
  5. * Does XOR 5 for all bytes in passed data.
  6. *
  7. * Run something like this to build:
  8. * fpc -Fu<path-to-Firebird.pas> -Mdelphi -fPIC cryptDb.pas
  9. *
  10. * The contents of this file are subject to the Initial
  11. * Developer's Public License Version 1.0 (the "License");
  12. * you may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at
  14. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  15. *
  16. * Software distributed under the License is distributed AS IS,
  17. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing rights
  19. * and limitations under the License.
  20. *
  21. * The Original Code was created by Alexander Peshkoff
  22. * for the Firebird Open Source RDBMS project.
  23. *
  24. * Copyright (c) 2016 Alexander Peshkoff <[email protected]>
  25. * and all contributors signed below.
  26. *
  27. * All Rights Reserved.
  28. * Contributor(s): ______________________________________. }
  29. library cryptDb;
  30. uses
  31. SysUtils,
  32. Classes,
  33. Firebird;
  34. Type
  35. TMyPluginModule = class(IPluginModuleImpl)
  36. private
  37. FRegistered: Boolean;
  38. public
  39. constructor Create;
  40. destructor Destroy; override;
  41. procedure registerMe;
  42. // TPluginModule implementation
  43. procedure doClean; override;
  44. procedure threadDetach; override;
  45. end;
  46. TMyCrypt = class(IDbCryptPluginImpl)
  47. private
  48. FCounter: Integer;
  49. FOwner: IReferenceCounted;
  50. FConfig: IPluginConfig;
  51. public
  52. constructor Create(config: IPluginConfig);
  53. destructor Destroy; override;
  54. // TRefCounted implementation
  55. procedure addRef; override;
  56. function release: Integer; override;
  57. // TPluginBase implementation
  58. procedure setOwner(ref: IReferenceCounted); override;
  59. function getOwner: IReferenceCounted; override;
  60. // TCryptPlugin implementation
  61. procedure setKey(status: IStatus; length: Cardinal; sources: IKeyHolderPluginPtr; keyName: PAnsiChar); override;
  62. procedure encrypt(status: IStatus; length: Cardinal; src, dst: Pointer); override;
  63. procedure decrypt(status: IStatus; length: Cardinal; src, dst: Pointer); override;
  64. procedure setInfo(status: IStatus; info: IDbCryptInfo); override;
  65. private
  66. procedure pxor(length: Cardinal; mem: Pointer);
  67. end;
  68. TMyFactory = class(IPluginFactoryImpl)
  69. public
  70. constructor Create(module: IPluginModule);
  71. destructor Destroy; override;
  72. // TPluginFactory implementation
  73. function createPlugin(status: IStatus; factoryParameter: IPluginConfig): IPluginBase; override;
  74. end;
  75. /// implementation
  76. Var
  77. Master: IMaster = nil;
  78. { TMyPluginModule }
  79. constructor TMyPluginModule.Create;
  80. begin
  81. inherited;
  82. FRegistered := false;
  83. end;
  84. destructor TMyPluginModule.Destroy;
  85. begin
  86. if FRegistered then
  87. begin
  88. Master.getPluginManager.unregisterModule(Self);
  89. doClean();
  90. end;
  91. inherited;
  92. end;
  93. procedure TMyPluginModule.doClean;
  94. begin
  95. FRegistered := False;
  96. end;
  97. procedure TMyPluginModule.threadDetach;
  98. begin
  99. end;
  100. procedure TMyPluginModule.registerMe;
  101. begin
  102. if not FRegistered then
  103. begin
  104. Master.getPluginManager.registerModule(Self);
  105. FRegistered := True;
  106. end
  107. end;
  108. { TMyFactory }
  109. function TMyFactory.createPlugin(status: IStatus; factoryParameter: IPluginConfig): IPluginBase;
  110. var
  111. plugin: IPluginBase;
  112. begin
  113. plugin := TMyCrypt.Create(factoryParameter);
  114. plugin.addRef;
  115. Result := plugin;
  116. end;
  117. { TMyCrypt }
  118. constructor TMyCrypt.Create(config: IPluginConfig);
  119. begin
  120. Inherited Create;
  121. FOwner := nil;
  122. FConfig := config;
  123. FConfig.addRef;
  124. end;
  125. destructor TMyCrypt.Destroy;
  126. begin
  127. FConfig.release;
  128. FConfig := nil;
  129. inherited;
  130. end;
  131. procedure TMyCrypt.addRef;
  132. begin
  133. InterlockedIncrement(FCounter);
  134. end;
  135. function TMyCrypt.release: Integer;
  136. begin
  137. if InterlockedDecrement(FCounter) = 0 then
  138. begin
  139. Result := 0;
  140. Free;
  141. end
  142. else
  143. Result := 1;
  144. end;
  145. procedure TMyCrypt.setOwner(ref: IReferenceCounted);
  146. begin
  147. FOwner := ref;
  148. end;
  149. function TMyCrypt.getOwner: IReferenceCounted;
  150. begin
  151. Result := FOwner;
  152. end;
  153. procedure TMyCrypt.setInfo(status: IStatus; info: IDbCryptInfo);
  154. begin
  155. status.init;
  156. // do nothing in this trivial sample
  157. end;
  158. procedure TMyCrypt.decrypt(status: IStatus; length: Cardinal; src, dst: Pointer);
  159. begin
  160. status.init;
  161. // decrypt here
  162. Move(src^, dst^, length);
  163. pxor(length, dst);
  164. end;
  165. procedure TMyCrypt.encrypt(status: IStatus; length: Cardinal; src, dst: Pointer);
  166. begin
  167. status.init;
  168. // encrypt here
  169. Move(src^, dst^, length);
  170. pxor(length, dst);
  171. end;
  172. procedure TMyCrypt.setKey(status: IStatus; length: Cardinal; sources: IKeyHolderPluginPtr; keyName: PAnsiChar);
  173. begin
  174. status.init;
  175. // get encryption key "keyName" from "sources" if necessary
  176. end;
  177. procedure TMyCrypt.pxor(length: Cardinal; mem: Pointer);
  178. var
  179. ptr: BytePtr;
  180. i: Integer;
  181. begin
  182. ptr := BytePtr(mem);
  183. for i := 1 to length do
  184. begin
  185. ptr^ := (ptr^) xor 5;
  186. Inc(ptr);
  187. end;
  188. end;
  189. { Factory }
  190. constructor TMyFactory.Create(module: IPluginModule);
  191. begin
  192. Inherited Create;
  193. end;
  194. destructor TMyFactory.Destroy;
  195. begin
  196. inherited;
  197. end;
  198. { entrypoint }
  199. Procedure firebird_plugin(masterInterface: IMaster); cdecl; export;
  200. var
  201. pluginManager: IPluginManager;
  202. module: TMyPluginModule;
  203. factory: IPluginFactory;
  204. Begin
  205. Master := masterInterface;
  206. pluginManager := master.getPluginManager;
  207. module := TMyPluginModule.Create;
  208. module.registerMe;
  209. factory := TMyFactory.Create(module);
  210. pluginManager.registerPluginFactory(IPluginManager.TYPE_DB_CRYPT, 'cryptDb', factory);
  211. End;
  212. exports
  213. firebird_plugin;
  214. begin
  215. end.