Esempio n. 1
0
        internal static string ImageToDataUri(Image img)
        {
            if (img == null)
            {
                Debug.Assert(false); return(string.Empty);
            }

            byte[] pb = null;
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, ImageFormat.Png);
                pb = ms.ToArray();
            }

            return(StrUtil.DataToDataUri(pb, "image/png"));
        }
Esempio n. 2
0
        public static bool Copy(byte[] pbToCopy, string strFormat, bool bIsEntryInfo,
                                IntPtr hOwner)
        {
            if (pbToCopy == null)
            {
                throw new ArgumentNullException("pbToCopy");
            }
            if (pbToCopy.Length == 0)
            {
                Clear(); return(true);
            }

            string strMedia = StrUtil.GetCustomMediaType(strFormat);
            string strData  = StrUtil.DataToDataUri(pbToCopy, strMedia);

            return(Copy(strData, false, bIsEntryInfo, null, null, hOwner));
        }
Esempio n. 3
0
        public static bool Copy(byte[] pbToCopy, string strFormat, bool bEncode,
                                bool bIsEntryInfo, IntPtr hOwner)
        {
            Debug.Assert(pbToCopy != null);
            if (pbToCopy == null)
            {
                throw new ArgumentNullException("pbToCopy");
            }

            if (bIsEntryInfo && !AppPolicy.Try(AppPolicyId.CopyToClipboard))
            {
                return(false);
            }

            string strEnc = null;

            try
            {
                if (bEncode || NativeLib.IsUnix())
                {
                    strEnc = StrUtil.DataToDataUri(pbToCopy,
                                                   ClipFmtToMimeType(strFormat));
                }

                if (!NativeLib.IsUnix())                // Windows
                {
                    if (!OpenW(hOwner, true))
                    {
                        throw new InvalidOperationException();
                    }

                    uint uFormat = NativeMethods.RegisterClipboardFormat(strFormat);

                    bool bFailed = false;
                    if (!AttachIgnoreFormatW())
                    {
                        bFailed = true;
                    }

                    if (!bEncode)
                    {
                        if (!SetDataW(uFormat, pbToCopy))
                        {
                            bFailed = true;
                        }
                    }
                    else                     // Encode
                    {
                        if (!SetDataW(uFormat, strEnc, false))
                        {
                            bFailed = true;
                        }
                    }

                    CloseW();

                    if (bFailed)
                    {
                        return(false);
                    }
                }
                else if (NativeLib.GetPlatformID() == PlatformID.MacOSX)
                {
                    SetStringM(strEnc);
                }
                else if (NativeLib.IsUnix())
                {
                    SetStringU(strEnc);
                }
                // else // Managed, no encoding
                // {
                //	Clear();
                //	DataObject doData = CreateProtectedDataObject(strFormat, pbToCopy);
                //	Clipboard.SetDataObject(doData);
                // }
            }
            catch (Exception) { Debug.Assert(false); return(false); }

            m_strFormat = strFormat;
            m_bEncoded  = (strEnc != null);

            SHA256Managed sha256 = new SHA256Managed();

            // if(strEnc != null)
            //	m_pbDataHash32 = sha256.ComputeHash(StrUtil.Utf8.GetBytes(strEnc));
            // else
            m_pbDataHash32 = sha256.ComputeHash(pbToCopy);

            RaiseCopyEvent(bIsEntryInfo, string.Empty);

            return(true);
        }
