Exemplo n.º 1
0
        /// <summary>
        /// Sets the digital signature on the specified file.
        /// </summary>
        /// <param name="filePath">
        /// The name of the file on which to perform the action.
        /// </param>
        /// <returns>
        /// The signature on the specified file.
        /// </returns>
        protected override Signature PerformAction(string filePath)
        {
            SigningOption option = GetSigningOption(IncludeChain);

            if (Certificate == null)
            {
                throw PSTraceSource.NewArgumentNullException("certificate");
            }

            //
            // if the cert is not good for signing, we cannot
            // process any more files. Exit the command.
            //
            if (!SecuritySupport.CertIsGoodForSigning(Certificate))
            {
                Exception e = PSTraceSource.NewArgumentException(
                    "certificate",
                    SignatureCommands.CertNotGoodForSigning);

                throw e;
            }

            if (!ShouldProcess(filePath))
            {
                return(null);
            }

            FileInfo readOnlyFileInfo = null;

            try
            {
                if (this.Force)
                {
                    try
                    {
                        // remove readonly attributes on the file
                        FileInfo fInfo = new(filePath);
                        if (fInfo != null)
                        {
                            // Save some disk write time by checking whether file is readonly..
                            if ((fInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                            {
                                // remember to reset the read-only attribute later
                                readOnlyFileInfo = fInfo;
                                // Make sure the file is not read only
                                fInfo.Attributes &= ~(FileAttributes.ReadOnly);
                            }
                        }
                    }
                    // These are the known exceptions for File.Load and StreamWriter.ctor
                    catch (ArgumentException e)
                    {
                        ErrorRecord er = new(
                            e,
                            "ForceArgumentException",
                            ErrorCategory.WriteError,
                            filePath);
                        WriteError(er);
                        return(null);
                    }
                    catch (IOException e)
                    {
                        ErrorRecord er = new(
                            e,
                            "ForceIOException",
                            ErrorCategory.WriteError,
                            filePath);
                        WriteError(er);
                        return(null);
                    }
                    catch (UnauthorizedAccessException e)
                    {
                        ErrorRecord er = new(
                            e,
                            "ForceUnauthorizedAccessException",
                            ErrorCategory.PermissionDenied,
                            filePath);
                        WriteError(er);
                        return(null);
                    }
                    catch (NotSupportedException e)
                    {
                        ErrorRecord er = new(
                            e,
                            "ForceNotSupportedException",
                            ErrorCategory.WriteError,
                            filePath);
                        WriteError(er);
                        return(null);
                    }
                    catch (System.Security.SecurityException e)
                    {
                        ErrorRecord er = new(
                            e,
                            "ForceSecurityException",
                            ErrorCategory.PermissionDenied,
                            filePath);
                        WriteError(er);
                        return(null);
                    }
                }

                //
                // ProcessRecord() code in base class has already
                // ascertained that filePath really represents an existing
                // file. Thus we can safely call GetFileSize() below.
                //

                if (SecurityUtils.GetFileSize(filePath) < 4)
                {
                    // Note that the message param comes first
                    string message = string.Format(
                        System.Globalization.CultureInfo.CurrentCulture,
                        UtilsStrings.FileSmallerThan4Bytes, filePath);

                    PSArgumentException e  = new(message, nameof(filePath));
                    ErrorRecord         er = SecurityUtils.CreateInvalidArgumentErrorRecord(
                        e,
                        "SignatureCommandsBaseFileSmallerThan4Bytes"
                        );

                    WriteError(er);

                    return(null);
                }

                return(SignatureHelper.SignFile(option,
                                                filePath,
                                                Certificate,
                                                TimestampServer,
                                                _hashAlgorithm));
            }
            finally
            {
                // reset the read-only attribute
                if (readOnlyFileInfo != null)
                {
                    readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly;
                }
            }
        }
Exemplo n.º 2
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);
            }
        }
        /// <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);
            }
        }
