Example #1
0
        async void EnrollClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                ActionstextBox.Text = "";
                var server = new U2FServerReferenceImpl(
                    new ChallengeGenerator(),
                    dataStore,
                    new BouncyCastleServerCrypto(),
                    new[] { "http://example.com", "https://example.com" });

                var myClient = new U2FClient(
                    new DummySender("http://example.com", new JObject()),
                    keyFactory);

                var signRequests = server.GetSignRequests(EnrollUserName.Text, EnrollAppId.Text);
                var regRequest   = server.GetRegistrationRequest(EnrollUserName.Text, EnrollAppId.Text);
                var cts          = new CancellationTokenSource(TimeSpan.FromMinutes(1));

                ActionstextBox.Text += "Register...\r\n";
                var x = await myClient.Register(new[] { regRequest }, signRequests, cts.Token);

                ActionstextBox.Text += "Register done, sending to server\r\n";

                var serverResp = server.ProcessRegistrationResponse(x, ToUnixTimeMilliseconds(SystemClock.Instance.Now));
                ActionstextBox.Text += "Server OK\r\n";
                ActionstextBox.Text += $"{serverResp}\r\n";
            }
            catch (Exception exception)
            {
                ActionstextBox.Text += "\r\n\r\n" + exception.ToString();
            }
        }
Example #2
0
        public async Task CanEnrollAndAuthenticate()
        {
            var appId     = new AppId(Encoders.Hex.DecodeData("d2e42c173c857991d5e1b6c81f3e07cbb9d5f57431fe41997c9445c14ce61ec4"));
            var challenge = Encoders.Hex.DecodeData("e6425678fbd7d3d8e311fbfb1db8d26c37cf9f16ac81c95848998a76ce3d3768");
            var u2f       = (await U2FClient.GetHIDU2FAsync()).First();

            // Refuse registration
            Debugger.Break();
            var cts = new CancellationTokenSource();

            cts.CancelAfter(5000);
            await Assert.ThrowsAsync <OperationCanceledException>(async() => await u2f.RegisterAsync(challenge, appId, cts.Token));

            // Accept registration
            Debugger.Break();
            var reg = await u2f.RegisterAsync(challenge, appId);

            Assert.NotNull(reg);

            // Refuse login
            Debugger.Break();
            cts = new CancellationTokenSource();
            cts.CancelAfter(5000);
            await Assert.ThrowsAsync <OperationCanceledException>(async() => await u2f.AuthenticateAsync(challenge, appId, reg.KeyHandle, cts.Token));

            // Accept registration
            Debugger.Break();
            var login = await u2f.AuthenticateAsync(challenge, appId, reg.KeyHandle);

            Assert.NotNull(login);
        }
Example #3
0
        private static async Task TestNew2()
        {
            var hidFactory = Win32HidDeviceFactory.Instance;
            var keyFactory = new U2FHidKeyFactory(hidFactory);

            var dataStore = new InMemoryServerDataStore(new GuidSessionIdGenerator());

            LoadDataStore(dataStore);
            var server = new U2FServerReferenceImpl(
                new ChallengeGenerator(),
                dataStore,
                new BouncyCastleServerCrypto(),
                new[] { "http://example.com", "https://example.com" });

            var myClient = new U2FClient(
                new DummySender("http://example.com", new JObject()),
                keyFactory);

            Console.WriteLine("Register or Sign ? (r/s)");
            var mode = Console.ReadLine();

            if (mode == "s")
            {
                var signRequests = server.GetSignRequests("vbfox", "http://example.com");
                Console.WriteLine("Sign requests obtained ({0})", signRequests.Count);
                var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10));
                var x   = await myClient.Sign(signRequests, cts.Token);

                Console.WriteLine("Signature done {0}", x);
                if (x != null)
                {
                    var serverResp = server.ProcessSignResponse(x);
                    Console.WriteLine("Server ok: {0}", serverResp);
                    SaveDataStore(dataStore);
                }
            }
            else if (mode == "r")
            {
                var signRequests = server.GetSignRequests("vbfox", "http://example.com");
                var regRequest   = server.GetRegistrationRequest("vbfox", "http://example.com");
                var cts          = new CancellationTokenSource(TimeSpan.FromMinutes(10));
                var x            = await myClient.Register(new[] { regRequest }, signRequests, cts.Token);

                Console.WriteLine("Authentication done {0}", x);
                if (x != null)
                {
                    var serverResp = server.ProcessRegistrationResponse(x, ToUnixTimeMilliseconds(SystemClock.Instance.Now));
                    Console.WriteLine("Server ok: {0}", serverResp);
                    SaveDataStore(dataStore);
                }
            }
            else
            {
                Console.WriteLine("???");
            }

            Console.ReadLine();
            return;
        }
