-
Notifications
You must be signed in to change notification settings - Fork 68
/
SigningHelper.cs
333 lines (290 loc) · 12.4 KB
/
SigningHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Mono.Cecil;
namespace Brutal.Dev.StrongNameSigner
{
/// <summary>
/// Static helper class for easily getting assembly information and strong-name signing .NET assemblies.
/// </summary>
public static class SigningHelper
{
/// <summary>
/// Generates a 1024 bit the strong-name key pair that can be written to an SNK file.
/// </summary>
/// <returns>A strong-name key pair array.</returns>
public static byte[] GenerateStrongNameKeyPair()
{
using (var provider = new RSACryptoServiceProvider(1024, new CspParameters() { KeyNumber = 2 }))
{
return provider.ExportCspBlob(!provider.PublicOnly);
}
}
/// <summary>
/// Signs the assembly at the specified path.
/// </summary>
/// <param name="assemblyPath">The path to the assembly you want to strong-name sign.</param>
/// <returns>The assembly information of the new strong-name signed assembly.</returns>
public static AssemblyInfo SignAssembly(string assemblyPath)
{
return SignAssembly(assemblyPath, string.Empty, string.Empty, string.Empty);
}
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyPath">The path to the assembly you want to strong-name sign.</param>
/// <param name="keyPath">The path to the strong-name key file you want to use (.snk or.pfx).</param>
/// <returns>The assembly information of the new strong-name signed assembly.</returns>
public static AssemblyInfo SignAssembly(string assemblyPath, string keyPath)
{
return SignAssembly(assemblyPath, keyPath, string.Empty, string.Empty);
}
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyPath">The path to the assembly you want to strong-name sign.</param>
/// <param name="keyPath">The path to the strong-name key file you want to use (.snk or .pfx).</param>
/// <param name="outputPath">The directory path where the strong-name signed assembly will be copied to.</param>
/// <returns>The assembly information of the new strong-name signed assembly.</returns>
/// <exception cref="System.ArgumentNullException">
/// assemblyPath parameter was not provided.
/// </exception>
/// <exception cref="System.IO.FileNotFoundException">
/// Could not find provided assembly file.
/// or
/// Could not find provided strong-name key file file.
/// </exception>
/// <exception cref="System.BadImageFormatException">
/// The file is not a .NET managed assembly.
/// </exception>
public static AssemblyInfo SignAssembly(string assemblyPath, string keyPath, string outputPath)
{
return SignAssembly(assemblyPath, keyPath, outputPath, string.Empty);
}
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyPath">The path to the assembly you want to strong-name sign.</param>
/// <param name="keyPath">The path to the strong-name key file you want to use (.snk or .pfx).</param>
/// <param name="outputPath">The directory path where the strong-name signed assembly will be copied to.</param>
/// <param name="keyFilePassword">The password for the provided strong-name key file.</param>
/// <returns>The assembly information of the new strong-name signed assembly.</returns>
/// <exception cref="System.ArgumentNullException">
/// assemblyPath parameter was not provided.
/// </exception>
/// <exception cref="System.IO.FileNotFoundException">
/// Could not find provided assembly file.
/// or
/// Could not find provided strong-name key file file.
/// </exception>
/// <exception cref="System.BadImageFormatException">
/// The file is not a .NET managed assembly.
/// </exception>
public static AssemblyInfo SignAssembly(string assemblyPath, string keyPath, string outputPath, string keyFilePassword)
{
// Verify assembly path was passed in.
if (string.IsNullOrWhiteSpace(assemblyPath))
{
throw new ArgumentNullException("assemblyPath");
}
// Make sure the file actually exists.
if (!File.Exists(assemblyPath))
{
throw new FileNotFoundException("Could not find provided assembly file.", assemblyPath);
}
if (string.IsNullOrWhiteSpace(outputPath))
{
// Overwrite the file if no output path is provided.
outputPath = Path.GetDirectoryName(assemblyPath);
}
else
{
// Create the directory structure.
Directory.CreateDirectory(outputPath);
}
string outputFile = Path.Combine(Path.GetFullPath(outputPath), Path.GetFileName(assemblyPath));
// Get the assembly info and go from there.
AssemblyInfo info = GetAssemblyInfo(assemblyPath);
// Don't sign assemblies with a strong-name signature.
if (info.IsSigned)
{
if (!outputFile.Equals(Path.GetFullPath(assemblyPath), StringComparison.OrdinalIgnoreCase))
{
File.Copy(assemblyPath, outputFile, true);
}
return info;
}
if (outputFile.Equals(Path.GetFullPath(assemblyPath), StringComparison.OrdinalIgnoreCase))
{
// Make a backup before overwriting.
File.Copy(outputFile, outputFile + ".unsigned", true);
}
try
{
AssemblyDefinition.ReadAssembly(assemblyPath, GetReadParameters(assemblyPath))
.Write(outputFile, new WriterParameters() { StrongNameKeyPair = GetStrongNameKeyPair(keyPath, keyFilePassword) });
}
catch (Exception)
{
// Restore the backup if something goes wrong.
if (outputFile.Equals(Path.GetFullPath(assemblyPath), StringComparison.OrdinalIgnoreCase))
{
File.Copy(outputFile + ".unsigned", outputFile, true);
}
throw;
}
return GetAssemblyInfo(outputFile);
}
/// <summary>
/// Gets .NET assembly information.
/// </summary>
/// <param name="assemblyPath">The path to an assembly you want to get information from.</param>
/// <returns>The assembly information.</returns>
/// <exception cref="System.ArgumentNullException">
/// assemblyPath parameter was not provided.
/// </exception>
/// <exception cref="System.IO.FileNotFoundException">
/// Could not find provided assembly file.
/// </exception>
public static AssemblyInfo GetAssemblyInfo(string assemblyPath)
{
// Verify assembly path was passed in.
if (string.IsNullOrWhiteSpace(assemblyPath))
{
throw new ArgumentNullException("assemblyPath");
}
// Make sure the file actually exists.
if (!File.Exists(assemblyPath))
{
throw new FileNotFoundException("Could not find provided assembly file.", assemblyPath);
}
var a = AssemblyDefinition.ReadAssembly(assemblyPath, GetReadParameters(assemblyPath));
return new AssemblyInfo()
{
FilePath = Path.GetFullPath(assemblyPath),
DotNetVersion = GetDotNetVersion(a.MainModule.Runtime),
IsSigned = a.MainModule.Attributes.HasFlag(ModuleAttributes.StrongNameSigned),
IsManagedAssembly = a.MainModule.Attributes.HasFlag(ModuleAttributes.ILOnly),
Is64BitOnly = a.MainModule.Architecture == TargetArchitecture.AMD64 || a.MainModule.Architecture == TargetArchitecture.IA64,
Is32BitOnly = a.MainModule.Attributes.HasFlag(ModuleAttributes.Required32Bit) && !a.MainModule.Attributes.HasFlag(ModuleAttributes.Preferred32Bit),
Is32BitPreferred = a.MainModule.Attributes.HasFlag(ModuleAttributes.Preferred32Bit)
};
}
/// <summary>
/// Fixes an assembly reference.
/// </summary>
/// <param name="assemblyPath">The path to the assembly you want to fix a reference for.</param>
/// <param name="referenceAssemblyPath">The path to the reference assembly path you want to fix in the first assembly.</param>
/// <exception cref="System.ArgumentNullException">
/// assemblyPath was not provided.
/// or
/// referenceAssemblyPath was not provided.
/// </exception>
/// <exception cref="System.IO.FileNotFoundException">
/// Could not find provided assembly file.
/// or
/// Could not find provided reference assembly file.
/// </exception>
public static bool FixAssemblyReference(string assemblyPath, string referenceAssemblyPath)
{
// Verify assembly path was passed in.
if (string.IsNullOrWhiteSpace(assemblyPath))
{
throw new ArgumentNullException("assemblyPath");
}
if (string.IsNullOrWhiteSpace(referenceAssemblyPath))
{
throw new ArgumentNullException("referenceAssemblyPath");
}
// Make sure the file actually exists.
if (!File.Exists(assemblyPath))
{
throw new FileNotFoundException("Could not find provided assembly file.", assemblyPath);
}
if (!File.Exists(referenceAssemblyPath))
{
throw new FileNotFoundException("Could not find provided reference assembly file.", referenceAssemblyPath);
}
bool fixApplied = false;
var a = AssemblyDefinition.ReadAssembly(assemblyPath, GetReadParameters(assemblyPath));
var b = AssemblyDefinition.ReadAssembly(referenceAssemblyPath, GetReadParameters(referenceAssemblyPath));
var assemblyReference = a.MainModule.AssemblyReferences.FirstOrDefault(r => r.Name == b.Name.Name && r.Version == b.Name.Version);
if (assemblyReference != null)
{
// Found a matching reference, let's set the public key token.
if (BitConverter.ToString(assemblyReference.PublicKeyToken) != BitConverter.ToString(b.Name.PublicKeyToken))
{
assemblyReference.PublicKeyToken = b.Name.PublicKeyToken ?? new byte[0];
a.Write(assemblyPath);
fixApplied = true;
}
}
var friendReference = b.CustomAttributes.SingleOrDefault(attr => attr.AttributeType.FullName == typeof(InternalsVisibleToAttribute).FullName &&
attr.ConstructorArguments[0].Value.ToString() == a.Name.Name);
if (friendReference != null && a.Name.HasPublicKey)
{
// Add the public key to the attribute.
var typeRef = friendReference.ConstructorArguments[0].Type;
friendReference.ConstructorArguments.Clear();
friendReference.ConstructorArguments.Add(new CustomAttributeArgument(typeRef, a.Name.Name + ", PublicKey=" + BitConverter.ToString(a.Name.PublicKey).Replace("-", string.Empty)));
b.Write(referenceAssemblyPath);
fixApplied = true;
}
return fixApplied;
}
private static ReaderParameters GetReadParameters(string assemblyPath)
{
var resolver = new DefaultAssemblyResolver();
if (!string.IsNullOrEmpty(assemblyPath) && File.Exists(assemblyPath))
{
resolver.AddSearchDirectory(Path.GetDirectoryName(assemblyPath));
}
return new ReaderParameters() { AssemblyResolver = resolver };
}
private static string GetDotNetVersion(TargetRuntime runtime)
{
switch (runtime)
{
case TargetRuntime.Net_1_0:
return "1.0.3705";
case TargetRuntime.Net_1_1:
return "1.1.4322";
case TargetRuntime.Net_2_0:
return "2.0.50727";
case TargetRuntime.Net_4_0:
return "4.0.30319";
}
return "UNKNOWN";
}
private static StrongNameKeyPair GetStrongNameKeyPair(string keyPath, string keyFilePassword)
{
byte[] keyPairArray = null;
if (!string.IsNullOrEmpty(keyPath))
{
if (!string.IsNullOrEmpty(keyFilePassword))
{
var cert = new X509Certificate2(keyPath, keyFilePassword, X509KeyStorageFlags.Exportable);
var provider = cert.PrivateKey as RSACryptoServiceProvider;
if (provider == null)
{
throw new InvalidOperationException("The key file is not password protected or the incorrect password was provided.");
}
keyPairArray = provider.ExportCspBlob(true);
}
else
{
keyPairArray = File.ReadAllBytes(keyPath);
}
}
else
{
keyPairArray = GenerateStrongNameKeyPair();
}
return new StrongNameKeyPair(keyPairArray);
}
}
}