nullable annotate System.Security.Cryptography.Cng
authorEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Mon, 10 Feb 2020 19:33:28 +0000 (19:33 +0000)
committerEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Fri, 14 Feb 2020 15:32:53 +0000 (15:32 +0000)
39 files changed:
src/libraries/Common/src/Internal/Cryptography/HashProviderCng.cs
src/libraries/Common/src/Interop/Windows/BCrypt/BCryptAlgorithmCache.cs
src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptOpenAlgorithmProvider.cs
src/libraries/Common/src/System/Security/Cryptography/ECCng.HashAlgorithm.cs
src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.cs
src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs
src/libraries/Common/src/System/Security/Cryptography/PasswordBasedEncryption.cs
src/libraries/System.Security.Cryptography.Cng/ref/System.Security.Cryptography.Cng.cs
src/libraries/System.Security.Cryptography.Cng/ref/System.Security.Cryptography.Cng.csproj
src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/BasicSymmetricCipherNCrypt.cs
src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngAlgorithmCore.cs
src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/CngSymmetricAlgorithmCore.cs
src/libraries/System.Security.Cryptography.Cng/src/Internal/Cryptography/Helpers.cs
src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs
src/libraries/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/AesCng.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithm.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngAlgorithmGroup.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Create.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.EC.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Exists.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Export.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Import.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.OpenHandle.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.Properties.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKey.StandardProperties.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyBlobFormat.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngKeyCreationParameters.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngPkcs8.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProperty.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngProvider.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/CngUIPolicy.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECCng.ImportExport.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.Key.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCng.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDiffieHellmanCngPublicKey.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.Key.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/ECDsaCng.cs
src/libraries/System.Security.Cryptography.Cng/src/System/Security/Cryptography/TripleDESCng.cs

index 4102f7f..a70eb59 100644 (file)
@@ -26,7 +26,7 @@ namespace Internal.Cryptography
         {
         }
 
-        internal HashProviderCng(string hashAlgId, ReadOnlySpan<byte> key, bool isHmac)
+        internal HashProviderCng(string? hashAlgId, ReadOnlySpan<byte> key, bool isHmac)
         {
             BCryptOpenAlgorithmProviderFlags dwFlags = BCryptOpenAlgorithmProviderFlags.None;
             if (isHmac)
index 30e40b2..f45f577 100644 (file)
@@ -18,7 +18,7 @@ internal partial class Interop
             /// <summary>
             ///     Return a SafeBCryptAlgorithmHandle of the desired algorithm and flags. This is a shared handle so do not dispose it!
             /// </summary>
-            public static SafeBCryptAlgorithmHandle GetCachedBCryptAlgorithmHandle(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags)
+            public static SafeBCryptAlgorithmHandle GetCachedBCryptAlgorithmHandle(string? hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags)
             {
                 // There aren't that many hash algorithms around so rather than use a LowLevelDictionary and guard it with a lock,
                 // we'll use a simple list. To avoid locking, we'll recreate the entire list each time an entry is added and replace it atomically.
@@ -53,7 +53,7 @@ internal partial class Interop
 
             private struct Entry
             {
-                public Entry(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, SafeBCryptAlgorithmHandle handle)
+                public Entry(string? hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, SafeBCryptAlgorithmHandle handle)
                     : this()
                 {
                     HashAlgorithmId = hashAlgorithmId;
@@ -61,7 +61,7 @@ internal partial class Interop
                     Handle = handle;
                 }
 
-                public string HashAlgorithmId { get; private set; }
+                public string? HashAlgorithmId { get; private set; }
                 public BCryptOpenAlgorithmProviderFlags Flags { get; private set; }
                 public SafeBCryptAlgorithmHandle Handle { get; private set; }
             }
index 1d1372b..d4a52fd 100644 (file)
@@ -14,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
index 7c58da4..799cc0b 100644 (file)
@@ -16,7 +16,7 @@ namespace System.Security.Cryptography
         /// </summary>
         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;
             }
index 20cef43..81ee01f 100644 (file)
@@ -115,7 +115,7 @@ namespace System.Security.Cryptography
                     return Interop.NCrypt.DeriveKeyMaterialHmac(
                         secretAgreement,
                         hashAlgorithm.Name,
-                        hmacKey,
+                        hmacKey!,
                         secretPrepend,
                         secretAppend,
                         flags);
index 8ffac24..4523de5 100644 (file)
@@ -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;
@@ -203,7 +204,7 @@ namespace System.Security.Cryptography
             ReadOnlySpan<byte> passwordBytes,
             KeyReader<TRet> keyReader,
             out int bytesRead,
-            out TRet ret)
+            [MaybeNull] out TRet ret)
         {
             AsnValueReader reader = new AsnValueReader(source.Span, AsnEncodingRules.BER);
             int read = reader.PeekEncodedValue().Length;
index 5780e28..678e989 100644 (file)
@@ -326,6 +326,8 @@ namespace System.Security.Cryptography
                 }
             }
 
