Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add well-known OIDs to System.Formats.Asn1 #75485

Merged
merged 2 commits into from Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
using System.Security.Cryptography;
using System.Text;

Expand Down Expand Up @@ -53,8 +55,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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably as a follow up, but since most dotted-decimal OIDs fit in a stackalloc char[256], maybe this path could use ValueStringBuilder instead of StringBuilder.

bytesConsumed = consumed;
return ret;
}

private static void ReadSubIdentifier(
Expand Down Expand Up @@ -172,20 +193,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 +287,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