ProtectionManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Microsoft.InformationProtection;
  5. using Microsoft.InformationProtection.Protection;
  6. using Serilog;
  7. using Serilog.Core;
  8. namespace AipGateway.AIP
  9. {
  10. public class ProtectionManager : AbstractManager
  11. {
  12. private IProtectionProfile _profile = null;
  13. private IProtectionEngine _engine = null;
  14. public ProtectionManager(Logger logger, string clientId) : base(logger, clientId)
  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.OnDiskEncrypted,
  39. CacheStorageType.InMemory,
  40. new ConsentDelegateImplementation());
  41. // IProtectionProfile은 특정 애플리케이션에 대한 모든 SDK 작업의 루트입니다.
  42. _profile = MIP.LoadProtectionProfile(profileSettings);
  43. }
  44. catch (Exception e)
  45. {
  46. SetError(1, "ProtectionManager::CreateProfile Failed.", e.Message);
  47. return false;
  48. }
  49. return _profile != null;
  50. }
  51. public override bool CreateEngine(ref Identity identity, ref AuthDelegateImplementation authDelegate)
  52. {
  53. try
  54. {
  55. authDelegate.ResetError();
  56. var engineSettings = new ProtectionEngineSettings(identity.Email, authDelegate, string.Empty, "")
  57. {
  58. Identity = identity
  59. };
  60. _engine = Task.Run(async () => await _profile.AddEngineAsync(engineSettings)).Result;
  61. }
  62. catch (Exception e)
  63. {
  64. if (authDelegate.LastErrNo != 0)
  65. {
  66. SetError(authDelegate.LastErrNo, "ProtectionManager::CreateEngine Failed.", authDelegate.LastErrMsg);
  67. }
  68. else
  69. {
  70. SetError(2, "ProtectionManager::CreateEngine Failed.", e.Message);
  71. }
  72. return false;
  73. }
  74. return _engine != null;
  75. }
  76. public List<TemplateDescriptor> GetTemplates()
  77. {
  78. return _engine.GetTemplates();
  79. }
  80. public IProtectionHandler CreatePublishingProtectionHandler(string templateId)
  81. {
  82. ProtectionDescriptor protectionDescriptor = new ProtectionDescriptor(templateId);
  83. PublishingSettings publishingSettings = new PublishingSettings(protectionDescriptor);
  84. IProtectionHandler protectionHandler = null;
  85. try
  86. {
  87. protectionHandler = _engine.CreateProtectionHandlerForPublishing(publishingSettings);
  88. }
  89. catch (Exception ex)
  90. {
  91. SetError(51, "ProtectionManager::CreatePublishingProtectionHandler Failed.", ex.Message);
  92. }
  93. return protectionHandler;
  94. }
  95. // Create a handler for consumption from the publishing license.
  96. public IProtectionHandler CreateConsumptionProtectionHandler(List<byte> serializedPublishingLicense, MipContext mipContext, string comments)
  97. {
  98. PublishingLicenseInfo plInfo = PublishingLicenseInfo.GetPublishingLicenseInfo(serializedPublishingLicense, mipContext);
  99. ConsumptionSettings consumptionSettings = new ConsumptionSettings(plInfo)
  100. {
  101. // This is a new required field for tracking content for Track and Revoke.
  102. ContentName = comments
  103. };
  104. IProtectionHandler protectionHandler = null;
  105. try
  106. {
  107. protectionHandler = _engine.CreateProtectionHandlerForConsumption(consumptionSettings);
  108. }
  109. catch (Exception ex)
  110. {
  111. SetError(52, "ProtectionManager::CreateConsumptionProtectionHandler Failed.", ex.Message);
  112. }
  113. return protectionHandler;
  114. }
  115. public byte[] Protect(IProtectionHandler handler, byte[] data)
  116. {
  117. long buffersize = handler.GetProtectedContentLength(data.Length, true);
  118. byte[] outputBuffer = new byte[buffersize];
  119. handler.EncryptBuffer(0, data, outputBuffer, true);
  120. return outputBuffer;
  121. }
  122. public byte[] Unprotect(IProtectionHandler handler, byte[] data)
  123. {
  124. long buffersize = data.Length;
  125. byte[] clearBuffer = new byte[buffersize];
  126. var bytesDecrypted = handler.DecryptBuffer(0, data, clearBuffer, true);
  127. byte[] outputBuffer = new byte[bytesDecrypted];
  128. for (int i = 0; i < bytesDecrypted; i++)
  129. {
  130. outputBuffer[i] = clearBuffer[i];
  131. }
  132. return outputBuffer;
  133. }
  134. }
  135. }