public void CanGenerateParseAndSaveRsaKey()
        {
            RsaKey key  = new RsaKey();
            RsaKey key2 = new RsaKey(key.ToBytes());

            Assert.True(key.ToBytes().SequenceEqual(key2.ToBytes()));
            Assert.True(key.PubKey.ToBytes().SequenceEqual(key2.PubKey.ToBytes()));
            Assert.True(new RsaPubKey(key.PubKey.ToBytes()).ToBytes().SequenceEqual(key2.PubKey.ToBytes()));
            Assert.Throws <FormatException>(() => new RsaKey(new byte[1]));
        }
Exemple #2
0
        async Task ConfigureAsyncCore(TumblerConfiguration conf, ClientInteraction interaction)
        {
            Cooperative = conf.Cooperative;
            ClassicTumblerParameters = conf.ClassicTumblerParameters.Clone();
            Network       = conf.Network;
            LocalEndpoint = conf.Listen;

            bool torConfigured = false;

            if (conf.TorSettings != null)
            {
                Exception error = null;
                try
                {
                    _Resources.Add(await conf.TorSettings.SetupAsync(interaction, conf.TorPath).ConfigureAwait(false));
                    Logs.Configuration.LogInformation("Successfully authenticated to Tor");
                    var torRSA = Path.Combine(conf.DataDir, "Tor.rsa");


                    string privateKey = null;
                    if (File.Exists(torRSA))
                    {
                        privateKey = File.ReadAllText(torRSA, Encoding.UTF8);
                    }

                    TorConnection = conf.TorSettings.CreateTorClient2();
                    _Resources.Add(TorConnection);

                    await TorConnection.ConnectAsync().ConfigureAwait(false);

                    await TorConnection.AuthenticateAsync().ConfigureAwait(false);

                    var result = await TorConnection.RegisterHiddenServiceAsync(conf.Listen, conf.TorSettings.VirtualPort, privateKey).ConfigureAwait(false);

                    if (privateKey == null)
                    {
                        File.WriteAllText(torRSA, result.PrivateKey, Encoding.UTF8);
                        Logs.Configuration.LogWarning($"Tor RSA private key generated to {torRSA}");
                    }

                    var tumblerUri = new TumblerUrlBuilder();
                    tumblerUri.Port = result.HiddenServiceUri.Port;
                    tumblerUri.Host = result.HiddenServiceUri.Host;
                    TumblerUris.Add(tumblerUri);
                    TorUri = tumblerUri.RoutableUri;
                    Logs.Configuration.LogInformation($"Tor configured on {result.HiddenServiceUri}");
                    torConfigured = true;
                }
                catch (ConfigException ex)
                {
                    error = ex;
                }
                catch (TorException ex)
                {
                    error = ex;
                }
                catch (ClientInteractionException)
                {
                }
                if (error != null)
                {
                    Logs.Configuration.LogWarning("Error while configuring Tor hidden service: " + error.Message);
                }
            }

            if (!torConfigured)
            {
                Logs.Configuration.LogWarning("The tumbler is not configured as a Tor Hidden service");
            }

            var rsaFile = Path.Combine(conf.DataDir, "Tumbler.pem");

            if (!File.Exists(rsaFile))
            {
                Logs.Configuration.LogWarning("RSA private key not found, please backup it. Creating...");
                TumblerKey = new RsaKey();
                File.WriteAllBytes(rsaFile, TumblerKey.ToBytes());
                Logs.Configuration.LogInformation("RSA key saved (" + rsaFile + ")");
            }
            else
            {
                Logs.Configuration.LogInformation("RSA private key found (" + rsaFile + ")");
                TumblerKey = new RsaKey(File.ReadAllBytes(rsaFile));
            }

            var voucherFile = Path.Combine(conf.DataDir, "Voucher.pem");

            if (!File.Exists(voucherFile))
            {
                Logs.Configuration.LogWarning("Creation of Voucher Key");
                VoucherKey = new RsaKey();
                File.WriteAllBytes(voucherFile, VoucherKey.ToBytes());
                Logs.Configuration.LogInformation("RSA key saved (" + voucherFile + ")");
            }
            else
            {
                Logs.Configuration.LogInformation("Voucher key found (" + voucherFile + ")");
                VoucherKey = new RsaKey(File.ReadAllBytes(voucherFile));
            }

            ClassicTumblerParameters.ServerKey  = TumblerKey.PubKey;
            ClassicTumblerParameters.VoucherKey = VoucherKey.PubKey;
            ClassicTumblerParametersHash        = ClassicTumblerParameters.GetHash();

            if (conf.AllowInsecure)
            {
                TumblerUris.Add(new TumblerUrlBuilder()
                {
                    Host = LocalEndpoint.Address.ToString(),
                    Port = LocalEndpoint.Port,
                });
            }

            var configurationHash = ClassicTumblerParameters.GetHash();

            foreach (var uri in TumblerUris)
            {
                uri.ConfigurationHash = configurationHash;
            }

            Logs.Configuration.LogInformation("");
            Logs.Configuration.LogInformation($"--------------------------------");
            Logs.Configuration.LogInformation($"Shareable URIs of the running tumbler are:");
            foreach (var uri in TumblerUris)
            {
                Logs.Configuration.LogInformation(uri.ToString());
            }
            Logs.Configuration.LogInformation($"--------------------------------");
            Logs.Configuration.LogInformation("");

            Repository = conf.DBreezeRepository;
            _Resources.Add(Repository);
            Tracker  = conf.Tracker;
            Services = conf.Services;
        }
        private static Tuple <RsaKey, RSAKeyData> LoadRSAKeyData(string dataDir, string keyName, bool noRSAProof)
        {
            RSAKeyData data = new RSAKeyData();
            RsaKey     key  = null;

            {
                var rsaFile = Path.Combine(dataDir, keyName);
                if (!File.Exists(rsaFile))
                {
                    Logs.Configuration.LogWarning("RSA private key not found, please backup it. Creating...");
                    key = new RsaKey();
                    File.WriteAllBytes(rsaFile, key.ToBytes());
                    Logs.Configuration.LogInformation("RSA key saved (" + rsaFile + ")");
                }
                else
                {
                    Logs.Configuration.LogInformation("RSA private key found (" + rsaFile + ")");
                    key = new RsaKey(File.ReadAllBytes(rsaFile));
                }
            }

            data.PublicKey = key.PubKey;


            if (!noRSAProof)
            {
                {
                    var poupard = Path.Combine(dataDir, "ProofPoupard-" + keyName);
                    PoupardSternProof poupardProof = null;
                    if (!File.Exists(poupard))
                    {
                        Logs.Configuration.LogInformation("Creating Poupard Stern proof...");
                        poupardProof = PoupardStern.ProvePoupardStern(key._Key, RSAKeyData.PoupardSetup);
                        MemoryStream  ms = new MemoryStream();
                        BitcoinStream bs = new BitcoinStream(ms, true);
                        bs.ReadWriteC(ref poupardProof);
                        File.WriteAllBytes(poupard, ms.ToArray());
                        Logs.Configuration.LogInformation("Poupard Stern proof created (" + poupard + ")");
                    }
                    else
                    {
                        Logs.Configuration.LogInformation("Poupard Stern Proof found (" + poupard + ")");
                        var           bytes = File.ReadAllBytes(poupard);
                        MemoryStream  ms    = new MemoryStream(bytes);
                        BitcoinStream bs    = new BitcoinStream(ms, false);
                        bs.ReadWriteC(ref poupardProof);
                    }
                    data.PoupardSternProof = poupardProof;
                }

                {
                    var permutation = Path.Combine(dataDir, "ProofPermutation-" + keyName);
                    PermutationTestProof permutationProof = null;
                    if (!File.Exists(permutation))
                    {
                        Logs.Configuration.LogInformation("Creating Permutation Test proof...");
                        permutationProof = PermutationTest.ProvePermutationTest(key._Key, RSAKeyData.PermutationSetup);
                        MemoryStream  ms = new MemoryStream();
                        BitcoinStream bs = new BitcoinStream(ms, true);
                        bs.ReadWriteC(ref permutationProof);
                        File.WriteAllBytes(permutation, ms.ToArray());
                        Logs.Configuration.LogInformation("Permutation Test proof created (" + permutation + ")");
                    }
                    else
                    {
                        Logs.Configuration.LogInformation("Permutation Test Proof found (" + permutation + ")");
                        var           bytes = File.ReadAllBytes(permutation);
                        MemoryStream  ms    = new MemoryStream(bytes);
                        BitcoinStream bs    = new BitcoinStream(ms, false);
                        bs.ReadWriteC(ref permutationProof);
                    }
                    data.PermutationTestProof = permutationProof;
                }
            }
            return(Tuple.Create(key, data));
        }
