Beispiel #1
0
        internal static void RegisterLicense(string license, DateTime releaseDate)
        {
            if (string.IsNullOrEmpty(license))
            {
                throw new JSchemaException("License text is empty.");
            }

            string[] licenseParts = license.Trim().Split('-');
            if (licenseParts.Length != 2)
            {
                throw new JSchemaException("Specified license text is invalid.");
            }

            if (!int.TryParse(licenseParts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out int licenseId))
            {
                throw new JSchemaException("Specified license text is invalid.");
            }

            byte[] licenseData;

            try
            {
                licenseData = Convert.FromBase64String(licenseParts[1]);
            }
            catch
            {
                throw new JSchemaException("Specified license text is invalid.");
            }

            if (licenseData.Length <= 128)
            {
                throw new JSchemaException("Specified license text is invalid.");
            }

            LicenseDetails deserializedLicense;

            using (MemoryStream ms = new MemoryStream(licenseData, 128, licenseData.Length - 128))
                using (JsonTextReader reader = new JsonTextReader(new StreamReader(ms)))
                {
                    JsonSerializer serializer = new JsonSerializer();

                    deserializedLicense = serializer.Deserialize <LicenseDetails>(reader);
                }

            byte[] data      = deserializedLicense.GetSignificateData();
            byte[] signature = SubArray(licenseData, 0, 128);

            if (!CryptographyHelpers.ValidateData(data, signature))
            {
                throw new JSchemaException("License text does not match signature.");
            }

            if (deserializedLicense.Id != licenseId)
            {
                throw new JSchemaException("License ID does not match signature license ID.");
            }

            if (deserializedLicense.ExpiryDate < releaseDate)
            {
                string message = "License is not valid for this version of Json.NET Schema. License free upgrade date expired on {0}. This version of Json.NET Schema was released on {1}.".FormatWith(
                    CultureInfo.InvariantCulture,
                    deserializedLicense.ExpiryDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
                    releaseDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));

                throw new JSchemaException(message);
            }

            if (deserializedLicense.Type == LicenseType.Test)
            {
                throw new JSchemaException("Specified license is for testing only.");
            }

            SetRegisteredLicense(deserializedLicense);
        }
Beispiel #2
0
        private static LicenseDetails ReadLicenseData(DateTime releaseDate, string licenseBase64, int licenseId)
        {
            byte[] licenseData;

            try
            {
                licenseData = Convert.FromBase64String(licenseBase64);
            }
            catch
            {
                throw new JSchemaException("Specified license text is invalid.");
            }

            if (licenseData.Length <= 128)
            {
                throw new JSchemaException("Specified license text is invalid.");
            }

            LicenseDetails deserializedLicense;

            using (MemoryStream ms = new MemoryStream(licenseData, 128, licenseData.Length - 128))
                using (JsonTextReader reader = new JsonTextReader(new StreamReader(ms)))
                {
                    JsonSerializer serializer = new JsonSerializer();

                    deserializedLicense = serializer.Deserialize <LicenseDetails>(reader) !;
                }

            byte[] data      = deserializedLicense.GetSignificateData();
            byte[] signature = SubArray(licenseData, 0, 128);

            if (!CryptographyHelpers.ValidateData(data, signature))
            {
                throw new JSchemaException("License text does not match signature.");
            }

            if (deserializedLicense.Id != licenseId)
            {
                throw new JSchemaException("License ID does not match signature license ID.");
            }

            if (deserializedLicense.ExpiryDate < releaseDate)
            {
                string message = "License is not valid for this version of Json.NET Schema. License free upgrade date expired on {0}. This version of Json.NET Schema was released on {1}.".FormatWith(
                    CultureInfo.InvariantCulture,
                    deserializedLicense.ExpiryDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
                    releaseDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));

                throw new JSchemaException(message);
            }

            const int unitTestLicenseId = 1002;

            if (deserializedLicense.Type == LicenseType.Test && (deserializedLicense.ExpiryDate <= DateTime.UtcNow || deserializedLicense.Id == unitTestLicenseId))
            {
                string message = "Specified test license expiried on {0}.".FormatWith(
                    CultureInfo.InvariantCulture,
                    deserializedLicense.ExpiryDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));

                throw new JSchemaException(message);
            }

            return(deserializedLicense);
        }