Exemplo n.º 1
0
		/// <summary>
		/// Adds or updates an existing <c>System.Reflection.AssemblySignatureKeyAttribute</c>
		/// attribute. This attribute is used in enhanced strong naming with key migration.
		/// See http://msdn.microsoft.com/en-us/library/hh415055.aspx
		/// </summary>
		/// <param name="identityPubKey">Identity public key</param>
		/// <param name="identityKey">Identity strong name key pair</param>
		/// <param name="signaturePubKey">Signature public key</param>
		public void UpdateOrCreateAssemblySignatureKeyAttribute(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey) {
			var manifestModule = ManifestModule;
			if (manifestModule == null)
				return;

			// Remove all existing attributes
			var ca = CustomAttributes.ExecuteLocked<CustomAttribute, object, CustomAttribute>(null, (tsList, arg) => {
				CustomAttribute foundCa = null;
				for (int i = 0; i < tsList.Count_NoLock(); i++) {
					var caTmp = tsList.Get_NoLock(i);
					if (caTmp.TypeFullName != "System.Reflection.AssemblySignatureKeyAttribute")
						continue;
					tsList.RemoveAt_NoLock(i);
					i--;
					if (foundCa == null)
						foundCa = caTmp;
				}
				return foundCa;
			});

			if (IsValidAssemblySignatureKeyAttribute(ca))
				ca.NamedArguments.Clear();
			else
				ca = CreateAssemblySignatureKeyAttribute();

			var counterSig = StrongNameKey.CreateCounterSignatureAsString(identityPubKey, identityKey, signaturePubKey);
			ca.ConstructorArguments[0] = new CAArgument(manifestModule.CorLibTypes.String, new UTF8String(signaturePubKey.ToString()));
			ca.ConstructorArguments[1] = new CAArgument(manifestModule.CorLibTypes.String, new UTF8String(counterSig));
			CustomAttributes.Add(ca);
		}
Exemplo n.º 2
0
		public PublicKey Open() {
			var dialog = new OpenFileDialog() {
				Filter = PickFilenameConstants.StrongNameKeyFilter,
				RestoreDirectory = true,
			};
			if (dialog.ShowDialog() != DialogResult.OK)
				return null;
			if (string.IsNullOrEmpty(dialog.FileName))
				return null;

			try {
				var snk = new StrongNameKey(dialog.FileName);
				return new PublicKey(snk.PublicKey);
			}
			catch {
			}

			try {
				var snk = new StrongNamePublicKey(dialog.FileName);
				return new PublicKey(snk.CreatePublicKey());
			}
			catch {
			}

			Shared.App.MsgBox.Instance.Show(string.Format(dnSpy_AsmEditor_Resources.Error_NotSNKFile, dialog.FileName), MsgBoxButton.OK, ownerWindow);
			return null;
		}