Exemple #4
0
        async Task ConfigureAsyncCore(TumblerConfiguration conf, ClientInteraction interaction)
        {
            Cooperative = conf.Cooperative;
            ClassicTumblerParameters = conf.ClassicTumblerParameters.Clone();
            Network = conf.Network;
            RPCClient rpcClient = null;

            try
            {
                rpcClient = conf.RPC.ConfigureRPCClient(conf.Network);
            }
            catch
            {
                throw new ConfigException("Please, fix rpc settings in " + conf.ConfigurationFile);
            }

            bool torConfigured = false;

            if (conf.TorSettings != null)
            {
                Exception error = null;
                try
                {
                    _Resources.Add(await conf.TorSettings.SetupAsync(interaction, conf.TorPath).ConfigureAwait(false));
                    Logs.Configuration.LogInformation("Successfully authenticated to Tor");
                    var torRSA = Path.Combine(conf.DataDir, "Tor.rsa");


                    string privateKey = null;
                    if (File.Exists(torRSA))
                    {
                        privateKey = File.ReadAllText(torRSA, Encoding.UTF8);
                    }

                    IPEndPoint routable = GetLocalEndpoint(conf);
                    TorConnection = conf.TorSettings.CreateTorClient2();
                    _Resources.Add(TorConnection);

                    await TorConnection.ConnectAsync().ConfigureAwait(false);

                    await TorConnection.AuthenticateAsync().ConfigureAwait(false);

                    var result = await TorConnection.RegisterHiddenServiceAsync(routable, conf.TorSettings.VirtualPort, privateKey).ConfigureAwait(false);

                    if (privateKey == null)
                    {
                        File.WriteAllText(torRSA, result.PrivateKey, Encoding.UTF8);
                        Logs.Configuration.LogWarning($"Tor RSA private key generated to {torRSA}");
                    }

                    TorUri = result.HiddenServiceUri;
                    Logs.Configuration.LogInformation($"Tor configured on {TorUri.AbsoluteUri}");
                    torConfigured = true;
                }
                catch (ConfigException ex)
                {
                    error = ex;
                }
                catch (TorException ex)
                {
                    error = ex;
                }
                catch (ClientInteractionException)
                {
                }
                if (error != null)
                {
                    Logs.Configuration.LogWarning("Error while configuring Tor hidden service: " + error.Message);
                }
            }

            if (!torConfigured)
            {
                Logs.Configuration.LogWarning("The tumbler is not configured as a Tor Hidden service");
            }

            var rsaFile = Path.Combine(conf.DataDir, "Tumbler.pem");

            if (!File.Exists(rsaFile))
            {
                Logs.Configuration.LogWarning("RSA private key not found, please backup it. Creating...");
                TumblerKey = new RsaKey();
                File.WriteAllBytes(rsaFile, TumblerKey.ToBytes());
                Logs.Configuration.LogInformation("RSA key saved (" + rsaFile + ")");
            }
            else
            {
                Logs.Configuration.LogInformation("RSA private key found (" + rsaFile + ")");
                TumblerKey = new RsaKey(File.ReadAllBytes(rsaFile));
            }

            var voucherFile = Path.Combine(conf.DataDir, "Voucher.pem");

            if (!File.Exists(voucherFile))
            {
                Logs.Configuration.LogWarning("Creation of Voucher Key");
                VoucherKey = new RsaKey();
                File.WriteAllBytes(voucherFile, VoucherKey.ToBytes());
                Logs.Configuration.LogInformation("RSA key saved (" + voucherFile + ")");
            }
            else
            {
                Logs.Configuration.LogInformation("Voucher key found (" + voucherFile + ")");
                VoucherKey = new RsaKey(File.ReadAllBytes(voucherFile));
            }

            ClassicTumblerParameters.ServerKey  = TumblerKey.PubKey;
            ClassicTumblerParameters.VoucherKey = VoucherKey.PubKey;
            ClassicTumblerParametersHash        = ClassicTumblerParameters.GetHash();

            if (TorUri != null)
            {
                TumblerUris.Add(CreateTumblerUri(TorUri));
            }

            foreach (var url in conf.GetUrls())
            {
                TumblerUris.Add(CreateTumblerUri(new Uri(url, UriKind.Absolute)));
            }


            Logs.Configuration.LogInformation("");
            Logs.Configuration.LogInformation($"--------------------------------");
            var uris = String.Join(Environment.NewLine, TumblerUris.ToArray().Select(u => u.AbsoluteUri).ToArray());

            Logs.Configuration.LogInformation($"Shareable URIs of the running tumbler are:");
            foreach (var uri in TumblerUris)
            {
                Logs.Configuration.LogInformation(uri.AbsoluteUri);
            }
            Logs.Configuration.LogInformation($"--------------------------------");
            Logs.Configuration.LogInformation("");

            var dbreeze = new DBreezeRepository(Path.Combine(conf.DataDir, "db2"));

            Repository = dbreeze;
            _Resources.Add(dbreeze);
            Tracker  = new Tracker(dbreeze, Network);
            Services = ExternalServices.CreateFromRPCClient(rpcClient, dbreeze, Tracker);
        }