Exemplo n.º 1
0
        public override bool Equals(object obj)
        {
            if (!(obj is RawRegisterResponse))
            {
                return(false);
            }
            if (this == obj)
            {
                return(true);
            }
            if (GetType() != obj.GetType())
            {
                return(false);
            }
            RawRegisterResponse other = (RawRegisterResponse)obj;

            if (!_attestationCertificate.Equals(other._attestationCertificate))
            {
                return(false);
            }
            if (!_keyHandle.SequenceEqual(other._keyHandle))
            {
                return(false);
            }
            if (!_signature.SequenceEqual(other._signature))
            {
                return(false);
            }
            return(_userPublicKey.SequenceEqual(other._userPublicKey));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts string response into RawRegisterResponse object
        /// </summary>
        /// <param name="rawDataBase64">raw string from client</param>
        /// <returns>RawRegisterResponse object</returns>
        public static RawRegisterResponse FromBase64(string rawDataBase64)
        {
            if (string.IsNullOrWhiteSpace(rawDataBase64))
            {
                throw new ArgumentException("Invalid argument were being passed.");
            }

            byte[] bytes = rawDataBase64.Base64StringToByteArray();

            Stream       stream       = new MemoryStream(bytes);
            BinaryReader binaryReader = new BinaryReader(stream);

            try
            {
                byte reservedByte = binaryReader.ReadByte();
                if (reservedByte != RegistrationReservedByteValue)
                {
                    throw new U2fException($"Incorrect value of reserved byte. Expected: {RegistrationReservedByteValue}. Was: {reservedByte}");
                }

                byte[] publicKey       = binaryReader.ReadBytes(65);
                byte   keyHandleLength = binaryReader.ReadByte();
                byte[] keyHandle       = binaryReader.ReadBytes(keyHandleLength);

                List <byte> rawCertBytes = new List <byte>();
                while (binaryReader.BaseStream.Length - binaryReader.BaseStream.Position > 0)
                {
                    byte end = binaryReader.ReadByte();

                    rawCertBytes.Add(end);
                }

                X509Certificate2 attestationCertificate = new X509Certificate2(rawCertBytes.ToArray());

                // reserved byte + public key length + key handle length + cert data length
                int size = 1 + 65 + 1 + keyHandle.Length + attestationCertificate.RawData.Length;

                byte[] signature = bytes.Skip(size).Take(bytes.Length - size).ToArray();

                RawRegisterResponse rawRegisterResponse = new RawRegisterResponse(
                    publicKey,
                    keyHandle,
                    attestationCertificate,
                    signature);

                return(rawRegisterResponse);
            }
            catch (Exception exception)
            {
                throw new U2fException("Error when parsing attestation certificate", exception);
            }
            finally
            {
                stream.Dispose();
                binaryReader.Dispose();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts string response into RawRegisterResponse object
        /// </summary>
        /// <param name="rawDataBase64">raw string from client</param>
        /// <returns>RawRegisterResponse object</returns>
        public static RawRegisterResponse FromBase64(string rawDataBase64)
        {
            if (string.IsNullOrWhiteSpace(rawDataBase64))
            {
                throw new ArgumentException("Invalid argument were being passed.");
            }

            byte[] bytes = rawDataBase64.Base64StringToByteArray();

            Stream       stream       = new MemoryStream(bytes);
            BinaryReader binaryReader = new BinaryReader(stream);

            try
            {
                byte reservedByte = binaryReader.ReadByte();
                if (reservedByte != RegistrationReservedByteValue)
                {
                    throw new U2fException(string.Format("Incorrect value of reserved byte. Expected: {0}. Was: {1}",
                                                         RegistrationReservedByteValue, reservedByte));
                }

                byte[] publicKey = binaryReader.ReadBytes(65);
                byte[] keyHandle = binaryReader.ReadBytes(binaryReader.ReadByte());
                X509CertificateParser x509CertificateParser  = new X509CertificateParser();
                X509Certificate       attestationCertificate = x509CertificateParser.ReadCertificate(stream);
                int size = (int)(binaryReader.BaseStream.Length - binaryReader.BaseStream.Position);


                byte[] signature = binaryReader.ReadBytes(size);

                RawRegisterResponse rawRegisterResponse = new RawRegisterResponse(
                    publicKey,
                    keyHandle,
                    attestationCertificate,
                    signature);

                return(rawRegisterResponse);
            }
            catch (Exception e)
            {
                throw new U2fException("Error when parsing attestation certificate", e);
            }
            finally
            {
                stream.Dispose();
                binaryReader.Dispose();
            }
        }