+            Debug.Assert(pwdTmpBytes != null);
+
             fixed (byte* pkcs8RentPin = sourceRent)
             fixed (byte* pwdTmpBytesPtr = pwdTmpBytes)
             {
index 49ed6ff..f9d3d3e 100644 (file)
@@ -43,9 +43,9 @@ namespace System.Security.Cryptography
         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 @@ namespace System.Security.Cryptography
         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<System.Security.Cryptography.CngAlgorithmGroup>
@@ -84,11 +84,11 @@ namespace System.Security.Cryptography
         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 @@ namespace System.Security.Cryptography
     {
         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 @@ namespace System.Security.Cryptography
         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 @@ namespace System.Security.Cryptography
         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 @@ namespace System.Security.Cryptography
     {
         private object _dummy;
         private int _dummyPrimitive;
-        public CngProperty(string name, byte[] value, System.Security.Cryptography.CngPropertyOptions options) { throw null; }
+        public CngProperty(string name, byte[]? value, System.Security.Cryptography.CngPropertyOptions options) { throw null; }
         public readonly string Name { get { throw null; } }
-        public readonly System.Security.Cryptography.CngPropertyOptions Options {  get { throw null; } }
-        public override bool Equals(object obj) { throw null; }
+        public readonly System.Security.Cryptography.CngPropertyOptions Options { get { throw null; } }
+        public override bool Equals(object? obj) { throw null; }
         public bool Equals(System.Security.Cryptography.CngProperty other) { throw null; }
         public override int GetHashCode() { throw null; }
-        public byte[] GetValue() { throw null; }
+        public byte[]? GetValue() { throw null; }
         public static bool operator ==(System.Security.Cryptography.CngProperty left, System.Security.Cryptography.CngProperty right) { throw null; }
         public static bool operator !=(System.Security.Cryptography.CngProperty left, System.Security.Cryptography.CngProperty right) { throw null; }
     }
@@ -227,25 +228,25 @@ namespace System.Security.Cryptography
         public static System.Security.Cryptography.CngProvider MicrosoftSmartCardKeyStorageProvider { get { throw null; } }
         public static System.Security.Cryptography.CngProvider MicrosoftSoftwareKeyStorageProvider { get { throw null; } }
         public string Provider { get { throw null; } }
-        public override bool Equals(object obj) { throw null; }
-        public bool Equals(System.Security.Cryptography.CngProvider other) { throw null; }
+        public override bool Equals(object? obj) { throw null; }
+        public bool Equals(System.Security.Cryptography.CngProvider? other) { throw null; }
         public override int GetHashCode() { throw null; }
-        public static bool operator ==(System.Security.Cryptography.CngProvider left, System.Security.Cryptography.CngProvider right) { throw null; }
-        public static bool operator !=(System.Security.Cryptography.CngProvider left, System.Security.Cryptography.CngProvider right) { throw null; }
+        public static bool operator ==(System.Security.Cryptography.CngProvider? left, System.Security.Cryptography.CngProvider? right) { throw null; }
+        public static bool operator !=(System.Security.Cryptography.CngProvider? left, System.Security.Cryptography.CngProvider? right) { throw null; }
         public override string ToString() { throw null; }
     }
     public sealed partial class CngUIPolicy
     {
         public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel) { }
-        public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel, string friendlyName) { }
-        public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel, string friendlyName, string description) { }
-        public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel, string friendlyName, string description, string useContext) { }
-        public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel, string friendlyName, string description, string useContext, string creationTitle) { }
-        public string CreationTitle { get { throw null; } }
-        public string Description { get { throw null; } }
-        public string FriendlyName { get { throw null; } }
+        public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel, string? friendlyName) { }
+        public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel, string? friendlyName, string? description) { }
+        public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel, string? friendlyName, string? description, string? useContext) { }
+        public CngUIPolicy(System.Security.Cryptography.CngUIProtectionLevels protectionLevel, string? friendlyName, string? description, string? useContext, string? creationTitle) { }
+        public string? CreationTitle { get { throw null; } }
+        public string? Description { get { throw null; } }
+        public string? FriendlyName { get { throw null; } }
         public System.Security.Cryptography.CngUIProtectionLevels ProtectionLevel { get { throw null; } }
