Пример #1
0
        public static PwgError Generate(out ProtectedString psOut,
                                        PwProfile pwProfile, byte[] pbUserEntropy,
                                        CustomPwGeneratorPool pwAlgorithmPool)
        {
            Debug.Assert(pwProfile != null);
            if (pwProfile == null)
            {
                throw new ArgumentNullException("pwProfile");
            }

            CryptoRandomStream crs = CreateCryptoStream(pbUserEntropy);
            PwgError           e   = PwgError.Unknown;

            if (pwProfile.GeneratorType == PasswordGeneratorType.CharSet)
            {
                e = CharSetBasedGenerator.Generate(out psOut, pwProfile, crs);
            }
            else if (pwProfile.GeneratorType == PasswordGeneratorType.Pattern)
            {
                e = PatternBasedGenerator.Generate(out psOut, pwProfile, crs);
            }
            else if (pwProfile.GeneratorType == PasswordGeneratorType.Custom)
            {
                e = GenerateCustom(out psOut, pwProfile, crs, pwAlgorithmPool);
            }
            else
            {
                Debug.Assert(false);
                psOut = ProtectedString.Empty;
            }

            return(e);
        }
Пример #2
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);
        }
Пример #3
0
        public static PwgError Generate(ProtectedString psOutBuffer,
                                        PwProfile pwProfile, byte[] pbUserEntropy)
        {
            Debug.Assert(psOutBuffer != null);
            if (psOutBuffer == null)
            {
                throw new ArgumentNullException("psOutBuffer");
            }
            Debug.Assert(pwProfile != null);
            if (pwProfile == null)
            {
                throw new ArgumentNullException("pwProfile");
            }

            psOutBuffer.Clear();

            CryptoRandomStream crs = CreateCryptoStream(pbUserEntropy);
            PwgError           e   = PwgError.Unknown;

            if (pwProfile.GeneratorType == PasswordGeneratorType.CharSet)
            {
                e = CharSetBasedGenerator.Generate(psOutBuffer, pwProfile, crs);
            }
            else if (pwProfile.GeneratorType == PasswordGeneratorType.Pattern)
            {
                e = PatternBasedGenerator.Generate(psOutBuffer, pwProfile, crs);
            }
            else
            {
                Debug.Assert(false);
            }

            return(e);
        }
Пример #4
0
        public static PwgError Generate(out ProtectedString psOut,
                                        PwProfile pwProfile, byte[] pbUserEntropy,
                                        CustomPwGeneratorPool pwAlgorithmPool)
        {
            Debug.Assert(pwProfile != null);
            if (pwProfile == null)
            {
                throw new ArgumentNullException("pwProfile");
            }

            PwgError           e   = PwgError.Unknown;
            CryptoRandomStream crs = null;

            byte[] pbKey = null;
            try
            {
                crs = CreateRandomStream(pbUserEntropy, out pbKey);

                if (pwProfile.GeneratorType == PasswordGeneratorType.CharSet)
                {
                    e = CharSetBasedGenerator.Generate(out psOut, pwProfile, crs);
                }
                else if (pwProfile.GeneratorType == PasswordGeneratorType.Pattern)
                {
                    e = PatternBasedGenerator.Generate(out psOut, pwProfile, crs);
                }
                else if (pwProfile.GeneratorType == PasswordGeneratorType.Custom)
                {
                    e = GenerateCustom(out psOut, pwProfile, crs, pwAlgorithmPool);
                }
                else
                {
                    Debug.Assert(false); psOut = ProtectedString.Empty;
                }
            }
            finally
            {
                if (crs != null)
                {
                    crs.Dispose();
                }
                if (pbKey != null)
                {
                    MemUtil.ZeroByteArray(pbKey);
                }
            }

            return(e);
        }
