public void SerializeObject()
        {
            var fidoAppId = new FidoAppId("http://example.com");
            var serialized = JsonConvert.SerializeObject(fidoAppId);

            Assert.AreEqual("\"http://example.com\"", serialized);
        }
Ejemplo n.º 2
0
        private void VerifyResponseSignature(FidoAppId appId, FidoRegistrationData registrationData, FidoClientData clientData)
        {
            if (appId == null)
            {
                throw new ArgumentNullException("appId");
            }
            if (registrationData == null)
            {
                throw new ArgumentNullException("registrationData");
            }
            if (clientData == null)
            {
                throw new ArgumentNullException("clientData");
            }

            if (String.IsNullOrEmpty(clientData.RawJsonValue))
            {
                throw new InvalidOperationException("Client data has no JSON representation");
            }

            var signedBytes = PackBytes(
                new byte[] { 0 },
                Helpers.Sha256(appId.ToString()),
                Helpers.Sha256(clientData.RawJsonValue),
                registrationData.KeyHandle.ToByteArray(),
                registrationData.UserPublicKey.ToByteArray());

            VerifySignature(registrationData.AttestationCertificate, registrationData.Signature, signedBytes);
        }
Ejemplo n.º 3
0
        public FidoStartedRegistration StartRegistration(FidoAppId appId)
        {
            var challengeBytes = _generateFidoChallenge.GenerateChallenge();
            var challenge      = WebSafeBase64Converter.ToBase64String(challengeBytes);

            return(new FidoStartedRegistration(appId, challenge));
        }
Ejemplo n.º 4
0
        public ActionResult Login(string keyHandle)
        {
            var model = new LoginDeviceViewModel {
                KeyHandle = keyHandle
            };

            try
            {
                var u2f   = new FidoUniversalTwoFactor();
                var appId = new FidoAppId(Request.Url);

                var deviceRegistration = GetFidoRepository().GetDeviceRegistrationsOfUser(GetCurrentUser()).FirstOrDefault(x => x.KeyHandle.ToWebSafeBase64() == keyHandle);
                if (deviceRegistration == null)
                {
                    ModelState.AddModelError("", "Unknown key handle: " + keyHandle);
                    return(View(model));
                }

                var startedRegistration = u2f.StartAuthentication(appId, deviceRegistration);

                model = new LoginDeviceViewModel
                {
                    AppId     = startedRegistration.AppId.ToString(),
                    Challenge = startedRegistration.Challenge,
                    KeyHandle = startedRegistration.KeyHandle.ToWebSafeBase64(),
                    UserName  = GetCurrentUser()
                };
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.GetType().Name + ": " + ex.Message);
            }

            return(View(model));
        }
Ejemplo n.º 5
0
        public ActionResult Login(string keyHandle)
        {
            var model = new LoginDeviceViewModel { KeyHandle = keyHandle };

            try
            {
                var u2f = new FidoUniversalTwoFactor();
                var appId = new FidoAppId(Request.Url);

                var deviceRegistration = GetFidoRepository().GetDeviceRegistrationsOfUser(GetCurrentUser()).FirstOrDefault(x => x.KeyHandle.ToWebSafeBase64() == keyHandle);
                if (deviceRegistration == null)
                {
                    ModelState.AddModelError("", "Unknown key handle: " + keyHandle);
                    return View(model);
                }

                var startedRegistration = u2f.StartAuthentication(appId, deviceRegistration);

                model = new LoginDeviceViewModel
                {
                    AppId = startedRegistration.AppId.ToString(),
                    Challenge = startedRegistration.Challenge,
                    KeyHandle = startedRegistration.KeyHandle.ToWebSafeBase64(),
                    UserName = GetCurrentUser()
                };
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.GetType().Name + ": " + ex.Message);
            }

            return View(model);
        }
Ejemplo n.º 6
0
        public void SerializeObject()
        {
            var fidoAppId  = new FidoAppId("http://example.com");
            var serialized = JsonConvert.SerializeObject(fidoAppId);

            Assert.AreEqual("\"http://example.com\"", serialized);
        }
        public FidoStartedRegistration(FidoAppId appId, string challenge)
        {
            if (appId == null) throw new ArgumentNullException("appId");
            if (challenge == null) throw new ArgumentNullException("challenge");

            AppId = appId;
            Challenge = challenge;
        }
        public FidoStartedAuthentication(FidoAppId appId, string challenge, FidoKeyHandle keyHandle)
        {
            if (appId == null) throw new ArgumentNullException("appId");
            if (challenge == null) throw new ArgumentNullException("challenge");
            if (keyHandle == null) throw new ArgumentNullException("keyHandle");

            AppId = appId;
            Challenge = challenge;
            KeyHandle = keyHandle;
        }
