private static void SaveBinary(string strName, ProtectedBinary pb, string strSaveDir) { if(pb == null) { Debug.Assert(false); return; } if(string.IsNullOrEmpty(strName)) strName = "File.bin"; string strPath; int iTry = 1; do { strPath = UrlUtil.EnsureTerminatingSeparator(strSaveDir, false); string strExt = UrlUtil.GetExtension(strName); string strDesc = UrlUtil.StripExtension(strName); strPath += strDesc; if(iTry > 1) strPath += " (" + iTry.ToString() + ")"; if(!string.IsNullOrEmpty(strExt)) strPath += "." + strExt; ++iTry; } while(File.Exists(strPath)); #if !KeePassLibSD byte[] pbData = pb.ReadData(); File.WriteAllBytes(strPath, pbData); MemUtil.ZeroByteArray(pbData); #else FileStream fs = new FileStream(strPath, FileMode.Create, FileAccess.Write, FileShare.None); byte[] pbData = pb.ReadData(); fs.Write(pbData, 0, pbData.Length); fs.Close(); #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"); 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 }
/// <summary> /// Construct a new protected binary data object. Copy the data from /// an existing object. /// </summary> /// <param name="pbTemplate">Existing <c>ProtectedBinary</c> object, /// which is used to initialize the new object. This parameter must /// not be <c>null</c>.</param> /// <exception cref="System.ArgumentNullException">Thrown if the input /// parameter is <c>null</c>.</exception> public ProtectedBinary(ProtectedBinary pbTemplate) { Debug.Assert(pbTemplate != null); if(pbTemplate == null) throw new ArgumentNullException("pbTemplate"); m_bDoProtect = pbTemplate.m_bDoProtect; byte[] pbBuf = pbTemplate.ReadData(); SetData(pbBuf); MemUtil.ZeroByteArray(pbBuf); }
private void ExecuteBinaryEditView(string strBinName, ProtectedBinary pb) { BinaryDataClass bdc = BinaryDataClassifier.Classify(strBinName, pb.ReadData()); DynamicMenuEventArgs args = new DynamicMenuEventArgs(strBinName, DataEditorForm.SupportsDataType(bdc) ? new EditableBinaryAttachment(strBinName) : null); OnEntryBinaryView(null, args); }
private void SubWriteValue(ProtectedBinary value) { if(value.IsProtected && (m_format != KdbxFormat.PlainXml)) { m_xmlWriter.WriteAttributeString(AttrProtected, ValTrue); byte[] pbEncoded = value.ReadXorredData(m_randomStream); if(pbEncoded.Length > 0) m_xmlWriter.WriteBase64(pbEncoded, 0, pbEncoded.Length); } else { if(m_pwDatabase.Compression == PwCompressionAlgorithm.GZip) { m_xmlWriter.WriteAttributeString(AttrCompressed, ValTrue); byte[] pbRaw = value.ReadData(); byte[] pbCmp = MemUtil.Compress(pbRaw); m_xmlWriter.WriteBase64(pbCmp, 0, pbCmp.Length); } else { byte[] pbRaw = value.ReadData(); m_xmlWriter.WriteBase64(pbRaw, 0, pbRaw.Length); } } }
private void WriteObject(string name, ProtectedBinary value) { Debug.Assert(name != null); Debug.Assert(value != null); if(value == null) throw new ArgumentNullException("value"); m_xmlWriter.WriteStartElement(ElemBinary); m_xmlWriter.WriteStartElement(ElemKey); m_xmlWriter.WriteString(StrUtil.SafeXmlString(name)); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteStartElement(ElemValue); if((value.IsProtected) && (m_format != Kdb4Format.PlainXml)) { m_xmlWriter.WriteAttributeString(AttrProtected, ValTrue); byte[] pbEncoded = value.ReadXorredData(m_randomStream); if(pbEncoded.Length > 0) m_xmlWriter.WriteBase64(pbEncoded, 0, pbEncoded.Length); } else { byte[] pbRaw = value.ReadData(); m_xmlWriter.WriteBase64(pbRaw, 0, pbRaw.Length); } m_xmlWriter.WriteEndElement(); // ElemValue m_xmlWriter.WriteEndElement(); // ElemBinary }
private static void TestProtectedMemory() { #if DEBUG byte[] pbData = Encoding.ASCII.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 = Encoding.ASCII.GetBytes("Test Test Test Test"); byte[] pbData3 = Encoding.ASCII.GetBytes("Test Test Test Test Test"); ProtectedBinary pb2 = new ProtectedBinary(true, pbData2); ProtectedBinary pb3 = new ProtectedBinary(true, pbData3); if(!pb.EqualsValue(pb2)) throw new SecurityException("ProtectedBinary-4"); if(pb.EqualsValue(pb3)) throw new SecurityException("ProtectedBinary-5"); if(pb2.EqualsValue(pb3)) throw new SecurityException("ProtectedBinary-6"); #endif }
public bool EqualsValue(ProtectedBinary pb) { if(pb == null) { Debug.Assert(false); throw new ArgumentNullException("pb"); } if((pb.m_xbEncrypted != null) && (m_xbEncrypted != null)) return pb.m_xbEncrypted.EqualsValue(m_xbEncrypted); if((pb.m_xbEncrypted != null) && (m_xbEncrypted == null)) { if(pb.m_xbEncrypted.Length != m_uDataLen) return false; byte[] pbThis = ReadData(); bool bEqThis = pb.m_xbEncrypted.EqualsValue(pbThis); MemUtil.ZeroByteArray(pbThis); return bEqThis; } if((pb.m_xbEncrypted == null) && (m_xbEncrypted != null)) { if(m_xbEncrypted.Length != pb.m_uDataLen) return false; byte[] pbOther = pb.ReadData(); bool bEqOther = m_xbEncrypted.EqualsValue(pbOther); MemUtil.ZeroByteArray(pbOther); return bEqOther; } // (pb.m_xbEncrypted == null) && (m_xbEncrypted == null) if(m_uDataLen != pb.m_uDataLen) return false; byte[] pbL = ReadData(); byte[] pbR = pb.ReadData(); bool bEq = MemUtil.ArraysEqual(pbL, pbR); MemUtil.ZeroByteArray(pbL); MemUtil.ZeroByteArray(pbR); return bEq; }