Skip to content

Commit 5a11d46

Browse files
Merge 7f73bdf into 18451d0
2 parents 18451d0 + 7f73bdf commit 5a11d46

File tree

10 files changed

+271
-65
lines changed

10 files changed

+271
-65
lines changed

Confuser.Core/ConfuserContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public NativeModuleWriterOptions RequestNative() {
173173
newOptions.ModuleKind = CurrentModuleWriterOptions.ModuleKind;
174174
newOptions.PEHeadersOptions = CurrentModuleWriterOptions.PEHeadersOptions;
175175
newOptions.ShareMethodBodies = CurrentModuleWriterOptions.ShareMethodBodies;
176+
newOptions.DelaySign = CurrentModuleWriterOptions.DelaySign;
176177
newOptions.StrongNameKey = CurrentModuleWriterOptions.StrongNameKey;
177178
newOptions.StrongNamePublicKey = CurrentModuleWriterOptions.StrongNamePublicKey;
178179
newOptions.Win32Resources = CurrentModuleWriterOptions.Win32Resources;

Confuser.Core/ConfuserEngine.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,23 @@ static void Inspection(ConfuserContext context) {
274274
context.Logger.Debug("Checking Strong Name...");
275275
foreach (ModuleDefMD module in context.Modules) {
276276
var snKey = context.Annotations.Get<StrongNameKey>(module, Marker.SNKey);
277-
if (snKey == null && module.IsStrongNameSigned)
278-
context.Logger.WarnFormat("[{0}] SN Key is not provided for a signed module, the output may not be working.", module.Name);
279-
else if (snKey != null && !module.IsStrongNameSigned)
280-
context.Logger.WarnFormat("[{0}] SN Key is provided for an unsigned module, the output may not be working.", module.Name);
281-
else if (snKey != null && module.IsStrongNameSigned &&
282-
!module.Assembly.PublicKey.Data.SequenceEqual(snKey.PublicKey))
283-
context.Logger.WarnFormat("[{0}] Provided SN Key and signed module's public key do not match, the output may not be working.", module.Name);
277+
var snPubKeyBytes = context.Annotations.Get<StrongNamePublicKey>(module, Marker.SNPubKey)?.CreatePublicKey();
278+
var snDelaySign = context.Annotations.Get<bool>(module, Marker.SNDelaySig, false);
279+
280+
if (snPubKeyBytes == null && snKey != null)
281+
snPubKeyBytes = snKey.PublicKey;
282+
283+
bool moduleIsSignedOrDelayedSigned = module.IsStrongNameSigned || !module.Assembly.PublicKey.IsNullOrEmpty;
284+
285+
bool isKeyProvided = snKey != null || (snDelaySign && snPubKeyBytes != null);
286+
287+
if (!isKeyProvided && moduleIsSignedOrDelayedSigned)
288+
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);
289+
else if (isKeyProvided && !moduleIsSignedOrDelayedSigned)
290+
context.Logger.WarnFormat("[{0}] SN Key or SN public Key is provided for an unsigned module, the output may not be working.", module.Name);
291+
else if (snPubKeyBytes != null && moduleIsSignedOrDelayedSigned &&
292+
!module.Assembly.PublicKey.Data.SequenceEqual(snPubKeyBytes))
293+
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);
284294
}
285295

