Skip to content

Commit a9d68ab

Browse files
removed dead code that had been removed in NETFX (#11742)
And some minor clean-up changes: * added readonly to a field * added parameter names to function parameters
1 parent 56af147 commit a9d68ab

File tree

2 files changed

+48
-105
lines changed

2 files changed

+48
-105
lines changed

src/Tasks/ManifestUtil/SecurityUtil.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public static PermissionSet XmlToPermissionSet(XmlElement element)
502502
[SupportedOSPlatform("windows")]
503503
public static void SignFile(string certThumbprint, Uri timestampUrl, string path)
504504
{
505-
SignFile(certThumbprint, timestampUrl, path, null, null);
505+
SignFile(certThumbprint, timestampUrl, path, targetFrameworkVersion: null, targetFrameworkIdentifier: null);
506506
}
507507

508508
/// <summary>
@@ -518,7 +518,7 @@ public static void SignFile(string certThumbprint,
518518
string path,
519519
string targetFrameworkVersion)
520520
{
521-
SignFile(certThumbprint, timestampUrl, path, targetFrameworkVersion, null);
521+
SignFile(certThumbprint, timestampUrl, path, targetFrameworkVersion, targetFrameworkIdentifier: null);
522522
}
523523

524524
/// <summary>
@@ -536,7 +536,7 @@ public static void SignFile(string certThumbprint,
536536
string targetFrameworkVersion,
537537
string targetFrameworkIdentifier)
538538
{
539-
SignFile(certThumbprint, timestampUrl, path, targetFrameworkVersion, targetFrameworkIdentifier, false);
539+
SignFile(certThumbprint, timestampUrl, path, targetFrameworkVersion, targetFrameworkIdentifier, disallowMansignTimestampFallback: false);
540540
}
541541

542542
/// <summary>
@@ -637,7 +637,7 @@ public static void SignFile(X509Certificate2 cert, Uri timestampUrl, string path
637637
{
638638
// setup resources
639639
System.Resources.ResourceManager resources = new System.Resources.ResourceManager("Microsoft.Build.Tasks.Core.Strings.ManifestUtilities", typeof(SecurityUtilities).Module.Assembly);
640-
SignFileInternal(cert, timestampUrl, path, true, resources);
640+
SignFileInternal(cert, timestampUrl, path, targetFrameworkSupportsSha256: true, resources);
641641
}
642642

643643
[SupportedOSPlatform("windows")]
@@ -701,6 +701,7 @@ private static void SignFileInternal(X509Certificate2 cert,
701701
{
702702
doc.Load(xr);
703703
}
704+
704705
var manifest = new SignedCmiManifest2(doc, useSha256);
705706
CmiManifestSigner2 signer;
706707
if (useSha256 && rsa is RSACryptoServiceProvider rsacsp)

src/Tasks/ManifestUtil/mansign2.cs

Lines changed: 43 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -550,126 +550,68 @@ private static void ReplacePublicKeyToken(XmlDocument manifestDom, AsymmetricAlg
550550
}
551551
}
552552

553+
[SuppressMessage("Security", "CA5350:Do Not Use Weak Cryptographic Algorithms", Justification = "SHA1 is retained for compatibility reasons as an option in VisualStudio signing page and consequently in the trust manager, default is SHA2.")]
553554
private static byte[] ComputeHashFromManifest(XmlDocument manifestDom, bool useSha256)
554555
{
555-
#if (true) // BUGBUG: Remove before RTM when old format support is no longer needed.
556-
return ComputeHashFromManifest(manifestDom, false, useSha256);
557-
}
556+
// Since the DOM given to us is not guaranteed to be normalized,
557+
// we need to normalize it ourselves. Also, we always preserve
558+
// white space as Fusion XML engine always preserve white space.
559+
XmlDocument normalizedDom = new XmlDocument();
560+
normalizedDom.PreserveWhitespace = true;
561+
562+
// Normalize the document
563+
using (TextReader stringReader = new StringReader(manifestDom.OuterXml))
564+
{
565+
XmlReaderSettings settings = new XmlReaderSettings();
566+
settings.DtdProcessing = DtdProcessing.Parse;
567+
using (XmlReader reader = XmlReader.Create(stringReader, settings, manifestDom.BaseURI))
568+
{
569+
normalizedDom.Load(reader);
570+
}
571+
}
558572

559-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5350:Do Not Use Weak Cryptographic Algorithms", Justification = "SHA1 is retained for compatibility reasons as an option in VisualStudio signing page and consequently in the trust manager, default is SHA2.")]
560-
private static byte[] ComputeHashFromManifest(XmlDocument manifestDom, bool oldFormat, bool useSha256)
561-
{
562-
if (oldFormat)
563-
{
564-
XmlDsigExcC14NTransform exc = new XmlDsigExcC14NTransform();
565-
exc.LoadInput(manifestDom);
573+
XmlDsigExcC14NTransform exc = new XmlDsigExcC14NTransform();
574+
exc.LoadInput(normalizedDom);
566575

567-
if (useSha256)
568-
{
576+
if (useSha256)
577+
{
569578
#pragma warning disable SA1111, SA1009 // Closing parenthesis should be on line of last parameter
570-
using (SHA256 sha2 = SHA256.Create(
579+
using (SHA256 sha2 = SHA256.Create(
571580
#if FEATURE_CRYPTOGRAPHIC_FACTORY_ALGORITHM_NAMES
572-
"System.Security.Cryptography.SHA256CryptoServiceProvider"
581+
"System.Security.Cryptography.SHA256CryptoServiceProvider"
573582
#endif
574-
))
583+
))
575584
#pragma warning restore SA1111, SA1009 // Closing parenthesis should be on line of last parameter
576-
{
577-
byte[] hash = sha2.ComputeHash(exc.GetOutput() as MemoryStream);
578-
if (hash == null)
579-
{
580-
throw new CryptographicException(Win32.TRUST_E_BAD_DIGEST);
581-
}
582-
583-
return hash;
584-
}
585-
}
586-
else
587585
{
588-
#pragma warning disable SA1111, SA1009 // Closing parenthesis should be on line of last parameter
589-
// codeql[cs/weak-crypto] SHA1 is retained for compatibility reasons as an option in VisualStudio signing page and consequently in the trust manager, default is SHA2. https://devdiv.visualstudio.com/DevDiv/_workitems/edit/139025
590-
using (SHA1 sha1 = SHA1.Create(
591-
#if FEATURE_CRYPTOGRAPHIC_FACTORY_ALGORITHM_NAMES
592-
"System.Security.Cryptography.SHA1CryptoServiceProvider"
593-
#endif
594-
))
595-
#pragma warning restore SA1111, SA1009 // Closing parenthesis should be on line of last parameter
586+
byte[] hash = sha2.ComputeHash(exc.GetOutput() as MemoryStream);
587+
if (hash == null)
596588
{
597-
byte[] hash = sha1.ComputeHash(exc.GetOutput() as MemoryStream);
598-
if (hash == null)
599-
{
600-
throw new CryptographicException(Win32.TRUST_E_BAD_DIGEST);
601-
}
602-
603-
return hash;
589+
throw new CryptographicException(Win32.TRUST_E_BAD_DIGEST);
604590
}
591+
592+
return hash;
605593
}
606594
}
607595
else
608596
{
609-
#endif
610-
// Since the DOM given to us is not guaranteed to be normalized,
611-
// we need to normalize it ourselves. Also, we always preserve
612-
// white space as Fusion XML engine always preserve white space.
613-
XmlDocument normalizedDom = new XmlDocument();
614-
normalizedDom.PreserveWhitespace = true;
615-
616-
// Normalize the document
617-
using (TextReader stringReader = new StringReader(manifestDom.OuterXml))
618-
{
619-
XmlReaderSettings settings = new XmlReaderSettings();
620-
settings.DtdProcessing = DtdProcessing.Parse;
621-
using (XmlReader reader = XmlReader.Create(stringReader, settings, manifestDom.BaseURI))
622-
{
623-
normalizedDom.Load(reader);
624-
}
625-
}
626-
627-
XmlDsigExcC14NTransform exc = new XmlDsigExcC14NTransform();
628-
exc.LoadInput(normalizedDom);
629-
630-
if (useSha256)
631-
{
632597
#pragma warning disable SA1111, SA1009 // Closing parenthesis should be on line of last parameter
633-
using (SHA256 sha2 = SHA256.Create(
598+
// codeql[cs/weak-crypto] SHA1 is retained for compatibility reasons as an option in VisualStudio signing page and consequently in the trust manager, default is SHA2. https://devdiv.visualstudio.com/DevDiv/_workitems/edit/139025
599+
using (SHA1 sha1 = SHA1.Create(
634600
#if FEATURE_CRYPTOGRAPHIC_FACTORY_ALGORITHM_NAMES
635-
"System.Security.Cryptography.SHA256CryptoServiceProvider"
601+
"System.Security.Cryptography.SHA1CryptoServiceProvider"
636602
#endif
637-
))
603+
))
638604
#pragma warning restore SA1111, SA1009 // Closing parenthesis should be on line of last parameter
639-
{
640-
byte[] hash = sha2.ComputeHash(exc.GetOutput() as MemoryStream);
641-
if (hash == null)
642-
{
643-
throw new CryptographicException(Win32.TRUST_E_BAD_DIGEST);
644-
}
645-
646-
return hash;
647-
}
648-
}
649-
else
650605
{
651-
#pragma warning disable SA1111, SA1009 // Closing parenthesis should be on line of last parameter
652-
// codeql[cs/weak-crypto] SHA1 is retained for compatibility reasons as an option in VisualStudio signing page and consequently in the trust manager, default is SHA2. https://devdiv.visualstudio.com/DevDiv/_workitems/edit/139025
653-
using (SHA1 sha1 = SHA1.Create(
654-
#if FEATURE_CRYPTOGRAPHIC_FACTORY_ALGORITHM_NAMES
655-
"System.Security.Cryptography.SHA1CryptoServiceProvider"
656-
#endif
657-
))
658-
#pragma warning restore SA1111, SA1009 // Closing parenthesis should be on line of last parameter
606+
byte[] hash = sha1.ComputeHash(exc.GetOutput() as MemoryStream);
607+
if (hash == null)
659608
{
660-
byte[] hash = sha1.ComputeHash(exc.GetOutput() as MemoryStream);
661-
if (hash == null)
662-
{
663-
throw new CryptographicException(Win32.TRUST_E_BAD_DIGEST);
664-
}
665-
666-
return hash;
609+
throw new CryptographicException(Win32.TRUST_E_BAD_DIGEST);
667610
}
668-
}
669611

670-
#if (true) // BUGBUG: Remove before RTM when old format support is no longer needed.
612+
return hash;
613+
}
671614
}
672-
#endif
673615
}
674616

675617
private const string AssemblyNamespaceUri = "urn:schemas-microsoft-com:asm.v1";
@@ -739,8 +681,8 @@ private static void AuthenticodeSignLicenseDom(XmlDocument licenseDom, CmiManife
739681
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
740682
if (signer.UseSha256)
741683
{
742-
signedXml.SignedInfo.SignatureMethod = Sha256SignatureMethodUri;
743-
}
684+
signedXml.SignedInfo.SignatureMethod = Sha256SignatureMethodUri;
685+
}
744686
else
745687
{
746688
signedXml.SignedInfo.SignatureMethod = Sha1SignatureMethodUri;
@@ -1108,12 +1050,12 @@ internal class CmiManifestSigner2
11081050
private X509Certificate2Collection _certificates;
11091051
private X509IncludeOption _includeOption;
11101052
private CmiManifestSignerFlag _signerFlag;
1111-
private bool _useSha256;
1053+
private readonly bool _useSha256;
11121054

11131055
private CmiManifestSigner2() { }
11141056

11151057
internal CmiManifestSigner2(AsymmetricAlgorithm strongNameKey) :
1116-
this(strongNameKey, null, false)
1058+
this(strongNameKey, certificate: null, useSha256: false)
11171059
{ }
11181060

11191061
internal CmiManifestSigner2(AsymmetricAlgorithm strongNameKey, X509Certificate2 certificate, bool useSha256)
@@ -1311,7 +1253,7 @@ internal CmiAuthenticodeSignerInfo(int errorCode)
13111253
}
13121254

13131255
internal CmiAuthenticodeSignerInfo(Win32.AXL_SIGNER_INFO signerInfo,
1314-
Win32.AXL_TIMESTAMPER_INFO timestamperInfo)
1256+
Win32.AXL_TIMESTAMPER_INFO timestamperInfo)
13151257
{
13161258
_error = (int)signerInfo.dwError;
13171259
if (signerInfo.pChainContext != IntPtr.Zero)

0 commit comments

Comments
 (0)