/// <summary>
        /// Deletes this instance.
        /// </summary>
        /// <returns>A function delegate.</returns>
        public Task DeleteAsync()
        {
            ContentKeyBaseCollection.VerifyContentKey(this);

            DataServiceContext dataContext = this._cloudMediaContext.DataContextFactory.CreateDataServiceContext();

            dataContext.AttachTo(ContentKeyCollection.ContentKeySet, this);
            dataContext.DeleteObject(this);

            return(dataContext.SaveChangesAsync(this));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deletes this instance.
        /// </summary>
        /// <returns>A function delegate.</returns>
        public Task DeleteAsync()
        {
            ContentKeyBaseCollection.VerifyContentKey(this);

            IMediaDataServiceContext dataContext = this.GetMediaContext().MediaServicesClassFactory.CreateDataServiceContext();

            dataContext.AttachTo(ContentKeyBaseCollection.ContentKeySet, this);
            dataContext.DeleteObject(this);

            MediaRetryPolicy retryPolicy = this.GetMediaContext().MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(this)));
        }
        /// <summary>
        /// Create a notification endpoint object in asynchronous mode.
        /// </summary>
        /// <param name="name">Name of notification endpoint</param>
        /// <param name="endPointType">Notification endpoint type</param>
        /// <param name="endPointAddress">Notification endpoint address</param>
        /// <param name="credential"></param>
        /// <returns>Task of creating notification endpoint.</returns>
        public Task <INotificationEndPoint> CreateAsync(string name, NotificationEndPointType endPointType,
                                                        string endPointAddress, byte[] credential)
        {
            if (credential == null || credential.Length == 0)
            {
                throw new ArgumentNullException("credential");
            }

            if (endPointType != NotificationEndPointType.WebHook)
            {
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, StringTable.SupportWebHookWithCredentialOnly, "endPointType"));
            }

            IMediaDataServiceContext dataContext =
                this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

            string protectionKeyId = ContentKeyBaseCollection.GetProtectionKeyIdForContentKey(MediaContext,
                                                                                              ContentKeyType.ConfigurationEncryption);
            X509Certificate2 certToUse = ContentKeyBaseCollection.GetCertificateForProtectionKeyId(MediaContext, protectionKeyId);

            byte[] encryptedContentKey = EncryptionUtils.EncryptSymmetricKeyData(certToUse, credential);

            NotificationEndPoint notificationEndPoint = new NotificationEndPoint
            {
                Name                        = name,
                EndPointType                = (int)endPointType,
                EndPointAddress             = endPointAddress,
                CredentialType              = (int)NotificationEndPointCredentialType.SigningKey,
                EncryptedEndPointCredential = Convert.ToBase64String(encryptedContentKey),
                ProtectionKeyType           = (int)ProtectionKeyType.X509CertificateThumbprint,
                ProtectionKeyId             = protectionKeyId
            };

            notificationEndPoint.SetMediaContext(MediaContext);
            dataContext.AddObject(NotificationEndPoints, notificationEndPoint);

            MediaRetryPolicy retryPolicy =
                this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(
                       () => dataContext.SaveChangesAsync(notificationEndPoint))
                   .ContinueWith <INotificationEndPoint>(
                       t =>
            {
                t.ThrowIfFaulted();

                return (NotificationEndPoint)t.Result.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Asynchronously creates a content key with the specifies key identifier and value.
        /// </summary>
        /// <param name="keyId">The key identifier.</param>
        /// <param name="contentKey">The value of the content key.</param>
        /// <param name="name">A friendly name for the content key.</param>
        /// <param name="contentKeyType">Type of content key to create.</param>
        /// <returns>
        /// A function delegate that returns the future result to be available through the Task&lt;IContentKey&gt;.
        /// </returns>
        public override Task <IContentKey> CreateAsync(Guid keyId, byte[] contentKey, string name, ContentKeyType contentKeyType)
        {
            if ((contentKeyType != ContentKeyType.CommonEncryption) && (contentKeyType != ContentKeyType.EnvelopeEncryption))
            {
                throw new ArgumentException(StringTable.ErrorUnsupportedContentKeyType, "contentKey");
            }

            if (keyId == Guid.Empty)
            {
                throw new ArgumentException(StringTable.ErrorCreateKey_EmptyGuidNotAllowed, "keyId");
            }

            if (contentKey == null)
            {
                throw new ArgumentNullException("contentKey");
            }

            if (contentKey.Length != EncryptionUtils.KeySizeInBytesForAes128)
            {
                throw new ArgumentException(StringTable.ErrorCommonEncryptionKeySize, "contentKey");
            }

            IMediaDataServiceContext dataContext = this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext();
            X509Certificate2         certToUse   = ContentKeyBaseCollection.GetCertificateToEncryptContentKey(MediaContext, ContentKeyType.CommonEncryption);

            ContentKeyData contentKeyData = null;

            if (contentKeyType == ContentKeyType.CommonEncryption)
            {
                contentKeyData = InitializeCommonContentKey(keyId, contentKey, name, certToUse);
            }
            else if (contentKeyType == ContentKeyType.EnvelopeEncryption)
            {
                contentKeyData = InitializeEnvelopeContentKey(keyId, contentKey, name, certToUse);
            }

            dataContext.AddObject(ContentKeySet, contentKeyData);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(contentKeyData))
                   .ContinueWith <IContentKey>(
                       t =>
            {
                t.ThrowIfFaulted();

                return (ContentKeyData)t.Result.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }
Exemplo n.º 5
0
        private static ContentKeyData CreateStorageContentKey(AssetData tempAsset, NullableFileEncryption fileEncryption, DataServiceContext dataContext)
        {
            // Create the content key.
            fileEncryption.Init();

            // Encrypt it for delivery to Nimbus.
            X509Certificate2 certToUse      = ContentKeyCollection.GetCertificateToEncryptContentKey(dataContext, ContentKeyType.StorageEncryption);
            ContentKeyData   contentKeyData = ContentKeyBaseCollection.CreateStorageContentKey(fileEncryption.FileEncryption, certToUse);

            dataContext.AddObject(ContentKeyCollection.ContentKeySet, contentKeyData);
            dataContext.SaveChanges();

            // Associate it with the asset.
            ((IAsset)tempAsset).ContentKeys.Add(contentKeyData);

            return(contentKeyData);
        }
        private ContentKeyData CreateStorageContentKey(AssetData tempAsset, NullableFileEncryption fileEncryption, IMediaDataServiceContext dataContext)
        {
            // Create the content key.
            fileEncryption.Init();

            // Encrypt it for delivery to Nimbus.
            X509Certificate2 certToUse      = ContentKeyCollection.GetCertificateToEncryptContentKey(MediaContext, ContentKeyType.StorageEncryption);
            ContentKeyData   contentKeyData = ContentKeyBaseCollection.InitializeStorageContentKey(fileEncryption.FileEncryption, certToUse);

            dataContext.AddObject(ContentKeyBaseCollection.ContentKeySet, contentKeyData);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            retryPolicy.ExecuteAction <IMediaDataServiceResponse>(() => dataContext.SaveChanges());

            // Associate it with the asset.
            ((IAsset)tempAsset).ContentKeys.Add(contentKeyData);

            return(contentKeyData);
        }
        private void ProtectTaskConfiguration(TaskTemplateData taskTemplate, ref X509Certificate2 certToUse, IMediaDataServiceContext dataContext)
        {
            using (ConfigurationEncryption configEncryption = new ConfigurationEncryption())
            {
                // Update the task template with the required data.
                taskTemplate.Configuration        = configEncryption.Encrypt(taskTemplate.Configuration);
                taskTemplate.EncryptionKeyId      = configEncryption.GetKeyIdentifierAsString();
                taskTemplate.EncryptionScheme     = ConfigurationEncryption.SchemeName;
                taskTemplate.EncryptionVersion    = ConfigurationEncryption.SchemeVersion;
                taskTemplate.InitializationVector = configEncryption.GetInitializationVectorAsString();

                if (certToUse == null)
                {
                    // Get the certificate to use to encrypt the configuration encryption key.
                    certToUse = ContentKeyBaseCollection.GetCertificateToEncryptContentKey(GetMediaContext(), ContentKeyType.ConfigurationEncryption);
                }

                // Create a content key object to hold the encryption key.
                ContentKeyData contentKeyData = ContentKeyBaseCollection.InitializeConfigurationContentKey(configEncryption, certToUse);
                dataContext.AddObject(ContentKeyBaseCollection.ContentKeySet, contentKeyData);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Asynchronously creates a content key with the specifies key identifier and value.
        /// </summary>
        /// <param name="keyId">The key identifier.</param>
        /// <param name="contentKey">The value of the content key.</param>
        /// <param name="name">A friendly name for the content key.</param>
        /// <returns>
        /// A function delegate that returns the future result to be available through the Task&lt;IContentKey&gt;.
        /// </returns>
        public override Task <IContentKey> CreateAsync(Guid keyId, byte[] contentKey, string name)
        {
            if (keyId == Guid.Empty)
            {
                throw new ArgumentException(StringTable.ErrorCreateKey_EmptyGuidNotAllowed, "keyId");
            }

            if (contentKey == null)
            {
                throw new ArgumentNullException("contentKey");
            }

            if (contentKey.Length != EncryptionUtils.KeySizeInBytesForAes128)
            {
                throw new ArgumentException(StringTable.ErrorCommonEncryptionKeySize, "contentKey");
            }

            DataServiceContext dataContext    = this._cloudMediaContext.DataContextFactory.CreateDataServiceContext();
            X509Certificate2   certToUse      = ContentKeyBaseCollection.GetCertificateToEncryptContentKey(dataContext, ContentKeyType.CommonEncryption);
            ContentKeyData     contentKeyData = CreateCommonContentKey(keyId, contentKey, name, certToUse);

            contentKeyData.InitCloudMediaContext(this._cloudMediaContext);

            dataContext.AddObject(ContentKeySet, contentKeyData);

            return(dataContext
                   .SaveChangesAsync(contentKeyData)
                   .ContinueWith <IContentKey>(
                       t =>
            {
                t.ThrowIfFaulted();

                return (ContentKeyData)t.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }