// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
[DllImport(Libraries.Advapi32, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CryptAcquireContextW")]
public static extern bool CryptAcquireContext(
out SafeProvHandle phProv,
- string szContainer,
- string szProvider,
+ string? szContainer,
+ string? szProvider,
int dwProvType,
uint dwFlags);
}
SafeHashHandle hHash,
bool Final,
int dwFlags,
- byte[] pbData,
+ byte[]? pbData,
ref int pdwDataLen,
int dwBufLen);
}
SafeKeyHandle hExpKey,
int dwBlobType,
int dwFlags,
- [In, Out] byte[] pbData,
+ [In, Out] byte[]? pbData,
ref int dwDataLen);
}
}
int dwProvType,
IntPtr pdwReserved,
GetDefaultProviderFlags dwFlags,
- StringBuilder pszProvName,
+ StringBuilder? pszProvName,
ref int pcbProvName);
}
}
public static extern bool CryptGetKeyParam(
SafeKeyHandle hKey,
CryptGetKeyParamFlags dwParam,
- byte[] pbData,
+ byte[]? pbData,
ref int pdwDataLen,
int dwFlags);
}
public static extern bool CryptSignHash(
SafeHashHandle hHash,
KeySpec dwKeySpec,
- string szDescription,
+ string? szDescription,
CryptSignAndVerifyHashFlags dwFlags,
- [Out] byte[] pbSignature,
+ [Out] byte[]? pbSignature,
[In, Out] ref int pdwSigLen);
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CryptVerifySignatureW")]
byte[] pbSignature,
int dwSigLen,
SafeKeyHandle hPubKey,
- string szDescription,
+ string? szDescription,
CryptSignAndVerifyHashFlags dwFlags);
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Diagnostics;
using Microsoft.Win32.SafeHandles;
/// </summary>
internal sealed class SafeHashHandle : SafeHandleZeroOrMinusOneIsInvalid
{
- private SafeProvHandle _parent;
+ private SafeProvHandle? _parent;
private SafeHashHandle() : base(true)
{
bool successfullyFreed = Interop.Advapi32.CryptDestroyHash(handle);
Debug.Assert(successfullyFreed);
- SafeProvHandle parent = _parent;
+ SafeProvHandle? parent = _parent;
_parent = null;
parent?.DangerousRelease();
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Diagnostics;
using Microsoft.Win32.SafeHandles;
{
private int _keySpec;
private bool _fPublicOnly;
- private SafeProvHandle _parent;
+ private SafeProvHandle? _parent;
private SafeKeyHandle() : base(true)
{
bool successfullyFreed = Interop.Advapi32.CryptDestroyKey(handle);
Debug.Assert(successfullyFreed);
- SafeProvHandle parent = _parent;
+ SafeProvHandle? parent = _parent;
_parent = null;
parent?.DangerousRelease();
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Diagnostics;
using Microsoft.Win32.SafeHandles;
/// </summary>
internal sealed class SafeProvHandle : SafeHandleZeroOrMinusOneIsInvalid
{
- private string _containerName;
- private string _providerName;
+ private string? _containerName;
+ private string? _providerName;
private int _type;
private uint _flags;
private bool _fPersistKeyInCsp;
_fPersistKeyInCsp = true;
}
- internal string ContainerName
+ internal string? ContainerName
{
get
{
}
}
- internal string ProviderName
+ internal string? ProviderName
{
get
{
public abstract partial class RSA : System.Security.Cryptography.AsymmetricAlgorithm
{
protected RSA() { }
- public override string KeyExchangeAlgorithm { get { throw null; } }
+ public override string? KeyExchangeAlgorithm { get { throw null; } }
public override string SignatureAlgorithm { get { throw null; } }
public static new System.Security.Cryptography.RSA Create() { throw null; }
public static System.Security.Cryptography.RSA Create(int keySizeInBits) { throw null; }
CryptographicOperations.ZeroMemory(rsaParameters.InverseQ);
}
- public override string KeyExchangeAlgorithm => "RSA";
+ public override string? KeyExchangeAlgorithm => "RSA";
public override string SignatureAlgorithm => "RSA";
private static Exception HashAlgorithmNameNullOrEmpty() =>
public override System.Security.Cryptography.CipherMode Mode { get { throw null; } set { } }
public override System.Security.Cryptography.PaddingMode Padding { get { throw null; } set { } }
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor() { throw null; }
- public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) { throw null; }
+ public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { throw null; }
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor() { throw null; }
- public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) { throw null; }
+ public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { throw null; }
protected override void Dispose(bool disposing) { }
public override void GenerateIV() { }
public override void GenerateKey() { }
public bool Accessible { get { throw null; } }
public bool Exportable { get { throw null; } }
public bool HardwareDevice { get { throw null; } }
- public string KeyContainerName { get { throw null; } }
+ public string? KeyContainerName { get { throw null; } }
public System.Security.Cryptography.KeyNumber KeyNumber { get { throw null; } }
public bool MachineKeyStore { get { throw null; } }
public bool Protected { get { throw null; } }
- public string ProviderName { get { throw null; } }
+ public string? ProviderName { get { throw null; } }
public int ProviderType { get { throw null; } }
public bool RandomlyGenerated { get { throw null; } }
public bool Removable { get { throw null; } }
}
public sealed partial class CspParameters
{
- public string KeyContainerName;
+ public string? KeyContainerName;
public int KeyNumber;
- public string ProviderName;
+ public string? ProviderName;
public int ProviderType;
public CspParameters() { }
public CspParameters(int dwTypeIn) { }
- public CspParameters(int dwTypeIn, string strProviderNameIn) { }
- public CspParameters(int dwTypeIn, string strProviderNameIn, string strContainerNameIn) { }
+ public CspParameters(int dwTypeIn, string? strProviderNameIn) { }
+ public CspParameters(int dwTypeIn, string? strProviderNameIn, string? strContainerNameIn) { }
public System.Security.Cryptography.CspProviderFlags Flags { get { throw null; } set { } }
[System.CLSCompliantAttribute(false)]
- public System.Security.SecureString KeyPassword { get { throw null; } set { } }
+ public System.Security.SecureString? KeyPassword { get { throw null; } set { } }
public System.IntPtr ParentWindowHandle { get { throw null; } set { } }
}
[System.FlagsAttribute]
{
public DESCryptoServiceProvider() { }
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; }
public override void GenerateIV() { }
public override void GenerateKey() { }
}
{
public DSACryptoServiceProvider() { }
public DSACryptoServiceProvider(int dwKeySize) { }
- public DSACryptoServiceProvider(int dwKeySize, System.Security.Cryptography.CspParameters parameters) { }
- public DSACryptoServiceProvider(System.Security.Cryptography.CspParameters parameters) { }
+ public DSACryptoServiceProvider(int dwKeySize, System.Security.Cryptography.CspParameters? parameters) { }
+ public DSACryptoServiceProvider(System.Security.Cryptography.CspParameters? parameters) { }
public System.Security.Cryptography.CspKeyContainerInfo CspKeyContainerInfo { get { throw null; } }
- public override string KeyExchangeAlgorithm { get { throw null; } }
+ public override string? KeyExchangeAlgorithm { get { throw null; } }
public override int KeySize { get { throw null; } }
public override System.Security.Cryptography.KeySizes[] LegalKeySizes { get { throw null; } }
public bool PersistKeyInCsp { get { throw null; } set { } }
public byte[] SignData(byte[] buffer) { throw null; }
public byte[] SignData(byte[] buffer, int offset, int count) { throw null; }
public byte[] SignData(System.IO.Stream inputStream) { throw null; }
- public byte[] SignHash(byte[] rgbHash, string str) { throw null; }
+ public byte[] SignHash(byte[] rgbHash, string? str) { throw null; }
public bool VerifyData(byte[] rgbData, byte[] rgbSignature) { throw null; }
- public bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature) { throw null; }
+ public bool VerifyHash(byte[] rgbHash, string? str, byte[] rgbSignature) { throw null; }
public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature) { throw null; }
}
public partial interface ICspAsymmetricAlgorithm
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public partial class PasswordDeriveBytes : System.Security.Cryptography.DeriveBytes
{
- public PasswordDeriveBytes(byte[] password, byte[] salt) { }
- public PasswordDeriveBytes(byte[] password, byte[] salt, System.Security.Cryptography.CspParameters cspParams) { }
- public PasswordDeriveBytes(byte[] password, byte[] salt, string hashName, int iterations) { }
- public PasswordDeriveBytes(byte[] password, byte[] salt, string hashName, int iterations, System.Security.Cryptography.CspParameters cspParams) { }
- public PasswordDeriveBytes(string strPassword, byte[] rgbSalt) { }
- public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, System.Security.Cryptography.CspParameters cspParams) { }
- public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, string strHashName, int iterations) { }
- public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, string strHashName, int iterations, System.Security.Cryptography.CspParameters cspParams) { }
+ public PasswordDeriveBytes(byte[] password, byte[]? salt) { }
+ public PasswordDeriveBytes(byte[] password, byte[]? salt, System.Security.Cryptography.CspParameters? cspParams) { }
+ public PasswordDeriveBytes(byte[] password, byte[]? salt, string hashName, int iterations) { }
+ public PasswordDeriveBytes(byte[] password, byte[]? salt, string hashName, int iterations, System.Security.Cryptography.CspParameters? cspParams) { }
+ public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt) { }
+ public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, System.Security.Cryptography.CspParameters? cspParams) { }
+ public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, string strHashName, int iterations) { }
+ public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, string strHashName, int iterations, System.Security.Cryptography.CspParameters? cspParams) { }
public string HashName { get { throw null; } set { } }
public int IterationCount { get { throw null; } set { } }
- public byte[] Salt { get { throw null; } set { } }
- public byte[] CryptDeriveKey(string algname, string alghashname, int keySize, byte[] rgbIV) { throw null; }
+ public byte[]? Salt { get { throw null; } set { } }
+ public byte[] CryptDeriveKey(string? algname, string? alghashname, int keySize, byte[] rgbIV) { throw null; }
protected override void Dispose(bool disposing) { }
#pragma warning disable 0809
[System.ObsoleteAttribute("Rfc2898DeriveBytes replaces PasswordDeriveBytes for deriving key material from a password and is preferred in new applications.")]
public RC2CryptoServiceProvider() { }
public override int EffectiveKeySize { get { throw null; } set { } }
public bool UseSalt { get { throw null; } set { } }
- public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) { throw null; }
- public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(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(byte[] rgbKey, byte[]? rgbIV) { throw null; }
public override void GenerateIV() { }
public override void GenerateKey() { }
}
{
public RSACryptoServiceProvider() { }
public RSACryptoServiceProvider(int dwKeySize) { }
- public RSACryptoServiceProvider(int dwKeySize, System.Security.Cryptography.CspParameters parameters) { }
- public RSACryptoServiceProvider(System.Security.Cryptography.CspParameters parameters) { }
+ public RSACryptoServiceProvider(int dwKeySize, System.Security.Cryptography.CspParameters? parameters) { }
+ public RSACryptoServiceProvider(System.Security.Cryptography.CspParameters? parameters) { }
public System.Security.Cryptography.CspKeyContainerInfo CspKeyContainerInfo { get { throw null; } }
- public override string KeyExchangeAlgorithm { get { throw null; } }
+ public override string? KeyExchangeAlgorithm { get { throw null; } }
public override int KeySize { get { throw null; } }
public override System.Security.Cryptography.KeySizes[] LegalKeySizes { get { throw null; } }
public bool PersistKeyInCsp { get { throw null; } set { } }
public byte[] SignData(byte[] buffer, object halg) { throw null; }
public byte[] SignData(System.IO.Stream inputStream, object halg) { throw null; }
public override byte[] SignHash(byte[] hash, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.RSASignaturePadding padding) { throw null; }
- public byte[] SignHash(byte[] rgbHash, string str) { throw null; }
+ public byte[] SignHash(byte[] rgbHash, string? str) { throw null; }
public bool VerifyData(byte[] buffer, object halg, byte[] signature) { throw null; }
public override bool VerifyHash(byte[] hash, byte[] signature, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.RSASignaturePadding padding) { throw null; }
public bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature) { throw null; }
public override System.Security.Cryptography.CipherMode Mode { get { throw null; } set { } }
public override System.Security.Cryptography.PaddingMode Padding { get { throw null; } set { } }
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor() { throw null; }
- public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) { throw null; }
+ public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { throw null; }
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor() { throw null; }
- public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) { throw null; }
+ public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { throw null; }
protected override void Dispose(bool disposing) { }
public override void GenerateIV() { }
public override void GenerateKey() { }
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
+ <Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Security.Cryptography.Csp.cs" />
private SafeProvHandle _hProvider;
private SafeKeyHandle _hKey;
- public BasicSymmetricCipherCsp(int algId, CipherMode cipherMode, int blockSizeInBytes, byte[] key, int effectiveKeyLength, bool addNoSaltFlag, byte[] iv, bool encrypting)
+ public BasicSymmetricCipherCsp(int algId, CipherMode cipherMode, int blockSizeInBytes, byte[] key, int effectiveKeyLength, bool addNoSaltFlag, byte[]? iv, bool encrypting)
: base(cipherMode.GetCipherIv(iv), blockSizeInBytes)
{
_encrypting = encrypting;
SetKeyParameter(_hKey, CryptGetKeyParamQueryType.KP_MODE, (int)cipherMode);
- byte[] currentIv = cipherMode.GetCipherIv(iv);
+ byte[]? currentIv = cipherMode.GetCipherIv(iv);
if (currentIv != null)
{
SetKeyParameter(_hKey, CryptGetKeyParamQueryType.KP_IV, currentIv);
if (disposing)
{
SafeKeyHandle hKey = _hKey;
- _hKey = null;
+ _hKey = null!;
if (hKey != null)
{
hKey.Dispose();
}
SafeProvHandle hProvider = _hProvider;
- _hProvider = null;
+ _hProvider = null!;
if (hProvider != null)
{
hProvider.Dispose();
private static SafeProvHandle AcquireSafeProviderHandle()
{
- SafeProvHandle safeProvHandle = null;
+ SafeProvHandle safeProvHandle;
var cspParams = new CspParameters((int)ProviderType.PROV_RSA_FULL);
CapiHelper.AcquireCsp(cspParams, out safeProvHandle);
return safeProvHandle;
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())
{
return null;
}
- public static byte[] TrimLargeIV(byte[] currentIV, int blockSizeInBits)
+ public static byte[]? TrimLargeIV(byte[]? currentIV, int blockSizeInBits)
{
int blockSizeBytes = checked((blockSizeInBits + 7) / 8);
}
else
{
- string oidValue = CryptoConfig.MapNameToOID(nameOrOid);
+ string? oidValue = CryptoConfig.MapNameToOID(nameOrOid);
if (oidValue == null)
oidValue = nameOrOid; // we were probably passed an OID value directly
<NoWarn>$(NoWarn);CA5373</NoWarn>
<NoWarn Condition="'$(TargetsUnix)' == 'true'">$(NoWarn);CS0809</NoWarn>
<TargetFrameworks>$(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix</TargetFrameworks>
+ <Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Security\Cryptography\AesCryptoServiceProvider.cs" />
public override KeySizes[] LegalBlockSizes => _impl.LegalBlockSizes;
public override KeySizes[] LegalKeySizes => _impl.LegalKeySizes;
public override ICryptoTransform CreateEncryptor() => _impl.CreateEncryptor();
- public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) => _impl.CreateEncryptor(rgbKey, rgbIV);
+ public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) => _impl.CreateEncryptor(rgbKey, rgbIV);
public override ICryptoTransform CreateDecryptor() => _impl.CreateDecryptor();
- public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) => _impl.CreateDecryptor(rgbKey, rgbIV);
+ public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) => _impl.CreateDecryptor(rgbKey, rgbIV);
public override void GenerateIV() => _impl.GenerateIV();
public override void GenerateKey() => _impl.GenerateKey();
bool isPrivate = (dsaParameters.X != null && dsaParameters.X.Length > 0);
// The private key should be the same length as Q
- if (isPrivate && dsaParameters.X.Length != DSS_Q_LEN)
+ if (isPrivate && dsaParameters.X!.Length != DSS_Q_LEN)
throw GetBadDataException();
uint bitLenP = (uint)dsaParameters.P.Length * 8;
if (isPrivate)
{
- bw.Write((uint)dsaParameters.X.Length * 8);
+ bw.Write((uint)dsaParameters.X!.Length * 8);
}
WriteDSSSeed(dsaParameters, bw);
if (bitLenJ != 0)
{
- bw.WriteReversed(dsaParameters.J);
+ bw.WriteReversed(dsaParameters.J!);
}
- bw.WriteReversed(dsaParameters.Y);
+ bw.WriteReversed(dsaParameters.Y!);
if (isPrivate)
{
- bw.WriteReversed(dsaParameters.X);
+ bw.WriteReversed(dsaParameters.X!);
}
}
else
if (isPrivate)
{
- bw.WriteReversed(dsaParameters.X);
+ bw.WriteReversed(dsaParameters.X!);
}
else
{
+ Debug.Assert(dsaParameters.Y != null);
bw.WriteReversed(dsaParameters.Y);
}
/// <summary>
/// Helper for DSACryptoServiceProvider.ExportParameters()
/// </summary>
- internal static DSAParameters ToDSAParameters(this byte[] cspBlob, bool includePrivateParameters, byte[] cspPublicBlob)
+ internal static DSAParameters ToDSAParameters(this byte[] cspBlob, bool includePrivateParameters, byte[]? cspPublicBlob)
{
try
{
/// <param name="dwProvType">provider type</param>
/// <param name="wszProvider">provider name</param>
/// <returns>Returns upgrade CSP name</returns>
- public static string UpgradeDSS(int dwProvType, string wszProvider)
+ public static string? UpgradeDSS(int dwProvType, string wszProvider)
{
- string wszUpgrade = null;
+ string? wszUpgrade = null;
if (string.Equals(wszProvider, MS_DEF_DSS_DH_PROV, StringComparison.Ordinal))
{
SafeProvHandle safeProvHandle;
if (isPrivate)
{
- bw.WriteReversed(rsaParameters.P);
- bw.WriteReversed(rsaParameters.Q);
- bw.WriteReversed(rsaParameters.DP);
- bw.WriteReversed(rsaParameters.DQ);
- bw.WriteReversed(rsaParameters.InverseQ);
- bw.WriteReversed(rsaParameters.D);
+ bw.WriteReversed(rsaParameters.P!);
+ bw.WriteReversed(rsaParameters.Q!);
+ bw.WriteReversed(rsaParameters.DP!);
+ bw.WriteReversed(rsaParameters.DQ!);
+ bw.WriteReversed(rsaParameters.InverseQ!);
+ bw.WriteReversed(rsaParameters.D!);
}
bw.Flush();
using System;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
/// <param name="dwProvType">Provider type</param>
/// <param name="wszProvider">Provider name</param>
/// <returns>Returns upgrade CSP name</returns>
- public static string UpgradeRSA(int dwProvType, string wszProvider)
+ public static string? UpgradeRSA(int dwProvType, string wszProvider)
{
bool requestedEnhanced = string.Equals(wszProvider, MS_ENHANCED_PROV, StringComparison.Ordinal);
bool requestedBase = string.Equals(wszProvider, MS_DEF_PROV, StringComparison.Ordinal);
- string wszUpgrade = null;
+ string? wszUpgrade = null;
if (requestedBase || requestedEnhanced)
{
SafeProvHandle safeProvHandle;
// check to see if there are upgrades available for the requested CSP
string providerNameString = providerName.ToString();
- string wszUpgrade = null;
+ string? wszUpgrade = null;
if (dwType == (int)ProviderType.PROV_RSA_FULL)
{
wszUpgrade = UpgradeRSA(dwType, providerNameString);
/// Acquire a handle to a crypto service provider and optionally a key container
/// This function implements the WszCryptAcquireContext_SO_TOLERANT
/// </summary>
- private static int AcquireCryptContext(out SafeProvHandle safeProvHandle, string keyContainer,
+ private static int AcquireCryptContext(out SafeProvHandle safeProvHandle, string? keyContainer,
string providerName, int providerType, uint flags)
{
const uint VerifyContextFlag = (uint)Interop.Advapi32.CryptAcquireContextFlags.CRYPT_VERIFYCONTEXT;
/// </summary>
public static int OpenCSP(CspParameters cspParameters, uint flags, out SafeProvHandle safeProvHandle)
{
- string providerName = null;
- string containerName = null;
+ string providerName;
+ string? containerName = null;
if (null == cspParameters)
{
throw new ArgumentException(SR.Format(SR.CspParameter_invalid, nameof(cspParameters)));
/// <summary>
/// This method helps reduce the duplicate code in the GetProviderParameter method
/// </summary>
- internal static int GetProviderParameterWorker(SafeProvHandle safeProvHandle, byte[] impType, ref int cb, CryptProvParam flags)
+ internal static int GetProviderParameterWorker(SafeProvHandle safeProvHandle, byte[]? impType, ref int cb, CryptProvParam flags)
{
int impTypeReturn = 0;
if (!Interop.Advapi32.CryptGetProvParam(safeProvHandle, flags, impType, ref cb))
int impTypeReturn = 0;
int returnType = 0; //using 0 for bool and 1 for string return types
bool retVal = false;
- string retStr = null;
+ string? retStr = null;
try
{
{
throw GetErrorCode().ToCryptographicException();
}
- byte[] permissions = null;
+ byte[]? permissions = null;
int permissionsReturn = 0;
permissions = new byte[Constants.SIZE_OF_DWORD];
cb = sizeof(byte) * Constants.SIZE_OF_DWORD;
case Constants.CLR_UNIQUE_CONTAINER:
{
returnType = 1;
- byte[] pb = null;
+ byte[]? pb = null;
impTypeReturn = GetProviderParameterWorker(safeProvHandle, pb, ref cb, CryptProvParam.PP_UNIQUE_CONTAINER);
pb = new byte[cb];
impTypeReturn = GetProviderParameterWorker(safeProvHandle, pb, ref cb, CryptProvParam.PP_UNIQUE_CONTAINER);
}
Debug.Assert(returnType == 0 || returnType == 1);
- return returnType == 0 ? (object)retVal : retStr;
+ return returnType == 0 ? (object)retVal : retStr!;
}
/// <summary>
/// <returns>Returns the key property</returns>
internal static byte[] GetKeyParameter(SafeKeyHandle safeKeyHandle, int keyParam)
{
- byte[] pb = null;
+ byte[]? pb = null;
int cb = 0;
VerifyValidHandle(safeKeyHandle); //This will throw if handle is invalid
/// <returns></returns>
internal static CspParameters SaveCspParameters(
CspAlgorithmType keyType,
- CspParameters userParameters,
+ CspParameters? userParameters,
CspProviderFlags defaultFlags,
out bool randomKeyContainer)
{
// The returned value in ohRetEncryptedKey is byte-reversed from the version CAPI gives us. This is for
// compatibility with previous releases of the CLR and other RSA implementations.
//
- internal static void EncryptKey(SafeKeyHandle safeKeyHandle, byte[] pbKey, int cbKey, bool foep, ref byte[] pbEncryptedKey)
+ internal static void EncryptKey(SafeKeyHandle safeKeyHandle, byte[] pbKey, int cbKey, bool foep, [NotNull] ref byte[]? pbEncryptedKey)
{
VerifyValidHandle(safeKeyHandle);
Debug.Assert(pbKey != null, "pbKey is null");
{
VerifyValidHandle(safeKeyHandle);
- byte[] pbRawData = null;
+ byte[] pbRawData;
int cbRawData = 0;
int dwBlobType = includePrivateParameters ? PRIVATEKEYBLOB : PUBLICKEYBLOB;
/// <summary>
/// Helper for signing and verifications that accept a string to specify a hashing algorithm.
/// </summary>
- public static int NameOrOidToHashAlgId(string nameOrOid, OidGroup oidGroup)
+ public static int NameOrOidToHashAlgId(string? nameOrOid, OidGroup oidGroup)
{
// Default Algorithm Id is CALG_SHA1
if (nameOrOid == null)
return CapiHelper.CALG_SHA1;
- string oidValue = CryptoConfig.MapNameToOID(nameOrOid);
+ string? oidValue = CryptoConfig.MapNameToOID(nameOrOid);
if (oidValue == null)
oidValue = nameOrOid; // we were probably passed an OID value directly
if (hashAlg == null)
throw new ArgumentNullException(nameof(hashAlg));
- string hashAlgString = hashAlg as string;
+ string? hashAlgString = hashAlg as string;
if (hashAlgString != null)
{
int algId = NameOrOidToHashAlgId(hashAlgString, OidGroup.HashAlgorithm);
}
else
{
- Type hashAlgType = hashAlg as Type;
- if ((object)hashAlgType != null)
+ Type? hashAlgType = hashAlg as Type;
+ if ((object?)hashAlgType != null)
{
if (typeof(MD5).IsAssignableFrom(hashAlgType))
return CapiHelper.CALG_MD5;
int dwFlags,
byte[] IV_Out,
int cbIV_In,
- ref byte[] pbKey)
+ [NotNull] ref byte[]? pbKey)
{
VerifyValidHandle(hProv);
- SafeHashHandle hHash = null;
- SafeKeyHandle hKey = null;
+ SafeHashHandle? hHash = null;
+ SafeKeyHandle? hKey = null;
try
{
if (!CryptCreateHash(hProv, algidHash, SafeKeyHandle.InvalidHandle, Interop.Advapi32.CryptCreateHashFlags.None, out hHash))
}
// Get the key contents
- byte[] rgbKey = null;
+ byte[]? rgbKey = null;
int cbKey = 0;
UnloadKey(hProv, hKey, ref rgbKey, ref cbKey);
// Helper method used by DeriveKey (above) to return the key contents.
// WARNING: This function side-effects its first argument (hProv)
- private static void UnloadKey(SafeProvHandle hProv, SafeKeyHandle hKey, ref byte[] key_out, ref int cb_out)
+ private static void UnloadKey(SafeProvHandle hProv, SafeKeyHandle hKey, [NotNull] ref byte[]? key_out, ref int cb_out)
{
- SafeKeyHandle hPubKey = null;
+ SafeKeyHandle? hPubKey = null;
try
{
// Import the public key
/// </summary>
private static SafeHashHandle CreateHashHandle(this SafeProvHandle hProv, byte[] hash, int calgHash)
{
- SafeHashHandle hHash;
+ SafeHashHandle? hHash;
if (!CryptCreateHash(hProv, calgHash, SafeKeyHandle.InvalidHandle, Interop.Advapi32.CryptCreateHashFlags.None, out hHash))
{
int hr = Marshal.GetHRForLastWin32Error();
{
get
{
- object retVal = ReadKeyParameterSilent(Constants.CLR_ACCESSIBLE, throwOnNotFound: false);
+ object? retVal = ReadKeyParameterSilent(Constants.CLR_ACCESSIBLE, throwOnNotFound: false);
if (retVal == null)
{
return false;
}
- return (bool)ReadKeyParameterSilent(Constants.CLR_EXPORTABLE);
+ return (bool)ReadKeyParameterSilent(Constants.CLR_EXPORTABLE)!;
}
}
/// <summary>
/// Get Key container Name
/// </summary>
- public string KeyContainerName
+ public string? KeyContainerName
{
get
{
return true;
}
- return (bool)ReadKeyParameterSilent(Constants.CLR_PROTECTED);
+ return (bool)ReadKeyParameterSilent(Constants.CLR_PROTECTED)!;
}
}
/// <summary>
/// Gets the provider name
/// </summary>
- public string ProviderName
+ public string? ProviderName
{
get
{
{
get
{
- return (string)ReadKeyParameterSilent(Constants.CLR_UNIQUE_CONTAINER);
+ return (string)ReadKeyParameterSilent(Constants.CLR_UNIQUE_CONTAINER)!;
}
}
/// <summary>
/// Read a parameter from the current key using CRYPT_SILENT, to avoid any potential UI prompts.
/// </summary>
- private object ReadKeyParameterSilent(int keyParam, bool throwOnNotFound = true)
+ private object? ReadKeyParameterSilent(int keyParam, bool throwOnNotFound = true)
{
const uint SilentFlags = (uint)Interop.Advapi32.CryptAcquireContextFlags.CRYPT_SILENT;
public sealed class CspParameters
{
public int ProviderType;
- public string ProviderName;
- public string KeyContainerName;
+ public string? ProviderName;
+ public string? KeyContainerName;
public int KeyNumber;
private int _flags;
private IntPtr _parentWindowHandle;
}
[CLSCompliantAttribute(false)]
- public SecureString KeyPassword { get; set; }
+ public SecureString? KeyPassword { get; set; }
public CspParameters() : this(CapiHelper.DefaultRsaProviderType, null, null) { }
public CspParameters(int dwTypeIn) : this(dwTypeIn, null, null) { }
- public CspParameters(int dwTypeIn, string strProviderNameIn) : this(dwTypeIn, strProviderNameIn, null) { }
- public CspParameters(int dwTypeIn, string strProviderNameIn, string strContainerNameIn) :
+ public CspParameters(int dwTypeIn, string? strProviderNameIn) : this(dwTypeIn, strProviderNameIn, null) { }
+ public CspParameters(int dwTypeIn, string? strProviderNameIn, string? strContainerNameIn) :
this(dwTypeIn, strProviderNameIn, strContainerNameIn, CspProviderFlags.NoFlags)
{
}
- internal CspParameters(int providerType, string providerName, string keyContainerName, CspProviderFlags flags)
+ internal CspParameters(int providerType, string? providerName, string? keyContainerName, CspProviderFlags flags)
{
ProviderType = providerType;
ProviderName = providerName;
public override ICryptoTransform CreateDecryptor() => _impl.CreateDecryptor();
public override ICryptoTransform CreateEncryptor() => _impl.CreateEncryptor();
- public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) =>
+ public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) =>
_impl.CreateEncryptor(rgbKey, Helpers.TrimLargeIV(rgbIV, BlockSize));
- public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) =>
+ public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) =>
_impl.CreateDecryptor(rgbKey, Helpers.TrimLargeIV(rgbIV, BlockSize));
protected override void Dispose(bool disposing)
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "This is the implementation of DES")]
- public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
+ public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV)
{
return CreateTransform(rgbKey, rgbIV == null ? null : rgbIV.CloneByteArray(), encrypting: false);
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "This is the implementation of DES")]
- public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
+ public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV)
{
return CreateTransform(rgbKey, rgbIV == null ? null : rgbIV.CloneByteArray(), encrypting: true);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "This is the implementation of DES")]
- private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting)
+ private ICryptoTransform CreateTransform(byte[] rgbKey, byte[]? rgbIV, bool encrypting)
{
// note: rgbIV is guaranteed to be cloned before this method, so no need to clone it again
_impl.ImportEncryptedPkcs8PrivateKey(password, source, out bytesRead);
}
- public override string KeyExchangeAlgorithm => _impl.KeyExchangeAlgorithm;
+ public override string? KeyExchangeAlgorithm => _impl.KeyExchangeAlgorithm;
public override int KeySize
{
private int _keySize;
private readonly CspParameters _parameters;
private readonly bool _randomKeyContainer;
- private SafeKeyHandle _safeKeyHandle;
- private SafeProvHandle _safeProvHandle;
+ private SafeKeyHandle? _safeKeyHandle;
+ private SafeProvHandle? _safeProvHandle;
private readonly SHA1 _sha1;
private static volatile CspProviderFlags s_useMachineKeyStore = 0;
private bool _disposed;
/// for the cryptographic service provider (CSP).
/// </summary>
/// <param name="parameters">The parameters for the CSP.</param>
- public DSACryptoServiceProvider(CspParameters parameters)
+ public DSACryptoServiceProvider(CspParameters? parameters)
: this(0, parameters)
{
}
/// <param name="dwKeySize">The size of the key for the cryptographic algorithm in bits.</param>
/// <param name="parameters">The parameters for the CSP.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350", Justification = "SHA1 is required by the FIPS 186-2 DSA spec.")]
- public DSACryptoServiceProvider(int dwKeySize, CspParameters parameters)
+ public DSACryptoServiceProvider(int dwKeySize, CspParameters? parameters)
{
if (dwKeySize < 0)
throw new ArgumentOutOfRangeException(nameof(dwKeySize), SR.ArgumentOutOfRange_NeedNonNegNum);
{
lock (_parameters)
{
- SafeProvHandle current = _safeProvHandle;
+ SafeProvHandle? current = _safeProvHandle;
if (ReferenceEquals(value, current))
{
if (current != null)
{
- SafeKeyHandle keyHandle = _safeKeyHandle;
+ SafeKeyHandle? keyHandle = _safeKeyHandle;
_safeKeyHandle = null;
keyHandle?.Dispose();
current.Dispose();
{
lock (_parameters)
{
- SafeKeyHandle current = _safeKeyHandle;
+ SafeKeyHandle? current = _safeKeyHandle;
if (ReferenceEquals(value, current))
{
}
}
- public override string KeyExchangeAlgorithm => null;
+ public override string? KeyExchangeAlgorithm => null;
public override string SignatureAlgorithm => "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
protected override void Dispose(bool disposing)
public override DSAParameters ExportParameters(bool includePrivateParameters)
{
byte[] cspBlob = ExportCspBlob(includePrivateParameters);
- byte[] cspPublicBlob = null;
+ byte[]? cspPublicBlob = null;
if (includePrivateParameters)
{
/// <param name="rgbHash">The hash value of the data to be signed.</param>
/// <param name="str">The name of the hash algorithm used to create the hash value of the data.</param>
/// <returns>The DSA signature for the specified hash value.</returns>
- public byte[] SignHash(byte[] rgbHash, string str)
+ public byte[] SignHash(byte[] rgbHash, string? str)
{
if (rgbHash == null)
throw new ArgumentNullException(nameof(rgbHash));
/// <param name="str">The name of the hash algorithm used to create the hash value of the data.</param>
/// <param name="rgbSignature">The signature data to be verified.</param>
/// <returns>true if the signature verifies as valid; otherwise, false.</returns>
- public bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature)
+ public bool VerifyHash(byte[] rgbHash, string? str, byte[] rgbSignature)
{
if (rgbHash == null)
throw new ArgumentNullException(nameof(rgbHash));
{
public partial class PasswordDeriveBytes : DeriveBytes
{
- public byte[] CryptDeriveKey(string algname, string alghashname, int keySize, byte[] rgbIV)
+ public byte[] CryptDeriveKey(string? algname, string? alghashname, int keySize, byte[] rgbIV)
{
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CAPI_Required, nameof(CryptDeriveKey)));
}
{
public partial class PasswordDeriveBytes : DeriveBytes
{
- private SafeProvHandle _safeProvHandle = null;
+ private SafeProvHandle? _safeProvHandle = null;
- public byte[] CryptDeriveKey(string algname, string alghashname, int keySize, byte[] rgbIV)
+ public byte[] CryptDeriveKey(string? algname, string? alghashname, int keySize, byte[] rgbIV)
{
if (keySize < 0)
throw new CryptographicException(SR.Cryptography_InvalidKeySize);
if (rgbIV == null)
throw new CryptographicException(SR.Cryptography_PasswordDerivedBytes_InvalidIV);
- byte[] key = null;
+ byte[]? key = null;
CapiHelper.DeriveKey(ProvHandle, algid, algidhash, _password, _password.Length, keySize << 16, rgbIV, rgbIV.Length, ref key);
return key;
}
}
}
- private static SafeProvHandle AcquireSafeProviderHandle(CspParameters cspParams)
+ private static SafeProvHandle AcquireSafeProviderHandle(CspParameters? cspParams)
{
if (cspParams == null)
{
cspParams = new CspParameters(CapiHelper.DefaultRsaProviderType);
}
- SafeProvHandle safeProvHandle = null;
- CapiHelper.AcquireCsp(cspParams, out safeProvHandle);
+ CapiHelper.AcquireCsp(cspParams, out SafeProvHandle safeProvHandle);
return safeProvHandle;
}
}
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
private int _extraCount;
private int _prefix;
private int _iterations;
- private byte[] _baseValue;
- private byte[] _extra;
- private byte[] _salt;
+ private byte[]? _baseValue;
+ private byte[]? _extra;
+ private byte[]? _salt;
private readonly byte[] _password;
- private string _hashName;
- private HashAlgorithm _hash;
- private readonly CspParameters _cspParams;
+ private string? _hashName;
+ private HashAlgorithm? _hash;
+ private readonly CspParameters? _cspParams;
- public PasswordDeriveBytes(string strPassword, byte[] rgbSalt) : this(strPassword, rgbSalt, new CspParameters()) { }
+ public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt) : this(strPassword, rgbSalt, new CspParameters()) { }
- public PasswordDeriveBytes(byte[] password, byte[] salt) : this(password, salt, new CspParameters()) { }
+ public PasswordDeriveBytes(byte[] password, byte[]? salt) : this(password, salt, new CspParameters()) { }
- public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, string strHashName, int iterations) :
+ public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, string strHashName, int iterations) :
this(strPassword, rgbSalt, strHashName, iterations, new CspParameters()) { }
- public PasswordDeriveBytes(byte[] password, byte[] salt, string hashName, int iterations) :
+ public PasswordDeriveBytes(byte[] password, byte[]? salt, string hashName, int iterations) :
this(password, salt, hashName, iterations, new CspParameters()) { }
- public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, CspParameters cspParams) :
+ public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, CspParameters? cspParams) :
this(strPassword, rgbSalt, "SHA1", 100, cspParams) { }
- public PasswordDeriveBytes(byte[] password, byte[] salt, CspParameters cspParams) :
+ public PasswordDeriveBytes(byte[] password, byte[]? salt, CspParameters? cspParams) :
this(password, salt, "SHA1", 100, cspParams) { }
- public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, string strHashName, int iterations, CspParameters cspParams) :
+ public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, string strHashName, int iterations, CspParameters? cspParams) :
this((new UTF8Encoding(false)).GetBytes(strPassword), rgbSalt, strHashName, iterations, cspParams) { }
- public PasswordDeriveBytes(byte[] password, byte[] salt, string hashName, int iterations, CspParameters cspParams)
+ public PasswordDeriveBytes(byte[] password, byte[]? salt, string hashName, int iterations, CspParameters? cspParams)
{
IterationCount = iterations;
Salt = salt;
public string HashName
{
- get { return _hashName; }
+ get { return _hashName!; }
set
{
if (_baseValue != null)
throw new CryptographicException(SR.Cryptography_PasswordDerivedBytes_ValuesFixed, nameof(HashName));
_hashName = value;
- _hash = (HashAlgorithm)CryptoConfig.CreateFromName(_hashName);
+ _hash = (HashAlgorithm?)CryptoConfig.CreateFromName(_hashName);
}
}
}
}
- public byte[] Salt
+ public byte[]? Salt
{
get
{
- return (byte[])_salt?.Clone();
+ return (byte[]?)_salt?.Clone();
}
set
{
if (_baseValue != null)
throw new CryptographicException(SR.Cryptography_PasswordDerivedBytes_ValuesFixed, nameof(Salt));
- _salt = (byte[])value?.Clone();
+ _salt = (byte[]?)value?.Clone();
}
}
private byte[] ComputeBaseValue()
{
+ Debug.Assert(_hash != null);
_hash.Initialize();
_hash.TransformBlock(_password, 0, _password.Length, _password, 0);
for (int i = 1; i < (_iterations - 1); i++)
{
- _hash.ComputeHash(_baseValue);
+ _hash.ComputeHash(_baseValue!);
_baseValue = _hash.Hash;
}
- return _baseValue;
+ return _baseValue!;
}
private byte[] ComputeBytes(int cb)
int ib = 0;
byte[] rgb;
- _hash.Initialize();
+ _hash!.Initialize();
cbHash = _hash.HashSize / 8;
rgb = new byte[((cb + cbHash - 1) / cbHash) * cbHash];
using (CryptoStream cs = new CryptoStream(Stream.Null, _hash, CryptoStreamMode.Write))
{
HashPrefix(cs);
- cs.Write(_baseValue, 0, _baseValue.Length);
+ cs.Write(_baseValue!, 0, _baseValue!.Length);
cs.Close();
}
- Buffer.BlockCopy(_hash.Hash, 0, rgb, ib, cbHash);
+ Buffer.BlockCopy(_hash.Hash!, 0, rgb, ib, cbHash);
ib += cbHash;
while (cb > ib)
cs.Close();
}
- Buffer.BlockCopy(_hash.Hash, 0, rgb, ib, cbHash);
+ Buffer.BlockCopy(_hash.Hash!, 0, rgb, ib, cbHash);
ib += cbHash;
}
public override ICryptoTransform CreateDecryptor() => _impl.CreateDecryptor();
public override ICryptoTransform CreateEncryptor() => _impl.CreateEncryptor();
- public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) =>
+ public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) =>
_impl.CreateEncryptor(rgbKey, Helpers.TrimLargeIV(rgbIV, BlockSize));
- public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) =>
+ public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) =>
_impl.CreateDecryptor(rgbKey, Helpers.TrimLargeIV(rgbIV, BlockSize));
protected override void Dispose(bool disposing)
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "This is the implementation of RC2")]
- public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
+ public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV)
{
return CreateTransform(rgbKey, rgbIV == null ? null : rgbIV.CloneByteArray(), encrypting: true);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "This is the implementation of RC2")]
- public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
+ public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV)
{
return CreateTransform(rgbKey, rgbIV == null ? null : rgbIV.CloneByteArray(), encrypting: false);
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "This is the implementation of RC2")]
- private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting)
+ private ICryptoTransform CreateTransform(byte[] rgbKey, byte[]? rgbIV, bool encrypting)
{
// note: rgbIV is guaranteed to be cloned before this method, so no need to clone it again
{
private readonly RandomNumberGenerator _impl;
- public RNGCryptoServiceProvider() : this((CspParameters)null) { }
- public RNGCryptoServiceProvider(string str) : this((CspParameters)null) { }
- public RNGCryptoServiceProvider(byte[] rgb) : this((CspParameters)null) { }
+ public RNGCryptoServiceProvider() : this((CspParameters?)null) { }
+ public RNGCryptoServiceProvider(string str) : this((CspParameters?)null) { }
+ public RNGCryptoServiceProvider(byte[] rgb) : this((CspParameters?)null) { }
- public RNGCryptoServiceProvider(CspParameters cspParams)
+ public RNGCryptoServiceProvider(CspParameters? cspParams)
{
if (cspParams != null)
throw new PlatformNotSupportedException();
_impl.ImportEncryptedPkcs8PrivateKey(password, source, out bytesRead);
}
- public override string KeyExchangeAlgorithm => _impl.KeyExchangeAlgorithm;
+ public override string? KeyExchangeAlgorithm => _impl.KeyExchangeAlgorithm;
public override int KeySize
{
private int _keySize;
private readonly CspParameters _parameters;
private readonly bool _randomKeyContainer;
- private SafeKeyHandle _safeKeyHandle;
- private SafeProvHandle _safeProvHandle;
+ private SafeKeyHandle? _safeKeyHandle;
+ private SafeProvHandle? _safeProvHandle;
private static volatile CspProviderFlags s_useMachineKeyStore = 0;
private bool _disposed;
{
}
- public RSACryptoServiceProvider(int dwKeySize, CspParameters parameters)
+ public RSACryptoServiceProvider(int dwKeySize, CspParameters? parameters)
: this(dwKeySize, parameters, false)
{
}
- public RSACryptoServiceProvider(CspParameters parameters)
+ public RSACryptoServiceProvider(CspParameters? parameters)
: this(0, parameters, true)
{
}
- private RSACryptoServiceProvider(int keySize, CspParameters parameters, bool useDefaultKeySize)
+ private RSACryptoServiceProvider(int keySize, CspParameters? parameters, bool useDefaultKeySize)
{
if (keySize < 0)
{
{
lock (_parameters)
{
- SafeProvHandle current = _safeProvHandle;
+ SafeProvHandle? current = _safeProvHandle;
if (ReferenceEquals(value, current))
{
if (current != null)
{
- SafeKeyHandle keyHandle = _safeKeyHandle;
+ SafeKeyHandle? keyHandle = _safeKeyHandle;
_safeKeyHandle = null;
keyHandle?.Dispose();
current.Dispose();
{
lock (_parameters)
{
- SafeKeyHandle current = _safeKeyHandle;
+ SafeKeyHandle? current = _safeKeyHandle;
if (ReferenceEquals(value, current))
{
}
}
- byte[] encryptedKey = null;
+ byte[]? encryptedKey = null;
CapiHelper.EncryptKey(SafeKeyHandle, rgb, rgb.Length, fOAEP, ref encryptedKey);
return encryptedKey;
}
/// <param name="rgbHash">The input data for which to compute the hash</param>
/// <param name="str">The hash algorithm to use to create the hash value. </param>
/// <returns>The RSA signature for the specified data.</returns>
- public byte[] SignHash(byte[] rgbHash, string str)
+ public byte[] SignHash(byte[] rgbHash, string? str)
{
if (rgbHash == null)
throw new ArgumentNullException(nameof(rgbHash));
return VerifyHash(hash, GetAlgorithmId(hashAlgorithm), signature);
}
- public override string KeyExchangeAlgorithm
+ public override string? KeyExchangeAlgorithm
{
get
{
public override void GenerateIV() => _impl.GenerateIV();
public override void GenerateKey() => _impl.GenerateKey();
- public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) =>
+ public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) =>
_impl.CreateEncryptor(rgbKey, Helpers.TrimLargeIV(rgbIV, BlockSize));
- public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) =>
+ public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) =>
_impl.CreateDecryptor(rgbKey, Helpers.TrimLargeIV(rgbIV, BlockSize));
protected override void Dispose(bool disposing)