public bool ReadXml(XmlReader reader, string password)
        {
            bool changed = false;

              Guid id;
            #if NETFX_4
            if (Guid.TryParse(reader.GetAttribute("id"), out id) == true)
              {
            Id = id;
              }
            #endif
            #if NETFX_3
            try
            {
                id = new Guid(reader.GetAttribute("id"));
                Id = id;
            }
            catch (Exception) { }
            #endif

            string authenticatorType = reader.GetAttribute("type");
              if (string.IsNullOrEmpty(authenticatorType) == false)
              {
            Type type = typeof(Authenticator).Assembly.GetType(authenticatorType, false, true);
            this.AuthenticatorData = Activator.CreateInstance(type) as Authenticator;
              }

            //string encrypted = reader.GetAttribute("encrypted");
            //if (string.IsNullOrEmpty(encrypted) == false)
            //{
            //	// read the encrypted text from the node
            //	string data = reader.ReadElementContentAsString();
            //	// decrypt
            //	Authenticator.PasswordTypes passwordType;
            //	data = Authenticator.DecryptSequence(data, encrypted, password, out passwordType);

            //	using (MemoryStream ms = new MemoryStream(Authenticator.StringToByteArray(data)))
            //	{
            //		reader = XmlReader.Create(ms);
            //		ReadXml(reader, password);
            //	}
            //	this.PasswordType = passwordType;
            //	this.Password = password;

            //	return;
            //}

              reader.MoveToContent();

              if (reader.IsEmptyElement)
              {
            reader.Read();
            return changed;
              }

              reader.Read();
              while (reader.EOF == false)
              {
            if (reader.IsStartElement())
            {
              switch (reader.Name)
              {
            case "name":
              Name = reader.ReadElementContentAsString();
              break;

            case "created":
              long t = reader.ReadElementContentAsLong();
              t += Convert.ToInt64(new TimeSpan(new DateTime(1970, 1, 1).Ticks).TotalMilliseconds);
              t *= TimeSpan.TicksPerMillisecond;
              Created = new DateTime(t).ToLocalTime();
              break;

            case "autorefresh":
              _autoRefresh = reader.ReadElementContentAsBoolean();
              break;

            case "allowcopy":
              _allowCopy= reader.ReadElementContentAsBoolean();
              break;

            case "copyoncode":
              _copyOnCode = reader.ReadElementContentAsBoolean();
              break;

            case "hideserial":
              _hideSerial = reader.ReadElementContentAsBoolean();
              break;

            case "skin":
              _skin = reader.ReadElementContentAsString();
              break;

                        case "hotkey":
                            _hotkey = new WinAuth.HotKey();
                            _hotkey.ReadXml(reader);
                            break;

                        case "authenticatordata":
                            try
                            {
                                // we don't pass the password as they are locked till clicked
                                changed = this.AuthenticatorData.ReadXml(reader) || changed;
                            }
                            catch (EncrpytedSecretDataException )
                            {
                                // no action needed
                            }
                            catch (BadPasswordException)
                            {
                                // no action needed
                            }
              break;

                        // v2
                        case "authenticator":
                            this.AuthenticatorData = Authenticator.ReadXmlv2(reader, password);
                            break;
                        // v2
                        case "autologin":
              var hks = new HoyKeySequence();
              hks.ReadXml(reader, password);
              break;
                        // v2
                        case "servertimediff":
                            this.AuthenticatorData.ServerTimeDiff = reader.ReadElementContentAsLong();
                            break;

            default:
              reader.Skip();
              break;
              }
            }
            else
            {
              reader.Read();
              break;
            }
              }

            return changed;
        }