Exemplo n.º 4
0
        protected override void ProcessRecord()
        {
            string         sddlForm;
            ObjectSecurity objectSecurity = this.securityDescriptor as ObjectSecurity;

            if (this.inputObject == null)
            {
                if (this.Path != null)
                {
                    if (objectSecurity != null)
                    {
                        if ((this.CentralAccessPolicy != null || this.ClearCentralAccessPolicy) && !DownLevelHelper.IsWin8AndAbove())
                        {
                            Exception parameterBindingException = new ParameterBindingException();
                            base.WriteError(new ErrorRecord(parameterBindingException, "SetAcl_OperationNotSupported", ErrorCategory.InvalidArgument, null));
                            return;
                        }
                        else
                        {
                            if (this.CentralAccessPolicy == null || !this.ClearCentralAccessPolicy)
                            {
                                IntPtr zero = IntPtr.Zero;
                                NativeMethods.TOKEN_PRIVILEGE tOKENPRIVILEGE = new NativeMethods.TOKEN_PRIVILEGE();
                                try
                                {
                                    if (this.CentralAccessPolicy == null)
                                    {
                                        if (this.ClearCentralAccessPolicy)
                                        {
                                            zero = this.GetEmptySacl();
                                            if (zero == IntPtr.Zero)
                                            {
                                                SystemException systemException = new SystemException(UtilsStrings.GetEmptySaclFail);
                                                base.WriteError(new ErrorRecord(systemException, "SetAcl_ClearCentralAccessPolicy", ErrorCategory.InvalidResult, null));
                                                return;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        zero = this.GetSaclWithCapId(this.CentralAccessPolicy);
                                        if (zero == IntPtr.Zero)
                                        {
                                            SystemException systemException1 = new SystemException(UtilsStrings.GetSaclWithCapIdFail);
                                            base.WriteError(new ErrorRecord(systemException1, "SetAcl_CentralAccessPolicy", ErrorCategory.InvalidResult, null));
                                            return;
                                        }
                                    }
                                    string[] path = this.Path;
                                    for (int i = 0; i < (int)path.Length; i++)
                                    {
                                        string str = path[i];
                                        Collection <PathInfo> pathInfos             = new Collection <PathInfo>();
                                        CmdletProviderContext cmdletProviderContext = base.CmdletProviderContext;
                                        cmdletProviderContext.PassThru = this.Passthru;
                                        if (!this.isLiteralPath)
                                        {
                                            pathInfos = base.SessionState.Path.GetResolvedPSPathFromPSPath(str, base.CmdletProviderContext);
                                        }
                                        else
                                        {
                                            ProviderInfo providerInfo = null;
                                            PSDriveInfo  pSDriveInfo  = null;
                                            string       unresolvedProviderPathFromPSPath = base.SessionState.Path.GetUnresolvedProviderPathFromPSPath(str, out providerInfo, out pSDriveInfo);
                                            pathInfos.Add(new PathInfo(pSDriveInfo, providerInfo, unresolvedProviderPathFromPSPath, base.SessionState));
                                            cmdletProviderContext.SuppressWildcardExpansion = true;
                                        }
                                        foreach (PathInfo pathInfo in pathInfos)
                                        {
                                            if (!base.ShouldProcess(pathInfo.Path))
                                            {
                                                continue;
                                            }
                                            try
                                            {
                                                base.InvokeProvider.SecurityDescriptor.Set(pathInfo.Path, objectSecurity, cmdletProviderContext);
                                                if (this.CentralAccessPolicy != null || this.ClearCentralAccessPolicy)
                                                {
                                                    if (pathInfo.Provider.NameEquals(base.Context.ProviderNames.FileSystem))
                                                    {
                                                        IntPtr tokenWithEnabledPrivilege = this.GetTokenWithEnabledPrivilege("SeSecurityPrivilege", tOKENPRIVILEGE);
                                                        if (tokenWithEnabledPrivilege != IntPtr.Zero)
                                                        {
                                                            int num = NativeMethods.SetNamedSecurityInfo(pathInfo.ProviderPath, NativeMethods.SeObjectType.SE_FILE_OBJECT, NativeMethods.SecurityInformation.SCOPE_SECURITY_INFORMATION, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, zero);
                                                            if (tokenWithEnabledPrivilege != IntPtr.Zero)
                                                            {
                                                                NativeMethods.TOKEN_PRIVILEGE tOKENPRIVILEGE1 = new NativeMethods.TOKEN_PRIVILEGE();
                                                                uint num1 = 0;
                                                                NativeMethods.AdjustTokenPrivileges(tokenWithEnabledPrivilege, false, ref tOKENPRIVILEGE, Marshal.SizeOf(tOKENPRIVILEGE1), ref tOKENPRIVILEGE1, ref num1);
                                                                NativeMethods.CloseHandle(tokenWithEnabledPrivilege);
                                                            }
                                                            if (num != 0)
                                                            {
                                                                SystemException win32Exception = new Win32Exception(num, UtilsStrings.SetCentralAccessPolicyFail);
                                                                base.WriteError(new ErrorRecord(win32Exception, "SetAcl_SetNamedSecurityInfo", ErrorCategory.InvalidResult, null));
                                                            }
                                                        }
                                                        else
                                                        {
                                                            SystemException systemException2 = new SystemException(UtilsStrings.GetTokenWithEnabledPrivilegeFail);
                                                            base.WriteError(new ErrorRecord(systemException2, "SetAcl_AdjustTokenPrivileges", ErrorCategory.InvalidResult, null));
                                                            return;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        Exception argumentException = new ArgumentException("Path");
                                                        base.WriteError(new ErrorRecord(argumentException, "SetAcl_Path", ErrorCategory.InvalidArgument, this.AclObject));
                                                        continue;
                                                    }
                                                }
                                            }
                                            catch (NotSupportedException notSupportedException)
                                            {
                                                object[] objArray = new object[1];
                                                objArray[0] = pathInfo.Path;
                                                ErrorRecord errorRecord = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.OperationNotSupportedOnPath, "SetAcl_OperationNotSupported", objArray);
                                                base.WriteError(errorRecord);
                                            }
                                        }
                                    }
                                    return;
                                }
                                finally
                                {
                                    Marshal.FreeHGlobal(zero);
                                }
                            }
                            else
                            {
                                Exception   exception    = new ArgumentException(UtilsStrings.InvalidCentralAccessPolicyParameters);
                                ErrorRecord errorRecord1 = SecurityUtils.CreateInvalidArgumentErrorRecord(exception, "SetAcl_OperationNotSupported");
                                base.WriteError(errorRecord1);
                                return;
                            }
                        }
                    }
                    else
                    {
                        Exception argumentException1 = new ArgumentException("AclObject");
                        base.WriteError(new ErrorRecord(argumentException1, "SetAcl_AclObject", ErrorCategory.InvalidArgument, this.AclObject));
                        return;
                    }
                }
                else
                {
                    Exception exception1 = new ArgumentException("Path");
                    base.WriteError(new ErrorRecord(exception1, "SetAcl_Path", ErrorCategory.InvalidArgument, this.AclObject));
                }
            }
            else
            {
                PSMethodInfo item = this.inputObject.Methods["SetSecurityDescriptor"];
                if (item == null)
                {
                    ErrorRecord errorRecord2 = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.SetMethodNotFound, "SetAcl_OperationNotSupported", new object[0]);
                    base.WriteError(errorRecord2);
                    return;
                }
                else
                {
                    CommonSecurityDescriptor commonSecurityDescriptor = this.securityDescriptor as CommonSecurityDescriptor;
                    if (objectSecurity == null)
                    {
                        if (commonSecurityDescriptor == null)
                        {
                            Exception argumentException2 = new ArgumentException("AclObject");
                            base.WriteError(new ErrorRecord(argumentException2, "SetAcl_AclObject", ErrorCategory.InvalidArgument, this.AclObject));
                            return;
                        }
                        else
                        {
                            sddlForm = commonSecurityDescriptor.GetSddlForm(AccessControlSections.All);
                        }
                    }
                    else
                    {
                        sddlForm = objectSecurity.GetSecurityDescriptorSddlForm(AccessControlSections.All);
                    }
                    try
                    {
                        object[] objArray1 = new object[1];
                        objArray1[0] = sddlForm;
                        item.Invoke(objArray1);
                        return;
                    }
                    catch (Exception exception3)
                    {
                        Exception exception2 = exception3;
                        CommandProcessorBase.CheckForSevereException(exception2);
                        ErrorRecord errorRecord3 = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.MethodInvokeFail, "SetAcl_OperationNotSupported", new object[0]);
                        base.WriteError(errorRecord3);
                    }
                }
            }
        }
