Esempio n. 1
0
        public CampfireNetClientBuilder WithDevelopmentNetworkClaims()
        {
            var rootRsa      = __HackPrivateKeyUtilities.DeserializePrivateKey(__HackPrivateKeyUtilities.__HACK_ROOT_PRIVATE_KEY);
            var rootIdentity = new Identity(rootRsa, new IdentityManager(), "hack_root");

            rootIdentity.GenerateRootChain();

            identity = new Identity(new IdentityManager(), "SomeAndroidIdentityName");
            identity.AddTrustChain(rootIdentity.GenerateNewChain(identity.PublicIdentity, Permission.All, Permission.All, identity.Name));
            return(this);
        }
Esempio n. 2
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Ok && data != null)
            {
                if (requestCode == CHOOSE_TRUST_CHAIN_FILE)
                {
                    var fullPath = data.Data.Path;

                    byte[] trustChainBytes = File.ReadAllBytes(fullPath);

                    try {
                        TrustChainNode[] nodes = TrustChainUtil.SegmentChain(trustChainBytes);

                        if (TrustChainUtil.ValidateTrustChain(nodes))
                        {
                            identity.AddTrustChain(trustChainBytes);
                        }
                        else
                        {
                            Toast.MakeText(this, "Could not validate the trust chain", ToastLength.Short).Show();
                        }
                    } catch (CryptographicException) {
                        Toast.MakeText(this, "Could not validate the trust chain", ToastLength.Short).Show();
                    }
                }
                else if (requestCode == CHOOSE_IDENTITY_FILE)
                {
                    var fullPath = data.Data.Path;
                    Console.WriteLine($"got path {fullPath}");

                    byte[] newIdentity = File.ReadAllBytes(fullPath);

                    if (newIdentity.Length != CryptoUtil.ASYM_KEY_SIZE_BYTES)
                    {
                        Toast.MakeText(this, "Identity is invalid length", ToastLength.Short).Show();
                    }
                    else
                    {
                        // TODO add changeable permissions
                        var newChainBytes = identity.GenerateNewChain(newIdentity, identity.PermissionsHeld,
                                                                      identity.PermissionsGrantable, "Child");

                        var trustChainFileName = string.Format(TRUST_CHAIN_FORMAT_STRING,
                                                               IdentityManager.GetIdentityString(newIdentity));
                        var trustChainFullPath = Path.Combine(privatePath, trustChainFileName);
                        File.WriteAllBytes(trustChainFullPath, newChainBytes);

                        startEmail("Trust chain", trustChainFullPath);
                    }
                }
            }
        }
Esempio n. 3
0
        public static void Main()
        {
//         // Generate root pk
//         var rsa = new RSACryptoServiceProvider(CryptoUtil.ASYM_KEY_SIZE_BITS);
//		   var bytes = __HackPrivateKeyUtilities.SerializePrivateKey(rsa);
//         Console.WriteLine($"new byte[] {{ {string.Join(", ", bytes)} }}");

            var rootRsa      = __HackPrivateKeyUtilities.DeserializePrivateKey(__HackPrivateKeyUtilities.__HACK_ROOT_PRIVATE_KEY);
            var rootIdentity = new Identity(rootRsa, new IdentityManager(), "hack_root");

            rootIdentity.GenerateRootChain();

//         Console.WriteLine("Enter key to begin");
            Console.ReadLine();
            Console.Clear();

            using (var adapter = new WindowsBluetoothAdapter()) {
                var broadcastMessageSerializer = new BroadcastMessageSerializer();
                var objectStore = new InMemoryCampfireNetObjectStore();
                //            var objectStore = new FileSystemCampfireNetObjectStore(Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName, "demo_store"));

                var identity = new Identity(new IdentityManager(), "Windows_Client");
                identity.AddTrustChain(rootIdentity.GenerateNewChain(identity.PublicIdentity, Permission.None, Permission.None, identity.Name));
                Console.WriteLine($"I am {string.Join(" > ", identity.TrustChain.Select(n => n.ThisId.ToHexString()))}");

                var clientMerkleTreeFactory = new ClientMerkleTreeFactory(broadcastMessageSerializer, objectStore);
                var client = new CampfireNetClient(identity, adapter, broadcastMessageSerializer, clientMerkleTreeFactory);
                client.RunAsync().Forget();

                client.MessageReceived += e => {
                    var s = Encoding.UTF8.GetString(e.Message.DecryptedPayload, 0, e.Message.DecryptedPayload.Length);
                    DebugConsole.WriteLine(new string(' ', Console.BufferWidth - 1), ConsoleColor.White, ConsoleColor.Red);
                    DebugConsole.WriteLine(("RECV: " + s).PadRight(Console.BufferWidth - 1), ConsoleColor.White, ConsoleColor.Red);
                    DebugConsole.WriteLine(new string(' ', Console.BufferWidth - 1), ConsoleColor.White, ConsoleColor.Red);
                };

                Console.WriteLine("My adapter id is: " + adapter.AdapterId + " AKA " + string.Join(" ", adapter.AdapterId.ToByteArray()));
                while (true)
                {
                    var line = Console.ReadLine();
                    client.BroadcastAsync(Encoding.UTF8.GetBytes(line)).Forget();
                }

                new ManualResetEvent(false).WaitOne();
            }
        }
