Skip to content

Commit

Permalink
Annotate System.Security.Cryptography.Primitives for nullable (dotne…
Browse files Browse the repository at this point in the history
…t/corefx#42298)

* Annotate System.Security.Cryptography.Primitives for nullable


Commit migrated from dotnet/corefx@7a517ee
  • Loading branch information
buyaa-n authored Nov 6, 2019
1 parent b762795 commit 89dd68c
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 86 deletions.
8 changes: 4 additions & 4 deletions src/libraries/Common/src/Internal/Cryptography/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Security.Cryptography;
#nullable enable
using System.Diagnostics.CodeAnalysis;

namespace Internal.Cryptography
{
internal static partial class Helpers
{
public static byte[] CloneByteArray(this byte[] src)
[return: NotNullIfNotNull("src")]
public static byte[]? CloneByteArray(this byte[]? src)
{
if (src == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
<NoWarn>$(NoWarn);CS1574;CS3016;CA5379;CA5384</NoWarn>
<Configurations>net461-Windows_NT-Debug;net461-Windows_NT-Release;netcoreapp-Debug;netcoreapp-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;netcoreapp3.0-Debug;netcoreapp3.0-Release;netcoreapp3.0-Windows_NT-Debug;netcoreapp3.0-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release;netstandard2.0-Debug;netstandard2.0-Release;netstandard2.0-Windows_NT-Debug;netstandard2.0-Windows_NT-Release;netstandard2.1-Debug;netstandard2.1-Release;netstandard2.1-Windows_NT-Debug;netstandard2.1-Windows_NT-Release</Configurations>
<DefineConstants Condition="'$(IsPartialFacadeAssembly)' != 'true' AND '$(TargetsNetCoreApp)' != 'true'">$(DefineConstants);INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
</PropertyGroup>
<Import Project="$(CommonPath)\System\Security\Cryptography\Asn1\AsnXml.targets" Condition="'$(IsPartialFacadeAssembly)' != 'true'" />
<Import Project="$(CommonPath)\System\Security\Cryptography\Asn1Reader\System.Security.Cryptography.Asn1Reader.Shared.projitems" Condition="'$(IsPartialFacadeAssembly)' != 'true'" />
Expand Down Expand Up @@ -42,6 +43,8 @@
<Compile Include="System\Security\Cryptography\Pkcs\SubjectIdentifierType.cs" />
<Compile Include="System\Security\Cryptography\Xml\X509IssuerSerial.cs" />
</ItemGroup>
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' == 'true'">
</ItemGroup>
<!-- Internal types (platform independent) -->
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true'">
<Compile Include="Internal\Cryptography\DecryptorPal.cs" />
Expand All @@ -51,6 +54,7 @@
<Compile Include="Internal\Cryptography\PkcsHelpers.cs" />
<Compile Include="Internal\Cryptography\PkcsPal.cs" />
<Compile Include="Internal\Cryptography\RecipientInfoPal.cs" />
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs" Link="System\Diagnostics\CodeAnalysis\NullableAttributes.cs" Condition="'$(TargetsNetCoreApp)' != 'true' AND '$(TargetGroup)' != 'netstandard2.1'" />
<Compile Include="$(CommonPath)\Internal\Cryptography\Helpers.cs">
<Link>Internal\Cryptography\Helpers.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace System.Security.Cryptography
public abstract partial class AsymmetricAlgorithm : System.IDisposable
{
protected int KeySizeValue;
protected System.Security.Cryptography.KeySizes[] LegalKeySizesValue;
[System.Diagnostics.CodeAnalysis.MaybeNullAttribute] protected System.Security.Cryptography.KeySizes[] LegalKeySizesValue;
protected AsymmetricAlgorithm() { }
public virtual string KeyExchangeAlgorithm { get { throw null; } }
public virtual string? KeyExchangeAlgorithm { get { throw null; } }
public virtual int KeySize { get { throw null; } set { } }
public virtual System.Security.Cryptography.KeySizes[] LegalKeySizes { get { throw null; } }
public virtual string SignatureAlgorithm { get { throw null; } }
public virtual string? SignatureAlgorithm { get { throw null; } }
public void Clear() { }
public static System.Security.Cryptography.AsymmetricAlgorithm Create() { throw null; }
public static System.Security.Cryptography.AsymmetricAlgorithm Create(string algName) { throw null; }
public static System.Security.Cryptography.AsymmetricAlgorithm? Create(string algName) { throw null; }
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
public virtual byte[] ExportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<byte> passwordBytes, System.Security.Cryptography.PbeParameters pbeParameters) { throw null; }
Expand Down Expand Up @@ -55,9 +55,9 @@ public partial class CryptographicUnexpectedOperationException : System.Security
{
public CryptographicUnexpectedOperationException() { }
protected CryptographicUnexpectedOperationException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public CryptographicUnexpectedOperationException(string message) { }
public CryptographicUnexpectedOperationException(string message, System.Exception inner) { }
public CryptographicUnexpectedOperationException(string format, string insert) { }
public CryptographicUnexpectedOperationException(string? message) { }
public CryptographicUnexpectedOperationException(string? message, System.Exception? inner) { }
public CryptographicUnexpectedOperationException(string format, string? insert) { }
}
public partial class CryptoStream : System.IO.Stream, System.IDisposable
{
Expand All @@ -69,8 +69,8 @@ public CryptoStream(System.IO.Stream stream, System.Security.Cryptography.ICrypt
public bool HasFlushedFinalBlock { get { throw null; } }
public override long Length { get { throw null; } }
public override long Position { get { throw null; } set { } }
public override System.IAsyncResult BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback callback, object state) { throw null; }
public override System.IAsyncResult BeginWrite(byte[] buffer, int offset, int count, System.AsyncCallback callback, object state) { throw null; }
public override System.IAsyncResult BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback? callback, object? state) { throw null; }
public override System.IAsyncResult BeginWrite(byte[] buffer, int offset, int count, System.AsyncCallback? callback, object? state) { throw null; }
public void Clear() { }
protected override void Dispose(bool disposing) { }
public override System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
Expand All @@ -96,12 +96,12 @@ public enum CryptoStreamMode
public abstract partial class HashAlgorithm : System.IDisposable, System.Security.Cryptography.ICryptoTransform
{
protected int HashSizeValue;
protected internal byte[] HashValue;
protected internal byte[]? HashValue;
protected int State;
protected HashAlgorithm() { }
public virtual bool CanReuseTransform { get { throw null; } }
public virtual bool CanTransformMultipleBlocks { get { throw null; } }
public virtual byte[] Hash { get { throw null; } }
public virtual byte[]? Hash { get { throw null; } }
public virtual int HashSize { get { throw null; } }
public virtual int InputBlockSize { get { throw null; } }
public virtual int OutputBlockSize { get { throw null; } }
Expand All @@ -110,14 +110,14 @@ public void Clear() { }
public byte[] ComputeHash(byte[] buffer, int offset, int count) { throw null; }
public byte[] ComputeHash(System.IO.Stream inputStream) { throw null; }
public static System.Security.Cryptography.HashAlgorithm Create() { throw null; }
public static System.Security.Cryptography.HashAlgorithm Create(string hashName) { throw null; }
public static System.Security.Cryptography.HashAlgorithm? Create(string hashName) { throw null; }
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
protected virtual void HashCore(System.ReadOnlySpan<byte> source) { }
protected abstract byte[] HashFinal();
public abstract void Initialize();
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { throw null; }
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[]? outputBuffer, int outputOffset) { throw null; }
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { throw null; }
public bool TryComputeHash(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
protected virtual bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
Expand All @@ -126,14 +126,14 @@ protected virtual void HashCore(System.ReadOnlySpan<byte> source) { }
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public HashAlgorithmName(string name) { throw null; }
public HashAlgorithmName(string? name) { throw null; }
public static System.Security.Cryptography.HashAlgorithmName MD5 { get { throw null; } }
public string Name { get { throw null; } }
public string? Name { get { throw null; } }
public static System.Security.Cryptography.HashAlgorithmName SHA1 { get { throw null; } }
public static System.Security.Cryptography.HashAlgorithmName SHA256 { get { throw null; } }
public static System.Security.Cryptography.HashAlgorithmName SHA384 { get { throw null; } }
public static System.Security.Cryptography.HashAlgorithmName SHA512 { get { throw null; } }
public override bool Equals(object obj) { throw null; }
public override bool Equals(object? obj) { throw null; }
public bool Equals(System.Security.Cryptography.HashAlgorithmName other) { throw null; }
public static System.Security.Cryptography.HashAlgorithmName FromOid(string oidValue) { throw null; }
public override int GetHashCode() { throw null; }
Expand All @@ -147,9 +147,9 @@ public abstract partial class HMAC : System.Security.Cryptography.KeyedHashAlgor
protected HMAC() { }
protected int BlockSizeValue { get { throw null; } set { } }
public string HashName { get { throw null; } set { } }
public override byte[] Key { get { throw null; } set { } }
public override byte[]? Key { get { throw null; } set { } }
public static new System.Security.Cryptography.HMAC Create() { throw null; }
public static new System.Security.Cryptography.HMAC Create(string algorithmName) { throw null; }
public static new System.Security.Cryptography.HMAC? Create(string algorithmName) { throw null; }
protected override void Dispose(bool disposing) { }
protected override void HashCore(byte[] rgb, int ib, int cb) { }
protected override void HashCore(System.ReadOnlySpan<byte> source) { }
Expand All @@ -163,16 +163,16 @@ public partial interface ICryptoTransform : System.IDisposable
bool CanTransformMultipleBlocks { get; }
int InputBlockSize { get; }
int OutputBlockSize { get; }
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset);
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[]? outputBuffer, int outputOffset);
byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount);
}
public abstract partial class KeyedHashAlgorithm : System.Security.Cryptography.HashAlgorithm
{
protected byte[] KeyValue;
protected KeyedHashAlgorithm() { }
public virtual byte[] Key { get { throw null; } set { } }
public virtual byte[]? Key { get { throw null; } set { } }
public static new System.Security.Cryptography.KeyedHashAlgorithm Create() { throw null; }
public static new System.Security.Cryptography.KeyedHashAlgorithm Create(string algName) { throw null; }
public static new System.Security.Cryptography.KeyedHashAlgorithm? Create(string algName) { throw null; }
protected override void Dispose(bool disposing) { }
}
public sealed partial class KeySizes
Expand Down Expand Up @@ -209,11 +209,11 @@ public abstract partial class SymmetricAlgorithm : System.IDisposable
{
protected int BlockSizeValue;
protected int FeedbackSizeValue;
protected byte[] IVValue;
protected byte[]? IVValue;
protected int KeySizeValue;
protected byte[] KeyValue;
protected System.Security.Cryptography.KeySizes[] LegalBlockSizesValue;
protected System.Security.Cryptography.KeySizes[] LegalKeySizesValue;
protected byte[]? KeyValue;
[System.Diagnostics.CodeAnalysis.MaybeNullAttribute] protected System.Security.Cryptography.KeySizes[] LegalBlockSizesValue;
[System.Diagnostics.CodeAnalysis.MaybeNullAttribute] protected System.Security.Cryptography.KeySizes[] LegalKeySizesValue;
protected System.Security.Cryptography.CipherMode ModeValue;
protected System.Security.Cryptography.PaddingMode PaddingValue;
protected SymmetricAlgorithm() { }
Expand All @@ -228,7 +228,7 @@ protected SymmetricAlgorithm() { }
public virtual System.Security.Cryptography.PaddingMode Padding { get { throw null; } set { } }
public void Clear() { }
public static System.Security.Cryptography.SymmetricAlgorithm Create() { throw null; }
public static System.Security.Cryptography.SymmetricAlgorithm Create(string algName) { throw null; }
public static System.Security.Cryptography.SymmetricAlgorithm? Create(string algName) { throw null; }
public virtual System.Security.Cryptography.ICryptoTransform CreateDecryptor() { throw null; }
public abstract System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);
public virtual System.Security.Cryptography.ICryptoTransform CreateEncryptor() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>netcoreapp-Debug;netcoreapp-Release</Configurations>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Security.Cryptography.Primitives.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPartialFacadeAssembly>true</IsPartialFacadeAssembly>
<Configurations>netcoreapp-Debug;netcoreapp-Release</Configurations>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Security\Cryptography\AsymmetricAlgorithm.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Security.Cryptography
{
public abstract class AsymmetricAlgorithm : IDisposable
{
protected int KeySizeValue;
protected KeySizes[] LegalKeySizesValue;
[MaybeNull] protected KeySizes[] LegalKeySizesValue = null!;

protected AsymmetricAlgorithm() { }

public static AsymmetricAlgorithm Create() =>
throw new PlatformNotSupportedException(SR.Cryptography_DefaultAlgorithm_NotSupported);

public static AsymmetricAlgorithm Create(string algName) =>
(AsymmetricAlgorithm)CryptoConfigForwarder.CreateFromName(algName);
public static AsymmetricAlgorithm? Create(string algName) =>
(AsymmetricAlgorithm?)CryptoConfigForwarder.CreateFromName(algName);

public virtual int KeySize
{
Expand All @@ -38,19 +41,19 @@ public virtual KeySizes[] LegalKeySizes
get
{
// Desktop compat: No null check is performed
return (KeySizes[])LegalKeySizesValue.Clone();
return (KeySizes[])LegalKeySizesValue!.Clone();
}
}

public virtual string SignatureAlgorithm
public virtual string? SignatureAlgorithm
{
get
{
throw new NotImplementedException();
}
}

public virtual string KeyExchangeAlgorithm
public virtual string? KeyExchangeAlgorithm
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ namespace System.Security.Cryptography
{
internal static class CryptoConfigForwarder
{
private static readonly Func<string, object> s_createFromName = BindCreateFromName();
private static readonly Func<string, object?> s_createFromName = BindCreateFromName();

private static Func<string, object> BindCreateFromName()
private static Func<string, object?> BindCreateFromName()
{
const string CryptoConfigTypeName =
"System.Security.Cryptography.CryptoConfig, System.Security.Cryptography.Algorithms";

const string CreateFromNameMethodName = "CreateFromName";

Type t = Type.GetType(CryptoConfigTypeName, throwOnError: true);
MethodInfo createFromName = t.GetMethod(CreateFromNameMethodName, new[] { typeof(string) });
Type t = Type.GetType(CryptoConfigTypeName, throwOnError: true)!;
MethodInfo? createFromName = t.GetMethod(CreateFromNameMethodName, new[] { typeof(string) });

if (createFromName == null)
{
throw new MissingMethodException(t.FullName, CreateFromNameMethodName);
}

return (Func<string, object>)createFromName.CreateDelegate(typeof(Func<string, object>));
return (Func<string, object?>)createFromName.CreateDelegate(typeof(Func<string, object?>));
}

internal static object CreateFromName(string name) => s_createFromName(name);
internal static object? CreateFromName(string name) => s_createFromName(name);

internal static HashAlgorithm CreateDefaultHashAlgorithm() =>
throw new PlatformNotSupportedException(SR.Cryptography_DefaultAlgorithm_NotSupported);
Expand Down
Loading

0 comments on commit 89dd68c

Please sign in to comment.