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
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ protected override void ProcessRecord()
}
else
{
importedString = new SecureString();
foreach (char currentChar in String) { importedString.AppendChar(currentChar); }
importedString = SecureStringHelper.FromPlainTextString(String);
}
}
catch (ArgumentException e)
Expand Down
9 changes: 3 additions & 6 deletions src/System.Management.Automation/DscSupport/CimDSCParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using Microsoft.Management.Infrastructure.Serialization;
using Microsoft.PowerShell.Commands;

using static Microsoft.PowerShell.SecureStringHelper;

namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
{
/// <summary>
Expand Down Expand Up @@ -261,12 +263,7 @@ private static object ConvertCimInstancePsCredential(string providerName, CimIns
throw invalidOperationException;
}

// Extract the password into a SecureString.
var password = new SecureString();
foreach (char t in plainPassWord)
{
password.AppendChar(t);
}
SecureString password = SecureStringHelper.FromPlainTextString(plainPassWord);

password.MakeReadOnly();
return new PSCredential(userName, password);
Expand Down
21 changes: 21 additions & 0 deletions src/System.Management.Automation/security/SecureStringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Management.Automation;
Expand Down Expand Up @@ -385,6 +386,26 @@ internal static SecureString Decrypt(string input, byte[] key, byte[] IV)
return s;
}
}

#nullable enable
/// <summary>Creates a new <see cref="SecureString"/> from a <see cref="string"/>.</summary>
/// <param name="plainTextString">Plain text string. Must not be null.</param>
/// <returns>A new SecureString.</returns>
internal static unsafe SecureString FromPlainTextString(string plainTextString)
{
Debug.Assert(plainTextString is not null);

if (plainTextString.Length == 0)
{
return new SecureString();
}

fixed (char* charsPtr = plainTextString)
{
return new SecureString(charsPtr, plainTextString.Length);
}
}
#nullable restore
}

/// <summary>
Expand Down