public bool TryDecode(string code, out ProductLicense license)
        {
            // License format:
            //  No. Bytes       Field                   Byte index
            //  1               X                       0
            //  1               License flags           1
            //  1               VersionNoApplicable     2
            //  2               ExpirationDate          3
            //  1               Y                       5
            //  1               ExpirationDays          6
            //  1               ExpirationLoads         7
            //  1               MaxSimultaneousUsers    8
            //  1               Z                       9

            // X = random(0,255)
            // Y = (byte[1] + byte[2] + byte[3] + byte[6] + byte[7] + byte[8]) % 255
            // Z = (X^2 + Y^2) % 255
            license = new ProductLicense();
            if (string.IsNullOrEmpty(code))
            {
                return(false);
            }

            byte[] bytes = Base32Converter.FromBase32String(code.Replace("-", string.Empty));
            if (bytes.Length != 10)
            {
                return(false);
            }

            bytes = bytes.Xor(ProductInformationServices.ProductInformation.ProductCode.ToByteArray());

            // apply internal mask
            StandardLicenseCodecTools.ApplyInternalMask(bytes);

            int X = (int)bytes[0];
            int Y = (int)bytes[5];
            int Z = (int)bytes[9];

            if (Y != (((int)bytes[1]) + ((int)bytes[2]) + ((int)bytes[3]) + ((int)bytes[6]) + ((int)bytes[7]) + ((int)bytes[8])) % 255)
            {
                return(false);
            }
            if ((X * X + Y * Y) % 255 != Z)
            {
                return(false);
            }
            license.Flags = (ProductLicenseFlags)bytes[1];
            license.MajorVersionApplicable = bytes[2];
            license.ExpirationDate         = StandardLicenseCodecTools.FromDaysSince2008Jan1(BitConverter.ToUInt16(bytes, 3));
            license.ExpirationDays         = bytes[6];
            license.ExpirationLoads        = bytes[7];
            license.MaxSimultaneousUsers   = bytes[8];

            return(true);
        }
        public override bool PerformAdditionalValidation(ProductLicense license)
        {
            bool valid = false;

            if (license.VersionAware)
            {
                if (StandardLicenseTool.GetProductMajorVersion(ProductInformationServices.ProductInformation.ProductVersion) == license.MajorVersionApplicable)
                {
                    valid = true;
                }
            }
            else
            {
                valid = true;
            }
            return(valid);
        }
 public virtual bool PerformAdditionalValidation(ProductLicense license)
 {
     return(true);
 }
        public string GenerateKey(ProductLicense license)
        {
            // License format:
            //  No. Bytes       Field                   Byte index
            //  1               X                       0
            //  1               License flags           1
            //  1               VersionNoApplicable     2
            //  2               ExpirationDate          3
            //  1               Y                       5
            //  1               ExpirationDays          6
            //  1               ExpirationLoads         7
            //  1               MaxSimultaneousUsers    8
            //  1               Z                       9

            // X = random(0,255)
            // Y = (byte[1] + byte[2] + byte[3] + byte[6] + byte[7] + byte[8]) % 255
            // Z = (X^2 + Y^2) % 255


            byte[] byteArray = new byte[10];
            Tools.Maths.RandomNumberGenerator.NextBytes(byteArray);


            byteArray[1] = (byte)license.Flags;
            if (license.VersionAware)
            {
                byteArray[2] = license.MajorVersionApplicable;
            }
            if (license.ExpiresAfterDate)
            {
                Array.Copy(
                    BitConverter.GetBytes(StandardLicenseCodecTools.ToDaysSince2008Jan1(license.ExpirationDate)),
                    0,
                    byteArray,
                    3,
                    2
                    );
            }

            if (license.ExpiresAfterDays)
            {
                byteArray[6] = license.ExpirationDays;
            }
            if (license.ExpiresAfterLoads)
            {
                byteArray[7] = license.ExpirationLoads;
            }
            if (license.LimitSimultaneousUsers)
            {
                byteArray[8] = license.MaxSimultaneousUsers;
            }


            // add in license validity bytes

            int X = Tools.Maths.RandomNumberGenerator.Next(0, 255);
            int Y = (((int)byteArray[1]) + ((int)byteArray[2]) + ((int)byteArray[3]) + ((int)byteArray[6]) + ((int)byteArray[7]) + ((int)byteArray[8])) % 255;
            int Z = (X * X + Y * Y) % 255;

            byteArray[0] = (byte)X;
            byteArray[5] = (byte)Y;
            byteArray[9] = (byte)Z;

            // apply internal mask
            StandardLicenseCodecTools.ApplyInternalMask(byteArray);

            // apply product code mask
            byteArray = byteArray.Xor(ProductInformationServices.ProductInformation.ProductCode.ToByteArray());

            return(Beutify(Base32Converter.ToBase32String(byteArray)));
        }
