From 26e8e5c223a3da3f990c7327b1e18b9d21b8f2d1 Mon Sep 17 00:00:00 2001 From: Geoffroy BONNEVILLE Date: Fri, 26 Jul 2019 18:28:53 +0200 Subject: [PATCH] Update to version 2.42.1 Some changes Removed FutureAccesList code as it works only with UWP --- ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs | 31 ++- .../Assets/LockScreenLogo.scale-200.png | Bin 0 -> 1430 bytes .../Assets/SplashScreen.scale-200.png | Bin 0 -> 7700 bytes .../Assets/Square150x150Logo.scale-200.png | Bin 0 -> 2937 bytes .../Assets/Square44x44Logo.scale-200.png | Bin 0 -> 1647 bytes ...x44Logo.targetsize-24_altform-unplated.png | Bin 0 -> 1255 bytes ModernKeePassLib.UwpTest/Assets/StoreLogo.png | Bin 0 -> 1451 bytes .../Assets/Wide310x150Logo.scale-200.png | Bin 0 -> 3204 bytes ModernKeePassLib.UwpTest/KcpKeyFileTests.cs | 86 ++++++++ .../ModernKeePassLib.UwpTest.csproj | 179 ++++++++++++++++ ModernKeePassLib.UwpTest/Package.appxmanifest | 46 +++++ .../Properties/AssemblyInfo.cs | 18 ++ .../Properties/Default.rd.xml | 29 +++ ModernKeePassLib.UwpTest/UnitTestApp.xaml | 7 + ModernKeePassLib.UwpTest/UnitTestApp.xaml.cs | 102 +++++++++ ModernKeePassLib.sln | 40 ++++ .../Cryptography/CryptoRandomStream.cs | 19 ++ .../Cryptography/CryptoStreamEx.cs | 58 +++--- .../Cryptography/KeyDerivation/Argon2Kdf.cs | 4 +- .../CharSetBasedGenerator.cs | 8 +- .../PatternBasedGenerator.cs | 166 +++++++-------- .../PasswordGenerator/PwCharSet.cs | 1 - .../PasswordGenerator/PwGenerator.cs | 40 ++-- ModernKeePassLib/Interfaces/IStatusLogger.cs | 2 +- ModernKeePassLib/Interfaces/ITimeLogger.cs | 2 +- ModernKeePassLib/Keys/CompositeKey.cs | 120 ++++++++--- ModernKeePassLib/Keys/KcpKeyFile.cs | 51 +++-- ModernKeePassLib/Keys/KcpPassword.cs | 2 +- ModernKeePassLib/Keys/KeyProvider.cs | 2 +- ModernKeePassLib/ModernKeePassLib.csproj | 4 +- ModernKeePassLib/Native/Native.PCL.cs | 6 + ModernKeePassLib/Native/NativeLib.cs | 81 +++++++- ModernKeePassLib/Native/NativeMethods.cs | 31 +++ ModernKeePassLib/PwDatabase.cs | 35 ++-- ModernKeePassLib/PwDefs.cs | 12 +- ModernKeePassLib/PwEnums.cs | 2 +- ModernKeePassLib/PwGroup.cs | 59 +++++- ModernKeePassLib/Resources/KLRes.Generated.cs | 17 +- ModernKeePassLib/Resources/KSRes.Generated.cs | 2 +- .../Serialization/FileTransactionEx.cs | 126 ++++++++--- .../Serialization/IOConnection.cs | 14 +- .../Serialization/IOConnectionInfo.cs | 28 +-- .../Serialization/KdbxFile.Read.Streamed.cs | 75 +++---- .../Serialization/KdbxFile.Read.cs | 49 ++--- .../Serialization/KdbxFile.Write.cs | 2 +- ModernKeePassLib/Serialization/KdbxFile.cs | 8 +- ModernKeePassLib/Translation/KPTranslation.cs | 1 + ModernKeePassLib/Utility/MemUtil.cs | 5 +- ModernKeePassLib/Utility/MonoWorkarounds.cs | 3 - ModernKeePassLib/Utility/StrUtil.cs | 109 +++++----- ModernKeePassLib/Utility/UrlUtil.cs | 195 +++++++++++++----- ModernKeePassLib/Utility/XmlUtilEx.cs | 2 +- 52 files changed, 1373 insertions(+), 506 deletions(-) create mode 100644 ModernKeePassLib.UwpTest/Assets/LockScreenLogo.scale-200.png create mode 100644 ModernKeePassLib.UwpTest/Assets/SplashScreen.scale-200.png create mode 100644 ModernKeePassLib.UwpTest/Assets/Square150x150Logo.scale-200.png create mode 100644 ModernKeePassLib.UwpTest/Assets/Square44x44Logo.scale-200.png create mode 100644 ModernKeePassLib.UwpTest/Assets/Square44x44Logo.targetsize-24_altform-unplated.png create mode 100644 ModernKeePassLib.UwpTest/Assets/StoreLogo.png create mode 100644 ModernKeePassLib.UwpTest/Assets/Wide310x150Logo.scale-200.png create mode 100644 ModernKeePassLib.UwpTest/KcpKeyFileTests.cs create mode 100644 ModernKeePassLib.UwpTest/ModernKeePassLib.UwpTest.csproj create mode 100644 ModernKeePassLib.UwpTest/Package.appxmanifest create mode 100644 ModernKeePassLib.UwpTest/Properties/AssemblyInfo.cs create mode 100644 ModernKeePassLib.UwpTest/Properties/Default.rd.xml create mode 100644 ModernKeePassLib.UwpTest/UnitTestApp.xaml create mode 100644 ModernKeePassLib.UwpTest/UnitTestApp.xaml.cs diff --git a/ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs b/ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs index e51fad2..647edc3 100644 --- a/ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs +++ b/ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading.Tasks; using ModernKeePassLib.Keys; using ModernKeePassLib.Utility; using Windows.Storage; @@ -24,7 +25,7 @@ namespace ModernKeePassLib.Test.Keys private const string ExpectedFileEnd = "\r\n\t\r\n"; [Fact] - public void TestConstruct() + public async Task TestConstruct() { var expectedKeyData = new byte[] { @@ -34,16 +35,14 @@ namespace ModernKeePassLib.Test.Keys 0x45, 0xfc, 0xc8, 0x92, 0xbd, 0xeb, 0xaf, 0xc3 }; - var folder = StorageFolder.GetFolderFromPathAsync(Path.GetTempPath()).GetAwaiter().GetResult(); - var file = folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting).GetAwaiter().GetResult(); - using (var fs = file.OpenStreamForWriteAsync().GetAwaiter().GetResult()) + var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetTempPath()); + var file = await folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting); + await using (var fs = await file.OpenStreamForWriteAsync()) { - using (var sw = new StreamWriter(fs)) - { - sw.Write(ExpectedFileStart); - sw.Write(TestKey); - sw.Write(ExpectedFileEnd); - } + await using var sw = new StreamWriter(fs); + sw.Write(ExpectedFileStart); + sw.Write(TestKey); + sw.Write(ExpectedFileEnd); } try @@ -54,19 +53,19 @@ namespace ModernKeePassLib.Test.Keys } finally { - file.DeleteAsync().GetAwaiter().GetResult(); + await file.DeleteAsync(); } } [Fact] - public void TestCreate() + public async Task TestCreate() { - var folder = StorageFolder.GetFolderFromPathAsync(Path.GetTempPath()).GetAwaiter().GetResult(); - var file = folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting).GetAwaiter().GetResult(); + var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetTempPath()); + var file = await folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting); KcpKeyFile.Create(file, null); try { - var fileContents = FileIO.ReadTextAsync(file).GetAwaiter().GetResult(); + var fileContents = await FileIO.ReadTextAsync(file); Assert.Equal(185, fileContents.Length); Assert.StartsWith(ExpectedFileStart, fileContents); @@ -74,7 +73,7 @@ namespace ModernKeePassLib.Test.Keys } finally { - file.DeleteAsync().GetAwaiter().GetResult(); + await file.DeleteAsync(); } } } diff --git a/ModernKeePassLib.UwpTest/Assets/LockScreenLogo.scale-200.png b/ModernKeePassLib.UwpTest/Assets/LockScreenLogo.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..735f57adb5dfc01886d137b4e493d7e97cf13af3 GIT binary patch literal 1430 zcmaJ>TTC2P7~aKltDttVHYH6u8Io4i*}3fO&d$gd*bA_<3j~&e7%8(eXJLfhS!M@! zKrliY>>6yT4+Kr95$!DoD(Qn-5TP|{V_KS`k~E6(LGS@#`v$hQo&^^BKsw3HIsZBT z_y6C2n`lK@apunKojRQ^(_P}Mgewt$(^BBKCTZ;*xa?J3wQ7~@S0lUvbcLeq1Bg4o zH-bvQi|wt~L7q$~a-gDFP!{&TQfc3fX*6=uHv* zT&1&U(-)L%Xp^djI2?~eBF2cxC@YOP$+9d?P&h?lPy-9M2UT9fg5jKm1t$m#iWE{M zIf%q9@;fyT?0UP>tcw-bLkz;s2LlKl2qeP0w zECS7Ate+Awk|KQ+DOk;fl}Xsy4o^CY=pwq%QAAKKl628_yNPsK>?A>%D8fQG6IgdJ ztnxttBz#NI_a@fk7SU`WtrpsfZsNs9^0(2a z@C3#YO3>k~w7?2hipBf{#b6`}Xw1hlG$yi?;1dDs7k~xDAw@jiI*+tc;t2Lflg&bM)0!Y;0_@=w%`LW^8DsYpS#-bLOklX9r?Ei}TScw|4DbpW%+7 zFgAI)f51s}{y-eWb|vrU-Ya!GuYKP)J7z#*V_k^Xo>4!1Yqj*m)x&0L^tg3GJbVAJ zJ-Pl$R=NAabouV=^z_t;^K*0AvFs!vYU>_<|I^#c?>>CR<(T?=%{;U=aI*SbZADLH z&(f2wz_Y0??Tf|g;?|1Znw6}6U43Q#qNRwv1vp9uFn1)V#*4p&%$mP9x&15^OaBiDS(XppT|z^>;B{PLVEbS3IFYV yGvCsSX*m literal 0 HcmV?d00001 diff --git a/ModernKeePassLib.UwpTest/Assets/SplashScreen.scale-200.png b/ModernKeePassLib.UwpTest/Assets/SplashScreen.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..023e7f1feda78d5100569825acedfd213a0d84e9 GIT binary patch literal 7700 zcmeHLYj~4Yw%(;oxoEH#Kxq-eR|+VkP17b#Vk;?4QwkI+A{L04G+#<<(x#Un1#+h5>eArRq zTw$)ZvTWW_Y?bDho0nPVTh08+s`sp!j74rJTTtXIDww0SILedFv?sZ?yb@@}GN;#8 znk_b~Q(A0YR#uV4ef!osoV1M3;vQ8N$O|fStfgf$S5;ddUNv`tWtGjM;koG#N;7M< zP*84lnx(bn_KF&9Z5Ai$)#Cs3a|$OFw>WKCT$of*L7_CqQEinflT|W{JT+aKp-E0v zsxmYg)1(T>DROm+LN1eQw8}KCTp=C!$H7`PU!t9_Hw@TsTI2`udRZv*!a5`#A9hK6Y95L(CDUX&_@QxKV z_feX{UhA#ZWlvgpL$#w^D#lq`_A4AzDqd|Zv6y9PX&DNcN|l}_D^{q@GG&H^Pg583 z8FI6N8^H7b5WjGp;urW)d7F+_lcp%KsLX0viCmE(OHH+=%ZfD_=`voUuoUxFO^L;- z;!;2{g-YiiO6m4bs89OuF9!p{FGtH-f%8<2gY!h9s)4ciN%{Kh1+`}{^}M~+TDH9N z^Z5PlgVXMC&2&k*Hw^Lb9gny#ro$MOIxIt{+r)EA10$VR3 zanN8D{TUkl+v0CQ_>ZoHP<M-x#8@8ZiT#$Kh`(uRaX1g$Bg|qy$<#7 zSSAi{Nb8Y=lvNVeio+UGLCAtoLBfL`iOv`)yoJMDJBN>4IH@(l7YRF;61@>qq1iM9 zr@b#OC~SAxSle?5Pp8Z78{VO0YFr1x7kZU64Z23eLf2T2#6J_t;-E}DkB?NufZ0Ug zi?J&byXeaB-uTNVhuiM!UVQw}bZrJ3GtAETYp->!{q#zfN7D3AS9@Q7*V^85jGx#R z(QxYV(wW#F0XF9^^s>>H8pPlVJ>)3Oz z&_X8Sf@~?cH_O*cgi$U#`v`RRfv#y3m(ZpKk^5uLup+lVs$~}FZU$r_+}#hl%?g5m z-u-}-666ssp-xWQak~>PPy$mRc|~?pVSs1_@mBEXpPVfLF6(Ktf1S* zPPh@QZ=tFMs?LM2(5P3L2;l_6XX6s&cYsP1ip#eg0`ZEP0HGYh{UmS@o`MihLLvkU zgyAG0G`b1|qjxxh1(ODKFE%AP}Dq=3vK$P7TXP4GrM1kQ72!GUVMDl`rDC&2;TA}*nF z8$nQD&6ys_nc1*E7$*1S@R8$ymy(sQV}imGSedB@{!QR5P&N_H=-^o!?LsWs+2|mH z-e=)T^SvI)=_JIm7}j4;@*Z17=(#}m=~YF~z~CLI+vdAGlJDcdF$TM?CVI1%LhUrN zaa6DJ=Yh$)$k&Oz{-~8yw^GM^8prYxSxo zvI4k#ibryMa%%*8oI-5m61Koa_A_xg=(fwp0aBX{;X4Q;NXUhtaoJDo1>TqhWtn=_ zd5~chq#&6~c%8JZK#t_&J(9EVUU&upYeIovLt1>vaHe}UUq>#RGQj!EN#5+0@T`(@ z^g~>*c`VGRiSt;!$_4+0hk^I!@O3``5=sZ8IwlxWW7km1B&_t&E*u0_9UBa#VqwY* zz>nxv?FAsVnRaD(Bui=6i==BFUw0k4n$>`umU`F2l?7CYTD^)c2X+d9X&ddS9|gj? zM?knGkGCX&W8offw8aLC2$D{PjC3nVZwd4k?eZH8*mZ)U@3Qk8RDFOz_#WUA#vnzy zyP>KrCfKwSXea7}jgJjBc}PGY+4#6%lbZyjhy`5sZd_Vy6Wz;ixa?czkN}J9It1K6 zY!eu>|AwF^fwZlLAYyQI*lM@^>O>Iu6Vf6i>Q$?v!SeUS<{>UYMwz$*%Aq?w^`j{h z!$GZbhu=^D{&ET8;))LL%ZBDZkQqRd2;u~!d9bHGmLRhLDctNgYyjsuvoSZ#iVdoB z2!f--UUA#U;<{je#?cYt^{PIyKa%hW>}uepWMyAI{{Zo7?2>?$c9;whJae%oN|I-kpTQSx_C$Z&;f zi2i)qmEn=y4U0uvk)$m;zKfjPK@oc?I`}1Jzl$Q~aoKBd3kt7L#7gyt|A_qgz6ai< z=X%D1i!d2h?rHR^R8SUj&G||dkC?DT>{o#Yau<@uqVT{Xef&XG}5*E4aPk{}~ zplx&XhaV)&1EfI3Em;Bw#O5SV^c;{twb-1Rw)+=0!e_BLbd7tYmXCH0wrlOSS+~`7He8Iqx0{CN+DVit9;*6L~JAN zD&cyT)2?h}xnYmL?^)<7YyzZ3$FHU^Eg;DLqAV{#wv#Wj7S`Jdl1pX&{3(uZ?!uh} zDc$ZTNV*7le_W6}Hju~GMTxZQ1aWCeUc%!jv3MHAzt>Y-nQK%zfT*3ebDQA5b?iGn; zBjv3B+GhLTexd_(CzZDP4|#n5^~scvB6#Pk%Ho!kQ>yYw((Dv{6=$g3jT1!u6gORW zx5#`7Wy-ZHRa~IxGHdrp(bm%lf>2%J660nj$fCqN(epv@y!l9s7@k6EvxS{AMP>WY zX4$@F8^kayphIx-RGO$+LYl9YdoI5d|4#q9##`_F5Xnx`&GPzp2fB{-{P@ATw=X@~ z_|&^UMWAKD;jjBKTK(~o?cUFRK8EX=6>cXpfzg4ZpMB>*w_^8GSiT-Jp|xBOnzM+j z*09-@-~qJ(eqWq5@R4i^u4^{McCP(!3}C|v_WsTR*bIUxN(Nx`u##3B4{sE`Z`v8w zAwIG`?1~PkID~W{uDzmqH98Pew_1(;x2%8r^vY{)_&J2K)cN{W+h5+g)ZcjP&Ci#O zgy|8K@4kyMfwilHd&6TDlhb%++Pk!>9HRld6HT7gwyZGrxS$}CsD6`>6!!2K1@Mjf z(P0WYB7V_OFZyeWrbOFb>O54BNXf~K&?}3=^v;v_wT{DKr?jN^DtN&DXwX%u?s*c6`%8>WFz z7}YW^tp0bp^NriE)AB6M2l<7rn7fzePtR*omOevpfm9n?}2V*+0iW;S)C zhg`NAjL?D=W#k*$aR{>pGf~lD-rVtD;5jW1_*Jn1j1=es@Kcx4ySM_bwcQCT=d+DV z>Sz~L=Hj@(X%31nK$mWI@7d>}ORB`K(p=+`UD)+99YUGQc7y^bHZ1F(8|tL0 zdK*DT0kSXG_{BKTpP2*2PecdKV9;dq$^ZZDP;Nyq1kp-&GI5eAyZsK!e3V zK@rPy*{(`KIfo+lc878mDKk^V#`VT05}64kBtk%DgwLrOvLMj5-;*GNKv6c6pzMuL z6EP%ob|_0IW}lLRXCP2!9wWhEw3LA7iF#1O1mIZ@Z=6&bz41F;@S_GvYAG-#CW3z{ zP3+6vHhvP&A3$##Vo9$dT^#MoGg^|MDm=Bt1d2RRwSZ<;ZHICpLBv5Xs!D?BH^(9_ z7`H=N&^v|Z-%mP}wNzG{aiFCsRgwzwq!N6obW9+7(R; z(SZ=23`|`>qil!LMGG{_Heq!BD>(Y-zV9wD)}hz25JA37YR%39;kI4y9pgtcUass6 zP24}ZY$vvYeI`zy&)A_X#nY3017ap*0&jx|mVwyGhg3;!keU53a}Uhm3BZI$N$6Se zLWlAmy1S0xKJm4G_U@sN_Tm=`$xWJSEwKU98rZ&)1R^*$$1vA3oG#&*%SMxY_~oGP zP&PFJatFLM-Ps%84IV-+Ow)T{C7cqUAvauy4C z(FRz&?6$Rypj{xO!`y=*J5o4@U8Q-(y5(*=YoKeZ+-1YdljXxkA#B)zo=FeQH#?Le zycNUmEEHWO9a=X^pb#&cOq7-`7UA87#|S22)<7RUtZo|(zibX=w;K3qur9vy#`MNV z6UUcf9ZwEnKCCp+OoBnF@OdbvH)ANXO0o~Pi9l8=x3))}L<#vO0-~O4!~--Ket?d} zJaqsj<@CD1%S2cTW%rOP{Vto%0sGW~1RMa_j^)5nil0Yw- z0EE#bP+l4#P^%PQ+N*oxu1Zq05xZ!bXfYTg>9c{(Iw*lnjR^>kz%lAN^zFce7rppy zY8zA~3GD=A6d*hze&l4D_wA~+O!56)BZTe_rEu}Ezi<4!kG|W#amBZ5{&XS2@6R~H z{9o^y*BkH4$~yX9U&@CgbOzX1bn9xqF|zh$Dh0Y5y*E0e90*$!ObrHY3Ok0`2=O~r zCuke6KrP9KOf?V(YDsM<6pX2nVoN%M$LT^q#FmtaF?1^27F*IcNX~XRB(|hCFvdcc zc)$=S-)acdk$g4?_>jRqxpI6M3vHZk?0c^3=byamYDNf;uB{3NlKW5IhnOS3DNkMV z?tK8?kJ}pmvp%&&eTVOVjHP`q34hN1@!aK}H(K!vI`~gf|Gv+FNEQD5Yd<~yX7k_l h&G-K)@HZb3BABY{)U1?^%I#E6`MGoTtustd{~yM6srvu` literal 0 HcmV?d00001 diff --git a/ModernKeePassLib.UwpTest/Assets/Square150x150Logo.scale-200.png b/ModernKeePassLib.UwpTest/Assets/Square150x150Logo.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..af49fec1a5484db1d52a7f9b5ec90a27c7030186 GIT binary patch literal 2937 zcma)84OCO-8BSud5)jwMLRVKgX(S?$n?Ld|vrsm<$CF7)&zTbyy1FE5bU`Q17MRv`9ue$;R(@8kR;#vJ*IM0>cJIAOte!d7oRgdH zd%ySjdB6L9=gX^A6)VzH7p2l@v~3zJAMw|DFy#^)F@@F*`mqUn=Il>l)8_+ab;nOW{%+iPx z+s{Eu|&pIs)Z7{La9~?xKfyl z#43?gjEL15d4WbOZo#SiP%>DB^+BcnJ=7dHEe;r#G=tuw|ka z%q@}##Uh7;tc%L_64m(kHtw74ty%BJMb)_1)#S0j`)F8_1jF7vScpsnH=0V19bO8y zR`0SjIdCUo&=>JwMQF8KHA<{ODHTiQh}0^@5QRmCA?gOH6_H3K^-_sNB^RrdNuK-R zOO*vOrKCVvDwgUck`kF(E7j{I#iiN;b*ZdCt4m@HPA`EuEqGGf4%!K<;(=I=&Vyrw z%TwcWtxa}8mCZ%Cyf&ActJ6_$ox5z6-D!0-dvnRx6t7y3d+h6QYpKWO;8OdnvERo7 zuEf>ih5`wqY)~o@OeVt-wM?Q!>QzdGRj!bz6fzYrfw$hZfAKzr2-M+D+R>}~oT574c;_3zquHcElqKIsryILt3g8n3jcMb+j?i?-L3FpZJ z2WRVBRdDPc+G5aaYg#5hpE+6nQ|(VSoxT3|biF;BUq#==-27Xi=gihDPYP$7?=9cP zYKE$jeQ|3~_L0VG-(F~2ZPyD0=k{J4Q~h(t__{-mz_w8{JDY9{`1ouzz!Vr5!ECdE z6U~O1k8c}24V7~zzXWTV-Pe4)y}wQJS&q%H5`Fo_f_JvIU489aCX$;P`u#!I-=^4ijC2{&9!O&h>mi?9oYD=GC#%)6{GzN6nQYw+Fal50!#x^asjBBR50i`+mho*ttoqV)ubM2KD9S~k7+FR4>{29?6 z{!l6kDdyTN0YJ9LgkPWeXm|gyi@zM3?0@{&pXT12w|78&W-q!RRF)&iLCEZVH<|fR zN0fr2^t8H(>L?>K#>^+jWROLral(Qy-xoBq1U7A&DV||wClb)Otd9?(gZ|8znMF}D zf<1haWz^s0qgecz;RFGt0C-B4g`jNGHsFU+;{<%t65v^sjk^h$lmWn#B0#_)9ij&d z-~lc`A)YYExi^7sBuPM^Y|wA2g*5?`K?#7tzELQYNxGo$UB$4J8RJp1k(8Jj+~hMT zlN~>M@KTTh^--8y3PK_NZ@AC!{PT=CziBzGd+wTJ^@icH!Bd}%)g8V)%K?|c&WTUk zy}qv1C%(fjRoZ4ozC3{O%@5?)XzH35zHns$pgU*Q?fj4v?fp1Qbm+j;3l;9jam9Da zXVcKjPlQ73x78QPu|Ffm6x?`~e3oD=gl=4kYK?={kD5j~QCXU)`HSdduNNENzA*2$ zOm3PzF!lN5e*06-f1Uot67wY#{o-S1!KZ7E=!~7ynnk9_iJR#kFoNbAOT#^2Gd17F zMmvU6>lndZQGd|ax9kUoXXO+$N?|j@6qpsF&_j7YXvwo_C{JpmLw5&#e6k>atv%es z5)7r*Wvv_JkUpT}M!_o!nVlEk1Zbl=a*2hQ*<|%*K1Glj^FcF`6kTzGQ3lz~2tCc@ z&x|tj;aH&1&9HwcJBcT`;{?a+pnej;M1HO(6Z{#J!cZA04hnFl;NXA+&`=7bjW_^o zfC40u3LMG?NdPtwGl>Tq6u}*QG)}-y;)lu-_>ee3kibW(69n0$0Zy!}9rQz%*v1iO zT9_H>99yIrSPYVy6^);rR}7Yo=J_T@hi+qhTZXnVWyf;JDYm5#eYLTxr*?kiNn!+Y zQ+LUkBafNJ#rH#C(?d5^;gw9o#%daEI{mA*LHPIHPU`#|H$hD zwm>0&+kahQ)E#%~k>&5@&#Vg82H?s%71=)(soi@174pi9--2{w{1$}Sz4zGn3Du&x bht0Iza^2ykEt4(epJ78uh5nDlX8(TxzDYwP literal 0 HcmV?d00001 diff --git a/ModernKeePassLib.UwpTest/Assets/Square44x44Logo.scale-200.png b/ModernKeePassLib.UwpTest/Assets/Square44x44Logo.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..ce342a2ec8a61291ba76c54604aea7e9d20af11b GIT binary patch literal 1647 zcmaJ?eM}Q)7(e+G1Q(|`V9JhTI2>MkceK4;p;PR&$Pi?ejk3YQ_3o`S&|W_dsOZ8# zWPTt69g`t$ab`0cj-Y0yiBSOqmd)tG7G(}M5aP0_%&9TijB#&)I{zSE^4@#z^FF`l z`8{8`o%wlL(UI|y2!cdsuVamHH~H86F!*-15em4)NqUpCQM5?aoC_eCf@lV4wvF2a zjDQn1JBL69f&@2M3rvzJcfE!eZ8FZUBlFlC5RD)it33{mF9#B82AiyQE%w)`vlwa> zv{<1sm&kSKK$&%2jSFn7$t&P%%6Ue>R=EAnG8N7fqynWG8L3p!4801a;8{+nliO(qd(jNJ_?+9W3#hLIDLoT6~3fx9=`CC-D}-AMrpEO7HK zt3$GicGPc?GmDjy7K2P@La;eu4!$zWCZ`ym{Z$b zu-O6RM&K4JT|BIZB`E-gxqG%FzanI#+2FFmqHqXG7yxWB=w55RGOM)$xMb(>kSNR z2w=1AZi%z=AmG~yea~XaXJR!v7vLn(RUnELfiB1|6D84ICOS}^Zo2AdN}<&*h}G_u z{xZ!(%>tLT3J3<5XhWy-tg+6)0nmUUENLW8TWA{R6bgVd3X;anYFZ^IRis*_P-C-r z;i>%1^eL3UI2-{w8nuFFcs0e~7J{O2k^~Ce%+Ly4U?|=!0LH=t6()xi<^I-rs+9sF z*q{E-CxZbGPeu#a;XJwE;9S1?#R&uns>^0G3p`hEUF*v`M?@h%T%J%RChmD|EVydq zmHWh*_=S%emRC*mhxaVLzT@>Z2SX0u9v*DIJ@WC^kLVdlGV6LpK$KIrlJqc zpJ921)+3JJdTx|<`G&kXpKkjGJv=76R`yYIQ{#c-`%+`#V(7}Q;&@6U8!Td1`d;?N z_9mnI#?AA}4J!r)LN4!E-@H5eXauuB7TOawS>Y|{-P?NNx-lq+z1W-+y(;39P&&LP zL{N80?&=C*qKmdA^moMZRuPcD!B<*mq$ch=0Cnlitw#txRWhb3%TQvPqjkC`F69G4b! ze7z9MZ#+;_#l?H37UqUhDFb^l&s2{oM$3I0o^Q!yx;;V)QmCMo)Tb_ui|mit8MS?U zm##6$sZZ1$@|s%?l@>4Z<*Q}sRBSKMhb4I{e5LdEhsHIHTe8Bod5c>6QtT>$XgUBz z6MK`kO$=jmt@FqggOhJ5j~e@ygRbG;<{Vu)*+nn9aQeo0;$#j;|MS=S$&L?BeV25z xs3B`@=#`5TF{^6(A1rvdY@|-RtQ|iS5{tyX+wH?;n8E)G$kykv-D^wh{{!TZT%7;_ literal 0 HcmV?d00001 diff --git a/ModernKeePassLib.UwpTest/Assets/Square44x44Logo.targetsize-24_altform-unplated.png b/ModernKeePassLib.UwpTest/Assets/Square44x44Logo.targetsize-24_altform-unplated.png new file mode 100644 index 0000000000000000000000000000000000000000..f6c02ce97e0a802b85f6021e822c89f8bf57d5cd GIT binary patch literal 1255 zcmaJ>TWs4@7*5+{G#S+&C!qC#> zf>5N3P6jO*Cz>ug*(_DmW=)kea&m$gZ^+nyiF`;j%w@}y8)>p*SH}C`m?DXeieF2U zyQHecc_L%Gh!7GMt+hG06y;+|p4>m~}PjA}rKViGiEnn7G0ZO<>G|7q;2?NwGCM3s?eued6%hd$B+ z*kQJ{#~$S=DFE(%=E+UkmlEI*%3llUf~8Ja9YU1Vui0IbGBkW_gHB%Rd&!!ioX zs40O?i9I{};kle7GMvE7(rk`la=gTI)47=>%?q@^iL-nUo3}h4S}N-KHn8t5mVP8w z&bSErwp+37 zNJJ8?a|{r5Q3R0Z5s-LB1WHOwYC@7pCHWND#cL1cZ?{kJ368_*(UDWUDyb<}0y@o# zfMF016iMWPCb6obAxT$JlB6(2DrlXDTB&!0`!m??4F(qWMhjVZo?JXQmz`1*58Z=& zcDmB|S-E@j?BoFGix0flckqdS4jsPNzhfWyWIM98GxcLs89C(~dw%$_t;JjX-SD}E zfiGV;{8Q%8r}w9x>EEigW81>`kvnU@pK)4+xk9@+bNj9L!AAZ@SZ@q|)&BmY3+HZx zul~BeG4|}-;L%cHViQGQX?^zFfO0&#cHwel=d`lH9sJ-@Sl@n*(8J2>%Ac`IxyY?Q z{=GhWvC#gu-~Ia7*n{=+;qM?Ul_wy1+u7ho;=`>EwP^g~R@{unBds`!#@}tluZQpS zm)M~nYEifJWJGx?_6DcTy>#uh%>!H9=hb^(v`=m3F1{L>db=<5_tm+_&knAQ2EU$s Mu9UqpbNZeC0BbUo^Z)<= literal 0 HcmV?d00001 diff --git a/ModernKeePassLib.UwpTest/Assets/StoreLogo.png b/ModernKeePassLib.UwpTest/Assets/StoreLogo.png new file mode 100644 index 0000000000000000000000000000000000000000..7385b56c0e4d3c6b0efe3324aa1194157d837826 GIT binary patch literal 1451 zcmaJ>eN5D57_Z|bH;{0+1#mbl)eTU3{h)Wf7EZV?;HD@XL@{B`Ui%(2aMxQ~xdXSv z5nzWi(LW)U2=Vc-cY@s7nPt{i0hc6!7xN4NNHI#EQl>YNBy8l4%x9gr_W-j zEZMQmmTIy(>;lblRfh`dIyTgc9W5d!VP$L4(kKrN1c5G~(O_#xG zAJCNTstD^5SeXFB+&$h=ToJP2H>xr$iqPs-#O*;4(!Fjw25-!gEb*)mU}=)J;Iu>w zxK(5XoD0wrPSKQ~rbL^Cw6O_03*l*}i=ydbu7adJ6y;%@tjFeXIXT+ms30pmbOP%Q zX}S;+LBh8Tea~TSkHzvX6$rYb)+n&{kSbIqh|c7hmlxmwSiq5iVhU#iEQ<>a18|O^Sln-8t&+t`*{qBWo5M?wFM(JuimAOb5!K#D}XbslM@#1ZVz_;!9U zpfEpLAOz=0g@bd6Xj_ILi-x^!M}73h^o@}hM$1jflTs|Yuj9AL@A3<-?MV4!^4q`e z)fO@A;{9K^?W?DbnesnPr6kK>$zaKo&;FhFd(GYFCIU^T+OIMb%Tqo+P%oq(IdX7S zf6+HLO?7o0m+p>~Tp5UrXWh!UH!wZ5kv!E`_w)PTpI(#Iw{AS`gH4^b(bm^ZCq^FZ zY9DD7bH}rq9mg88+KgA$Zp!iWncuU2n1AuIa@=sWvUR-s`Qb{R*kk(SPU^`$6BXz8 zn#7yaFOIK%qGxyi`dYtm#&qqox0$h=pNi#u=M8zUG@bpiZ=3sT=1}Trr}39cC)H|v zbL?W)=&s4zrh)7>L(|cc%$1#!zfL?HjpeP%T+x_a+jZ16b^iKOHxFEX$7d|8${H-* zIrOJ5w&i$>*D>AKaIoYg`;{L@jM((Kt?$N$5OnuPqVvq**Nm}(f0wwOF%iX_Pba;V z;m@wxX&NcV3?<1+u?A{y_DIj7#m3Af1rCE)o`D&Y3}0%7E;iX1yMDiS)sh0wKi!36 zL!Wmq?P^Ku&rK~HJd97KkLTRl>ScGFYZNlYytWnhmuu|)L&ND8_PmkayQb{HOY640 bno1(wj@u8DCVuFR|31B*4ek@pZJqxCDDe1x literal 0 HcmV?d00001 diff --git a/ModernKeePassLib.UwpTest/Assets/Wide310x150Logo.scale-200.png b/ModernKeePassLib.UwpTest/Assets/Wide310x150Logo.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..288995b397fdbef1fb7e85afd71445d5de1952c5 GIT binary patch literal 3204 zcmbVPeQXow8NYmBd90>}0NP?GhXW~VaeThm=a0tV#EwJMI!)6M3}|c4_Bl3=Kd>G0 z(GHx1wl<7(tP?FsOQkTilSo*iIvF%uArExJ73~P zSv1xEy!U(Wd4A9D`FQV@W3@F^qJ@PEF$@z`Z!*BbFsS(^?B zyiAzJ+q})bkgiQHWqEb*jJD-coHYr1^iocg)l!Qa{Xqs-l~6J}p-|##ZHYofskQ3$ zI0;xzXyhazBeXhIsg5A=%ufo@f)1yy&ScKS0;HF^!r_2UE^lpZEom(+@duma3awTv zCrCL-%D_SvYWIcdHkmI}#50(fkUi)Qgx!80ju>g1za^}ff>JI8Z@^-iCiaCgg@TgF z+vtE?Q9{VQUX&MW9SYYmGcxA14%N2@7FwBTD4N<(2{nWgV8$e3?-F=L^&FrtWn~(U_Q~~^uYiyeY6-KoTnfh9AWz@ zIKje0)u!_Lw)E}G!#kEfwKVdNt(UAf9*f>tEL_(=xco-T%jTi@7YlC3hs2ik%Le0H ztj}RTeCF(5mwvi3_56>-yB?l;J>-1%!9~=fs|QcNG3J~a@JCu`4SB460s0ZO+##4fFUSGLcj_ja^fL4&BKALfb#$6$O?>P@qx2Agl^x0i&ugt zsy5Pyu=()`7HRMG3IB7F1@`_ z+-!J%#i6e^U$e#+C%Q>_qVRzWRsG^W_n+@OcX@vzI&z;mzHNb!GQ?LWA(wtpqHqTM z1OFw_{Zn?fD)p)`c`kOgv{de=v@suGRqY{N^U7gI1VF3*F=obwaXI6ob5__Yn zVTguS!%(NI09J8x#AO_aW!9W7k*UvB;IWDFC3srwftr{kHj%g)fvnAm;&h_dnl~

MY- zf+K}sCe8qU6Ujs`3ua{U0Of$R_gVQBuUA za0v=mu#vIOqiiAZOr&h*$WyOw&k-xr$;G4Ixa!#TJNr>95(h>l%)PUy4p+^SgR(uR zta%k*?ny-+nAr8spEk1fo{J4i!b^Fia`N{_F6@zidA2ZTTrjl#^5Z-2KfB@Cu}l9s z(*|Z2jc?p~vn2f)3y9i*7zJV1L{$?|&q)4oaT;uXi6>1GkRXVTOzAz(RHEmr=eFIi z`}<>-Q?K0GN8!IYxeP1XKXO+jsJbp~o^);Bc;%b7Flpe7;1`Ny@3r7ZR;?R)aJt8C ziNlEC<@3f_lIV4TwV}&e;D!Ee5_|e#g0LUh=5vmYWYm7&2h*M>QPKvGh9-)wfMMW3 z8J9b%1k7dzPzO0_NGQy92BZ^FR6R~6;^6?lqO;-QUP4BY%cG%3vEhbm#>4vIhPBh3 z-+pZGjh$x%Hp{?=FHsMp0&wNPlj00us{&`1ZOZTqs8%4X&xH=UDr*xyBW(Zp&Em94 zf)ZSfn#yg0N)>!1kWdkqJ^S*z0FF5|fj&qcE#Na|%OY0$uO>!&hP+1ywfD_WXk@4J(?MBftK7>$Nvqh@tDuarN%PrTLQ2Uzysx>UV=V zk^RrDSvdQ?0;=hY67EgII-f4`t=+i*yS=Y~!XlqIy_4x&%+OdfbKOFPXS2X5%4R{N z$SQMX^AK6(fA\r\n" + + "\r\n" + + "\t\r\n" + + "\t\t1.00\r\n" + + "\t\r\n" + + "\t\r\n" + + "\t\t"; + + private const string ExpectedFileEnd = "\r\n\t\r\n"; + + [Fact] + public async Task TestConstruct() + { + var expectedKeyData = new byte[] + { + 0x95, 0x94, 0xdc, 0xb9, 0x91, 0xc6, 0x65, 0xa0, + 0x81, 0xf6, 0x6f, 0xca, 0x07, 0x1a, 0x30, 0xd1, + 0x1d, 0x65, 0xcf, 0x8d, 0x9c, 0x60, 0xfb, 0xe6, + 0x45, 0xfc, 0xc8, 0x92, 0xbd, 0xeb, 0xaf, 0xc3 + }; + + var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetTempPath()); + var file = await folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting); + var token = StorageApplicationPermissions.FutureAccessList.Add(file); + using (var fs = await file.OpenStreamForWriteAsync()) + { + using (var sw = new StreamWriter(fs)) + { + sw.Write(ExpectedFileStart); + sw.Write(TestKey); + sw.Write(ExpectedFileEnd); + } + } + + try + { + var keyFile = new KcpKeyFile(token); + var keyData = keyFile.KeyData.ReadData(); + Assert.True(MemUtil.ArraysEqual(keyData, expectedKeyData)); + } + finally + { + await file.DeleteAsync(); + } + } + + [Fact] + public async Task TestCreate() + { + var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetTempPath()); + var file = await folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting); + var token = StorageApplicationPermissions.FutureAccessList.Add(file); + KcpKeyFile.Create(token, null); + try + { + var fileContents = await FileIO.ReadTextAsync(file); + + Assert.Equal(185, fileContents.Length); + Assert.StartsWith(ExpectedFileStart, fileContents); + Assert.EndsWith(ExpectedFileEnd, fileContents); + } + finally + { + await file.DeleteAsync(); + } + } + } +} + diff --git a/ModernKeePassLib.UwpTest/ModernKeePassLib.UwpTest.csproj b/ModernKeePassLib.UwpTest/ModernKeePassLib.UwpTest.csproj new file mode 100644 index 0000000..dc8adae --- /dev/null +++ b/ModernKeePassLib.UwpTest/ModernKeePassLib.UwpTest.csproj @@ -0,0 +1,179 @@ + + + + + Debug + x86 + {E43877FF-6725-43DA-A3AF-F1A7CB86EE03} + AppContainerExe + Properties + ModernKeePassLib.UwpTest + ModernKeePassLib.UwpTest + en-US + UAP + 10.0.17763.0 + 10.0.17763.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(VisualStudioVersion) + + + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x86 + false + prompt + true + + + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x86 + false + prompt + true + true + + + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM + false + prompt + true + + + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM + false + prompt + true + true + + + true + bin\ARM64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM64 + false + prompt + true + true + + + bin\ARM64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM64 + false + prompt + true + true + + + true + bin\x64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x64 + false + prompt + true + + + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x64 + false + prompt + true + true + + + PackageReference + + + + + + + + + UnitTestApp.xaml + + + + + MSBuild:Compile + Designer + + + + + Designer + + + + + + + + + + + + + + + 6.2.8 + + + 1.4.0 + + + 1.4.0 + + + 2.4.1 + + + + + {15a7e2e5-2b46-4975-afbd-2898e47e5ba1} + ModernKeePassLib + + + + 14.0 + + + + \ No newline at end of file diff --git a/ModernKeePassLib.UwpTest/Package.appxmanifest b/ModernKeePassLib.UwpTest/Package.appxmanifest new file mode 100644 index 0000000..b350b75 --- /dev/null +++ b/ModernKeePassLib.UwpTest/Package.appxmanifest @@ -0,0 +1,46 @@ + + + + + + + + + ModernKeePassLib.UwpTest + GeoffroyBONNEVILLE + Assets\StoreLogo.png + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ModernKeePassLib.UwpTest/Properties/AssemblyInfo.cs b/ModernKeePassLib.UwpTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..cc4d859 --- /dev/null +++ b/ModernKeePassLib.UwpTest/Properties/AssemblyInfo.cs @@ -0,0 +1,18 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("ModernKeePassLib.UwpTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ModernKeePassLib.UwpTest")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +[assembly: AssemblyMetadata("TargetPlatform","UAP")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: ComVisible(false)] \ No newline at end of file diff --git a/ModernKeePassLib.UwpTest/Properties/Default.rd.xml b/ModernKeePassLib.UwpTest/Properties/Default.rd.xml new file mode 100644 index 0000000..996a839 --- /dev/null +++ b/ModernKeePassLib.UwpTest/Properties/Default.rd.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/ModernKeePassLib.UwpTest/UnitTestApp.xaml b/ModernKeePassLib.UwpTest/UnitTestApp.xaml new file mode 100644 index 0000000..eeb9bca --- /dev/null +++ b/ModernKeePassLib.UwpTest/UnitTestApp.xaml @@ -0,0 +1,7 @@ + + + diff --git a/ModernKeePassLib.UwpTest/UnitTestApp.xaml.cs b/ModernKeePassLib.UwpTest/UnitTestApp.xaml.cs new file mode 100644 index 0000000..41c3425 --- /dev/null +++ b/ModernKeePassLib.UwpTest/UnitTestApp.xaml.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.ApplicationModel; +using Windows.ApplicationModel.Activation; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Navigation; + +namespace ModernKeePassLib.UwpTest +{ + ///

+ /// Provides application-specific behavior to supplement the default Application class. + /// + sealed partial class App : Application + { + /// + /// Initializes the singleton application object. This is the first line of authored code + /// executed, and as such is the logical equivalent of main() or WinMain(). + /// + public App() + { + this.InitializeComponent(); + this.Suspending += OnSuspending; + } + + /// + /// Invoked when the application is launched normally by the end user. Other entry points + /// will be used such as when the application is launched to open a specific file. + /// + /// Details about the launch request and process. + protected override void OnLaunched(LaunchActivatedEventArgs e) + { + +#if DEBUG + if (System.Diagnostics.Debugger.IsAttached) + { + this.DebugSettings.EnableFrameRateCounter = true; + } +#endif + + Frame rootFrame = Window.Current.Content as Frame; + + // Do not repeat app initialization when the Window already has content, + // just ensure that the window is active + if (rootFrame == null) + { + // Create a Frame to act as the navigation context and navigate to the first page + rootFrame = new Frame(); + + rootFrame.NavigationFailed += OnNavigationFailed; + + if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) + { + //TODO: Load state from previously suspended application + } + + // Place the frame in the current Window + Window.Current.Content = rootFrame; + } + + Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI(); + + // Ensure the current window is active + Window.Current.Activate(); + + Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(e.Arguments); + } + + /// + /// Invoked when Navigation to a certain page fails + /// + /// The Frame which failed navigation + /// Details about the navigation failure + void OnNavigationFailed(object sender, NavigationFailedEventArgs e) + { + throw new Exception("Failed to load Page " + e.SourcePageType.FullName); + } + + /// + /// Invoked when application execution is being suspended. Application state is saved + /// without knowing whether the application will be terminated or resumed with the contents + /// of memory still intact. + /// + /// The source of the suspend request. + /// Details about the suspend request. + private void OnSuspending(object sender, SuspendingEventArgs e) + { + var deferral = e.SuspendingOperation.GetDeferral(); + //TODO: Save application state and stop any background activity + deferral.Complete(); + } + } +} diff --git a/ModernKeePassLib.sln b/ModernKeePassLib.sln index 5477438..05b52e1 100644 --- a/ModernKeePassLib.sln +++ b/ModernKeePassLib.sln @@ -10,17 +10,57 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|ARM = Debug|ARM + Debug|ARM64 = Debug|ARM64 + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|ARM = Release|ARM + Release|ARM64 = Release|ARM64 + Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|ARM.ActiveCfg = Debug|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|ARM.Build.0 = Debug|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|ARM64.Build.0 = Debug|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|x64.ActiveCfg = Debug|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|x64.Build.0 = Debug|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|x86.ActiveCfg = Debug|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|x86.Build.0 = Debug|Any CPU {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|Any CPU.ActiveCfg = Release|Any CPU {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|Any CPU.Build.0 = Release|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|ARM.ActiveCfg = Release|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|ARM.Build.0 = Release|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|ARM64.ActiveCfg = Release|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|ARM64.Build.0 = Release|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|x64.ActiveCfg = Release|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|x64.Build.0 = Release|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|x86.ActiveCfg = Release|Any CPU + {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|x86.Build.0 = Release|Any CPU {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|ARM.ActiveCfg = Debug|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|ARM.Build.0 = Debug|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|ARM64.Build.0 = Debug|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|x64.ActiveCfg = Debug|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|x64.Build.0 = Debug|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|x86.ActiveCfg = Debug|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|x86.Build.0 = Debug|Any CPU {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|Any CPU.ActiveCfg = Release|Any CPU {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|Any CPU.Build.0 = Release|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|ARM.ActiveCfg = Release|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|ARM.Build.0 = Release|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|ARM64.ActiveCfg = Release|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|ARM64.Build.0 = Release|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|x64.ActiveCfg = Release|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|x64.Build.0 = Release|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|x86.ActiveCfg = Release|Any CPU + {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ModernKeePassLib/Cryptography/CryptoRandomStream.cs b/ModernKeePassLib/Cryptography/CryptoRandomStream.cs index edadfbc..e17fc83 100644 --- a/ModernKeePassLib/Cryptography/CryptoRandomStream.cs +++ b/ModernKeePassLib/Cryptography/CryptoRandomStream.cs @@ -225,6 +225,25 @@ namespace ModernKeePassLib.Cryptography return MemUtil.BytesToUInt64(pb); } + internal ulong GetRandomUInt64(ulong uMaxExcl) + { + if(uMaxExcl == 0) { Debug.Assert(false); throw new ArgumentOutOfRangeException("uMaxExcl"); } + + ulong uGen, uRem; + do + { + uGen = GetRandomUInt64(); + uRem = uGen % uMaxExcl; + } + while((uGen - uRem) > (ulong.MaxValue - (uMaxExcl - 1UL))); + // This ensures that the last number of the block (i.e. + // (uGen - uRem) + (uMaxExcl - 1)) is generatable; + // for signed longs, overflow to negative number: + // while((uGen - uRem) + (uMaxExcl - 1) < 0); + + return uRem; + } + #if CRSBENCHMARK public static string Benchmark() { diff --git a/ModernKeePassLib/Cryptography/CryptoStreamEx.cs b/ModernKeePassLib/Cryptography/CryptoStreamEx.cs index 3b4e70b..a7beb03 100644 --- a/ModernKeePassLib/Cryptography/CryptoStreamEx.cs +++ b/ModernKeePassLib/Cryptography/CryptoStreamEx.cs @@ -28,37 +28,39 @@ using System.Security.Cryptography; namespace ModernKeePassLib.Cryptography { - public sealed class CryptoStreamEx : CryptoStream - { - private ICryptoTransform m_t; - private SymmetricAlgorithm m_a; + public sealed class CryptoStreamEx : CryptoStream + { + private ICryptoTransform m_t; + private SymmetricAlgorithm m_a; - public CryptoStreamEx(Stream s, ICryptoTransform t, CryptoStreamMode m, - SymmetricAlgorithm a) : base(s, t, m) - { - m_t = t; - m_a = a; - } + public CryptoStreamEx(Stream s, ICryptoTransform t, CryptoStreamMode m, + SymmetricAlgorithm a) : base(s, t, m) + { + m_t = t; + m_a = a; + } - protected override void Dispose(bool disposing) - { - try { base.Dispose(disposing); } - // Unnecessary exception from CryptoStream with - // RijndaelManagedTransform when a stream hasn't been - // read completely (e.g. incorrect master key) - catch (CryptographicException) { } - catch (Exception) { Debug.Assert(false); } + protected override void Dispose(bool disposing) + { + try { base.Dispose(disposing); } + // Unnecessary exception from CryptoStream with + // RijndaelManagedTransform when a stream hasn't been + // read completely (e.g. incorrect master key) + catch(CryptographicException) { } + // Similar to above, at the beginning of the stream + catch(IndexOutOfRangeException) { } + catch(Exception) { Debug.Assert(false); } - if (disposing) - { - try { if (m_t != null) { m_t.Dispose(); m_t = null; } } - catch (Exception) { Debug.Assert(false); } + if(disposing) + { + try { if(m_t != null) { m_t.Dispose(); m_t = null; } } + catch(Exception) { Debug.Assert(false); } - // In .NET 2.0, SymmetricAlgorithm.Dispose() is not public - try { if (m_a != null) { m_a.Clear(); m_a = null; } } - catch (Exception) { Debug.Assert(false); } - } - } - } + // In .NET 2.0, SymmetricAlgorithm.Dispose() is not public + try { if(m_a != null) { m_a.Clear(); m_a = null; } } + catch(Exception) { Debug.Assert(false); } + } + } + } } #endif diff --git a/ModernKeePassLib/Cryptography/KeyDerivation/Argon2Kdf.cs b/ModernKeePassLib/Cryptography/KeyDerivation/Argon2Kdf.cs index dcb92ee..ec78e39 100644 --- a/ModernKeePassLib/Cryptography/KeyDerivation/Argon2Kdf.cs +++ b/ModernKeePassLib/Cryptography/KeyDerivation/Argon2Kdf.cs @@ -42,13 +42,13 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation private const uint MaxVersion = 0x13; private const int MinSalt = 8; - private const int MaxSalt = int.MaxValue; // .NET limit; 2^32 - 1 in spec + private const int MaxSalt = int.MaxValue; // .NET limit; 2^32 - 1 in spec. internal const ulong MinIterations = 1; internal const ulong MaxIterations = uint.MaxValue; internal const ulong MinMemory = 1024 * 8; // For parallelism = 1 - // internal const ulong MaxMemory = (ulong)uint.MaxValue * 1024UL; // Spec + // internal const ulong MaxMemory = (ulong)uint.MaxValue * 1024UL; // Spec. internal const ulong MaxMemory = int.MaxValue; // .NET limit internal const uint MinParallelism = 1; diff --git a/ModernKeePassLib/Cryptography/PasswordGenerator/CharSetBasedGenerator.cs b/ModernKeePassLib/Cryptography/PasswordGenerator/CharSetBasedGenerator.cs index a50df78..763a0a3 100644 --- a/ModernKeePassLib/Cryptography/PasswordGenerator/CharSetBasedGenerator.cs +++ b/ModernKeePassLib/Cryptography/PasswordGenerator/CharSetBasedGenerator.cs @@ -36,20 +36,20 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator if(pwProfile.Length == 0) return PwgError.Success; PwCharSet pcs = new PwCharSet(pwProfile.CharSet.ToString()); - PwGenerator.PrepareCharSet(pcs, pwProfile); + if(!PwGenerator.PrepareCharSet(pcs, pwProfile)) + return PwgError.InvalidCharSet; char[] v = new char[pwProfile.Length]; try { for(int i = 0; i < v.Length; ++i) { - char ch = PwGenerator.GenerateCharacter(pwProfile, - pcs, crsRandomSource); - + char ch = PwGenerator.GenerateCharacter(pcs, crsRandomSource); if(ch == char.MinValue) return PwgError.TooFewCharacters; v[i] = ch; + if(pwProfile.NoRepeatingCharacters) pcs.Remove(ch); } byte[] pbUtf8 = StrUtil.Utf8.GetBytes(v); diff --git a/ModernKeePassLib/Cryptography/PasswordGenerator/PatternBasedGenerator.cs b/ModernKeePassLib/Cryptography/PasswordGenerator/PatternBasedGenerator.cs index 55773e6..b06ac42 100644 --- a/ModernKeePassLib/Cryptography/PasswordGenerator/PatternBasedGenerator.cs +++ b/ModernKeePassLib/Cryptography/PasswordGenerator/PatternBasedGenerator.cs @@ -34,92 +34,61 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator { psOut = ProtectedString.Empty; + string strPattern = pwProfile.Pattern; + if(string.IsNullOrEmpty(strPattern)) return PwgError.Success; + + CharStream cs = new CharStream(strPattern); LinkedList llGenerated = new LinkedList(); - PwCharSet pcsCurrent = new PwCharSet(); - PwCharSet pcsCustom = new PwCharSet(); - PwCharSet pcsUsed = new PwCharSet(); - bool bInCharSetDef = false; + PwCharSet pcs = new PwCharSet(); - string strPattern = ExpandPattern(pwProfile.Pattern); - if(strPattern.Length == 0) return PwgError.Success; - - CharStream csStream = new CharStream(strPattern); - char ch = csStream.ReadChar(); - - while(ch != char.MinValue) + while(true) { - pcsCurrent.Clear(); + char ch = cs.ReadChar(); + if(ch == char.MinValue) break; - bool bGenerateChar = false; + pcs.Clear(); if(ch == '\\') { - ch = csStream.ReadChar(); - if(ch == char.MinValue) // Backslash at the end - { - llGenerated.AddLast('\\'); - break; - } + ch = cs.ReadChar(); + if(ch == char.MinValue) return PwgError.InvalidPattern; - if(bInCharSetDef) pcsCustom.Add(ch); - else - { - llGenerated.AddLast(ch); - pcsUsed.Add(ch); - } - } - else if(ch == '^') - { - ch = csStream.ReadChar(); - if(ch == char.MinValue) // ^ at the end - { - llGenerated.AddLast('^'); - break; - } - - if(bInCharSetDef) pcsCustom.Remove(ch); + pcs.Add(ch); // Allow "{...}" support and char check } else if(ch == '[') { - pcsCustom.Clear(); - bInCharSetDef = true; + if(!ReadCustomCharSet(cs, pcs)) + return PwgError.InvalidPattern; } - else if(ch == ']') + else { - pcsCurrent.Add(pcsCustom.ToString()); + if(!pcs.AddCharSet(ch)) + return PwgError.InvalidPattern; + } - bInCharSetDef = false; - bGenerateChar = true; - } - else if(bInCharSetDef) + int nCount = 1; + if(cs.PeekChar() == '{') { - if(pcsCustom.AddCharSet(ch) == false) - pcsCustom.Add(ch); + nCount = ReadCount(cs); + if(nCount < 0) return PwgError.InvalidPattern; } - else if(pcsCurrent.AddCharSet(ch) == false) - { - llGenerated.AddLast(ch); - pcsUsed.Add(ch); - } - else bGenerateChar = true; - if(bGenerateChar) + for(int i = 0; i < nCount; ++i) { - PwGenerator.PrepareCharSet(pcsCurrent, pwProfile); - + if(!PwGenerator.PrepareCharSet(pcs, pwProfile)) + return PwgError.InvalidCharSet; if(pwProfile.NoRepeatingCharacters) - pcsCurrent.Remove(pcsUsed.ToString()); - - char chGen = PwGenerator.GenerateCharacter(pwProfile, - pcsCurrent, crsRandomSource); + { + foreach(char chUsed in llGenerated) + pcs.Remove(chUsed); + } + char chGen = PwGenerator.GenerateCharacter(pcs, + crsRandomSource); if(chGen == char.MinValue) return PwgError.TooFewCharacters; llGenerated.AddLast(chGen); - pcsUsed.Add(chGen); } - - ch = csStream.ReadChar(); } if(llGenerated.Count == 0) return PwgError.Success; @@ -135,53 +104,70 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator MemUtil.ZeroByteArray(pbUtf8); MemUtil.ZeroArray(v); - llGenerated.Clear(); - return PwgError.Success; } - private static string ExpandPattern(string strPattern) + private static bool ReadCustomCharSet(CharStream cs, PwCharSet pcsOut) { - if(strPattern == null) { Debug.Assert(false); return string.Empty; } - - string str = strPattern; + Debug.Assert(cs.PeekChar() != '['); // Consumed already + Debug.Assert(pcsOut.Size == 0); + bool bAdd = true; while(true) { - int nOpen = FindFirstUnescapedChar(str, '{'); - int nClose = FindFirstUnescapedChar(str, '}'); + char ch = cs.ReadChar(); + if(ch == char.MinValue) return false; + if(ch == ']') break; - if((nOpen >= 0) && (nOpen < nClose)) + if(ch == '\\') { - string strCount = str.Substring(nOpen + 1, nClose - nOpen - 1); - str = str.Remove(nOpen, nClose - nOpen + 1); + ch = cs.ReadChar(); + if(ch == char.MinValue) return false; - uint uRepeat; - if(StrUtil.TryParseUInt(strCount, out uRepeat) && (nOpen >= 1)) - { - if(uRepeat == 0) - str = str.Remove(nOpen - 1, 1); - else - str = str.Insert(nOpen, new string(str[nOpen - 1], (int)uRepeat - 1)); - } + 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()); } - else break; } - return str; + return true; } - private static int FindFirstUnescapedChar(string str, char ch) + private static int ReadCount(CharStream cs) { - for(int i = 0; i < str.Length; ++i) - { - char chCur = str[i]; + if(cs.ReadChar() != '{') { Debug.Assert(false); return -1; } - if(chCur == '\\') ++i; // Next is escaped, skip it - else if(chCur == ch) return i; + // 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 -1; + return (int)n; } } } diff --git a/ModernKeePassLib/Cryptography/PasswordGenerator/PwCharSet.cs b/ModernKeePassLib/Cryptography/PasswordGenerator/PwCharSet.cs index 4774b7f..463de3f 100644 --- a/ModernKeePassLib/Cryptography/PasswordGenerator/PwCharSet.cs +++ b/ModernKeePassLib/Cryptography/PasswordGenerator/PwCharSet.cs @@ -43,7 +43,6 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator public static readonly string UpperHex = "0123456789ABCDEF"; public static readonly string LowerHex = "0123456789abcdef"; - public static readonly string Invalid = "\t\r\n"; public static readonly string LookAlike = @"O0l1I|"; internal static readonly string MenuAccels = PwCharSet.LowerCase + PwCharSet.Digits; diff --git a/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs b/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs index 472bec0..b2f33f7 100644 --- a/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs +++ b/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs @@ -36,7 +36,9 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator Success = 0, Unknown = 1, TooFewCharacters = 2, - UnknownAlgorithm = 3 + UnknownAlgorithm = 3, + InvalidCharSet = 4, + InvalidPattern = 5 } /// @@ -94,30 +96,33 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator return new CryptoRandomStream(CrsAlgorithm.ChaCha20, pbKey); } - internal static char GenerateCharacter(PwProfile pwProfile, - PwCharSet pwCharSet, CryptoRandomStream crsRandomSource) + internal static char GenerateCharacter(PwCharSet pwCharSet, + CryptoRandomStream crsRandomSource) { - if(pwCharSet.Size == 0) return char.MinValue; + uint cc = pwCharSet.Size; + if(cc == 0) return char.MinValue; - ulong uIndex = crsRandomSource.GetRandomUInt64(); - uIndex %= (ulong)pwCharSet.Size; - - char ch = pwCharSet[(uint)uIndex]; - - if(pwProfile.NoRepeatingCharacters) - pwCharSet.Remove(ch); - - return ch; + uint i = (uint)crsRandomSource.GetRandomUInt64(cc); + return pwCharSet[i]; } - internal static void PrepareCharSet(PwCharSet pwCharSet, PwProfile pwProfile) + internal static bool PrepareCharSet(PwCharSet pwCharSet, PwProfile pwProfile) { - pwCharSet.Remove(PwCharSet.Invalid); + uint cc = pwCharSet.Size; + for(uint i = 0; i < cc; ++i) + { + char ch = pwCharSet[i]; + if((ch == char.MinValue) || (ch == '\t') || (ch == '\r') || + (ch == '\n') || char.IsSurrogate(ch)) + return false; + } if(pwProfile.ExcludeLookAlike) pwCharSet.Remove(PwCharSet.LookAlike); - if(pwProfile.ExcludeCharacters.Length > 0) + if(!string.IsNullOrEmpty(pwProfile.ExcludeCharacters)) pwCharSet.Remove(pwProfile.ExcludeCharacters); + + return true; } internal static void Shuffle(char[] v, CryptoRandomStream crsRandomSource) @@ -127,8 +132,7 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator for(int i = v.Length - 1; i >= 1; --i) { - ulong r = crsRandomSource.GetRandomUInt64(); - int j = (int)(r % (ulong)(i + 1)); + int j = (int)crsRandomSource.GetRandomUInt64((ulong)(i + 1)); char t = v[i]; v[i] = v[j]; diff --git a/ModernKeePassLib/Interfaces/IStatusLogger.cs b/ModernKeePassLib/Interfaces/IStatusLogger.cs index 8a275cd..3675210 100644 --- a/ModernKeePassLib/Interfaces/IStatusLogger.cs +++ b/ModernKeePassLib/Interfaces/IStatusLogger.cs @@ -87,7 +87,7 @@ namespace ModernKeePassLib.Interfaces bool SetText(string strNewText, LogStatusType lsType); /// - /// Check if the user cancelled the current work. + /// Check whether the user cancelled the current work. /// /// Returns true if the caller should continue /// the current work. diff --git a/ModernKeePassLib/Interfaces/ITimeLogger.cs b/ModernKeePassLib/Interfaces/ITimeLogger.cs index 20dd502..4c6fd73 100644 --- a/ModernKeePassLib/Interfaces/ITimeLogger.cs +++ b/ModernKeePassLib/Interfaces/ITimeLogger.cs @@ -66,7 +66,7 @@ namespace ModernKeePassLib.Interfaces } /// - /// Flag that determines if the object does expire. + /// Flag that determines whether the object expires. /// bool Expires { diff --git a/ModernKeePassLib/Keys/CompositeKey.cs b/ModernKeePassLib/Keys/CompositeKey.cs index a9dcff9..cdcce64 100644 --- a/ModernKeePassLib/Keys/CompositeKey.cs +++ b/ModernKeePassLib/Keys/CompositeKey.cs @@ -21,9 +21,12 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; +using System.Threading; using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography.KeyDerivation; +using ModernKeePassLib.Interfaces; +using ModernKeePassLib.Native; using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; @@ -166,7 +169,6 @@ namespace ModernKeePassLib.Keys { ValidateUserKeys(); - // Concatenate user key data List lData = new List(); int cbData = 0; foreach(IUserKey pKey in m_vUserKeys) @@ -199,13 +201,17 @@ namespace ModernKeePassLib.Keys { if(ckOther == null) throw new ArgumentNullException("ckOther"); + bool bEqual; byte[] pbThis = CreateRawCompositeKey32(); - byte[] pbOther = ckOther.CreateRawCompositeKey32(); - bool bResult = MemUtil.ArraysEqual(pbThis, pbOther); - MemUtil.ZeroByteArray(pbOther); - MemUtil.ZeroByteArray(pbThis); + try + { + byte[] pbOther = ckOther.CreateRawCompositeKey32(); + bEqual = MemUtil.ArraysEqual(pbThis, pbOther); + MemUtil.ZeroByteArray(pbOther); + } + finally { MemUtil.ZeroByteArray(pbThis); } - return bResult; + return bEqual; } [Obsolete] @@ -231,31 +237,90 @@ namespace ModernKeePassLib.Keys { if(p == null) { Debug.Assert(false); throw new ArgumentNullException("p"); } - byte[] pbRaw32 = CreateRawCompositeKey32(); - if((pbRaw32 == null) || (pbRaw32.Length != 32)) - { Debug.Assert(false); return null; } + byte[] pbRaw32 = null, pbTrf32 = null; + ProtectedBinary pbRet = null; - KdfEngine kdf = KdfPool.Get(p.KdfUuid); - if(kdf == null) // CryptographicExceptions are translated to "file corrupted" - throw new Exception(KLRes.UnknownKdf + MessageService.NewParagraph + - KLRes.FileNewVerOrPlgReq + MessageService.NewParagraph + - "UUID: " + p.KdfUuid.ToHexString() + "."); - - byte[] pbTrf32 = kdf.Transform(pbRaw32, p); - if(pbTrf32 == null) { Debug.Assert(false); return null; } - - if(pbTrf32.Length != 32) + try { - Debug.Assert(false); - pbTrf32 = CryptoUtil.HashSha256(pbTrf32); + pbRaw32 = CreateRawCompositeKey32(); + if((pbRaw32 == null) || (pbRaw32.Length != 32)) + { Debug.Assert(false); return null; } + + KdfEngine kdf = KdfPool.Get(p.KdfUuid); + if(kdf == null) // CryptographicExceptions are translated to "file corrupted" + throw new Exception(KLRes.UnknownKdf + MessageService.NewParagraph + + KLRes.FileNewVerOrPlgReq + MessageService.NewParagraph + + "UUID: " + p.KdfUuid.ToHexString() + "."); + + pbTrf32 = kdf.Transform(pbRaw32, p); + if(pbTrf32 == null) { Debug.Assert(false); return null; } + if(pbTrf32.Length != 32) + { + Debug.Assert(false); + pbTrf32 = CryptoUtil.HashSha256(pbTrf32); + } + + pbRet = new ProtectedBinary(true, pbTrf32); + } + finally + { + if(pbRaw32 != null) MemUtil.ZeroByteArray(pbRaw32); + if(pbTrf32 != null) MemUtil.ZeroByteArray(pbTrf32); } - ProtectedBinary pbRet = new ProtectedBinary(true, pbTrf32); - MemUtil.ZeroByteArray(pbTrf32); - MemUtil.ZeroByteArray(pbRaw32); return pbRet; } + private sealed class CkGkTaskInfo + { + public volatile ProtectedBinary Key = null; + public volatile string Error = null; + } + + internal ProtectedBinary GenerateKey32Ex(KdfParameters p, IStatusLogger sl) + { + if(sl == null) return GenerateKey32(p); + + CkGkTaskInfo ti = new CkGkTaskInfo(); + + ThreadStart f = delegate() + { + if(ti == null) { Debug.Assert(false); return; } + + try { ti.Key = GenerateKey32(p); } + catch(ThreadAbortException exAbort) + { + ti.Error = ((exAbort != null) ? exAbort.Message : null); + Thread.ResetAbort(); + } + catch(Exception ex) + { + Debug.Assert(false); + ti.Error = ((ex != null) ? ex.Message : null); + } + }; + + Thread th = new Thread(f); + th.Start(); + + Debug.Assert(PwDefs.UIUpdateDelay >= 2); + while(!th.Join(PwDefs.UIUpdateDelay / 2)) + { + if(!sl.ContinueWork()) + { + try { th.Abort(); } + catch(Exception) { Debug.Assert(false); } + + throw new OperationCanceledException(); + } + } + + if(!string.IsNullOrEmpty(ti.Error)) throw new Exception(ti.Error); + + Debug.Assert(ti.Key != null); + return ti.Key; + } + private void ValidateUserKeys() { int nAccounts = 0; @@ -280,14 +345,11 @@ namespace ModernKeePassLib.Keys { get { - return KLRes.InvalidCompositeKey + MessageService.NewParagraph + - KLRes.InvalidCompositeKeyHint; + return (KLRes.InvalidCompositeKey + MessageService.NewParagraph + + KLRes.InvalidCompositeKeyHint); } } - /// - /// Construct a new invalid composite key exception. - /// public InvalidCompositeKeyException() { } diff --git a/ModernKeePassLib/Keys/KcpKeyFile.cs b/ModernKeePassLib/Keys/KcpKeyFile.cs index c62493a..9ec1d5c 100644 --- a/ModernKeePassLib/Keys/KcpKeyFile.cs +++ b/ModernKeePassLib/Keys/KcpKeyFile.cs @@ -67,14 +67,13 @@ namespace ModernKeePassLib.Keys { get { return m_pbKeyData; } } - #if ModernKeePassLib - public KcpKeyFile(StorageFile strKeyFile) - { - Construct(IOConnectionInfo.FromFile(strKeyFile), false); - } + public KcpKeyFile(StorageFile keyFile) + { + Construct(IOConnectionInfo.FromStorageFile(keyFile), false); + } #else - public KcpKeyFile(string strKeyFile) + public KcpKeyFile(string strKeyFile) { Construct(IOConnectionInfo.FromPath(strKeyFile), false); } @@ -183,19 +182,19 @@ namespace ModernKeePassLib.Keys return null; } - /// - /// Create a new, random key-file. - /// - /// Path where the key-file should be saved to. - /// If the file exists already, it will be overwritten. - /// Additional entropy used to generate - /// the random key. May be null (in this case only the KeePass-internal - /// random number generator is used). - /// Returns a FileSaveResult error code. + /// + /// Create a new, random key-file. + /// + /// Path where the key-file should be saved to. + /// If the file exists already, it will be overwritten. + /// Additional entropy used to generate + /// the random key. May be null (in this case only the KeePass-internal + /// random number generator is used). + /// Returns a FileSaveResult error code. #if ModernKeePassLib - public static void Create(StorageFile strFilePath, byte[] pbAdditionalEntropy) + public static void Create(StorageFile file, byte[] pbAdditionalEntropy) #else - public static void Create(string strFilePath, byte[] pbAdditionalEntropy) + public static void Create(string strFilePath, byte[] pbAdditionalEntropy) #endif { byte[] pbKey32 = CryptoRandom.Instance.GetRandomBytes(32); @@ -215,7 +214,11 @@ namespace ModernKeePassLib.Keys } } - CreateXmlKeyFile(strFilePath, pbFinalKey32); +#if ModernKeePassLib + CreateXmlKeyFile(file, pbFinalKey32); +#else + CreateXmlKeyFile(strFilePath, pbFinalKey32); +#endif } // ================================================================ @@ -276,19 +279,23 @@ namespace ModernKeePassLib.Keys return pbKeyData; } + #if ModernKeePassLib - private static void CreateXmlKeyFile(StorageFile strFile, byte[] pbKeyData) + private static void CreateXmlKeyFile(StorageFile file, byte[] pbKeyData) + { + Debug.Assert(file != null); + if (file == null) throw new ArgumentNullException(nameof(file)); #else - private static void CreateXmlKeyFile(string strFile, byte[] pbKeyData) -#endif + private static void CreateXmlKeyFile(string strFile, byte[] pbKeyData) { Debug.Assert(strFile != null); if(strFile == null) throw new ArgumentNullException("strFile"); +#endif Debug.Assert(pbKeyData != null); if(pbKeyData == null) throw new ArgumentNullException("pbKeyData"); #if ModernKeePassLib - IOConnectionInfo ioc = IOConnectionInfo.FromFile(strFile); + var ioc = IOConnectionInfo.FromStorageFile(file); #else IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile); #endif diff --git a/ModernKeePassLib/Keys/KcpPassword.cs b/ModernKeePassLib/Keys/KcpPassword.cs index b69bae4..0eb10ea 100644 --- a/ModernKeePassLib/Keys/KcpPassword.cs +++ b/ModernKeePassLib/Keys/KcpPassword.cs @@ -21,9 +21,9 @@ using System; using System.Diagnostics; using System.Text; +using ModernKeePassLib.Cryptography; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; -using ModernKeePassLib.Cryptography; namespace ModernKeePassLib.Keys { diff --git a/ModernKeePassLib/Keys/KeyProvider.cs b/ModernKeePassLib/Keys/KeyProvider.cs index 16a9f1d..87b04ef 100644 --- a/ModernKeePassLib/Keys/KeyProvider.cs +++ b/ModernKeePassLib/Keys/KeyProvider.cs @@ -145,7 +145,7 @@ namespace ModernKeePassLib.Keys public override byte[] GetKey(KeyProviderQueryContext ctx) { - return new byte[]{ 2, 3, 5, 7, 11, 13 }; + return new byte[] { 2, 3, 5, 7, 11, 13 }; } } #endif diff --git a/ModernKeePassLib/ModernKeePassLib.csproj b/ModernKeePassLib/ModernKeePassLib.csproj index 3ff2a23..83ad1df 100644 --- a/ModernKeePassLib/ModernKeePassLib.csproj +++ b/ModernKeePassLib/ModernKeePassLib.csproj @@ -3,14 +3,14 @@ netstandard2.0 true - 2.41.1 + 2.42.1 Geoffroy Bonneville https://www.gnu.org/licenses/gpl-3.0.en.html https://github.com/wismna/ModernKeePass Portable KeePass Password Management Library that targets .Net Standard and WinRT. Allows reading, editing and writing to KeePass 2.x databases. wismna ModernKeePassLib - Allow opening Storage File from FutureAccessList with a token + Update to version 2.42.1 KeePass KeePassLib Portable PCL NetStandard ModernKeePass Copyright © 2019 Geoffroy Bonneville diff --git a/ModernKeePassLib/Native/Native.PCL.cs b/ModernKeePassLib/Native/Native.PCL.cs index 8c7a312..815f49d 100644 --- a/ModernKeePassLib/Native/Native.PCL.cs +++ b/ModernKeePassLib/Native/Native.PCL.cs @@ -22,6 +22,12 @@ namespace ModernKeePassLib.Native { return Environment.OSVersion.Platform; } + + internal static string DecodeArgsToPath(string strApp) + { + if (!string.IsNullOrEmpty(strApp)) return strApp; + return string.Empty; + } } internal static class NativeMethods diff --git a/ModernKeePassLib/Native/NativeLib.cs b/ModernKeePassLib/Native/NativeLib.cs index a10f433..c3fbe15 100644 --- a/ModernKeePassLib/Native/NativeLib.cs +++ b/ModernKeePassLib/Native/NativeLib.cs @@ -234,16 +234,16 @@ namespace ModernKeePassLib.Native { ProcessStartInfo psi = new ProcessStartInfo(); + psi.FileName = EncodePath(strAppPath); + if(!string.IsNullOrEmpty(strParams)) psi.Arguments = strParams; + psi.CreateNoWindow = true; - psi.FileName = strAppPath; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = false; + psi.RedirectStandardOutput = bStdOut; - if(strStdInput != null) psi.RedirectStandardInput = true; - if(!string.IsNullOrEmpty(strParams)) psi.Arguments = strParams; - Process p = Process.Start(psi); pToDispose = p; @@ -291,7 +291,7 @@ namespace ModernKeePassLib.Native #if !ModernKeePassLib if((f & AppRunFlags.DoEvents) != AppRunFlags.None) { - List
lDisabledForms = new List(); + List lDisabledForms = new List(); if((f & AppRunFlags.DisableForms) != AppRunFlags.None) { foreach(Form form in Application.OpenForms) @@ -456,5 +456,76 @@ namespace ModernKeePassLib.Native // https://referencesource.microsoft.com/#mscorlib/system/runtime/interopservices/windowsruntime/winrtclassactivator.cs return Type.GetType(strType + ", Windows, ContentType=WindowsRuntime", false); } + + internal static string EncodeDataToArgs(string strData) + { + if(strData == null) { Debug.Assert(false); return string.Empty; } + + // Cf. EncodePath and DecodeArgsToPath + if(MonoWorkarounds.IsRequired(3471228285U) && IsUnix()) + { + string str = strData; + + str = str.Replace("\\", "\\\\"); + str = str.Replace("\"", "\\\""); + + // Whether '\'' needs to be encoded depends on the context + // (e.g. surrounding quotes); as we do not know what the + // caller does with the returned string, we assume that + // it will be used in a context where '\'' must not be + // encoded; this behavior is documented + // str = str.Replace("\'", "\\\'"); + + return str; + } + + // See SHELLEXECUTEINFO structure documentation + return strData.Replace("\"", "\"\"\""); + } + + /// + /// Encode a path for Process.Start. + /// + internal static string EncodePath(string strPath) + { + if(strPath == null) { Debug.Assert(false); return string.Empty; } + + // Cf. EncodeDataToArgs and DecodeArgsToPath + if(MonoWorkarounds.IsRequired(3471228285U) && IsUnix()) + { + string str = strPath; + + str = str.Replace("\\", "\\\\"); + str = str.Replace("\"", "\\\""); + + // '\'' must not be encoded in paths (only in args) + // str = str.Replace("\'", "\\\'"); + + return str; + } + + return strPath; // '\"' is not allowed in paths on Windows + } + + /// + /// Decode command line arguments to a path for Process.Start. + /// + internal static string DecodeArgsToPath(string strArgs) + { + if(strArgs == null) { Debug.Assert(false); return string.Empty; } + + string str = strArgs; + + // Cf. EncodeDataToArgs and EncodePath + // if(MonoWorkarounds.IsRequired(3471228285U) && IsUnix()) + // { + // string strPlh = Guid.NewGuid().ToString(); + // str = str.Replace("\\\\", strPlh); + // str = str.Replace("\\\'", "\'"); + // str = str.Replace(strPlh, "\\\\"); // Restore + // } + + return str; // '\"' is not allowed in paths on Windows + } } } diff --git a/ModernKeePassLib/Native/NativeMethods.cs b/ModernKeePassLib/Native/NativeMethods.cs index 6880a7a..69d21fd 100644 --- a/ModernKeePassLib/Native/NativeMethods.cs +++ b/ModernKeePassLib/Native/NativeMethods.cs @@ -39,6 +39,9 @@ namespace ModernKeePassLib.Native internal const uint FILE_SUPPORTS_TRANSACTIONS = 0x00200000; internal const int MAX_TRANSACTION_DESCRIPTION_LENGTH = 64; + internal static readonly Guid FOLDERID_SkyDrive = new Guid( + "A52BBA46-E9E1-435F-B3D9-28DAA648C0F6"); + // internal const uint TF_SFT_SHOWNORMAL = 0x00000001; // internal const uint TF_SFT_HIDDEN = 0x00000008; @@ -179,6 +182,10 @@ namespace ModernKeePassLib.Native string lpNewFileName, IntPtr lpProgressRoutine, IntPtr lpData, UInt32 dwFlags, IntPtr hTransaction); + [DllImport("Shell32.dll")] + private static extern int SHGetKnownFolderPath(ref Guid rfid, uint dwFlags, + IntPtr hToken, out IntPtr ppszPath); + #if (!KeePassLibSD && !KeePassUAP) [DllImport("ShlWApi.dll", CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] @@ -256,5 +263,29 @@ namespace ModernKeePassLib.Native return strRtDir; #endif } + + internal static string GetKnownFolderPath(Guid g) + { + if(Marshal.SystemDefaultCharSize != 2) { Debug.Assert(false); return string.Empty; } + + IntPtr pszPath = IntPtr.Zero; + try + { + if(SHGetKnownFolderPath(ref g, 0, IntPtr.Zero, out pszPath) == 0) + { + if(pszPath != IntPtr.Zero) + return Marshal.PtrToStringUni(pszPath); + else { Debug.Assert(false); } + } + } + catch(Exception) { Debug.Assert(false); } + finally + { + try { if(pszPath != IntPtr.Zero) Marshal.FreeCoTaskMem(pszPath); } + catch(Exception) { Debug.Assert(false); } + } + + return string.Empty; + } } } diff --git a/ModernKeePassLib/PwDatabase.cs b/ModernKeePassLib/PwDatabase.cs index 4e63824..6d9104e 100644 --- a/ModernKeePassLib/PwDatabase.cs +++ b/ModernKeePassLib/PwDatabase.cs @@ -21,10 +21,9 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Drawing; -#if ModernKeePassLib -using Windows.UI.Xaml.Controls; +#if !KeePassUAP +using System.Drawing; #endif using ModernKeePassLib.Collections; @@ -132,7 +131,7 @@ namespace ModernKeePassLib } /// - /// IOConnection of the currently opened database file. + /// IOConnection of the currently open database file. /// Is never null. /// public IOConnectionInfo IOConnectionInfo @@ -660,8 +659,8 @@ namespace ModernKeePassLib } /// - /// Save the currently opened database. The file is written to the location - /// it has been opened from. + /// Save the currently open database. The file is written to the + /// location it has been opened from. /// /// Logger that recieves status information. public void Save(IStatusLogger slLogger) @@ -695,16 +694,16 @@ namespace ModernKeePassLib } /// - /// Save the currently opened database to a different location. If + /// Save the currently open database to a different location. If /// is true, the specified /// location is made the default location for future saves /// using SaveDatabase. /// /// New location to serialize the database to. - /// If true, the new location is made the - /// standard location for the database. If false, a copy of the currently - /// opened database is saved to the specified location, but it isn't - /// made the default location (i.e. no lock files will be moved for + /// If true, the new location is made + /// the standard location for the database. If false, a copy of the + /// currently open database is saved to the specified location, but it + /// isn't made the default location (i.e. no lock files will be moved for /// example). /// Logger that recieves status information. public void SaveAs(IOConnectionInfo ioConnection, bool bIsPrimaryNow, @@ -736,8 +735,8 @@ namespace ModernKeePassLib } /// - /// Closes the currently opened database. No confirmation message is shown - /// before closing. Unsaved changes will be lost. + /// Closes the currently open database. No confirmation message + /// is shown before closing. Unsaved changes will be lost. /// public void Close() { @@ -801,6 +800,12 @@ namespace ModernKeePassLib pgNew.Uuid = pg.Uuid; pgNew.AssignProperties(pg, false, true); + if(!pgLocalContainer.CanAddGroup(pgNew)) + { + Debug.Assert(false); + pgLocalContainer = m_pgRootGroup; + pgLocalContainer.CheckCanAddGroup(pgNew); + } // pgLocalContainer.AddGroup(pgNew, true); InsertObjectAtBestPos(pgLocalContainer.Groups, pgNew, ppSrc); pgNew.ParentGroup = pgLocalContainer; @@ -1087,8 +1092,8 @@ namespace ModernKeePassLib if(pgLocal.IsContainedIn(pg)) continue; + if(!pgLocal.CanAddGroup(pg)) { Debug.Assert(false); continue; } pg.ParentGroup.Groups.Remove(pg); - // pgLocal.AddGroup(pg, true); InsertObjectAtBestPos(pgLocal.Groups, pg, ppSrc); pg.ParentGroup = pgLocal; @@ -1643,7 +1648,7 @@ namespace ModernKeePassLib return null; } -#elif !KeePassLibSD +#elif !KeePassLibSD && !ModernKeePassLib [Obsolete("Additionally specify the size.")] public Image GetCustomIcon(PwUuid pwIconId) { diff --git a/ModernKeePassLib/PwDefs.cs b/ModernKeePassLib/PwDefs.cs index 441e711..4683691 100644 --- a/ModernKeePassLib/PwDefs.cs +++ b/ModernKeePassLib/PwDefs.cs @@ -55,18 +55,18 @@ namespace ModernKeePassLib /// e.g. 2.19 = 0x02130000. /// It is highly recommended to use FileVersion64 instead. ///
- public static readonly uint Version32 = 0x02290000; + public static readonly uint Version32 = 0x022A0100; /// /// Version, encoded as 64-bit unsigned integer /// (component-wise, 16 bits per component). /// - public static readonly ulong FileVersion64 = 0x0002002900000000UL; + public static readonly ulong FileVersion64 = 0x0002002A00010000UL; /// /// Version, encoded as string. /// - public static readonly string VersionString = "2.41"; + public static readonly string VersionString = "2.42.1"; public static readonly string Copyright = @"Copyright © 2003-2019 Dominik Reichl"; @@ -181,6 +181,12 @@ namespace ModernKeePassLib /// public static readonly string DefaultAutoTypeSequenceTan = @"{PASSWORD}"; + /// + /// Maximum time (in milliseconds) after which the user interface + /// should be updated. + /// + internal const int UIUpdateDelay = 50; + /// /// Check if a name is a standard field name. /// diff --git a/ModernKeePassLib/PwEnums.cs b/ModernKeePassLib/PwEnums.cs index d817303..0dab7c5 100644 --- a/ModernKeePassLib/PwEnums.cs +++ b/ModernKeePassLib/PwEnums.cs @@ -61,7 +61,7 @@ namespace ModernKeePassLib } /// - /// Methods for merging password databases/entries. + /// Methods for merging databases/entries. /// public enum PwMergeMethod { diff --git a/ModernKeePassLib/PwGroup.cs b/ModernKeePassLib/PwGroup.cs index 6a58611..6b089ef 100644 --- a/ModernKeePassLib/PwGroup.cs +++ b/ModernKeePassLib/PwGroup.cs @@ -25,19 +25,24 @@ using System.Text.RegularExpressions; using ModernKeePassLib.Collections; using ModernKeePassLib.Delegates; using ModernKeePassLib.Interfaces; +using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; namespace ModernKeePassLib { /// - /// A group containing several password entries. + /// A group containing subgroups and entries. /// public sealed class PwGroup : ITimeLogger, IStructureItem, IDeepCloneable { public const bool DefaultAutoTypeEnabled = true; public const bool DefaultSearchingEnabled = true; + // In the tree view of Windows 10, the X coordinate is reset + // to 0 after 256 nested nodes + private const uint MaxDepth = 126; // Depth 126 = level 127 < 256/2 + private PwObjectList m_listGroups = new PwObjectList(); private PwObjectList m_listEntries = new PwObjectList(); private PwGroup m_pParentGroup = null; @@ -139,7 +144,8 @@ namespace ModernKeePassLib { get { return m_pParentGroup; } - // Plugins: use PwGroup.AddGroup instead. + // Plugins: use the PwGroup.AddGroup method instead. + // Internal: check depth using CanAddGroup/CheckCanAddGroup. internal set { Debug.Assert(value != this); m_pParentGroup = value; } } @@ -1244,7 +1250,7 @@ namespace ModernKeePassLib PwGroup pg = m_pParentGroup; while(pg != null) { - if((!bIncludeTopMostGroup) && (pg.m_pParentGroup == null)) + if(!bIncludeTopMostGroup && (pg.m_pParentGroup == null)) break; strPath = pg.Name + strSeparator + strPath; @@ -1367,21 +1373,34 @@ namespace ModernKeePassLib #endif /// - /// Get the level of the group (i.e. the number of parent groups). + /// Get the depth of this group (i.e. the number of ancestors). /// - /// Number of parent groups. - public uint GetLevel() + /// Depth of this group. + public uint GetDepth() { PwGroup pg = m_pParentGroup; - uint uLevel = 0; + uint d = 0; while(pg != null) { - pg = pg.ParentGroup; - ++uLevel; + pg = pg.m_pParentGroup; + ++d; } - return uLevel; + return d; + } + + private uint GetHeight() + { + if(m_listGroups.UCount == 0) return 0; + + uint h = 0; + foreach(PwGroup pgSub in m_listGroups) + { + h = Math.Max(h, pgSub.GetHeight()); + } + + return (h + 1); } public string GetAutoTypeSequenceInherited() @@ -1520,13 +1539,31 @@ namespace ModernKeePassLib { if(subGroup == null) throw new ArgumentNullException("subGroup"); + CheckCanAddGroup(subGroup); m_listGroups.Add(subGroup); - if(bTakeOwnership) subGroup.m_pParentGroup = this; + if(bTakeOwnership) subGroup.ParentGroup = this; if(bUpdateLocationChangedOfSub) subGroup.LocationChanged = DateTime.UtcNow; } + internal bool CanAddGroup(PwGroup pgSub) + { + if(pgSub == null) { Debug.Assert(false); return false; } + + uint dCur = GetDepth(), hSub = pgSub.GetHeight(); + return ((dCur + hSub + 1) <= MaxDepth); + } + + internal void CheckCanAddGroup(PwGroup pgSub) + { + if(!CanAddGroup(pgSub)) + { + Debug.Assert(false); + throw new InvalidOperationException(KLRes.StructsTooDeep); + } + } + /// /// Add an entry to this group. /// diff --git a/ModernKeePassLib/Resources/KLRes.Generated.cs b/ModernKeePassLib/Resources/KLRes.Generated.cs index 007d875..3e4a6c2 100644 --- a/ModernKeePassLib/Resources/KLRes.Generated.cs +++ b/ModernKeePassLib/Resources/KLRes.Generated.cs @@ -59,6 +59,7 @@ namespace ModernKeePassLib.Resources m_strOldFormat = TryGetEx(dictNew, "OldFormat", m_strOldFormat); m_strPassive = TryGetEx(dictNew, "Passive", m_strPassive); m_strPreAuth = TryGetEx(dictNew, "PreAuth", m_strPreAuth); + m_strStructsTooDeep = TryGetEx(dictNew, "StructsTooDeep", m_strStructsTooDeep); m_strTimeout = TryGetEx(dictNew, "Timeout", m_strTimeout); m_strTryAgainSecs = TryGetEx(dictNew, "TryAgainSecs", m_strTryAgainSecs); m_strUnknownHeaderId = TryGetEx(dictNew, "UnknownHeaderId", m_strUnknownHeaderId); @@ -101,6 +102,7 @@ namespace ModernKeePassLib.Resources "OldFormat", "Passive", "PreAuth", + "StructsTooDeep", "Timeout", "TryAgainSecs", "UnknownHeaderId", @@ -346,10 +348,10 @@ namespace ModernKeePassLib.Resources } private static string m_strFrameworkNotImplExcp = - @"The .NET framework/runtime under which KeePass is currently running does not support this operation."; + @"The .NET Framework/runtime under which KeePass is currently running does not support this operation."; /// /// Look up a localized string similar to - /// 'The .NET framework/runtime under which KeePass is currently running does not support this operation.'. + /// 'The .NET Framework/runtime under which KeePass is currently running does not support this operation.'. /// public static string FrameworkNotImplExcp { @@ -477,6 +479,17 @@ namespace ModernKeePassLib.Resources get { return m_strPreAuth; } } + private static string m_strStructsTooDeep = + @"Structures are nested too deeply."; + /// + /// Look up a localized string similar to + /// 'Structures are nested too deeply.'. + /// + public static string StructsTooDeep + { + get { return m_strStructsTooDeep; } + } + private static string m_strTimeout = @"Timeout"; /// diff --git a/ModernKeePassLib/Resources/KSRes.Generated.cs b/ModernKeePassLib/Resources/KSRes.Generated.cs index 7b49547..298a2ce 100644 --- a/ModernKeePassLib/Resources/KSRes.Generated.cs +++ b/ModernKeePassLib/Resources/KSRes.Generated.cs @@ -9,7 +9,7 @@ namespace ModernKeePassLib.Resources /// /// A strongly-typed resource class, for looking up localized strings, etc. /// - public static class KSRes + public static partial class KSRes { private static string TryGetEx(Dictionary dictNew, string strName, string strDefault) diff --git a/ModernKeePassLib/Serialization/FileTransactionEx.cs b/ModernKeePassLib/Serialization/FileTransactionEx.cs index 01be96c..0f57384 100644 --- a/ModernKeePassLib/Serialization/FileTransactionEx.cs +++ b/ModernKeePassLib/Serialization/FileTransactionEx.cs @@ -23,18 +23,19 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Text; - +using Windows.Storage.AccessCache; #if (!ModernKeePassLib && !KeePassLibSD && !KeePassRT) using System.Security.AccessControl; #endif - -using ModernKeePassLib.Native; -using ModernKeePassLib.Utility; -using System.Threading.Tasks; +#if ModernKeePassLib using Windows.Storage; -using Windows.Storage.Streams; +#endif + using ModernKeePassLib.Cryptography; +using ModernKeePassLib.Delegates; +using ModernKeePassLib.Native; using ModernKeePassLib.Resources; +using ModernKeePassLib.Utility; namespace ModernKeePassLib.Serialization { @@ -210,10 +211,10 @@ namespace ModernKeePassLib.Serialization // trying to set 'Owner' or 'Group' can result in an // UnauthorizedAccessException; thus we restore 'Access' (DACL) only const AccessControlSections acs = AccessControlSections.Access; - #endif bool bEfsEncrypted = false; byte[] pbSec = null; + DateTime? otCreation = null; bool bBaseExists = IOConnection.FileExists(m_iocBase); @@ -228,9 +229,7 @@ namespace ModernKeePassLib.Serialization try { if(bEfsEncrypted) File.Decrypt(m_iocBase.Path); } // For TxF catch(Exception) { Debug.Assert(false); } #endif -#if ModernKeePassLib - otCreation = m_iocBase.StorageFile.DateCreated.UtcDateTime; -#else +#if !ModernKeePassLib otCreation = File.GetCreationTimeUtc(m_iocBase.Path); #endif #if !ModernKeePassLib @@ -238,7 +237,7 @@ namespace ModernKeePassLib.Serialization FileSecurity sec = File.GetAccessControl(m_iocBase.Path, acs); if(sec != null) pbSec = sec.GetSecurityDescriptorBinaryForm(); #endif - } + } catch(Exception) { Debug.Assert(NativeLib.IsUnix()); } // if((long)(faBase & FileAttributes.ReadOnly) != 0) @@ -337,6 +336,7 @@ namespace ModernKeePassLib.Serialization { if(NativeLib.IsUnix()) return; if(!m_iocBase.IsLocalFile()) return; + if(IsOneDriveWorkaroundRequired()) return; string strID = StrUtil.AlphaNumericOnly(Convert.ToBase64String( CryptoRandom.Instance.GetRandomBytes(16))); @@ -354,7 +354,7 @@ namespace ModernKeePassLib.Serialization #if ModernKeePassLib var tempFile = ApplicationData.Current.TemporaryFolder.CreateFileAsync(m_iocTemp.Path).GetAwaiter() .GetResult(); - m_iocTemp = IOConnectionInfo.FromFile(tempFile); + m_iocTemp = IOConnectionInfo.FromStorageFile(tempFile); #else m_iocTemp = IOConnectionInfo.FromPath(strTemp); #endif @@ -370,13 +370,10 @@ namespace ModernKeePassLib.Serialization if(TxfMoveWithTx()) return true; - // Move the temporary file onto the base file's drive first, - // such that it cannot happen that both the base file and - // the temporary file are deleted/corrupted -#if ModernKeePassLib - m_iocTemp.StorageFile = ApplicationData.Current.TemporaryFolder.CreateFileAsync(m_iocTemp.Path).GetAwaiter() - .GetResult(); -#else + // Move the temporary file onto the base file's drive first, + // such that it cannot happen that both the base file and + // the temporary file are deleted/corrupted +#if !ModernKeePassLib const uint f = (NativeMethods.MOVEFILE_COPY_ALLOWED | NativeMethods.MOVEFILE_REPLACE_EXISTING); bool b = NativeMethods.MoveFileEx(m_iocTemp.Path, m_iocTxfMidFallback.Path, f); @@ -391,9 +388,7 @@ namespace ModernKeePassLib.Serialization private bool TxfMoveWithTx() { -#if ModernKeePassLib - return true; -#else +#if !ModernKeePassLib IntPtr hTx = new IntPtr((int)NativeMethods.INVALID_HANDLE_VALUE); Debug.Assert(hTx.ToInt64() == NativeMethods.INVALID_HANDLE_VALUE); try @@ -438,9 +433,8 @@ namespace ModernKeePassLib.Serialization catch(Exception) { Debug.Assert(false); } } } - - return false; #endif + return false; } internal static void ClearOld() @@ -470,5 +464,89 @@ namespace ModernKeePassLib.Serialization } catch(Exception) { Debug.Assert(false); } } + + // https://sourceforge.net/p/keepass/discussion/329220/thread/672ffecc65/ + // https://sourceforge.net/p/keepass/discussion/329221/thread/514786c23a/ + private bool IsOneDriveWorkaroundRequired() + { +#if !ModernKeePassLib + if(NativeLib.IsUnix()) return false; + + try + { + string strReleaseId = (Registry.GetValue( + "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", + "ReleaseId", string.Empty) as string); + if(strReleaseId != "1809") return false; + + string strFile = m_iocBase.Path; + + GFunc fMatch = delegate(string strRoot, string strSfx) + { + if(string.IsNullOrEmpty(strRoot)) return false; + string strPfx = UrlUtil.EnsureTerminatingSeparator( + strRoot, false) + strSfx; + return strFile.StartsWith(strPfx, StrUtil.CaseIgnoreCmp); + }; + GFunc fMatchEnv = delegate(string strEnv, string strSfx) + { + return fMatch(Environment.GetEnvironmentVariable(strEnv), strSfx); + }; + + string strKnown = NativeMethods.GetKnownFolderPath( + NativeMethods.FOLDERID_SkyDrive); + if(fMatch(strKnown, string.Empty)) return true; + + if(fMatchEnv("USERPROFILE", "OneDrive\\")) return true; + if(fMatchEnv("OneDrive", string.Empty)) return true; + if(fMatchEnv("OneDriveCommercial", string.Empty)) return true; + if(fMatchEnv("OneDriveConsumer", string.Empty)) return true; + + using(RegistryKey kAccs = Registry.CurrentUser.OpenSubKey( + "Software\\Microsoft\\OneDrive\\Accounts", false)) + { + string[] vAccs = (((kAccs != null) ? kAccs.GetSubKeyNames() : + null) ?? new string[0]); + + foreach(string strAcc in vAccs) + { + if(string.IsNullOrEmpty(strAcc)) { Debug.Assert(false); continue; } + + using(RegistryKey kTenants = kAccs.OpenSubKey( + strAcc + "\\Tenants", false)) + { + string[] vTenants = (((kTenants != null) ? + kTenants.GetSubKeyNames() : null) ?? new string[0]); + + foreach(string strT in vTenants) + { + if(string.IsNullOrEmpty(strT)) { Debug.Assert(false); continue; } + + using(RegistryKey kT = kTenants.OpenSubKey(strT, false)) + { + string[] vPaths = (((kT != null) ? + kT.GetValueNames() : null) ?? new string[0]); + + foreach(string strPath in vPaths) + { + if((strPath == null) || (strPath.Length < 4) || + (strPath[1] != ':')) + { + Debug.Assert(false); + continue; + } + + if(fMatch(strPath, string.Empty)) return true; + } + } + } + } + } + } + } + catch(Exception) { Debug.Assert(false); } +#endif + return false; + } } } diff --git a/ModernKeePassLib/Serialization/IOConnection.cs b/ModernKeePassLib/Serialization/IOConnection.cs index 7d20259..35b64cc 100644 --- a/ModernKeePassLib/Serialization/IOConnection.cs +++ b/ModernKeePassLib/Serialization/IOConnection.cs @@ -24,7 +24,6 @@ using System.IO; using System.Net; using System.Reflection; using System.Text; - #if (!ModernKeePassLib && !KeePassLibSD && !KeePassUAP) using System.Net.Cache; using System.Net.Security; @@ -600,7 +599,12 @@ namespace ModernKeePassLib.Serialization private static Stream OpenReadLocal(IOConnectionInfo ioc) { +#if ModernKeePassLib return ioc.StorageFile.OpenAsync(FileAccessMode.Read).GetAwaiter().GetResult().AsStream(); +#else + return new FileStream(ioc.Path, FileMode.Open, FileAccess.Read, + FileShare.Read); +#endif } #if (!ModernKeePassLib && !KeePassLibSD && !KeePassRT) @@ -654,7 +658,7 @@ namespace ModernKeePassLib.Serialization RaiseIOAccessPreEvent(ioc, IOAccessType.Exists); #if ModernKeePassLib - return ioc.StorageFile.IsAvailable; + return ioc.StorageFile != null; #else if(ioc.IsLocalFile()) return File.Exists(ioc.Path); @@ -696,7 +700,7 @@ namespace ModernKeePassLib.Serialization #if ModernKeePassLib if (!ioc.IsLocalFile()) return; - ioc.StorageFile?.DeleteAsync().GetAwaiter().GetResult(); + ioc.StorageFile?.DeleteAsync().GetAwaiter().GetResult(); #else if(ioc.IsLocalFile()) { File.Delete(ioc.Path); return; } @@ -718,7 +722,7 @@ namespace ModernKeePassLib.Serialization } #endif #endif - } + } /// /// Rename/move a file. For local file system and WebDAV, the @@ -735,7 +739,7 @@ namespace ModernKeePassLib.Serialization #if ModernKeePassLib if (!iocFrom.IsLocalFile()) return; - iocFrom.StorageFile?.RenameAsync(iocTo.Path).GetAwaiter().GetResult(); + iocFrom.StorageFile?.RenameAsync(iocTo.Path).GetAwaiter().GetResult(); #else if(iocFrom.IsLocalFile()) { File.Move(iocFrom.Path, iocTo.Path); return; } diff --git a/ModernKeePassLib/Serialization/IOConnectionInfo.cs b/ModernKeePassLib/Serialization/IOConnectionInfo.cs index 8148907..c432f31 100644 --- a/ModernKeePassLib/Serialization/IOConnectionInfo.cs +++ b/ModernKeePassLib/Serialization/IOConnectionInfo.cs @@ -25,14 +25,12 @@ using System.IO; using System.Text; using System.Xml.Serialization; #if ModernKeePassLib +using System.Threading.Tasks; using Windows.Storage; -//using PCLStorage; #endif using ModernKeePassLib.Interfaces; using ModernKeePassLib.Utility; -using System.Threading.Tasks; -using Windows.Storage.AccessCache; namespace ModernKeePassLib.Serialization { @@ -70,6 +68,8 @@ namespace ModernKeePassLib.Serialization { // private IOFileFormatHint m_ioHint = IOFileFormatHint.None; + public StorageFile StorageFile { get; set; } + private string m_strUrl = string.Empty; public string Path { @@ -316,29 +316,17 @@ namespace ModernKeePassLib.Serialization } #if ModernKeePassLib - public static IOConnectionInfo FromFile(StorageFile file) + public static IOConnectionInfo FromStorageFile(StorageFile file) { IOConnectionInfo ioc = new IOConnectionInfo(); - ioc.StorageFile = file; - ioc.Path = file.Path; - ioc.CredSaveMode = IOCredSaveMode.NoSave; - - return ioc; - } - - public static IOConnectionInfo FromPath(string strPath) - { - IOConnectionInfo ioc = new IOConnectionInfo(); - - ioc.Path = strPath; - ioc.StorageFile = StorageApplicationPermissions.FutureAccessList.GetFileAsync(strPath).GetAwaiter().GetResult(); + ioc.StorageFile = file; ioc.CredSaveMode = IOCredSaveMode.NoSave; return ioc; } #else - public static IOConnectionInfo FromPath(string strPath) + public static IOConnectionInfo FromPath(string strPath) { IOConnectionInfo ioc = new IOConnectionInfo(); @@ -348,10 +336,6 @@ namespace ModernKeePassLib.Serialization return ioc; } #endif - - - public StorageFile StorageFile { get; set; } - public bool CanProbablyAccess() { #if ModernKeePassLib diff --git a/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs b/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs index 4ab2046..f0ffd31 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs @@ -30,13 +30,14 @@ using System.Drawing; using ModernKeePassLib; using ModernKeePassLib.Collections; -using ModernKeePassLib.Cryptography; -using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Interfaces; using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; +using ModernKeePassLib.Cryptography; +using ModernKeePassLib.Cryptography.Cipher; + namespace ModernKeePassLib.Serialization { /// @@ -97,36 +98,10 @@ namespace ModernKeePassLib.Serialization private void ReadXmlStreamed(Stream sXml, Stream sParent) { - ReadDocumentStreamed(CreateXmlReader(sXml), sParent); - } - - internal static XmlReaderSettings CreateStdXmlReaderSettings() - { - XmlReaderSettings xrs = new XmlReaderSettings(); - - xrs.CloseInput = true; - xrs.IgnoreComments = true; - xrs.IgnoreProcessingInstructions = true; - xrs.IgnoreWhitespace = true; - -#if ModernKeePassLib || KeePassUAP - xrs.DtdProcessing = DtdProcessing.Prohibit; -#else -#if !KeePassLibSD - // Also see PrepMonoDev.sh script - xrs.ProhibitDtd = true; // Obsolete in .NET 4, but still there - // xrs.DtdProcessing = DtdProcessing.Prohibit; // .NET 4 only -#endif - xrs.ValidationType = ValidationType.None; -#endif - - return xrs; - } - - private static XmlReader CreateXmlReader(Stream readerStream) - { - XmlReaderSettings xrs = CreateStdXmlReaderSettings(); - return XmlReader.Create(readerStream, xrs); + using(XmlReader xr = XmlUtilEx.CreateXmlReader(sXml)) + { + ReadDocumentStreamed(xr, sParent); + } } private void ReadDocumentStreamed(XmlReader xr, Stream sParentStream) @@ -179,7 +154,7 @@ namespace ModernKeePassLib.Serialization } ++uTagCounter; - if(((uTagCounter % 256) == 0) && bSupportsStatus) + if(((uTagCounter & 0xFFU) == 0) && bSupportsStatus) { Debug.Assert(lStreamLength == sParentStream.Length); uint uPct = (uint)((sParentStream.Position * 100) / @@ -189,7 +164,8 @@ namespace ModernKeePassLib.Serialization // position/length values (M120413) if(uPct > 100) { Debug.Assert(false); uPct = 100; } - m_slLogger.SetProgress(uPct); + if(!m_slLogger.SetProgress(uPct)) + throw new OperationCanceledException(); } } @@ -1028,28 +1004,31 @@ namespace ModernKeePassLib.Serialization private void ReadUnknown(XmlReader xr) { Debug.Assert(false); // Unknown node! + Debug.Assert(xr.NodeType == XmlNodeType.Element); - if(xr.IsEmptyElement) { m_bReadNextNode = true; return; } + bool bRead = false; + int cOpen = 0; - string strUnknownName = xr.Name; - - XorredBuffer xb = ProcessNode(xr); - if(xb != null) { xb.Dispose(); return; } // ProcessNode sets m_bReadNextNode - - bool bRead = true; - while(true) + do { if(bRead) xr.Read(); + bRead = true; - if(xr.NodeType == XmlNodeType.EndElement) break; - if(xr.NodeType != XmlNodeType.Element) { bRead = true; continue; } + if(xr.NodeType == XmlNodeType.EndElement) --cOpen; + else if(xr.NodeType == XmlNodeType.Element) + { + if(!xr.IsEmptyElement) + { + XorredBuffer xb = ProcessNode(xr); + if(xb != null) { xb.Dispose(); bRead = m_bReadNextNode; continue; } - ReadUnknown(xr); - bRead = m_bReadNextNode; + ++cOpen; + } + } } + while(cOpen > 0); - Debug.Assert(xr.Name == strUnknownName); // On end tag - m_bReadNextNode = true; + m_bReadNextNode = bRead; } private XorredBuffer ProcessNode(XmlReader xr) diff --git a/ModernKeePassLib/Serialization/KdbxFile.Read.cs b/ModernKeePassLib/Serialization/KdbxFile.Read.cs index a7a9733..2f3921f 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.Read.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.Read.cs @@ -55,26 +55,23 @@ namespace ModernKeePassLib.Serialization /// public sealed partial class KdbxFile { + /// + /// Load a KDBX file. + /// + /// File to load. + /// Format. + /// Status logger (optional). #if ModernKeePassLib public void Load(StorageFile file, KdbxFormat fmt, IStatusLogger slLogger) - { - IOConnectionInfo ioc = IOConnectionInfo.FromFile(file); - Load(IOConnection.OpenRead(ioc), fmt, slLogger); - } - + { + IOConnectionInfo ioc = IOConnectionInfo.FromStorageFile(file); #else - /// - /// Load a KDBX file. - /// - /// File to load. - /// Format. - /// Status logger (optional). - public void Load(string strFilePath, KdbxFormat fmt, IStatusLogger slLogger) + public void Load(string strFilePath, KdbxFormat fmt, IStatusLogger slLogger) { IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFilePath); +#endif Load(IOConnection.OpenRead(ioc), fmt, slLogger); } -#endif /// /// Load a KDBX file from a stream. @@ -202,19 +199,17 @@ namespace ModernKeePassLib.Serialization } #if KeePassDebug_WriteXml - // FileStream fsOut = new FileStream("Raw.xml", FileMode.Create, - // FileAccess.Write, FileShare.None); - // try - // { - // while(true) - // { - // int b = sXml.ReadByte(); - // if(b == -1) break; - // fsOut.WriteByte((byte)b); - // } - // } - // catch(Exception) { } - // fsOut.Close(); +#warning XML output is enabled! + /* using(FileStream fsOut = new FileStream("Raw.xml", FileMode.Create, + FileAccess.Write, FileShare.None)) + { + while(true) + { + int b = sXml.ReadByte(); + if(b == -1) throw new EndOfStreamException(); + fsOut.WriteByte((byte)b); + } + } */ #endif ReadXmlStreamed(sXml, sHashing); @@ -413,7 +408,7 @@ namespace ModernKeePassLib.Serialization default: Debug.Assert(false); if(m_slLogger != null) - m_slLogger.SetText(KLRes.UnknownHeaderId + @": " + + m_slLogger.SetText(KLRes.UnknownHeaderId + ": " + kdbID.ToString() + "!", LogStatusType.Warning); break; } diff --git a/ModernKeePassLib/Serialization/KdbxFile.Write.cs b/ModernKeePassLib/Serialization/KdbxFile.Write.cs index ffbd6aa..d4a6226 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.Write.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.Write.cs @@ -429,7 +429,7 @@ namespace ModernKeePassLib.Serialization }; if(!pgRoot.TraverseTree(TraversalMethod.PreOrder, gh, eh)) - throw new InvalidOperationException(); + throw new OperationCanceledException(); while(groupStack.Count > 1) { diff --git a/ModernKeePassLib/Serialization/KdbxFile.cs b/ModernKeePassLib/Serialization/KdbxFile.cs index 190830c..55a01c7 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.cs @@ -378,8 +378,8 @@ namespace ModernKeePassLib.Serialization Debug.Assert(m_pwDatabase != null); Debug.Assert(m_pwDatabase.MasterKey != null); - ProtectedBinary pbinUser = m_pwDatabase.MasterKey.GenerateKey32( - m_pwDatabase.KdfParameters); + ProtectedBinary pbinUser = m_pwDatabase.MasterKey.GenerateKey32Ex( + m_pwDatabase.KdfParameters, m_slLogger); Debug.Assert(pbinUser != null); if(pbinUser == null) throw new SecurityException(KLRes.InvalidCompositeKey); @@ -492,7 +492,7 @@ namespace ModernKeePassLib.Serialization { if(pb == null) { Debug.Assert(false); return; } - if(string.IsNullOrEmpty(strName)) strName = "File.bin"; + strName = UrlUtil.GetSafeFileName(strName); string strPath; int iTry = 1; @@ -500,8 +500,8 @@ namespace ModernKeePassLib.Serialization { strPath = UrlUtil.EnsureTerminatingSeparator(strSaveDir, false); - string strExt = UrlUtil.GetExtension(strName); string strDesc = UrlUtil.StripExtension(strName); + string strExt = UrlUtil.GetExtension(strName); strPath += strDesc; if(iTry > 1) diff --git a/ModernKeePassLib/Translation/KPTranslation.cs b/ModernKeePassLib/Translation/KPTranslation.cs index a02b7d7..8aee9f1 100644 --- a/ModernKeePassLib/Translation/KPTranslation.cs +++ b/ModernKeePassLib/Translation/KPTranslation.cs @@ -46,6 +46,7 @@ namespace ModernKeePassLib.Translation public sealed class KPTranslation { public static readonly string FileExtension = "lngx"; + internal const string FileExtension1x = "lng"; private KPTranslationProperties m_props = new KPTranslationProperties(); public KPTranslationProperties Properties diff --git a/ModernKeePassLib/Utility/MemUtil.cs b/ModernKeePassLib/Utility/MemUtil.cs index adbe85b..995373b 100644 --- a/ModernKeePassLib/Utility/MemUtil.cs +++ b/ModernKeePassLib/Utility/MemUtil.cs @@ -264,8 +264,7 @@ namespace ModernKeePassLib.Utility [MethodImpl(MioNoOptimize)] public static void ZeroByteArray(byte[] pbArray) { - Debug.Assert(pbArray != null); - if(pbArray == null) throw new ArgumentNullException("pbArray"); + if(pbArray == null) { Debug.Assert(false); return; } Array.Clear(pbArray, 0, pbArray.Length); } @@ -277,7 +276,7 @@ namespace ModernKeePassLib.Utility [MethodImpl(MioNoOptimize)] public static void ZeroArray(T[] v) { - if(v == null) { Debug.Assert(false); throw new ArgumentNullException("v"); } + if(v == null) { Debug.Assert(false); return; } Array.Clear(v, 0, v.Length); } diff --git a/ModernKeePassLib/Utility/MonoWorkarounds.cs b/ModernKeePassLib/Utility/MonoWorkarounds.cs index b26e328..a960d05 100644 --- a/ModernKeePassLib/Utility/MonoWorkarounds.cs +++ b/ModernKeePassLib/Utility/MonoWorkarounds.cs @@ -116,9 +116,6 @@ namespace ModernKeePassLib.Utility // 1760: // Input focus is not restored when activating a form. // https://sourceforge.net/p/keepass/bugs/1760/ - // 2139: - // Shortcut keys are ignored. - // https://sourceforge.net/p/keepass/feature-requests/2139/ // 2140: // Explicit control focusing is ignored. // https://sourceforge.net/p/keepass/feature-requests/2140/ diff --git a/ModernKeePassLib/Utility/StrUtil.cs b/ModernKeePassLib/Utility/StrUtil.cs index caa06bc..4cfc02f 100644 --- a/ModernKeePassLib/Utility/StrUtil.cs +++ b/ModernKeePassLib/Utility/StrUtil.cs @@ -24,7 +24,6 @@ using System.Diagnostics; using System.Globalization; using System.IO; using System.Text; -using System.Text.RegularExpressions; #if !KeePassUAP using System.Drawing; @@ -44,45 +43,41 @@ namespace ModernKeePassLib.Utility /// public sealed class CharStream { - private string m_strString = string.Empty; - private int m_nPos = 0; + private readonly string m_str; + private int m_iPos = 0; + + public long Position + { + get { return m_iPos; } + set + { + if((value < 0) || (value > int.MaxValue)) + throw new ArgumentOutOfRangeException("value"); + m_iPos = (int)value; + } + } public CharStream(string str) { - Debug.Assert(str != null); - if(str == null) throw new ArgumentNullException("str"); + if(str == null) { Debug.Assert(false); throw new ArgumentNullException("str"); } - m_strString = str; - } - - public void Seek(SeekOrigin org, int nSeek) - { - if(org == SeekOrigin.Begin) - m_nPos = nSeek; - else if(org == SeekOrigin.Current) - m_nPos += nSeek; - else if(org == SeekOrigin.End) - m_nPos = m_strString.Length + nSeek; + m_str = str; } public char ReadChar() { - if(m_nPos < 0) return char.MinValue; - if(m_nPos >= m_strString.Length) return char.MinValue; + if(m_iPos >= m_str.Length) return char.MinValue; - char chRet = m_strString[m_nPos]; - ++m_nPos; - return chRet; + return m_str[m_iPos++]; } public char ReadChar(bool bSkipWhiteSpace) { - if(bSkipWhiteSpace == false) return ReadChar(); + if(!bSkipWhiteSpace) return ReadChar(); while(true) { char ch = ReadChar(); - if((ch != ' ') && (ch != '\t') && (ch != '\r') && (ch != '\n')) return ch; } @@ -90,29 +85,25 @@ namespace ModernKeePassLib.Utility public char PeekChar() { - if(m_nPos < 0) return char.MinValue; - if(m_nPos >= m_strString.Length) return char.MinValue; + if(m_iPos >= m_str.Length) return char.MinValue; - return m_strString[m_nPos]; + return m_str[m_iPos]; } public char PeekChar(bool bSkipWhiteSpace) { - if(bSkipWhiteSpace == false) return PeekChar(); + if(!bSkipWhiteSpace) return PeekChar(); - int iIndex = m_nPos; - while(true) + int i = m_iPos; + while(i < m_str.Length) { - if(iIndex < 0) return char.MinValue; - if(iIndex >= m_strString.Length) return char.MinValue; - - char ch = m_strString[iIndex]; - + char ch = m_str[i]; if((ch != ' ') && (ch != '\t') && (ch != '\r') && (ch != '\n')) return ch; - - ++iIndex; + ++i; } + + return char.MinValue; } } @@ -405,44 +396,46 @@ namespace ModernKeePassLib.Utility return str; } - /// - /// Split up a command line into application and argument. - /// - /// Command line to split. - /// Application path. - /// Arguments. - public static void SplitCommandLine(string strCmdLine, out string strApp, out string strArgs) + public static void SplitCommandLine(string strCmdLine, out string strApp, + out string strArgs) { - Debug.Assert(strCmdLine != null); if(strCmdLine == null) throw new ArgumentNullException("strCmdLine"); + SplitCommandLine(strCmdLine, out strApp, out strArgs, true); + } + + internal static void SplitCommandLine(string strCmdLine, out string strApp, + out string strArgs, bool bDecodeAppToPath) + { + if(strCmdLine == null) { Debug.Assert(false); throw new ArgumentNullException("strCmdLine"); } string str = strCmdLine.Trim(); - - strApp = null; strArgs = null; + strApp = null; + strArgs = null; if(str.StartsWith("\"")) { - int nSecond = str.IndexOf('\"', 1); - if(nSecond >= 1) + int iSecond = UrlUtil.IndexOfSecondEnclQuote(str); + if(iSecond >= 1) { - strApp = str.Substring(1, nSecond - 1).Trim(); - strArgs = str.Remove(0, nSecond + 1).Trim(); + strApp = str.Substring(1, iSecond - 1).Trim(); + strArgs = str.Remove(0, iSecond + 1).Trim(); } } if(strApp == null) { - int nSpace = str.IndexOf(' '); - - if(nSpace >= 0) + int iSpace = str.IndexOf(' '); + if(iSpace >= 0) { - strApp = str.Substring(0, nSpace); - strArgs = str.Remove(0, nSpace).Trim(); + strApp = str.Substring(0, iSpace).Trim(); + strArgs = str.Remove(0, iSpace + 1).Trim(); } - else strApp = strCmdLine; + else strApp = str; } - if(strApp == null) strApp = string.Empty; + if(strApp == null) { Debug.Assert(false); strApp = string.Empty; } if(strArgs == null) strArgs = string.Empty; + + if(bDecodeAppToPath) strApp = NativeLib.DecodeArgsToPath(strApp); } // /// @@ -1289,7 +1282,7 @@ namespace ModernKeePassLib.Utility if(uBytes <= uGB) return (((uBytes - 1UL) / uMB) + 1UL).ToString() + " MB"; if(uBytes <= uTB) return (((uBytes - 1UL) / uGB) + 1UL).ToString() + " GB"; - return (((uBytes - 1UL)/ uTB) + 1UL).ToString() + " TB"; + return (((uBytes - 1UL) / uTB) + 1UL).ToString() + " TB"; } public static string FormatDataSizeKB(ulong uBytes) diff --git a/ModernKeePassLib/Utility/UrlUtil.cs b/ModernKeePassLib/Utility/UrlUtil.cs index 1e89085..51ddcc4 100644 --- a/ModernKeePassLib/Utility/UrlUtil.cs +++ b/ModernKeePassLib/Utility/UrlUtil.cs @@ -23,6 +23,7 @@ using System.Diagnostics; using System.Globalization; using System.IO; using System.Text; +using System.Text.RegularExpressions; #if ModernKeePassLib using Windows.Storage; @@ -38,12 +39,8 @@ namespace ModernKeePassLib.Utility /// public static class UrlUtil { - private static readonly char[] m_vDirSeps = new char[] { - '\\', '/', UrlUtil.LocalDirSepChar }; -#if !ModernKeePassLib - private static readonly char[] m_vPathTrimCharsWs = new char[] { + private static readonly char[] g_vPathTrimCharsWs = new char[] { '\"', ' ', '\t', '\r', '\n' }; -#endif public static char LocalDirSepChar { @@ -57,6 +54,32 @@ namespace ModernKeePassLib.Utility #endif } + private static char[] g_vDirSepChars = null; + private static char[] DirSepChars + { + get + { + if(g_vDirSepChars == null) + { + List l = new List(); + l.Add('/'); // For URLs, also on Windows + + // On Unix-like systems, '\\' is not a separator + if(!NativeLib.IsUnix()) l.Add('\\'); + + if(!l.Contains(UrlUtil.LocalDirSepChar)) + { + Debug.Assert(false); + l.Add(UrlUtil.LocalDirSepChar); + } + + g_vDirSepChars = l.ToArray(); + } + + return g_vDirSepChars; + } + } + /// /// Get the directory (path) of a file name. The returned string may be /// terminated by a directory separator character. Example: @@ -79,7 +102,7 @@ namespace ModernKeePassLib.Utility Debug.Assert(strFile != null); if(strFile == null) throw new ArgumentNullException("strFile"); - int nLastSep = strFile.LastIndexOfAny(m_vDirSeps); + int nLastSep = strFile.LastIndexOfAny(UrlUtil.DirSepChars); if(nLastSep < 0) return string.Empty; // No directory if(bEnsureValidDirSpec && (nLastSep == 2) && (strFile[1] == ':') && @@ -103,7 +126,7 @@ namespace ModernKeePassLib.Utility { Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath"); - int nLastSep = strPath.LastIndexOfAny(m_vDirSeps); + int nLastSep = strPath.LastIndexOfAny(UrlUtil.DirSepChars); if(nLastSep < 0) return strPath; if(nLastSep >= (strPath.Length - 1)) return string.Empty; @@ -120,7 +143,7 @@ namespace ModernKeePassLib.Utility { Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath"); - int nLastDirSep = strPath.LastIndexOfAny(m_vDirSeps); + int nLastDirSep = strPath.LastIndexOfAny(UrlUtil.DirSepChars); int nLastExtDot = strPath.LastIndexOf('.'); if(nLastExtDot <= nLastDirSep) return strPath; @@ -137,7 +160,7 @@ namespace ModernKeePassLib.Utility { Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath"); - int nLastDirSep = strPath.LastIndexOfAny(m_vDirSeps); + int nLastDirSep = strPath.LastIndexOfAny(UrlUtil.DirSepChars); int nLastExtDot = strPath.LastIndexOf('.'); if(nLastExtDot <= nLastDirSep) return string.Empty; @@ -162,11 +185,8 @@ namespace ModernKeePassLib.Utility if(nLength <= 0) return string.Empty; char chLast = strPath[nLength - 1]; - - for(int i = 0; i < m_vDirSeps.Length; ++i) - { - if(chLast == m_vDirSeps[i]) return strPath; - } + if(Array.IndexOf(UrlUtil.DirSepChars, chLast) >= 0) + return strPath; if(bUrl) return (strPath + '/'); return (strPath + UrlUtil.LocalDirSepChar); @@ -230,21 +250,35 @@ namespace ModernKeePassLib.Utility return false; } */ + internal static int IndexOfSecondEnclQuote(string str) + { + if(str == null) { Debug.Assert(false); return -1; } + if(str.Length <= 1) return -1; + if(str[0] != '\"') { Debug.Assert(false); return -1; } + + if(NativeLib.IsUnix()) + { + // Find non-escaped quote + string strFlt = str.Replace("\\\\", new string( + StrUtil.GetUnusedChar(str + "\\\""), 2)); // Same length + Match m = Regex.Match(strFlt, "[^\\\\]\\u0022"); + int i = (((m != null) && m.Success) ? m.Index : -1); + return ((i >= 0) ? (i + 1) : -1); // Index of quote + } + + // Windows does not allow quotes in folder/file names + return str.IndexOf('\"', 1); + } + public static string GetQuotedAppPath(string strPath) { if(strPath == null) { Debug.Assert(false); return string.Empty; } - // int nFirst = strPath.IndexOf('\"'); - // int nSecond = strPath.IndexOf('\"', nFirst + 1); - // if((nFirst >= 0) && (nSecond >= 0)) - // return strPath.Substring(nFirst + 1, nSecond - nFirst - 1); - // return strPath; - string str = strPath.Trim(); if(str.Length <= 1) return str; if(str[0] != '\"') return str; - int iSecond = str.IndexOf('\"', 1); + int iSecond = IndexOfSecondEnclQuote(str); if(iSecond <= 0) return str; return str.Substring(1, iSecond - 1); @@ -256,7 +290,7 @@ namespace ModernKeePassLib.Utility if(strUrl == null) throw new ArgumentNullException("strUrl"); string str = strUrl; - if(str.StartsWith(@"file:///", StrUtil.CaseIgnoreCmp)) + if(str.StartsWith("file:///", StrUtil.CaseIgnoreCmp)) str = str.Substring(8, str.Length - 8); str = str.Replace('/', UrlUtil.LocalDirSepChar); @@ -338,8 +372,8 @@ namespace ModernKeePassLib.Utility string strBase = GetShortestAbsolutePath(strBaseFile); string strTarget = GetShortestAbsolutePath(strTargetFile); - string[] vBase = strBase.Split(m_vDirSeps); - string[] vTarget = strTarget.Split(m_vDirSeps); + string[] vBase = strBase.Split(UrlUtil.DirSepChars); + string[] vTarget = strTarget.Split(UrlUtil.DirSepChars); int i = 0; while((i < (vBase.Length - 1)) && (i < (vTarget.Length - 1)) && @@ -416,13 +450,14 @@ namespace ModernKeePassLib.Utility if(IsUncPath(strPath)) { char chSep = strPath[0]; - Debug.Assert(Array.IndexOf(m_vDirSeps, chSep) >= 0); + char[] vSep = ((chSep == '/') ? (new char[] { '/' }) : + (new char[] { '\\', '/' })); List l = new List(); #if !KeePassLibSD - string[] v = strPath.Split(m_vDirSeps, StringSplitOptions.None); + string[] v = strPath.Split(vSep, StringSplitOptions.None); #else - string[] v = strPath.Split(m_vDirSeps); + string[] v = strPath.Split(vSep); #endif Debug.Assert((v.Length >= 3) && (v[0].Length == 0) && (v[1].Length == 0)); @@ -463,8 +498,8 @@ namespace ModernKeePassLib.Utility } catch(Exception) { Debug.Assert(false); return strPath; } - Debug.Assert(str.IndexOf("\\..\\") < 0); - foreach(char ch in m_vDirSeps) + Debug.Assert((str.IndexOf("\\..\\") < 0) || NativeLib.IsUnix()); + foreach(char ch in UrlUtil.DirSepChars) { string strSep = new string(ch, 1); str = str.Replace(strSep + "." + strSep, strSep); @@ -494,24 +529,30 @@ namespace ModernKeePassLib.Utility return nLength; } + internal static string GetScheme(string strUrl) + { + if(string.IsNullOrEmpty(strUrl)) return string.Empty; + + int i = strUrl.IndexOf(':'); + if(i > 0) return strUrl.Substring(0, i); + + return string.Empty; + } + public static string RemoveScheme(string strUrl) { if(string.IsNullOrEmpty(strUrl)) return string.Empty; - int nNetScheme = strUrl.IndexOf(@"://", StrUtil.CaseIgnoreCmp); - int nShScheme = strUrl.IndexOf(@":/", StrUtil.CaseIgnoreCmp); - int nSmpScheme = strUrl.IndexOf(@":", StrUtil.CaseIgnoreCmp); + int i = strUrl.IndexOf(':'); + if(i < 0) return strUrl; // No scheme to remove + ++i; - if((nNetScheme < 0) && (nShScheme < 0) && (nSmpScheme < 0)) - return strUrl; // No scheme + // A single '/' indicates a path (absolute) and should not be removed + if(((i + 1) < strUrl.Length) && (strUrl[i] == '/') && + (strUrl[i + 1] == '/')) + i += 2; // Skip authority prefix - int nMin = Math.Min(Math.Min((nNetScheme >= 0) ? nNetScheme : int.MaxValue, - (nShScheme >= 0) ? nShScheme : int.MaxValue), - (nSmpScheme >= 0) ? nSmpScheme : int.MaxValue); - - if(nMin == nNetScheme) return strUrl.Substring(nMin + 3); - if(nMin == nShScheme) return strUrl.Substring(nMin + 2); - return strUrl.Substring(nMin + 1); + return strUrl.Substring(i); } public static string ConvertSeparators(string strPath) @@ -538,22 +579,50 @@ namespace ModernKeePassLib.Utility public static string FilterFileName(string strName) { - if(strName == null) { Debug.Assert(false); return string.Empty; } + if(string.IsNullOrEmpty(strName)) { Debug.Assert(false); return string.Empty; } - string str = strName; + // https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file - str = str.Replace('/', '-'); - str = str.Replace('\\', '-'); - str = str.Replace(":", string.Empty); - str = str.Replace("*", string.Empty); - str = str.Replace("?", string.Empty); - str = str.Replace("\"", string.Empty); - str = str.Replace(@"'", string.Empty); - str = str.Replace('<', '('); - str = str.Replace('>', ')'); - str = str.Replace('|', '-'); + StringBuilder sb = new StringBuilder(strName.Length); + foreach(char ch in strName) + { + if(ch < '\u0020') continue; - return str; + switch(ch) + { + case '\"': + case '*': + case ':': + case '?': + break; + + case '/': + case '\\': + case '|': + sb.Append('-'); + break; + + case '<': + sb.Append('('); + break; + + case '>': + sb.Append(')'); + break; + + default: sb.Append(ch); break; + } + } + + // Trim trailing spaces and periods + for(int i = sb.Length - 1; i >= 0; --i) + { + char ch = sb[i]; + if((ch == ' ') || (ch == '.')) sb.Remove(i, 1); + else break; + } + + return sb.ToString(); } /// @@ -672,7 +741,7 @@ namespace ModernKeePassLib.Utility foreach(string strPathRaw in v) { if(strPathRaw == null) { Debug.Assert(false); continue; } - string strPath = strPathRaw.Trim(m_vPathTrimCharsWs); + string strPath = strPathRaw.Trim(g_vPathTrimCharsWs); if(strPath.Length == 0) { Debug.Assert(false); continue; } Debug.Assert(strPath == strPathRaw); @@ -709,7 +778,7 @@ namespace ModernKeePassLib.Utility if(fi == null) { Debug.Assert(false); continue; } string strPathRaw = fi.FullName; if(strPathRaw == null) { Debug.Assert(false); continue; } - string strPath = strPathRaw.Trim(m_vPathTrimCharsWs); + string strPath = strPathRaw.Trim(g_vPathTrimCharsWs); if(strPath.Length == 0) { Debug.Assert(false); continue; } Debug.Assert(strPath == strPathRaw); @@ -783,5 +852,19 @@ namespace ModernKeePassLib.Utility char ch = char.ToUpperInvariant(strPath[0]); return (((ch >= 'A') && (ch <= 'Z')) ? ch : '\0'); } + + internal static string GetSafeFileName(string strName) + { + Debug.Assert(!string.IsNullOrEmpty(strName)); + + string str = FilterFileName(GetFileName(strName ?? string.Empty)); + + if(string.IsNullOrEmpty(str)) + { + Debug.Assert(false); + return "File.dat"; + } + return str; + } } } diff --git a/ModernKeePassLib/Utility/XmlUtilEx.cs b/ModernKeePassLib/Utility/XmlUtilEx.cs index 87a25e8..a755070 100644 --- a/ModernKeePassLib/Utility/XmlUtilEx.cs +++ b/ModernKeePassLib/Utility/XmlUtilEx.cs @@ -50,7 +50,7 @@ namespace ModernKeePassLib.Utility xrs.IgnoreProcessingInstructions = true; xrs.IgnoreWhitespace = true; -#if KeePassUAP +#if KeePassUAP || ModernKeePassLib xrs.DtdProcessing = DtdProcessing.Prohibit; #else // Also see PrepMonoDev.sh script