Esempio n. 1
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));
        }
        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);
            }
        }
Esempio n. 3
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));
        }