Example #2
0
		/// <summary>
		/// Click the OK button to save the keys
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void okButton_Click(object sender, EventArgs e)
		{
			WinAPI.VirtualKeyCode key = (keyCombo.SelectedItem as KeyItem != null ? ((KeyItem)keyCombo.SelectedItem).Key : default(WinAPI.VirtualKeyCode));
			if (key == 0)
			{
				this.Hotkey = null;
			}
			else if (this.Hotkey == null)
			{
				this.Hotkey = new HotKey();
			}

			if (this.Hotkey != null)
			{
				WinAPI.KeyModifiers modifiers = WinAPI.KeyModifiers.None;
				if (shiftToggle.Checked)
				{
					modifiers |= WinAPI.KeyModifiers.Shift;
				}
				if (ctrlToggle.Checked)
				{
					modifiers |= WinAPI.KeyModifiers.Control;
				}
				if (altToggle.Checked)
				{
					modifiers |= WinAPI.KeyModifiers.Alt;
				}

				// check it is available if this is a different hotkey
				if ((key != this.Hotkey.Key || modifiers != this.Hotkey.Modifiers) && KeyboardHook.IsHotkeyAvailable(this, (Keys)key, modifiers) == false)
				{
					WinAuthForm.ErrorDialog(this, strings.HotKeyNotAvailable);
					this.DialogResult = System.Windows.Forms.DialogResult.None;
					return;
				}

				this.Hotkey.Key = key;
				this.Hotkey.Modifiers = modifiers;

				if (notifyRadioButton.Checked == true)
				{
					this.Hotkey.Action = HotKey.HotKeyActions.Notify;
					this.Hotkey.Window = null;
					this.Hotkey.Advanced = null;
				}
				else if (injectRadioButton.Checked == true)
				{
					this.Hotkey.Action = HotKey.HotKeyActions.Inject;
					this.Hotkey.Window = this.injectTextbox.Text;
					this.Hotkey.Advanced = null;
				}
				else if (pasteRadioButton.Checked == true)
				{
					this.Hotkey.Action = HotKey.HotKeyActions.Copy;
					this.Hotkey.Window = null;
					this.Hotkey.Advanced = null;
				}
				else if (advancedRadioButton.Checked == true)
				{
					this.Hotkey.Action = HotKey.HotKeyActions.Advanced;
					this.Hotkey.Window = null;
					this.Hotkey.Advanced = this.advancedTextbox.Text;
				}
			}
		}
Example #3
0
        public bool ReadXml(XmlReader reader, string password)
        {
            bool changed = false;

            Guid id;

            if (Guid.TryParse(reader.GetAttribute("id"), out id) == true)
            {
                Id = id;
            }

            string authenticatorType = reader.GetAttribute("type");

            if (string.IsNullOrEmpty(authenticatorType) == false)
            {
                Type type = typeof(Authenticator).Assembly.GetType(authenticatorType, false, true);
                this.AuthenticatorData = Activator.CreateInstance(type) as Authenticator;
            }

            //string encrypted = reader.GetAttribute("encrypted");
            //if (string.IsNullOrEmpty(encrypted) == false)
            //{
            //	// read the encrypted text from the node
            //	string data = reader.ReadElementContentAsString();
            //	// decrypt
            //	Authenticator.PasswordTypes passwordType;
            //	data = Authenticator.DecryptSequence(data, encrypted, password, out passwordType);

            //	using (MemoryStream ms = new MemoryStream(Authenticator.StringToByteArray(data)))
            //	{
            //		reader = XmlReader.Create(ms);
            //		ReadXml(reader, password);
            //	}
            //	this.PasswordType = passwordType;
            //	this.Password = password;

            //	return;
            //}

            reader.MoveToContent();

            if (reader.IsEmptyElement)
            {
                reader.Read();
                return(changed);
            }

            reader.Read();
            while (reader.EOF == false)
            {
                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                    case "name":
                        Name = reader.ReadElementContentAsString();
                        break;

                    case "created":
                        long t = reader.ReadElementContentAsLong();
                        t      += Convert.ToInt64(new TimeSpan(new DateTime(1970, 1, 1).Ticks).TotalMilliseconds);
                        t      *= TimeSpan.TicksPerMillisecond;
                        Created = new DateTime(t).ToLocalTime();
                        break;

                    case "autorefresh":
                        _autoRefresh = reader.ReadElementContentAsBoolean();
                        break;

                    case "allowcopy":
                        _allowCopy = reader.ReadElementContentAsBoolean();
                        break;

                    case "copyoncode":
                        _copyOnCode = reader.ReadElementContentAsBoolean();
                        break;

                    case "hideserial":
                        _hideSerial = reader.ReadElementContentAsBoolean();
                        break;

                    case "skin":
                        _skin = reader.ReadElementContentAsString();
                        break;

                    case "hotkey":
                        _hotkey = new WinAuth.HotKey();
                        _hotkey.ReadXml(reader);
                        break;

                    case "authenticatordata":
                        try
                        {
                            // we don't pass the password as they are locked till clicked
                            changed = this.AuthenticatorData.ReadXml(reader) || changed;
                        }
                        catch (EncrpytedSecretDataException)
                        {
                            // no action needed
                        }
                        catch (BadPasswordException)
                        {
                            // no action needed
                        }
                        break;

                    // v2
                    case "authenticator":
                        this.AuthenticatorData = Authenticator.ReadXmlv2(reader, password);
                        break;

                    // v2
                    case "autologin":
                        var hks = new HoyKeySequence();
                        hks.ReadXml(reader, password);
                        break;

                    // v2
                    case "servertimediff":
                        this.AuthenticatorData.ServerTimeDiff = reader.ReadElementContentAsLong();
                        break;


                    default:
                        reader.Skip();
                        break;
                    }
                }
                else
                {
                    reader.Read();
                    break;
                }
            }

            return(changed);
        }
