From 4a4eb1cc61c48a943f6295e974d6d50f1704efd5 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 17 May 2020 02:49:36 -0500 Subject: [PATCH] work on keyring identity storage --- tests/doublekeyPrivateTest.cs | 4 +- tests/doublekeyPublicTest.cs | 4 +- tests/keyring/add.cs | 45 ++++++++++++++++--- .../chestcrypto/identity/identity.cs | 2 +- .../chestcrypto/keys/doublekey/exceptions.cs | 37 +++++++++++---- .../chestcrypto/keys/doublekey/private.cs | 6 +-- .../chestcrypto/keys/doublekey/public.cs | 10 ++++- treasurechest/keyring/keyring.cs | 20 ++++++++- 8 files changed, 103 insertions(+), 25 deletions(-) diff --git a/tests/doublekeyPrivateTest.cs b/tests/doublekeyPrivateTest.cs index 4845494..2d34615 100644 --- a/tests/doublekeyPrivateTest.cs +++ b/tests/doublekeyPrivateTest.cs @@ -25,7 +25,7 @@ namespace DoubleKeyPrivateTests new chestcrypto.DoublePrivateKey(invalid); success = true; } - catch (chestcrypto.InvalidDoubleKeyException){ + catch (chestcrypto.exceptions.InvalidDoubleKeyException){ Console.WriteLine("Throws properly for too small array size"); } if (success){ @@ -36,7 +36,7 @@ namespace DoubleKeyPrivateTests new chestcrypto.DoublePrivateKey(invalid2); success = true; } - catch (chestcrypto.InvalidDoubleKeyException){ + catch (chestcrypto.exceptions.InvalidDoubleKeyException){ Console.WriteLine("Throws properly for too large array size"); } if (success){ diff --git a/tests/doublekeyPublicTest.cs b/tests/doublekeyPublicTest.cs index d40b9df..fe3a936 100644 --- a/tests/doublekeyPublicTest.cs +++ b/tests/doublekeyPublicTest.cs @@ -25,7 +25,7 @@ namespace DoubleKeyPublicTests new chestcrypto.DoublePublicKey(invalid); success = true; } - catch (chestcrypto.InvalidDoubleKeyException){ + catch (chestcrypto.exceptions.InvalidDoubleKeyException){ Console.WriteLine("Throws properly for too small array size"); } if (success){ @@ -36,7 +36,7 @@ namespace DoubleKeyPublicTests new chestcrypto.DoublePublicKey(invalid2); success = true; } - catch (chestcrypto.InvalidDoubleKeyException){ + catch (chestcrypto.exceptions.InvalidDoubleKeyException){ Console.WriteLine("Throws properly for too large array size"); } if (success){ diff --git a/tests/keyring/add.cs b/tests/keyring/add.cs index 9584744..2b570a4 100644 --- a/tests/keyring/add.cs +++ b/tests/keyring/add.cs @@ -5,6 +5,7 @@ using Sodium; using System.Collections.Generic; using keyring; using chestcrypto; +using chestcrypto.exceptions; namespace KeyRingTests { @@ -15,6 +16,38 @@ namespace KeyRingTests { } + [Test] + public void TestKeyRingStoreNoDupe(){ + string tempFile = Path.GetTempFileName(); + + DoublePublicKey getKey(){ + KeyRing keyRing = new KeyRing(); + byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey; + byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PublicKey; + + byte[] combinedKey = new byte[signingKey.Length + encryptionKey.Length]; + Buffer.BlockCopy(signingKey, 0, combinedKey, 0, signingKey.Length); + Buffer.BlockCopy(encryptionKey, 0, combinedKey, signingKey.Length, encryptionKey.Length); + DoublePublicKey combo = new DoublePublicKey(signingKey, encryptionKey); + return combo; + } + DoublePublicKey combo = getKey(); + KeyRing keyRing = new KeyRing(); + keyRing.addPublicKey(combo); + try{ + keyRing.addPublicKey(combo); + Assert.Fail(); + } + catch(DuplicateIdentityException){ + + } + + List storedKeys = keyRing.getIdentityPublicKeys(); + if (storedKeys.Count != 1){ + Assert.Fail(); + } + } + [Test] public void TestKeyRingStore() { @@ -33,16 +66,18 @@ namespace KeyRingTests bool success = false; storedKeys.ForEach(delegate(byte[] key) { - if (key.Equals(combinedKey)){ - success = true; + for (int x = 0; x < combinedKey.Length; x++){ + if (combinedKey[x] == key[x]){ + success = true; + continue; + } + success = false; } + }); if (! success){ Assert.Fail(); } - - } - } } \ No newline at end of file diff --git a/treasurechest/chestcrypto/identity/identity.cs b/treasurechest/chestcrypto/identity/identity.cs index b9df173..bd26579 100644 --- a/treasurechest/chestcrypto/identity/identity.cs +++ b/treasurechest/chestcrypto/identity/identity.cs @@ -17,7 +17,7 @@ namespace chestcrypto{ } public Identity(DoublePublicKey publicKey){ - + this.publicKey = publicKey; } public Identity(DoublePrivateKey privateKey){ diff --git a/treasurechest/chestcrypto/keys/doublekey/exceptions.cs b/treasurechest/chestcrypto/keys/doublekey/exceptions.cs index 40a6e44..32d48ee 100644 --- a/treasurechest/chestcrypto/keys/doublekey/exceptions.cs +++ b/treasurechest/chestcrypto/keys/doublekey/exceptions.cs @@ -2,20 +2,39 @@ using System; namespace chestcrypto { - public class InvalidDoubleKeyException : Exception - { - public InvalidDoubleKeyException() + namespace exceptions{ + public class DuplicateIdentityException : Exception { + public DuplicateIdentityException() + { + } + + public DuplicateIdentityException(string message) + : base(message) + { + } + + public DuplicateIdentityException(string message, Exception inner) + : base(message, inner) + { + } } - public InvalidDoubleKeyException(string message) - : base(message) + public class InvalidDoubleKeyException : Exception { - } + public InvalidDoubleKeyException() + { + } - public InvalidDoubleKeyException(string message, Exception inner) - : base(message, inner) - { + public InvalidDoubleKeyException(string message) + : base(message) + { + } + + public InvalidDoubleKeyException(string message, Exception inner) + : base(message, inner) + { + } } } diff --git a/treasurechest/chestcrypto/keys/doublekey/private.cs b/treasurechest/chestcrypto/keys/doublekey/private.cs index 6b09973..a6d20d6 100644 --- a/treasurechest/chestcrypto/keys/doublekey/private.cs +++ b/treasurechest/chestcrypto/keys/doublekey/private.cs @@ -14,10 +14,10 @@ namespace chestcrypto{ public DoublePrivateKey(byte[] sign, byte[] encrypt){ if (sign.Length != 64){ - throw new InvalidDoubleKeyException("Signing private key must be 64 bytes in length."); + throw new exceptions.InvalidDoubleKeyException("Signing private key must be 64 bytes in length."); } if (encrypt.Length != 32){ - throw new InvalidDoubleKeyException("Signing private key must be 32 bytes in length."); + throw new exceptions.InvalidDoubleKeyException("Signing private key must be 32 bytes in length."); } signingPrivateKey = sign; encryptPrivateKey = encrypt; @@ -25,7 +25,7 @@ namespace chestcrypto{ public DoublePrivateKey(byte[] combinedKey){ if (combinedKey.Length != 96){ - throw new InvalidDoubleKeyException("Invalid key length, must be 96 bytes in length"); + throw new exceptions.InvalidDoubleKeyException("Invalid key length, must be 96 bytes in length"); } for (int i = 0; i < combinedKey.Length; i++){ if (i < 64){ diff --git a/treasurechest/chestcrypto/keys/doublekey/public.cs b/treasurechest/chestcrypto/keys/doublekey/public.cs index 1d70181..b70f44b 100644 --- a/treasurechest/chestcrypto/keys/doublekey/public.cs +++ b/treasurechest/chestcrypto/keys/doublekey/public.cs @@ -11,12 +11,18 @@ namespace chestcrypto{ public byte[] getRawDouble(){ return ByteCombiner.Combine(signingPublicKey, encryptPublicKey); } + public byte[] getSigningPublicKey(){ + return signingPublicKey; + } + public byte[] getEncryptPublicKey(){ + return encryptPublicKey; + } public DoublePublicKey(byte[] sign, byte[] encrypt){ // Construct double key from two separate byte arrays if (sign.Length != 32 || encrypt.Length != 32){ - throw new InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes"); + throw new exceptions.InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes"); } signingPublicKey = sign; encryptPublicKey = encrypt; @@ -25,7 +31,7 @@ namespace chestcrypto{ public DoublePublicKey(byte[] joinedKey){ // Construct double key from one bytearray if (joinedKey.Length != 64){ - throw new InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes"); + throw new exceptions.InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes"); } for (int i = 0; i < joinedKey.Length; i++){ if (i < 32){ diff --git a/treasurechest/keyring/keyring.cs b/treasurechest/keyring/keyring.cs index 6928f7f..019e273 100644 --- a/treasurechest/keyring/keyring.cs +++ b/treasurechest/keyring/keyring.cs @@ -1,5 +1,6 @@ using chestcrypto; using chestcrypto.identity; +using chestcrypto.exceptions; using System.Collections.Generic; namespace keyring{ @@ -9,16 +10,29 @@ namespace keyring{ private string storageFile = null; private List identities = new List(); + private bool identityExists(Identity iden){ + bool success = false; + identities.ForEach(delegate(Identity ident) + { + if (ident.getDoublePublicKey().Equals(iden.getDoublePublicKey())){ + success = true; + return; + } + }); + return success; + } + public KeyRing(string storageFile){ } public KeyRing(){} public List getIdentityPublicKeys(){ - List pubKeys; + List pubKeys = new List(); identities.ForEach(delegate(Identity identity){ pubKeys.Add(identity.getDoublePublicKey().getRawDouble()); }); + return pubKeys; } public void addPublicKey(DoublePublicKey key){ @@ -26,7 +40,11 @@ namespace keyring{ Identity newIdentity = new Identity(key); + if (identityExists(newIdentity)){ + throw new DuplicateIdentityException("An identity with that public key already exists"); + } + identities.Add(newIdentity); }