Exemplo n.º 5
0
        protected override Signature PerformAction(string filePath)
        {
            Signature     signature;
            SigningOption signingOption = SetAuthenticodeSignatureCommand.GetSigningOption(this.IncludeChain);

            if (this.Certificate != null)
            {
                if (SecuritySupport.CertIsGoodForSigning(this.Certificate))
                {
                    if (base.ShouldProcess(filePath))
                    {
                        FileInfo fileInfo = null;
                        try
                        {
                            if (this.Force)
                            {
                                try
                                {
                                    FileInfo fileInfo1 = new FileInfo(filePath);
                                    if (fileInfo1 != null && (fileInfo1.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                    {
                                        fileInfo = fileInfo1;
                                        FileInfo attributes = fileInfo1;
                                        attributes.Attributes = attributes.Attributes & (FileAttributes.Hidden | FileAttributes.System | FileAttributes.Directory | FileAttributes.Archive | FileAttributes.Device | FileAttributes.Normal |
                                                                                         FileAttributes.Temporary | FileAttributes.SparseFile | FileAttributes.ReparsePoint | FileAttributes.Compressed | FileAttributes.Offline |
                                                                                         FileAttributes.NotContentIndexed | FileAttributes.Encrypted
#if !MONO
                                                                                         | FileAttributes.IntegrityStream | FileAttributes.NoScrubData
#endif
                                                                                         );
                                    }
                                }
                                catch (ArgumentException argumentException1)
                                {
                                    ArgumentException argumentException = argumentException1;
                                    ErrorRecord       errorRecord       = new ErrorRecord(argumentException, "ForceArgumentException", ErrorCategory.WriteError, filePath);
                                    base.WriteError(errorRecord);
                                    signature = null;
                                    return(signature);
                                }
                                catch (IOException oException1)
                                {
                                    IOException oException   = oException1;
                                    ErrorRecord errorRecord1 = new ErrorRecord(oException, "ForceIOException", ErrorCategory.WriteError, filePath);
                                    base.WriteError(errorRecord1);
                                    signature = null;
                                    return(signature);
                                }
                                catch (UnauthorizedAccessException unauthorizedAccessException1)
                                {
                                    UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                                    ErrorRecord errorRecord2 = new ErrorRecord(unauthorizedAccessException, "ForceUnauthorizedAccessException", ErrorCategory.PermissionDenied, filePath);
                                    base.WriteError(errorRecord2);
                                    signature = null;
                                    return(signature);
                                }
                                catch (NotSupportedException notSupportedException1)
                                {
                                    NotSupportedException notSupportedException = notSupportedException1;
                                    ErrorRecord           errorRecord3          = new ErrorRecord(notSupportedException, "ForceNotSupportedException", ErrorCategory.WriteError, filePath);
                                    base.WriteError(errorRecord3);
                                    signature = null;
                                    return(signature);
                                }
                                catch (SecurityException securityException1)
                                {
                                    SecurityException securityException = securityException1;
                                    ErrorRecord       errorRecord4      = new ErrorRecord(securityException, "ForceSecurityException", ErrorCategory.PermissionDenied, filePath);
                                    base.WriteError(errorRecord4);
                                    signature = null;
                                    return(signature);
                                }
                            }
                            if (SecurityUtils.GetFileSize(filePath) >= (long)4)
                            {
                                signature = SignatureHelper.SignFile(signingOption, filePath, this.Certificate, this.TimestampServer, this.hashAlgorithm);
                            }
                            else
                            {
                                object[] objArray = new object[1];
                                objArray[0] = filePath;
                                string str = string.Format(CultureInfo.CurrentCulture, UtilsStrings.FileSmallerThan4Bytes, objArray);
                                PSArgumentException pSArgumentException = new PSArgumentException(str, "filePath");
                                ErrorRecord         errorRecord5        = SecurityUtils.CreateInvalidArgumentErrorRecord(pSArgumentException, "SignatureCommandsBaseFileSmallerThan4Bytes");
                                base.WriteError(errorRecord5);
                                signature = null;
                            }
                        }
                        finally
                        {
                            if (fileInfo != null)
                            {
                                FileInfo attributes1 = fileInfo;
                                attributes1.Attributes = attributes1.Attributes | FileAttributes.ReadOnly;
                            }
                        }
                        return(signature);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    Exception exception = PSTraceSource.NewArgumentException("certificate", "SignatureCommands", "CertNotGoodForSigning", new object[0]);
                    throw exception;
                }
            }
            else
            {
                throw PSTraceSource.NewArgumentNullException("certificate");
            }
        }
        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);
            }
        }