Partager via


Decryption_GetContentID.cpp

[The AD RMS SDK leveraging functionality exposed by the client in Msdrm.dll is available for use in Windows Server 2008, Windows Vista, Windows Server 2008 R2, Windows 7, Windows Server 2012, and Windows 8. It may be altered or unavailable in subsequent versions. Instead, use Active Directory Rights Management Services SDK 2.1, which leverages functionality exposed by the client in Msipc.dll.]

The following example shows how to retrieve a content ID value from the end-user license. This is the same content ID added to the issuance license in Encryption_GetOfflineSignedIL.cpp. When an end-user license is created for decryption, the content ID embedded in the issuance license is added to the end-user license. The GetContentID function shown here is called in Decryption_GetBoundLicense.cpp.

#include "DecryptingContent.h"

/*===================================================================
File:      Decryption_GetContentID.cpp

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Copyright (C) Microsoft.  All rights reserved.
===================================================================*/

/////////////////////////////////////////////////////////////////////
// The GetContentID function returns the address of a pointer to a
// string that contains the ID for the content to be decrypted. The
// content ID is in the end user license.
//
HRESULT GetContentID(
              PWSTR pwszEUL,
              PWSTR* ppwszContentID)
{
  ///////////////////////////////////////////////////////////////////
  // Declare variables:
  //   hr.....................Return value      
  //   hQueryHandle...........The work [content] object in the EUL
  //   hSubQueryHandle........The content ID in the EUL (and the IL)
  //   pwszContentID..........The content ID in the EUL (and the IL)
  //   uiIdValue..............Number of characters in the content ID
  //   encodingType...........Encoding type of item queried for
  //
  HRESULT           hr                  = S_OK;
  DRMQUERYHANDLE    hQueryHandle        = NULL;
  DRMQUERYHANDLE    hSubQueryHandle     = NULL;
  PWSTR             pwszContentID       = NULL;
  UINT              uiIdValue           = 0;
  DRMENCODINGTYPE   encodingType;

  wprintf(L"\r\nEntering GetContentID.\r\n");

  // Parse the unbound end user license (EUL) to retrieve the 
 // EUL handle.
 hr = DRMParseUnboundLicense(
            pwszEUL,                  // End user license
            &hQueryHandle );          // EUL handle
 if (FAILED(hr)) goto e_Exit;
  wprintf(L"DRMParseUnboundLicense: hQueryHandle = %i\r\n", 
          hQueryHandle);

 // Retrieve the handle of the WORK object.
 hr = DRMGetUnboundLicenseObject(
            hQueryHandle,             // EUL handle
            L"work",                  // Object to find
            0,                        // Object index
            &hSubQueryHandle);        // Handle to the WORK object
 if (FAILED(hr)) goto e_Exit;
  wprintf(L"DRMGetUnboundLicenseObject: hSubQueryHandle = %i\r\n", 
          hSubQueryHandle);

  // Retrieve the content ID value
  //   - Call DRMGetUnboundLicenseAttribute to retrieve the
  //     length of the content ID, including the terminating
  //     null character.
  //   - Allocate memory for the content ID.
  //   - Call DRMGetUnboundLicenseAttribute again to retrieve
  //     the content ID.
  hr = DRMGetUnboundLicenseAttribute( 
            hSubQueryHandle,          // WORK object handle
            L"id-value",              // Attribute to retrieve
            0,                        // Attribute index
            &encodingType,            // Encoding type of return
            &uiIdValue,               // Return value length
            NULL);
 if (FAILED(hr)) goto e_Exit;

  // Allocate memory for the content ID.
  pwszContentID = new WCHAR[uiIdValue];
 if (NULL == pwszContentID)
 {
  hr = E_OUTOFMEMORY;
  goto e_Exit;
 }

 hr = DRMGetUnboundLicenseAttribute( 
            hSubQueryHandle, 
            L"id-value", 
            0, 
            &encodingType, 
            &uiIdValue, 
            (BYTE*)pwszContentID);
 if (FAILED(hr)) goto e_Exit;
  wprintf(L"DRMGetUnboundLicenseAttribute: pwszContentID = %s\r\n", 
          pwszContentID);  

  // Return value.
  *ppwszContentID = pwszContentID;

e_Exit:
  if (NULL != pwszContentID)
  {
    pwszContentID = NULL;
  }
 if (NULL != hQueryHandle)
 {
  hr = DRMCloseQueryHandle(hQueryHandle);
    hQueryHandle = NULL;
 }
 if (NULL != hSubQueryHandle)
 {
  hr = DRMCloseQueryHandle(hSubQueryHandle);
    hSubQueryHandle = NULL;
 }

  wprintf(L"Leaving GetContentID: hr = %x\r\n", hr);
  return hr;
}

Decrypting Content

Decrypting Content Code Example

Encrypting Content