Exemplo n.º 1
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <remarks>If <paramref name="hashAlgo"/> is an unsupported hash algorithm, then
		/// <see cref="AssemblyHashAlgorithm.SHA1"/> will be used as the hash algorithm.</remarks>
		/// <param name="hashAlgo">The algorithm to use</param>
		public AssemblyHash(AssemblyHashAlgorithm hashAlgo) {
			switch (hashAlgo) {
			case AssemblyHashAlgorithm.MD5:
				hasher = MD5.Create();
				break;

			case AssemblyHashAlgorithm.None:
			case AssemblyHashAlgorithm.MD2:
			case AssemblyHashAlgorithm.MD4:
			case AssemblyHashAlgorithm.SHA1:
			case AssemblyHashAlgorithm.MAC:
			case AssemblyHashAlgorithm.SSL3_SHAMD5:
			case AssemblyHashAlgorithm.HMAC:
			case AssemblyHashAlgorithm.TLS1PRF:
			case AssemblyHashAlgorithm.HASH_REPLACE_OWF:
			default:
				hasher = SHA1.Create();
				break;

			case AssemblyHashAlgorithm.SHA_256:
				hasher = SHA256.Create();
				break;

			case AssemblyHashAlgorithm.SHA_384:
				hasher = SHA384.Create();
				break;

			case AssemblyHashAlgorithm.SHA_512:
				hasher = SHA512.Create();
				break;
			}
		}
Exemplo n.º 2
0
        internal ImmutableArray<byte> GetHash(AssemblyHashAlgorithm algorithmId)
        {
            using (HashAlgorithm algorithm = TryGetAlgorithm(algorithmId))
            {
                // ERR_CryptoHashFailed has already been reported:
                if (algorithm == null)
                {
                    return ImmutableArray.Create<byte>();
                }

                switch (algorithmId)
                {
                    case AssemblyHashAlgorithm.None:
                    case AssemblyHashAlgorithm.Sha1:
                        return GetHash(ref _lazySHA1Hash, algorithm);

                    case AssemblyHashAlgorithm.Sha256:
                        return GetHash(ref _lazySHA256Hash, algorithm);

                    case AssemblyHashAlgorithm.Sha384:
                        return GetHash(ref _lazySHA384Hash, algorithm);

                    case AssemblyHashAlgorithm.Sha512:
                        return GetHash(ref _lazySHA512Hash, algorithm);

                    case AssemblyHashAlgorithm.MD5:
                        return GetHash(ref _lazyMD5Hash, algorithm);

                    default:
                        throw ExceptionUtilities.UnexpectedValue(algorithmId);
                }
            }
        }
Exemplo n.º 3
0
		public AssemblyHash (AssemblyHashAlgorithm algorithm, byte[] value)
		{
			_algorithm = algorithm;
			if (value != null)
				_value = (byte[]) value.Clone ();
			else
				_value = null;
		}
Exemplo n.º 4
0
 public void Read(ClrModuleReader reader)
 {
     this.HashAlgId = (AssemblyHashAlgorithm)reader.Binary.ReadUInt32();
     this.Version = reader.ReadVersion();
     this.Flags = (AssemblyFlags)reader.Binary.ReadUInt32();
     this.PublicKey = reader.ReadBlob();
     this.Name = reader.ReadString();
     this.Culture = reader.ReadString();
 }
Exemplo n.º 5
0
		/// <summary>
		/// Hash data
		/// </summary>
		/// <remarks>If <paramref name="hashAlgo"/> is an unsupported hash algorithm, then
		/// <see cref="AssemblyHashAlgorithm.SHA1"/> will be used as the hash algorithm.</remarks>
		/// <param name="data">The data</param>
		/// <param name="hashAlgo">The algorithm to use</param>
		/// <returns>Hashed data or null if <paramref name="data"/> was <c>null</c></returns>
		public static byte[] Hash(byte[] data, AssemblyHashAlgorithm hashAlgo) {
			if (data == null)
				return null;

			using (var asmHash = new AssemblyHash(hashAlgo)) {
				asmHash.Hash(data);
				return asmHash.ComputeHash();
			}
		}
Exemplo n.º 6
0
     public AssemblyHash(AssemblyHashAlgorithm algorithm, byte[] value) {
         _Algorithm = algorithm;
         _Value = null;
 
         if (value != null) {
             int length = value.Length;
             _Value = new byte[length];
             Array.Copy(value, _Value, length);
         }
     }
        public AssemblyNameReference(string name, Version version)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            this.name = name;
            this.version = version;
            this.hash_algorithm = AssemblyHashAlgorithm.None;
            this.token = new MetadataToken(TokenType.AssemblyRef);
        }
 public AssemblyHash(byte[] value)
 {
     this._Algorithm = AssemblyHashAlgorithm.SHA1;
     this._Value = null;
     if (value != null)
     {
         int length = value.Length;
         this._Value = new byte[length];
         Array.Copy(value, this._Value, length);
     }
 }
Exemplo n.º 9
0
 public AssemblyNameReference(string name, string culture, Version version)
 {
     if (name == null)
         throw new ArgumentNullException ("name");
     if (culture == null)
         throw new ArgumentNullException ("culture");
     m_name = name;
     m_culture = culture;
     m_version = version;
     m_hashAlgo = AssemblyHashAlgorithm.None;
 }