-        public string UseContext { get { throw null; } }
+        public string? UseContext { get { throw null; } }
     }
     [System.FlagsAttribute]
     public enum CngUIProtectionLevels
@@ -260,7 +261,7 @@ namespace System.Security.Cryptography
         public DSACng(int keySize) { }
         public DSACng(System.Security.Cryptography.CngKey key) { }
         public System.Security.Cryptography.CngKey Key { get { throw null; } }
-        public override string KeyExchangeAlgorithm { get { throw null; } }
+        public override string? KeyExchangeAlgorithm { get { throw null; } }
         public override System.Security.Cryptography.KeySizes[] LegalKeySizes { get { throw null; } }
         public override string SignatureAlgorithm { get { throw null; } }
         public override byte[] CreateSignature(byte[] rgbHash) { throw null; }
@@ -282,15 +283,15 @@ namespace System.Security.Cryptography
         public ECDiffieHellmanCng(System.Security.Cryptography.ECCurve curve) { }
 #endif
         public System.Security.Cryptography.CngAlgorithm HashAlgorithm { get { throw null; } set { } }
-        public byte[] HmacKey { get { throw null; } set { } }
+        public byte[]? HmacKey { get { throw null; } set { } }
         public System.Security.Cryptography.CngKey Key { get { throw null; } }
         public System.Security.Cryptography.ECDiffieHellmanKeyDerivationFunction KeyDerivationFunction { get { throw null; } set { } }
         public override int KeySize { get { throw null; } set { } }
-        public byte[] Label { get { throw null; } set { } }
+        public byte[]? Label { get { throw null; } set { } }
         public override System.Security.Cryptography.ECDiffieHellmanPublicKey PublicKey { get { throw null; } }
-        public byte[] SecretAppend { get { throw null; } set { } }
-        public byte[] SecretPrepend { get { throw null; } set { } }
-        public byte[] Seed { get { throw null; } set { } }
+        public byte[]? SecretAppend { get { throw null; } set { } }
+        public byte[]? SecretPrepend { get { throw null; } set { } }
+        public byte[]? Seed { get { throw null; } set { } }
         public bool UseSecretAgreementAsHmacKey { get { throw null; } }
 #if FEATURE_ECDH_DERIVEFROM
         public override byte[] DeriveKeyFromHash(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, byte[] secretPrepend, byte[] secretAppend) { throw null; }
@@ -402,9 +403,9 @@ namespace System.Security.Cryptography
         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() { }
index be197ef..4459f60 100644 (file)
@@ -9,6 +9,7 @@
     <TargetFrameworks>netcoreapp3.0;netstandard2.1;net461;net462;net47;$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
     <ExcludeCurrentNetCoreAppFromPackage>true</ExcludeCurrentNetCoreAppFromPackage>
     <ExcludeCurrentFullFrameworkFromPackage>true</ExcludeCurrentFullFrameworkFromPackage>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="System.Security.Cryptography.Cng.cs" />