286296
var marker = context.Registry.GetService<IMarkerService>();
@@ -322,8 +332,26 @@ static void BeginModule(ConfuserContext context) {
322332
if (!context.CurrentModule.IsILOnly || context.CurrentModule.VTableFixups != null)
323333
context.RequestNative();
324334

325-
var snKey = context.Annotations.Get<StrongNameKey>(context.CurrentModule, Marker.SNKey);
326-
context.CurrentModuleWriterOptions.InitializeStrongNameSigning(context.CurrentModule, snKey);
335+
var snKey = context.Annotations.Get<StrongNameKey>(context.CurrentModule, Marker.SNKey);
336+
var snPubKey = context.Annotations.Get<StrongNamePublicKey>(context.CurrentModule, Marker.SNPubKey);
337+
var snSigKey = context.Annotations.Get<StrongNameKey>(context.CurrentModule, Marker.SNSigKey);
338+
var snSigPubKey = context.Annotations.Get<StrongNamePublicKey>(context.CurrentModule, Marker.SNSigPubKey);
339+
340+
var snDelaySig = context.Annotations.Get<bool>(context.CurrentModule, Marker.SNDelaySig, false);
341+
342+
context.CurrentModuleWriterOptions.DelaySign = snDelaySig;
343+
344+
if (snKey != null && snPubKey != null && snSigKey != null && snSigPubKey != null)
345+
context.CurrentModuleWriterOptions.InitializeEnhancedStrongNameSigning(context.CurrentModule, snSigKey, snSigPubKey, snKey, snPubKey);
346+
else if (snSigPubKey != null && snSigKey != null)
347+
context.CurrentModuleWriterOptions.InitializeEnhancedStrongNameSigning(context.CurrentModule, snSigKey, snSigPubKey);
348+
else
349+
context.CurrentModuleWriterOptions.InitializeStrongNameSigning(context.CurrentModule, snKey);
350+
351+
if (snDelaySig) {
352+
context.CurrentModuleWriterOptions.StrongNamePublicKey = snPubKey;
353+
context.CurrentModuleWriterOptions.StrongNameKey = null;
354+
}
327355

328356
foreach (TypeDef type in context.CurrentModule.GetTypes())
329357
foreach (MethodDef method in type.Methods) {

Confuser.Core/Marker.cs

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,52 @@
99
using dnlib.DotNet;
1010

1111
namespace Confuser.Core {
12-
using Rules = Dictionary<Rule, PatternExpression>;
13-
12+
using Rules = Dictionary<Rule, PatternExpression>;
13+
1414
/// <summary>
1515
/// Resolves and marks the modules with protection settings according to the rules.
1616
/// </summary>
17-
public class Marker {
17+
public class Marker {
1818
/// <summary>
1919
/// Annotation key of Strong Name Key.
2020
/// </summary>
21-
public static readonly object SNKey = new object();
22-
21+
public static readonly object SNKey = new object();
22+
23+
/// <summary>
24+
/// Annotation key of Strong Name Public Key.
25+
/// </summary>
26+
public static readonly object SNPubKey = new object();
27+
28+
/// <summary>
29+
/// Annotation key of Strong Name delay signing.
30+
/// </summary>
31+
public static readonly object SNDelaySig = new object();
32+
33+
/// <summary>
34+
/// Annotation key of Strong Name Signature Key.
35+
/// </summary>
36+
public static readonly object SNSigKey = new object();
37+
38+
/// <summary>
39+
/// Annotation key of Strong Name Public Signature Key.
40+
/// </summary>
41+
public static readonly object SNSigPubKey = new object();
42+
2343
/// <summary>
2444
/// Annotation key of rules.
2545
/// </summary>
26-
public static readonly object RulesKey = new object();
27-
46+
public static readonly object RulesKey = new object();
47+
2848
/// <summary>
2949
/// The packers available to use.
3050
/// </summary>
31-
protected Dictionary<string, Packer> packers;
32-
51+
protected Dictionary<string, Packer> packers;
52+
3353
/// <summary>
3454
/// The protections available to use.
3555
/// </summary>
36-
protected Dictionary<string, Protection> protections;
37-
56+
protected Dictionary<string, Protection> protections;
57+
3858
/// <summary>
3959
/// Initalizes the Marker with specified protections and packers.
4060
/// </summary>
@@ -43,8 +63,8 @@ public class Marker {
4363
public virtual void Initalize(IList<Protection> protections, IList<Packer> packers) {
4464
this.protections = protections.ToDictionary(prot => prot.Id, prot => prot, StringComparer.OrdinalIgnoreCase);
4565
this.packers = packers.ToDictionary(packer => packer.Id, packer => packer, StringComparer.OrdinalIgnoreCase);
46-
}
47-
66+
}
67+
4868
/// <summary>
4969
/// Fills the protection settings with the specified preset.
5070
/// </summary>
@@ -54,8 +74,20 @@ void FillPreset(ProtectionPreset preset, ProtectionSettings settings) {
5474
foreach (Protection prot in protections.Values)
5575
if (prot.Preset != ProtectionPreset.None && prot.Preset <= preset && !settings.ContainsKey(prot))
5676
settings.Add(prot, new Dictionary<string, string>());
57-
}
58-
77+
}
78+
79+
public static StrongNamePublicKey LoadSNPubKey(ConfuserContext context, string path) {
80+
if (path == null) return null;
81+
82+
try {
83+
return new StrongNamePublicKey(path);
84+
}
85+
catch (Exception ex) {
86+
context.Logger.ErrorException("Cannot load the Strong Name Public Key located at: " + path, ex);
87+
throw new ConfuserException(ex);
88+
}
89+
}
90+
5991
/// <summary>
6092
/// Loads the Strong Name Key at the specified path with a optional password.
6193
/// </summary>
@@ -70,9 +102,9 @@ public static StrongNameKey LoadSNKey(ConfuserContext context, string path, stri
70102
if (path == null) return null;
71103

72104
try {
73-
if (pass != null) //pfx
74-
{
75-
// http://stackoverflow.com/a/12196742/462805
105+
if (pass != null) //pfx
106+
{
107+
// http://stackoverflow.com/a/12196742/462805
76108
var cert = new X509Certificate2();
77109
cert.Import(path, pass, X509KeyStorageFlags.Exportable);
78110

@@ -88,8 +120,8 @@ public static StrongNameKey LoadSNKey(ConfuserContext context, string path, stri
88120
context.Logger.ErrorException("Cannot load the Strong Name Key located at: " + path, ex);
89121
throw new ConfuserException(ex);
90122
}
91-
}
92-
123+
}
124+
93125
/// <summary>
94126
/// Loads the assembly and marks the project.
95127
/// </summary>
@@ -135,20 +167,25 @@ protected internal virtual MarkerResult MarkProject(ConfuserProject proj, Confus
135167
Rules rules = ParseRules(proj, module.Item1, context);
136168

137169
context.Annotations.Set(module.Item2, SNKey, LoadSNKey(context, module.Item1.SNKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNKeyPath), module.Item1.SNKeyPassword));
170+
context.Annotations.Set(module.Item2, SNSigKey, LoadSNKey(context, module.Item1.SNSigKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNSigKeyPath), module.Item1.SNKeyPassword));
171+
context.Annotations.Set(module.Item2, SNPubKey, LoadSNPubKey(context, module.Item1.SNPubKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNPubKeyPath)));
172+
context.Annotations.Set(module.Item2, SNSigPubKey, LoadSNPubKey(context, module.Item1.SNPubSigKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNPubSigKeyPath)));
173+
context.Annotations.Set(module.Item2, SNDelaySig, module.Item1.SNDelaySig);
174+
138175
context.Annotations.Set(module.Item2, RulesKey, rules);
139176

140177
foreach (IDnlibDef def in module.Item2.FindDefinitions()) {
141178
ApplyRules(context, def, rules);
142179
context.CheckCancellation();
143-
}
144-
145-
// Packer parameters are stored in modules
180+
}
181+
182+
// Packer parameters are stored in modules
146183
if (packerParams != null)
147184
ProtectionParameters.GetParameters(context, module.Item2)[packer] = packerParams;
148185
}
149186
return new MarkerResult(modules.Select(module => module.Item2).ToList(), packer, extModules);
150-
}
151-
187+
}
188+
152189
/// <summary>
153190
/// Marks the member definition.
154191
/// </summary>
@@ -158,8 +195,8 @@ protected internal virtual void MarkMember(IDnlibDef member, ConfuserContext con
158195
ModuleDef module = ((IMemberRef)member).Module;
159196
var rules = context.Annotations.Get<Rules>(module, RulesKey);
160197
ApplyRules(context, member, rules);
161-
}
162-
198+
}
199+
163200
/// <summary>
164201
/// Parses the rules' patterns.
165202
/// </summary>
@@ -189,8 +226,8 @@ protected Rules ParseRules(ConfuserProject proj, ProjectModule module, ConfuserC
189226
}
190227
}
191228
return ret;
192-
}
193-
229+
}
230+
194231
/// <summary>
195232
/// Applies the rules to the target definition.
196233
/// </summary>
@@ -218,4 +255,4 @@ protected void ApplyRules(ConfuserContext context, IDnlibDef target, Rules rules
218255
ProtectionParameters.SetParameters(context, target, ret);
219256
}
220257
}
221-
}
258+
}

