Skip to content

Commit

Permalink
Add well-known OIDs to System.Formats.Asn1
Browse files Browse the repository at this point in the history
This adds most of the OIDs that are read or written during the execution of any of the
crypto tests or SslStream tests as a well-known pattern of bytes and a pre-allocated/
const/interned string.

Reading a well-known OID drops from ~300ns to ~60ns and saves on allocating the output
string, and possibly an `int[]` in BigInteger for large-RID OIDs, while increasing the
cost for a less well-known OID by about 40ns.

Writing a well-known OID drops from ~1200ns to ~90ns, and increases the cost for a less
well-known OID by only about 10ns.

For purposes of this initial list, an oid was well-known if (more or less):
* It was the output of ReadObjectIdentifier more than twice, and
* It was not a facetious/test value (e.g. 0.0), and
* It was not a vendor-specific Certificate Practices Statement; or
* It was an obviously linked OID potentially skewed out by our test data
  * e.g. EcdsaWithSha512 would be automatically included by closeness to EcdsaWithSha256.
  • Loading branch information
bartonjs committed Sep 13, 2022
1 parent dc0e2d9 commit 046fc40
Show file tree
Hide file tree
Showing 4 changed files with 459 additions and 16 deletions.
Expand Up @@ -53,6 +53,7 @@ System.Formats.Asn1.AsnWriter</PackageDescription>
<Compile Include="System\Formats\Asn1\SetOfValueComparer.cs" />
<Compile Include="System\Formats\Asn1\TagClass.cs" />
<Compile Include="System\Formats\Asn1\UniversalTagNumber.cs" />
<Compile Include="System\Formats\Asn1\WellKnownOids.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
Expand Down
Expand Up @@ -53,8 +53,27 @@ public static partial class AsnDecoder
out int bytesConsumed,
Asn1Tag? expectedTag = null)
{
// TODO: Inline this call when it won't cause a PR/diff problem.
return ReadObjectIdentifier(source, ruleSet, expectedTag, out bytesConsumed);
// T-REC-X.690-201508 sec 8.19.1
ReadOnlySpan<byte> contents = GetPrimitiveContentSpan(
source,
ruleSet,
expectedTag ?? Asn1Tag.ObjectIdentifier,
UniversalTagNumber.ObjectIdentifier,
out int consumed);

#if NETCOREAPP
string? wellKnown = WellKnownOids.GetValue(contents);

if (wellKnown is not null)
{
bytesConsumed = consumed;
return wellKnown;
}
#endif

string ret = ReadObjectIdentifier(contents);
bytesConsumed = consumed;
return ret;
}

private static void ReadSubIdentifier(
Expand Down Expand Up @@ -172,20 +191,8 @@ public static partial class AsnDecoder
CryptoPool.Return(tmpBytes, bytesWritten);
}

private static string ReadObjectIdentifier(
ReadOnlySpan<byte> source,
AsnEncodingRules ruleSet,
Asn1Tag? expectedTag,
out int totalBytesRead)
private static string ReadObjectIdentifier(ReadOnlySpan<byte> contents)
{
// T-REC-X.690-201508 sec 8.19.1
ReadOnlySpan<byte> contents = GetPrimitiveContentSpan(
source,
ruleSet,
expectedTag ?? Asn1Tag.ObjectIdentifier,
UniversalTagNumber.ObjectIdentifier,
out int consumed);

// T-REC-X.690-201508 sec 8.19.2 says the minimum length is 1
if (contents.Length < 1)
{
Expand Down Expand Up @@ -278,7 +285,6 @@ public static partial class AsnDecoder
contents = contents.Slice(bytesRead);
}

totalBytesRead = consumed;
return builder.ToString();
}
}
Expand Down
Expand Up @@ -59,6 +59,19 @@ public void WriteObjectIdentifier(ReadOnlySpan<char> oidValue, Asn1Tag? tag = nu
{
CheckUniversalTag(tag, UniversalTagNumber.ObjectIdentifier);

#if NETCOREAPP
ReadOnlySpan<byte> wellKnownContents = WellKnownOids.GetContents(oidValue);

if (!wellKnownContents.IsEmpty)
{
WriteTag(tag?.AsPrimitive() ?? Asn1Tag.ObjectIdentifier);
WriteLength(wellKnownContents.Length);
wellKnownContents.CopyTo(_buffer.AsSpan(_offset));
_offset += wellKnownContents.Length;
return;
}
#endif

WriteObjectIdentifierCore(tag?.AsPrimitive() ?? Asn1Tag.ObjectIdentifier, oidValue);
}

Expand Down

0 comments on commit 046fc40

Please sign in to comment.