index b3f443d..ddcde47 100644 (file)
@@ -51,7 +51,7 @@ namespace Internal.Cryptography
 
             int numBytesWritten;
             ErrorCode errorCode;
-            using (SafeNCryptKeyHandle keyHandle = _cngKey.Handle)
+            using (SafeNCryptKeyHandle keyHandle = _cngKey!.Handle)
             {
                 var inputSpan = new ReadOnlySpan<byte>(input, inputOffset, count);
                 var outputSpan = new Span<byte>(output, outputOffset, count);
@@ -116,11 +116,11 @@ namespace Internal.Cryptography
             if (IV != null)
             {
                 CngProperty prop = new CngProperty(Interop.NCrypt.NCRYPT_INITIALIZATION_VECTOR, IV, CngPropertyOptions.None);
-                _cngKey.SetProperty(prop);
+                _cngKey!.SetProperty(prop);
             }
         }
 
-        private CngKey _cngKey;
+        private CngKey? _cngKey;
         private readonly bool _encrypting;
 
         private static readonly CngProperty s_ECBMode =
index 72bd377..e9ea410 100644 (file)
@@ -17,7 +17,7 @@ namespace Internal.Cryptography
     {
         private readonly string _disposedName;
         public CngAlgorithm DefaultKeyType;
-        private CngKey _lazyKey;
+        private CngKey? _lazyKey;
         private bool _disposed;
 
         public CngAlgorithmCore(string disposedName) : this()
index 20449c4..58c05c6 100644 (file)
@@ -110,12 +110,12 @@ namespace Internal.Cryptography
             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 @@ namespace Internal.Cryptography
             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 @@ namespace Internal.Cryptography
 
             // 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 @@ namespace Internal.Cryptography
         {
             Debug.Assert(!KeyInPlainText);
 
-            return CngKey.Open(_keyName, _provider, _optionOptions);
+            return CngKey.Open(_keyName!, _provider!, _optionOptions);
         }
 
         private bool KeyInPlainText
@@ -197,8 +197,8 @@ namespace Internal.Cryptography
         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;
index 4a43665..9967ecf 100644 (file)
@@ -21,7 +21,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())
             {
@@ -50,7 +50,7 @@ namespace Internal.Cryptography
         //
         // 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 @@ namespace Internal.Cryptography
         /// null - if property not defined on key.
         /// throws - for any other type of error.
         /// </returns>
-        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 @@ namespace Internal.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.)
         /// </summary>
-        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 @@ namespace Internal.Cryptography
             {
                 fixed (byte* pValue = &value[0])
                 {
-                    string valueAsString = Marshal.PtrToStringUni((IntPtr)pValue);
+                    string? valueAsString = Marshal.PtrToStringUni((IntPtr)pValue);
                     return valueAsString;
                 }
             }
@@ -127,7 +127,7 @@ namespace Internal.Cryptography
         /// </summary>
         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);
index 65382f3..94d3f0f 100644 (file)
@@ -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 @@ namespace Microsoft.Win32.SafeHandles
         /// <summary>
         ///     If the handle is a Duplicate, this points at the safe handle which actually owns the native handle.
         /// </summary>
-        private SafeNCryptHandle _holder;
+        private SafeNCryptHandle? _holder;
 
