public bool Equals(ProtectedString ps, bool bCheckProtEqual) { if (ps == null) { throw new ArgumentNullException("ps"); } if (object.ReferenceEquals(this, ps)) { return(true); // Perf. opt. } bool bPA = m_bIsProtected, bPB = ps.m_bIsProtected; if (bCheckProtEqual && (bPA != bPB)) { return(false); } if (!bPA && !bPB) { return(ReadString() == ps.ReadString()); } byte[] pbA = ReadUtf8(), pbB = null; bool bEq; try { pbB = ps.ReadUtf8(); bEq = MemUtil.ArraysEqual(pbA, pbB); } finally { if (bPA) { MemUtil.ZeroByteArray(pbA); } if (bPB && (pbB != null)) { MemUtil.ZeroByteArray(pbB); } } return(bEq); }
private static void TestProtectedObjects() { #if DEBUG Encoding enc = StrUtil.Utf8; byte[] pbData = enc.GetBytes("Test Test Test Test"); ProtectedBinary pb = new ProtectedBinary(true, pbData); if(!pb.IsProtected) throw new SecurityException("ProtectedBinary-1"); byte[] pbDec = pb.ReadData(); if(!MemUtil.ArraysEqual(pbData, pbDec)) throw new SecurityException("ProtectedBinary-2"); if(!pb.IsProtected) throw new SecurityException("ProtectedBinary-3"); byte[] pbData2 = enc.GetBytes("Test Test Test Test"); byte[] pbData3 = enc.GetBytes("Test Test Test Test Test"); ProtectedBinary pb2 = new ProtectedBinary(true, pbData2); ProtectedBinary pb3 = new ProtectedBinary(true, pbData3); if(!pb.Equals(pb2)) throw new SecurityException("ProtectedBinary-4"); if(pb.Equals(pb3)) throw new SecurityException("ProtectedBinary-5"); if(pb2.Equals(pb3)) throw new SecurityException("ProtectedBinary-6"); if(pb.GetHashCode() != pb2.GetHashCode()) throw new SecurityException("ProtectedBinary-7"); if(!((object)pb).Equals((object)pb2)) throw new SecurityException("ProtectedBinary-8"); if(((object)pb).Equals((object)pb3)) throw new SecurityException("ProtectedBinary-9"); if(((object)pb2).Equals((object)pb3)) throw new SecurityException("ProtectedBinary-10"); ProtectedString ps = new ProtectedString(); if(ps.Length != 0) throw new SecurityException("ProtectedString-1"); if(!ps.IsEmpty) throw new SecurityException("ProtectedString-2"); if(ps.ReadString().Length != 0) throw new SecurityException("ProtectedString-3"); ps = new ProtectedString(true, "Test"); ProtectedString ps2 = new ProtectedString(true, enc.GetBytes("Test")); if(ps.IsEmpty) throw new SecurityException("ProtectedString-4"); pbData = ps.ReadUtf8(); pbData2 = ps2.ReadUtf8(); if(!MemUtil.ArraysEqual(pbData, pbData2)) throw new SecurityException("ProtectedString-5"); if(pbData.Length != 4) throw new SecurityException("ProtectedString-6"); if(ps.ReadString() != ps2.ReadString()) throw new SecurityException("ProtectedString-7"); pbData = ps.ReadUtf8(); pbData2 = ps2.ReadUtf8(); if(!MemUtil.ArraysEqual(pbData, pbData2)) throw new SecurityException("ProtectedString-8"); if(!ps.IsProtected) throw new SecurityException("ProtectedString-9"); if(!ps2.IsProtected) throw new SecurityException("ProtectedString-10"); Random r = new Random(); string str = string.Empty; ps = new ProtectedString(); for(int i = 0; i < 100; ++i) { bool bProt = ((r.Next() % 4) != 0); ps = ps.WithProtection(bProt); int x = r.Next(str.Length + 1); int c = r.Next(20); char ch = (char)r.Next(1, 256); string strIns = new string(ch, c); str = str.Insert(x, strIns); ps = ps.Insert(x, strIns); if(ps.IsProtected != bProt) throw new SecurityException("ProtectedString-11"); if(ps.ReadString() != str) throw new SecurityException("ProtectedString-12"); ps = ps.WithProtection(bProt); x = r.Next(str.Length); c = r.Next(str.Length - x + 1); str = str.Remove(x, c); ps = ps.Remove(x, c); if(ps.IsProtected != bProt) throw new SecurityException("ProtectedString-13"); if(ps.ReadString() != str) throw new SecurityException("ProtectedString-14"); } #endif }
private static void TestProtectedObjects() { #if DEBUG Encoding enc = StrUtil.Utf8; byte[] pbData = enc.GetBytes("Test Test Test Test"); ProtectedBinary pb = new ProtectedBinary(true, pbData); if(!pb.IsProtected) throw new SecurityException("ProtectedBinary-1"); byte[] pbDec = pb.ReadData(); if(!MemUtil.ArraysEqual(pbData, pbDec)) throw new SecurityException("ProtectedBinary-2"); if(!pb.IsProtected) throw new SecurityException("ProtectedBinary-3"); byte[] pbData2 = enc.GetBytes("Test Test Test Test"); byte[] pbData3 = enc.GetBytes("Test Test Test Test Test"); ProtectedBinary pb2 = new ProtectedBinary(true, pbData2); ProtectedBinary pb3 = new ProtectedBinary(true, pbData3); if(!pb.Equals(pb2)) throw new SecurityException("ProtectedBinary-4"); if(pb.Equals(pb3)) throw new SecurityException("ProtectedBinary-5"); if(pb2.Equals(pb3)) throw new SecurityException("ProtectedBinary-6"); if(pb.GetHashCode() != pb2.GetHashCode()) throw new SecurityException("ProtectedBinary-7"); if(!((object)pb).Equals((object)pb2)) throw new SecurityException("ProtectedBinary-8"); if(((object)pb).Equals((object)pb3)) throw new SecurityException("ProtectedBinary-9"); if(((object)pb2).Equals((object)pb3)) throw new SecurityException("ProtectedBinary-10"); ProtectedString ps = new ProtectedString(); if(ps.Length != 0) throw new SecurityException("ProtectedString-1"); if(!ps.IsEmpty) throw new SecurityException("ProtectedString-2"); if(ps.ReadString().Length != 0) throw new SecurityException("ProtectedString-3"); ps = new ProtectedString(true, "Test"); ProtectedString ps2 = new ProtectedString(true, enc.GetBytes("Test")); if(ps.IsEmpty) throw new SecurityException("ProtectedString-4"); pbData = ps.ReadUtf8(); pbData2 = ps2.ReadUtf8(); if(!MemUtil.ArraysEqual(pbData, pbData2)) throw new SecurityException("ProtectedString-5"); if(pbData.Length != 4) throw new SecurityException("ProtectedString-6"); if(ps.ReadString() != ps2.ReadString()) throw new SecurityException("ProtectedString-7"); pbData = ps.ReadUtf8(); pbData2 = ps2.ReadUtf8(); if(!MemUtil.ArraysEqual(pbData, pbData2)) throw new SecurityException("ProtectedString-8"); if(!ps.IsProtected) throw new SecurityException("ProtectedString-9"); if(!ps2.IsProtected) throw new SecurityException("ProtectedString-10"); #endif }
private void OnPwGenOpen(object sender, EventArgs e) { PwGeneratorForm pgf = new PwGeneratorForm(); byte[] pbCurPassword = m_secPassword.ToUtf8(); bool bAtLeastOneChar = (pbCurPassword.Length > 0); ProtectedString ps = new ProtectedString(true, pbCurPassword); Array.Clear(pbCurPassword, 0, pbCurPassword.Length); PwProfile opt = PwProfile.DeriveFromPassword(ps); pgf.InitEx(bAtLeastOneChar ? opt : null, true, false); // pgf.InitEx(null, true, false); if(pgf.ShowDialog() == DialogResult.OK) { byte[] pbEntropy = EntropyForm.CollectEntropyIfEnabled(pgf.SelectedProfile); ProtectedString psNew = new ProtectedString(true); PwGenerator.Generate(psNew, pgf.SelectedProfile, pbEntropy, Program.PwGeneratorPool); byte[] pbNew = psNew.ReadUtf8(); m_secPassword.SetPassword(pbNew); m_secRepeat.SetPassword(pbNew); Array.Clear(pbNew, 0, pbNew.Length); } UIUtil.DestroyForm(pgf); EnableControlsEx(); }
private void OnProfilesDynamicMenuClick(object sender, DynamicMenuEventArgs e) { PwProfile pwp = null; if(e.ItemName == DeriveFromPrevious) { byte[] pbCur = m_secPassword.ToUtf8(); ProtectedString psCur = new ProtectedString(true, pbCur); Array.Clear(pbCur, 0, pbCur.Length); pwp = PwProfile.DeriveFromPassword(psCur); } else { foreach(PwProfile pwgo in Program.Config.PasswordGenerator.UserProfiles) { if(pwgo.Name == e.ItemName) { pwp = pwgo; break; } } } if(pwp != null) { ProtectedString psNew = new ProtectedString(true); PwGenerator.Generate(psNew, pwp, null, Program.PwGeneratorPool); byte[] pbNew = psNew.ReadUtf8(); m_secPassword.SetPassword(pbNew); m_secRepeat.SetPassword(pbNew); Array.Clear(pbNew, 0, pbNew.Length); } else { Debug.Assert(false); } }
public static PwProfile DeriveFromPassword(ProtectedString psPassword) { PwProfile pp = new PwProfile(); Debug.Assert(psPassword != null); if(psPassword == null) return pp; PwCharSet pcs = pp.CharSet; byte[] pbUTF8 = psPassword.ReadUtf8(); char[] vChars = Encoding.UTF8.GetChars(pbUTF8); pp.GeneratorType = PasswordGeneratorType.CharSet; pp.Length = (uint)vChars.Length; foreach(char ch in vChars) { if((ch >= 'A') && (ch <= 'Z')) pcs.Add(PwCharSet.UpperCase); else if((ch >= 'a') && (ch <= 'z')) pcs.Add(PwCharSet.LowerCase); else if((ch >= '0') && (ch <= '9')) pcs.Add(PwCharSet.Digits); else if((@"!#$%&'*+,./:;=?@^").IndexOf(ch) >= 0) pcs.Add(pcs.SpecialChars); else if(ch == ' ') pcs.Add(' '); else if(ch == '-') pcs.Add('-'); else if(ch == '_') pcs.Add('_'); else if(ch == '\"') pcs.Add(pcs.SpecialChars); else if(ch == '\\') pcs.Add(pcs.SpecialChars); else if((@"()[]{}<>").IndexOf(ch) >= 0) pcs.Add(PwCharSet.Brackets); else if((ch >= '~') && (ch <= 255)) pcs.Add(pcs.HighAnsiChars); else pcs.Add(ch); } Array.Clear(vChars, 0, vChars.Length); Array.Clear(pbUTF8, 0, pbUTF8.Length); return pp; }