public static string[] ValidateActivationkey(ICDESecrets mySecrets, string activationKeyString, Guid deviceId, List <TheLicense> allLicenses, out List <TheLicenseActivationInformation> activatedLicenses, out DateTimeOffset expirationDate)
        {
            expirationDate    = DateTimeOffset.MinValue;
            activatedLicenses = null;

            string signingKey = mySecrets.GetActivationKeySignatureKey();

            var normalizedKey = activationKeyString.Trim().Replace("-", "").ToUpper().Replace('O', '0').Replace('U', 'V').Replace('I', 'J').Replace('L', 'J');

            if (normalizedKey.Length != 36)
            {
                return(new string[] { "Invalid activation key: not the proper length", String.Format("{0}", activationKeyString) });
            }

            byte[] activationKey = TheActivationUtils.Base32Decode(normalizedKey);
            if (activationKey == null || activationKey.Length != 23 || activationKey[22] != 15)
            {
                return(new string[] { "Invalid activation key: failed to decode.", String.Format("{0}", activationKeyString) });
            }
            string activationKeyHash = Convert.ToBase64String((SHA1.Create().ComputeHash(activationKey)));

            byte[] signature = new byte[8];
            activationKey.Take(8).ToArray().CopyTo(signature, 0);

            int expirationInDays = (activationKey[8] + (activationKey[9] << 8));

            if (expirationInDays < 0)
            {
                return(new string[] { "Invalid activation key: invalid expiration date.", String.Format("{0}. Expiration: {1}", activationKeyString, expirationInDays) });
            }
            expirationDate = new DateTime(2016, 1, 1, 0, 0, 0, DateTimeKind.Utc) + new TimeSpan(expirationInDays, 0, 0, 0);
            if (expirationDate < DateTime.Now)
            {
                return(new string[] { "Invalid activation key: key expired.", String.Format("{0}. Expiration: {1}", activationKeyString, expirationDate.ToString()) });
            }

            ActivationFlags flags = (ActivationFlags)activationKey[10];

            if ((flags & ActivationFlags.RequireOnline) != 0)
            {
                return(new string[] { "Invalid activation key: online activation required but not supported in this cdeEngine version.", String.Format("{0}", activationKeyString) });
            }
            byte licenseCount = activationKey[12];

            if (licenseCount > MaxLicensesInActivationKey)
            {
                return(new string[] { "Invalid activation key: too many licenses specified.", String.Format("{0}. License Count: {1}", activationKeyString, licenseCount) });
            }
            if (licenseCount == 0)
            {
                return(new string[] { "Invalid activation key: no licenses specified.", String.Format("{0}. License Count: {1}", activationKeyString, 0) });
            }
            if (licenseCount > allLicenses.Count)
            {
                return(new string[] { "Unable to apply activation key: some licenses not available on the system.", String.Format("{0}. License Count in key: {1}. Total valid licenses on system: {2}", activationKeyString, licenseCount, allLicenses.Count) });
            }

            byte[] parameters  = new byte[TheActivationUtils.MaxLicenseParameters];
            int    paramOffset = 13;

            for (int j = 0; j < TheActivationUtils.MaxLicenseParameters; j++)
            {
                parameters[j] = activationKey[paramOffset];
                paramOffset++;
            }

            int[]        candidateIndices = new int[licenseCount];
            TheLicense[] candidates       = new TheLicense[licenseCount];
            bool         done             = false;

            do
            {
                bool validCombination = true;
                for (int i = 0; i < licenseCount; i++)
                {
                    candidates[i] = allLicenses[candidateIndices[i]];
                    if (i > 0 && String.CompareOrdinal(candidates[i].LicenseId.ToString(), candidates[i - 1].LicenseId.ToString()) <= 0)
                    {
                        validCombination = false;
                        break;
                    }
                }

                if (validCombination && TheActivationUtils.GenerateLicenseSignature(deviceId, signingKey, (uint)expirationInDays, candidates.ToArray(), parameters, flags, out byte[] candidateSignature))
Пример #2
0
        /// <summary>
        /// Retrieves information from a token generated using the GetActivationRequestKey method.
        /// Typically used by a cdeEngine applicationId owner to generate activation keys.
        /// </summary>
        /// <param name="activationRequestKey">The token obtained from GetActivationRequestKey.</param>
        /// <param name="creationTime">The time when the token was created (local machine time, truncated to 10 minute boundary)</param>
        /// <param name="skuId">Application owner-defined SKU identifier, that is typically used to identify the set of licenses to included in the activation key.</param>
        /// <param name="deviceId">NodeId of the node where the activationRequestKey was generated. The activation key is typically bound to this node id.</param>
        /// <returns></returns>
        public static bool ParseActivationRequestKey(string activationRequestKey, out DateTime creationTime, out uint skuId, out Guid deviceId)
        {
            creationTime = DateTime.MinValue;
            skuId        = 0;
            deviceId     = Guid.Empty;

            var normalizedKey = activationRequestKey.Replace("-", "").ToUpper().Replace('O', '0').Replace('U', 'V').Replace('I', 'J').Replace('L', 'J');

            if (normalizedKey.Length != 36)
            {
                return(false);
            }

            byte[] activationRequestKeyArray = TheActivationUtils.Base32Decode(normalizedKey);

            if (activationRequestKeyArray.Length != 23)
            {
                return(false);
            }

            byte checksum = 0;
            int  j        = 0;

            byte[] tGu = new byte[16];

            uint tenMinuteIntervalsSinceUnixEpoch = 0;

            for (int i = 0; i < tGu.Length; i++)
            {
                if (i < 3)
                {
                    tenMinuteIntervalsSinceUnixEpoch += (uint)(activationRequestKeyArray[j] ^ checksum) << (i * 8);
                    checksum += activationRequestKeyArray[j];
                    j++;
                }
                else if (i < 5)
                {
                    skuId    += (uint)(activationRequestKeyArray[j] ^ checksum) << ((i - 3) * 8);
                    checksum += activationRequestKeyArray[j];
                    j++;
                }
                tGu[i]    = (byte)(activationRequestKeyArray[j] ^ checksum);
                checksum += activationRequestKeyArray[j];
                j++;
            }
            if (activationRequestKeyArray[j] != (checksum & 0xff) || activationRequestKeyArray[j + 1] != 15)
            {
                skuId = 0;
                return(false);
            }
            //if (activationRequestKeyArray[j+1] != (byte) ((checksum >> 8) + activationRequestKeyArray[0]))
            //{
            //    return Guid.Empty;
            //}
            //if (activationRequestKeyArray[j + 2] != (byte)(((activationRequestKeyArray[0]) | 0x02) & 0x03))
            //{
            //    return Guid.Empty;
            //}
            creationTime = new DateTime(1970, 1, 1) + new TimeSpan(0, (int)(tenMinuteIntervalsSinceUnixEpoch * 10), 0);
            deviceId     = new Guid(tGu);
            return(true);
        }