Exemple #1
0
        // This method has only one caller GetGrantsFromBoundUseLicense(), which is
        // in the 

#if DEBUG
        static private DateTime GetBoundLicenseDateTimeAttribute(
            SafeRightsManagementHandle queryHandle,
            string attributeType,
            uint attributeIndex,
            DateTime defaultValue)
        {
            uint attributeSize = SystemTime.Size;
            byte[] dataBuffer = new byte[attributeSize];
            uint encodingType;

            int hr = SafeNativeMethods.DRMGetBoundLicenseAttribute(
                queryHandle, attributeType, attributeIndex, out encodingType,
                ref attributeSize, dataBuffer);

            if (encodingType != (uint)LicenseAttributeEncoding.Time)
            {
                throw new RightsManagementException(RightsManagementFailureCode.InvalidLicense);
            }

            if ((hr == (int)RightsManagementFailureCode.NoMoreData) ||
                 (hr == (int)RightsManagementFailureCode.QueryReportsNoResults))
            {
                return defaultValue;
            }
            Errors.ThrowOnErrorCode(hr);

            Debug.Assert(attributeSize == SystemTime.Size); // if isn't true it is an indication of a problem in the underlying libraries

            SystemTime sysTime = new SystemTime(dataBuffer);

            return sysTime.GetDateTime(defaultValue);
        }
        private void GetIssuanceLicenseInfo(
                                                    out DateTime timeFrom,
                                                    out DateTime timeUntil,
                                                    DistributionPointInfo distributionPointInfo,
                                                    out string distributionPointName,
                                                    out string distributionPointUri,
                                                    out ContentUser owner,
                                                    out bool officialFlag)
        {
            uint distributionPointNameLength = 0;
            uint distributionPointUriLength = 0;
            bool officialFlagTemp = false;
            SafeRightsManagementPubHandle ownerHandleTemp = null;

            int hr = SafeNativeMethods.DRMGetIssuanceLicenseInfo(
                                                                        _issuanceLicenseHandle,
                                                                        null,
                                                                        null,
                                                                        (uint)distributionPointInfo,
                                                                        ref distributionPointNameLength,
                                                                        null,
                                                                        ref distributionPointUriLength,
                                                                        null,
                                                                        out ownerHandleTemp,
                                                                        out officialFlagTemp);
            Errors.ThrowOnErrorCode(hr);

            if (ownerHandleTemp != null)
            {
                // As a result of calling DRMGetIssuanceLicenseInfo twice,
                // we are getting 2 handles. We are going to dispose the first one 
                // and preserve the second one.
                ownerHandleTemp.Dispose();
                ownerHandleTemp = null;
            }

            StringBuilder distributionPointNameTemp = null;
            // allocate memory as necessary, it seems that Unmanaged libraries really do not like
            // getting a non null buffer of size 0 
            if (distributionPointNameLength > 0)
            {
                distributionPointNameTemp = new StringBuilder(checked((int)distributionPointNameLength));
            }

            StringBuilder distributionPointUriTemp = null;
            // allocate memory as necessary, it seems that Unmanaged libraries really do not like
            // getting a non null buffer of size 0 
            if (distributionPointUriLength > 0)
            {
                distributionPointUriTemp = new StringBuilder(checked((int)distributionPointUriLength));
            }

            SystemTime timeFromTemp = new SystemTime(DateTime.Now);
            SystemTime timeUntilTemp = new SystemTime(DateTime.Now);

            hr = SafeNativeMethods.DRMGetIssuanceLicenseInfo(
                                                                        _issuanceLicenseHandle,
                                                                        timeFromTemp,
                                                                        timeUntilTemp,
                                                                        (uint)distributionPointInfo,
                                                                        ref distributionPointNameLength,
                                                                        distributionPointNameTemp,
                                                                        ref distributionPointUriLength,
                                                                        distributionPointUriTemp,
                                                                        out ownerHandleTemp,
                                                                        out officialFlagTemp);
            Errors.ThrowOnErrorCode(hr);

            timeFrom = timeFromTemp.GetDateTime(DateTime.MinValue);
            timeUntil = timeUntilTemp.GetDateTime(DateTime.MaxValue);

            // only if we got some data back we shall try to process it 
            if (distributionPointNameTemp != null)
            {
                distributionPointName = distributionPointNameTemp.ToString();
            }
            else
            {
                distributionPointName = null;
            }

            // only if we got some data back we shall try to process it 
            if (distributionPointUriTemp != null)
            {
                distributionPointUri = distributionPointUriTemp.ToString();
            }
            else
            {
                distributionPointUri = null;
            }

            // if we have owner let's convert it to a user and preserve 
            // handler for further destruction
            owner = null;
            if (ownerHandleTemp != null)
            {
                _pubHandlesList.Add(ownerHandleTemp);

                if (!ownerHandleTemp.IsInvalid)
                {
                    owner = GetUserFromHandle(ownerHandleTemp);
                }
            }

            officialFlag = officialFlagTemp;
        }
        private RevocationPoint GetRevocationPoint()
        {
            uint idLength = 0;
            uint idTypeLength = 0;
            uint urlLength = 0;
            uint nameLength = 0;
            uint publicKeyLength = 0;
            SystemTime frequency = new SystemTime(DateTime.Now);

            int hr = SafeNativeMethods.DRMGetRevocationPoint(
                                _issuanceLicenseHandle,
                                ref idLength,
                                null,
                                ref idTypeLength,
                                null,
                                ref urlLength,
                                null,
                                frequency,
                                ref nameLength,
                                null,
                                ref publicKeyLength,
                                null);
            if (hr == (int)RightsManagementFailureCode.RevocationInfoNotSet)
            {
                return null;
            }

            Errors.ThrowOnErrorCode(hr);

            // allocate memory as necessary, it seems that Unmanaged libraries really do not like
            // getting a non null buffer of size 0 
            StringBuilder idTemp = null;
            if (idLength > 0)
            {
                idTemp = new StringBuilder(checked((int)idLength));
            }

            StringBuilder idTypeTemp = null;
            if (idTypeLength > 0)
            {
                idTypeTemp = new StringBuilder(checked((int)idTypeLength));
            }

            StringBuilder urlTemp = null;
            if (urlLength > 0)
            {
                urlTemp = new StringBuilder(checked((int)urlLength));
            }

            StringBuilder nameTemp = null;
            if (nameLength > 0)
            {
                nameTemp = new StringBuilder(checked((int)nameLength));
            }

            StringBuilder publicKeyTemp = null;
            if (publicKeyLength > 0)
            {
                publicKeyTemp = new StringBuilder(checked((int)publicKeyLength));
            }

            hr = SafeNativeMethods.DRMGetRevocationPoint(
                                _issuanceLicenseHandle,
                                ref idLength,
                                idTemp,
                                ref idTypeLength,
                                idTypeTemp,
                                ref urlLength,
                                urlTemp,
                                frequency,
                                ref nameLength,
                                nameTemp,
                                ref publicKeyLength,
                                publicKeyTemp);
            Errors.ThrowOnErrorCode(hr);

            RevocationPoint resultRevocationPoint = new RevocationPoint();

            resultRevocationPoint.Id = (idTemp == null) ? null : idTemp.ToString();
            resultRevocationPoint.IdType = (idTypeTemp == null) ? null : idTypeTemp.ToString();
            resultRevocationPoint.Url = (urlTemp == null) ? null : new Uri(urlTemp.ToString());
            resultRevocationPoint.Name = (nameTemp == null) ? null : nameTemp.ToString();
            resultRevocationPoint.PublicKey = (publicKeyTemp == null) ? null : publicKeyTemp.ToString();
            resultRevocationPoint.Frequency = frequency;

            return resultRevocationPoint;
        }
        static private Nullable<ContentRight> GetRightFromHandle(SafeRightsManagementPubHandle rightHandle,
                                                        out DateTime validFrom,
                                                        out DateTime validUntil)
        {
            uint rightNameLength = 0;
            StringBuilder rightName;

            int hr = SafeNativeMethods.DRMGetRightInfo(rightHandle,
                                                                                ref rightNameLength,
                                                                                null,
                                                                                null,
                                                                                null);
            Errors.ThrowOnErrorCode(hr);

            rightName = new StringBuilder(checked((int)rightNameLength));
            SystemTime validFromSysTime = new SystemTime(DateTime.Now);
            SystemTime validUntilSysTime = new SystemTime(DateTime.Now);

            hr = SafeNativeMethods.DRMGetRightInfo(rightHandle,
                                                                                ref rightNameLength,
                                                                                rightName,
                                                                                validFromSysTime,
                                                                                validUntilSysTime);
            Errors.ThrowOnErrorCode(hr);

            validFrom = validFromSysTime.GetDateTime(DateTime.MinValue);
            validUntil = validUntilSysTime.GetDateTime(DateTime.MaxValue);

            return ClientSession.GetRightFromString(rightName.ToString());
        }
        /// <summary>
        /// constructor that buils an issuance license from scratch
        /// </summary>
        private void Initialize(
                                        DateTime validFrom,
                                        DateTime validUntil,
                                        string referralInfoName,
                                        Uri referralInfoUri,
                                        ContentUser owner,
                                        string issuanceLicense,
                                        SafeRightsManagementHandle boundLicenseHandle,
                                        Guid contentId,
                                        ICollection<ContentGrant> grantCollection,
                                        IDictionary<int, LocalizedNameDescriptionPair> localizedNameDescriptionDictionary,
                                        IDictionary<string, string> applicationSpecificDataDictionary,
                                        int rightValidityIntervalDays,
                                        RevocationPoint revocationPoint)
        {
            // according to the unmanaged RM SDK spec only the following scenarios are supported:
            // 1. This can be called to create an issuance license from a template. 
            //       issuanceLicense         An unsigned issuance license from 
            //                                   a file or by passing an issuance license 
            //                                   handle into DRMGetIssuanceLicenseTemplate 
            //       boundLicenseHandle   NULL
            //
            // 2. This allows you to reuse rights information (the list follows this table).
            //       issuance license        A signed issuance license
            //       boundLicenseHandle   Handle to license bound by OWNER or VIEWRIGHTSDATA right
            //
            // 3. This creates an issuance license from scratch. It includes no users, rights, metadata, or policies.
            //       issuance license         NULL
            //       boundLicenseHandle   NULL

            Debug.Assert(!boundLicenseHandle.IsClosed); // it must be either present or not
            // closed handle is an indication of some internal error

            Invariant.Assert((boundLicenseHandle.IsInvalid) || (issuanceLicense != null));

            SystemTime validFromSysTime = null;
            SystemTime validUntilSysTime = null;

            if ((validFrom != DateTime.MinValue) || (validUntil != DateTime.MaxValue))
            {
                // we need to use non null values if at least one of the time boundaries isn't default
                // DRM SDK will not enforce date time unless both timeFrom and timeUnti are set 
                validFromSysTime = new SystemTime((DateTime)validFrom);
                validUntilSysTime = new SystemTime((DateTime)validUntil);
            }

            string referralInfoUriStr = null;
            if (referralInfoUri != null)
            {
                referralInfoUriStr = referralInfoUri.ToString();
            }

            // input parameter must be initialized to the invalid handle 
            // attempt to pass in a null throws an exception from the Safe 
            // Handle Marshalling code  
            SafeRightsManagementPubHandle ownerHandle;

            if (owner != null)
            {
                ownerHandle = GetHandleFromUser(owner);
            }
            else
            {
                ownerHandle = SafeRightsManagementPubHandle.InvalidHandle;
            }

            int hr;

            _issuanceLicenseHandle = null;

            hr = SafeNativeMethods.DRMCreateIssuanceLicense(
                validFromSysTime,
                validUntilSysTime,
                referralInfoName,
                referralInfoUriStr,
                ownerHandle,
                issuanceLicense,
                boundLicenseHandle,
                out _issuanceLicenseHandle);

            Errors.ThrowOnErrorCode(hr);
            Invariant.Assert((_issuanceLicenseHandle != null) &&
                                       (!_issuanceLicenseHandle.IsInvalid));

            Debug.Assert(rightValidityIntervalDays >= 0); // our internal code makes the guarantee that is is not negative
            if (rightValidityIntervalDays > 0)
            {
                // If it is 0 we shouldn't override the value as it might be coming from a template 
                SafeNativeMethods.DRMSetIntervalTime(_issuanceLicenseHandle, (uint)rightValidityIntervalDays);
            }

            if (grantCollection != null)
            {
                foreach (ContentGrant grant in grantCollection)
                {
                    AddGrant(grant);
                }
            }

            // Set localized name description info 
            if (localizedNameDescriptionDictionary != null)
            {
                foreach (KeyValuePair<int, LocalizedNameDescriptionPair> nameDescriptionEntry in localizedNameDescriptionDictionary)
                {
                    AddNameDescription(nameDescriptionEntry.Key, nameDescriptionEntry.Value);
                }
            }

            // Set application specific data 
            if (applicationSpecificDataDictionary != null)
            {
                foreach (KeyValuePair<string, string> applicationSpecificDataEntry in applicationSpecificDataDictionary)
                {
                    AddApplicationSpecificData(applicationSpecificDataEntry.Key, applicationSpecificDataEntry.Value);
                }
            }

            // set metafata as required 
            if (contentId != null)
            {
                hr = SafeNativeMethods.DRMSetMetaData(
                    _issuanceLicenseHandle,
                    contentId.ToString("B"),
                    DefaultContentType,
                    null,
                    null,
                    null,
                    null);

                Errors.ThrowOnErrorCode(hr);
            }

            // set revocation point if required 
            if (revocationPoint != null)
            {
                SetRevocationPoint(revocationPoint);
            }
        }
 internal static int DRMSetRevocationPoint(
                         SafeRightsManagementPubHandle issuanceLicenseHandle, 
                         bool flagDelete,
                         string id,
                         string idType,
                         string url, 
                         SystemTime frequency,
                         string name, 
                         string publicKey) 
 {
     SecurityHelper.DemandRightsManagementPermission(); 
     return UnsafeNativeMethods.DRMSetRevocationPoint(
                         issuanceLicenseHandle,
                         flagDelete,
                         id, 
                         idType,
                         url, 
                         frequency, 
                         name,
                         publicKey); 
 }
        private SafeRightsManagementPubHandle GetRightHandle(ContentGrant grant)
        {
            SafeRightsManagementPubHandle rightHandle = null;

            // we only need to use date time inteval if at least one of the values isn't default to Min max 
            // If both of the Min Max we can leave tyhem as nulls otherwise (if at least one not default)
            // we need to set both 
            SystemTime systemTimeValidFrom = null;
            SystemTime systemTimeValidUntil = null;

            if ((grant.ValidFrom != DateTime.MinValue) || (grant.ValidUntil != DateTime.MaxValue))
            {
                // we need to use non null values if at least one of the time boundaries isn't default
                // DRM SDK will not enforce date time unless both timeFrom and timeUnti are set 
                systemTimeValidFrom = new SystemTime(grant.ValidFrom);
                systemTimeValidUntil = new SystemTime(grant.ValidUntil);
            }

            int hr = SafeNativeMethods.DRMCreateRight(
                ClientSession.GetStringFromRight(grant.Right),
                systemTimeValidFrom,    // SystemTime timeFrom, 
                systemTimeValidUntil,    // SystemTime timeUntil,
                0,       // countExtendedInfo,    
                null,    // string [] extendedInfoNames,
                null,    // string [] extendedInfoValues,
                out rightHandle);

            Errors.ThrowOnErrorCode(hr);
            Debug.Assert((rightHandle != null) && (!rightHandle.IsInvalid));

            _pubHandlesList.Add(rightHandle);

            return rightHandle;
        }
 internal static int DRMGetRevocationPoint( 
                         SafeRightsManagementPubHandle issuanceLicenseHandle,
                         ref uint idLength, 
                         StringBuilder id, 
                         ref uint idTypeLength,
                         StringBuilder idType, 
                         ref uint urlLength,
                         StringBuilder url,
                         SystemTime frequency,
                         ref uint nameLength, 
                         StringBuilder name,
                         ref uint publicKeyLength, 
                         StringBuilder publicKey) 
 {
     SecurityHelper.DemandRightsManagementPermission(); 
     return UnsafeNativeMethods.DRMGetRevocationPoint(
                         issuanceLicenseHandle,
                         ref idLength,
                         id, 
                         ref idTypeLength,
                         idType, 
                         ref urlLength, 
                         url,
                         frequency, 
                         ref nameLength,
                         name,
                         ref publicKeyLength,
                         publicKey); 
 }
 internal static int DRMGetIssuanceLicenseInfo(
                          SafeRightsManagementPubHandle issuanceLicenseHandle, 
                          SystemTime timeFrom,
                          SystemTime timeUntil, 
                          uint flags, 
                          ref uint distributionPointNameLength,
                          StringBuilder DistributionPointName, 
                          ref uint distributionPointUriLength,
                          StringBuilder DistributionPointUri,
                          out SafeRightsManagementPubHandle ownerHandle,
                          out bool officialFlag) 
 {
     SecurityHelper.DemandRightsManagementPermission(); 
     int res = UnsafeNativeMethods.DRMGetIssuanceLicenseInfo( 
                         issuanceLicenseHandle,
                         timeFrom, 
                         timeUntil,
                         flags,
                         ref distributionPointNameLength,
                         DistributionPointName, 
                         ref distributionPointUriLength,
                         DistributionPointUri, 
                         out ownerHandle, 
                         out officialFlag);
     // on some platforms in the failure cases the out parameter is being created with the value 0 
     // in order to simplify error handling and Disposing of those handles we will just close them as
     // soon as we detect such case
     if ((ownerHandle != null) && ownerHandle.IsInvalid)
     { 
         ownerHandle.Dispose();
         ownerHandle = null; 
     } 
     return res;
 } 
 internal static int DRMCreateRight(
                          string rightName, 
                          SystemTime timeFrom,
                          SystemTime timeUntil,
                          uint countExtendedInfo,
                          string[] extendedInfoNames, 
                          string[] extendedInfoValues,
                          out SafeRightsManagementPubHandle rightHandle) 
 { 
     SecurityHelper.DemandRightsManagementPermission();
     int res = UnsafeNativeMethods.DRMCreateRight( 
                         rightName,
                         timeFrom,
                         timeUntil,
                         countExtendedInfo, 
                         extendedInfoNames,
                         extendedInfoValues, 
                         out rightHandle); 
     // on some platforms in the failure cases the out parameter is being created with the value 0
     // in order to simplify error handling and Disposing of those handles we will just close them as 
     // soon as we detect such case
     if ((rightHandle != null) && rightHandle.IsInvalid)
     {
         rightHandle.Dispose(); 
         rightHandle = null;
     } 
     return res; 
 }
 internal static int DRMGetRightInfo(
                          SafeRightsManagementPubHandle rightHandle,
                          ref uint rightNameLength,
                          StringBuilder rightName, 
                          SystemTime timeFrom,
                          SystemTime timeUntil) 
 { 
     SecurityHelper.DemandRightsManagementPermission();
     return UnsafeNativeMethods.DRMGetRightInfo( 
                         rightHandle,
                         ref rightNameLength,
                         rightName,
                         timeFrom, 
                         timeUntil);
 } 
        internal static int DRMCreateIssuanceLicense(
                                 SystemTime timeFrom, 
                                 SystemTime timeUntil, 
                                 string referralInfoName,
                                 string referralInfoUrl, 
                                 SafeRightsManagementPubHandle ownerUserHandle,
                                 string issuanceLicense,
                                 SafeRightsManagementHandle boundLicenseHandle,
                                 out SafeRightsManagementPubHandle issuanceLicenseHandle) 
        {
            SecurityHelper.DemandRightsManagementPermission(); 
            int res = UnsafeNativeMethods.DRMCreateIssuanceLicense( 
                                timeFrom,
                                timeUntil, 
                                referralInfoName,
                                referralInfoUrl,
                                ownerUserHandle,
                                issuanceLicense, 
                                boundLicenseHandle,
                                out issuanceLicenseHandle); 
 
            // on some platforms in the failure cases the out parameter is being created with the value 0
            // in order to simplify error handling and Disposing of those handles we will just close them as 
            // soon as we detect such case
            if ((issuanceLicenseHandle != null) && issuanceLicenseHandle.IsInvalid)
            {
                issuanceLicenseHandle.Dispose(); 
                issuanceLicenseHandle = null;
            } 
            return res; 
        }