public static void TagMustBeCorrect_Custom(PublicEncodingRules ruleSet) { byte[] inputData = "850D3530303130323132333435365A".HexToByteArray(); AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); Assert.Throws <ArgumentException>( "expectedTag", () => reader.ReadUtcTime(Asn1Tag.Null)); Assert.True(reader.HasData, "HasData after bad universal tag"); Assert.Throws <CryptographicException>(() => reader.ReadUtcTime()); Assert.True(reader.HasData, "HasData after default tag"); Assert.Throws <CryptographicException>( () => reader.ReadUtcTime(new Asn1Tag(TagClass.Application, 5))); Assert.True(reader.HasData, "HasData after wrong custom class"); Assert.Throws <CryptographicException>( () => reader.ReadUtcTime(new Asn1Tag(TagClass.ContextSpecific, 7))); Assert.True(reader.HasData, "HasData after wrong custom tag value"); Assert.Equal( new DateTimeOffset(1950, 1, 2, 12, 34, 56, TimeSpan.Zero), reader.ReadUtcTime(new Asn1Tag(TagClass.ContextSpecific, 5))); Assert.False(reader.HasData, "HasData after reading value"); }
public static void ParseTime_Valid( PublicEncodingRules ruleSet, string inputHex, int year, int month, int day, int hour, int minute, int second, int offsetHour, int offsetMinute) { byte[] inputData = inputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); DateTimeOffset value = reader.ReadUtcTime(); Assert.Equal(year, value.Year); Assert.Equal(month, value.Month); Assert.Equal(day, value.Day); Assert.Equal(hour, value.Hour); Assert.Equal(minute, value.Minute); Assert.Equal(second, value.Second); Assert.Equal(0, value.Millisecond); Assert.Equal(new TimeSpan(offsetHour, offsetMinute, 0), value.Offset); }
public static void TagMustBeCorrect_Custom(AsnEncodingRules ruleSet) { byte[] inputData = "850F32303136313130363031323334355A".HexToByteArray(); AsnReader reader = new AsnReader(inputData, ruleSet); AssertExtensions.Throws <ArgumentException>( "expectedTag", () => reader.ReadGeneralizedTime(Asn1Tag.Null)); Assert.True(reader.HasData, "HasData after bad universal tag"); Assert.Throws <AsnContentException>(() => reader.ReadUtcTime()); Assert.True(reader.HasData, "HasData after default tag"); Assert.Throws <AsnContentException>( () => reader.ReadGeneralizedTime(new Asn1Tag(TagClass.Application, 5))); Assert.True(reader.HasData, "HasData after wrong custom class"); Assert.Throws <AsnContentException>( () => reader.ReadGeneralizedTime(new Asn1Tag(TagClass.ContextSpecific, 7))); Assert.True(reader.HasData, "HasData after wrong custom tag value"); Assert.Equal( new DateTimeOffset(2016, 11, 6, 1, 23, 45, TimeSpan.Zero), reader.ReadGeneralizedTime(new Asn1Tag(TagClass.ContextSpecific, 5))); Assert.False(reader.HasData, "HasData after reading value"); }
public static void ReadUtcTime_TwoYearMaximum(int maximum, int interpretedYear) { byte[] inputData = "170D3132303130323233353935395A".HexToByteArray(); AsnReader reader = new AsnReader(inputData, AsnEncodingRules.BER); DateTimeOffset value = reader.ReadUtcTime(maximum); Assert.Equal(interpretedYear, value.Year); }
public static DateTime DecodeUtcTime(byte[] encodedUtcTime) { // Read using BER because the CMS specification says the encoding is BER. AsnReader reader = new AsnReader(encodedUtcTime, AsnEncodingRules.BER); DateTimeOffset value = reader.ReadUtcTime(); reader.ThrowIfNotEmpty(); return(value.UtcDateTime); }
public static void ReadUtcTime_Throws( string description, PublicEncodingRules ruleSet, string inputHex) { byte[] inputData = inputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); Assert.Throws <CryptographicException>(() => reader.ReadUtcTime()); }
public static void ReadUtcTime_Throws( string description, AsnEncodingRules ruleSet, string inputHex) { _ = description; byte[] inputData = inputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, ruleSet); Assert.Throws <AsnContentException>(() => reader.ReadUtcTime()); }
public static void ReadUtcTime_TwoYearMaximum_FromOptions_CustomTag(int maximum, int interpretedYear) { byte[] inputData = "820D3132303130323233353935395A".HexToByteArray(); AsnReaderOptions options = new AsnReaderOptions { UtcTimeTwoDigitYearMax = maximum }; AsnReader reader = new AsnReader(inputData, AsnEncodingRules.BER, options); DateTimeOffset value = reader.ReadUtcTime(maximum, new Asn1Tag(TagClass.ContextSpecific, 2)); Assert.Equal(interpretedYear, value.Year); }
public static void ReadUtcTime_WayTooBig_Throws() { // Need to exceed the length that the shared pool will return for 17: byte[] inputData = new byte[4097 + 4]; inputData[0] = 0x17; inputData[1] = 0x82; inputData[2] = 0x10; inputData[3] = 0x01; AsnReader reader = new AsnReader(inputData, AsnEncodingRules.BER); Assert.Throws <CryptographicException>(() => reader.ReadUtcTime()); }
public static void TagMustBeCorrect_Universal(AsnEncodingRules ruleSet) { byte[] inputData = "170D3530303130323132333435365A".HexToByteArray(); AsnReader reader = new AsnReader(inputData, ruleSet); AssertExtensions.Throws <ArgumentException>( "expectedTag", () => reader.ReadUtcTime(expectedTag: Asn1Tag.Null)); Assert.True(reader.HasData, "HasData after bad universal tag"); Assert.Throws <AsnContentException>( () => reader.ReadUtcTime(expectedTag: new Asn1Tag(TagClass.ContextSpecific, 0))); Assert.True(reader.HasData, "HasData after wrong tag"); Assert.Equal( new DateTimeOffset(1950, 1, 2, 12, 34, 56, TimeSpan.Zero), reader.ReadUtcTime()); Assert.False(reader.HasData, "HasData after read"); }
public static void ParseTime_InvalidValue_LegalString() { byte[] inputData = "17113030303030303030303030302D31353030".HexToByteArray(); var exception = Assert.Throws <CryptographicException>( () => { AsnReader reader = new AsnReader(inputData, AsnEncodingRules.BER); reader.ReadUtcTime(); }); Assert.NotNull(exception.InnerException); Assert.IsType <ArgumentOutOfRangeException>(exception.InnerException); }
public static void ExpectedTag_IgnoresConstructed( PublicEncodingRules ruleSet, string inputHex, PublicTagClass tagClass, int tagValue) { byte[] inputData = inputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); DateTimeOffset val1 = reader.ReadUtcTime(new Asn1Tag((TagClass)tagClass, tagValue, true)); Assert.False(reader.HasData); reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); DateTimeOffset val2 = reader.ReadUtcTime(new Asn1Tag((TagClass)tagClass, tagValue, false)); Assert.False(reader.HasData); Assert.Equal(val1, val2); }
internal static void Decode(AsnReader reader, out TimeAsn decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = default; Asn1Tag tag = reader.PeekTag(); if (tag.HasSameClassAndValue(Asn1Tag.UtcTime)) { decoded.UtcTime = reader.ReadUtcTime(); } else if (tag.HasSameClassAndValue(Asn1Tag.GeneralizedTime)) { decoded.GeneralTime = reader.ReadGeneralizedTime(); } else { throw new CryptographicException(); } }
public static void ReadMicrosoftComCert() { byte[] bytes = MicrosoftDotComSslCertBytes; AsnReader fileReader = new AsnReader(bytes, AsnEncodingRules.DER); AsnReader certReader = fileReader.ReadSequence(); Assert.False(fileReader.HasData, "fileReader.HasData"); AsnReader tbsCertReader = certReader.ReadSequence(); AsnReader sigAlgReader = certReader.ReadSequence(); Assert.True( certReader.TryReadPrimitiveBitString( out int unusedBitCount, out ReadOnlyMemory <byte> signature), "certReader.TryReadPrimitiveBitStringValue"); Assert.Equal(0, unusedBitCount); AssertRefSame(signature, ref bytes[1176], "Signature is a ref to bytes[1176]"); Assert.False(certReader.HasData, "certReader.HasData"); AsnReader versionExplicitWrapper = tbsCertReader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0)); Assert.True(versionExplicitWrapper.TryReadInt32(out int certVersion)); Assert.Equal(2, certVersion); Assert.False(versionExplicitWrapper.HasData, "versionExplicitWrapper.HasData"); ReadOnlyMemory <byte> serialBytes = tbsCertReader.ReadIntegerBytes(); AssertRefSame(serialBytes, ref bytes[15], "Serial number starts at bytes[15]"); AsnReader tbsSigAlgReader = tbsCertReader.ReadSequence(); Assert.Equal("1.2.840.113549.1.1.11", tbsSigAlgReader.ReadObjectIdentifier()); Assert.True(tbsSigAlgReader.HasData, "tbsSigAlgReader.HasData before ReadNull"); tbsSigAlgReader.ReadNull(); Assert.False(tbsSigAlgReader.HasData, "tbsSigAlgReader.HasData after ReadNull"); AsnReader issuerReader = tbsCertReader.ReadSequence(); Asn1Tag printableString = new Asn1Tag(UniversalTagNumber.PrintableString); AssertRdn(issuerReader, "2.5.4.6", 57, printableString, bytes, "issuer[C]"); AssertRdn(issuerReader, "2.5.4.10", 70, printableString, bytes, "issuer[O]"); AssertRdn(issuerReader, "2.5.4.11", 101, printableString, bytes, "issuer[OU]"); AssertRdn(issuerReader, "2.5.4.3", 134, printableString, bytes, "issuer[CN]"); Assert.False(issuerReader.HasData, "issuerReader.HasData"); AsnReader validityReader = tbsCertReader.ReadSequence(); Assert.Equal(new DateTimeOffset(2014, 10, 15, 0, 0, 0, TimeSpan.Zero), validityReader.ReadUtcTime()); Assert.Equal(new DateTimeOffset(2016, 10, 15, 23, 59, 59, TimeSpan.Zero), validityReader.ReadUtcTime()); Assert.False(validityReader.HasData, "validityReader.HasData"); AsnReader subjectReader = tbsCertReader.ReadSequence(); Asn1Tag utf8String = new Asn1Tag(UniversalTagNumber.UTF8String); AssertRdn(subjectReader, "1.3.6.1.4.1.311.60.2.1.3", 220, printableString, bytes, "subject[EV Country]"); AssertRdn(subjectReader, "1.3.6.1.4.1.311.60.2.1.2", 241, utf8String, bytes, "subject[EV State]", "Washington"); AssertRdn(subjectReader, "2.5.4.15", 262, printableString, bytes, "subject[Business Category]"); AssertRdn(subjectReader, "2.5.4.5", 293, printableString, bytes, "subject[Serial Number]"); AssertRdn(subjectReader, "2.5.4.6", 313, printableString, bytes, "subject[C]"); AssertRdn(subjectReader, "2.5.4.17", 326, utf8String, bytes, "subject[Postal Code]", "98052"); AssertRdn(subjectReader, "2.5.4.8", 342, utf8String, bytes, "subject[ST]", "Washington"); AssertRdn(subjectReader, "2.5.4.7", 363, utf8String, bytes, "subject[L]", "Redmond"); AssertRdn(subjectReader, "2.5.4.9", 381, utf8String, bytes, "subject[Street Address]", "1 Microsoft Way"); AssertRdn(subjectReader, "2.5.4.10", 407, utf8String, bytes, "subject[O]", "Microsoft Corporation"); AssertRdn(subjectReader, "2.5.4.11", 439, utf8String, bytes, "subject[OU]", "MSCOM"); AssertRdn(subjectReader, "2.5.4.3", 455, utf8String, bytes, "subject[CN]", "www.microsoft.com"); Assert.False(subjectReader.HasData, "subjectReader.HasData"); AsnReader subjectPublicKeyInfo = tbsCertReader.ReadSequence(); AsnReader spkiAlgorithm = subjectPublicKeyInfo.ReadSequence(); Assert.Equal("1.2.840.113549.1.1.1", spkiAlgorithm.ReadObjectIdentifier()); spkiAlgorithm.ReadNull(); Assert.False(spkiAlgorithm.HasData, "spkiAlgorithm.HasData"); Assert.True( subjectPublicKeyInfo.TryReadPrimitiveBitString( out unusedBitCount, out ReadOnlyMemory <byte> encodedPublicKey), "subjectPublicKeyInfo.TryReadBitStringBytes"); Assert.Equal(0, unusedBitCount); AssertRefSame(encodedPublicKey, ref bytes[498], "Encoded public key starts at byte 498"); Assert.False(subjectPublicKeyInfo.HasData, "subjectPublicKeyInfo.HasData"); AsnReader publicKeyReader = new AsnReader(encodedPublicKey, AsnEncodingRules.DER); AsnReader rsaPublicKeyReader = publicKeyReader.ReadSequence(); AssertRefSame(rsaPublicKeyReader.ReadIntegerBytes(), ref bytes[506], "RSA Modulus is at bytes[502]"); Assert.True(rsaPublicKeyReader.TryReadInt32(out int rsaExponent)); Assert.Equal(65537, rsaExponent); Assert.False(rsaPublicKeyReader.HasData, "rsaPublicKeyReader.HasData"); Assert.False(publicKeyReader.HasData, "publicKeyReader.HasData"); AsnReader extensionsContainer = tbsCertReader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 3)); AsnReader extensions = extensionsContainer.ReadSequence(); Assert.False(extensionsContainer.HasData, "extensionsContainer.HasData"); AsnReader sanExtension = extensions.ReadSequence(); Assert.Equal("2.5.29.17", sanExtension.ReadObjectIdentifier()); Assert.True(sanExtension.TryReadPrimitiveOctetString(out ReadOnlyMemory <byte> sanExtensionBytes)); Assert.False(sanExtension.HasData, "sanExtension.HasData"); AsnReader sanExtensionPayload = new AsnReader(sanExtensionBytes, AsnEncodingRules.DER); AsnReader sanExtensionValue = sanExtensionPayload.ReadSequence(); Assert.False(sanExtensionPayload.HasData, "sanExtensionPayload.HasData"); Asn1Tag dnsName = new Asn1Tag(TagClass.ContextSpecific, 2); Assert.Equal("www.microsoft.com", sanExtensionValue.ReadCharacterString(UniversalTagNumber.IA5String, dnsName)); Assert.Equal("wwwqa.microsoft.com", sanExtensionValue.ReadCharacterString(UniversalTagNumber.IA5String, dnsName)); Assert.False(sanExtensionValue.HasData, "sanExtensionValue.HasData"); AsnReader basicConstraints = extensions.ReadSequence(); Assert.Equal("2.5.29.19", basicConstraints.ReadObjectIdentifier()); Assert.True(basicConstraints.TryReadPrimitiveOctetString(out ReadOnlyMemory <byte> basicConstraintsBytes)); AsnReader basicConstraintsPayload = new AsnReader(basicConstraintsBytes, AsnEncodingRules.DER); AsnReader basicConstraintsValue = basicConstraintsPayload.ReadSequence(); Assert.False(basicConstraintsValue.HasData, "basicConstraintsValue.HasData"); Assert.False(basicConstraintsPayload.HasData, "basicConstraintsPayload.HasData"); AsnReader keyUsageExtension = extensions.ReadSequence(); Assert.Equal("2.5.29.15", keyUsageExtension.ReadObjectIdentifier()); Assert.True(keyUsageExtension.ReadBoolean(), "keyUsageExtension.ReadBoolean() (IsCritical)"); Assert.True(keyUsageExtension.TryReadPrimitiveOctetString(out ReadOnlyMemory <byte> keyUsageBytes)); AsnReader keyUsagePayload = new AsnReader(keyUsageBytes, AsnEncodingRules.DER); Assert.Equal( X509KeyUsageCSharpStyle.DigitalSignature | X509KeyUsageCSharpStyle.KeyEncipherment, keyUsagePayload.ReadNamedBitListValue <X509KeyUsageCSharpStyle>()); Assert.False(keyUsagePayload.HasData, "keyUsagePayload.HasData"); AssertExtension(extensions, "2.5.29.37", false, 863, bytes); AssertExtension(extensions, "2.5.29.32", false, 894, bytes); AssertExtension(extensions, "2.5.29.35", false, 998, bytes); AssertExtension(extensions, "2.5.29.31", false, 1031, bytes); AssertExtension(extensions, "1.3.6.1.5.5.7.1.1", false, 1081, bytes); Assert.False(extensions.HasData, "extensions.HasData"); Assert.Equal("1.2.840.113549.1.1.11", sigAlgReader.ReadObjectIdentifier()); sigAlgReader.ReadNull(); Assert.False(sigAlgReader.HasData); }
public static void ReadSequenceOf_PreservesOptions(AsnEncodingRules ruleSet) { // [5] (UtcTime) 500102123456Z // UtcTime 120102235959Z // // They're sorted backwards, though. const string PayloadHex = "850D3530303130323132333435365A" + "170D3132303130323233353935395A"; byte[] inputData; // Build the rule-specific form of SEQUENCE { [PRIVATE 9] SEQUENCE { SET-OF { dates }, NULL } } // The outer Set-Of is also invalid, because the NULL should be first. if (ruleSet == AsnEncodingRules.DER) { inputData = ("3024" + "E922" + "A21E" + PayloadHex + "0500").HexToByteArray(); } else { string inputHex = "3080" + "E980" + "A280" + PayloadHex + "0000" + "0500" + "0000" + "0000"; inputData = inputHex.HexToByteArray(); } AsnReaderOptions options = new AsnReaderOptions { SkipSetSortOrderVerification = true, UtcTimeTwoDigitYearMax = 2011, }; AsnReader initial = new AsnReader(inputData, ruleSet, options); AsnReader outer = initial.ReadSequence(); Assert.False(initial.HasData); AsnReader inner = outer.ReadSequence(new Asn1Tag(TagClass.Private, 9)); Assert.False(outer.HasData); Asn1Tag setTag = new Asn1Tag(TagClass.ContextSpecific, 2); if (ruleSet != AsnEncodingRules.BER) { Assert.Throws <AsnContentException>(() => inner.ReadSetOf(false, setTag)); } // This confirms that we've passed SkipSetOrderVerification this far. AsnReader setOf = inner.ReadSetOf(setTag); Assert.True(inner.HasData); Assert.Equal( new DateTimeOffset(1950, 1, 2, 12, 34, 56, TimeSpan.Zero), setOf.ReadUtcTime(new Asn1Tag(TagClass.ContextSpecific, 5))); // This confirms that we've passed UtcTimeTwoDigitYearMax, // the default would call this 2012. Assert.Equal( new DateTimeOffset(1912, 1, 2, 23, 59, 59, TimeSpan.Zero), setOf.ReadUtcTime()); Assert.False(setOf.HasData); inner.ReadNull(); Assert.False(inner.HasData); setOf.ThrowIfNotEmpty(); inner.ThrowIfNotEmpty(); outer.ThrowIfNotEmpty(); initial.ThrowIfNotEmpty(); }