-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Use HashSet/SearchValues when known OID list is long #117849
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Formats.Asn1; | ||
| using System.Runtime.InteropServices; | ||
|
|
@@ -14,7 +15,7 @@ internal static partial class KeyFormatHelper | |
| internal delegate void KeyReader<TRet>(ReadOnlyMemory<byte> key, in AlgorithmIdentifierAsn algId, out TRet ret); | ||
|
|
||
| internal static unsafe void ReadSubjectPublicKeyInfo<TRet>( | ||
| string[] validOids, | ||
| IStringLookup validOids, | ||
| ReadOnlySpan<byte> source, | ||
| KeyReader<TRet> keyReader, | ||
| out int bytesRead, | ||
|
|
@@ -30,7 +31,7 @@ internal static unsafe void ReadSubjectPublicKeyInfo<TRet>( | |
| } | ||
|
|
||
| internal static ReadOnlyMemory<byte> ReadSubjectPublicKeyInfo( | ||
| string[] validOids, | ||
| IStringLookup validOids, | ||
| ReadOnlyMemory<byte> source, | ||
| out int bytesRead) | ||
| { | ||
|
|
@@ -49,7 +50,7 @@ internal static ReadOnlyMemory<byte> ReadSubjectPublicKeyInfo( | |
| throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e); | ||
| } | ||
|
|
||
| if (Array.IndexOf(validOids, spki.Algorithm.Algorithm) < 0) | ||
| if (!validOids.Contains(spki.Algorithm.Algorithm)) | ||
| { | ||
| throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
| } | ||
|
|
@@ -59,7 +60,7 @@ internal static ReadOnlyMemory<byte> ReadSubjectPublicKeyInfo( | |
| } | ||
|
|
||
| private static void ReadSubjectPublicKeyInfo<TRet>( | ||
| string[] validOids, | ||
| IStringLookup validOids, | ||
| ReadOnlyMemory<byte> source, | ||
| KeyReader<TRet> keyReader, | ||
| out int bytesRead, | ||
|
|
@@ -80,7 +81,7 @@ private static void ReadSubjectPublicKeyInfo<TRet>( | |
| throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e); | ||
| } | ||
|
|
||
| if (Array.IndexOf(validOids, spki.Algorithm.Algorithm) < 0) | ||
| if (!validOids.Contains(spki.Algorithm.Algorithm)) | ||
| { | ||
| throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
| } | ||
|
|
@@ -90,7 +91,7 @@ private static void ReadSubjectPublicKeyInfo<TRet>( | |
| } | ||
|
|
||
| internal static unsafe void ReadPkcs8<TRet>( | ||
| string[] validOids, | ||
| IStringLookup validOids, | ||
| ReadOnlySpan<byte> source, | ||
| KeyReader<TRet> keyReader, | ||
| out int bytesRead, | ||
|
|
@@ -106,7 +107,7 @@ internal static unsafe void ReadPkcs8<TRet>( | |
| } | ||
|
|
||
| internal static ReadOnlyMemory<byte> ReadPkcs8( | ||
| string[] validOids, | ||
| IStringLookup validOids, | ||
| ReadOnlyMemory<byte> source, | ||
| out int bytesRead) | ||
| { | ||
|
|
@@ -116,7 +117,7 @@ internal static ReadOnlyMemory<byte> ReadPkcs8( | |
| int read = reader.PeekEncodedValue().Length; | ||
| PrivateKeyInfoAsn.Decode(ref reader, source, out PrivateKeyInfoAsn privateKeyInfo); | ||
|
|
||
| if (Array.IndexOf(validOids, privateKeyInfo.PrivateKeyAlgorithm.Algorithm) < 0) | ||
| if (!validOids.Contains(privateKeyInfo.PrivateKeyAlgorithm.Algorithm)) | ||
| { | ||
| throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
| } | ||
|
|
@@ -131,7 +132,7 @@ internal static ReadOnlyMemory<byte> ReadPkcs8( | |
| } | ||
|
|
||
| private static void ReadPkcs8<TRet>( | ||
| string[] validOids, | ||
| IStringLookup validOids, | ||
| ReadOnlyMemory<byte> source, | ||
| KeyReader<TRet> keyReader, | ||
| out int bytesRead, | ||
|
|
@@ -143,7 +144,7 @@ private static void ReadPkcs8<TRet>( | |
| int read = reader.PeekEncodedValue().Length; | ||
| PrivateKeyInfoAsn.Decode(ref reader, source, out PrivateKeyInfoAsn privateKeyInfo); | ||
|
|
||
| if (Array.IndexOf(validOids, privateKeyInfo.PrivateKeyAlgorithm.Algorithm) < 0) | ||
| if (!validOids.Contains(privateKeyInfo.PrivateKeyAlgorithm.Algorithm)) | ||
| { | ||
| throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
| } | ||
|
|
@@ -204,5 +205,27 @@ internal static AsnWriter WritePkcs8( | |
| writer.PopSequence(); | ||
| return writer; | ||
| } | ||
|
|
||
| internal interface IStringLookup | ||
| { | ||
| bool Contains(string value); | ||
| } | ||
|
|
||
| internal sealed class StringLookupArray(string[] backingArray) : IStringLookup | ||
| { | ||
| public bool Contains(string value) => Array.IndexOf(backingArray, value) >= 0; | ||
| } | ||
|
|
||
| #if NET9_0_OR_GREATER | ||
| internal sealed class StringLookup(SearchValues<string> backingSearchValues) : IStringLookup | ||
| { | ||
| public bool Contains(string value) => backingSearchValues.Contains(value); | ||
|
Comment on lines
+220
to
+222
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Despite the name, The only searches this is being used for are |
||
| } | ||
| #else | ||
| internal sealed class StringLookup(HashSet<string> backingSet) : IStringLookup | ||
| { | ||
| public bool Contains(string value) => backingSet.Contains(value); | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The StringLookupArray class stores the backing array but doesn't make a defensive copy. Consider making a copy of the input array to prevent external modifications that could affect lookup behavior.