Esempio n. 4
0
        private void AutoOpenEntryPriv(AutoExecItem a, bool bManual)
        {
            PwEntry    pe        = a.Entry;
            PwDatabase pdContext = a.Database;

            SprContext ctxNoEsc = new SprContext(pe, pdContext, SprCompileFlags.All);
            SprContext ctxEsc   = new SprContext(pe, pdContext, SprCompileFlags.All,
                                                 false, true);

            string strDb;

            if (!GetString(pe, PwDefs.UrlField, ctxEsc, true, out strDb))
            {
                return;
            }

            IOConnectionInfo ioc = IOConnectionInfo.FromPath(strDb);

            //TODO

            /*if (ioc.IsLocalFile() && !UrlUtil.IsAbsolutePath(strDb))
             *  ioc = IOConnectionInfo.FromPath(UrlUtil.MakeAbsolutePath(
             *      WinUtil.GetExecutable(), strDb));*/
            if (ioc.Path.Length == 0)
            {
                return;
            }

            string strIocUserName;

            if (GetString(pe, "IocUserName", ctxNoEsc, true, out strIocUserName))
            {
                ioc.UserName = strIocUserName;
            }

            string strIocPassword;

            if (GetString(pe, "IocPassword", ctxNoEsc, true, out strIocPassword))
            {
                ioc.Password = strIocPassword;
            }

            if ((strIocUserName.Length != 0) && (strIocPassword.Length != 0))
            {
                ioc.IsComplete = true;
            }

            string str;

            if (GetString(pe, "IocTimeout", ctxNoEsc, true, out str))
            {
                long l;
                if (long.TryParse(str, out l))
                {
                    ioc.Properties.SetLong(IocKnownProperties.Timeout, l);
                }
            }

            bool?ob = GetBoolEx(pe, "IocPreAuth", ctxNoEsc);

            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.PreAuth, ob.Value);
            }

            if (GetString(pe, "IocUserAgent", ctxNoEsc, true, out str))
            {
                ioc.Properties.Set(IocKnownProperties.UserAgent, str);
            }

            ob = GetBoolEx(pe, "IocExpect100Continue", ctxNoEsc);
            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.Expect100Continue, ob.Value);
            }

            ob = GetBoolEx(pe, "IocPassive", ctxNoEsc);
            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.Passive, ob.Value);
            }

            ob = GetBoolEx(pe, "SkipIfNotExists", ctxNoEsc);
            if (!ob.HasValue) // Backw. compat.
            {
                ob = GetBoolEx(pe, "Skip if not exists", ctxNoEsc);
            }
            if (ob.HasValue && ob.Value)
            {
                if (!IOConnection.FileExists(ioc))
                {
                    return;
                }
            }

            CompositeKey ck = new CompositeKey();

            if (GetString(pe, PwDefs.PasswordField, ctxNoEsc, false, out str))
            {
                ck.AddUserKey(new KcpPassword(str));
            }

            if (GetString(pe, PwDefs.UserNameField, ctxNoEsc, false, out str))
            {
                string           strAbs = str;
                IOConnectionInfo iocKey = IOConnectionInfo.FromPath(strAbs);
                if (iocKey.IsLocalFile() && !UrlUtil.IsAbsolutePath(strAbs))
                {
                    //TODO
                    /*      strAbs = UrlUtil.MakeAbsolutePath(WinUtil.GetExecutable(), strAbs);*/
                }


                ob = GetBoolEx(pe, "SkipIfKeyFileNotExists", ctxNoEsc);
                if (ob.HasValue && ob.Value)
                {
                    IOConnectionInfo iocKeyAbs = IOConnectionInfo.FromPath(strAbs);
                    if (!IOConnection.FileExists(iocKeyAbs))
                    {
                        return;
                    }
                }

                try { ck.AddUserKey(new KcpKeyFile(strAbs)); }
                catch (InvalidOperationException)
                {
                    //TODO
                    throw new Exception("TODO");
                    //throw new Exception(strAbs + MessageService.NewParagraph + KPRes.KeyFileError);
                }
                catch (Exception) { throw; }
            }
            else // Try getting key file from attachments
            {
                ProtectedBinary pBin = pe.Binaries.Get("KeyFile.bin");
                if (pBin != null)
                {
                    ck.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromPath(
                                                     StrUtil.DataToDataUri(pBin.ReadData(), null))));
                }
            }

            if (GetString(pe, "KeyProvider", ctxNoEsc, true, out str))
            {
                /*TODO KeyProvider kp = m_host.KeyProviderPool.Get(str);
                 * if (kp == null)
                 *  throw new Exception(@"Unknown key provider: '" + str + @"'!");
                 *
                 * KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(
                 *  ioc, false, false);
                 *
                 * bool bPerformHash = !kp.DirectKey;
                 * byte[] pbProvKey = kp.GetKey(ctxKP);
                 * if ((pbProvKey != null) && (pbProvKey.Length != 0))
                 * {
                 *  ck.AddUserKey(new KcpCustomKey(str, pbProvKey, bPerformHash));
                 *  MemUtil.ZeroByteArray(pbProvKey);
                 * }
                 * else return; // Provider has shown error message*/
                throw new Exception("KeyProvider not supported");
            }

            ob = GetBoolEx(pe, "UserAccount", ctxNoEsc);
            if (ob.HasValue && ob.Value)
            {
                ck.AddUserKey(new KcpUserAccount());
            }

            if (ck.UserKeyCount == 0)
            {
                return;
            }

            GetString(pe, "Focus", ctxNoEsc, true, out str);
            bool bRestoreFocus = str.Equals("Restore", StrUtil.CaseIgnoreCmp);

            /*TODO
             * PwDatabase pdPrev = m_host.MainWindow.ActiveDatabase;
             *
             * m_host.MainWindow.OpenDatabase(ioc, ck, true);
             *
             * if (bRestoreFocus && (pdPrev != null) && !bManual)
             * {
             *  PwDocument docPrev = m_host.MainWindow.DocumentManager.FindDocument(
             *      pdPrev);
             *  if (docPrev != null) m_host.MainWindow.MakeDocumentActive(docPrev);
             *  else { Debug.Assert(false); }
             * }*/
        }
        public static bool AutoOpenEntry(Activity activity, AutoExecItem item, bool bManual,
                                         ActivityLaunchMode launchMode)
        {
            string           str;
            PwEntry          pe       = item.Entry;
            SprContext       ctxNoEsc = new SprContext(pe, item.Database, SprCompileFlags.All);
            IOConnectionInfo ioc;

            if (!TryGetDatabaseIoc(item, out ioc))
            {
                return(false);
            }

            var ob = GetBoolEx(pe, "SkipIfNotExists", ctxNoEsc);

            if (!ob.HasValue) // Backw. compat.
            {
                ob = GetBoolEx(pe, "Skip if not exists", ctxNoEsc);
            }
            if (ob.HasValue && ob.Value)
            {
                if (!CheckFileExsts(ioc))
                {
                    return(false);
                }
            }

            CompositeKey ck = new CompositeKey();

            if (GetString(pe, PwDefs.PasswordField, ctxNoEsc, false, out str))
            {
                ck.AddUserKey(new KcpPassword(str));
            }

            if (GetString(pe, PwDefs.UserNameField, ctxNoEsc, false, out str))
            {
                string           strAbs = str;
                IOConnectionInfo iocKey = IOConnectionInfo.FromPath(strAbs);
                if (iocKey.IsLocalFile() && !UrlUtil.IsAbsolutePath(strAbs))
                {
                    //local relative paths not supported on Android
                    return(false);
                }


                ob = GetBoolEx(pe, "SkipIfKeyFileNotExists", ctxNoEsc);
                if (ob.HasValue && ob.Value)
                {
                    IOConnectionInfo iocKeyAbs = IOConnectionInfo.FromPath(strAbs);
                    if (!CheckFileExsts(iocKeyAbs))
                    {
                        return(false);
                    }
                }

                try { ck.AddUserKey(new KcpKeyFile(strAbs)); }
                catch (InvalidOperationException)
                {
                    Toast.MakeText(Application.Context, Resource.String.error_adding_keyfile, ToastLength.Long).Show();
                    return(false);
                }
                catch (Exception) { throw; }
            }
            else // Try getting key file from attachments
            {
                ProtectedBinary pBin = pe.Binaries.Get("KeyFile.bin");
                if (pBin != null)
                {
                    ck.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromPath(
                                                     StrUtil.DataToDataUri(pBin.ReadData(), null))));
                }
            }

            GetString(pe, "Focus", ctxNoEsc, true, out str);
            bool bRestoreFocus = str.Equals("Restore", StrUtil.CaseIgnoreCmp);

            PasswordActivity.Launch(activity, ioc, ck, launchMode, !
                                    bRestoreFocus);

            App.Kp2a.RegisterChildDatabase(ioc);

            return(true);
        }