Ejemplo n.º 9
0
        public ActionResult TESTREG()
        {
            var u2f   = new FidoUniversalTwoFactor();
            var appId = new FidoAppId(Request.Url);
            var startedRegistration = u2f.StartRegistration(appId);

            GetFidoRepository().StoreStartedRegistration(GetCurrentUser(), startedRegistration);

            var model = new RegisterNewDeviceViewModel
            {
                AppId     = startedRegistration.AppId.ToString(),
                Challenge = startedRegistration.Challenge,
                UserName  = GetCurrentUser()
            };

            return(View(model));
        }
Ejemplo n.º 10
0
        public FidoStartedAuthentication StartAuthentication(FidoAppId appId, FidoDeviceRegistration deviceRegistration)
        {
            if (appId == null)
            {
                throw new ArgumentNullException("appId");
            }
            if (deviceRegistration == null)
            {
                throw new ArgumentNullException("deviceRegistration");
            }

            var challenge = _generateFidoChallenge.GenerateChallenge();

            return(new FidoStartedAuthentication(appId,
                                                 WebSafeBase64Converter.ToBase64String(challenge),
                                                 deviceRegistration.KeyHandle));
        }
Ejemplo n.º 11
0
        private void VerifyAuthSignature(FidoAppId appId, FidoSignatureData signatureData, FidoClientData clientData,
                                         FidoDeviceRegistration deviceRegistration)
        {
            if (appId == null)
            {
                throw new ArgumentNullException("appId");
            }
            if (signatureData == null)
            {
                throw new ArgumentNullException("signatureData");
            }
            if (clientData == null)
            {
                throw new ArgumentNullException("clientData");
            }
            if (deviceRegistration == null)
            {
                throw new ArgumentNullException("deviceRegistration");
            }

            if (String.IsNullOrEmpty(clientData.RawJsonValue))
            {
                throw new InvalidOperationException("Client data has no JSON representation");
            }

            var counterBytes = BitConverter.GetBytes(signatureData.Counter);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(counterBytes);
            }

            var signedBytes = PackBytes(
                Helpers.Sha256(appId.ToString()),
                new [] { signatureData.UserPresence },
                counterBytes,
                Helpers.Sha256(clientData.RawJsonValue));

            VerifySignature(deviceRegistration, signatureData.Signature, signedBytes);

            if (signatureData.UserPresence != UserPresentFlag)
            {
                throw new InvalidOperationException("User presence invalid during authentication");
            }
        }
Ejemplo n.º 12
0
        public ActionResult Register(NewUserViewModel newUserModel)
        {
            var u2f   = new FidoUniversalTwoFactor();
            var appId = new FidoAppId(Request.Url);
            var startedRegistration = u2f.StartRegistration(appId);


            GetFidoRepository().StoreStartedRegistration(newUserModel.UserName, startedRegistration);

            var model = new RegisterNewDeviceViewModel
            {
                AppId     = startedRegistration.AppId.ToString(),
                Challenge = startedRegistration.Challenge,
                UserName  = newUserModel.UserName,
                Email     = newUserModel.Email
            };

            return(View(model));
        }
