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.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";
}
}

View File

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

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){
EncryptMenu.enterMenu(strings);
}
else if (choice == 3){
new KeyRingMenu();
}
else if (choice == mainMenuOptionsSize){
// Exit is final option
break;

View File

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

View File

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

View File

@ -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<int> remove = new List<int>();
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();
}
}
}