ProtectionManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.InformationProtection;
  7. using Microsoft.InformationProtection.Protection;
  8. namespace AipGateway.AIP
  9. {
  10. public class ProtectionManager : AbstractManager
  11. {
  12. private IProtectionProfile _profile = null;
  13. private IProtectionEngine _engine = null;
  14. public ProtectionManager()
  15. {
  16. }
  17. ~ProtectionManager() => this.Dispose(false);
  18. public override void Dispose()
  19. {
  20. this.Dispose(true);
  21. GC.SuppressFinalize((object)this);
  22. }
  23. protected virtual void Dispose(bool disposing)
  24. {
  25. lock (this)
  26. {
  27. if (_engine != null) _engine.Dispose();
  28. if (_profile != null) _profile.Dispose();
  29. _profile = null;
  30. _engine = null;
  31. }
  32. }
  33. public override bool CreateProfile(ref MipContext mipContext)
  34. {
  35. try
  36. {
  37. var profileSettings = new ProtectionProfileSettings(mipContext,
  38. CacheStorageType.OnDisk,
  39. new ConsentDelegateImplementation());
  40. // IProtectionProfile은 특정 애플리케이션에 대한 모든 SDK 작업의 루트입니다.
  41. _profile = MIP.LoadProtectionProfile(profileSettings);
  42. }
  43. catch (Exception e)
  44. {
  45. SetError(1, "ProtectionManager::CreateProfile Failed.", e.Message);
  46. return false;
  47. }
  48. return _profile != null;
  49. }
  50. public override bool CreateEngine(ref Identity identity, ref AuthDelegateImplementation authDelegate)
  51. {
  52. try
  53. {
  54. authDelegate.ResetError();
  55. var engineSettings = new ProtectionEngineSettings(identity.Email, authDelegate, string.Empty, "")
  56. {
  57. Identity = identity
  58. };
  59. _engine = Task.Run(async () => await _profile.AddEngineAsync(engineSettings)).Result;
  60. //Console.WriteLine("Protection Engine Templates ======================================================");
  61. //var templates = _engine.GetTemplates();
  62. //for (int ii = 0; ii < templates.Count; ii++)
  63. //{
  64. // Console.WriteLine("{0}: {1}, {2}", ii.ToString(), templates[ii].Id + " : " + templates[ii].Name, templates[ii].Description);
  65. //}
  66. //Console.WriteLine("=======================================================================");
  67. }
  68. catch (Exception e)
  69. {
  70. if (authDelegate.LastErrNo != 0)
  71. {
  72. SetError(authDelegate.LastErrNo, "ProtectionManager::CreateEngine Failed.", authDelegate.LastErrMsg);
  73. }
  74. else
  75. {
  76. SetError(2, "ProtectionManager::CreateEngine Failed.", e.Message);
  77. }
  78. return false;
  79. }
  80. return _engine != null;
  81. }
  82. public List<TemplateDescriptor> GetTemplates()
  83. {
  84. return _engine.GetTemplates();
  85. }
  86. public IProtectionHandler CreatePublishingProtectionHandler(string templateId)
  87. {
  88. ProtectionDescriptor protectionDescriptor = new ProtectionDescriptor(templateId);
  89. PublishingSettings publishingSettings = new PublishingSettings(protectionDescriptor);
  90. IProtectionHandler protectionHandler = null;
  91. try
  92. {
  93. protectionHandler = _engine.CreateProtectionHandlerForPublishing(publishingSettings);
  94. }
  95. catch (Exception ex)
  96. {
  97. SetError(51, "ProtectionManager::CreatePublishingProtectionHandler Failed.", ex.Message);
  98. }
  99. return protectionHandler;
  100. }
  101. // Create a handler for consumption from the publishing license.
  102. public IProtectionHandler CreateConsumptionProtectionHandler(List<byte> serializedPublishingLicense, MipContext mipContext, string comments)
  103. {
  104. PublishingLicenseInfo plInfo = PublishingLicenseInfo.GetPublishingLicenseInfo(serializedPublishingLicense, mipContext);
  105. ConsumptionSettings consumptionSettings = new ConsumptionSettings(plInfo)
  106. {
  107. // This is a new required field for tracking content for Track and Revoke.
  108. ContentName = comments
  109. };
  110. IProtectionHandler protectionHandler = null;
  111. try
  112. {
  113. protectionHandler = _engine.CreateProtectionHandlerForConsumption(consumptionSettings);
  114. }
  115. catch (Exception ex)
  116. {
  117. SetError(52, "ProtectionManager::CreateConsumptionProtectionHandler Failed.", ex.Message);
  118. }
  119. return protectionHandler;
  120. }
  121. public byte[] Protect(IProtectionHandler handler, byte[] data)
  122. {
  123. long buffersize = handler.GetProtectedContentLength(data.Length, true);
  124. byte[] outputBuffer = new byte[buffersize];
  125. handler.EncryptBuffer(0, data, outputBuffer, true);
  126. return outputBuffer;
  127. }
  128. public byte[] Unprotect(IProtectionHandler handler, byte[] data)
  129. {
  130. long buffersize = data.Length;
  131. byte[] clearBuffer = new byte[buffersize];
  132. var bytesDecrypted = handler.DecryptBuffer(0, data, clearBuffer, true);
  133. byte[] outputBuffer = new byte[bytesDecrypted];
  134. for (int i = 0; i < bytesDecrypted; i++)
  135. {
  136. outputBuffer[i] = clearBuffer[i];
  137. }
  138. return outputBuffer;
  139. }
  140. public bool SetProtect(string fileName, string actualFileName, string email, string templateId, string comments)
  141. {
  142. // var outFileName = actualFileName == string.Empty ? fileName : actualFileName;
  143. // var handler = CreateFileHandler(fileName, outFileName);
  144. // if (handler == null)
  145. // {
  146. // return false;
  147. // }
  148. //
  149. // var publishHandler = CreatePublishingProtectionHandler(templateId);
  150. // if (publishHandler == null)
  151. // {
  152. // return false;
  153. // }
  154. //
  155. //
  156. // var protectionDescriptor = new ProtectionDescriptor(templateId);
  157. //
  158. // try
  159. // {
  160. // handler.SetProtection(new ProtectionDescriptor(templateId));
  161. // }
  162. // catch (Exception ex)
  163. // {
  164. // SetError(52, "FileManager::SetLabel Failed.", ex.Message);
  165. // return false;
  166. // }
  167. //
  168. // bool result = false;
  169. // if (handler.IsModified())
  170. // {
  171. // result = Task.Run(async () => await handler.CommitAsync(outFileName)).Result;
  172. // }
  173. //
  174. // if (result)
  175. // {
  176. // handler.NotifyCommitSuccessful(fileName);
  177. // }
  178. // else
  179. // {
  180. // SetError(53, "FileManager::SetProtect Failed.", "Template Id: " + templateId + ", SetProtect Failed.");
  181. // }
  182. // return result;
  183. return true;
  184. }
  185. public void GetTemplateById(string templateId)
  186. {
  187. }
  188. }
  189. }