-        private SafeHandle _parentHandle;
+        private SafeHandle? _parentHandle;
 
         protected SafeNCryptHandle() : base(true)
         {
@@ -98,6 +99,7 @@ namespace Microsoft.Win32.SafeHandles
         /// <summary>
         ///     Wrapper for the _holder field which ensures that we're in a consistent state
         /// </summary>
+        [MaybeNull]
         private SafeNCryptHandle Holder
         {
             get
@@ -166,7 +168,7 @@ namespace Microsoft.Win32.SafeHandles
                         {
                             if (acquiredHolder)
                             {
-                                Holder.DangerousRelease();
+                                Holder!.DangerousRelease();
                             }
                         }
 
@@ -239,7 +241,7 @@ namespace Microsoft.Win32.SafeHandles
             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 @@ namespace Microsoft.Win32.SafeHandles
         {
             if (_ownershipState == OwnershipState.Duplicate)
             {
-                Holder.DangerousRelease();
+                Holder!.DangerousRelease();
                 return true;
             }
             else if (_parentHandle != null)
index 003b88e..b7430d7 100644 (file)
@@ -8,6 +8,7 @@
     <TargetFrameworks>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</TargetFrameworks>
     <ExcludeCurrentNetCoreAppFromPackage>true</ExcludeCurrentNetCoreAppFromPackage>
     <ExcludeCurrentFullFrameworkFromPackage>true</ExcludeCurrentFullFrameworkFromPackage>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
   <PropertyGroup Condition="'$(TargetsWindows)' != 'true'">
     <GeneratePlatformNotSupportedAssemblyMessage>SR.PlatformNotSupported_CryptographyCng</GeneratePlatformNotSupportedAssemblyMessage>
index 202ad17..4feb3b1 100644 (file)
@@ -67,7 +67,7 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             return _core.CreateEncryptor();
         }
 
-        public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
+        public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV)
         {
             return _core.CreateEncryptor(rgbKey, rgbIV);
         }
index d288409..cababd9 100644 (file)
@@ -36,7 +36,7 @@ namespace System.Security.Cryptography
             }
         }
 
-        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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             }
         }
 
-        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;
     }
index 2195835..b67ade6 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Security.Cryptography
             }
         }
 
-        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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             }
         }
 
-        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;
     }
index e0190d1..506cc95 100644 (file)
@@ -24,12 +24,12 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
                         throw errorCode.ToCryptographicException();
                 }
 
-                CngUIPolicy uiPolicy = creationParameters.UIPolicy;
+                CngUIPolicy? uiPolicy = creationParameters.UIPolicy;
                 if (uiPolicy != null)
                 {
                     InitializeKeyUiPolicyProperties(keyHandle, uiPolicy);
@@ -104,7 +104,7 @@ namespace System.Security.Cryptography
                 // 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 @@ namespace System.Security.Cryptography
                         throw errorCode.ToCryptographicException();
                 }
 
-                string useContext = uiPolicy.UseContext;
+                string? useContext = uiPolicy.UseContext;
                 if (useContext != null)
                 {
                     int useContextByteLength = checked((useContext.Length + 1) * sizeof(char));
index 3065f22..b6570d3 100644 (file)
@@ -26,7 +26,7 @@ namespace System.Security.Cryptography
                 algorithm == CngAlgorithm.ECDsa.Algorithm);
         }
 
-        internal string GetCurveName(out string oidValue)
+        internal string? GetCurveName(out string? oidValue)
         {
             if (IsECNamedCurve())
             {
@@ -72,7 +72,7 @@ namespace System.Security.Cryptography
         /// </summary>
         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 @@ namespace System.Security.Cryptography
             return CngAlgorithm.ECDiffieHellman;
         }
 
-        internal static CngKey Create(ECCurve curve, Func<string, CngAlgorithm> algorithmResolver)
+        internal static CngKey Create(ECCurve curve, Func<string?, CngAlgorithm> algorithmResolver)
         {
             System.Diagnostics.Debug.Assert(algorithmResolver != null);
 
@@ -199,7 +199,7 @@ namespace System.Security.Cryptography
                 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);
                 }
 
