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

Use ReadOnlySpan<byte> instead of byte arrays #35125

Merged
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 @@ -10,9 +10,9 @@ internal partial class Interop
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool CryptImportKey(
internal static extern unsafe bool CryptImportKey(
SafeProvHandle hProv,
byte[] pbData,
byte* pbData,
int dwDataLen,
SafeKeyHandle hPubKey,
int dwFlags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Internal.NativeCrypto
/// </summary>
internal static partial class CapiHelper
{
private static readonly byte[] s_RgbPubKey =
private static ReadOnlySpan<byte> RgbPubKey => new byte[]
{
0x06, 0x02, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x00,
0x52, 0x53, 0x41, 0x31, 0x00, 0x02, 0x00, 0x00,
Expand Down Expand Up @@ -1022,7 +1022,7 @@ internal static void ImportKeyBlob(SafeProvHandle saveProvHandle, CspProviderFla
}

SafeKeyHandle hKey;
if (!CryptImportKey(saveProvHandle, keyBlob, keyBlob.Length, SafeKeyHandle.InvalidHandle, dwCapiFlags, out hKey))
if (!CryptImportKey(saveProvHandle, keyBlob, SafeKeyHandle.InvalidHandle, dwCapiFlags, out hKey))
{
int hr = Marshal.GetHRForLastWin32Error();

Expand Down Expand Up @@ -1330,7 +1330,7 @@ private static void UnloadKey(SafeProvHandle hProv, SafeKeyHandle hKey, [NotNull
try
{
// Import the public key
if (!CryptImportKey(hProv, s_RgbPubKey, s_RgbPubKey.Length, SafeKeyHandle.InvalidHandle, 0, out hPubKey))
if (!CryptImportKey(hProv, RgbPubKey, SafeKeyHandle.InvalidHandle, 0, out hPubKey))
{
int hr = Marshal.GetHRForLastWin32Error();
throw hr.ToCryptographicException();
Expand Down Expand Up @@ -1469,19 +1469,21 @@ public static bool CryptGenKey(
return response;
}

public static bool CryptImportKey(
public static unsafe bool CryptImportKey(
SafeProvHandle hProv,
byte[] pbData,
int dwDataLen,
ReadOnlySpan<byte> pbData,
SafeKeyHandle hPubKey,
int dwFlags,
out SafeKeyHandle phKey)
{
bool response = Interop.Advapi32.CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, out phKey);
fixed (byte* pbDataPtr = pbData)
{
bool response = Interop.Advapi32.CryptImportKey(hProv, pbDataPtr, pbData.Length, hPubKey, dwFlags, out phKey);

phKey.SetParent(hProv);
phKey.SetParent(hProv);

return response;
return response;
}
}

public static bool CryptCreateHash(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ namespace Internal.Cryptography
{
internal static partial class PkcsHelpers
{
private static readonly byte[] s_pSpecifiedDefaultParameters = { 0x04, 0x00 };

#if !NETCOREAPP && !NETSTANDARD2_1
// Compatibility API.
internal static void AppendData(this IncrementalHash hasher, ReadOnlySpan<byte> data)
Expand Down Expand Up @@ -532,8 +530,6 @@ public static byte[] EncodeOctetString(byte[] octets)
}
}

private static readonly byte[] s_invalidEmptyOid = { 0x06, 0x00 };

public static byte[] EncodeUtcTime(DateTime utcTime)
{
const int maxLegalYear = 2049;
Expand Down Expand Up @@ -573,16 +569,16 @@ public static DateTime DecodeUtcTime(byte[] encodedUtcTime)
return value.UtcDateTime;
}

public static string DecodeOid(byte[] encodedOid)
public static string DecodeOid(ReadOnlySpan<byte> encodedOid)
{
// Windows compat.
if (s_invalidEmptyOid.AsSpan().SequenceEqual(encodedOid))
// Windows compat for a zero length OID.
if (encodedOid.Length == 2 && encodedOid[0] == 0x06 && encodedOid[1] == 0x00)
{
return string.Empty;
}

// Read using BER because the CMS specification says the encoding is BER.
AsnReader reader = new AsnReader(encodedOid, AsnEncodingRules.BER);
AsnValueReader reader = new AsnValueReader(encodedOid, AsnEncodingRules.BER);
string value = reader.ReadObjectIdentifierAsString();
reader.ThrowIfNotEmpty();
return value;
Expand Down Expand Up @@ -623,8 +619,10 @@ public static bool TryGetRsaOaepEncryptionPadding(
return false;
}

ReadOnlySpan<byte> pSpecifiedDefaultParameters = new byte[] { 0x04, 0x00 };

if (oaepParameters.PSourceFunc.Parameters != null &&
!oaepParameters.PSourceFunc.Parameters.Value.Span.SequenceEqual(s_pSpecifiedDefaultParameters))
!oaepParameters.PSourceFunc.Parameters.Value.Span.SequenceEqual(pSpecifiedDefaultParameters))
{
exception = new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
return false;
Expand Down