public SettingsPage()
        {
            InitializeComponent();

            m_config = ConfigFactory.GetInstance();

            //initialize to the current settings
            chk_lock_on_suspend.IsToggled = m_config.LockOnSuspend;
            chk_save_on_suspend.IsToggled = m_config.SaveOnSuspend;
            chk_fingerprint.IsToggled     = m_config.UseFingerprint;

            //we need to handle the fingerprint enable separately
            chk_fingerprint.Toggled += new EventHandler <ToggledEventArgs>((o, e) =>
            {
                if (chk_fingerprint.IsToggled)
                {
                    IFingerprint fp = FingerprintFactory.GetInstance();
                    fp.InitReader();
                    if (fp.IsReady())
                    {
                        PasswordPrompt pmt = new PasswordPrompt()
                        {
                            IsNavPage = true, PromptTitle = "Verify your Password", PositiveButtonText = "Verify", RestorePage = true
                        };
                        pmt.OnPromptSaved += new Prompt.PromptClosedEventListener(() =>
                        {
                            if (pmt.Password == null || pmt.Password.Length == 0)
                            {
                                return;
                            }

                            //attempt to decrypt the private key, just as a verification method
                            if (!LocknoteMgr.GetInstance().DecryptPrivateKey(pmt.Password))
                            { //decryption failed, incorrect password was entered
                                NotificationFactory.ShortAlert("Password is incorrect");
                                pmt.Show(((NavigationPage)((HomeMDP)Application.Current.MainPage).Detail));
                                return;
                            }

                            //we verified the password is correct, now we can prompt the user to scan a fingerprint
                            Page back = Application.Current.MainPage;
                            Application.Current.MainPage = new FingerprintPage(new EventHandler((oo, ee) =>
                            {
                                byte[] data = (byte[])oo; //page returns the encrypted password
                                if (data != null)
                                {                         //only if was not skipped
                                  //encrypt the password and save it
                                    ConfigFactory.GetInstance().EncryptedPassword = data;
                                    ConfigFactory.GetInstance().UseFingerprint    = true;
                                    NotificationFactory.ShortAlert("Fingerprint unlock enabled");
                                }
                                else
                                {
                                    ConfigFactory.GetInstance().EncryptedPassword = new byte[] { 0 };
                                    ConfigFactory.GetInstance().UseFingerprint    = false;
                                    chk_fingerprint.IsToggled = false;
                                }
                                Application.Current.MainPage = back;
                            }), fp, pmt.Password);
                        });
                        pmt.OnPromptDismissed += new Prompt.PromptClosedEventListener(() =>
                        {
                            chk_fingerprint.IsToggled = false;
                        });
                        pmt.Show(((NavigationPage)((HomeMDP)Application.Current.MainPage).Detail));
                    }
                }
                else
                {
                    ConfigFactory.GetInstance().EncryptedPassword = new byte[] { 0 };
                    ConfigFactory.GetInstance().UseFingerprint    = false;
                }
            });

            //set the button handlers
            btn_save.Clicked += new EventHandler((o, e) =>
            {
                m_config.LockOnSuspend = chk_lock_on_suspend.IsToggled;
                m_config.SaveOnSuspend = chk_save_on_suspend.IsToggled;

                NotificationFactory.ShortAlert("Settings Saved!");
            });
            btn_change_password.Clicked += new EventHandler((o, e) =>
            {
                PasswordEntryView pep = new PasswordEntryView();
                pep.OnSave           += new EventHandler((oo, ee) =>
                {
                    LocknoteMgr.GetInstance().ReencryptPrivateKey(pep.Text);
                    ((NavigationPage)((HomeMDP)Application.Current.MainPage).Detail).PopAsync();
                    NotificationFactory.ShortAlert("Password changed");
                });
                Xamarin.Forms.ContentPage pg = new ContentPage();
                pg.Content = pep;
                ((NavigationPage)((HomeMDP)Application.Current.MainPage).Detail).PushAsync(pg);
            });
        }
