public static void ReadEcPublicKey() { const string PublicKeyValue = "04" + "2363DD131DA65E899A2E63E9E05E50C830D4994662FFE883DB2B9A767DCCABA2" + "F07081B5711BE1DEE90DFC8DE17970C2D937A16CD34581F52B8D59C9E9532D13"; const string InputHex = "3059" + "3013" + "06072A8648CE3D0201" + "06082A8648CE3D030107" + "0342" + "00" + PublicKeyValue; byte[] inputData = InputHex.HexToByteArray(); var spki = AsnSerializer.Deserialize <SubjectPublicKeyInfo>( inputData, AsnEncodingRules.DER); Assert.Equal("1.2.840.10045.2.1", spki.AlgorithmIdentifier.Algorithm.Value); Assert.Equal(PublicKeyValue, spki.PublicKey.ByteArrayToHex()); AsnReader reader = new AsnReader(spki.AlgorithmIdentifier.Parameters, AsnEncodingRules.DER); string curveOid = reader.ReadObjectIdentifierAsString(); Assert.False(reader.HasData, "reader.HasData"); Assert.Equal("1.2.840.10045.3.1.7", curveOid); }
public static void ReadEcPublicKey_DefiniteLength(PublicEncodingRules ruleSet) { const string InputHex = "3059" + "3013" + "06072A8648CE3D0201" + "06082A8648CE3D030107" + "0342" + "00" + "04" + "2363DD131DA65E899A2E63E9E05E50C830D4994662FFE883DB2B9A767DCCABA2" + "F07081B5711BE1DEE90DFC8DE17970C2D937A16CD34581F52B8D59C9E9532D13"; byte[] inputData = InputHex.HexToByteArray(); ReadEcPublicKey((AsnEncodingRules)ruleSet, inputData); }
public static void Decode_WrongContentType() { const string InputHex = "3080" + "0609608648016503040201" + "A080" + "3002" + "0500" + "0000" + "0000"; byte[] inputData = InputHex.HexToByteArray(); SignedCms cms = new SignedCms(); Assert.Throws<CryptographicException>(() => cms.Decode(inputData)); }
public static void ReadNamedBitList_BitArray_EveryPattern(AsnEncodingRules ruleSet) { const string InputHex = // Tag "DF836B" + // Length "820101" + // Unused bit count "00" + // Reversed bits for byte patterns 0x00-0x1F "008040C020A060E0109050D030B070F0088848C828A868E8189858D838B878F8" + // Reversed bits for byte patterns 0x20-0x3F "048444C424A464E4149454D434B474F40C8C4CCC2CAC6CEC1C9C5CDC3CBC7CFC" + // Reversed bits for byte patterns 0x40-0x5F "028242C222A262E2129252D232B272F20A8A4ACA2AAA6AEA1A9A5ADA3ABA7AFA" + // Reversed bits for byte patterns 0x60-0x7F "068646C626A666E6169656D636B676F60E8E4ECE2EAE6EEE1E9E5EDE3EBE7EFE" + // Reversed bits for byte patterns 0x80-0x9F "018141C121A161E1119151D131B171F1098949C929A969E9199959D939B979F9" + // Reversed bits for byte patterns 0xA0-0xBF "058545C525A565E5159555D535B575F50D8D4DCD2DAD6DED1D9D5DDD3DBD7DFD" + // Reversed bits for byte patterns 0xC0-0xDF "038343C323A363E3139353D333B373F30B8B4BCB2BAB6BEB1B9B5BDB3BBB7BFB" + // Reversed bits for byte patterns 0xE0-0xFF "078747C727A767E7179757D737B777F70F8F4FCF2FAF6FEF1F9F5FDF3FBF7FFF"; byte[] inputData = InputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, ruleSet); byte[] allTheBytes = new byte[256]; for (int i = 0; i < allTheBytes.Length; i++) { allTheBytes[i] = (byte)i; } BitArray bits = reader.ReadNamedBitList(new Asn1Tag(TagClass.Private, 491)); Assert.Equal(allTheBytes.Length * 8, bits.Length); Assert.False(reader.HasData); byte[] actual = new byte[allTheBytes.Length]; bits.CopyTo(actual, 0); Assert.Equal(actual, actual); }
public static void Deserialize_NamedBitLists() { const string InputHex = "3080" + "0303000841" + "0000"; byte[] inputBytes = InputHex.HexToByteArray(); var variants = AsnSerializer.Deserialize <NamedBitListModeVariants>( inputBytes, AsnEncodingRules.BER); Assert.Equal( SomeFlagsEnum.BitFour | SomeFlagsEnum.BitNine | SomeFlagsEnum.BitFifteen, variants.DefaultMode); }
public static void Deserialize_UtcTime_WithTwoYearMax() { const string UtcTimeValue = "170D3132303130323233353935395A"; const string InputHex = "3080" + UtcTimeValue + UtcTimeValue + UtcTimeValue + "0000"; byte[] inputBytes = InputHex.HexToByteArray(); UtcTimeTwoDigitYears dates = AsnSerializer.Deserialize <UtcTimeTwoDigitYears>( inputBytes, AsnEncodingRules.BER); Assert.Equal(new DateTimeOffset(1912, 1, 2, 23, 59, 59, TimeSpan.Zero), dates.ErnestoSabatoLifetime); Assert.Equal(new DateTimeOffset(2012, 1, 2, 23, 59, 59, TimeSpan.Zero), dates.MayanPhenomenon); Assert.Equal(new DateTimeOffset(2012, 1, 2, 23, 59, 59, TimeSpan.Zero), dates.ImplicitMax); }
public static void AllTheSimpleThings() { const string InputHex = "3080" + "0101FF" + "0201FE" + "020101" + "0202FEFF" + "02020101" + "0204FEFFFFFF" + "020401000001" + "0208FEFFFFFFFFFFFFFF" + "02080100000000000001" + "0209010000000000000001" + "0303000102" + "0404FF0055AA" + "0500" + "06082A8648CE3D030107" + "06072A8648CE3D0201" + "06092A864886F70D010101" + "0A011E" + "0C2544722E2026204D72732E20536D697468E280904A6F6E657320EFB9A0206368696C6472656E" + "162144722E2026204D72732E20536D6974682D4A6F6E65732026206368696C6472656E" + "1E42" + "00440072002E002000260020004D00720073002E00200053006D006900740068" + "2010004A006F006E006500730020FE600020006300680069006C006400720065" + "006E" + "3080" + "010100" + "010100" + "0101FF" + "0101FF" + "010100" + "0000" + "3180" + "020100" + "020101" + "0201FE" + "0201FF" + "02020100" + "0000" + "3080" + "020100" + "020101" + "020200FE" + "02017F" + "020200FF" + "0000" + "170D3530303130323132333435365A" + "170D3530303130323132333435365A" + "181432303136313130363031323334352E373635345A" + "180F32303136313130363031323334355A" + "020F0102030405060708090A0B0C0D0E0F" + "0000"; byte[] inputData = InputHex.HexToByteArray(); var atst = AsnSerializer.Deserialize <AllTheSimpleThings>( inputData, AsnEncodingRules.BER); const string UnicodeVerifier = "Dr. & Mrs. Smith\u2010Jones \uFE60 children"; const string AsciiVerifier = "Dr. & Mrs. Smith-Jones & children"; Assert.False(atst.NotBool, "atst.NotBool"); Assert.Equal(-2, atst.SByte); Assert.Equal(1, atst.Byte); Assert.Equal(unchecked ((short)0xFEFF), atst.Short); Assert.Equal(0x0101, atst.UShort); Assert.Equal(unchecked ((int)0xFEFFFFFF), atst.Int); Assert.Equal((uint)0x01000001, atst.UInt); Assert.Equal(unchecked ((long)0xFEFFFFFFFFFFFFFF), atst.Long); Assert.Equal(0x0100000000000001UL, atst.ULong); Assert.Equal("010000000000000001", atst.BigIntBytes.ByteArrayToHex()); Assert.Equal("0102", atst.BitStringBytes.ByteArrayToHex()); Assert.Equal("FF0055AA", atst.OctetStringBytes.ByteArrayToHex()); Assert.Equal("0500", atst.Null.ByteArrayToHex()); Assert.Equal("1.2.840.10045.3.1.7", atst.UnattrOid.Value); Assert.Equal("1.2.840.10045.3.1.7", atst.UnattrOid.FriendlyName); Assert.Equal("1.2.840.10045.2.1", atst.WithName.Value); Assert.Equal("ECC", atst.WithName.FriendlyName); Assert.Equal("1.2.840.113549.1.1.1", atst.OidString); Assert.Equal(UniversalTagNumber.BMPString, atst.LinearEnum); Assert.Equal(UnicodeVerifier, atst.Utf8Encoded); Assert.Equal(AsciiVerifier, atst.Ia5Encoded); Assert.Equal(UnicodeVerifier, atst.BmpEncoded); Assert.Equal(new[] { false, false, true, true, false }, atst.Bools); Assert.Equal(new[] { 0, 1, -2, -1, 256 }, atst.Ints); Assert.Equal(new byte[] { 0, 1, 254, 127, 255 }, atst.LittleUInts); Assert.Equal(new DateTimeOffset(1950, 1, 2, 12, 34, 56, TimeSpan.Zero), atst.UtcTime2049); Assert.Equal(new DateTimeOffset(2050, 1, 2, 12, 34, 56, TimeSpan.Zero), atst.UtcTime2099); Assert.Equal(new DateTimeOffset(2016, 11, 6, 1, 23, 45, 765, TimeSpan.Zero), atst.GeneralizedTimeWithFractions); Assert.Equal(new DateTimeOffset(2016, 11, 6, 1, 23, 45, TimeSpan.Zero), atst.GeneralizedTimeNoFractions); Assert.Equal(BigInteger.Parse("0102030405060708090A0B0C0D0E0F", NumberStyles.HexNumber), atst.BigInteger); }