Example #1
0
        /// <summary>
        /// PSCredential
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        private PSCredential(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
            {
                return;
            }

            _userName = (string)info.GetValue("UserName", typeof(string));

            // deserialize to secure string
            string safePassword = (string)info.GetValue("Password", typeof(string));

            if (safePassword == string.Empty)
            {
                _password = new SecureString();
            }
            else
            {
                byte[] key;
                byte[] iv;
                if (s_delegate != null && s_delegate(context, out key, out iv))
                {
                    _password = SecureStringHelper.Decrypt(safePassword, key, iv);
                }
                else
                {
                    _password = SecureStringHelper.Unprotect(safePassword);
                }
            }
        }
        /// <summary>
        /// Processes records from the input pipeline.
        /// For each input object, the command decrypts the data,
        /// then exports a new SecureString created from the object.
        /// </summary>
        protected override void ProcessRecord()
        {
            SecureString importedString = null;

            Utils.CheckArgForNullOrEmpty(_s, "String");

            try
            {
                string encryptedContent = String;
                byte[] iv = null;

                // If this is a V2 package
                if (String.IndexOf(SecureStringHelper.SecureStringExportHeader,
                                   StringComparison.OrdinalIgnoreCase) == 0)
                {
                    try
                    {
                        // Trim out the header, and retrieve the
                        // rest of the string
                        string remainingData = this.String.Substring(
                            SecureStringHelper.SecureStringExportHeader.Length,
                            String.Length - SecureStringHelper.SecureStringExportHeader.Length);

                        // Unpack it from Base64, get the string
                        // representation, then parse it into its components.
                        byte[]   inputBytes   = Convert.FromBase64String(remainingData);
                        string   dataPackage  = System.Text.Encoding.Unicode.GetString(inputBytes);
                        string[] dataElements = dataPackage.Split(Utils.Separators.Pipe);

                        if (dataElements.Length == 3)
                        {
                            encryptedContent = dataElements[2];
                            iv = Convert.FromBase64String(dataElements[1]);
                        }
                    }
                    catch (FormatException)
                    {
                        // Will be raised if we can't convert the
                        // input from a Base64 string. This means
                        // it's not really a V2 package.
                        encryptedContent = String;
                        iv = null;
                    }
                }

                if (SecureKey != null)
                {
                    Dbg.Diagnostics.Assert(Key == null, "Only one encryption key should be specified");
                    importedString = SecureStringHelper.Decrypt(encryptedContent, SecureKey, iv);
                }
                else if (Key != null)
                {
                    importedString = SecureStringHelper.Decrypt(encryptedContent, Key, iv);
                }
                else if (!AsPlainText)
                {
                    importedString = SecureStringHelper.Unprotect(String);
                }
                else
                {
                    importedString = SecureStringHelper.FromPlainTextString(String);
                }
            }
            catch (ArgumentException e)
            {
                ErrorRecord er =
                    SecurityUtils.CreateInvalidArgumentErrorRecord(
                        e,
                        "ImportSecureString_InvalidArgument"
                        );
                WriteError(er);
            }
            catch (CryptographicException e)
            {
                ErrorRecord er =
                    SecurityUtils.CreateInvalidArgumentErrorRecord(
                        e,
                        "ImportSecureString_InvalidArgument_CryptographicError"
                        );
                WriteError(er);
            }

            if (importedString != null)
            {
                WriteObject(importedString);
            }
        }