Beispiel #2
0
        /*
         * Decrypt the private key and start the app
         */
        public void ResumeApp()
        {
            //attempt to load the keys if not already loaded
            if (!m_ln.Loaded)
            {
                m_ln.LoadKeys();
            }

            //prompt the user to enter a password to decrypt the private key
            PasswordPrompt p = new PasswordPrompt()
            {
                NegativeButtonVisible = false, PositiveButtonText = "Decrypt"
            };

            p.OnPromptSaved += new Prompt.PromptClosedEventListener(() =>
            {
                if (p.Password == null || p.Password.Length == 0)
                {
                    return;
                }

                //attempt to decrypt the public key
                if (!m_ln.DecryptPrivateKey(p.Password))
                { //decryption failed, incorrect password was entered
                    NotificationFactory.ShortAlert("Password is incorrect");
                    p.Show();
                    return;
                }

                //erase the entered password
                Eraser.SecureErase(p.Password);
                p.Password = "";

                //private key decrypted, start the app normally
                StartAppFinal();
            });
            //check if fingerprint auth is enabled
            if (m_config.UseFingerprint && m_config.EncryptedPassword.Length > 1)
            {
                //prompt for fingerprint
                IFingerprint fp = FingerprintFactory.GetInstance();
                fp.InitReader();
                if (fp.IsReady())
                {
                    Application.Current.MainPage = new FingerprintPage(new EventHandler((oo, ee) =>
                    {
                        byte[] data = (byte[])oo; //page returns the decrypted password

                        //go to password entry if skipped
                        if (data == null)
                        {
                            p.Show();
                            return;
                        }

                        //decrypt the password
                        string pass = Encoding.UTF8.GetString(data);

                        //attempt to decrypt the private key
                        if (!m_ln.DecryptPrivateKey(pass))
                        { //decryption failed
                            return;
                        }

                        //erase the decrypted password
                        Eraser.SecureErase(pass);
                        p.Password = "";

                        //private key decrypted, start the app normally
                        StartAppFinal();
                    }), fp, "");
                }
            }
            else
            {
                p.Show();
            }
        }
        public FirstTimeSetup()
        {
            InitializeComponent();

            //create the password entry view
            PasswordEntryView pep = new PasswordEntryView();

            pep.OnSave += new EventHandler((o, e) =>
            {
                //get the public key ASN1
                SubjectPublicKeyInfo pubki = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(m_keypair.Public);
                //get the private key ASN1
                PrivateKeyInfo privki = PrivateKeyInfoFactory.CreatePrivateKeyInfo(m_keypair.Private);

                //encrypt the private key
                byte[] encPrivKey = Crypto.EncryptKey(privki.GetDerEncoded(), pep.Text);
                m_keypair         = null;

                //delete the old notebooks
                if (Directory.Exists(NoteManager.GetNotebookDir()))
                {
                    Directory.Delete(NoteManager.GetNotebookDir(), true);
                }

                //save the keys to file
                KeyManager.SaveKeys(encPrivKey, pubki.GetDerEncoded());

                //erase the data
                Eraser.SecureErase(encPrivKey);

                //ask if the user wants to use a fingerprint
                IFingerprint fp = FingerprintFactory.GetInstance();
                fp.InitReader();
                if (fp.IsReady())
                {
                    Application.Current.MainPage = new FingerprintPage(new EventHandler((oo, ee) =>
                    {
                        byte[] data = (byte[])oo; //page returns the encrypted password
                        if (data != null)
                        {                         //only if was not skipped
                            //encrypt the password and save it
                            ConfigFactory.GetInstance().EncryptedPassword = data;
                            ConfigFactory.GetInstance().UseFingerprint    = true;
                        }
                        else
                        {
                            ConfigFactory.GetInstance().EncryptedPassword = new byte[] { 0 };
                            ConfigFactory.GetInstance().UseFingerprint    = false;
                        }
                        //trigger the setup complete event
                        if (OnSetupComplete != null)
                        {
                            OnSetupComplete(this, new EventArgs());
                        }
                    }), fp, pep.Text);
                }
                else
                {
                    //trigger the setup complete event
                    if (OnSetupComplete != null)
                    {
                        OnSetupComplete(this, new EventArgs());
                    }
                }
            });

            //create the activity indicator layout
            StackLayout actLayout = new StackLayout()
            {
                VerticalOptions = LayoutOptions.CenterAndExpand
            };
            ActivityIndicator actInd = new ActivityIndicator();;

            actLayout.Children.Add(actInd);
            actLayout.Children.Add(new Label()
            {
                Text = "Generating key pair", TextColor = Color.DarkGray, HorizontalTextAlignment = TextAlignment.Center
            });

            TapRandomizer tapRnd = new TapRandomizer();

            tapRnd.OnRandomized += new EventHandler((o, e) =>
            {
                m_seed = tapRnd.Seed;
                //show wait animation
                actInd.IsRunning = true;
                this.Content     = actLayout;
                //generate the key pair
                Crypto.StartGenerateKeypair(m_seed, new Crypto.GenCompleteEventHandler((keypair) =>
                {
                    m_keypair = keypair;
                    //hide wait animation
                    actInd.IsRunning = false;
                    //show the password entry page
                    this.Content = pep;
                }));
            });

            this.Content = tapRnd;
        }