Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 48 additions & 19 deletions src/System.Management.Automation/security/SecureStringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,17 @@ internal static string Protect(SecureString input)
byte[] protectedData = null;

data = GetData(input);
#if UNIX
// DPAPI doesn't exist on UNIX so we simply use the string as a byte-array
protectedData = data;
#else
protectedData = ProtectedData.Protect(data, null,
DataProtectionScope.CurrentUser);
for (int i = 0; i < data.Length; i++)
{
data[i] = 0;
}
#endif

output = ByteArrayToString(protectedData);

Expand Down Expand Up @@ -189,9 +194,14 @@ internal static SecureString Unprotect(string input)

protectedData = ByteArrayFromString(input);

#if UNIX
// DPAPI isn't supported in UNIX, so we just translate the byte-array back to a string
data = protectedData;
#else
data = ProtectedData.Unprotect(protectedData, null,
DataProtectionScope.CurrentUser);

#endif
s = New(data);

return s;
Expand Down Expand Up @@ -400,7 +410,7 @@ internal EncryptionResult(string encrypted, string IV)
internal string IV { get; }
}

#if CORECLR
#if !UNIX

// The DPAPIs implemented in this section are temporary workaround.
// CoreCLR team will bring 'ProtectedData' type to Project K eventually.
Expand All @@ -421,7 +431,9 @@ internal static class ProtectedData
public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
if (userData == null)
{
throw new ArgumentNullException("userData");
}

GCHandle pbDataIn = new GCHandle();
GCHandle pOptionalEntropy = new GCHandle();
Expand All @@ -446,13 +458,14 @@ public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtec
dwFlags |= CAPI.CRYPTPROTECT_LOCAL_MACHINE;
unsafe
{
if (!CAPI.CryptProtectData(new IntPtr(&dataIn),
string.Empty,
new IntPtr(&entropy),
IntPtr.Zero,
IntPtr.Zero,
dwFlags,
new IntPtr(&blob)))
if (!CAPI.CryptProtectData(
pDataIn: new IntPtr(&dataIn),
szDataDescr: string.Empty,
pOptionalEntropy: new IntPtr(&entropy),
pvReserved: IntPtr.Zero,
pPromptStruct: IntPtr.Zero,
dwFlags: dwFlags,
pDataBlob: new IntPtr(&blob)))
{
int lastWin32Error = Marshal.GetLastWin32Error();

Expand All @@ -473,7 +486,9 @@ public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtec

// In some cases, the API would fail due to OOM but simply return a null pointer.
if (blob.pbData == IntPtr.Zero)
{
throw new OutOfMemoryException();
}

byte[] encryptedData = new byte[(int)blob.cbData];
Marshal.Copy(blob.pbData, encryptedData, 0, encryptedData.Length);
Expand All @@ -483,9 +498,13 @@ public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtec
finally
{
if (pbDataIn.IsAllocated)
{
pbDataIn.Free();
}
if (pOptionalEntropy.IsAllocated)
{
pOptionalEntropy.Free();
}
if (blob.pbData != IntPtr.Zero)
{
CAPI.ZeroMemory(blob.pbData, blob.cbData);
Expand All @@ -499,11 +518,10 @@ public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtec
/// </summary>
public static byte[] Unprotect(byte[] encryptedData, byte[] optionalEntropy, DataProtectionScope scope)
{
#if UNIX
throw new PlatformNotSupportedException(Serialization.DeserializeSecureStringNotSupported);
#else
if (encryptedData == null)
{
throw new ArgumentNullException("encryptedData");
}

GCHandle pbDataIn = new GCHandle();
GCHandle pOptionalEntropy = new GCHandle();
Expand All @@ -525,22 +543,30 @@ public static byte[] Unprotect(byte[] encryptedData, byte[] optionalEntropy, Dat

uint dwFlags = CAPI.CRYPTPROTECT_UI_FORBIDDEN;
if (scope == DataProtectionScope.LocalMachine)
{
dwFlags |= CAPI.CRYPTPROTECT_LOCAL_MACHINE;
}

unsafe
{
if (!CAPI.CryptUnprotectData(new IntPtr(&dataIn),
IntPtr.Zero,
new IntPtr(&entropy),
IntPtr.Zero,
IntPtr.Zero,
dwFlags,
new IntPtr(&userData)))
if (!CAPI.CryptUnprotectData(
pDataIn: new IntPtr(&dataIn),
ppszDataDescr: IntPtr.Zero,
pOptionalEntropy: new IntPtr(&entropy),
pvReserved: IntPtr.Zero,
pPromptStruct: IntPtr.Zero,
dwFlags: dwFlags,
pDataBlob: new IntPtr(&userData)))
{
throw new CryptographicException(Marshal.GetLastWin32Error());
}
}

// In some cases, the API would fail due to OOM but simply return a null pointer.
if (userData.pbData == IntPtr.Zero)
{
throw new OutOfMemoryException();
}

byte[] data = new byte[(int)userData.cbData];
Marshal.Copy(userData.pbData, data, 0, data.Length);
Expand All @@ -550,16 +576,19 @@ public static byte[] Unprotect(byte[] encryptedData, byte[] optionalEntropy, Dat
finally
{
if (pbDataIn.IsAllocated)
{
pbDataIn.Free();
}
if (pOptionalEntropy.IsAllocated)
{
pOptionalEntropy.Free();
}
if (userData.pbData != IntPtr.Zero)
{
CAPI.ZeroMemory(userData.pbData, userData.cbData);
CAPI.LocalFree(userData.pbData);
}
}
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ Describe "SecureString conversion tests" -Tags "CI" {
$string = "ABCD"
$secureString = [System.Security.SecureString]::New()
$string.ToCharArray() | foreach-object { $securestring.AppendChar($_) }
$defaultParamValues = $PSdefaultParameterValues.Clone()
$PSdefaultParameterValues = @{}
if ( ! $IsWindows ) { $PSdefaultParameterValues["it:pending"] = $true }
}
AfterAll {
$global:PSdefaultParameterValues = $defaultParamValues
}

It "using null arguments to ConvertFrom-SecureString produces an exception" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,29 +170,7 @@ Describe "CliXml test" -Tags "CI" {
$testPath | Should -Not -Exist
}

It "should fail to import PSCredential on non-Windows" -Skip:$IsWindows {
$cliXml = @"
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Management.Automation.PSCredential</T>
<T>System.Object</T>
</TN>
<ToString>System.Management.Automation.PSCredential</ToString>
<Props>
<S N="UserName">foo</S>
<!-- this is just an empty password, but needs to be a valid hash -->
<SS N="Password">01000000d08c9ddf0115d1118c7a00c04fc297eb01000000977756228672474da9bfbe7b6acc02cb0000000002000000000003660000c0000000100000002529194617877bbbc952c6aacb6535f00000000004800000a000000010000000703ca974777a8335d5e2f8b3e592ff4208000000635d9fcda035734e140000001b6940e3222117a1014c67377e8786549f3dca29</SS>
</Props>
</Obj>
</Objs>
"@
$path = "$testdrive/cred.xml"
Set-Content -Path $path -Value $cliXml
{ Import-Clixml -Path $path } | Should -Throw -ErrorId "System.PlatformNotSupportedException,Microsoft.PowerShell.Commands.ImportClixmlCommand"
}

It "should import PSCredential" -Skip:(!$IsWindows) {
It "should import PSCredential" {
$UserName = "Foo"
$pass = ConvertTo-SecureString (New-RandomHexString) -AsPlainText -Force
$cred = [PSCredential]::new($UserName, $pass)
Expand Down