public LicenseCriteria Parse(IClientLicense clientLicense)
        {
            var license = XDocument.Parse(clientLicense.Content.OuterXml).Root;

            if (license == null)
            {
                throw new InvalidDataException("Document is invalid. A root node was expected");
            }

            var licenseDetails = license.Elements()
                                        .Where(element => element.Name.LocalName != LicenseElements.Signature)
                                        .Select(element => new KeyValuePair<string, string>(element.Name.LocalName, element.Value))
                                        .ToDictionary(pair => pair.Key, pair => pair.Value);

            var licenseCriteria = new LicenseCriteria
            {
                ExpirationDate = DateTimeOffset.Parse(licenseDetails[LicenseElements.ExpirationDate]),
                IssueDate = DateTimeOffset.Parse(licenseDetails[LicenseElements.IssueDate]),
                Id = Guid.Parse(licenseDetails[LicenseElements.Id]),
                Type = licenseDetails[LicenseElements.Type]
            };
            
            licenseDetails.Remove(LicenseElements.ExpirationDate);
            licenseDetails.Remove(LicenseElements.Id);
            licenseDetails.Remove(LicenseElements.IssueDate);
            licenseDetails.Remove(LicenseElements.Type);

            licenseCriteria.MetaData = licenseDetails;

            return licenseCriteria;
        }
Esempio n. 2
0
        public LicenseCriteria Parse(IClientLicense clientLicense)
        {
            var license = XDocument.Parse(clientLicense.Content.OuterXml).Root;

            if (license == null)
            {
                throw new InvalidDataException("Document is invalid. A root node was expected");
            }

            var licenseDetails = license.Elements()
                                 .Where(element => element.Name.LocalName != LicenseElements.Signature)
                                 .Select(element => new KeyValuePair <string, string>(element.Name.LocalName, element.Value))
                                 .ToDictionary(pair => pair.Key, pair => pair.Value);

            var licenseCriteria = new LicenseCriteria
            {
                ExpirationDate = DateTimeOffset.Parse(licenseDetails[LicenseElements.ExpirationDate]),
                IssueDate      = DateTimeOffset.Parse(licenseDetails[LicenseElements.IssueDate]),
                Id             = Guid.Parse(licenseDetails[LicenseElements.Id]),
                Type           = licenseDetails[LicenseElements.Type]
            };

            licenseDetails.Remove(LicenseElements.ExpirationDate);
            licenseDetails.Remove(LicenseElements.Id);
            licenseDetails.Remove(LicenseElements.IssueDate);
            licenseDetails.Remove(LicenseElements.Type);

            licenseCriteria.MetaData = licenseDetails;

            return(licenseCriteria);
        }
Esempio n. 3
0
        /// <summary>
        /// Validates the supplied client license against the private key and the set of validation rules. Violations are exposed via exceptions or aggregate exceptions.
        /// </summary>
        /// <param name="clientLicense">License to validate</param>
        /// <param name="publicKey">Public Key to validate the license with</param>
        /// <param name="validationRules">List of validation rules to examine</param>
        /// <exception cref="InvalidLicenseException">Indicates that the license is invalid / corrupt / empty.</exception>
        /// <exception cref="LicenseViolationException">Indicates that a license validation rule has been violated.</exception>
        /// <exception cref="AggregateException">Indicates that one or more license validation rules have been violated.</exception>
        public void Validate(IClientLicense clientLicense, ICryptoKey publicKey, IEnumerable <ILicenseValidationRule> validationRules, string elementKey = null)
        {
            if (!LicenseSignatureValidator.ValidateSignature(clientLicense, publicKey))
            {
                throw new InvalidLicenseException(clientLicense);
            }

            this.LicenseCriteria = this.licenseCriteriaParser.Parse(clientLicense, elementKey);

            validationRules.ForEachFailEnd(x => x.Validate(this.LicenseCriteria));
        }
