/// <summary>
        /// Generates a new floating license.
        /// </summary>
        /// <param name="name">Name of the license holder</param>
        /// <param name="publicKey">The public key of the license server</param>
        /// <returns>The generated license XML string</returns>
        public string GenerateFloatingLicense(string name, string publicKey)
        {
            using (var rsa = new RSACryptoServiceProvider())
            {
                RSAKeyExtensions.FromXmlString(rsa, privateKey);
                var doc     = new XmlDocument();
                var license = doc.CreateElement("floating-license");
                doc.AppendChild(license);

                var publicKeyEl = doc.CreateElement("license-server-public-key");
                license.AppendChild(publicKeyEl);
                publicKeyEl.InnerText = publicKey;

                var nameEl = doc.CreateElement("name");
                license.AppendChild(nameEl);
                nameEl.InnerText = name;

                var signature = GetXmlDigitalSignature(doc, rsa);
                doc.FirstChild.AppendChild(doc.ImportNode(signature, true));

                var ms     = new MemoryStream();
                var writer = XmlWriter.Create(ms, new XmlWriterSettings
                {
                    Indent   = true,
                    Encoding = Encoding.UTF8
                });
                doc.Save(writer);
                ms.Position = 0;
                return(new StreamReader(ms).ReadToEnd());
            }
        }
        /// <summary>
        /// Generates a new license using a specified signing <paramref name="algorithm"/>
        /// </summary>
        /// <param name="name">The name of the license holder</param>
        /// <param name="id">Id of the license holder</param>
        /// <param name="expirationDate">License expiry date</param>
        /// <param name="attributes">Extra information stored as key/value in the license file</param>
        /// <param name="licenseType">Type of the license</param>
        /// <param name="algorithm">Signing algorithm to use for signing the XML</param>
        /// <returns>The generated license string</returns>
        public string Generate(string name, Guid id, DateTime expirationDate, IDictionary <string, string> attributes, LicenseType licenseType, SigningAlgorithm algorithm)
        {
            using (var rsa = new RSACryptoServiceProvider())
            {
                RSAKeyExtensions.FromXmlString(rsa, privateKey);
                var doc = CreateDocument(id, name, expirationDate, attributes, licenseType);

                var signature = GetXmlDigitalSignature(doc, rsa, algorithm);
                doc.FirstChild.AppendChild(doc.ImportNode(signature, true));

                var ms     = new MemoryStream();
                var writer = XmlWriter.Create(ms, new XmlWriterSettings
                {
                    Indent   = true,
                    Encoding = Encoding.UTF8
                });
                doc.Save(writer);
                ms.Position = 0;
                return(new StreamReader(ms).ReadToEnd());
            }
        }
Exemple #3
0
        private bool TryGetValidDocument(string licensePublicKey, XmlDocument doc)
        {
            var rsa = new RSACryptoServiceProvider();

            RSAKeyExtensions.FromXmlString(rsa, licensePublicKey);

            var nsMgr = new XmlNamespaceManager(doc.NameTable);

            nsMgr.AddNamespace("sig", "http://www.w3.org/2000/09/xmldsig#");

            var signedXml = new SignedXml(doc);
            var sig       = (XmlElement)doc.SelectSingleNode("//sig:Signature", nsMgr);

            if (sig == null)
            {
                Log.WarnFormat("[Licensing] Could not find this signature node on license:\r\n{0}", License);
                return(false);
            }
            signedXml.LoadXml(sig);

            return(signedXml.CheckSignature(rsa));
        }