Exemple #1
0
        private static string ReplacePath(string str, string strPlaceholder,
                                          string strFill, SprContentFlags cf)
        {
            if (str == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (strPlaceholder == null)
            {
                Debug.Assert(false); return(str);
            }
            if (strPlaceholder.Length == 0)
            {
                Debug.Assert(false); return(str);
            }
            if (strFill == null)
            {
                return(str);                            // No assert
            }
            string strRep;

            if ((cf != null) && cf.EncodeQuotesForCommandLine)
            {
                strRep = "\"" + SprEngine.TransformContent(strFill, cf) + "\"";
            }
            else
            {
                strRep = SprEngine.TransformContent("\"" + strFill + "\"", cf);
            }

            return(StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, strRep));
        }
Exemple #2
0
        private static string ReplaceHmacOtpPlaceholder(string strText,
                                                        PwEntry pe, PwDatabase pd, SprContentFlags cf)
        {
            if ((pe == null) || (pd == null))
            {
                return(strText);
            }

            string str = strText;

            const string strHmacOtpPlh = @"{HMACOTP}";

            if (str.IndexOf(strHmacOtpPlh, StrUtil.CaseIgnoreCmp) >= 0)
            {
                const string strKeyField     = "HmacOtp-Secret";
                const string strCounterField = "HmacOtp-Counter";

                byte[] pbSecret = Encoding.UTF8.GetBytes(pe.Strings.ReadSafe(
                                                             strKeyField));

                string strCounter = pe.Strings.ReadSafe(strCounterField);
                ulong  uCounter;
                ulong.TryParse(strCounter, out uCounter);

                string strValue = HmacOtp.Generate(pbSecret, uCounter, 6, false, -1);

                pe.Strings.Set(strCounterField, new ProtectedString(false,
                                                                    (uCounter + 1).ToString()));
                pd.Modified = true;

                str = StrUtil.ReplaceCaseInsensitive(str, strHmacOtpPlh, strValue);
            }

            return(str);
        }
        public static string FillPlaceholders(string strText, SprContentFlags cf)
        {
            string str = strText;

            str = AppLocator.ReplacePath(str, @"{INTERNETEXPLORER}", AppLocator.InternetExplorerPath, cf);
            str = AppLocator.ReplacePath(str, @"{FIREFOX}", AppLocator.FirefoxPath, cf);
            str = AppLocator.ReplacePath(str, @"{OPERA}", AppLocator.OperaPath, cf);

            return(str);
        }
Exemple #4
0
        public static string FillPlaceholders(string strText, PwEntry pe,
                                              PwDatabase pd, SprContentFlags cf)
        {
            if (pe == null)
            {
                return(strText);
            }

            string str = strText;

            str = ReplacePickPw(str, pe, pd, cf);

            return(str);
        }
Exemple #5
0
        public static string FillPlaceholdersFinal(string strText, PwEntry pe,
                                                   PwDatabase pd, SprContentFlags cf)
        {
            if (pe == null)
            {
                return(strText);
            }

            string str = strText;

            str = ReplaceNewPasswordPlaceholder(str, pe, pd, cf);
            str = ReplaceHmacOtpPlaceholder(str, pe, pd, cf);

            return(str);
        }
Exemple #6
0
        /* private static string ReplacePickPw(string strText, PwEntry pe,
         *      SprContentFlags cf)
         * {
         *      string str = strText;
         *
         *      for(int iID = 1; iID < (int.MaxValue - 1); ++iID)
         *      {
         *              string strPlaceholder = @"{PICKPASSWORDCHARS";
         *              if(iID > 1) strPlaceholder += iID.ToString();
         *              strPlaceholder += @"}";
         *
         *              if(str.IndexOf(strPlaceholder, StrUtil.CaseIgnoreCmp) >= 0)
         *              {
         *                      ProtectedString ps = pe.Strings.Get(PwDefs.PasswordField);
         *                      if(ps != null)
         *                      {
         *                              byte[] pb = ps.ReadUtf8();
         *                              bool bNotEmpty = (pb.Length > 0);
         *                              Array.Clear(pb, 0, pb.Length);
         *
         *                              if(bNotEmpty)
         *                              {
         *                                      CharPickerForm cpf = new CharPickerForm();
         *                                      cpf.InitEx(ps, true, true, 0);
         *
         *                                      if(cpf.ShowDialog() == DialogResult.OK)
         *                                              str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder,
         *                                                      SprEngine.TransformContent(cpf.SelectedCharacters.ReadString(), cf));
         *                              }
         *                      }
         *
         *                      str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, string.Empty);
         *              }
         *              else break;
         *      }
         *
         *      return str;
         * } */

        private static string ReplacePickPw(string strText, PwEntry pe,
                                            PwDatabase pd, SprContentFlags cf)
        {
            string str = strText;

            while (true)
            {
                const string strStart = @"{PICKPASSWORDCHARS";

                int iStart = str.IndexOf(strStart, StrUtil.CaseIgnoreCmp);
                if (iStart < 0)
                {
                    break;
                }

                int iEnd = str.IndexOf('}', iStart);
                if (iEnd < 0)
                {
                    break;
                }

                string strPlaceholder = str.Substring(iStart, iEnd - iStart + 1);

                string strParam = str.Substring(iStart + strStart.Length,
                                                iEnd - (iStart + strStart.Length));
                string[] vParams = strParam.Split(new char[] { ':' });

                uint uCharCount = 0;
                if (vParams.Length >= 2)
                {
                    uint.TryParse(vParams[1], out uCharCount);
                }

                str = ReplacePickPlaceholder(str, strPlaceholder, pe, pd, cf, uCharCount);
            }

            return(str);
        }