Пример #5
0
        internal static string ErrorToString(PwgError e, bool bHeader)
        {
            if (e == PwgError.Success)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if ((e == PwgError.Unknown) && bHeader)
            {
                return(KLRes.PwGenFailed);
            }

            string str = KLRes.UnknownError;

            switch (e)
            {
            // case PwgError.Success:
            //	break;

            case PwgError.Unknown:
                break;

            case PwgError.TooFewCharacters:
                str = KLRes.CharSetTooFewChars;
                break;

            case PwgError.UnknownAlgorithm:
                str = KLRes.AlgorithmUnknown;
                break;

            case PwgError.InvalidCharSet:
                str = KLRes.CharSetInvalid;
                break;

            case PwgError.InvalidPattern:
                str = KLRes.PatternInvalid;
                break;

            default:
                Debug.Assert(false);
                break;
            }

            if (bHeader)
            {
                str = KLRes.PwGenFailed + MessageService.NewParagraph + str;
            }

            return(str);
        }
Пример #6
0
        private ProtectedString CreatePassword()
        {
            ProtectedString pw = new ProtectedString();
            PwProfile       pf = new PwProfile();

            pf.GeneratorType = PasswordGeneratorType.Pattern;
            //use 256bit Hex-Key-Profile
            pf.Pattern = "h{64}";
            CustomPwGeneratorPool pwGenPool = new CustomPwGeneratorPool();
            PwgError perr = PwGenerator.Generate(out pw, pf, null, pwGenPool);

            if (perr != PwgError.Success)
            {
                throw new Exception("Error while creating new password!");
            }
            return(pw);
        }
Пример #7
0
        private static string GeneratePassword(string strProfile, SprContext ctx)
        {
            PwProfile prf = Program.Config.PasswordGenerator.AutoGeneratedPasswordsProfile;

            if (!string.IsNullOrEmpty(strProfile))
            {
                if (strProfile == @"~")
                {
                    prf = PwProfile.DeriveFromPassword(ctx.Entry.Strings.GetSafe(
                                                           PwDefs.PasswordField));
                }
                else
                {
                    List <PwProfile> lPrf = PwGeneratorUtil.GetAllProfiles(false);
                    foreach (PwProfile p in lPrf)
                    {
                        if (strProfile.Equals(p.Name, StrUtil.CaseIgnoreCmp))
                        {
                            prf = p;
                            break;
                        }
                    }
                }
            }

            ProtectedString ps;
            PwgError        e = PwGenerator.Generate(out ps, prf, null,
                                                     Program.PwGeneratorPool);

            if ((e != PwgError.Success) || (ps == null))
            {
                return(string.Empty);
            }

            string strGen = ps.ReadString();

            strGen = SprEngine.TransformContent(strGen, ctx);
            return(strGen);
        }
Пример #8
0
        internal static ProtectedString GenerateAcceptable(PwProfile prf,
                                                           byte[] pbUserEntropy, PwEntry peOptCtx, PwDatabase pdOptCtx,
                                                           bool bShowErrorUI, ref bool bAcceptAlways, out string strError)
        {
            strError = null;

            ProtectedString ps  = ProtectedString.Empty;
            SprContext      ctx = new SprContext(peOptCtx, pdOptCtx,
                                                 SprCompileFlags.NonActive, false, false);

            while (true)
            {
                try
                {
                    PwgError e = PwGenerator.Generate(out ps, prf, pbUserEntropy,
                                                      Program.PwGeneratorPool);

                    if (e != PwgError.Success)
                    {
                        strError = PwGenerator.ErrorToString(e, true);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    strError = PwGenerator.ErrorToString(ex, true);
                    break;
                }
                finally
                {
                    if (ps == null)
                    {
                        Debug.Assert(false); ps = ProtectedString.Empty;
                    }
                }

                if (bAcceptAlways)
                {
                    break;
                }

                string str    = ps.ReadString();
                string strCmp = SprEngine.Compile(str, ctx);

                if (str != strCmp)
                {
                    if (prf.GeneratorType == PasswordGeneratorType.CharSet)
                    {
                        continue;                         // Silently try again
                    }
                    string strText = str + MessageService.NewParagraph +
                                     KPRes.GenPwSprVariant + MessageService.NewParagraph +
                                     KPRes.GenPwAccept;

                    if (!MessageService.AskYesNo(strText, null, false))
                    {
                        continue;
                    }
                    bAcceptAlways = true;
                }

                break;
            }

            if (!string.IsNullOrEmpty(strError))
            {
                ps = ProtectedString.Empty;
                if (bShowErrorUI)
                {
                    MessageService.ShowWarning(strError);
                }
            }

            return(ps);
        }