Exemplo n.º 1
0
        /// <summary>
        /// Removes locators, delivery policies and keys associated with an asset
        /// </summary>
        /// <param name="asset"></param>
        private static void CleanUpAsset(IAsset asset)
        {
            foreach (var locator in asset.Locators)
            {
                ILocator locatorRefreshed = _context.Locators.Where(p => p.Id == locator.Id).FirstOrDefault();
                if (locatorRefreshed != null)
                {
                    locatorRefreshed.Delete();
                }
            }

            var deliveryPolicies = asset.DeliveryPolicies.ToList();

            foreach (var deliveryPolicy in deliveryPolicies)
            {
                asset.DeliveryPolicies.Remove(deliveryPolicy);
                var deliveryPolicyRefreshed = _context.AssetDeliveryPolicies.Where(p => p.Id == deliveryPolicy.Id).FirstOrDefault();
                if (deliveryPolicyRefreshed != null)
                {
                    deliveryPolicyRefreshed.Delete();
                }
            }

            var keys = asset.ContentKeys.ToList();

            foreach (var key in keys)
            {
                asset.ContentKeys.Remove(key);
                IContentKey keyRefreshed = _context.ContentKeys.Where(p => p.Id == key.Id).FirstOrDefault();
                if (keyRefreshed != null)
                {
                    keyRefreshed.Delete();
                }
            }
        }
 private static void UpdateDeleteContentKey(IContentKey key)
 {
     key.AuthorizationPolicyId = Guid.NewGuid().ToString();
     key.Update();
     key.AuthorizationPolicyId = Guid.NewGuid().ToString();
     key.UpdateAsync();
     key.Delete();
 }
        private static Dictionary <Asset​Delivery​Policy​Configuration​Key, String> CreateDeliveryPolicyConfiguration(List <string> assetDeliveryPolicyContentProtectionNames, IContentKeyAuthorizationPolicyOption fairplayPolicyOpiton)
        {
            Dictionary <Asset​Delivery​Policy​Configuration​Key, String> configKeys = new Dictionary <Asset​Delivery​Policy​Configuration​Key, String>();

            foreach (var c in assetDeliveryPolicyContentProtectionNames)
            {
                MediaServicesHelper.AssetDeliveryContentProtection cp = MediaServicesHelper.AMSAssetDeliveryContentProtection[c];
                switch (cp)
                {
                case MediaServicesHelper.AssetDeliveryContentProtection.AESClearKey:
                    //  Get the Key Delivery Base Url by removing the Query parameter.
                    //  The Dynamic Encryption service will automatically add the correct key identifier to the url
                    //  when it generates the Envelope encrypted content manifest.  Omitting the IV will also cause
                    //  the Dynamice Encryption service to generate a deterministic IV for the content automatically.
                    //  By using the EnvelopeBaseKeyAcquisitionUrl and omitting the IV, this allows the AssetDelivery
                    //  policy to be reused by more than one asset.
                    IContentKey dummyKey1            = CreateDummyContentKey(ContentKeyType.EnvelopeEncryption);
                    Uri         aesKeyAcquisitionUri = dummyKey1.GetKeyDeliveryUrl(ContentKeyDeliveryType.BaselineHttp);
                    UriBuilder  uriBuilder1          = new UriBuilder(aesKeyAcquisitionUri);
                    uriBuilder1.Query    = String.Empty;
                    aesKeyAcquisitionUri = uriBuilder1.Uri;
                    // The following policy configuration specifies:
                    //   key url that will have KID=<Guid> appended to the envelope and
                    //   the Initialization Vector (IV) to use for the envelope encryption.
                    configKeys.Add(AssetDeliveryPolicyConfigurationKey.EnvelopeBaseKeyAcquisitionUrl, aesKeyAcquisitionUri.ToString());
                    dummyKey1.Delete();
                    break;

                case MediaServicesHelper.AssetDeliveryContentProtection.PlayReady:
                    IContentKey dummyKey2 = CreateDummyContentKey(ContentKeyType.CommonEncryption);
                    Uri         playreadyKeyAcquisitionUrl = dummyKey2.GetKeyDeliveryUrl(ContentKeyDeliveryType.PlayReadyLicense);
                    configKeys.Add(AssetDeliveryPolicyConfigurationKey.PlayReadyLicenseAcquisitionUrl, playreadyKeyAcquisitionUrl.ToString());
                    dummyKey2.Delete();
                    break;

                case MediaServicesHelper.AssetDeliveryContentProtection.Widevine:
                    IContentKey dummyKey3 = CreateDummyContentKey(ContentKeyType.CommonEncryption);
                    Uri         widevineKeyAcquisitionUrl = dummyKey3.GetKeyDeliveryUrl(ContentKeyDeliveryType.Widevine);
                    UriBuilder  uriBuilder3 = new UriBuilder(widevineKeyAcquisitionUrl);
                    uriBuilder3.Query         = String.Empty;
                    widevineKeyAcquisitionUrl = uriBuilder3.Uri;
                    configKeys.Add(AssetDeliveryPolicyConfigurationKey.WidevineLicenseAcquisitionUrl, widevineKeyAcquisitionUrl.ToString());
                    dummyKey3.Delete();
                    break;

                case MediaServicesHelper.AssetDeliveryContentProtection.FairPlay:
                    IContentKey dummyKey4 = CreateDummyContentKey(ContentKeyType.CommonEncryption);
                    Uri         fairplayKeyAcquisitionUrl = dummyKey4.GetKeyDeliveryUrl(ContentKeyDeliveryType.FairPlay);
                    configKeys.Add(AssetDeliveryPolicyConfigurationKey.FairPlayLicenseAcquisitionUrl, fairplayKeyAcquisitionUrl.ToString().Replace("https://", "skd://"));
                    dummyKey4.Delete();
                    FairPlayConfiguration fairplayConfig = JsonConvert.DeserializeObject <FairPlayConfiguration>(fairplayPolicyOpiton.KeyDeliveryConfiguration);
                    configKeys.Add(AssetDeliveryPolicyConfigurationKey.CommonEncryptionIVForCbcs, fairplayConfig.ContentEncryptionIV);
                    break;
                }
            }

            return(configKeys);
        }
        public void TestCreateFairPlayAuthorizationPolicy()
        {
            Guid keyId = Guid.NewGuid();

            byte[]      contentKey = Guid.NewGuid().ToByteArray();
            IContentKey key        = _mediaContext.ContentKeys.Create(keyId, contentKey, "testKey", ContentKeyType.CommonEncryptionCbcs);

            byte[]      askBytes = Guid.NewGuid().ToByteArray();
            var         askId    = Guid.NewGuid();
            IContentKey askKey   = _mediaContext.ContentKeys.Create(askId, askBytes, "askKey", ContentKeyType.FairPlayASk);

            string pfxPassword   = "******";
            var    pfxPasswordId = Guid.NewGuid();

            byte[]      pfxPasswordBytes = System.Text.Encoding.UTF8.GetBytes(pfxPassword);
            IContentKey pfxPasswordKey   = _mediaContext.ContentKeys.Create(pfxPasswordId, pfxPasswordBytes, "pfxPasswordKey", ContentKeyType.FairPlayPfxPassword);

            byte[] iv = Guid.NewGuid().ToByteArray();

            var restrictions = new List <ContentKeyAuthorizationPolicyRestriction>
            {
                new ContentKeyAuthorizationPolicyRestriction
                {
                    Name = "Open",
                    KeyRestrictionType = (int)ContentKeyRestrictionType.Open,
                    Requirements       = null
                }
            };

            var appCert = new X509Certificate2("amscer.pfx", pfxPassword, X509KeyStorageFlags.Exportable);

            string configuration = FairPlayConfiguration.CreateSerializedFairPlayOptionConfiguration(
                appCert,
                pfxPassword,
                pfxPasswordId,
                askId,
                iv);

            var policyOption = _mediaContext.ContentKeyAuthorizationPolicyOptions.Create(
                "fairPlayTest",
                ContentKeyDeliveryType.FairPlay,
                restrictions,
                configuration);

            var contentKeyAuthorizationPolicy = _mediaContext.ContentKeyAuthorizationPolicies.CreateAsync("Key no restrictions").Result;

            contentKeyAuthorizationPolicy.Options.Add(policyOption);

            key.AuthorizationPolicyId = contentKeyAuthorizationPolicy.Id;
            key = key.UpdateAsync().Result;

            key.Delete();
            pfxPasswordKey.Delete();
            askKey.Delete();
            contentKeyAuthorizationPolicy.Delete();
        }
        public void QueryContentKeyBYAuthorizationPolicyId()
        {
            IContentKeyAuthorizationPolicy policy = _mediaContext.ContentKeyAuthorizationPolicies.CreateAsync(testRun).Result;
            IContentKey contentKey = _mediaContext.ContentKeys.CreateAsync(Guid.NewGuid(), new byte[16]).Result;

            contentKey.AuthorizationPolicyId = policy.Id;
            contentKey.Update();
            IContentKey updatedKey = _mediaContext.ContentKeys.Where(c => c.AuthorizationPolicyId == policy.Id).FirstOrDefault();

            Assert.IsNotNull(updatedKey.AuthorizationPolicyId);

            contentKey.Delete();
        }
        private static IContentKey CreateKeyWithPolicy(IAsset asset)
        {
            IContentKey key = asset.ContentKeys.Where(k => k.ContentKeyType == ContentKeyType.CommonEncryption).SingleOrDefault();

            if (key != null)
            {
                CleanupKey(key);
                key.Delete();
            }

            var keyId = Guid.NewGuid();

            byte[] contentKey = Guid.NewGuid().ToByteArray();

            ContentKeyType contentKeyType = ContentKeyType.CommonEncryption;
            IContentKeyAuthorizationPolicyOption policyOption;

            var restrictions = new List <ContentKeyAuthorizationPolicyRestriction>
            {
                new ContentKeyAuthorizationPolicyRestriction
                {
                    Name = "Open",
                    KeyRestrictionType = (int)ContentKeyRestrictionType.Open,
                    Requirements       = null
                }
            };

            string configuration = "{}";

            //string configuration = "{\"allowed_track_types\":\"SD_HD\",\"content_key_specs\":[{\"track_type\":\"SD\",\"security_level\":1,\"required_output_protection\":{\"hdcp\":\"HDCP_NONE\"}}],\"policy_overrides\":{\"can_play\":true,\"can_persist\":true,\"can_renew\":false}}";

            policyOption = _mediaContext.ContentKeyAuthorizationPolicyOptions.Create(
                "widevinetest",
                ContentKeyDeliveryType.Widevine,
                restrictions,
                configuration);

            key = _mediaContext.ContentKeys.Create(keyId, contentKey, "TestWidevineKey", contentKeyType);

            var contentKeyAuthorizationPolicy = _mediaContext.ContentKeyAuthorizationPolicies.CreateAsync("test").Result;

            contentKeyAuthorizationPolicy.Options.Add(policyOption);

            key.AuthorizationPolicyId = contentKeyAuthorizationPolicy.Id;
            key = key.UpdateAsync().Result;

            asset.ContentKeys.Add(key);

            return(key);
        }
        public void ShouldDeleteContentKeyWithDifferentContexts()
        {
            Guid keyId = Guid.NewGuid();

            byte[]      contentKeyBytes = GetRandomBuffer(16);
            IContentKey key             = _mediaContext.ContentKeys.Create(keyId, contentKeyBytes);

            string keyIdentifier = key.Id;

            key.Delete();

            foreach (IContentKey contentKey in _mediaContext.ContentKeys)
            {
                Assert.IsFalse(contentKey.Id == keyIdentifier);
            }
        }
