diff --git a/Confuser.Core/ConfuserContext.cs b/Confuser.Core/ConfuserContext.cs index 23fbf5607..fd455b2d6 100644 --- a/Confuser.Core/ConfuserContext.cs +++ b/Confuser.Core/ConfuserContext.cs @@ -173,6 +173,7 @@ public NativeModuleWriterOptions RequestNative() { newOptions.ModuleKind = CurrentModuleWriterOptions.ModuleKind; newOptions.PEHeadersOptions = CurrentModuleWriterOptions.PEHeadersOptions; newOptions.ShareMethodBodies = CurrentModuleWriterOptions.ShareMethodBodies; + newOptions.DelaySign = CurrentModuleWriterOptions.DelaySign; newOptions.StrongNameKey = CurrentModuleWriterOptions.StrongNameKey; newOptions.StrongNamePublicKey = CurrentModuleWriterOptions.StrongNamePublicKey; newOptions.Win32Resources = CurrentModuleWriterOptions.Win32Resources; diff --git a/Confuser.Core/ConfuserEngine.cs b/Confuser.Core/ConfuserEngine.cs index 867df1165..43b35bc49 100644 --- a/Confuser.Core/ConfuserEngine.cs +++ b/Confuser.Core/ConfuserEngine.cs @@ -272,15 +272,8 @@ static void Inspection(ConfuserContext context) { } context.Logger.Debug("Checking Strong Name..."); - foreach (ModuleDefMD module in context.Modules) { - var snKey = context.Annotations.Get(module, Marker.SNKey); - if (snKey == null && module.IsStrongNameSigned) - context.Logger.WarnFormat("[{0}] SN Key is not provided for a signed module, the output may not be working.", module.Name); - else if (snKey != null && !module.IsStrongNameSigned) - context.Logger.WarnFormat("[{0}] SN Key is provided for an unsigned module, the output may not be working.", module.Name); - else if (snKey != null && module.IsStrongNameSigned && - !module.Assembly.PublicKey.Data.SequenceEqual(snKey.PublicKey)) - context.Logger.WarnFormat("[{0}] Provided SN Key and signed module's public key do not match, the output may not be working.", module.Name); + foreach (var module in context.Modules) { + CheckStrongName(context, module); } var marker = context.Registry.GetService(); @@ -300,6 +293,28 @@ static void Inspection(ConfuserContext context) { } } + static void CheckStrongName(ConfuserContext context, ModuleDef module) { + var snKey = context.Annotations.Get(module, Marker.SNKey); + var snPubKeyBytes = context.Annotations.Get(module, Marker.SNPubKey)?.CreatePublicKey(); + var snDelaySign = context.Annotations.Get(module, Marker.SNDelaySig); + + if (snPubKeyBytes == null && snKey != null) + snPubKeyBytes = snKey.PublicKey; + + bool moduleIsSignedOrDelayedSigned = module.IsStrongNameSigned || !module.Assembly.PublicKey.IsNullOrEmpty; + + bool isKeyProvided = snKey != null || (snDelaySign && snPubKeyBytes != null); + + if (!isKeyProvided && moduleIsSignedOrDelayedSigned) + context.Logger.WarnFormat("[{0}] SN Key or SN public Key is not provided for a signed module, the output may not be working.", module.Name); + else if (isKeyProvided && !moduleIsSignedOrDelayedSigned) + context.Logger.WarnFormat("[{0}] SN Key or SN public Key is provided for an unsigned module, the output may not be working.", module.Name); + else if (snPubKeyBytes != null && moduleIsSignedOrDelayedSigned && + !module.Assembly.PublicKey.Data.SequenceEqual(snPubKeyBytes)) + context.Logger.WarnFormat("[{0}] Provided SN public Key and signed module's public key do not match, the output may not be working.", + module.Name); + } + static void CopyPEHeaders(PEHeadersOptions writerOptions, ModuleDefMD module) { var image = module.Metadata.PEImage; writerOptions.MajorImageVersion = image.ImageNTHeaders.OptionalHeader.MajorImageVersion; @@ -322,8 +337,26 @@ static void BeginModule(ConfuserContext context) { if (!context.CurrentModule.IsILOnly || context.CurrentModule.VTableFixups != null) context.RequestNative(); - var snKey = context.Annotations.Get(context.CurrentModule, Marker.SNKey); - context.CurrentModuleWriterOptions.InitializeStrongNameSigning(context.CurrentModule, snKey); + var snKey = context.Annotations.Get(context.CurrentModule, Marker.SNKey); + var snPubKey = context.Annotations.Get(context.CurrentModule, Marker.SNPubKey); + var snSigKey = context.Annotations.Get(context.CurrentModule, Marker.SNSigKey); + var snSigPubKey = context.Annotations.Get(context.CurrentModule, Marker.SNSigPubKey); + + var snDelaySig = context.Annotations.Get(context.CurrentModule, Marker.SNDelaySig, false); + + context.CurrentModuleWriterOptions.DelaySign = snDelaySig; + + if (snKey != null && snPubKey != null && snSigKey != null && snSigPubKey != null) + context.CurrentModuleWriterOptions.InitializeEnhancedStrongNameSigning(context.CurrentModule, snSigKey, snSigPubKey, snKey, snPubKey); + else if (snSigPubKey != null && snSigKey != null) + context.CurrentModuleWriterOptions.InitializeEnhancedStrongNameSigning(context.CurrentModule, snSigKey, snSigPubKey); + else + context.CurrentModuleWriterOptions.InitializeStrongNameSigning(context.CurrentModule, snKey); + + if (snDelaySig) { + context.CurrentModuleWriterOptions.StrongNamePublicKey = snPubKey; + context.CurrentModuleWriterOptions.StrongNameKey = null; + } foreach (TypeDef type in context.CurrentModule.GetTypes()) foreach (MethodDef method in type.Methods) { diff --git a/Confuser.Core/Marker.cs b/Confuser.Core/Marker.cs index c4b5aa98e..3f0b6f46b 100644 --- a/Confuser.Core/Marker.cs +++ b/Confuser.Core/Marker.cs @@ -9,32 +9,52 @@ using dnlib.DotNet; namespace Confuser.Core { - using Rules = Dictionary; - + using Rules = Dictionary; + /// /// Resolves and marks the modules with protection settings according to the rules. /// - public class Marker { + public class Marker { /// /// Annotation key of Strong Name Key. /// - public static readonly object SNKey = new object(); - + public static readonly object SNKey = new object(); + + /// + /// Annotation key of Strong Name Public Key. + /// + public static readonly object SNPubKey = new object(); + + /// + /// Annotation key of Strong Name delay signing. + /// + public static readonly object SNDelaySig = new object(); + + /// + /// Annotation key of Strong Name Signature Key. + /// + public static readonly object SNSigKey = new object(); + + /// + /// Annotation key of Strong Name Public Signature Key. + /// + public static readonly object SNSigPubKey = new object(); + /// /// Annotation key of rules. /// - public static readonly object RulesKey = new object(); - + public static readonly object RulesKey = new object(); + /// /// The packers available to use. /// - protected Dictionary packers; - + protected Dictionary packers; + /// /// The protections available to use. /// - protected Dictionary protections; - + protected Dictionary protections; + /// /// Initalizes the Marker with specified protections and packers. /// @@ -43,8 +63,8 @@ public class Marker { public virtual void Initalize(IList protections, IList packers) { this.protections = protections.ToDictionary(prot => prot.Id, prot => prot, StringComparer.OrdinalIgnoreCase); this.packers = packers.ToDictionary(packer => packer.Id, packer => packer, StringComparer.OrdinalIgnoreCase); - } - + } + /// /// Fills the protection settings with the specified preset. /// @@ -54,8 +74,20 @@ void FillPreset(ProtectionPreset preset, ProtectionSettings settings) { foreach (Protection prot in protections.Values) if (prot.Preset != ProtectionPreset.None && prot.Preset <= preset && !settings.ContainsKey(prot)) settings.Add(prot, new Dictionary()); - } - + } + + public static StrongNamePublicKey LoadSNPubKey(ConfuserContext context, string path) { + if (path == null) return null; + + try { + return new StrongNamePublicKey(path); + } + catch (Exception ex) { + context.Logger.ErrorException("Cannot load the Strong Name Public Key located at: " + path, ex); + throw new ConfuserException(ex); + } + } + /// /// Loads the Strong Name Key at the specified path with a optional password. /// @@ -70,9 +102,9 @@ public static StrongNameKey LoadSNKey(ConfuserContext context, string path, stri if (path == null) return null; try { - if (pass != null) //pfx - { - // http://stackoverflow.com/a/12196742/462805 + if (pass != null) //pfx + { + // http://stackoverflow.com/a/12196742/462805 var cert = new X509Certificate2(); cert.Import(path, pass, X509KeyStorageFlags.Exportable); @@ -88,8 +120,8 @@ public static StrongNameKey LoadSNKey(ConfuserContext context, string path, stri context.Logger.ErrorException("Cannot load the Strong Name Key located at: " + path, ex); throw new ConfuserException(ex); } - } - + } + /// /// Loads the assembly and marks the project. /// @@ -135,20 +167,25 @@ protected internal virtual MarkerResult MarkProject(ConfuserProject proj, Confus Rules rules = ParseRules(proj, module.Item1, context); context.Annotations.Set(module.Item2, SNKey, LoadSNKey(context, module.Item1.SNKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNKeyPath), module.Item1.SNKeyPassword)); + context.Annotations.Set(module.Item2, SNSigKey, LoadSNKey(context, module.Item1.SNSigKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNSigKeyPath), module.Item1.SNKeyPassword)); + context.Annotations.Set(module.Item2, SNPubKey, LoadSNPubKey(context, module.Item1.SNPubKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNPubKeyPath))); + context.Annotations.Set(module.Item2, SNSigPubKey, LoadSNPubKey(context, module.Item1.SNPubSigKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNPubSigKeyPath))); + context.Annotations.Set(module.Item2, SNDelaySig, module.Item1.SNDelaySig); + context.Annotations.Set(module.Item2, RulesKey, rules); foreach (IDnlibDef def in module.Item2.FindDefinitions()) { ApplyRules(context, def, rules); context.CheckCancellation(); - } - - // Packer parameters are stored in modules + } + + // Packer parameters are stored in modules if (packerParams != null) ProtectionParameters.GetParameters(context, module.Item2)[packer] = packerParams; } return new MarkerResult(modules.Select(module => module.Item2).ToList(), packer, extModules); - } - + } + /// /// Marks the member definition. /// @@ -158,8 +195,8 @@ protected internal virtual void MarkMember(IDnlibDef member, ConfuserContext con ModuleDef module = ((IMemberRef)member).Module; var rules = context.Annotations.Get(module, RulesKey); ApplyRules(context, member, rules); - } - + } + /// /// Parses the rules' patterns. /// @@ -189,8 +226,8 @@ protected Rules ParseRules(ConfuserProject proj, ProjectModule module, ConfuserC } } return ret; - } - + } + /// /// Applies the rules to the target definition. /// @@ -218,4 +255,4 @@ protected void ApplyRules(ConfuserContext context, IDnlibDef target, Rules rules ProtectionParameters.SetParameters(context, target, ret); } } -} \ No newline at end of file +} diff --git a/Confuser.Core/ObfAttrMarker.cs b/Confuser.Core/ObfAttrMarker.cs index 0c26bbfe1..142781fbe 100644 --- a/Confuser.Core/ObfAttrMarker.cs +++ b/Confuser.Core/ObfAttrMarker.cs @@ -374,6 +374,11 @@ ProtectionSettingsInfo AddRule(ObfuscationAttributeInfo attr, List(); @@ -425,8 +430,23 @@ void MarkModule(ProjectModule projModule, ModuleDefMD module, Rules rules, bool } snKeyPath = snKeyPath == null ? null : Path.Combine(project.BaseDirectory, snKeyPath); + snPubKeyPath = snPubKeyPath == null ? null : Path.Combine(project.BaseDirectory, snPubKeyPath); + snSigKeyPath = snSigKeyPath == null ? null : Path.Combine(project.BaseDirectory, snSigKeyPath); + snPubSigKeyPath = snPubSigKeyPath == null ? null : Path.Combine(project.BaseDirectory, snPubSigKeyPath); + var snKey = LoadSNKey(context, snKeyPath, snKeyPass); - context.Annotations.Set(module, SNKey, snKey); + context.Annotations.Set(module, SNKey, snKey); + + var snPubKey = LoadSNPubKey(context, snPubKeyPath); + context.Annotations.Set(module, SNPubKey, snPubKey); + + context.Annotations.Set(module, SNDelaySig, snDelaySig); + + var snSigKey = LoadSNKey(context, snSigKeyPath, snKeyPass); + context.Annotations.Set(module, SNSigKey, snSigKey); + + var snSigPubKey = LoadSNPubKey(context, snPubSigKeyPath); + context.Annotations.Set(module, SNSigPubKey, snSigPubKey); using (stack.Apply(module, layer)) ProcessModule(module, stack); diff --git a/Confuser.Core/Packer.cs b/Confuser.Core/Packer.cs index 078c582c0..29147055f 100644 --- a/Confuser.Core/Packer.cs +++ b/Confuser.Core/Packer.cs @@ -27,7 +27,7 @@ public abstract class Packer : ConfuserComponent { /// The stub module. /// The strong name key. /// The packer protection that applies to the stub. - protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, Protection prot = null) { + 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()); @@ -75,7 +75,7 @@ protected void ProtectStub(ConfuserContext context, string fileName, byte[] modu new ConfuserParameters { Logger = new PackerLogger(context.Logger), PluginDiscovery = discovery, - Marker = new PackerMarker(snKey), + Marker = new PackerMarker(snKey, snPubKey, snDelaySig, snSigKey, snPubSigKey), Project = proj, PackerInitiated = true }, context.token).Wait(); @@ -165,15 +165,28 @@ public void Finish(bool successful) { internal class PackerMarker : Marker { readonly StrongNameKey snKey; + readonly StrongNamePublicKey snPubKey; + readonly bool snDelaySig; + readonly StrongNameKey snSigKey; + readonly StrongNamePublicKey snPubSigKey; - public PackerMarker(StrongNameKey snKey) { + 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; } protected internal override MarkerResult MarkProject(ConfuserProject proj, ConfuserContext context) { MarkerResult result = base.MarkProject(proj, context); - foreach (ModuleDefMD module in result.Modules) - context.Annotations.Set(module, SNKey, snKey); + foreach (ModuleDefMD module in result.Modules) { + context.Annotations.Set(module, SNKey, snKey); + context.Annotations.Set(module, SNPubKey, snPubKey); + context.Annotations.Set(module, SNDelaySig, snDelaySig); + context.Annotations.Set(module, SNSigKey, snSigKey); + context.Annotations.Set(module, SNSigPubKey, snPubSigKey); + } return result; } } diff --git a/Confuser.Core/Project/ConfuserPrj.xsd b/Confuser.Core/Project/ConfuserPrj.xsd index bdad775b7..43cfa8290 100644 --- a/Confuser.Core/Project/ConfuserPrj.xsd +++ b/Confuser.Core/Project/ConfuserPrj.xsd @@ -53,7 +53,11 @@ + + + + @@ -72,4 +76,4 @@ - \ No newline at end of file + diff --git a/Confuser.Core/Project/ConfuserProject.cs b/Confuser.Core/Project/ConfuserProject.cs index 40f220f87..105534c4b 100644 --- a/Confuser.Core/Project/ConfuserProject.cs +++ b/Confuser.Core/Project/ConfuserProject.cs @@ -38,8 +38,31 @@ public ProjectModule() { /// Gets or sets the password of the strong name private key. /// /// The password of the strong name private key, or null if not necessary. - public string SNKeyPassword { get; set; } - + public string SNKeyPassword { get; set; } + + /// + /// Gets or sets if the generated assembly should be delayed signed. + /// + public bool SNDelaySig { get; set; } + + /// + /// Gets or sets the path to the strong name public key for signing. + /// + /// The path to the strong name public key, or null if not necessary. + public string SNPubKeyPath { get; set; } + + /// + /// Gets or sets the path to the strong name private key used for enhanced signing. + /// + /// The path to the strong name private key used for enhanced signing, or null if not necessary. + public string SNSigKeyPath { get; set; } + + /// + /// Gets or sets the path to the strong name public key used for enhanced signing. + /// + /// The path to the strong name public key used for enhanced signing, or null if not necessary. + public string SNPubSigKeyPath { get; set; } + /// /// Gets a list of protection rules applies to the module. /// @@ -101,9 +124,28 @@ internal XmlElement Save(XmlDocument xmlDoc) { XmlAttribute snKeyPassAttr = xmlDoc.CreateAttribute("snKeyPass"); snKeyPassAttr.Value = SNKeyPassword; elem.Attributes.Append(snKeyPassAttr); + } + if (SNDelaySig) { + XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snDelaySig"); + snKeyAttr.Value = SNDelaySig ? "true" : "false"; + elem.Attributes.Append(snKeyAttr); + } + if (SNPubKeyPath != null) { + XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snPubKey"); + snKeyAttr.Value = SNPubKeyPath; + elem.Attributes.Append(snKeyAttr); + } + if (SNSigKeyPath != null) { + XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snSigKey"); + snKeyAttr.Value = SNSigKeyPath; + elem.Attributes.Append(snKeyAttr); + } + if (SNPubSigKeyPath != null) { + XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snPubSigKey"); + snKeyAttr.Value = SNPubSigKeyPath; + elem.Attributes.Append(snKeyAttr); } - foreach (Rule i in Rules) elem.AppendChild(i.Save(xmlDoc)); @@ -130,8 +172,30 @@ internal void Load(XmlElement elem) { if (elem.Attributes["snKeyPass"] != null) SNKeyPassword = elem.Attributes["snKeyPass"].Value.NullIfEmpty(); else - SNKeyPassword = null; - + SNKeyPassword = null; + + bool delaySig = false; + + if (elem.Attributes["snDelaySig"] != null) + bool.TryParse(elem.Attributes["snDelaySig"].Value, out delaySig); + + SNDelaySig = delaySig; + + if (elem.Attributes["snPubKey"] != null) + SNPubKeyPath = elem.Attributes["snPubKey"].Value.NullIfEmpty(); + else + SNPubKeyPath = null; + + if (elem.Attributes["snSigKey"] != null) + SNSigKeyPath = elem.Attributes["snSigKey"].Value.NullIfEmpty(); + else + SNSigKeyPath = null; + + if (elem.Attributes["snPubSigKey"] != null) + SNPubSigKeyPath = elem.Attributes["snPubSigKey"].Value.NullIfEmpty(); + else + SNPubSigKeyPath = null; + Rules.Clear(); foreach (XmlElement i in elem.ChildNodes.OfType()) { var rule = new Rule(); @@ -156,7 +220,11 @@ public ProjectModule Clone() { var ret = new ProjectModule(); ret.Path = Path; ret.IsExternal = IsExternal; - ret.SNKeyPath = SNKeyPath; + ret.SNKeyPath = SNKeyPath; + ret.SNPubKeyPath = SNPubKeyPath; + ret.SNDelaySig = SNDelaySig; + ret.SNPubSigKeyPath = SNPubSigKeyPath; + ret.SNSigKeyPath = SNSigKeyPath; ret.SNKeyPassword = SNKeyPassword; foreach (var r in Rules) ret.Rules.Add(r.Clone()); diff --git a/Confuser.MSBuild.Tasks/CreateProjectTask.cs b/Confuser.MSBuild.Tasks/CreateProjectTask.cs index 0de560fa9..f962f22e9 100644 --- a/Confuser.MSBuild.Tasks/CreateProjectTask.cs +++ b/Confuser.MSBuild.Tasks/CreateProjectTask.cs @@ -18,7 +18,15 @@ public sealed class CreateProjectTask : Task { public ITaskItem[] SatelliteAssemblyPaths { get; set; } - public ITaskItem KeyFilePath { get; set; } + public ITaskItem KeyFilePath { get; set; } + + public ITaskItem DelaySig { get; set; } + + public ITaskItem PubKeyFilePath { get; set; } + + public ITaskItem SigKeyFilePath { get; set; } + + public ITaskItem PubSigKeyFilePath { get; set; } [Required, Output] public ITaskItem ResultProject { get; set; } @@ -35,19 +43,36 @@ public override bool Execute() { } project.BaseDirectory = Path.GetDirectoryName(AssemblyPath.ItemSpec); - var mainModule = GetOrCreateProjectModule(project, AssemblyPath.ItemSpec); + var mainModule = GetOrCreateProjectModule(project, AssemblyPath.ItemSpec); + if (!string.IsNullOrWhiteSpace(KeyFilePath?.ItemSpec)) { mainModule.SNKeyPath = KeyFilePath.ItemSpec; } + if (!string.IsNullOrWhiteSpace(PubKeyFilePath?.ItemSpec)) { + mainModule.SNPubKeyPath = PubKeyFilePath.ItemSpec; + } + if (!string.IsNullOrWhiteSpace(SigKeyFilePath?.ItemSpec)) { + mainModule.SNSigKeyPath = SigKeyFilePath.ItemSpec; + } + if (!string.IsNullOrWhiteSpace(PubSigKeyFilePath?.ItemSpec)) { + mainModule.SNPubSigKeyPath = PubSigKeyFilePath.ItemSpec; + } + if (!string.IsNullOrWhiteSpace(DelaySig?.ItemSpec)) { + bool.TryParse(DelaySig.ItemSpec, out bool delaySig); + mainModule.SNDelaySig = delaySig; + } if (SatelliteAssemblyPaths != null) { foreach (var satelliteAssembly in SatelliteAssemblyPaths) { - if (!string.IsNullOrWhiteSpace(satelliteAssembly?.ItemSpec)) { - var satelliteModule = GetOrCreateProjectModule(project, satelliteAssembly.ItemSpec); - if (!string.IsNullOrWhiteSpace(KeyFilePath?.ItemSpec)) { - satelliteModule.SNKeyPath = KeyFilePath.ItemSpec; - } - } + if (string.IsNullOrWhiteSpace(satelliteAssembly?.ItemSpec)) continue; + + var satelliteModule = GetOrCreateProjectModule(project, satelliteAssembly.ItemSpec); + + satelliteModule.SNKeyPath = mainModule.SNKeyPath; + satelliteModule.SNPubKeyPath = mainModule.SNPubKeyPath; + satelliteModule.SNSigKeyPath = mainModule.SNSigKeyPath; + satelliteModule.SNPubSigKeyPath = mainModule.SNPubSigKeyPath; + satelliteModule.SNDelaySig = mainModule.SNDelaySig; } } diff --git a/Confuser.Protections/Compress/Compressor.cs b/Confuser.Protections/Compress/Compressor.cs index 6f4819345..c08e140cc 100644 --- a/Confuser.Protections/Compress/Compressor.cs +++ b/Confuser.Protections/Compress/Compressor.cs @@ -82,17 +82,24 @@ protected override void Pack(ConfuserContext context, ProtectionParameters param InjectStub(context, ctx, parameters, stubModule); - var snKey = context.Annotations.Get(originModule, Marker.SNKey); + var snKey = context.Annotations.Get(originModule, Marker.SNKey); + var snPubKey = context.Annotations.Get(originModule, Marker.SNPubKey); + var snDelaySig = context.Annotations.Get(originModule, Marker.SNDelaySig, false); + var snSigKey = context.Annotations.Get(originModule, Marker.SNSigKey); + var snPubSigKey = context.Annotations.Get(originModule, Marker.SNSigPubKey); + using (var ms = new MemoryStream()) { var options = new ModuleWriterOptions(stubModule) { - StrongNameKey = snKey + StrongNameKey = snKey, + StrongNamePublicKey = snPubKey, + DelaySign = snDelaySig }; var injector = new KeyInjector(ctx); options.WriterEvent += injector.WriterEvent; stubModule.Write(ms, options); context.CheckCancellation(); - ProtectStub(context, context.OutputPaths[ctx.ModuleIndex], ms.ToArray(), snKey, new StubProtection(ctx, originModule)); + ProtectStub(context, context.OutputPaths[ctx.ModuleIndex], ms.ToArray(), snKey, snPubKey, snSigKey, snPubKey, snDelaySig, new StubProtection(ctx, originModule)); } } diff --git a/Confuser.Protections/Resources/MDPhase.cs b/Confuser.Protections/Resources/MDPhase.cs index 5b8d2d5c3..208b3454b 100644 --- a/Confuser.Protections/Resources/MDPhase.cs +++ b/Confuser.Protections/Resources/MDPhase.cs @@ -38,8 +38,10 @@ void OnWriterEvent(object sender, ModuleWriterEventArgs e) { // move resources string asmName = ctx.Name.RandomName(RenameMode.Letters); PublicKey pubKey = null; - if (writer.TheOptions.StrongNameKey != null) - pubKey = PublicKeyBase.CreatePublicKey(writer.TheOptions.StrongNameKey.PublicKey); + if (writer.TheOptions.StrongNamePublicKey != null) + pubKey = PublicKeyBase.CreatePublicKey(writer.TheOptions.StrongNamePublicKey.CreatePublicKey()); + else if (writer.TheOptions.StrongNameKey != null) + pubKey = PublicKeyBase.CreatePublicKey(writer.TheOptions.StrongNameKey.PublicKey); var assembly = new AssemblyDefUser(asmName, new Version(0, 0), pubKey); assembly.Modules.Add(new ModuleDefUser(asmName + ".dll")); ModuleDef module = assembly.ManifestModule; @@ -54,7 +56,7 @@ void OnWriterEvent(object sender, ModuleWriterEventArgs e) { } byte[] moduleBuff; using (var ms = new MemoryStream()) { - module.Write(ms, new ModuleWriterOptions(e.Writer.Module) { StrongNameKey = writer.TheOptions.StrongNameKey }); + module.Write(ms, new ModuleWriterOptions(e.Writer.Module) { StrongNameKey = writer.TheOptions.StrongNameKey, StrongNamePublicKey = writer.TheOptions.StrongNamePublicKey, DelaySign = writer.TheOptions.DelaySign }); moduleBuff = ms.ToArray(); }