using System;
using System.Diagnostics;
-using System.Security;
namespace Microsoft.Win32
{
/// <summary>Current Config Root Key. This is where current configuration information is stored.</summary>
public static readonly RegistryKey CurrentConfig = RegistryKey.OpenBaseKey(RegistryHive.CurrentConfig, RegistryView.Default);
- [SecuritySafeCritical]
public static object GetValue(string keyName, string valueName, object defaultValue)
{
string subKeyName;
using (RegistryKey key = basekey.OpenSubKey(subKeyName))
{
- return key != null ?
- key.GetValue(valueName, defaultValue) :
- null;
+ return key?.GetValue(valueName, defaultValue);
}
}
SetValue(keyName, valueName, value, RegistryValueKind.Unknown);
}
- [SecuritySafeCritical]
public static void SetValue(string keyName, string valueName, object value, RegistryValueKind valueKind)
{
string subKeyName;
/// If the keyName is not valid, we will throw ArgumentException.
/// The return value shouldn't be null.
/// </summary>
- [System.Security.SecurityCritical]
private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName)
{
if (keyName == null)
using Microsoft.Win32.SafeHandles;
using System;
-using System.Security;
using System.Security.AccessControl;
namespace Microsoft.Win32
{
public sealed partial class RegistryKey : IDisposable
{
- [SecuritySafeCritical]
private void ClosePerfDataKey()
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private void FlushCore()
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private RegistryKey CreateSubKeyInternalCore(string subkey, bool writable, RegistryOptions registryOptions)
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private void DeleteSubKeyCore(string subkey, bool throwOnMissingSubKey)
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private void DeleteSubKeyTreeCore(string subkey)
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private void DeleteValueCore(string name, bool throwOnMissingValue)
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private static RegistryKey OpenRemoteBaseKeyCore(RegistryHive hKey, string machineName, RegistryView view)
{
throw new PlatformNotSupportedException(SR.Security_RegistryPermission); // remote stores not supported on Unix
}
- [SecurityCritical]
private RegistryKey InternalOpenSubKeyCore(string name, RegistryRights rights, bool throwOnPermissionFailure)
{
// TODO: Implement this
private SafeRegistryHandle SystemKeyHandle
{
- [SecurityCritical]
get
{
// TODO: Implement this
}
}
- [SecurityCritical]
private int InternalSubKeyCountCore()
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecurityCritical]
private string[] InternalGetSubKeyNamesCore(int subkeys)
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecurityCritical]
private int InternalValueCountCore()
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private string[] GetValueNamesCore(int values)
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecurityCritical]
private Object InternalGetValueCore(string name, Object defaultValue, bool doNotExpand)
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private RegistryValueKind GetValueKindCore(string name)
{
// TODO: Implement this
throw new PlatformNotSupportedException();
}
- [SecuritySafeCritical]
private void SetValueCore(string name, Object value, RegistryValueKind valueKind)
{
- try
- {
- // TODO: Implement this
- throw new PlatformNotSupportedException();
- }
- catch (Exception exc) when (exc is OverflowException || exc is InvalidOperationException || exc is FormatException || exc is InvalidCastException)
- {
- ThrowHelper.ThrowArgumentException(SR.Arg_RegSetMismatchedKind);
- }
+ // TODO: Implement this
+ throw new PlatformNotSupportedException();
}
}
}
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
-using System.Security;
using System.Security.AccessControl;
/*
{
public sealed partial class RegistryKey : IDisposable
{
- [SecuritySafeCritical]
private void ClosePerfDataKey()
{
// System keys should never be closed. However, we want to call RegCloseKey
Interop.mincore.RegCloseKey(HKEY_PERFORMANCE_DATA);
}
- [SecuritySafeCritical]
private void FlushCore()
{
if (_hkey != null && IsDirty())
}
}
- [SecuritySafeCritical]
private unsafe RegistryKey CreateSubKeyInternalCore(string subkey, bool writable, RegistryOptions registryOptions)
{
Interop.mincore.SECURITY_ATTRIBUTES secAttrs = default(Interop.mincore.SECURITY_ATTRIBUTES);
return null;
}
- [SecuritySafeCritical]
private void DeleteSubKeyCore(string subkey, bool throwOnMissingSubKey)
{
int ret = Interop.mincore.RegDeleteKeyEx(_hkey, subkey, (int)_regView, 0);
}
}
- [SecuritySafeCritical]
private void DeleteSubKeyTreeCore(string subkey)
{
int ret = Interop.mincore.RegDeleteKeyEx(_hkey, subkey, (int)_regView, 0);
}
}
- [SecuritySafeCritical]
private void DeleteValueCore(string name, bool throwOnMissingValue)
{
int errorCode = Interop.mincore.RegDeleteValue(_hkey, name);
/// </summary>
/// <param name="hKeyHive">HKEY_* to open.</param>
/// <returns>The RegistryKey requested.</returns>
- [SecuritySafeCritical]
private static RegistryKey OpenBaseKeyCore(RegistryHive hKeyHive, RegistryView view)
{
IntPtr hKey = (IntPtr)((int)hKeyHive);
return key;
}
- [SecuritySafeCritical]
private static RegistryKey OpenRemoteBaseKeyCore(RegistryHive hKey, string machineName, RegistryView view)
{
int index = (int)hKey & 0x0FFFFFFF;
return key;
}
- [SecurityCritical]
private RegistryKey InternalOpenSubKeyCore(string name, RegistryRights rights, bool throwOnPermissionFailure)
{
SafeRegistryHandle result = null;
private SafeRegistryHandle SystemKeyHandle
{
- [SecurityCritical]
get
{
Debug.Assert(IsSystemKey());
}
}
- [SecurityCritical]
private int InternalSubKeyCountCore()
{
int subkeys = 0;
return subkeys;
}
- [SecurityCritical]
private unsafe string[] InternalGetSubKeyNamesCore(int subkeys)
{
string[] names = new string[subkeys];
return names;
}
- [SecurityCritical]
private int InternalValueCountCore()
{
int values = 0;
/// <summary>Retrieves an array of strings containing all the value names.</summary>
/// <returns>All value names.</returns>
- [SecuritySafeCritical]
private unsafe string[] GetValueNamesCore(int values)
{
string[] names = new string[values];
return names;
}
- [SecurityCritical]
private object InternalGetValueCore(string name, object defaultValue, bool doNotExpand)
{
object data = defaultValue;
return data;
}
- [SecuritySafeCritical]
private RegistryValueKind GetValueKindCore(string name)
{
int type = 0;
(RegistryValueKind)type;
}
- [SecuritySafeCritical]
private unsafe void SetValueCore(string name, object value, RegistryValueKind valueKind)
{
int ret = 0;
/// error, and depending on the error, insert a string into the message
/// gotten from the ResourceManager.
/// </summary>
- [SecuritySafeCritical]
private void Win32Error(int errorCode, string str)
{
switch (errorCode)
}
}
- [SecuritySafeCritical]
private static void Win32ErrorStatic(int errorCode, string str)
{
switch (errorCode)
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
-using System.Diagnostics.Contracts;
using System.IO;
-using System.Security;
using System.Security.AccessControl;
using System.Text;
private const int MaxKeyLength = 255;
private const int MaxValueLength = 16383;
- [SecurityCritical]
private volatile SafeRegistryHandle _hkey;
private volatile string _keyName;
private volatile bool _remoteKey;
/// <summary>
/// Creates a RegistryKey. This key is bound to hkey, if writable is <b>false</b> then no write operations will be allowed.
/// </summary>
- [SecurityCritical]
private RegistryKey(SafeRegistryHandle hkey, bool writable, RegistryView view) :
this(hkey, writable, false, false, false, view)
{
/// The remoteKey flag when set to true indicates that we are dealing with registry entries
/// on a remote machine and requires the program making these calls to have full trust.
/// </summary>
- [SecurityCritical]
private RegistryKey(SafeRegistryHandle hkey, bool writable, bool systemkey, bool remoteKey, bool isPerfData, RegistryView view)
{
ValidateKeyView(view);
}
}
- [SecuritySafeCritical]
public void Flush()
{
FlushCore();
}
- [SecuritySafeCritical]
public void Dispose()
{
if (_hkey != null)
return CreateSubKeyInternal(subkey, writable, options);
}
- [SecuritySafeCritical]
private RegistryKey CreateSubKeyInternal(string subkey, bool writable, RegistryOptions registryOptions)
{
ValidateKeyOptions(registryOptions);
DeleteSubKey(subkey, true);
}
- [SecuritySafeCritical]
public void DeleteSubKey(string subkey, bool throwOnMissingSubKey)
{
ValidateKeyName(subkey);
DeleteSubKeyTree(subkey, throwOnMissingSubKey: true);
}
- [SecuritySafeCritical]
public void DeleteSubKeyTree(string subkey, bool throwOnMissingSubKey)
{
ValidateKeyName(subkey);
/// An internal version which does no security checks or argument checking. Skipping the
/// security checks should give us a slight perf gain on large trees.
/// </summary>
- [SecurityCritical]
private void DeleteSubKeyTreeInternal(string subkey)
{
RegistryKey key = InternalOpenSubKey(subkey, true);
DeleteValue(name, true);
}
- [SecuritySafeCritical]
public void DeleteValue(string name, bool throwOnMissingValue)
{
EnsureWriteable();
DeleteValueCore(name, throwOnMissingValue);
}
- [SecuritySafeCritical]
public static RegistryKey OpenBaseKey(RegistryHive hKey, RegistryView view)
{
ValidateKeyView(view);
return OpenRemoteBaseKey(hKey, machineName, RegistryView.Default);
}
- [SecuritySafeCritical]
public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey, string machineName, RegistryView view)
{
if (machineName == null)
/// read-only access.
/// </summary>
/// <returns>the Subkey requested, or <b>null</b> if the operation failed.</returns>
- [SecuritySafeCritical]
public RegistryKey OpenSubKey(string name, bool writable)
{
return InternalOpenSubKey(name, writable);
/// This required no security checks. This is to get around the Deleting SubKeys which only require
/// write permission. They call OpenSubKey which required read. Now instead call this function w/o security checks
/// </summary>
- [SecurityCritical]
private RegistryKey InternalOpenSubKey(string name, bool writable)
{
ValidateKeyName(name);
/// <summary>Returns a subkey with read only permissions.</summary>
/// <param name="name">Name or path of subkey to open.</param>
/// <returns>The Subkey requested, or <b>null</b> if the operation failed.</returns>
- [SecurityCritical]
public RegistryKey OpenSubKey(string name)
{
return OpenSubKey(name, false);
/// <returns>A count of subkeys.</returns>
public int SubKeyCount
{
- [SecuritySafeCritical]
get { return InternalSubKeyCount(); }
}
public RegistryView View
{
- [SecuritySafeCritical]
get
{
EnsureNotDisposed();
public SafeRegistryHandle Handle
{
- [SecurityCritical]
get
{
EnsureNotDisposed();
}
}
- [SecurityCritical]
public static RegistryKey FromHandle(SafeRegistryHandle handle)
{
return FromHandle(handle, RegistryView.Default);
}
- [SecurityCritical]
public static RegistryKey FromHandle(SafeRegistryHandle handle, RegistryView view)
{
if (handle == null) throw new ArgumentNullException(nameof(handle));
return new RegistryKey(handle, writable: true, view: view);
}
- [SecurityCritical]
private int InternalSubKeyCount()
{
EnsureNotDisposed();
/// <summary>Retrieves an array of strings containing all the subkey names.</summary>
/// <returns>All subkey names.</returns>
- [SecurityCritical]
public string[] GetSubKeyNames()
{
return InternalGetSubKeyNames();
}
- [SecurityCritical]
private string[] InternalGetSubKeyNames()
{
- EnsureNotDisposed();
int subkeys = InternalSubKeyCount();
return subkeys > 0 ?
InternalGetSubKeyNamesCore(subkeys) :
/// <returns>A count of values.</returns>
public int ValueCount
{
- [SecuritySafeCritical]
- get { return InternalValueCount(); }
- }
-
- [SecurityCritical]
- private int InternalValueCount()
- {
- EnsureNotDisposed();
- return InternalValueCountCore();
+ get
+ {
+ EnsureNotDisposed();
+ return InternalValueCountCore();
+ }
}
/// <summary>Retrieves an array of strings containing all the value names.</summary>
/// <returns>All value names.</returns>
- [SecuritySafeCritical]
public string[] GetValueNames()
{
- EnsureNotDisposed();
-
- int values = InternalValueCount();
+ int values = ValueCount;
return values > 0 ?
GetValueNamesCore(values) :
Array.Empty<string>();
/// </remarks>
/// <param name="name">Name of value to retrieve.</param>
/// <returns>The data associated with the value.</returns>
- [SecuritySafeCritical]
public object GetValue(string name)
{
return InternalGetValue(name, null, false, true);
/// <param name="name">Name of value to retrieve.</param>
/// <param name="defaultValue">Value to return if <i>name</i> doesn't exist.</param>
/// <returns>The data associated with the value.</returns>
- [SecuritySafeCritical]
public object GetValue(string name, object defaultValue)
{
return InternalGetValue(name, defaultValue, false, true);
}
- [SecuritySafeCritical]
public object GetValue(string name, object defaultValue, RegistryValueOptions options)
{
if (options < RegistryValueOptions.None || options > RegistryValueOptions.DoNotExpandEnvironmentNames)
return InternalGetValue(name, defaultValue, doNotExpand, checkSecurity: true);
}
- [SecurityCritical]
private object InternalGetValue(string name, object defaultValue, bool doNotExpand, bool checkSecurity)
{
if (checkSecurity)
return InternalGetValueCore(name, defaultValue, doNotExpand);
}
- [SecuritySafeCritical]
public RegistryValueKind GetValueKind(string name)
{
EnsureNotDisposed();
public string Name
{
- [SecuritySafeCritical]
get
{
EnsureNotDisposed();
SetValue(name, value, RegistryValueKind.Unknown);
}
- [SecuritySafeCritical]
public void SetValue(string name, object value, RegistryValueKind valueKind)
{
if (value == null)
/// <summary>Retrieves a string representation of this key.</summary>
/// <returns>A string representing the key.</returns>
- [SecuritySafeCritical]
public override string ToString()
{
EnsureNotDisposed();
private static void FixupPath(StringBuilder path)
{
- Contract.Requires(path != null);
+ Debug.Assert(path != null);
+
int length = path.Length;
bool fixup = false;
char markerChar = (char)0xFFFF;
}
}
- [SecurityCritical]
private void EnsureNotDisposed()
{
if (_hkey == null)
}
}
- [SecurityCritical]
private void EnsureWriteable()
{
EnsureNotDisposed();
private static void ValidateKeyName(string name)
{
- Contract.Ensures(name != null);
if (name == null)
{
ThrowHelper.ThrowArgumentNullException(nameof(name));
// See the LICENSE file in the project root for more information.
using System.Runtime.InteropServices;
-using System.Security;
namespace Microsoft.Win32.SafeHandles
{
public sealed partial class SafeRegistryHandle : SafeHandle
{
- [SecurityCritical]
- protected override bool ReleaseHandle()
- {
- // TODO: implement this if necessary
- return true;
- }
+ // TODO: implement this if necessary
+ protected override bool ReleaseHandle() => true;
}
}
// See the LICENSE file in the project root for more information.
using System.Runtime.InteropServices;
-using System.Security;
namespace Microsoft.Win32.SafeHandles
{
public sealed partial class SafeRegistryHandle : SafeHandle
{
- [SecurityCritical]
- protected override bool ReleaseHandle()
- {
- return (Interop.mincore.RegCloseKey(handle) == Interop.mincore.Errors.ERROR_SUCCESS);
- }
+ protected override bool ReleaseHandle() =>
+ Interop.mincore.RegCloseKey(handle) == Interop.mincore.Errors.ERROR_SUCCESS;
}
}
using System;
using System.Runtime.InteropServices;
-using System.Security;
namespace Microsoft.Win32.SafeHandles
{
- [SecurityCritical]
public sealed partial class SafeRegistryHandle : SafeHandle
{
- [SecurityCritical]
internal SafeRegistryHandle() : base(IntPtr.Zero, true) { }
- [SecurityCritical]
public SafeRegistryHandle(IntPtr preexistingHandle, bool ownsHandle) : base(IntPtr.Zero, ownsHandle)
{
SetHandle(preexistingHandle);
}
- public override bool IsInvalid
- {
- [SecurityCritical]
- get { return handle == new IntPtr(0) || handle == new IntPtr(-1); }
- }
+ public override bool IsInvalid => handle == IntPtr.Zero || handle == new IntPtr(-1);
}
}
// See the LICENSE file in the project root for more information.
using System;
-using System.Diagnostics.Contracts;
+using System.Security;
namespace Microsoft.Win32
{
- [Pure]
internal static class ThrowHelper
{
internal static void ThrowArgumentException(string msg)
internal static void ThrowSecurityException(string msg)
{
- throw new System.Security.SecurityException(msg);
+ throw new SecurityException(msg);
}
internal static void ThrowUnauthorizedAccessException(string msg)
"dependencies": {
"Microsoft.NETCore.Platforms": "1.0.1",
"System.Collections": "4.0.10",
- "System.Diagnostics.Contracts": "4.0.0",
"System.Diagnostics.Debug": "4.0.10",
"System.Diagnostics.Tools": "4.0.0",
"System.Globalization": "4.0.10",