Пример #1
0
        public ProtectedFieldEditor()
        {
            InitializeComponent();

            mSecureEdit = new SecureEdit();
            mSecureEdit.Attach(mTextBox, OnPasswordTextChanged, mToggleHidden.Checked);
        }
Пример #2
0
        private void GlobalWindowManager_WindowAdded(object sender, GwmWindowEventArgs e)
        {
            // get references for password fields on unlocking and creating a database
            // if a yubikey is inserted, the password field is overwriten with a value
            // derived from the password and the yubikey-challenge-response

            if (e.Form is KeyPromptForm)
            {
                _keyPromtForm            = (KeyPromptForm)e.Form;
                _keyPromtForm_okButton   = (Button)_keyPromtForm.Controls.Find("m_btnOK", false)[0];
                _keyPromtForm_usePw      = (CheckBox)_keyPromtForm.Controls.Find("m_cbPassword", false)[0];
                _keyPromtForm_secureEdit = (SecureEdit)typeof(KeyPromptForm)
                                           .GetField("m_secPassword", BindingFlags.NonPublic | BindingFlags.Instance)
                                           .GetValue(_keyPromtForm);

                injectHandler(_keyPromtForm_okButton, keyPromtForm_onConfirmClick);
            }

            else if (e.Form is KeyCreationForm)
            {
                _createKeyForm              = (KeyCreationForm)e.Form;
                _createKeyForm_okButton     = (Button)_createKeyForm.Controls.Find("m_btnCreate", false)[0];
                _createKeyForm_usePw        = (CheckBox)_createKeyForm.Controls.Find("m_cbPassword", false)[0];
                _createKeyForm_pwInputGroup = (PwInputControlGroup)typeof(KeyCreationForm)
                                              .GetField("m_icgPassword", BindingFlags.NonPublic | BindingFlags.Instance)
                                              .GetValue(_createKeyForm);

                _createKeyForm.VisibleChanged += _createKeyForm_VisibleChanged;

                injectHandler(_createKeyForm_okButton, keyCreateForm_onConfirmClick);
            }
        }