Esempio n. 6
0
        private static void ExportGroup(StringBuilder sb, PwGroup pg, uint uIndent,
                                        PwDatabase pd)
        {
            ExportData(sb, uIndent, "<DT><H3", null, false, null, false);
            ExportTimes(sb, pg);
            ExportData(sb, 0, ">", pg.Name, true, "</H3>", true);
            ExportData(sb, uIndent, "<DL><p>", null, false, null, true);

            foreach (PwGroup pgSub in pg.Groups)
            {
                ExportGroup(sb, pgSub, uIndent + 1, pd);
            }

#if DEBUG
            List <string> l = new List <string>();
            l.Add("Tag 1");
            l.Add("Tag 2");
            Debug.Assert(StrUtil.TagsToString(l, false) == "Tag 1;Tag 2");
#endif

            foreach (PwEntry pe in pg.Entries)
            {
                string strUrl = pe.Strings.ReadSafe(PwDefs.UrlField);
                if (strUrl.Length == 0)
                {
                    continue;
                }

                // Encode only when really required; '&' does not need
                // to be encoded
                bool bEncUrl = (strUrl.IndexOfAny(new char[] {
                    '\"', '<', '>'
                }) >= 0);

                ExportData(sb, uIndent + 1, "<DT><A HREF=\"", strUrl, bEncUrl,
                           "\"", false);

                ExportTimes(sb, pe);

                if (!pe.CustomIconUuid.Equals(PwUuid.Zero) && (pd != null))
                {
                    try
                    {
                        Image imgIcon = pd.GetCustomIcon(pe.CustomIconUuid, 16, 16);
                        if (imgIcon != null)
                        {
                            using (MemoryStream msIcon = new MemoryStream())
                            {
                                imgIcon.Save(msIcon, ImageFormat.Png);
                                byte[] pbIcon  = msIcon.ToArray();
                                string strIcon = StrUtil.DataToDataUri(pbIcon,
                                                                       "image/png");

                                ExportData(sb, 0, " ICON=\"", strIcon, false,
                                           "\"", false);
                            }
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                    catch (Exception) { Debug.Assert(false); }
                }

                if (pe.Tags.Count > 0)
                {
                    string strTags = StrUtil.TagsToString(pe.Tags, false);
                    strTags = strTags.Replace(';', ',');                     // Without space

                    ExportData(sb, 0, " TAGS=\"", strTags, true, "\"", false);
                }

                string strTitle = pe.Strings.ReadSafe(PwDefs.TitleField);
                if (strTitle.Length == 0)
                {
                    strTitle = strUrl;
                }

                ExportData(sb, 0, ">", strTitle, true, "</A>", true);

                string strNotes = pe.Strings.ReadSafe(PwDefs.NotesField);
                if (strNotes.Length > 0)
                {
                    ExportData(sb, uIndent + 1, "<DD>", strNotes, true, null, true);
                }
            }

            ExportData(sb, uIndent, "</DL><p>", null, false, null, true);
        }