Example #1
0
        public static void Integer32(byte[] value, int expected)
        {
            var reader = new Asn1Reader(value);

            Assert.Equal(expected, reader.Integer32());
            Assert.True(reader.SuccessComplete);
        }
Example #2
0
        public static void Integer32Invalid(byte[] value)
        {
            var reader = new Asn1Reader(value);

            Assert.Equal(0, reader.Integer32());
            Assert.False(reader.Success);
            Assert.False(reader.SuccessComplete);
        }
Example #3
0
        public static void Integer32Invalid(byte[] value)
        {
            var span   = new ReadOnlySpan <byte>(value);
            var reader = new Asn1Reader(ref span);

            Assert.Equal(0, reader.Integer32());
            Assert.False(reader.Success);
            Assert.False(reader.SuccessComplete);
        }
Example #4
0
        public static void UnexpectedDataAfterEnd2()
        {
            var span   = new ReadOnlySpan <byte>(new byte[] { 0x30, 0x06, 0x02, 0x01, 0x17, 0x02, 0x01, 0x42 });
            var reader = new Asn1Reader(ref span);

            reader.BeginSequence();
            Assert.True(reader.Success);
            Assert.Equal(0x17, reader.Integer32());
            Assert.True(reader.Success);
            reader.End();
            Assert.False(reader.Success);
            Assert.False(reader.SuccessComplete);
        }
Example #5
0
        public static void UnexpectedDataAfterEnd1()
        {
            var value  = new byte[] { 0x30, 0x03, 0x02, 0x01, 0x17, 0x02, 0x01, 0x42 };
            var reader = new Asn1Reader(value);

            reader.BeginSequence();
            Assert.True(reader.Success);
            Assert.Equal(0x17, reader.Integer32());
            Assert.True(reader.Success);
            reader.End();
            Assert.True(reader.Success);
            Assert.False(reader.SuccessComplete);
        }
Example #6
0
        public static void IntegerSequence(byte[] value, int[] expected)
        {
            var reader = new Asn1Reader(value);

            reader.BeginSequence();
            for (var i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i], reader.Integer32());
            }
            reader.End();
            Assert.True(reader.Success);
            Assert.True(reader.SuccessComplete);
        }
Example #7
0
        public static void SequenceGraceful()
        {
            var span   = new ReadOnlySpan <byte>(new byte[] { 0x30, 0x06, 0x30, 0x04, 0x30, 0x02, 0x30, 0x00 });
            var reader = new Asn1Reader(ref span);

            reader.BeginSequence();
            Assert.True(reader.Success);
            reader.BeginSequence();
            Assert.True(reader.Success);
            Assert.Equal(0, reader.Integer32());
            Assert.False(reader.Success);
            reader.End();
            Assert.False(reader.Success);
            reader.End();
            Assert.False(reader.Success);
            Assert.False(reader.SuccessComplete);
        }
Example #8
0
        public static void SequenceGraceful()
        {
            var value  = new byte[] { 0x30, 0x06, 0x30, 0x04, 0x30, 0x02, 0x30, 0x00 };
            var reader = new Asn1Reader(value);

            reader.BeginSequence();
            Assert.True(reader.Success);
            reader.BeginSequence();
            Assert.True(reader.Success);
            Assert.Equal(0, reader.Integer32());
            Assert.False(reader.Success);
            reader.End();
            Assert.False(reader.Success);
            reader.End();
            Assert.False(reader.Success);
            Assert.False(reader.SuccessComplete);
        }
Example #9
0
        public static void PkixPrivateKey()
        {
            var a = new X25519();
            var b = Utilities.RandomBytes.Slice(0, a.PrivateKeySize);

            using (var k = Key.Import(a, b, KeyBlobFormat.RawPrivateKey, KeyExportPolicies.AllowPlaintextExport))
            {
                var blob = k.Export(KeyBlobFormat.PkixPrivateKey);

                var reader = new Asn1Reader(blob);
                reader.BeginSequence();
                Assert.Equal(0, reader.Integer32());
                reader.BeginSequence();
                Assert.Equal(s_oid, reader.ObjectIdentifier().ToArray());
                reader.End();
                var curvePrivateKey = new Asn1Reader(reader.OctetString());
                Assert.Equal(b.ToArray(), curvePrivateKey.OctetString().ToArray());
                Assert.True(curvePrivateKey.SuccessComplete);
                reader.End();
                Assert.True(reader.SuccessComplete);
            }
        }
Example #10
0
        public static void PkixPrivateKey()
        {
            var a = new X25519();
            var b = Utilities.RandomBytes.Slice(0, a.PrivateKeySize);

            using (var k = Key.Import(a, b, KeyBlobFormat.RawPrivateKey, KeyFlags.AllowExport))
            {
                var blob = new ReadOnlySpan <byte>(k.Export(KeyBlobFormat.PkixPrivateKey));

                var reader = new Asn1Reader(ref blob);
                reader.BeginSequence();
                Assert.Equal(0, reader.Integer32());
                reader.BeginSequence();
                Assert.Equal(s_oid, reader.ObjectIdentifier().ToArray());
                reader.End();
                var edPrivateKey = reader.OctetString();
                reader.End();
                Assert.True(reader.SuccessComplete);

                var reader2 = new Asn1Reader(ref edPrivateKey);
                Assert.Equal(b.ToArray(), reader2.OctetString().ToArray());
                Assert.True(reader2.SuccessComplete);
            }
        }