Exemplo n.º 1
0
        private static string EncryptString(Rsa rsaKey, string value)
        {
            Debug.Assert(rsaKey != null);
            Debug.Assert(!string.IsNullOrEmpty(value));

            byte[] bytes          = Encoding.UTF8.GetBytes(value);
            byte[] encryptedBytes = rsaKey.Encrypt(bytes);
            return(LicenseManager.BytesToString(encryptedBytes));
        }
Exemplo n.º 2
0
        private static string DecryptString(Rsa rsaKey, string encryptedText)
        {
            Debug.Assert(rsaKey != null);
            Debug.Assert(!string.IsNullOrEmpty(encryptedText));

            byte[] encryptedBytes = LicenseManager.BytesFromString(encryptedText);
            byte[] decryptedBytes = rsaKey.Decrypt(encryptedBytes);
            return(Encoding.UTF8.GetString(decryptedBytes));
        }
Exemplo n.º 3
0
        /// <summary>Gets a license for specified assembly, given specified design time or runtime mode.</summary>
        /// <param name="assembly">The specified assembly.</param>
        /// <param name="designMode">Specifies the design time or runtime mode. <see langword="true" /> for design time, otherwise runtime.</param>
        /// <returns>A <see cref="License" /> object, or <see langword="null" /> for the assembly is not licensed.</returns>
        /// <remarks>The returned <see cref="License" /> object may not be able to be granted as a valid license. Call
        /// <see href="xref:DevZest.Licensing.LicenseManager.Check*">LicenseManager.Check</see> method to determine whether a
        /// valid license can be granted.</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="assembly" /> is <see langword="null"/>.</exception>
        public static License GetLicense(Assembly assembly, bool designMode)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            LicenseManager manager = GetLicenseManager(assembly);

            return(manager.ProvideLicense(designMode));
        }