index 7b4f1f5..d8ca057 100644 (file)
@@ -39,7 +39,7 @@ namespace System.Security.Cryptography
 
             using (SafeNCryptProviderHandle providerHandle = provider.OpenStorageProvider())
             {
-                SafeNCryptKeyHandle keyHandle = null;
+                SafeNCryptKeyHandle? keyHandle = null;
                 try
                 {
                     ErrorCode errorCode = Interop.NCrypt.NCryptOpenKey(providerHandle, out keyHandle, keyName, 0, options);
index 9006103..26627f4 100644 (file)
@@ -91,15 +91,16 @@ namespace System.Security.Cryptography
             int kdfCount)
         {
             bool ret = ExportPkcs8KeyBlob(
-                true,
+                allocate: true,
                 _keyHandle,
                 password,
                 kdfCount,
                 Span<byte>.Empty,
                 out _,
-                out byte[] allocated);
+                out byte[]? allocated);
 
             Debug.Assert(ret);
+            Debug.Assert(allocated != null); // on account of allocate parameter being true
             return allocated;
         }
 
@@ -130,7 +131,7 @@ namespace System.Security.Cryptography
             int kdfCount,
             Span<byte> destination,
             out int bytesWritten,
-            out byte[] allocated)
+            out byte[]? allocated)
         {
             using (SafeUnicodeStringHandle stringHandle = new SafeUnicodeStringHandle(password))
             {
@@ -226,7 +227,7 @@ namespace System.Security.Cryptography
                     {
                         byte[] trimmed = new byte[numBytesNeeded];
                         destination.Slice(0, numBytesNeeded).CopyTo(trimmed);
-                        Array.Clear(allocated, 0, numBytesNeeded);
+                        Array.Clear(allocated!, 0, numBytesNeeded);
                         allocated = trimmed;
                     }
 
index 27eb685..90585f8 100644 (file)
@@ -29,7 +29,7 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
 
         internal static CngKey Import(
             byte[] keyBlob,
-            string curveName,
+            string? curveName,
             CngKeyBlobFormat format,
             CngProvider provider)
         {
@@ -113,7 +113,7 @@ namespace System.Security.Cryptography
 
         internal static CngKey Import(
             ReadOnlySpan<byte> keyBlob,
-            string curveName,
+            string? curveName,
             CngKeyBlobFormat format,
             CngProvider provider)
         {
@@ -123,7 +123,7 @@ namespace System.Security.Cryptography
                 throw new ArgumentNullException(nameof(provider));
 
             SafeNCryptProviderHandle providerHandle = provider.OpenStorageProvider();
-            SafeNCryptKeyHandle keyHandle = null;
+            SafeNCryptKeyHandle? keyHandle = null;
             ErrorCode errorCode;
 
             if (curveName == null)
index 0be5ef6..4215ee3 100644 (file)
@@ -31,7 +31,7 @@ namespace System.Security.Cryptography
             providerHandle.SetHandleValue(rawProviderHandle);
 
             // Set up a key object wrapping the handle
-            CngKey key = null;
+            CngKey? key = null;
             try
             {
                 key = new CngKey(providerHandle, keyHandleCopy);
index b0abfe2..0e7716f 100644 (file)
@@ -21,7 +21,7 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
         {
             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)
index 98cfc82..0a375c6 100644 (file)
@@ -29,7 +29,7 @@ namespace System.Security.Cryptography
         {
             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 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     Name of the algorithm this key can be used with
         /// </summary>
-        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 @@ namespace System.Security.Cryptography
         ///     the CLR. Other ephemeral keys, such as those imported by handle, will get a CryptographicException
         ///     if they read this property.
         /// </summary>
-        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 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     KSP which holds this key
         /// </summary>
-        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 @@ namespace System.Security.Cryptography
             get
             {
                 CngUIProtectionLevels uiProtectionLevel;
-                string friendlyName;
-                string description;
-                string creationTitle;
+                string? friendlyName;
+                string? description;
+                string? creationTitle;
                 unsafe
                 {
                     int numBytesNeeded;
@@ -300,7 +300,7 @@ namespace System.Security.Cryptography
                     }
                 }
 
-                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 @@ namespace System.Security.Cryptography
         ///     Unique name of the key, null if it is ephemeral. See the comments on the Name property for
         ///     details about names of ephemeral keys.
         /// </summary>
-        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;
             }
         }
index 80c58eb..a5b682a 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Security.Cryptography
             }
         }
 
-        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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
         }
 
 
-        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;
     }