Exemplo n.º 8
0
        private void CleanupKeyAndPolicy(IContentKey contentKey, IContentKeyAuthorizationPolicy contentKeyAuthorizationPolicy, IContentKeyAuthorizationPolicyOption policyOption)
        {
            if (contentKey != null)
            {
                contentKey.Delete();
            }

            if (contentKeyAuthorizationPolicy != null)
            {
                contentKeyAuthorizationPolicy.Delete();
            }

            /*
             * if (policyOption != null)
             * {
             *  policyOption.Delete();
             * }
             */
        }
Exemplo n.º 9
0
        public static void CleanupKey(CloudMediaContext mediaContext, IContentKey key)
        {
            IContentKeyAuthorizationPolicy policy = null;

            if (key.AuthorizationPolicyId != null)
            {
                policy = mediaContext.ContentKeyAuthorizationPolicies
                         .Where(o => o.Id == key.AuthorizationPolicyId)
                         .SingleOrDefault();
            }

            if (policy != null)
            {
                if (key.ContentKeyType == ContentKeyType.CommonEncryptionCbcs)
                {
                    string template = policy.Options.Single().KeyDeliveryConfiguration;

                    var config = JsonConvert.DeserializeObject <FairPlayConfiguration>(template);

                    IContentKey ask = mediaContext
                                      .ContentKeys
                                      .Where(k => k.Id == Constants.ContentKeyIdPrefix + config.ASkId.ToString())
                                      .SingleOrDefault();

                    if (ask != null)
                    {
                        ask.Delete();
                    }

                    IContentKey pfxPassword = mediaContext
                                              .ContentKeys
                                              .Where(k => k.Id == Constants.ContentKeyIdPrefix + config.FairPlayPfxPasswordId.ToString())
                                              .SingleOrDefault();

                    if (pfxPassword != null)
                    {
                        pfxPassword.Delete();
                    }
                }

                policy.Delete();
            }
        }
        public void UpdateContentKeyAuthorizationPolicyId()
        {
            IContentKeyAuthorizationPolicy policy = _mediaContext.ContentKeyAuthorizationPolicies.CreateAsync(testRun).Result;
            IContentKey contentKey = _mediaContext.ContentKeys.CreateAsync(Guid.NewGuid(), new byte[16]).Result;

            contentKey.AuthorizationPolicyId = policy.Id;
            IContentKey updated = contentKey.UpdateAsync().Result;

            IContentKey updatedContentKey = _mediaContext.ContentKeys.Where(c => c.Id == contentKey.Id).FirstOrDefault();

            //var updatedWithPolicy = _dataContext.ContentKeys.Where(c => c.AuthorizationPolicyId == policy.Id).FirstOrDefault();
            Assert.IsNotNull(updatedContentKey.AuthorizationPolicyId);
            Assert.AreEqual(policy.Id, updatedContentKey.AuthorizationPolicyId);

            contentKey.AuthorizationPolicyId = null;
            updated = contentKey.UpdateAsync().Result;
            Assert.IsNull(contentKey.AuthorizationPolicyId);
            updatedContentKey = _mediaContext.ContentKeys.Where(c => c.Id == contentKey.Id).FirstOrDefault();
            Assert.IsNull(updatedContentKey.AuthorizationPolicyId);
            contentKey.Delete();
        }
        public static void RemoveContentKey(string id)
        {
            IContentKey contentKey = _context.ContentKeys.Where(p => p.Id == id).FirstOrDefault();

            if (contentKey != null)
            {
                try
                {
                    string keyId   = contentKey.Id;
                    string keyName = contentKey.Name;
                    contentKey.Delete();
                    Console.WriteLine("Deleted Content Key {0} : {1}", keyId, keyName);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error on deleting Content Key {0} : {1}", id, e.ToString());
                }
            }
            else
            {
                Console.WriteLine("Error: Content Key {0} Not Found", id);
            }
            return;
        }
        private void CleanupKeyAndPolicy(IContentKey contentKey, IContentKeyAuthorizationPolicy contentKeyAuthorizationPolicy, IContentKeyAuthorizationPolicyOption policyOption)
        {
            if (contentKey != null)
            {
                contentKey.Delete();
            }

            if (contentKeyAuthorizationPolicy != null)
            {
                contentKeyAuthorizationPolicy.Delete();
            }

            /*
            if (policyOption != null)
            {
                policyOption.Delete();
            }
            */
        }
 private static void UpdateDeleteContentKey(IContentKey key)
 {
     key.AuthorizationPolicyId = Guid.NewGuid().ToString();
     key.Update();
     key.AuthorizationPolicyId = Guid.NewGuid().ToString();
     key.UpdateAsync();
     key.Delete();
 }