Example #4
0
        /// <summary>
        ///     Click the OK button to save the keys
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void okButton_Click(object sender, EventArgs e)
        {
            var key = keyCombo.SelectedItem as KeyItem != null ? ((KeyItem)keyCombo.SelectedItem).Key : default;

            if (key == 0)
            {
                Hotkey = null;
            }
            else if (Hotkey == null)
            {
                Hotkey = new HotKey();
            }

            if (Hotkey != null)
            {
                var modifiers = WinAPI.KeyModifiers.None;
                if (shiftToggle.Checked)
                {
                    modifiers |= WinAPI.KeyModifiers.Shift;
                }
                if (ctrlToggle.Checked)
                {
                    modifiers |= WinAPI.KeyModifiers.Control;
                }
                if (altToggle.Checked)
                {
                    modifiers |= WinAPI.KeyModifiers.Alt;
                }

                // check it is available if this is a different hotkey
                if ((key != Hotkey.Key || modifiers != Hotkey.Modifiers) &&
                    KeyboardHook.IsHotkeyAvailable(this, (Keys)key, modifiers) == false)
                {
                    WinAuthForm.ErrorDialog(this, strings.HotKeyNotAvailable);
                    DialogResult = DialogResult.None;
                    return;
                }

                Hotkey.Key       = key;
                Hotkey.Modifiers = modifiers;

                if (notifyRadioButton.Checked)
                {
                    Hotkey.Action   = HotKey.HotKeyActions.Notify;
                    Hotkey.Window   = null;
                    Hotkey.Advanced = null;
                }
                else if (injectRadioButton.Checked)
                {
                    Hotkey.Action   = HotKey.HotKeyActions.Inject;
                    Hotkey.Window   = injectTextbox.Text;
                    Hotkey.Advanced = null;
                }
                else if (pasteRadioButton.Checked)
                {
                    Hotkey.Action   = HotKey.HotKeyActions.Copy;
                    Hotkey.Window   = null;
                    Hotkey.Advanced = null;
                }
                else if (advancedRadioButton.Checked)
                {
                    Hotkey.Action   = HotKey.HotKeyActions.Advanced;
                    Hotkey.Window   = null;
                    Hotkey.Advanced = advancedTextbox.Text;
                }
            }
        }
