Ejemplo n.º 1
0
        internal static License VerifySignedLicense(Rsa rsa, string signedLicense)
        {
            string signature, xaml;

            using (StringReader reader = new StringReader(signedLicense))
            {
                string signatureLine = reader.ReadLine();
                xaml = reader.ReadToEnd();

                if (!signatureLine.StartsWith(SignatureHeader, StringComparison.Ordinal))
                {
                    throw new InvalidOperationException(ExceptionMessages.CanNotVerifySignedLicense);
                }

                signature = signatureLine.Substring(SignatureHeader.Length, signatureLine.Length - SignatureHeader.Length);
            }

            byte[] xamlBytes      = Encoding.UTF8.GetBytes(xaml);
            byte[] signatureBytes = BytesFromString(signature);
            if (!rsa.VerifyData(xamlBytes, Hasher, signatureBytes))
            {
                throw new InvalidOperationException(ExceptionMessages.CanNotVerifySignedLicense);
            }

            License license = License.LoadFromXaml(xaml);

            license.SignedString = signedLicense;
            return(license);
        }
Ejemplo n.º 2
0
        private void InternalConstruct(string publicKeyXml)
        {
            if (string.IsNullOrEmpty(publicKeyXml))
            {
                throw new ArgumentNullException("publicKeyXml");
            }

            _publicKey = new Rsa(publicKeyXml);
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
0
        internal static LicenseKey DecryptFromString(Rsa rsaKey, string encryptedLicenseKey)
        {
            if (string.IsNullOrEmpty(encryptedLicenseKey))
            {
                return(LicenseKey.Empty);
            }

            return(new LicenseKey(DecryptString(rsaKey, encryptedLicenseKey)));
        }
Ejemplo n.º 6
0
        internal static string SignLicense(Rsa rsa, License license)
        {
            string xaml = XamlWriter.Save(license);

            byte[] xamlBytes      = Encoding.UTF8.GetBytes(xaml);
            byte[] signatureBytes = rsa.SignData(xamlBytes, Hasher);
            string signature      = BytesToString(signatureBytes);

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine(SignatureHeader + signature);
            stringBuilder.Append(xaml);
            return(stringBuilder.ToString());
        }
Ejemplo n.º 7
0
        internal string EncryptToString(Rsa rsaKey)
        {
            if (rsaKey == null)
            {
                throw new ArgumentNullException("rsaKey");
            }

            if (IsEmpty)
            {
                return(null);
            }

            return(EncryptString(rsaKey, ToString()));
        }
Ejemplo n.º 8
0
        private Rsa GetPrivateKey(string product)
        {
            if (!_cachePrivateKey)
            {
                return(new Rsa(GetPrivateKeyXml(product)));
            }

            if (s_cachedPrivateKeys.ContainsKey(product))
            {
                return(s_cachedPrivateKeys[product]);
            }

            string xml = GetPrivateKeyXml(product);
            Rsa    rsa = new Rsa(xml);

            s_cachedPrivateKeys.Add(product, rsa);
            return(rsa);
        }
Ejemplo n.º 9
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);
            }
        }
Ejemplo n.º 10
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));
        }