public bool MoveNext() { bool result = false; if (_justReset) { _justReset = false; if (!_patternEnumerator.MoveNext()) { s_tracer.TraceError("No patterns were specified"); return(false); } if (!_lookupPathsEnumerator.MoveNext()) { s_tracer.TraceError("No lookup paths were specified"); return(false); } GetNewDirectoryResults(_patternEnumerator.Current, _lookupPathsEnumerator.Current); } while (true) // while lookupPathsEnumerator is valid { while (true) // while patternEnumerator is valid { // Try moving to the next path in the current results if (!_currentDirectoryResultsEnumerator.MoveNext()) { s_tracer.WriteLine("Current directory results are invalid"); // Since a path was not found in the current result, // advance the pattern and try again if (!_patternEnumerator.MoveNext()) { s_tracer.WriteLine("Current patterns exhausted in current directory: {0}", _lookupPathsEnumerator.Current); break; } // Get the results of the next pattern GetNewDirectoryResults(_patternEnumerator.Current, _lookupPathsEnumerator.Current); } else { s_tracer.WriteLine("Next path found: {0}", _currentDirectoryResultsEnumerator.Current); result = true; break; } // Since we have reset the results, loop again to find the next result. } if (result) { break; } // Since the path was not found in the current results, and all patterns were exhausted, // advance the path and continue if (!_lookupPathsEnumerator.MoveNext()) { s_tracer.WriteLine("All lookup paths exhausted, no more matches can be found"); break; } // Reset the pattern enumerator and get new results using the new lookup path _patternEnumerator = _patterns.GetEnumerator(); if (!_patternEnumerator.MoveNext()) { s_tracer.WriteLine("All patterns exhausted, no more matches can be found"); break; } GetNewDirectoryResults(_patternEnumerator.Current, _lookupPathsEnumerator.Current); } return(result); }
internal static Signature SignFile(SigningOption option, string fileName, X509Certificate2 certificate, string timeStampServerUrl, string hashAlgorithm) { bool result = false; Signature signature = null; IntPtr pSignInfo = IntPtr.Zero; DWORD error = 0; string hashOid = null; Utils.CheckArgForNullOrEmpty(fileName, "fileName"); Utils.CheckArgForNull(certificate, "certificate"); // If given, TimeStamp server URLs must begin with http:// if (!String.IsNullOrEmpty(timeStampServerUrl)) { if ((timeStampServerUrl.Length <= 7) || (timeStampServerUrl.IndexOf("http://", StringComparison.OrdinalIgnoreCase) != 0)) { throw PSTraceSource.NewArgumentException( "certificate", Authenticode.TimeStampUrlRequired); } } // Validate that the hash algorithm is valid if (!String.IsNullOrEmpty(hashAlgorithm)) { IntPtr intptrAlgorithm = Marshal.StringToHGlobalUni(hashAlgorithm); IntPtr oidPtr = NativeMethods.CryptFindOIDInfo(NativeConstants.CRYPT_OID_INFO_NAME_KEY, intptrAlgorithm, 0); // If we couldn't find an OID for the hash // algorithm, it was invalid. if (oidPtr == IntPtr.Zero) { throw PSTraceSource.NewArgumentException( "certificate", Authenticode.InvalidHashAlgorithm); } else { NativeMethods.CRYPT_OID_INFO oidInfo = ClrFacade.PtrToStructure <NativeMethods.CRYPT_OID_INFO>(oidPtr); hashOid = oidInfo.pszOID; } } if (!SecuritySupport.CertIsGoodForSigning(certificate)) { throw PSTraceSource.NewArgumentException( "certificate", Authenticode.CertNotGoodForSigning); } SecuritySupport.CheckIfFileExists(fileName); //SecurityUtils.CheckIfFileSmallerThan4Bytes(fileName); try { // CryptUI is not documented either way, but does not // support empty strings for the timestamp server URL. // It expects null, only. Instead, it randomly AVs if you // try. string timeStampServerUrlForCryptUI = null; if (!String.IsNullOrEmpty(timeStampServerUrl)) { timeStampServerUrlForCryptUI = timeStampServerUrl; } // // first initialize the struct to pass to // CryptUIWizDigitalSign() function // NativeMethods.CRYPTUI_WIZ_DIGITAL_SIGN_INFO si = NativeMethods.InitSignInfoStruct(fileName, certificate, timeStampServerUrlForCryptUI, hashOid, option); pSignInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(si)); Marshal.StructureToPtr(si, pSignInfo, false); // // sign the file // // The GetLastWin32Error of this is checked, but PreSharp doesn't seem to be // able to see that. #pragma warning disable 56523 result = NativeMethods.CryptUIWizDigitalSign( (DWORD)NativeMethods.CryptUIFlags.CRYPTUI_WIZ_NO_UI, IntPtr.Zero, IntPtr.Zero, pSignInfo, IntPtr.Zero); #pragma warning enable 56523 if (si.pSignExtInfo != null) { ClrFacade.DestroyStructure <NativeMethods.CRYPTUI_WIZ_DIGITAL_SIGN_EXTENDED_INFO>(si.pSignExtInfo); Marshal.FreeCoTaskMem(si.pSignExtInfo); } if (!result) { error = GetLastWin32Error(); // // ISSUE-2004/05/08-kumarp : there seems to be a bug // in CryptUIWizDigitalSign(). // It returns 80004005 or 80070001 // but it signs the file correctly. Mask this error // till we figure out this odd behavior. // if ((error == 0x80004005) || (error == 0x80070001) || // CryptUIWizDigitalSign introduced a breaking change in Win8 to return this // error code (ERROR_INTERNET_NAME_NOT_RESOLVED) when you provide an invalid // timestamp server. It used to be 0x80070001. // Also masking this out so that we don't introduce a breaking change ourselves. (error == 0x80072EE7) ) { result = true; } else { if (error == Win32Errors.NTE_BAD_ALGID) { throw PSTraceSource.NewArgumentException( "certificate", Authenticode.InvalidHashAlgorithm); } s_tracer.TraceError("CryptUIWizDigitalSign: failed: {0:x}", error); } } if (result) { signature = GetSignature(fileName, null); } else { signature = new Signature(fileName, (DWORD)error); } } finally { ClrFacade.DestroyStructure <NativeMethods.CRYPTUI_WIZ_DIGITAL_SIGN_INFO>(pSignInfo); Marshal.FreeCoTaskMem(pSignInfo); } return(signature); }
internal static System.Management.Automation.Signature SignFile(SigningOption option, string fileName, X509Certificate2 certificate, string timeStampServerUrl, string hashAlgorithm) { bool flag = false; System.Management.Automation.Signature signature = null; IntPtr zero = IntPtr.Zero; uint error = 0; string pszOID = null; Utils.CheckArgForNullOrEmpty(fileName, "fileName"); Utils.CheckArgForNull(certificate, "certificate"); if (!string.IsNullOrEmpty(timeStampServerUrl) && ((timeStampServerUrl.Length <= 7) || (timeStampServerUrl.IndexOf("http://", StringComparison.OrdinalIgnoreCase) != 0))) { throw PSTraceSource.NewArgumentException("certificate", "Authenticode", "TimeStampUrlRequired", new object[0]); } if (!string.IsNullOrEmpty(hashAlgorithm)) { IntPtr pvKey = Marshal.StringToHGlobalUni(hashAlgorithm); IntPtr ptr = System.Management.Automation.Security.NativeMethods.CryptFindOIDInfo(2, pvKey, 0); if (ptr == IntPtr.Zero) { throw PSTraceSource.NewArgumentException("certificate", "Authenticode", "InvalidHashAlgorithm", new object[0]); } System.Management.Automation.Security.NativeMethods.CRYPT_OID_INFO crypt_oid_info = (System.Management.Automation.Security.NativeMethods.CRYPT_OID_INFO)Marshal.PtrToStructure(ptr, typeof(System.Management.Automation.Security.NativeMethods.CRYPT_OID_INFO)); pszOID = crypt_oid_info.pszOID; } if (!SecuritySupport.CertIsGoodForSigning(certificate)) { throw PSTraceSource.NewArgumentException("certificate", "Authenticode", "CertNotGoodForSigning", new object[0]); } SecuritySupport.CheckIfFileExists(fileName); try { string str2 = null; if (!string.IsNullOrEmpty(timeStampServerUrl)) { str2 = timeStampServerUrl; } System.Management.Automation.Security.NativeMethods.CRYPTUI_WIZ_DIGITAL_SIGN_INFO structure = System.Management.Automation.Security.NativeMethods.InitSignInfoStruct(fileName, certificate, str2, pszOID, option); zero = Marshal.AllocCoTaskMem(Marshal.SizeOf(structure)); Marshal.StructureToPtr(structure, zero, false); flag = System.Management.Automation.Security.NativeMethods.CryptUIWizDigitalSign(1, IntPtr.Zero, IntPtr.Zero, zero, IntPtr.Zero); Marshal.DestroyStructure(structure.pSignExtInfo, typeof(System.Management.Automation.Security.NativeMethods.CRYPTUI_WIZ_DIGITAL_SIGN_EXTENDED_INFO)); Marshal.FreeCoTaskMem(structure.pSignExtInfo); if (!flag) { error = GetLastWin32Error(); switch (error) { case 0x80004005: case 0x80070001: case 0x80072ee7: flag = true; goto Label_01CF; case 0x80090008: throw PSTraceSource.NewArgumentException("certificate", "Authenticode", "InvalidHashAlgorithm", new object[0]); } tracer.TraceError("CryptUIWizDigitalSign: failed: {0:x}", new object[] { error }); } Label_01CF: if (flag) { return(GetSignature(fileName, null)); } signature = new System.Management.Automation.Signature(fileName, error); } finally { Marshal.DestroyStructure(zero, typeof(System.Management.Automation.Security.NativeMethods.CRYPTUI_WIZ_DIGITAL_SIGN_INFO)); Marshal.FreeCoTaskMem(zero); } return(signature); }
public bool MoveNext() { bool flag = false; if (this.justReset) { this.justReset = false; if (!this.patternEnumerator.MoveNext()) { tracer.TraceError("No patterns were specified", new object[0]); return(false); } if (!this.lookupPathsEnumerator.MoveNext()) { tracer.TraceError("No lookup paths were specified", new object[0]); return(false); } this.GetNewDirectoryResults(this.patternEnumerator.Current, this.lookupPathsEnumerator.Current); } Label_0075: if (!this.currentDirectoryResultsEnumerator.MoveNext()) { tracer.WriteLine("Current directory results are invalid", new object[0]); if (!this.patternEnumerator.MoveNext()) { tracer.WriteLine("Current patterns exhausted in current directory: {0}", new object[] { this.lookupPathsEnumerator.Current }); goto Label_0118; } this.GetNewDirectoryResults(this.patternEnumerator.Current, this.lookupPathsEnumerator.Current); } else { tracer.WriteLine("Next path found: {0}", new object[] { this.currentDirectoryResultsEnumerator.Current }); flag = true; goto Label_0118; } if (!flag) { goto Label_0075; } Label_0118: if (!flag) { if (!this.lookupPathsEnumerator.MoveNext()) { tracer.WriteLine("All lookup paths exhausted, no more matches can be found", new object[0]); } else { this.patternEnumerator = this.patterns.GetEnumerator(); if (!this.patternEnumerator.MoveNext()) { tracer.WriteLine("All patterns exhausted, no more matches can be found", new object[0]); } else { this.GetNewDirectoryResults(this.patternEnumerator.Current, this.lookupPathsEnumerator.Current); if (!flag) { goto Label_0075; } } } } tracer.WriteLine("result = {0}", new object[] { flag }); return(flag); }
private object CoerceTypeAsNeeded(CommandParameterInternal argument, string parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, object currentValue) { if (argument == null) { throw PSTraceSource.NewArgumentNullException("argument"); } if (toType == null) { throw PSTraceSource.NewArgumentNullException("toType"); } if (collectionTypeInfo == null) { collectionTypeInfo = new ParameterCollectionTypeInformation(toType); } object result = currentValue; using (bindingTracer.TraceScope("COERCE arg to [{0}]", new object[] { toType })) { Type c = null; try { if (IsNullParameterValue(currentValue)) { return(this.HandleNullParameterForSpecialTypes(argument, parameterName, toType, currentValue)); } c = currentValue.GetType(); if (toType.IsAssignableFrom(c)) { bindingTracer.WriteLine("Parameter and arg types the same, no coercion is needed.", new object[0]); return(currentValue); } bindingTracer.WriteLine("Trying to convert argument value from {0} to {1}", new object[] { c, toType }); if (toType == typeof(PSObject)) { if ((this.command != null) && (currentValue == this.command.CurrentPipelineObject.BaseObject)) { currentValue = this.command.CurrentPipelineObject; } bindingTracer.WriteLine("The parameter is of type [{0}] and the argument is an PSObject, so the parameter value is the argument value wrapped into an PSObject.", new object[] { toType }); return(LanguagePrimitives.AsPSObjectOrNull(currentValue)); } if ((toType == typeof(string)) && (c == typeof(PSObject))) { PSObject obj3 = (PSObject)currentValue; if (obj3 == AutomationNull.Value) { bindingTracer.WriteLine("CONVERT a null PSObject to a null string.", new object[0]); return(null); } } if (((toType == typeof(bool)) || (toType == typeof(SwitchParameter))) || (toType == typeof(bool?))) { Type type = null; if (c == typeof(PSObject)) { PSObject obj4 = (PSObject)currentValue; currentValue = obj4.BaseObject; if (currentValue is SwitchParameter) { SwitchParameter parameter = (SwitchParameter)currentValue; currentValue = parameter.IsPresent; } type = currentValue.GetType(); } else { type = c; } if (type == typeof(bool)) { if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject((bool)currentValue)); } return(new SwitchParameter((bool)currentValue)); } if (type == typeof(int)) { if (((int)LanguagePrimitives.ConvertTo(currentValue, typeof(int), CultureInfo.InvariantCulture)) != 0) { if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject(true)); } return(new SwitchParameter(true)); } if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject(false)); } return(new SwitchParameter(false)); } if (LanguagePrimitives.IsNumeric(Type.GetTypeCode(type))) { double num = (double)LanguagePrimitives.ConvertTo(currentValue, typeof(double), CultureInfo.InvariantCulture); if (num == 0.0) { if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject(false)); } return(new SwitchParameter(false)); } if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject(true)); } return(new SwitchParameter(true)); } ParameterBindingException exception = new ParameterBindingException(ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(argument), parameterName, toType, c, "ParameterBinderStrings", "CannotConvertArgument", new object[] { type, "" }); throw exception; } if ((collectionTypeInfo.ParameterCollectionType == ParameterCollectionType.ICollectionGeneric) || (collectionTypeInfo.ParameterCollectionType == ParameterCollectionType.IList)) { object obj5 = PSObject.Base(currentValue); if (obj5 != null) { ConversionRank conversionRank = LanguagePrimitives.GetConversionRank(obj5.GetType(), toType); if ((((conversionRank == ConversionRank.Constructor) || (conversionRank == ConversionRank.ImplicitCast)) || (conversionRank == ConversionRank.ExplicitCast)) && LanguagePrimitives.TryConvertTo(currentValue, toType, Thread.CurrentThread.CurrentCulture, out result)) { return(result); } } } if (collectionTypeInfo.ParameterCollectionType != ParameterCollectionType.NotCollection) { bindingTracer.WriteLine("ENCODING arg into collection", new object[0]); bool coercionRequired = false; return(this.EncodeCollection(argument, parameterName, collectionTypeInfo, toType, currentValue, collectionTypeInfo.ElementType != null, out coercionRequired)); } if (((((GetIList(currentValue) != null) && (toType != typeof(object))) && ((toType != typeof(PSObject)) && (toType != typeof(PSListModifier)))) && ((!toType.IsGenericType || (toType.GetGenericTypeDefinition() != typeof(PSListModifier <>))) && (!toType.IsGenericType || (toType.GetGenericTypeDefinition() != typeof(FlagsExpression <>))))) && !toType.IsEnum) { throw new NotSupportedException(); } bindingTracer.WriteLine("CONVERT arg type to param type using LanguagePrimitives.ConvertTo", new object[0]); bool flag2 = false; if (this.context.LanguageMode == PSLanguageMode.ConstrainedLanguage) { object obj6 = PSObject.Base(currentValue); bool flag3 = obj6 is PSObject; bool flag4 = (obj6 != null) && typeof(IDictionary).IsAssignableFrom(obj6.GetType()); flag2 = ((((PSLanguageMode)this.Command.CommandInfo.DefiningLanguageMode) == PSLanguageMode.FullLanguage) && !flag3) && !flag4; } try { if (flag2) { this.context.LanguageMode = PSLanguageMode.FullLanguage; } result = LanguagePrimitives.ConvertTo(currentValue, toType, Thread.CurrentThread.CurrentCulture); } finally { if (flag2) { this.context.LanguageMode = PSLanguageMode.ConstrainedLanguage; } } bindingTracer.WriteLine("CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [{0}]", new object[] { (result == null) ? "null" : result.ToString() }); return(result); } catch (NotSupportedException exception2) { bindingTracer.TraceError("ERROR: COERCE FAILED: arg [{0}] could not be converted to the parameter type [{1}]", new object[] { (result == null) ? "null" : result, toType }); ParameterBindingException exception3 = new ParameterBindingException(exception2, ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(argument), parameterName, toType, c, "ParameterBinderStrings", "CannotConvertArgument", new object[] { (result == null) ? "null" : result, exception2.Message }); throw exception3; } catch (PSInvalidCastException exception4) { object[] args = new object[] { result ?? "null", toType }; bindingTracer.TraceError("ERROR: COERCE FAILED: arg [{0}] could not be converted to the parameter type [{1}]", args); ParameterBindingException exception5 = new ParameterBindingException(exception4, ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(argument), parameterName, toType, c, "ParameterBinderStrings", "CannotConvertArgumentNoMessage", new object[] { exception4.Message }); throw exception5; } } return(result); }