Ejemplo n.º 13
0
        public ActionResult Login(LoginDeviceViewModel model)
        {
            model = model ?? new LoginDeviceViewModel();

            try
            {
                if (!String.IsNullOrEmpty(model.RawAuthenticationResponse))
                {
                    var u2f   = new FidoUniversalTwoFactor();
                    var appId = new FidoAppId(Request.Url);

                    var deviceRegistration = GetFidoRepository().GetDeviceRegistrationsOfUser(GetCurrentUser()).FirstOrDefault(x => x.KeyHandle.ToWebSafeBase64() == model.KeyHandle);
                    if (deviceRegistration == null)
                    {
                        ModelState.AddModelError("", "Unknown key handle: " + model.KeyHandle);
                        return(View(new LoginDeviceViewModel()));
                    }

                    var challenge = model.Challenge;

                    var startedAuthentication = new FidoStartedAuthentication(appId, challenge,
                                                                              FidoKeyHandle.FromWebSafeBase64(model.KeyHandle ?? ""));

                    var counter = u2f.FinishAuthentication(startedAuthentication, model.RawAuthenticationResponse, deviceRegistration, GetTrustedDomains());

                    // save the counter somewhere, the device registration of the next authentication should use this updated counter
                    //deviceRegistration.Counter = counter;

                    return(RedirectToAction("LoginSuccess"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.GetType().Name + ": " + ex.Message);
            }

            return(View(model));
        }
Ejemplo n.º 14
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            AppId = new FidoAppId(string.Format("{0}://{1}", Request.Scheme, Request.Host));

            base.OnActionExecuting(context);
        }
Ejemplo n.º 15
0
        public FidoStartedAuthentication StartAuthentication(FidoAppId appId, FidoDeviceRegistration deviceRegistration)
        {
            if (appId == null) throw new ArgumentNullException("appId");
            if (deviceRegistration == null) throw new ArgumentNullException("deviceRegistration");

            var challenge = _generateFidoChallenge.GenerateChallenge();

            return new FidoStartedAuthentication(appId,
                WebSafeBase64Converter.ToBase64String(challenge),
                deviceRegistration.KeyHandle);
        }
Ejemplo n.º 16
0
        public FidoStartedRegistration StartRegistration(FidoAppId appId)
        {
            var challengeBytes = _generateFidoChallenge.GenerateChallenge();
            var challenge = WebSafeBase64Converter.ToBase64String(challengeBytes);

            return new FidoStartedRegistration(appId, challenge);
        }
Ejemplo n.º 17
0
        private void VerifyAuthSignature(FidoAppId appId, FidoSignatureData signatureData, FidoClientData clientData, 
			FidoDeviceRegistration deviceRegistration)
        {
            if (appId == null) throw new ArgumentNullException("appId");
            if (signatureData == null) throw new ArgumentNullException("signatureData");
            if (clientData == null) throw new ArgumentNullException("clientData");
            if (deviceRegistration == null) throw new ArgumentNullException("deviceRegistration");

            if (String.IsNullOrEmpty(clientData.RawJsonValue))
                throw new InvalidOperationException("Client data has no JSON representation");

            var counterBytes = BitConverter.GetBytes(signatureData.Counter);
            if (BitConverter.IsLittleEndian)
                Array.Reverse(counterBytes);

            var signedBytes = PackBytes(
                Helpers.Sha256(appId.ToString()),
                new [] { signatureData.UserPresence },
                counterBytes,
                Helpers.Sha256(clientData.RawJsonValue));

            VerifySignature(deviceRegistration, signatureData.Signature, signedBytes);

            if (signatureData.UserPresence != UserPresentFlag)
                throw new InvalidOperationException("User presence invalid during authentication");
        }
Ejemplo n.º 18
0
        private void VerifyResponseSignature(FidoAppId appId, FidoRegistrationData registrationData, FidoClientData clientData)
        {
            if (appId == null) throw new ArgumentNullException("appId");
            if (registrationData == null) throw new ArgumentNullException("registrationData");
            if (clientData == null) throw new ArgumentNullException("clientData");

            if (String.IsNullOrEmpty(clientData.RawJsonValue))
                throw new InvalidOperationException("Client data has no JSON representation");

            var signedBytes = PackBytes(
                new byte[] { 0 },
                Helpers.Sha256(appId.ToString()),
                Helpers.Sha256(clientData.RawJsonValue),
                registrationData.KeyHandle.ToByteArray(),
                registrationData.UserPublicKey.ToByteArray());

            VerifySignature(registrationData.AttestationCertificate, registrationData.Signature, signedBytes);
        }
Ejemplo n.º 19
0
        public ActionResult Login(LoginDeviceViewModel model)
        {
            model = model ?? new LoginDeviceViewModel();

            try
            {
                if (!String.IsNullOrEmpty(model.RawAuthenticationResponse))
                {
                    var u2f = new FidoUniversalTwoFactor();
                    var appId = new FidoAppId(Request.Url);

                    var deviceRegistration = GetFidoRepository().GetDeviceRegistrationsOfUser(GetCurrentUser()).FirstOrDefault(x => x.KeyHandle.ToWebSafeBase64() == model.KeyHandle);
                    if (deviceRegistration == null)
                    {
                        ModelState.AddModelError("", "Unknown key handle: " + model.KeyHandle);
                        return View(new LoginDeviceViewModel());
                    }

                    var challenge = model.Challenge;

                    var startedAuthentication = new FidoStartedAuthentication(appId, challenge,
                        FidoKeyHandle.FromWebSafeBase64(model.KeyHandle ?? ""));

                    var counter = u2f.FinishAuthentication(startedAuthentication, model.RawAuthenticationResponse, deviceRegistration, GetTrustedDomains());

                    // save the counter somewhere, the device registration of the next authentication should use this updated counter
                    deviceRegistration.Counter = counter;

                    return RedirectToAction("LoginSuccess");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.GetType().Name + ": " + ex.Message);
            }

            return View(model);
        }
Ejemplo n.º 20
0
        public ActionResult Register()
        {
            var u2f = new FidoUniversalTwoFactor();
            var appId = new FidoAppId(Request.Url);
            var startedRegistration = u2f.StartRegistration(appId);

            GetFidoRepository().StoreStartedRegistration(GetCurrentUser(), startedRegistration);

            var model = new RegisterNewDeviceViewModel
            {
                AppId = startedRegistration.AppId.ToString(),
                Challenge = startedRegistration.Challenge,
                UserName = GetCurrentUser()
            };

            return View(model);
        }