Esempio n. 4
0
        public void LoadTrustChains(Identity identity)
        {
            var privateFolder  = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var trustChainPath = Path.Combine(privateFolder, $"trust_chain_{IdentityManager.GetIdentityString(identity.PublicIdentityHash)}.bin");

            bool hasTrustChain = File.Exists(trustChainPath);

            if (hasTrustChain)
            {
                identity.AddTrustChain(File.ReadAllBytes(trustChainPath));

                var files = Directory.GetFiles(privateFolder);

                int   numValidChains = 0;
                Regex fileRegex      = new Regex(TRUST_CHAIN_FILE_REGEX);
                foreach (var file in files)
                {
                    var match = fileRegex.Match(file);
                    if (match.Success)
                    {
                        byte[] trustChain = null;

                        try {
                            trustChain = File.ReadAllBytes(file);
                            if (identity.ValidateAndAdd(trustChain))
                            {
                                numValidChains++;
                            }
                        } catch (CryptographicException) { }

                        if (trustChain == null)
                        {
                            File.Delete(file);
                        }
                    }
                }

                Toast.MakeText(ApplicationContext, $"Loaded {numValidChains} saved identities", ToastLength.Short).Show();
            }
            else
            {
                Toast.MakeText(ApplicationContext, $"No trust chain found", ToastLength.Short).Show();
            }
        }
Esempio n. 5
0
        public void Startup()
        {
            var nativeBluetoothAdapter  = Helper.EnableBluetooth(this);
            var androidBluetoothAdapter = new AndroidBluetoothAdapterFactory().Create(this, ApplicationContext, nativeBluetoothAdapter);

            var prefs = Application.Context.GetSharedPreferences("CampfireChat", FileCreationMode.Private);

            var userName = prefs.GetString("Name", null);

            if (userName == null)
            {
                Globals.CampfireNetClient = CampfireNetClientBuilder.CreateNew()
                                            .WithBluetoothAdapter(androidBluetoothAdapter)
                                            .Build();
                Helper.UpdateIdentity(prefs, Globals.CampfireNetClient.Identity);
            }
            else
            {
                var rsa        = Helper.InitRSA(prefs);
                var identity   = new Identity(new IdentityManager(), rsa, userName);
                var trustChain = prefs.GetString("TC", null);
                if (trustChain != null)
                {
                    identity.AddTrustChain(Helper.HexStringToByteArray(trustChain));
                }
                Globals.CampfireNetClient = CampfireNetClientBuilder.CreateNew()
                                            .WithBluetoothAdapter(androidBluetoothAdapter)
                                            .WithIdentity(identity).Build();
            }

            if (Globals.CampfireChatClient == null)
            {
                Globals.CampfireChatClient = CampfireChatClientFactory.Create(Globals.CampfireNetClient);
                Globals.CampfireNetClient.RunAsync().Forget();
            }

            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
        }