Exemplo n.º 10
0
 public static IncrementalHash Create(AssemblyHashAlgorithm hashAlgorithm)
 {
     if (PortableShim.IncrementalHash.TypeOpt != null)
     {
         return new Core(hashAlgorithm);
     }
     else
     {
         return new Desktop(hashAlgorithm);
     }
 }
Exemplo n.º 11
0
		public AssemblyOptions(AssemblyDef asm) {
			HashAlgorithm = asm.HashAlgorithm;
			Version = asm.Version;
			Attributes = asm.Attributes;
			PublicKey = asm.PublicKey;
			Name = asm.Name;
			Culture = asm.Culture;
			ClrVersion = Module.ClrVersion.DefaultVersion;
			CustomAttributes.AddRange(asm.CustomAttributes);
			DeclSecurities.AddRange(asm.DeclSecurities);
		}
Exemplo n.º 12
0
		private void LoadData(CLIFile pFile)
		{
			HashAlgId = (AssemblyHashAlgorithm)pFile.ReadUInt32();
			MajorVersion = pFile.ReadUInt16();
			MinorVersion = pFile.ReadUInt16();
			BuildNumber = pFile.ReadUInt16();
			RevisionNumber = pFile.ReadUInt16();
			Flags = (AssemblyFlags)pFile.ReadUInt32();
			PublicKey = pFile.ReadBlobHeap(pFile.ReadHeapIndex(HeapOffsetSizes.Blob32Bit));
			Name = pFile.ReadStringHeap(pFile.ReadHeapIndex(HeapOffsetSizes.Strings32Bit));
			Culture = pFile.ReadStringHeap(pFile.ReadHeapIndex(HeapOffsetSizes.Strings32Bit));
		}
Exemplo n.º 13
0
            /// <summary>
            /// Returns the actual FX implementation of HashAlgorithmName for given hash algorithm id.
            /// </summary>
            private static object GetHashAlgorithmNameObj(AssemblyHashAlgorithm algorithmId)
            {
                switch (algorithmId)
                {
                    case AssemblyHashAlgorithm.Sha1:
                        return PortableShim.HashAlgorithmName.SHA1;

                    default:
                        // More algorithms can be added as needed.
                        throw ExceptionUtilities.UnexpectedValue(algorithmId);
                }
            }
Exemplo n.º 14
0
 public AssemblyReference(string name, AssemblyAttributes attributes, Version version, AssemblyHashAlgorithm hashAlgorithm, uint publicKey, string culture)
     : base(new MetaDataRow(
         (byte)version.Major, (byte)version.Minor, (byte)version.Build, (byte)version.Revision,
         (uint)attributes,
         publicKey,
         0U,
         0U,
         (uint)hashAlgorithm))
 {
     this._name = name;
     this._culture = culture;
 }
Exemplo n.º 15
0
	public AssemblyHash(AssemblyHashAlgorithm algorithmId, byte[] value)
			{
				hashAlg = algorithmId;
				if(value != null)
				{
					hash = new byte [value.Length];
					Array.Copy(value, hash, value.Length);
				}
				else
				{
					hash = null;
				}
			}
Exemplo n.º 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AssemblyRow"/> struct.
        /// </summary>
        /// <param name="hashAlgId">The hash alg id.</param>
        /// <param name="majorVersion">The major version.</param>
        /// <param name="minorVersion">The minor version.</param>
        /// <param name="buildNumber">The build number.</param>
        /// <param name="revision">The revision.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="publicKey">The public key.</param>
        /// <param name="name">The name.</param>
        /// <param name="culture">The culture.</param>
        public AssemblyRow(AssemblyHashAlgorithm hashAlgId,
							ushort majorVersion, ushort minorVersion, ushort buildNumber, ushort revision,
							AssemblyAttributes flags, HeapIndexToken publicKey, HeapIndexToken name, HeapIndexToken culture)
        {
            this.hashAlgId = hashAlgId;
            this.majorVersion = majorVersion;
            this.minorVersion = minorVersion;
            this.buildNumber = buildNumber;
            this.revisionNumber = revision;
            this.flags = flags;
            this.publicKey = publicKey;
            this.name = name;
            this.culture = culture;
        }
Exemplo n.º 17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AssemblyRow"/> struct.
        /// </summary>
        /// <param name="hashAlgId">The hash alg id.</param>
        /// <param name="majorVersion">The major version.</param>
        /// <param name="minorVersion">The minor version.</param>
        /// <param name="buildNumber">The build number.</param>
        /// <param name="revision">The revision.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="publicKey">The public key.</param>
        /// <param name="name">The name.</param>
        /// <param name="culture">The culture.</param>
        public AssemblyRow(AssemblyHashAlgorithm hashAlgId,
							ushort majorVersion, ushort minorVersion, ushort buildNumber, ushort revision,
							AssemblyAttributes flags, HeapIndexToken publicKey, HeapIndexToken name, HeapIndexToken culture)
        {
            HashAlgId = hashAlgId;
            MajorVersion = majorVersion;
            MinorVersion = minorVersion;
            BuildNumber = buildNumber;
            Revision = revision;
            Flags = flags;
            PublicKey = publicKey;
            Name = name;
            Culture = culture;
        }
