1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2024 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using KeePassLib.Security;
using KeePassLib.Utility;
namespace KeePassLib.Cryptography.PasswordGenerator
{
internal static class PatternBasedGenerator
{
internal static PwgError Generate(out ProtectedString psOut,
PwProfile pwProfile, CryptoRandomStream crsRandomSource)
{
psOut = ProtectedString.Empty;
string strPattern = pwProfile.Pattern;
if(string.IsNullOrEmpty(strPattern)) return PwgError.Success;
CharStream cs = new CharStream(strPattern);
LinkedList<char> llGenerated = new LinkedList<char>();
PwCharSet pcs = new PwCharSet();
while(true)
{
char ch = cs.ReadChar();
if(ch == char.MinValue) break;
pcs.Clear();
if(ch == '\\')
{
ch = cs.ReadChar();
if(ch == char.MinValue) return PwgError.InvalidPattern;
pcs.Add(ch); // Allow "{...}" support and char check
}
else if(ch == '[')
{
if(!ReadCustomCharSet(cs, pcs))
return PwgError.InvalidPattern;
}
else
{
if(!pcs.AddCharSet(ch))
return PwgError.InvalidPattern;
}
int nCount = 1;
if(cs.PeekChar() == '{')
{
nCount = ReadCount(cs);
if(nCount < 0) return PwgError.InvalidPattern;
}
for(int i = 0; i < nCount; ++i)
{
if(!PwGenerator.PrepareCharSet(pcs, pwProfile))
return PwgError.InvalidCharSet;
if(pwProfile.NoRepeatingCharacters)
{
foreach(char chUsed in llGenerated)
pcs.Remove(chUsed);
}
char chGen = PwGenerator.GenerateCharacter(pcs,
crsRandomSource);
if(chGen == char.MinValue) return PwgError.TooFewCharacters;
llGenerated.AddLast(chGen);
}
}
if(llGenerated.Count == 0) return PwgError.Success;
char[] v = new char[llGenerated.Count];
llGenerated.CopyTo(v, 0);
if(pwProfile.PatternPermutePassword)
PwGenerator.Shuffle(v, crsRandomSource);
byte[] pbUtf8 = StrUtil.Utf8.GetBytes(v);
psOut = new ProtectedString(true, pbUtf8);
MemUtil.ZeroByteArray(pbUtf8);
MemUtil.ZeroArray<char>(v);
return PwgError.Success;
}
private static bool ReadCustomCharSet(CharStream cs, PwCharSet pcsOut)
{
Debug.Assert(cs.PeekChar() != '['); // Consumed already
Debug.Assert(pcsOut.Size == 0);
bool bAdd = true;
while(true)
{
char ch = cs.ReadChar();
if(ch == char.MinValue) return false;
if(ch == ']') break;
if(ch == '\\')
{
ch = cs.ReadChar();
if(ch == char.MinValue) return false;
if(bAdd) pcsOut.Add(ch);
else pcsOut.Remove(ch);
}
else if(ch == '^')
{
if(bAdd) bAdd = false;
else return false; // '^' toggles the mode only once
}
else
{
PwCharSet pcs = new PwCharSet();
if(!pcs.AddCharSet(ch)) return false;
if(bAdd) pcsOut.Add(pcs.ToString());
else pcsOut.Remove(pcs.ToString());
}
}
return true;
}
private static int ReadCount(CharStream cs)
{
if(cs.ReadChar() != '{') { Debug.Assert(false); return -1; }
// Ensure not empty
char chFirst = cs.PeekChar();
if((chFirst < '0') || (chFirst > '9')) return -1;
long n = 0;
while(true)
{
char ch = cs.ReadChar();
if(ch == '}') break;
if((ch >= '0') && (ch <= '9'))
{
n = (n * 10L) + (long)(ch - '0');
if(n > int.MaxValue) return -1;
}
else return -1;
}
return (int)n;
}
}
}
|