Example #3
0
        /// <summary>
        /// Processes records from the input pipeline.
        /// For each input object, the command decrypts the data,
        /// then exports a new SecureString created from the object.
        /// </summary>
        protected override void ProcessRecord()
        {
            SecureString importedString = null;

            Utils.CheckArgForNullOrEmpty(_s, "String");

            try
            {
                string encryptedContent = String;
                byte[] iv = null;

                // If this is a V2 package
                if (String.IndexOf(SecureStringHelper.SecureStringExportHeader,
                                   StringComparison.OrdinalIgnoreCase) == 0)
                {
                    try
                    {
                        // Trim out the header, and retrieve the
                        // rest of the string
                        string remainingData = this.String.Substring(
                            SecureStringHelper.SecureStringExportHeader.Length,
                            String.Length - SecureStringHelper.SecureStringExportHeader.Length);

                        // Unpack it from Base64, get the string
                        // representation, then parse it into its components.
                        byte[]   inputBytes   = Convert.FromBase64String(remainingData);
                        string   dataPackage  = System.Text.Encoding.Unicode.GetString(inputBytes);
                        string[] dataElements = dataPackage.Split(Utils.Separators.Pipe);

                        if (dataElements.Length == 3)
                        {
                            encryptedContent = dataElements[2];
                            iv = Convert.FromBase64String(dataElements[1]);
                        }
                    }
                    catch (FormatException)
                    {
                        // Will be raised if we can't convert the
                        // input from a Base64 string. This means
                        // it's not really a V2 package.
                        encryptedContent = String;
                        iv = null;
                    }
                }

                if (SecureKey != null)
                {
                    Dbg.Diagnostics.Assert(Key == null, "Only one encryption key should be specified");
                    importedString = SecureStringHelper.Decrypt(encryptedContent, SecureKey, iv);
                }
                else if (Key != null)
                {
                    importedString = SecureStringHelper.Decrypt(encryptedContent, Key, iv);
                }
                else if (!AsPlainText)
                {
                    importedString = SecureStringHelper.Unprotect(String);
                }
                else
                {
                    if (!Force)
                    {
                        string error =
                            SecureStringCommands.ForceRequired;
                        Exception e = new ArgumentException(error);
                        WriteError(new ErrorRecord(e, "ImportSecureString_ForceRequired", ErrorCategory.InvalidArgument, null));
                    }
                    else
                    {
                        // The entire purpose of the SecureString is to prevent a secret from being
                        // permanently stored in memory as a .Net string.  If they use the
                        // -AsPlainText and -Force flags, they consciously have made the decision to be OK
                        // with that.
                        importedString = new SecureString();
                        foreach (char currentChar in String)
                        {
                            importedString.AppendChar(currentChar);
                        }
                    }
                }
            }
            catch (ArgumentException e)
            {
                ErrorRecord er =
                    SecurityUtils.CreateInvalidArgumentErrorRecord(
                        e,
                        "ImportSecureString_InvalidArgument"
                        );
                WriteError(er);
            }
            catch (CryptographicException e)
            {
                ErrorRecord er =
                    SecurityUtils.CreateInvalidArgumentErrorRecord(
                        e,
                        "ImportSecureString_InvalidArgument_CryptographicError"
                        );
                WriteError(er);
            }

            if (importedString != null)
            {
                WriteObject(importedString);
            }
        }
        protected override void ProcessRecord()
        {
            SecureString secureString = null;

            Utils.CheckArgForNullOrEmpty(this.s, "String");
            try
            {
                string str      = this.String;
                byte[] numArray = null;
                if (this.String.IndexOf(SecureStringHelper.SecureStringExportHeader, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    try
                    {
                        string str1      = this.String.Substring(SecureStringHelper.SecureStringExportHeader.Length, this.String.Length - SecureStringHelper.SecureStringExportHeader.Length);
                        byte[] numArray1 = Convert.FromBase64String(str1);
                        string str2      = Encoding.Unicode.GetString(numArray1);
                        char[] chrArray  = new char[1];
                        chrArray[0] = '|';
                        string[] strArrays = str2.Split(chrArray);
                        if ((int)strArrays.Length == 3)
                        {
                            str      = strArrays[2];
                            numArray = Convert.FromBase64String(strArrays[1]);
                        }
                    }
                    catch (FormatException formatException)
                    {
                        str      = this.String;
                        numArray = null;
                    }
                }
                if (base.SecureKey == null)
                {
                    if (base.Key == null)
                    {
                        if (this.AsPlainText)
                        {
                            if (this.Force)
                            {
                                secureString = new SecureString();
                                string str3 = this.String;
                                for (int i = 0; i < str3.Length; i++)
                                {
                                    char chr = str3[i];
                                    secureString.AppendChar(chr);
                                }
                            }
                            else
                            {
                                string    forceRequired     = SecureStringCommands.ForceRequired;
                                Exception argumentException = new ArgumentException(forceRequired);
                                base.WriteError(new ErrorRecord(argumentException, "ImportSecureString_ForceRequired", ErrorCategory.InvalidArgument, null));
                            }
                        }
                        else
                        {
                            secureString = SecureStringHelper.Unprotect(this.String);
                        }
                    }
                    else
                    {
                        secureString = SecureStringHelper.Decrypt(str, base.Key, numArray);
                    }
                }
                else
                {
                    secureString = SecureStringHelper.Decrypt(str, base.SecureKey, numArray);
                }
            }
            catch (ArgumentException argumentException2)
            {
                ArgumentException argumentException1 = argumentException2;
                ErrorRecord       errorRecord        = SecurityUtils.CreateInvalidArgumentErrorRecord(argumentException1, "ImportSecureString_InvalidArgument");
                base.WriteError(errorRecord);
            }
            catch (CryptographicException cryptographicException1)
            {
                CryptographicException cryptographicException = cryptographicException1;
                ErrorRecord            errorRecord1           = SecurityUtils.CreateInvalidArgumentErrorRecord(cryptographicException, "ImportSecureString_InvalidArgument_CryptographicError");
                base.WriteError(errorRecord1);
            }
            if (secureString != null)
            {
                base.WriteObject(secureString);
            }
        }