index dac86de..6d40724 100644 (file)
@@ -4,6 +4,7 @@
 
 using System;
 using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 
 namespace System.Security.Cryptography
 {
@@ -46,6 +47,7 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     KSP to create the key in
         /// </summary>
+        [MaybeNull]
         public CngProvider Provider
         {
             get
@@ -65,8 +67,8 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     Settings for UI shown on access to the key
         /// </summary>
-        public CngUIPolicy UIPolicy { get; set; }
+        public CngUIPolicy? UIPolicy { get; set; }
 
-        private CngProvider _provider;
+        private CngProvider? _provider;
     }
 }
index af6a209..092b824 100644 (file)
@@ -17,7 +17,7 @@ namespace System.Security.Cryptography
 
             internal string GetAlgorithmGroup()
             {
-                return Key.AlgorithmGroup.AlgorithmGroup;
+                return Key.AlgorithmGroup!.AlgorithmGroup;
             }
 
             internal void FreeKey()
index 7d00555..0be24b1 100644 (file)
@@ -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<CngProperty>
     {
-        public CngProperty(string name, byte[] value, CngPropertyOptions options)
+        public CngProperty(string name, byte[]? value, CngPropertyOptions options)
             : this()
         {
             if (name == null)
@@ -37,7 +37,7 @@ namespace System.Security.Cryptography
         ///     Contents of the property
         /// </summary>
         /// <returns></returns>
-        public byte[] GetValue()
+        public byte[]? GetValue()
         {
             return (_value == null) ? null : _value.CloneByteArray();
         }
@@ -47,7 +47,7 @@ namespace System.Security.Cryptography
         /// </summary>
         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 @@ namespace System.Security.Cryptography
             return !left.Equals(right);
         }
 
-        internal byte[] GetValueWithoutCopying()
+        internal byte[]? GetValueWithoutCopying()
         {
             return _value;
         }
 
-        private readonly byte[] _value;
+        private readonly byte[]? _value;
         private int? _lazyHashCode;
     }
 }
index 6ab58ed..1e9b0cb 100644 (file)
@@ -36,7 +36,7 @@ namespace System.Security.Cryptography
             }
         }
 
-        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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             }
         }
 
-        private static CngProvider s_msSmartCardKsp;
-        private static CngProvider s_msSoftwareKsp;
+        private static CngProvider? s_msSmartCardKsp;
+        private static CngProvider? s_msSoftwareKsp;
 
         private readonly string _provider;
     }
index 6ff8d5e..11bfd29 100644 (file)
@@ -17,22 +17,22 @@ namespace System.Security.Cryptography
         {
         }
 
-        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 @@ namespace System.Security.Cryptography
         ///     Friendly name to describe the key with in the dialog box that appears when the key is accessed,
         ///     null for default name
         /// </summary>
-        public string FriendlyName { get; private set; }
+        public string? FriendlyName { get; private set; }
 
         /// <summary>
         ///     Description text displayed in the dialog box when the key is accessed, null for the default text
         /// </summary>
-        public string Description { get; private set; }
+        public string? Description { get; private set; }
 
         /// <summary>
         ///     Description of how the key will be used
         /// </summary>
-        public string UseContext { get; private set; }
+        public string? UseContext { get; private set; }
 
         /// <summary>
         ///     Title of the dialog box displayed when a newly created key is finalized, null for the default title
         /// </summary>
-        public string CreationTitle { get; private set; }
+        public string? CreationTitle { get; private set; }
     }
 }
index ac943ba..48c3178 100644 (file)
@@ -42,7 +42,7 @@ namespace System.Security.Cryptography
             CngKey key,
             bool includePrivateParameters,
             out CngKeyBlobFormat format,
