public void CanEnrollAndAuthenticate() { var appId = new AppId(Encoders.Hex.DecodeData("d2e42c173c857991d5e1b6c81f3e07cbb9d5f57431fe41997c9445c14ce61ec4")); var challenge = Encoders.Hex.DecodeData("e6425678fbd7d3d8e311fbfb1db8d26c37cf9f16ac81c95848998a76ce3d3768"); U2FClient u2f = U2FClient.GetHIDU2F().First(); // Refuse registration Debugger.Break(); CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(5000); Assert.Throws <OperationCanceledException>(() => u2f.Register(challenge, appId, cts.Token)); // Accept registration Debugger.Break(); var reg = u2f.Register(challenge, appId); Assert.NotNull(reg); // Refuse login Debugger.Break(); cts = new CancellationTokenSource(); cts.CancelAfter(5000); Assert.Throws <OperationCanceledException>(() => u2f.Authenticate(challenge, appId, reg.KeyHandle, cts.Token)); // Accept registration Debugger.Break(); var login = u2f.Authenticate(challenge, appId, reg.KeyHandle); Assert.NotNull(login); Assert.True(login.UserPresence); }
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(); } }
public async Task Register() { var keySignRequests = new[] { new KeySignRequest(U2FVersion.V2, BROWSER_DATA_SIGN_SHA256, APP_ID_SIGN_SHA256, KEY_HANDLE) }; var keyRegisterRequests = new[] { new KeyRegisterRequest(APP_ID_ENROLL_SHA256, BROWSER_DATA_ENROLL_SHA256) }; keyOperations .Setup( x => x.Register( It.Is <ICollection <KeyRegisterRequest> >(reqs => reqs.SequenceEqual(keyRegisterRequests)), It.Is <ICollection <KeySignRequest> >(reqs => reqs.SequenceEqual(keySignRequests)), It.IsAny <CancellationToken>())) .Returns( (ICollection <KeyRegisterRequest> registerReqs, ICollection <KeySignRequest> signReqs, CancellationToken ct) => Task.FromResult( RegisterOperationResult.Success( registerReqs.Single(), new KeyRegisterResponse( USER_PUBLIC_KEY_ENROLL_HEX, KEY_HANDLE, VENDOR_CERTIFICATE, SIGNATURE_ENROLL)))); var signRequest = new SignRequest(U2FConsts.U2Fv2, SERVER_CHALLENGE_SIGN_BASE64, APP_ID_SIGN, KEY_HANDLE_BASE64, SESSION_ID); var registerRequest = new RegisterRequest(U2FConsts.U2Fv2, SERVER_CHALLENGE_ENROLL_BASE64, APP_ID_ENROLL, SESSION_ID); var result = await u2FClient.Register(new [] { registerRequest }, new[] { signRequest }, CancellationToken.None); Assert.AreEqual(new RegisterResponse(REGISTRATION_DATA_BASE64, BROWSER_DATA_ENROLL_BASE64, SESSION_ID), result); }
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; }