-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
SecurityUtil.cs
798 lines (712 loc) · 41.6 KB
/
SecurityUtil.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
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Deployment.Internal.CodeSigning;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Permissions;
using System.Security.Policy;
using System.Text;
using System.Xml;
using Microsoft.Build.Shared.FileSystem;
using FrameworkNameVersioning = System.Runtime.Versioning.FrameworkName;
namespace Microsoft.Build.Tasks.Deployment.ManifestUtilities
{
/// <summary>
/// Provides a set of utility functions for manipulating security permision sets and signing.
/// </summary>
[ComVisible(false)]
public static class SecurityUtilities
{
private const string PermissionSetsFolder = "PermissionSets";
private const string LocalIntranet = "LocalIntranet";
private const string Internet = "Internet";
private const string Custom = "Custom";
private const string ToolName = "signtool.exe";
private const int Fx2MajorVersion = 2;
private const int Fx3MajorVersion = 3;
private static readonly Version s_dotNet40Version = new Version("4.0");
private static readonly Version s_dotNet45Version = new Version("4.5");
private const string InternetPermissionSetXml = "<PermissionSet class=\"System.Security.PermissionSet\" version=\"1\" ID=\"Custom\" SameSite=\"site\">\n" +
"<IPermission class=\"System.Security.Permissions.FileDialogPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Access=\"Open\" />\n" +
"<IPermission class=\"System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Allowed=\"ApplicationIsolationByUser\" UserQuota=\"512000\" />\n" +
"<IPermission class=\"System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Flags=\"Execution\" />\n" +
"<IPermission class=\"System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Window=\"SafeTopLevelWindows\" Clipboard=\"OwnClipboard\" />\n" +
"<IPermission class=\"System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" version=\"1\" Level=\"SafePrinting\" />\n" +
"</PermissionSet>";
private const string LocalIntranetPermissionSetXml = "<PermissionSet class=\"System.Security.PermissionSet\" version=\"1\" ID=\"Custom\" SameSite=\"site\">\n" +
"<IPermission class=\"System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Read=\"USERNAME\" />\n" +
"<IPermission class=\"System.Security.Permissions.FileDialogPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Unrestricted=\"true\" />\n" +
"<IPermission class=\"System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Allowed=\"AssemblyIsolationByUser\" UserQuota=\"9223372036854775807\" Expiry=\"9223372036854775807\" Permanent=\"True\" />\n" +
"<IPermission class=\"System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Flags=\"ReflectionEmit\" />\n" +
"<IPermission class=\"System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Flags=\"Assertion, Execution, BindingRedirects\" />\n" +
"<IPermission class=\"System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Unrestricted=\"true\" />\n" +
"<IPermission class=\"System.Net.DnsPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Unrestricted=\"true\" />\n" +
"<IPermission class=\"System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" version=\"1\" Level=\"DefaultPrinting\" />\n" +
"</PermissionSet>";
private const string InternetPermissionSetWithWPFXml = "<PermissionSet class=\"System.Security.PermissionSet\" version=\"1\" ID=\"Custom\" SameSite=\"site\">\n" +
"<IPermission class=\"System.Security.Permissions.FileDialogPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Access=\"Open\" />\n" +
"<IPermission class=\"System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Allowed=\"ApplicationIsolationByUser\" UserQuota=\"512000\" />\n" +
"<IPermission class=\"System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Flags=\"Execution\" />\n" +
"<IPermission class=\"System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Window=\"SafeTopLevelWindows\" Clipboard=\"OwnClipboard\" />\n" +
"<IPermission class=\"System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" version=\"1\" Level=\"SafePrinting\" />\n" +
"<IPermission class=\"System.Security.Permissions.MediaPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\" version=\"1\" Audio=\"SafeAudio\" Video=\"SafeVideo\" Image=\"SafeImage\" />\n" +
"<IPermission class=\"System.Security.Permissions.WebBrowserPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\" version=\"1\" Level=\"Safe\" />\n" +
"</PermissionSet>";
private const string LocalIntranetPermissionSetWithWPFXml = "<PermissionSet class=\"System.Security.PermissionSet\" version=\"1\" ID=\"Custom\" SameSite=\"site\">\n" +
"<IPermission class=\"System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Read=\"USERNAME\" />\n" +
"<IPermission class=\"System.Security.Permissions.FileDialogPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Unrestricted=\"true\" />\n" +
"<IPermission class=\"System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Allowed=\"AssemblyIsolationByUser\" UserQuota=\"9223372036854775807\" Expiry=\"9223372036854775807\" Permanent=\"True\" />\n" +
"<IPermission class=\"System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Flags=\"ReflectionEmit\" />\n" +
"<IPermission class=\"System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Flags=\"Assertion, Execution, BindingRedirects\" />\n" +
"<IPermission class=\"System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Unrestricted=\"true\" />\n" +
"<IPermission class=\"System.Net.DnsPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Unrestricted=\"true\" />\n" +
"<IPermission class=\"System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" version=\"1\" Level=\"DefaultPrinting\" />\n" +
"<IPermission class=\"System.Security.Permissions.MediaPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\" version=\"1\" Audio=\"SafeAudio\" Video=\"SafeVideo\" Image=\"SafeImage\" />\n" +
"<IPermission class=\"System.Security.Permissions.WebBrowserPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\" version=\"1\" Level=\"Safe\" />\n" +
"</PermissionSet>";
/// <summary>
/// Generates a permission set by computed the zone default permission set and adding any included permissions.
/// </summary>
/// <param name="targetZone">Specifies a zone default permission set, which is obtained from machine policy. Valid values are "Internet", "LocalIntranet", or "Custom". If "Custom" is specified, the generated permission set is based only on the includedPermissionSet parameter.</param>
/// <param name="includedPermissionSet">A PermissionSet object containing the set of permissions to be explicitly included in the generated permission set. Permissions specified in this parameter will be included verbatim in the generated permission set, regardless of targetZone parameter.</param>
/// <param name="excludedPermissions">This property is no longer used.</param>
/// <returns>The generated permission set.</returns>
public static PermissionSet ComputeZonePermissionSet(string targetZone, PermissionSet includedPermissionSet, string[] excludedPermissions)
{
return ComputeZonePermissionSetHelper(targetZone, includedPermissionSet, null, string.Empty);
}
internal static PermissionSet ComputeZonePermissionSetHelper(string targetZone, PermissionSet includedPermissionSet, ITaskItem[] dependencies, string targetFrameworkMoniker)
{
// Custom Set.
if (String.IsNullOrEmpty(targetZone) || String.Equals(targetZone, Custom, StringComparison.OrdinalIgnoreCase))
{
// just return the included set, no magic
return includedPermissionSet.Copy();
}
PermissionSet retSet = GetNamedPermissionSetFromZone(targetZone, targetFrameworkMoniker);
return retSet;
}
private static PermissionSet GetNamedPermissionSetFromZone(string targetZone, string targetFrameworkMoniker)
{
switch (targetZone)
{
case LocalIntranet:
return GetNamedPermissionSet(LocalIntranet, targetFrameworkMoniker);
case Internet:
return GetNamedPermissionSet(Internet, targetFrameworkMoniker);
default:
throw new ArgumentException(String.Empty /* no message */, nameof(targetZone));
}
}
private static PermissionSet GetNamedPermissionSet(string targetZone, string targetFrameworkMoniker)
{
FrameworkNameVersioning fn;
if (!string.IsNullOrEmpty(targetFrameworkMoniker))
{
fn = new FrameworkNameVersioning(targetFrameworkMoniker);
}
else
{
fn = new FrameworkNameVersioning(".NETFramework", s_dotNet40Version);
}
int majorVersion = fn.Version.Major;
if (majorVersion == Fx2MajorVersion)
{
return XmlToPermissionSet((GetXmlElement(targetZone, majorVersion)));
}
else if (majorVersion == Fx3MajorVersion)
{
return XmlToPermissionSet((GetXmlElement(targetZone, majorVersion)));
}
else
{
return XmlToPermissionSet((GetXmlElement(targetZone, fn)));
}
}
private static XmlElement GetXmlElement(string targetZone, FrameworkNameVersioning fn)
{
IList<string> paths = ToolLocationHelper.GetPathToReferenceAssemblies(fn);
// Is the targeted CLR even installed?
if (paths.Count > 0)
{
// first one is always framework requested.
string path = Path.Combine(paths[0], PermissionSetsFolder);
// PermissionSets folder doesn't exit
if (FileSystems.Default.DirectoryExists(path))
{
string[] files = Directory.GetFiles(path, "*.xml");
var filesInfo = new FileInfo[files.Length];
int indexFound = -1;
// trim the extension.
for (int i = 0; i < files.Length; i++)
{
filesInfo[i] = new FileInfo(files[i]);
string fileInfoNoExt = Path.GetFileNameWithoutExtension(files[i]);
if (string.Equals(fileInfoNoExt, targetZone, StringComparison.OrdinalIgnoreCase))
{
indexFound = i;
break;
}
}
if (indexFound != -1)
{
FileInfo resultFile = filesInfo[indexFound];
using (FileStream fs = resultFile.OpenRead())
{
try
{
var sr = new StreamReader(fs);
string data = sr.ReadToEnd();
if (!string.IsNullOrEmpty(data))
{
var doc = new XmlDocument();
var xrSettings =
new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Ignore,
ConformanceLevel = ConformanceLevel.Auto
};
// http://msdn.microsoft.com/en-us/library/h2344bs2(v=vs.110).aspx
// PermissionSets do not conform to document level, which is the default setting.
try
{
fs.Position = 0; // Reset to 0 before using this stream in any other reader.
using (XmlReader xr = XmlReader.Create(fs, xrSettings))
{
doc.Load(xr);
return doc.DocumentElement;
}
}
catch (Exception)
{
//continue.
}
}
}
catch (ArgumentException)
{
//continue.
}
}
}
}
}
return GetCurrentCLRPermissions(targetZone);
}
[SuppressMessage("Microsoft.Security.Xml", "CA3057: DoNotUseLoadXml.")]
private static XmlElement GetCurrentCLRPermissions(string targetZone)
{
SecurityZone zone;
switch (targetZone)
{
case LocalIntranet:
zone = SecurityZone.Intranet;
break;
case Internet:
zone = SecurityZone.Internet;
break;
default:
throw new ArgumentException(String.Empty /* no message */, nameof(targetZone));
}
var evidence = new Evidence(new EvidenceBase[] { new Zone(zone), new System.Runtime.Hosting.ActivationArguments(new System.ApplicationIdentity("")) }, null);
PermissionSet sandbox = SecurityManager.GetStandardSandbox(evidence);
string resultInString = sandbox.ToString();
if (!string.IsNullOrEmpty(resultInString))
{
var doc = new XmlDocument();
// CA3057: DoNotUseLoadXml. Suppressed since the xml being loaded is a string representation of the PermissionSet.
doc.LoadXml(resultInString);
return doc.DocumentElement;
}
return null;
}
private static XmlElement GetXmlElement(string targetZone, int majorVersion)
{
XmlDocument doc;
switch (majorVersion)
{
case Fx2MajorVersion:
doc = CreateXmlDocV2(targetZone);
break;
case Fx3MajorVersion:
doc = CreateXmlDocV3(targetZone);
break;
default:
throw new ArgumentException(String.Empty /* no message */, nameof(majorVersion));
}
XmlElement rootElement = doc.DocumentElement;
return rootElement;
}
[SuppressMessage("Microsoft.Security.Xml", "CA3057: DoNotUseLoadXml.")]
private static XmlDocument CreateXmlDocV2(string targetZone)
{
var doc = new XmlDocument();
switch (targetZone)
{
case LocalIntranet:
// CA3057: DoNotUseLoadXml. Suppressed since is LocalIntranetPermissionSetXml a constant string.
doc.LoadXml(LocalIntranetPermissionSetXml);
return doc;
case Internet:
// CA3057: DoNotUseLoadXml. Suppressed since is InternetPermissionSetXml a constant string.
doc.LoadXml(InternetPermissionSetXml);
return doc;
default:
throw new ArgumentException(String.Empty /* no message */, nameof(targetZone));
}
}
[SuppressMessage("Microsoft.Security.Xml", "CA3057: DoNotUseLoadXml.")]
private static XmlDocument CreateXmlDocV3(string targetZone)
{
var doc = new XmlDocument();
switch (targetZone)
{
case LocalIntranet:
// CA3057: DoNotUseLoadXml. Suppressed since is LocalIntranetPermissionSetXml a constant string.
doc.LoadXml(LocalIntranetPermissionSetWithWPFXml);
return doc;
case Internet:
// CA3057: DoNotUseLoadXml. Suppressed since is InternetPermissionSetXml a constant string.
doc.LoadXml(InternetPermissionSetWithWPFXml);
return doc;
default:
throw new ArgumentException(String.Empty /* no message */, nameof(targetZone));
}
}
internal static bool ParseElementForAssemblyIdentification(SecurityElement el,
out String className,
out String assemblyName, // for example "WindowsBase"
out String assemblyVersion)
{
className = null;
assemblyName = null;
assemblyVersion = null;
String fullClassName = el.Attribute("class");
if (fullClassName == null)
{
return false;
}
if (fullClassName.IndexOf('\'') >= 0)
{
fullClassName = fullClassName.Replace('\'', '\"');
}
int commaIndex = fullClassName.IndexOf(',');
// If the classname is tagged with assembly information, find where
// the assembly information begins.
if (commaIndex == -1)
{
return false;
}
int namespaceClassNameLength = commaIndex;
className = fullClassName.Substring(0, namespaceClassNameLength);
String assemblyFullName = fullClassName.Substring(commaIndex + 1);
var an = new AssemblyName(assemblyFullName);
assemblyName = an.Name;
assemblyVersion = an.Version.ToString();
return true;
}
/// <summary>
/// Converts an array of permission identity strings to a permission set object.
/// </summary>
/// <param name="ids">An array of permission identity strings.</param>
/// <returns>The converted permission set.</returns>
public static PermissionSet IdentityListToPermissionSet(string[] ids)
{
var document = new XmlDocument();
XmlElement permissionSetElement = document.CreateElement("PermissionSet");
document.AppendChild(permissionSetElement);
foreach (string id in ids)
{
XmlElement permissionElement = document.CreateElement("IPermission");
XmlAttribute a = document.CreateAttribute("class");
a.Value = id;
permissionElement.Attributes.Append(a);
permissionSetElement.AppendChild(permissionElement);
}
return XmlToPermissionSet(permissionSetElement);
}
/// <summary>
/// Converts a permission set object to an array of permission identity strings.
/// </summary>
/// <param name="permissionSet">The input permission set to be converted.</param>
/// <returns>An array of permission identity strings.</returns>
[SuppressMessage("Microsoft.Security.Xml", "CA3057: DoNotUseLoadXml.")]
public static string[] PermissionSetToIdentityList(PermissionSet permissionSet)
{
string psXml = permissionSet?.ToString() ?? "<PermissionSet/>";
var psDocument = new XmlDocument();
// CA3057: DoNotUseLoadXml. Suppressed since 'psXml' is a trusted or a constant string.
psDocument.LoadXml(psXml);
return XmlToIdentityList(psDocument.DocumentElement);
}
[SuppressMessage("Microsoft.Security.Xml", "CA3057: DoNotUseLoadXml.")]
internal static XmlDocument PermissionSetToXml(PermissionSet ps)
{
XmlDocument inputDocument = new XmlDocument();
string xml = ps?.ToString() ?? "<PermissionSet/>";
// CA3057: DoNotUseLoadXml. Suppressed since 'xml' is a trusted or a constant string.
inputDocument.LoadXml(xml);
var outputDocument = new XmlDocument();
XmlElement psElement = XmlUtil.CloneElementToDocument(inputDocument.DocumentElement, outputDocument, XmlNamespaces.asmv2);
outputDocument.AppendChild(psElement);
return outputDocument;
}
private static SecurityElement XmlElementToSecurityElement(XmlElement xe)
{
SecurityElement se = new SecurityElement(xe.Name);
foreach (XmlAttribute xa in xe.Attributes)
se.AddAttribute(xa.Name, xa.Value);
foreach (XmlNode xn in xe.ChildNodes)
if (xn.NodeType == XmlNodeType.Element)
se.AddChild(XmlElementToSecurityElement((XmlElement)xn));
return se;
}
private static string[] XmlToIdentityList(XmlElement psElement)
{
XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(psElement.OwnerDocument.NameTable);
XmlNodeList nodes = psElement.SelectNodes(XPaths.permissionClassAttributeQuery, nsmgr);
if (nodes == null || nodes.Count == 0)
nodes = psElement.SelectNodes(XmlUtil.TrimPrefix(XPaths.permissionClassAttributeQuery));
string[] a;
if (nodes != null)
{
a = new string[nodes.Count];
int i = 0;
foreach (XmlNode node in nodes)
{
a[i++] = node.Value;
}
}
else
{
a = Array.Empty<string>();
}
return a;
}
/// <summary>
/// Converts an XmlElement into a PermissionSet object.
/// </summary>
/// <param name="element">An XML representation of the permission set.</param>
/// <returns>The converted permission set.</returns>
public static PermissionSet XmlToPermissionSet(XmlElement element)
{
if (element == null)
{
return null;
}
SecurityElement se = XmlElementToSecurityElement(element);
if (se == null)
{
return null;
}
PermissionSet ps = new PermissionSet(PermissionState.None);
try
{
ps = new ReadOnlyPermissionSet(se);
}
catch (ArgumentException ex)
{
//UNDONE: Need to log exception thrown from PermissionSet.FromXml
Debug.Fail(String.Format(CultureInfo.CurrentCulture, "PermissionSet.FromXml failed: {0}\r\n\r\n{1}", ex.Message, element.OuterXml));
return null;
}
return ps;
}
/// <summary>
/// Signs a ClickOnce manifest or PE file.
/// </summary>
/// <param name="certThumbprint">Hexadecimal string that contains the SHA-1 hash of the certificate.</param>
/// <param name="timestampUrl">URL that specifies an address of a time stamping server.</param>
/// <param name="path">Path of the file to sign with the certificate.</param>
public static void SignFile(string certThumbprint, Uri timestampUrl, string path)
{
SignFile(certThumbprint, timestampUrl, path, null);
}
/// <summary>
/// Signs a ClickOnce manifest or PE file.
/// </summary>
/// <param name="certThumbprint">Hexadecimal string that contains the SHA-1 hash of the certificate.</param>
/// <param name="timestampUrl">URL that specifies an address of a time stamping server.</param>
/// <param name="path">Path of the file to sign with the certificate.</param>
/// <param name="targetFrameworkVersion">Version of the .NET Framework for the target.</param>
public static void SignFile(string certThumbprint, Uri timestampUrl, string path, string targetFrameworkVersion)
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager("Microsoft.Build.Tasks.Core.Strings.ManifestUtilities", typeof(SecurityUtilities).Module.Assembly);
if (String.IsNullOrEmpty(certThumbprint))
{
throw new ArgumentNullException(nameof(certThumbprint));
}
X509Certificate2 cert = GetCert(certThumbprint);
if (cert == null)
{
throw new ArgumentException(resources.GetString("CertNotInStore"), nameof(certThumbprint));
}
if (!String.IsNullOrEmpty(targetFrameworkVersion))
{
Version targetVersion = Util.GetTargetFrameworkVersion(targetFrameworkVersion);
if (targetVersion == null)
{
throw new ArgumentException("TargetFrameworkVersion");
}
// SHA-256 digest can be parsed only with .NET 4.5 or higher.
bool isTargetFrameworkSha256Supported = targetVersion.CompareTo(s_dotNet45Version) >= 0;
SignFileInternal(cert, timestampUrl, path, isTargetFrameworkSha256Supported, resources);
}
else
{
SignFile(cert, timestampUrl, path);
}
}
// We need to refactor these functions to handle real sign tool
/// <summary>
/// Signs a ClickOnce manifest.
/// </summary>
/// <param name="certPath">The certificate to be used to sign the file.</param>
/// <param name="certPassword">The certificate password.</param>
/// <param name="timestampUrl">URL that specifies an address of a time stamping server.</param>
/// <param name="path">Path of the file to sign with the certificate.</param>
/// <remarks>This function is only for signing a manifest, not a PE file.</remarks>
public static void SignFile(string certPath, SecureString certPassword, Uri timestampUrl, string path)
{
X509Certificate2 cert = new X509Certificate2(certPath, certPassword, X509KeyStorageFlags.PersistKeySet);
SignFile(cert, timestampUrl, path);
}
private static bool UseSha256Algorithm(X509Certificate2 cert)
{
Oid oid = cert.SignatureAlgorithm;
return string.Equals(oid.FriendlyName, "sha256RSA", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Signs a ClickOnce manifest or PE file.
/// </summary>
/// <param name="cert">The certificate to be used to sign the file.</param>
/// <param name="timestampUrl">URL that specifies an address of a time stamping server.</param>
/// <param name="path">Path of the file to sign with the certificate.</param>
/// <remarks>This function can only sign a PE file if the X509Certificate2 parameter represents a certificate in the
/// current user's personal certificate store.</remarks>
public static void SignFile(X509Certificate2 cert, Uri timestampUrl, string path)
{
// setup resources
System.Resources.ResourceManager resources = new System.Resources.ResourceManager("Microsoft.Build.Tasks.Core.Strings.ManifestUtilities", typeof(SecurityUtilities).Module.Assembly);
SignFileInternal(cert, timestampUrl, path, true, resources);
}
private static void SignFileInternal(X509Certificate2 cert, Uri timestampUrl, string path, bool targetFrameworkSupportsSha256, System.Resources.ResourceManager resources)
{
if (cert == null)
{
throw new ArgumentNullException(nameof(cert));
}
if (String.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
if (!FileSystems.Default.FileExists(path))
{
throw new FileNotFoundException(String.Format(CultureInfo.InvariantCulture, resources.GetString("SecurityUtil.SignTargetNotFound"), path), path);
}
bool useSha256 = UseSha256Algorithm(cert) && targetFrameworkSupportsSha256;
if (PathUtil.IsPEFile(path))
{
if (IsCertInStore(cert))
SignPEFile(cert, timestampUrl, path, resources, useSha256);
else
throw new InvalidOperationException(resources.GetString("SignFile.CertNotInStore"));
}
else
{
using(RSA rsa = CngLightup.GetRSAPrivateKey(cert))
{
if (rsa == null)
throw new ApplicationException(resources.GetString("SecurityUtil.OnlyRSACertsAreAllowed"));
try
{
var doc = new XmlDocument { PreserveWhitespace = true };
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
using (XmlReader xr = XmlReader.Create(path, xrSettings))
{
doc.Load(xr);
}
var manifest = new SignedCmiManifest2(doc, useSha256);
CmiManifestSigner2 signer;
if (useSha256 && rsa is RSACryptoServiceProvider)
{
RSACryptoServiceProvider csp = SignedCmiManifest2.GetFixedRSACryptoServiceProvider(rsa as RSACryptoServiceProvider, useSha256);
signer = new CmiManifestSigner2(csp, cert, useSha256);
}
else
{
signer = new CmiManifestSigner2(rsa, cert, useSha256);
}
if (timestampUrl == null)
manifest.Sign(signer);
else
manifest.Sign(signer, timestampUrl.ToString());
doc.Save(path);
}
catch (Exception ex)
{
int exceptionHR = Marshal.GetHRForException(ex);
if (exceptionHR == -2147012889 || exceptionHR == -2147012867)
{
throw new ApplicationException(resources.GetString("SecurityUtil.TimestampUrlNotFound"), ex);
}
throw new ApplicationException(ex.Message, ex);
}
}
}
}
private static void SignPEFile(X509Certificate2 cert, Uri timestampUrl, string path, System.Resources.ResourceManager resources, bool useSha256)
{
var startInfo = new ProcessStartInfo(
GetPathToTool(resources),
GetCommandLineParameters(cert.Thumbprint, timestampUrl, path, useSha256))
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
Process signTool = null;
try
{
signTool = Process.Start(startInfo);
signTool.WaitForExit();
while (!signTool.HasExited)
{
System.Threading.Thread.Sleep(50);
}
switch (signTool.ExitCode)
{
case 0:
// everything was fine
break;
case 1:
// error, report it
throw new ApplicationException(String.Format(CultureInfo.InvariantCulture, resources.GetString("SecurityUtil.SigntoolFail"), path, signTool.StandardError.ReadToEnd()));
case 2:
// warning, report it
throw new WarningException(String.Format(CultureInfo.InvariantCulture, resources.GetString("SecurityUtil.SigntoolWarning"), path, signTool.StandardError.ReadToEnd()));
default:
// treat as error
throw new ApplicationException(String.Format(CultureInfo.InvariantCulture, resources.GetString("SecurityUtil.SigntoolFail"), path, signTool.StandardError.ReadToEnd()));
}
}
finally
{
signTool?.Close();
}
}
internal static string GetCommandLineParameters(string certThumbprint, Uri timestampUrl, string path, bool useSha256)
{
var commandLine = new StringBuilder();
if (useSha256)
{
commandLine.Append(String.Format(CultureInfo.InvariantCulture, "sign /fd sha256 /sha1 {0} ", certThumbprint));
}
else
{
commandLine.Append(String.Format(CultureInfo.InvariantCulture, "sign /sha1 {0} ", certThumbprint));
}
if (timestampUrl != null)
{
commandLine.Append(String.Format(CultureInfo.InvariantCulture, "/t {0} ", timestampUrl.ToString()));
}
commandLine.Append(string.Format(CultureInfo.InvariantCulture, "\"{0}\"", path));
return commandLine.ToString();
}
internal static string GetPathToTool(System.Resources.ResourceManager resources)
{
#pragma warning disable 618 // Disabling warning on using internal ToolLocationHelper API. At some point we should migrate this.
string toolPath = ToolLocationHelper.GetPathToWindowsSdkFile(ToolName, TargetDotNetFrameworkVersion.VersionLatest, VisualStudioVersion.VersionLatest);
if (toolPath == null || !FileSystems.Default.FileExists(toolPath))
{
toolPath = ToolLocationHelper.GetPathToWindowsSdkFile(ToolName, TargetDotNetFrameworkVersion.Version45,
VisualStudioVersion.Version110);
}
if (toolPath == null || !FileSystems.Default.FileExists(toolPath))
{
var pathToDotNetFrameworkSdk = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Version40, VisualStudioVersion.Version100);
if (pathToDotNetFrameworkSdk != null)
{
toolPath = Path.Combine(pathToDotNetFrameworkSdk, "bin", ToolName);
}
}
if (toolPath == null || !FileSystems.Default.FileExists(toolPath))
{
toolPath = GetVersionIndependentToolPath(ToolName);
}
if (toolPath == null || !FileSystems.Default.FileExists(toolPath))
{
toolPath = Path.Combine(Directory.GetCurrentDirectory(), ToolName);
}
if (!FileSystems.Default.FileExists(toolPath))
{
throw new ApplicationException(String.Format(CultureInfo.CurrentCulture,
resources.GetString("SecurityUtil.SigntoolNotFound"), toolPath));
}
return toolPath;
#pragma warning restore 618
}
internal static X509Certificate2 GetCert(string thumbprint)
{
var personalStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
personalStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection foundCerts = personalStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
if (foundCerts.Count == 1)
{
return foundCerts[0];
}
}
finally
{
personalStore.Close();
}
return null;
}
private static bool IsCertInStore(X509Certificate2 cert)
{
var personalStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
personalStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection foundCerts = personalStore.Certificates.Find(X509FindType.FindByThumbprint, cert.Thumbprint, false);
if (foundCerts.Count == 1)
return true;
}
finally
{
personalStore.Close();
}
return false;
}
private static string GetVersionIndependentToolPath(string toolName)
{
RegistryKey localMachineKey = Registry.LocalMachine;
const string versionIndependentToolKeyName = @"Software\Microsoft\ClickOnce\SignTool";
using (RegistryKey versionIndependentToolKey = localMachineKey.OpenSubKey(versionIndependentToolKeyName, writable: false))
{
string versionIndependentToolPath = null;
if (versionIndependentToolKey != null)
{
versionIndependentToolPath = versionIndependentToolKey.GetValue("Path") as string;
}
return versionIndependentToolPath != null ? Path.Combine(versionIndependentToolPath, toolName) : null;
}
}
}
}