Exemplo n.º 18
0
	private AssemblyName(AssemblyName other)
			{
				codeBase = other.codeBase;
				culture = other.culture;
				flags = other.flags;
				name = other.name;
				hashAlg = other.hashAlg;
				keyPair = other.keyPair;
				version = (version == null
					? null : (Version)(other.version.Clone()));
				versionCompat = other.versionCompat;
				publicKey = (other.publicKey == null
					? null : (byte[])(other.publicKey.Clone()));
				publicKeyToken = (other.publicKeyToken == null
					? null : (byte[])(other.publicKeyToken.Clone()));
			}
Exemplo n.º 19
0
        internal static bool IsSupportedAlgorithm(AssemblyHashAlgorithm algorithmId)
        {
            switch (algorithmId)
            {
                case AssemblyHashAlgorithm.None:
                case AssemblyHashAlgorithm.Sha1:
                case AssemblyHashAlgorithm.Sha256:
                case AssemblyHashAlgorithm.Sha384:
                case AssemblyHashAlgorithm.Sha512:
                case AssemblyHashAlgorithm.MD5:
                    return true;

                default:
                    return false;
            }
        }
Exemplo n.º 20
0
		internal AssemblyBuilder(Universe universe, AssemblyName name, string dir, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions)
			: base(universe)
		{
			this.name = name.Name;
			SetVersionHelper(name.Version);
			if (!string.IsNullOrEmpty(name.Culture))
			{
				this.culture = name.Culture;
			}
			this.flags = name.RawFlags;
			this.hashAlgorithm = name.HashAlgorithm;
			if (this.hashAlgorithm == AssemblyHashAlgorithm.None)
			{
				this.hashAlgorithm = AssemblyHashAlgorithm.SHA1;
			}
			this.keyPair = name.KeyPair;
			if (this.keyPair != null)
			{
				this.publicKey = this.keyPair.PublicKey;
			}
			else
			{
				byte[] publicKey = name.GetPublicKey();
				if (publicKey != null && publicKey.Length != 0)
				{
					this.publicKey = (byte[])publicKey.Clone();
				}
			}
			this.dir = dir ?? ".";
			this.requiredPermissions = requiredPermissions;
			this.optionalPermissions = optionalPermissions;
			this.refusedPermissions = refusedPermissions;
			if (universe.HasMscorlib && !universe.Mscorlib.__IsMissing && universe.Mscorlib.ImageRuntimeVersion != null)
			{
				this.imageRuntimeVersion = universe.Mscorlib.ImageRuntimeVersion;
			}
			else
			{
				this.imageRuntimeVersion = typeof(object).Assembly.ImageRuntimeVersion;
			}
		}
Exemplo n.º 21
0
        public static byte[] GetPublicKeyToken(byte[] publicKey, AssemblyHashAlgorithm hashAlgo)
        {
            byte[] token = null;

            if (publicKey != null && publicKey.Length > 0)
            {
                HashAlgorithm ha;
                switch (hashAlgo)
                {
                    case AssemblyHashAlgorithm.Reserved:
                        ha = MD5.Create(); break;
                    default:
                        ha = SHA1.Create(); break;
                }
                byte[] hash = ha.ComputeHash(publicKey);
                // we need the last 8 bytes in reverse order
                token = new byte[8];
                Array.Copy(hash, (hash.Length - 8), token, 0, 8);
                Array.Reverse(token, 0, 8);
            }
            return token;
        }
Exemplo n.º 22
0
        private static HashAlgorithm TryGetAlgorithm(AssemblyHashAlgorithm algorithmId)
        {
            switch (algorithmId)
            {
                case AssemblyHashAlgorithm.None:
                case AssemblyHashAlgorithm.Sha1:
                    return new SHA1CryptoServiceProvider();

                case AssemblyHashAlgorithm.Sha256:
                    return new SHA256CryptoServiceProvider();

                case AssemblyHashAlgorithm.Sha384:
                    return new SHA384CryptoServiceProvider();

                case AssemblyHashAlgorithm.Sha512:
                    return new SHA512CryptoServiceProvider();

                case AssemblyHashAlgorithm.MD5:
                    return new MD5CryptoServiceProvider();

                default:
                    return null;
            }
        }
