From: buyaa-n Date: Tue, 11 Feb 2020 06:04:54 +0000 (-0800) Subject: Annotate System.Security.Cryptography.Algorithms for nullable (#2375) X-Git-Tag: submit/tizen/20210909.063632~9867 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=07a5ee89b835a915ff3ac082ba3677fe39384a18;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Annotate System.Security.Cryptography.Algorithms for nullable (#2375) * Annotate System.Security.Cryptography.Algorithms for nullable --- diff --git a/src/libraries/Common/src/Internal/Cryptography/BasicSymmetricCipher.cs b/src/libraries/Common/src/Internal/Cryptography/BasicSymmetricCipher.cs index f4cd174..59bf4af 100644 --- a/src/libraries/Common/src/Internal/Cryptography/BasicSymmetricCipher.cs +++ b/src/libraries/Common/src/Internal/Cryptography/BasicSymmetricCipher.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Security.Cryptography; @@ -22,7 +23,7 @@ namespace Internal.Cryptography // internal abstract class BasicSymmetricCipher : IDisposable { - protected BasicSymmetricCipher(byte[] iv, int blockSizeInBytes) + protected BasicSymmetricCipher(byte[]? iv, int blockSizeInBytes) { IV = iv; BlockSizeInBytes = blockSizeInBytes; @@ -52,6 +53,6 @@ namespace Internal.Cryptography } } - protected byte[] IV { get; private set; } + protected byte[]? IV { get; private set; } } } diff --git a/src/libraries/Common/src/Internal/Cryptography/BasicSymmetricCipherBCrypt.cs b/src/libraries/Common/src/Internal/Cryptography/BasicSymmetricCipherBCrypt.cs index 47fd299..e8ee331 100644 --- a/src/libraries/Common/src/Internal/Cryptography/BasicSymmetricCipherBCrypt.cs +++ b/src/libraries/Common/src/Internal/Cryptography/BasicSymmetricCipherBCrypt.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Security.Cryptography; @@ -13,10 +14,10 @@ namespace Internal.Cryptography { private readonly bool _encrypting; private SafeKeyHandle _hKey; - private byte[] _currentIv; // CNG mutates this with the updated IV for the next stage on each Encrypt/Decrypt call. - // The base IV holds a copy of the original IV for Reset(), until it is cleared by Dispose(). + private byte[]? _currentIv; // CNG mutates this with the updated IV for the next stage on each Encrypt/Decrypt call. + // The base IV holds a copy of the original IV for Reset(), until it is cleared by Dispose(). - public BasicSymmetricCipherBCrypt(SafeAlgorithmHandle algorithm, CipherMode cipherMode, int blockSizeInBytes, byte[] key, bool ownsParentHandle, byte[] iv, bool encrypting) + public BasicSymmetricCipherBCrypt(SafeAlgorithmHandle algorithm, CipherMode cipherMode, int blockSizeInBytes, byte[] key, bool ownsParentHandle, byte[]? iv, bool encrypting) : base(cipherMode.GetCipherIv(iv), blockSizeInBytes) { Debug.Assert(algorithm != null); @@ -43,13 +44,13 @@ namespace Internal.Cryptography if (disposing) { SafeKeyHandle hKey = _hKey; - _hKey = null; + _hKey = null!; if (hKey != null) { hKey.Dispose(); } - byte[] currentIv = _currentIv; + byte[]? currentIv = _currentIv; _currentIv = null; if (currentIv != null) { @@ -115,7 +116,7 @@ namespace Internal.Cryptography { if (IV != null) { - Buffer.BlockCopy(IV, 0, _currentIv, 0, IV.Length); + Buffer.BlockCopy(IV, 0, _currentIv!, 0, IV.Length); } } } diff --git a/src/libraries/Common/src/Internal/Cryptography/HashProviderCng.cs b/src/libraries/Common/src/Internal/Cryptography/HashProviderCng.cs index ce4e1f5..4102f7f 100644 --- a/src/libraries/Common/src/Internal/Cryptography/HashProviderCng.cs +++ b/src/libraries/Common/src/Internal/Cryptography/HashProviderCng.cs @@ -9,6 +9,7 @@ using NTSTATUS = Interop.BCrypt.NTSTATUS; using BCryptOpenAlgorithmProviderFlags = Interop.BCrypt.BCryptOpenAlgorithmProviderFlags; using BCryptCreateHashFlags = Interop.BCrypt.BCryptCreateHashFlags; +#nullable enable namespace Internal.Cryptography { // @@ -21,7 +22,7 @@ namespace Internal.Cryptography // // - "key" activates MAC hashing if present. If null, this HashProvider performs a regular old hash. // - public HashProviderCng(string hashAlgId, byte[] key) : this(hashAlgId, key, isHmac: key != null) + public HashProviderCng(string hashAlgId, byte[]? key) : this(hashAlgId, key, isHmac: key != null) { } @@ -39,7 +40,7 @@ namespace Internal.Cryptography // Win7 won't set hHash, Win8+ will; and both will set _hHash. // So keep hHash trapped in this scope to prevent (mis-)use of it. { - SafeBCryptHashHandle hHash = null; + SafeBCryptHashHandle? hHash = null; NTSTATUS ntStatus = Interop.BCrypt.BCryptCreateHash(_hAlgorithm, out hHash, IntPtr.Zero, 0, key, key == null ? 0 : key.Length, BCryptCreateHashFlags.BCRYPT_HASH_REUSABLE_FLAG); if (ntStatus == NTSTATUS.STATUS_INVALID_PARAMETER) { @@ -62,6 +63,7 @@ namespace Internal.Cryptography { int cbSizeOfHashSize; int hashSize; + Debug.Assert(_hHash != null); NTSTATUS ntStatus = Interop.BCrypt.BCryptGetProperty(_hHash, Interop.BCrypt.BCryptPropertyStrings.BCRYPT_HASH_LENGTH, &hashSize, sizeof(int), out cbSizeOfHashSize, 0); if (ntStatus != NTSTATUS.STATUS_SUCCESS) throw Interop.BCrypt.CreateCryptographicException(ntStatus); @@ -71,6 +73,7 @@ namespace Internal.Cryptography public sealed override unsafe void AppendHashData(ReadOnlySpan source) { + Debug.Assert(_hHash != null); NTSTATUS ntStatus = Interop.BCrypt.BCryptHashData(_hHash, source, source.Length, 0); if (ntStatus != NTSTATUS.STATUS_SUCCESS) { @@ -95,6 +98,7 @@ namespace Internal.Cryptography return false; } + Debug.Assert(_hHash != null); NTSTATUS ntStatus = Interop.BCrypt.BCryptFinishHash(_hHash, destination, _hashSize, 0); if (ntStatus != NTSTATUS.STATUS_SUCCESS) { @@ -138,7 +142,7 @@ namespace Internal.Cryptography private void DestroyHash() { - SafeBCryptHashHandle hHash = _hHash; + SafeBCryptHashHandle? hHash = _hHash; _hHash = null; if (hHash != null) { @@ -149,8 +153,8 @@ namespace Internal.Cryptography } private readonly SafeBCryptAlgorithmHandle _hAlgorithm; - private SafeBCryptHashHandle _hHash; - private byte[] _key; + private SafeBCryptHashHandle? _hHash; + private byte[]? _key; private readonly bool _reusable; private readonly int _hashSize; diff --git a/src/libraries/Common/src/Internal/Cryptography/UniversalCryptoDecryptor.cs b/src/libraries/Common/src/Internal/Cryptography/UniversalCryptoDecryptor.cs index 5ca2be5..c7cdc14 100644 --- a/src/libraries/Common/src/Internal/Cryptography/UniversalCryptoDecryptor.cs +++ b/src/libraries/Common/src/Internal/Cryptography/UniversalCryptoDecryptor.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Security.Cryptography; @@ -77,7 +78,7 @@ namespace Internal.Cryptography // Otherwise the decryption buffer is just the input data. // - byte[] ciphertext = null; + byte[]? ciphertext = null; if (_heldoverCipher == null) { @@ -123,7 +124,7 @@ namespace Internal.Cryptography { if (disposing) { - byte[] heldoverCipher = _heldoverCipher; + byte[]? heldoverCipher = _heldoverCipher; _heldoverCipher = null; if (heldoverCipher != null) { @@ -248,6 +249,6 @@ namespace Internal.Cryptography // whether this is the final block that needs depadding. This block is held (in encrypted form) in _heldoverCipher. The next call to TransformBlock // or TransformFinalBlock must include the decryption of _heldoverCipher in the results. // - private byte[] _heldoverCipher; + private byte[]? _heldoverCipher; } } diff --git a/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFError.cs b/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFError.cs index af3b5cd..216b350 100644 --- a/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFError.cs +++ b/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFError.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -29,7 +30,7 @@ internal static partial class Interop } } - internal static string GetErrorDescription(SafeCFErrorHandle cfError) + internal static string? GetErrorDescription(SafeCFErrorHandle cfError) { Debug.Assert(cfError != null); diff --git a/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFString.cs b/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFString.cs index 17fb307..98010fa 100644 --- a/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFString.cs +++ b/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFString.cs @@ -43,7 +43,7 @@ internal static partial class Interop if (interiorPointer != IntPtr.Zero) { - return Marshal.PtrToStringUTF8(interiorPointer); + return Marshal.PtrToStringUTF8(interiorPointer)!; } SafeCFDataHandle cfData = CFStringCreateExternalRepresentation( diff --git a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.KeyAgree.cs b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.KeyAgree.cs index e213670..f585b821 100644 --- a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.KeyAgree.cs +++ b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.KeyAgree.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -20,7 +21,7 @@ internal static partial class Interop out SafeCFDataHandle cfDataOut, out SafeCFErrorHandle cfErrorOut); - internal static byte[] EcdhKeyAgree( + internal static byte[]? EcdhKeyAgree( SafeSecKeyRefHandle privateKey, SafeSecKeyRefHandle publicKey, Span opportunisticDestination, diff --git a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Keychain.cs b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Keychain.cs index c58c98d..7ed270a 100644 --- a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Keychain.cs +++ b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Keychain.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Collections.Generic; using System.Diagnostics; @@ -371,7 +372,7 @@ namespace System.Security.Cryptography.Apple lock (s_lookup) { - SafeTemporaryKeychainHandle temporaryHandle; + SafeTemporaryKeychainHandle? temporaryHandle; if (s_lookup.TryGetValue(keychain.DangerousGetHandle(), out temporaryHandle)) { @@ -393,7 +394,7 @@ namespace System.Security.Cryptography.Apple lock (s_lookup) { - SafeTemporaryKeychainHandle temporaryHandle; + SafeTemporaryKeychainHandle? temporaryHandle; if (s_lookup.TryGetValue(keychain.DangerousGetHandle(), out temporaryHandle)) { diff --git a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecErr.cs b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecErr.cs index 48154a2..0c6bb80 100644 --- a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecErr.cs +++ b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecErr.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; internal static partial class Interop @@ -10,7 +11,7 @@ internal static partial class Interop { internal static Exception CreateExceptionForOSStatus(int osStatus) { - string msg = GetSecErrorString(osStatus); + string? msg = GetSecErrorString(osStatus); if (msg == null) { diff --git a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecErrMessage.cs b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecErrMessage.cs index c8192f4..16d5bed 100644 --- a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecErrMessage.cs +++ b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecErrMessage.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; @@ -12,7 +13,7 @@ internal static partial class Interop [DllImport(Libraries.AppleCryptoNative)] private static extern SafeCFStringHandle AppleCryptoNative_SecCopyErrorMessageString(int osStatus); - internal static string GetSecErrorString(int osStatus) + internal static string? GetSecErrorString(int osStatus) { using (SafeCFStringHandle cfString = AppleCryptoNative_SecCopyErrorMessageString(osStatus)) { diff --git a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.Export.cs b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.Export.cs index 2bf6bbe..1a8b00c 100644 --- a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.Export.cs +++ b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.Export.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -17,14 +18,14 @@ internal static partial class Interop [DllImport(Libraries.AppleCryptoNative)] private static extern int AppleCryptoNative_SecKeyExport( - SafeSecKeyRefHandle key, + SafeSecKeyRefHandle? key, int exportPrivate, SafeCreateHandle cfExportPassphrase, out SafeCFDataHandle cfDataOut, out int pOSStatus); internal static SafeCFDataHandle SecKeyExportData( - SafeSecKeyRefHandle key, + SafeSecKeyRefHandle? key, bool exportPrivate, ReadOnlySpan password) { @@ -70,7 +71,7 @@ internal static partial class Interop } internal static byte[] SecKeyExport( - SafeSecKeyRefHandle key, + SafeSecKeyRefHandle? key, bool exportPrivate, string password) { diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs index 6faf7f8..02f5f13 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs @@ -2,7 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; @@ -45,7 +47,7 @@ internal static partial class Interop return new SafeBignumHandle(handle, true); } - internal static byte[] ExtractBignum(IntPtr bignum, int targetSize) + internal static byte[]? ExtractBignum(IntPtr bignum, int targetSize) { // Given that the only reference held to bignum is an IntPtr, create an unowned SafeHandle // to ensure that we don't destroy the key after extraction. @@ -55,7 +57,7 @@ internal static partial class Interop } } - private static unsafe byte[] ExtractBignum(SafeBignumHandle bignum, int targetSize) + private static unsafe byte[]? ExtractBignum(SafeBignumHandle? bignum, int targetSize) { if (bignum == null || bignum.IsInvalid) { diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Dsa.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Dsa.cs index d22c641..0949eff 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Dsa.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Dsa.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -125,10 +126,10 @@ internal static partial class Interop DSAParameters dsaParameters = new DSAParameters { - P = Crypto.ExtractBignum(p_bn, pgy_cb), - Q = Crypto.ExtractBignum(q_bn, qx_cb), - G = Crypto.ExtractBignum(g_bn, pgy_cb), - Y = Crypto.ExtractBignum(y_bn, pgy_cb), + P = Crypto.ExtractBignum(p_bn, pgy_cb)!, + Q = Crypto.ExtractBignum(q_bn, qx_cb)!, + G = Crypto.ExtractBignum(g_bn, pgy_cb)!, + Y = Crypto.ExtractBignum(y_bn, pgy_cb)!, }; if (includePrivateParameters) @@ -167,7 +168,7 @@ internal static partial class Interop int gLength, byte[] y, int yLength, - byte[] x, + byte[]? x, int xLength); } } diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ERR.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ERR.cs index 2d945c7..a98241c 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ERR.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ERR.cs @@ -2,12 +2,12 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Security.Cryptography; -#nullable enable internal static partial class Interop { internal static partial class Crypto diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.ImportExport.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.ImportExport.cs index 5780184..85c256d 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.ImportExport.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.ImportExport.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics; using System.Runtime.InteropServices; using System.Security.Cryptography; @@ -18,13 +19,13 @@ internal static partial class Interop string oid, byte[] qx, int qxLength, byte[] qy, int qyLength, - byte[] d, int dLength); + byte[]? d, int dLength); internal static SafeEcKeyHandle EcKeyCreateByKeyParameters( string oid, byte[] qx, int qxLength, byte[] qy, int qyLength, - byte[] d, int dLength) + byte[]? d, int dLength) { SafeEcKeyHandle key; int rc = EcKeyCreateByKeyParameters(out key, oid, qx, qxLength, qy, qyLength, d, dLength); @@ -41,28 +42,28 @@ internal static partial class Interop [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcKeyCreateByExplicitParameters")] internal static extern SafeEcKeyHandle EcKeyCreateByExplicitParameters( ECCurve.ECCurveType curveType, - byte[] qx, int qxLength, - byte[] qy, int qyLength, - byte[] d, int dLength, + byte[]? qx, int qxLength, + byte[]? qy, int qyLength, + byte[]? d, int dLength, byte[] p, int pLength, byte[] a, int aLength, byte[] b, int bLength, byte[] gx, int gxLength, byte[] gy, int gyLength, byte[] order, int nLength, - byte[] cofactor, int cofactorLength, - byte[] seed, int seedLength); + byte[]? cofactor, int cofactorLength, + byte[]? seed, int seedLength); internal static SafeEcKeyHandle EcKeyCreateByExplicitCurve(ECCurve curve) { byte[] p; if (curve.IsPrime) { - p = curve.Prime; + p = curve.Prime!; } else if (curve.IsCharacteristic2) { - p = curve.Polynomial; + p = curve.Polynomial!; } else { @@ -75,12 +76,12 @@ internal static partial class Interop null, 0, null, 0, p, p.Length, - curve.A, curve.A.Length, - curve.B, curve.B.Length, - curve.G.X, curve.G.X.Length, - curve.G.Y, curve.G.Y.Length, - curve.Order, curve.Order.Length, - curve.Cofactor, curve.Cofactor.Length, + curve.A!, curve.A!.Length, + curve.B!, curve.B!.Length, + curve.G.X!, curve.G.X!.Length, + curve.G.Y!, curve.G.Y!.Length, + curve.Order!, curve.Order!.Length, + curve.Cofactor, curve.Cofactor!.Length, curve.Seed, curve.Seed == null ? 0 : curve.Seed.Length); if (key == null || key.IsInvalid) @@ -264,22 +265,22 @@ internal static partial class Interop var curve = parameters.Curve; curve.CurveType = curveType; - curve.A = Crypto.ExtractBignum(a_bn, cbFieldLength); - curve.B = Crypto.ExtractBignum(b_bn, cbFieldLength); + curve.A = Crypto.ExtractBignum(a_bn, cbFieldLength)!; + curve.B = Crypto.ExtractBignum(b_bn, cbFieldLength)!; curve.G = new ECPoint { X = Crypto.ExtractBignum(gx_bn, cbFieldLength), Y = Crypto.ExtractBignum(gy_bn, cbFieldLength) }; - curve.Order = Crypto.ExtractBignum(order_bn, cbSubgroupOrder); + curve.Order = Crypto.ExtractBignum(order_bn, cbSubgroupOrder)!; if (curveType == ECCurve.ECCurveType.Characteristic2) { - curve.Polynomial = Crypto.ExtractBignum(p_bn, pFieldLength); + curve.Polynomial = Crypto.ExtractBignum(p_bn, pFieldLength)!; } else { - curve.Prime = Crypto.ExtractBignum(p_bn, pFieldLength); + curve.Prime = Crypto.ExtractBignum(p_bn, pFieldLength)!; } // Optional parameters diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcKey.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcKey.cs index b9e7c91..bf78e9c 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcKey.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcKey.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using Microsoft.Win32.SafeHandles; using System; using System.Diagnostics; @@ -13,7 +14,7 @@ internal static partial class Interop { [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcKeyCreateByOid")] private static extern SafeEcKeyHandle CryptoNative_EcKeyCreateByOid(string oid); - internal static SafeEcKeyHandle EcKeyCreateByOid(string oid) + internal static SafeEcKeyHandle? EcKeyCreateByOid(string oid) { SafeEcKeyHandle handle = CryptoNative_EcKeyCreateByOid(oid); if (handle == null || handle.IsInvalid) diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Rsa.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Rsa.cs index bbf39bf..425a493 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Rsa.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Rsa.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -153,8 +154,8 @@ internal static partial class Interop RSAParameters rsaParameters = new RSAParameters { - Modulus = Crypto.ExtractBignum(n, modulusSize), - Exponent = Crypto.ExtractBignum(e, 0), + Modulus = Crypto.ExtractBignum(n, modulusSize)!, + Exponent = Crypto.ExtractBignum(e, 0)!, }; if (includePrivateParameters) @@ -193,21 +194,21 @@ internal static partial class Interop [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool SetRsaParameters( SafeRsaHandle key, - byte[] n, + byte[]? n, int nLength, - byte[] e, + byte[]? e, int eLength, - byte[] d, + byte[]? d, int dLength, - byte[] p, + byte[]? p, int pLength, - byte[] dmp1, + byte[]? dmp1, int dmp1Length, - byte[] q, + byte[]? q, int qLength, - byte[] dmq1, + byte[]? dmq1, int dmq1Length, - byte[] iqmp, + byte[]? iqmp, int iqmpLength); internal enum RsaPadding : int diff --git a/src/libraries/Common/src/Interop/Windows/BCrypt/Cng.cs b/src/libraries/Common/src/Interop/Windows/BCrypt/Cng.cs index df8835d..bcb5bae 100644 --- a/src/libraries/Common/src/Interop/Windows/BCrypt/Cng.cs +++ b/src/libraries/Common/src/Interop/Windows/BCrypt/Cng.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Text; using System.Diagnostics; @@ -12,7 +13,6 @@ using static Interop; using static Interop.BCrypt; using Microsoft.Win32.SafeHandles; -#nullable enable namespace Internal.NativeCrypto { internal static partial class BCryptNative @@ -69,7 +69,7 @@ namespace Internal.NativeCrypto public const string BCRYPT_CHAIN_MODE_GCM = "ChainingModeGCM"; public const string BCRYPT_CHAIN_MODE_CCM = "ChainingModeCCM"; - public static SafeAlgorithmHandle BCryptOpenAlgorithmProvider(string pszAlgId, string pszImplementation, OpenAlgorithmProviderFlags dwFlags) + public static SafeAlgorithmHandle BCryptOpenAlgorithmProvider(string pszAlgId, string? pszImplementation, OpenAlgorithmProviderFlags dwFlags) { SafeAlgorithmHandle hAlgorithm; NTSTATUS ntStatus = Interop.BCryptOpenAlgorithmProvider(out hAlgorithm, pszAlgId, pszImplementation, (int)dwFlags); @@ -111,7 +111,7 @@ namespace Internal.NativeCrypto internal static class Interop { [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] - public static extern NTSTATUS BCryptOpenAlgorithmProvider(out SafeAlgorithmHandle phAlgorithm, string pszAlgId, string pszImplementation, int dwFlags); + public static extern NTSTATUS BCryptOpenAlgorithmProvider(out SafeAlgorithmHandle phAlgorithm, string pszAlgId, string? pszImplementation, int dwFlags); [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] public static extern unsafe NTSTATUS BCryptSetProperty(SafeAlgorithmHandle hObject, string pszProperty, string pbInput, int cbInput, int dwFlags); diff --git a/src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptEncryptDecrypt.cs b/src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptEncryptDecrypt.cs index 572fcce..b377d1f 100644 --- a/src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptEncryptDecrypt.cs +++ b/src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptEncryptDecrypt.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -13,7 +14,7 @@ internal static partial class Interop internal static partial class BCrypt { // Note: input and output are allowed to be the same buffer. BCryptEncrypt will correctly do the encryption in place according to CNG documentation. - internal static int BCryptEncrypt(SafeKeyHandle hKey, byte[] input, int inputOffset, int inputCount, byte[] iv, byte[] output, int outputOffset, int outputCount) + internal static int BCryptEncrypt(SafeKeyHandle hKey, byte[] input, int inputOffset, int inputCount, byte[]? iv, byte[] output, int outputOffset, int outputCount) { Debug.Assert(input != null); Debug.Assert(inputOffset >= 0); @@ -41,7 +42,7 @@ internal static partial class Interop } // Note: input and output are allowed to be the same buffer. BCryptDecrypt will correctly do the decryption in place according to CNG documentation. - internal static int BCryptDecrypt(SafeKeyHandle hKey, byte[] input, int inputOffset, int inputCount, byte[] iv, byte[] output, int outputOffset, int outputCount) + internal static int BCryptDecrypt(SafeKeyHandle hKey, byte[] input, int inputOffset, int inputCount, byte[]? iv, byte[] output, int outputOffset, int outputCount) { Debug.Assert(input != null); Debug.Assert(inputOffset >= 0); @@ -69,9 +70,9 @@ internal static partial class Interop } [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] - public static extern unsafe NTSTATUS BCryptEncrypt(SafeKeyHandle hKey, byte* pbInput, int cbInput, IntPtr paddingInfo, [In, Out] byte[] pbIV, int cbIV, byte* pbOutput, int cbOutput, out int cbResult, int dwFlags); + public static extern unsafe NTSTATUS BCryptEncrypt(SafeKeyHandle hKey, byte* pbInput, int cbInput, IntPtr paddingInfo, [In, Out] byte[]? pbIV, int cbIV, byte* pbOutput, int cbOutput, out int cbResult, int dwFlags); [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] - public static extern unsafe NTSTATUS BCryptDecrypt(SafeKeyHandle hKey, byte* pbInput, int cbInput, IntPtr paddingInfo, [In, Out] byte[] pbIV, int cbIV, byte* pbOutput, int cbOutput, out int cbResult, int dwFlags); + public static extern unsafe NTSTATUS BCryptDecrypt(SafeKeyHandle hKey, byte* pbInput, int cbInput, IntPtr paddingInfo, [In, Out] byte[]? pbIV, int cbIV, byte* pbOutput, int cbOutput, out int cbResult, int dwFlags); } } diff --git a/src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptOpenAlgorithmProvider.cs b/src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptOpenAlgorithmProvider.cs index 829b6e3..1d1372b 100644 --- a/src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptOpenAlgorithmProvider.cs +++ b/src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptOpenAlgorithmProvider.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -13,7 +14,7 @@ internal partial class Interop internal partial class BCrypt { [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] - internal static extern NTSTATUS BCryptOpenAlgorithmProvider(out SafeBCryptAlgorithmHandle phAlgorithm, string pszAlgId, string pszImplementation, BCryptOpenAlgorithmProviderFlags dwFlags); + internal static extern NTSTATUS BCryptOpenAlgorithmProvider(out SafeBCryptAlgorithmHandle phAlgorithm, string pszAlgId, string? pszImplementation, BCryptOpenAlgorithmProviderFlags dwFlags); [Flags] internal enum BCryptOpenAlgorithmProviderFlags : int diff --git a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Keys.cs b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Keys.cs index ee4f09c..212bfde 100644 --- a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Keys.cs +++ b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Keys.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Runtime.InteropServices; using System.Security.Cryptography; @@ -24,7 +25,7 @@ internal static partial class Interop internal static extern ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, [Out] out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags); [DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)] - internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, [Out] byte[] pbOutput, int cbOutput, [Out] out int pcbResult, int dwFlags); + internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, [Out] byte[]? pbOutput, int cbOutput, [Out] out int pcbResult, int dwFlags); [DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)] internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, ref byte pbOutput, int cbOutput, [Out] out int pcbResult, int dwFlags); @@ -36,7 +37,7 @@ internal static partial class Interop internal static extern ErrorCode NCryptDeleteKey(SafeNCryptKeyHandle hKey, int dwFlags); [DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)] - internal static extern ErrorCode NCryptCreatePersistedKey(SafeNCryptProviderHandle hProvider, out SafeNCryptKeyHandle phKey, string pszAlgId, string pszKeyName, int dwLegacyKeySpec, CngKeyCreationOptions dwFlags); + internal static extern ErrorCode NCryptCreatePersistedKey(SafeNCryptProviderHandle hProvider, out SafeNCryptKeyHandle phKey, string pszAlgId, string? pszKeyName, int dwLegacyKeySpec, CngKeyCreationOptions dwFlags); [DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)] internal static extern ErrorCode NCryptFinalizeKey(SafeNCryptKeyHandle hKey, int dwFlags); diff --git a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.NCryptDeriveKeyMaterial.cs b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.NCryptDeriveKeyMaterial.cs index 1aebafb..c8468d5 100644 --- a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.NCryptDeriveKeyMaterial.cs +++ b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.NCryptDeriveKeyMaterial.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Runtime.InteropServices; using System.Security.Cryptography; @@ -21,7 +22,7 @@ internal static partial class Interop SafeNCryptSecretHandle hSharedSecret, string pwszKDF, [In] ref NCryptBufferDesc pParameterList, - [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbDerivedKey, + [Out, MarshalAs(UnmanagedType.LPArray)] byte[]? pbDerivedKey, int cbDerivedKey, [Out] out int pcbResult, SecretAgreementFlags dwFlags); @@ -34,9 +35,9 @@ internal static partial class Interop SafeNCryptSecretHandle secretAgreement, string kdf, string hashAlgorithm, - byte[] hmacKey, - byte[] secretPrepend, - byte[] secretAppend, + byte[]? hmacKey, + byte[]? secretPrepend, + byte[]? secretAppend, SecretAgreementFlags flags) { // First marshal the hash algoritm @@ -68,7 +69,7 @@ internal static partial class Interop if (pHmacKey != null) { NCryptBuffer hmacKeyBuffer = default; - hmacKeyBuffer.cbBuffer = hmacKey.Length; + hmacKeyBuffer.cbBuffer = hmacKey!.Length; hmacKeyBuffer.BufferType = BufferType.KdfHmacKey; hmacKeyBuffer.pvBuffer = new IntPtr(pHmacKey); @@ -79,7 +80,7 @@ internal static partial class Interop if (pSecretPrepend != null) { NCryptBuffer secretPrependBuffer = default; - secretPrependBuffer.cbBuffer = secretPrepend.Length; + secretPrependBuffer.cbBuffer = secretPrepend!.Length; secretPrependBuffer.BufferType = BufferType.KdfSecretPrepend; secretPrependBuffer.pvBuffer = new IntPtr(pSecretPrepend); @@ -90,7 +91,7 @@ internal static partial class Interop if (pSecretAppend != null) { NCryptBuffer secretAppendBuffer = default; - secretAppendBuffer.cbBuffer = secretAppend.Length; + secretAppendBuffer.cbBuffer = secretAppend!.Length; secretAppendBuffer.BufferType = BufferType.KdfSecretAppend; secretAppendBuffer.pvBuffer = new IntPtr(pSecretAppend); @@ -175,8 +176,8 @@ internal static partial class Interop internal static byte[] DeriveKeyMaterialHash( SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, - byte[] secretPrepend, - byte[] secretAppend, + byte[]? secretPrepend, + byte[]? secretAppend, SecretAgreementFlags flags) { return DeriveKeyMaterial( @@ -195,9 +196,9 @@ internal static partial class Interop internal static byte[] DeriveKeyMaterialHmac( SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, - byte[] hmacKey, - byte[] secretPrepend, - byte[] secretAppend, + byte[]? hmacKey, + byte[]? secretPrepend, + byte[]? secretAppend, SecretAgreementFlags flags) { return DeriveKeyMaterial( diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs index eea60ac..14edf5a9 100644 --- a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs +++ b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -13,7 +14,7 @@ namespace Microsoft.Win32.SafeHandles /// Specifies the type of SafeHandle. internal static class SafeHandleCache where T : SafeHandle { - private static T s_invalidHandle; + private static T? s_invalidHandle; /// /// Gets a cached, invalid handle. As the instance is cached, it should either never be Disposed @@ -22,7 +23,7 @@ namespace Microsoft.Win32.SafeHandles /// internal static T GetInvalidHandle(Func invalidHandleFactory) { - T currentHandle = Volatile.Read(ref s_invalidHandle); + T? currentHandle = Volatile.Read(ref s_invalidHandle); if (currentHandle == null) { T newHandle = invalidHandleFactory(); diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeInteriorHandle.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeInteriorHandle.cs index c1e14c3..dcc03dc 100644 --- a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeInteriorHandle.cs +++ b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeInteriorHandle.cs @@ -2,11 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Diagnostics; using System.Runtime.InteropServices; -#nullable enable namespace Microsoft.Win32.SafeHandles { internal abstract class SafeInteriorHandle : SafeHandle diff --git a/src/libraries/Common/src/System/Security/Cryptography/Asn1/AlgorithmIdentifierAsn.manual.cs b/src/libraries/Common/src/System/Security/Cryptography/Asn1/AlgorithmIdentifierAsn.manual.cs index f350387..7235166 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Asn1/AlgorithmIdentifierAsn.manual.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Asn1/AlgorithmIdentifierAsn.manual.cs @@ -30,7 +30,7 @@ namespace System.Security.Cryptography.Asn1 return true; } - return Parameters.Value.Span.SequenceEqual(other.Parameters.Value.Span); + return Parameters!.Value.Span.SequenceEqual(other.Parameters!.Value.Span); } internal bool HasNullEquivalentParameters() diff --git a/src/libraries/Common/src/System/Security/Cryptography/Asn1/ECDomainParameters.xml.cs b/src/libraries/Common/src/System/Security/Cryptography/Asn1/ECDomainParameters.xml.cs index c762729..4c89970 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Asn1/ECDomainParameters.xml.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Asn1/ECDomainParameters.xml.cs @@ -2,13 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable #pragma warning disable SA1028 // ignore whitespace warnings for generated code using System; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Cryptography.Asn1; -#nullable enable namespace System.Security.Cryptography.Asn1 { [StructLayout(LayoutKind.Sequential)] diff --git a/src/libraries/Common/src/System/Security/Cryptography/Asn1/Pbkdf2SaltChoice.xml.cs b/src/libraries/Common/src/System/Security/Cryptography/Asn1/Pbkdf2SaltChoice.xml.cs index 14b5449..0ff367a 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Asn1/Pbkdf2SaltChoice.xml.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Asn1/Pbkdf2SaltChoice.xml.cs @@ -2,13 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable #pragma warning disable SA1028 // ignore whitespace warnings for generated code using System; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Cryptography.Asn1; -#nullable enable namespace System.Security.Cryptography.Asn1 { [StructLayout(LayoutKind.Sequential)] diff --git a/src/libraries/Common/src/System/Security/Cryptography/Asn1/asn.xslt b/src/libraries/Common/src/System/Security/Cryptography/Asn1/asn.xslt index 9313313..a182bd6 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Asn1/asn.xslt +++ b/src/libraries/Common/src/System/Security/Cryptography/Asn1/asn.xslt @@ -127,6 +127,7 @@ namespace // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable #pragma warning disable SA1028 // ignore whitespace warnings for generated code using System; using System.Collections.Generic; @@ -134,7 +135,6 @@ using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Cryptography.Asn1; -#nullable enable namespace { [StructLayout(LayoutKind.Sequential)] diff --git a/src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.cs b/src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.cs index 6316db7..29c41cb 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.cs @@ -151,7 +151,7 @@ namespace System.Security.Cryptography finally { CryptographicOperations.ZeroMemory(decryptedSpan); - CryptoPool.Return(decrypted.Array); + CryptoPool.Return(decrypted.Array!); } } } @@ -203,7 +203,7 @@ namespace System.Security.Cryptography finally { CryptographicOperations.ZeroMemory(decryptedSpan); - CryptoPool.Return(decrypted.Array, clearSize: 0); + CryptoPool.Return(decrypted.Array!, clearSize: 0); } } } diff --git a/src/libraries/Common/src/System/Security/Cryptography/DSACng.ImportExport.cs b/src/libraries/Common/src/System/Security/Cryptography/DSACng.ImportExport.cs index 3f7a4ac..1d94246 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/DSACng.ImportExport.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/DSACng.ImportExport.cs @@ -43,7 +43,7 @@ namespace System.Security.Cryptography if (parameters.G.Length != keySizeInBytes || parameters.Y.Length != keySizeInBytes) throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedPGY); - if (hasPrivateKey && parameters.X.Length != parameters.Q.Length) + if (hasPrivateKey && parameters.X!.Length != parameters.Q.Length) throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedQX); byte[] blob; @@ -220,19 +220,19 @@ namespace System.Security.Cryptography } // The Q length is hardcoded into BCRYPT_DSA_KEY_BLOB, so check it now we can give a nicer error message. - if (parameters.Q.Length != Sha1HashOutputSize) + if (parameters.Q!.Length != Sha1HashOutputSize) throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_QRestriction_ShortKey); Interop.BCrypt.Emit(blob, ref offset, parameters.Q); Debug.Assert(offset == sizeof(BCRYPT_DSA_KEY_BLOB), $"Expected offset = sizeof(BCRYPT_DSA_KEY_BLOB), got {offset} != {sizeof(BCRYPT_DSA_KEY_BLOB)}"); - Interop.BCrypt.Emit(blob, ref offset, parameters.P); - Interop.BCrypt.Emit(blob, ref offset, parameters.G); - Interop.BCrypt.Emit(blob, ref offset, parameters.Y); + Interop.BCrypt.Emit(blob, ref offset, parameters.P!); + Interop.BCrypt.Emit(blob, ref offset, parameters.G!); + Interop.BCrypt.Emit(blob, ref offset, parameters.Y!); if (includePrivate) { - Interop.BCrypt.Emit(blob, ref offset, parameters.X); + Interop.BCrypt.Emit(blob, ref offset, parameters.X!); } Debug.Assert(offset == blobSize, $"Expected offset = blobSize, got {offset} != {blobSize}"); @@ -256,12 +256,12 @@ namespace System.Security.Cryptography { int blobSize = sizeof(BCRYPT_DSA_KEY_BLOB_V2) + - (parameters.Seed == null ? parameters.Q.Length : parameters.Seed.Length) + // Use Q size if Seed is not present - parameters.Q.Length + - parameters.P.Length + - parameters.G.Length + - parameters.Y.Length + - (includePrivateParameters ? parameters.X.Length : 0); + (parameters.Seed == null ? parameters.Q!.Length : parameters.Seed.Length) + // Use Q size if Seed is not present + parameters.Q!.Length + + parameters.P!.Length + + parameters.G!.Length + + parameters.Y!.Length + + (includePrivateParameters ? parameters.X!.Length : 0); blob = new byte[blobSize]; fixed (byte* pDsaBlob = &blob[0]) @@ -317,7 +317,7 @@ namespace System.Security.Cryptography if (includePrivateParameters) { - Interop.BCrypt.Emit(blob, ref offset, parameters.X); + Interop.BCrypt.Emit(blob, ref offset, parameters.X!); } Debug.Assert(offset == blobSize, $"Expected offset = blobSize, got {offset} != {blobSize}"); diff --git a/src/libraries/Common/src/System/Security/Cryptography/DSACng.cs b/src/libraries/Common/src/System/Security/Cryptography/DSACng.cs index f9e716a..b92deb1 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/DSACng.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/DSACng.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using Internal.Cryptography; using System.Diagnostics; using System.IO; @@ -45,7 +46,7 @@ namespace System.Security.Cryptography } public override string SignatureAlgorithm => "DSA"; - public override string KeyExchangeAlgorithm => null; + public override string? KeyExchangeAlgorithm => null; // Need to override since base methods throw a "override me" exception: makes SignData/VerifyData function. protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) => diff --git a/src/libraries/Common/src/System/Security/Cryptography/DSAKeyFormatHelper.cs b/src/libraries/Common/src/System/Security/Cryptography/DSAKeyFormatHelper.cs index 12a128e..7de0284 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/DSAKeyFormatHelper.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/DSAKeyFormatHelper.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; using System.Numerics; @@ -183,7 +184,7 @@ namespace System.Security.Cryptography writer.PopSequence(); } - private static void WriteKeyComponent(AsnWriter writer, byte[] component, bool bitString) + private static void WriteKeyComponent(AsnWriter writer, byte[]? component, bool bitString) { using (AsnWriter inner = new AsnWriter(AsnEncodingRules.DER)) { diff --git a/src/libraries/Common/src/System/Security/Cryptography/DSAOpenSsl.cs b/src/libraries/Common/src/System/Security/Cryptography/DSAOpenSsl.cs index 90bad69..417ebb4 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/DSAOpenSsl.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/DSAOpenSsl.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics; using System.IO; using Internal.Cryptography; @@ -103,7 +104,7 @@ namespace System.Security.Cryptography if (parameters.G.Length != keySize || parameters.Y.Length != keySize) throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedPGY); - if (hasPrivateKey && parameters.X.Length != parameters.Q.Length) + if (hasPrivateKey && parameters.X!.Length != parameters.Q.Length) throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedQX); ThrowIfDisposed(); @@ -146,7 +147,7 @@ namespace System.Security.Cryptography if (disposing) { FreeKey(); - _key = null; + _key = null!; } base.Dispose(disposing); diff --git a/src/libraries/Common/src/System/Security/Cryptography/DSASecurityTransforms.cs b/src/libraries/Common/src/System/Security/Cryptography/DSASecurityTransforms.cs index 92177fa..6f4e111 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/DSASecurityTransforms.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/DSASecurityTransforms.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; using System.IO; @@ -26,7 +27,7 @@ namespace System.Security.Cryptography { public sealed partial class DSASecurityTransforms : DSA { - private SecKeyPair _keys; + private SecKeyPair? _keys; private bool _disposed; public DSASecurityTransforms() @@ -144,7 +145,7 @@ namespace System.Security.Cryptography if (parameters.G.Length != keySize || parameters.Y.Length != keySize) throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedPGY); - if (hasPrivateKey && parameters.X.Length != parameters.Q.Length) + if (hasPrivateKey && parameters.X!.Length != parameters.Q.Length) throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedQX); if (!(8 * parameters.P.Length).IsLegalSize(LegalKeySizes)) @@ -353,7 +354,7 @@ namespace System.Security.Cryptography { ThrowIfDisposed(); - SecKeyPair current = _keys; + SecKeyPair? current = _keys; if (current != null) { @@ -372,7 +373,7 @@ namespace System.Security.Cryptography { ThrowIfDisposed(); - SecKeyPair current = _keys; + SecKeyPair? current = _keys; _keys = newKeyPair; current?.Dispose(); diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECCng.HashAlgorithm.cs b/src/libraries/Common/src/System/Security/Cryptography/ECCng.HashAlgorithm.cs index 1ab0602..7c58da4 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECCng.HashAlgorithm.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECCng.HashAlgorithm.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics; using Internal.NativeCrypto; using static Interop.Crypt32; @@ -15,7 +16,7 @@ namespace System.Security.Cryptography /// internal static Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM GetHashAlgorithmId(HashAlgorithmName? name) { - if (name.HasValue == false || string.IsNullOrEmpty(name.Value.Name)) + if (name.HasValue == false || string.IsNullOrEmpty(name!.Value.Name)) { return Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM.BCRYPT_NO_CURVE_GENERATION_ALG_ID; } @@ -52,7 +53,7 @@ namespace System.Security.Cryptography /// /// Is the curve named, or once of the special nist curves /// - internal static bool IsECNamedCurve(string algorithm) + internal static bool IsECNamedCurve(string? algorithm) { return (algorithm == BCryptNative.AlgorithmName.ECDH || algorithm == BCryptNative.AlgorithmName.ECDsa); @@ -61,7 +62,7 @@ namespace System.Security.Cryptography /// /// Maps algorithm to curve name accounting for the special nist curves /// - internal static string SpecialNistAlgorithmToCurveName(string algorithm, out string oidValue) + internal static string SpecialNistAlgorithmToCurveName(string? algorithm, out string oidValue) { switch (algorithm) { diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.cs b/src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.cs index f1e98f9..482f598 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using Internal.Cryptography; using Microsoft.Win32.SafeHandles; using System.Diagnostics; @@ -34,11 +35,11 @@ namespace System.Security.Cryptography // byte[cbKey] D int blobSize = sizeof(BCRYPT_ECCKEY_BLOB) + - parameters.Q.X.Length + - parameters.Q.Y.Length; + parameters.Q.X!.Length + + parameters.Q.Y!.Length; if (includePrivateParameters) { - blobSize += parameters.D.Length; + blobSize += parameters.D!.Length; } blob = new byte[blobSize]; @@ -57,7 +58,7 @@ namespace System.Security.Cryptography Interop.BCrypt.Emit(blob, ref offset, parameters.Q.Y); if (includePrivateParameters) { - Interop.BCrypt.Emit(blob, ref offset, parameters.D); + Interop.BCrypt.Emit(blob, ref offset, parameters.D!); } // We better have computed the right allocation size above! @@ -92,20 +93,20 @@ namespace System.Security.Cryptography // byte[cbSubgroupOrder] D int blobSize = sizeof(BCRYPT_ECCFULLKEY_BLOB) + - curve.Prime.Length + - curve.A.Length + - curve.B.Length + - curve.G.X.Length + - curve.G.Y.Length + - curve.Order.Length + - curve.Cofactor.Length + + curve.Prime!.Length + + curve.A!.Length + + curve.B!.Length + + curve.G.X!.Length + + curve.G.Y!.Length + + curve.Order!.Length + + curve.Cofactor!.Length + (curve.Seed == null ? 0 : curve.Seed.Length) + - parameters.Q.X.Length + - parameters.Q.Y.Length; + parameters.Q.X!.Length + + parameters.Q.Y!.Length; if (includePrivateParameters) { - blobSize += parameters.D.Length; + blobSize += parameters.D!.Length; } blob = new byte[blobSize]; @@ -141,7 +142,7 @@ namespace System.Security.Cryptography Interop.BCrypt.Emit(blob, ref offset, parameters.Q.Y); if (includePrivateParameters) { - Interop.BCrypt.Emit(blob, ref offset, parameters.D); + Interop.BCrypt.Emit(blob, ref offset, parameters.D!); } // We better have computed the right allocation size above! @@ -278,13 +279,13 @@ namespace System.Security.Cryptography // byte[cbSeed] Seed int blobSize = sizeof(BCRYPT_ECC_PARAMETER_HEADER) + - curve.Prime.Length + - curve.A.Length + - curve.B.Length + - curve.G.X.Length + - curve.G.Y.Length + - curve.Order.Length + - curve.Cofactor.Length + + curve.Prime!.Length + + curve.A!.Length + + curve.B!.Length + + curve.G.X!.Length + + curve.G.Y!.Length + + curve.Order!.Length + + curve.Cofactor!.Length + (curve.Seed == null ? 0 : curve.Seed.Length); byte[] blob = new byte[blobSize]; @@ -386,7 +387,7 @@ namespace System.Security.Cryptography /// to the pre-Win10 magic numbers to support import on pre-Win10 environments /// that don't have the named curve functionality. /// - private static KeyBlobMagicNumber EcdsaCurveNameToMagicNumber(string name, bool includePrivateParameters) => + private static KeyBlobMagicNumber EcdsaCurveNameToMagicNumber(string? name, bool includePrivateParameters) => EcdsaCurveNameToAlgorithm(name) switch { AlgorithmName.ECDsaP256 => includePrivateParameters ? @@ -411,7 +412,7 @@ namespace System.Security.Cryptography /// to the pre-Win10 magic numbers to support import on pre-Win10 environments /// that don't have the named curve functionality. /// - private static KeyBlobMagicNumber EcdhCurveNameToMagicNumber(string name, bool includePrivateParameters) => + private static KeyBlobMagicNumber EcdhCurveNameToMagicNumber(string? name, bool includePrivateParameters) => EcdhCurveNameToAlgorithm(name) switch { AlgorithmName.ECDHP256 => includePrivateParameters ? @@ -520,7 +521,7 @@ namespace System.Security.Cryptography /// Map a curve name to algorithm. This enables curves that worked pre-Win10 /// to work with newer APIs for import and export. /// - internal static string EcdsaCurveNameToAlgorithm(string algorithm) + internal static string EcdsaCurveNameToAlgorithm(string? algorithm) { switch (algorithm) { @@ -545,7 +546,7 @@ namespace System.Security.Cryptography /// Map a curve name to algorithm. This enables curves that worked pre-Win10 /// to work with newer APIs for import and export. /// - internal static string EcdhCurveNameToAlgorithm(string algorithm) + internal static string EcdhCurveNameToAlgorithm(string? algorithm) { switch (algorithm) { diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.ImportExport.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.ImportExport.cs index ce4dae3..54eb1ef 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.ImportExport.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.ImportExport.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using Internal.NativeCrypto; namespace System.Security.Cryptography @@ -64,8 +65,8 @@ namespace System.Security.Cryptography { ECParameters ecparams = default; - string curveName = GetCurveName(out string oidValue); - byte[] blob = null; + string? curveName = GetCurveName(out string? oidValue); + byte[]? blob = null; try { diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.cs index 1462232..20cef43 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using Microsoft.Win32.SafeHandles; namespace System.Security.Cryptography @@ -74,8 +75,8 @@ namespace System.Security.Cryptography public override byte[] DeriveKeyFromHash( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] secretPrepend, - byte[] secretAppend) + byte[]? secretPrepend, + byte[]? secretAppend) { if (otherPartyPublicKey == null) throw new ArgumentNullException(nameof(otherPartyPublicKey)); @@ -96,9 +97,9 @@ namespace System.Security.Cryptography public override byte[] DeriveKeyFromHmac( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] hmacKey, - byte[] secretPrepend, - byte[] secretAppend) + byte[]? hmacKey, + byte[]? secretPrepend, + byte[]? secretAppend) { if (otherPartyPublicKey == null) throw new ArgumentNullException(nameof(otherPartyPublicKey)); diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanDerivation.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanDerivation.cs index 636b26c..afa34b6 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanDerivation.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanDerivation.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics; using System.Runtime.InteropServices; @@ -12,7 +13,7 @@ namespace System.Security.Cryptography /// /// Derive the raw ECDH value into , if present, otherwise returning the value. /// - internal delegate byte[] DeriveSecretAgreement(ECDiffieHellmanPublicKey otherPartyPublicKey, IncrementalHash hasher); + internal delegate byte[]? DeriveSecretAgreement(ECDiffieHellmanPublicKey otherPartyPublicKey, IncrementalHash? hasher); internal static byte[] DeriveKeyFromHash( ECDiffieHellmanPublicKey otherPartyPublicKey, @@ -28,7 +29,7 @@ namespace System.Security.Cryptography { hash.AppendData(secretPrepend); - byte[] secretAgreement = deriveSecretAgreement(otherPartyPublicKey, hash); + byte[]? secretAgreement = deriveSecretAgreement(otherPartyPublicKey, hash); // We want the side effect, and it should not have returned the answer. Debug.Assert(secretAgreement == null); @@ -41,7 +42,7 @@ namespace System.Security.Cryptography internal static unsafe byte[] DeriveKeyFromHmac( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] hmacKey, + byte[]? hmacKey, ReadOnlySpan secretPrepend, ReadOnlySpan secretAppend, DeriveSecretAgreement deriveSecretAgreement) @@ -60,8 +61,8 @@ namespace System.Security.Cryptography if (useSecretAsKey) { hmacKey = deriveSecretAgreement(otherPartyPublicKey, null); - Debug.Assert(hmacKey != null); } + Debug.Assert(hmacKey != null); // Reduce the likelihood of the value getting copied during heap compaction. fixed (byte* pinnedHmacKey = hmacKey) @@ -78,7 +79,7 @@ namespace System.Security.Cryptography } else { - byte[] secretAgreement = deriveSecretAgreement(otherPartyPublicKey, hash); + byte[]? secretAgreement = deriveSecretAgreement(otherPartyPublicKey, hash); // We want the side effect, and it should not have returned the answer. Debug.Assert(secretAgreement == null); } @@ -117,7 +118,7 @@ namespace System.Security.Cryptography const int Sha1Size = 20; const int Md5Size = 16; - byte[] secretAgreement = deriveSecretAgreement(otherPartyPublicKey, null); + byte[]? secretAgreement = deriveSecretAgreement(otherPartyPublicKey, null); Debug.Assert(secretAgreement != null); // Reduce the likelihood of the value getting copied during heap compaction. diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.Derive.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.Derive.cs index 30a81b6..877d736 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.Derive.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.Derive.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics; using Microsoft.Win32.SafeHandles; @@ -22,8 +23,8 @@ namespace System.Security.Cryptography public override byte[] DeriveKeyFromHash( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] secretPrepend, - byte[] secretAppend) + byte[]? secretPrepend, + byte[]? secretAppend) { if (otherPartyPublicKey == null) throw new ArgumentNullException(nameof(otherPartyPublicKey)); @@ -43,9 +44,9 @@ namespace System.Security.Cryptography public override byte[] DeriveKeyFromHmac( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] hmacKey, - byte[] secretPrepend, - byte[] secretAppend) + byte[]? hmacKey, + byte[]? secretPrepend, + byte[]? secretAppend) { if (otherPartyPublicKey == null) throw new ArgumentNullException(nameof(otherPartyPublicKey)); @@ -84,7 +85,7 @@ namespace System.Security.Cryptography /// /// Get the secret agreement generated between two parties /// - private byte[] DeriveSecretAgreement(ECDiffieHellmanPublicKey otherPartyPublicKey, IncrementalHash hasher) + private byte[]? DeriveSecretAgreement(ECDiffieHellmanPublicKey otherPartyPublicKey, IncrementalHash? hasher) { Debug.Assert(otherPartyPublicKey != null); @@ -92,7 +93,7 @@ namespace System.Security.Cryptography // which will throw an OpenSslCryptoException if no private key is available ECParameters thisKeyExplicit = ExportExplicitParameters(true); bool thisIsNamed = Interop.Crypto.EcKeyHasCurveName(_key.Value); - ECDiffieHellmanOpenSslPublicKey otherKey = otherPartyPublicKey as ECDiffieHellmanOpenSslPublicKey; + ECDiffieHellmanOpenSslPublicKey? otherKey = otherPartyPublicKey as ECDiffieHellmanOpenSslPublicKey; bool disposeOtherKey = false; if (otherKey == null) @@ -109,9 +110,9 @@ namespace System.Security.Cryptography bool otherIsNamed = otherKey.HasCurveName; - SafeEvpPKeyHandle ourKey = null; - SafeEvpPKeyHandle theirKey = null; - byte[] rented = null; + SafeEvpPKeyHandle? ourKey = null; + SafeEvpPKeyHandle? theirKey = null; + byte[]? rented = null; int secretLength = 0; try diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.cs index c82ece0..ffb24db 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSsl.cs @@ -42,7 +42,7 @@ namespace System.Security.Cryptography if (disposing) { _key?.Dispose(); - _key = null; + _key = null!; } base.Dispose(disposing); diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSslPublicKey.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSslPublicKey.cs index 3bde2dd..3977421 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSslPublicKey.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanOpenSslPublicKey.cs @@ -70,7 +70,7 @@ namespace System.Security.Cryptography if (disposing) { _key?.Dispose(); - _key = null; + _key = null!; } base.Dispose(disposing); diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanSecurityTransforms.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanSecurityTransforms.cs index 347c55d..6c02379 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanSecurityTransforms.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanSecurityTransforms.cs @@ -126,8 +126,8 @@ namespace System.Security.Cryptography public override byte[] DeriveKeyFromHash( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] secretPrepend, - byte[] secretAppend) + byte[]? secretPrepend, + byte[]? secretAppend) { if (otherPartyPublicKey == null) throw new ArgumentNullException(nameof(otherPartyPublicKey)); @@ -147,9 +147,9 @@ namespace System.Security.Cryptography public override byte[] DeriveKeyFromHmac( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] hmacKey, - byte[] secretPrepend, - byte[] secretAppend) + byte[]? hmacKey, + byte[]? secretPrepend, + byte[]? secretAppend) { if (otherPartyPublicKey == null) throw new ArgumentNullException(nameof(otherPartyPublicKey)); @@ -186,7 +186,7 @@ namespace System.Security.Cryptography (pubKey, hasher) => DeriveSecretAgreement(pubKey, hasher)); } - private byte[] DeriveSecretAgreement(ECDiffieHellmanPublicKey otherPartyPublicKey, IncrementalHash hasher) + private byte[]? DeriveSecretAgreement(ECDiffieHellmanPublicKey otherPartyPublicKey, IncrementalHash? hasher) { if (!(otherPartyPublicKey is ECDiffieHellmanSecurityTransformsPublicKey secTransPubKey)) { @@ -205,7 +205,7 @@ namespace System.Security.Cryptography nameof(otherPartyPublicKey)); } - SafeSecKeyRefHandle thisPrivate = GetKeys().PrivateKey; + SafeSecKeyRefHandle? thisPrivate = GetKeys().PrivateKey; if (thisPrivate == null) { @@ -216,7 +216,7 @@ namespace System.Security.Cryptography // 66 bytes ((521 + 7) / 8), the Span path will always succeed. Span secretSpan = stackalloc byte[66]; - byte[] secret = Interop.AppleCrypto.EcdhKeyAgree( + byte[]? secret = Interop.AppleCrypto.EcdhKeyAgree( thisPrivate, otherPublic, secretSpan, diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDsaCng.ImportExport.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDsaCng.ImportExport.cs index 03814d8..4d538c2 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDsaCng.ImportExport.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDsaCng.ImportExport.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using Internal.NativeCrypto; namespace System.Security.Cryptography @@ -44,7 +45,7 @@ namespace System.Security.Cryptography { // FriendlyName is required; an attempt was already made to default it in ECCurve if (string.IsNullOrEmpty(curve.Oid.FriendlyName)) - throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_InvalidCurveOid, curve.Oid.Value.ToString())); + throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_InvalidCurveOid, curve.Oid.Value!.ToString())); byte[] ecNamedCurveBlob = ECCng.GetNamedCurveBlob(ref parameters, ecdh: false); ImportKeyBlob(ecNamedCurveBlob, curve.Oid.FriendlyName, includePrivateParameters); @@ -85,7 +86,7 @@ namespace System.Security.Cryptography { ECParameters ecparams = default; - string curveName = GetCurveName(out string oidValue); + string? curveName = GetCurveName(out string? oidValue); if (string.IsNullOrEmpty(curveName)) { diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECDsaOpenSsl.cs b/src/libraries/Common/src/System/Security/Cryptography/ECDsaOpenSsl.cs index a87059a..a95b3db 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECDsaOpenSsl.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECDsaOpenSsl.cs @@ -169,7 +169,7 @@ namespace System.Security.Cryptography if (disposing) { _key?.Dispose(); - _key = null; + _key = null!; } base.Dispose(disposing); diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECOpenSsl.ImportExport.cs b/src/libraries/Common/src/System/Security/Cryptography/ECOpenSsl.ImportExport.cs index f6b4bcb..609ec11 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECOpenSsl.ImportExport.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECOpenSsl.ImportExport.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using Microsoft.Win32.SafeHandles; using System.Diagnostics; @@ -109,12 +110,12 @@ namespace System.Security.Cryptography // Use oid Value first if present, otherwise FriendlyName string oid = !string.IsNullOrEmpty(parameters.Curve.Oid.Value) ? - parameters.Curve.Oid.Value : parameters.Curve.Oid.FriendlyName; + parameters.Curve.Oid.Value : parameters.Curve.Oid.FriendlyName!; SafeEcKeyHandle key = Interop.Crypto.EcKeyCreateByKeyParameters( oid, - parameters.Q.X, parameters.Q.X.Length, - parameters.Q.Y, parameters.Q.Y.Length, + parameters.Q.X!, parameters.Q.X!.Length, + parameters.Q.Y!, parameters.Q.Y!.Length, parameters.D, parameters.D == null ? 0 : parameters.D.Length); return key; @@ -125,16 +126,16 @@ namespace System.Security.Cryptography Debug.Assert(parameters.Curve.IsPrime); SafeEcKeyHandle key = Interop.Crypto.EcKeyCreateByExplicitParameters( parameters.Curve.CurveType, - parameters.Q.X, parameters.Q.X.Length, - parameters.Q.Y, parameters.Q.Y.Length, + parameters.Q.X, parameters.Q.X!.Length, + parameters.Q.Y, parameters.Q.Y!.Length, parameters.D, parameters.D == null ? 0 : parameters.D.Length, - parameters.Curve.Prime, parameters.Curve.Prime.Length, - parameters.Curve.A, parameters.Curve.A.Length, - parameters.Curve.B, parameters.Curve.B.Length, - parameters.Curve.G.X, parameters.Curve.G.X.Length, - parameters.Curve.G.Y, parameters.Curve.G.Y.Length, - parameters.Curve.Order, parameters.Curve.Order.Length, - parameters.Curve.Cofactor, parameters.Curve.Cofactor.Length, + parameters.Curve.Prime!, parameters.Curve.Prime!.Length, + parameters.Curve.A!, parameters.Curve.A!.Length, + parameters.Curve.B!, parameters.Curve.B!.Length, + parameters.Curve.G.X!, parameters.Curve.G.X!.Length, + parameters.Curve.G.Y!, parameters.Curve.G.Y!.Length, + parameters.Curve.Order!, parameters.Curve.Order!.Length, + parameters.Curve.Cofactor, parameters.Curve.Cofactor!.Length, parameters.Curve.Seed, parameters.Curve.Seed == null ? 0 : parameters.Curve.Seed.Length); return key; @@ -145,16 +146,16 @@ namespace System.Security.Cryptography Debug.Assert(parameters.Curve.IsCharacteristic2); SafeEcKeyHandle key = Interop.Crypto.EcKeyCreateByExplicitParameters( parameters.Curve.CurveType, - parameters.Q.X, parameters.Q.X.Length, - parameters.Q.Y, parameters.Q.Y.Length, + parameters.Q.X, parameters.Q.X!.Length, + parameters.Q.Y, parameters.Q.Y!.Length, parameters.D, parameters.D == null ? 0 : parameters.D.Length, - parameters.Curve.Polynomial, parameters.Curve.Polynomial.Length, - parameters.Curve.A, parameters.Curve.A.Length, - parameters.Curve.B, parameters.Curve.B.Length, - parameters.Curve.G.X, parameters.Curve.G.X.Length, - parameters.Curve.G.Y, parameters.Curve.G.Y.Length, - parameters.Curve.Order, parameters.Curve.Order.Length, - parameters.Curve.Cofactor, parameters.Curve.Cofactor.Length, + parameters.Curve.Polynomial!, parameters.Curve.Polynomial!.Length, + parameters.Curve.A!, parameters.Curve.A!.Length, + parameters.Curve.B!, parameters.Curve.B!.Length, + parameters.Curve.G.X!, parameters.Curve.G.X!.Length, + parameters.Curve.G.Y!, parameters.Curve.G.Y!.Length, + parameters.Curve.Order!, parameters.Curve.Order!.Length, + parameters.Curve.Cofactor, parameters.Curve.Cofactor!.Length, parameters.Curve.Seed, parameters.Curve.Seed == null ? 0 : parameters.Curve.Seed.Length); return key; @@ -170,7 +171,7 @@ namespace System.Security.Cryptography public static SafeEcKeyHandle GenerateKeyByKeySize(int keySize) { - string oid = null; + string oid; switch (keySize) { case 256: oid = ECDSA_P256_OID_VALUE; break; @@ -181,7 +182,7 @@ namespace System.Security.Cryptography throw new InvalidOperationException(SR.Cryptography_InvalidKeySize); } - SafeEcKeyHandle key = Interop.Crypto.EcKeyCreateByOid(oid); + SafeEcKeyHandle? key = Interop.Crypto.EcKeyCreateByOid(oid); if (key == null || key.IsInvalid) throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CurveNotSupported, oid)); diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECOpenSsl.cs b/src/libraries/Common/src/System/Security/Cryptography/ECOpenSsl.cs index b2e2eb4..d41b801 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECOpenSsl.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECOpenSsl.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics; using Microsoft.Win32.SafeHandles; @@ -9,7 +10,7 @@ namespace System.Security.Cryptography { internal sealed partial class ECOpenSsl : IDisposable { - private Lazy _key; + private Lazy _key = null!; // Always initialized public ECOpenSsl(ECCurve curve) { @@ -86,12 +87,11 @@ namespace System.Security.Cryptography if (curve.IsNamed) { - string oid = null; // Use oid Value first if present, otherwise FriendlyName because Oid maintains a hard-coded // cache that may have different casing for FriendlyNames than OpenSsl - oid = !string.IsNullOrEmpty(curve.Oid.Value) ? curve.Oid.Value : curve.Oid.FriendlyName; + string oid = !string.IsNullOrEmpty(curve.Oid.Value) ? curve.Oid.Value : curve.Oid.FriendlyName!; - SafeEcKeyHandle key = Interop.Crypto.EcKeyCreateByOid(oid); + SafeEcKeyHandle? key = Interop.Crypto.EcKeyCreateByOid(oid); if (key == null || key.IsInvalid) { @@ -132,7 +132,7 @@ namespace System.Security.Cryptography _key.Value?.Dispose(); } - _key = null; + _key = null!; } } } diff --git a/src/libraries/Common/src/System/Security/Cryptography/EccKeyFormatHelper.cs b/src/libraries/Common/src/System/Security/Cryptography/EccKeyFormatHelper.cs index cae5d7c..785fa56 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/EccKeyFormatHelper.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/EccKeyFormatHelper.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Collections; using System.Diagnostics; @@ -138,7 +139,7 @@ namespace System.Security.Cryptography } else { - domainParameters = ECDomainParameters.Decode(algId.Parameters.Value, AsnEncodingRules.DER); + domainParameters = ECDomainParameters.Decode(algId.Parameters!.Value, AsnEncodingRules.DER); } ret = new ECParameters @@ -488,10 +489,11 @@ namespace System.Security.Cryptography // On Windows the FriendlyName is populated in places where the Value mightn't be. if (string.IsNullOrEmpty(oid.Value)) { + Debug.Assert(oid.FriendlyName != null); oid = Oid.FromFriendlyName(oid.FriendlyName, OidGroup.All); } - writer.WriteObjectIdentifier(oid.Value); + writer.WriteObjectIdentifier(oid.Value!); } else if (ecParameters.Curve.IsExplicit) { @@ -605,7 +607,7 @@ namespace System.Security.Cryptography ref int k2, ref int k3) { - byte[] polynomial = ecParameters.Curve.Polynomial; + byte[] polynomial = ecParameters.Curve.Polynomial!; int lastIndex = polynomial.Length - 1; // The most significant byte needs a set bit, and the least significant bit must be set. @@ -691,8 +693,8 @@ namespace System.Security.Cryptography private static void WriteCurve(in ECCurve curve, AsnWriter writer) { writer.PushSequence(); - WriteFieldElement(curve.A, writer); - WriteFieldElement(curve.B, writer); + WriteFieldElement(curve.A!, writer); + WriteFieldElement(curve.B!, writer); if (curve.Seed != null) { @@ -716,7 +718,7 @@ namespace System.Security.Cryptography private static void WriteUncompressedBasePoint(in ECParameters ecParameters, AsnWriter writer) { - int basePointLength = ecParameters.Curve.G.X.Length * 2 + 1; + int basePointLength = ecParameters.Curve.G.X!.Length * 2 + 1; byte[] tmp = CryptoPool.Rent(basePointLength); tmp[0] = 0x04; ecParameters.Curve.G.X.CopyTo(tmp.AsSpan(1)); @@ -728,7 +730,7 @@ namespace System.Security.Cryptography private static void WriteUncompressedPublicKey(in ECParameters ecParameters, AsnWriter writer) { - int publicKeyLength = ecParameters.Q.X.Length * 2 + 1; + int publicKeyLength = ecParameters.Q.X!.Length * 2 + 1; writer.WriteBitString( publicKeyLength, @@ -737,7 +739,7 @@ namespace System.Security.Cryptography { publicKeyBytes[0] = 0x04; point.X.AsSpan().CopyTo(publicKeyBytes.Slice(1)); - point.Y.AsSpan().CopyTo(publicKeyBytes.Slice(1 + point.X.Length)); + point.Y.AsSpan().CopyTo(publicKeyBytes.Slice(1 + point.X!.Length)); }); } diff --git a/src/libraries/Common/src/System/Security/Cryptography/EccSecurityTransforms.cs b/src/libraries/Common/src/System/Security/Cryptography/EccSecurityTransforms.cs index 97f2369..d095711 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/EccSecurityTransforms.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/EccSecurityTransforms.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; using System.Runtime.InteropServices; @@ -12,7 +13,7 @@ namespace System.Security.Cryptography { internal sealed class EccSecurityTransforms : IDisposable { - private SecKeyPair _keys; + private SecKeyPair? _keys; private bool _disposed; private readonly string _disposedName; @@ -90,7 +91,7 @@ namespace System.Security.Cryptography { ThrowIfDisposed(); - SecKeyPair current = _keys; + SecKeyPair? current = _keys; if (current != null) { @@ -111,7 +112,7 @@ namespace System.Security.Cryptography { ThrowIfDisposed(); - SecKeyPair current = _keys; + SecKeyPair? current = _keys; _keys = keyPair; current?.Dispose(); } diff --git a/src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs b/src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs index 2647c35..8ffac24 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; using System.Runtime.CompilerServices; @@ -233,7 +234,7 @@ namespace System.Security.Cryptography if (innerRead != decryptedMemory.Length) { - ret = default; + ret = default!; throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } @@ -329,9 +330,9 @@ namespace System.Security.Cryptography out string encryptionAlgorithmOid, out bool isPkcs12); - byte[] encryptedRent = null; + byte[]? encryptedRent = null; Span encryptedSpan = default; - AsnWriter writer = null; + AsnWriter? writer = null; try { @@ -385,7 +386,7 @@ namespace System.Security.Cryptography finally { CryptographicOperations.ZeroMemory(encryptedSpan); - CryptoPool.Return(encryptedRent, clearSize: 0); + CryptoPool.Return(encryptedRent!, clearSize: 0); writer?.Dispose(); cipher.Dispose(); @@ -485,7 +486,7 @@ namespace System.Security.Cryptography finally { CryptographicOperations.ZeroMemory(decrypted); - CryptoPool.Return(decrypted.Array, clearSize: 0); + CryptoPool.Return(decrypted.Array!, clearSize: 0); } } @@ -524,7 +525,7 @@ namespace System.Security.Cryptography finally { CryptographicOperations.ZeroMemory(decrypted); - CryptoPool.Return(decrypted.Array, clearSize: 0); + CryptoPool.Return(decrypted.Array!, clearSize: 0); } } } diff --git a/src/libraries/Common/src/System/Security/Cryptography/PasswordBasedEncryption.cs b/src/libraries/Common/src/System/Security/Cryptography/PasswordBasedEncryption.cs index c5af8f1..5780e28 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/PasswordBasedEncryption.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/PasswordBasedEncryption.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; @@ -77,7 +78,7 @@ namespace System.Security.Cryptography // Maybe some future PBES3 will have one with a default. HashAlgorithmName digestAlgorithmName; - SymmetricAlgorithm cipher = null; + SymmetricAlgorithm? cipher = null; bool pkcs12 = false; @@ -161,8 +162,8 @@ namespace System.Security.Cryptography { Span buf = stackalloc byte[128]; ReadOnlySpan effectivePasswordBytes = stackalloc byte[0]; - byte[] rented = null; - System.Text.Encoding encoding = null; + byte[]? rented = null; + System.Text.Encoding? encoding = null; if (passwordBytes.Length > 0 || password.Length == 0) { @@ -299,7 +300,7 @@ namespace System.Security.Cryptography byte[] destination, Span ivDest) { - byte[] pwdTmpBytes = null; + byte[]? pwdTmpBytes = null; byte[] derivedKey; byte[] iv = cipher.IV; @@ -356,14 +357,14 @@ namespace System.Security.Cryptography { if (passwordBytes.Length > 0) { - Debug.Assert(pwdTmpBytes.Length == passwordBytes.Length); + Debug.Assert(pwdTmpBytes!.Length == passwordBytes.Length); passwordBytes.CopyTo(pwdTmpBytes); } else if (password.Length > 0) { int length = encoding.GetBytes(password, pwdTmpBytes); - if (length != pwdTmpBytes.Length) + if (length != pwdTmpBytes!.Length) { Debug.Fail($"UTF-8 encoding size changed between GetByteCount and GetBytes"); throw new CryptographicException(); @@ -371,7 +372,7 @@ namespace System.Security.Cryptography } else { - Debug.Assert(pwdTmpBytes.Length == 0); + Debug.Assert(pwdTmpBytes!.Length == 0); } using (var pbkdf2 = new Rfc2898DeriveBytes(pwdTmpBytes, salt.ToArray(), iterationCount, prf)) @@ -436,8 +437,8 @@ namespace System.Security.Cryptography { Span buf = stackalloc byte[128]; ReadOnlySpan effectivePasswordBytes = stackalloc byte[0]; - byte[] rented = null; - System.Text.Encoding encoding = null; + byte[]? rented = null; + System.Text.Encoding? encoding = null; if (passwordBytes.Length > 0 || password.Length == 0) { @@ -547,7 +548,7 @@ namespace System.Security.Cryptography byte? requestedKeyLength, ref Span iv) { - string algId = encryptionScheme.Algorithm.Value; + string? algId = encryptionScheme.Algorithm.Value; if (algId == Oids.Aes128Cbc || algId == Oids.Aes192Cbc || diff --git a/src/libraries/Common/src/System/Security/Cryptography/Pkcs12Kdf.cs b/src/libraries/Common/src/System/Security/Cryptography/Pkcs12Kdf.cs index f7f7687..c2c6188 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Pkcs12Kdf.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Pkcs12Kdf.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Collections.Generic; using System.Diagnostics; using System.Text; @@ -85,7 +86,7 @@ namespace System.Security.Cryptography.Pkcs // https://tools.ietf.org/html/rfc7292#appendix-B.2 Debug.Assert(iterationCount >= 1); - if (!s_uvLookup.TryGetValue(hashAlgorithm, out Tuple uv)) + if (!s_uvLookup.TryGetValue(hashAlgorithm, out Tuple? uv)) { throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name); } @@ -128,7 +129,7 @@ namespace System.Security.Cryptography.Pkcs // 4. Set I=S||P to be the concatenation of S and P. int ILen = SLen + PLen; Span I = stackalloc byte[0]; - byte[] IRented = null; + byte[]? IRented = null; if (ILen <= 1024) { diff --git a/src/libraries/Common/src/System/Security/Cryptography/RSACng.ImportExport.cs b/src/libraries/Common/src/System/Security/Cryptography/RSACng.ImportExport.cs index 79fe528..86950a2 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/RSACng.ImportExport.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/RSACng.ImportExport.cs @@ -104,8 +104,8 @@ namespace System.Security.Cryptography parameters.Modulus.Length; if (includePrivate) { - blobSize += parameters.P.Length + - parameters.Q.Length; + blobSize += parameters.P!.Length + + parameters.Q!.Length; } byte[] rsaBlob = new byte[blobSize]; @@ -120,8 +120,8 @@ namespace System.Security.Cryptography if (includePrivate) { - pBcryptBlob->cbPrime1 = parameters.P.Length; - pBcryptBlob->cbPrime2 = parameters.Q.Length; + pBcryptBlob->cbPrime1 = parameters.P!.Length; + pBcryptBlob->cbPrime2 = parameters.Q!.Length; } int offset = sizeof(BCRYPT_RSAKEY_BLOB); @@ -131,8 +131,8 @@ namespace System.Security.Cryptography if (includePrivate) { - Interop.BCrypt.Emit(rsaBlob, ref offset, parameters.P); - Interop.BCrypt.Emit(rsaBlob, ref offset, parameters.Q); + Interop.BCrypt.Emit(rsaBlob, ref offset, parameters.P!); + Interop.BCrypt.Emit(rsaBlob, ref offset, parameters.Q!); } // We better have computed the right allocation size above! diff --git a/src/libraries/Common/src/System/Security/Cryptography/RSACng.SignVerify.cs b/src/libraries/Common/src/System/Security/Cryptography/RSACng.SignVerify.cs index 3560eff..1c73ed5 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/RSACng.SignVerify.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/RSACng.SignVerify.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.InteropServices; @@ -35,7 +36,7 @@ namespace System.Security.Cryptography hashAlgorithm, alg => { - using (HashProviderCng hashProvider = new HashProviderCng(alg.Name, null)) + using (HashProviderCng hashProvider = new HashProviderCng(alg.Name!, null)) { return hashProvider.HashSizeInBytes; } @@ -52,7 +53,7 @@ namespace System.Security.Cryptography throw new ArgumentNullException(nameof(hash)); } - string hashAlgorithmName = hashAlgorithm.Name; + string? hashAlgorithmName = hashAlgorithm.Name; if (string.IsNullOrEmpty(hashAlgorithmName)) { throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm)); @@ -99,7 +100,7 @@ namespace System.Security.Cryptography public override unsafe bool TrySignHash(ReadOnlySpan hash, Span destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, out int bytesWritten) { - string hashAlgorithmName = hashAlgorithm.Name; + string? hashAlgorithmName = hashAlgorithm.Name; if (string.IsNullOrEmpty(hashAlgorithmName)) { throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm)); @@ -159,7 +160,7 @@ namespace System.Security.Cryptography public override unsafe bool VerifyHash(ReadOnlySpan hash, ReadOnlySpan signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) { - string hashAlgorithmName = hashAlgorithm.Name; + string? hashAlgorithmName = hashAlgorithm.Name; if (string.IsNullOrEmpty(hashAlgorithmName)) { throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm)); diff --git a/src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs b/src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs index cb0b92f..8a13530 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; using System.IO; @@ -88,11 +89,11 @@ namespace System.Security.Cryptography if (padding == null) throw new ArgumentNullException(nameof(padding)); - Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding, out RsaPaddingProcessor oaepProcessor); + Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding, out RsaPaddingProcessor? oaepProcessor); SafeRsaHandle key = GetKey(); int rsaSize = Interop.Crypto.RsaSize(key); - byte[] buf = null; + byte[]? buf = null; Span destination = default; try @@ -111,7 +112,7 @@ namespace System.Security.Cryptography finally { CryptographicOperations.ZeroMemory(destination); - CryptoPool.Return(buf, clearSize: 0); + CryptoPool.Return(buf!, clearSize: 0); } } @@ -126,7 +127,7 @@ namespace System.Security.Cryptography throw new ArgumentNullException(nameof(padding)); } - Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding, out RsaPaddingProcessor oaepProcessor); + Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding, out RsaPaddingProcessor? oaepProcessor); SafeRsaHandle key = GetKey(); int keySizeBytes = Interop.Crypto.RsaSize(key); @@ -136,7 +137,7 @@ namespace System.Security.Cryptography if (destination.Length < keySizeBytes) { Span tmp = stackalloc byte[0]; - byte[] rent = null; + byte[]? rent = null; // RSA up through 4096 stackalloc if (keySizeBytes <= 512) @@ -185,7 +186,7 @@ namespace System.Security.Cryptography ReadOnlySpan data, Span destination, Interop.Crypto.RsaPadding rsaPadding, - RsaPaddingProcessor rsaPaddingProcessor, + RsaPaddingProcessor? rsaPaddingProcessor, out int bytesWritten) { // If rsaPadding is PKCS1 or OAEP-SHA1 then no depadding method should be present. @@ -211,7 +212,7 @@ namespace System.Security.Cryptography } Span decryptBuf = destination; - byte[] paddingBuf = null; + byte[]? paddingBuf = null; if (rsaPaddingProcessor != null) { @@ -259,7 +260,7 @@ namespace System.Security.Cryptography if (padding == null) throw new ArgumentNullException(nameof(padding)); - Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding, out RsaPaddingProcessor oaepProcessor); + Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding, out RsaPaddingProcessor? oaepProcessor); SafeRsaHandle key = GetKey(); byte[] buf = new byte[Interop.Crypto.RsaSize(key)]; @@ -288,7 +289,7 @@ namespace System.Security.Cryptography throw new ArgumentNullException(nameof(padding)); } - Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding, out RsaPaddingProcessor oaepProcessor); + Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding, out RsaPaddingProcessor? oaepProcessor); SafeRsaHandle key = GetKey(); return TryEncrypt(key, data, destination, rsaPadding, oaepProcessor, out bytesWritten); @@ -299,7 +300,7 @@ namespace System.Security.Cryptography ReadOnlySpan data, Span destination, Interop.Crypto.RsaPadding rsaPadding, - RsaPaddingProcessor rsaPaddingProcessor, + RsaPaddingProcessor? rsaPaddingProcessor, out int bytesWritten) { int rsaSize = Interop.Crypto.RsaSize(key); @@ -346,7 +347,7 @@ namespace System.Security.Cryptography private static Interop.Crypto.RsaPadding GetInteropPadding( RSAEncryptionPadding padding, - out RsaPaddingProcessor rsaPaddingProcessor) + out RsaPaddingProcessor? rsaPaddingProcessor) { if (padding == RSAEncryptionPadding.Pkcs1) { @@ -487,7 +488,7 @@ namespace System.Security.Cryptography if (disposing) { FreeKey(); - _key = null; + _key = null!; } base.Dispose(disposing); @@ -641,7 +642,7 @@ namespace System.Security.Cryptography hashAlgorithm, padding, true, out int bytesWritten, - out byte[] signature)) + out byte[]? signature)) { Debug.Fail("TrySignHash should not return false in allocation mode"); throw new CryptographicException(); @@ -674,7 +675,7 @@ namespace System.Security.Cryptography padding, false, out bytesWritten, - out byte[] alloced); + out byte[]? alloced); Debug.Assert(alloced == null); return ret; @@ -687,7 +688,7 @@ namespace System.Security.Cryptography RSASignaturePadding padding, bool allocateSignature, out int bytesWritten, - out byte[] signature) + out byte[]? signature) { Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name)); Debug.Assert(padding != null); @@ -851,7 +852,7 @@ namespace System.Security.Cryptography { // All of the current HashAlgorithmName values correspond to the SN values in OpenSSL 0.9.8. // If there's ever a new one that doesn't, translate it here. - string sn = hashAlgorithmName.Name; + string sn = hashAlgorithmName.Name!; int nid = Interop.Crypto.ObjSn2Nid(sn); diff --git a/src/libraries/Common/src/System/Security/Cryptography/RSASecurityTransforms.cs b/src/libraries/Common/src/System/Security/Cryptography/RSASecurityTransforms.cs index 683086e..a21db3d 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/RSASecurityTransforms.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/RSASecurityTransforms.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; using System.IO; @@ -27,7 +28,7 @@ namespace System.Security.Cryptography { public sealed partial class RSASecurityTransforms : RSA { - private SecKeyPair _keys; + private SecKeyPair? _keys; public RSASecurityTransforms() : this(2048) @@ -326,7 +327,7 @@ namespace System.Security.Cryptography out bytesWritten); } - RsaPaddingProcessor processor; + RsaPaddingProcessor? processor; switch (padding.Mode) { @@ -558,7 +559,7 @@ namespace System.Security.Cryptography ThrowIfDisposed(); - RsaPaddingProcessor processor = null; + RsaPaddingProcessor? processor = null; if (padding.Mode == RSASignaturePaddingMode.Pss) { @@ -774,7 +775,7 @@ namespace System.Security.Cryptography private void ThrowIfDisposed() { - SecKeyPair current = _keys; + SecKeyPair? current = _keys; if (current != null && current.PublicKey == null) { @@ -785,7 +786,7 @@ namespace System.Security.Cryptography internal SecKeyPair GetKeys() { ThrowIfDisposed(); - SecKeyPair current = _keys; + SecKeyPair? current = _keys; if (current != null) { @@ -806,7 +807,7 @@ namespace System.Security.Cryptography { ThrowIfDisposed(); - SecKeyPair current = _keys; + SecKeyPair? current = _keys; _keys = newKeyPair; current?.Dispose(); diff --git a/src/libraries/Common/src/System/Security/Cryptography/RsaPaddingProcessor.cs b/src/libraries/Common/src/System/Security/Cryptography/RsaPaddingProcessor.cs index 2f54f03..291915e 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/RsaPaddingProcessor.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/RsaPaddingProcessor.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers.Binary; using System.Collections.Concurrent; using System.Diagnostics; @@ -87,7 +88,7 @@ namespace System.Security.Cryptography { // https://tools.ietf.org/html/rfc3447#section-7.1.1 - byte[] dbMask = null; + byte[]? dbMask = null; Span dbMaskSpan = Span.Empty; try diff --git a/src/libraries/Common/src/System/Security/Cryptography/SecKeyPair.cs b/src/libraries/Common/src/System/Security/Cryptography/SecKeyPair.cs index 45719a6..219a03e 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/SecKeyPair.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/SecKeyPair.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Security.Cryptography.Apple; namespace System.Security.Cryptography @@ -9,9 +10,9 @@ namespace System.Security.Cryptography internal sealed class SecKeyPair : IDisposable { internal SafeSecKeyRefHandle PublicKey { get; private set; } - internal SafeSecKeyRefHandle PrivateKey { get; private set; } + internal SafeSecKeyRefHandle? PrivateKey { get; private set; } - private SecKeyPair(SafeSecKeyRefHandle publicKey, SafeSecKeyRefHandle privateKey) + private SecKeyPair(SafeSecKeyRefHandle publicKey, SafeSecKeyRefHandle? privateKey) { PublicKey = publicKey; PrivateKey = privateKey; @@ -22,7 +23,7 @@ namespace System.Security.Cryptography PrivateKey?.Dispose(); PrivateKey = null; PublicKey?.Dispose(); - PublicKey = null; + PublicKey = null!; } internal static SecKeyPair PublicPrivatePair(SafeSecKeyRefHandle publicKey, SafeSecKeyRefHandle privateKey) diff --git a/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs b/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs index f60d0ed..8cc3786 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs @@ -11,7 +11,7 @@ namespace System.Security.Cryptography { protected Aes() { } public static new System.Security.Cryptography.Aes Create() { throw null; } - public static new System.Security.Cryptography.Aes Create(string algorithmName) { throw null; } + public static new System.Security.Cryptography.Aes? Create(string algorithmName) { throw null; } } public sealed partial class AesCcm : System.IDisposable { @@ -19,10 +19,10 @@ namespace System.Security.Cryptography public AesCcm(System.ReadOnlySpan key) { } public static System.Security.Cryptography.KeySizes NonceByteSizes { get { throw null; } } public static System.Security.Cryptography.KeySizes TagByteSizes { get { throw null; } } - public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[] associatedData = null) { } + public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData = null) { } public void Decrypt(System.ReadOnlySpan nonce, System.ReadOnlySpan ciphertext, System.ReadOnlySpan tag, System.Span plaintext, System.ReadOnlySpan associatedData = default(System.ReadOnlySpan)) { } public void Dispose() { } - public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[] associatedData = null) { } + public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null) { } public void Encrypt(System.ReadOnlySpan nonce, System.ReadOnlySpan plaintext, System.Span ciphertext, System.Span tag, System.ReadOnlySpan associatedData = default(System.ReadOnlySpan)) { } } public sealed partial class AesGcm : System.IDisposable @@ -31,10 +31,10 @@ namespace System.Security.Cryptography public AesGcm(System.ReadOnlySpan key) { } public static System.Security.Cryptography.KeySizes NonceByteSizes { get { throw null; } } public static System.Security.Cryptography.KeySizes TagByteSizes { get { throw null; } } - public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[] associatedData = null) { } + public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData = null) { } public void Decrypt(System.ReadOnlySpan nonce, System.ReadOnlySpan ciphertext, System.ReadOnlySpan tag, System.Span plaintext, System.ReadOnlySpan associatedData = default(System.ReadOnlySpan)) { } public void Dispose() { } - public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[] associatedData = null) { } + public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null) { } public void Encrypt(System.ReadOnlySpan nonce, System.ReadOnlySpan plaintext, System.Span ciphertext, System.Span tag, System.ReadOnlySpan associatedData = default(System.ReadOnlySpan)) { } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] @@ -51,9 +51,9 @@ namespace System.Security.Cryptography public override System.Security.Cryptography.CipherMode Mode { get { throw null; } set { } } public override System.Security.Cryptography.PaddingMode Padding { get { throw null; } set { } } public override System.Security.Cryptography.ICryptoTransform CreateDecryptor() { throw null; } - public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) { throw null; } + public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { throw null; } public override System.Security.Cryptography.ICryptoTransform CreateEncryptor() { throw null; } - public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) { throw null; } + public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { throw null; } protected override void Dispose(bool disposing) { } public override void GenerateIV() { } public override void GenerateKey() { } @@ -61,16 +61,16 @@ namespace System.Security.Cryptography public abstract partial class AsymmetricKeyExchangeDeformatter { protected AsymmetricKeyExchangeDeformatter() { } - public abstract string Parameters { get; set; } + public abstract string? Parameters { get; set; } public abstract byte[] DecryptKeyExchange(byte[] rgb); public abstract void SetKey(System.Security.Cryptography.AsymmetricAlgorithm key); } public abstract partial class AsymmetricKeyExchangeFormatter { protected AsymmetricKeyExchangeFormatter() { } - public abstract string Parameters { get; } + public abstract string? Parameters { get; } public abstract byte[] CreateKeyExchange(byte[] data); - public abstract byte[] CreateKeyExchange(byte[] data, System.Type symAlgType); + public abstract byte[] CreateKeyExchange(byte[] data, System.Type? symAlgType); public abstract void SetKey(System.Security.Cryptography.AsymmetricAlgorithm key); } public abstract partial class AsymmetricSignatureDeformatter @@ -95,10 +95,10 @@ namespace System.Security.Cryptography public static bool AllowOnlyFipsAlgorithms { get { throw null; } } public static void AddAlgorithm(System.Type algorithm, params string[] names) { } public static void AddOID(string oid, params string[] names) { } - public static object CreateFromName(string name) { throw null; } - public static object CreateFromName(string name, params object[] args) { throw null; } + public static object? CreateFromName(string name) { throw null; } + public static object? CreateFromName(string name, params object?[]? args) { throw null; } public static byte[] EncodeOID(string str) { throw null; } - public static string MapNameToOID(string name) { throw null; } + public static string? MapNameToOID(string name) { throw null; } } public abstract partial class DeriveBytes : System.IDisposable { @@ -114,7 +114,7 @@ namespace System.Security.Cryptography protected DES() { } public override byte[] Key { get { throw null; } set { } } public static new System.Security.Cryptography.DES Create() { throw null; } - public static new System.Security.Cryptography.DES Create(string algName) { throw null; } + public static new System.Security.Cryptography.DES? Create(string algName) { throw null; } public static bool IsSemiWeakKey(byte[] rgbKey) { throw null; } public static bool IsWeakKey(byte[] rgbKey) { throw null; } } @@ -124,7 +124,7 @@ namespace System.Security.Cryptography public static new System.Security.Cryptography.DSA Create() { throw null; } public static System.Security.Cryptography.DSA Create(int keySizeInBits) { throw null; } public static System.Security.Cryptography.DSA Create(System.Security.Cryptography.DSAParameters parameters) { throw null; } - public static new System.Security.Cryptography.DSA Create(string algName) { throw null; } + public static new System.Security.Cryptography.DSA? Create(string algName) { throw null; } public abstract byte[] CreateSignature(byte[] rgbHash); public abstract System.Security.Cryptography.DSAParameters ExportParameters(bool includePrivateParameters); public override void FromXmlString(string xmlString) { } @@ -157,11 +157,11 @@ namespace System.Security.Cryptography { public int Counter; public byte[] G; - public byte[] J; + public byte[]? J; public byte[] P; public byte[] Q; - public byte[] Seed; - public byte[] X; + public byte[]? Seed; + public byte[]? X; public byte[] Y; } public partial class DSASignatureDeformatter : System.Security.Cryptography.AsymmetricSignatureDeformatter @@ -186,14 +186,14 @@ namespace System.Security.Cryptography private int _dummyPrimitive; public byte[] A; public byte[] B; - public byte[] Cofactor; + public byte[]? Cofactor; public System.Security.Cryptography.ECCurve.ECCurveType CurveType; public System.Security.Cryptography.ECPoint G; public System.Security.Cryptography.HashAlgorithmName? Hash; public byte[] Order; public byte[] Polynomial; public byte[] Prime; - public byte[] Seed; + public byte[]? Seed; public bool IsCharacteristic2 { get { throw null; } } public bool IsExplicit { get { throw null; } } public bool IsNamed { get { throw null; } } @@ -238,15 +238,15 @@ namespace System.Security.Cryptography protected ECDiffieHellman() { } public override string KeyExchangeAlgorithm { get { throw null; } } public abstract System.Security.Cryptography.ECDiffieHellmanPublicKey PublicKey { get; } - public override string SignatureAlgorithm { get { throw null; } } + public override string? SignatureAlgorithm { get { throw null; } } public static new System.Security.Cryptography.ECDiffieHellman Create() { throw null; } public static System.Security.Cryptography.ECDiffieHellman Create(System.Security.Cryptography.ECCurve curve) { throw null; } public static System.Security.Cryptography.ECDiffieHellman Create(System.Security.Cryptography.ECParameters parameters) { throw null; } - public static new System.Security.Cryptography.ECDiffieHellman Create(string algorithm) { throw null; } + public static new System.Security.Cryptography.ECDiffieHellman? Create(string algorithm) { throw null; } public byte[] DeriveKeyFromHash(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, System.Security.Cryptography.HashAlgorithmName hashAlgorithm) { throw null; } - public virtual byte[] DeriveKeyFromHash(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, byte[] secretPrepend, byte[] secretAppend) { throw null; } - public byte[] DeriveKeyFromHmac(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, byte[] hmacKey) { throw null; } - public virtual byte[] DeriveKeyFromHmac(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend) { throw null; } + public virtual byte[] DeriveKeyFromHash(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, byte[]? secretPrepend, byte[]? secretAppend) { throw null; } + public byte[] DeriveKeyFromHmac(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, byte[]? hmacKey) { throw null; } + public virtual byte[] DeriveKeyFromHmac(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, byte[]? hmacKey, byte[]? secretPrepend, byte[]? secretAppend) { throw null; } public virtual byte[] DeriveKeyMaterial(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey) { throw null; } public virtual byte[] DeriveKeyTls(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, byte[] prfLabel, byte[] prfSeed) { throw null; } public virtual byte[] ExportECPrivateKey() { throw null; } @@ -281,12 +281,12 @@ namespace System.Security.Cryptography public abstract partial class ECDsa : System.Security.Cryptography.AsymmetricAlgorithm { protected ECDsa() { } - public override string KeyExchangeAlgorithm { get { throw null; } } + public override string? KeyExchangeAlgorithm { get { throw null; } } public override string SignatureAlgorithm { get { throw null; } } public static new System.Security.Cryptography.ECDsa Create() { throw null; } public static System.Security.Cryptography.ECDsa Create(System.Security.Cryptography.ECCurve curve) { throw null; } public static System.Security.Cryptography.ECDsa Create(System.Security.Cryptography.ECParameters parameters) { throw null; } - public static new System.Security.Cryptography.ECDsa Create(string algorithm) { throw null; } + public static new System.Security.Cryptography.ECDsa? Create(string algorithm) { throw null; } public virtual byte[] ExportECPrivateKey() { throw null; } public virtual System.Security.Cryptography.ECParameters ExportExplicitParameters(bool includePrivateParameters) { throw null; } public virtual System.Security.Cryptography.ECParameters ExportParameters(bool includePrivateParameters) { throw null; } @@ -323,22 +323,22 @@ namespace System.Security.Cryptography public partial struct ECParameters { public System.Security.Cryptography.ECCurve Curve; - public byte[] D; + public byte[]? D; public System.Security.Cryptography.ECPoint Q; public void Validate() { } } public partial struct ECPoint { - public byte[] X; - public byte[] Y; + public byte[]? X; + public byte[]? Y; } public static class HKDF { - public static byte[] Extract(HashAlgorithmName hashAlgorithmName, byte[] ikm, byte[] salt = null) { throw null; } + public static byte[] Extract(HashAlgorithmName hashAlgorithmName, byte[] ikm, byte[]? salt = null) { throw null; } public static int Extract(HashAlgorithmName hashAlgorithmName, ReadOnlySpan ikm, ReadOnlySpan salt, Span prk) { throw null; } - public static byte[] Expand(HashAlgorithmName hashAlgorithmName, byte[] prk, int outputLength, byte[] info = null) { throw null; } + public static byte[] Expand(HashAlgorithmName hashAlgorithmName, byte[] prk, int outputLength, byte[]? info = null) { throw null; } public static void Expand(HashAlgorithmName hashAlgorithmName, ReadOnlySpan prk, Span output, ReadOnlySpan info) { throw null; } - public static byte[] DeriveKey(HashAlgorithmName hashAlgorithmName, byte[] ikm, int outputLength, byte[] salt = null, byte[] info = null) { throw null; } + public static byte[] DeriveKey(HashAlgorithmName hashAlgorithmName, byte[] ikm, int outputLength, byte[]? salt = null, byte[]? info = null) { throw null; } public static void DeriveKey(HashAlgorithmName hashAlgorithmName, ReadOnlySpan ikm, Span output, ReadOnlySpan salt, ReadOnlySpan info) { throw null; } } public partial class HMACMD5 : System.Security.Cryptography.HMAC @@ -427,7 +427,7 @@ namespace System.Security.Cryptography { protected MD5() { } public static new System.Security.Cryptography.MD5 Create() { throw null; } - public static new System.Security.Cryptography.MD5 Create(string algName) { throw null; } + public static new System.Security.Cryptography.MD5? Create(string algName) { throw null; } } public partial class PKCS1MaskGenerationMethod : System.Security.Cryptography.MaskGenerationMethod { @@ -439,7 +439,7 @@ namespace System.Security.Cryptography { protected RandomNumberGenerator() { } public static System.Security.Cryptography.RandomNumberGenerator Create() { throw null; } - public static System.Security.Cryptography.RandomNumberGenerator Create(string rngName) { throw null; } + public static System.Security.Cryptography.RandomNumberGenerator? Create(string rngName) { throw null; } public void Dispose() { } protected virtual void Dispose(bool disposing) { } public static void Fill(System.Span data) { } @@ -459,7 +459,7 @@ namespace System.Security.Cryptography public virtual int EffectiveKeySize { get { throw null; } set { } } public override int KeySize { get { throw null; } set { } } public static new System.Security.Cryptography.RC2 Create() { throw null; } - public static new System.Security.Cryptography.RC2 Create(string AlgName) { throw null; } + public static new System.Security.Cryptography.RC2? Create(string AlgName) { throw null; } } public partial class Rfc2898DeriveBytes : System.Security.Cryptography.DeriveBytes { @@ -484,7 +484,7 @@ namespace System.Security.Cryptography { protected Rijndael() { } public static new System.Security.Cryptography.Rijndael Create() { throw null; } - public static new System.Security.Cryptography.Rijndael Create(string algName) { throw null; } + public static new System.Security.Cryptography.Rijndael? Create(string algName) { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public sealed partial class RijndaelManaged : System.Security.Cryptography.Rijndael @@ -498,9 +498,9 @@ namespace System.Security.Cryptography public override System.Security.Cryptography.CipherMode Mode { get { throw null; } set { } } public override System.Security.Cryptography.PaddingMode Padding { get { throw null; } set { } } public override System.Security.Cryptography.ICryptoTransform CreateDecryptor() { throw null; } - public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) { throw null; } + public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { throw null; } public override System.Security.Cryptography.ICryptoTransform CreateEncryptor() { throw null; } - public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) { throw null; } + public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { throw null; } protected override void Dispose(bool disposing) { } public override void GenerateIV() { } public override void GenerateKey() { } @@ -513,7 +513,7 @@ namespace System.Security.Cryptography public static new System.Security.Cryptography.RSA Create() { throw null; } public static System.Security.Cryptography.RSA Create(int keySizeInBits) { throw null; } public static System.Security.Cryptography.RSA Create(System.Security.Cryptography.RSAParameters parameters) { throw null; } - public static new System.Security.Cryptography.RSA Create(string algName) { throw null; } + public static new System.Security.Cryptography.RSA? Create(string algName) { throw null; } public virtual byte[] Decrypt(byte[] data, System.Security.Cryptography.RSAEncryptionPadding padding) { throw null; } public virtual byte[] DecryptValue(byte[] rgb) { throw null; } public virtual byte[] Encrypt(byte[] data, System.Security.Cryptography.RSAEncryptionPadding padding) { throw null; } @@ -565,11 +565,11 @@ namespace System.Security.Cryptography public static System.Security.Cryptography.RSAEncryptionPadding OaepSHA512 { get { throw null; } } public static System.Security.Cryptography.RSAEncryptionPadding Pkcs1 { get { throw null; } } public static System.Security.Cryptography.RSAEncryptionPadding CreateOaep(System.Security.Cryptography.HashAlgorithmName hashAlgorithm) { throw null; } - public override bool Equals(object obj) { throw null; } - public bool Equals(System.Security.Cryptography.RSAEncryptionPadding other) { throw null; } + public override bool Equals(object? obj) { throw null; } + public bool Equals(System.Security.Cryptography.RSAEncryptionPadding? other) { throw null; } public override int GetHashCode() { throw null; } - public static bool operator ==(System.Security.Cryptography.RSAEncryptionPadding left, System.Security.Cryptography.RSAEncryptionPadding right) { throw null; } - public static bool operator !=(System.Security.Cryptography.RSAEncryptionPadding left, System.Security.Cryptography.RSAEncryptionPadding right) { throw null; } + public static bool operator ==(System.Security.Cryptography.RSAEncryptionPadding? left, System.Security.Cryptography.RSAEncryptionPadding? right) { throw null; } + public static bool operator !=(System.Security.Cryptography.RSAEncryptionPadding? left, System.Security.Cryptography.RSAEncryptionPadding? right) { throw null; } public override string ToString() { throw null; } } public enum RSAEncryptionPaddingMode @@ -581,7 +581,7 @@ namespace System.Security.Cryptography { public RSAOAEPKeyExchangeDeformatter() { } public RSAOAEPKeyExchangeDeformatter(System.Security.Cryptography.AsymmetricAlgorithm key) { } - public override string Parameters { get { throw null; } set { } } + public override string? Parameters { get { throw null; } set { } } public override byte[] DecryptKeyExchange(byte[] rgbData) { throw null; } public override void SetKey(System.Security.Cryptography.AsymmetricAlgorithm key) { } } @@ -589,30 +589,30 @@ namespace System.Security.Cryptography { public RSAOAEPKeyExchangeFormatter() { } public RSAOAEPKeyExchangeFormatter(System.Security.Cryptography.AsymmetricAlgorithm key) { } - public byte[] Parameter { get { throw null; } set { } } - public override string Parameters { get { throw null; } } - public System.Security.Cryptography.RandomNumberGenerator Rng { get { throw null; } set { } } + public byte[]? Parameter { get { throw null; } set { } } + public override string? Parameters { get { throw null; } } + public System.Security.Cryptography.RandomNumberGenerator? Rng { get { throw null; } set { } } public override byte[] CreateKeyExchange(byte[] rgbData) { throw null; } - public override byte[] CreateKeyExchange(byte[] rgbData, System.Type symAlgType) { throw null; } + public override byte[] CreateKeyExchange(byte[] rgbData, System.Type? symAlgType) { throw null; } public override void SetKey(System.Security.Cryptography.AsymmetricAlgorithm key) { } } public partial struct RSAParameters { - public byte[] D; - public byte[] DP; - public byte[] DQ; + public byte[]? D; + public byte[]? DP; + public byte[]? DQ; public byte[] Exponent; - public byte[] InverseQ; + public byte[]? InverseQ; public byte[] Modulus; - public byte[] P; - public byte[] Q; + public byte[]? P; + public byte[]? Q; } public partial class RSAPKCS1KeyExchangeDeformatter : System.Security.Cryptography.AsymmetricKeyExchangeDeformatter { public RSAPKCS1KeyExchangeDeformatter() { } public RSAPKCS1KeyExchangeDeformatter(System.Security.Cryptography.AsymmetricAlgorithm key) { } - public override string Parameters { get { throw null; } set { } } - public System.Security.Cryptography.RandomNumberGenerator RNG { get { throw null; } set { } } + public override string? Parameters { get { throw null; } set { } } + public System.Security.Cryptography.RandomNumberGenerator? RNG { get { throw null; } set { } } public override byte[] DecryptKeyExchange(byte[] rgbIn) { throw null; } public override void SetKey(System.Security.Cryptography.AsymmetricAlgorithm key) { } } @@ -621,9 +621,9 @@ namespace System.Security.Cryptography public RSAPKCS1KeyExchangeFormatter() { } public RSAPKCS1KeyExchangeFormatter(System.Security.Cryptography.AsymmetricAlgorithm key) { } public override string Parameters { get { throw null; } } - public System.Security.Cryptography.RandomNumberGenerator Rng { get { throw null; } set { } } + public System.Security.Cryptography.RandomNumberGenerator? Rng { get { throw null; } set { } } public override byte[] CreateKeyExchange(byte[] rgbData) { throw null; } - public override byte[] CreateKeyExchange(byte[] rgbData, System.Type symAlgType) { throw null; } + public override byte[] CreateKeyExchange(byte[] rgbData, System.Type? symAlgType) { throw null; } public override void SetKey(System.Security.Cryptography.AsymmetricAlgorithm key) { } } public partial class RSAPKCS1SignatureDeformatter : System.Security.Cryptography.AsymmetricSignatureDeformatter @@ -648,11 +648,11 @@ namespace System.Security.Cryptography public System.Security.Cryptography.RSASignaturePaddingMode Mode { get { throw null; } } public static System.Security.Cryptography.RSASignaturePadding Pkcs1 { get { throw null; } } public static System.Security.Cryptography.RSASignaturePadding Pss { get { throw null; } } - public override bool Equals(object obj) { throw null; } - public bool Equals(System.Security.Cryptography.RSASignaturePadding other) { throw null; } + public override bool Equals(object? obj) { throw null; } + public bool Equals(System.Security.Cryptography.RSASignaturePadding? other) { throw null; } public override int GetHashCode() { throw null; } - public static bool operator ==(System.Security.Cryptography.RSASignaturePadding left, System.Security.Cryptography.RSASignaturePadding right) { throw null; } - public static bool operator !=(System.Security.Cryptography.RSASignaturePadding left, System.Security.Cryptography.RSASignaturePadding right) { throw null; } + public static bool operator ==(System.Security.Cryptography.RSASignaturePadding? left, System.Security.Cryptography.RSASignaturePadding? right) { throw null; } + public static bool operator !=(System.Security.Cryptography.RSASignaturePadding? left, System.Security.Cryptography.RSASignaturePadding? right) { throw null; } public override string ToString() { throw null; } } public enum RSASignaturePaddingMode @@ -664,7 +664,7 @@ namespace System.Security.Cryptography { protected SHA1() { } public static new System.Security.Cryptography.SHA1 Create() { throw null; } - public static new System.Security.Cryptography.SHA1 Create(string hashName) { throw null; } + public static new System.Security.Cryptography.SHA1? Create(string hashName) { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public sealed partial class SHA1Managed : System.Security.Cryptography.SHA1 @@ -681,7 +681,7 @@ namespace System.Security.Cryptography { protected SHA256() { } public static new System.Security.Cryptography.SHA256 Create() { throw null; } - public static new System.Security.Cryptography.SHA256 Create(string hashName) { throw null; } + public static new System.Security.Cryptography.SHA256? Create(string hashName) { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public sealed partial class SHA256Managed : System.Security.Cryptography.SHA256 @@ -698,7 +698,7 @@ namespace System.Security.Cryptography { protected SHA384() { } public static new System.Security.Cryptography.SHA384 Create() { throw null; } - public static new System.Security.Cryptography.SHA384 Create(string hashName) { throw null; } + public static new System.Security.Cryptography.SHA384? Create(string hashName) { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public sealed partial class SHA384Managed : System.Security.Cryptography.SHA384 @@ -715,7 +715,7 @@ namespace System.Security.Cryptography { protected SHA512() { } public static new System.Security.Cryptography.SHA512 Create() { throw null; } - public static new System.Security.Cryptography.SHA512 Create(string hashName) { throw null; } + public static new System.Security.Cryptography.SHA512? Create(string hashName) { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public sealed partial class SHA512Managed : System.Security.Cryptography.SHA512 @@ -732,12 +732,12 @@ namespace System.Security.Cryptography { public SignatureDescription() { } public SignatureDescription(System.Security.SecurityElement el) { } - public string DeformatterAlgorithm { get { throw null; } set { } } - public string DigestAlgorithm { get { throw null; } set { } } - public string FormatterAlgorithm { get { throw null; } set { } } - public string KeyAlgorithm { get { throw null; } set { } } + public string? DeformatterAlgorithm { get { throw null; } set { } } + public string? DigestAlgorithm { get { throw null; } set { } } + public string? FormatterAlgorithm { get { throw null; } set { } } + public string? KeyAlgorithm { get { throw null; } set { } } public virtual System.Security.Cryptography.AsymmetricSignatureDeformatter CreateDeformatter(System.Security.Cryptography.AsymmetricAlgorithm key) { throw null; } - public virtual System.Security.Cryptography.HashAlgorithm CreateDigest() { throw null; } + public virtual System.Security.Cryptography.HashAlgorithm? CreateDigest() { throw null; } public virtual System.Security.Cryptography.AsymmetricSignatureFormatter CreateFormatter(System.Security.Cryptography.AsymmetricAlgorithm key) { throw null; } } public abstract partial class TripleDES : System.Security.Cryptography.SymmetricAlgorithm @@ -745,7 +745,7 @@ namespace System.Security.Cryptography protected TripleDES() { } public override byte[] Key { get { throw null; } set { } } public static new System.Security.Cryptography.TripleDES Create() { throw null; } - public static new System.Security.Cryptography.TripleDES Create(string str) { throw null; } + public static new System.Security.Cryptography.TripleDES? Create(string str) { throw null; } public static bool IsWeakKey(byte[] rgbKey) { throw null; } } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.csproj b/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.csproj index 9585cb4..db2463f 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.csproj +++ b/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent) + enable diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.OSX.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.OSX.cs index 410e200..ab83849 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.OSX.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.OSX.cs @@ -12,7 +12,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.Unix.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.Unix.cs index 9cedcc8..51b4ff1 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.Unix.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.Unix.cs @@ -13,7 +13,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.Windows.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.Windows.cs index 0da257a..b911377 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.Windows.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.Windows.cs @@ -13,7 +13,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.cs index 569afea..6eec09c 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.cs @@ -14,7 +14,7 @@ namespace Internal.Cryptography return CreateTransform(Key, IV, encrypting: false); } - public sealed override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) + public sealed override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: false); } @@ -24,7 +24,7 @@ namespace Internal.Cryptography return CreateTransform(Key, IV, encrypting: true); } - public sealed override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) + public sealed override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: true); } @@ -48,7 +48,7 @@ namespace Internal.Cryptography base.Dispose(disposing); } - private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting) + private ICryptoTransform CreateTransform(byte[] rgbKey, byte[]? rgbIV, bool encrypting) { // note: rbgIV is guaranteed to be cloned before this method, so no need to clone it again diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AppleCCCryptor.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AppleCCCryptor.cs index 8c1813b..fec06f9 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AppleCCCryptor.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AppleCCCryptor.cs @@ -11,14 +11,14 @@ namespace Internal.Cryptography internal sealed class AppleCCCryptor : BasicSymmetricCipher { private readonly bool _encrypting; - private SafeAppleCryptorHandle _cryptor; + private SafeAppleCryptorHandle _cryptor = null!; public AppleCCCryptor( Interop.AppleCrypto.PAL_SymmetricAlgorithm algorithm, CipherMode cipherMode, int blockSizeInBytes, byte[] key, - byte[] iv, + byte[]? iv, bool encrypting) : base(cipherMode.GetCipherIv(iv), blockSizeInBytes) { @@ -32,7 +32,7 @@ namespace Internal.Cryptography if (disposing) { _cryptor?.Dispose(); - _cryptor = null; + _cryptor = null!; } base.Dispose(disposing); @@ -149,7 +149,7 @@ namespace Internal.Cryptography int ret; int ccStatus; - byte[] iv = IV; + byte[]? iv = IV; fixed (byte* pbKey = key) fixed (byte* pbIv = iv) @@ -190,7 +190,7 @@ namespace Internal.Cryptography int ret; int ccStatus; - byte[] iv = IV; + byte[]? iv = IV; fixed (byte* pbIv = iv) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.OSX.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.OSX.cs index e52ab6b..560de5e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.OSX.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.OSX.cs @@ -12,7 +12,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.Unix.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.Unix.cs index 24de909..5da0d0c 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.Unix.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.Unix.cs @@ -13,7 +13,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.Windows.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.Windows.cs index 047f0ac..df95032 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.Windows.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.Windows.cs @@ -13,7 +13,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.cs index 11ab5f9..cf6142a 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/DesImplementation.cs @@ -17,7 +17,7 @@ namespace Internal.Cryptography return CreateTransform(Key, IV, encrypting: false); } - public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: false); } @@ -27,7 +27,7 @@ namespace Internal.Cryptography return CreateTransform(Key, IV, encrypting: true); } - public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: true); } @@ -51,7 +51,7 @@ namespace Internal.Cryptography KeyValue = key; } - private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting) + private ICryptoTransform CreateTransform(byte[] rgbKey, byte[]? rgbIV, bool encrypting) { // note: rgbIV is guaranteed to be cloned before this method, so no need to clone it again diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HMACCommon.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HMACCommon.cs index 63f3b8b..0310801 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HMACCommon.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HMACCommon.cs @@ -52,9 +52,9 @@ namespace Internal.Cryptography ActualKey = ChangeKeyImpl(key); } - private byte[] ChangeKeyImpl(ReadOnlySpan key) + private byte[]? ChangeKeyImpl(ReadOnlySpan key) { - byte[] modifiedKey = null; + byte[]? modifiedKey = null; // If _blockSize is -1 the key isn't going to be extractable by the object holder, // so there's no point in recalculating it in managed code. @@ -70,7 +70,7 @@ namespace Internal.Cryptography } HashProvider oldHashProvider = _hMacProvider; - _hMacProvider = null; + _hMacProvider = null!; oldHashProvider?.Dispose(true); _hMacProvider = HashProviderDispenser.CreateMacProvider(_hashAlgorithmId, key); @@ -79,7 +79,7 @@ namespace Internal.Cryptography // The actual key used for hashing. This will not be the same as the original key passed to ChangeKey() if the original key exceeded the // hash algorithm's block size. (See RFC 2104, section 2) - public byte[] ActualKey { get; private set; } + public byte[]? ActualKey { get; private set; } // Adds new data to be hashed. This can be called repeatedly in order to hash data from noncontiguous sources. public void AppendHashData(byte[] data, int offset, int count) => @@ -100,13 +100,13 @@ namespace Internal.Cryptography if (disposing && _hMacProvider != null) { _hMacProvider.Dispose(true); - _hMacProvider = null; + _hMacProvider = null!; } } private readonly string _hashAlgorithmId; - private HashProvider _hMacProvider; - private volatile HashProvider _lazyHashProvider; + private HashProvider _hMacProvider = null!; // Initialized in helper + private volatile HashProvider? _lazyHashProvider; private readonly int _blockSize; } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashAlgorithmNames.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashAlgorithmNames.cs index 5da5e17..a7a3e38 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashAlgorithmNames.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashAlgorithmNames.cs @@ -22,7 +22,7 @@ namespace Internal.Cryptography /// /// Map HashAlgorithm type to string; .NET Framework uses CryptoConfig functionality. /// - public static string ToAlgorithmName(this HashAlgorithm hashAlgorithm) + public static string? ToAlgorithmName(this HashAlgorithm hashAlgorithm) { if (hashAlgorithm is SHA1) return HashAlgorithmNames.SHA1; diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs index 1c85f79..6216dbe 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs @@ -171,7 +171,7 @@ namespace Internal.Cryptography if (disposing && _hmacCtx != null) { _hmacCtx.Dispose(); - _hmacCtx = null; + _hmacCtx = null!; } } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/Helpers.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/Helpers.cs index 19bdfd8..5ca3a10 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/Helpers.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/Helpers.cs @@ -17,7 +17,7 @@ namespace Internal.Cryptography return cipherMode != CipherMode.ECB; } - public static byte[] GetCipherIv(this CipherMode cipherMode, byte[] iv) + public static byte[]? GetCipherIv(this CipherMode cipherMode, byte[]? iv) { if (cipherMode.UsesIv()) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/OpenSslCipher.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/OpenSslCipher.cs index f625574..b43bfbc 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/OpenSslCipher.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/OpenSslCipher.cs @@ -14,9 +14,9 @@ namespace Internal.Cryptography internal class OpenSslCipher : BasicSymmetricCipher { private readonly bool _encrypting; - private SafeEvpCipherCtxHandle _ctx; + private SafeEvpCipherCtxHandle _ctx = null!; - public OpenSslCipher(IntPtr algorithm, CipherMode cipherMode, int blockSizeInBytes, byte[] key, int effectiveKeyLength, byte[] iv, bool encrypting) + public OpenSslCipher(IntPtr algorithm, CipherMode cipherMode, int blockSizeInBytes, byte[] key, int effectiveKeyLength, byte[]? iv, bool encrypting) : base(cipherMode.GetCipherIv(iv), blockSizeInBytes) { Debug.Assert(algorithm != IntPtr.Zero); @@ -33,7 +33,7 @@ namespace Internal.Cryptography if (_ctx != null) { _ctx.Dispose(); - _ctx = null; + _ctx = null!; } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.OSX.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.OSX.cs index 3e0012d..2cfc98b 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.OSX.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.OSX.cs @@ -13,7 +13,7 @@ namespace Internal.Cryptography PaddingMode paddingMode, byte[] key, int effectiveKeyLength, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Unix.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Unix.cs index 30118f4..efd6cbe 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Unix.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Unix.cs @@ -15,7 +15,7 @@ namespace Internal.Cryptography PaddingMode paddingMode, byte[] key, int effectiveKeyLength, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Windows.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Windows.cs index fe0f2dd..e5c04cb 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Windows.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Windows.cs @@ -15,7 +15,7 @@ namespace Internal.Cryptography PaddingMode paddingMode, byte[] key, int effectiveKeyLength, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.cs index 74f333b..43ab028 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.cs @@ -30,7 +30,7 @@ namespace Internal.Cryptography return CreateTransform(Key, IV, encrypting: false); } - public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: false); } @@ -40,7 +40,7 @@ namespace Internal.Cryptography return CreateTransform(Key, IV, encrypting: true); } - public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: true); } @@ -59,7 +59,7 @@ namespace Internal.Cryptography Key = key; } - private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting) + private ICryptoTransform CreateTransform(byte[] rgbKey, byte[]? rgbIV, bool encrypting) { // note: rgbIV is guaranteed to be cloned before this method, so no need to clone it again diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RijndaelImplementation.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RijndaelImplementation.cs index 8f59d14..938f322 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RijndaelImplementation.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RijndaelImplementation.cs @@ -74,9 +74,9 @@ namespace Internal.Cryptography public override KeySizes[] LegalKeySizes => _impl.LegalKeySizes; public override ICryptoTransform CreateEncryptor() => _impl.CreateEncryptor(); - public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) => _impl.CreateEncryptor(rgbKey, rgbIV); + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) => _impl.CreateEncryptor(rgbKey, rgbIV); public override ICryptoTransform CreateDecryptor() => _impl.CreateDecryptor(); - public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) => _impl.CreateDecryptor(rgbKey, rgbIV); + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) => _impl.CreateDecryptor(rgbKey, rgbIV); public override void GenerateIV() => _impl.GenerateIV(); public override void GenerateKey() => _impl.GenerateKey(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.OSX.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.OSX.cs index 126ae37..b52479f 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.OSX.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.OSX.cs @@ -12,7 +12,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.Unix.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.Unix.cs index 0cbb325..1d5c6b6 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.Unix.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.Unix.cs @@ -13,7 +13,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.Windows.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.Windows.cs index ca80377..c43cb9e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.Windows.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.Windows.cs @@ -13,7 +13,7 @@ namespace Internal.Cryptography CipherMode cipherMode, PaddingMode paddingMode, byte[] key, - byte[] iv, + byte[]? iv, int blockSize, bool encrypting) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.cs index da1d4d0..c4c42f5 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/TripleDesImplementation.cs @@ -17,7 +17,7 @@ namespace Internal.Cryptography return CreateTransform(Key, IV, encrypting: false); } - public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: false); } @@ -27,7 +27,7 @@ namespace Internal.Cryptography return CreateTransform(Key, IV, encrypting: true); } - public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: true); } @@ -46,7 +46,7 @@ namespace Internal.Cryptography Key = key; } - private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting) + private ICryptoTransform CreateTransform(byte[] rgbKey, byte[]? rgbIV, bool encrypting) { // note: rgbIV is guaranteed to be cloned before this method, so no need to clone it again diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj b/src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj index ff88d7d..1303909 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj @@ -4,6 +4,7 @@ $(DefineConstants);INTERNAL_ASYMMETRIC_IMPLEMENTATIONS CS1573;CS3016;CA5350;CA5351;CA5379;CA5384;CA5385;$(NoWarn) $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-OSX + enable diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Aes.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Aes.cs index d3f1d75..a1d785f 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Aes.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Aes.cs @@ -24,9 +24,9 @@ namespace System.Security.Cryptography return new AesImplementation(); } - public static new Aes Create(string algorithmName) + public static new Aes? Create(string algorithmName) { - return (Aes)CryptoConfig.CreateFromName(algorithmName); + return (Aes?)CryptoConfig.CreateFromName(algorithmName); } private static readonly KeySizes[] s_legalBlockSizes = { new KeySizes(128, 128, 0) }; diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Unix.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Unix.cs index 835048a..3c61b09 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Unix.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Unix.cs @@ -9,7 +9,7 @@ namespace System.Security.Cryptography { public sealed partial class AesCcm { - private byte[] _key; + private byte[] _key = null!; private void ImportKey(ReadOnlySpan key) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Windows.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Windows.cs index b16acd3..451a80a 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Windows.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Windows.cs @@ -10,7 +10,7 @@ namespace System.Security.Cryptography public sealed partial class AesCcm { private static readonly SafeAlgorithmHandle s_aesCcm = AesBCryptModes.OpenAesAlgorithm(Cng.BCRYPT_CHAIN_MODE_CCM); - private SafeKeyHandle _keyHandle; + private SafeKeyHandle _keyHandle = null!; // Always initialized in helper private void ImportKey(ReadOnlySpan key) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.cs index 21d55e3..f9f00bc 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.cs @@ -26,7 +26,7 @@ namespace System.Security.Cryptography ImportKey(key); } - public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[] associatedData = null) + public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null) { AesAEAD.CheckArgumentsForNull(nonce, plaintext, ciphertext, tag); Encrypt((ReadOnlySpan)nonce, plaintext, ciphertext, tag, associatedData); @@ -43,7 +43,7 @@ namespace System.Security.Cryptography EncryptInternal(nonce, plaintext, ciphertext, tag, associatedData); } - public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[] associatedData = null) + public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData = null) { AesAEAD.CheckArgumentsForNull(nonce, plaintext, ciphertext, tag); Decrypt((ReadOnlySpan)nonce, ciphertext, tag, plaintext, associatedData); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.Unix.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.Unix.cs index 702d5a6..bf04741 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.Unix.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.Unix.cs @@ -9,7 +9,7 @@ namespace System.Security.Cryptography { public sealed partial class AesGcm { - private SafeEvpCipherCtxHandle _ctxHandle; + private SafeEvpCipherCtxHandle _ctxHandle = null!; private void ImportKey(ReadOnlySpan key) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.Windows.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.Windows.cs index 9ee976d..ca5b435 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.Windows.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.Windows.cs @@ -10,7 +10,7 @@ namespace System.Security.Cryptography public partial class AesGcm { private static readonly SafeAlgorithmHandle s_aesGcm = AesBCryptModes.OpenAesAlgorithm(Cng.BCRYPT_CHAIN_MODE_GCM); - private SafeKeyHandle _keyHandle; + private SafeKeyHandle _keyHandle = null!; // Always initialized in helper private void ImportKey(ReadOnlySpan key) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.cs index 5295d05..bbda05e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.cs @@ -27,7 +27,7 @@ namespace System.Security.Cryptography ImportKey(key); } - public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[] associatedData = null) + public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null) { AesAEAD.CheckArgumentsForNull(nonce, plaintext, ciphertext, tag); Encrypt((ReadOnlySpan)nonce, plaintext, ciphertext, tag, associatedData); @@ -44,7 +44,7 @@ namespace System.Security.Cryptography EncryptInternal(nonce, plaintext, ciphertext, tag, associatedData); } - public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[] associatedData = null) + public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData = null) { AesAEAD.CheckArgumentsForNull(nonce, plaintext, ciphertext, tag); Decrypt((ReadOnlySpan)nonce, ciphertext, tag, plaintext, associatedData); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesManaged.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesManaged.cs index a48dc3b..e267fa1 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesManaged.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesManaged.cs @@ -61,9 +61,9 @@ namespace System.Security.Cryptography public override KeySizes[] LegalBlockSizes => _impl.LegalBlockSizes; public override KeySizes[] LegalKeySizes => _impl.LegalKeySizes; public override ICryptoTransform CreateEncryptor() => _impl.CreateEncryptor(); - public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) => _impl.CreateEncryptor(rgbKey, rgbIV); + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) => _impl.CreateEncryptor(rgbKey, rgbIV); public override ICryptoTransform CreateDecryptor() => _impl.CreateDecryptor(); - public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) => _impl.CreateDecryptor(rgbKey, rgbIV); + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) => _impl.CreateDecryptor(rgbKey, rgbIV); public override void GenerateIV() => _impl.GenerateIV(); public override void GenerateKey() => _impl.GenerateKey(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricKeyExchangeDeformatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricKeyExchangeDeformatter.cs index da3dfac..bef8420 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricKeyExchangeDeformatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricKeyExchangeDeformatter.cs @@ -7,7 +7,7 @@ namespace System.Security.Cryptography public abstract class AsymmetricKeyExchangeDeformatter { protected AsymmetricKeyExchangeDeformatter() { } - public abstract string Parameters { get; set; } + public abstract string? Parameters { get; set; } public abstract void SetKey(AsymmetricAlgorithm key); public abstract byte[] DecryptKeyExchange(byte[] rgb); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricKeyExchangeFormatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricKeyExchangeFormatter.cs index a1022a4..5c3eb01 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricKeyExchangeFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricKeyExchangeFormatter.cs @@ -8,12 +8,12 @@ namespace System.Security.Cryptography { protected AsymmetricKeyExchangeFormatter() { } - public abstract string Parameters { get; } + public abstract string? Parameters { get; } public abstract void SetKey(AsymmetricAlgorithm key); public abstract byte[] CreateKeyExchange(byte[] data); // For .NET Framework compat, keep this even though symAlgType is not used. - public abstract byte[] CreateKeyExchange(byte[] data, Type symAlgType); + public abstract byte[] CreateKeyExchange(byte[] data, Type? symAlgType); } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricSignatureDeformatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricSignatureDeformatter.cs index 91feca9..4e216e4 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricSignatureDeformatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricSignatureDeformatter.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; using Internal.Cryptography; namespace System.Security.Cryptography @@ -18,7 +19,8 @@ namespace System.Security.Cryptography if (hash == null) throw new ArgumentNullException(nameof(hash)); - SetHashAlgorithm(hash.ToAlgorithmName()); + SetHashAlgorithm(hash.ToAlgorithmName()!); + Debug.Assert(hash.Hash != null); return VerifySignature(hash.Hash, rgbSignature); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricSignatureFormatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricSignatureFormatter.cs index ec2bcbd..81c3e05 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricSignatureFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AsymmetricSignatureFormatter.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; using Internal.Cryptography; namespace System.Security.Cryptography @@ -18,7 +19,8 @@ namespace System.Security.Cryptography if (hash == null) throw new ArgumentNullException(nameof(hash)); - SetHashAlgorithm(hash.ToAlgorithmName()); + SetHashAlgorithm(hash.ToAlgorithmName()!); + Debug.Assert(hash.Hash != null); return CreateSignature(hash.Hash); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CngKeyLite.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CngKeyLite.cs index 28cab72..77498b6 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CngKeyLite.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CngKeyLite.cs @@ -239,10 +239,10 @@ namespace System.Security.Cryptography kdfCount, Span.Empty, out _, - out byte[] allocated); + out byte[]? allocated); Debug.Assert(ret); - return allocated; + return allocated!; } internal static bool TryExportPkcs8KeyBlob( @@ -273,7 +273,7 @@ namespace System.Security.Cryptography int kdfCount, Span destination, out int bytesWritten, - out byte[] allocated) + out byte[]? allocated) { using (SafeUnicodeStringHandle stringHandle = new SafeUnicodeStringHandle(password)) { @@ -568,7 +568,7 @@ namespace System.Security.Cryptography /// null - if property not defined on key. /// throws - for any other type of error. /// - private static byte[] GetProperty(SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) + private static byte[]? GetProperty(SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) { Debug.Assert(!ncryptHandle.IsInvalid); unsafe @@ -599,10 +599,10 @@ namespace System.Security.Cryptography /// Retrieve a well-known CNG string property. (Note: .NET Framework compat: this helper likes to return special values rather than throw exceptions for missing /// or ill-formatted property values. Only use it for well-known properties that are unlikely to be ill-formatted.) /// - internal static string GetPropertyAsString(SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) + internal static string? GetPropertyAsString(SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) { Debug.Assert(!ncryptHandle.IsInvalid); - byte[] value = GetProperty(ncryptHandle, propertyName, options); + byte[]? value = GetProperty(ncryptHandle, propertyName, options); if (value == null) return null; // .NET Framework compat: return null if key not present. if (value.Length == 0) @@ -612,13 +612,13 @@ namespace System.Security.Cryptography { fixed (byte* pValue = &value[0]) { - string valueAsString = Marshal.PtrToStringUni((IntPtr)pValue); + string valueAsString = Marshal.PtrToStringUni((IntPtr)pValue)!; return valueAsString; } } } - internal static string GetCurveName(SafeNCryptHandle ncryptHandle) + internal static string? GetCurveName(SafeNCryptHandle ncryptHandle) { Debug.Assert(!ncryptHandle.IsInvalid); return GetPropertyAsString(ncryptHandle, KeyPropertyName.ECCCurveName, CngPropertyOptions.None); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CngPkcs8.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CngPkcs8.cs index 88bc955..8037b65 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CngPkcs8.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CngPkcs8.cs @@ -12,7 +12,7 @@ namespace System.Security.Cryptography { internal SafeNCryptKeyHandle KeyHandle; - internal string GetAlgorithmGroup() + internal string? GetAlgorithmGroup() { return CngKeyLite.GetPropertyAsString( KeyHandle, diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs index da12745..3d9583c 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs @@ -33,8 +33,8 @@ namespace System.Security.Cryptography private const string ECDsaIdentifier = "ECDsa"; - private static volatile Dictionary s_defaultOidHT; - private static volatile Dictionary s_defaultNameHT; + private static volatile Dictionary? s_defaultOidHT; + private static volatile Dictionary? s_defaultNameHT; private static readonly ConcurrentDictionary appNameHT = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); private static readonly ConcurrentDictionary appOidHT = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); @@ -326,18 +326,18 @@ namespace System.Security.Cryptography } } - public static object CreateFromName(string name, params object[] args) + public static object? CreateFromName(string name, params object?[]? args) { if (name == null) throw new ArgumentNullException(nameof(name)); // Check to see if we have an application defined mapping - appNameHT.TryGetValue(name, out Type retvalType); + appNameHT.TryGetValue(name, out Type? retvalType); // We allow the default table to Types and Strings // Types get used for types in .Algorithms assembly. // strings get used for delay-loaded stuff in other assemblies such as .Csp. - if (retvalType == null && DefaultNameHT.TryGetValue(name, out object retvalObj)) + if (retvalType == null && DefaultNameHT.TryGetValue(name, out object? retvalObj)) { retvalType = retvalObj as Type; @@ -419,14 +419,14 @@ namespace System.Security.Cryptography cons = candidates.ToArray(); // Bind to matching ctor. - ConstructorInfo rci = Type.DefaultBinder.BindToMethod( + ConstructorInfo? rci = Type.DefaultBinder.BindToMethod( ConstructorDefault, cons, ref args, null, null, null, - out object state) as ConstructorInfo; + out object? state) as ConstructorInfo; // Check for ctor we don't like (non-existent, delegate or decorated with declarative linktime demand). if (rci == null || typeof(Delegate).IsAssignableFrom(rci.DeclaringType)) @@ -446,7 +446,7 @@ namespace System.Security.Cryptography return retval; } - public static object CreateFromName(string name) + public static object? CreateFromName(string name) { return CreateFromName(name, null); } @@ -478,12 +478,12 @@ namespace System.Security.Cryptography } } - public static string MapNameToOID(string name) + public static string? MapNameToOID(string name) { if (name == null) throw new ArgumentNullException(nameof(name)); - appOidHT.TryGetValue(name, out string oidName); + appOidHT.TryGetValue(name, out string? oidName); if (string.IsNullOrEmpty(oidName) && !DefaultOidHT.TryGetValue(name, out oidName)) { @@ -547,7 +547,7 @@ namespace System.Security.Cryptography return encodedOidNums; } - private static void EncodeSingleOidNum(uint value, byte[] destination, ref int index) + private static void EncodeSingleOidNum(uint value, byte[]? destination, ref int index) { // Write directly to destination starting at index, and update index based on how many bytes written. // If destination is null, just return updated index. diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DES.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DES.cs index 5bc0088..443cc84 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DES.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DES.cs @@ -24,9 +24,9 @@ namespace System.Security.Cryptography return new DesImplementation(); } - public static new DES Create(string algName) + public static new DES? Create(string algName) { - return (DES)CryptoConfig.CreateFromName(algName); + return (DES?)CryptoConfig.CreateFromName(algName); } public override byte[] Key @@ -103,7 +103,7 @@ namespace System.Security.Cryptography return false; } - private static bool IsLegalKeySize(byte[] rgbKey) + private static bool IsLegalKeySize(byte[]? rgbKey) { if (rgbKey != null && rgbKey.Length == 8) return true; diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.Xml.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.Xml.cs index 2cab2ac..cfa242d 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.Xml.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; using System.Text; namespace System.Security.Cryptography @@ -15,7 +16,7 @@ namespace System.Security.Cryptography string name, int sizeHint = -1) { - byte[] ret = XmlKeyHelper.ReadCryptoBinary(ref state, name, sizeHint); + byte[]? ret = XmlKeyHelper.ReadCryptoBinary(ref state, name, sizeHint); if (ret == null) { @@ -35,10 +36,10 @@ namespace System.Security.Cryptography byte[] q = ReadRequiredElement(ref state, nameof(DSAParameters.Q)); byte[] g = ReadRequiredElement(ref state, nameof(DSAParameters.G), p.Length); byte[] y = ReadRequiredElement(ref state, nameof(DSAParameters.Y), p.Length); - byte[] j = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(DSAParameters.J)); - byte[] seed = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(DSAParameters.Seed)); + byte[]? j = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(DSAParameters.J)); + byte[]? seed = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(DSAParameters.Seed)); int counter = 0; - byte[] x = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(DSAParameters.X), q.Length); + byte[]? x = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(DSAParameters.X), q.Length); if (seed != null) { @@ -103,6 +104,7 @@ namespace System.Security.Cryptography // StringBuilder to need to grow. DSAParameters keyParameters = ExportParameters(includePrivateParameters); + Debug.Assert(keyParameters.P != null); StringBuilder builder = new StringBuilder((keyParameters.P.Length << 1) / 3); builder.Append(""); XmlKeyHelper.WriteCryptoBinary(nameof(DSAParameters.P), keyParameters.P, builder); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.cs index bf67d51..23391a0 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.cs @@ -20,9 +20,9 @@ namespace System.Security.Cryptography protected DSA() { } - public static new DSA Create(string algName) + public static new DSA? Create(string algName) { - return (DSA)CryptoConfig.CreateFromName(algName); + return (DSA?)CryptoConfig.CreateFromName(algName); } public static DSA Create(int keySizeInBits) diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSACng.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSACng.cs index 6c05638..f689c20 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSACng.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSACng.cs @@ -19,7 +19,7 @@ namespace System.Security.Cryptography { public sealed partial class DSACng : DSA { - private SafeNCryptKeyHandle _keyHandle; + private SafeNCryptKeyHandle? _keyHandle; private int _lastKeySize; private bool _disposed; @@ -50,7 +50,7 @@ namespace System.Security.Cryptography _lastKeySize = keySize; } - return new DuplicateSafeNCryptKeyHandle(_keyHandle); + return new DuplicateSafeNCryptKeyHandle(_keyHandle!); } private byte[] ExportKeyBlob(bool includePrivateParameters) diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSAParameters.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSAParameters.cs index 1c12257..397d00e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSAParameters.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSAParameters.cs @@ -9,13 +9,13 @@ namespace System.Security.Cryptography // so you cannot accidently send it along with the public parameters. public struct DSAParameters { - public byte[] P; - public byte[] Q; - public byte[] G; - public byte[] Y; - public byte[] J; - public byte[] X; - public byte[] Seed; + public byte[]? P; + public byte[]? Q; + public byte[]? G; + public byte[]? Y; + public byte[]? J; + public byte[]? X; + public byte[]? Seed; public int Counter; } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSASignatureDeformatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSASignatureDeformatter.cs index eec7918..64c4566 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSASignatureDeformatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSASignatureDeformatter.cs @@ -8,7 +8,7 @@ namespace System.Security.Cryptography { public class DSASignatureDeformatter : AsymmetricSignatureDeformatter { - private DSA _dsaKey; + private DSA? _dsaKey; public DSASignatureDeformatter() { } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSASignatureFormatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSASignatureFormatter.cs index d14d38d..8da4420 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSASignatureFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSASignatureFormatter.cs @@ -8,7 +8,7 @@ namespace System.Security.Cryptography { public class DSASignatureFormatter : AsymmetricSignatureFormatter { - private DSA _dsaKey; + private DSA? _dsaKey; public DSASignatureFormatter() { } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCngKey.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCngKey.cs index 47dc3a2..8eb59e0 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCngKey.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCngKey.cs @@ -10,9 +10,9 @@ namespace System.Security.Cryptography { internal sealed partial class ECCngKey { - private SafeNCryptKeyHandle _keyHandle; + private SafeNCryptKeyHandle? _keyHandle; private int _lastKeySize; - private string _lastAlgorithm; + private string? _lastAlgorithm; private bool _disposed; private readonly string _algorithmGroup; private readonly string _disposedName; @@ -29,12 +29,12 @@ namespace System.Security.Cryptography internal int KeySize { get; private set; } - internal string GetCurveName(int callerKeySizeProperty, out string oidValue) + internal string? GetCurveName(int callerKeySizeProperty, out string? oidValue) { // Ensure key\handle is created using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle(callerKeySizeProperty)) { - string algorithm = _lastAlgorithm; + string? algorithm = _lastAlgorithm; if (ECCng.IsECNamedCurve(algorithm)) { @@ -54,7 +54,7 @@ namespace System.Security.Cryptography if (ECCng.IsECNamedCurve(_lastAlgorithm)) { // Curve was previously created, so use that - return new DuplicateSafeNCryptKeyHandle(_keyHandle); + return new DuplicateSafeNCryptKeyHandle(_keyHandle!); } else { @@ -98,7 +98,7 @@ namespace System.Security.Cryptography KeySize = callerKeySizeProperty; } - return new DuplicateSafeNCryptKeyHandle(_keyHandle); + return new DuplicateSafeNCryptKeyHandle(_keyHandle!); } } @@ -112,7 +112,7 @@ namespace System.Security.Cryptography DisposeKey(); } - string algorithm = null; + string algorithm; int keySize = 0; if (curve.IsNamed) @@ -216,7 +216,7 @@ namespace System.Security.Cryptography _lastKeySize = 0; } - internal void SetHandle(SafeNCryptKeyHandle keyHandle, string algorithmName) + internal void SetHandle(SafeNCryptKeyHandle keyHandle, string? algorithmName) { ThrowIfDisposed(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCurve.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCurve.cs index 40b7683..c69fee8 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCurve.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCurve.cs @@ -19,12 +19,12 @@ namespace System.Security.Cryptography /// /// Coefficient A. Applies only to Explicit curves. /// - public byte[] A; + public byte[]? A; /// /// Coefficient B. Applies only to Explicit curves. /// - public byte[] B; + public byte[]? B; /// /// Base Point. Applies only to Explicit curves. @@ -34,17 +34,17 @@ namespace System.Security.Cryptography /// /// Order of the group generated by G = (x,y). Applies only to Explicit curves. /// - public byte[] Order; + public byte[]? Order; /// /// Cofactor (optional). Applies only to Explicit curves. /// - public byte[] Cofactor; + public byte[]? Cofactor; /// /// Seed of the curve (optional). Applies only to Explicit curves. /// - public byte[] Seed; + public byte[]? Seed; /// /// Curve Type. @@ -59,12 +59,12 @@ namespace System.Security.Cryptography /// /// The binary polynomial. Applies only to Characteristic2 curves. /// - public byte[] Polynomial; + public byte[]? Polynomial; /// /// The prime specifying the base field. Applies only to Prime curves. /// - public byte[] Prime; + public byte[]? Prime; private Oid _oid; /// @@ -140,9 +140,9 @@ namespace System.Security.Cryptography return ECCurve.CreateFromValueAndName(oidValue, null); } - private static ECCurve CreateFromValueAndName(string oidValue, string oidFriendlyName) + private static ECCurve CreateFromValueAndName(string? oidValue, string? oidFriendlyName) { - Oid oid = null; + Oid? oid = null; if (oidValue == null && oidFriendlyName != null) { @@ -232,7 +232,7 @@ namespace System.Security.Cryptography { if (!hasErrors) { - if (Prime == null || Prime.Length != A.Length) + if (Prime == null || Prime.Length != A!.Length) { hasErrors = true; } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellman.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellman.cs index 7804055..6109ae3 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellman.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellman.cs @@ -23,12 +23,12 @@ namespace System.Security.Cryptography get { return "ECDiffieHellman"; } } - public override string SignatureAlgorithm + public override string? SignatureAlgorithm { get { return null; } } - public static new ECDiffieHellman Create(string algorithm) + public static new ECDiffieHellman? Create(string algorithm) { if (algorithm == null) { @@ -71,8 +71,8 @@ namespace System.Security.Cryptography public virtual byte[] DeriveKeyFromHash( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] secretPrepend, - byte[] secretAppend) + byte[]? secretPrepend, + byte[]? secretAppend) { throw DerivedClassMustOverride(); } @@ -89,7 +89,7 @@ namespace System.Security.Cryptography public byte[] DeriveKeyFromHmac( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] hmacKey) + byte[]? hmacKey) { return DeriveKeyFromHmac(otherPartyPublicKey, hashAlgorithm, hmacKey, null, null); } @@ -108,9 +108,9 @@ namespace System.Security.Cryptography public virtual byte[] DeriveKeyFromHmac( ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, - byte[] hmacKey, - byte[] secretPrepend, - byte[] secretAppend) + byte[]? hmacKey, + byte[]? secretPrepend, + byte[]? secretAppend) { throw DerivedClassMustOverride(); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCng.Derive.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCng.Derive.cs index 71d99dc..dedd791 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCng.Derive.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCng.Derive.cs @@ -45,7 +45,7 @@ namespace System.Security.Cryptography using (ECDiffieHellmanCng otherPartyCng = (ECDiffieHellmanCng)Create(otherPartyParameters)) using (SafeNCryptKeyHandle otherPartyHandle = otherPartyCng.GetDuplicatedKeyHandle()) { - string importedKeyAlgorithmGroup = + string? importedKeyAlgorithmGroup = CngKeyLite.GetPropertyAsString( otherPartyHandle, CngKeyLite.KeyPropertyName.AlgorithmGroup, diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs index 0c6f09b..a41d055 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs @@ -13,7 +13,7 @@ namespace System.Security.Cryptography { private readonly ECCngKey _key = new ECCngKey(BCryptNative.AlgorithmName.ECDH, nameof(ECDiffieHellman)); - private string GetCurveName(out string oidValue) => _key.GetCurveName(KeySize, out oidValue); + private string? GetCurveName(out string? oidValue) => _key.GetCurveName(KeySize, out oidValue); public override void GenerateKey(ECCurve curve) { @@ -32,7 +32,7 @@ namespace System.Security.Cryptography { get { - string curveName = GetCurveName(out _); + string? curveName = GetCurveName(out _); return new ECDiffieHellmanCngPublicKey( curveName == null diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs index e355c85..4990309 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs @@ -11,11 +11,11 @@ namespace System.Security.Cryptography public sealed partial class ECDiffieHellmanCngPublicKey : ECDiffieHellmanPublicKey { private byte[] _keyBlob; - internal string _curveName; + internal string? _curveName; protected override void Dispose(bool disposing) { - _keyBlob = null; + _keyBlob = null!; base.Dispose(disposing); } @@ -29,7 +29,7 @@ namespace System.Security.Cryptography throw new PlatformNotSupportedException(); } - internal ECDiffieHellmanCngPublicKey(byte[] keyBlob, string curveName) : base(keyBlob) + internal ECDiffieHellmanCngPublicKey(byte[] keyBlob, string? curveName) : base(keyBlob) { Debug.Assert(keyBlob != null); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanPublicKey.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanPublicKey.cs index a8ac341..a07f99f 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanPublicKey.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanPublicKey.cs @@ -23,7 +23,7 @@ namespace System.Security.Cryptography throw new ArgumentNullException(nameof(keyBlob)); } - _keyBlob = keyBlob.Clone() as byte[]; + _keyBlob = (byte[])keyBlob.Clone(); } public void Dispose() @@ -35,7 +35,7 @@ namespace System.Security.Cryptography public virtual byte[] ToByteArray() { - return _keyBlob.Clone() as byte[]; + return (byte[])_keyBlob.Clone(); } // This method must be implemented by derived classes. In order to conform to the contract, it cannot be abstract. diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsa.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsa.cs index 2e1fae7..9d04547 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsa.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsa.cs @@ -18,7 +18,7 @@ namespace System.Security.Cryptography protected ECDsa() { } - public static new ECDsa Create(string algorithm) + public static new ECDsa? Create(string algorithm) { if (algorithm == null) { @@ -196,7 +196,7 @@ namespace System.Security.Cryptography public abstract byte[] SignHash(byte[] hash); public abstract bool VerifyHash(byte[] hash, byte[] signature); - public override string KeyExchangeAlgorithm => null; + public override string? KeyExchangeAlgorithm => null; public override string SignatureAlgorithm => "ECDsa"; protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsaCng.Key.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsaCng.Key.cs index 96b7503..83f50f9 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsaCng.Key.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDsaCng.Key.cs @@ -13,7 +13,7 @@ namespace System.Security.Cryptography { private readonly ECCngKey _key = new ECCngKey(AlgorithmName.ECDsa, nameof(ECDsa)); - private string GetCurveName(out string oidValue) => _key.GetCurveName(KeySize, out oidValue); + private string? GetCurveName(out string? oidValue) => _key.GetCurveName(KeySize, out oidValue); public override void GenerateKey(ECCurve curve) { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECParameters.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECParameters.cs index ee305ce..f325ba6 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECParameters.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECParameters.cs @@ -21,7 +21,7 @@ namespace System.Security.Cryptography /// /// Private Key. Not always present. /// - public byte[] D; + public byte[]? D; /// /// The Curve. @@ -50,12 +50,12 @@ namespace System.Security.Cryptography if (Curve.IsExplicit) { // Explicit curves require D length to match Curve.Order - hasErrors = (D != null && (D.Length != Curve.Order.Length)); + hasErrors = (D != null && (D.Length != Curve.Order!.Length)); } else if (Curve.IsNamed) { // Named curves require D length to match Q.X and Q.Y - hasErrors = (D != null && (D.Length != Q.X.Length)); + hasErrors = (D != null && (D.Length != Q.X!.Length)); } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECPoint.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECPoint.cs index 71439d1..6280374 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECPoint.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECPoint.cs @@ -9,7 +9,7 @@ namespace System.Security.Cryptography /// public struct ECPoint { - public byte[] X; - public byte[] Y; + public byte[]? X; + public byte[]? Y; } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HKDF.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HKDF.cs index 23edddc..45a15b2 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HKDF.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HKDF.cs @@ -24,7 +24,7 @@ namespace System.Security.Cryptography /// The input keying material. /// The optional salt value (a non-secret random value). If not provided it defaults to a byte array of zeros. /// The pseudo random key (prk). - public static byte[] Extract(HashAlgorithmName hashAlgorithmName, byte[] ikm, byte[] salt = null) + public static byte[] Extract(HashAlgorithmName hashAlgorithmName, byte[] ikm, byte[]? salt = null) { if (ikm == null) throw new ArgumentNullException(nameof(ikm)); @@ -83,7 +83,7 @@ namespace System.Security.Cryptography /// The length of the output keying material. /// The optional context and application specific information. /// The output keying material. - public static byte[] Expand(HashAlgorithmName hashAlgorithmName, byte[] prk, int outputLength, byte[] info = null) + public static byte[] Expand(HashAlgorithmName hashAlgorithmName, byte[] prk, int outputLength, byte[]? info = null) { if (prk == null) throw new ArgumentNullException(nameof(prk)); @@ -173,7 +173,7 @@ namespace System.Security.Cryptography /// The optional salt value (a non-secret random value). If not provided it defaults to a byte array of zeros. /// The optional context and application specific information. /// The output keying material. - public static byte[] DeriveKey(HashAlgorithmName hashAlgorithmName, byte[] ikm, int outputLength, byte[] salt = null, byte[] info = null) + public static byte[] DeriveKey(HashAlgorithmName hashAlgorithmName, byte[] ikm, int outputLength, byte[]? salt = null, byte[]? info = null) { if (ikm == null) throw new ArgumentNullException(nameof(ikm)); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACMD5.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACMD5.cs index 8826d89..d13c9bb 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACMD5.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACMD5.cs @@ -27,7 +27,7 @@ namespace System.Security.Cryptography this.HashName = HashAlgorithmNames.MD5; _hMacCommon = new HMACCommon(HashAlgorithmNames.MD5, key, BlockSize); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; // this not really needed as it'll initialize BlockSizeValue with same value it has which is 64. // we just want to be explicit in all HMAC extended classes BlockSizeValue = BlockSize; @@ -48,7 +48,7 @@ namespace System.Security.Cryptography } _hMacCommon.ChangeKey(value); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; } } @@ -77,7 +77,7 @@ namespace System.Security.Cryptography HMACCommon hMacCommon = _hMacCommon; if (hMacCommon != null) { - _hMacCommon = null; + _hMacCommon = null!; hMacCommon.Dispose(disposing); } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA1.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA1.cs index 7d5d75d..080674c 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA1.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA1.cs @@ -28,7 +28,7 @@ namespace System.Security.Cryptography this.HashName = HashAlgorithmNames.SHA1; _hMacCommon = new HMACCommon(HashAlgorithmNames.SHA1, key, BlockSize); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; // this not really needed as it'll initialize BlockSizeValue with same value it has which is 64. // we just want to be explicit in all HMAC extended classes BlockSizeValue = BlockSize; @@ -55,7 +55,7 @@ namespace System.Security.Cryptography } _hMacCommon.ChangeKey(value); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; } } @@ -84,7 +84,7 @@ namespace System.Security.Cryptography HMACCommon hMacCommon = _hMacCommon; if (hMacCommon != null) { - _hMacCommon = null; + _hMacCommon = null!; hMacCommon.Dispose(disposing); } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA256.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA256.cs index 1f66c87..ae62a68 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA256.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA256.cs @@ -31,7 +31,7 @@ namespace System.Security.Cryptography this.HashName = HashAlgorithmNames.SHA256; _hMacCommon = new HMACCommon(HashAlgorithmNames.SHA256, key, BlockSize); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; // this not really needed as it'll initialize BlockSizeValue with same value it has which is 64. // we just want to be explicit in all HMAC extended classes BlockSizeValue = BlockSize; @@ -52,7 +52,7 @@ namespace System.Security.Cryptography } _hMacCommon.ChangeKey(value); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; } } @@ -81,7 +81,7 @@ namespace System.Security.Cryptography HMACCommon hMacCommon = _hMacCommon; if (hMacCommon != null) { - _hMacCommon = null; + _hMacCommon = null!; hMacCommon.Dispose(disposing); } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA384.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA384.cs index 21ebed1..fe71ba0 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA384.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA384.cs @@ -31,7 +31,7 @@ namespace System.Security.Cryptography this.HashName = HashAlgorithmNames.SHA384; _hMacCommon = new HMACCommon(HashAlgorithmNames.SHA384, key, BlockSize); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; // change the default value of BlockSizeValue to 128 instead of 64 BlockSizeValue = BlockSize; HashSizeValue = _hMacCommon.HashSizeInBits; @@ -68,7 +68,7 @@ namespace System.Security.Cryptography } _hMacCommon.ChangeKey(value); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; } } @@ -97,7 +97,7 @@ namespace System.Security.Cryptography HMACCommon hMacCommon = _hMacCommon; if (hMacCommon != null) { - _hMacCommon = null; + _hMacCommon = null!; hMacCommon.Dispose(disposing); } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA512.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA512.cs index db40de1..b97f0b7 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA512.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/HMACSHA512.cs @@ -31,7 +31,7 @@ namespace System.Security.Cryptography this.HashName = HashAlgorithmNames.SHA512; _hMacCommon = new HMACCommon(HashAlgorithmNames.SHA512, key, BlockSize); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; // change the default value of BlockSizeValue to 128 instead of 64 BlockSizeValue = BlockSize; HashSizeValue = _hMacCommon.HashSizeInBits; @@ -66,7 +66,7 @@ namespace System.Security.Cryptography } _hMacCommon.ChangeKey(value); - base.Key = _hMacCommon.ActualKey; + base.Key = _hMacCommon.ActualKey!; } } @@ -95,7 +95,7 @@ namespace System.Security.Cryptography HMACCommon hMacCommon = _hMacCommon; if (hMacCommon != null) { - _hMacCommon = null; + _hMacCommon = null!; hMacCommon.Dispose(disposing); } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/IncrementalHash.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/IncrementalHash.cs index 709c560..a606026 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/IncrementalHash.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/IncrementalHash.cs @@ -13,8 +13,8 @@ namespace System.Security.Cryptography public sealed class IncrementalHash : IDisposable { private readonly HashAlgorithmName _algorithmName; - private HashProvider _hash; - private HMACCommon _hmac; + private HashProvider? _hash; + private HMACCommon? _hmac; private bool _disposed; private IncrementalHash(HashAlgorithmName name, HashProvider hash) @@ -104,7 +104,7 @@ namespace System.Security.Cryptography } else { - _hmac.AppendHashData(data); + _hmac!.AppendHashData(data); } } @@ -125,7 +125,7 @@ namespace System.Security.Cryptography Debug.Assert((_hash != null) ^ (_hmac != null)); return _hash != null ? _hash.FinalizeHashAndReset() : - _hmac.FinalizeHashAndReset(); + _hmac!.FinalizeHashAndReset(); } public bool TryGetHashAndReset(Span destination, out int bytesWritten) @@ -138,7 +138,7 @@ namespace System.Security.Cryptography Debug.Assert((_hash != null) ^ (_hmac != null)); return _hash != null ? _hash.TryFinalizeHashAndReset(destination, out bytesWritten) : - _hmac.TryFinalizeHashAndReset(destination, out bytesWritten); + _hmac!.TryFinalizeHashAndReset(destination, out bytesWritten); } /// diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/MD5.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/MD5.cs index 6a2515e..f251b0d 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/MD5.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/MD5.cs @@ -21,7 +21,7 @@ namespace System.Security.Cryptography public static new MD5 Create() => new Implementation(); - public static new MD5 Create(string algName) => (MD5)CryptoConfig.CreateFromName(algName); + public static new MD5? Create(string algName) => (MD5?)CryptoConfig.CreateFromName(algName); private sealed class Implementation : MD5 { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/PKCS1MaskGenerationMethod.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/PKCS1MaskGenerationMethod.cs index d5f3420..d8cfb35 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/PKCS1MaskGenerationMethod.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/PKCS1MaskGenerationMethod.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; using Internal.Cryptography; namespace System.Security.Cryptography @@ -24,7 +25,7 @@ namespace System.Security.Cryptography public override byte[] GenerateMask(byte[] rgbSeed, int cbReturn) { - using (HashAlgorithm hasher = CryptoConfig.CreateFromName(_hashNameValue) as HashAlgorithm) + using (HashAlgorithm? hasher = CryptoConfig.CreateFromName(_hashNameValue) as HashAlgorithm) { if (hasher is null) { @@ -41,6 +42,7 @@ namespace System.Security.Cryptography Helpers.ConvertIntToByteArray(counter++, rgbCounter); hasher.TransformBlock(rgbSeed, 0, rgbSeed.Length, rgbSeed, 0); hasher.TransformFinalBlock(rgbCounter, 0, 4); + Debug.Assert(hasher.Hash != null); byte[] hash = hasher.Hash; hasher.Initialize(); Buffer.BlockCopy(hash, 0, rgbT, ib, Math.Min(rgbT.Length - ib, hash.Length)); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RC2.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RC2.cs index 918db08..1300ff4 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RC2.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RC2.cs @@ -26,9 +26,9 @@ namespace System.Security.Cryptography return new RC2Implementation(); } - public static new RC2 Create(string AlgName) + public static new RC2? Create(string AlgName) { - return (RC2)CryptoConfig.CreateFromName(AlgName); + return (RC2?)CryptoConfig.CreateFromName(AlgName); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.Xml.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.Xml.cs index cdccc5e..8db68a5 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.Xml.cs @@ -13,7 +13,7 @@ namespace System.Security.Cryptography string name, int sizeHint = -1) { - byte[] ret = XmlKeyHelper.ReadCryptoBinary(ref state, name, sizeHint); + byte[]? ret = XmlKeyHelper.ReadCryptoBinary(ref state, name, sizeHint); if (ret == null) { @@ -36,12 +36,12 @@ namespace System.Security.Cryptography // .NET Framework doesn't report any element other than Modulus/Exponent as required, // it just lets import fail if they're imbalanced. - byte[] p = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.P), halfN); - byte[] q = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.Q), halfN); - byte[] dp = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.DP), halfN); - byte[] dq = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.DQ), halfN); - byte[] qInv = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.InverseQ), halfN); - byte[] d = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.D), n.Length); + byte[]? p = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.P), halfN); + byte[]? q = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.Q), halfN); + byte[]? dp = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.DP), halfN); + byte[]? dq = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.DQ), halfN); + byte[]? qInv = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.InverseQ), halfN); + byte[]? d = XmlKeyHelper.ReadCryptoBinary(ref state, nameof(RSAParameters.D), n.Length); RSAParameters keyParameters = new RSAParameters { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.cs index 3be56f9..31e89e5 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.cs @@ -12,9 +12,9 @@ namespace System.Security.Cryptography { public abstract partial class RSA : AsymmetricAlgorithm { - public static new RSA Create(string algName) + public static new RSA? Create(string algName) { - return (RSA)CryptoConfig.CreateFromName(algName); + return (RSA?)CryptoConfig.CreateFromName(algName); } public static RSA Create(int keySizeInBits) diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSACng.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSACng.cs index 614ea7a..c4d873f 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSACng.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSACng.cs @@ -20,7 +20,7 @@ namespace System.Security.Cryptography { public sealed partial class RSACng : RSA { - private SafeNCryptKeyHandle _keyHandle; + private SafeNCryptKeyHandle? _keyHandle; private int _lastKeySize; private bool _disposed; @@ -51,7 +51,7 @@ namespace System.Security.Cryptography _lastKeySize = keySize; } - return new DuplicateSafeNCryptKeyHandle(_keyHandle); + return new DuplicateSafeNCryptKeyHandle(_keyHandle!); } private byte[] ExportKeyBlob(bool includePrivateParameters) @@ -142,7 +142,7 @@ namespace System.Security.Cryptography if (disposing) { _keyHandle?.Dispose(); - _keyHandle = null; + _keyHandle = null!; _disposed = true; } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAEncryptionPadding.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAEncryptionPadding.cs index 422cad8..f7921cd 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAEncryptionPadding.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAEncryptionPadding.cs @@ -93,19 +93,19 @@ namespace System.Security.Cryptography return (((h1 << 5) + h1) ^ h2); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return Equals(obj as RSAEncryptionPadding); } - public bool Equals(RSAEncryptionPadding other) + public bool Equals(RSAEncryptionPadding? other) { return !object.ReferenceEquals(other, null) && _mode == other._mode && _oaepHashAlgorithm == other._oaepHashAlgorithm; } - public static bool operator ==(RSAEncryptionPadding left, RSAEncryptionPadding right) + public static bool operator ==(RSAEncryptionPadding? left, RSAEncryptionPadding? right) { if (object.ReferenceEquals(left, null)) { @@ -115,7 +115,7 @@ namespace System.Security.Cryptography return left.Equals(right); } - public static bool operator !=(RSAEncryptionPadding left, RSAEncryptionPadding right) + public static bool operator !=(RSAEncryptionPadding? left, RSAEncryptionPadding? right) { return !(left == right); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAOAEPKeyExchangeDeformatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAOAEPKeyExchangeDeformatter.cs index 8982333..ec7c7fc 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAOAEPKeyExchangeDeformatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAOAEPKeyExchangeDeformatter.cs @@ -6,7 +6,7 @@ namespace System.Security.Cryptography { public class RSAOAEPKeyExchangeDeformatter : AsymmetricKeyExchangeDeformatter { - private RSA _rsaKey; + private RSA? _rsaKey; public RSAOAEPKeyExchangeDeformatter() { } public RSAOAEPKeyExchangeDeformatter(AsymmetricAlgorithm key) @@ -17,7 +17,7 @@ namespace System.Security.Cryptography _rsaKey = (RSA)key; } - public override string Parameters + public override string? Parameters { get { return null; } set { } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAOAEPKeyExchangeFormatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAOAEPKeyExchangeFormatter.cs index df67717..5ebceb1 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAOAEPKeyExchangeFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAOAEPKeyExchangeFormatter.cs @@ -6,9 +6,9 @@ namespace System.Security.Cryptography { public class RSAOAEPKeyExchangeFormatter : AsymmetricKeyExchangeFormatter { - private byte[] ParameterValue; - private RSA _rsaKey; - private RandomNumberGenerator RngValue; + private byte[]? ParameterValue; + private RSA? _rsaKey; + private RandomNumberGenerator? RngValue; public RSAOAEPKeyExchangeFormatter() { } public RSAOAEPKeyExchangeFormatter(AsymmetricAlgorithm key) @@ -19,7 +19,7 @@ namespace System.Security.Cryptography _rsaKey = (RSA)key; } - public byte[] Parameter + public byte[]? Parameter { get { @@ -43,12 +43,12 @@ namespace System.Security.Cryptography } } - public override string Parameters + public override string? Parameters { get { return null; } } - public RandomNumberGenerator Rng + public RandomNumberGenerator? Rng { get { return RngValue; } set { RngValue = value; } @@ -62,7 +62,7 @@ namespace System.Security.Cryptography _rsaKey = (RSA)key; } - public override byte[] CreateKeyExchange(byte[] rgbData, Type symAlgType) + public override byte[] CreateKeyExchange(byte[] rgbData, Type? symAlgType) { return CreateKeyExchange(rgbData); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1KeyExchangeDeformatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1KeyExchangeDeformatter.cs index d69b14c..6e8dc47 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1KeyExchangeDeformatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1KeyExchangeDeformatter.cs @@ -6,8 +6,8 @@ namespace System.Security.Cryptography { public class RSAPKCS1KeyExchangeDeformatter : AsymmetricKeyExchangeDeformatter { - private RSA _rsaKey; - private RandomNumberGenerator RngValue; + private RSA? _rsaKey; + private RandomNumberGenerator? RngValue; public RSAPKCS1KeyExchangeDeformatter() { } @@ -19,13 +19,13 @@ namespace System.Security.Cryptography _rsaKey = (RSA)key; } - public RandomNumberGenerator RNG + public RandomNumberGenerator? RNG { get { return RngValue; } set { RngValue = value; } } - public override string Parameters + public override string? Parameters { get { return null; } set { } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1KeyExchangeFormatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1KeyExchangeFormatter.cs index 37f541e..5151e6a 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1KeyExchangeFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1KeyExchangeFormatter.cs @@ -6,8 +6,8 @@ namespace System.Security.Cryptography { public class RSAPKCS1KeyExchangeFormatter : AsymmetricKeyExchangeFormatter { - private RSA _rsaKey; - private RandomNumberGenerator RngValue; + private RSA? _rsaKey; + private RandomNumberGenerator? RngValue; public RSAPKCS1KeyExchangeFormatter() { } @@ -27,7 +27,7 @@ namespace System.Security.Cryptography } } - public RandomNumberGenerator Rng + public RandomNumberGenerator? Rng { get { return RngValue; } set { RngValue = value; } @@ -41,7 +41,7 @@ namespace System.Security.Cryptography _rsaKey = (RSA)key; } - public override byte[] CreateKeyExchange(byte[] rgbData, Type symAlgType) + public override byte[] CreateKeyExchange(byte[] rgbData, Type? symAlgType) { return CreateKeyExchange(rgbData); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1SignatureDeformatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1SignatureDeformatter.cs index cde2c1a..aee42a7 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1SignatureDeformatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1SignatureDeformatter.cs @@ -8,8 +8,8 @@ namespace System.Security.Cryptography { public class RSAPKCS1SignatureDeformatter : AsymmetricSignatureDeformatter { - private RSA _rsaKey; - private string _algName; + private RSA? _rsaKey; + private string? _algName; public RSAPKCS1SignatureDeformatter() { } public RSAPKCS1SignatureDeformatter(AsymmetricAlgorithm key) diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1SignatureFormatter.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1SignatureFormatter.cs index 2db257c..2f223d7 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1SignatureFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAPKCS1SignatureFormatter.cs @@ -8,8 +8,8 @@ namespace System.Security.Cryptography { public class RSAPKCS1SignatureFormatter : AsymmetricSignatureFormatter { - private RSA _rsaKey; - private string _algName; + private RSA? _rsaKey; + private string? _algName; public RSAPKCS1SignatureFormatter() { } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAParameters.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAParameters.cs index 9a728de..9e06c00 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAParameters.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSAParameters.cs @@ -11,13 +11,13 @@ namespace System.Security.Cryptography [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct RSAParameters { - public byte[] D; - public byte[] DP; - public byte[] DQ; - public byte[] Exponent; - public byte[] InverseQ; - public byte[] Modulus; - public byte[] P; - public byte[] Q; + public byte[]? D; + public byte[]? DP; + public byte[]? DQ; + public byte[]? Exponent; + public byte[]? InverseQ; + public byte[]? Modulus; + public byte[]? P; + public byte[]? Q; } } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSASignaturePadding.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSASignaturePadding.cs index 60059b1..6bfa15d 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSASignaturePadding.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSASignaturePadding.cs @@ -52,17 +52,17 @@ namespace System.Security.Cryptography return _mode.GetHashCode(); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return Equals(obj as RSASignaturePadding); } - public bool Equals(RSASignaturePadding other) + public bool Equals(RSASignaturePadding? other) { return !object.ReferenceEquals(other, null) && _mode == other._mode; } - public static bool operator ==(RSASignaturePadding left, RSASignaturePadding right) + public static bool operator ==(RSASignaturePadding? left, RSASignaturePadding? right) { if (object.ReferenceEquals(left, null)) { @@ -72,7 +72,7 @@ namespace System.Security.Cryptography return left.Equals(right); } - public static bool operator !=(RSASignaturePadding left, RSASignaturePadding right) + public static bool operator !=(RSASignaturePadding? left, RSASignaturePadding? right) { return !(left == right); } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RandomNumberGenerator.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RandomNumberGenerator.cs index 1986d56..783c737 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RandomNumberGenerator.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RandomNumberGenerator.cs @@ -16,9 +16,9 @@ namespace System.Security.Cryptography return new RandomNumberGeneratorImplementation(); } - public static RandomNumberGenerator Create(string rngName) + public static RandomNumberGenerator? Create(string rngName) { - return (RandomNumberGenerator)CryptoConfig.CreateFromName(rngName); + return (RandomNumberGenerator?)CryptoConfig.CreateFromName(rngName); } public void Dispose() diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rfc2898DeriveBytes.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rfc2898DeriveBytes.cs index 4e2f80a..70cb65e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rfc2898DeriveBytes.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rfc2898DeriveBytes.cs @@ -20,7 +20,7 @@ namespace System.Security.Cryptography private HMAC _hmac; private readonly int _blockSize; - private byte[] _buffer; + private byte[] _buffer = null!; // Initialized in helper private uint _block; private int _startIndex; private int _endIndex; @@ -145,7 +145,7 @@ namespace System.Security.Cryptography if (_hmac != null) { _hmac.Dispose(); - _hmac = null; + _hmac = null!; } if (_buffer != null) diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rijndael.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rijndael.cs index 8987960..7ab00ed 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rijndael.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/Rijndael.cs @@ -15,9 +15,9 @@ namespace System.Security.Cryptography return new RijndaelImplementation(); } - public static new Rijndael Create(string algName) + public static new Rijndael? Create(string algName) { - return (Rijndael)CryptoConfig.CreateFromName(algName); + return (Rijndael?)CryptoConfig.CreateFromName(algName); } protected Rijndael() diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RijndaelManaged.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RijndaelManaged.cs index 3c0f230..a4027d4 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RijndaelManaged.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RijndaelManaged.cs @@ -69,9 +69,9 @@ namespace System.Security.Cryptography // LegalBlockSizes not forwarded because base has correct information public override KeySizes[] LegalKeySizes => _impl.LegalKeySizes; public override ICryptoTransform CreateEncryptor() => _impl.CreateEncryptor(); - public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) => _impl.CreateEncryptor(rgbKey, rgbIV); + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) => _impl.CreateEncryptor(rgbKey, rgbIV); public override ICryptoTransform CreateDecryptor() => _impl.CreateDecryptor(); - public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) => _impl.CreateDecryptor(rgbKey, rgbIV); + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) => _impl.CreateDecryptor(rgbKey, rgbIV); public override void GenerateIV() => _impl.GenerateIV(); public override void GenerateKey() => _impl.GenerateKey(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1.cs index 4e82df0..36d264e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1.cs @@ -22,7 +22,7 @@ namespace System.Security.Cryptography [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350", Justification = "This is the implementaton of SHA1")] public static new SHA1 Create() => new Implementation(); - public static new SHA1 Create(string hashName) => (SHA1)CryptoConfig.CreateFromName(hashName); + public static new SHA1? Create(string hashName) => (SHA1?)CryptoConfig.CreateFromName(hashName); private sealed class Implementation : SHA1 { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA256.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA256.cs index 194d06d..913d195 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA256.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA256.cs @@ -21,7 +21,7 @@ namespace System.Security.Cryptography public static new SHA256 Create() => new Implementation(); - public static new SHA256 Create(string hashName) => (SHA256)CryptoConfig.CreateFromName(hashName); + public static new SHA256? Create(string hashName) => (SHA256?)CryptoConfig.CreateFromName(hashName); private sealed class Implementation : SHA256 { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA384.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA384.cs index 467846a..0f2f64d 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA384.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA384.cs @@ -21,7 +21,7 @@ namespace System.Security.Cryptography public static new SHA384 Create() => new Implementation(); - public static new SHA384 Create(string hashName) => (SHA384)CryptoConfig.CreateFromName(hashName); + public static new SHA384? Create(string hashName) => (SHA384?)CryptoConfig.CreateFromName(hashName); private sealed class Implementation : SHA384 { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA512.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA512.cs index cc64089..2b2a692 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA512.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA512.cs @@ -21,7 +21,7 @@ namespace System.Security.Cryptography public static new SHA512 Create() => new Implementation(); - public static new SHA512 Create(string hashName) => (SHA512)CryptoConfig.CreateFromName(hashName); + public static new SHA512? Create(string hashName) => (SHA512?)CryptoConfig.CreateFromName(hashName); private sealed class Implementation : SHA512 { diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SignatureDescription.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SignatureDescription.cs index 8459387..6198390 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SignatureDescription.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SignatureDescription.cs @@ -2,14 +2,16 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; + namespace System.Security.Cryptography { public class SignatureDescription { - public string KeyAlgorithm { get; set; } - public string DigestAlgorithm { get; set; } - public string FormatterAlgorithm { get; set; } - public string DeformatterAlgorithm { get; set; } + public string? KeyAlgorithm { get; set; } + public string? DigestAlgorithm { get; set; } + public string? FormatterAlgorithm { get; set; } + public string? DeformatterAlgorithm { get; set; } public SignatureDescription() { @@ -27,21 +29,21 @@ namespace System.Security.Cryptography public virtual AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key) { - AsymmetricSignatureDeformatter item = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm); - item.SetKey(key); + AsymmetricSignatureDeformatter? item = (AsymmetricSignatureDeformatter?)CryptoConfig.CreateFromName(DeformatterAlgorithm!); + item!.SetKey(key); return item; } public virtual AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key) { - AsymmetricSignatureFormatter item = (AsymmetricSignatureFormatter)CryptoConfig.CreateFromName(FormatterAlgorithm); - item.SetKey(key); + AsymmetricSignatureFormatter? item = (AsymmetricSignatureFormatter?)CryptoConfig.CreateFromName(FormatterAlgorithm!); + item!.SetKey(key); return item; } - public virtual HashAlgorithm CreateDigest() + public virtual HashAlgorithm? CreateDigest() { - return (HashAlgorithm)CryptoConfig.CreateFromName(DigestAlgorithm); + return (HashAlgorithm?)CryptoConfig.CreateFromName(DigestAlgorithm!); } } -} \ No newline at end of file +} diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/TripleDES.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/TripleDES.cs index 2738ab2..5bf4cad 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/TripleDES.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/TripleDES.cs @@ -25,9 +25,9 @@ namespace System.Security.Cryptography return new TripleDesImplementation(); } - public static new TripleDES Create(string str) + public static new TripleDES? Create(string str) { - return (TripleDES)CryptoConfig.CreateFromName(str); + return (TripleDES?)CryptoConfig.CreateFromName(str); } public override byte[] Key diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/XmlKeyHelper.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/XmlKeyHelper.cs index af965fc..9012886 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/XmlKeyHelper.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/XmlKeyHelper.cs @@ -34,9 +34,9 @@ namespace System.Security.Cryptography return state.HasElement(name); } - internal static byte[] ReadCryptoBinary(ref ParseState state, string name, int sizeHint = -1) + internal static byte[]? ReadCryptoBinary(ref ParseState state, string name, int sizeHint = -1) { - string value = state.GetValue(name); + string? value = state.GetValue(name); if (value == null) { @@ -166,13 +166,13 @@ namespace System.Security.Cryptography internal struct ParseState { - private IEnumerable _enumerable; - private IEnumerator _enumerator; + private IEnumerable? _enumerable; + private IEnumerator? _enumerator; private int _index; internal static ParseState ParseDocument(string xmlString) { - object rootElement = Functions.ParseDocument(xmlString); + object? rootElement = Functions.ParseDocument(xmlString); return new ParseState { @@ -184,7 +184,7 @@ namespace System.Security.Cryptography internal bool HasElement(string localName) { - string value = GetValue(localName); + string? value = GetValue(localName); bool ret = value != null; @@ -198,7 +198,7 @@ namespace System.Security.Cryptography return ret; } - internal string GetValue(string localName) + internal string? GetValue(string localName) { if (_enumerable == null) { @@ -229,7 +229,7 @@ namespace System.Security.Cryptography while (idx != origIdx) { - string curName = Functions.GetLocalName(_enumerator.Current); + string? curName = Functions.GetLocalName(_enumerator.Current); if (localName == curName) { @@ -265,36 +265,36 @@ namespace System.Security.Cryptography private static class Functions { - private static readonly Type s_xDocument = Type.GetType("System.Xml.Linq.XDocument, System.Private.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"); + private static readonly Type s_xDocument = Type.GetType("System.Xml.Linq.XDocument, System.Private.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51")!; private static readonly Func s_xDocumentCreate = (Func)s_xDocument.GetMethod( "Parse", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string) }, - null).CreateDelegate(typeof(Func)); - private static readonly PropertyInfo s_docRootProperty = s_xDocument.GetProperty("Root"); + null)!.CreateDelegate(typeof(Func)); + private static readonly PropertyInfo s_docRootProperty = s_xDocument.GetProperty("Root")!; private static readonly MethodInfo s_getElementsMethod = s_docRootProperty.PropertyType.GetMethod( "Elements", BindingFlags.Instance | BindingFlags.Public, null, Array.Empty(), - null); - private static readonly PropertyInfo s_elementNameProperty = s_docRootProperty.PropertyType.GetProperty("Name"); - private static readonly PropertyInfo s_nameNameProperty = s_elementNameProperty.PropertyType.GetProperty("LocalName"); - private static readonly PropertyInfo s_elementValueProperty = s_docRootProperty.PropertyType.GetProperty("Value"); + null)!; + private static readonly PropertyInfo s_elementNameProperty = s_docRootProperty.PropertyType.GetProperty("Name")!; + private static readonly PropertyInfo s_nameNameProperty = s_elementNameProperty.PropertyType.GetProperty("LocalName")!; + private static readonly PropertyInfo s_elementValueProperty = s_docRootProperty.PropertyType.GetProperty("Value")!; - internal static object ParseDocument(string xmlString) => + internal static object? ParseDocument(string xmlString) => s_docRootProperty.GetValue(s_xDocumentCreate(xmlString)); - internal static IEnumerable GetElements(object element) => - (IEnumerable)s_getElementsMethod.Invoke(element, Array.Empty()); + internal static IEnumerable? GetElements(object? element) => + (IEnumerable?)s_getElementsMethod.Invoke(element, Array.Empty()); - internal static string GetLocalName(object element) => - (string)s_nameNameProperty.GetValue(s_elementNameProperty.GetValue(element)); + internal static string? GetLocalName(object? element) => + (string?)s_nameNameProperty.GetValue(s_elementNameProperty.GetValue(element)); - internal static string GetValue(object element) => - (string)s_elementValueProperty.GetValue(element); + internal static string? GetValue(object? element) => + (string?)s_elementValueProperty.GetValue(element); } } }