public static Stream GetTransformedStream(string filePath, CMS_HASH_TRANSFORM transform) { Stream stream = (Stream)null; if (transform == CMS_HASH_TRANSFORM.CMS_HASH_TRANSFORM_MANIFESTINVARIANT) { PEStream peStream = (PEStream)null; try { peStream = new PEStream(filePath, true); peStream.ZeroOutOptionalHeaderCheckSum(); peStream.ZeroOutDefaultId1ManifestResource(); stream = (Stream)peStream; return(stream); } finally { if (peStream != stream && peStream != null) { peStream.Close(); } } } else { if (transform == CMS_HASH_TRANSFORM.CMS_HASH_TRANSFORM_IDENTITY) { return((Stream) new FileStream(filePath, FileMode.Open, FileAccess.Read)); } throw new NotSupportedException(string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_TransformAlgorithmNotSupported"), new object[1] { (object)transform.ToString() })); } }
public static Stream GetTransformedStream(string filePath, System.Deployment.Internal.Isolation.Manifest.CMS_HASH_TRANSFORM transform) { Stream stream = null; if (transform == System.Deployment.Internal.Isolation.Manifest.CMS_HASH_TRANSFORM.CMS_HASH_TRANSFORM_MANIFESTINVARIANT) { PEStream stream2 = null; try { stream2 = new PEStream(filePath, true); stream2.ZeroOutOptionalHeaderCheckSum(); stream2.ZeroOutDefaultId1ManifestResource(); return(stream2); } finally { if ((stream2 != stream) && (stream2 != null)) { stream2.Close(); } } } if (transform != System.Deployment.Internal.Isolation.Manifest.CMS_HASH_TRANSFORM.CMS_HASH_TRANSFORM_IDENTITY) { throw new NotSupportedException(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_TransformAlgorithmNotSupported"), new object[] { transform.ToString() })); } return(new FileStream(filePath, FileMode.Open, FileAccess.Read)); }
public static void VerifyStrongNameAssembly(string filePath, AssemblyManifest assemblyManifest) { string fileName = Path.GetFileName(filePath); if (assemblyManifest.Identity.PublicKeyToken == null) { throw new InvalidDeploymentException(ExceptionTypes.Validation, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_StrongNameAsmWithNoPKT"), new object[] { fileName })); } bool ignoreSelfReferentialFileHash = false; if (assemblyManifest.ManifestSourceFormat == ManifestSourceFormat.XmlFile) { assemblyManifest.ValidateSignature(null); } else if (assemblyManifest.ManifestSourceFormat == ManifestSourceFormat.ID_1) { bool flag2; if (assemblyManifest.ComplibIdentity == null) { byte[] buffer = null; PEStream stream = null; MemoryStream s = null; try { stream = new PEStream(filePath, true); buffer = stream.GetDefaultId1ManifestResource(); if (buffer != null) { s = new MemoryStream(buffer); } if (s == null) { throw new InvalidDeploymentException(ExceptionTypes.StronglyNamedAssemblyVerification, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_StronglyNamedAssemblyNotVerifiable"), new object[] { fileName })); } assemblyManifest.ValidateSignature(s); goto Label_01C3; } finally { if (stream != null) { stream.Close(); } if (s != null) { s.Close(); } } } if (!assemblyManifest.ComplibIdentity.Equals(assemblyManifest.Identity)) { throw new InvalidDeploymentException(ExceptionTypes.IdentityMatchValidationForMixedModeAssembly, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_IdentitiesDoNotMatchForMixedModeAssembly"), new object[] { fileName })); } if (!Microsoft.Runtime.Hosting.StrongNameHelpers.StrongNameSignatureVerificationEx(filePath, false, out flag2)) { throw new InvalidDeploymentException(ExceptionTypes.SignatureValidation, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_StrongNameSignatureInvalid"), new object[] { fileName })); } ignoreSelfReferentialFileHash = true; } else { bool flag3; if (assemblyManifest.ManifestSourceFormat != ManifestSourceFormat.CompLib) { throw new InvalidDeploymentException(ExceptionTypes.StronglyNamedAssemblyVerification, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_StronglyNamedAssemblyNotVerifiable"), new object[] { fileName })); } if (!Microsoft.Runtime.Hosting.StrongNameHelpers.StrongNameSignatureVerificationEx(filePath, false, out flag3)) { throw new InvalidDeploymentException(ExceptionTypes.SignatureValidation, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_StrongNameSignatureInvalid"), new object[] { fileName })); } ignoreSelfReferentialFileHash = true; } Label_01C3: VerifyManifestComponentFiles(assemblyManifest, filePath, ignoreSelfReferentialFileHash); }
public static void VerifyStrongNameAssembly(string filePath, AssemblyManifest assemblyManifest) { string fileName = Path.GetFileName(filePath); if (assemblyManifest.Identity.PublicKeyToken == null) { throw new InvalidDeploymentException(ExceptionTypes.Validation, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_StrongNameAsmWithNoPKT"), new object[1] { (object)fileName })); } bool ignoreSelfReferentialFileHash = false; if (assemblyManifest.ManifestSourceFormat == ManifestSourceFormat.XmlFile) { assemblyManifest.ValidateSignature((Stream)null); } else if (assemblyManifest.ManifestSourceFormat == ManifestSourceFormat.ID_1) { if (assemblyManifest.ComplibIdentity == null) { PEStream peStream = (PEStream)null; MemoryStream memoryStream = (MemoryStream)null; try { peStream = new PEStream(filePath, true); byte[] manifestResource = peStream.GetDefaultId1ManifestResource(); if (manifestResource != null) { memoryStream = new MemoryStream(manifestResource); } if (memoryStream != null) { assemblyManifest.ValidateSignature((Stream)memoryStream); } else { throw new InvalidDeploymentException(ExceptionTypes.StronglyNamedAssemblyVerification, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_StronglyNamedAssemblyNotVerifiable"), new object[1] { (object)fileName })); } } finally { if (peStream != null) { peStream.Close(); } if (memoryStream != null) { memoryStream.Close(); } } } else { if (!assemblyManifest.ComplibIdentity.Equals((object)assemblyManifest.Identity)) { throw new InvalidDeploymentException(ExceptionTypes.IdentityMatchValidationForMixedModeAssembly, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_IdentitiesDoNotMatchForMixedModeAssembly"), new object[1] { (object)fileName })); } bool pfWasVerified; if (!StrongNameHelpers.StrongNameSignatureVerificationEx(filePath, false, out pfWasVerified)) { throw new InvalidDeploymentException(ExceptionTypes.SignatureValidation, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_StrongNameSignatureInvalid"), new object[1] { (object)fileName })); } ignoreSelfReferentialFileHash = true; } } else if (assemblyManifest.ManifestSourceFormat == ManifestSourceFormat.CompLib) { bool pfWasVerified; if (!StrongNameHelpers.StrongNameSignatureVerificationEx(filePath, false, out pfWasVerified)) { throw new InvalidDeploymentException(ExceptionTypes.SignatureValidation, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_StrongNameSignatureInvalid"), new object[1] { (object)fileName })); } ignoreSelfReferentialFileHash = true; } else { throw new InvalidDeploymentException(ExceptionTypes.StronglyNamedAssemblyVerification, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_StronglyNamedAssemblyNotVerifiable"), new object[1] { (object)fileName })); } ComponentVerifier.VerifyManifestComponentFiles(assemblyManifest, filePath, ignoreSelfReferentialFileHash); }
private void LoadFromInternalManifestFile(string filePath) { byte[] buffer = null; PEStream stream = null; MemoryStream stream2 = null; AssemblyManifest manifest = null; bool isImageFileDll = true; try { stream = new PEStream(filePath, true); buffer = stream.GetDefaultId1ManifestResource(); if (buffer != null) { stream2 = new MemoryStream(buffer); manifest = new AssemblyManifest(stream2); Logger.AddInternalState("id1Manifest is parsed successfully."); this._id1ManifestPresent = true; } isImageFileDll = stream.IsImageFileDll; } catch (IOException exception) { ManifestLoadExceptionHelper(exception, filePath); } catch (Win32Exception exception2) { ManifestLoadExceptionHelper(exception2, filePath); } catch (InvalidDeploymentException exception3) { ManifestLoadExceptionHelper(exception3, filePath); } finally { if (stream != null) { stream.Close(); } if (stream2 != null) { stream2.Close(); } } if (manifest != null) { if (!manifest.Identity.IsEmpty) { if (!this.LoadFromPEResources(filePath)) { ManifestLoadExceptionHelper(new DeploymentException(ExceptionTypes.InvalidManifest, Resources.GetString("Ex_CannotLoadInternalManifest")), filePath); } this._complibIdentity = ExtractIdentityFromCompLibAssembly(filePath); Logger.AddInternalState("_complibIdentity =" + ((this._complibIdentity == null) ? "null" : this._complibIdentity.ToString())); } else if (!isImageFileDll) { if (!this.LoadFromCompLibAssembly(filePath)) { ManifestLoadExceptionHelper(new DeploymentException(ExceptionTypes.InvalidManifest, Resources.GetString("Ex_CannotLoadInternalManifest")), filePath); } this._id1Identity = manifest.Identity; this._id1RequestedExecutionLevel = manifest.RequestedExecutionLevel; } else { ManifestLoadExceptionHelper(new DeploymentException(ExceptionTypes.InvalidManifest, Resources.GetString("Ex_EmptyIdentityInternalManifest")), filePath); } } else if (!this.LoadFromCompLibAssembly(filePath)) { ManifestLoadExceptionHelper(new DeploymentException(ExceptionTypes.InvalidManifest, Resources.GetString("Ex_CannotLoadInternalManifest")), filePath); } }
public static Stream GetTransformedStream(string filePath, System.Deployment.Internal.Isolation.Manifest.CMS_HASH_TRANSFORM transform) { Stream stream = null; if (transform == System.Deployment.Internal.Isolation.Manifest.CMS_HASH_TRANSFORM.CMS_HASH_TRANSFORM_MANIFESTINVARIANT) { PEStream stream2 = null; try { stream2 = new PEStream(filePath, true); stream2.ZeroOutOptionalHeaderCheckSum(); stream2.ZeroOutDefaultId1ManifestResource(); return stream2; } finally { if ((stream2 != stream) && (stream2 != null)) { stream2.Close(); } } } if (transform != System.Deployment.Internal.Isolation.Manifest.CMS_HASH_TRANSFORM.CMS_HASH_TRANSFORM_IDENTITY) { throw new NotSupportedException(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_TransformAlgorithmNotSupported"), new object[] { transform.ToString() })); } return new FileStream(filePath, FileMode.Open, FileAccess.Read); }