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

Remove some delegate/closures from X509Pal #50376

Merged
merged 1 commit into from
Mar 29, 2021
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 @@ -2,10 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Text;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;

namespace Internal.Cryptography.Pal.Native
{
Expand Down Expand Up @@ -73,11 +73,12 @@ public static byte[] ValueAsAscii(this Oid oid)
}

public unsafe delegate void DecodedObjectReceiver(void* pvDecodedObject, int cbDecodedObject);
public unsafe delegate TResult DecodedObjectReceiver<TResult>(void* pvDecodedObject, int cbDecodedObject);

public static void DecodeObject(
public static TResult DecodeObject<TResult>(
this byte[] encoded,
CryptDecodeObjectStructType lpszStructType,
DecodedObjectReceiver receiver)
DecodedObjectReceiver<TResult> receiver)
{
unsafe
{
Expand Down Expand Up @@ -109,14 +110,14 @@ public static void DecodeObject(
throw Marshal.GetLastWin32Error().ToCryptographicException();
}

receiver(decoded, cb);
return receiver(decoded, cb);
}
}

public static void DecodeObject(
public static TResult DecodeObject<TResult>(
this byte[] encoded,
string lpszStructType,
DecodedObjectReceiver receiver)
DecodedObjectReceiver<TResult> receiver)
{
unsafe
{
Expand Down Expand Up @@ -148,7 +149,7 @@ public static void DecodeObject(
throw Marshal.GetLastWin32Error().ToCryptographicException();
}

receiver(decoded, cb);
return receiver(decoded, cb);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ public void DecodeX509KeyUsageExtension(byte[] encoded, out X509KeyUsageFlags ke
{
unsafe
{
uint keyUsagesAsUint = 0;
encoded.DecodeObject(
uint keyUsagesAsUint = encoded.DecodeObject(
CryptDecodeObjectStructType.X509_KEY_USAGE,
delegate (void* pvDecoded, int cbDecoded)
static delegate (void* pvDecoded, int cbDecoded)
{
Debug.Assert(cbDecoded >= sizeof(CRYPT_BIT_BLOB));
CRYPT_BIT_BLOB* pBlob = (CRYPT_BIT_BLOB*)pvDecoded;
keyUsagesAsUint = 0;
byte* pbData = pBlob->pbData;

if (pbData != null)
Expand All @@ -58,13 +56,13 @@ public void DecodeX509KeyUsageExtension(byte[] encoded, out X509KeyUsageFlags ke
switch (pBlob->cbData)
{
case 1:
keyUsagesAsUint = *pbData;
break;
return *pbData;
case 2:
keyUsagesAsUint = *(ushort*)(pbData);
break;
return *(ushort*)(pbData);
}
}

return 0u;
}
);
keyUsages = (X509KeyUsageFlags)keyUsagesAsUint;
Expand Down Expand Up @@ -95,51 +93,33 @@ public void DecodeX509BasicConstraintsExtension(byte[] encoded, out bool certifi
{
unsafe
{
bool localCertificateAuthority = false;
bool localHasPathLengthConstraint = false;
int localPathLengthConstraint = 0;

encoded.DecodeObject(
(certificateAuthority, hasPathLengthConstraint, pathLengthConstraint) = encoded.DecodeObject(
CryptDecodeObjectStructType.X509_BASIC_CONSTRAINTS,
delegate (void* pvDecoded, int cbDecoded)
static delegate (void* pvDecoded, int cbDecoded)
{
Debug.Assert(cbDecoded >= sizeof(CERT_BASIC_CONSTRAINTS_INFO));
CERT_BASIC_CONSTRAINTS_INFO* pBasicConstraints = (CERT_BASIC_CONSTRAINTS_INFO*)pvDecoded;
localCertificateAuthority = (pBasicConstraints->SubjectType.pbData[0] & CERT_BASIC_CONSTRAINTS_INFO.CERT_CA_SUBJECT_FLAG) != 0;
localHasPathLengthConstraint = pBasicConstraints->fPathLenConstraint != 0;
localPathLengthConstraint = pBasicConstraints->dwPathLenConstraint;
}
);

certificateAuthority = localCertificateAuthority;
hasPathLengthConstraint = localHasPathLengthConstraint;
pathLengthConstraint = localPathLengthConstraint;
return ((pBasicConstraints->SubjectType.pbData[0] & CERT_BASIC_CONSTRAINTS_INFO.CERT_CA_SUBJECT_FLAG) != 0,
pBasicConstraints->fPathLenConstraint != 0,
pBasicConstraints->dwPathLenConstraint);
});
}
}

public void DecodeX509BasicConstraints2Extension(byte[] encoded, out bool certificateAuthority, out bool hasPathLengthConstraint, out int pathLengthConstraint)
{
unsafe
{
bool localCertificateAuthority = false;
bool localHasPathLengthConstraint = false;
int localPathLengthConstraint = 0;

encoded.DecodeObject(
(certificateAuthority, hasPathLengthConstraint, pathLengthConstraint) = encoded.DecodeObject(
CryptDecodeObjectStructType.X509_BASIC_CONSTRAINTS2,
delegate (void* pvDecoded, int cbDecoded)
static delegate (void* pvDecoded, int cbDecoded)
{
Debug.Assert(cbDecoded >= sizeof(CERT_BASIC_CONSTRAINTS2_INFO));
CERT_BASIC_CONSTRAINTS2_INFO* pBasicConstraints2 = (CERT_BASIC_CONSTRAINTS2_INFO*)pvDecoded;
localCertificateAuthority = pBasicConstraints2->fCA != 0;
localHasPathLengthConstraint = pBasicConstraints2->fPathLenConstraint != 0;
localPathLengthConstraint = pBasicConstraints2->dwPathLenConstraint;
}
);

certificateAuthority = localCertificateAuthority;
hasPathLengthConstraint = localHasPathLengthConstraint;
pathLengthConstraint = localPathLengthConstraint;
return (pBasicConstraints2->fCA != 0,
pBasicConstraints2->fPathLenConstraint != 0,
pBasicConstraints2->dwPathLenConstraint);
});
}
}

Expand All @@ -163,14 +143,14 @@ public byte[] EncodeX509EnhancedKeyUsageExtension(OidCollection usages)

public void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCollection usages)
{
OidCollection localUsages = new OidCollection();

unsafe
{
encoded.DecodeObject(
usages = encoded.DecodeObject(
CryptDecodeObjectStructType.X509_ENHANCED_KEY_USAGE,
delegate (void* pvDecoded, int cbDecoded)
static delegate (void* pvDecoded, int cbDecoded)
{
var localUsages = new OidCollection();

Debug.Assert(cbDecoded >= sizeof(CERT_ENHKEY_USAGE));
CERT_ENHKEY_USAGE* pEnhKeyUsage = (CERT_ENHKEY_USAGE*)pvDecoded;
int count = pEnhKeyUsage->cUsageIdentifier;
Expand All @@ -181,11 +161,10 @@ public void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCollectio
Oid oid = new Oid(oidValue);
localUsages.Add(oid);
}
}
);
}

usages = localUsages;
return localUsages;
});
}
}

public byte[] EncodeX509SubjectKeyIdentifierExtension(ReadOnlySpan<byte> subjectKeyIdentifier)
Expand All @@ -204,17 +183,14 @@ public void DecodeX509SubjectKeyIdentifierExtension(byte[] encoded, out byte[] s
{
unsafe
{
byte[] localSubjectKeyIdentifier = null!;
encoded.DecodeObject(
subjectKeyIdentifier = encoded.DecodeObject(
Oids.SubjectKeyIdentifier,
delegate (void* pvDecoded, int cbDecoded)
static delegate (void* pvDecoded, int cbDecoded)
{
Debug.Assert(cbDecoded >= sizeof(CRYPTOAPI_BLOB));
CRYPTOAPI_BLOB* pBlob = (CRYPTOAPI_BLOB*)pvDecoded;
localSubjectKeyIdentifier = pBlob->ToByteArray();
}
);
subjectKeyIdentifier = localSubjectKeyIdentifier;
return pBlob->ToByteArray();
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,46 +283,32 @@ private static byte[] ConstructDSSPublicKeyCspBlob(byte[] encodedKeyValue, byte[
{
unsafe
{
byte[]? decodedKeyValue = null;

encodedKeyValue.DecodeObject(
return encodedKeyValue.DecodeObject(
CryptDecodeObjectStructType.X509_DSS_PUBLICKEY,
delegate (void* pvDecoded, int cbDecoded)
static delegate (void* pvDecoded, int cbDecoded)
{
Debug.Assert(cbDecoded >= sizeof(CRYPTOAPI_BLOB));
CRYPTOAPI_BLOB* pBlob = (CRYPTOAPI_BLOB*)pvDecoded;
decodedKeyValue = pBlob->ToByteArray();
}
);

return decodedKeyValue;
return pBlob->ToByteArray();
});
}
}

private static void DecodeDssParameters(byte[] encodedParameters, out byte[] p, out byte[] q, out byte[] g)
{
byte[] pLocal = null!;
byte[] qLocal = null!;
byte[] gLocal = null!;

unsafe
{
encodedParameters.DecodeObject(
(p, q, g) = encodedParameters.DecodeObject(
CryptDecodeObjectStructType.X509_DSS_PARAMETERS,
delegate (void* pvDecoded, int cbDecoded)
{
Debug.Assert(cbDecoded >= sizeof(CERT_DSS_PARAMETERS));
CERT_DSS_PARAMETERS* pCertDssParameters = (CERT_DSS_PARAMETERS*)pvDecoded;
pLocal = pCertDssParameters->p.ToByteArray();
qLocal = pCertDssParameters->q.ToByteArray();
gLocal = pCertDssParameters->g.ToByteArray();
}
);
return (pCertDssParameters->p.ToByteArray(),
pCertDssParameters->q.ToByteArray(),
pCertDssParameters->g.ToByteArray());
});
}

p = pLocal;
q = qLocal;
g = gLocal;
}

private static bool HasExplicitParameters(SafeBCryptKeyHandle bcryptHandle)
Expand Down