Microsoft 資訊保護 SDK - 保護處理者概念

在 MIP 保護 SDK 中,它 mip::ProtectionHandler 揭示了加密與解密受保護串流與緩衝區、執行存取檢查、取得發佈授權,以及從受保護資訊中取得屬性的功能。

要求

要建立一個用來處理特定檔案的 ProtectionHandler,您需要:

  • 一個 mip::MipContext
  • 一個 mip::ProtectionProfile
  • mip::ProtectionEngine 已添加到 ProtectionProfile
  • 一個繼承mip::ProtectionHandler::Observer的類別。
  • mip::ProtectionDescriptor 或出版授權

建立保護處理程序

mip::ProtectionHandler 物件可用於 保護消耗 操作。 處理器可根據不同情境使用四個函式之一來建立。

  • mip::ProtectionEngine->CreateProtectionHandlerForConsumptionAsync()
  • mip::ProtectionEngine->CreateProtectionHandlerForConsumption()
  • mip::ProtectionEngine->CreateProtectionHandlerForPublishingAsync()
  • mip::ProtectionEngine->CreateProtectionHandlerForPublishing()

這些函數接受 a mip::ProtectionHandler::PublishingSettingsmip::ProtectionHandler::ConsumptionSettings 物件。

建立出版處理程式

建立發佈處理器需要三個步驟:

  1. 建立 mip::ProtectionDescriptor 物件。
  2. 使用 mip::ProtectionDescriptor 來實例化 mip::ProtectionHandler::PublishingSettings
  3. 呼叫 mip::ProtectionEngine::CreateProtectionHandlerForPublishingAsync() 傳遞物件 PublishingSettings 、觀察者與承諾。

從描述符建立

如果是保護尚未被保護的內容,或是對內容施加新的保護(即已解密),必須建立 a mip::ProtectionDescriptor 。 建構完成後,它用來實例化物件 mip::ProtectionHandler::PublishingSettings() 。 結果會透過mip::ProtectionHandler::Observer返回。

// Create the protection descriptor, passing in a templateId. 
auto descriptorBuilder = mip::ProtectionDescriptorBuilder::CreateFromTemplate(protectionOptions.templateId);
std::shared_ptr<mip::ProtectionDescriptor> descriptor = descriptorBuilder->Build();

// Define the handler promise, future, and observer.
auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<ProtectionHandler>>>();
auto handlerFuture = handlerPromise->get_future();
auto handlerObserver = std::make_shared<ProtectionHandlerObserverImpl>();

// Create the PublishingSettings object using the previously-created descriptor as input.
mip::ProtectionHandler::PublishingSettings publishingSettings = mip::ProtectionHandler::PublishingSettings(descriptor);

// Create/get the publishing handler from the publishing settings, observer, and promise.
mEngine->CreateProtectionHandlerForPublishingAsync(publishingSettings, handlerObserver, handlerPromise);
auto handler = handlerFuture.get();
return handler;

成功建立物件 ProtectionHandler 後,可以執行保護操作(加密/解密)。 必須從處理器取得發佈授權,並隨加密內容一併儲存。 發佈授權可透過呼叫取得: handler->GetSerializedPublishingLicense();

未取得相應出版授權的受保護內容 無法解密

建立消耗處理程序

建立消耗處理程序需要三個步驟:

  1. 從受保護內容中擷取序列化的出版授權 std::vector<uint8_t>
  2. 使用序列化發行授權來初始化mip::ProtectionHandler::ConsumptionSettings
  3. 呼叫 mip::ProtectionEngine::CreateProtectionHandlerForConsumptionAsync() 傳遞物件 ConsumptionSettings 、觀察者與承諾。

此範例假設出版授權已從某個來源讀取並儲存在 std::vector<uint8_t> serializedPublishingLicense

//TODO: Implement GetPublishingLicense()
//Snip implies that function reads PL from source file, database, stream, etc.
std::vector<uint8_t> serializedPublishingLicense = GetPublishingLicense(filePath);

// Define the handler promise, future, and observer.
auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<ProtectionHandler>>>();
auto handlerFuture = handlerPromise->get_future();
shared_ptr<ProtectionHandlerObserverImpl> handlerObserver = std::make_shared<ProtectionHandlerObserverImpl>();

// Create the consumption settings object from the publishing license.
mip::ProtectionHandler::ConsumptionSettings consumptionSettings = mip::ProtectionHandler::ConsumptionSettings(serializedPublishingLicense);

// Create/get the publishing handler from the publishing settings, observer, and promise.
mEngine->CreateProtectionHandlerForConsumptionAsync(consumptionSettings, handlerObserver, handlerPromise);
auto handler = handlerFuture.get();