Esempio n. 1
0
        /*private void GenerateLicense() {
         *      DSA dsa = new DSACryptoServiceProvider();
         *      dsa.FromXmlString(PrivateKey);
         *
         *      string vfyhash =
         *              "Sam Bacsa" +
         *              "netMercs Group" +
         *              "Unlimited" +
         *              "CMS-102022-00UL" +
         *              "Never" +
         *              "1.2" +
         *              "1.3";
         *
         *      // Grab all the featurebits
         *      foreach (bool val in _features.Values)
         *              vfyhash += val.ToString();
         *
         *      // Hash it and get the result
         *      string hash = Convert.ToBase64String(dsa.CreateSignature(Convert.FromBase64String(CConfig.SHA1(vfyhash))));
         * }*/

        private bool VerifyLicense()
        {
            // Verifies the loaded license details to see if they
            // check out
            g.LogDebug("LicenseManager::VerifyLicense: Enter");

            g.LogDebug("LicenseManager::VerifyLicense: Loading DSA object and public key");
            DSA dsa = new DSACryptoServiceProvider();

            dsa.FromXmlString(PublicKey);

            g.LogDebug("LicenseManager::VerifyLicense: Object loaded.  Key is " + PublicKey);

            string vfyhash = _user + _company + _limit + _serial + ((_expires.Year == 1) ? "Never" : _expires.ToString())
                             + _version.ToString() + _version_limit.ToString();

            // Load the feature bits
            foreach (bool val in _features.Values)
            {
                vfyhash += val.ToString();
            }

            g.LogDebug("LicenseManager::VerifyLicense: vfyhash is " + vfyhash);
            g.LogDebug("LicenseManager::VerifyLicense: Verifying signature");

            bool result = dsa.VerifySignature(
                Convert.FromBase64String(CConfig.SHA1(vfyhash)),
                Convert.FromBase64String(_signature)
                );

            g.LogDebug("LicenseManager::VerifyLicense: Result of signature verification is: " + result.ToString());
            g.LogDebug("LicenseManager::VerifyLicense: Leave");

            return(result);
        }
Esempio n. 2
0
        internal static bool VerifyLocalKeyfile(string filename, out AuthInfo authinfo)
        {
            StreamReader reader = null;
            string       data   = "";

            authinfo = null;

            try {
                reader = new StreamReader(filename);
                data   = reader.ReadToEnd();
                reader.Close();
            } catch (Exception exc) {
                MessageBox.Show("Offline key authorization failure: Could not read keyfile.\n\n\n" + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            Regex rx = new Regex(@"\s*Keyfile(\n*|\s*)?" +
                                 @"(?<inner>" +
                                 @"(?>" +
                                 @"\{(?<LEVEL>)" +
                                 @"|" +
                                 @"\};(?<-LEVEL>)" +
                                 @"|" +
                                 @"(?!\{|\};)." +
                                 @")+" +
                                 @"(?(LEVEL)(?!))" +
                                 @")"
                                 , RegexOptions.IgnoreCase | RegexOptions.Singleline);

            Match topmatch = rx.Match(data);

            if (!topmatch.Success)
            {
                MessageBox.Show("Offline key authorization failure: Could not find valid 'keyfile' block.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            string keyfile_data = topmatch.Groups["inner"].Value;

            // Once we have the top matched group, parse each line in it
            rx = new Regex(@"\s*\b(?<name>[A-Z]*)\b\s*=\s*((" + "\"" + @"\s*(?<single>.*?)\s*" + "\"" + @"\s*;)|(\{(?<multi>.*?)\};))", RegexOptions.Singleline | RegexOptions.IgnoreCase);

            // 1 = name
            // 4 = normal definition
            // 6 = multiline definition

            string version     = "";
            string name        = "";
            string expires     = "";
            string machinecode = "";
            string featurebit  = "";
            string hash        = "";
            string signature   = "";
            string email       = "";

            foreach (Match m in rx.Matches(keyfile_data))
            {
                switch (m.Groups["name"].Value.ToLower())
                {
                case "version":
                    version = m.Groups["single"].Value;
                    break;

                case "name":
                    name = m.Groups["single"].Value;
                    break;

                case "expires":
                    expires = m.Groups["single"].Value;
                    break;

                case "machinecode":
                    machinecode = m.Groups["single"].Value;
                    break;

                case "featurebit":
                    featurebit = m.Groups["single"].Value;
                    break;

                case "hash":
                    hash = m.Groups["single"].Value;
                    break;

                case "keysignature":
                    signature = m.Groups["multi"].Value;
                    break;

                case "email":
                    email = m.Groups["single"].Value;
                    break;
                }
            }

            // Clean up signature
            signature = signature.Replace(" ", "");
            signature = signature.Replace("\t", "");
            signature = signature.Replace("\n", "");
            signature = signature.Replace("\r", "");


            // Check field validity
            if (version == "" || expires == "" || machinecode == "" || featurebit == "" || hash == "" || name == "" || email == "" || signature == "")
            {
                MessageBox.Show("Offline key authorization failure: Keyfile construction incomplete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            // Check version
            if (version != "1.00")
            {
                MessageBox.Show("Offline key authorization failure: Incorrect version (Expected '1.00')", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            // Check expiration date
            try {
                DateTime dt = DateTime.Parse(expires);
                if (DateTime.Now.Ticks >= dt.Ticks)
                {
                    MessageBox.Show("Offline key authorization has expired.  Please visit www.torquedev.com to request a new offline authorization file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
            } catch (Exception exc) {
                MessageBox.Show("Offline key authorization failure: Unable to verify date stamp.  Error returned was: " + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            // Check if the machine codes match
            if (machinecode != CConfig.GetHardKey())
            {
                MessageBox.Show("Offline key authorization failure: Machine code mismatch.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            // Verify hash of data
            string sighash = CConfig.SHA1(version + name + machinecode + expires + featurebit + email);

            if (sighash != hash)
            {
                MessageBox.Show("Offline key authorization failure: Checksum mismatch.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            // Verify signature by first loading the public key into memory
            DSA dsa = new DSACryptoServiceProvider();

            try {
                dsa.FromXmlString(CVerify.PublicKey);
            } catch (Exception exc) {
                MessageBox.Show("Offline key authorization failure: Unable to load DSA public key.  Error returned was: " + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            // Verify the data against the signature
            try {
                if (!dsa.VerifySignature(Convert.FromBase64String(hash), Convert.FromBase64String(signature)))
                {
                    MessageBox.Show("Offline key authorization failure: File signature is invalid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    dsa.Clear();
                    return(false);
                }
                else
                {
                    // Signature checked out.  Create the passback reference
                    authinfo = new AuthInfo(version, name, expires, machinecode, featurebit, hash, signature, email);
                    return(true);
                }
            } catch (Exception exc) {
                MessageBox.Show("Offline key authorization failure: DSA verification failure.  Error returned was: " + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                dsa.Clear();
                return(false);
            }
        }