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

Reduce the nullability bypasses in CryptoPool.Return #55782

Merged
merged 2 commits into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -198,8 +198,7 @@ internal static unsafe Pkcs8Response ImportEncryptedPkcs8PrivateKey(
}
finally
{
CryptographicOperations.ZeroMemory(decryptedSpan);
CryptoPool.Return(decrypted.Array!);
CryptoPool.Return(decrypted);
}
}
catch (AsnContentException e)
Expand Down Expand Up @@ -277,8 +276,7 @@ internal static unsafe Pkcs8Response ImportEncryptedPkcs8PrivateKey(
}
finally
{
CryptographicOperations.ZeroMemory(decryptedSpan);
CryptoPool.Return(decrypted.Array!, clearSize: 0);
CryptoPool.Return(decrypted);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,19 @@ private static AsnWriter WriteEncryptedPkcs8(
out string encryptionAlgorithmOid,
out bool isPkcs12);

byte[]? encryptedRent = null;
Span<byte> encryptedSpan = default;
AsnWriter? writer = null;

try
{
Debug.Assert(cipher.BlockSize <= 128, $"Encountered unexpected block size: {cipher.BlockSize}");
Span<byte> iv = stackalloc byte[cipher.BlockSize / 8];
Span<byte> salt = stackalloc byte[16];
Debug.Assert(cipher.BlockSize <= 128, $"Encountered unexpected block size: {cipher.BlockSize}");
Span<byte> iv = stackalloc byte[cipher.BlockSize / 8];
Span<byte> salt = stackalloc byte[16];

// We need at least one block size beyond the input data size.
encryptedRent = CryptoPool.Rent(
checked(pkcs8Writer.GetEncodedLength() + (cipher.BlockSize / 8)));
// We need at least one block size beyond the input data size.
byte[]? encryptedRent = CryptoPool.Rent(
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
checked(pkcs8Writer.GetEncodedLength() + (cipher.BlockSize / 8)));

try
{
RandomNumberGenerator.Fill(salt);

int written = PasswordBasedEncryption.Encrypt(
Expand Down Expand Up @@ -242,7 +241,7 @@ private static AsnWriter WriteEncryptedPkcs8(
finally
{
CryptographicOperations.ZeroMemory(encryptedSpan);
CryptoPool.Return(encryptedRent!, clearSize: 0);
CryptoPool.Return(encryptedRent, clearSize: 0);

cipher.Dispose();
}
Expand Down Expand Up @@ -348,8 +347,7 @@ internal static AsnWriter ReencryptPkcs8(
}
finally
{
CryptographicOperations.ZeroMemory(decrypted);
CryptoPool.Return(decrypted.Array!, clearSize: 0);
CryptoPool.Return(decrypted);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -385,8 +383,7 @@ internal static AsnWriter ReencryptPkcs8(
}
finally
{
CryptographicOperations.ZeroMemory(decrypted);
CryptoPool.Return(decrypted.Array!, clearSize: 0);
CryptoPool.Return(decrypted);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
SafeRsaHandle key = GetKey();

int rsaSize = Interop.AndroidCrypto.RsaSize(key);
byte[]? buf = null;
Span<byte> destination = default;
byte[]? buf = CryptoPool.Rent(rsaSize);
bartonjs marked this conversation as resolved.
Show resolved Hide resolved

try
{
buf = CryptoPool.Rent(rsaSize);
destination = new Span<byte>(buf, 0, rsaSize);

if (!TryDecrypt(key, data, destination, rsaPadding, oaepProcessor, out int bytesWritten))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
ValidatePadding(padding);
SafeEvpPKeyHandle key = GetKey();
int rsaSize = Interop.Crypto.EvpPKeySize(key);
byte[]? buf = null;
Span<byte> destination = default;
byte[]? buf = CryptoPool.Rent(rsaSize);
bartonjs marked this conversation as resolved.
Show resolved Hide resolved

try
{
buf = CryptoPool.Rent(rsaSize);
destination = new Span<byte>(buf, 0, rsaSize);

int bytesWritten = Decrypt(key, data, destination, padding);
Expand All @@ -101,7 +100,7 @@ public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
finally
{
CryptographicOperations.ZeroMemory(destination);
CryptoPool.Return(buf!, clearSize: 0);
CryptoPool.Return(buf, clearSize: 0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ public static Pkcs8PrivateKeyInfo DecryptAndDecode(
}
finally
{
CryptographicOperations.ZeroMemory(decryptedMemory.Span);
CryptoPool.Return(decrypted.Array!, clearSize: 0);
CryptoPool.Return(decrypted);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -229,8 +228,7 @@ public static Pkcs8PrivateKeyInfo DecryptAndDecode(
}
finally
{
CryptographicOperations.ZeroMemory(decryptedMemory.Span);
CryptoPool.Return(decrypted.Array!, clearSize: 0);
CryptoPool.Return(decrypted);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ private AsymmetricAlgorithm LoadKey(SafeBagAsn safeBag, ReadOnlySpan<char> passw
}
finally
{
CryptoPool.Return(decrypted.Array!, clearSize: decrypted.Count);
CryptoPool.Return(decrypted);
}
}

Expand Down