Confuser.Core/ObfAttrMarker.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ ProtectionSettingsInfo AddRule(ObfuscationAttributeInfo attr, List<ProtectionSet
374374

375375
void MarkModule(ProjectModule projModule, ModuleDefMD module, Rules rules, bool isMain) {
376376
string snKeyPath = projModule.SNKeyPath, snKeyPass = projModule.SNKeyPassword;
377+
string snPubKeyPath = projModule.SNPubKeyPath;
378+
bool snDelaySig = projModule.SNDelaySig;
379+
string snSigKeyPath = projModule.SNSigKeyPath;
380+
string snPubSigKeyPath = projModule.SNPubSigKeyPath;
381+
377382
var stack = new ProtectionSettingsStack(context, protections);
378383

379384
var layer = new List<ProtectionSettingsInfo>();
@@ -425,8 +430,23 @@ void MarkModule(ProjectModule projModule, ModuleDefMD module, Rules rules, bool
425430
}
426431

427432
snKeyPath = snKeyPath == null ? null : Path.Combine(project.BaseDirectory, snKeyPath);
433+
snPubKeyPath = snPubKeyPath == null ? null : Path.Combine(project.BaseDirectory, snPubKeyPath);
434+
snSigKeyPath = snSigKeyPath == null ? null : Path.Combine(project.BaseDirectory, snSigKeyPath);
435+
snPubSigKeyPath = snPubSigKeyPath == null ? null : Path.Combine(project.BaseDirectory, snPubSigKeyPath);
436+
428437
var snKey = LoadSNKey(context, snKeyPath, snKeyPass);
429-
context.Annotations.Set(module, SNKey, snKey);
438+
context.Annotations.Set(module, SNKey, snKey);
439+
440+
var snPubKey = LoadSNPubKey(context, snPubKeyPath);
441+
context.Annotations.Set(module, SNPubKey, snPubKey);
442+
443+
context.Annotations.Set(module, SNDelaySig, snDelaySig);
444+
445+
var snSigKey = LoadSNKey(context, snSigKeyPath, snKeyPass);
446+
context.Annotations.Set(module, SNSigKey, snSigKey);
447+
448+
var snSigPubKey = LoadSNPubKey(context, snPubSigKeyPath);
449+
context.Annotations.Set(module, SNSigPubKey, snSigPubKey);
430450

431451
using (stack.Apply(module, layer))
432452
ProcessModule(module, stack);

Confuser.Core/Packer.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class Packer : ConfuserComponent {
2727
/// <param name="module">The stub module.</param>
2828
/// <param name="snKey">The strong name key.</param>
2929
/// <param name="prot">The packer protection that applies to the stub.</param>
30-
protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, Protection prot = null) {
30+
protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, StrongNamePublicKey snPubKey, StrongNameKey snSigKey, StrongNamePublicKey snPubSigKey, bool snDelaySig, Protection prot = null) {
3131
string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
3232
try {
3333
string outDir = Path.Combine(tmpDir, Path.GetRandomFileName());
@@ -75,7 +75,7 @@ protected void ProtectStub(ConfuserContext context, string fileName, byte[] modu
7575
new ConfuserParameters {
7676
Logger = new PackerLogger(context.Logger),
7777
PluginDiscovery = discovery,
78-
Marker = new PackerMarker(snKey),
78+
Marker = new PackerMarker(snKey, snPubKey, snDelaySig, snSigKey, snPubSigKey),
7979
Project = proj,
8080
PackerInitiated = true
8181
}, context.token).Wait();
@@ -165,15 +165,28 @@ public void Finish(bool successful) {
165165

166166
internal class PackerMarker : Marker {
167167
readonly StrongNameKey snKey;
168+
readonly StrongNamePublicKey snPubKey;
169+
readonly bool snDelaySig;
170+
readonly StrongNameKey snSigKey;
171+
readonly StrongNamePublicKey snPubSigKey;
168172

169-
public PackerMarker(StrongNameKey snKey) {
173+
public PackerMarker(StrongNameKey snKey, StrongNamePublicKey snPubKey, bool snDelaySig, StrongNameKey snSigKey, StrongNamePublicKey snPubSigKey) {
170174
this.snKey = snKey;
175+
this.snPubKey = snPubKey;
176+
this.snDelaySig = snDelaySig;
177+
this.snSigKey = snSigKey;
178+
this.snPubSigKey = snPubSigKey;
171179
}
172180

173181
protected internal override MarkerResult MarkProject(ConfuserProject proj, ConfuserContext context) {
174182
MarkerResult result = base.MarkProject(proj, context);
175-
foreach (ModuleDefMD module in result.Modules)
176-
context.Annotations.Set(module, SNKey, snKey);
183+
foreach (ModuleDefMD module in result.Modules) {
184+
context.Annotations.Set(module, SNKey, snKey);
185+
context.Annotations.Set(module, SNPubKey, snPubKey);
186+
context.Annotations.Set(module, SNDelaySig, snDelaySig);
187+
context.Annotations.Set(module, SNSigKey, snSigKey);
188+
context.Annotations.Set(module, SNSigPubKey, snPubSigKey);
189+
}
177190
return result;
178191
}
179192
}

Confuser.Core/Project/ConfuserPrj.xsd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@
5353
<xs:attribute name="path" type="xs:string" use="required" />
5454
<xs:attribute name="external" type="xs:boolean" default="false" />
5555
<xs:attribute name="snKey" type="xs:string" use="optional" />
56+
<xs:attribute name="snPubKey" type="xs:string" use="optional" />
57+
<xs:attribute name="snSigKey" type="xs:string" use="optional" />
58+
<xs:attribute name="snPubSigKey" type="xs:string" use="optional" />
5659
<xs:attribute name="snKeyPass" type="xs:string" use="optional" />
60+
<xs:attribute name="snDelaySig" type="xs:boolean" default="false" />
5761
</xs:complexType>
5862

5963

@@ -72,4 +76,4 @@
7276
<xs:attribute name="debug" type="xs:boolean" default="false" />
7377
</xs:complexType>
7478
</xs:element>
75-
</xs:schema>
79+
</xs:schema>

0 commit comments

Comments
 (0)