99using dnlib . DotNet ;
1010
1111namespace 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+ }
0 commit comments