Exemplo n.º 4
0
        private static string GetAssemblyHash(Assembly assembly)
        {
            foreach (object obj in GetEnumerable(assembly.Evidence.GetHostEnumerator()))
            {
                Hash hash = obj as Hash;
                if (hash != null)
                {
                    return(LicenseManager.BytesToString(hash.SHA1));
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        /// <summary>Gets <see cref="License" /> from <see cref="LicensePublisher" /> using specified culture.</summary>
        /// <param name="cultureInfo">The specified culture information.</param>
        /// <param name="product">The product name.</param>
        /// <param name="version">The product version.</param>
        /// <param name="licenseKey">The license key.</param>
        /// <param name="category">The license category.</param>
        /// <param name="name">The user name.</param>
        /// <param name="company">The user company.</param>
        /// <param name="email">The user email address.</param>
        /// <param name="data">The data to set in the license.</param>
        /// <returns>The response from <see cref="LicensePublisher" /> which encapsulates a <see cref="License" /> object or
        /// an error message.</returns>
        public LicensePublisherResponse GetLicense(CultureInfo cultureInfo, string product, Version version, LicenseKey licenseKey, string category, string name, string company, string email, string data)
        {
            string encryptedResponse = base.Channel.Publish(cultureInfo.LCID, product, version.ToString(), licenseKey.EncryptToString(PublicKey), category, name, company, email, data);
            string response          = licenseKey.Decrypt(encryptedResponse);

            if (response.StartsWith(ErrorHeader, StringComparison.Ordinal))
            {
                string errorMessage = response.Substring(ErrorHeader.Length, response.Length - ErrorHeader.Length);
                return(new LicensePublisherResponse(errorMessage));
            }
            else
            {
                return(new LicensePublisherResponse(LicenseManager.VerifySignedLicense(PublicKey, response)));
            }
        }
Exemplo n.º 6
0
        protected sealed override LicenseError Validate()
        {
            if (_verified)
            {
                Byte[] encryptedBytes = LicenseManager.BytesFromString(Data);
                Byte[] bytes          = ProtectedData.Unprotect(encryptedBytes, null, DataProtectionScope.LocalMachine);
                if (new Guid(bytes) != Guid)
                {
                    _error = new LicenseError(Assembly, LicenseErrorReason.InvalidLicense, Messages.InvalidMachineData, this);
                }
                _verified = true;
            }

            return(_error);
        }
Exemplo n.º 7
0
        public static void Reset(Assembly assembly)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            if (s_managers.ContainsKey(assembly))
            {
                LicenseManager manager = s_managers[assembly];
                Debug.Assert(manager != null);
                bool succeed = s_managers.Remove(assembly);
                Debug.Assert(succeed);
            }
        }
Exemplo n.º 8
0
        internal string Decrypt(string encryptedText)
        {
            if (IsEmpty)
            {
                Byte[] bytes = Convert.FromBase64String(encryptedText);
                return(Encoding.UTF8.GetString(bytes));
            }

            byte[] input = LicenseManager.BytesFromString(encryptedText);
            byte[] output;
            using (TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider())
            {
                output = Transform(input, des.CreateDecryptor(GetTripleDesKey(_key), GetTripleDesIv(_key)));
            }
            return(Encoding.UTF8.GetString(output));
        }
Exemplo n.º 9
0
        internal string Encrypt(string text)
        {
            byte[] input = Encoding.UTF8.GetBytes(text);

            if (IsEmpty)
            {
                return(Convert.ToBase64String(input));
            }

            byte[] output;
            using (TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider())
            {
                output = Transform(input, des.CreateEncryptor(GetTripleDesKey(_key), GetTripleDesIv(_key)));
            }
            return(LicenseManager.BytesToString(output));
        }
Exemplo n.º 10
0
        private License LoadLicense(string license, object providerData)
        {
            if (string.IsNullOrEmpty(license))
            {
                return(null);
            }

            Assembly assembly     = Assembly;
            string   publicKeyXml = PublicKeyXmlFromAssembly(assembly);

            if (string.IsNullOrEmpty(publicKeyXml))
            {
                throw new InvalidOperationException(ExceptionMessages.FormatNullPublicKey(assembly));
            }
            using (Rsa publicKey = new Rsa(publicKeyXml))
            {
                License result = (License)LicenseManager.VerifySignedLicense(publicKey, license);
                result.ProviderData = providerData;
                return(result);
            }
        }
Exemplo n.º 11
0
        private static LicenseManager GetLicenseManager(Assembly assembly)
        {
            Debug.Assert(assembly != null);

            LicenseManager manager;

            if (Managers.ContainsKey(assembly))
            {
                manager = Managers[assembly];
                // Check Manager.Assembly to prevent it being changed by reflection
                if (manager.Assembly != assembly)
                {
                    throw new InvalidOperationException();
                }
                return(manager);
            }

            manager = new LicenseManager(assembly);

            Managers.Add(assembly, manager);
            return(manager);
        }
Exemplo n.º 12
0
        public string Publish(int culture, string product, string version, string encryptedLicenseKey, string category, string name, string company, string email, string data)
        {
            Rsa privateKey = GetPrivateKey(product);

            if (privateKey == null)
            {
                throw new InvalidOperationException(ExceptionMessages.FormatNullPrivateKey(product));
            }

            string     responseString;
            LicenseKey licenseKey;

            try
            {
                licenseKey = LicenseKey.DecryptFromString(privateKey, encryptedLicenseKey);

                LicensePublisherResponse response = GetLicense(new CultureInfo(culture), product, new Version(version), licenseKey, category, name, company, email, data);
                License license = response.License;

                if (license == null)
                {
                    responseString = LicenseClient.ErrorHeader + response.ErrorMessage;
                }
                else
                {
                    responseString = LicenseManager.SignLicense(privateKey, license);
                }
            }
            finally
            {
                if (!_cachePrivateKey)
                {
                    ((IDisposable)privateKey).Dispose();
                }
            }

            return(licenseKey.Encrypt(responseString));
        }
Exemplo n.º 13
0
 /// <summary>Gets the private key XML string from Strong Name Key (.snk) stream.</summary>
 /// <param name="stream">The stream of .snk file.</param>
 /// <returns>The private key XML string.</returns>
 public static string PrivateKeyXmlFromSnkFile(Stream stream)
 {
     return(LicenseManager.PrivateKeyXmlFromSnkFile(stream));
 }
Exemplo n.º 14
0
 /// <overloads>Gets the private key XML string from Strong Name Key (.snk) file or stream.</overloads>
 /// <summary>Gets the private key XML string from Strong Name Key (.snk) file.</summary>
 /// <param name="snkFilePath">Full path and name of the .snk file.</param>
 /// <returns>The private key XML string.</returns>
 public static string PrivateKeyXmlFromSnkFile(string snkFilePath)
 {
     return(LicenseManager.PrivateKeyXmlFromSnkFile(snkFilePath));
 }
Exemplo n.º 15
0
 /// <overloads>Gets public key XML string from specified assembly.</overloads>
 /// <summary>Gets public key XML string from specified assembly.</summary>
 /// <param name="assembly">The specified assembly.</param>
 /// <returns>The public key XML. <see langword="null" /> if assembly is not signed with a strong name.</returns>
 public static string PublicKeyXmlFromAssembly(Assembly assembly)
 {
     return(LicenseManager.PublicKeyXmlFromAssembly(assembly));
 }
Exemplo n.º 16
0
 internal void Freeze(LicenseManager manager)
 {
     System.Diagnostics.Debug.Assert(manager != null);
     _manager = manager;
 }
Exemplo n.º 17
0
 /// <summary>Gets public key XML string from specified assembly file.</summary>
 /// <param name="assemblyPath">The full path of the assembly file.</param>
 /// <returns>The public key XML. <see langword="null" /> if assembly is not signed with a strong name.</returns>
 public static string PublicKeyXmlFromAssembly(string assemblyPath)
 {
     return(LicenseManager.PublicKeyXmlFromAssembly(assemblyPath));
 }