Example #1
0
        public static void ExpectedTag_IgnoresConstructed(
            AsnEncodingRules ruleSet,
            string inputHex,
            TagClass tagClass,
            int tagValue)
        {
            Asn1Tag primitiveTag   = new Asn1Tag(tagClass, tagValue, false);
            Asn1Tag constructedTag = new Asn1Tag(tagClass, tagValue, true);

            byte[] inputData = inputHex.HexToByteArray();

            AsnReader   reader = new AsnReader(inputData, ruleSet);
            ShortBacked val1   = reader.ReadEnumeratedValue <ShortBacked>(constructedTag);

            Assert.False(reader.HasData);

            reader = new AsnReader(inputData, ruleSet);
            ShortBacked val2 = reader.ReadEnumeratedValue <ShortBacked>(primitiveTag);

            Assert.False(reader.HasData);

            Assert.Equal(val1, val2);

            reader = new AsnReader(inputData, ruleSet);
            ShortBacked val3 = (ShortBacked)reader.ReadEnumeratedValue(typeof(ShortBacked), constructedTag);

            Assert.False(reader.HasData);

            Assert.Equal(val1, val3);

            reader = new AsnReader(inputData, ruleSet);
            ShortBacked val4 = (ShortBacked)reader.ReadEnumeratedValue(typeof(ShortBacked), primitiveTag);

            Assert.False(reader.HasData);

            Assert.Equal(val1, val4);

            reader = new AsnReader(inputData, ruleSet);
            ReadOnlyMemory <byte> bytes1 = reader.ReadEnumeratedBytes(constructedTag);

            Assert.False(reader.HasData);

            reader = new AsnReader(inputData, ruleSet);
            ReadOnlyMemory <byte> bytes2 = reader.ReadEnumeratedBytes(primitiveTag);

            Assert.False(reader.HasData);

            Assert.Equal(bytes1.ByteArrayToHex(), bytes2.ByteArrayToHex());
            Assert.Equal("FF", bytes1.ByteArrayToHex());
        }
Example #2
0
        public static void TagMustBeCorrect_Custom(PublicEncodingRules ruleSet)
        {
            byte[]    inputData = { 0x87, 2, 0, 0x80 };
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.ReadIntegerBytes(Asn1Tag.Null));

            Assert.True(reader.HasData, "HasData after bad universal tag");

            Assert.Throws <CryptographicException>(() => reader.ReadIntegerBytes());

            Assert.True(reader.HasData, "HasData after default tag");

            Assert.Throws <CryptographicException>(() => reader.ReadIntegerBytes(new Asn1Tag(TagClass.Application, 0)));

            Assert.True(reader.HasData, "HasData after wrong custom class");

            Assert.Throws <CryptographicException>(() => reader.ReadIntegerBytes(new Asn1Tag(TagClass.ContextSpecific, 1)));

            Assert.True(reader.HasData, "HasData after wrong custom tag value");

            ReadOnlyMemory <byte> value = reader.ReadIntegerBytes(new Asn1Tag(TagClass.ContextSpecific, 7));

            Assert.Equal("0080", value.ByteArrayToHex());
            Assert.False(reader.HasData, "HasData after reading value");
        }
Example #3
0
        public static void ReadEnumeratedBytes(PublicEncodingRules ruleSet)
        {
            const string Payload = "0102030405060708090A0B0C0D0E0F10";

            // ENUMERATED (payload) followed by INTEGER (0)
            byte[]    data   = ("0A10" + Payload + "020100").HexToByteArray();
            AsnReader reader = new AsnReader(data, (AsnEncodingRules)ruleSet);

            ReadOnlyMemory <byte> contents = reader.ReadEnumeratedBytes();

            Assert.Equal(0x10, contents.Length);
            Assert.Equal(Payload, contents.ByteArrayToHex());
        }
Example #4
0
        public static void ReadIntegerBytes()
        {
            const string Payload = "0102030405060708090A0B0C0D0E0F10";

            // INTEGER (payload) followed by INTEGER (0)
            byte[]    data   = ("0210" + Payload + "020100").HexToByteArray();
            AsnReader reader = new AsnReader(data, AsnEncodingRules.DER);

            ReadOnlyMemory <byte> contents = reader.ReadIntegerBytes();

            Assert.Equal(0x10, contents.Length);
            Assert.Equal(Payload, contents.ByteArrayToHex());
        }
