Exemplo n.º 1
0
        public void Save(AsymmetricCipherKeyPair keypair)
        {
            //create the notebook's directory if it doesn't exist
            Directory.CreateDirectory(NotebookDirectory);

            //create the metadata
            StringWriter wtr    = new StringWriter();
            XmlWriter    xmlWtr = XmlWriter.Create(wtr);

            xmlWtr.WriteStartDocument();
            xmlWtr.WriteStartElement("notebook");
            xmlWtr.WriteElementString("title", m_title);
            foreach (Section sec in m_sections)
            {
                xmlWtr.WriteElementString("section", sec.ID);
                //also save the section
                sec.Save(keypair);
            }
            xmlWtr.WriteEndElement();
            xmlWtr.WriteEndDocument();
            xmlWtr.Close();

            //encrypt the metadata
            byte[] rawMetadata = Encoding.UTF8.GetBytes(wtr.GetStringBuilder().ToString());
            byte[] encMetadata = Crypto.AsymmetricEncrypt(rawMetadata, ref keypair);
            Eraser.SecureErase(rawMetadata);

            //write the encrypted metadata to a file
            string absPathMetadata = Path.Combine(NotebookDirectory, (string)Application.Current.Resources["MetadataFile"]);

            File.WriteAllBytes(absPathMetadata, encMetadata);
        }
Exemplo n.º 2
0
 public void SecureErase()
 {
     Eraser.SecureErase(m_encPrivKey);
     Eraser.SecureErase(m_pubKey);
     Eraser.SecureErase(m_decPrivKey);
     Eraser.SecureErase(m_mgr);
     m_encPrivKey = null;
     m_pubKey     = null;
     m_decPrivKey = null;
     m_mgr.ClearHandlers();
 }
Exemplo n.º 3
0
        public bool Load(AsymmetricCipherKeyPair keypair)
        {
            //load the encrypted metadata
            string absPathMetadata = Path.Combine(NotebookDirectory, (string)Application.Current.Resources["MetadataFile"]);

            byte[] encMetadata = File.ReadAllBytes(absPathMetadata);

            //decrypt the metadata
            byte[] decMetadata = Crypto.AsymmetricDecrypt(encMetadata, ref keypair);
            if (decMetadata == null)
            {
                return(false);
            }

            //get the metadata
            string metadata = Encoding.UTF8.GetString(decMetadata);

            //start parsing the metadata
            XmlReader rdr = XmlReader.Create(new StringReader(metadata));

            //check for the root tag
            if (!rdr.ReadToFollowing("notebook"))
            {
                return(false);
            }

            //get the notebook title
            if (!rdr.ReadToFollowing("title"))
            {
                return(false);
            }
            rdr.Read();
            this.Title = rdr.Value;

            //get the sections
            while (rdr.ReadToFollowing("section"))
            {
                rdr.Read();
                Section newSection = new Section(NotebookDirectory, rdr.Value);
                if (!newSection.Load(keypair))
                {
                    return(false);
                }
                m_sections.Add(newSection);
            }
            Eraser.SecureErase(metadata);
            Eraser.SecureErase(decMetadata);

            return(true);
        }
Exemplo n.º 4
0
        public bool Load(AsymmetricCipherKeyPair keypair)
        {
            //read the encrypted metadata from file
            byte[] encMetadata = File.ReadAllBytes(PageFile);

            //decrypt the metadata
            byte[] decMetadata = Crypto.AsymmetricDecrypt(encMetadata, ref keypair);
            if (decMetadata == null)
            {
                return(false);
            }

            //start parsing the metadata
            string    metadata = Encoding.UTF8.GetString(decMetadata);
            XmlReader rdr      = XmlReader.Create(new StringReader(metadata));

            //check for page root tag
            if (!rdr.ReadToFollowing("page"))
            {
                return(false);
            }

            //get the page title
            if (!rdr.ReadToFollowing("title"))
            {
                return(false);
            }
            rdr.Read();
            this.Title = rdr.Value;

            //get the page content
            if (!rdr.ReadToFollowing("content"))
            {
                return(false);
            }
            rdr.Read();
            this.Content = rdr.Value;

            //close the reader
            rdr.Close();
            Eraser.SecureErase(decMetadata);
            Eraser.SecureErase(metadata);

            return(true);
        }
Exemplo n.º 5
0
        public void Save(AsymmetricCipherKeyPair keypair)
        {
            //create the metadata
            StringWriter wtr    = new StringWriter();
            XmlWriter    xmlWtr = XmlWriter.Create(wtr);

            xmlWtr.WriteStartDocument();
            xmlWtr.WriteStartElement("page");
            xmlWtr.WriteElementString("title", m_title);
            xmlWtr.WriteElementString("content", m_content);
            xmlWtr.WriteEndElement();
            xmlWtr.WriteEndDocument();
            xmlWtr.Close();

            //encrypt the metadata
            byte[] rawMetadata = Encoding.UTF8.GetBytes(wtr.GetStringBuilder().ToString());
            byte[] encMetadata = Crypto.AsymmetricEncrypt(rawMetadata, ref keypair);
            Eraser.SecureErase(rawMetadata);

            //write the encrypted metadata to a file
            File.WriteAllBytes(PageFile, encMetadata);
        }
Exemplo n.º 6
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();
            }
        }
Exemplo n.º 7
0
        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;
        }