Skip to content

Commit

Permalink
Nullable: shared/System/Security (dotnet/coreclr#23623)
Browse files Browse the repository at this point in the history
* Nullable: shared/System/Security

* fix windows

* address feedback

* IIIIdentity.Identity?

* apply feedback

Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
krwq authored and stephentoub committed Apr 6, 2019
1 parent fcf3e31 commit ed49d90
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
namespace System.Security
{
// AllowPartiallyTrustedCallersAttribute:
// Indicates that the Assembly is secure and can be used by untrusted
// and semitrusted clients
// For v.1, this is valid only on Assemblies, but could be expanded to
// For v.1, this is valid only on Assemblies, but could be expanded to
// include Module, Method, class
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)]
public sealed class AllowPartiallyTrustedCallersAttribute : Attribute
Expand All @@ -16,4 +17,3 @@ public AllowPartiallyTrustedCallersAttribute() { }
public PartialTrustVisibilityLevel PartialTrustVisibilityLevel { get; set; }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Globalization;
using System.Runtime.Serialization;

Expand All @@ -22,17 +23,17 @@ public CryptographicException(int hr)
HResult = hr;
}

public CryptographicException(string message)
public CryptographicException(string? message)
: base(message)
{
}

public CryptographicException(string message, Exception inner)
public CryptographicException(string? message, Exception? inner)
: base(message, inner)
{
}