Esempio n. 4
0
        public LicenseCriteria Parse(IClientLicense clientLicense, string elementKey = null)
        {
            XElement license;

            if (!string.IsNullOrEmpty(elementKey))
            {
                var hashSha256 = new SHA256CryptoServiceProvider();
                var keyArray   = hashSha256.ComputeHash(Encoding.UTF8.GetBytes(elementKey));

                //Always release the resources and flush data
                // of the Cryptographic service provide. Best Practice
                hashSha256.Clear();

                // Create a new TripleDES key.
                var rijndaelkey = Rijndael.Create();

                rijndaelkey.Key = keyArray;

                var xdoc = clientLicense.Content;
                Decrypt(xdoc, rijndaelkey);
                license = XDocument.Parse(xdoc.OuterXml).Root;
            }
            else
            {
                license = XDocument.Parse(clientLicense.Content.OuterXml).Root;
            }

            if (license == null)
            {
                throw new FormatException("Could not parse XML document");
            }

            var licenseDetails = license.Elements()
                                 .Where(element => element.Name.LocalName != LicenseElements.Signature)
                                 .Select(element => new KeyValuePair <string, string>(element.Name.LocalName, element.Value))
                                 .ToDictionary(pair => pair.Key, pair => pair.Value);

            var licenseCriteria = new LicenseCriteria
            {
                ExpirationDate = DateTimeOffset.Parse(licenseDetails[LicenseElements.ExpirationDate]),
                IssueDate      = DateTimeOffset.Parse(licenseDetails[LicenseElements.IssueDate]),
                Id             = Guid.Parse(licenseDetails[LicenseElements.Id]),
                Type           = licenseDetails[LicenseElements.Type]
            };

            licenseDetails.Remove(LicenseElements.ExpirationDate);
            licenseDetails.Remove(LicenseElements.Id);
            licenseDetails.Remove(LicenseElements.IssueDate);
            licenseDetails.Remove(LicenseElements.Type);

            licenseCriteria.MetaData = licenseDetails;

            return(licenseCriteria);
        }
        /// <summary>
        /// Validates the supplied client license against the private key and the set of validation rules. Violations are exposed via exceptions or aggregate exceptions.
        /// </summary>
        /// <param name="clientLicense">License to validate</param>
        /// <param name="publicKey">Public Key to validate the license with</param>
        /// <param name="validationRules">List of validation rules to examine</param>
        /// <exception cref="InvalidLicenseException">Indicates that the license is invalid / corrupt / empty.</exception>
        /// <exception cref="LicenseViolationException">Indicates that a license validation rule has been violated.</exception>
        /// <exception cref="AggregateException">Indicates that one or more license validation rules have been violated.</exception>
        public void Validate(IClientLicense clientLicense, ICryptoKey publicKey, IEnumerable<ILicenseValidationRule> validationRules)
        {
            if (!LicenseSignatureValidator.ValidateSignature(clientLicense, publicKey))
            {
                throw new InvalidLicenseException(clientLicense);
            }

            this.LicenseCriteria = this.licenseCriteriaParser.Parse(clientLicense);

            validationRules.ForEachFailEnd(x => x.Validate(this.LicenseCriteria));
        }
        public static bool ValidateSignature(IClientLicense license, ICryptoKey publicKey)
        {
            var namespaceManager = new XmlNamespaceManager(license.Content.NameTable);
            namespaceManager.AddNamespace("sig", "http://www.w3.org/2000/09/xmldsig#");

            var signature = (XmlElement)license.Content.SelectSingleNode("//sig:Signature", namespaceManager);

            if (signature == null)
            {
                return false;
            }

            var signedXml = new SignedXml(license.Content);
            signedXml.LoadXml(signature);

            using (var publicKeyProvider = new RsaPublicKeyProvider())
            {
                return signedXml.CheckSignature(publicKeyProvider.Create(publicKey));
            }
        }
        public static bool ValidateSignature(IClientLicense license, ICryptoKey publicKey)
        {
            var namespaceManager = new XmlNamespaceManager(license.Content.NameTable);

            namespaceManager.AddNamespace("sig", "http://www.w3.org/2000/09/xmldsig#");

            var signature = (XmlElement)license.Content.SelectSingleNode("//sig:Signature", namespaceManager);

            if (signature == null)
            {
                return(false);
            }

            var signedXml = new SignedXml(license.Content);

            signedXml.LoadXml(signature);

            using (var publicKeyProvider = new RsaPublicKeyProvider())
            {
                return(signedXml.CheckSignature(publicKeyProvider.Create(publicKey)));
            }
        }
 public InvalidLicenseException(IClientLicense clientLicense)
 {
     this.ClientLicense = clientLicense;
 }
Esempio n. 9
0
 public InvalidLicenseException(IClientLicense clientLicense)
 {
     this.ClientLicense = clientLicense;
 }