Esempio n. 1
0
        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);
        }
Esempio n. 4
0
        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);
        }
 public virtual void AddComponentsToStream(PEStream.StreamComponentList stream)
 {
     stream.Add(this);
 }
 public override void AddComponentsToStream(PEStream.StreamComponentList stream)
 {
     stream.Add(this);
     stream.Add(this._resourceRawData);
 }
 public Section(FileStream file, PEStream.SectionHeader sectionHeader)
 {
     base._address = sectionHeader.PointerToRawData;
     base._size = sectionHeader.SizeOfRawData;
     base._data = new PEStream.DiskDataBlock(file, base._address, base._size);
     this._sectionHeader = sectionHeader;
 }
 public int Add(PEStream.PEComponent peComponent)
 {
     if (peComponent.Size > 0L)
     {
         return this.Add(peComponent);
     }
     return -1;
 }
 public ResourceSection(FileStream file, PEStream.SectionHeader sectionHeader, bool partialConstruct) : base(file, sectionHeader)
 {
     this._resourceDirectoryStrings = new ArrayList();
     this._resourceDirectory = new PEStream.ResourceDirectory(this, file, (long) sectionHeader.PointerToRawData, (long) sectionHeader.PointerToRawData, (long) (sectionHeader.VirtualAddress - sectionHeader.PointerToRawData), partialConstruct);
     base._address = 0L;
     base._size = 0L;
     base._data = null;
 }
 public override void AddComponentsToStream(PEStream.StreamComponentList stream)
 {
     this._resourceDirectory.AddComponentsToStream(stream);
     foreach (PEStream.ResourceDirectoryString str in this._resourceDirectoryStrings)
     {
         str.AddComponentsToStream(stream);
     }
 }
 public override void AddComponentsToStream(PEStream.StreamComponentList stream)
 {
     stream.Add(this);
     foreach (PEStream.ResourceDirectoryEntry entry in this._resourceDirectoryEntries)
     {
         entry.AddComponentsToStream(stream);
     }
     foreach (PEStream.ResourceComponent component in this._resourceDirectoryItems.Values)
     {
         component.AddComponentsToStream(stream);
     }
 }
 public ResourceDirectory(PEStream.ResourceSection resourceSection, FileStream file, long rootResourceAddress, long resourceAddress, long addressDelta, bool partialConstruct)
 {
     this._imageResourceDirectory = (PEStream.IMAGE_RESOURCE_DIRECTORY) PEStream.PEComponent.ReadData(file, resourceAddress, this._imageResourceDirectory.GetType());
     base._address = resourceAddress;
     base._size = base.CalculateSize(this._imageResourceDirectory);
     base._data = this._imageResourceDirectory;
     long address = base._address + base._size;
     int num2 = 0;
     for (num2 = 0; num2 < this._imageResourceDirectory.NumberOfIdEntries; num2++)
     {
         PEStream.ResourceDirectoryEntry entry = new PEStream.ResourceDirectoryEntry(file, address);
         this._resourceDirectoryEntries.Add(entry);
         address += entry.Size;
     }
     for (num2 = 0; num2 < this._imageResourceDirectory.NumberOfNamedEntries; num2++)
     {
         PEStream.ResourceDirectoryEntry entry2 = new PEStream.ResourceDirectoryEntry(file, address);
         this._resourceDirectoryEntries.Add(entry2);
         address += entry2.Size;
     }
     foreach (PEStream.ResourceDirectoryEntry entry3 in this._resourceDirectoryEntries)
     {
         bool flag = false;
         object key = null;
         if (entry3.NameIsString)
         {
             key = resourceSection.CreateResourceDirectoryString(file, rootResourceAddress + entry3.NameOffset).NameString;
         }
         else
         {
             key = entry3.Id;
             if ((rootResourceAddress == resourceAddress) && (entry3.Id == 0x18))
             {
                 flag = true;
             }
         }
         entry3.Key = key;
         object obj3 = null;
         if (entry3.IsDirectory)
         {
             if (!partialConstruct || (partialConstruct && flag))
             {
                 obj3 = new PEStream.ResourceDirectory(resourceSection, file, rootResourceAddress, rootResourceAddress + entry3.OffsetToData, addressDelta, false);
             }
         }
         else
         {
             obj3 = new PEStream.ResourceData(file, rootResourceAddress, rootResourceAddress + entry3.OffsetToData, addressDelta);
         }
         if (obj3 != null)
         {
             this._resourceDirectoryItems.Add(key, obj3);
         }
     }
 }
 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);
 }
 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);
 }