Beispiel #1
0
        public static void Main(string[] args, KeyStorage keyStorage)
        {
            Console.WriteLine ("Keys");

            if (args.Length == 1) {
                Console.WriteLine ();
                Console.WriteLine ("Public keys:");
                foreach (PublicKey pub in keyStorage.PublicKeys)
                    Console.WriteLine ("	" + pub);
                Console.WriteLine ();
                Console.WriteLine ("Private keys:");
                foreach (PrivateKey priv in keyStorage.PrivateKeys)
                    Console.WriteLine ("	" + priv.ToString ());
                Console.WriteLine ();

                return;
            }

            if (args.Length == 3 && args [1] == "generate") {
                PrivateKey key = PrivateKey.Generate ();
                string name = Path.GetFileName (args [2]);
                keyStorage.Add (name, key);

                Console.WriteLine ();
                Console.WriteLine ("New key: " + name);
                Console.WriteLine (key.ToString ());
                Console.WriteLine ();

                return;
            }

            throw new HelpException ("Missing arguments");
        }
Beispiel #2
0
        public static void Main(string[] args, KeyStorage keyStorage)
        {
            //Usage
            if (args.Length != 4)
                throw new HelpException ("Missing arguments");
            string sourcePath = args [1];
            string repoPath = args [2];
            string receipientName = args [3];

            //Source
            if (Directory.Exists (sourcePath) == false)
                throw new HelpException ("Source directory not found: " + sourcePath);

            //Repo
            Repo repo = Repo.Create (repoPath);

            //Sender and Recipient keys
            PrivateKey senderKey = keyStorage.DefaultKey;
            PublicKey recipientKey = keyStorage.GetPublic (receipientName);

            //Prepare Route message recording of ChunkID
            RouteRepo rr = new RouteRepo (repo);

            //Prepare Encryption
            EncryptedRepo er = new EncryptedRepo (rr, null);
            er.AddKey (recipientKey);

            Console.Write ("Generating Tree...");

            //Send Tree
            ChunkHash tree = TreeChunk.GenerateChunk (sourcePath, er);

            //TreeMessage
            TreeMessage tm = new TreeMessage (tree, Path.GetDirectoryName (sourcePath));
            Chunk tmc = Message.ToChunk (tm, senderKey);
            ChunkHash tmch = er.WriteChunk (tmc);
            er.StoreMessage ("file", tmch);

            //RouteMessage
            RouteMessage rm = rr.RouteMessage;
            rm.MessageChunkHash = tmch.bytes;
            rm.To = receipientName;

            //Store unencrypted RouteMessage
            Chunk rmChunk = Message.ToChunk (rm);
            repo.WriteChunk (rmChunk);
            repo.StoreMessage ("route", rmChunk.ChunkHash);
            Console.WriteLine ("RouteMessage Stored");
        }
Beispiel #3
0
        public static Message FromChunk(Chunk chunk, KeyStorage keyStorage)
        {
            if (chunk == null)
                return null;

            using (MemoryStream ms = new MemoryStream(chunk.Data)) {
                byte[] whisper = new byte[7];
                if (ms.Read (whisper, 0, whisper.Length) != whisper.Length)
                    throw new InvalidDataException ("Header not right length");
                if (Encoding.ASCII.GetString (whisper) != "Whisper")
                    throw new InvalidDataException ("Missing header");

                MessageHeader header = MessageHeader.Deserialize (ProtocolParser.ReadBytes (ms));

                byte[] messageBytes = ProtocolParser.ReadBytes (ms);
                Message message;
                switch (header.MessageId) {
                case 1:
                    message = TreeMessage.Deserialize (messageBytes);
                    break;
                case 2:
                    message = RouteMessage.Deserialize (messageBytes);
                    break;
                case 3:
                    message = ListMessage.Deserialize (messageBytes);
                    break;
                default:
                    throw new NotImplementedException ();
                }

                //Verify signature
                if (header.Signature != null) {
                    foreach (PublicKey key in keyStorage.PublicKeys) {
                        if (key.Verify (messageBytes, header.Signature)) {
                            message.Signature = key;
                            break;
                        }
                    }
                }

                return message;
            }
        }