Exemple #7
0
        public static string FillPlaceholders(string strText, PwEntry pe,
                                              SprContentFlags cf)
        {
            if (pe == null)
            {
                return(strText);
            }

            string str = strText;

            if (str.ToUpper().IndexOf(@"{PICKPASSWORDCHARS}") >= 0)
            {
                ProtectedString ps = pe.Strings.Get(PwDefs.PasswordField);
                if (ps != null)
                {
                    byte[] pb        = ps.ReadUtf8();
                    bool   bNotEmpty = (pb.Length > 0);
                    Array.Clear(pb, 0, pb.Length);

                    if (bNotEmpty)
                    {
                        CharPickerForm cpf = new CharPickerForm();
                        cpf.InitEx(ps, true, true);

                        if (cpf.ShowDialog() == DialogResult.OK)
                        {
                            str = StrUtil.ReplaceCaseInsensitive(str, @"{PICKPASSWORDCHARS}",
                                                                 SprEngine.TransformContent(cpf.SelectedCharacters.ReadString(), cf));
                        }
                    }
                }

                str = StrUtil.ReplaceCaseInsensitive(str, @"{PICKPASSWORDCHARS}", string.Empty);
            }

            return(str);
        }
Exemple #8
0
        private static string ReplaceNewPasswordPlaceholder(string strText,
                                                            PwEntry pe, PwDatabase pd, SprContentFlags cf)
        {
            if ((pe == null) || (pd == null))
            {
                return(strText);
            }

            string str = strText;

            const string strNewPwPlh = @"{NEWPASSWORD}";

            if (str.IndexOf(strNewPwPlh, StrUtil.CaseIgnoreCmp) >= 0)
            {
                ProtectedString psAutoGen = new ProtectedString(
                    pd.MemoryProtection.ProtectPassword);
                PwgError e = PwGenerator.Generate(psAutoGen,
                                                  Program.Config.PasswordGenerator.AutoGeneratedPasswordsProfile,
                                                  null, Program.PwGeneratorPool);

                if (e == PwgError.Success)
                {
                    pe.CreateBackup();
                    pe.Strings.Set(PwDefs.PasswordField, psAutoGen);
                    pd.Modified = true;

                    string strIns = SprEngine.TransformContent(psAutoGen.ReadString(), cf);
                    str = StrUtil.ReplaceCaseInsensitive(str, strNewPwPlh, strIns);
                }
            }

            return(str);
        }
Exemple #9
0
        private static string ReplacePickPlaceholder(string str,
                                                     string strPlaceholder, PwEntry pe, PwDatabase pd, SprContentFlags cf,
                                                     uint uCharCount)
        {
            if (str.IndexOf(strPlaceholder, StrUtil.CaseIgnoreCmp) < 0)
            {
                return(str);
            }

            ProtectedString ps = pe.Strings.Get(PwDefs.PasswordField);

            if (ps != null)
            {
                string strPassword = ps.ReadString();
                string strPick     = SprEngine.Compile(strPassword, false, pe, pd,
                                                       false, false); // Do not transform content yet

                if (!string.IsNullOrEmpty(strPick))
                {
                    ProtectedString psPick = new ProtectedString(false, strPick);
                    CharPickerForm  dlg    = new CharPickerForm();
                    dlg.InitEx(psPick, true, true, uCharCount);

                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder,
                                                             SprEngine.TransformContent(dlg.SelectedCharacters.ReadString(), cf));
                    }
                }
            }

            return(StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, string.Empty));
        }