Example #5
0
        protected bool ReadXmlInternal(XmlReader reader, string password = null)
        {
            bool changed = false;

            decimal version;

            if (decimal.TryParse(reader.GetAttribute("version"), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out version) == true)
            {
                Version = version;

                if (version > WinAuthConfig.CURRENTVERSION)
                {
                    // ensure we don't overwrite a newer config
                    throw new WinAuthInvalidNewerConfigException(string.Format(strings.ConfigIsNewer, version));
                }
            }

            string encrypted = reader.GetAttribute("encrypted");

            this.PasswordType = Authenticator.DecodePasswordTypes(encrypted);
            if (this.PasswordType != Authenticator.PasswordTypes.None)
            {
                // read the encrypted text from the node
                string data = reader.ReadElementContentAsString();
                // decrypt
                data = Authenticator.DecryptSequence(data, this.PasswordType, password);

                using (MemoryStream ms = new MemoryStream(Authenticator.StringToByteArray(data)))
                {
                    reader  = XmlReader.Create(ms);
                    changed = ReadXml(reader, password);
                }

                this.PasswordType = Authenticator.DecodePasswordTypes(encrypted);
                this.Password     = password;

                return(changed);
            }

            reader.MoveToContent();
            if (reader.IsEmptyElement)
            {
                reader.Read();
                return(changed);
            }

            bool   defaultAutoRefresh = true;
            bool   defaultAllowCopy   = false;
            bool   defaultCopyOnCode  = false;
            bool   defaultHideSerial  = true;
            string defaultSkin        = null;

            reader.Read();
            while (reader.EOF == false)
            {
                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                    case "config":
                        changed = ReadXmlInternal(reader, password) || changed;
                        break;

                    // 3.2 has new layout
                    case "data":
                    {
                        encrypted         = reader.GetAttribute("encrypted");
                        this.PasswordType = Authenticator.DecodePasswordTypes(encrypted);
                        if (this.PasswordType != Authenticator.PasswordTypes.None)
                        {
                            HashAlgorithm hasher;
                            string        hash = reader.GetAttribute("sha1");
                            if (string.IsNullOrEmpty(hash) == false)
                            {
                                hasher = Authenticator.SafeHasher("SHA1");
                            }
                            else
                            {
                                // old version has md5
                                hash   = reader.GetAttribute("md5");
                                hasher = Authenticator.SafeHasher("MD5");
                            }
                            // read the encrypted text from the node
                            string data = reader.ReadElementContentAsString();

                            hasher.ComputeHash(Authenticator.StringToByteArray(data));
                            hasher.Dispose();

                            // decrypt
                            data = Authenticator.DecryptSequence(data, this.PasswordType, password);
                            byte[] plain = Authenticator.StringToByteArray(data);

                            using (MemoryStream ms = new MemoryStream(plain))
                            {
                                var datareader = XmlReader.Create(ms);
                                changed = ReadXmlInternal(datareader, password) || changed;
                            }

                            this.PasswordType = Authenticator.DecodePasswordTypes(encrypted);
                            this.Password     = password;
                        }
                    }
                    break;

                    case "alwaysontop":
                        _alwaysOnTop = reader.ReadElementContentAsBoolean();
                        break;

                    case "usetrayicon":
                        _useTrayIcon = reader.ReadElementContentAsBoolean();
                        break;

                    case "notifyaction":
                        string s = reader.ReadElementContentAsString();
                        if (string.IsNullOrEmpty(s) == false)
                        {
                            try {
                                _notifyAction = (NotifyActions)Enum.Parse(typeof(NotifyActions), s, true);
                            }
                            catch (Exception) { }
                        }
                        break;

                    case "startwithwindows":
                        _startWithWindows = reader.ReadElementContentAsBoolean();
                        break;

                    case "autosize":
                        _autoSize = reader.ReadElementContentAsBoolean();
                        break;

                    case "copysearchedsingle":
                        _copySearchedSingle = reader.ReadElementContentAsBoolean();
                        break;

                    case "sortalphabetically":
                        _sortAlphabetically = reader.ReadElementContentAsBoolean();
                        break;

                    case "autoexitaftercopy":
                        _autoExitAfterCopy = reader.ReadElementContentAsBoolean();
                        break;

                    case "left":
                        _position.X = reader.ReadElementContentAsInt();
                        break;

                    case "top":
                        _position.Y = reader.ReadElementContentAsInt();
                        break;

                    case "width":
                        _width = reader.ReadElementContentAsInt();
                        break;

                    case "height":
                        _height = reader.ReadElementContentAsInt();
                        break;

                    case "shadowtype":
                        _shadowType = reader.ReadElementContentAsString();
                        break;

                    case "pgpkey":
                        _pgpKey = reader.ReadElementContentAsString();
                        break;

                    case "settings":
                        XmlSerializer serializer = new XmlSerializer(typeof(setting[]), new XmlRootAttribute()
                        {
                            ElementName = "settings"
                        });
                        _settings = ((setting[])serializer.Deserialize(reader)).ToDictionary(e => e.Key, e => e.Value);
                        break;

                    // previous setting used as defaults for new
                    case "autorefresh":
                        defaultAutoRefresh = reader.ReadElementContentAsBoolean();
                        break;

                    case "allowcopy":
                        defaultAllowCopy = reader.ReadElementContentAsBoolean();
                        break;

                    case "copyoncode":
                        defaultCopyOnCode = reader.ReadElementContentAsBoolean();
                        break;

                    case "hideserial":
                        defaultHideSerial = reader.ReadElementContentAsBoolean();
                        break;

                    case "skin":
                        defaultSkin = reader.ReadElementContentAsString();
                        break;

                    case "WinAuthAuthenticator":
                        var wa = new WinAuthAuthenticator();
                        changed = wa.ReadXml(reader, password) || changed;
                        this.Add(wa);
                        if (this.CurrentAuthenticator == null)
                        {
                            this.CurrentAuthenticator = wa;
                        }
                        break;

                    // for old 2.x configs
                    case "authenticator":
                        var waold = new WinAuthAuthenticator();
                        waold.AuthenticatorData = Authenticator.ReadXmlv2(reader, password);
                        if (waold.AuthenticatorData is BattleNetAuthenticator)
                        {
                            waold.Name = "Battle.net";
                        }
                        this.Add(waold);
                        this.CurrentAuthenticator = waold;
                        waold.AutoRefresh         = defaultAutoRefresh;
                        waold.AllowCopy           = defaultAllowCopy;
                        waold.CopyOnCode          = defaultCopyOnCode;
                        waold.HideSerial          = defaultHideSerial;
                        break;

                    // old 2.x auto login script
                    case "autologin":
                        var hks = new HoyKeySequence();
                        hks.ReadXml(reader, password);
                        if (hks.HotKey != 0)
                        {
                            if (this.CurrentAuthenticator.HotKey == null)
                            {
                                this.CurrentAuthenticator.HotKey = new HotKey();
                            }
                            HotKey hotkey = this.CurrentAuthenticator.HotKey;
                            hotkey.Action    = HotKey.HotKeyActions.Inject;
                            hotkey.Key       = hks.HotKey;
                            hotkey.Modifiers = hks.Modifiers;
                            if (hks.WindowTitleRegex == true && string.IsNullOrEmpty(hks.WindowTitle) == false)
                            {
                                hotkey.Window = "/" + Regex.Escape(hks.WindowTitle);
                            }
                            else if (string.IsNullOrEmpty(hks.WindowTitle) == false)
                            {
                                hotkey.Window = hks.WindowTitle;
                            }
                            else if (string.IsNullOrEmpty(hks.ProcessName) == false)
                            {
                                hotkey.Window = hks.ProcessName;
                            }
                            if (hks.Advanced == true)
                            {
                                hotkey.Action   = HotKey.HotKeyActions.Advanced;
                                hotkey.Advanced = hks.AdvancedScript;
                            }
                        }
                        break;

                    default:
                        reader.Skip();
                        break;
                    }
                }
                else
                {
                    reader.Read();
                    break;
                }
            }

            return(changed);
        }