示例#1
0
        private XorredBuffer ProcessNode(XmlReader xr)
        {
            // Debug.Assert(xr.NodeType == XmlNodeType.Element);

            XorredBuffer xb = null;

            if (xr.HasAttributes)
            {
                if (xr.MoveToAttribute(AttrProtected))
                {
                    if (xr.Value == ValTrue)
                    {
                        xr.MoveToElement();
                        string strEncrypted = ReadStringRaw(xr);

                        byte[] pbEncrypted;
                        if (strEncrypted.Length > 0)
                        {
                            pbEncrypted = Convert.FromBase64String(strEncrypted);
                        }
                        else
                        {
                            pbEncrypted = MemUtil.EmptyByteArray;
                        }

                        byte[] pbPad = m_randomStream.GetRandomBytes((uint)pbEncrypted.Length);

                        xb = new XorredBuffer(pbEncrypted, pbPad);
                    }
                }
            }

            return(xb);
        }
示例#2
0
        private ProtectedBinary ReadProtectedBinary(XmlReader xr)
        {
            if (xr.MoveToAttribute(AttrRef))
            {
                string strRef = xr.Value;
                if (strRef != null)
                {
                    ProtectedBinary pb = BinPoolGet(strRef);
                    if (pb != null)
                    {
                        // https://sourceforge.net/p/keepass/feature-requests/2023/
                        xr.MoveToElement();
#if DEBUG
                        string strInner = ReadStringRaw(xr);
                        Debug.Assert(string.IsNullOrEmpty(strInner));
#else
                        ReadStringRaw(xr);
#endif

                        return(pb);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            bool bCompressed = false;
            if (xr.MoveToAttribute(AttrCompressed))
            {
                bCompressed = (xr.Value == ValTrue);
            }

            XorredBuffer xb = ProcessNode(xr);
            if (xb != null)
            {
                Debug.Assert(!bCompressed);                 // See SubWriteValue(ProtectedBinary value)
                return(new ProtectedBinary(true, xb));
            }

            string strValue = ReadString(xr);
            if (strValue.Length == 0)
            {
                return(new ProtectedBinary());
            }

            byte[] pbData = Convert.FromBase64String(strValue);
            if (bCompressed)
            {
                pbData = MemUtil.Decompress(pbData);
            }
            return(new ProtectedBinary(false, pbData));
        }
        private ProtectedBinary ReadProtectedBinary(XmlReader xr)
        {
            if (xr.MoveToAttribute(AttrRef))
            {
                string strRef = xr.Value;
                if (strRef != null)
                {
                    ProtectedBinary pb = BinPoolGet(strRef);
                    if (pb != null)
                    {
                        return(pb);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            bool bCompressed = false;

            if (xr.MoveToAttribute(AttrCompressed))
            {
                bCompressed = (xr.Value == ValTrue);
            }

            XorredBuffer xb = ProcessNode(xr);

            if (xb != null)
            {
                Debug.Assert(!bCompressed);                 // See SubWriteValue(ProtectedBinary value)
                return(new ProtectedBinary(true, xb));
            }

            string strValue = ReadString(xr);

            if (strValue.Length == 0)
            {
                return(new ProtectedBinary());
            }

            byte[] pbData = Convert.FromBase64String(strValue);
            if (bCompressed)
            {
                pbData = MemUtil.Decompress(pbData);
            }
            return(new ProtectedBinary(false, pbData));
        }
示例#4
0
        internal string ReadString(XmlReader xr)
        {
            XorredBuffer xb = ProcessNode(xr);

            if (xb != null)
            {
                byte[] pb = xb.ReadPlainText();
                return(Encoding.UTF8.GetString(pb, 0, pb.Length));
            }

            m_bReadNextNode = false;             // ReadElementString skips end tag
            return(xr.ReadElementString());
        }
示例#5
0
        internal ProtectedString ReadProtectedString(XmlReader xr)
        {
            XorredBuffer xb = ProcessNode(xr);

            if (xb != null)
            {
                return(new ProtectedString(true, xb));
            }

            ProtectedString ps = new ProtectedString(false, ReadString(xr));

            return(ps);
        }
        internal void ReadProtectedBinaryEx(XmlNode xmlNode, ProtectedBinaryDictionary dictStorage)
        {
            ProcessNode(xmlNode);

            string       strKey  = string.Empty;
            XorredBuffer xbValue = null;

            byte[] pbValue = null;

            foreach (XmlNode xmlChild in xmlNode.ChildNodes)
            {
                if (xmlChild.Name == ElemKey)
                {
                    ProcessNode(xmlChild);
                    strKey = xmlChild.InnerText;
                }
                else if (xmlChild.Name == ElemValue)
                {
                    xbValue = ProcessNode(xmlChild);

                    if (xbValue == null)
                    {
                        string strInner = xmlChild.InnerText;

                        if (strInner.Length > 0)
                        {
                            pbValue = Convert.FromBase64String(strInner);
                        }
                        else
                        {
                            pbValue = new byte[0];
                        }
                    }
                }
                else
                {
                    ReadUnknown(xmlChild);
                }
            }

            if (xbValue != null)
            {
                Debug.Assert(pbValue == null);
                dictStorage.Set(strKey, new ProtectedBinary(true, xbValue));
            }
            else
            {
                Debug.Assert(pbValue != null);
                dictStorage.Set(strKey, new ProtectedBinary(false, pbValue));
            }
        }
示例#7
0
        public void Run()
        {
            using (StreamReader reader = File.OpenText(SourceFilePath))
            {
                LegacyIdBytes = Encoding.UTF8.GetBytes(
                    reader.ReadLine().Trim());
                LegacySecretBytes = Encoding.UTF8.GetBytes(
                    reader.ReadLine().Trim());
                ClientIdBytes = Encoding.UTF8.GetBytes(
                    reader.ReadLine().Trim());
                ClientSecretBytes = Encoding.UTF8.GetBytes(
                    reader.ReadLine().Trim());
                LegacyIdPadBytes     = new byte[LegacyIdBytes.Length];
                LegacySecretPadBytes = new byte[LegacySecretBytes.Length];
                ClientIdPadBytes     = new byte[ClientIdBytes.Length];
                ClientSecretPadBytes = new byte[ClientSecretBytes.Length];

                using (RandomNumberGenerator rng =
                           RandomNumberGenerator.Create())
                {
                    rng.GetNonZeroBytes(LegacyIdPadBytes);
                    rng.GetNonZeroBytes(LegacySecretPadBytes);
                    rng.GetNonZeroBytes(ClientIdPadBytes);
                    rng.GetNonZeroBytes(ClientSecretPadBytes);
                }

                XorredBuffer clientIdBuf = new XorredBuffer(LegacyIdBytes,
                                                            LegacyIdPadBytes);
                XorredBuffer secretBuf = new XorredBuffer(LegacySecretBytes,
                                                          LegacySecretPadBytes);

                LegacyIdCsharpConstant        = GenSrc(clientIdBuf.ReadPlainText());
                LegacyIdPadCsharpConstant     = GenSrc(LegacyIdPadBytes);
                LegacySecretCsharpConstant    = GenSrc(secretBuf.ReadPlainText());
                LegacySecretPadCsharpConstant = GenSrc(LegacySecretPadBytes);

                clientIdBuf = new XorredBuffer(ClientIdBytes,
                                               ClientIdPadBytes);
                secretBuf = new XorredBuffer(ClientSecretBytes,
                                             ClientSecretPadBytes);

                ClientIdCsharpConstant        = GenSrc(clientIdBuf.ReadPlainText());
                ClientIdPadCsharpConstant     = GenSrc(ClientIdPadBytes);
                ClientSecretCsharpConstant    = GenSrc(secretBuf.ReadPlainText());
                ClientSecretPadCsharpConstant = GenSrc(ClientSecretPadBytes);
            }
        }
示例#8
0
        private string ReadString(XmlReader xr)
        {
            XorredBuffer xb = ProcessNode(xr);

            if (xb != null)
            {
                byte[] pb = xb.ReadPlainText();
                if (pb.Length == 0)
                {
                    return(string.Empty);
                }
                return(StrUtil.Utf8.GetString(pb, 0, pb.Length));
            }

            m_bReadNextNode = false;             // ReadElementString skips end tag
            return(xr.ReadElementString());
        }
示例#9
0
        internal ProtectedBinary ReadProtectedBinary(XmlReader xr)
        {
            XorredBuffer xb = ProcessNode(xr);

            if (xb != null)
            {
                return(new ProtectedBinary(true, xb));
            }

            string strValue = ReadString(xr);

            if (strValue.Length == 0)
            {
                return(new ProtectedBinary(false));
            }

            return(new ProtectedBinary(false, Convert.FromBase64String(strValue)));
        }
示例#10
0
        static ProtectedString GetData(byte[] data, byte[] pad)
        {
            XorredBuffer buf = null;

            try
            {
                buf = new XorredBuffer(data, pad);
                return(new ProtectedString(true, buf));
            }
            finally
            {
                // $$REVIEW
                // XorredBuffer isn't IDisposable until sometime after KeePass
                // v2.35, so fool the compiler and dispose if necessary.
                object unknown = buf;
                using (IDisposable disposable = unknown as IDisposable) { }
            }
        }
        private void ReadUnknown(XmlReader xr)
        {
            Debug.Assert(false);             // Unknown node!

            if (xr.IsEmptyElement)
            {
                m_bReadNextNode = true; return;
            }

            string strUnknownName = xr.Name;

            XorredBuffer xb = ProcessNode(xr);

            if (xb != null)
            {
                xb.Dispose(); return;
            }                                                    // ProcessNode sets m_bReadNextNode

            bool bRead = true;

            while (true)
            {
                if (bRead)
                {
                    xr.Read();
                }

                if (xr.NodeType == XmlNodeType.EndElement)
                {
                    break;
                }
                if (xr.NodeType != XmlNodeType.Element)
                {
                    bRead = true; continue;
                }

                ReadUnknown(xr);
                bRead = m_bReadNextNode;
            }

            Debug.Assert(xr.Name == strUnknownName);             // On end tag
            m_bReadNextNode = true;
        }
        public void InternalChangeKey()
        {
            // arrange
            var data          = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
            var firstPad      = new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
            var protectedData = new byte[data.Length];

            for (var i = 0; i < data.Length; i++)
            {
                protectedData[i] = (byte)(data[i] ^ firstPad[i]);
            }
            var secondPad = new byte[] { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };

            // act
            var actual = XorredBuffer.InternalChangeKey(protectedData, firstPad, secondPad);

            // assert
            Assert.AreEqual(data.Length, actual.Length);
            for (var i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(0x20, actual[i]);
            }
        }
示例#13
0
        private ProtectedString ReadProtectedString(XmlReader xr)
        {
            XorredBuffer xb = ProcessNode(xr);

            if (xb != null)
            {
                try { return(new ProtectedString(true, xb)); }
                finally { xb.Dispose(); }
            }

            bool bProtect = false;

            if (m_format == KdbxFormat.PlainXml)
            {
                if (xr.MoveToAttribute(AttrProtectedInMemPlainXml))
                {
                    string strProtect = xr.Value;
                    bProtect = ((strProtect != null) && (strProtect == ValTrue));
                }
            }

            return(new ProtectedString(bProtect, ReadString(xr)));
        }
示例#14
0
        private string ReadString(XmlReader xr)
        {
            XorredBuffer xb = ProcessNode(xr);

            if (xb != null)
            {
                Debug.Assert(false);                 // Protected data is unexpected here
                try
                {
                    byte[] pb = xb.ReadPlainText();
                    if (pb.Length == 0)
                    {
                        return(string.Empty);
                    }
                    try { return(StrUtil.Utf8.GetString(pb, 0, pb.Length)); }
                    finally { MemUtil.ZeroByteArray(pb); }
                }
                finally { xb.Dispose(); }
            }

            m_bReadNextNode = false;             // ReadElementString skips end tag
            return(xr.ReadElementString());
        }
示例#15
0
        private void ReadUnknown(XmlReader xr)
        {
            Debug.Assert(false);             // Unknown node!
            Debug.Assert(xr.NodeType == XmlNodeType.Element);

            bool bRead = false;
            int  cOpen = 0;

            do
            {
                if (bRead)
                {
                    xr.Read();
                }
                bRead = true;

                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;
                        }

                        ++cOpen;
                    }
                }
            }while(cOpen > 0);

            m_bReadNextNode = bRead;
        }
示例#16
0
        private XorredBuffer ProcessNode(XmlReader xr)
        {
            // Debug.Assert(xr.NodeType == XmlNodeType.Element);

            XorredBuffer xb = null;

            if (xr.HasAttributes)
            {
                if (xr.MoveToAttribute(AttrProtected))
                {
                    if (xr.Value == ValTrue)
                    {
                        xr.MoveToElement();

                        byte[] pbEncrypted = ReadBase64(xr, true);
                        byte[] pbPad       = m_randomStream.GetRandomBytes((uint)pbEncrypted.Length);

                        xb = new XorredBuffer(pbEncrypted, pbPad);
                    }
                }
            }

            return(xb);
        }
        internal void ReadProtectedStringEx(XmlNode xmlNode, ProtectedStringDictionary dictStorage)
        {
            ProcessNode(xmlNode);

            string       strKey   = string.Empty;
            XorredBuffer xbValue  = null;
            string       strValue = null;

            foreach (XmlNode xmlChild in xmlNode.ChildNodes)
            {
                if (xmlChild.Name == ElemKey)
                {
                    ProcessNode(xmlChild);
                    strKey = xmlChild.InnerText;
                }
                else if (xmlChild.Name == ElemValue)
                {
                    xbValue = ProcessNode(xmlChild);

                    // If contents aren't protected: read as plain-text string
                    if (xbValue == null)
                    {
                        strValue = xmlChild.InnerText;
                    }
                }
                else
                {
                    ReadUnknown(xmlChild);
                }
            }

            if (xbValue != null)
            {
                Debug.Assert(strValue == null);
                dictStorage.Set(strKey, new ProtectedString(true, xbValue));
            }
            else
            {
                Debug.Assert(strValue != null);
                dictStorage.Set(strKey, new ProtectedString(false, strValue));
            }

#if DEBUG
            if (m_format == Kdb4Format.Default)
            {
                if (strKey == PwDefs.TitleField)
                {
                    Debug.Assert(m_pwDatabase.MemoryProtection.ProtectTitle ==
                                 dictStorage.Get(strKey).IsProtected);
                }
                else if (strKey == PwDefs.UserNameField)
                {
                    Debug.Assert(m_pwDatabase.MemoryProtection.ProtectUserName ==
                                 dictStorage.Get(strKey).IsProtected);
                }
                else if (strKey == PwDefs.PasswordField)
                {
                    Debug.Assert(m_pwDatabase.MemoryProtection.ProtectPassword ==
                                 dictStorage.Get(strKey).IsProtected);
                }
                else if (strKey == PwDefs.UrlField)
                {
                    Debug.Assert(m_pwDatabase.MemoryProtection.ProtectUrl ==
                                 dictStorage.Get(strKey).IsProtected);
                }
                else if (strKey == PwDefs.NotesField)
                {
                    Debug.Assert(m_pwDatabase.MemoryProtection.ProtectNotes ==
                                 dictStorage.Get(strKey).IsProtected);
                }
            }
#endif
        }
示例#18
0
        private ProtectedBinary ReadProtectedBinary(XmlReader xr)
        {
            if (xr.MoveToAttribute(AttrRef))
            {
                string strRef = xr.Value;
                if (!string.IsNullOrEmpty(strRef))
                {
                    int iRef;
                    if (StrUtil.TryParseIntInvariant(strRef, out iRef))
                    {
                        ProtectedBinary pb = m_pbsBinaries.Get(iRef);
                        if (pb != null)
                        {
                            // https://sourceforge.net/p/keepass/feature-requests/2023/
                            xr.MoveToElement();
#if DEBUG
                            string strInner = ReadStringRaw(xr);
                            Debug.Assert(string.IsNullOrEmpty(strInner));
#else
                            ReadStringRaw(xr);
#endif

                            return(pb);
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            bool bCompressed = false;
            if (xr.MoveToAttribute(AttrCompressed))
            {
                bCompressed = (xr.Value == ValTrue);
            }

            XorredBuffer xb = ProcessNode(xr);
            if (xb != null)
            {
                Debug.Assert(!bCompressed);                 // See SubWriteValue(ProtectedBinary value)
                try { return(new ProtectedBinary(true, xb)); }
                finally { xb.Dispose(); }
            }

            byte[] pbData = ReadBase64(xr, true);
            if (pbData.Length == 0)
            {
                return(new ProtectedBinary());
            }

            if (bCompressed)
            {
                pbData = MemUtil.Decompress(pbData);
            }
            return(new ProtectedBinary(false, pbData));
        }