/EndPoint/Core/DiskCryptor.cs

https://github.com/mydlp/mydlp-endpoint-win · C# · 269 lines · 211 code · 33 blank · 25 comment · 24 complexity · b4ba8e7ce54c48529897ec4781f14b97 MD5 · raw file

  1. // Copyright (C) 2011 Huseyin Ozgur Batur <ozgur@medra.com.tr>
  2. //
  3. //--------------------------------------------------------------------------
  4. // This file is part of MyDLP.
  5. //
  6. // MyDLP is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // MyDLP is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with MyDLP. If not, see <http://www.gnu.org/licenses/>.
  18. //--------------------------------------------------------------------------
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Text;
  22. using System.Threading;
  23. using System.Runtime.InteropServices;
  24. using System.IO;
  25. using Microsoft.Win32;
  26. using System.Diagnostics;
  27. using System.ComponentModel;
  28. namespace MyDLP.EndPoint.Core
  29. {
  30. public class DiskCryptor
  31. {
  32. private static string getDCPath()
  33. {
  34. string middir = @"x86";
  35. if (Configuration.IsOS64Bit())
  36. middir = @"x64";
  37. return Configuration.AppPath + @"\internal\dcrypt\" + middir;
  38. }
  39. private static string getDCCon()
  40. {
  41. return "cd " + getDCPath() + " && dccon.exe";
  42. }
  43. private static string getDCInst()
  44. {
  45. return "cd " + getDCPath() + " && dcinst.exe";
  46. }
  47. protected static void installDC()
  48. {
  49. string command = getDCInst() + @" -setup";
  50. ExecuteParameters eparams = new ExecuteParameters(command, "DC install");
  51. ProcessControl.CommandOutputSync(eparams);
  52. }
  53. protected static void configureDC()
  54. {
  55. string command = getDCCon() + @" -config-mydlp";
  56. ExecuteParameters eparams = new ExecuteParameters(command, "DC config mydlp");
  57. ProcessControl.CommandOutputSync(eparams);
  58. }
  59. protected static void deconfigureDC()
  60. {
  61. string command = getDCCon() + @" -deconfig-mydlp";
  62. ExecuteParameters eparams = new ExecuteParameters(command, "DC deconfig mydlp");
  63. ProcessControl.CommandOutputSync(eparams);
  64. }
  65. protected static string getPartitionId(string driveLetter)
  66. {
  67. string command = getDCCon() + @" -enum";
  68. ExecuteParameters eparams = new ExecuteParameters(command, "DC enum");
  69. string output = ProcessControl.CommandOutputSync(eparams);
  70. string[] lines = output.Split('\n');
  71. foreach (string line in lines)
  72. {
  73. if (line.Contains("reboot you system"))
  74. return null;
  75. if (line.Contains("is not compatible with the version of Windows"))
  76. return null;
  77. string[] parts = line.Split('|');
  78. if (parts.Length != 4)
  79. continue;
  80. string drivePart = parts[1];
  81. if (drivePart.Contains(" " + driveLetter + ": "))
  82. {
  83. string partitionIdPart = parts[0];
  84. return partitionIdPart.Trim();
  85. }
  86. }
  87. return null;
  88. }
  89. protected static bool isEncrypted(string partitionId)
  90. {
  91. string command = getDCCon() + @" -info " + partitionId;
  92. ExecuteParameters eparams = new ExecuteParameters(command, "DC isEncrypted");
  93. string output = ProcessControl.CommandOutputSync(eparams);
  94. string[] lines = output.Split('\n');
  95. foreach (string line in lines)
  96. {
  97. if (line.StartsWith("Cipher:"))
  98. return true;
  99. }
  100. return false;
  101. }
  102. protected static bool doesNeedFormatting(string partitionId)
  103. {
  104. string command = getDCCon() + @" -info " + partitionId;
  105. ExecuteParameters eparams = new ExecuteParameters(command, "DC doesNeedFormatting");
  106. string output = ProcessControl.CommandOutputSync(eparams);
  107. string[] lines = output.Split('\n');
  108. foreach (string line in lines)
  109. {
  110. if (line.StartsWith("Status:") &&
  111. (line.Contains("boot") || line.Contains("system"))
  112. )
  113. return false;
  114. if (line.StartsWith("Device:") && line.Contains(@"\\Device\CdRom"))
  115. return false;
  116. if (line.StartsWith("Cipher:"))
  117. return false;
  118. if (line.Contains("reboot you system"))
  119. return false;
  120. if (line.Contains("is not compatible with the version of Windows"))
  121. return false;
  122. }
  123. return true;
  124. }
  125. protected static bool isMounted(string partitionId)
  126. {
  127. string command = getDCCon() + @" -info " + partitionId;
  128. ExecuteParameters eparams = new ExecuteParameters(command, "DC isMounted");
  129. string output = ProcessControl.CommandOutputSync(eparams);
  130. string[] lines = output.Split('\n');
  131. foreach (string line in lines)
  132. {
  133. if (line.StartsWith("Status:"))
  134. {
  135. if (line.Contains(" mounted"))
  136. return true;
  137. if (line.Contains(" unmounted"))
  138. return false;
  139. }
  140. }
  141. return false;
  142. }
  143. protected static void formatPartition(string partitionId, string fsType)
  144. {
  145. string keyfile = Engine.GetShortPath(SeapClient.GetKeyfile());
  146. if (File.Exists(keyfile))
  147. {
  148. string command = getDCCon() + @" -format " + partitionId + " -q -" + fsType + " -a -p mydlp -kf " + keyfile;
  149. ExecuteParameters eparams = new ExecuteParameters(command, "DC format");
  150. ProcessControl.CommandOutputSync(eparams);
  151. File.Delete(keyfile);
  152. }
  153. }
  154. protected static void mountPartition(string partitionId)
  155. {
  156. string keyfile = Engine.GetShortPath(SeapClient.GetKeyfile());
  157. if (File.Exists(keyfile))
  158. {
  159. string command = getDCCon() + @" -mount " + partitionId + " -p mydlp -kf " + keyfile;
  160. ExecuteParameters eparams = new ExecuteParameters(command, "DC mount");
  161. ProcessControl.CommandOutputSync(eparams);
  162. File.Delete(keyfile);
  163. }
  164. }
  165. protected static void unmountPartition(string partitionId)
  166. {
  167. string command = getDCCon() + @" -unmount " + partitionId + " -f";
  168. ExecuteParameters eparams = new ExecuteParameters(command, "DC unmount");
  169. ProcessControl.CommandOutputSync(eparams);
  170. }
  171. protected static void cleanupMemory()
  172. {
  173. string command = getDCCon() + @" -clean";
  174. ExecuteParameters eparams = new ExecuteParameters(command, "DC clean");
  175. ProcessControl.CommandOutputSync(eparams);
  176. }
  177. protected static void mountAllEncryptedPartitions()
  178. {
  179. string keyfile = Engine.GetShortPath(SeapClient.GetKeyfile());
  180. if (File.Exists(keyfile))
  181. {
  182. string command = getDCCon() + @" -mountall -p mydlp -kf " + keyfile;
  183. ExecuteParameters eparams = new ExecuteParameters(command, "DC mountall");
  184. ProcessControl.CommandOutputSync(eparams);
  185. File.Delete(keyfile);
  186. }
  187. }
  188. protected static void unmountAllEncryptedPartitions()
  189. {
  190. string command = getDCCon() + @" -unmountall -f";
  191. ExecuteParameters eparams = new ExecuteParameters(command, "DC unmountall");
  192. ProcessControl.CommandOutputSync(eparams);
  193. }
  194. // when reg entry usbstor_encryption 0 -> 1
  195. public static void StartDcrypt()
  196. {
  197. installDC();
  198. configureDC();
  199. }
  200. // when reg entry usbstor_encryption 1 -> 0
  201. public static void StopDcrypt()
  202. {
  203. deconfigureDC();
  204. unmountAllEncryptedPartitions();
  205. cleanupMemory();
  206. }
  207. // should be called after reiving key. when hasKey state turns 0 to 1.
  208. public static void AfterKeyReceive()
  209. {
  210. mountAllEncryptedPartitions();
  211. }
  212. // should be called after losing key. when hasKey state turns 1 to 0.
  213. public static void AfterKeyLose()
  214. {
  215. unmountAllEncryptedPartitions();
  216. cleanupMemory();
  217. }
  218. // should not contain semicolon eg. E
  219. public static bool DoesDriveLetterNeedsFormatting(string driveLetter)
  220. {
  221. mountAllEncryptedPartitions();
  222. string partitionId = getPartitionId(driveLetter);
  223. if (partitionId == null) return false;
  224. return doesNeedFormatting(partitionId);
  225. }
  226. // fstype can be: fat , fat32, exfat, ntfs, raw
  227. // windows shows only: fat32, exfat and ntfs
  228. public static bool FormatDriveLetter(string driveLetter, string fsType)
  229. {
  230. string partitionId = getPartitionId(driveLetter);
  231. if (partitionId == null) return false;
  232. formatPartition(partitionId, fsType);
  233. return true;
  234. }
  235. }
  236. }