AipFileManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Pipes;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using Microsoft.InformationProtection;
  8. using Microsoft.InformationProtection.File;
  9. using Microsoft.InformationProtection.Policy;
  10. using Microsoft.InformationProtection.Protection;
  11. using LogLevel = Microsoft.InformationProtection.LogLevel;
  12. namespace AipGateway.AIP
  13. {
  14. public sealed class AipFileManager
  15. {
  16. public int LastErrNo { get; internal set; }
  17. public string LastErrMsg { get; internal set; }
  18. private readonly AipConfig _aipConfig;
  19. private readonly ApplicationInfo _appInfo;
  20. private AuthDelegateImplementation _authDelegate;
  21. private MipContext _mipContext;
  22. private readonly FileManager _fileManager = null;
  23. private readonly PolicyManager _policyManager = null;
  24. private readonly ProtectionManager _protectionManager = null;
  25. public AipFileManager(AipConfig aipConfig)
  26. {
  27. _aipConfig = aipConfig;
  28. _appInfo = new ApplicationInfo
  29. {
  30. ApplicationId = aipConfig.ClientId,
  31. ApplicationName = aipConfig.AppName,
  32. ApplicationVersion = aipConfig.AppVersion
  33. };
  34. LastErrNo = 0;
  35. LastErrMsg = string.Empty;
  36. _fileManager = new FileManager();
  37. _policyManager = new PolicyManager();
  38. _protectionManager = new ProtectionManager();
  39. }
  40. ~AipFileManager() => this.Dispose(false);
  41. public void Dispose()
  42. {
  43. this.Dispose(true);
  44. GC.SuppressFinalize((object)this);
  45. }
  46. private void Dispose(bool disposing)
  47. {
  48. lock (this)
  49. {
  50. if (_fileManager != null)
  51. {
  52. _fileManager.Dispose();
  53. }
  54. if (_policyManager != null)
  55. {
  56. _policyManager.Dispose();
  57. }
  58. if (_protectionManager != null)
  59. {
  60. _protectionManager.Dispose();
  61. }
  62. _authDelegate = null;
  63. }
  64. }
  65. private void SetError(int errNo, string errMsg1, string errMsg2 = "No Exception Message.")
  66. {
  67. LastErrNo = errNo;
  68. LastErrMsg = errMsg1 + "\r\n" + errMsg2;
  69. Console.WriteLine("AipFileManager::SetError ==> {0}, {1}, {2}", errNo, errMsg1, errMsg2);
  70. }
  71. public bool Initialize()
  72. {
  73. _authDelegate = new AuthDelegateImplementation(_aipConfig);
  74. try
  75. {
  76. // Initialize SDK DLLs. If DLLs are missing or wrong type, this will throw an exception
  77. MIP.Initialize(MipComponent.File);
  78. //MIP.Initialize(MipComponent.Policy);
  79. //MIP.Initialize(MipComponent.Protection); // Protection
  80. }
  81. catch (Exception ex) {
  82. SetError(1, "MIP.Initialize Failed.", ex.Message);
  83. return false;
  84. }
  85. // Create MipConfiguration Object
  86. var mipConfiguration = new MipConfiguration(_appInfo, _aipConfig.MipData, LogLevel.Trace, false);
  87. // mipConfiguration.LoggerDelegateOverride = this;
  88. //Enable DKE
  89. // Dictionary<FlightingFeature, bool> featureSettings = mipConfiguration.FeatureSettingsOverride ?? new Dictionary<FlightingFeature, bool>();
  90. // featureSettings[FlightingFeature.DoubleKey] = true;
  91. // mipConfiguration.FeatureSettingsOverride = featureSettings;
  92. // Create MipContext using MipConfiguration
  93. _mipContext = MIP.CreateMipContext(mipConfiguration);
  94. return true;
  95. }
  96. public bool CreateProfile()
  97. {
  98. if (_authDelegate == null || _mipContext == null)
  99. {
  100. SetError(999, "AipFileManager::CreateProfile Failed.", "Library Is not Initialized.");
  101. return false;
  102. }
  103. if (!_fileManager.CreateProfile(ref _mipContext))
  104. {
  105. SetError(11, "AipFileManager::CreateProfile ==> FileManager::CreateProfile Failed.");
  106. return false;
  107. }
  108. if (!_policyManager.CreateProfile(ref _mipContext))
  109. {
  110. SetError(12, "AipFileManager::CreateProfile ==> PolicyManager::CreateProfile Failed.");
  111. return false;
  112. }
  113. if (!_protectionManager.CreateProfile(ref _mipContext))
  114. {
  115. SetError(13, "AipFileManager::CreateProfile ==> ProtectionManager::CreateProfile Failed.");
  116. return false;
  117. }
  118. return true;
  119. }
  120. public bool CreateEngine()
  121. {
  122. if (_authDelegate == null || _mipContext == null)
  123. {
  124. SetError(999, "AipFileManager::CreateEngine Failed.", "Library Is not Initialized.");
  125. return false;
  126. }
  127. Identity identity = new Identity(_aipConfig.EMail);
  128. if (!_fileManager.CreateEngine(ref identity, ref _authDelegate))
  129. {
  130. SetError(21, "AipFileManager::CreateEngine ==> FileManager::CreateEngine Failed.");
  131. return false;
  132. }
  133. if (!_policyManager.CreateEngine(ref identity, ref _authDelegate))
  134. {
  135. SetError(22, "AipFileManager::CreateEngine ==> PolicyManager::CreateEngine Failed.");
  136. return false;
  137. }
  138. if (!_protectionManager.CreateEngine(ref identity, ref _authDelegate))
  139. {
  140. SetError(23, "AipFileManager::CreateEngine ==> ProtectionManager::CreateEngine Failed.");
  141. return false;
  142. }
  143. return true;
  144. }
  145. public bool IsProtected(Stream inputStream, string filePath)
  146. {
  147. IFileStatus status = FileHandler.GetFileStatus(inputStream, filePath, _mipContext);
  148. bool result = status.IsProtected();
  149. return result;
  150. }
  151. public bool IsLabeledOrProtected(Stream inputStream, string filePath)
  152. {
  153. IFileStatus status = FileHandler.GetFileStatus(inputStream, filePath, _mipContext);
  154. bool isLabeled = status.IsLabeled();
  155. bool isProtected = status.IsProtected();
  156. return (isLabeled || isProtected);
  157. }
  158. public AipFileInfo GetFileInfo(string fileName)
  159. {
  160. if (_authDelegate == null || _mipContext == null)
  161. {
  162. SetError(999, "AipFileManager::GetFileInfo Failed.", "Library is not Initialized.");
  163. return new AipFileInfo
  164. {
  165. errorCode = 10,
  166. errorMessage = "AIP 라이브러리가 초기화 되지 않았습니다.",
  167. };
  168. }
  169. AipFileInfo fileInfo = _fileManager.GetFileInfo(fileName);
  170. if (fileInfo == null)
  171. {
  172. SetError(_fileManager.LastErrNo, "AipFileManager::GetFileInfo Failed.", _fileManager.LastErrMsg);
  173. return new AipFileInfo
  174. {
  175. errorCode = _fileManager.LastErrNo == 0 ? 11 : _fileManager.LastErrNo,
  176. errorMessage = _fileManager.LastErrMsg,
  177. };
  178. }
  179. return fileInfo;
  180. }
  181. public AipFileInfo GetFileInfo(Stream fileStream, string outputFileName)
  182. {
  183. if (_authDelegate == null || _mipContext == null)
  184. {
  185. SetError(999, "AipFileManager::GetFileInfo Failed.", "Library Is not Initialized.");
  186. return new AipFileInfo
  187. {
  188. errorCode = 10,
  189. errorMessage = "AIP 라이브러리가 초기화 되지 않았습니다.",
  190. };
  191. }
  192. AipFileInfo fileInfo = _fileManager.GetFileInfo(fileStream, outputFileName);
  193. if (fileInfo == null)
  194. {
  195. SetError(_fileManager.LastErrNo, "AipFileManager::GetFileInfo Failed.", _fileManager.LastErrMsg);
  196. return new AipFileInfo
  197. {
  198. errorCode = _fileManager.LastErrNo == 0 ? 12 : _fileManager.LastErrNo,
  199. errorMessage = _fileManager.LastErrMsg,
  200. };
  201. }
  202. return fileInfo;
  203. }
  204. public List<AipLabel> SensitivityLabels()
  205. {
  206. if (_authDelegate == null || _mipContext == null)
  207. {
  208. SetError(999, "AipFileManager::SensitivityLabels Failed.", "Library Is not Initialized.");
  209. return null;
  210. }
  211. var result = new List<AipLabel>();
  212. try
  213. {
  214. var labels = _fileManager.SensitivityLabels();
  215. foreach (var label in labels)
  216. {
  217. var aipLabel = Utilities.LabelToAip(label);
  218. if (aipLabel != null)
  219. {
  220. if (label.Children.Count > 0)
  221. {
  222. foreach (var child in label.Children)
  223. {
  224. var aipChildLabel = Utilities.LabelToAip(child);
  225. if (aipChildLabel != null) aipLabel.Children.Add(aipChildLabel);
  226. }
  227. }
  228. result.Add(aipLabel);
  229. }
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. SetError(31, "AipFileManager::SensitivityLabels Failed.", ex.Message);
  235. result = new List<AipLabel>();
  236. }
  237. return result;
  238. }
  239. public List<AipLabel> ListSensitivityLabels()
  240. {
  241. if (_authDelegate == null || _mipContext == null)
  242. {
  243. SetError(999, "AipFileManager::ListSensitivityLabels Failed.", "Library Is not Initialized.");
  244. return null;
  245. }
  246. var result = new List<AipLabel>();
  247. try
  248. {
  249. var labels = _policyManager.ListSensitivityLabels();
  250. foreach (var label in labels)
  251. {
  252. var aipLabel = Utilities.LabelToAip(label);
  253. if (aipLabel != null)
  254. {
  255. if (label.Children.Count > 0)
  256. {
  257. foreach (var child in label.Children)
  258. {
  259. var aipChildLabel = Utilities.LabelToAip(child);
  260. if (aipChildLabel != null) aipLabel.Children.Add(aipChildLabel);
  261. }
  262. }
  263. result.Add(aipLabel);
  264. }
  265. }
  266. }
  267. catch (Exception ex)
  268. {
  269. SetError(32, "AipFileManager::ListSensitivityLabels Failed.", ex.Message);
  270. result = new List<AipLabel>();
  271. }
  272. return result;
  273. }
  274. public List<AipTemplate> GetTemplates()
  275. {
  276. if (_authDelegate == null || _mipContext == null)
  277. {
  278. SetError(999, "AipFileManager::GetTemplates Failed.", "Library Is not Initialized.");
  279. return null;
  280. }
  281. var result = new List<AipTemplate>();
  282. try
  283. {
  284. var templates = _protectionManager.GetTemplates();
  285. foreach (var template in templates)
  286. {
  287. var aipTemplate = Utilities.TemplateToAip(template);
  288. if (aipTemplate != null)
  289. {
  290. result.Add(aipTemplate);
  291. }
  292. }
  293. }
  294. catch (Exception ex)
  295. {
  296. SetError(33, "AipFileManager::GetTemplates Failed.", ex.Message);
  297. result = new List<AipTemplate>();
  298. }
  299. return result;
  300. }
  301. /// <summary>
  302. /// ///////////////////////////////////////////////////////
  303. /// </summary>
  304. /// <param name="fileName"></param>
  305. /// <param name="actualFileName"></param>
  306. /// <param name="email"></param>
  307. /// <param name="labelId"></param>
  308. /// <param name="templateId"></param>
  309. /// <param name="comments"></param>
  310. /// <returns></returns>
  311. public SetFileInfo SetLabel(string fileName, string actualFileName, string email, string labelId, string templateId, string comments = "")
  312. {
  313. // 레이블 및 템플릿 정보 가져오기
  314. return _fileManager.SetLabel(fileName, actualFileName, email, labelId, templateId, comments);
  315. }
  316. public SetFileInfo SetLabel(Stream fileStream, string actualFileName, string email, string labelId, string templateId, string comments = "")
  317. {
  318. // 레이블 및 템플릿 정보 가져오기
  319. return _fileManager.SetLabel(fileStream, actualFileName, email, labelId, templateId, comments);
  320. }
  321. /// <summary>
  322. /// ///////////////////////////////////////////////////////////
  323. /// </summary>
  324. /// <param name="fileName"></param>
  325. /// <param name="actualFileName"></param>
  326. /// <param name="email"></param>
  327. /// <param name="comments"></param>
  328. /// <param name="isDelProtection"></param>
  329. /// <returns></returns>
  330. public SetFileInfo DeleteLabel(string fileName, string actualFileName, string email, string comments = "", bool isDelProtection = false)
  331. {
  332. return _fileManager.DeleteLabel(fileName, actualFileName, email, comments, isDelProtection);
  333. }
  334. public SetFileInfo DeleteLabel(Stream fileStream, string actualFileName, string email, string comments = "", bool isDelProtection = false)
  335. {
  336. return _fileManager.DeleteLabel(fileStream, actualFileName, email, comments, isDelProtection);
  337. }
  338. /// <summary>
  339. /// //////////////////////////////////////////////////////////////
  340. /// </summary>
  341. /// <param name="fileName"></param>
  342. /// <param name="actualFileName"></param>
  343. /// <param name="email"></param>
  344. /// <param name="templateId"></param>
  345. /// <param name="comments"></param>
  346. /// <returns></returns>
  347. public SetFileInfo SetProtection(string fileName, string actualFileName, string email, string templateId, string comments = "")
  348. {
  349. return _fileManager.SetProtection(fileName, actualFileName, email, templateId, comments);
  350. }
  351. public SetFileInfo SetProtection(Stream fileStream, string actualFileName, string email, string templateId, string comments = "")
  352. {
  353. return _fileManager.SetProtection(fileStream, actualFileName, email, templateId, comments);
  354. }
  355. /// <summary>
  356. /// /////////////////////////////////////////////////////////////////////////
  357. /// </summary>
  358. /// <param name="fileName"></param>
  359. /// <param name="actualFileName"></param>
  360. /// <param name="email"></param>
  361. /// <param name="comments"></param>
  362. /// <returns></returns>
  363. public SetFileInfo DeleteProtection(string fileName, string actualFileName, string email, string comments = "")
  364. {
  365. return _fileManager.RemoveProtection(fileName, actualFileName, email, comments);
  366. }
  367. public SetFileInfo DeleteProtection(Stream fileStream, string actualFileName, string email, string comments = "")
  368. {
  369. return _fileManager.RemoveProtection(fileStream, actualFileName, email, comments);
  370. }
  371. public AipFileStatus GetAipFileStatus(string fileName)
  372. {
  373. try
  374. {
  375. var fileStatus = FileHandler.GetFileStatus(fileName, _mipContext);
  376. AipFileStatus result = new AipFileStatus
  377. {
  378. IsProtected = fileStatus.IsProtected(),
  379. IsLabeled = fileStatus.IsLabeled(),
  380. ContainsProtectedObjects = fileStatus.ContainsProtectedObjects()
  381. };
  382. return result;
  383. }
  384. catch (Exception ex)
  385. {
  386. SetError(81, "AipFileManager::GetAipFileStatus Failed.", ex.Message);
  387. }
  388. return null;
  389. }
  390. }
  391. }