Beispiel #4
0
        public static void Main(string[] args, KeyStorage keyStorage)
        {
            if (args.Length != 2)
                throw new HelpException ("Missing arguments");

            //Repo
            Repo repo = Repo.Create (args [1]);

            //Find message
            ICollection<ChunkHash > messages = repo.GetMessageList ("file");
            EncryptedRepo es = new EncryptedRepo (repo, keyStorage);

            //Iterate over all messages
            foreach (ChunkHash mid in messages) {
                Console.Write (mid.ToString ().Substring (0, 10) + "... ");

                Message message = Message.FromChunk (es.ReadChunk (mid), keyStorage);

                //No key found
                if (message == null) {
                    Console.WriteLine ("no key");
                    continue;
                }

                if (message.Signature != null)
                    Console.Write ("(signed by " + message.Signature.Name + ") ");

                TreeMessage tm = message as TreeMessage;
                if (tm != null) {
                    Console.WriteLine ("TreeMessage " + tm.Name);
                    continue;
                }

                RouteMessage rm = message as RouteMessage;
                if (rm != null) {
                    Console.WriteLine ("RouteMessage to " + rm.To);
                    continue;
                }

                Console.WriteLine ("unknown message type: " + message.GetType ().Name);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Test reading and writing to repo
        /// </summary>
        public static void Main(string[] args, KeyStorage keyStorage)
        {
            //Test buffer to transmit
            byte[] buffer = new byte[4096];
            Random r = new Random ();
            r.NextBytes (buffer);

            string recipientName = null;

            if (args.Length == 3)
                recipientName = args [2];
            if (args.Length > 3 || args.Length < 2)
                throw new HelpException ("Missing arguments");

            string repoPath = args [1];

            //Repository
            Repo repo = Repo.Create (repoPath);

            //Sender and Recipient keys
            PublicKey recipientKey = null;
            if (recipientName != null) {
                recipientKey = keyStorage.GetPublic (recipientName);
                EncryptedRepo er = new EncryptedRepo (repo, keyStorage);
                er.AddKey (recipientKey);
                repo = er;
            }

            Chunk c = new Chunk (buffer);

            var ch = repo.WriteChunk (c);

            Chunk c2 = repo.ReadChunk (ch);

            for (int n = 0; n < buffer.Length; n++)
                if (buffer [n] != c2.Data [n])
                    throw new InvalidDataException ("Failed at byte " + n);

            Console.WriteLine ("Test succeded");
        }
Beispiel #6
0
        public static void Main(string[] args, KeyStorage keyStorage)
        {
            if (args.Length != 4)
                throw new HelpException ("Missing arguments");

            //Storage
            Repo repo = Repo.Create (args [1]);
            repo = new EncryptedRepo (repo, keyStorage);

            //Find message
            Chunk chunk = null;
            if (args [2].Length == 64) {
                ChunkHash id = ChunkHash.FromHashBytes (Hash.FromString (args [2]).bytes);
                chunk = repo.ReadChunk (id);
            } else {
                ICollection<ChunkHash> messages = repo.GetMessageList ("file");
                foreach (ChunkHash bh in messages) {
                    if (bh.ToString ().StartsWith (args [2]))
                        chunk = repo.ReadChunk (bh);
                }
            }

            Message message = Message.FromChunk (chunk, keyStorage);
            if (message == null) {
                Console.Error.WriteLine ("Message not found");
                return;
            }
            TreeMessage tm = message as TreeMessage;
            if (tm == null) {
                Console.Error.WriteLine ("Not a TreeMessage: " + message.GetType ().Name);
                return;
            }

            Console.WriteLine ("Found TreeMessage " + tm.Name);
            string targetPath = Path.Combine (args [3], tm.Name);
            TreeChunk.Extract (repo, ChunkHash.FromHashBytes (tm.TreeChunkHash), targetPath);
        }
Beispiel #7
0
 /// <summary>
 /// Encrypts all chunks before sending them to the backend repo
 /// </summary>
 /// <param name="backendRepo">
 /// This is where the encrypted chunks are sent
 /// </param>
 /// <param name="keyStorage">
 /// If decrypting, this is where we look for private keys
 /// </param>
 public EncryptedRepo(Repo backendRepo, KeyStorage keyStorage)
     : base(backendRepo)
 {
     this.keyStorage = keyStorage;
 }