work on menus

This commit is contained in:
Kevin Froman 2020-06-02 02:57:58 -05:00
parent ccf406a0b3
commit c156ce8213
8 changed files with 133 additions and 22 deletions

View File

@ -21,6 +21,8 @@ namespace treasurechestCLI {
stringInst.ENCRYPT_MENU_USE_PUBKEY = "Usar clave pública"; 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.ENTER_MESSAGE_UNTIL_DONE = "Ingrese su mensaje y termine con -q en una nueva línea.";
stringInst.PASSPHRASE = "Frase de contraseña"; stringInst.PASSPHRASE = "Frase de contraseña";
stringInst.ADD_IDENTITY = "Agregar identidad";
stringInst.CREATE_IDENTITY = "Crear nueva identidad";
} }
} }

View File

@ -23,6 +23,8 @@ namespace treasurechestCLI {
public string ENCRYPT_MENU_USE_PUBKEY; public string ENCRYPT_MENU_USE_PUBKEY;
public string ENTER_MESSAGE_UNTIL_DONE; public string ENTER_MESSAGE_UNTIL_DONE;
public string PASSPHRASE; public string PASSPHRASE;
public string ADD_IDENTITY;
public string CREATE_IDENTITY;
public Strings(){ public Strings(){
@ -48,6 +50,8 @@ namespace treasurechestCLI {
ENCRYPT_MENU_USE_PUBKEY = "Use public key"; ENCRYPT_MENU_USE_PUBKEY = "Use public key";
ENTER_MESSAGE_UNTIL_DONE = "Enter your message and finish with -q on a new line."; ENTER_MESSAGE_UNTIL_DONE = "Enter your message and finish with -q on a new line.";
PASSPHRASE = "Passphrase"; PASSPHRASE = "Passphrase";
ADD_IDENTITY = "Add identity";
CREATE_IDENTITY = "Create new identity";
break; break;
} }

View File

@ -0,0 +1,14 @@
using treasurechestCLI.translations;
namespace treasurechestCLI{
internal class EncryptWithPubkeyUI{
public static void Menu(){
}
}
}

View File

@ -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;
}
}
}

View File

@ -47,6 +47,9 @@ namespace treasurechestCLI
if (choice == 1){ if (choice == 1){
EncryptMenu.enterMenu(strings); EncryptMenu.enterMenu(strings);
} }
else if (choice == 3){
new KeyRingMenu();
}
else if (choice == mainMenuOptionsSize){ else if (choice == mainMenuOptionsSize){
// Exit is final option // Exit is final option
break; break;

View File

@ -26,21 +26,10 @@ namespace sessionPrivateTestsCleaning
session.setMinimumKeyExpireSeconds(1); session.setMinimumKeyExpireSeconds(1);
session.setMessageDelay((long) 1); session.setMessageDelay((long) 1);
session.addPrivate(newK, getFutureTime(2)); session.addPrivate(newK, getFutureTime(2));
bool atLeastOneLoop = false; session.addPrivate(PublicKeyBox.GenerateKeyPair().PrivateKey, getFutureTime(1));
while(true){ Thread.Sleep(3);
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(); session.cleanPrivate();
} Assert.IsTrue(session.getAllPrivateKeys().Length == 0);
Assert.IsTrue(atLeastOneLoop);
} }

View File

@ -2,6 +2,7 @@ using NUnit.Framework;
using System; using System;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using chestcrypto.session; using chestcrypto.session;
using chestcrypto.session.crypto; using chestcrypto.session.crypto;
using chestcrypto.exceptions; using chestcrypto.exceptions;
@ -28,12 +29,43 @@ namespace sessionTestEncrypt
var ourNew = PublicKeyBox.GenerateKeyPair(); var ourNew = PublicKeyBox.GenerateKeyPair();
session.addPrivate(ourNew.PrivateKey, getFutureTime(1000)); session.addPrivate(ourNew.PrivateKey, getFutureTime(1000));
byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message); byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message);
Assert.AreEqual( Assert.IsTrue(
Enumerable.SequenceEqual(
SessionCrypto.decrypt(session, encrypted), SessionCrypto.decrypt(session, encrypted),
message 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] [Test]
public void TestDecryptOlderKey(){ public void TestDecryptOlderKey(){
var us = PublicKeyBox.GenerateKeyPair(); var us = PublicKeyBox.GenerateKeyPair();
@ -45,9 +77,10 @@ namespace sessionTestEncrypt
session.addPrivate(ourNew.PrivateKey, getFutureTime(1000)); session.addPrivate(ourNew.PrivateKey, getFutureTime(1000));
byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message); byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message);
session.addPrivate(ourNew2.PrivateKey, getFutureTime(1005)); session.addPrivate(ourNew2.PrivateKey, getFutureTime(1005));
Assert.AreEqual( Assert.IsTrue(
Enumerable.SequenceEqual(
SessionCrypto.decrypt(session, encrypted), SessionCrypto.decrypt(session, encrypted),
message message)
); );
} }
@ -61,9 +94,10 @@ namespace sessionTestEncrypt
Session session = new Session(us.PrivateKey, them.PublicKey, true, 5); Session session = new Session(us.PrivateKey, them.PublicKey, true, 5);
session.addPublic(ephemeral.PublicKey, getFutureTime(1000)); session.addPublic(ephemeral.PublicKey, getFutureTime(1000));
byte[] encrypted = SessionCrypto.encrypt(session, message); byte[] encrypted = SessionCrypto.encrypt(session, message);
Assert.AreEqual( Assert.IsTrue(
Enumerable.SequenceEqual(
Curve25519.decrypt(ephemeral.PrivateKey, us.PublicKey, encrypted), Curve25519.decrypt(ephemeral.PrivateKey, us.PublicKey, encrypted),
message message)
); );
} }

View File

@ -127,6 +127,9 @@ namespace chestcrypto{
public void cleanPrivate(){ public void cleanPrivate(){
// Can't use predicate approach because we want to zero out private keys // Can't use predicate approach because we want to zero out private keys
if (ourPrivateKeys.Count == 0){
return;
}
List<int> remove = new List<int>(); List<int> remove = new List<int>();
for (int i = 0; i < ourPrivateKeys.Count; i++){ for (int i = 0; i < ourPrivateKeys.Count; i++){
@ -137,8 +140,13 @@ namespace chestcrypto{
} }
} }
foreach(int i in remove){ foreach(int i in remove){
try{
ourPrivateKeys.RemoveAt((int) i); ourPrivateKeys.RemoveAt((int) i);
} }
catch(System.ArgumentOutOfRangeException){
ourPrivateKeys.Clear();
}
}
} }
} }