-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathSigningHelper.cs
More file actions
764 lines (653 loc) · 32.5 KB
/
SigningHelper.cs
File metadata and controls
764 lines (653 loc) · 32.5 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using Mono.Cecil;
using Mono.Security.Cryptography;
namespace Brutal.Dev.StrongNameSigner
{
/// <summary>
/// Static helper class for easily getting assembly information and strong-name signing .NET assemblies.
/// </summary>
public static class SigningHelper
{
private const string Size = @"[\u0080-\u00FF]{0,4}[\u0000-\u0079]";
private static readonly Regex BamlRegex = new(
@"(?<marker>\u001C)(?<totalsize>" + Size +
")(?<id>..)(?<size>" + Size +
@")(?<name>(?:\w+\.)*\w+), Version=(?<version>(?:\d+\.){3}\d+), Culture=(?<culture>(?:\w|\-)+), PublicKeyToken=(?<token>null|(?:\d|[abcdef]){16})",
RegexOptions.CultureInvariant | RegexOptions.Singleline);
private static byte[] keyPairCache;
/// <summary>
/// Provide a message logging method. If this is not set then the console will be used.
/// </summary>
public static Action<string> Log { get; set; }
/// <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) => 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="keyFilePath">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 keyFilePath) => SignAssembly(assemblyPath, keyFilePath, 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="keyFilePath">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.</exception>
/// <exception cref="System.BadImageFormatException">The file is not a .NET managed assembly.</exception>
public static AssemblyInfo SignAssembly(string assemblyPath, string keyFilePath, string outputPath) => SignAssembly(assemblyPath, keyFilePath, 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="keyFilePath">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>
/// <param name="probingPaths">Additional paths to probe for references.</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.</exception>
/// <exception cref="System.BadImageFormatException">The file is not a .NET managed assembly.</exception>
public static AssemblyInfo SignAssembly(string assemblyPath, string keyFilePath, string outputPath, string keyFilePassword, params string[] probingPaths)
{
// Verify assembly path was passed in.
if (string.IsNullOrWhiteSpace(assemblyPath))
{
throw new ArgumentNullException(nameof(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);
}
var outputFile = Path.Combine(Path.GetFullPath(outputPath), Path.GetFileName(assemblyPath));
SignAssemblies([new InputOutputFilePair(assemblyPath, outputFile)], keyFilePath, keyFilePassword, probingPaths);
return new AssemblyInfo(outputFile, probingPaths);
}
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyPaths">The paths to all the assemblies you want to strong-name sign and their references to fix.</param>
/// <exception cref="System.IO.FileNotFoundException">Could not find one of the provided assembly files.
/// or
/// Could not find provided strong-name key file.</exception>
/// <exception cref="System.BadImageFormatException">One or more files are not a .NET managed assemblies.</exception>
public static bool SignAssemblies(IEnumerable<string> assemblyPaths) => SignAssemblies(assemblyPaths, string.Empty, string.Empty);
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyPaths">The paths to all the assemblies you want to strong-name sign and their references to fix.</param>
/// <param name="keyFilePath">The path to the strong-name key file you want to use (.snk or .pfx).</param>
/// <exception cref="System.IO.FileNotFoundException">Could not find one of the provided assembly files.
/// or
/// Could not find provided strong-name key file.</exception>
/// <exception cref="System.BadImageFormatException">One or more files are not a .NET managed assemblies.</exception>
public static bool SignAssemblies(IEnumerable<string> assemblyPaths, string keyFilePath) => SignAssemblies(assemblyPaths, keyFilePath, string.Empty);
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyPaths">The paths to all the assemblies you want to strong-name sign and their references to fix.</param>
/// <param name="keyFilePath">The path to the strong-name key file you want to use (.snk or .pfx).</param>
/// <param name="keyFilePassword">The password for the provided strong-name key file.</param>
/// <param name="probingPaths">Additional paths to probe for references.</param>
/// <exception cref="System.IO.FileNotFoundException">Could not find one of the provided assembly files.
/// or
/// Could not find provided strong-name key file.</exception>
/// <exception cref="System.BadImageFormatException">One or more files are not a .NET managed assemblies.</exception>
public static bool SignAssemblies(IEnumerable<string> assemblyPaths, string keyFilePath, string keyFilePassword, params string[] probingPaths)
=> SignAssemblies(assemblyPaths.Select(path => new InputOutputFilePair(path, path)), keyFilePath, keyFilePassword, probingPaths);
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyInputOutputPaths">The input and output paths to all the assemblies you want to strong-name sign and their references to fix.</param>
/// <exception cref="System.IO.FileNotFoundException">Could not find one of the provided assembly files.
/// or
/// Could not find provided strong-name key file.</exception>
/// <exception cref="System.BadImageFormatException">One or more files are not a .NET managed assemblies.</exception>
public static bool SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInputOutputPaths) => SignAssemblies(assemblyInputOutputPaths, string.Empty, string.Empty);
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyInputOutputPaths">The input and output paths to all the assemblies you want to strong-name sign and their references to fix.</param>
/// <param name="keyFilePath">The path to the strong-name key file you want to use (.snk or .pfx).</param>
/// <exception cref="System.IO.FileNotFoundException">Could not find one of the provided assembly files.
/// or
/// Could not find provided strong-name key file.</exception>
/// <exception cref="System.BadImageFormatException">One or more files are not a .NET managed assemblies.</exception>
public static bool SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInputOutputPaths, string keyFilePath) => SignAssemblies(assemblyInputOutputPaths, keyFilePath, string.Empty);
/// <summary>
/// Signs the assembly at the specified path with your own strong-name key file.
/// </summary>
/// <param name="assemblyInputOutputPaths">The input and output paths to all the assemblies you want to strong-name sign and their references to fix.</param>
/// <param name="keyFilePath">The path to the strong-name key file you want to use (.snk or .pfx).</param>
/// <param name="keyFilePassword">The password for the provided strong-name key file.</param>
/// <param name="probingPaths">Additional paths to probe for references.</param>
/// <exception cref="System.IO.FileNotFoundException">Could not find one of the provided assembly files.
/// or
/// Could not find provided strong-name key file.</exception>
/// <exception cref="System.BadImageFormatException">One or more files are not a .NET managed assemblies.</exception>
public static bool SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInputOutputPaths, string keyFilePath, string keyFilePassword, params string[] probingPaths)
{
// If no logger has been set, just use the console.
Log ??= Console.WriteLine;
// Verify assembly paths were passed in.
if (assemblyInputOutputPaths?.Any() != true)
{
Log("No assembly paths were provided.");
return false;
}
var hasErrors = false;
var step = 1;
// Make sure the files actually exist.
foreach (var assemblyInputPath in assemblyInputOutputPaths.Select(aio => aio.InputFilePath))
{
if (!File.Exists(assemblyInputPath))
{
throw new FileNotFoundException($"Could not find provided input assembly file '{assemblyInputPath}'.", assemblyInputPath);
}
}
Log($"{step++}. Loading assemblies...");
// Convert all paths into AssemblyInfo objects.
var allAssemblies = new HashSet<AssemblyInfo>();
// File locking issues in Mono.Cecil are a real pain in the ass so let's create a working directory of files to process, then copy them back when we're done.
var tempFilePathToInputOutputFilePairMap = new Dictionary<string, InputOutputFilePair>();
var tempPath = Path.Combine(Path.GetTempPath(), "StrongNameSigner-" + Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
foreach (var inputOutputFilePair in assemblyInputOutputPaths)
{
try
{
var tempFilePath = Path.Combine(tempPath, $"{Path.GetFileNameWithoutExtension(inputOutputFilePair.InputFilePath)}.{Guid.NewGuid()}{Path.GetExtension(inputOutputFilePair.InputFilePath)}");
File.Copy(inputOutputFilePair.InputFilePath, tempFilePath, true);
if (inputOutputFilePair.HasSymbols)
{
File.Copy(inputOutputFilePair.InputPdbPath, Path.ChangeExtension(tempFilePath, ".pdb"), true);
}
tempFilePathToInputOutputFilePairMap.Add(tempFilePath, inputOutputFilePair);
allAssemblies.Add(new AssemblyInfo(tempFilePath, probingPaths));
}
catch (BadImageFormatException ex)
{
Log($" Unsupported assembly '{inputOutputFilePair.InputFilePath}': {ex.Message}");
hasErrors = true;
}
catch (Exception ex)
{
Log($" Failed to load assembly '{inputOutputFilePair.InputFilePath}': {ex.Message}");
hasErrors = true;
}
}
Log($"{step++}. Checking assembly references...");
try
{
// Start with assemblies that are not signed.
var assembliesToProcess = new HashSet<AssemblyInfo>(allAssemblies.Where(a => !a.IsSigned));
var keyPair = GetStrongNameKeyPair(keyFilePath, keyFilePassword);
var publicKey = GetPublicKey(keyPair);
var token = GetPublicKeyToken(publicKey);
// Add references that need to be updated and signed.
var set = new HashSet<string>(assembliesToProcess.Select(x => x.Definition.Name.Name));
foreach (var assembly in allAssemblies)
{
Log($" Checking assembly references in '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
foreach (var reference in assembly.Definition.MainModule.AssemblyReferences
.Where(reference => set.Contains(reference.Name)))
{
reference.PublicKey = publicKey;
assembliesToProcess.Add(assembly);
}
}
Log($"{step++}. Strong-name unsigned assemblies...");
// Strong-name sign all the unsigned assemblies.
foreach (var assembly in assembliesToProcess.Where(a => !a.IsSigned))
{
Log($" Signing assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
var name = assembly.Definition.Name;
name.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
name.PublicKey = publicKey;
name.HasPublicKey = true;
name.Attributes |= AssemblyAttributes.PublicKey;
assembly.Refresh();
}
Log($"{step++}. Fix InternalVisibleToAttribute references...");
// Fix InternalVisibleToAttribute.
foreach (var assembly in allAssemblies)
{
foreach (var internalVisibleArg in assembly.Definition.CustomAttributes
.Where(attr => attr.AttributeType.FullName == typeof(InternalsVisibleToAttribute).FullName && attr.HasConstructorArguments)
.Select(attr => new { Attribute = attr, Arguments = attr.ConstructorArguments })
.ToList())
{
var constructorArguments = internalVisibleArg.Arguments;
var argument = constructorArguments[0];
if (argument.Type == assembly.Definition.MainModule.TypeSystem.String)
{
var originalAssemblyName = (string)argument.Value;
var signedAssembly = assembliesToProcess.FirstOrDefault(a => a.Definition.Name.Name == originalAssemblyName);
if (signedAssembly is not null)
{
Log($" Fixing {signedAssembly.Definition.Name.Name} friend reference in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
var assemblyName = signedAssembly.Definition.Name.Name + ", PublicKey=" + BitConverter.ToString(signedAssembly.Definition.Name.PublicKey).Replace("-", string.Empty);
var updatedArgument = new CustomAttributeArgument(argument.Type, assemblyName);
constructorArguments.Clear();
constructorArguments.Add(updatedArgument);
}
else if (!originalAssemblyName.Contains("PublicKey"))
{
Log($" Removing invalid friend reference from assembly '{assembly.FilePath}'.");
assembly.Definition.CustomAttributes.Remove(internalVisibleArg.Attribute);
}
}
}
}
Log($"{step++}. Fix CustomAttributes with Type references...");
// Fix CustomAttributes with Type references.
FixCustomAttributes(allAssemblies, tempFilePathToInputOutputFilePairMap);
Log($"{step++}. Fix BAML references...");
// Fix BAML references.
foreach (var assembly in allAssemblies)
{
foreach (var resources in assembly.Definition.Modules.Select(module => module.Resources))
{
var resArray = resources.ToArray();
for (var resIndex = 0; resIndex < resArray.Length; resIndex++)
{
var resource = resArray[resIndex];
if (resource.ResourceType == ResourceType.Embedded)
{
if (!resource.Name.EndsWith(".g.resources"))
{
continue;
}
var embeddedResource = (EmbeddedResource)resource;
var modifyResource = false;
using var memoryStream = new MemoryStream();
using var writer = new ResourceWriter(memoryStream);
using var resourceStream = embeddedResource.GetResourceStream();
using var reader = new ResourceReader(resourceStream);
foreach (var entry in reader.OfType<DictionaryEntry>().ToArray())
{
var resourceName = entry.Key.ToString();
if (resourceName.EndsWith(".baml", StringComparison.InvariantCulture) && entry.Value is Stream bamlStream)
{
var br = new BinaryReader(bamlStream);
var dataBytes = br.ReadBytes((int)br.BaseStream.Length);
var charList = dataBytes.Select(b => (char)b).ToList();
var data = new string([.. charList]);
var elementsToReplace = new List<Match>();
foreach (Match match in BamlRegex.Matches(data))
{
var name = match.Groups["name"].Value;
if (assembliesToProcess.Any(x => x.Definition.Name.Name == name))
{
elementsToReplace.Add(match);
}
}
if (elementsToReplace.Count != 0)
{
assembliesToProcess.Add(assembly);
modifyResource = true;
FixBinaryBaml(token, writer, resourceName, charList, elementsToReplace);
}
else
{
bamlStream.Position = 0;
writer.AddResource(resourceName, bamlStream);
}
}
else
{
writer.AddResource(resourceName, entry.Value);
}
}
if (modifyResource)
{
Log($" Replacing BAML entry in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
resources.RemoveAt(resIndex);
writer.Generate();
var array = memoryStream.ToArray();
memoryStream.Position = 0;
var newEmbedded = new EmbeddedResource(resource.Name, resource.Attributes, array);
resources.Insert(resIndex, newEmbedded);
}
}
}
}
}
Log($"{step++}. Save assembly changes...");
// Write all updated assemblies.
foreach (var assembly in assembliesToProcess.Where(a => !a.Definition.Name.IsRetargetable))
{
var inputOutputFilePair = tempFilePathToInputOutputFilePairMap[assembly.FilePath];
if (inputOutputFilePair.IsSameFile)
{
try
{
File.Copy(inputOutputFilePair.InputFilePath, inputOutputFilePair.BackupAssemblyPath, true);
if (inputOutputFilePair.HasSymbols)
{
File.Copy(inputOutputFilePair.InputPdbPath, inputOutputFilePair.BackupPdbPath, true);
}
}
catch (IOException ioex)
{
Log($" Failed to backup assembly '{inputOutputFilePair.InputFilePath}': {ioex.Message}");
hasErrors = true;
}
}
Log($" Saving changes to assembly '{inputOutputFilePair.OutputFilePath}'.");
try
{
assembly.Save(inputOutputFilePair.OutputFilePath, keyPair);
}
catch (Exception ex)
{
Log($" Failed to save assembly '{inputOutputFilePair.OutputFilePath}': {ex.Message}");
hasErrors = true;
if (inputOutputFilePair.IsSameFile)
{
try
{
// Restore the backup that would have been created above.
File.Copy(inputOutputFilePair.BackupAssemblyPath, inputOutputFilePair.InputFilePath, true);
File.Delete(inputOutputFilePair.BackupAssemblyPath);
}
catch (IOException ioex)
{
Log($" Failed to restore assembly '{inputOutputFilePair.InputFilePath} from backup '{inputOutputFilePair.BackupAssemblyPath}': {ioex.Message}");
}
if (inputOutputFilePair.HasSymbols)
{
try
{
File.Copy(inputOutputFilePair.BackupPdbPath, inputOutputFilePair.InputPdbPath, true);
File.Delete(inputOutputFilePair.BackupPdbPath);
}
catch (IOException ioex)
{
Log($" Failed to restore PDB '{inputOutputFilePair.InputPdbPath} from backup '{inputOutputFilePair.BackupPdbPath}': {ioex.Message}");
}
}
}
}
finally
{
assembly.Dispose();
}
}
}
finally
{
Log($"{step++}. Cleanup...");
tempFilePathToInputOutputFilePairMap.Clear();
foreach (var assembly in allAssemblies)
{
assembly.Dispose();
}
try
{
Directory.Delete(tempPath, true);
}
catch (IOException ioex)
{
Log($" Failed to delete temp working directory '{tempPath}': {ioex.Message}");
hasErrors = true;
}
}
return !hasErrors;
}
private static void FixCustomAttributes(HashSet<AssemblyInfo> allAssemblies, Dictionary<string, InputOutputFilePair> tempFilePathToInputOutputFilePairMap)
{
var assembliesByName = new Dictionary<string, List<AssemblyInfo>>();
foreach (var assembly in allAssemblies)
{
if (!assembliesByName.TryGetValue(assembly.Definition.Name.Name, out var value))
{
assembliesByName.Add(assembly.Definition.Name.Name, [assembly]);
}
else
{
value.Add(assembly);
}
}
foreach (var assembly in allAssemblies)
{
var types = assembly.Definition.Modules
.SelectMany(m => m.GetTypes())
.ToList();
var methods = types
.SelectMany(t => t.Methods)
.ToList();
// Assembly-level custom attributes
FixAttributes(assembly.Definition.CustomAttributes, assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
// Module-level custom attributes
FixAttributes(assembly.Definition.Modules
.Where(m => m.HasCustomAttributes)
.SelectMany(m => m.CustomAttributes), assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
// Type-level custom attributes
FixAttributes(types
.Where(t => t.HasCustomAttributes)
.SelectMany(t => t.CustomAttributes), assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
// Method-level custom attributes
FixAttributes(methods
.Where(m => m.HasCustomAttributes)
.SelectMany(m => m.CustomAttributes), assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
// Parameter-level custom attributes
FixAttributes(methods
.SelectMany(m => m.Parameters)
.Where(p => p.HasCustomAttributes)
.SelectMany(p => p.CustomAttributes), assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
// Method return type custom attributes
FixAttributes(methods
.Where(m => m.MethodReturnType.HasCustomAttributes)
.SelectMany(m => m.MethodReturnType.CustomAttributes), assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
// Field-level custom attributes
FixAttributes(types
.SelectMany(t => t.Fields)
.Where(f => f.HasCustomAttributes)
.SelectMany(f => f.CustomAttributes), assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
// Event-level custom attributes
FixAttributes(types
.SelectMany(t => t.Events)
.Where(e => e.HasCustomAttributes)
.SelectMany(e => e.CustomAttributes), assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
// Property-level custom attributes
FixAttributes(types
.SelectMany(t => t.Properties)
.Where(p => p.HasCustomAttributes)
.SelectMany(p => p.CustomAttributes), assembly, tempFilePathToInputOutputFilePairMap, assembliesByName);
}
}
private static void FixAttributes(
IEnumerable<CustomAttribute> customAttributes,
AssemblyInfo assembly,
Dictionary<string, InputOutputFilePair> tempFilePathToInputOutputFilePairMap,
Dictionary<string, List<AssemblyInfo>> assembliesByName)
{
foreach (var customAttribute in customAttributes)
{
var hasConstructorArguments = false;
try
{
hasConstructorArguments = customAttribute.HasConstructorArguments;
}
catch (AssemblyResolutionException ex)
{
Log($" Failed to check custom attribute '{customAttribute.AttributeType.FullName}' in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}': {ex.Message}");
}
if (hasConstructorArguments)
{
foreach (var argument in customAttribute.ConstructorArguments.ToArray())
{
if (argument.Type.FullName == "System.Type" && argument.Value is TypeReference typeRef && assembliesByName.TryGetValue(typeRef.Scope.Name, out var value))
{
foreach (var signedAssembly in value.Select(sa => sa.Definition))
{
Log($" Fixing {signedAssembly.Name.Name} reference in CustomAttribute in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
// Import the type reference into the current module, so it gets the correct scope.
// Without this import, the type reference in the ILASM will only point to the type (like "Brutal.Dev.StrongNameSigner.TestAssembly.A.A")
// instead of the full assembly-qualified name (like "Brutal.Dev.StrongNameSigner.TestAssembly.A.A, Brutal.Dev.StrongNameSigner.TestAssembly.A, Version=1.0.0.0, PublicKeyToken=...").
var importedTypeRef = assembly.Definition.MainModule.ImportReference(signedAssembly.MainModule.GetType(typeRef.FullName));
var updatedArgument = new CustomAttributeArgument(argument.Type, importedTypeRef);
var idx = customAttribute.ConstructorArguments.IndexOf(argument);
// Skip is not found (already removed).
if (idx >= 0)
{
customAttribute.ConstructorArguments.RemoveAt(idx);
customAttribute.ConstructorArguments.Insert(idx, updatedArgument);
}
}
}
}
}
}
}
private static byte[] GenerateStrongNameKeyPair()
{
using var provider = new RSACryptoServiceProvider(4096);
return provider.ExportCspBlob(!provider.PublicOnly);
}
private static byte[] GetStrongNameKeyPair(string keyFilePath, string keyFilePassword)
{
if (!string.IsNullOrEmpty(keyFilePath))
{
if (!string.IsNullOrEmpty(keyFilePassword))
{
var cert = new X509Certificate2(keyFilePath, keyFilePassword, X509KeyStorageFlags.Exportable);
if (cert.PrivateKey is not RSACryptoServiceProvider provider)
{
throw new InvalidOperationException("The key file is not password protected or the incorrect password was provided.");
}
return provider.ExportCspBlob(true);
}
else
{
return File.ReadAllBytes(keyFilePath);
}
}
else
{
// Only cache generated keys so all signed assemblies use the same public key.
if (keyPairCache is not null)
{
return keyPairCache;
}
keyPairCache = GenerateStrongNameKeyPair();
return keyPairCache;
}
}
// https://github.com/atykhyy/cecil/master/Mono.Security.Cryptography/CryptoService.cs
private static byte[] GetPublicKey(byte[] keyBlob)
{
using var rsa = CryptoConvert.FromCapiKeyBlob(keyBlob);
var cspBlob = CryptoConvert.ToCapiPublicKeyBlob(rsa);
var publicKey = new byte[12 + cspBlob.Length];
Buffer.BlockCopy(cspBlob, 0, publicKey, 12, cspBlob.Length);
// The first 12 bytes are documented at:
// http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
// ALG_ID - Signature
publicKey[1] = 36;
// ALG_ID - Hash
publicKey[4] = 4;
publicKey[5] = 128;
// Length of Public Key (in bytes)
publicKey[8] = (byte)(cspBlob.Length >> 0);
publicKey[9] = (byte)(cspBlob.Length >> 8);
publicKey[10] = (byte)(cspBlob.Length >> 16);
publicKey[11] = (byte)(cspBlob.Length >> 24);
return publicKey;
}
private static string GetPublicKeyToken(byte[] publicKey)
{
using var csp = new SHA1CryptoServiceProvider();
var hash = csp.ComputeHash(publicKey);
var token = new byte[8];
for (var i = 0; i < 8; i++)
{
token[i] = hash[hash.Length - (i + 1)];
}
return string.Concat(token.Select(x => x.ToString("x2")));
}
private static void FixBinaryBaml(string publicKeyToken, ResourceWriter rw, string resourceName, List<char> charList, List<Match> elementsToReplace)
{
elementsToReplace = [.. elementsToReplace.OrderBy(x => x.Index)];
using var buffer = new MemoryStream();
using var bufferWriter = new BinaryWriter(buffer);
for (var i = 0; i < charList.Count; i++)
{
if (elementsToReplace.Count > 0 && elementsToReplace[0].Index == i)
{
var match = elementsToReplace[0];
bufferWriter.Write((byte)0x1C);
var newAssembly =
string.Format(
"{0}, Version={1}, Culture={2}, PublicKeyToken={3}",
match.Groups["name"].Value,
match.Groups["version"].Value,
match.Groups["culture"].Value,
publicKeyToken);
var length = Get7BitEncoded(newAssembly.Length).Length + newAssembly.Length + 3;
var totalLength = Get7BitEncoded(length);
bufferWriter.Write(totalLength);
var id = match.Groups["id"].Value;
bufferWriter.Write((byte)id[0]);
bufferWriter.Write((byte)id[1]);
bufferWriter.Write(newAssembly);
#pragma warning disable S127 // "for" loop stop conditions should be invariant
i += match.Length - 1;
#pragma warning restore S127 // "for" loop stop conditions should be invariant
elementsToReplace.RemoveAt(0);
}
else
{
var b = (byte)charList[i];
bufferWriter.Write(b);
}
}
bufferWriter.Flush();
var mst = new MemoryStream(buffer.ToArray());
rw.AddResource(resourceName, mst);
}
private static byte[] Get7BitEncoded(int value)
{
var list = new List<byte>();
var num = (uint)value;
while (num >= 128U)
{
list.Add((byte)(num | 128U));
num >>= 7;
}
list.Add((byte)num);
return [.. list];
}
}
}