Пример #3
0
        private CompositeKey CreateCompositeKey(string masterKey)
        {
            CompositeKey m_pKey = new CompositeKey();

            SecureEdit se = new SecureEdit(masterKey);

            byte[] pb = se.ToUtf8();
            m_pKey.AddUserKey(new KcpPassword(pb));
            Array.Clear(pb, 0, pb.Length);

            return(m_pKey);
        }
 private void init_child_vals()
 {
     foreach (KeyValuePair <EntryTemplate, Control> pair in et_to_control)
     {
         if (pair.Value == null)
         {
             continue;
         }
         EntryTemplate t        = pair.Key;
         String        get_name = t.fieldName;
         if (t.fieldName == "@confirm")
         {
             get_name = PwDefs.PasswordField;
         }
         ProtectedString str = null;
         if (t.fieldName == "@override")
         {
             get_name = "";
             find_override_url_control(form);
             Debug.Assert(override_url_control != null || new_override_url_control != null);
             if (new_override_url_control != null)
             {
                 str = new ProtectedString(false, new_override_url_control.Text);
             }
             else
             {
                 str = new ProtectedString(false, override_url_control.Text);
             }
         }
         if (get_name != "")
         {
             str = form.EntryStrings.Get(get_name);
         }
         if (str == null)
         {
             str = new ProtectedString(t.type.StartsWith("Protected"), "");
         }
         if (t.type == "Inline" || t.type == "Inline URL")
         {
             TextBox box = (TextBox)pair.Value;
             String  val = str.ReadString();
             val      = val.Replace("\r", "");
             val      = val.Replace("\n", "\r\n");
             box.Text = val;
         }
         else if (t.type == "Protected Inline")
         {
             SecureEdit sedit = et_to_secure_edit[t];
             sedit.SetPassword(str.ReadUtf8());
         }
         else if (t.type == "Listbox")
         {
             ComboBox combobox = (ComboBox)pair.Value;
             combobox.SelectedItem = str.ReadString();
         }
         else if (t.type == "Checkbox")
         {
             bool     val;
             CheckBox box = (CheckBox)pair.Value;
             box.Checked = false;
             if (Boolean.TryParse(str.ReadString(), out val))
             {
                 box.Checked = val;
             }
         }
         else if (t.type == "Date" || t.type == "Time" || t.type == "Date Time")
         {
             DateTimePicker picker = (DateTimePicker)pair.Value;
             if (t.fieldName == "@exp_date")
             {
                 find_expires_control(form);
                 Debug.Assert(expires_control != null && expires_cbx_control != null);
                 picker.Value   = expires_control.Value;
                 picker.Checked = expires_cbx_control.Checked;
             }
             else
             {
                 DateTime val;
                 if (DateTime.TryParse(str.ReadString(), out val))
                 {
                     picker.Value = val;
                 }
             }
         }
     }
 }
 private void save_child_vals()
 {
     foreach (KeyValuePair <EntryTemplate, Control> pair in et_to_control)
     {
         if (pair.Value == null)
         {
             continue;
         }
         EntryTemplate   t = pair.Key;
         ProtectedString str;
         if (t.type == "Date" || t.type == "Time" || t.type == "Date Time")
         {
             DateTimePicker picker = (DateTimePicker)pair.Value;
             if (t.fieldName == "@exp_date")
             {
                 find_expires_control(form);
                 Debug.Assert(expires_control != null && expires_cbx_control != null);
                 expires_cbx_control.Checked = picker.Checked;
                 expires_control.Value       = picker.Value;
                 continue;
             }
             str = new ProtectedString(false, picker.Value.ToString());
         }
         else if (t.type == "Checkbox")
         {
             CheckBox checkbox = (CheckBox)pair.Value;
             str = new ProtectedString(false, checkbox.Checked.ToString());
         }
         else if (t.type == "Inline" || t.type == "Inline URL")
         {
             TextBox box = (TextBox)pair.Value;
             str = new ProtectedString(false, box.Text == null ? "" : box.Text.Replace("\r", ""));
         }
         else if (t.type == "Listbox")
         {
             ComboBox combobox = (ComboBox)pair.Value;
             str = new ProtectedString(false, combobox.SelectedItem == null ? "" : combobox.SelectedItem.ToString());
         }
         else if (t.type == "Protected Inline")
         {
             SecureEdit sedit = et_to_secure_edit[t];
             str = new ProtectedString(true, sedit.ToUtf8());
         }
         else
         {
             continue;
         }
         str = str.WithProtection(t.type.StartsWith("Protected"));
         if (t.fieldName == "@confirm")
         {
             //form.m_secRepeat.SetProtectedString(str, form.EntryRef, "Viewing/Editing Entry");
             continue;
         }
         if (t.fieldName == "@override")
         {
             find_override_url_control(form);
             Debug.Assert(new_override_url_control != null || override_url_control != null);
             if (new_override_url_control != null)
             {
                 new_override_url_control.Text = str.ReadString();
             }
             else
             {
                 override_url_control.Text = str.ReadString();
             }
             continue;
         }
         form.EntryStrings.Set(t.fieldName, str);
         //if (t.fieldName == PwDefs.PasswordField && current_password_confirm_field == null)
         //form.m_secRepeat.SetProtectedString(str, form.EntryRef, "Viewing/Editing Entry");
     }
 }
        //private int ScaleX(int i) {
        //	return DpiUtil.ScaleIntX(i);
        //}
        //private int ScaleY(int i) {
        //	return DpiUtil.ScaleIntY(i);
        //}

        private bool InitializeChildView(TabPage page, String uuid)
        {
            if (et_to_label != null)
            {
                add_child_items_to_tab(page);
                return(true);
            }

            init_pwgen_button();
            et_to_label       = new Dictionary <EntryTemplate, Label>();
            et_to_control     = new Dictionary <EntryTemplate, Control>();
            et_to_secure_edit = new Dictionary <EntryTemplate, SecureEdit>();
            et_to_control2    = new Dictionary <EntryTemplate, Control>();
            SecureEdit entry_pass         = null;
            SecureEdit entry_pass_confirm = null;

            int     control_offset_y = 10;
            PwUuid  par_uuid         = new PwUuid(KeePassLib.Utility.MemUtil.HexStringToByteArray(uuid));
            PwEntry par_template     = m_host.Database.RootGroup.FindEntry(par_uuid, true);

            if (par_template == null)
            {
                return(false);
            }
            if (par_template.Strings.Get("_etm_template") == null)
            {
                return(false);
            }
            List <EntryTemplate> cur                 = parse_entry(par_template.Strings);
            const int            LABEL_WIDTH         = 130;
            const int            LEFT_CONTROL_OFFSET = LABEL_WIDTH + 5;
            int CONTROL_WIDTH = TAB_WIDTH - LABEL_WIDTH - 55;

            foreach (EntryTemplate t in cur)
            {
                Label label = new Label();
                label.Text = t.title + ":";
                //label.AutoSize = false;
                label.Top      = control_offset_y;
                label.Left     = 0;
                label.AutoSize = false;
                label.Width    = LABEL_WIDTH;

                label.AutoEllipsis = true;
                label.TextAlign    = ContentAlignment.MiddleRight;
                FontUtil.AssignDefaultBold(label);

                et_to_label[t] = label;
                if (t.type == "Divider")
                {
                    label.Font       = new Font(label.Font.FontFamily, label.Font.Size * 1.1f, FontStyle.Bold | FontStyle.Underline);
                    label.TextAlign  = ContentAlignment.BottomLeft;
                    label.Width      = CONTROL_WIDTH + LABEL_WIDTH;
                    label.Text       = t.title;              //remove :
                    et_to_control[t] = null;
                }
                else if (t.type == "Checkbox")
                {
                    CheckBox checkbox = new CheckBox();
                    checkbox.Top     = control_offset_y;
                    checkbox.Left    = LEFT_CONTROL_OFFSET;
                    et_to_control[t] = checkbox;
                }
                else if (t.type == "Listbox")
                {
                    ComboBox combobox = new ComboBox();
                    combobox.Top     = control_offset_y;
                    combobox.Left    = LEFT_CONTROL_OFFSET;
                    combobox.Width   = CONTROL_WIDTH;
                    et_to_control[t] = combobox;
                    if (!String.IsNullOrEmpty(t.options))
                    {
                        String[] opts = t.options.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (String opt in opts)
                        {
                            combobox.Items.Add(opt.Trim());
                        }
                    }
                }
                else if (t.type == "Date" || t.type == "Time" || t.type == "Date Time")
                {
                    DateTimePicker picker = new DateTimePicker();
                    picker.Top          = control_offset_y;
                    picker.Left         = LEFT_CONTROL_OFFSET;
                    picker.CustomFormat = "";
                    picker.Width        = CONTROL_WIDTH;
                    picker.Format       = DateTimePickerFormat.Custom;
                    if (t.type == "Date" || t.type == "Date Time")
                    {
                        picker.CustomFormat = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
                    }
                    if (t.type == "Date Time")
                    {
                        picker.CustomFormat += " ";
                    }
                    if (t.type == "Time" || t.type == "Date Time")
                    {
                        picker.CustomFormat += System.Globalization.DateTimeFormatInfo.CurrentInfo.LongTimePattern;
                    }
                    if (t.fieldName == "@exp_date")
                    {
                        picker.ShowCheckBox = true;
                    }
                    et_to_control[t] = picker;
                }
                else if (t.type == "Inline" || t.type == "Protected Inline" || t.type == "Inline URL")
                {
                    TextBox box = new TextBox();
                    box.Top   = control_offset_y;
                    box.Left  = LEFT_CONTROL_OFFSET;
                    box.Width = t.type == "Inline URL" ? CONTROL_WIDTH - 30 : CONTROL_WIDTH;
                    int lines = LinesFromOption(t.options);
                    if (t.type == "Inline URL")
                    {
                        lines = 1;
                    }
                    if (lines > 1)
                    {
                        box.Multiline     = true;
                        box.AcceptsReturn = true;
                        box.Height        = 13 * lines + 10;
                        box.ScrollBars    = ScrollBars.Both;
                        control_offset_y += 13 * (lines - 1);
                    }
                    et_to_control[t] = box;
                    if (t.type == "Protected Inline")
                    {
                        SecureEdit sedit = new SecureEdit();
                        sedit.Attach(box, null, true);
                        et_to_secure_edit[t] = sedit;
                        if (t.fieldName != "@confirm")
                        {
                            CheckBox chk = new CheckBox();
                            chk.Appearance = Appearance.Button;
                            chk.Image      = Resources.Resources.B17x05_3BlackDots;
                            chk.Location   = new Point(box.Left + box.Width + 10, control_offset_y);
                            chk.Size       = new Size(32, 23);
                            chk.TextAlign  = ContentAlignment.MiddleCenter;
                            chk.UseVisualStyleBackColor = true;
                            chk.Tag             = t;
                            chk.Checked         = true;
                            chk.CheckedChanged += chk_CheckedChanged;
                            et_to_control2[t]   = chk;
                        }
                        else
                        {
                            et_to_control2[t]                  = m_btnGenPw;
                            et_to_control2[t].Location         = new Point(box.Left + box.Width + 10, control_offset_y);
                            current_password_confirm_field     = sedit;
                            current_password_confirm_field_txt = box;
                            entry_pass_confirm                 = sedit;
                        }
                        if (t.fieldName == PwDefs.PasswordField)
                        {
                            entry_pass = current_password_field = sedit;
                        }
                    }
                    else if (t.type == "Inline URL")
                    {
                        var link = new LinkLabel {
                            Text = "Open"
                        };
                        link.LinkClicked += (sender, args) => WinUtil.OpenUrl(box.Text ?? "", form.EntryRef);
                        link.Location     = new Point(box.Left + box.Width + 10, control_offset_y);
                        link.Width        = 50;
                        et_to_control2[t] = link;
                    }
                }
                else if (t.type == "Popout" || t.type == "Protected Popout")
                {
                    Button btn = new Button();
                    btn.Text = "View/Edit";
                    if (t.type == "Protected Popout")
                    {
                        btn.Text = "View/Edit Secure";
                    }
                    btn.Tag   = t;
                    btn.Width = CONTROL_WIDTH;
                    btn.Left  = LEFT_CONTROL_OFFSET;
                    //btn.Height = 20;
                    btn.Top          = control_offset_y;
                    btn.Click       += btn_popout_Click;
                    et_to_control[t] = btn;
                }

                control_offset_y += 30;
            }
            client_remove_button       = new Button();
            client_remove_button.Text  = "Remove As Template Child";
            client_remove_button.Width = CONTROL_WIDTH;
            client_remove_button.Left  = LEFT_CONTROL_OFFSET;
            //client_remove_button.Height = 20;
            client_remove_button.Top    = control_offset_y;
            client_remove_button.Click += client_remove_button_Click;
            if (entry_pass_confirm != null && entry_pass != null)
            {
            }
            add_child_items_to_tab(page);
            return(true);
        }