Пример #1
0
        private DateTime ReadTime(XmlReader xr)
        {
            // Cf. WriteObject(string, DateTime)
            if ((m_format == KdbxFormat.Default) && (m_uFileVersion >= FileVersion32_4))
            {
                // long l = ReadLong(xr, -1);
                // if(l != -1) return DateTime.FromBinary(l);

                byte[] pb = ReadBase64(xr, false);
                if (pb.Length != 8)
                {
                    Debug.Assert(false);
                    byte[] pb8 = new byte[8];
                    Array.Copy(pb, pb8, Math.Min(pb.Length, 8));                     // Little-endian
                    pb = pb8;
                }
                long lSec = MemUtil.BytesToInt64(pb);
                return(new DateTime(lSec * TimeSpan.TicksPerSecond, DateTimeKind.Utc));
            }
            else
            {
                string str = ReadString(xr);

                DateTime dt;
                if (TimeUtil.TryDeserializeUtc(str, out dt))
                {
                    return(dt);
                }
            }

            Debug.Assert(false);
            return(m_dtNow);
        }
Пример #2
0
        private static bool CreateMutexUnix(string strName, bool bInitiallyOwned)
        {
            string strPath = GetMutexPath(strName);

            try
            {
                if (File.Exists(strPath))
                {
                    byte[] pbEnc = File.ReadAllBytes(strPath);
                    byte[] pb    = CryptoUtil.UnprotectData(pbEnc, GmpOptEnt,
                                                            DataProtectionScope.CurrentUser);
                    if (pb.Length == 12)
                    {
                        long     lTime = MemUtil.BytesToInt64(pb, 0);
                        DateTime dt    = DateTime.FromBinary(lTime);

                        if ((DateTime.UtcNow - dt).TotalSeconds < GmpMutexValidSecs)
                        {
                            int pid = MemUtil.BytesToInt32(pb, 8);
                            try
                            {
                                Process.GetProcessById(pid);                  // Throws if process is not running
                                return(false);                                // Actively owned by other process
                            }
                            catch (Exception) { }
                        }

                        // Release the old mutex since process is not running
                        ReleaseMutexUnix(strName);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
            }
            catch (Exception) { Debug.Assert(false); }

            try { WriteMutexFilePriv(strPath); }
            catch (Exception) { Debug.Assert(false); }

            m_vMutexesUnix.Add(new KeyValuePair <string, string>(strName, strPath));
            return(true);
        }
Пример #3
0
        private DateTime ReadTime(XmlReader xr)
        {
            // Cf. WriteObject(string, DateTime)
            if ((m_format == KdbxFormat.Default) && (m_uFileVersion >= FileVersion32_4))
            {
                // long l = ReadLong(xr, -1);
                // if(l != -1) return DateTime.FromBinary(l);

                string str = ReadString(xr);
                byte[] pb  = Convert.FromBase64String(str);
                if (pb.Length != 8)
                {
                    Debug.Assert(false);
                    byte[] pb8 = new byte[8];
                    Array.Copy(pb, pb8, Math.Min(pb.Length, 8));                     // Little-endian
                    pb = pb8;
                }
                long lSec = MemUtil.BytesToInt64(pb);
                try
                {
                    return(new DateTime(lSec * TimeSpan.TicksPerSecond, DateTimeKind.Utc));
                }
                catch (System.ArgumentOutOfRangeException e)
                {
                    //files might contain bad data, e.g. see #868. Fall back to MinValue
                    Kp2aLog.Log("Failed to read date from file.");
                    return(DateTime.MinValue);
                }
            }
            else
            {
                string str = ReadString(xr);

                DateTime dt;
                if (TimeUtil.TryDeserializeUtc(str, out dt))
                {
                    return(dt);
                }
            }

            Debug.Assert(false);
            return(m_dtNow);
        }
Пример #4
0
        public static VariantDictionary Deserialize(byte[] pb)
        {
            if (pb == null)
            {
                Debug.Assert(false); return(null);
            }

            VariantDictionary d = new VariantDictionary();

            using (MemoryStream ms = new MemoryStream(pb, false))
            {
                ushort uVersion = MemUtil.BytesToUInt16(MemUtil.Read(ms, 2));
                if ((uVersion & VdmCritical) > (VdVersion & VdmCritical))
                {
                    throw new FormatException(KLRes.FileNewVerReq);
                }

                while (true)
                {
                    int iType = ms.ReadByte();
                    if (iType < 0)
                    {
                        throw new EndOfStreamException(KLRes.FileCorrupted);
                    }
                    byte btType = (byte)iType;
                    if (btType == (byte)VdType.None)
                    {
                        break;
                    }

                    int    cbName = MemUtil.BytesToInt32(MemUtil.Read(ms, 4));
                    byte[] pbName = MemUtil.Read(ms, cbName);
                    if (pbName.Length != cbName)
                    {
                        throw new EndOfStreamException(KLRes.FileCorrupted);
                    }
                    string strName = StrUtil.Utf8.GetString(pbName, 0, pbName.Length);

                    int    cbValue = MemUtil.BytesToInt32(MemUtil.Read(ms, 4));
                    byte[] pbValue = MemUtil.Read(ms, cbValue);
                    if (pbValue.Length != cbValue)
                    {
                        throw new EndOfStreamException(KLRes.FileCorrupted);
                    }

                    switch (btType)
                    {
                    case (byte)VdType.UInt32:
                        if (cbValue == 4)
                        {
                            d.SetUInt32(strName, MemUtil.BytesToUInt32(pbValue));
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                        break;

                    case (byte)VdType.UInt64:
                        if (cbValue == 8)
                        {
                            d.SetUInt64(strName, MemUtil.BytesToUInt64(pbValue));
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                        break;

                    case (byte)VdType.Bool:
                        if (cbValue == 1)
                        {
                            d.SetBool(strName, (pbValue[0] != 0));
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                        break;

                    case (byte)VdType.Int32:
                        if (cbValue == 4)
                        {
                            d.SetInt32(strName, MemUtil.BytesToInt32(pbValue));
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                        break;

                    case (byte)VdType.Int64:
                        if (cbValue == 8)
                        {
                            d.SetInt64(strName, MemUtil.BytesToInt64(pbValue));
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                        break;

                    case (byte)VdType.String:
                        d.SetString(strName, StrUtil.Utf8.GetString(pbValue, 0, pbValue.Length));
                        break;

                    case (byte)VdType.ByteArray:
                        d.SetByteArray(strName, pbValue);
                        break;

                    default:
                        Debug.Assert(false);                                 // Unknown type
                        break;
                    }
                }

                Debug.Assert(ms.ReadByte() < 0);
            }

            return(d);
        }
Пример #5
0
        private static void TcpProcessMessage(BinaryReader br)
        {
            if (br.ReadByte() != 1)
            {
                Debug.Assert(false); return;
            }

            int cb = MemUtil.BytesToInt32(br.ReadBytes(4));

            if ((cb <= 0) || (cb > IpcTcpMsgSizeMax))
            {
                Debug.Assert(false); return;
            }

            byte[] pbEnc = br.ReadBytes(cb);
            if ((pbEnc == null) || (pbEnc.Length != cb))
            {
                Debug.Assert(false); return;
            }

            const int cbID = (int)PwUuid.UuidSize;

            byte[] pb = CryptoUtil.UnprotectData(pbEnc, IpcTcpOptEnt,
                                                 DataProtectionScope.CurrentUser);
            if ((pb == null) || (pb.Length != (cbID + 8 + 4 + 4)))
            {
                Debug.Assert(false);
                return;
            }

            PwUuid puID = new PwUuid(MemUtil.Mid(pb, 0, cbID));

            foreach (KeyValuePair <PwUuid, DateTime> kvp in g_lOldMsgs)
            {
                if (puID.Equals(kvp.Key))
                {
                    Debug.Assert(false); return;
                }
            }

            DateTime dtNow = DateTime.UtcNow, dtMsg = DateTime.FromBinary(
                MemUtil.BytesToInt64(pb, cbID));

            if ((dtNow - dtMsg).TotalMilliseconds > IpcTcpTtlMs)
            {
                Debug.Assert(false);
                return;
            }

            g_lOldMsgs.Add(new KeyValuePair <PwUuid, DateTime>(puID, dtMsg));

            int      iMsg   = MemUtil.BytesToInt32(pb, cbID + 8);
            int      lParam = MemUtil.BytesToInt32(pb, cbID + 8 + 4);
            MainForm mf     = Program.MainForm;

            VoidDelegate f = delegate()
            {
                try { mf.ProcessAppMessage(new IntPtr(iMsg), new IntPtr(lParam)); }
                catch (Exception) { Debug.Assert(false); }
            };

            mf.Invoke(f);
        }