Example #5
0
        public static void ExpectedTag_IgnoresConstructed(
            PublicEncodingRules ruleSet,
            string inputHex,
            PublicTagClass tagClass,
            int tagValue)
        {
            byte[]                inputData = inputHex.HexToByteArray();
            AsnReader             reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);
            ReadOnlyMemory <byte> val1      = reader.ReadIntegerBytes(new Asn1Tag((TagClass)tagClass, tagValue, true));

            Assert.False(reader.HasData);
            reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet);
            ReadOnlyMemory <byte> val2 = reader.ReadIntegerBytes(new Asn1Tag((TagClass)tagClass, tagValue, false));

            Assert.False(reader.HasData);

            Assert.Equal(val1.ByteArrayToHex(), val2.ByteArrayToHex());
        }
Example #6
0
        public static void TagMustBeCorrect_Universal(PublicEncodingRules ruleSet)
        {
            byte[]    inputData = { 2, 1, 0x7E };
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.ReadIntegerBytes(Asn1Tag.Null));

            Assert.True(reader.HasData, "HasData after bad universal tag");

            Assert.Throws <CryptographicException>(() => reader.ReadIntegerBytes(new Asn1Tag(TagClass.ContextSpecific, 0)));

            Assert.True(reader.HasData, "HasData after wrong tag");

            ReadOnlyMemory <byte> value = reader.ReadIntegerBytes();

            Assert.Equal("7E", value.ByteArrayToHex());
            Assert.False(reader.HasData, "HasData after read");
        }
 /// <summary>
 /// Assert equality by comparing hex string representations
 /// </summary>
 /// <param name="expected"></param>
 /// <param name="actual"></param>
 public static void HexEqual(ReadOnlyMemory <byte> expected, ReadOnlyMemory <byte> actual) => Assert.Equal(expected.ByteArrayToHex(), actual.ByteArrayToHex());
Example #8
0
        public static void EncryptDecryptMixBytesAndChars(bool encryptBytes, bool withSpan)
        {
            Pkcs12SafeContents contents = new Pkcs12SafeContents();

            contents.AddSecret(s_zeroOid, s_derNull);

            string      password          = nameof(EncryptDecryptMixBytesAndChars);
            Span <byte> passwordUtf8Bytes = stackalloc byte[password.Length];

            Encoding.UTF8.GetBytes(password, passwordUtf8Bytes);

            Pkcs12Builder builder = new Pkcs12Builder();

            if (encryptBytes)
            {
                builder.AddSafeContentsEncrypted(contents, passwordUtf8Bytes, s_pbkdf2Parameters);
            }
            else
            {
                builder.AddSafeContentsEncrypted(contents, password, s_pbkdf2Parameters);
            }

            builder.SealWithMac(password, HashAlgorithmName.SHA1, 2048);

            byte[]     encoded = builder.Encode();
            Pkcs12Info info    = Pkcs12Info.Decode(encoded, out _, skipCopy: true);

            Assert.True(info.VerifyMac(password));
            ReadOnlyCollection <Pkcs12SafeContents> authSafe = info.AuthenticatedSafe;

            Assert.Equal(1, authSafe.Count);

            Pkcs12SafeContents readContents = authSafe[0];

            Assert.Equal(
                Pkcs12ConfidentialityMode.Password,
                readContents.ConfidentialityMode);

            if (encryptBytes)
            {
                if (withSpan)
                {
                    readContents.Decrypt(password.AsSpan());
                }
                else
                {
                    readContents.Decrypt(password);
                }
            }
            else
            {
                if (withSpan)
                {
                    readContents.Decrypt(passwordUtf8Bytes);
                }
                else
                {
                    readContents.Decrypt(passwordUtf8Bytes.ToArray());
                }
            }

            Assert.Equal(
                Pkcs12ConfidentialityMode.None,
                readContents.ConfidentialityMode);

            List <Pkcs12SafeBag> bags = readContents.GetBags().ToList();

            Assert.Equal(1, bags.Count);
            Pkcs12SecretBag secretBag = Assert.IsType <Pkcs12SecretBag>(bags[0]);

            Assert.Equal(s_zeroOid.Value, secretBag.GetSecretType().Value);
            Assert.Equal(s_derNull.ByteArrayToHex(), secretBag.SecretValue.ByteArrayToHex());
        }