Clean up usage of ArrayPool in the Cryptography assemblies
authorJeremy Barton <jbarton@microsoft.com>
Fri, 17 May 2019 13:28:17 +0000 (06:28 -0700)
committerGitHub <noreply@github.com>
Fri, 17 May 2019 13:28:17 +0000 (06:28 -0700)
commit254f31e756957e3f6d46439f6e3760ca267740f0
tree53870dc8012ff0c994f2c6fc07358e2c94df41e2
parent920127f8cce49b21991158014068eeb22eb4a977
Clean up usage of ArrayPool in the Cryptography assemblies

The primary motivator of this change is to prepare for moving cryptography from
the public, shared ArrayPool instance to a private pool (or pools), just as a defense
in depth strategy.  Since only the shared ArrayPool instance has GC cooperation
the change to a private pool is not happening at this time.

What this change does provide:
* Every ArrayPool.Rent was identified as being private to the assembly or shared
  out.  If the array gets shared out, it is not appropriate to use a private pool.
* CryptoPool clears returned arrays by default. Instead of an optional bool for
  clearing it has an optional length to clear (from index 0).
* Some rents were removed altogether, via a couple strategies:
  * Opportunistic stackalloc
  * Add a SpanAction-based AsnWriter.WriteBitString overload to avoid the
    pattern of rent-write-call-copy (instead do request-write-done).
  * Add AsnWriter.ValueEquals to prevent rent-encode-compare.
* At least one Rent with no Return was found and fixed.
* Fixed several Rent-growth strategies that could double-Return on exceptions.
* Changed Rfc2898DeriveBytes to just use the one field array instead of a
   lot of rent-write-copy-to-the-field.
* Moves some Return calls out of finally blocks in Async methods to avoid a
  Return-while-in-use path with Task composition when one Task gets aborted
  and another is still running.

Commit migrated from https://github.com/dotnet/corefx/commit/64609475281f2be05a0b9ca5b7bbdaf43b12dc55
58 files changed:
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Encode.cs
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.PooledCrypto.cs [new file with mode: 0644]
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnReader.BitString.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnReader.GeneralizedTime.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnReader.Integer.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnReader.OctetString.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnReader.Oid.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnReader.Text.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnReader.UtcTime.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnWriter.BitString.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnWriter.Oid.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnWriter.Text.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1Reader/AsnWriter.cs
src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.cs
src/libraries/Common/src/System/Security/Cryptography/CryptoPool.cs [new file with mode: 0644]
src/libraries/Common/src/System/Security/Cryptography/DSACng.SignVerify.cs
src/libraries/Common/src/System/Security/Cryptography/DSAOpenSsl.cs
src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.Derive.cs
src/libraries/Common/src/System/Security/Cryptography/ECDsaOpenSsl.cs
src/libraries/Common/src/System/Security/Cryptography/EccKeyFormatHelper.cs
src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs
src/libraries/Common/src/System/Security/Cryptography/PasswordBasedEncryption.cs
src/libraries/Common/src/System/Security/Cryptography/Pkcs12Kdf.cs
src/libraries/Common/src/System/Security/Cryptography/RSACng.EncryptDecrypt.cs
src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs
src/libraries/Common/src/System/Security/Cryptography/RSASecurityTransforms.cs
src/libraries/Common/src/System/Security/Cryptography/RsaPaddingProcessor.cs
src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/OpenSslCipher.cs
src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj
src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.cs
src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsa.cs
src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.cs
src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RandomNumberGenerator.cs
src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rfc2898DeriveBytes.cs
src/libraries/System.Security.Cryptography.Algorithms/tests/Rfc2898Tests.cs
src/libraries/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj
src/libraries/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj
src/libraries/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.csproj
src/libraries/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj
src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/AnyOS/ManagedPal.Asn.cs
src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/AnyOS/ManagedPal.Decrypt.cs
src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/AnyOS/ManagedPal.KeyTrans.cs
src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System.Security.Cryptography.Pkcs.csproj
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/CmsSignature.DSA.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/CmsSignature.ECDsa.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Pkcs12Builder.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Pkcs12SafeContents.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Pkcs8PrivateKeyInfo.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SignedCms.cs
src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj
src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/AsymmetricAlgorithm.cs
src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs
src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HashAlgorithm.cs
src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CrlCache.cs
src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509ChainProcessor.cs
src/libraries/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj