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 { } MsgBox.Instance.Show(string.Format(dnSpy_AsmEditor_Resources.Error_NotSNKFile, dialog.FileName), MsgBoxButton.OK, ownerWindow); return(null); }
public void CanSignAssembly(string inputAssembly, string outputAssembly) { string keyFile = Path.Combine(KeyDir, "test.snk"); var key = new StrongNameKey(keyFile); var signer = new StrongNameSigner(); signer.SignAssembly(inputAssembly, key, outputAssembly); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { int exitCode = Exec.RunNetFxTool("sn.exe", new[] { "-v", outputAssembly }); exitCode.Should() .Be(0); } PEReader assemblyReader = GetPEReader(outputAssembly); assemblyReader.PEHeaders.CorHeader.Flags.Should() .HaveFlag(CorFlags.StrongNameSigned); AssemblyName assemblyName = GetAssemblyName(assemblyReader.GetMetadataReader()); StrongNamePublicKey publicKey = key.GetPublicKey(); assemblyName.GetPublicKey() .Should() .BeEquivalentTo(publicKey.CreatePublicKey()); assemblyName.GetPublicKeyToken() .Should() .BeEquivalentTo(publicKey.CreatePublicKeyToken()); }
public PackerMarker(StrongNameKey snKey, StrongNamePublicKey snPubKey, bool snDelaySig, StrongNameKey snSigKey, StrongNamePublicKey snPubSigKey) { this.snKey = snKey; this.snPubKey = snPubKey; this.snDelaySig = snDelaySig; this.snSigKey = snSigKey; this.snPubSigKey = snPubSigKey; }
/// <summary> /// Initializes <see cref="StrongNameKey"/> and <see cref="StrongNamePublicKey"/> /// for enhanced strong name signing (with key migration). See /// http://msdn.microsoft.com/en-us/library/hh415055.aspx /// </summary> /// <param name="module">Module</param> /// <param name="signatureKey">Signature strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> /// <param name="identityKey">Identity strong name key pair</param> /// <param name="identityPubKey">Identity public key</param> public void InitializeEnhancedStrongNameSigning(ModuleDef module, StrongNameKey signatureKey, StrongNamePublicKey signaturePubKey, StrongNameKey identityKey, StrongNamePublicKey identityPubKey) { StrongNameKey = signatureKey; StrongNameKey.HashAlgorithm = signaturePubKey.HashAlgorithm; StrongNamePublicKey = identityPubKey; if (module.Assembly != null) { module.Assembly.UpdateOrCreateAssemblySignatureKeyAttribute(identityPubKey, identityKey, signaturePubKey); } }
public void DelaySignAssembly(string assemblyPath, StrongNamePublicKey publicKey, string outAssemblyPath = null) { ModuleDefMD module = LoadAssembly(assemblyPath); var options = new ModuleWriterOptions(module) { DelaySign = true, StrongNamePublicKey = publicKey }; module.Write(outAssemblyPath ?? assemblyPath, options); }
public void CanLoadPublicKeyGeneratedBySn() { string keyFile = Path.Combine(KeyDir, "test_public_sha1.snk"); var loader = new StrongNameKeyLoader(); StrongNamePublicKey key = loader.LoadPublicKey(keyFile); key.HashAlgorithm.Should() .Be(AssemblyHashAlgorithm.SHA1); key.SignatureAlgorithm.Should() .Be(SignatureAlgorithm.CALG_RSA_SIGN); }
public void PersistentStrongNamePublicKey() { using var rsa = RSA.Create(); var rsaParameters = rsa.ExportParameters(true); var publicKey = new StrongNamePublicKey(rsaParameters); using var tempStream = new MemoryStream(); publicKey.Write(new BinaryStreamWriter(tempStream)); var newPublicKey = StrongNamePublicKey.FromReader(new ByteArrayReader(tempStream.ToArray())); Assert.Equal(publicKey.Modulus, newPublicKey.Modulus); Assert.Equal(publicKey.PublicExponent, newPublicKey.PublicExponent); }
public int Execute(CommandLineApplication cmd) { bool delaySign = _delaySign.HasValue(); bool force = _force.HasValue(); string keyFile = _keyFile.Value; string assemblyFile = _assemblyFile.Value; string outAssemblyFile = !string.IsNullOrEmpty(_outAssemblyFile.Value) ? _outAssemblyFile.Value : null; try { if (File.Exists(outAssemblyFile) && !force) { throw new FileAlreadyExistsException(outAssemblyFile); } if (delaySign) { StrongNamePublicKey publicKey = _keyLoader.LoadPublicKey(keyFile); _signer.DelaySignAssembly(assemblyFile, publicKey, outAssemblyFile); } else { StrongNameKey key = _keyLoader.LoadKey(keyFile); _signer.SignAssembly(assemblyFile, key, outAssemblyFile); } } catch (Exception error) { cmd.Error.WriteLine("ERROR: {0}", error.Message); return(ExitCodes.FromException(error)); } cmd.Out.WriteLine("Assembly '{0}' successfully signed.", outAssemblyFile); return(ExitCodes.Success); }
public static byte[] CreatePublicKeyToken(this StrongNamePublicKey key) { byte[] publicKey = key.CreatePublicKey(); string hashAlg; switch (key.HashAlgorithm) { case AssemblyHashAlgorithm.SHA1: hashAlg = HashAlgorithmName.SHA1.Name; break; case AssemblyHashAlgorithm.SHA_256: hashAlg = HashAlgorithmName.SHA256.Name; break; case AssemblyHashAlgorithm.SHA_384: hashAlg = HashAlgorithmName.SHA384.Name; break; case AssemblyHashAlgorithm.SHA_512: hashAlg = HashAlgorithmName.SHA512.Name; break; default: throw new ArgumentOutOfRangeException(); } var ha = HashAlgorithm.Create("SHA1"); byte[] hash = ha.ComputeHash(publicKey); var keyToken = new byte[8]; Buffer.BlockCopy(hash, (hash.Length - 8), keyToken, 0, 8); Array.Reverse(keyToken, 0, 8); return(keyToken); }
public void CanDelaySignAssembly(string inputAssembly, string outputAssembly) { string keyFile = Path.Combine(KeyDir, "test_public_sha1.snk"); var publicKey = new StrongNamePublicKey(keyFile); var signer = new StrongNameSigner(); signer.DelaySignAssembly(inputAssembly, publicKey, outputAssembly); PEReader assemblyReader = GetPEReader(outputAssembly); assemblyReader.PEHeaders.CorHeader.Flags.Should() .NotHaveFlag(CorFlags.StrongNameSigned); AssemblyName assemblyName = GetAssemblyName(assemblyReader.GetMetadataReader()); assemblyName.GetPublicKey() .Should() .BeEquivalentTo(publicKey.CreatePublicKey()); assemblyName.GetPublicKeyToken() .Should() .BeEquivalentTo(publicKey.CreatePublicKeyToken()); }
/// <summary> /// Initializes <see cref="StrongNameKey"/> and <see cref="StrongNamePublicKey"/> /// for enhanced strong name signing (with key migration). See /// http://msdn.microsoft.com/en-us/library/hh415055.aspx /// </summary> /// <param name="module">Module</param> /// <param name="signatureKey">Signature strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> /// <param name="identityKey">Identity strong name key pair</param> /// <param name="identityPubKey">Identity public key</param> public void InitializeEnhancedStrongNameSigning(ModuleDef module, StrongNameKey signatureKey, StrongNamePublicKey signaturePubKey, StrongNameKey identityKey, StrongNamePublicKey identityPubKey) { StrongNameKey = signatureKey; StrongNameKey.HashAlgorithm = signaturePubKey.HashAlgorithm; StrongNamePublicKey = identityPubKey; if (module.Assembly != null) module.Assembly.UpdateOrCreateAssemblySignatureKeyAttribute(identityPubKey, identityKey, signaturePubKey); }
/// <summary> /// Initializes <see cref="StrongNameKey"/> and <see cref="StrongNamePublicKey"/> /// for enhanced strong name signing (without key migration). See /// http://msdn.microsoft.com/en-us/library/hh415055.aspx /// </summary> /// <param name="module">Module</param> /// <param name="signatureKey">Signature strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> public void InitializeEnhancedStrongNameSigning(ModuleDef module, StrongNameKey signatureKey, StrongNamePublicKey signaturePubKey) { InitializeStrongNameSigning(module, signatureKey); StrongNameKey.HashAlgorithm = signaturePubKey.HashAlgorithm; }
/// <summary> /// Initializes <see cref="StrongNameKey"/> and <see cref="StrongNamePublicKey"/> /// for enhanced strong name signing (without key migration). See /// http://msdn.microsoft.com/en-us/library/hh415055.aspx /// </summary> /// <param name="module">Module</param> /// <param name="signatureKey">Signature strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> public void InitializeEnhancedStrongNameSigning(ModuleDef module, StrongNameKey signatureKey, StrongNamePublicKey signaturePubKey) { InitializeStrongNameSigning(module, signatureKey); StrongNameKey.HashAlgorithm = signaturePubKey.HashAlgorithm; }
/// <summary> /// Protects the stub using original project settings replace the current output with the protected stub. /// </summary> /// <param name="context">The working context.</param> /// <param name="fileName">The result file name.</param> /// <param name="module">The stub module.</param> /// <param name="snKey">The strong name key.</param> /// <param name="prot">The packer protection that applies to the stub.</param> protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, StrongNamePublicKey snPubKey, StrongNameKey snSigKey, StrongNamePublicKey snPubSigKey, bool snDelaySig, Protection prot = null) { string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try { string outDir = Path.Combine(tmpDir, Path.GetRandomFileName()); Directory.CreateDirectory(tmpDir); for (int i = 0; i < context.OutputModules.Count; i++) { string path = Path.GetFullPath(Path.Combine(tmpDir, context.OutputPaths[i])); var dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.WriteAllBytes(path, context.OutputModules[i]); } File.WriteAllBytes(Path.Combine(tmpDir, fileName), module); var proj = new ConfuserProject(); proj.Seed = context.Project.Seed; foreach (Rule rule in context.Project.Rules) { proj.Rules.Add(rule); } proj.Add(new ProjectModule { Path = fileName }); proj.BaseDirectory = tmpDir; proj.OutputDirectory = outDir; foreach (var path in context.Project.ProbePaths) { proj.ProbePaths.Add(path); } proj.ProbePaths.Add(context.Project.BaseDirectory); PluginDiscovery discovery = null; if (prot != null) { var rule = new Rule { Preset = ProtectionPreset.None, Inherit = true, Pattern = "true" }; rule.Add(new SettingItem <Protection> { Id = prot.Id, Action = SettingItemAction.Add }); proj.Rules.Add(rule); discovery = new PackerDiscovery(prot); } try { ConfuserEngine .Run( new ConfuserParameters { Logger = new PackerLogger(context.Logger), PluginDiscovery = discovery, Marker = new PackerMarker(snKey, snPubKey, snDelaySig, snSigKey, snPubSigKey), Project = proj, PackerInitiated = true }, context.token).Wait(); } catch (AggregateException ex) { context.Logger.Error("Failed to protect packer stub."); throw new ConfuserException(ex); } context.OutputModules = new[] { File.ReadAllBytes(Path.Combine(outDir, fileName)) }; context.OutputPaths = new[] { fileName }; } finally { try { if (Directory.Exists(tmpDir)) { Directory.Delete(tmpDir, true); } } catch (IOException ex) { context.Logger.WarnException("Failed to remove temporary files of packer.", ex); } } }