From c156ce82138a8e352bfd55e07fe51af6c8ed76aa Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 2 Jun 2020 02:57:58 -0500 Subject: [PATCH] work on menus --- cli/translations/spanish.cs | 2 + cli/translations/text.cs | 4 ++ cli/ui/interfaces/encrypt/pubkey.cs | 14 +++++ cli/ui/menus/KeyRingMenu.cs | 57 ++++++++++++++++++++ cli/ui/menus/MainMenu.cs | 3 ++ tests/session/testSessionClean.cs | 19 ++----- tests/session/testSessionEncrypt.cs | 44 +++++++++++++-- treasurechest/chestcrypto/session/session.cs | 12 ++++- 8 files changed, 133 insertions(+), 22 deletions(-) create mode 100644 cli/ui/interfaces/encrypt/pubkey.cs diff --git a/cli/translations/spanish.cs b/cli/translations/spanish.cs index da50405..555ec22 100644 --- a/cli/translations/spanish.cs +++ b/cli/translations/spanish.cs @@ -21,6 +21,8 @@ namespace treasurechestCLI { stringInst.ENCRYPT_MENU_USE_PUBKEY = "Usar clave pública"; stringInst.ENTER_MESSAGE_UNTIL_DONE = "Ingrese su mensaje y termine con -q en una nueva línea."; stringInst.PASSPHRASE = "Frase de contraseña"; + stringInst.ADD_IDENTITY = "Agregar identidad"; + stringInst.CREATE_IDENTITY = "Crear nueva identidad"; } } diff --git a/cli/translations/text.cs b/cli/translations/text.cs index 762afac..2732b1c 100644 --- a/cli/translations/text.cs +++ b/cli/translations/text.cs @@ -23,6 +23,8 @@ namespace treasurechestCLI { public string ENCRYPT_MENU_USE_PUBKEY; public string ENTER_MESSAGE_UNTIL_DONE; public string PASSPHRASE; + public string ADD_IDENTITY; + public string CREATE_IDENTITY; public Strings(){ @@ -48,6 +50,8 @@ namespace treasurechestCLI { ENCRYPT_MENU_USE_PUBKEY = "Use public key"; ENTER_MESSAGE_UNTIL_DONE = "Enter your message and finish with -q on a new line."; PASSPHRASE = "Passphrase"; + ADD_IDENTITY = "Add identity"; + CREATE_IDENTITY = "Create new identity"; break; } diff --git a/cli/ui/interfaces/encrypt/pubkey.cs b/cli/ui/interfaces/encrypt/pubkey.cs new file mode 100644 index 0000000..b6bcacd --- /dev/null +++ b/cli/ui/interfaces/encrypt/pubkey.cs @@ -0,0 +1,14 @@ +using treasurechestCLI.translations; + +namespace treasurechestCLI{ + internal class EncryptWithPubkeyUI{ + + public static void Menu(){ + + + + } + + } + +} \ No newline at end of file diff --git a/cli/ui/menus/KeyRingMenu.cs b/cli/ui/menus/KeyRingMenu.cs index e69de29..af3e07a 100644 --- a/cli/ui/menus/KeyRingMenu.cs +++ b/cli/ui/menus/KeyRingMenu.cs @@ -0,0 +1,57 @@ +using System; +using treasurechestCLI; +using treasurechest.STDIOWrapper; + +namespace treasurechestCLI +{ + + + internal class KeyRingMenu{ + + public KeyRingMenu(){ + int choice = 1; + translations.Strings strings = new translations.Strings(); + string[] options = {strings.ADD_IDENTITY, + strings.CREATE_IDENTITY, + strings.EXPORT_IDENTITY, + strings.RETURN_TO_PREVIOUS_MENU + }; + while (true){ + for (int i = 0; i < options.Length; i++){ + STDIO.O((i + 1).ToString() + ". " + options[i]); + } + try{ + choice = Int32.Parse(System.Console.ReadLine()); + if (choice >= options.Length) throw new System.OverflowException(); + } + catch (System.OverflowException){ + // User being silly with input + STDIO.O(strings.MAIN_MENU_SELECT_INTEGER); + } + catch(System.FormatException){ + // Too lazy to check strings, force them to use int from menu which is faster anyway + STDIO.O(strings.MAIN_MENU_SELECT_INTEGER); + } + catch(System.ArgumentNullException){ + // Can happen when stream closes (e.g. ctrl-d) + // since menu is intended to be directly human interfaced, user probably wants to exit + choice = options.Length; + } + switch(choice){ + case 1: + + break; + case 2: + break; + case 3: + goto breakLoop; + } + } + breakLoop: + return; + } + + } + + +} \ No newline at end of file diff --git a/cli/ui/menus/MainMenu.cs b/cli/ui/menus/MainMenu.cs index 0229e20..5b3df58 100644 --- a/cli/ui/menus/MainMenu.cs +++ b/cli/ui/menus/MainMenu.cs @@ -47,6 +47,9 @@ namespace treasurechestCLI if (choice == 1){ EncryptMenu.enterMenu(strings); } + else if (choice == 3){ + new KeyRingMenu(); + } else if (choice == mainMenuOptionsSize){ // Exit is final option break; diff --git a/tests/session/testSessionClean.cs b/tests/session/testSessionClean.cs index 3c49292..e4ebdf6 100644 --- a/tests/session/testSessionClean.cs +++ b/tests/session/testSessionClean.cs @@ -26,21 +26,10 @@ namespace sessionPrivateTestsCleaning session.setMinimumKeyExpireSeconds(1); session.setMessageDelay((long) 1); session.addPrivate(newK, getFutureTime(2)); - bool atLeastOneLoop = false; - while(true){ - try{ - if (Enumerable.SequenceEqual(session.getLatestPrivateKey(), newK)){ - Thread.Sleep(25); // ms - atLeastOneLoop = true; // key should not be deleted instantly - continue; - } - } - catch(System.ArgumentOutOfRangeException){ - break; - } - session.cleanPrivate(); - } - Assert.IsTrue(atLeastOneLoop); + session.addPrivate(PublicKeyBox.GenerateKeyPair().PrivateKey, getFutureTime(1)); + Thread.Sleep(3); + session.cleanPrivate(); + Assert.IsTrue(session.getAllPrivateKeys().Length == 0); } diff --git a/tests/session/testSessionEncrypt.cs b/tests/session/testSessionEncrypt.cs index 7afc5c6..7ae2918 100644 --- a/tests/session/testSessionEncrypt.cs +++ b/tests/session/testSessionEncrypt.cs @@ -2,6 +2,7 @@ using NUnit.Framework; using System; using System.Linq; using System.Text; +using System.Threading; using chestcrypto.session; using chestcrypto.session.crypto; using chestcrypto.exceptions; @@ -28,12 +29,43 @@ namespace sessionTestEncrypt var ourNew = PublicKeyBox.GenerateKeyPair(); session.addPrivate(ourNew.PrivateKey, getFutureTime(1000)); byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message); - Assert.AreEqual( + Assert.IsTrue( + Enumerable.SequenceEqual( SessionCrypto.decrypt(session, encrypted), message + ) ); } + + [Test] + public void TestDecryptExpired(){ + var us = PublicKeyBox.GenerateKeyPair(); + var them = PublicKeyBox.GenerateKeyPair(); + byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend"); + Session session = new Session(us.PrivateKey, them.PublicKey, true, 5); + session.setMinimumKeyExpireSeconds(1); + session.setMessageDelay((long) 1); + var ourNew = PublicKeyBox.GenerateKeyPair(); + var ourNew2 = PublicKeyBox.GenerateKeyPair(); + session.addPrivate(ourNew.PrivateKey, getFutureTime(1)); + byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message); + session.addPrivate(ourNew2.PrivateKey, getFutureTime(1)); + session.cleanPrivate(); + try{ + Assert.IsFalse( + Enumerable.SequenceEqual( + SessionCrypto.decrypt(session, encrypted), + message + ) + ); + } + catch(System.Security.Cryptography.CryptographicException){ + return; + } + Assert.Fail(); + } + [Test] public void TestDecryptOlderKey(){ var us = PublicKeyBox.GenerateKeyPair(); @@ -45,9 +77,10 @@ namespace sessionTestEncrypt session.addPrivate(ourNew.PrivateKey, getFutureTime(1000)); byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message); session.addPrivate(ourNew2.PrivateKey, getFutureTime(1005)); - Assert.AreEqual( + Assert.IsTrue( + Enumerable.SequenceEqual( SessionCrypto.decrypt(session, encrypted), - message + message) ); } @@ -61,9 +94,10 @@ namespace sessionTestEncrypt Session session = new Session(us.PrivateKey, them.PublicKey, true, 5); session.addPublic(ephemeral.PublicKey, getFutureTime(1000)); byte[] encrypted = SessionCrypto.encrypt(session, message); - Assert.AreEqual( + Assert.IsTrue( + Enumerable.SequenceEqual( Curve25519.decrypt(ephemeral.PrivateKey, us.PublicKey, encrypted), - message + message) ); } diff --git a/treasurechest/chestcrypto/session/session.cs b/treasurechest/chestcrypto/session/session.cs index 068da4b..d77da1a 100644 --- a/treasurechest/chestcrypto/session/session.cs +++ b/treasurechest/chestcrypto/session/session.cs @@ -94,7 +94,7 @@ namespace chestcrypto{ public byte[] getLatestPrivateKey(){ if (ourPrivateKeys.Count == 0 && strictMode) throw new NoSessionKeyAvailable(); - var key = ourPrivateKeys[ourPrivateKeys.Count -1]; + var key = ourPrivateKeys[ourPrivateKeys.Count - 1]; validateTimestamp(key.Item1); return key.Item2; } @@ -127,6 +127,9 @@ namespace chestcrypto{ public void cleanPrivate(){ // Can't use predicate approach because we want to zero out private keys + if (ourPrivateKeys.Count == 0){ + return; + } List remove = new List(); for (int i = 0; i < ourPrivateKeys.Count; i++){ @@ -137,7 +140,12 @@ namespace chestcrypto{ } } foreach(int i in remove){ - ourPrivateKeys.RemoveAt((int) i); + try{ + ourPrivateKeys.RemoveAt((int) i); + } + catch(System.ArgumentOutOfRangeException){ + ourPrivateKeys.Clear(); + } } }