From 55926d67c5a606890b7ed709ea5bc230bb2c91e7 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 29 Mar 2021 13:34:59 -0400 Subject: [PATCH] Remove some delegate/closures from X509Pal --- .../Pal.Windows/Native/Helpers.cs | 17 ++-- .../Pal.Windows/X509Pal.CustomExtensions.cs | 82 +++++++------------ .../Pal.Windows/X509Pal.PublicKey.cs | 32 ++------ 3 files changed, 47 insertions(+), 84 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/Native/Helpers.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/Native/Helpers.cs index 5a7b3ea8ef3ee7..afec7590d3e9b0 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/Native/Helpers.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/Native/Helpers.cs @@ -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 { @@ -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(void* pvDecodedObject, int cbDecodedObject); - public static void DecodeObject( + public static TResult DecodeObject( this byte[] encoded, CryptDecodeObjectStructType lpszStructType, - DecodedObjectReceiver receiver) + DecodedObjectReceiver receiver) { unsafe { @@ -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( this byte[] encoded, string lpszStructType, - DecodedObjectReceiver receiver) + DecodedObjectReceiver receiver) { unsafe { @@ -148,7 +149,7 @@ public static void DecodeObject( throw Marshal.GetLastWin32Error().ToCryptographicException(); } - receiver(decoded, cb); + return receiver(decoded, cb); } } diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/X509Pal.CustomExtensions.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/X509Pal.CustomExtensions.cs index 57883ba6d90916..f7f1c5a5ebb2aa 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/X509Pal.CustomExtensions.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/X509Pal.CustomExtensions.cs @@ -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) @@ -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; @@ -95,25 +93,16 @@ 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); + }); } } @@ -121,25 +110,16 @@ public void DecodeX509BasicConstraints2Extension(byte[] encoded, out bool certif { 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); + }); } } @@ -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; @@ -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 subjectKeyIdentifier) @@ -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(); + }); } } diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/X509Pal.PublicKey.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/X509Pal.PublicKey.cs index dd11ea04d08309..19bce7dfa75fa6 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/X509Pal.PublicKey.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/X509Pal.PublicKey.cs @@ -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)