Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);CP_NO_ZEROMEMORY</DefineConstants>
<IsPackable>true</IsPackable>
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
<PackageDescription>Provides classes that can read and write the ASN.1 BER, CER, and DER data formats.

Commonly Used Types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public static Enum ReadEnumeratedValue(
}

// T-REC-X.690-201508 sec 8.4 says the contents are the same as for integers.
int sizeLimit = Marshal.SizeOf(backingType);
int sizeLimit = GetPrimitiveIntegerSize(backingType);

if (backingType == typeof(int) ||
backingType == typeof(long) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static Enum ReadNamedBitListValue(
}

Span<byte> stackSpan = stackalloc byte[sizeof(ulong)];
int sizeLimit = Marshal.SizeOf(backingType);
int sizeLimit = GetPrimitiveIntegerSize(backingType);
stackSpan = stackSpan.Slice(0, sizeLimit);

bool read = TryReadBitString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,19 @@ private static AsnContentException GetValidityException(LengthValidity validity)
}
}

private static int GetPrimitiveIntegerSize(Type primitiveType)
{
if (primitiveType == typeof(byte) || primitiveType == typeof(sbyte))
return 1;
if (primitiveType == typeof(short) || primitiveType == typeof(ushort))
return 2;
if (primitiveType == typeof(int) || primitiveType == typeof(uint))
return 4;
if (primitiveType == typeof(long) || primitiveType == typeof(ulong))
return 8;
return 0;
}

/// <summary>
/// Get the number of bytes between the start of <paramref name="source" /> and
/// the End-of-Contents marker
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/System.Formats.Asn1/tests/Reader/ReadLength.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private delegate Asn1Tag ReadTagAndLengthDelegate(
[InlineData(4, 255, "0481FF")]
[InlineData(2, 256, "02820100")]
[InlineData(4, int.MaxValue, "04847FFFFFFF")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/72548", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public static void MinimalPrimitiveLength(int tagValue, int length, string inputHex)
{
byte[] inputBytes = inputHex.HexToByteArray();
Expand Down Expand Up @@ -60,6 +61,7 @@ public static void ReadWithUnknownRuleSet(int invalidRuleSetValue)
[InlineData("048201")]
[InlineData("04830102")]
[InlineData("0484010203")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/72548", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public static void ReadWithInsufficientData(string inputHex)
{
byte[] inputData = inputHex.HexToByteArray();
Expand Down Expand Up @@ -95,6 +97,7 @@ public static void ReadWithInsufficientData(string inputHex)
[InlineData("CER 5 byte spread", AsnEncodingRules.CER, "04850100000000")]
[InlineData("DER 5 byte spread", AsnEncodingRules.DER, "04850100000000")]
[InlineData("BER padded 5 byte spread", AsnEncodingRules.BER, "0486000100000000")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/72548", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public static void InvalidLengths(
string description,
AsnEncodingRules rules,
Expand All @@ -111,6 +114,7 @@ public static void InvalidLengths(
[Theory]
[InlineData(AsnEncodingRules.BER)]
[InlineData(AsnEncodingRules.CER)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/72548", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public static void IndefiniteLength(AsnEncodingRules ruleSet)
{
// SEQUENCE (indefinite)
Expand All @@ -135,6 +139,7 @@ public static void IndefiniteLength(AsnEncodingRules ruleSet)
[InlineData(0, "0483000000")]
[InlineData(1, "048A00000000000000000001")]
[InlineData(128, "049000000000000000000000000000000080")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/72548", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public static void BerNonMinimalLength(int expectedLength, string inputHex)
{
byte[] inputData = inputHex.HexToByteArray();
Expand All @@ -156,6 +161,7 @@ public static void BerNonMinimalLength(int expectedLength, string inputHex)
[InlineData(AsnEncodingRules.BER, 4, 0, 5, "0483000000" + "0500")]
[InlineData(AsnEncodingRules.DER, 1, 1, 2, "0101" + "FF")]
[InlineData(AsnEncodingRules.CER, 0x10, null, 2, "3080" + "0500" + "0000")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/72548", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public static void ReadWithDataRemaining(
AsnEncodingRules ruleSet,
int tagValue,
Expand Down
1 change: 0 additions & 1 deletion src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.DirectoryServices.Protocols\tests\System.DirectoryServices.Protocols.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Drawing.Primitives\tests\System.Drawing.Primitives.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Dynamic.Runtime\tests\System.Dynamic.Runtime.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Formats.Asn1\tests\System.Formats.Asn1.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Formats.Cbor\tests\System.Formats.Cbor.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Globalization\tests\System.Globalization.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Globalization\tests\NlsTests\System.Globalization.Nls.Tests.csproj" />
Expand Down