Esempio n. 1
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);
        }