예제 #1
0
        /// <summary>
        /// Compare the strong name signing state of the existing wrapper to the signing
        /// state we are requesting in this run of the task. Return true if they match (e.g.
        /// from a signing perspective, the wrapper is up-to-date) or false otherwise.
        /// </summary>
        private bool SigningRequirementsMatchExistingWrapper(ComReferenceWrapperInfo wrapperInfo)
        {
            StrongNameLevel desiredStrongNameLevel = StrongNameLevel.None;

            if (!string.IsNullOrEmpty(KeyFile) || !string.IsNullOrEmpty(KeyContainer))
            {
                desiredStrongNameLevel = DelaySign ? StrongNameLevel.DelaySigned : StrongNameLevel.FullySigned;
            }

            // ...and see what we have already
            StrongNameLevel currentStrongNameLevel = StrongNameUtils.GetAssemblyStrongNameLevel(wrapperInfo.path);

            // if not matching, need to regenerate wrapper
            if (desiredStrongNameLevel != currentStrongNameLevel)
            {
                return(false);
            }

            // if the wrapper needs a strong name, see if the public keys match
            if (desiredStrongNameLevel == StrongNameLevel.DelaySigned ||
                desiredStrongNameLevel == StrongNameLevel.FullySigned)
            {
                // get desired public key
                StrongNameUtils.GetStrongNameKey(Log, KeyFile, KeyContainer, out _, out byte[] desiredPublicKey);

                // get current public key
                AssemblyName assemblyName = AssemblyName.GetAssemblyName(wrapperInfo.path);

                if (assemblyName == null)
                {
                    return(false);
                }

                byte[] currentPublicKey = assemblyName.GetPublicKey();

                if (currentPublicKey.Length != desiredPublicKey.Length)
                {
                    return(false);
                }

                // compare public keys byte by byte
                for (int i = 0; i < currentPublicKey.Length; i++)
                {
                    if (currentPublicKey[i] != desiredPublicKey[i])
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
예제 #2
0
        protected virtual bool IsWrapperUpToDate(ComReferenceWrapperInfo wrapperInfo, DateTime componentTimestamp)
        {
            if ((this.ReferenceInfo.typeLibPath == null) || (this.ReferenceInfo.typeLibPath.Length == 0))
            {
                throw new ComReferenceResolutionException();
            }
            if (!File.Exists(wrapperInfo.path))
            {
                return(false);
            }
            if (DateTime.Compare(File.GetLastWriteTime(this.ReferenceInfo.typeLibPath), componentTimestamp) != 0)
            {
                return(false);
            }
            StrongNameLevel none = StrongNameLevel.None;

            if (((this.KeyFile != null) && (this.KeyFile.Length > 0)) || ((this.KeyContainer != null) && (this.KeyContainer.Length > 0)))
            {
                if (this.DelaySign)
                {
                    none = StrongNameLevel.DelaySigned;
                }
                else
                {
                    none = StrongNameLevel.FullySigned;
                }
            }
            StrongNameLevel assemblyStrongNameLevel = StrongNameUtils.GetAssemblyStrongNameLevel(wrapperInfo.path);

            if (none != assemblyStrongNameLevel)
            {
                return(false);
            }
            if ((none == StrongNameLevel.DelaySigned) || (none == StrongNameLevel.FullySigned))
            {
                StrongNameKeyPair pair;
                byte[]            publicKey = null;
                StrongNameUtils.GetStrongNameKey(base.Log, this.KeyFile, this.KeyContainer, out pair, out publicKey);
                AssemblyName assemblyName = AssemblyName.GetAssemblyName(wrapperInfo.path);
                if (assemblyName == null)
                {
                    return(false);
                }
                byte[] buffer2 = assemblyName.GetPublicKey();
                if (buffer2.Length != publicKey.Length)
                {
                    return(false);
                }
                for (int i = 0; i < buffer2.Length; i++)
                {
                    if (buffer2[i] != publicKey[i])
                    {
                        return(false);
                    }
                }
            }
            try
            {
                wrapperInfo.assembly = Assembly.UnsafeLoadFrom(wrapperInfo.path);
            }
            catch (BadImageFormatException)
            {
                wrapperInfo.assembly = null;
            }
            return(wrapperInfo.assembly != null);
        }