Exemplo n.º 5
0
        protected virtual ProductRights DetermineRightsFromLicense(ProductLicense license)
        {
            var rights = ProductRights.None;

            if (license.VersionAware)
            {
                rights.AppliesToVersion = true;
                rights.Version          = license.MajorVersionApplicable;
            }

            #region Get license data

            if (license.DoesNotExpire)
            {
                rights.ExpiresAfterDate   = false;
                rights.HasFiniteDays      = false;
                rights.HasFiniteInstances = false;
                rights.HasFiniteUses      = false;
            }
            else
            {
                // Date
                if (license.ExpiresAfterDate)
                {
                    rights.ExpiresAfterDate  = true;
                    rights.ExpirationDateUTC = license.ExpirationDate.ToUniversalTime();
                }
                else
                {
                    rights.ExpiresAfterDate  = false;
                    rights.ExpirationDateUTC = DateTime.MaxValue;
                }
                // Days
                if (license.ExpiresAfterDays)
                {
                    rights.HasFiniteDays    = true;
                    rights.TotalDaysAllowed = license.ExpirationDays;
                    rights.DaysRemaining    = (license.ExpirationDays - ProductUsageServices.ProductUsageInformation.DaysUsedBySystem + 1).ClipTo(0, int.MaxValue);
                }
                else
                {
                    rights.HasFiniteDays    = false;
                    rights.TotalDaysAllowed = int.MaxValue;
                    rights.DaysRemaining    = int.MaxValue;
                }

                // Uses
                if (license.ExpiresAfterLoads)
                {
                    rights.HasFiniteUses    = true;
                    rights.TotalUsesAllowed = license.ExpirationLoads;
                    rights.UsesRemaining    = (license.ExpirationLoads - ProductUsageServices.ProductUsageInformation.NumberOfUsesBySystem + 1).ClipTo(0, int.MaxValue);
                }
                else
                {
                    rights.HasFiniteUses    = false;
                    rights.TotalUsesAllowed = int.MaxValue;
                    rights.UsesRemaining    = int.MaxValue;
                }

                // Instances
                if (license.LimitSimultaneousUsers)
                {
                    rights.HasFiniteInstances    = true;
                    rights.TotalInstancesAllowed = license.MaxSimultaneousUsers;
                    rights.InstancesRemaining    = (license.MaxSimultaneousUsers - DuplicateProcessDetector.CountRunningInstancesOfThisApplication() + 1).ClipTo(0, int.MaxValue);
                }
                else
                {
                    rights.HasFiniteInstances    = false;
                    rights.TotalInstancesAllowed = int.MaxValue;
                    rights.InstancesRemaining    = int.MaxValue;
                }
            }

            // Get feature rights
            if (license.LimitedFeatureSet)
            {
                rights.FeatureRights = ProductFeatureRights.Partial;
            }
            else
            {
                rights.FeatureRights = ProductFeatureRights.Full;
            }

            #endregion

            // Determine if license has expired
            bool hasExpired = false;
            if (license.ExpiresAfterDate && DateTime.UtcNow > rights.ExpirationDateUTC)
            {
                hasExpired = true;
            }
            if (license.ExpiresAfterDays && rights.DaysRemaining == 0)
            {
                hasExpired = true;
            }
            if (license.ExpiresAfterLoads && rights.UsesRemaining == 0)
            {
                hasExpired = true;
            }
            if (license.LimitSimultaneousUsers && rights.InstancesRemaining == 0)
            {
                hasExpired = true;
            }

            // Apply expiration effect on license
            if (hasExpired)
            {
                if (license.CrippleOnExpiration)
                {
                    rights.FeatureRights = ProductFeatureRights.Partial;
                }
                if (license.DisableOnExpiration)
                {
                    rights.FeatureRights = ProductFeatureRights.None;
                }
            }

            return(rights);
        }