From a6abf1694105eb184b748acb5dd08dfd6801c611 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Mon, 12 Apr 2021 22:05:20 +0100 Subject: [PATCH] Add SecureStringHelper.FromPlainTextString (#14124) --- .../security/SecureStringCommands.cs | 3 +-- .../DscSupport/CimDSCParser.cs | 9 +++----- .../security/SecureStringHelper.cs | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Security/security/SecureStringCommands.cs b/src/Microsoft.PowerShell.Security/security/SecureStringCommands.cs index 6333541e24..da4dcf59d4 100644 --- a/src/Microsoft.PowerShell.Security/security/SecureStringCommands.cs +++ b/src/Microsoft.PowerShell.Security/security/SecureStringCommands.cs @@ -359,8 +359,7 @@ namespace Microsoft.PowerShell.Commands } else { - importedString = new SecureString(); - foreach (char currentChar in String) { importedString.AppendChar(currentChar); } + importedString = SecureStringHelper.FromPlainTextString(String); } } catch (ArgumentException e) diff --git a/src/System.Management.Automation/DscSupport/CimDSCParser.cs b/src/System.Management.Automation/DscSupport/CimDSCParser.cs index 5dcd8947a4..d5374f5c50 100644 --- a/src/System.Management.Automation/DscSupport/CimDSCParser.cs +++ b/src/System.Management.Automation/DscSupport/CimDSCParser.cs @@ -22,6 +22,8 @@ using Microsoft.Management.Infrastructure.Generic; using Microsoft.Management.Infrastructure.Serialization; using Microsoft.PowerShell.Commands; +using static Microsoft.PowerShell.SecureStringHelper; + namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal { /// @@ -259,12 +261,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal 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); diff --git a/src/System.Management.Automation/security/SecureStringHelper.cs b/src/System.Management.Automation/security/SecureStringHelper.cs index 4611463ae0..6d38859162 100644 --- a/src/System.Management.Automation/security/SecureStringHelper.cs +++ b/src/System.Management.Automation/security/SecureStringHelper.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.Diagnostics; using System.Globalization; using System.IO; using System.Management.Automation; @@ -385,6 +386,26 @@ namespace Microsoft.PowerShell return s; } } + +#nullable enable + /// Creates a new from a . + /// Plain text string. Must not be null. + /// A new SecureString. + 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 } ///