Ejemplo n.º 1
0
 public void WriteTo(ArraySegment<byte> segment)
 {
     using (var stream = segment.AsStream())
     {
         WriteTo(stream);
     }
 }
Ejemplo n.º 2
0
 public void WriteTo(ArraySegment <byte> segment)
 {
     using (var stream = segment.AsStream())
     {
         WriteTo(stream);
     }
 }
Ejemplo n.º 3
0
        /// <exception cref="U2FException"/>
        public static KeyRegisterResponse DecodeKeyRegisterResponse(ArraySegment <byte> data)
        {
            try
            {
                using (var stream = data.AsStream())
                    using (var inputStream = new EndianReader(stream, Endianness.BigEndian))
                    {
                        var reservedByte  = inputStream.ReadByte();
                        var userPublicKey = inputStream.ReadBytes(65);
                        var keyHandleSize = inputStream.ReadByte();
                        var keyHandle     = inputStream.ReadBytes(keyHandleSize);

                        var parser = new X509CertificateParser();
                        var attestationCertificate = parser.ReadCertificate(inputStream.BaseStream);

                        var signatureSize = (int)(inputStream.BaseStream.Length - inputStream.BaseStream.Position);
                        var signature     = inputStream.ReadBytes(signatureSize);
                        if (reservedByte != RegistrationReservedByteValue)
                        {
                            throw new U2FException(
                                      $"Incorrect value of reserved byte. Expected: {RegistrationReservedByteValue}. Was: {reservedByte}");
                        }
                        return(new KeyRegisterResponse(userPublicKey, keyHandle,
                                                       attestationCertificate, signature));
                    }
            }
            catch (IOException e)
            {
                throw new U2FException("Error when parsing raw RegistrationResponse", e);
            }
            catch (CertificateException e)
            {
                throw new U2FException("Error when parsing attestation certificate", e);
            }
        }
Ejemplo n.º 4
0
        /// <exception cref="U2FException"/>
        public static KeySignResponse DecodeKeySignResponse(ArraySegment <byte> data)
        {
            try
            {
                using (var stream = data.AsStream())
                {
                    using (var inputStream = new EndianReader(stream, Endianness.BigEndian))
                    {
                        var userPresence  = inputStream.ReadByte();
                        var counter       = inputStream.ReadInt32();
                        var signatureSize = (int)(inputStream.BaseStream.Length - inputStream.BaseStream.Position);
                        var signature     = inputStream.ReadBytes(signatureSize);

                        return(new KeySignResponse(userPresence, counter, signature));
                    }
                }
            }
            catch (IOException e)
            {
                throw new U2FException("Error when parsing rawSignData", e);
            }
        }
Ejemplo n.º 5
0
        public static void AssertBinary(ArraySegment<byte> actualBytes, Endianness endianess, params object[] expectedContent)
        {
            var str = SegmentToString(actualBytes);

            using (var stream = actualBytes.AsStream())
            {
                var reader = new EndianReader(stream, endianess);

                foreach (var expectedObject in expectedContent)
                {
                    var index = stream.Position;
                    var type = expectedObject.GetType();
                    if (type == typeof (uint))
                    {
                        var expected = (uint)expectedObject;
                        var actual = reader.ReadUInt32();
                        Check.That(actual).IsEqualTo(expected);
                    }
                    else if (type == typeof(ushort))
                    {
                        var expected = (ushort)expectedObject;
                        var actual = reader.ReadUInt16();
                        Check.That(actual).IsEqualTo(expected);
                    }
                    else if (type == typeof(byte))
                    {
                        var expected = (byte)expectedObject;
                        var actual = reader.ReadByte();
                        if (actual != expected)
                        {
                            throw new AssertionException(
                                $"Byte at index {index} is 0x{actual:X2} but 0x{expected:X2} was expected.\r\n\r\n{str}");
                        }
                    } else if (type == typeof (BytesHolder))
                    {
                        var holder = (BytesHolder)expectedObject;
                        reader.Read(holder.Bytes, 0, holder.ByteCount);
                    }
                    else if (type == typeof (ArraySegment<byte>))
                    {
                        var expected = (ArraySegment<byte>)expectedObject;
                        var actual = new byte[expected.Count];
                        reader.Read(actual, 0, actual.Length);
                        Check.That(expected.ContentEquals(actual.Segment())).IsTrue();
                    }
                    else if (type == typeof (byte[]))
                    {
                        var expected = (byte[])expectedObject;
                        var actual = new byte[expected.Length];
                        reader.Read(actual, 0, actual.Length);
                        if (!expected.Segment().ContentEquals(actual.Segment()))
                        {
                            var actualStr = SegmentToString(actual.Segment());
                            var expectedStr = SegmentToString(expected.Segment());
                            throw new AssertionException($"The 2 binary data differ, expected: \r\n{expectedStr}\r\nRead:\r\n{actualStr}\r\nFull actual data:\r\n{str}");
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public static void AssertBinary(ArraySegment <byte> actualBytes, Endianness endianess, params object[] expectedContent)
        {
            var str = SegmentToString(actualBytes);

            using (var stream = actualBytes.AsStream())
            {
                var reader = new EndianReader(stream, endianess);

                foreach (var expectedObject in expectedContent)
                {
                    var index = stream.Position;
                    var type  = expectedObject.GetType();
                    if (type == typeof(uint))
                    {
                        var expected = (uint)expectedObject;
                        var actual   = reader.ReadUInt32();
                        Check.That(actual).IsEqualTo(expected);
                    }
                    else if (type == typeof(ushort))
                    {
                        var expected = (ushort)expectedObject;
                        var actual   = reader.ReadUInt16();
                        Check.That(actual).IsEqualTo(expected);
                    }
                    else if (type == typeof(byte))
                    {
                        var expected = (byte)expectedObject;
                        var actual   = reader.ReadByte();
                        if (actual != expected)
                        {
                            throw new AssertionException(
                                      $"Byte at index {index} is 0x{actual:X2} but 0x{expected:X2} was expected.\r\n\r\n{str}");
                        }
                    }
                    else if (type == typeof(BytesHolder))
                    {
                        var holder = (BytesHolder)expectedObject;
                        reader.Read(holder.Bytes, 0, holder.ByteCount);
                    }
                    else if (type == typeof(ArraySegment <byte>))
                    {
                        var expected = (ArraySegment <byte>)expectedObject;
                        var actual   = new byte[expected.Count];
                        reader.Read(actual, 0, actual.Length);
                        Check.That(expected.ContentEquals(actual.Segment())).IsTrue();
                    }
                    else if (type == typeof(byte[]))
                    {
                        var expected = (byte[])expectedObject;
                        var actual   = new byte[expected.Length];
                        reader.Read(actual, 0, actual.Length);
                        if (!expected.Segment().ContentEquals(actual.Segment()))
                        {
                            var actualStr   = SegmentToString(actual.Segment());
                            var expectedStr = SegmentToString(expected.Segment());
                            throw new AssertionException($"The 2 binary data differ, expected: \r\n{expectedStr}\r\nRead:\r\n{actualStr}\r\nFull actual data:\r\n{str}");
                        }
                    }
                }
            }
        }