예제 #1
0
        protected override XmlElement SerializeEntity(XmlDocument document, XmlNamespaceManager namespaces, XmlElement container, UsageRule entity)
        {
            var element = new UsageRuleElement
            {
                KeyId = entity.KeyId
            };

            if (entity.VideoFilters?.Count > 0)
            {
                element.VideoFilters = entity.VideoFilters
                                       .Select(f => new VideoFilterElement
                {
                    MinPixels = f.MinPixels,
                    MaxPixels = f.MaxPixels,
                    MinFps    = f.MinFramesPerSecond,
                    MaxFps    = f.MaxFramesPerSecond,
                    Wcg       = f.WideColorGamut,
                    Hdr       = f.HighDynamicRange
                })
                                       .ToArray();
            }

            if (entity.AudioFilters?.Count > 0)
            {
                element.AudioFilters = entity.AudioFilters
                                       .Select(f => new AudioFilterElement
                {
                    MinChannels = f.MinChannels,
                    MaxChannels = f.MaxChannels
                })
                                       .ToArray();
            }

            if (entity.BitrateFilters?.Count > 0)
            {
                element.BitrateFilters = entity.BitrateFilters
                                         .Select(f => new BitrateFilterElement
                {
                    MinBitrate = f.MinBitrate,
                    MaxBitrate = f.MaxBitrate
                })
                                         .ToArray();
            }

            if (entity.LabelFilters?.Count > 0)
            {
                element.LabelFilters = entity.LabelFilters
                                       .Select(f => new LabelFilterElement
                {
                    Label = f.Label
                })
                                       .ToArray();
            }

            return(XmlHelpers.AppendChildAndReuseNamespaces(element, container));
        }
예제 #2
0
        protected override XmlElement SerializeEntity(XmlDocument document, XmlNamespaceManager namespaces, XmlElement container, DrmSystem entity)
        {
            var drmSystemElement = new DrmSystemElement
            {
                SystemId = entity.SystemId,
                KeyId    = entity.KeyId,
                Pssh     = entity.Pssh,
                SmoothStreamingProtectionHeaderData = entity.SmoothStreamingProtectionHeaderData
            };

            if (entity.ContentProtectionData != null)
            {
                drmSystemElement.ContentProtectionData = Convert.ToBase64String(Encoding.UTF8.GetBytes(entity.ContentProtectionData));
            }

            if (entity.HdsSignalingData != null)
            {
                drmSystemElement.HdsSignalingData = Convert.ToBase64String(Encoding.UTF8.GetBytes(entity.HdsSignalingData));
            }

            if (entity.HlsSignalingData?.MasterPlaylistData != null)
            {
                drmSystemElement.HlsSignalingData.Add(new HlsSignalingDataElement
                {
                    Playlist = HlsPlaylistType.Master,
                    Value    = Convert.ToBase64String(Encoding.UTF8.GetBytes(entity.HlsSignalingData.MasterPlaylistData))
                });
            }

            if (entity.HlsSignalingData?.MediaPlaylistData != null)
            {
                drmSystemElement.HlsSignalingData.Add(new HlsSignalingDataElement
                {
                    Playlist = HlsPlaylistType.Media,
                    Value    = Convert.ToBase64String(Encoding.UTF8.GetBytes(entity.HlsSignalingData.MediaPlaylistData))
                });
            }

            return(XmlHelpers.AppendChildAndReuseNamespaces(drmSystemElement, container));
        }
예제 #3
0
        protected override XmlElement SerializeEntity(XmlDocument document, XmlNamespaceManager namespaces, XmlElement container, Recipient entity)
        {
            var recipientRsa = entity.Certificate.GetRSAPublicKey();

            // Ensure that we have the document-scoped cryptographic material available.
            if (Document.DocumentKey == null)
            {
                Document.GenerateKeys();
            }

            var encryptedDocumentKey = recipientRsa.Encrypt(Document.DocumentKey, RSAEncryptionPadding.OaepSHA1);
            var encryptedMacKey      = recipientRsa.Encrypt(Document.MacKey, RSAEncryptionPadding.OaepSHA1);

            var element = new DeliveryDataElement
            {
                DeliveryKey = new DeliveryKeyElement
                {
                    X509Data = new X509Data
                    {
                        Certificate = entity.Certificate.GetRawCertData()
                    }
                },
                DocumentKey = new DocumentKeyElement
                {
                    Algorithm = Constants.Aes256CbcAlgorithm,
                    Data      = new DataElement
                    {
                        Secret = new SecretDataElement
                        {
                            EncryptedValue = new EncryptedXmlValue
                            {
                                EncryptionMethod = new EncryptionMethodDeclaration
                                {
                                    Algorithm = Constants.RsaOaepAlgorithm
                                },
                                CipherData = new CipherDataContainer
                                {
                                    CipherValue = encryptedDocumentKey
                                }
                            }
                        }
                    }
                },
                MacMethod = new MacMethodElement
                {
                    Algorithm = Constants.HmacSha512Algorithm,
                    Key       = new EncryptedXmlValue
                    {
                        EncryptionMethod = new EncryptionMethodDeclaration
                        {
                            Algorithm = Constants.RsaOaepAlgorithm
                        },
                        CipherData = new CipherDataContainer
                        {
                            CipherValue = encryptedMacKey
                        }
                    }
                }
            };

            return(XmlHelpers.AppendChildAndReuseNamespaces(element, container));
        }
예제 #4
0
        protected override XmlElement SerializeEntity(XmlDocument document, XmlNamespaceManager namespaces, XmlElement container, ContentKey entity)
        {
            var element = new ContentKeyElement
            {
                KeyId      = entity.Id,
                ExplicitIv = entity.ExplicitIv,
                Data       = new DataElement
                {
                    Secret = new SecretDataElement()
                }
            };

            if (Document.Recipients.Any())
            {
                // We have to encrypt the key. Okay. Ensure we have the crypto values available.
                if (Document.DocumentKey == null)
                {
                    Document.GenerateKeys();
                }

                // Unique IV is generated for every content key.
                var iv = new byte[128 / 8];

                using (var random = RandomNumberGenerator.Create())
                    random.GetBytes(iv);

                var aes = new AesManaged
                {
                    BlockSize = 128,
                    KeySize   = 256,
                    Key       = Document.DocumentKey,
                    Mode      = CipherMode.CBC,
                    Padding   = PaddingMode.PKCS7,
                    IV        = iv
                };

                var mac = new HMACSHA512(Document.MacKey);

                using (var encryptor = aes.CreateEncryptor())
                {
                    var encryptedValue = encryptor.TransformFinalBlock(entity.Value, 0, entity.Value.Length);

                    // NB! We prepend the IV to the value when saving an encrypted value to the document field.
                    var fieldValue = iv.Concat(encryptedValue).ToArray();

                    element.Data.Secret.EncryptedValue = new EncryptedXmlValue
                    {
                        CipherData = new CipherDataContainer
                        {
                            CipherValue = fieldValue
                        },
                        EncryptionMethod = new EncryptionMethodDeclaration
                        {
                            Algorithm = Constants.Aes256CbcAlgorithm
                        }
                    };

                    // Never not MAC.
                    element.Data.Secret.ValueMAC = mac.ComputeHash(fieldValue);
                }
            }
            else
            {
                // We are saving the key in the clear.
                element.Data.Secret.PlainValue = entity.Value;
            }

            return(XmlHelpers.AppendChildAndReuseNamespaces(element, container));
        }