private void frmAuthUser_Load(object sender, System.EventArgs e) { this.Show(); this.txtEmail.Focus(); this.txtMachineCode.Text = CConfig.GetHardKey(); }
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); } }