Ejemplo n.º 1
0
        public static async Task <U2FHidDevice> OpenAsync(IHidDevice hidDevice)
        {
            var device = new U2FHidDevice(hidDevice);
            await device.InitAsync();

            return(device);
        }
Ejemplo n.º 2
0
        public static async Task <AuthenticateResponse> AuthenticateAsync(U2FHidDevice u2fHidDevice, StartedAuthentication request, string facet, bool checkOnly)
        {
            ValidateRequest(request, facet);

            var sha256   = new SHA256Managed();
            var appParam = sha256.ComputeHash(Encoding.ASCII.GetBytes(request.AppId));

            var clientDataString = GetAuthenticationClientData(request.Challenge, facet);
            var clientParam      = sha256.ComputeHash(Encoding.ASCII.GetBytes(clientDataString));

            var keyHandleDecoded = Utils.Base64StringToByteArray(request.KeyHandle);

            var byteArrayBuilder = new ByteArrayBuilder();

            byteArrayBuilder.Append(clientParam);
            byteArrayBuilder.Append(appParam);
            byteArrayBuilder.Append((byte)keyHandleDecoded.Length);
            byteArrayBuilder.Append(keyHandleDecoded);

            var data = byteArrayBuilder.GetBytes();
            var p1   = (byte)(checkOnly ? 0x07 : 0x03);
            var p2   = (byte)0x00;

            var response = await u2fHidDevice.SendApduAsync(Constants.INS_SIGN, p1, p2, data);

            var responseBase64   = Utils.ByteArrayToBase64String(response);
            var clientDataBase64 = Utils.ByteArrayToBase64String(Encoding.ASCII.GetBytes(clientDataString));

            var authenticateResponse = new AuthenticateResponse(clientDataBase64, responseBase64, request.KeyHandle);

            return(authenticateResponse);
        }
Ejemplo n.º 3
0
        public static async Task AuthenticateAsync(IHidDevice hidDevice, DeviceRegistration deviceRegistration, string appId, string facet, bool checkOnly = false, CancellationToken?cancellationToken = null)
        {
            cancellationToken = cancellationToken ?? CancellationToken.None;

            if (hidDevice == null || !hidDevice.IsConnected)
            {
                throw new ArgumentException("Hid device not connected", nameof(hidDevice));
            }

            using (var u2fHidDevice = await U2FHidDevice.OpenAsync(hidDevice))
            {
                var startAuthentication = U2F.StartAuthentication(appId, deviceRegistration);

                Log.Debug("Touch token to authenticate");
                var authenticateResponse = await WaitForTokenInputAsync(() => U2Fv2.AuthenticateAsync(u2fHidDevice, startAuthentication, facet, checkOnly), cancellationToken.Value).ConfigureAwait(false);

                U2F.FinishAuthentication(startAuthentication, authenticateResponse, deviceRegistration);
                Log.Debug("Authenticated");
            }
        }
Ejemplo n.º 4
0
        public static async Task <RegisterResponse> RegisterAsync(U2FHidDevice u2fHidDevice, StartedRegistration request, string facet)
        {
            ValidateRequest(request, facet);

            var appParam = GetApplicationParameter(request.AppId);

            var clientData     = GetRegistrationClientData(request.Challenge, facet);
            var challengeParam = GetChallengeParameter(clientData);

            var data = challengeParam.Concat(appParam).ToArray();
            var p1   = (byte)0x03;
            var p2   = (byte)0x00;

            var response = await u2fHidDevice.SendApduAsync(Constants.INS_ENROLL, p1, p2, data);

            var registrationDataBase64 = Utils.ByteArrayToBase64String(response);
            var clientDataBase64       = Utils.ByteArrayToBase64String(Encoding.ASCII.GetBytes(clientData));

            var registerResponse = new RegisterResponse(registrationDataBase64, clientDataBase64);

            return(registerResponse);
        }
Ejemplo n.º 5
0
        public static async Task <DeviceRegistration> RegisterAsync(IHidDevice hidDevice, string appId, string facet, CancellationToken?cancellationToken = null)
        {
            cancellationToken = cancellationToken ?? CancellationToken.None;

            if (hidDevice == null || !hidDevice.IsConnected)
            {
                throw new ArgumentException("Hid device not connected", nameof(hidDevice));
            }

            using (var u2fHidDevice = await U2FHidDevice.OpenAsync(hidDevice))
            {
                var startRegistration = U2F.StartRegistration(appId);

                Log.Debug("Touch token to register");
                var registerResponse = await WaitForTokenInputAsync(() => U2Fv2.RegisterAsync(u2fHidDevice, startRegistration, facet), cancellationToken.Value);

                var deviceRegistration = U2F.FinishRegistration(startRegistration, registerResponse);
                Log.Debug("Registered");

                return(deviceRegistration);
            }
        }