Exemplo n.º 3
0
        /// <summary>
        /// Creates a counter signature, just like
        /// <c>sn -a IdentityPubKey.snk IdentityKey.snk SignaturePubKey.snk</c> can do.
        /// The public key <c>sn</c> prints is <paramref name="signaturePubKey"/>'s value.
        /// </summary>
        /// <param name="identityPubKey">Identity public key</param>
        /// <param name="identityKey">Identity strong name key pair</param>
        /// <param name="signaturePubKey">Signature public key</param>
        /// <returns>The counter signature</returns>
        public static byte[] CreateCounterSignature(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey)
        {
            var hash = AssemblyHash.Hash(signaturePubKey.CreatePublicKey(), identityPubKey.HashAlgorithm);

            using (var rsa = identityKey.CreateRSA()) {
                var rsaFmt      = new RSAPKCS1SignatureFormatter(rsa);
                string hashName = identityPubKey.HashAlgorithm.GetName();
                rsaFmt.SetHashAlgorithm(hashName);
                var snSig = rsaFmt.CreateSignature(hash);
                Array.Reverse(snSig);
                return(snSig);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds or updates an existing <c>System.Reflection.AssemblySignatureKeyAttribute</c>
        /// attribute. This attribute is used in enhanced strong naming with key migration.
        /// See http://msdn.microsoft.com/en-us/library/hh415055.aspx
        /// </summary>
        /// <param name="identityPubKey">Identity public key</param>
        /// <param name="identityKey">Identity strong name key pair</param>
        /// <param name="signaturePubKey">Signature public key</param>
        public void UpdateOrCreateAssemblySignatureKeyAttribute(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey)
        {
            if (ManifestModule == null)
            {
                return;
            }

            // Remove all existing attributes
            CustomAttribute ca = null;

            for (int i = 0; i < CustomAttributes.Count; i++)
            {
                var caTmp = CustomAttributes[i];
                if (caTmp.TypeFullName != "System.Reflection.AssemblySignatureKeyAttribute")
                {
                    continue;
                }
                CustomAttributes.RemoveAt(i);
                i--;
                if (ca == null)
                {
                    ca = caTmp;
                }
            }

            if (IsValidAssemblySignatureKeyAttribute(ca))
            {
                ca.NamedArguments.Clear();
            }
            else
            {
                ca = CreateAssemblySignatureKeyAttribute();
            }

            var counterSig = StrongNameKey.CreateCounterSignatureAsString(identityPubKey, identityKey, signaturePubKey);

            ca.ConstructorArguments[0] = new CAArgument(ManifestModule.CorLibTypes.String, new UTF8String(signaturePubKey.ToString()));
            ca.ConstructorArguments[1] = new CAArgument(ManifestModule.CorLibTypes.String, new UTF8String(counterSig));
            CustomAttributes.Add(ca);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a counter signature, just like
        /// <c>sn -a IdentityPubKey.snk IdentityKey.snk SignaturePubKey.snk</c> can do.
        /// The public key <c>sn</c> prints is <paramref name="signaturePubKey"/>'s value.
        /// </summary>
        /// <param name="identityPubKey">Identity public key</param>
        /// <param name="identityKey">Identity strong name key pair</param>
        /// <param name="signaturePubKey">Signature public key</param>
        /// <returns>The counter signature as a hex string</returns>
        public static string CreateCounterSignatureAsString(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey)
        {
            var counterSignature = CreateCounterSignature(identityPubKey, identityKey, signaturePubKey);

            return(Utils.ToHex(counterSignature, false));
        }
Exemplo n.º 6
0
        byte[] CreatePublicKey_NoLock()
        {
            var halg = hashAlg == 0 ? AssemblyHashAlgorithm.SHA1 : hashAlg;

            return(StrongNamePublicKey.CreatePublicKey(SignatureAlgorithm.CALG_RSA_SIGN, halg, modulus, publicExponent));
        }
Exemplo n.º 7
0
		/// <summary>
		/// Creates a counter signature, just like
		/// <c>sn -a IdentityPubKey.snk IdentityKey.snk SignaturePubKey.snk</c> can do.
		/// The public key <c>sn</c> prints is <paramref name="signaturePubKey"/>'s value.
		/// </summary>
		/// <param name="identityPubKey">Identity public key</param>
		/// <param name="identityKey">Identity strong name key pair</param>
		/// <param name="signaturePubKey">Signature public key</param>
		/// <returns>The counter signature</returns>
		public static byte[] CreateCounterSignature(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey) {
			var hash = AssemblyHash.Hash(signaturePubKey.CreatePublicKey(), identityPubKey.HashAlgorithm);
			using (var rsa = identityKey.CreateRSA()) {
				var rsaFmt = new RSAPKCS1SignatureFormatter(rsa);
				string hashName = identityPubKey.HashAlgorithm.GetName();
				rsaFmt.SetHashAlgorithm(hashName);
				var snSig = rsaFmt.CreateSignature(hash);
				Array.Reverse(snSig);
				return snSig;
			}
		}
Exemplo n.º 8
0
		/// <summary>
		/// Creates a counter signature, just like
		/// <c>sn -a IdentityPubKey.snk IdentityKey.snk SignaturePubKey.snk</c> can do.
		/// The public key <c>sn</c> prints is <paramref name="signaturePubKey"/>'s value.
		/// </summary>
		/// <param name="identityPubKey">Identity public key</param>
		/// <param name="identityKey">Identity strong name key pair</param>
		/// <param name="signaturePubKey">Signature public key</param>
		/// <returns>The counter signature as a hex string</returns>
		public static string CreateCounterSignatureAsString(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey) {
			var counterSignature = CreateCounterSignature(identityPubKey, identityKey, signaturePubKey);
			return Utils.ToHex(counterSignature, false);
		}