Ejemplo n.º 1
0
        public static void SequenceStackUnderflow()
        {
            var span   = new ReadOnlySpan <byte>(new byte[0]);
            var reader = new Asn1Reader(ref span);

            try { reader.End(); Assert.True(false); } catch (IndexOutOfRangeException) { } // cannot use Assert.Throws
        }
Ejemplo n.º 2
0
        public static void SequenceStackUnderflow()
        {
            var value  = Array.Empty <byte>();
            var reader = new Asn1Reader(value);

            try { reader.End(); Assert.True(false); } catch (InvalidOperationException) { } // cannot use Assert.Throws
        }
Ejemplo n.º 3
0
        internal override bool TryReadAlgorithmIdentifier(
            ref Asn1Reader reader,
            out ReadOnlySpan <byte> nonce)
        {
            bool success = true;

            reader.BeginSequence();
            success &= reader.ObjectIdentifier().SequenceEqual(s_oid.Bytes);
            reader.BeginSequence();
            nonce    = reader.OctetString();
            success &= (nonce.Length == crypto_aead_aes256gcm_NPUBBYTES);
            reader.End();
            reader.End();
            success &= reader.Success;
            return(success);
        }
Ejemplo n.º 4
0
        public static void SequenceStackUnderflow()
        {
            var value  = new byte[0];
            var reader = new Asn1Reader(value);

            try { reader.End(); Assert.True(false); } catch (IndexOutOfRangeException) { } // cannot use Assert.Throws
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public static void PkixPublicKey()
        {
            var a = KeyAgreementAlgorithm.X25519;
            var b = Utilities.RandomBytes.Slice(0, a.PrivateKeySize);

            using var k = Key.Import(a, b, KeyBlobFormat.RawPrivateKey);
            var publicKeyBytes = k.Export(KeyBlobFormat.RawPublicKey);
            var blob           = k.Export(KeyBlobFormat.PkixPublicKey);

            var reader = new Asn1Reader(blob);

            reader.BeginSequence();
            reader.BeginSequence();
            Assert.Equal(s_oid, reader.ObjectIdentifier().ToArray());
            reader.End();
            Assert.Equal(publicKeyBytes, reader.BitString().ToArray());
            reader.End();
            Assert.True(reader.SuccessComplete);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public static void InnerLengthGreaterThanOuterLength()
        {
            var span   = new ReadOnlySpan <byte>(new byte[] { 0x30, 0x08, 0x04, 0x0A, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA });
            var reader = new Asn1Reader(ref span);

            reader.BeginSequence();
            Assert.True(reader.Success);
            Assert.Equal(new byte[0], reader.OctetString().ToArray());
            Assert.False(reader.Success);
            reader.End();
            Assert.False(reader.Success);
            Assert.False(reader.SuccessComplete);
        }
Ejemplo n.º 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, 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);
            }
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        public static void InnerLengthGreaterThanOuterLength()
        {
            var value  = new byte[] { 0x30, 0x08, 0x04, 0x0A, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA };
            var reader = new Asn1Reader(value);

            reader.BeginSequence();
            Assert.True(reader.Success);
            Assert.Equal(Array.Empty <byte>(), reader.OctetString().ToArray());
            Assert.False(reader.Success);
            reader.End();
            Assert.False(reader.Success);
            Assert.False(reader.SuccessComplete);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
            }
        }
Ejemplo n.º 15
0
        public static void Sequence(int depth, byte[] value)
        {
            var reader = new Asn1Reader(value);

            for (var i = 0; i < depth; i++)
            {
                reader.BeginSequence();
                Assert.True(reader.Success);
            }
            for (var i = 0; i < depth; i++)
            {
                reader.End();
                Assert.True(reader.Success);
            }
            Assert.True(reader.SuccessComplete);
        }