Exemplo n.º 23
0
 public IAssembly LoadFrom(string assemblyFile, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
 {
     return new AssemblyWrap(Assembly.LoadFrom(assemblyFile,hashValue,hashAlgorithm));
 }
Exemplo n.º 24
0
        /// <summary>
        ///     Strong name hashes the .NET file
        /// </summary>
        /// <param name="hashAlg">Hash algorithm</param>
        /// <param name="snSigOffset">Strong name sig offset (relative to start of .NET PE file)</param>
        /// <param name="snSigSize">Size of strong name signature</param>
        /// <returns>The strong name hash of the .NET file</returns>
        private byte[] StrongNameHashData(AssemblyHashAlgorithm hashAlg, long snSigOffset, uint snSigSize)
        {
            var reader = new BinaryReader(stream);

            snSigOffset += baseOffset;
            var snSigOffsetEnd = snSigOffset + snSigSize;

            using (var hasher = new AssemblyHash(hashAlg))
            {
                var buffer = new byte[0x8000];

                // Hash the DOS header. It's defined to be all data from the start of
                // the file up to the NT headers.
                stream.Position = baseOffset + 0x3C;
                var ntHeadersOffs = reader.ReadUInt32();
                stream.Position = baseOffset;
                hasher.Hash(stream, ntHeadersOffs, buffer);

                // Hash NT headers, but hash authenticode + checksum as 0s
                stream.Position += 6;
                int numSections = reader.ReadUInt16();
                stream.Position -= 8;
                hasher.Hash(stream, 0x18, buffer); // magic + FileHeader

                var is32bit = reader.ReadUInt16() == 0x010B;
                stream.Position -= 2;
                var optHeaderSize = is32bit ? 0x60 : 0x70;
                if (stream.Read(buffer, 0, optHeaderSize) != optHeaderSize)
                {
                    throw new IOException("Could not read data");
                }
                // Clear checksum
                for (var i = 0; i < 4; i++)
                {
                    buffer[0x40 + i] = 0;
                }
                hasher.Hash(buffer, 0, optHeaderSize);

                const int imageDirsSize = 16 * 8;
                if (stream.Read(buffer, 0, imageDirsSize) != imageDirsSize)
                {
                    throw new IOException("Could not read data");
                }
                // Clear authenticode data dir
                for (var i = 0; i < 8; i++)
                {
                    buffer[4 * 8 + i] = 0;
                }
                hasher.Hash(buffer, 0, imageDirsSize);

                // Hash section headers
                var sectHeadersOffs = stream.Position;
                hasher.Hash(stream, (uint)numSections * 0x28, buffer);

                // Hash all raw section data but make sure we don't hash the location
                // where the strong name signature will be stored.
                for (var i = 0; i < numSections; i++)
                {
                    stream.Position = sectHeadersOffs + i * 0x28 + 0x10;
                    var sizeOfRawData    = reader.ReadUInt32();
                    var pointerToRawData = reader.ReadUInt32();

                    stream.Position = baseOffset + pointerToRawData;
                    while (sizeOfRawData > 0)
                    {
                        var pos = stream.Position;

                        if (snSigOffset <= pos && pos < snSigOffsetEnd)
                        {
                            var skipSize = (uint)(snSigOffsetEnd - pos);
                            if (skipSize >= sizeOfRawData)
                            {
                                break;
                            }
                            sizeOfRawData   -= skipSize;
                            stream.Position += skipSize;
                            continue;
                        }

                        if (pos >= snSigOffsetEnd)
                        {
                            hasher.Hash(stream, sizeOfRawData, buffer);
                            break;
                        }

                        var maxLen = (uint)Math.Min(snSigOffset - pos, sizeOfRawData);
                        hasher.Hash(stream, maxLen, buffer);
                        sizeOfRawData -= maxLen;
                    }
                }

                return(hasher.ComputeHash());
            }
        }
Exemplo n.º 25
0
 public void __SetAssemblyAlgorithmId(AssemblyHashAlgorithm hashAlgorithm)
 {
     this.hashAlgorithm = hashAlgorithm;
 }
Exemplo n.º 26
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <remarks>If <paramref name="hashAlgo"/> is an unsupported hash algorithm, then
 /// <see cref="AssemblyHashAlgorithm.SHA1"/> will be used as the hash algorithm.</remarks>
 /// <param name="hashAlgo">The algorithm to use</param>
 public AssemblyHash(AssemblyHashAlgorithm hashAlgo) =>
Exemplo n.º 27
0
        public int ExecuteAssembly(string assemblyFile, Evidence assemblySecurity, string[] args, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
        {
#if AVM
            throw new NotSupportedException();
#else
            Assembly a = Assembly.LoadFrom(assemblyFile, assemblySecurity, hashValue, hashAlgorithm);
            return(ExecuteAssemblyInternal(a, args));
#endif
        }
Exemplo n.º 28
0
 internal virtual ImmutableArray <byte> GetHash(AssemblyHashAlgorithm algorithmId)
 {
     throw ExceptionUtilities.Unreachable;
 }
Exemplo n.º 29
0
 /// <summary>
 /// Creates a new instance of <see cref="CILAssemblyName"/> with specified values.
 /// </summary>
 /// <param name="name">Assembly name.</param>
 /// <param name="major">Major version of the assembly name.</param>
 /// <param name="minor">Minor version of the assembly name.</param>
 /// <param name="build">Build number of the assembly name.</param>
 /// <param name="revision">Revision of the assembly name.</param>
 /// <param name="hashAlgorithm">The hash algorithm of the assembly name. For more information <see cref="AssemblyHashAlgorithm"/>.</param>
 /// <param name="flags">Assembly flags of the assembly name. For more information <see cref="AssemblyFlags"/>.</param>
 /// <param name="pKey">Public key of the assembly name. Specify <c>null</c> or empty array to create assembly name without public key.</param>
 /// <param name="culture">Culture string of the assembly name. Specify <c>null</c> or empty string to create culture-neutral assembly name.</param>
 /// <returns>A new <see cref="CILAssemblyName"/> with specified parameters.</returns>
 public CILAssemblyName(String name, Int32 major, Int32 minor, Int32 build, Int32 revision, AssemblyHashAlgorithm hashAlgorithm = AssemblyHashAlgorithm.None, AssemblyFlags flags = AssemblyFlags.None, Byte[] pKey = null, String culture = null)
 {
     this._hashAlgorithm = (Int32)hashAlgorithm;
     this._flags         = (Int32)flags;
     this._majorVersion  = major;
     this._minorVersion  = minor;
     this._buildNumber   = build;
     this._revision      = revision;
     this._publicKey     = pKey.IsNullOrEmpty() ? null : pKey;
     this._name          = name;
     this._culture       = culture;
 }
Exemplo n.º 30
0
 internal void SetHashControl(byte[] hash, AssemblyHashAlgorithm hashAlgorithm)
 {
     _HashForControl          = hash;
     _HashAlgorithmForControl = hashAlgorithm;
 }
Exemplo n.º 31
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="modulus">Modulus</param>
 /// <param name="publicExponent">Public exponent</param>
 /// <param name="hashAlgorithm">Hash algorithm</param>
 public StrongNamePublicKey(byte[] modulus, byte[] publicExponent, AssemblyHashAlgorithm hashAlgorithm)
     : this(modulus, publicExponent, hashAlgorithm, SignatureAlgorithm.CALG_RSA_SIGN)
 {
 }
Exemplo n.º 32
0
        public static Assembly LoadFrom(string assemblyFile, System.Security.Policy.Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
        {
            StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller;

            return(RuntimeAssembly.InternalLoadFrom(assemblyFile, securityEvidence, hashValue, hashAlgorithm, false, false, ref lookForMyCaller));
        }
Exemplo n.º 33
0
        public static Assembly LoadFrom(string assemblyFile, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
        {
            StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller;

            return(RuntimeAssembly.InternalLoadFrom(assemblyFile, null, hashValue, hashAlgorithm, false, false, ref lookForMyCaller));
        }
Exemplo n.º 34
0
 internal override ImmutableArray <byte> GetHash(AssemblyHashAlgorithm algorithmId)
 {
     return(module.GetHash(algorithmId));
 }
Exemplo n.º 35
0
        internal unsafe void FillName(MonoAssemblyName *native, string codeBase, bool addVersion, bool addPublickey, bool defaultToken, bool assemblyRef)
        {
            this.name = RuntimeMarshal.PtrToUtf8String(native->name);

            this.major    = native->major;
            this.minor    = native->minor;
            this.build    = native->build;
            this.revision = native->revision;

            this.flags = (AssemblyNameFlags)native->flags;

            this.hashalg = (AssemblyHashAlgorithm)native->hash_alg;

            this.versioncompat          = AssemblyVersionCompatibility.SameMachine;
            this.processor_architecture = (ProcessorArchitecture)native->arch;

            if (addVersion)
            {
                this.version = new Version(this.major, this.minor, this.build, this.revision);
            }

            this.codebase = codeBase;

#if NETCORE
            if (native->culture != IntPtr.Zero)
            {
                this.cultureinfo = CultureInfo.GetCultureInfo(RuntimeMarshal.PtrToUtf8String(native->culture));
            }
#else
            if (native->culture != IntPtr.Zero)
            {
                this.cultureinfo = CultureInfo.CreateCulture(RuntimeMarshal.PtrToUtf8String(native->culture), assemblyRef);
            }
#endif

            if (native->public_key != IntPtr.Zero)
            {
                this.publicKey = RuntimeMarshal.DecodeBlobArray(native->public_key);
                this.flags    |= AssemblyNameFlags.PublicKey;
            }
            else if (addPublickey)
            {
                this.publicKey = EmptyArray <byte> .Value;
                this.flags    |= AssemblyNameFlags.PublicKey;
            }

            // MonoAssemblyName keeps the public key token as an hexadecimal string
            if (native->public_key_token [0] != 0)
            {
                byte[] keyToken = new byte [8];
                for (int i = 0, j = 0; i < 8; ++i)
                {
                    keyToken [i]  = (byte)(RuntimeMarshal.AsciHexDigitValue(native->public_key_token [j++]) << 4);
                    keyToken [i] |= (byte)RuntimeMarshal.AsciHexDigitValue(native->public_key_token [j++]);
                }
                this.keyToken = keyToken;
            }
            else if (defaultToken)
            {
                this.keyToken = EmptyArray <byte> .Value;
            }
        }
Exemplo n.º 36
0
 public AssemblyAlgorithmIdAttribute(AssemblyHashAlgorithm algorithmId)
 {
     m_algId = (uint)algorithmId;
 }
Exemplo n.º 37
0
 public static Assembly LoadFrom(String assemblyFile, byte [] hashValue, AssemblyHashAlgorithm hashAlgorithm)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 38
0
 public AssemblyName()
 {
     _HashAlgorithm        = AssemblyHashAlgorithm.None;
     _VersionCompatibility = AssemblyVersionCompatibility.SameMachine;
     _Flags = AssemblyNameFlags.None;
 }
Exemplo n.º 39
0
 public static Assembly LoadFrom(string assemblyFile, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
 {
     throw new PlatformNotSupportedException();
 }
Exemplo n.º 40
0
 public static Assembly LoadFrom(string assemblyFile, byte[]?hashValue, AssemblyHashAlgorithm hashAlgorithm)
 {
     throw new NotSupportedException(SR.NotSupported_AssemblyLoadFromHash);
 }
Exemplo n.º 41
0
 public StrongNameDataHashBuilder(Stream imageStream, AssemblyHashAlgorithm hashAlgorithm)
 {
     _imageStream   = imageStream ?? throw new ArgumentNullException(nameof(imageStream));
     _hashAlgorithm = hashAlgorithm;
 }
Exemplo n.º 42
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="asmName">Assembly name info</param>
 /// <exception cref="ArgumentNullException">If <paramref name="asmName"/> is <c>null</c></exception>
 public AssemblyDefUser(AssemblyName asmName)
     : this(new AssemblyNameInfo(asmName))
 {
     this.hashAlgId = (AssemblyHashAlgorithm)asmName.HashAlgorithm;
     this.flags     = (AssemblyAttributes)asmName.Flags;
 }
Exemplo n.º 43
0
 public ObjectHandle CreateComInstanceFrom(string assemblyFile, string typeName,
                                           byte [] hashValue, AssemblyHashAlgorithm hashAlgorithm)
 {
     return(Activator.CreateComInstanceFrom(assemblyFile, typeName, hashValue, hashAlgorithm));
 }
Exemplo n.º 44
0
		public int ExecuteAssembly (string assemblyFile, string[] args, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
		{
			Assembly a = Assembly.LoadFrom (assemblyFile, null, hashValue, hashAlgorithm);
			return ExecuteAssemblyInternal (a, args);
		}
Exemplo n.º 45
0
 // FIXME: What are we missing?
 public static Assembly LoadFrom(String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
 {
     if (assemblyFile == null)
     {
         throw new ArgumentNullException("assemblyFile");
     }
     if (assemblyFile == String.Empty)
     {
         throw new ArgumentException("Name can't be the empty string", "assemblyFile");
     }
     throw new NotImplementedException();
 }
Exemplo n.º 46
0
 public int ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
 {
     Assembly assembly = Assembly.LoadFrom(assemblyFile, assemblySecurity, hashValue, hashAlgorithm);
     if (args == null)
     {
         args = new string[0];
     }
     return this.nExecuteAssembly(assembly, args);
 }
Exemplo n.º 47
0
        /// <summary>
        /// Workaround for unity mono being weird
        /// </summary>
        internal static AssemblyName GetAssemblyName(MetadataReader reader, StringHandle nameHandle, Version version, StringHandle cultureHandle, BlobHandle publicKeyOrTokenHandle, AssemblyHashAlgorithm assemblyHashAlgorithm, AssemblyFlags flags)
        {
            var name             = reader.GetString(nameHandle);
            var cultureName      = (!cultureHandle.IsNil) ? reader.GetString(cultureHandle) : null;
            var hashAlgorithm    = (System.Configuration.Assemblies.AssemblyHashAlgorithm)assemblyHashAlgorithm;
            var publicKeyOrToken = !publicKeyOrTokenHandle.IsNil ? reader.GetBlobBytes(publicKeyOrTokenHandle) : null;

            var assemblyName = new AssemblyName(name);

            try
            {
                assemblyName.Version = version;
            }
            catch (NotImplementedException)
            {
            }

            try
            {
                assemblyName.CultureName = cultureName;
            }
            catch (NotImplementedException)
            {
            }

            try
            {
                assemblyName.HashAlgorithm = hashAlgorithm;
            }
            catch (NotImplementedException)
            {
            }

            try
            {
                assemblyName.Flags = GetAssemblyNameFlags(flags);
            }
            catch (NotImplementedException)
            {
            }

            try
            {
                assemblyName.ContentType = GetContentTypeFromAssemblyFlags(flags);
            }
            catch (NotImplementedException)
            {
            }

            var hasPublicKey = (flags & AssemblyFlags.PublicKey) != 0;

            if (hasPublicKey)
            {
                assemblyName.SetPublicKey(publicKeyOrToken);
            }
            else
            {
                assemblyName.SetPublicKeyToken(publicKeyOrToken);
            }

            return(assemblyName);
        }
Exemplo n.º 48
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public static Assembly LoadFrom(String assemblyFile,
                                        Evidence securityEvidence,
                                        byte[] hashValue,
                                        AssemblyHashAlgorithm hashAlgorithm)
        {
            Contract.Ensures(Contract.Result<Assembly>() != null);

            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;

            return RuntimeAssembly.InternalLoadFrom(
                assemblyFile, 
                securityEvidence, 
                hashValue, 
                hashAlgorithm, 
                false,
                false,
                ref stackMark);
        }
Exemplo n.º 49
0
 ImmutableArray <byte> Cci.IFileReference.GetHashValue(AssemblyHashAlgorithm algorithmId)
 {
     return(_underlyingModule.GetHash(algorithmId));
 }
Exemplo n.º 50
0
 public AssemblyDefinition(string name, AssemblyAttributes attributes, Version version, AssemblyHashAlgorithm hashAlgorithm, uint publicKey, string culture)
     : base(new MetaDataRow(
                (uint)hashAlgorithm,
                (byte)version.Major,
                (byte)version.Minor,
                (byte)version.Build,
                (byte)version.Revision,
                (uint)attributes,
                publicKey,
                0U,
                0U))
 {
     this.name    = name;
     this.culture = culture;
 }
Exemplo n.º 51
0
		public ObjectHandle CreateComInstanceFrom (string assemblyFile, string typeName,
			byte [] hashValue, AssemblyHashAlgorithm hashAlgorithm)
		{
			return Activator.CreateComInstanceFrom (assemblyFile, typeName, hashValue ,hashAlgorithm);
		}
        public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
        {
            if (relocsOnly)
            {
                return(new ObjectData(Array.Empty <byte>(), null, 1, null));
            }

            if (!_emissionCompleted)
            {
                foreach (ISignatureEmitter emitter in _signatureEmitters)
                {
                    emitter.MaterializeSignature();
                }

                _emissionCompleted = true;
            }

            MetadataBuilder metadataBuilder = new MetadataBuilder();

            AssemblyHashAlgorithm hashAlgorithm         = AssemblyHashAlgorithm.None;
            BlobHandle            publicKeyBlob         = default(BlobHandle);
            AssemblyFlags         manifestAssemblyFlags = default(AssemblyFlags);
            Version manifestAssemblyVersion             = new Version(0, 0, 0, 0);

            if ((factory.CompositeImageSettings != null) && factory.CompilationModuleGroup.IsCompositeBuildMode)
            {
                if (factory.CompositeImageSettings.PublicKey != null)
                {
                    hashAlgorithm          = AssemblyHashAlgorithm.Sha1;
                    publicKeyBlob          = metadataBuilder.GetOrAddBlob(factory.CompositeImageSettings.PublicKey);
                    manifestAssemblyFlags |= AssemblyFlags.PublicKey;
                }

                if (factory.CompositeImageSettings.AssemblyVersion != null)
                {
                    manifestAssemblyVersion = factory.CompositeImageSettings.AssemblyVersion;
                }
            }

            string manifestMetadataAssemblyName = "ManifestMetadata";

            metadataBuilder.AddAssembly(
                metadataBuilder.GetOrAddString(manifestMetadataAssemblyName),
                manifestAssemblyVersion,
                culture: default(StringHandle),
                publicKey: publicKeyBlob,
                flags: manifestAssemblyFlags,
                hashAlgorithm: hashAlgorithm);

            metadataBuilder.AddModule(
                0,
                metadataBuilder.GetOrAddString(manifestMetadataAssemblyName),
                default(GuidHandle), default(GuidHandle), default(GuidHandle));

            // Module type
            metadataBuilder.AddTypeDefinition(
                default(TypeAttributes),
                default(StringHandle),
                metadataBuilder.GetOrAddString("<Module>"),
                baseType: default(EntityHandle),
                fieldList: MetadataTokens.FieldDefinitionHandle(1),
                methodList: MetadataTokens.MethodDefinitionHandle(1));

            foreach (var idAndAssemblyName in _moduleIdToAssemblyNameMap.OrderBy(x => x.Key))
            {
                AssemblyName  assemblyName  = idAndAssemblyName.Value;
                AssemblyFlags assemblyFlags = 0;
                byte[]        publicKeyOrToken;
                if ((assemblyName.Flags & AssemblyNameFlags.PublicKey) != 0)
                {
                    assemblyFlags   |= AssemblyFlags.PublicKey;
                    publicKeyOrToken = assemblyName.GetPublicKey();
                }
                else
                {
                    publicKeyOrToken = assemblyName.GetPublicKeyToken();
                }
                if ((assemblyName.Flags & AssemblyNameFlags.Retargetable) != 0)
                {
                    assemblyFlags |= AssemblyFlags.Retargetable;
                }

                AssemblyReferenceHandle newHandle = metadataBuilder.AddAssemblyReference(
                    name: metadataBuilder.GetOrAddString(assemblyName.Name),
                    version: assemblyName.Version,
                    culture: metadataBuilder.GetOrAddString(assemblyName.CultureName),
                    publicKeyOrToken: metadataBuilder.GetOrAddBlob(publicKeyOrToken),
                    flags: assemblyFlags,
                    hashValue: default(BlobHandle) /* TODO */);
            }

            MetadataRootBuilder metadataRootBuilder = new MetadataRootBuilder(metadataBuilder);
            BlobBuilder         metadataBlobBuilder = new BlobBuilder();

            metadataRootBuilder.Serialize(metadataBlobBuilder, methodBodyStreamRva: 0, mappedFieldDataStreamRva: 0);

            return(new ObjectData(
                       data: metadataBlobBuilder.ToArray(),
                       relocs: Array.Empty <Relocation>(),
                       alignment: 1,
                       definedSymbols: new ISymbolDefinitionNode[] { this }));
        }
Exemplo n.º 53
0
 public ObjectHandle CreateComInstanceFrom(string assemblyFile, string typeName, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
 {
     if (this == null)
     {
         throw new NullReferenceException();
     }
     return Activator.CreateComInstanceFrom(assemblyFile, typeName, hashValue, hashAlgorithm);
 }
        private static AssemblyName ParseAssemblyName(this MetadataReader metadataReader, StringHandle nameHandle, StringHandle cultureHandle, BlobHandle publicKeyTokenHandle, Version version, AssemblyHashAlgorithm assemblyHashAlgorithm, AssemblyFlags flags)
        {
            string culture = cultureHandle.IsNil ? "" : metadataReader.GetString(cultureHandle);
            string name    = metadataReader.GetString(nameHandle);

            AssemblyName assemblyName = new AssemblyName(name);

            assemblyName.Version = version;
#if net452
            assemblyName.CultureInfo = CultureInfo.CreateSpecificCulture(culture);
#else
            assemblyName.CultureName = culture;
#endif
            assemblyName.HashAlgorithm = (System.Configuration.Assemblies.AssemblyHashAlgorithm)assemblyHashAlgorithm;
            assemblyName.Flags         = GetAssemblyNameFlags(flags);
            assemblyName.ContentType   = GetContentTypeFromAssemblyFlags(flags);
            ReadPublicKeyToken(metadataReader, assemblyName, publicKeyTokenHandle, flags);

            return(assemblyName);
        }
Exemplo n.º 55
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        internal static RuntimeAssembly InternalLoadFrom(String assemblyFile, 
                                                         Evidence securityEvidence,
                                                         byte[] hashValue, 
                                                         AssemblyHashAlgorithm hashAlgorithm,
                                                         bool forIntrospection,
                                                         bool suppressSecurityChecks,
                                                         ref StackCrawlMark stackMark)
        {
            if (assemblyFile == null)
                throw new ArgumentNullException("assemblyFile");

            Contract.EndContractBlock();

#if FEATURE_CAS_POLICY
            if (securityEvidence != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
            }
#endif // FEATURE_CAS_POLICY
            AssemblyName an = new AssemblyName();
            an.CodeBase = assemblyFile;
            an.SetHashControl(hashValue, hashAlgorithm);
            // The stack mark is used for MDA filtering
            return InternalLoadAssemblyName(an, securityEvidence, null, ref stackMark, true /*thrownOnFileNotFound*/, forIntrospection, suppressSecurityChecks);
        }
Exemplo n.º 56
0
 private void Add(AssemblyHashAlgorithm a)
 {
 }
Exemplo n.º 57
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public static Assembly LoadFrom(String assemblyFile,
                                        byte[] hashValue,
                                        AssemblyHashAlgorithm hashAlgorithm)
        {
            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;

            return RuntimeAssembly.InternalLoadFrom(
                assemblyFile, 
                null, 
                hashValue, 
                hashAlgorithm, 
                false,
                false,
                ref stackMark);
        }
Exemplo n.º 58
0
        internal static RuntimeAssembly InternalLoadFrom(string assemblyFile, System.Security.Policy.Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, bool forIntrospection, bool suppressSecurityChecks, ref StackCrawlMark stackMark)
        {
            if (assemblyFile == null)
            {
                throw new ArgumentNullException("assemblyFile");
            }
            if ((securityEvidence != null) && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
            }
            AssemblyName assemblyRef = new AssemblyName {
                CodeBase = assemblyFile
            };

            assemblyRef.SetHashControl(hashValue, hashAlgorithm);
            return(InternalLoadAssemblyName(assemblyRef, securityEvidence, ref stackMark, forIntrospection, suppressSecurityChecks));
        }
Exemplo n.º 59
0
        internal AssemblyName GetAssemblyName(StringHandle nameHandle, Version version, StringHandle cultureHandle, BlobHandle publicKeyOrTokenHandle, AssemblyHashAlgorithm assemblyHashAlgorithm, AssemblyFlags flags)
        {
            string name          = GetString(nameHandle);
            string cultureName   = (!cultureHandle.IsNil) ? GetString(cultureHandle) : null;
            var    hashAlgorithm = (System.Configuration.Assemblies.AssemblyHashAlgorithm)assemblyHashAlgorithm;

            byte[] publicKeyOrToken = !publicKeyOrTokenHandle.IsNil ? GetBlobBytes(publicKeyOrTokenHandle) : null;

            var assemblyName = new AssemblyName(name)
            {
                Version       = version,
                CultureName   = cultureName,
                HashAlgorithm = hashAlgorithm,
                Flags         = GetAssemblyNameFlags(flags),
                ContentType   = GetContentTypeFromAssemblyFlags(flags)
            };

            bool hasPublicKey = (flags & AssemblyFlags.PublicKey) != 0;

            if (hasPublicKey)
            {
                assemblyName.SetPublicKey(publicKeyOrToken);
            }
            else
            {
                assemblyName.SetPublicKeyToken(publicKeyOrToken);
            }

            return(assemblyName);
        }
 ImmutableArray <byte> Cci.IFileReference.GetHashValue(AssemblyHashAlgorithm algorithmId)
 {
     return(_hashes.GetHash(algorithmId));
 }