Example #4
0
        public virtual void Setup()
        {
            keyOperations = new Mock <IKeyOperations>(MockBehavior.Strict);
            sender        = new Mock <ISender>(MockBehavior.Strict);
            sender.SetupGet(x => x.ChannelId).Returns(CHANNEL_ID_JSON);
            sender.SetupGet(x => x.Origin).Returns(ORIGIN);

            var mockClock = new Mock <IClock>(MockBehavior.Strict);

            mockClock.Setup(x => x.Now).Returns(Instant.FromMillisecondsSinceUnixEpoch(0));
            u2FClient = new U2FClient(sender.Object, keyOperations.Object);
        }
Example #5
0
        private static async Task TestNew()
        {
            var hidFactory = Win32HidDeviceFactory.Instance;
            var keyFactory = new U2FHidKeyFactory(hidFactory);
            var keyIds     = await keyFactory.FindAllAsync();

            var keyId = keyIds.First();

            var dataStore = new InMemoryServerDataStore(new GuidSessionIdGenerator());

            LoadDataStore(dataStore);
            var server = new U2FServerReferenceImpl(
                new ChallengeGenerator(),
                dataStore,
                new BouncyCastleServerCrypto(),
                new[] { "http://example.com", "https://example.com" });

            var myClient = new U2FClient(
                new DummySender("http://example.com", new JObject()),
                (o, a, ct) => Task.FromResult(true),
                (o, a, ct) => Task.FromResult(true),
                keyFactory,
                BouncyCastleClientCrypto.Instance);

            var signRequests = server.GetSignRequests("vbfox", "http://example.com");

            var x = await myClient.Sign(signRequests, CancellationToken.None);

            return;


            var origin    = "http://example.com";
            var channelId = new JObject();



            var requestAndClientDatas = signRequests
                                        .Select(signRequest =>
            {
                string clientDataB64;
                var authRequest = U2FClientReferenceImpl.SignRequestToAuthenticateRequest("http://example.com", signRequest, new JObject(),
                                                                                          out clientDataB64, BouncyCastleClientCrypto.Instance);
                return(Tuple.Create(signRequest, clientDataB64, authRequest));
            })
                                        .ToList();

            new U2FClient(
                new DummySender("http://example.com", new JObject()),
                (o, a, ct) => Task.FromResult(true),
                (o, a, ct) => Task.FromResult(true),
                keyFactory,
                BouncyCastleClientCrypto.Instance);



            using (var u2f = await keyId.OpenAsync())
            {
                var key = new U2FDeviceKey(u2f);



                var client = new U2FClientReferenceImpl(
                    BouncyCastleClientCrypto.Instance,
                    new SimpleOriginVerifier(new[] { "http://example.com", "https://example.com" }),
                    new ChannelProvider(),
                    server,
                    key,
                    SystemClock.Instance);


                //client.Register("http://example.com", "vbfox");
                //SaveDataStore(dataStore);

                client.Authenticate("http://example.com", "vbfox");
                SaveDataStore(dataStore);
            }

            Console.WriteLine("Done.");
            Console.ReadLine();
        }