public CryptographicException(string format, string insert)
public CryptographicException(string format, string? insert)
: base(string.Format(format, insert))
{
}
Expand Down
7 changes: 4 additions & 3 deletions src/Common/src/CoreLib/System/Security/IPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
namespace System.Security
{
#if PROJECTN
Expand All @@ -11,8 +12,8 @@ public partial interface IPermission : ISecurityEncodable
{
IPermission Copy();
void Demand();
IPermission Intersect(IPermission target);
bool IsSubsetOf(IPermission target);
IPermission Union(IPermission target);
IPermission? Intersect(IPermission? target);
bool IsSubsetOf(IPermission? target);
IPermission? Union(IPermission? target);
}
}
3 changes: 2 additions & 1 deletion src/Common/src/CoreLib/System/Security/ISecurityEncodable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
namespace System.Security
{
#if PROJECTN
Expand All @@ -10,6 +11,6 @@ namespace System.Security
public partial interface ISecurityEncodable
{
void FromXml(SecurityElement e);
SecurityElement ToXml();
SecurityElement? ToXml();
}
}
1 change: 1 addition & 0 deletions src/Common/src/CoreLib/System/Security/IStackWalk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
namespace System.Security
{
#if PROJECTN
Expand Down
31 changes: 16 additions & 15 deletions src/Common/src/CoreLib/System/Security/PermissionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Security.Permissions;
using System.Collections;
using System.Runtime.Serialization;
Expand All @@ -14,13 +15,13 @@ namespace System.Security
public partial class PermissionSet : ICollection, IEnumerable, IDeserializationCallback, ISecurityEncodable, IStackWalk
{
public PermissionSet(PermissionState state) { }
public PermissionSet(PermissionSet permSet) { }
public PermissionSet(PermissionSet? permSet) { }
public virtual int Count { get { return 0; } }
public virtual bool IsReadOnly { get { return false; } }
public virtual bool IsSynchronized { get { return false; } }
public virtual object SyncRoot { get { return this; } }
public IPermission AddPermission(IPermission perm) { return AddPermissionImpl(perm); }
protected virtual IPermission AddPermissionImpl(IPermission perm) { return default(IPermission); }
public IPermission? AddPermission(IPermission? perm) { return AddPermissionImpl(perm); }
protected virtual IPermission? AddPermissionImpl(IPermission? perm) { return default(IPermission); }
public void Assert() { }
public bool ContainsNonCodeAccessPermissions() { return false; }
[Obsolete]
Expand All @@ -30,26 +31,26 @@ public virtual void CopyTo(Array array, int index) { }
public void Demand() { }
[Obsolete]
public void Deny() { throw new PlatformNotSupportedException(SR.PlatformNotSupported_CAS); }
public override bool Equals(object o) => base.Equals(o);
public override bool Equals(object? o) => base.Equals(o);
public virtual void FromXml(SecurityElement et) { }
public IEnumerator GetEnumerator() { return GetEnumeratorImpl(); }
protected virtual IEnumerator GetEnumeratorImpl() { return Array.Empty<object>().GetEnumerator(); }
public override int GetHashCode() => base.GetHashCode();
public IPermission GetPermission(Type permClass) { return GetPermissionImpl(permClass); }
protected virtual IPermission GetPermissionImpl(Type permClass) { return default(IPermission); }
public PermissionSet Intersect(PermissionSet other) { return default(PermissionSet); }
public IPermission? GetPermission(Type? permClass) { return GetPermissionImpl(permClass); }
protected virtual IPermission? GetPermissionImpl(Type? permClass) { return default(IPermission); }
public PermissionSet? Intersect(PermissionSet? other) { return default(PermissionSet); }
public bool IsEmpty() { return false; }
public bool IsSubsetOf(PermissionSet target) { return false; }
public bool IsSubsetOf(PermissionSet? target) { return false; }
public bool IsUnrestricted() { return false; }
public void PermitOnly() { throw new PlatformNotSupportedException(SR.PlatformNotSupported_CAS); }
public IPermission RemovePermission(Type permClass) { return RemovePermissionImpl(permClass); }
protected virtual IPermission RemovePermissionImpl(Type permClass) { return default(IPermission); }
public IPermission? RemovePermission(Type? permClass) { return RemovePermissionImpl(permClass); }
protected virtual IPermission? RemovePermissionImpl(Type? permClass) { return default(IPermission); }
public static void RevertAssert() { }
public IPermission SetPermission(IPermission perm) { return SetPermissionImpl(perm); }
protected virtual IPermission SetPermissionImpl(IPermission perm) { return default(IPermission); }
public IPermission? SetPermission(IPermission? perm) { return SetPermissionImpl(perm); }
protected virtual IPermission? SetPermissionImpl(IPermission? perm) { return default(IPermission); }
void IDeserializationCallback.OnDeserialization(object sender) { }
public override string ToString() => base.ToString();
public virtual SecurityElement ToXml() { return default(SecurityElement); }
public PermissionSet Union(PermissionSet other) { return default(PermissionSet); }
public override string ToString() => base.ToString()!;
public virtual SecurityElement? ToXml() { return default(SecurityElement); }
public PermissionSet? Union(PermissionSet? other) { return default(PermissionSet); }
}
}
5 changes: 3 additions & 2 deletions src/Common/src/CoreLib/System/Security/Principal/IIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
//
// All identities will implement this interface
//
Expand All @@ -16,10 +17,10 @@ namespace System.Security.Principal
public interface IIdentity
{
// Access to the name string
string Name { get; }
string? Name { get; }

// Access to Authentication 'type' info
string AuthenticationType { get; }
string? AuthenticationType { get; }

// Determine if this represents the unauthenticated identity
bool IsAuthenticated { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
//
// All roles will implement this interface
//
Expand All @@ -16,7 +17,7 @@ namespace System.Security.Principal
public interface IPrincipal
{
// Retrieve the identity object
IIdentity Identity { get; }
IIdentity? Identity { get; }

// Perform a check for a specific role
bool IsInRole(string role);
Expand Down
1 change: 1 addition & 0 deletions src/Common/src/CoreLib/System/Security/SafeBSTRHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Runtime;
using System.Diagnostics;
using System.Runtime.InteropServices;
Expand Down
20 changes: 15 additions & 5 deletions src/Common/src/CoreLib/System/Security/SecureString.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Diagnostics;
using System.Runtime;
using System.Runtime.InteropServices;
Expand All @@ -10,17 +11,17 @@
namespace System.Security
{
// SecureString attempts to provide a defense-in-depth solution.
//
//
// On Windows, this is done with several mechanisms:
// 1. keeping the data in unmanaged memory so that copies of it aren't implicitly made by the GC moving it around
// 2. zero'ing out that unmanaged memory so that the string is reliably removed from memory when done with it
// 3. encrypting the data while it's not being used (it's unencrypted to manipulate and use it)
//
//
// On Unix, we do 1 and 2, but we don't do 3 as there's no CryptProtectData equivalent.

public sealed partial class SecureString
{
private UnmanagedBuffer _buffer;
private UnmanagedBuffer? _buffer;

internal SecureString(SecureString str)
{
Expand All @@ -31,6 +32,7 @@ internal SecureString(SecureString str)
// Copy the string into the newly allocated space
if (_decryptedLength > 0)
{
Debug.Assert(str._buffer != null && _buffer != null); ;
UnmanagedBuffer.Copy(str._buffer, _buffer, (ulong)(str._decryptedLength * sizeof(char)));
}
}
Expand All @@ -45,6 +47,7 @@ private unsafe void InitializeSecureString(char* value, int length)
return;
}

Debug.Assert(_buffer != null);
// Copy the string into the newly allocated space
byte* ptr = null;
try
Expand Down Expand Up @@ -73,13 +76,15 @@ private void DisposeCore()
private void ClearCore()
{
_decryptedLength = 0;
Debug.Assert(_buffer != null);
_buffer.Clear();
}

private unsafe void AppendCharCore(char c)
{
// Make sure we have enough space for the new character, then write it at the end.
EnsureCapacity(_decryptedLength + 1);
Debug.Assert(_buffer != null);
_buffer.Write((ulong)(_decryptedLength * sizeof(char)), c);
_decryptedLength++;
}
Expand All @@ -89,6 +94,7 @@ private unsafe void InsertAtCore(int index, char c)
// Make sure we have enough space for the new character, then shift all of the characters above it and insert it.
EnsureCapacity(_decryptedLength + 1);
byte* ptr = null;
Debug.Assert(_buffer != null);
try
{
_buffer.AcquirePointer(ref ptr);
Expand All @@ -111,6 +117,7 @@ private unsafe void RemoveAtCore(int index)
{
// Shift down all values above the specified index, then null out the empty space at the end.
byte* ptr = null;
Debug.Assert(_buffer != null);
try
{
_buffer.AcquirePointer(ref ptr);
Expand All @@ -132,6 +139,7 @@ private unsafe void RemoveAtCore(int index)
private void SetAtCore(int index, char c)
{
// Overwrite the character at the specified index
Debug.Assert(_buffer != null);
_buffer.Write((ulong)(index * sizeof(char)), c);
}

Expand All @@ -141,7 +149,8 @@ internal unsafe IntPtr MarshalToBSTRCore()
IntPtr ptr = IntPtr.Zero;
IntPtr result = IntPtr.Zero;
byte* bufferPtr = null;

Debug.Assert(_buffer != null);

try
{
_buffer.AcquirePointer(ref bufferPtr);
Expand Down Expand Up @@ -176,6 +185,7 @@ internal unsafe IntPtr MarshalToStringCore(bool globalAlloc, bool unicode)

byte* bufferPtr = null;
IntPtr stringPtr = IntPtr.Zero, result = IntPtr.Zero;
Debug.Assert(_buffer != null);
try
{
_buffer.AcquirePointer(ref bufferPtr);
Expand Down Expand Up @@ -203,7 +213,7 @@ internal unsafe IntPtr MarshalToStringCore(bool globalAlloc, bool unicode)
}
finally
{
// If there was a failure, such that result isn't initialized,
// If there was a failure, such that result isn't initialized,
// release the string if we had one.
if (stringPtr != IntPtr.Zero && result == IntPtr.Zero)
{
Expand Down
Loading

0 comments on commit ed49d90

Please sign in to comment.