diff --git a/src/libraries/Common/src/Internal/Cryptography/HashProviderCng.cs b/src/libraries/Common/src/Internal/Cryptography/HashProviderCng.cs index 4102f7fbd410be..30e975e39b0a09 100644 --- a/src/libraries/Common/src/Internal/Cryptography/HashProviderCng.cs +++ b/src/libraries/Common/src/Internal/Cryptography/HashProviderCng.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 Microsoft.Win32.SafeHandles; @@ -9,7 +10,6 @@ using BCryptOpenAlgorithmProviderFlags = Interop.BCrypt.BCryptOpenAlgorithmProviderFlags; using BCryptCreateHashFlags = Interop.BCrypt.BCryptCreateHashFlags; -#nullable enable namespace Internal.Cryptography { // diff --git a/src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.cs b/src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.cs index 29c41cbc54d136..6f4beba67f06c5 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/CngPkcs8.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; 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 1d942466bc4929..ded87debde3a1c 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/DSACng.ImportExport.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/DSACng.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 System.Diagnostics; 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 7c58da464869bc..799cc0bde54ef6 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/ECCng.HashAlgorithm.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/ECCng.HashAlgorithm.cs @@ -16,7 +16,7 @@ internal static partial class ECCng /// internal static Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM GetHashAlgorithmId(HashAlgorithmName? name) { - if (name.HasValue == false || string.IsNullOrEmpty(name!.Value.Name)) + if (name is null || string.IsNullOrEmpty(name.Value.Name)) { return Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM.BCRYPT_NO_CURVE_GENERATION_ALG_ID; } diff --git a/src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs b/src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs index 8ffac244ac8058..5ec20090be9354 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs @@ -5,6 +5,7 @@ #nullable enable using System.Buffers; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security.Cryptography.Asn1; 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 86950a2179739b..1e577f7437b8cb 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/RSACng.ImportExport.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/RSACng.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 Internal.Cryptography; using Internal.NativeCrypto; diff --git a/src/libraries/System.Security.Cryptography.Cng/ref/System.Security.Cryptography.Cng.cs b/src/libraries/System.Security.Cryptography.Cng/ref/System.Security.Cryptography.Cng.cs index 49ed6ff7ccb5de..fdd76283abaf5e 100644 --- a/src/libraries/System.Security.Cryptography.Cng/ref/System.Security.Cryptography.Cng.cs +++ b/src/libraries/System.Security.Cryptography.Cng/ref/System.Security.Cryptography.Cng.cs @@ -43,9 +43,9 @@ public AesCng(string keyName, System.Security.Cryptography.CngProvider provider, public override byte[] Key { get { throw null; } set { } } public override int KeySize { 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() { } @@ -68,11 +68,11 @@ public CngAlgorithm(string algorithm) { } public static System.Security.Cryptography.CngAlgorithm Sha256 { get { throw null; } } public static System.Security.Cryptography.CngAlgorithm Sha384 { get { throw null; } } public static System.Security.Cryptography.CngAlgorithm Sha512 { get { throw null; } } - public override bool Equals(object obj) { throw null; } - public bool Equals(System.Security.Cryptography.CngAlgorithm other) { throw null; } + public override bool Equals(object? obj) { throw null; } + public bool Equals(System.Security.Cryptography.CngAlgorithm? other) { throw null; } public override int GetHashCode() { throw null; } - public static bool operator ==(System.Security.Cryptography.CngAlgorithm left, System.Security.Cryptography.CngAlgorithm right) { throw null; } - public static bool operator !=(System.Security.Cryptography.CngAlgorithm left, System.Security.Cryptography.CngAlgorithm right) { throw null; } + public static bool operator ==(System.Security.Cryptography.CngAlgorithm? left, System.Security.Cryptography.CngAlgorithm? right) { throw null; } + public static bool operator !=(System.Security.Cryptography.CngAlgorithm? left, System.Security.Cryptography.CngAlgorithm? right) { throw null; } public override string ToString() { throw null; } } public sealed partial class CngAlgorithmGroup : System.IEquatable @@ -84,11 +84,11 @@ public CngAlgorithmGroup(string algorithmGroup) { } public static System.Security.Cryptography.CngAlgorithmGroup ECDiffieHellman { get { throw null; } } public static System.Security.Cryptography.CngAlgorithmGroup ECDsa { get { throw null; } } public static System.Security.Cryptography.CngAlgorithmGroup Rsa { get { throw null; } } - public override bool Equals(object obj) { throw null; } - public bool Equals(System.Security.Cryptography.CngAlgorithmGroup other) { throw null; } + public override bool Equals(object? obj) { throw null; } + public bool Equals(System.Security.Cryptography.CngAlgorithmGroup? other) { throw null; } public override int GetHashCode() { throw null; } - public static bool operator ==(System.Security.Cryptography.CngAlgorithmGroup left, System.Security.Cryptography.CngAlgorithmGroup right) { throw null; } - public static bool operator !=(System.Security.Cryptography.CngAlgorithmGroup left, System.Security.Cryptography.CngAlgorithmGroup right) { throw null; } + public static bool operator ==(System.Security.Cryptography.CngAlgorithmGroup? left, System.Security.Cryptography.CngAlgorithmGroup? right) { throw null; } + public static bool operator !=(System.Security.Cryptography.CngAlgorithmGroup? left, System.Security.Cryptography.CngAlgorithmGroup? right) { throw null; } public override string ToString() { throw null; } } [System.FlagsAttribute] @@ -104,22 +104,22 @@ public sealed partial class CngKey : System.IDisposable { internal CngKey() { } public System.Security.Cryptography.CngAlgorithm Algorithm { get { throw null; } } - public System.Security.Cryptography.CngAlgorithmGroup AlgorithmGroup { get { throw null; } } + public System.Security.Cryptography.CngAlgorithmGroup? AlgorithmGroup { get { throw null; } } public System.Security.Cryptography.CngExportPolicies ExportPolicy { get { throw null; } } public Microsoft.Win32.SafeHandles.SafeNCryptKeyHandle Handle { get { throw null; } } public bool IsEphemeral { get { throw null; } } public bool IsMachineKey { get { throw null; } } - public string KeyName { get { throw null; } } + public string? KeyName { get { throw null; } } public int KeySize { get { throw null; } } public System.Security.Cryptography.CngKeyUsages KeyUsage { get { throw null; } } public System.IntPtr ParentWindowHandle { get { throw null; } set { } } - public System.Security.Cryptography.CngProvider Provider { get { throw null; } } + public System.Security.Cryptography.CngProvider? Provider { get { throw null; } } public Microsoft.Win32.SafeHandles.SafeNCryptProviderHandle ProviderHandle { get { throw null; } } public System.Security.Cryptography.CngUIPolicy UIPolicy { get { throw null; } } - public string UniqueName { get { throw null; } } + public string? UniqueName { get { throw null; } } public static System.Security.Cryptography.CngKey Create(System.Security.Cryptography.CngAlgorithm algorithm) { throw null; } - public static System.Security.Cryptography.CngKey Create(System.Security.Cryptography.CngAlgorithm algorithm, string keyName) { throw null; } - public static System.Security.Cryptography.CngKey Create(System.Security.Cryptography.CngAlgorithm algorithm, string keyName, System.Security.Cryptography.CngKeyCreationParameters creationParameters) { throw null; } + public static System.Security.Cryptography.CngKey Create(System.Security.Cryptography.CngAlgorithm algorithm, string? keyName) { throw null; } + public static System.Security.Cryptography.CngKey Create(System.Security.Cryptography.CngAlgorithm algorithm, string? keyName, System.Security.Cryptography.CngKeyCreationParameters? creationParameters) { throw null; } public void Delete() { } public void Dispose() { } public static bool Exists(string keyName) { throw null; } @@ -148,11 +148,11 @@ public CngKeyBlobFormat(string format) { } public static System.Security.Cryptography.CngKeyBlobFormat GenericPublicBlob { get { throw null; } } public static System.Security.Cryptography.CngKeyBlobFormat OpaqueTransportBlob { get { throw null; } } public static System.Security.Cryptography.CngKeyBlobFormat Pkcs8PrivateBlob { get { throw null; } } - public override bool Equals(object obj) { throw null; } - public bool Equals(System.Security.Cryptography.CngKeyBlobFormat other) { throw null; } + public override bool Equals(object? obj) { throw null; } + public bool Equals(System.Security.Cryptography.CngKeyBlobFormat? other) { throw null; } public override int GetHashCode() { throw null; } - public static bool operator ==(System.Security.Cryptography.CngKeyBlobFormat left, System.Security.Cryptography.CngKeyBlobFormat right) { throw null; } - public static bool operator !=(System.Security.Cryptography.CngKeyBlobFormat left, System.Security.Cryptography.CngKeyBlobFormat right) { throw null; } + public static bool operator ==(System.Security.Cryptography.CngKeyBlobFormat? left, System.Security.Cryptography.CngKeyBlobFormat? right) { throw null; } + public static bool operator !=(System.Security.Cryptography.CngKeyBlobFormat? left, System.Security.Cryptography.CngKeyBlobFormat? right) { throw null; } public override string ToString() { throw null; } } [System.FlagsAttribute] @@ -170,8 +170,9 @@ public CngKeyCreationParameters() { } public System.Security.Cryptography.CngKeyUsages? KeyUsage { get { throw null; } set { } } public System.Security.Cryptography.CngPropertyCollection Parameters { get { throw null; } } public System.IntPtr ParentWindowHandle { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public System.Security.Cryptography.CngProvider Provider { get { throw null; } set { } } - public System.Security.Cryptography.CngUIPolicy UIPolicy { get { throw null; } set { } } + public System.Security.Cryptography.CngUIPolicy? UIPolicy { get { throw null; } set { } } } [System.FlagsAttribute] public enum CngKeyHandleOpenOptions @@ -200,13 +201,13 @@ public partial struct CngProperty : System.IEquatablenetcoreapp3.0;netstandard2.1;net461;net462;net47;$(NetCoreAppCurrent);$(NetFrameworkCurrent) true true + enable diff --git a/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/BasicSymmetricCipherNCrypt.cs b/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/BasicSymmetricCipherNCrypt.cs index b3f443d8169f0f..635db77f5f88f8 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/BasicSymmetricCipherNCrypt.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/BasicSymmetricCipherNCrypt.cs @@ -51,7 +51,7 @@ public sealed override int Transform(byte[] input, int inputOffset, int count, b int numBytesWritten; ErrorCode errorCode; - using (SafeNCryptKeyHandle keyHandle = _cngKey.Handle) + using (SafeNCryptKeyHandle keyHandle = _cngKey!.Handle) { var inputSpan = new ReadOnlySpan(input, inputOffset, count); var outputSpan = new Span(output, outputOffset, count); @@ -104,7 +104,7 @@ protected sealed override void Dispose(bool disposing) if (_cngKey != null) { _cngKey.Dispose(); - _cngKey = null; + _cngKey = null!; } } @@ -116,7 +116,7 @@ private void Reset() if (IV != null) { CngProperty prop = new CngProperty(Interop.NCrypt.NCRYPT_INITIALIZATION_VECTOR, IV, CngPropertyOptions.None); - _cngKey.SetProperty(prop); + _cngKey!.SetProperty(prop); } } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngAlgorithmCore.cs b/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngAlgorithmCore.cs index 72bd3774cf1a01..e9ea410a575e26 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngAlgorithmCore.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngAlgorithmCore.cs @@ -17,7 +17,7 @@ internal struct CngAlgorithmCore { private readonly string _disposedName; public CngAlgorithm DefaultKeyType; - private CngKey _lazyKey; + private CngKey? _lazyKey; private bool _disposed; public CngAlgorithmCore(string disposedName) : this() diff --git a/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngSymmetricAlgorithmCore.cs b/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngSymmetricAlgorithmCore.cs index 20449c477d7f21..58c05c6ff93fcd 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngSymmetricAlgorithmCore.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngSymmetricAlgorithmCore.cs @@ -110,12 +110,12 @@ public ICryptoTransform CreateDecryptor() return CreateCryptoTransform(encrypting: false); } - public ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) + public ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateCryptoTransform(rgbKey, rgbIV, encrypting: true); } - public ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) + public ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { return CreateCryptoTransform(rgbKey, rgbIV, encrypting: false); } @@ -130,7 +130,7 @@ private ICryptoTransform CreateCryptoTransform(bool encrypting) return CreatePersistedCryptoTransformCore(ProduceCngKey, _outer.IV, encrypting); } - private ICryptoTransform CreateCryptoTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting) + private ICryptoTransform CreateCryptoTransform(byte[] rgbKey, byte[]? rgbIV, bool encrypting) { if (rgbKey == null) throw new ArgumentNullException(nameof(rgbKey)); @@ -149,14 +149,14 @@ private ICryptoTransform CreateCryptoTransform(byte[] rgbKey, byte[] rgbIV, bool // CloneByteArray is null-preserving. So even when GetCipherIv returns null the iv variable // is correct, and detached from the input parameter. - byte[] iv = _outer.Mode.GetCipherIv(rgbIV).CloneByteArray(); + byte[]? iv = _outer.Mode.GetCipherIv(rgbIV).CloneByteArray(); key = _outer.PreprocessKey(key); return CreateEphemeralCryptoTransformCore(key, iv, encrypting); } - private ICryptoTransform CreateEphemeralCryptoTransformCore(byte[] key, byte[] iv, bool encrypting) + private ICryptoTransform CreateEphemeralCryptoTransformCore(byte[] key, byte[]? iv, bool encrypting) { int blockSizeInBytes = _outer.BlockSize.BitSizeToByteSize(); SafeAlgorithmHandle algorithmModeHandle = _outer.GetEphemeralModeHandle(); @@ -186,7 +186,7 @@ private CngKey ProduceCngKey() { Debug.Assert(!KeyInPlainText); - return CngKey.Open(_keyName, _provider, _optionOptions); + return CngKey.Open(_keyName!, _provider!, _optionOptions); } private bool KeyInPlainText @@ -197,8 +197,8 @@ private bool KeyInPlainText private readonly ICngSymmetricAlgorithm _outer; // If using a stored CNG key, these fields provide the CngKey.Open() parameters. If using a plaintext key, _keyName is set to null. - private string _keyName; - private readonly CngProvider _provider; + private string? _keyName; + private readonly CngProvider? _provider; private readonly CngKeyOpenOptions _optionOptions; private const int BitsPerByte = 8; diff --git a/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/Helpers.cs b/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/Helpers.cs index 4a436659757178..9967ecf33ae3e0 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/Helpers.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/Helpers.cs @@ -21,7 +21,7 @@ public static bool UsesIv(this CipherMode cipherMode) 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()) { @@ -50,7 +50,7 @@ public static byte[] GetCipherIv(this CipherMode cipherMode, byte[] iv) // // which always sets "p" to a non-NULL pointer for a non-null byte array. // - public static byte[] MapZeroLengthArrayToNonNullPointer(this byte[] src) + public static byte[]? MapZeroLengthArrayToNonNullPointer(this byte[]? src) { if (src != null && src.Length == 0) return new byte[1]; @@ -74,7 +74,7 @@ public static SafeNCryptProviderHandle OpenStorageProvider(this CngProvider prov /// null - if property not defined on key. /// throws - for any other type of error. /// - public static byte[] GetProperty(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) + public static byte[]? GetProperty(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) { unsafe { @@ -104,9 +104,9 @@ public static byte[] GetProperty(this SafeNCryptHandle ncryptHandle, string prop /// 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.) /// - public static string GetPropertyAsString(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) + public static string? GetPropertyAsString(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) { - byte[] value = ncryptHandle.GetProperty(propertyName, options); + byte[]? value = ncryptHandle.GetProperty(propertyName, options); if (value == null) return null; // .NET Framework compat: return null if key not present. if (value.Length == 0) @@ -115,7 +115,7 @@ public static string GetPropertyAsString(this SafeNCryptHandle ncryptHandle, str { fixed (byte* pValue = &value[0]) { - string valueAsString = Marshal.PtrToStringUni((IntPtr)pValue); + string? valueAsString = Marshal.PtrToStringUni((IntPtr)pValue); return valueAsString; } } @@ -127,7 +127,7 @@ public static string GetPropertyAsString(this SafeNCryptHandle ncryptHandle, str /// public static int GetPropertyAsDword(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options) { - byte[] value = ncryptHandle.GetProperty(propertyName, options); + byte[]? value = ncryptHandle.GetProperty(propertyName, options); if (value == null) return 0; // .NET Framework compat: return 0 if key not present. return BitConverter.ToInt32(value, 0); diff --git a/src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs b/src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs index 65382f3d99be03..94d3f0fcdc8f85 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using ErrorCode = Interop.NCrypt.ErrorCode; @@ -61,9 +62,9 @@ private enum OwnershipState /// /// If the handle is a Duplicate, this points at the safe handle which actually owns the native handle. /// - private SafeNCryptHandle _holder; + private SafeNCryptHandle? _holder; - private SafeHandle _parentHandle; + private SafeHandle? _parentHandle; protected SafeNCryptHandle() : base(true) { @@ -98,6 +99,7 @@ protected SafeNCryptHandle(IntPtr handle, SafeHandle parentHandle) /// /// Wrapper for the _holder field which ensures that we're in a consistent state /// + [MaybeNull] private SafeNCryptHandle Holder { get @@ -166,7 +168,7 @@ private bool IsValidOpenState { if (acquiredHolder) { - Holder.DangerousRelease(); + Holder!.DangerousRelease(); } } @@ -239,7 +241,7 @@ private bool IsValidOpenState bool addedRef = false; T duplicate = new T(); - Holder.DangerousAddRef(ref addedRef); + Holder!.DangerousAddRef(ref addedRef); duplicate.SetHandle(Holder.DangerousGetHandle()); duplicate.Holder = Holder; // Transitions to OwnershipState.Duplicate @@ -309,7 +311,7 @@ protected override bool ReleaseHandle() { if (_ownershipState == OwnershipState.Duplicate) { - Holder.DangerousRelease(); + Holder!.DangerousRelease(); return true; } else if (_parentHandle != null) diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj b/src/libraries/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj index 003b88e8d85d3c..b7430d7f345ef1 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj +++ b/src/libraries/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj @@ -8,6 +8,7 @@ netstandard2.0;netstandard2.1;net461-Windows_NT;netcoreapp3.0-Windows_NT;netcoreapp3.0;net462-Windows_NT;net47-Windows_NT;$(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent);$(NetFrameworkCurrent)-Windows_NT true true + enable SR.PlatformNotSupported_CryptographyCng diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/AesCng.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/AesCng.cs index 202ad173586671..4feb3b1fa65f15 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/AesCng.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/AesCng.cs @@ -67,7 +67,7 @@ public override ICryptoTransform CreateDecryptor() return _core.CreateDecryptor(); } - public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { return _core.CreateDecryptor(rgbKey, rgbIV); } @@ -78,7 +78,7 @@ public override ICryptoTransform CreateEncryptor() return _core.CreateEncryptor(); } - public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { return _core.CreateEncryptor(rgbKey, rgbIV); } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithm.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithm.cs index d2884094afc9aa..cababd9cbe2678 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithm.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithm.cs @@ -36,7 +36,7 @@ public string Algorithm } } - public static bool operator ==(CngAlgorithm left, CngAlgorithm right) + public static bool operator ==(CngAlgorithm? left, CngAlgorithm? right) { if (object.ReferenceEquals(left, null)) { @@ -46,7 +46,7 @@ public string Algorithm return left.Equals(right); } - public static bool operator !=(CngAlgorithm left, CngAlgorithm right) + public static bool operator !=(CngAlgorithm? left, CngAlgorithm? right) { if (object.ReferenceEquals(left, null)) { @@ -56,14 +56,14 @@ public string Algorithm return !left.Equals(right); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { Debug.Assert(_algorithm != null); return Equals(obj as CngAlgorithm); } - public bool Equals(CngAlgorithm other) + public bool Equals(CngAlgorithm? other) { if (object.ReferenceEquals(other, null)) { @@ -201,20 +201,20 @@ public static CngAlgorithm Sha512 } } - private static CngAlgorithm s_ecdh; - private static CngAlgorithm s_ecdhp256; - private static CngAlgorithm s_ecdhp384; - private static CngAlgorithm s_ecdhp521; - private static CngAlgorithm s_ecdsa; - private static CngAlgorithm s_ecdsap256; - private static CngAlgorithm s_ecdsap384; - private static CngAlgorithm s_ecdsap521; - private static CngAlgorithm s_md5; - private static CngAlgorithm s_sha1; - private static CngAlgorithm s_sha256; - private static CngAlgorithm s_sha384; - private static CngAlgorithm s_sha512; - private static CngAlgorithm s_rsa; + private static CngAlgorithm? s_ecdh; + private static CngAlgorithm? s_ecdhp256; + private static CngAlgorithm? s_ecdhp384; + private static CngAlgorithm? s_ecdhp521; + private static CngAlgorithm? s_ecdsa; + private static CngAlgorithm? s_ecdsap256; + private static CngAlgorithm? s_ecdsap384; + private static CngAlgorithm? s_ecdsap521; + private static CngAlgorithm? s_md5; + private static CngAlgorithm? s_sha1; + private static CngAlgorithm? s_sha256; + private static CngAlgorithm? s_sha384; + private static CngAlgorithm? s_sha512; + private static CngAlgorithm? s_rsa; private readonly string _algorithm; } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithmGroup.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithmGroup.cs index 2195835acfc781..b67ade68de3660 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithmGroup.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithmGroup.cs @@ -37,7 +37,7 @@ public string AlgorithmGroup } } - public static bool operator ==(CngAlgorithmGroup left, CngAlgorithmGroup right) + public static bool operator ==(CngAlgorithmGroup? left, CngAlgorithmGroup? right) { if (object.ReferenceEquals(left, null)) { @@ -47,7 +47,7 @@ public string AlgorithmGroup return left.Equals(right); } - public static bool operator !=(CngAlgorithmGroup left, CngAlgorithmGroup right) + public static bool operator !=(CngAlgorithmGroup? left, CngAlgorithmGroup? right) { if (object.ReferenceEquals(left, null)) { @@ -57,14 +57,14 @@ public string AlgorithmGroup return !left.Equals(right); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { Debug.Assert(_algorithmGroup != null); return Equals(obj as CngAlgorithmGroup); } - public bool Equals(CngAlgorithmGroup other) + public bool Equals(CngAlgorithmGroup? other) { if (object.ReferenceEquals(other, null)) { @@ -130,11 +130,11 @@ public static CngAlgorithmGroup Rsa } } - private static CngAlgorithmGroup s_dh; - private static CngAlgorithmGroup s_dsa; - private static CngAlgorithmGroup s_ecdh; - private static CngAlgorithmGroup s_ecdsa; - private static CngAlgorithmGroup s_rsa; + private static CngAlgorithmGroup? s_dh; + private static CngAlgorithmGroup? s_dsa; + private static CngAlgorithmGroup? s_ecdh; + private static CngAlgorithmGroup? s_ecdsa; + private static CngAlgorithmGroup? s_rsa; private readonly string _algorithmGroup; } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Create.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Create.cs index e0190d11e7e031..506cc95fc6d351 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Create.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Create.cs @@ -24,12 +24,12 @@ public static CngKey Create(CngAlgorithm algorithm) return Create(algorithm, keyName: null); } - public static CngKey Create(CngAlgorithm algorithm, string keyName) + public static CngKey Create(CngAlgorithm algorithm, string? keyName) { return Create(algorithm, keyName, creationParameters: null); } - public static CngKey Create(CngAlgorithm algorithm, string keyName, CngKeyCreationParameters creationParameters) + public static CngKey Create(CngAlgorithm algorithm, string? keyName, CngKeyCreationParameters? creationParameters) { if (algorithm == null) throw new ArgumentNullException(nameof(algorithm)); @@ -37,7 +37,7 @@ public static CngKey Create(CngAlgorithm algorithm, string keyName, CngKeyCreati if (creationParameters == null) creationParameters = new CngKeyCreationParameters(); - SafeNCryptProviderHandle providerHandle = creationParameters.Provider.OpenStorageProvider(); + SafeNCryptProviderHandle providerHandle = creationParameters.Provider!.OpenStorageProvider(); SafeNCryptKeyHandle keyHandle; ErrorCode errorCode = Interop.NCrypt.NCryptCreatePersistedKey(providerHandle, out keyHandle, algorithm.Algorithm, keyName, 0, creationParameters.KeyCreationOptions); if (errorCode != ErrorCode.ERROR_SUCCESS) @@ -95,7 +95,7 @@ private static void InitializeKeyProperties(SafeNCryptKeyHandle keyHandle, CngKe throw errorCode.ToCryptographicException(); } - CngUIPolicy uiPolicy = creationParameters.UIPolicy; + CngUIPolicy? uiPolicy = creationParameters.UIPolicy; if (uiPolicy != null) { InitializeKeyUiPolicyProperties(keyHandle, uiPolicy); @@ -104,7 +104,7 @@ private static void InitializeKeyProperties(SafeNCryptKeyHandle keyHandle, CngKe // Iterate over the custom properties, setting those as well. foreach (CngProperty property in creationParameters.Parameters) { - byte[] value = property.GetValueWithoutCopying(); + byte[]? value = property.GetValueWithoutCopying(); int valueLength = (value == null) ? 0 : value.Length; fixed (byte* pValue = value.MapZeroLengthArrayToNonNullPointer()) { @@ -141,7 +141,7 @@ private static void InitializeKeyUiPolicyProperties(SafeNCryptKeyHandle keyHandl throw errorCode.ToCryptographicException(); } - string useContext = uiPolicy.UseContext; + string? useContext = uiPolicy.UseContext; if (useContext != null) { int useContextByteLength = checked((useContext.Length + 1) * sizeof(char)); diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.EC.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.EC.cs index 3065f2238ac95b..b6570d397e41ba 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.EC.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.EC.cs @@ -26,7 +26,7 @@ internal static bool IsECNamedCurve(string algorithm) algorithm == CngAlgorithm.ECDsa.Algorithm); } - internal string GetCurveName(out string oidValue) + internal string? GetCurveName(out string? oidValue) { if (IsECNamedCurve()) { @@ -72,7 +72,7 @@ private string GetECSpecificCurveName(out string oidValue) /// internal static CngProperty GetPropertyFromNamedCurve(ECCurve curve) { - string curveName = curve.Oid.FriendlyName; + string curveName = curve.Oid.FriendlyName!; unsafe { byte[] curveNameBytes = new byte[(curveName.Length + 1) * sizeof(char)]; // +1 to add trailing null @@ -131,7 +131,7 @@ internal static CngAlgorithm EcdhCurveNameToAlgorithm(string name) return CngAlgorithm.ECDiffieHellman; } - internal static CngKey Create(ECCurve curve, Func algorithmResolver) + internal static CngKey Create(ECCurve curve, Func algorithmResolver) { System.Diagnostics.Debug.Assert(algorithmResolver != null); @@ -199,7 +199,7 @@ internal static CngKey Create(ECCurve curve, Func algorith if (errorCode == Interop.NCrypt.ErrorCode.NTE_INVALID_PARAMETER || errorCode == Interop.NCrypt.ErrorCode.NTE_NOT_SUPPORTED) { - string target = curve.IsNamed ? curve.Oid.FriendlyName : curve.CurveType.ToString(); + string? target = curve.IsNamed ? curve.Oid.FriendlyName : curve.CurveType.ToString(); throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CurveNotSupported, target), e); } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Exists.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Exists.cs index 7b4f1f575ff855..d8ca057ab1c68c 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Exists.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Exists.cs @@ -39,7 +39,7 @@ public static bool Exists(string keyName, CngProvider provider, CngKeyOpenOption using (SafeNCryptProviderHandle providerHandle = provider.OpenStorageProvider()) { - SafeNCryptKeyHandle keyHandle = null; + SafeNCryptKeyHandle? keyHandle = null; try { ErrorCode errorCode = Interop.NCrypt.NCryptOpenKey(providerHandle, out keyHandle, keyName, 0, options); diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Export.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Export.cs index 90061030ced831..4f2a30f8c6a493 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Export.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Export.cs @@ -91,15 +91,16 @@ internal byte[] ExportPkcs8KeyBlob( int kdfCount) { bool ret = ExportPkcs8KeyBlob( - true, + allocate: true, _keyHandle, password, kdfCount, Span.Empty, out _, - out byte[] allocated); + out byte[]? allocated); Debug.Assert(ret); + Debug.Assert(allocated != null); // since `allocate: true` return allocated; } @@ -130,7 +131,7 @@ internal static unsafe bool ExportPkcs8KeyBlob( int kdfCount, Span destination, out int bytesWritten, - out byte[] allocated) + out byte[]? allocated) { using (SafeUnicodeStringHandle stringHandle = new SafeUnicodeStringHandle(password)) { @@ -226,7 +227,7 @@ ref MemoryMarshal.GetReference(destination), { byte[] trimmed = new byte[numBytesNeeded]; destination.Slice(0, numBytesNeeded).CopyTo(trimmed); - Array.Clear(allocated, 0, numBytesNeeded); + Array.Clear(allocated!, 0, numBytesNeeded); allocated = trimmed; } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Import.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Import.cs index 27eb685edd1126..90585f88aaed66 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Import.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Import.cs @@ -29,7 +29,7 @@ public static CngKey Import(byte[] keyBlob, CngKeyBlobFormat format) return Import(keyBlob, format, provider: CngProvider.MicrosoftSoftwareKeyStorageProvider); } - internal static CngKey Import(byte[] keyBlob, string curveName, CngKeyBlobFormat format) + internal static CngKey Import(byte[] keyBlob, string? curveName, CngKeyBlobFormat format) { return Import(keyBlob, curveName, format, provider: CngProvider.MicrosoftSoftwareKeyStorageProvider); } @@ -101,7 +101,7 @@ ref MemoryMarshal.GetReference(keyBlob), internal static CngKey Import( byte[] keyBlob, - string curveName, + string? curveName, CngKeyBlobFormat format, CngProvider provider) { @@ -113,7 +113,7 @@ internal static CngKey Import( internal static CngKey Import( ReadOnlySpan keyBlob, - string curveName, + string? curveName, CngKeyBlobFormat format, CngProvider provider) { @@ -123,7 +123,7 @@ internal static CngKey Import( throw new ArgumentNullException(nameof(provider)); SafeNCryptProviderHandle providerHandle = provider.OpenStorageProvider(); - SafeNCryptKeyHandle keyHandle = null; + SafeNCryptKeyHandle? keyHandle = null; ErrorCode errorCode; if (curveName == null) diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.OpenHandle.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.OpenHandle.cs index 0be5ef6a46f678..4215ee30f6cd27 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.OpenHandle.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.OpenHandle.cs @@ -31,7 +31,7 @@ public static CngKey Open(SafeNCryptKeyHandle keyHandle, CngKeyHandleOpenOptions providerHandle.SetHandleValue(rawProviderHandle); // Set up a key object wrapping the handle - CngKey key = null; + CngKey? key = null; try { key = new CngKey(providerHandle, keyHandleCopy); diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Properties.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Properties.cs index b0abfe2a201c42..0e7716f04fec8d 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Properties.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Properties.cs @@ -21,7 +21,7 @@ public CngProperty GetProperty(string name, CngPropertyOptions options) if (name == null) throw new ArgumentNullException(nameof(name)); - byte[] value = _keyHandle.GetProperty(name, options); + byte[]? value = _keyHandle.GetProperty(name, options); if (value == null) throw ErrorCode.NTE_NOT_FOUND.ToCryptographicException(); @@ -58,7 +58,7 @@ public void SetProperty(CngProperty property) { unsafe { - byte[] propertyValue = property.GetValueWithoutCopying(); + byte[]? propertyValue = property.GetValueWithoutCopying(); // .NET Framework compat. It would have nicer to throw an ArgumentNull exception or something... if (propertyValue == null) diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.StandardProperties.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.StandardProperties.cs index 98cfc8203c29ec..0a375c6f54f306 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.StandardProperties.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.StandardProperties.cs @@ -29,7 +29,7 @@ public CngAlgorithm Algorithm { get { - string algorithm = _keyHandle.GetPropertyAsString(KeyPropertyName.Algorithm, CngPropertyOptions.None); + string algorithm = _keyHandle.GetPropertyAsString(KeyPropertyName.Algorithm, CngPropertyOptions.None)!; // .NET Framework compat: Don't check for null. Just let CngAlgorithm handle it. return new CngAlgorithm(algorithm); } @@ -39,12 +39,12 @@ public CngAlgorithm Algorithm /// /// Name of the algorithm this key can be used with /// - public CngAlgorithmGroup AlgorithmGroup + public CngAlgorithmGroup? AlgorithmGroup { get { - string algorithmGroup = _keyHandle.GetPropertyAsString(KeyPropertyName.AlgorithmGroup, CngPropertyOptions.None); + string? algorithmGroup = _keyHandle.GetPropertyAsString(KeyPropertyName.AlgorithmGroup, CngPropertyOptions.None); if (algorithmGroup == null) return null; return new CngAlgorithmGroup(algorithmGroup); @@ -144,14 +144,14 @@ public bool IsMachineKey /// the CLR. Other ephemeral keys, such as those imported by handle, will get a CryptographicException /// if they read this property. /// - public string KeyName + public string? KeyName { get { if (IsEphemeral) return null; - string keyName = _keyHandle.GetPropertyAsString(KeyPropertyName.Name, CngPropertyOptions.None); + string? keyName = _keyHandle.GetPropertyAsString(KeyPropertyName.Name, CngPropertyOptions.None); return keyName; } } @@ -226,11 +226,11 @@ public IntPtr ParentWindowHandle /// /// KSP which holds this key /// - public CngProvider Provider + public CngProvider? Provider { get { - string provider = _providerHandle.GetPropertyAsString(ProviderPropertyName.Name, CngPropertyOptions.None); + string? provider = _providerHandle.GetPropertyAsString(ProviderPropertyName.Name, CngPropertyOptions.None); if (provider == null) return null; return new CngProvider(provider); @@ -256,9 +256,9 @@ public CngUIPolicy UIPolicy get { CngUIProtectionLevels uiProtectionLevel; - string friendlyName; - string description; - string creationTitle; + string? friendlyName; + string? description; + string? creationTitle; unsafe { int numBytesNeeded; @@ -300,7 +300,7 @@ public CngUIPolicy UIPolicy } } - string useContext = _keyHandle.GetPropertyAsString(KeyPropertyName.UseContext, CngPropertyOptions.None); + string? useContext = _keyHandle.GetPropertyAsString(KeyPropertyName.UseContext, CngPropertyOptions.None); return new CngUIPolicy(uiProtectionLevel, friendlyName, description, useContext, creationTitle); } @@ -310,14 +310,14 @@ public CngUIPolicy UIPolicy /// Unique name of the key, null if it is ephemeral. See the comments on the Name property for /// details about names of ephemeral keys. /// - public string UniqueName + public string? UniqueName { get { if (IsEphemeral) return null; - string uniqueName = _keyHandle.GetPropertyAsString(KeyPropertyName.UniqueName, CngPropertyOptions.None); + string? uniqueName = _keyHandle.GetPropertyAsString(KeyPropertyName.UniqueName, CngPropertyOptions.None); return uniqueName; } } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyBlobFormat.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyBlobFormat.cs index 80c58eb43d20a6..a5b682afb41b10 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyBlobFormat.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyBlobFormat.cs @@ -37,7 +37,7 @@ public string Format } } - public static bool operator ==(CngKeyBlobFormat left, CngKeyBlobFormat right) + public static bool operator ==(CngKeyBlobFormat? left, CngKeyBlobFormat? right) { if (object.ReferenceEquals(left, null)) { @@ -47,7 +47,7 @@ public string Format return left.Equals(right); } - public static bool operator !=(CngKeyBlobFormat left, CngKeyBlobFormat right) + public static bool operator !=(CngKeyBlobFormat? left, CngKeyBlobFormat? right) { if (object.ReferenceEquals(left, null)) { @@ -57,14 +57,14 @@ public string Format return !left.Equals(right); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { Debug.Assert(_format != null); return Equals(obj as CngKeyBlobFormat); } - public bool Equals(CngKeyBlobFormat other) + public bool Equals(CngKeyBlobFormat? other) { if (object.ReferenceEquals(other, null)) { @@ -155,14 +155,14 @@ public static CngKeyBlobFormat Pkcs8PrivateBlob } - private static CngKeyBlobFormat s_eccPrivate; - private static CngKeyBlobFormat s_eccPublic; - private static CngKeyBlobFormat s_eccFullPrivate; - private static CngKeyBlobFormat s_eccFullPublic; - private static CngKeyBlobFormat s_genericPrivate; - private static CngKeyBlobFormat s_genericPublic; - private static CngKeyBlobFormat s_opaqueTransport; - private static CngKeyBlobFormat s_pkcs8Private; + private static CngKeyBlobFormat? s_eccPrivate; + private static CngKeyBlobFormat? s_eccPublic; + private static CngKeyBlobFormat? s_eccFullPrivate; + private static CngKeyBlobFormat? s_eccFullPublic; + private static CngKeyBlobFormat? s_genericPrivate; + private static CngKeyBlobFormat? s_genericPublic; + private static CngKeyBlobFormat? s_opaqueTransport; + private static CngKeyBlobFormat? s_pkcs8Private; private readonly string _format; } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyCreationParameters.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyCreationParameters.cs index dac86de952e4eb..affae1f3d9386b 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyCreationParameters.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyCreationParameters.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Security.Cryptography { @@ -65,8 +66,8 @@ public CngProvider Provider /// /// Settings for UI shown on access to the key /// - public CngUIPolicy UIPolicy { get; set; } + public CngUIPolicy? UIPolicy { get; set; } - private CngProvider _provider; + private CngProvider _provider = null!; // will be initialized via property setter called by ctor } } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngPkcs8.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngPkcs8.cs index af6a209532e011..092b824d41f17b 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngPkcs8.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngPkcs8.cs @@ -17,7 +17,7 @@ internal struct Pkcs8Response internal string GetAlgorithmGroup() { - return Key.AlgorithmGroup.AlgorithmGroup; + return Key.AlgorithmGroup!.AlgorithmGroup; } internal void FreeKey() diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProperty.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProperty.cs index 7d00555788986b..0be24b19616a23 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProperty.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProperty.cs @@ -16,7 +16,7 @@ namespace System.Security.Cryptography [StructLayout(LayoutKind.Sequential)] // The [StructLayout] is here to prevent a spurious ApiReviewer alert. We do not actually depend on the layout of this struct. public struct CngProperty : IEquatable { - public CngProperty(string name, byte[] value, CngPropertyOptions options) + public CngProperty(string name, byte[]? value, CngPropertyOptions options) : this() { if (name == null) @@ -37,7 +37,7 @@ public CngProperty(string name, byte[] value, CngPropertyOptions options) /// Contents of the property /// /// - public byte[] GetValue() + public byte[]? GetValue() { return (_value == null) ? null : _value.CloneByteArray(); } @@ -47,7 +47,7 @@ public byte[] GetValue() /// public CngPropertyOptions Options { get; private set; } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is CngProperty && Equals((CngProperty)obj); } @@ -117,12 +117,12 @@ public override int GetHashCode() return !left.Equals(right); } - internal byte[] GetValueWithoutCopying() + internal byte[]? GetValueWithoutCopying() { return _value; } - private readonly byte[] _value; + private readonly byte[]? _value; private int? _lazyHashCode; } } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProvider.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProvider.cs index 6ab58edf5542d5..1e9b0cb93a2469 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProvider.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProvider.cs @@ -36,7 +36,7 @@ public string Provider } } - public static bool operator ==(CngProvider left, CngProvider right) + public static bool operator ==(CngProvider? left, CngProvider? right) { if (object.ReferenceEquals(left, null)) return object.ReferenceEquals(right, null); @@ -44,7 +44,7 @@ public string Provider return left.Equals(right); } - public static bool operator !=(CngProvider left, CngProvider right) + public static bool operator !=(CngProvider? left, CngProvider? right) { if (object.ReferenceEquals(left, null)) { @@ -54,14 +54,14 @@ public string Provider return !left.Equals(right); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { Debug.Assert(_provider != null); return Equals(obj as CngProvider); } - public bool Equals(CngProvider other) + public bool Equals(CngProvider? other) { if (object.ReferenceEquals(other, null)) { @@ -103,8 +103,8 @@ public static CngProvider MicrosoftSoftwareKeyStorageProvider } } - private static CngProvider s_msSmartCardKsp; - private static CngProvider s_msSoftwareKsp; + private static CngProvider? s_msSmartCardKsp; + private static CngProvider? s_msSoftwareKsp; private readonly string _provider; } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngUIPolicy.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngUIPolicy.cs index 6ff8d5e764b956..11bfd29a4db07c 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngUIPolicy.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngUIPolicy.cs @@ -17,22 +17,22 @@ public CngUIPolicy(CngUIProtectionLevels protectionLevel) { } - public CngUIPolicy(CngUIProtectionLevels protectionLevel, string friendlyName) + public CngUIPolicy(CngUIProtectionLevels protectionLevel, string? friendlyName) : this(protectionLevel, friendlyName, description: null) { } - public CngUIPolicy(CngUIProtectionLevels protectionLevel, string friendlyName, string description) + public CngUIPolicy(CngUIProtectionLevels protectionLevel, string? friendlyName, string? description) : this(protectionLevel, friendlyName, description, useContext: null) { } - public CngUIPolicy(CngUIProtectionLevels protectionLevel, string friendlyName, string description, string useContext) + public CngUIPolicy(CngUIProtectionLevels protectionLevel, string? friendlyName, string? description, string? useContext) : this(protectionLevel, friendlyName, description, useContext, creationTitle: null) { } - public CngUIPolicy(CngUIProtectionLevels protectionLevel, string friendlyName, string description, string useContext, string creationTitle) + public CngUIPolicy(CngUIProtectionLevels protectionLevel, string? friendlyName, string? description, string? useContext, string? creationTitle) { ProtectionLevel = protectionLevel; FriendlyName = friendlyName; @@ -50,21 +50,21 @@ public CngUIPolicy(CngUIProtectionLevels protectionLevel, string friendlyName, s /// Friendly name to describe the key with in the dialog box that appears when the key is accessed, /// null for default name /// - public string FriendlyName { get; private set; } + public string? FriendlyName { get; private set; } /// /// Description text displayed in the dialog box when the key is accessed, null for the default text /// - public string Description { get; private set; } + public string? Description { get; private set; } /// /// Description of how the key will be used /// - public string UseContext { get; private set; } + public string? UseContext { get; private set; } /// /// Title of the dialog box displayed when a newly created key is finalized, null for the default title /// - public string CreationTitle { get; private set; } + public string? CreationTitle { get; private set; } } } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECCng.ImportExport.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECCng.ImportExport.cs index ac943badce7e06..48c3178896326c 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECCng.ImportExport.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECCng.ImportExport.cs @@ -42,7 +42,7 @@ internal static byte[] ExportKeyBlob( CngKey key, bool includePrivateParameters, out CngKeyBlobFormat format, - out string curveName) + out string? curveName) { curveName = key.GetCurveName(out _); bool forceGenericBlob = false; diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs index 166e87cf804312..c54c5d9806ffe5 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs @@ -99,7 +99,7 @@ public override void GenerateKey(ECCurve curve) private CngKey GetKey() { - CngKey key = null; + CngKey key; if (_core.IsKeyGeneratedNamedCurve()) { @@ -108,7 +108,7 @@ private CngKey GetKey() } else { - CngAlgorithm algorithm = null; + CngAlgorithm algorithm; int keySize = 0; // Map the current key size to a CNG algorithm name diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.cs index ae7242fd035564..a06ccd3800e27f 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.cs @@ -24,11 +24,11 @@ public sealed partial class ECDiffieHellmanCng : ECDiffieHellman private CngAlgorithmCore _core = new CngAlgorithmCore(nameof(ECDiffieHellmanCng)) { DefaultKeyType = CngAlgorithm.ECDiffieHellman }; private CngAlgorithm _hashAlgorithm = CngAlgorithm.Sha256; private ECDiffieHellmanKeyDerivationFunction _kdf = ECDiffieHellmanKeyDerivationFunction.Hash; - private byte[] _hmacKey; - private byte[] _label; - private byte[] _secretAppend; - private byte[] _secretPrepend; - private byte[] _seed; + private byte[]? _hmacKey; + private byte[]? _label; + private byte[]? _secretAppend; + private byte[]? _secretPrepend; + private byte[]? _seed; public ECDiffieHellmanCng(CngKey key) { @@ -86,7 +86,7 @@ public ECDiffieHellmanKeyDerivationFunction KeyDerivationFunction /// /// Key used with the HMAC KDF /// - public byte[] HmacKey + public byte[]? HmacKey { get { return _hmacKey; } set { _hmacKey = value; } @@ -95,7 +95,7 @@ public byte[] HmacKey /// /// Label bytes used for the TLS KDF /// - public byte[] Label + public byte[]? Label { get { return _label; } set { _label = value; } @@ -104,7 +104,7 @@ public byte[] Label /// /// Bytes to append to the raw secret agreement before processing by the KDF /// - public byte[] SecretAppend + public byte[]? SecretAppend { get { return _secretAppend; } set { _secretAppend = value; } @@ -113,7 +113,7 @@ public byte[] SecretAppend /// /// Bytes to prepend to the raw secret agreement before processing by the KDF /// - public byte[] SecretPrepend + public byte[]? SecretPrepend { get { return _secretPrepend; } set { _secretPrepend = value; } @@ -122,7 +122,7 @@ public byte[] SecretPrepend /// /// Seed bytes used for the TLS KDF /// - public byte[] Seed + public byte[]? Seed { get { return _seed; } set { _seed = value; } @@ -151,7 +151,7 @@ private void DisposeKey() _core.DisposeKey(); } - internal string GetCurveName(out string oidValue) + internal string? GetCurveName(out string? oidValue) { return Key.GetCurveName(out oidValue); } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs index 36a01de4dde5ab..78f6520146951f 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs @@ -10,13 +10,13 @@ namespace System.Security.Cryptography public sealed partial class ECDiffieHellmanCngPublicKey : ECDiffieHellmanPublicKey { private readonly CngKeyBlobFormat _format; - private readonly string _curveName; + private readonly string? _curveName; private bool _disposed; /// /// Wrap a CNG key /// - internal ECDiffieHellmanCngPublicKey(byte[] keyBlob, string curveName, CngKeyBlobFormat format) : base(keyBlob) + internal ECDiffieHellmanCngPublicKey(byte[] keyBlob, string? curveName, CngKeyBlobFormat format) : base(keyBlob) { _format = format; // Can be null for P256, P384, P521, or an explicit blob @@ -79,7 +79,7 @@ public static ECDiffieHellmanPublicKey FromByteArray(byte[] publicKeyBlob, CngKe internal static ECDiffieHellmanCngPublicKey FromKey(CngKey key) { CngKeyBlobFormat format; - string curveName; + string? curveName; byte[] blob = ECCng.ExportKeyBlob(key, false, out format, out curveName); return new ECDiffieHellmanCngPublicKey(blob, curveName, format); } @@ -133,7 +133,7 @@ public override ECParameters ExportParameters() using (CngKey key = Import()) { ECParameters ecparams = default; - string curveName = key.GetCurveName(out _); + string? curveName = key.GetCurveName(out _); if (string.IsNullOrEmpty(curveName)) { diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.Key.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.Key.cs index a4ca9ace74b2b5..7a6e4cececa3f3 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.Key.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.Key.cs @@ -88,7 +88,7 @@ public override void GenerateKey(ECCurve curve) private CngKey GetKey() { - CngKey key = null; + CngKey? key = null; if (_core.IsKeyGeneratedNamedCurve()) { @@ -97,7 +97,7 @@ private CngKey GetKey() } else { - CngAlgorithm algorithm = null; + CngAlgorithm? algorithm = null; int keySize = 0; // Map the current key size to a CNG algorithm name diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.cs index 584614303b8168..f34da3301443b5 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.cs @@ -62,7 +62,7 @@ private void DisposeKey() _core.DisposeKey(); } - private static bool IsEccAlgorithmGroup(CngAlgorithmGroup algorithmGroup) + private static bool IsEccAlgorithmGroup(CngAlgorithmGroup? algorithmGroup) { // Sometimes, when reading from certificates, ECDSA keys get identified as ECDH. // Windows allows the ECDH keys to perform both key exchange (ECDH) and signing (ECDSA), @@ -72,7 +72,7 @@ private static bool IsEccAlgorithmGroup(CngAlgorithmGroup algorithmGroup) return algorithmGroup == CngAlgorithmGroup.ECDsa || algorithmGroup == CngAlgorithmGroup.ECDiffieHellman; } - internal string GetCurveName(out string oidValue) + internal string? GetCurveName(out string? oidValue) { return Key.GetCurveName(out oidValue); } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/TripleDESCng.cs b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/TripleDESCng.cs index 78cd22c1df4565..a3c25f7c540499 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/TripleDESCng.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/TripleDESCng.cs @@ -68,7 +68,7 @@ public override ICryptoTransform CreateDecryptor() return _core.CreateDecryptor(); } - public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { return _core.CreateDecryptor(rgbKey, rgbIV); } @@ -79,7 +79,7 @@ public override ICryptoTransform CreateEncryptor() return _core.CreateEncryptor(); } - public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { return _core.CreateEncryptor(rgbKey, rgbIV); }