internal static AssemblyManifest FromDocument(string localPath, AssemblyManifest.ManifestType manifestType, Uri sourceUri)
        {
            AssemblyManifest manifest;

            CodeMarker_Singleton.Instance.CodeMarker(CodeMarkerEvent.perfParseBegin);
            Logger.AddMethodCall("ManifestReader.FromDocument(" + localPath + ") called.");
            FileInfo info = new FileInfo(localPath);

            if (info.Length > 0x1000000L)
            {
                throw new DeploymentException(Resources.GetString("Ex_ManifestFileTooLarge"));
            }
            FileStream input = new FileStream(localPath, FileMode.Open, FileAccess.Read);

            try
            {
                XmlReader reader = PolicyKeys.SkipSchemaValidation() ? XmlReader.Create(input) : ManifestValidatingReader.Create(input);
                while (reader.Read())
                {
                }
                Logger.AddInternalState("Schema validation passed.");
                manifest = new AssemblyManifest(input);
                Logger.AddInternalState("Manifest is parsed successfully.");
                if (!PolicyKeys.SkipSemanticValidation())
                {
                    manifest.ValidateSemantics(manifestType);
                }
                Logger.AddInternalState("Semantic validation passed.");
                if (!PolicyKeys.SkipSignatureValidation())
                {
                    input.Position = 0L;
                    manifest.ValidateSignature(input);
                }
                Logger.AddInternalState("Signature validation passed.");
            }
            catch (XmlException exception)
            {
                string message = string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_ManifestFromDocument"), new object[] { (sourceUri != null) ? sourceUri.AbsoluteUri : Path.GetFileName(localPath) });
                throw new InvalidDeploymentException(ExceptionTypes.ManifestParse, message, exception);
            }
            catch (XmlSchemaValidationException exception2)
            {
                string str2 = string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_ManifestFromDocument"), new object[] { (sourceUri != null) ? sourceUri.AbsoluteUri : Path.GetFileName(localPath) });
                throw new InvalidDeploymentException(ExceptionTypes.ManifestParse, str2, exception2);
            }
            catch (InvalidDeploymentException exception3)
            {
                string str3 = string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_ManifestFromDocument"), new object[] { (sourceUri != null) ? sourceUri.AbsoluteUri : Path.GetFileName(localPath) });
                throw new InvalidDeploymentException(ExceptionTypes.ManifestParse, str3, exception3);
            }
            finally
            {
                if (input != null)
                {
                    input.Dispose();
                }
            }
            CodeMarker_Singleton.Instance.CodeMarker(CodeMarkerEvent.perfParseEnd);
            return(manifest);
        }
        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);
        }
Beispiel #3
0
        internal static AssemblyManifest FromDocument(string localPath, AssemblyManifest.ManifestType manifestType, Uri sourceUri)
        {
            CodeMarker_Singleton.Instance.CodeMarker(7302);
            Logger.AddMethodCall("ManifestReader.FromDocument(" + localPath + ") called.");
            if (new FileInfo(localPath).Length > 16777216L)
            {
                throw new DeploymentException(Resources.GetString("Ex_ManifestFileTooLarge"));
            }
            AssemblyManifest assemblyManifest;

            using (FileStream fileStream = new FileStream(localPath, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    XmlReader xmlReader = PolicyKeys.SkipSchemaValidation() ? XmlReader.Create((Stream)fileStream, new XmlReaderSettings()
                    {
                        DtdProcessing = DtdProcessing.Prohibit,
                        XmlResolver   = (XmlResolver)null
                    }) : ManifestValidatingReader.Create((Stream)fileStream);
                    do
                    {
                        ;
                    }while (xmlReader.Read());
                    Logger.AddInternalState("Schema validation passed.");
                    assemblyManifest = new AssemblyManifest(fileStream);
                    Logger.AddInternalState("Manifest is parsed successfully.");
                    if (!PolicyKeys.SkipSemanticValidation())
                    {
                        assemblyManifest.ValidateSemantics(manifestType);
                    }
                    Logger.AddInternalState("Semantic validation passed.");
                    if (!PolicyKeys.SkipSignatureValidation())
                    {
                        fileStream.Position = 0L;
                        assemblyManifest.ValidateSignature((Stream)fileStream);
                    }
                    Logger.AddInternalState("Signature validation passed.");
                }
                catch (XmlException ex)
                {
                    throw new InvalidDeploymentException(ExceptionTypes.ManifestParse, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_ManifestFromDocument"), new object[1]
                    {
                        (object)(sourceUri != (Uri)null ? sourceUri.AbsoluteUri : Path.GetFileName(localPath))
                    }), (Exception)ex);
                }
                catch (XmlSchemaValidationException ex)
                {
                    throw new InvalidDeploymentException(ExceptionTypes.ManifestParse, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_ManifestFromDocument"), new object[1]
                    {
                        (object)(sourceUri != (Uri)null ? sourceUri.AbsoluteUri : Path.GetFileName(localPath))
                    }), (Exception)ex);
                }
                catch (InvalidDeploymentException ex)
                {
                    throw new InvalidDeploymentException(ExceptionTypes.ManifestParse, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Resources.GetString("Ex_ManifestFromDocument"), new object[1]
                    {
                        (object)(sourceUri != (Uri)null ? sourceUri.AbsoluteUri : Path.GetFileName(localPath))
                    }), (Exception)ex);
                }
            }
            CodeMarker_Singleton.Instance.CodeMarker(7303);
            return(assemblyManifest);
        }
Beispiel #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);
        }