-            out string curveName)
+            out string? curveName)
         {
             curveName = key.GetCurveName(out _);
             bool forceGenericBlob = false;
index 166e87c..233e132 100644 (file)
@@ -99,7 +99,7 @@ namespace System.Security.Cryptography
 
         private CngKey GetKey()
         {
-            CngKey key = null;
+            CngKey? key = null;
 
             if (_core.IsKeyGeneratedNamedCurve())
             {
@@ -108,7 +108,7 @@ namespace System.Security.Cryptography
             }
             else
             {
-                CngAlgorithm algorithm = null;
+                CngAlgorithm? algorithm = null;
                 int keySize = 0;
 
                 // Map the current key size to a CNG algorithm name
index ae7242f..a06ccd3 100644 (file)
@@ -24,11 +24,11 @@ namespace System.Security.Cryptography
         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 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     Key used with the HMAC KDF
         /// </summary>
-        public byte[] HmacKey
+        public byte[]? HmacKey
         {
             get { return _hmacKey; }
             set { _hmacKey = value; }
@@ -95,7 +95,7 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     Label bytes used for the TLS KDF
         /// </summary>
-        public byte[] Label
+        public byte[]? Label
         {
             get { return _label; }
             set { _label = value; }
@@ -104,7 +104,7 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     Bytes to append to the raw secret agreement before processing by the KDF
         /// </summary>
-        public byte[] SecretAppend
+        public byte[]? SecretAppend
         {
             get { return _secretAppend; }
             set { _secretAppend = value; }
@@ -113,7 +113,7 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     Bytes to prepend to the raw secret agreement before processing by the KDF
         /// </summary>
-        public byte[] SecretPrepend
+        public byte[]? SecretPrepend
         {
             get { return _secretPrepend; }
             set { _secretPrepend = value; }
@@ -122,7 +122,7 @@ namespace System.Security.Cryptography
         /// <summary>
         ///     Seed bytes used for the TLS KDF
         /// </summary>
-        public byte[] Seed
+        public byte[]? Seed
         {
             get { return _seed; }
             set { _seed = value; }
@@ -151,7 +151,7 @@ namespace System.Security.Cryptography
             _core.DisposeKey();
         }
 
-        internal string GetCurveName(out string oidValue)
+        internal string? GetCurveName(out string? oidValue)
         {
             return Key.GetCurveName(out oidValue);
         }
index 36a01de..78f6520 100644 (file)
@@ -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;
 
         /// <summary>
         /// Wrap a CNG key
         /// </summary>
-        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 @@ namespace System.Security.Cryptography
         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 @@ namespace System.Security.Cryptography
             using (CngKey key = Import())
             {
                 ECParameters ecparams = default;
-                string curveName = key.GetCurveName(out _);
+                string? curveName = key.GetCurveName(out _);
 
                 if (string.IsNullOrEmpty(curveName))
                 {
index a4ca9ac..7a6e4ce 100644 (file)
@@ -88,7 +88,7 @@ namespace System.Security.Cryptography
 
         private CngKey GetKey()
         {
-            CngKey key = null;
+            CngKey? key = null;
 
             if (_core.IsKeyGeneratedNamedCurve())
             {
@@ -97,7 +97,7 @@ namespace System.Security.Cryptography
             }
             else
             {
-                CngAlgorithm algorithm = null;
+                CngAlgorithm? algorithm = null;
                 int keySize = 0;
 
                 // Map the current key size to a CNG algorithm name
index 5846143..f34da33 100644 (file)
@@ -62,7 +62,7 @@ namespace System.Security.Cryptography
             _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 @@ namespace System.Security.Cryptography
             return algorithmGroup == CngAlgorithmGroup.ECDsa || algorithmGroup == CngAlgorithmGroup.ECDiffieHellman;
         }
 
-        internal string GetCurveName(out string oidValue)
+        internal string? GetCurveName(out string? oidValue)
         {
             return Key.GetCurveName(out oidValue);
         }
index 78cd22c..a3c25f7 100644 (file)
@@ -68,7 +68,7 @@ namespace System.Security.Cryptography
             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 @@ namespace System.Security.Cryptography
             return _core.CreateEncryptor();
         }
 
-        public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
+        public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV)
         {
             return _core.CreateEncryptor(rgbKey, rgbIV);
         }