From 170747709fcee2c8a085e14bb632025227233294 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Sun, 27 Mar 2022 07:53:21 +0100 Subject: [PATCH 01/67] First pass --- .../System.Security.Cryptography.Xml.csproj | 1 + .../Xml/AncestralNamespaceContextManager.cs | 8 +-- .../Cryptography/Xml/AttributeSortOrder.cs | 6 +-- .../Cryptography/Xml/CanonicalXmlDocument.cs | 16 +++--- .../Cryptography/Xml/CanonicalXmlNodeList.cs | 14 ++--- .../Cryptography/Xml/CanonicalXmlText.cs | 2 +- .../Security/Cryptography/Xml/CipherData.cs | 8 +-- .../Cryptography/Xml/CipherReference.cs | 4 +- .../Security/Cryptography/Xml/DataObject.cs | 17 ++++--- .../Cryptography/Xml/EncryptedReference.cs | 9 ++-- .../Cryptography/Xml/EncryptedType.cs | 28 +++++----- .../Security/Cryptography/Xml/EncryptedXml.cs | 48 ++++++++--------- .../Cryptography/Xml/EncryptionProperty.cs | 17 ++++--- .../Xml/EncryptionPropertyCollection.cs | 12 ++--- .../Cryptography/Xml/KeyInfoEncryptedKey.cs | 4 +- .../Cryptography/Xml/MyXmlDocument.cs | 2 +- .../Cryptography/Xml/NamespaceFrame.cs | 4 +- .../Cryptography/Xml/NamespaceSortOrder.cs | 6 +-- .../Xml/RSAPKCS1SignatureDescription.cs | 3 +- .../Cryptography/Xml/ReferenceList.cs | 8 +-- .../Security/Cryptography/Xml/SignedXml.cs | 51 ++++++++++--------- .../Cryptography/Xml/TransformChain.cs | 8 +-- .../System/Security/Cryptography/Xml/Utils.cs | 4 +- 23 files changed, 145 insertions(+), 135 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj b/src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj index 20a3d77a4641f..a4ece4da5d8cc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj +++ b/src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj @@ -5,6 +5,7 @@ disable $(NoWarn);nullable $(NoWarn);CA1850 + enable true Provides classes to support the creation and validation of XML digital signatures. The classes in this namespace implement the World Wide Web Consortium Recommendation, "XML-Signature Syntax and Processing", described at http://www.w3.org/TR/xmldsig-core/. diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs index 0bbafb6db14ca..d5e46146f6c3b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs @@ -20,9 +20,9 @@ internal NamespaceFrame GetCurrentScope() return GetScopeAt(_ancestorStack.Count - 1); } - protected XmlAttribute GetNearestRenderedNamespaceWithMatchingPrefix(string nsPrefix, out int depth) + protected XmlAttribute? GetNearestRenderedNamespaceWithMatchingPrefix(string nsPrefix, out int depth) { - XmlAttribute attr; + XmlAttribute? attr; depth = -1; for (int i = _ancestorStack.Count - 1; i >= 0; i--) { @@ -35,9 +35,9 @@ protected XmlAttribute GetNearestRenderedNamespaceWithMatchingPrefix(string nsPr return null; } - protected XmlAttribute GetNearestUnrenderedNamespaceWithMatchingPrefix(string nsPrefix, out int depth) + protected XmlAttribute? GetNearestUnrenderedNamespaceWithMatchingPrefix(string nsPrefix, out int depth) { - XmlAttribute attr; + XmlAttribute? attr; depth = -1; for (int i = _ancestorStack.Count - 1; i >= 0; i--) { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AttributeSortOrder.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AttributeSortOrder.cs index c0ff1120f8b18..ece8cfb89fa20 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AttributeSortOrder.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AttributeSortOrder.cs @@ -11,10 +11,10 @@ internal sealed class AttributeSortOrder : IComparer { internal AttributeSortOrder() { } - public int Compare(object a, object b) + public int Compare(object? a, object? b) { - XmlNode nodeA = a as XmlNode; - XmlNode nodeB = b as XmlNode; + XmlNode? nodeA = a as XmlNode; + XmlNode? nodeB = b as XmlNode; if ((nodeA == null) || (nodeB == null)) throw new ArgumentException(); int namespaceCompare = string.CompareOrdinal(nodeA.NamespaceURI, nodeB.NamespaceURI); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index 9713d998428f2..b519bb4a2a273 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -61,32 +61,32 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace } } - public override XmlElement CreateElement(string prefix, string localName, string namespaceURI) + public override XmlElement CreateElement(string? prefix, string localName, string? namespaceURI) { return new CanonicalXmlElement(prefix, localName, namespaceURI, this, _defaultNodeSetInclusionState); } - public override XmlAttribute CreateAttribute(string prefix, string localName, string namespaceURI) + public override XmlAttribute CreateAttribute(string? prefix, string localName, string? namespaceURI) { return new CanonicalXmlAttribute(prefix, localName, namespaceURI, this, _defaultNodeSetInclusionState); } - protected override XmlAttribute CreateDefaultAttribute(string prefix, string localName, string namespaceURI) + protected override XmlAttribute CreateDefaultAttribute(string? prefix, string localName, string? namespaceURI) { return new CanonicalXmlAttribute(prefix, localName, namespaceURI, this, _defaultNodeSetInclusionState); } - public override XmlText CreateTextNode(string text) + public override XmlText CreateTextNode(string? text) { return new CanonicalXmlText(text, this, _defaultNodeSetInclusionState); } - public override XmlWhitespace CreateWhitespace(string prefix) + public override XmlWhitespace CreateWhitespace(string? prefix) { return new CanonicalXmlWhitespace(prefix, this, _defaultNodeSetInclusionState); } - public override XmlSignificantWhitespace CreateSignificantWhitespace(string text) + public override XmlSignificantWhitespace CreateSignificantWhitespace(string? text) { return new CanonicalXmlSignificantWhitespace(text, this, _defaultNodeSetInclusionState); } @@ -96,7 +96,7 @@ public override XmlProcessingInstruction CreateProcessingInstruction(string targ return new CanonicalXmlProcessingInstruction(target, data, this, _defaultNodeSetInclusionState); } - public override XmlComment CreateComment(string data) + public override XmlComment CreateComment(string? data) { return new CanonicalXmlComment(data, this, _defaultNodeSetInclusionState, _includeComments); } @@ -106,7 +106,7 @@ public override XmlEntityReference CreateEntityReference(string name) return new CanonicalXmlEntityReference(name, this, _defaultNodeSetInclusionState); } - public override XmlCDataSection CreateCDataSection(string data) + public override XmlCDataSection CreateCDataSection(string? data) { return new CanonicalXmlCDataSection(data, this, _defaultNodeSetInclusionState); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs index e1308ea884980..239eb54b53e57 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs @@ -15,7 +15,7 @@ internal CanonicalXmlNodeList() _nodeArray = new ArrayList(); } - public override XmlNode Item(int index) + public override XmlNode? Item(int index) { return (XmlNode)_nodeArray[index]; } @@ -31,7 +31,7 @@ public override int Count } // IList methods - public int Add(object value) + public int Add(object? value) { if (!(value is XmlNode)) throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, "node"); @@ -43,24 +43,24 @@ public void Clear() _nodeArray.Clear(); } - public bool Contains(object value) + public bool Contains(object? value) { return _nodeArray.Contains(value); } - public int IndexOf(object value) + public int IndexOf(object? value) { return _nodeArray.IndexOf(value); } - public void Insert(int index, object value) + public void Insert(int index, object? value) { if (!(value is XmlNode)) throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(value)); _nodeArray.Insert(index, value); } - public void Remove(object value) + public void Remove(object? value) { _nodeArray.Remove(value); } @@ -80,7 +80,7 @@ public bool IsReadOnly get { return _nodeArray.IsReadOnly; } } - object IList.this[int index] + object? IList.this[int index] { get { return _nodeArray[index]; } set diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlText.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlText.cs index 90982628d4be3..de81dfea23e08 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlText.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlText.cs @@ -11,7 +11,7 @@ internal sealed class CanonicalXmlText : XmlText, ICanonicalizableNode { private bool _isInNodeSet; - public CanonicalXmlText(string strData, XmlDocument doc, bool defaultNodeSetInclusionState) + public CanonicalXmlText(string? strData, XmlDocument doc, bool defaultNodeSetInclusionState) : base(strData, doc) { _isInNodeSet = defaultNodeSetInclusionState; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs index 877c641a4eabd..af75f20b93713 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs @@ -7,9 +7,9 @@ namespace System.Security.Cryptography.Xml { public sealed class CipherData { - private XmlElement _cachedXml; - private CipherReference _cipherReference; - private byte[] _cipherValue; + private XmlElement? _cachedXml; + private CipherReference? _cipherReference; + private byte[]? _cipherValue; public CipherData() { } @@ -31,7 +31,7 @@ private bool CacheValid } } - public CipherReference CipherReference + public CipherReference? CipherReference { get { return _cipherReference; } set diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs index b87286eb7f118..be4939a4915cc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs @@ -7,7 +7,7 @@ namespace System.Security.Cryptography.Xml { public sealed class CipherReference : EncryptedReference { - private byte[] _cipherValue; + private byte[]? _cipherValue; public CipherReference() : base() { @@ -25,7 +25,7 @@ public CipherReference(string uri, TransformChain transformChain) : base(uri, tr } // This method is used to cache results from resolved cipher references. - internal byte[] CipherValue + internal byte[]? CipherValue { get { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs index 83d5929dc68f0..813a6230dc54f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs @@ -7,11 +7,11 @@ namespace System.Security.Cryptography.Xml { public class DataObject { - private string _id; - private string _mimeType; - private string _encoding; + private string? _id; + private string? _mimeType; + private string? _encoding; private CanonicalXmlNodeList _elData; - private XmlElement _cachedXml; + private XmlElement? _cachedXml; // // public constructors @@ -42,7 +42,7 @@ public DataObject(string id, string mimeType, string encoding, XmlElement data) // public properties // - public string Id + public string? Id { get { return _id; } set @@ -52,7 +52,7 @@ public string Id } } - public string MimeType + public string? MimeType { get { return _mimeType; } set @@ -62,7 +62,7 @@ public string MimeType } } - public string Encoding + public string? Encoding { get { return _encoding; } set @@ -102,7 +102,7 @@ private bool CacheValid // public methods // - public XmlElement GetXml() + public XmlElement? GetXml() { if (CacheValid) return (_cachedXml); @@ -122,6 +122,7 @@ internal XmlElement GetXml(XmlDocument document) if (!string.IsNullOrEmpty(_encoding)) objectElement.SetAttribute("Encoding", _encoding); + // red flag - not changed, but always false if (_elData != null) { foreach (XmlNode node in _elData) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index 315e5ea0cb122..57232187dc604 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -9,8 +9,8 @@ public abstract class EncryptedReference { private string _uri; private string _referenceType; - private TransformChain _transformChain; - internal XmlElement _cachedXml; + private TransformChain? _transformChain; + internal XmlElement? _cachedXml; protected EncryptedReference() : this(string.Empty, new TransformChain()) { @@ -74,7 +74,8 @@ protected internal bool CacheValid public virtual XmlElement GetXml() { - if (CacheValid) return _cachedXml; + // red flag + if (CacheValid) return _cachedXml!; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; @@ -115,7 +116,7 @@ public virtual void LoadXml(XmlElement value) // Transforms XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable); nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); - XmlNode transformsNode = value.SelectSingleNode("ds:Transforms", nsm); + XmlNode? transformsNode = value.SelectSingleNode("ds:Transforms", nsm); if (transformsNode != null) TransformChain.LoadXml(transformsNode as XmlElement); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs index 61c5339f0264f..899f1324f0752 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs @@ -7,15 +7,15 @@ namespace System.Security.Cryptography.Xml { public abstract class EncryptedType { - private string _id; - private string _type; - private string _mimeType; - private string _encoding; - private EncryptionMethod _encryptionMethod; - private CipherData _cipherData; - private EncryptionPropertyCollection _props; - private KeyInfo _keyInfo; - internal XmlElement _cachedXml; + private string? _id; + private string? _type; + private string? _mimeType; + private string? _encoding; + private EncryptionMethod? _encryptionMethod; + private CipherData? _cipherData; + private EncryptionPropertyCollection? _props; + private KeyInfo? _keyInfo; + internal XmlElement? _cachedXml; internal bool CacheValid { @@ -25,7 +25,7 @@ internal bool CacheValid } } - public virtual string Id + public virtual string? Id { get { return _id; } set @@ -35,7 +35,7 @@ public virtual string Id } } - public virtual string Type + public virtual string? Type { get { return _type; } set @@ -45,7 +45,7 @@ public virtual string Type } } - public virtual string MimeType + public virtual string? MimeType { get { return _mimeType; } set @@ -55,7 +55,7 @@ public virtual string MimeType } } - public virtual string Encoding + public virtual string? Encoding { get { return _encoding; } set @@ -71,7 +71,7 @@ public KeyInfo KeyInfo set => _keyInfo = value; } - public virtual EncryptionMethod EncryptionMethod + public virtual EncryptionMethod? EncryptionMethod { get { return _encryptionMethod; } set diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index fc860409ec515..72182674669e9 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -59,8 +59,8 @@ public class EncryptedXml // private readonly XmlDocument _document; - private Evidence _evidence; - private XmlResolver _xmlResolver; + private Evidence? _evidence; + private XmlResolver? _xmlResolver; // hash table defining the key name mapping private const int _capacity = 4; // 4 is a reasonable capacity for // the key name mapping hash table @@ -79,7 +79,7 @@ public EncryptedXml() : this(new XmlDocument()) { } public EncryptedXml(XmlDocument document) : this(document, null) { } - public EncryptedXml(XmlDocument document, Evidence evidence) + public EncryptedXml(XmlDocument document, Evidence? evidence) { _document = document; _evidence = evidence; @@ -124,14 +124,14 @@ public int XmlDSigSearchDepth } // The evidence of the document being loaded: will be used to resolve external URIs - public Evidence DocumentEvidence + public Evidence? DocumentEvidence { get { return _evidence; } set { _evidence = value; } } // The resolver to use for external entities - public XmlResolver Resolver + public XmlResolver? Resolver { get { return _xmlResolver; } set { _xmlResolver = value; } @@ -179,7 +179,7 @@ private byte[] GetCipherValue(CipherData cipherData) throw new ArgumentNullException(nameof(cipherData)); } - Stream inputStream = null; + Stream? inputStream = null; if (cipherData.CipherValue != null) { @@ -198,7 +198,7 @@ private byte[] GetCipherValue(CipherData cipherData) if (cipherData.CipherReference.Uri.Length == 0) { // self referenced Uri - string baseUri = _document?.BaseURI; + string? baseUri = _document?.BaseURI; TransformChain tc = cipherData.CipherReference.TransformChain; if (tc == null) { @@ -229,7 +229,7 @@ private byte[] GetCipherValue(CipherData cipherData) throw new CryptographicException(SR.Cryptography_Xml_UriNotResolved, cipherData.CipherReference.Uri); } // read the output stream into a memory stream - byte[] cipherValue = null; + byte[]? cipherValue = null; using (MemoryStream ms = new MemoryStream()) { Utils.Pump(decInputStream, ms); @@ -253,13 +253,13 @@ private byte[] GetCipherValue(CipherData cipherData) // // This describes how the application wants to associate id references to elements - public virtual XmlElement GetIdElement(XmlDocument document, string idValue) + public virtual XmlElement? GetIdElement(XmlDocument document, string idValue) { return SignedXml.DefaultGetIdElement(document, idValue); } // default behaviour is to look for the IV in the CipherValue - public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string symmetricAlgorithmUri) + public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symmetricAlgorithmUri) { if (encryptedData is null) { @@ -297,7 +297,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string symmet // default behaviour is to look for keys defined by an EncryptedKey clause // either directly or through a KeyInfoRetrievalMethod, and key names in the key mapping - public virtual SymmetricAlgorithm GetDecryptionKey(EncryptedData encryptedData, string symmetricAlgorithmUri) + public virtual SymmetricAlgorithm? GetDecryptionKey(EncryptedData encryptedData, string symmetricAlgorithmUri) { if (encryptedData is null) { @@ -307,10 +307,10 @@ public virtual SymmetricAlgorithm GetDecryptionKey(EncryptedData encryptedData, if (encryptedData.KeyInfo == null) return null; IEnumerator keyInfoEnum = encryptedData.KeyInfo.GetEnumerator(); - KeyInfoRetrievalMethod kiRetrievalMethod; - KeyInfoName kiName; - KeyInfoEncryptedKey kiEncKey; - EncryptedKey ek = null; + KeyInfoRetrievalMethod? kiRetrievalMethod; + KeyInfoName? kiName; + KeyInfoEncryptedKey? kiEncKey; + EncryptedKey? ek = null; while (keyInfoEnum.MoveNext()) { @@ -324,12 +324,12 @@ public virtual SymmetricAlgorithm GetDecryptionKey(EncryptedData encryptedData, // try to get it from a CarriedKeyName XmlNamespaceManager nsm = new XmlNamespaceManager(_document.NameTable); nsm.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl); - XmlNodeList encryptedKeyList = _document.SelectNodes("//enc:EncryptedKey", nsm); + XmlNodeList? encryptedKeyList = _document.SelectNodes("//enc:EncryptedKey", nsm); if (encryptedKeyList != null) { foreach (XmlNode encryptedKeyNode in encryptedKeyList) { - XmlElement encryptedKeyElement = encryptedKeyNode as XmlElement; + XmlElement? encryptedKeyElement = encryptedKeyNode as XmlElement; EncryptedKey ek1 = new EncryptedKey(); ek1.LoadXml(encryptedKeyElement); if (ek1.CarriedKeyName == keyName && ek1.Recipient == Recipient) @@ -384,7 +384,7 @@ public virtual SymmetricAlgorithm GetDecryptionKey(EncryptedData encryptedData, } // Try to decrypt the EncryptedKey given the key mapping - public virtual byte[] DecryptEncryptedKey(EncryptedKey encryptedKey) + public virtual byte[]? DecryptEncryptedKey(EncryptedKey encryptedKey) { if (encryptedKey is null) { @@ -395,11 +395,11 @@ public virtual byte[] DecryptEncryptedKey(EncryptedKey encryptedKey) return null; IEnumerator keyInfoEnum = encryptedKey.KeyInfo.GetEnumerator(); - KeyInfoName kiName; - KeyInfoX509Data kiX509Data; - KeyInfoRetrievalMethod kiRetrievalMethod; - KeyInfoEncryptedKey kiEncKey; - EncryptedKey ek; + KeyInfoName? kiName; + KeyInfoX509Data? kiX509Data; + KeyInfoRetrievalMethod? kiRetrievalMethod; + KeyInfoEncryptedKey? kiEncKey; + EncryptedKey? ek; bool fOAEP; while (keyInfoEnum.MoveNext()) @@ -409,7 +409,7 @@ public virtual byte[] DecryptEncryptedKey(EncryptedKey encryptedKey) { // Get the decryption key from the key mapping string keyName = kiName.Value; - object kek = _keyNameMapping[keyName]; + object? kek = _keyNameMapping[keyName]; if (kek != null) { if (encryptedKey.CipherData == null || encryptedKey.CipherData.CipherValue == null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs index 6077bd264f8c5..6c79ead3be13a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs @@ -1,16 +1,17 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml { public sealed class EncryptionProperty { - private string _target; - private string _id; + private string? _target; + private string? _id; private XmlElement _elemProp; - private XmlElement _cachedXml; + private XmlElement? _cachedXml; // We are being lax here as per the spec public EncryptionProperty() { } @@ -29,12 +30,12 @@ public EncryptionProperty(XmlElement elementProperty) _cachedXml = null; } - public string Id + public string? Id { get { return _id; } } - public string Target + public string? Target { get { return _target; } } @@ -64,7 +65,8 @@ private bool CacheValid public XmlElement GetXml() { - if (CacheValid) return _cachedXml; + // red flag + if (CacheValid) return _cachedXml!; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; @@ -73,7 +75,8 @@ public XmlElement GetXml() internal XmlElement GetXml(XmlDocument document) { - return document.ImportNode(_elemProp, true) as XmlElement; + //red flag + return (document.ImportNode(_elemProp, true) as XmlElement)!; } public void LoadXml(XmlElement value) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs index 0fca5ef01afeb..94553fab29a06 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs @@ -25,7 +25,7 @@ public int Count } /// - int IList.Add(object value) + int IList.Add(object? value) { if (!(value is EncryptionProperty)) throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(value)); @@ -44,7 +44,7 @@ public void Clear() } /// - bool IList.Contains(object value) + bool IList.Contains(object? value) { if (!(value is EncryptionProperty)) throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(value)); @@ -58,7 +58,7 @@ public bool Contains(EncryptionProperty value) } /// - int IList.IndexOf(object value) + int IList.IndexOf(object? value) { if (!(value is EncryptionProperty)) throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(value)); @@ -72,7 +72,7 @@ public int IndexOf(EncryptionProperty value) } /// - void IList.Insert(int index, object value) + void IList.Insert(int index, object? value) { if (!(value is EncryptionProperty)) throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(value)); @@ -86,7 +86,7 @@ public void Insert(int index, EncryptionProperty value) } /// - void IList.Remove(object value) + void IList.Remove(object? value) { if (!(value is EncryptionProperty)) throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(value)); @@ -133,7 +133,7 @@ public EncryptionProperty this[int index] } /// - object IList.this[int index] + object? IList.this[int index] { get { return _props[index]; } set diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoEncryptedKey.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoEncryptedKey.cs index 2ddeee99d9902..02321596fdb83 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoEncryptedKey.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoEncryptedKey.cs @@ -7,7 +7,7 @@ namespace System.Security.Cryptography.Xml { public class KeyInfoEncryptedKey : KeyInfoClause { - private EncryptedKey _encryptedKey; + private EncryptedKey? _encryptedKey; public KeyInfoEncryptedKey() { } @@ -16,7 +16,7 @@ public KeyInfoEncryptedKey(EncryptedKey encryptedKey) _encryptedKey = encryptedKey; } - public EncryptedKey EncryptedKey + public EncryptedKey? EncryptedKey { get { return _encryptedKey; } set { _encryptedKey = value; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/MyXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/MyXmlDocument.cs index 808a3bbbd777f..281c64f6929e9 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/MyXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/MyXmlDocument.cs @@ -7,7 +7,7 @@ namespace System.Security.Cryptography.Xml { internal sealed class MyXmlDocument : XmlDocument { - protected override XmlAttribute CreateDefaultAttribute(string prefix, string localName, string namespaceURI) + protected override XmlAttribute CreateDefaultAttribute(string? prefix, string localName, string? namespaceURI) { return CreateAttribute(prefix, localName, namespaceURI); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs index 1af976d8fb8a3..4aa6ce9bd113b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs @@ -21,7 +21,7 @@ internal void AddRendered(XmlAttribute attr) _rendered.Add(Utils.GetNamespacePrefix(attr), attr); } - internal XmlAttribute GetRendered(string nsPrefix) + internal XmlAttribute? GetRendered(string nsPrefix) { return (XmlAttribute)_rendered[nsPrefix]; } @@ -31,7 +31,7 @@ internal void AddUnrendered(XmlAttribute attr) _unrendered.Add(Utils.GetNamespacePrefix(attr), attr); } - internal XmlAttribute GetUnrendered(string nsPrefix) + internal XmlAttribute? GetUnrendered(string nsPrefix) { return (XmlAttribute)_unrendered[nsPrefix]; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceSortOrder.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceSortOrder.cs index 1e998ef7e88e2..7a0766dbdc3fd 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceSortOrder.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceSortOrder.cs @@ -10,10 +10,10 @@ internal sealed class NamespaceSortOrder : IComparer { internal NamespaceSortOrder() { } - public int Compare(object a, object b) + public int Compare(object? a, object? b) { - XmlNode nodeA = a as XmlNode; - XmlNode nodeB = b as XmlNode; + XmlNode? nodeA = a as XmlNode; + XmlNode? nodeB = b as XmlNode; if ((nodeA == null) || (nodeB == null)) throw new ArgumentException(); bool nodeAdefault = Utils.IsDefaultNamespaceNode(nodeA); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs index f19884fbc8860..5b9f14552f864 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs @@ -16,7 +16,8 @@ public RSAPKCS1SignatureDescription(string hashAlgorithmName) public sealed override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key) { var item = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm); - item.SetKey(key); + //red flag + item!.SetKey(key); item.SetHashAlgorithm(DigestAlgorithm); return item; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs index ed01208d6d738..b370daa218bb9 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs @@ -42,12 +42,12 @@ public void Clear() _references.Clear(); } - public bool Contains(object value) + public bool Contains(object? value) { return _references.Contains(value); } - public int IndexOf(object value) + public int IndexOf(object? value) { return _references.IndexOf(value); } @@ -65,7 +65,7 @@ public void Insert(int index, object value) _references.Insert(index, value); } - public void Remove(object value) + public void Remove(object? value) { _references.Remove(value); } @@ -94,7 +94,7 @@ public EncryptedReference this[int index] } /// - object IList.this[int index] + object? IList.this[int index] { get { return _references[index]; } set diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index 5d722d0f180f5..df34a9e18944b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; using System.Xml; @@ -13,28 +14,28 @@ namespace System.Security.Cryptography.Xml public class SignedXml { protected Signature m_signature; - protected string m_strSigningKeyName; + protected string? m_strSigningKeyName; - private AsymmetricAlgorithm _signingKey; - private XmlDocument _containingDocument; - private IEnumerator _keyInfoEnum; - private X509Certificate2Collection _x509Collection; - private IEnumerator _x509Enum; + private AsymmetricAlgorithm? _signingKey; + private XmlDocument? _containingDocument; + private IEnumerator? _keyInfoEnum; + private X509Certificate2Collection? _x509Collection; + private IEnumerator? _x509Enum; private bool[] _refProcessed; private int[] _refLevelCache; internal XmlResolver _xmlResolver; - internal XmlElement _context; + internal XmlElement? _context; private bool _bResolverSet; private Func _signatureFormatValidator = DefaultSignatureFormatValidator; - private Collection _safeCanonicalizationMethods; + private Collection? _safeCanonicalizationMethods; // Built in canonicalization algorithm URIs - private static IList s_knownCanonicalizationMethods; + private static IList? s_knownCanonicalizationMethods; // Built in transform algorithm URIs (excluding canonicalization URIs) - private static IList s_defaultSafeTransformMethods; + private static IList? s_defaultSafeTransformMethods; // additional HMAC Url identifiers private const string XmlDsigMoreHMACMD5Url = "http://www.w3.org/2001/04/xmldsig-more#hmac-md5"; @@ -44,7 +45,7 @@ public class SignedXml private const string XmlDsigMoreHMACRIPEMD160Url = "http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160"; // defines the XML encryption processing rules - private EncryptedXml _exml; + private EncryptedXml? _exml; // // public constant Url identifiers most frequently used within the XML Signature classes @@ -110,7 +111,7 @@ public SignedXml(XmlElement elem) Initialize(elem); } - private void Initialize(XmlElement element) + private void Initialize(XmlElement? element) { _containingDocument = element?.OwnerDocument; _context = element; @@ -127,7 +128,7 @@ private void Initialize(XmlElement element) // /// - public string SigningKeyName + public string? SigningKeyName { get { return m_strSigningKeyName; } set { m_strSigningKeyName = value; } @@ -461,14 +462,14 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) // virtual methods // - protected virtual AsymmetricAlgorithm GetPublicKey() + protected virtual AsymmetricAlgorithm? GetPublicKey() { if (KeyInfo == null) throw new CryptographicException(SR.Cryptography_Xml_KeyInfoRequired); if (_x509Enum != null) { - AsymmetricAlgorithm key = GetNextCertificatePublicKey(); + AsymmetricAlgorithm? key = GetNextCertificatePublicKey(); if (key != null) return key; } @@ -491,7 +492,7 @@ protected virtual AsymmetricAlgorithm GetPublicKey() if (_x509Collection.Count > 0) { _x509Enum = _x509Collection.GetEnumerator(); - AsymmetricAlgorithm key = GetNextCertificatePublicKey(); + AsymmetricAlgorithm? key = GetNextCertificatePublicKey(); if (key != null) return key; } @@ -519,11 +520,11 @@ private X509Certificate2Collection BuildBagOfCerts() return collection; } - private AsymmetricAlgorithm GetNextCertificatePublicKey() + private AsymmetricAlgorithm? GetNextCertificatePublicKey() { while (_x509Enum.MoveNext()) { - X509Certificate2 certificate = (X509Certificate2)_x509Enum.Current; + X509Certificate2? certificate = (X509Certificate2)_x509Enum.Current; if (certificate != null) return Utils.GetAnyPublicKey(certificate); } @@ -531,12 +532,12 @@ private AsymmetricAlgorithm GetNextCertificatePublicKey() return null; } - public virtual XmlElement GetIdElement(XmlDocument document, string idValue) + public virtual XmlElement? GetIdElement(XmlDocument? document, string idValue) { return DefaultGetIdElement(document, idValue); } - internal static XmlElement DefaultGetIdElement(XmlDocument document, string idValue) + internal static XmlElement? DefaultGetIdElement(XmlDocument? document, string idValue) { if (document == null) return null; @@ -843,10 +844,10 @@ public ArrayList References set { _references = value; } } - public int Compare(object a, object b) + public int Compare(object? a, object? b) { - Reference referenceA = a as Reference; - Reference referenceB = b as Reference; + Reference? referenceA = a as Reference; + Reference? referenceB = b as Reference; // Get the indexes int iIndexA = 0; @@ -1056,7 +1057,7 @@ private bool CheckSignedInfo(KeyedHashAlgorithm macAlg) return m_signature.SignatureValue.AsSpan().SequenceEqual(hashValue.AsSpan(0, m_signature.SignatureValue.Length)); } - private static XmlElement GetSingleReferenceTarget(XmlDocument document, string idAttributeName, string idValue) + private static XmlElement? GetSingleReferenceTarget(XmlDocument document, string idAttributeName, string idValue) { // idValue has already been tested as an NCName (unless overridden for compatibility), so there's no // escaping that needs to be done here. @@ -1071,7 +1072,7 @@ private static XmlElement GetSingleReferenceTarget(XmlDocument document, string // In this case, we'll treat it the same as having found nothing across all fallbacks (but shortcut so that we don't // fall into a trap of finding a secondary element which wasn't the originally signed one). - XmlNodeList nodeList = document.SelectNodes(xPath); + XmlNodeList? nodeList = document.SelectNodes(xPath); if (nodeList == null || nodeList.Count == 0) { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index 2aeda5a2ad23f..fdccef1c32eb7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -186,14 +186,16 @@ internal void LoadXml(XmlElement value) XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable); nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); - XmlNodeList transformNodes = value.SelectNodes("ds:Transform", nsm); - if (transformNodes.Count == 0) + XmlNodeList? transformNodes = value.SelectNodes("ds:Transform", nsm); + //red flag + if (transformNodes!.Count == 0) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Transforms"); _transforms.Clear(); for (int i = 0; i < transformNodes.Count; ++i) { - XmlElement transformElement = (XmlElement)transformNodes.Item(i); + //red flag + XmlElement transformElement = (XmlElement)transformNodes.Item(i)!; string algorithm = Utils.GetAttribute(transformElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); Transform transform = CryptoHelpers.CreateFromName(algorithm); if (transform == null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index e5de6bee4c3c6..7349f78e0831f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -674,7 +674,7 @@ internal static X509Certificate2Collection BuildBagOfCerts(KeyInfoX509Data keyIn { if (stores[index] != null) { - X509Certificate2Collection filters = null; + X509Certificate2Collection? filters = null; // We don't care if we can't open the store. try { @@ -756,7 +756,7 @@ internal static bool IsSelfSigned(X509Chain chain) return false; } - internal static AsymmetricAlgorithm GetAnyPublicKey(X509Certificate2 certificate) + internal static AsymmetricAlgorithm? GetAnyPublicKey(X509Certificate2 certificate) { AsymmetricAlgorithm algorithm = (AsymmetricAlgorithm)certificate.GetRSAPublicKey() ?? certificate.GetECDsaPublicKey(); From 431c22e281ed6869628e687e7998439093f81bfa Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Sun, 27 Mar 2022 09:13:38 +0100 Subject: [PATCH 02/67] More annotations --- .../Xml/AncestralNamespaceContextManager.cs | 16 ++++++---- .../Cryptography/Xml/KeyInfoX509Data.cs | 30 ++++++++++--------- .../tests/KeyInfoX509DataTest.cs | 2 +- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs index d5e46146f6c3b..dc3ff66a8f708 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs @@ -10,12 +10,12 @@ internal abstract class AncestralNamespaceContextManager { internal ArrayList _ancestorStack = new ArrayList(); - internal NamespaceFrame GetScopeAt(int i) + internal NamespaceFrame? GetScopeAt(int i) { return (NamespaceFrame)_ancestorStack[i]; } - internal NamespaceFrame GetCurrentScope() + internal NamespaceFrame? GetCurrentScope() { return GetScopeAt(_ancestorStack.Count - 1); } @@ -26,7 +26,8 @@ internal NamespaceFrame GetCurrentScope() depth = -1; for (int i = _ancestorStack.Count - 1; i >= 0; i--) { - if ((attr = GetScopeAt(i).GetRendered(nsPrefix)) != null) + // red flag + if ((attr = GetScopeAt(i)!.GetRendered(nsPrefix)) != null) { depth = i; return attr; @@ -41,7 +42,8 @@ internal NamespaceFrame GetCurrentScope() depth = -1; for (int i = _ancestorStack.Count - 1; i >= 0; i--) { - if ((attr = GetScopeAt(i).GetUnrendered(nsPrefix)) != null) + //red flag + if ((attr = GetScopeAt(i)!.GetUnrendered(nsPrefix)) != null) { depth = i; return attr; @@ -84,12 +86,14 @@ internal void LoadRenderedNamespaces(SortedList nsRenderedList) internal void AddRendered(XmlAttribute attr) { - GetCurrentScope().AddRendered(attr); + //red flag + GetCurrentScope()!.AddRendered(attr); } internal void AddUnrendered(XmlAttribute attr) { - GetCurrentScope().AddUnrendered(attr); + //red flag + GetCurrentScope()!.AddUnrendered(attr); } } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs index cfde60fc31ddb..e3a21d60a1614 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs @@ -12,15 +12,15 @@ namespace System.Security.Cryptography.Xml public class KeyInfoX509Data : KeyInfoClause { // An array of certificates representing the certificate chain - private ArrayList _certificates; + private ArrayList? _certificates; // An array of issuer serial structs - private ArrayList _issuerSerials; + private ArrayList? _issuerSerials; // An array of SKIs - private ArrayList _subjectKeyIds; + private ArrayList? _subjectKeyIds; // An array of subject names - private ArrayList _subjectNames; + private ArrayList? _subjectNames; // A raw byte data representing a certificate revocation list - private byte[] _CRL; + private byte[]? _CRL; // // public constructors @@ -97,7 +97,7 @@ public KeyInfoX509Data(X509Certificate cert, X509IncludeOption includeOption) // public properties // - public ArrayList Certificates + public ArrayList? Certificates { get { return _certificates; } } @@ -132,7 +132,7 @@ public void AddSubjectKeyId(string subjectKeyId) _subjectKeyIds.Add(Utils.DecodeHexString(subjectKeyId)); } - public ArrayList SubjectNames + public ArrayList? SubjectNames { get { return _subjectNames; } } @@ -143,7 +143,7 @@ public void AddSubjectName(string subjectName) _subjectNames.Add(subjectName); } - public ArrayList IssuerSerials + public ArrayList? IssuerSerials { get { return _issuerSerials; } } @@ -171,7 +171,7 @@ internal void InternalAddIssuerSerial(string issuerName, string serialNumber) _issuerSerials.Add(Utils.CreateX509IssuerSerial(issuerName, serialNumber)); } - public byte[] CRL + public byte[]? CRL { get { return _CRL; } set { _CRL = value; } @@ -270,11 +270,13 @@ public override void LoadXml(XmlElement element) XmlNamespaceManager nsm = new XmlNamespaceManager(element.OwnerDocument.NameTable); nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); - XmlNodeList x509IssuerSerialNodes = element.SelectNodes("ds:X509IssuerSerial", nsm); - XmlNodeList x509SKINodes = element.SelectNodes("ds:X509SKI", nsm); - XmlNodeList x509SubjectNameNodes = element.SelectNodes("ds:X509SubjectName", nsm); - XmlNodeList x509CertificateNodes = element.SelectNodes("ds:X509Certificate", nsm); - XmlNodeList x509CRLNodes = element.SelectNodes("ds:X509CRL", nsm); + //red flag - no existing null checks before usage, so should we assume that it's not null, + //or should we add checks ourselves? (or is the dammit operator fine here?) + XmlNodeList x509IssuerSerialNodes = element.SelectNodes("ds:X509IssuerSerial", nsm)!; + XmlNodeList x509SKINodes = element.SelectNodes("ds:X509SKI", nsm)!; + XmlNodeList x509SubjectNameNodes = element.SelectNodes("ds:X509SubjectName", nsm)!; + XmlNodeList x509CertificateNodes = element.SelectNodes("ds:X509Certificate", nsm)!; + XmlNodeList x509CRLNodes = element.SelectNodes("ds:X509CRL", nsm)!; if ((x509CRLNodes.Count == 0 && x509IssuerSerialNodes.Count == 0 && x509SKINodes.Count == 0 && x509SubjectNameNodes.Count == 0 && x509CertificateNodes.Count == 0)) // Bad X509Data tag, or Empty tag diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoX509DataTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoX509DataTest.cs index cf3092e87ccba..81aad6bc59d89 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoX509DataTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoX509DataTest.cs @@ -61,7 +61,7 @@ public class KeyInfoX509DataTest 0x68,0xB3,0xCA,0xDC,0xD3,0xF9,0x0C,0xC0,0x66,0xDF,0x85,0x84,0x4B,0x20,0x5D,0x41,0xAC,0xC0,0xEC,0x37,0x92,0x0E,0x97,0x19,0xBF,0x53,0x35,0x63,0x27,0x18,0x33,0x35,0x42,0x4D,0xF0,0x2D,0x6D,0xA7,0xA4,0x98,0xAA,0x57,0xF3,0xD2,0xB8,0x6E,0x4E,0x8F,0xFF,0xBE,0x6F,0x4E,0x0F,0x0B,0x44,0x24,0xEE,0xDF,0x4C,0x22,0x5B,0x44,0x98,0x94,0xCB,0xB8,0xA3,0x2F,0x30,0x2D,0x30,0x1D,0x06,0x03,0x55,0x1D,0x0E,0x04,0x16,0x04,0x14,0x9D,0x2D,0x73,0xC3,0xB8,0xE3,0x4D,0x29,0x28,0xC3,0x65,0xBE,0xA9,0x98,0xCB,0xD6,0x8A,0x06,0x68, 0x9C,0x30,0x0C,0x06,0x03,0x55,0x1D,0x13,0x04,0x05,0x30,0x03,0x01,0x01,0xFF,0x30,0x09,0x06,0x07,0x2A,0x86,0x48,0xCE,0x38,0x04,0x03,0x03,0x2F,0x00,0x30,0x2C,0x02,0x14,0x5A,0x1B,0x2D,0x08,0x0E,0xE6,0x99,0x38,0x8F,0xB5,0x09,0xC9,0x89,0x79,0x7E,0x01,0x30,0xBD,0xCE,0xF0,0x02,0x14,0x71,0x7B,0x08,0x51,0x97,0xCE,0x4D,0x1F,0x6A,0x84,0x47,0x3A,0xC0,0xBD,0x13,0x89,0x81,0xB9,0x01,0x97 }; - static byte[] x509crl = { 0x30, 0x82, 0x01, 0x05, 0x30, 0x72, 0x02, 0x01, 0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x51, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x1A, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x41, 0x72, 0x6D, 0x65, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x63, 0x65, 0x73, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x7D, 0xA2, 0xD1, 0x19, 0x6D, 0x0F, 0x0F, 0xCB, 0xE4, 0xA3, 0xBE, 0xE0, 0x36, 0x0A, 0xF3, 0x4C, 0x9B, 0xAF, 0xE6, 0x4F, 0xF6, 0xE3, 0xAF, 0xCF, 0x55, 0xF3, 0xC6, 0xDB, 0xAB, 0x4C, 0x16, 0x32, 0xAA, 0x73, 0xAD, 0xCC, 0xDC, 0x32, 0x33, 0x60, 0xDF, 0x8B, 0xCC, 0x93, 0xB5, 0x4F, 0x6A, 0xEC, 0x70, 0x53, 0xAF, 0xCF, 0x07, 0x0F, 0xA0, 0xCD, 0x66, 0xAC, 0x00, 0x57, 0xC6, 0x5C, 0x5D, 0x21, 0xB1, 0xBD, 0x30, 0x89, 0x8E, 0x77, 0x8D, 0xD4, 0x69, 0x7E, 0xC0, 0x36, 0x7E, 0xD2, 0xD8, 0x20, 0x71, 0x08, 0x80, 0xD2, 0xCB, 0x74, 0x8B, 0xD8, 0x42, 0x17, 0x04, 0x99, 0x80, 0xA4, 0x52, 0x70, 0x2E, 0xC0, 0xE3, 0x8C, 0x0B, 0xFF, 0x79, 0xB7, 0x45, 0x77, 0xDC, 0xC5, 0xCF, 0x43, 0x98, 0x91, 0x7D, 0xF1, 0x01, 0xF7, 0x53, 0xD7, 0xC6, 0x51, 0x35, 0xF0, 0x89, 0xCC, 0xC1, 0xFF, 0xE2, 0x89 }; + static byte[]? x509crl = { 0x30, 0x82, 0x01, 0x05, 0x30, 0x72, 0x02, 0x01, 0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x51, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x1A, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x41, 0x72, 0x6D, 0x65, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x63, 0x65, 0x73, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x7D, 0xA2, 0xD1, 0x19, 0x6D, 0x0F, 0x0F, 0xCB, 0xE4, 0xA3, 0xBE, 0xE0, 0x36, 0x0A, 0xF3, 0x4C, 0x9B, 0xAF, 0xE6, 0x4F, 0xF6, 0xE3, 0xAF, 0xCF, 0x55, 0xF3, 0xC6, 0xDB, 0xAB, 0x4C, 0x16, 0x32, 0xAA, 0x73, 0xAD, 0xCC, 0xDC, 0x32, 0x33, 0x60, 0xDF, 0x8B, 0xCC, 0x93, 0xB5, 0x4F, 0x6A, 0xEC, 0x70, 0x53, 0xAF, 0xCF, 0x07, 0x0F, 0xA0, 0xCD, 0x66, 0xAC, 0x00, 0x57, 0xC6, 0x5C, 0x5D, 0x21, 0xB1, 0xBD, 0x30, 0x89, 0x8E, 0x77, 0x8D, 0xD4, 0x69, 0x7E, 0xC0, 0x36, 0x7E, 0xD2, 0xD8, 0x20, 0x71, 0x08, 0x80, 0xD2, 0xCB, 0x74, 0x8B, 0xD8, 0x42, 0x17, 0x04, 0x99, 0x80, 0xA4, 0x52, 0x70, 0x2E, 0xC0, 0xE3, 0x8C, 0x0B, 0xFF, 0x79, 0xB7, 0x45, 0x77, 0xDC, 0xC5, 0xCF, 0x43, 0x98, 0x91, 0x7D, 0xF1, 0x01, 0xF7, 0x53, 0xD7, 0xC6, 0x51, 0x35, 0xF0, 0x89, 0xCC, 0xC1, 0xFF, 0xE2, 0x89 }; [Fact] public void Constructor_Empty() From 7146969e99029581e3a189addc0b0a1bd48c7cfa Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Sun, 27 Mar 2022 10:08:33 +0100 Subject: [PATCH 03/67] Rever mistaken change to test --- .../tests/KeyInfoX509DataTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoX509DataTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoX509DataTest.cs index 81aad6bc59d89..cf3092e87ccba 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoX509DataTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoX509DataTest.cs @@ -61,7 +61,7 @@ public class KeyInfoX509DataTest 0x68,0xB3,0xCA,0xDC,0xD3,0xF9,0x0C,0xC0,0x66,0xDF,0x85,0x84,0x4B,0x20,0x5D,0x41,0xAC,0xC0,0xEC,0x37,0x92,0x0E,0x97,0x19,0xBF,0x53,0x35,0x63,0x27,0x18,0x33,0x35,0x42,0x4D,0xF0,0x2D,0x6D,0xA7,0xA4,0x98,0xAA,0x57,0xF3,0xD2,0xB8,0x6E,0x4E,0x8F,0xFF,0xBE,0x6F,0x4E,0x0F,0x0B,0x44,0x24,0xEE,0xDF,0x4C,0x22,0x5B,0x44,0x98,0x94,0xCB,0xB8,0xA3,0x2F,0x30,0x2D,0x30,0x1D,0x06,0x03,0x55,0x1D,0x0E,0x04,0x16,0x04,0x14,0x9D,0x2D,0x73,0xC3,0xB8,0xE3,0x4D,0x29,0x28,0xC3,0x65,0xBE,0xA9,0x98,0xCB,0xD6,0x8A,0x06,0x68, 0x9C,0x30,0x0C,0x06,0x03,0x55,0x1D,0x13,0x04,0x05,0x30,0x03,0x01,0x01,0xFF,0x30,0x09,0x06,0x07,0x2A,0x86,0x48,0xCE,0x38,0x04,0x03,0x03,0x2F,0x00,0x30,0x2C,0x02,0x14,0x5A,0x1B,0x2D,0x08,0x0E,0xE6,0x99,0x38,0x8F,0xB5,0x09,0xC9,0x89,0x79,0x7E,0x01,0x30,0xBD,0xCE,0xF0,0x02,0x14,0x71,0x7B,0x08,0x51,0x97,0xCE,0x4D,0x1F,0x6A,0x84,0x47,0x3A,0xC0,0xBD,0x13,0x89,0x81,0xB9,0x01,0x97 }; - static byte[]? x509crl = { 0x30, 0x82, 0x01, 0x05, 0x30, 0x72, 0x02, 0x01, 0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x51, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x1A, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x41, 0x72, 0x6D, 0x65, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x63, 0x65, 0x73, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x7D, 0xA2, 0xD1, 0x19, 0x6D, 0x0F, 0x0F, 0xCB, 0xE4, 0xA3, 0xBE, 0xE0, 0x36, 0x0A, 0xF3, 0x4C, 0x9B, 0xAF, 0xE6, 0x4F, 0xF6, 0xE3, 0xAF, 0xCF, 0x55, 0xF3, 0xC6, 0xDB, 0xAB, 0x4C, 0x16, 0x32, 0xAA, 0x73, 0xAD, 0xCC, 0xDC, 0x32, 0x33, 0x60, 0xDF, 0x8B, 0xCC, 0x93, 0xB5, 0x4F, 0x6A, 0xEC, 0x70, 0x53, 0xAF, 0xCF, 0x07, 0x0F, 0xA0, 0xCD, 0x66, 0xAC, 0x00, 0x57, 0xC6, 0x5C, 0x5D, 0x21, 0xB1, 0xBD, 0x30, 0x89, 0x8E, 0x77, 0x8D, 0xD4, 0x69, 0x7E, 0xC0, 0x36, 0x7E, 0xD2, 0xD8, 0x20, 0x71, 0x08, 0x80, 0xD2, 0xCB, 0x74, 0x8B, 0xD8, 0x42, 0x17, 0x04, 0x99, 0x80, 0xA4, 0x52, 0x70, 0x2E, 0xC0, 0xE3, 0x8C, 0x0B, 0xFF, 0x79, 0xB7, 0x45, 0x77, 0xDC, 0xC5, 0xCF, 0x43, 0x98, 0x91, 0x7D, 0xF1, 0x01, 0xF7, 0x53, 0xD7, 0xC6, 0x51, 0x35, 0xF0, 0x89, 0xCC, 0xC1, 0xFF, 0xE2, 0x89 }; + static byte[] x509crl = { 0x30, 0x82, 0x01, 0x05, 0x30, 0x72, 0x02, 0x01, 0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x51, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x1A, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x41, 0x72, 0x6D, 0x65, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x63, 0x65, 0x73, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x7D, 0xA2, 0xD1, 0x19, 0x6D, 0x0F, 0x0F, 0xCB, 0xE4, 0xA3, 0xBE, 0xE0, 0x36, 0x0A, 0xF3, 0x4C, 0x9B, 0xAF, 0xE6, 0x4F, 0xF6, 0xE3, 0xAF, 0xCF, 0x55, 0xF3, 0xC6, 0xDB, 0xAB, 0x4C, 0x16, 0x32, 0xAA, 0x73, 0xAD, 0xCC, 0xDC, 0x32, 0x33, 0x60, 0xDF, 0x8B, 0xCC, 0x93, 0xB5, 0x4F, 0x6A, 0xEC, 0x70, 0x53, 0xAF, 0xCF, 0x07, 0x0F, 0xA0, 0xCD, 0x66, 0xAC, 0x00, 0x57, 0xC6, 0x5C, 0x5D, 0x21, 0xB1, 0xBD, 0x30, 0x89, 0x8E, 0x77, 0x8D, 0xD4, 0x69, 0x7E, 0xC0, 0x36, 0x7E, 0xD2, 0xD8, 0x20, 0x71, 0x08, 0x80, 0xD2, 0xCB, 0x74, 0x8B, 0xD8, 0x42, 0x17, 0x04, 0x99, 0x80, 0xA4, 0x52, 0x70, 0x2E, 0xC0, 0xE3, 0x8C, 0x0B, 0xFF, 0x79, 0xB7, 0x45, 0x77, 0xDC, 0xC5, 0xCF, 0x43, 0x98, 0x91, 0x7D, 0xF1, 0x01, 0xF7, 0x53, 0xD7, 0xC6, 0x51, 0x35, 0xF0, 0x89, 0xCC, 0xC1, 0xFF, 0xE2, 0x89 }; [Fact] public void Constructor_Empty() From e3d32f5c9419882795d2c4271b5bd6a4ea1f9e0c Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Sun, 27 Mar 2022 10:21:34 +0100 Subject: [PATCH 04/67] More annotations --- .../Cryptography/Xml/ReferenceList.cs | 2 +- .../Security/Cryptography/Xml/Transform.cs | 2 +- .../Xml/XmlDecryptionTransform.cs | 2 +- .../Xml/XmlDsigBase64Transform.cs | 2 +- .../Cryptography/Xml/XmlDsigC14NTransform.cs | 2 +- .../Xml/XmlDsigEnvelopedSignatureTransform.cs | 2 +- .../Xml/XmlDsigExcC14NTransform.cs | 4 +-- .../Cryptography/Xml/XmlDsigXPathTransform.cs | 4 +-- .../Cryptography/Xml/XmlDsigXsltTransform.cs | 2 +- .../Cryptography/Xml/XmlLicenseTransform.cs | 30 +++++++++++-------- 10 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs index b370daa218bb9..01f9e202a2520 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs @@ -75,7 +75,7 @@ public void RemoveAt(int index) _references.RemoveAt(index); } - public EncryptedReference Item(int index) + public EncryptedReference? Item(int index) { return (EncryptedReference)_references[index]; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs index 45a05d6d062b4..5a7770ebc5758 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs @@ -141,7 +141,7 @@ internal XmlElement GetXml(XmlDocument document, string name) public abstract void LoadInnerXml(XmlNodeList nodeList); - protected abstract XmlNodeList GetInnerXml(); + protected abstract XmlNodeList? GetInnerXml(); public abstract void LoadInput(object obj); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs index b31bf6a700365..b7c69da72ad7b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs @@ -109,7 +109,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) } } - protected override XmlNodeList GetInnerXml() + protected override XmlNodeList? GetInnerXml() { if (ExceptUris.Count == 0) return null; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigBase64Transform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigBase64Transform.cs index ed1b2b16db26e..bfb26b0b1b5ba 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigBase64Transform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigBase64Transform.cs @@ -33,7 +33,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) { } - protected override XmlNodeList GetInnerXml() + protected override XmlNodeList? GetInnerXml() { return null; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs index d70778e8fcf37..1f49aa94ea63c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs @@ -40,7 +40,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform); } - protected override XmlNodeList GetInnerXml() + protected override XmlNodeList? GetInnerXml() { return null; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs index 3aa5b2b5cbb06..a86e1df72cca4 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs @@ -51,7 +51,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) } // An enveloped signature has no inner XML elements - protected override XmlNodeList GetInnerXml() + protected override XmlNodeList? GetInnerXml() { return null; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs index e0007c207fc25..c76dc498557d1 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs @@ -11,7 +11,7 @@ public class XmlDsigExcC14NTransform : Transform private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlDocument), typeof(XmlNodeList) }; private readonly Type[] _outputTypes = { typeof(Stream) }; private readonly bool _includeComments; - private string _inclusiveNamespacesPrefixList; + private string? _inclusiveNamespacesPrefixList; private ExcCanonicalXml _excCanonicalXml; public XmlDsigExcC14NTransform() : this(false, null) { } @@ -91,7 +91,7 @@ public override void LoadInput(object obj) throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(obj)); } - protected override XmlNodeList GetInnerXml() + protected override XmlNodeList? GetInnerXml() { if (InclusiveNamespacesPrefixList == null) return null; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs index bd5675c3640fa..2bb0211dbadcd 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs @@ -15,7 +15,7 @@ public class XmlDsigXPathTransform : Transform private readonly Type[] _outputTypes = { typeof(XmlNodeList) }; private string _xpathexpr; private XmlDocument _document; - private XmlNamespaceManager _nsm; + private XmlNamespaceManager? _nsm; public XmlDsigXPathTransform() { @@ -83,7 +83,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform); } - protected override XmlNodeList GetInnerXml() + protected override XmlNodeList? GetInnerXml() { XmlDocument document = new XmlDocument(); XmlElement element = document.CreateElement(null, "XPath", SignedXml.XmlDsigNamespaceUrl); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs index 486888f48f13d..b702110fd1960 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs @@ -72,7 +72,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) _xslFragment = firstDataElement.OuterXml.Trim(null); } - protected override XmlNodeList GetInnerXml() + protected override XmlNodeList? GetInnerXml() { return _xslNodes; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs index 5d4ec348f7288..daaeab2e98dcc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs @@ -39,18 +39,19 @@ public IRelDecryptor Decryptor private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecryptor decryptor) { - XmlElement encryptionMethod; - XmlElement keyInfo; - XmlElement cipherData; + XmlElement? encryptionMethod; + XmlElement? keyInfo; + XmlElement? cipherData; EncryptionMethod encryptionMethodObj; KeyInfo keyInfoObj; CipherData cipherDataObj; for (int i = 0, count = encryptedGrantList.Count; i < count; i++) { - encryptionMethod = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/enc:EncryptionMethod", _namespaceManager) as XmlElement; - keyInfo = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/dsig:KeyInfo", _namespaceManager) as XmlElement; - cipherData = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/enc:CipherData", _namespaceManager) as XmlElement; + //red flag + encryptionMethod = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/enc:EncryptionMethod", _namespaceManager) as XmlElement; + keyInfo = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/dsig:KeyInfo", _namespaceManager) as XmlElement; + cipherData = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/enc:CipherData", _namespaceManager) as XmlElement; if ((encryptionMethod != null) && (keyInfo != null) && (cipherData != null)) @@ -63,9 +64,9 @@ private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecrypto keyInfoObj.LoadXml(keyInfo); cipherDataObj.LoadXml(cipherData); - MemoryStream toDecrypt = null; - Stream decryptedContent = null; - StreamReader streamReader = null; + MemoryStream? toDecrypt = null; + Stream? decryptedContent = null; + StreamReader? streamReader = null; try { @@ -79,7 +80,8 @@ private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecrypto streamReader = new StreamReader(decryptedContent); string clearContent = streamReader.ReadToEnd(); - encryptedGrantList[i].ParentNode.InnerXml = clearContent; + // red flag + encryptedGrantList[i]!.ParentNode.InnerXml = clearContent; } finally { @@ -92,7 +94,7 @@ private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecrypto } // License transform has no inner XML elements - protected override XmlNodeList GetInnerXml() + protected override XmlNodeList? GetInnerXml() { return null; } @@ -147,12 +149,14 @@ public override void LoadInput(object obj) if (currentLicenseContext == null) throw new CryptographicException(SR.Cryptography_Xml_XrmlMissingLicence); - XmlNodeList issuerList = currentLicenseContext.SelectNodes("descendant-or-self::r:license[1]/r:issuer", _namespaceManager); + //red flag + XmlNodeList issuerList = currentLicenseContext.SelectNodes("descendant-or-self::r:license[1]/r:issuer", _namespaceManager)!; // Remove all issuer nodes except current for (int i = 0, count = issuerList.Count; i < count; i++) { - if (issuerList[i] == currentIssuerContext) + //red flag + if (issuerList[i]! == currentIssuerContext) continue; if ((issuerList[i].LocalName == ElementIssuer) && From 78c81140c3b065c8d6455ecfd5d5bde1c165bbb7 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Sun, 27 Mar 2022 12:55:31 +0100 Subject: [PATCH 05/67] More annotations --- .../C14NAncestralNamespaceContextManager.cs | 10 ++--- .../Security/Cryptography/Xml/CanonicalXml.cs | 10 ++--- .../Cryptography/Xml/CanonicalXmlAttribute.cs | 2 +- .../Cryptography/Xml/CanonicalXmlElement.cs | 8 ++-- .../Security/Cryptography/Xml/CipherData.cs | 8 ++-- .../Cryptography/Xml/CipherReference.cs | 4 +- .../Cryptography/Xml/CryptoHelpers.cs | 4 +- .../Security/Cryptography/Xml/DSAKeyValue.cs | 24 +++++------ .../Xml/DSASignatureDescription.cs | 4 +- .../Cryptography/Xml/EncryptedData.cs | 20 +++++----- .../Security/Cryptography/Xml/EncryptedKey.cs | 40 +++++++++---------- .../Cryptography/Xml/EncryptedReference.cs | 4 +- .../Security/Cryptography/Xml/EncryptedXml.cs | 18 ++++----- .../Cryptography/Xml/SignedXmlDebugLog.cs | 2 +- .../Cryptography/Xml/SymmetricKeyWrap.cs | 20 +++++----- .../Security/Cryptography/Xml/Transform.cs | 14 +++---- .../Cryptography/Xml/TransformChain.cs | 4 +- .../System/Security/Cryptography/Xml/Utils.cs | 4 +- 18 files changed, 100 insertions(+), 100 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs index 8dcde4c2298d3..90ee686feaa91 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs @@ -26,8 +26,8 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender, } int rDepth; - XmlAttribute local = (XmlAttribute)nsLocallyDeclared[nsPrefix]; - XmlAttribute rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth); + XmlAttribute? local = (XmlAttribute)nsLocallyDeclared[nsPrefix]; + XmlAttribute? rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth); if (local != null) { if (Utils.IsNonRedundantNamespaceDecl(local, rAncestral)) @@ -42,7 +42,7 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender, else { int uDepth; - XmlAttribute uAncestral = GetNearestUnrenderedNamespaceWithMatchingPrefix(nsPrefix, out uDepth); + XmlAttribute? uAncestral = GetNearestUnrenderedNamespaceWithMatchingPrefix(nsPrefix, out uDepth); if (uAncestral != null && uDepth > rDepth && Utils.IsNonRedundantNamespaceDecl(uAncestral, rAncestral)) { if (Utils.IsXmlNamespaceNode(uAncestral)) @@ -62,7 +62,7 @@ internal override void GetNamespacesToRender(XmlElement element, SortedList attr { attrib = (XmlAttribute)a; int rDepth; - XmlAttribute rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(Utils.GetNamespacePrefix(attrib), out rDepth); + XmlAttribute? rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(Utils.GetNamespacePrefix(attrib), out rDepth); if (Utils.IsNonRedundantNamespaceDecl(attrib, rAncestral)) { nsLocallyDeclared.Remove(Utils.GetNamespacePrefix(attrib)); @@ -75,7 +75,7 @@ internal override void GetNamespacesToRender(XmlElement element, SortedList attr for (int i = _ancestorStack.Count - 1; i >= 0; i--) { - foreach (object a in GetScopeAt(i).GetUnrendered().Values) + foreach (object a in GetScopeAt(i)!.GetUnrendered().Values) { attrib = (XmlAttribute)a; if (attrib != null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs index a42e1b1770307..6756ca3c468ae 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs @@ -51,7 +51,7 @@ internal CanonicalXml(XmlNodeList nodeList, XmlResolver resolver, bool includeCo throw new ArgumentNullException(nameof(nodeList)); } - XmlDocument doc = Utils.GetOwnerDocument(nodeList); + XmlDocument? doc = Utils.GetOwnerDocument(nodeList); if (doc == null) throw new ArgumentException(nameof(nodeList)); @@ -79,8 +79,8 @@ private static void MarkInclusionStateForNodes(XmlNodeList nodeList, XmlDocument do { - XmlNode currentNode = (XmlNode)elementList[index]; - XmlNode currentNodeCanonical = (XmlNode)elementListCanonical[index]; + XmlNode currentNode = (XmlNode)elementList[index]!; + XmlNode currentNodeCanonical = (XmlNode)elementListCanonical[index]!; XmlNodeList childNodes = currentNode.ChildNodes; XmlNodeList childNodesCanonical = currentNodeCanonical.ChildNodes; for (int i = 0; i < childNodes.Count; i++) @@ -93,14 +93,14 @@ private static void MarkInclusionStateForNodes(XmlNodeList nodeList, XmlDocument MarkNodeAsIncluded(childNodesCanonical[i]); } - XmlAttributeCollection attribNodes = childNodes[i].Attributes; + XmlAttributeCollection? attribNodes = childNodes[i]!.Attributes; if (attribNodes != null) { for (int j = 0; j < attribNodes.Count; j++) { if (Utils.NodeInList(attribNodes[j], nodeList)) { - MarkNodeAsIncluded(childNodesCanonical[i].Attributes.Item(j)); + MarkNodeAsIncluded(childNodesCanonical[i]!.Attributes!.Item(j)!); } } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlAttribute.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlAttribute.cs index 761fc37439adf..c845f1fbd4e85 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlAttribute.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlAttribute.cs @@ -11,7 +11,7 @@ internal sealed class CanonicalXmlAttribute : XmlAttribute, ICanonicalizableNode { private bool _isInNodeSet; - public CanonicalXmlAttribute(string prefix, string localName, string namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState) + public CanonicalXmlAttribute(string? prefix, string localName, string? namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState) : base(prefix, localName, namespaceURI, doc) { IsInNodeSet = defaultNodeSetInclusionState; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index 7d3174b078006..5aff502d7c2c7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -68,11 +68,11 @@ public void Write(StringBuilder strBuilder, DocPosition docPos, AncestralNamespa strBuilder.Append('<').Append(Name); foreach (object attr in nsListToRender.GetKeyList()) { - (attr as CanonicalXmlAttribute).Write(strBuilder, docPos, anc); + (attr as CanonicalXmlAttribute)!.Write(strBuilder, docPos, anc); } foreach (object attr in attrListToRender.GetKeyList()) { - (attr as CanonicalXmlAttribute).Write(strBuilder, docPos, anc); + (attr as CanonicalXmlAttribute)!.Write(strBuilder, docPos, anc); } strBuilder.Append('>'); } @@ -141,11 +141,11 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace hash.TransformBlock(rgbData, 0, rgbData.Length, rgbData, 0); foreach (object attr in nsListToRender.GetKeyList()) { - (attr as CanonicalXmlAttribute).WriteHash(hash, docPos, anc); + (attr as CanonicalXmlAttribute)!.WriteHash(hash, docPos, anc); } foreach (object attr in attrListToRender.GetKeyList()) { - (attr as CanonicalXmlAttribute).WriteHash(hash, docPos, anc); + (attr as CanonicalXmlAttribute)!.WriteHash(hash, docPos, anc); } rgbData = utf8.GetBytes(">"); hash.TransformBlock(rgbData, 0, rgbData.Length, rgbData, 0); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs index af75f20b93713..4d4a1715704f7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs @@ -46,7 +46,7 @@ public CipherReference? CipherReference } } - public byte[] CipherValue + public byte[]? CipherValue { get { return _cipherValue; } set @@ -63,7 +63,7 @@ public byte[] CipherValue public XmlElement GetXml() { - if (CacheValid) return _cachedXml; + if (CacheValid) return _cachedXml!; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; @@ -100,8 +100,8 @@ public void LoadXml(XmlElement value) XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable); nsm.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl); - XmlNode cipherValueNode = value.SelectSingleNode("enc:CipherValue", nsm); - XmlNode cipherReferenceNode = value.SelectSingleNode("enc:CipherReference", nsm); + XmlNode? cipherValueNode = value.SelectSingleNode("enc:CipherValue", nsm); + XmlNode? cipherReferenceNode = value.SelectSingleNode("enc:CipherReference", nsm); if (cipherValueNode != null) { if (cipherReferenceNode != null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs index be4939a4915cc..d02e2f1e84342 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs @@ -41,7 +41,7 @@ internal byte[]? CipherValue public override XmlElement GetXml() { - if (CacheValid) return _cachedXml; + if (CacheValid) return _cachedXml!; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; @@ -79,7 +79,7 @@ public override void LoadXml(XmlElement value) // Transforms XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable); nsm.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl); - XmlNode transformsNode = value.SelectSingleNode("enc:Transforms", nsm); + XmlNode? transformsNode = value.SelectSingleNode("enc:Transforms", nsm); if (transformsNode != null) TransformChain.LoadXml(transformsNode as XmlElement); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CryptoHelpers.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CryptoHelpers.cs index 0f9365f9e0164..1e602141bcfa6 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CryptoHelpers.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CryptoHelpers.cs @@ -7,7 +7,7 @@ internal static class CryptoHelpers { private static readonly char[] _invalidChars = new char[] { ',', '`', '[', '*', '&' }; - public static object CreateFromKnownName(string name) => + public static object? CreateFromKnownName(string name) => name switch { "http://www.w3.org/TR/2001/REC-xml-c14n-20010315" => new XmlDsigC14NTransform(), @@ -38,7 +38,7 @@ public static object CreateFromKnownName(string name) => _ => null, }; - public static T CreateFromName(string name) where T : class + public static T? CreateFromName(string? name) where T : class { if (name == null || name.IndexOfAny(_invalidChars) >= 0) { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs index d1b347e137993..19d2f73817c67 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs @@ -84,19 +84,19 @@ internal override XmlElement GetXml(XmlDocument xmlDocument) XmlElement dsaKeyValueElement = xmlDocument.CreateElement(DSAKeyValueElementName, SignedXml.XmlDsigNamespaceUrl); XmlElement pElement = xmlDocument.CreateElement(PElementName, SignedXml.XmlDsigNamespaceUrl); - pElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(dsaParams.P))); + pElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(dsaParams.P!))); dsaKeyValueElement.AppendChild(pElement); XmlElement qElement = xmlDocument.CreateElement(QElementName, SignedXml.XmlDsigNamespaceUrl); - qElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(dsaParams.Q))); + qElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(dsaParams.Q!))); dsaKeyValueElement.AppendChild(qElement); XmlElement gElement = xmlDocument.CreateElement(GElementName, SignedXml.XmlDsigNamespaceUrl); - gElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(dsaParams.G))); + gElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(dsaParams.G!))); dsaKeyValueElement.AppendChild(gElement); XmlElement yElement = xmlDocument.CreateElement(YElementName, SignedXml.XmlDsigNamespaceUrl); - yElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(dsaParams.Y))); + yElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(dsaParams.Y!))); dsaKeyValueElement.AppendChild(yElement); // Add optional components if present @@ -155,28 +155,28 @@ public override void LoadXml(XmlElement value) XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(value.OwnerDocument.NameTable); xmlNamespaceManager.AddNamespace(xmlDsigNamespacePrefix, SignedXml.XmlDsigNamespaceUrl); - XmlNode dsaKeyValueElement = value.SelectSingleNode($"{xmlDsigNamespacePrefix}:{DSAKeyValueElementName}", xmlNamespaceManager); + XmlNode? dsaKeyValueElement = value.SelectSingleNode($"{xmlDsigNamespacePrefix}:{DSAKeyValueElementName}", xmlNamespaceManager); if (dsaKeyValueElement == null) { throw new CryptographicException(SR.Format(SR.MustContainChildElement, KeyValueElementName, DSAKeyValueElementName)); } - XmlNode yNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{YElementName}", xmlNamespaceManager); + XmlNode? yNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{YElementName}", xmlNamespaceManager); if (yNode == null) throw new CryptographicException(SR.Format(SR.ElementMissing, YElementName)); - XmlNode pNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{PElementName}", xmlNamespaceManager); - XmlNode qNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{QElementName}", xmlNamespaceManager); + XmlNode? pNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{PElementName}", xmlNamespaceManager); + XmlNode? qNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{QElementName}", xmlNamespaceManager); if ((pNode == null && qNode != null) || (pNode != null && qNode == null)) throw new CryptographicException(SR.Format(SR.ElementCombinationMissing, PElementName, QElementName)); - XmlNode gNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{GElementName}", xmlNamespaceManager); - XmlNode jNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{JElementName}", xmlNamespaceManager); + XmlNode? gNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{GElementName}", xmlNamespaceManager); + XmlNode? jNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{JElementName}", xmlNamespaceManager); - XmlNode seedNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{SeedElementName}", xmlNamespaceManager); - XmlNode pgenCounterNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{PgenCounterElementName}", xmlNamespaceManager); + XmlNode? seedNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{SeedElementName}", xmlNamespaceManager); + XmlNode? pgenCounterNode = dsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{PgenCounterElementName}", xmlNamespaceManager); if ((seedNode == null && pgenCounterNode != null) || (seedNode != null && pgenCounterNode == null)) throw new CryptographicException(SR.Format(SR.ElementCombinationMissing, SeedElementName, PgenCounterElementName)); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSASignatureDescription.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSASignatureDescription.cs index 1d3fe08ab8dde..2b423e681e0fb 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSASignatureDescription.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSASignatureDescription.cs @@ -19,7 +19,7 @@ public DSASignatureDescription() public sealed override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key) { - var item = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm); + var item = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm!)!; item.SetKey(key); item.SetHashAlgorithm(HashAlgorithm); return item; @@ -27,7 +27,7 @@ public sealed override AsymmetricSignatureDeformatter CreateDeformatter(Asymmetr public sealed override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key) { - var item = (AsymmetricSignatureFormatter)CryptoConfig.CreateFromName(FormatterAlgorithm); + var item = (AsymmetricSignatureFormatter)CryptoConfig.CreateFromName(FormatterAlgorithm!)!; item.SetKey(key); item.SetHashAlgorithm(HashAlgorithm); return item; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs index 7320dd628118c..594671992f9f4 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs @@ -23,39 +23,39 @@ public override void LoadXml(XmlElement value) MimeType = Utils.GetAttribute(value, "MimeType", EncryptedXml.XmlEncNamespaceUrl); Encoding = Utils.GetAttribute(value, "Encoding", EncryptedXml.XmlEncNamespaceUrl); - XmlNode encryptionMethodNode = value.SelectSingleNode("enc:EncryptionMethod", nsm); + XmlNode? encryptionMethodNode = value.SelectSingleNode("enc:EncryptionMethod", nsm); // EncryptionMethod EncryptionMethod = new EncryptionMethod(); if (encryptionMethodNode != null) - EncryptionMethod.LoadXml(encryptionMethodNode as XmlElement); + EncryptionMethod.LoadXml((encryptionMethodNode as XmlElement)!); // Key Info KeyInfo = new KeyInfo(); - XmlNode keyInfoNode = value.SelectSingleNode("ds:KeyInfo", nsm); + XmlNode? keyInfoNode = value.SelectSingleNode("ds:KeyInfo", nsm); if (keyInfoNode != null) - KeyInfo.LoadXml(keyInfoNode as XmlElement); + KeyInfo.LoadXml((keyInfoNode as XmlElement)!); // CipherData - XmlNode cipherDataNode = value.SelectSingleNode("enc:CipherData", nsm); + XmlNode? cipherDataNode = value.SelectSingleNode("enc:CipherData", nsm); if (cipherDataNode == null) throw new CryptographicException(SR.Cryptography_Xml_MissingCipherData); CipherData = new CipherData(); - CipherData.LoadXml(cipherDataNode as XmlElement); + CipherData.LoadXml((cipherDataNode as XmlElement)!); // EncryptionProperties - XmlNode encryptionPropertiesNode = value.SelectSingleNode("enc:EncryptionProperties", nsm); + XmlNode? encryptionPropertiesNode = value.SelectSingleNode("enc:EncryptionProperties", nsm); if (encryptionPropertiesNode != null) { // Select the EncryptionProperty elements inside the EncryptionProperties element - XmlNodeList encryptionPropertyNodes = encryptionPropertiesNode.SelectNodes("enc:EncryptionProperty", nsm); + XmlNodeList? encryptionPropertyNodes = encryptionPropertiesNode.SelectNodes("enc:EncryptionProperty", nsm); if (encryptionPropertyNodes != null) { foreach (XmlNode node in encryptionPropertyNodes) { EncryptionProperty ep = new EncryptionProperty(); - ep.LoadXml(node as XmlElement); + ep.LoadXml((node as XmlElement)!); EncryptionProperties.Add(ep); } } @@ -67,7 +67,7 @@ public override void LoadXml(XmlElement value) public override XmlElement GetXml() { - if (CacheValid) return (_cachedXml); + if (CacheValid) return (_cachedXml!); XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs index 47f374e9e0297..c8b83cf9a7088 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs @@ -7,9 +7,9 @@ namespace System.Security.Cryptography.Xml { public sealed class EncryptedKey : EncryptedType { - private string _recipient; - private string _carriedKeyName; - private ReferenceList _referenceList; + private string? _recipient; + private string? _carriedKeyName; + private ReferenceList? _referenceList; public EncryptedKey() { } @@ -23,7 +23,7 @@ public string Recipient } } - public string CarriedKeyName + public string? CarriedKeyName { get { return _carriedKeyName; } set @@ -62,74 +62,74 @@ public override void LoadXml(XmlElement value) Encoding = Utils.GetAttribute(value, "Encoding", EncryptedXml.XmlEncNamespaceUrl); Recipient = Utils.GetAttribute(value, "Recipient", EncryptedXml.XmlEncNamespaceUrl); - XmlNode encryptionMethodNode = value.SelectSingleNode("enc:EncryptionMethod", nsm); + XmlNode? encryptionMethodNode = value.SelectSingleNode("enc:EncryptionMethod", nsm); // EncryptionMethod EncryptionMethod = new EncryptionMethod(); if (encryptionMethodNode != null) - EncryptionMethod.LoadXml(encryptionMethodNode as XmlElement); + EncryptionMethod.LoadXml((encryptionMethodNode as XmlElement)!); // Key Info KeyInfo = new KeyInfo(); - XmlNode keyInfoNode = value.SelectSingleNode("ds:KeyInfo", nsm); + XmlNode? keyInfoNode = value.SelectSingleNode("ds:KeyInfo", nsm); if (keyInfoNode != null) - KeyInfo.LoadXml(keyInfoNode as XmlElement); + KeyInfo.LoadXml((keyInfoNode as XmlElement)!); // CipherData - XmlNode cipherDataNode = value.SelectSingleNode("enc:CipherData", nsm); + XmlNode? cipherDataNode = value.SelectSingleNode("enc:CipherData", nsm); if (cipherDataNode == null) throw new CryptographicException(SR.Cryptography_Xml_MissingCipherData); CipherData = new CipherData(); - CipherData.LoadXml(cipherDataNode as XmlElement); + CipherData.LoadXml((cipherDataNode as XmlElement)!); // EncryptionProperties - XmlNode encryptionPropertiesNode = value.SelectSingleNode("enc:EncryptionProperties", nsm); + XmlNode? encryptionPropertiesNode = value.SelectSingleNode("enc:EncryptionProperties", nsm); if (encryptionPropertiesNode != null) { // Select the EncryptionProperty elements inside the EncryptionProperties element - XmlNodeList encryptionPropertyNodes = encryptionPropertiesNode.SelectNodes("enc:EncryptionProperty", nsm); + XmlNodeList? encryptionPropertyNodes = encryptionPropertiesNode.SelectNodes("enc:EncryptionProperty", nsm); if (encryptionPropertyNodes != null) { foreach (XmlNode node in encryptionPropertyNodes) { EncryptionProperty ep = new EncryptionProperty(); - ep.LoadXml(node as XmlElement); + ep.LoadXml((node as XmlElement)!); EncryptionProperties.Add(ep); } } } // CarriedKeyName - XmlNode carriedKeyNameNode = value.SelectSingleNode("enc:CarriedKeyName", nsm); + XmlNode? carriedKeyNameNode = value.SelectSingleNode("enc:CarriedKeyName", nsm); if (carriedKeyNameNode != null) { CarriedKeyName = carriedKeyNameNode.InnerText; } // ReferenceList - XmlNode referenceListNode = value.SelectSingleNode("enc:ReferenceList", nsm); + XmlNode? referenceListNode = value.SelectSingleNode("enc:ReferenceList", nsm); if (referenceListNode != null) { // Select the DataReference elements inside the ReferenceList element - XmlNodeList dataReferenceNodes = referenceListNode.SelectNodes("enc:DataReference", nsm); + XmlNodeList? dataReferenceNodes = referenceListNode.SelectNodes("enc:DataReference", nsm); if (dataReferenceNodes != null) { foreach (XmlNode node in dataReferenceNodes) { DataReference dr = new DataReference(); - dr.LoadXml(node as XmlElement); + dr.LoadXml((node as XmlElement)!); ReferenceList.Add(dr); } } // Select the KeyReference elements inside the ReferenceList element - XmlNodeList keyReferenceNodes = referenceListNode.SelectNodes("enc:KeyReference", nsm); + XmlNodeList? keyReferenceNodes = referenceListNode.SelectNodes("enc:KeyReference", nsm); if (keyReferenceNodes != null) { foreach (XmlNode node in keyReferenceNodes) { KeyReference kr = new KeyReference(); - kr.LoadXml(node as XmlElement); + kr.LoadXml((node as XmlElement)!); ReferenceList.Add(kr); } } @@ -141,7 +141,7 @@ public override void LoadXml(XmlElement value) public override XmlElement GetXml() { - if (CacheValid) return _cachedXml; + if (CacheValid) return _cachedXml!; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index 57232187dc604..f6445d876f0f1 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -8,7 +8,7 @@ namespace System.Security.Cryptography.Xml public abstract class EncryptedReference { private string _uri; - private string _referenceType; + private string? _referenceType; private TransformChain? _transformChain; internal XmlElement? _cachedXml; @@ -54,7 +54,7 @@ public void AddTransform(Transform transform) TransformChain.Add(transform); } - protected string ReferenceType + protected string? ReferenceType { get { return _referenceType; } set diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 72182674669e9..54c04524eb7cc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -68,7 +68,7 @@ public class EncryptedXml private PaddingMode _padding; private CipherMode _mode; private Encoding _encoding; - private string _recipient; + private string? _recipient; private int _xmlDsigSearchDepthCounter; private int _xmlDsigSearchDepth; @@ -204,25 +204,25 @@ private byte[] GetCipherValue(CipherData cipherData) { throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported); } - decInputStream = tc.TransformToOctetStream(_document, _xmlResolver, baseUri); + decInputStream = tc.TransformToOctetStream(_document!, _xmlResolver!, baseUri!); } else if (cipherData.CipherReference.Uri[0] == '#') { string idref = Utils.ExtractIdFromLocalUri(cipherData.CipherReference.Uri); // Serialize - XmlElement idElem = GetIdElement(_document, idref); + XmlElement? idElem = GetIdElement(_document, idref); if (idElem == null || idElem.OuterXml == null) { throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported); } inputStream = new MemoryStream(_encoding.GetBytes(idElem.OuterXml)); - string baseUri = _document?.BaseURI; + string? baseUri = _document?.BaseURI; TransformChain tc = cipherData.CipherReference.TransformChain; if (tc == null) { throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported); } - decInputStream = tc.TransformToOctetStream(inputStream, _xmlResolver, baseUri); + decInputStream = tc.TransformToOctetStream(inputStream, _xmlResolver!, baseUri!); } else { @@ -329,7 +329,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme { foreach (XmlNode encryptedKeyNode in encryptedKeyList) { - XmlElement? encryptedKeyElement = encryptedKeyNode as XmlElement; + XmlElement encryptedKeyElement = (encryptedKeyNode as XmlElement)!; EncryptedKey ek1 = new EncryptedKey(); ek1.LoadXml(encryptedKeyElement); if (ek1.CarriedKeyName == keyName && ek1.Recipient == Recipient) @@ -478,7 +478,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme { ek = kiEncKey.EncryptedKey; // recursively process EncryptedKey elements - byte[] encryptionKey = DecryptEncryptedKey(ek); + byte[]? encryptionKey = DecryptEncryptedKey(ek); if (encryptionKey != null) { // this is a symmetric algorithm for sure @@ -592,8 +592,8 @@ public EncryptedData Encrypt(XmlElement inputElement, string keyName) throw new CryptographicException(SR.Cryptography_Xml_MissingEncryptionKey); // kek is either a SymmetricAlgorithm or an RSA key, otherwise, we wouldn't be able to insert it in the hash table - SymmetricAlgorithm symKey = encryptionKey as SymmetricAlgorithm; - RSA rsa = encryptionKey as RSA; + SymmetricAlgorithm symKey = (encryptionKey as SymmetricAlgorithm)!; + RSA rsa = (encryptionKey as RSA)!; // Create the EncryptedData object, using an AES-256 session key by default. EncryptedData ed = new EncryptedData(); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs index bbe28edd204b8..3895d28e035e3 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs @@ -318,7 +318,7 @@ internal static void LogBeginCheckSignatureFormat(SignedXml signedXml, Func Date: Mon, 28 Mar 2022 06:55:15 +0100 Subject: [PATCH 06/67] More annotations --- .../Cryptography/Xml/CipherReference.cs | 2 +- .../Cryptography/Xml/EncryptedReference.cs | 2 +- .../Security/Cryptography/Xml/EncryptedXml.cs | 34 +++++++++---------- .../Cryptography/Xml/EncryptionMethod.cs | 12 +++---- .../System/Security/Cryptography/Xml/Utils.cs | 6 ++-- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs index d02e2f1e84342..e0fe389105945 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs @@ -81,7 +81,7 @@ public override void LoadXml(XmlElement value) nsm.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl); XmlNode? transformsNode = value.SelectSingleNode("enc:Transforms", nsm); if (transformsNode != null) - TransformChain.LoadXml(transformsNode as XmlElement); + TransformChain.LoadXml((transformsNode as XmlElement)!); // cache the Xml _cachedXml = value; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index f6445d876f0f1..e42eff1b11d4d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -118,7 +118,7 @@ public virtual void LoadXml(XmlElement value) nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); XmlNode? transformsNode = value.SelectSingleNode("ds:Transforms", nsm); if (transformsNode != null) - TransformChain.LoadXml(transformsNode as XmlElement); + TransformChain.LoadXml((transformsNode as XmlElement)!); // cache the Xml _cachedXml = value; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 54c04524eb7cc..d8af93ba13d94 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -297,7 +297,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme // default behaviour is to look for keys defined by an EncryptedKey clause // either directly or through a KeyInfoRetrievalMethod, and key names in the key mapping - public virtual SymmetricAlgorithm? GetDecryptionKey(EncryptedData encryptedData, string symmetricAlgorithmUri) + public virtual SymmetricAlgorithm? GetDecryptionKey(EncryptedData encryptedData, string? symmetricAlgorithmUri) { if (encryptedData is null) { @@ -346,7 +346,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme { string idref = Utils.ExtractIdFromLocalUri(kiRetrievalMethod.Uri); ek = new EncryptedKey(); - ek.LoadXml(GetIdElement(_document, idref)); + ek.LoadXml(GetIdElement(_document, idref)!); break; } kiEncKey = keyInfoEnum.Current as KeyInfoEncryptedKey; @@ -368,11 +368,11 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme throw new CryptographicException(SR.Cryptography_Xml_MissingAlgorithm); symmetricAlgorithmUri = encryptedData.EncryptionMethod.KeyAlgorithm; } - byte[] key = DecryptEncryptedKey(ek); + byte[]? key = DecryptEncryptedKey(ek); if (key == null) throw new CryptographicException(SR.Cryptography_Xml_MissingDecryptionKey); - SymmetricAlgorithm symAlg = CryptoHelpers.CreateFromName(symmetricAlgorithmUri); + SymmetricAlgorithm? symAlg = CryptoHelpers.CreateFromName(symmetricAlgorithmUri); if (symAlg == null) { throw new CryptographicException(SR.Cryptography_Xml_MissingAlgorithm); @@ -432,7 +432,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme X509Certificate2Collection collection = Utils.BuildBagOfCerts(kiX509Data, CertUsageType.Decryption); foreach (X509Certificate2 certificate in collection) { - using (RSA privateKey = certificate.GetRSAPrivateKey()) + using (RSA? privateKey = certificate.GetRSAPrivateKey()) { if (privateKey != null) { @@ -452,7 +452,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme { string idref = Utils.ExtractIdFromLocalUri(kiRetrievalMethod.Uri); ek = new EncryptedKey(); - ek.LoadXml(GetIdElement(_document, idref)); + ek.LoadXml(GetIdElement(_document, idref)!); try { //Following checks if XML dsig processing is in loop and within the limit defined by machine @@ -478,11 +478,11 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme { ek = kiEncKey.EncryptedKey; // recursively process EncryptedKey elements - byte[]? encryptionKey = DecryptEncryptedKey(ek); + byte[]? encryptionKey = DecryptEncryptedKey(ek!); if (encryptionKey != null) { // this is a symmetric algorithm for sure - SymmetricAlgorithm symAlg = CryptoHelpers.CreateFromName(encryptedKey.EncryptionMethod.KeyAlgorithm); + SymmetricAlgorithm? symAlg = CryptoHelpers.CreateFromName(encryptedKey.EncryptionMethod!.KeyAlgorithm); if (symAlg == null) { throw new CryptographicException(SR.Cryptography_Xml_MissingAlgorithm); @@ -540,7 +540,7 @@ public EncryptedData Encrypt(XmlElement inputElement, X509Certificate2 certifica throw new ArgumentNullException(nameof(certificate)); } - using (RSA rsaPublicKey = certificate.GetRSAPublicKey()) + using (RSA? rsaPublicKey = certificate.GetRSAPublicKey()) { if (rsaPublicKey == null) throw new NotSupportedException(SR.NotSupported_KeyAlgorithm); @@ -584,7 +584,7 @@ public EncryptedData Encrypt(XmlElement inputElement, string keyName) throw new ArgumentNullException(nameof(keyName)); } - object encryptionKey = null; + object? encryptionKey = null; if (_keyNameMapping != null) encryptionKey = _keyNameMapping[keyName]; @@ -601,7 +601,7 @@ public EncryptedData Encrypt(XmlElement inputElement, string keyName) ed.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url); // Include the key name in the EncryptedKey KeyInfo. - string encryptionMethod = null; + string? encryptionMethod = null; if (symKey == null) { encryptionMethod = EncryptedXml.XmlEncRSA15Url; @@ -821,14 +821,14 @@ public void ReplaceData(XmlElement inputElement, byte[] decryptedData) } } - XmlNode importedNode = inputElement.OwnerDocument.ImportNode(importDocument.DocumentElement, true); + XmlNode importedNode = inputElement.OwnerDocument.ImportNode(importDocument.DocumentElement!, true); parent.RemoveChild(inputElement); parent.AppendChild(importedNode); } else { - XmlNode dummy = parent.OwnerDocument.CreateElement(parent.Prefix, parent.LocalName, parent.NamespaceURI); + XmlNode dummy = parent.OwnerDocument!.CreateElement(parent.Prefix, parent.LocalName, parent.NamespaceURI); try { @@ -839,10 +839,10 @@ public void ReplaceData(XmlElement inputElement, byte[] decryptedData) dummy.InnerXml = _encoding.GetString(decryptedData); // Move the children of the dummy node up to the parent. - XmlNode child = dummy.FirstChild; - XmlNode sibling = inputElement.NextSibling; + XmlNode? child = dummy.FirstChild; + XmlNode? sibling = inputElement.NextSibling; - XmlNode nextChild = null; + XmlNode? nextChild = null; while (child != null) { nextChild = child.NextSibling; @@ -888,7 +888,7 @@ public static void ReplaceElement(XmlElement inputElement, EncryptedData encrypt inputElement.AppendChild(elemED); break; case false: - XmlNode parentNode = inputElement.ParentNode; + XmlNode parentNode = inputElement.ParentNode!; // remove the input element from the containing document parentNode.ReplaceChild(elemED, inputElement); break; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs index 4ef3bf89a8796..adb6ab4ae894f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs @@ -7,16 +7,16 @@ namespace System.Security.Cryptography.Xml { public class EncryptionMethod { - private XmlElement _cachedXml; + private XmlElement? _cachedXml; private int _keySize; - private string _algorithm; + private string? _algorithm; public EncryptionMethod() { _cachedXml = null; } - public EncryptionMethod(string algorithm) + public EncryptionMethod(string? algorithm) { _algorithm = algorithm; _cachedXml = null; @@ -42,7 +42,7 @@ public int KeySize } } - public string KeyAlgorithm + public string? KeyAlgorithm { get { return _algorithm; } set @@ -54,7 +54,7 @@ public string KeyAlgorithm public XmlElement GetXml() { - if (CacheValid) return (_cachedXml); + if (CacheValid) return (_cachedXml!); XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; @@ -90,7 +90,7 @@ public void LoadXml(XmlElement value) XmlElement encryptionMethodElement = value; _algorithm = Utils.GetAttribute(encryptionMethodElement, "Algorithm", EncryptedXml.XmlEncNamespaceUrl); - XmlNode keySizeNode = value.SelectSingleNode("enc:KeySize", nsm); + XmlNode? keySizeNode = value.SelectSingleNode("enc:KeySize", nsm); if (keySizeNode != null) { KeySize = Convert.ToInt32(Utils.DiscardWhiteSpaces(keySizeNode.InnerText), null); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 1498b3e674140..aaa81484309ea 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -190,7 +190,7 @@ internal static XmlReader PreProcessStreamInput(Stream inputStream, XmlResolver return reader; } - internal static XmlReaderSettings GetSecureXmlReaderSettings(XmlResolver xmlResolver) + internal static XmlReaderSettings GetSecureXmlReaderSettings(XmlResolver? xmlResolver) { XmlReaderSettings settings = new XmlReaderSettings(); settings.XmlResolver = xmlResolver; @@ -426,7 +426,7 @@ internal static Hashtable TokenizePrefixListString(string s) return set; } - internal static string EscapeWhitespaceData(string data) + internal static string EscapeWhitespaceData(string? data) { StringBuilder sb = new StringBuilder(); sb.Append(data); @@ -434,7 +434,7 @@ internal static string EscapeWhitespaceData(string data) return sb.ToString(); } - internal static string EscapeTextData(string data) + internal static string EscapeTextData(string? data) { StringBuilder sb = new StringBuilder(); sb.Append(data); From c8a39832267e980a4b68f2705fedf6b04a2acaaf Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 06:22:17 +0100 Subject: [PATCH 07/67] More annotations --- .../Xml/AncestralNamespaceContextManager.cs | 10 ++-- .../C14NAncestralNamespaceContextManager.cs | 2 +- .../Security/Cryptography/Xml/CanonicalXml.cs | 2 +- .../Cryptography/Xml/CanonicalXmlNodeList.cs | 2 +- .../Cryptography/Xml/CipherReference.cs | 2 +- .../Security/Cryptography/Xml/DSAKeyValue.cs | 8 ++-- .../Security/Cryptography/Xml/EncryptedKey.cs | 2 +- .../Security/Cryptography/Xml/EncryptedXml.cs | 18 +++---- .../Cryptography/Xml/EncryptionProperty.cs | 7 ++- .../Cryptography/Xml/ExcCanonicalXml.cs | 10 ++-- .../Security/Cryptography/Xml/KeyInfo.cs | 10 ++-- .../Security/Cryptography/Xml/KeyInfoName.cs | 6 +-- .../Xml/KeyInfoRetrievalMethod.cs | 4 +- .../Security/Cryptography/Xml/Reference.cs | 4 +- .../Security/Cryptography/Xml/SignedInfo.cs | 10 ++-- .../Security/Cryptography/Xml/SignedXml.cs | 2 +- .../Security/Cryptography/Xml/Transform.cs | 10 ++-- .../Cryptography/Xml/TransformChain.cs | 2 +- .../System/Security/Cryptography/Xml/Utils.cs | 48 +++++++++---------- .../Xml/XmlDsigEnvelopedSignatureTransform.cs | 6 +-- .../Xml/XmlDsigExcC14NTransform.cs | 4 +- 21 files changed, 84 insertions(+), 85 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs index dc3ff66a8f708..a2b8f69dad235 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs @@ -10,12 +10,12 @@ internal abstract class AncestralNamespaceContextManager { internal ArrayList _ancestorStack = new ArrayList(); - internal NamespaceFrame? GetScopeAt(int i) + internal NamespaceFrame GetScopeAt(int i) { - return (NamespaceFrame)_ancestorStack[i]; + return (NamespaceFrame)_ancestorStack[i]!; } - internal NamespaceFrame? GetCurrentScope() + internal NamespaceFrame GetCurrentScope() { return GetScopeAt(_ancestorStack.Count - 1); } @@ -27,7 +27,7 @@ internal abstract class AncestralNamespaceContextManager for (int i = _ancestorStack.Count - 1; i >= 0; i--) { // red flag - if ((attr = GetScopeAt(i)!.GetRendered(nsPrefix)) != null) + if ((attr = GetScopeAt(i).GetRendered(nsPrefix)) != null) { depth = i; return attr; @@ -43,7 +43,7 @@ internal abstract class AncestralNamespaceContextManager for (int i = _ancestorStack.Count - 1; i >= 0; i--) { //red flag - if ((attr = GetScopeAt(i)!.GetUnrendered(nsPrefix)) != null) + if ((attr = GetScopeAt(i).GetUnrendered(nsPrefix)) != null) { depth = i; return attr; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs index 90ee686feaa91..45a55c2c73824 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs @@ -75,7 +75,7 @@ internal override void GetNamespacesToRender(XmlElement element, SortedList attr for (int i = _ancestorStack.Count - 1; i >= 0; i--) { - foreach (object a in GetScopeAt(i)!.GetUnrendered().Values) + foreach (object a in GetScopeAt(i).GetUnrendered().Values) { attrib = (XmlAttribute)a; if (attrib != null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs index 6756ca3c468ae..9a90ad34ba9bf 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs @@ -31,7 +31,7 @@ internal CanonicalXml(Stream inputStream, bool includeComments, XmlResolver reso } internal CanonicalXml(XmlDocument document, XmlResolver resolver) : this(document, resolver, false) { } - internal CanonicalXml(XmlDocument document, XmlResolver resolver, bool includeComments) + internal CanonicalXml(XmlDocument document, XmlResolver? resolver, bool includeComments) { if (document is null) { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs index 239eb54b53e57..1acf90c24282e 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs @@ -17,7 +17,7 @@ internal CanonicalXmlNodeList() public override XmlNode? Item(int index) { - return (XmlNode)_nodeArray[index]; + return (XmlNode)_nodeArray[index]!; } public override IEnumerator GetEnumerator() diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs index e0fe389105945..5fdd29471d63d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs @@ -73,7 +73,7 @@ public override void LoadXml(XmlElement value) } ReferenceType = value.LocalName; - string uri = Utils.GetAttribute(value, "URI", EncryptedXml.XmlEncNamespaceUrl); + string? uri = Utils.GetAttribute(value, "URI", EncryptedXml.XmlEncNamespaceUrl); Uri = uri ?? throw new CryptographicException(SR.Cryptography_Xml_UriRequired); // Transforms diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs index 19d2f73817c67..c60440bafff35 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs @@ -10,7 +10,7 @@ namespace System.Security.Cryptography.Xml { public class DSAKeyValue : KeyInfoClause { - private DSA _key; + private DSA? _key; // // public constructors @@ -23,7 +23,7 @@ public DSAKeyValue() _key = DSA.Create(); } - public DSAKeyValue(DSA key) + public DSAKeyValue(DSA? key) { _key = key; } @@ -32,7 +32,7 @@ public DSAKeyValue(DSA key) // public properties // - public DSA Key + public DSA? Key { get { return _key; } set { _key = value; } @@ -78,7 +78,7 @@ public override XmlElement GetXml() internal override XmlElement GetXml(XmlDocument xmlDocument) { - DSAParameters dsaParams = _key.ExportParameters(false); + DSAParameters dsaParams = _key!.ExportParameters(false); XmlElement keyValueElement = xmlDocument.CreateElement(KeyValueElementName, SignedXml.XmlDsigNamespaceUrl); XmlElement dsaKeyValueElement = xmlDocument.CreateElement(DSAKeyValueElementName, SignedXml.XmlDsigNamespaceUrl); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs index c8b83cf9a7088..1e9e3ecb83593 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs @@ -13,7 +13,7 @@ public sealed class EncryptedKey : EncryptedType public EncryptedKey() { } - public string Recipient + public string? Recipient { get => _recipient ??= string.Empty; // an unspecified value for an XmlAttribute is string.Empty set diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index d8af93ba13d94..849f5818460fe 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -318,9 +318,9 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme if (kiName != null) { // Get the decryption key from the key mapping - string keyName = kiName.Value; + string? keyName = kiName.Value; if ((SymmetricAlgorithm)_keyNameMapping[keyName] != null) - return (SymmetricAlgorithm)_keyNameMapping[keyName]; + return (SymmetricAlgorithm)_keyNameMapping[keyName]!; // try to get it from a CarriedKeyName XmlNamespaceManager nsm = new XmlNamespaceManager(_document.NameTable); nsm.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl); @@ -408,7 +408,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme if (kiName != null) { // Get the decryption key from the key mapping - string keyName = kiName.Value; + string? keyName = kiName.Value; object? kek = _keyNameMapping[keyName]; if (kek != null) { @@ -660,15 +660,15 @@ public void DecryptDocument() // Look for all EncryptedData elements and decrypt them XmlNamespaceManager nsm = new XmlNamespaceManager(_document.NameTable); nsm.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl); - XmlNodeList encryptedDataList = _document.SelectNodes("//enc:EncryptedData", nsm); + XmlNodeList? encryptedDataList = _document.SelectNodes("//enc:EncryptedData", nsm); if (encryptedDataList != null) { foreach (XmlNode encryptedDataNode in encryptedDataList) { - XmlElement encryptedDataElement = encryptedDataNode as XmlElement; + XmlElement encryptedDataElement = (encryptedDataNode as XmlElement)!; EncryptedData ed = new EncryptedData(); ed.LoadXml(encryptedDataElement); - SymmetricAlgorithm symAlg = GetDecryptionKey(ed, null); + SymmetricAlgorithm? symAlg = GetDecryptionKey(ed, null); if (symAlg == null) throw new CryptographicException(SR.Cryptography_Xml_MissingDecryptionKey); byte[] decrypted = DecryptData(ed, symAlg); @@ -693,7 +693,7 @@ public byte[] EncryptData(byte[] plaintext, SymmetricAlgorithm symmetricAlgorith CipherMode origMode = symmetricAlgorithm.Mode; PaddingMode origPadding = symmetricAlgorithm.Padding; - byte[] cipher = null; + byte[]? cipher = null; try { symmetricAlgorithm.Mode = _mode; @@ -761,11 +761,11 @@ public byte[] DecryptData(EncryptedData encryptedData, SymmetricAlgorithm symmet byte[] origIV = symmetricAlgorithm.IV; // read the IV from cipherValue - byte[] decryptionIV = null; + byte[]? decryptionIV = null; if (_mode != CipherMode.ECB) decryptionIV = GetDecryptionIV(encryptedData, null); - byte[] output = null; + byte[]? output = null; try { int lengthIV = 0; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs index 6c79ead3be13a..502f3930c1f37 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs @@ -10,7 +10,7 @@ public sealed class EncryptionProperty { private string? _target; private string? _id; - private XmlElement _elemProp; + private XmlElement? _elemProp; private XmlElement? _cachedXml; // We are being lax here as per the spec @@ -40,7 +40,7 @@ public string? Target get { return _target; } } - public XmlElement PropertyElement + public XmlElement? PropertyElement { get { return _elemProp; } set @@ -75,8 +75,7 @@ public XmlElement GetXml() internal XmlElement GetXml(XmlDocument document) { - //red flag - return (document.ImportNode(_elemProp, true) as XmlElement)!; + return (document.ImportNode(_elemProp!, true) as XmlElement)!; } public void LoadXml(XmlElement value) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs index 4a846539bcccc..e2f2b10d88c3b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs @@ -85,8 +85,8 @@ private static void MarkInclusionStateForNodes(XmlNodeList nodeList, XmlDocument do { - XmlNode currentNode = (XmlNode)elementList[index]; - XmlNode currentNodeCanonical = (XmlNode)elementListCanonical[index]; + XmlNode currentNode = (XmlNode)elementList[index]!; + XmlNode currentNodeCanonical = (XmlNode)elementListCanonical[index]!; XmlNodeList childNodes = currentNode.ChildNodes; XmlNodeList childNodesCanonical = currentNodeCanonical.ChildNodes; for (int i = 0; i < childNodes.Count; i++) @@ -96,17 +96,17 @@ private static void MarkInclusionStateForNodes(XmlNodeList nodeList, XmlDocument if (Utils.NodeInList(childNodes[i], nodeList)) { - MarkNodeAsIncluded(childNodesCanonical[i]); + MarkNodeAsIncluded(childNodesCanonical[i]!); } - XmlAttributeCollection attribNodes = childNodes[i].Attributes; + XmlAttributeCollection? attribNodes = childNodes[i]!.Attributes; if (attribNodes != null) { for (int j = 0; j < attribNodes.Count; j++) { if (Utils.NodeInList(attribNodes[j], nodeList)) { - MarkNodeAsIncluded(childNodesCanonical[i].Attributes.Item(j)); + MarkNodeAsIncluded(childNodesCanonical[i]!.Attributes!.Item(j)!); } } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs index 81bad806cd9be..cee0ecc8f9b73 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs @@ -8,7 +8,7 @@ namespace System.Security.Cryptography.Xml { public class KeyInfo : IEnumerable { - private string _id; + private string? _id; private readonly ArrayList _keyInfoClauses; // @@ -24,7 +24,7 @@ public KeyInfo() // public properties // - public string Id + public string? Id { get { return _id; } set { _id = value; } @@ -49,7 +49,7 @@ internal XmlElement GetXml(XmlDocument xmlDocument) // Add all the clauses that go underneath it for (int i = 0; i < _keyInfoClauses.Count; ++i) { - XmlElement xmlElement = ((KeyInfoClause)_keyInfoClauses[i]).GetXml(xmlDocument); + XmlElement xmlElement = ((KeyInfoClause)_keyInfoClauses[i]!).GetXml(xmlDocument); if (xmlElement != null) { keyInfoElement.AppendChild(xmlElement); @@ -70,10 +70,10 @@ public void LoadXml(XmlElement value) if (!Utils.VerifyAttributes(keyInfoElement, "Id")) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "KeyInfo"); - XmlNode child = keyInfoElement.FirstChild; + XmlNode? child = keyInfoElement.FirstChild; while (child != null) { - XmlElement elem = child as XmlElement; + XmlElement? elem = child as XmlElement; if (elem != null) { // Create the right type of KeyInfoClause; we use a combination of the namespace and tag name (local name) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoName.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoName.cs index 7c39edd82a724..46082bbb0aaeb 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoName.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoName.cs @@ -7,7 +7,7 @@ namespace System.Security.Cryptography.Xml { public class KeyInfoName : KeyInfoClause { - private string _keyName; + private string? _keyName; // // public constructors @@ -15,7 +15,7 @@ public class KeyInfoName : KeyInfoClause public KeyInfoName() : this(null) { } - public KeyInfoName(string keyName) + public KeyInfoName(string? keyName) { Value = keyName; } @@ -24,7 +24,7 @@ public KeyInfoName(string keyName) // public properties // - public string Value + public string? Value { get { return _keyName; } set { _keyName = value; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs index 65b6853437e9a..8e46833bfe635 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs @@ -7,8 +7,8 @@ namespace System.Security.Cryptography.Xml { public class KeyInfoRetrievalMethod : KeyInfoClause { - private string _uri; - private string _type; + private string? _uri; + private string? _type; // // public constructors diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index 05c7625170e5b..bd1970f6cf3e5 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -18,9 +18,9 @@ public class Reference private string _digestMethod; private byte[] _digestValue; private HashAlgorithm _hashAlgorithm; - private readonly object _refTarget; + private readonly object? _refTarget; private readonly ReferenceTargetType _refTargetType; - private XmlElement _cachedXml; + private XmlElement? _cachedXml; private SignedXml _signedXml; internal CanonicalXmlNodeList _namespaces; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs index 6cac8c541c3b0..c4e03d749d72f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs @@ -13,11 +13,11 @@ public class SignedInfo : ICollection private string _signatureMethod; private string _signatureLength; private readonly ArrayList _references; - private XmlElement _cachedXml; - private SignedXml _signedXml; - private Transform _canonicalizationMethodTransform; + private XmlElement? _cachedXml; + private SignedXml? _signedXml; + private Transform? _canonicalizationMethodTransform; - internal SignedXml SignedXml + internal SignedXml? SignedXml { get { return _signedXml; } set { _signedXml = value; } @@ -149,7 +149,7 @@ internal bool CacheValid public XmlElement GetXml() { - if (CacheValid) return _cachedXml; + if (CacheValid) return _cachedXml!; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index df34a9e18944b..cdbe2da9269b4 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -246,7 +246,7 @@ public bool CheckSignature() return CheckSignatureReturningKey(out _); } - public bool CheckSignatureReturningKey(out AsymmetricAlgorithm signingKey) + public bool CheckSignatureReturningKey(out AsymmetricAlgorithm? signingKey) { SignedXmlDebugLog.LogBeginSignatureVerification(this, _context); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs index 85a61322d376d..5f35d78372a37 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs @@ -30,19 +30,19 @@ public abstract class Transform private Hashtable? _propagatedNamespaces; private XmlElement? _context; - internal string BaseURI + internal string? BaseURI { get { return _baseUri; } set { _baseUri = value; } } - internal SignedXml SignedXml + internal SignedXml? SignedXml { get { return _signedXml; } set { _signedXml = value; } } - internal Reference Reference + internal Reference? Reference { get { return _reference; } set { _reference = value; } @@ -64,7 +64,7 @@ public string Algorithm set { _algorithm = value; } } - public XmlResolver Resolver + public XmlResolver? Resolver { internal get { @@ -181,7 +181,7 @@ public Hashtable PropagatedNamespaces if (_propagatedNamespaces != null) return _propagatedNamespaces; - Reference reference = Reference; + Reference? reference = Reference; SignedXml signedXml = (reference == null ? SignedXml : reference.SignedXml); // If the reference is not a Uri reference with a DataObject target, return an empty hashtable. diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index 8c976fd7df3e4..a4fb26b925116 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -150,7 +150,7 @@ internal Stream TransformToOctetStream(object? inputObject, Type inputType, XmlR throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType); } - internal Stream TransformToOctetStream(Stream input, XmlResolver resolver, string baseUri) + internal Stream TransformToOctetStream(Stream? input, XmlResolver resolver, string baseUri) { return TransformToOctetStream(input, typeof(Stream), resolver, baseUri); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index aaa81484309ea..0e58301d3ece1 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -50,10 +50,10 @@ internal static bool IsRedundantNamespace(XmlElement element, string prefix, str throw new ArgumentNullException(nameof(element)); } - XmlNode ancestorNode = ((XmlNode)element).ParentNode; + XmlNode? ancestorNode = ((XmlNode)element).ParentNode; while (ancestorNode != null) { - XmlElement ancestorElement = ancestorNode as XmlElement; + XmlElement? ancestorElement = ancestorNode as XmlElement; if (ancestorElement != null) if (HasNamespace(ancestorElement, prefix, value)) return true; ancestorNode = ancestorNode.ParentNode; @@ -62,9 +62,9 @@ internal static bool IsRedundantNamespace(XmlElement element, string prefix, str return false; } - internal static string GetAttribute(XmlElement element, string localName, string namespaceURI) + internal static string? GetAttribute(XmlElement element, string localName, string namespaceURI) { - string s = (element.HasAttribute(localName) ? element.GetAttribute(localName) : null); + string? s = (element.HasAttribute(localName) ? element.GetAttribute(localName) : null); if (s == null && element.HasAttribute(localName, namespaceURI)) s = element.GetAttribute(localName, namespaceURI); return s; @@ -75,12 +75,12 @@ internal static bool HasAttribute(XmlElement element, string localName, string n return element.HasAttribute(localName) || element.HasAttribute(localName, namespaceURI); } - internal static bool VerifyAttributes(XmlElement element, string expectedAttrName) + internal static bool VerifyAttributes(XmlElement element, string? expectedAttrName) { return VerifyAttributes(element, expectedAttrName == null ? null : new string[] { expectedAttrName }); } - internal static bool VerifyAttributes(XmlElement element, string[] expectedAttrNames) + internal static bool VerifyAttributes(XmlElement element, string[]? expectedAttrNames) { foreach (XmlAttribute attr in element.Attributes) { @@ -118,7 +118,7 @@ internal static bool IsDefaultNamespaceNode(XmlNode n) internal static bool IsEmptyDefaultNamespaceNode(XmlNode n) { - return IsDefaultNamespaceNode(n) && n.Value.Length == 0; + return IsDefaultNamespaceNode(n) && n.Value!.Length == 0; } internal static string GetNamespacePrefix(XmlAttribute a) @@ -249,12 +249,12 @@ internal static XmlDocument PreProcessElementInput(XmlElement elem, XmlResolver internal static XmlDocument DiscardComments(XmlDocument document) { - XmlNodeList nodeList = document.SelectNodes("//comment()"); + XmlNodeList? nodeList = document.SelectNodes("//comment()"); if (nodeList != null) { foreach (XmlNode node1 in nodeList) { - node1.ParentNode.RemoveChild(node1); + node1.ParentNode!.RemoveChild(node1); } } return document; @@ -272,7 +272,7 @@ internal static XmlNodeList AllDescendantNodes(XmlNode node, bool includeComment do { - XmlNode rootNode = (XmlNode)elementList[index]; + XmlNode rootNode = (XmlNode)elementList[index]!; // Add the children nodes XmlNodeList childNodes = rootNode.ChildNodes; if (childNodes != null) @@ -286,10 +286,10 @@ internal static XmlNodeList AllDescendantNodes(XmlNode node, bool includeComment } } // Add the attribute nodes - XmlAttributeCollection attribNodes = rootNode.Attributes; + XmlAttributeCollection? attribNodes = rootNode.Attributes; if (attribNodes != null) { - foreach (XmlNode attribNode in rootNode.Attributes) + foreach (XmlNode attribNode in rootNode.Attributes!) { if (attribNode.LocalName == "xmlns" || attribNode.Prefix == "xmlns") namespaceList.Add(attribNode); @@ -366,8 +366,8 @@ internal static string ExtractIdFromLocalUri(string uri) // This removes all children of an element. internal static void RemoveAllChildren(XmlElement inputElement) { - XmlNode child = inputElement.FirstChild; - XmlNode sibling; + XmlNode? child = inputElement.FirstChild; + XmlNode? sibling; while (child != null) { @@ -384,7 +384,7 @@ internal static void RemoveAllChildren(XmlElement inputElement) internal static long Pump(Stream input, Stream output) { // Use MemoryStream's WriteTo(Stream) method if possible - MemoryStream inputMS = input as MemoryStream; + MemoryStream? inputMS = input as MemoryStream; if (inputMS != null && inputMS.Position == 0) { inputMS.WriteTo(output); @@ -463,7 +463,7 @@ internal static string EscapeAttributeValue(string value) return sb.ToString(); } - internal static XmlDocument GetOwnerDocument(XmlNodeList nodeList) + internal static XmlDocument? GetOwnerDocument(XmlNodeList nodeList) { foreach (XmlNode node in nodeList) { @@ -473,7 +473,7 @@ internal static XmlDocument GetOwnerDocument(XmlNodeList nodeList) return null; } - internal static void AddNamespaces(XmlElement elem, CanonicalXmlNodeList namespaces) + internal static void AddNamespaces(XmlElement elem, CanonicalXmlNodeList? namespaces) { if (namespaces != null) { @@ -504,18 +504,18 @@ internal static void AddNamespaces(XmlElement elem, Hashtable namespaces) } // This method gets the attributes that should be propagated - internal static CanonicalXmlNodeList GetPropagatedAttributes(XmlElement elem) + internal static CanonicalXmlNodeList? GetPropagatedAttributes(XmlElement? elem) { if (elem == null) return null; CanonicalXmlNodeList namespaces = new CanonicalXmlNodeList(); - XmlNode ancestorNode = elem; + XmlNode? ancestorNode = elem; bool bDefNamespaceToAdd = true; while (ancestorNode != null) { - XmlElement ancestorElement = ancestorNode as XmlElement; + XmlElement? ancestorElement = ancestorNode as XmlElement; if (ancestorElement == null) { ancestorNode = ancestorNode.ParentNode; @@ -625,7 +625,7 @@ internal static int GetHexArraySize(byte[] hex) } // Mimic the behavior of the X509IssuerSerial constructor with null and empty checks - internal static X509IssuerSerial CreateX509IssuerSerial(string issuerName, string serialNumber) + internal static X509IssuerSerial CreateX509IssuerSerial(string? issuerName, string? serialNumber) { if (issuerName == null || issuerName.Length == 0) throw new ArgumentException(SR.Arg_EmptyOrNullString, nameof(issuerName)); @@ -642,7 +642,7 @@ internal static X509IssuerSerial CreateX509IssuerSerial(string issuerName, strin internal static X509Certificate2Collection BuildBagOfCerts(KeyInfoX509Data keyInfoX509Data, CertUsageType certUsageType) { X509Certificate2Collection collection = new X509Certificate2Collection(); - ArrayList decryptionIssuerSerials = (certUsageType == CertUsageType.Decryption ? new ArrayList() : null); + ArrayList? decryptionIssuerSerials = (certUsageType == CertUsageType.Decryption ? new ArrayList() : null); if (keyInfoX509Data.Certificates != null) { foreach (X509Certificate2 certificate in keyInfoX509Data.Certificates) @@ -653,7 +653,7 @@ internal static X509Certificate2Collection BuildBagOfCerts(KeyInfoX509Data keyIn collection.Add(certificate); break; case CertUsageType.Decryption: - decryptionIssuerSerials.Add(CreateX509IssuerSerial(certificate.IssuerName.Name, certificate.SerialNumber)); + decryptionIssuerSerials!.Add(CreateX509IssuerSerial(certificate.IssuerName.Name, certificate.SerialNumber)); break; } } @@ -758,7 +758,7 @@ internal static bool IsSelfSigned(X509Chain chain) internal static AsymmetricAlgorithm? GetAnyPublicKey(X509Certificate2 certificate) { - AsymmetricAlgorithm algorithm = (AsymmetricAlgorithm)certificate.GetRSAPublicKey() ?? certificate.GetECDsaPublicKey(); + AsymmetricAlgorithm? algorithm = (AsymmetricAlgorithm)certificate.GetRSAPublicKey()! ?? certificate.GetECDsaPublicKey(); #if NETCOREAPP if (algorithm is null && !OperatingSystem.IsTvOS() && !OperatingSystem.IsIOS()) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs index a86e1df72cca4..42705e2d75b56 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs @@ -10,10 +10,10 @@ public class XmlDsigEnvelopedSignatureTransform : Transform { private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlNodeList), typeof(XmlDocument) }; private readonly Type[] _outputTypes = { typeof(XmlNodeList), typeof(XmlDocument) }; - private XmlNodeList _inputNodeList; + private XmlNodeList? _inputNodeList; private readonly bool _includeComments; - private XmlNamespaceManager _nsm; - private XmlDocument _containingDocument; + private XmlNamespaceManager? _nsm; + private XmlDocument? _containingDocument; private int _signaturePosition; internal int SignaturePosition diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs index c76dc498557d1..d4b298df8eb01 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs @@ -20,14 +20,14 @@ public XmlDsigExcC14NTransform(bool includeComments) : this(includeComments, nul public XmlDsigExcC14NTransform(string inclusiveNamespacesPrefixList) : this(false, inclusiveNamespacesPrefixList) { } - public XmlDsigExcC14NTransform(bool includeComments, string inclusiveNamespacesPrefixList) + public XmlDsigExcC14NTransform(bool includeComments, string? inclusiveNamespacesPrefixList) { _includeComments = includeComments; _inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList; Algorithm = (includeComments ? SignedXml.XmlDsigExcC14NWithCommentsTransformUrl : SignedXml.XmlDsigExcC14NTransformUrl); } - public string InclusiveNamespacesPrefixList + public string? InclusiveNamespacesPrefixList { get { return _inclusiveNamespacesPrefixList; } set { _inclusiveNamespacesPrefixList = value; } From a5a10852251b392d8e6e8cc6a4813d48b778ffac Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 21:10:20 +0100 Subject: [PATCH 08/67] More annotations --- .../Xml/AncestralNamespaceContextManager.cs | 8 ++--- .../C14NAncestralNamespaceContextManager.cs | 8 ++--- .../Security/Cryptography/Xml/CanonicalXml.cs | 6 ++-- .../Cryptography/Xml/CanonicalXmlComment.cs | 2 +- .../Cryptography/Xml/CanonicalXmlElement.cs | 8 ++--- .../Cryptography/Xml/CipherReference.cs | 4 +-- .../Cryptography/Xml/DataReference.cs | 4 +-- .../Cryptography/Xml/EncryptedReference.cs | 10 +++---- .../ExcAncestralNamespaceContextManager.cs | 10 +++---- .../Cryptography/Xml/ExcCanonicalXml.cs | 4 +-- .../Xml/KeyInfoRetrievalMethod.cs | 4 +-- .../Cryptography/Xml/KeyInfoX509Data.cs | 8 ++--- .../Security/Cryptography/Xml/KeyReference.cs | 4 +-- .../Cryptography/Xml/NamespaceFrame.cs | 8 ++--- .../Xml/RSAPKCS1SignatureDescription.cs | 8 ++--- .../Security/Cryptography/Xml/Reference.cs | 30 +++++++++---------- .../Security/Cryptography/Xml/Signature.cs | 4 +-- .../Security/Cryptography/Xml/SignedXml.cs | 22 +++++++------- .../Cryptography/Xml/SignedXmlDebugLog.cs | 14 ++++----- .../Security/Cryptography/Xml/Transform.cs | 4 +-- .../System/Security/Cryptography/Xml/Utils.cs | 20 ++++++------- .../Xml/XmlDecryptionTransform.cs | 2 +- .../Cryptography/Xml/XmlDsigC14NTransform.cs | 2 +- .../Xml/XmlDsigEnvelopedSignatureTransform.cs | 2 +- .../Xml/XmlDsigExcC14NTransform.cs | 2 +- .../tests/KeyInfoRetrievalMethodTest.cs | 2 +- .../tests/ReferenceTest.cs | 4 +-- .../tests/SignedXmlTest.cs | 4 +-- 28 files changed, 104 insertions(+), 104 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs index a2b8f69dad235..04971d778e2f7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs @@ -62,8 +62,8 @@ internal void ExitElementContext() _ancestorStack.RemoveAt(_ancestorStack.Count - 1); } - internal abstract void TrackNamespaceNode(XmlAttribute attr, SortedList nsListToRender, Hashtable nsLocallyDeclared); - internal abstract void TrackXmlNamespaceNode(XmlAttribute attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared); + internal abstract void TrackNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, Hashtable nsLocallyDeclared); + internal abstract void TrackXmlNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared); internal abstract void GetNamespacesToRender(XmlElement element, SortedList attrListToRender, SortedList nsListToRender, Hashtable nsLocallyDeclared); internal void LoadUnrenderedNamespaces(Hashtable nsLocallyDeclared) @@ -84,13 +84,13 @@ internal void LoadRenderedNamespaces(SortedList nsRenderedList) } } - internal void AddRendered(XmlAttribute attr) + internal void AddRendered(XmlAttribute? attr) { //red flag GetCurrentScope()!.AddRendered(attr); } - internal void AddUnrendered(XmlAttribute attr) + internal void AddUnrendered(XmlAttribute? attr) { //red flag GetCurrentScope()!.AddUnrendered(attr); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs index 45a55c2c73824..c96d3cc79727f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs @@ -26,7 +26,7 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender, } int rDepth; - XmlAttribute? local = (XmlAttribute)nsLocallyDeclared[nsPrefix]; + XmlAttribute? local = (XmlAttribute)nsLocallyDeclared[nsPrefix]!; XmlAttribute? rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth); if (local != null) { @@ -55,7 +55,7 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender, internal override void GetNamespacesToRender(XmlElement element, SortedList attrListToRender, SortedList nsListToRender, Hashtable nsLocallyDeclared) { - XmlAttribute attrib; + XmlAttribute? attrib; object[] attrs = new object[nsLocallyDeclared.Count]; nsLocallyDeclared.Values.CopyTo(attrs, 0); foreach (object a in attrs) @@ -84,12 +84,12 @@ internal override void GetNamespacesToRender(XmlElement element, SortedList attr } } - internal override void TrackNamespaceNode(XmlAttribute attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) + internal override void TrackNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) { nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr), attr); } - internal override void TrackXmlNamespaceNode(XmlAttribute attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) + internal override void TrackXmlNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) { nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr), attr); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs index 9a90ad34ba9bf..cd9fb2c5b44f9 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs @@ -90,7 +90,7 @@ private static void MarkInclusionStateForNodes(XmlNodeList nodeList, XmlDocument if (Utils.NodeInList(childNodes[i], nodeList)) { - MarkNodeAsIncluded(childNodesCanonical[i]); + MarkNodeAsIncluded(childNodesCanonical[i]!); } XmlAttributeCollection? attribNodes = childNodes[i]!.Attributes; @@ -117,11 +117,11 @@ internal byte[] GetBytes() return utf8.GetBytes(sb.ToString()); } - internal byte[] GetDigestedBytes(HashAlgorithm hash) + internal byte[]? GetDigestedBytes(HashAlgorithm hash) { _c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr); hash.TransformFinalBlock(Array.Empty(), 0, 0); - byte[] res = (byte[])hash.Hash.Clone(); + byte[]? res = (byte[])hash.Hash.Clone(); // reinitialize the hash so it is still usable after the call hash.Initialize(); return res; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs index 99c4a2fdbcd7f..01b8c9014bcfc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs @@ -55,7 +55,7 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace hash.TransformBlock(rgbData, 0, rgbData.Length, rgbData, 0); rgbData = utf8.GetBytes(""); hash.TransformBlock(rgbData, 0, rgbData.Length, rgbData, 0); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index 5aff502d7c2c7..3c0ffbcd4ea0c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -33,7 +33,7 @@ public void Write(StringBuilder strBuilder, DocPosition docPos, AncestralNamespa XmlAttributeCollection attrList = Attributes; if (attrList != null) { - foreach (XmlAttribute attr in attrList) + foreach (XmlAttribute? attr in attrList) { if (((CanonicalXmlAttribute)attr).IsInNodeSet || Utils.IsNamespaceNode(attr) || Utils.IsXmlNamespaceNode(attr)) { @@ -56,7 +56,7 @@ public void Write(StringBuilder strBuilder, DocPosition docPos, AncestralNamespa if (!Utils.IsCommittedNamespace(this, Prefix, NamespaceURI)) { string name = ((Prefix.Length > 0) ? "xmlns" + ":" + Prefix : "xmlns"); - XmlAttribute nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name); + XmlAttribute? nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name); nsattrib.Value = NamespaceURI; anc.TrackNamespaceNode(nsattrib, nsListToRender, nsLocallyDeclared); } @@ -106,7 +106,7 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace XmlAttributeCollection attrList = Attributes; if (attrList != null) { - foreach (XmlAttribute attr in attrList) + foreach (XmlAttribute? attr in attrList) { if (((CanonicalXmlAttribute)attr).IsInNodeSet || Utils.IsNamespaceNode(attr) || Utils.IsXmlNamespaceNode(attr)) { @@ -129,7 +129,7 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace if (!Utils.IsCommittedNamespace(this, Prefix, NamespaceURI)) { string name = ((Prefix.Length > 0) ? "xmlns" + ":" + Prefix : "xmlns"); - XmlAttribute nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name); + XmlAttribute? nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name); nsattrib.Value = NamespaceURI; anc.TrackNamespaceNode(nsattrib, nsListToRender, nsLocallyDeclared); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs index 5fdd29471d63d..c34da1163806e 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs @@ -14,12 +14,12 @@ public CipherReference() : base() ReferenceType = "CipherReference"; } - public CipherReference(string uri) : base(uri) + public CipherReference(string? uri) : base(uri) { ReferenceType = "CipherReference"; } - public CipherReference(string uri, TransformChain transformChain) : base(uri, transformChain) + public CipherReference(string? uri, TransformChain transformChain) : base(uri, transformChain) { ReferenceType = "CipherReference"; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs index 18ca820c66139..4059826eb8029 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs @@ -10,12 +10,12 @@ public DataReference() : base() ReferenceType = "DataReference"; } - public DataReference(string uri) : base(uri) + public DataReference(string? uri) : base(uri) { ReferenceType = "DataReference"; } - public DataReference(string uri, TransformChain transformChain) : base(uri, transformChain) + public DataReference(string? uri, TransformChain transformChain) : base(uri, transformChain) { ReferenceType = "DataReference"; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index e42eff1b11d4d..c1ce9c443e447 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -7,7 +7,7 @@ namespace System.Security.Cryptography.Xml { public abstract class EncryptedReference { - private string _uri; + private string? _uri; private string? _referenceType; private TransformChain? _transformChain; internal XmlElement? _cachedXml; @@ -16,18 +16,18 @@ public abstract class EncryptedReference { } - protected EncryptedReference(string uri) : this(uri, new TransformChain()) + protected EncryptedReference(string? uri) : this(uri, new TransformChain()) { } - protected EncryptedReference(string uri, TransformChain transformChain) + protected EncryptedReference(string? uri, TransformChain transformChain) { TransformChain = transformChain; Uri = uri; _cachedXml = null; } - public string Uri + public string? Uri { get { return _uri; } set @@ -108,7 +108,7 @@ public virtual void LoadXml(XmlElement value) ReferenceType = value.LocalName; - string uri = Utils.GetAttribute(value, "URI", EncryptedXml.XmlEncNamespaceUrl); + string? uri = Utils.GetAttribute(value, "URI", EncryptedXml.XmlEncNamespaceUrl); if (uri == null) throw new ArgumentNullException(SR.Cryptography_Xml_UriRequired); Uri = uri; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs index 4c5738fcf69ff..6826b4395afec 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs @@ -17,7 +17,7 @@ internal ExcAncestralNamespaceContextManager(string inclusiveNamespacesPrefixLis _inclusivePrefixSet = Utils.TokenizePrefixListString(inclusiveNamespacesPrefixList); } - private bool HasNonRedundantInclusivePrefix(XmlAttribute attr) + private bool HasNonRedundantInclusivePrefix(XmlAttribute? attr) { string nsPrefix = Utils.GetNamespacePrefix(attr); return _inclusivePrefixSet.ContainsKey(nsPrefix) && @@ -33,7 +33,7 @@ private void GatherNamespaceToRender(string nsPrefix, SortedList nsListToRender, } int rDepth; - XmlAttribute local = (XmlAttribute)nsLocallyDeclared[nsPrefix]; + XmlAttribute? local = (XmlAttribute)nsLocallyDeclared[nsPrefix]; XmlAttribute rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth); if (local != null) @@ -47,7 +47,7 @@ private void GatherNamespaceToRender(string nsPrefix, SortedList nsListToRender, else { int uDepth; - XmlAttribute uAncestral = GetNearestUnrenderedNamespaceWithMatchingPrefix(nsPrefix, out uDepth); + XmlAttribute? uAncestral = GetNearestUnrenderedNamespaceWithMatchingPrefix(nsPrefix, out uDepth); if (uAncestral != null && uDepth > rDepth && Utils.IsNonRedundantNamespaceDecl(uAncestral, rAncestral)) { nsListToRender.Add(uAncestral, null); @@ -66,7 +66,7 @@ internal override void GetNamespacesToRender(XmlElement element, SortedList attr } } - internal override void TrackNamespaceNode(XmlAttribute attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) + internal override void TrackNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) { if (!Utils.IsXmlPrefixDefinitionNode(attr)) { @@ -77,7 +77,7 @@ internal override void TrackNamespaceNode(XmlAttribute attr, SortedList nsListTo } } - internal override void TrackXmlNamespaceNode(XmlAttribute attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) + internal override void TrackXmlNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) { // exclusive canonicalization treats Xml namespaces as simple attributes. They are not propagated. attrListToRender.Add(attr, null); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs index e2f2b10d88c3b..cefe182f483bc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs @@ -65,11 +65,11 @@ internal byte[] GetBytes() return utf8.GetBytes(sb.ToString()); } - internal byte[] GetDigestedBytes(HashAlgorithm hash) + internal byte[]? GetDigestedBytes(HashAlgorithm hash) { _c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr); hash.TransformFinalBlock(Array.Empty(), 0, 0); - byte[] res = (byte[])hash.Hash.Clone(); + byte[]? res = (byte[])hash.Hash.Clone(); // reinitialize the hash so it is still usable after the call hash.Initialize(); return res; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs index 8e46833bfe635..9b00310d97cba 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs @@ -16,7 +16,7 @@ public class KeyInfoRetrievalMethod : KeyInfoClause public KeyInfoRetrievalMethod() { } - public KeyInfoRetrievalMethod(string strUri) + public KeyInfoRetrievalMethod(string? strUri) { _uri = strUri; } @@ -31,7 +31,7 @@ public KeyInfoRetrievalMethod(string strUri, string typeName) // public properties // - public string Uri + public string? Uri { get { return _uri; } set { _uri = value; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs index e3a21d60a1614..ce385265050b3 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs @@ -115,7 +115,7 @@ public void AddCertificate(X509Certificate certificate) _certificates.Add(x509); } - public ArrayList SubjectKeyIds + public ArrayList? SubjectKeyIds { get { return _subjectKeyIds; } } @@ -286,12 +286,12 @@ public override void LoadXml(XmlElement element) Clear(); if (x509CRLNodes.Count != 0) - _CRL = Convert.FromBase64String(Utils.DiscardWhiteSpaces(x509CRLNodes.Item(0).InnerText)); + _CRL = Convert.FromBase64String(Utils.DiscardWhiteSpaces(x509CRLNodes.Item(0)!.InnerText)); foreach (XmlNode issuerSerialNode in x509IssuerSerialNodes) { - XmlNode x509IssuerNameNode = issuerSerialNode.SelectSingleNode("ds:X509IssuerName", nsm); - XmlNode x509SerialNumberNode = issuerSerialNode.SelectSingleNode("ds:X509SerialNumber", nsm); + XmlNode? x509IssuerNameNode = issuerSerialNode.SelectSingleNode("ds:X509IssuerName", nsm); + XmlNode? x509SerialNumberNode = issuerSerialNode.SelectSingleNode("ds:X509SerialNumber", nsm); if (x509IssuerNameNode == null || x509SerialNumberNode == null) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "IssuerSerial"); InternalAddIssuerSerial(x509IssuerNameNode.InnerText.Trim(), x509SerialNumberNode.InnerText.Trim()); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs index fd8d8452608da..40471720e7061 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs @@ -10,12 +10,12 @@ public KeyReference() : base() ReferenceType = "KeyReference"; } - public KeyReference(string uri) : base(uri) + public KeyReference(string? uri) : base(uri) { ReferenceType = "KeyReference"; } - public KeyReference(string uri, TransformChain transformChain) : base(uri, transformChain) + public KeyReference(string? uri, TransformChain transformChain) : base(uri, transformChain) { ReferenceType = "KeyReference"; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs index 4aa6ce9bd113b..edb07610ca439 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs @@ -16,24 +16,24 @@ internal sealed class NamespaceFrame internal NamespaceFrame() { } - internal void AddRendered(XmlAttribute attr) + internal void AddRendered(XmlAttribute? attr) { _rendered.Add(Utils.GetNamespacePrefix(attr), attr); } internal XmlAttribute? GetRendered(string nsPrefix) { - return (XmlAttribute)_rendered[nsPrefix]; + return (XmlAttribute?)_rendered[nsPrefix]; } - internal void AddUnrendered(XmlAttribute attr) + internal void AddUnrendered(XmlAttribute? attr) { _unrendered.Add(Utils.GetNamespacePrefix(attr), attr); } internal XmlAttribute? GetUnrendered(string nsPrefix) { - return (XmlAttribute)_unrendered[nsPrefix]; + return (XmlAttribute?)_unrendered[nsPrefix]; } internal Hashtable GetUnrendered() diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs index 5b9f14552f864..7ede048d08ebc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs @@ -15,18 +15,18 @@ public RSAPKCS1SignatureDescription(string hashAlgorithmName) public sealed override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key) { - var item = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm); + var item = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm!)!; //red flag item!.SetKey(key); - item.SetHashAlgorithm(DigestAlgorithm); + item.SetHashAlgorithm(DigestAlgorithm!); return item; } public sealed override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key) { - var item = (AsymmetricSignatureFormatter)CryptoConfig.CreateFromName(FormatterAlgorithm); + var item = (AsymmetricSignatureFormatter)CryptoConfig.CreateFromName(FormatterAlgorithm!)!; item.SetKey(key); - item.SetHashAlgorithm(DigestAlgorithm); + item.SetHashAlgorithm(DigestAlgorithm!); return item; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index bd1970f6cf3e5..40e3453b72ad1 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -11,18 +11,18 @@ public class Reference { internal const string DefaultDigestMethod = SignedXml.XmlDsigSHA256Url; - private string _id; - private string _uri; - private string _type; + private string? _id; + private string? _uri; + private string? _type; private TransformChain _transformChain; private string _digestMethod; - private byte[] _digestValue; - private HashAlgorithm _hashAlgorithm; + private byte[]? _digestValue; + private HashAlgorithm? _hashAlgorithm; private readonly object? _refTarget; private readonly ReferenceTargetType _refTargetType; private XmlElement? _cachedXml; - private SignedXml _signedXml; - internal CanonicalXmlNodeList _namespaces; + private SignedXml? _signedXml; + internal CanonicalXmlNodeList? _namespaces; // // public constructors @@ -46,7 +46,7 @@ public Reference(Stream stream) _digestMethod = DefaultDigestMethod; } - public Reference(string uri) + public Reference(string? uri) { _transformChain = new TransformChain(); _refTarget = uri; @@ -69,13 +69,13 @@ internal Reference(XmlElement element) // public properties // - public string Id + public string? Id { get { return _id; } set { _id = value; } } - public string Uri + public string? Uri { get { return _uri; } set @@ -85,7 +85,7 @@ public string Uri } } - public string Type + public string? Type { get { return _type; } set @@ -105,7 +105,7 @@ public string DigestMethod } } - public byte[] DigestValue + public byte[]? DigestValue { get { return _digestValue; } set @@ -133,7 +133,7 @@ internal bool CacheValid } } - internal SignedXml SignedXml + internal SignedXml? SignedXml { get { return _signedXml; } set { _signedXml = value; } @@ -337,7 +337,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList // What we want to do is pump the input through the TransformChain and then // hash the output of the chain document is the document context for resolving relative references - internal byte[] CalculateHashValue(XmlDocument document, CanonicalXmlNodeList refList) + internal byte[]? CalculateHashValue(XmlDocument document, CanonicalXmlNodeList refList) { // refList is a list of elements that might be targets of references // Now's the time to create our hashing algorithm @@ -351,7 +351,7 @@ internal byte[] CalculateHashValue(XmlDocument document, CanonicalXmlNodeList re WebResponse response = null; Stream inputStream = null; XmlResolver resolver = null; - byte[] hashval = null; + byte[]? hashval = null; try { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Signature.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Signature.cs index e88334f9ee1a6..388f5f95a23de 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Signature.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Signature.cs @@ -10,7 +10,7 @@ public class Signature { private string _id; private SignedInfo _signedInfo; - private byte[] _signatureValue; + private byte[]? _signatureValue; private string _signatureValueId; private KeyInfo _keyInfo; private IList _embeddedObjects; @@ -54,7 +54,7 @@ public SignedInfo SignedInfo } } - public byte[] SignatureValue + public byte[]? SignatureValue { get { return _signatureValue; } set { _signatureValue = value; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index cdbe2da9269b4..ac966d45d0262 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -193,7 +193,7 @@ public string SignatureLength get { return m_signature.SignedInfo.SignatureLength; } } - public byte[] SignatureValue + public byte[]? SignatureValue { get { return m_signature.SignatureValue; } } @@ -273,14 +273,14 @@ public bool CheckSignatureReturningKey(out AsymmetricAlgorithm? signingKey) return bRet; } - public bool CheckSignature(AsymmetricAlgorithm key) + public bool CheckSignature(AsymmetricAlgorithm? key) { if (!CheckSignatureFormat()) { return false; } - if (!CheckSignedInfo(key)) + if (!CheckSignedInfo(key!)) { SignedXmlDebugLog.LogVerificationFailure(this, SR.Log_VerificationFailed_SignedInfo); return false; @@ -358,7 +358,7 @@ public bool CheckSignature(X509Certificate2 certificate, bool verifySignatureOnl } } - using (AsymmetricAlgorithm publicKey = Utils.GetAnyPublicKey(certificate)) + using (AsymmetricAlgorithm? publicKey = Utils.GetAnyPublicKey(certificate)) { if (!CheckSignature(publicKey)) { @@ -451,7 +451,7 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) "RIPEMD160" => SignedXml.XmlDsigMoreHMACRIPEMD160Url, _ => throw new CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch), }; - byte[] hashValue = GetC14NDigest(hash); + byte[]? hashValue = GetC14NDigest(hash); SignedXmlDebugLog.LogSigning(this, hash); m_signature.SignatureValue = new byte[signatureLength / 8]; @@ -603,7 +603,7 @@ private X509Certificate2Collection BuildBagOfCerts() // private bool _bCacheValid; - private byte[] _digestedSignedInfo; + private byte[]? _digestedSignedInfo; private static bool DefaultSignatureFormatValidator(SignedXml signedXml) { @@ -770,7 +770,7 @@ private static IList DefaultSafeTransformMethods } } - private byte[] GetC14NDigest(HashAlgorithm hash) + private byte[]? GetC14NDigest(HashAlgorithm hash) { bool isKeyedHashAlgorithm = hash is KeyedHashAlgorithm; if (isKeyedHashAlgorithm || !_bCacheValid || !SignedInfo.CacheValid) @@ -916,7 +916,7 @@ private bool CheckDigestedReferences() } SignedXmlDebugLog.LogVerifyReference(this, digestedReference); - byte[] calculatedHash; + byte[]? calculatedHash; try { calculatedHash = digestedReference.CalculateHashValue(_containingDocument, m_signature.ReferencedItems); @@ -944,7 +944,7 @@ private bool CheckDigestedReferences() // This method makes no attempt to disguise the length of either of its inputs. It is assumed the attacker has // knowledge of the algorithms used, and thus the output length. Length is difficult to properly blind in modern CPUs. [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] - private static bool CryptographicEquals(byte[] a, byte[] b) + private static bool CryptographicEquals(byte[]? a, byte[]? b) { System.Diagnostics.Debug.Assert(a != null); System.Diagnostics.Debug.Assert(b != null); @@ -1012,7 +1012,7 @@ private bool CheckSignedInfo(AsymmetricAlgorithm key) HashAlgorithm hashAlgorithm = signatureDescription.CreateDigest(); if (hashAlgorithm == null) throw new CryptographicException(SR.Cryptography_Xml_CreateHashAlgorithmFailed); - byte[] hashval = GetC14NDigest(hashAlgorithm); + byte[]? hashval = GetC14NDigest(hashAlgorithm); AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = signatureDescription.CreateDeformatter(key); SignedXmlDebugLog.LogVerifySignedInfo(this, @@ -1051,7 +1051,7 @@ private bool CheckSignedInfo(KeyedHashAlgorithm macAlg) throw new CryptographicException(SR.Cryptography_Xml_InvalidSignatureLength); // Calculate the hash - byte[] hashValue = GetC14NDigest(macAlg); + byte[]? hashValue = GetC14NDigest(macAlg); SignedXmlDebugLog.LogVerifySignedInfo(this, macAlg, hashValue, m_signature.SignatureValue); return m_signature.SignatureValue.AsSpan().SequenceEqual(hashValue.AsSpan(0, m_signature.SignatureValue.Length)); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs index 3895d28e035e3..a0f04eeee6388 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs @@ -191,7 +191,7 @@ private static bool VerboseLoggingEnabled /// /// Convert the byte array into a hex string /// - private static string FormatBytes(byte[] bytes) + private static string FormatBytes(byte[]? bytes) { if (bytes == null) return NullString; @@ -807,8 +807,8 @@ internal static void LogVerifyReference(SignedXml signedXml, Reference reference /// hash value the signature expected the reference to have internal static void LogVerifyReferenceHash(SignedXml signedXml, Reference reference, - byte[] actualHash, - byte[] expectedHash) + byte[]? actualHash, + byte[]? expectedHash) { Debug.Assert(signedXml != null, "signedXml != null"); Debug.Assert(reference != null, "reference != null"); @@ -850,8 +850,8 @@ internal static void LogVerifySignedInfo(SignedXml signedXml, SignatureDescription signatureDescription, HashAlgorithm hashAlgorithm, AsymmetricSignatureDeformatter asymmetricSignatureDeformatter, - byte[] actualHashValue, - byte[] signatureValue) + byte[]? actualHashValue, + byte[]? signatureValue) { Debug.Assert(signedXml != null, "signedXml != null"); Debug.Assert(signatureDescription != null, "signatureDescription != null"); @@ -896,8 +896,8 @@ internal static void LogVerifySignedInfo(SignedXml signedXml, /// raw signature value internal static void LogVerifySignedInfo(SignedXml signedXml, KeyedHashAlgorithm mac, - byte[] actualHashValue, - byte[] signatureValue) + byte[]? actualHashValue, + byte[]? signatureValue) { Debug.Assert(signedXml != null, "signedXml != null"); Debug.Assert(mac != null, "mac != null"); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs index 5f35d78372a37..7087d16e229a7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs @@ -149,7 +149,7 @@ internal XmlElement GetXml(XmlDocument document, string name) public abstract object GetOutput(Type type); - public virtual byte[] GetDigestedOutput(HashAlgorithm hash) + public virtual byte[]? GetDigestedOutput(HashAlgorithm hash) { return hash.ComputeHash((Stream)GetOutput(typeof(Stream))); } @@ -193,7 +193,7 @@ public Hashtable PropagatedNamespaces return _propagatedNamespaces; } - CanonicalXmlNodeList namespaces = null; + CanonicalXmlNodeList? namespaces = null; if (reference != null) namespaces = reference._namespaces; else if (signedXml?._context != null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 0e58301d3ece1..4d3518abd060d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -98,41 +98,41 @@ internal static bool VerifyAttributes(XmlElement element, string[]? expectedAttr return true; } - internal static bool IsNamespaceNode(XmlNode n) + internal static bool IsNamespaceNode(XmlNode? n) { return n.NodeType == XmlNodeType.Attribute && (n.Prefix.Equals("xmlns") || (n.Prefix.Length == 0 && n.LocalName.Equals("xmlns"))); } - internal static bool IsXmlNamespaceNode(XmlNode n) + internal static bool IsXmlNamespaceNode(XmlNode? n) { return n.NodeType == XmlNodeType.Attribute && n.Prefix.Equals("xml"); } // We consider xml:space style attributes as default namespace nodes since they obey the same propagation rules - internal static bool IsDefaultNamespaceNode(XmlNode n) + internal static bool IsDefaultNamespaceNode(XmlNode? n) { bool b1 = n.NodeType == XmlNodeType.Attribute && n.Prefix.Length == 0 && n.LocalName.Equals("xmlns"); bool b2 = IsXmlNamespaceNode(n); return b1 || b2; } - internal static bool IsEmptyDefaultNamespaceNode(XmlNode n) + internal static bool IsEmptyDefaultNamespaceNode(XmlNode? n) { return IsDefaultNamespaceNode(n) && n.Value!.Length == 0; } - internal static string GetNamespacePrefix(XmlAttribute a) + internal static string GetNamespacePrefix(XmlAttribute? a) { Debug.Assert(IsNamespaceNode(a) || IsXmlNamespaceNode(a)); return a.Prefix.Length == 0 ? string.Empty : a.LocalName; } - internal static bool HasNamespacePrefix(XmlAttribute a, string nsPrefix) + internal static bool HasNamespacePrefix(XmlAttribute? a, string nsPrefix) { return GetNamespacePrefix(a).Equals(nsPrefix); } - internal static bool IsNonRedundantNamespaceDecl(XmlAttribute a, XmlAttribute? nearestAncestorWithSamePrefix) + internal static bool IsNonRedundantNamespaceDecl(XmlAttribute? a, XmlAttribute? nearestAncestorWithSamePrefix) { if (nearestAncestorWithSamePrefix == null) return !IsEmptyDefaultNamespaceNode(a); @@ -140,7 +140,7 @@ internal static bool IsNonRedundantNamespaceDecl(XmlAttribute a, XmlAttribute? n return !nearestAncestorWithSamePrefix.Value.Equals(a.Value); } - internal static bool IsXmlPrefixDefinitionNode(XmlAttribute a) + internal static bool IsXmlPrefixDefinitionNode(XmlAttribute? a) { return false; // return a.Prefix.Equals("xmlns") && a.LocalName.Equals("xml") && a.Value.Equals(NamespaceUrlForXmlPrefix); @@ -324,7 +324,7 @@ internal static bool NodeInList(XmlNode? node, XmlNodeList nodeList) return false; } - internal static string GetIdFromLocalUri(string uri, out bool discardComments) + internal static string GetIdFromLocalUri(string? uri, out bool discardComments) { string idref = uri.Substring(1); // initialize the return value @@ -345,7 +345,7 @@ internal static string GetIdFromLocalUri(string uri, out bool discardComments) return idref; } - internal static string ExtractIdFromLocalUri(string uri) + internal static string ExtractIdFromLocalUri(string? uri) { string idref = uri.Substring(1); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs index b7c69da72ad7b..54886e938853d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs @@ -91,7 +91,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) if (elem.LocalName == "Except" && elem.NamespaceURI == XmlDecryptionTransformNamespaceUrl) { // the Uri is required - string uri = Utils.GetAttribute(elem, "URI", XmlDecryptionTransformNamespaceUrl); + string? uri = Utils.GetAttribute(elem, "URI", XmlDecryptionTransformNamespaceUrl); if (uri == null || uri.Length == 0 || uri[0] != '#') throw new CryptographicException(SR.Cryptography_Xml_UriRequired); if (!Utils.VerifyAttributes(elem, "URI")) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs index 1f49aa94ea63c..9ccfd1e060a5f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs @@ -80,7 +80,7 @@ public override object GetOutput(Type type) return new MemoryStream(_cXml.GetBytes()); } - public override byte[] GetDigestedOutput(HashAlgorithm hash) + public override byte[]? GetDigestedOutput(HashAlgorithm hash) { return _cXml.GetDigestedBytes(hash); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs index 42705e2d75b56..b2d29245edb5f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs @@ -131,7 +131,7 @@ public override object GetOutput() if (signatureList == null) return _inputNodeList; CanonicalXmlNodeList resultNodeList = new CanonicalXmlNodeList(); - foreach (XmlNode node in _inputNodeList) + foreach (XmlNode? node in _inputNodeList) { if (node == null) continue; // keep namespaces diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs index d4b298df8eb01..98071ad5f3dfa 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs @@ -117,7 +117,7 @@ public override object GetOutput(Type type) return new MemoryStream(_excCanonicalXml.GetBytes()); } - public override byte[] GetDigestedOutput(HashAlgorithm hash) + public override byte[]? GetDigestedOutput(HashAlgorithm hash) { return _excCanonicalXml.GetDigestedBytes(hash); } diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs index a16525ecb4077..39d5e65b63a44 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs @@ -28,7 +28,7 @@ public void TestNewEmptyKeyNode() [Fact] public void TestNewKeyNode() { - string uri = "http://www.go-mono.com/"; + string? uri = "http://www.go-mono.com/"; KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod(); uri1.Uri = uri; XmlElement xel = uri1.GetXml(); diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/ReferenceTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/ReferenceTest.cs index d84b2fb9c7aec..06d08b1d13fe5 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/ReferenceTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/ReferenceTest.cs @@ -39,7 +39,7 @@ public void Ctor() [InlineData(" ")] [InlineData("uri")] [InlineData("http://mysite.com/")] - public void Ctor_Uri(string uri) + public void Ctor_Uri(string? uri) { Reference reference = new Reference(uri); @@ -189,7 +189,7 @@ public void AddAllTransforms() { Reference reference = new Reference(); // adding an empty hash value - byte[] hash = new byte[20]; + byte[]? hash = new byte[20]; reference.DigestMethod = SignedXml.XmlDsigSHA1Url; reference.DigestValue = hash; XmlElement xel = reference.GetXml(); diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/SignedXmlTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/SignedXmlTest.cs index 8841cf2f821f0..aafbc7310837d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/SignedXmlTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/SignedXmlTest.cs @@ -657,7 +657,7 @@ public void DigestValue_CRLF() s.Position = 0; - byte[] digest; + byte[]? digest; using (HashAlgorithm hash = SHA1.Create()) { digest = hash.ComputeHash(s); @@ -717,7 +717,7 @@ public void DigestValue_LF() s.Position = 0; - byte[] digest; + byte[]? digest; using (HashAlgorithm hash = SHA1.Create()) { digest = hash.ComputeHash(s); From 9be11d87c28de39238043a629ec7c1d8ed396137 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 21:19:01 +0100 Subject: [PATCH 09/67] More annotations --- .../Security/Cryptography/Xml/Reference.cs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index 40e3453b72ad1..dfcf25d41883f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -219,7 +219,7 @@ public void LoadXml(XmlElement value) // Transforms bool hasTransforms = false; TransformChain = new TransformChain(); - XmlNodeList transformsNodes = value.SelectNodes("ds:Transforms", nsm); + XmlNodeList? transformsNodes = value.SelectNodes("ds:Transforms", nsm); if (transformsNodes != null && transformsNodes.Count != 0) { if (transformsNodes.Count > 1) @@ -227,15 +227,15 @@ public void LoadXml(XmlElement value) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/Transforms"); } hasTransforms = true; - XmlElement transformsElement = transformsNodes[0] as XmlElement; - if (!Utils.VerifyAttributes(transformsElement, (string[])null)) + XmlElement transformsElement = (transformsNodes[0] as XmlElement)!; + if (!Utils.VerifyAttributes(transformsElement, (string[]?)null)) { throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/Transforms"); } - XmlNodeList transformNodes = transformsElement.SelectNodes("ds:Transform", nsm); + XmlNodeList? transformNodes = transformsElement.SelectNodes("ds:Transform", nsm); if (transformNodes != null) { - if (transformNodes.Count != transformsElement.SelectNodes("*").Count) + if (transformNodes.Count != transformsElement.SelectNodes("*")!.Count) { throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/Transforms"); } @@ -245,13 +245,13 @@ public void LoadXml(XmlElement value) } foreach (XmlNode transformNode in transformNodes) { - XmlElement transformElement = transformNode as XmlElement; - string algorithm = Utils.GetAttribute(transformElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); + XmlElement transformElement = (transformNode as XmlElement)!; + string? algorithm = Utils.GetAttribute(transformElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); if (algorithm == null || !Utils.VerifyAttributes(transformElement, "Algorithm")) { throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform); } - Transform transform = CryptoHelpers.CreateFromName(algorithm); + Transform? transform = CryptoHelpers.CreateFromName(algorithm); if (transform == null) { throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform); @@ -265,7 +265,7 @@ public void LoadXml(XmlElement value) { // Walk back to the Signature tag. Find the nearest signature ancestor // Signature-->SignedInfo-->Reference-->Transforms-->Transform - XmlNode signatureTag = transformElement.SelectSingleNode("ancestor::ds:Signature[1]", nsm); + XmlNode? signatureTag = transformElement.SelectSingleNode("ancestor::ds:Signature[1]", nsm); // Resolve the reference to get starting point for position calculation. XmlNode referenceTarget = @@ -273,7 +273,7 @@ public void LoadXml(XmlElement value) ? transformElement.OwnerDocument : SignedXml.GetIdElement(transformElement.OwnerDocument, Utils.GetIdFromLocalUri(_uri, out bool _)); - XmlNodeList signatureList = referenceTarget?.SelectNodes(".//ds:Signature", nsm); + XmlNodeList? signatureList = referenceTarget?.SelectNodes(".//ds:Signature", nsm); if (signatureList != null) { int position = 0; @@ -293,26 +293,26 @@ public void LoadXml(XmlElement value) } // DigestMethod - XmlNodeList digestMethodNodes = value.SelectNodes("ds:DigestMethod", nsm); + XmlNodeList? digestMethodNodes = value.SelectNodes("ds:DigestMethod", nsm); if (digestMethodNodes == null || digestMethodNodes.Count == 0 || digestMethodNodes.Count > 1) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestMethod"); - XmlElement digestMethodElement = digestMethodNodes[0] as XmlElement; - _digestMethod = Utils.GetAttribute(digestMethodElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); + XmlElement digestMethodElement = (digestMethodNodes[0] as XmlElement)!; + _digestMethod = Utils.GetAttribute(digestMethodElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl)!; if (_digestMethod == null || !Utils.VerifyAttributes(digestMethodElement, "Algorithm")) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestMethod"); // DigestValue - XmlNodeList digestValueNodes = value.SelectNodes("ds:DigestValue", nsm); + XmlNodeList? digestValueNodes = value.SelectNodes("ds:DigestValue", nsm); if (digestValueNodes == null || digestValueNodes.Count == 0 || digestValueNodes.Count > 1) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestValue"); - XmlElement digestValueElement = digestValueNodes[0] as XmlElement; + XmlElement digestValueElement = (digestValueNodes[0] as XmlElement)!; _digestValue = Convert.FromBase64String(Utils.DiscardWhiteSpaces(digestValueElement.InnerText)); - if (!Utils.VerifyAttributes(digestValueElement, (string[])null)) + if (!Utils.VerifyAttributes(digestValueElement, (string[]?)null)) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestValue"); // Verify that there aren't any extra nodes that aren't allowed int expectedChildNodeCount = hasTransforms ? 3 : 2; - if (value.SelectNodes("*").Count != expectedChildNodeCount) + if (value.SelectNodes("*")!.Count != expectedChildNodeCount) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference"); // cache the Xml From 7d4b1aeaede9b002161598cc55f9c1c34fc4092f Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 21:31:01 +0100 Subject: [PATCH 10/67] More annotations --- .../Security/Cryptography/Xml/EncryptedXml.cs | 2 +- .../Security/Cryptography/Xml/Reference.cs | 38 +++++++++---------- .../Cryptography/Xml/SignedXmlDebugLog.cs | 4 +- .../Cryptography/Xml/TransformChain.cs | 18 ++++----- .../System/Security/Cryptography/Xml/Utils.cs | 18 ++++----- 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 849f5818460fe..3e105548b42d2 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -189,7 +189,7 @@ private byte[] GetCipherValue(CipherData cipherData) { if (cipherData.CipherReference.CipherValue != null) return cipherData.CipherReference.CipherValue; - Stream decInputStream; + Stream? decInputStream; if (cipherData.CipherReference.Uri == null) { throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index dfcf25d41883f..9f1666dc76b64 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -153,7 +153,7 @@ internal ReferenceTargetType ReferenceTargetType public XmlElement GetXml() { - if (CacheValid) return (_cachedXml); + if (CacheValid) return (_cachedXml!); XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; @@ -188,13 +188,13 @@ internal XmlElement GetXml(XmlDocument document) if (DigestValue == null) { - if (_hashAlgorithm.Hash == null) + if (_hashAlgorithm!.Hash == null) throw new CryptographicException(SR.Cryptography_Xml_DigestValueRequired); DigestValue = _hashAlgorithm.Hash; } XmlElement digestValueElement = document.CreateElement("DigestValue", SignedXml.XmlDsigNamespaceUrl); - digestValueElement.AppendChild(document.CreateTextNode(Convert.ToBase64String(_digestValue))); + digestValueElement.AppendChild(document.CreateTextNode(Convert.ToBase64String(_digestValue!))); referenceElement.AppendChild(digestValueElement); return referenceElement; @@ -347,10 +347,10 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList // Let's go get the target. string baseUri = (document == null ? System.Environment.CurrentDirectory + "\\" : document.BaseURI); - Stream hashInputStream = null; - WebResponse response = null; - Stream inputStream = null; - XmlResolver resolver = null; + Stream? hashInputStream = null; + WebResponse? response = null; + Stream? inputStream = null; + XmlResolver? resolver = null; byte[]? hashval = null; try @@ -369,10 +369,10 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList if (_uri == null) { // We need to create a DocumentNavigator out of the XmlElement - resolver = (SignedXml.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); + resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); // In the case of a Uri-less reference, we will simply pass null to the transform chain. // The first transform in the chain is expected to know how to retrieve the data to hash. - hashInputStream = TransformChain.TransformToOctetStream((Stream)null, resolver, baseUri); + hashInputStream = TransformChain.TransformToOctetStream((Stream?)null, resolver, baseUri); } else if (_uri.Length == 0) { @@ -382,7 +382,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList throw new CryptographicException(SR.Format(SR.Cryptography_Xml_SelfReferenceRequiresContext, _uri)); // Normalize the containing document - resolver = (SignedXml.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); + resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); XmlDocument docWithNoComments = Utils.DiscardComments(Utils.PreProcessDocumentInput(document, resolver, baseUri)); hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver, baseUri); } @@ -398,12 +398,12 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList throw new CryptographicException(SR.Format(SR.Cryptography_Xml_SelfReferenceRequiresContext, _uri)); // We should not discard comments here!!! - resolver = (SignedXml.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); + resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessDocumentInput(document, resolver, baseUri), resolver, baseUri); break; } - XmlElement elem = SignedXml.GetIdElement(document, idref); + XmlElement? elem = SignedXml!.GetIdElement(document, idref); if (elem != null) _namespaces = Utils.GetPropagatedAttributes(elem.ParentNode as XmlElement); @@ -414,12 +414,12 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList { foreach (XmlNode node in refList) { - XmlElement tempElem = node as XmlElement; + XmlElement? tempElem = node as XmlElement; if ((tempElem != null) && (Utils.HasAttribute(tempElem, "Id", SignedXml.XmlDsigNamespaceUrl)) - && (Utils.GetAttribute(tempElem, "Id", SignedXml.XmlDsigNamespaceUrl).Equals(idref))) + && (Utils.GetAttribute(tempElem, "Id", SignedXml.XmlDsigNamespaceUrl)!.Equals(idref))) { elem = tempElem; - if (_signedXml._context != null) + if (_signedXml!._context != null) _namespaces = Utils.GetPropagatedAttributes(_signedXml._context); break; } @@ -430,9 +430,9 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList if (elem == null) throw new CryptographicException(SR.Cryptography_Xml_InvalidReference); - XmlDocument normDocument = Utils.PreProcessElementInput(elem, resolver, baseUri); + XmlDocument normDocument = Utils.PreProcessElementInput(elem, resolver!, baseUri); // Add the propagated attributes - Utils.AddNamespaces(normDocument.DocumentElement, _namespaces); + Utils.AddNamespaces(normDocument.DocumentElement!, _namespaces); resolver = (SignedXml.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); if (discardComments) @@ -454,7 +454,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList break; case ReferenceTargetType.XmlElement: // We need to create a DocumentNavigator out of the XmlElement - resolver = (SignedXml.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); + resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessElementInput((XmlElement)_refTarget, resolver, baseUri), resolver, baseUri); break; default: @@ -463,7 +463,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList // Compute the new hash value hashInputStream = SignedXmlDebugLog.LogReferenceData(this, hashInputStream); - hashval = _hashAlgorithm.ComputeHash(hashInputStream); + hashval = _hashAlgorithm.ComputeHash(hashInputStream!); } finally { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs index a0f04eeee6388..cfedf51821efb 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs @@ -571,7 +571,7 @@ internal static void LogNamespacePropagation(SignedXml signedXml, XmlNodeList na /// The reference being processed /// Stream containing the output of the reference /// Stream containing the output of the reference - internal static Stream LogReferenceData(Reference reference, Stream data) + internal static Stream? LogReferenceData(Reference reference, Stream? data) { if (VerboseLoggingEnabled) { @@ -582,7 +582,7 @@ internal static Stream LogReferenceData(Reference reference, Stream data) // value. // - MemoryStream ms = new MemoryStream(); + MemoryStream? ms = new MemoryStream(); // First read the input stream into our temporary stream byte[] buffer = new byte[4096]; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index a4fb26b925116..c4178eb521a27 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -58,7 +58,7 @@ public Transform this[int index] // The goal behind this method is to pump the input stream through the transforms and get back something that // can be hashed - internal Stream TransformToOctetStream(object? inputObject, Type inputType, XmlResolver resolver, string baseUri) + internal Stream? TransformToOctetStream(object? inputObject, Type inputType, XmlResolver resolver, string baseUri) { object? currentInput = inputObject; foreach (Transform transform in _transforms) @@ -68,7 +68,7 @@ internal Stream TransformToOctetStream(object? inputObject, Type inputType, XmlR //in this case, no translation necessary, pump it through transform.Resolver = resolver; transform.BaseURI = baseUri; - transform.LoadInput(currentInput); + transform.LoadInput(currentInput!); currentInput = transform.GetOutput(); } else @@ -79,7 +79,7 @@ internal Stream TransformToOctetStream(object? inputObject, Type inputType, XmlR { if (transform.AcceptsType(typeof(XmlDocument))) { - Stream currentInputStream = currentInput as Stream; + Stream currentInputStream = (currentInput as Stream)!; XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; XmlReader valReader = Utils.PreProcessStreamInput(currentInputStream, resolver, baseUri); @@ -138,24 +138,24 @@ internal Stream TransformToOctetStream(object? inputObject, Type inputType, XmlR if (currentInput is XmlNodeList) { CanonicalXml c14n = new CanonicalXml((XmlNodeList)currentInput, resolver, false); - MemoryStream ms = new MemoryStream(c14n.GetBytes()); + MemoryStream? ms = new MemoryStream(c14n.GetBytes()); return ms; } if (currentInput is XmlDocument) { CanonicalXml c14n = new CanonicalXml((XmlDocument)currentInput, resolver); - MemoryStream ms = new MemoryStream(c14n.GetBytes()); + MemoryStream? ms = new MemoryStream(c14n.GetBytes()); return ms; } throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType); } - internal Stream TransformToOctetStream(Stream? input, XmlResolver resolver, string baseUri) + internal Stream? TransformToOctetStream(Stream? input, XmlResolver resolver, string baseUri) { return TransformToOctetStream(input, typeof(Stream), resolver, baseUri); } - internal Stream TransformToOctetStream(XmlDocument document, XmlResolver resolver, string baseUri) + internal Stream? TransformToOctetStream(XmlDocument document, XmlResolver resolver, string baseUri) { return TransformToOctetStream(document, typeof(XmlDocument), resolver, baseUri); } @@ -196,8 +196,8 @@ internal void LoadXml(XmlElement value) { //red flag XmlElement transformElement = (XmlElement)transformNodes.Item(i)!; - string algorithm = Utils.GetAttribute(transformElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); - Transform transform = CryptoHelpers.CreateFromName(algorithm); + string? algorithm = Utils.GetAttribute(transformElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); + Transform? transform = CryptoHelpers.CreateFromName(algorithm); if (transform == null) throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform); // let the transform read the children of the transformElement for data diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 4d3518abd060d..5a643502af86b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -98,41 +98,41 @@ internal static bool VerifyAttributes(XmlElement element, string[]? expectedAttr return true; } - internal static bool IsNamespaceNode(XmlNode? n) + internal static bool IsNamespaceNode(XmlNode n) { return n.NodeType == XmlNodeType.Attribute && (n.Prefix.Equals("xmlns") || (n.Prefix.Length == 0 && n.LocalName.Equals("xmlns"))); } - internal static bool IsXmlNamespaceNode(XmlNode? n) + internal static bool IsXmlNamespaceNode(XmlNode n) { return n.NodeType == XmlNodeType.Attribute && n.Prefix.Equals("xml"); } // We consider xml:space style attributes as default namespace nodes since they obey the same propagation rules - internal static bool IsDefaultNamespaceNode(XmlNode? n) + internal static bool IsDefaultNamespaceNode(XmlNode n) { bool b1 = n.NodeType == XmlNodeType.Attribute && n.Prefix.Length == 0 && n.LocalName.Equals("xmlns"); bool b2 = IsXmlNamespaceNode(n); return b1 || b2; } - internal static bool IsEmptyDefaultNamespaceNode(XmlNode? n) + internal static bool IsEmptyDefaultNamespaceNode(XmlNode n) { return IsDefaultNamespaceNode(n) && n.Value!.Length == 0; } - internal static string GetNamespacePrefix(XmlAttribute? a) + internal static string GetNamespacePrefix(XmlAttribute a) { Debug.Assert(IsNamespaceNode(a) || IsXmlNamespaceNode(a)); return a.Prefix.Length == 0 ? string.Empty : a.LocalName; } - internal static bool HasNamespacePrefix(XmlAttribute? a, string nsPrefix) + internal static bool HasNamespacePrefix(XmlAttribute a, string nsPrefix) { return GetNamespacePrefix(a).Equals(nsPrefix); } - internal static bool IsNonRedundantNamespaceDecl(XmlAttribute? a, XmlAttribute? nearestAncestorWithSamePrefix) + internal static bool IsNonRedundantNamespaceDecl(XmlAttribute a, XmlAttribute? nearestAncestorWithSamePrefix) { if (nearestAncestorWithSamePrefix == null) return !IsEmptyDefaultNamespaceNode(a); @@ -140,7 +140,7 @@ internal static bool IsNonRedundantNamespaceDecl(XmlAttribute? a, XmlAttribute? return !nearestAncestorWithSamePrefix.Value.Equals(a.Value); } - internal static bool IsXmlPrefixDefinitionNode(XmlAttribute? a) + internal static bool IsXmlPrefixDefinitionNode(XmlAttribute a) { return false; // return a.Prefix.Equals("xmlns") && a.LocalName.Equals("xml") && a.Value.Equals(NamespaceUrlForXmlPrefix); @@ -381,7 +381,7 @@ internal static void RemoveAllChildren(XmlElement inputElement) // an output stream, connecting them up and reading until // hitting the end of the input stream. // returns the number of bytes copied - internal static long Pump(Stream input, Stream output) + internal static long Pump(Stream? input, Stream output) { // Use MemoryStream's WriteTo(Stream) method if possible MemoryStream? inputMS = input as MemoryStream; From f9e3066e13f43ab3cab5133d65bd30c11dbd111c Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 21:39:10 +0100 Subject: [PATCH 11/67] More annotations --- .../Cryptography/Xml/XmlDsigBase64Transform.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigBase64Transform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigBase64Transform.cs index bfb26b0b1b5ba..3cbb48b60bdde 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigBase64Transform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigBase64Transform.cs @@ -12,7 +12,7 @@ public class XmlDsigBase64Transform : Transform { private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlNodeList), typeof(XmlDocument) }; private readonly Type[] _outputTypes = { typeof(Stream) }; - private CryptoStream _cs; + private CryptoStream? _cs; public XmlDsigBase64Transform() { @@ -52,7 +52,7 @@ public override void LoadInput(object obj) } if (obj is XmlDocument) { - LoadXmlNodeListInput(((XmlDocument)obj).SelectNodes("//.")); + LoadXmlNodeListInput(((XmlDocument)obj).SelectNodes("//.")!); return; } } @@ -93,7 +93,7 @@ private void LoadXmlNodeListInput(XmlNodeList nodeList) StringBuilder sb = new StringBuilder(); foreach (XmlNode node in nodeList) { - XmlNode result = node.SelectSingleNode("self::text()"); + XmlNode? result = node.SelectSingleNode("self::text()"); if (result != null) sb.Append(result.OuterXml); } @@ -118,14 +118,14 @@ private void LoadXmlNodeListInput(XmlNodeList nodeList) public override object GetOutput() { - return _cs; + return _cs!; } public override object GetOutput(Type type) { if (type != typeof(Stream) && !type.IsSubclassOf(typeof(Stream))) throw new ArgumentException(SR.Cryptography_Xml_TransformIncorrectInputType, nameof(type)); - return _cs; + return _cs!; } } } From 79d8688d9f2aeeae2bb1da2bd62c08d7ee155d4e Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 21:58:41 +0100 Subject: [PATCH 12/67] More annotations --- .../Security/Cryptography/Xml/SignedXml.cs | 130 +++++++++--------- .../Cryptography/Xml/SignedXmlDebugLog.cs | 32 ++--- 2 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index ac966d45d0262..d709f0ff301ce 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -13,7 +13,7 @@ namespace System.Security.Cryptography.Xml { public class SignedXml { - protected Signature m_signature; + protected Signature? m_signature; protected string? m_strSigningKeyName; private AsymmetricAlgorithm? _signingKey; @@ -22,10 +22,10 @@ public class SignedXml private X509Certificate2Collection? _x509Collection; private IEnumerator? _x509Enum; - private bool[] _refProcessed; - private int[] _refLevelCache; + private bool[]? _refProcessed; + private int[]? _refLevelCache; - internal XmlResolver _xmlResolver; + internal XmlResolver? _xmlResolver; internal XmlElement? _context; private bool _bResolverSet; @@ -156,12 +156,12 @@ public Func SignatureFormatValidator set { _signatureFormatValidator = value; } } - public Collection SafeCanonicalizationMethods + public Collection? SafeCanonicalizationMethods { get { return _safeCanonicalizationMethods; } } - public AsymmetricAlgorithm SigningKey + public AsymmetricAlgorithm? SigningKey { get { return _signingKey; } set { _signingKey = value; } @@ -173,44 +173,44 @@ public EncryptedXml EncryptedXml set => _exml = value; } - public Signature Signature + public Signature? Signature { get { return m_signature; } } public SignedInfo SignedInfo { - get { return m_signature.SignedInfo; } + get { return m_signature!.SignedInfo; } } public string SignatureMethod { - get { return m_signature.SignedInfo.SignatureMethod; } + get { return m_signature!.SignedInfo.SignatureMethod; } } public string SignatureLength { - get { return m_signature.SignedInfo.SignatureLength; } + get { return m_signature!.SignedInfo.SignatureLength; } } public byte[]? SignatureValue { - get { return m_signature.SignatureValue; } + get { return m_signature!.SignatureValue; } } public KeyInfo KeyInfo { - get { return m_signature.KeyInfo; } - set { m_signature.KeyInfo = value; } + get { return m_signature!.KeyInfo; } + set { m_signature!.KeyInfo = value; } } public XmlElement GetXml() { // If we have a document context, then return a signature element in this context if (_containingDocument != null) - return m_signature.GetXml(_containingDocument); + return m_signature!.GetXml(_containingDocument); else - return m_signature.GetXml(); + return m_signature!.GetXml(); } public void LoadXml(XmlElement value) @@ -220,7 +220,7 @@ public void LoadXml(XmlElement value) throw new ArgumentNullException(nameof(value)); } - m_signature.LoadXml(value); + m_signature!.LoadXml(value); _context ??= value; @@ -233,12 +233,12 @@ public void LoadXml(XmlElement value) public void AddReference(Reference reference) { - m_signature.SignedInfo.AddReference(reference); + m_signature!.SignedInfo.AddReference(reference); } public void AddObject(DataObject dataObject) { - m_signature.AddObject(dataObject); + m_signature!.AddObject(dataObject); } public bool CheckSignature() @@ -252,7 +252,7 @@ public bool CheckSignatureReturningKey(out AsymmetricAlgorithm? signingKey) signingKey = null; bool bRet = false; - AsymmetricAlgorithm key; + AsymmetricAlgorithm? key; if (!CheckSignatureFormat()) { @@ -273,14 +273,14 @@ public bool CheckSignatureReturningKey(out AsymmetricAlgorithm? signingKey) return bRet; } - public bool CheckSignature(AsymmetricAlgorithm? key) + public bool CheckSignature(AsymmetricAlgorithm key) { if (!CheckSignatureFormat()) { return false; } - if (!CheckSignedInfo(key!)) + if (!CheckSignedInfo(key)) { SignedXmlDebugLog.LogVerificationFailure(this, SR.Log_VerificationFailed_SignedInfo); return false; @@ -327,7 +327,7 @@ public bool CheckSignature(X509Certificate2 certificate, bool verifySignatureOnl // Check key usages to make sure it is good for signing. foreach (X509Extension extension in certificate.Extensions) { - if (string.Equals(extension.Oid.Value, "2.5.29.15" /* szOID_KEY_USAGE */, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(extension.Oid!.Value, "2.5.29.15" /* szOID_KEY_USAGE */, StringComparison.OrdinalIgnoreCase)) { X509KeyUsageExtension keyUsage = new X509KeyUsageExtension(); keyUsage.CopyFrom(extension); @@ -360,7 +360,7 @@ public bool CheckSignature(X509Certificate2 certificate, bool verifySignatureOnl using (AsymmetricAlgorithm? publicKey = Utils.GetAnyPublicKey(certificate)) { - if (!CheckSignature(publicKey)) + if (!CheckSignature(publicKey!)) { return false; } @@ -372,12 +372,12 @@ public bool CheckSignature(X509Certificate2 certificate, bool verifySignatureOnl public void ComputeSignature() { - SignedXmlDebugLog.LogBeginSignatureComputation(this, _context); + SignedXmlDebugLog.LogBeginSignatureComputation(this, _context!); BuildDigestedReferences(); // Load the key - AsymmetricAlgorithm key = SigningKey; + AsymmetricAlgorithm? key = SigningKey; if (key == null) throw new CryptographicException(SR.Cryptography_Xml_LoadKeyFailed); @@ -401,10 +401,10 @@ public void ComputeSignature() } // See if there is a signature description class defined in the Config file - SignatureDescription signatureDescription = CryptoHelpers.CreateFromName(SignedInfo.SignatureMethod); + SignatureDescription? signatureDescription = CryptoHelpers.CreateFromName(SignedInfo.SignatureMethod); if (signatureDescription == null) throw new CryptographicException(SR.Cryptography_Xml_SignatureDescriptionNotCreated); - HashAlgorithm hashAlg = signatureDescription.CreateDigest(); + HashAlgorithm? hashAlg = signatureDescription.CreateDigest(); if (hashAlg == null) throw new CryptographicException(SR.Cryptography_Xml_CreateHashAlgorithmFailed); @@ -415,7 +415,7 @@ public void ComputeSignature() AsymmetricSignatureFormatter asymmetricSignatureFormatter = signatureDescription.CreateFormatter(key); SignedXmlDebugLog.LogSigning(this, key, signatureDescription, hashAlg, asymmetricSignatureFormatter); - m_signature.SignatureValue = asymmetricSignatureFormatter.CreateSignature(hashAlg); + m_signature!.SignatureValue = asymmetricSignatureFormatter.CreateSignature(hashAlg); } public void ComputeSignature(KeyedHashAlgorithm macAlg) @@ -425,12 +425,12 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) throw new ArgumentNullException(nameof(macAlg)); } - HMAC hash = macAlg as HMAC; + HMAC? hash = macAlg as HMAC; if (hash == null) throw new CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch); int signatureLength; - if (m_signature.SignedInfo.SignatureLength == null) + if (m_signature!.SignedInfo.SignatureLength == null) signatureLength = hash.HashSize; else signatureLength = Convert.ToInt32(m_signature.SignedInfo.SignatureLength, null); @@ -455,7 +455,7 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) SignedXmlDebugLog.LogSigning(this, hash); m_signature.SignatureValue = new byte[signatureLength / 8]; - Buffer.BlockCopy(hashValue, 0, m_signature.SignatureValue, 0, signatureLength / 8); + Buffer.BlockCopy(hashValue!, 0, m_signature.SignatureValue, 0, signatureLength / 8); } // @@ -522,9 +522,9 @@ private X509Certificate2Collection BuildBagOfCerts() private AsymmetricAlgorithm? GetNextCertificatePublicKey() { - while (_x509Enum.MoveNext()) + while (_x509Enum!.MoveNext()) { - X509Certificate2? certificate = (X509Certificate2)_x509Enum.Current; + X509Certificate2? certificate = (X509Certificate2?)_x509Enum.Current; if (certificate != null) return Utils.GetAnyPublicKey(certificate); } @@ -556,14 +556,14 @@ private X509Certificate2Collection BuildBagOfCerts() } // Get the element with idValue - XmlElement elem = document.GetElementById(idValue); + XmlElement? elem = document.GetElementById(idValue); if (elem != null) { // Have to check for duplicate ID values from the DTD. XmlDocument docClone = (XmlDocument)document.CloneNode(true); - XmlElement cloneElem = docClone.GetElementById(idValue); + XmlElement? cloneElem = docClone.GetElementById(idValue); // If it's null here we want to know about it, because it means that // GetElementById failed to work across the cloning, and our uniqueness @@ -575,7 +575,7 @@ private X509Certificate2Collection BuildBagOfCerts() { cloneElem.Attributes.RemoveAll(); - XmlElement cloneElem2 = docClone.GetElementById(idValue); + XmlElement? cloneElem2 = docClone.GetElementById(idValue); if (cloneElem2 != null) { @@ -635,7 +635,7 @@ private bool DoesSignatureUseTruncatedHmac() } // See if we're signed witn an HMAC algorithm - HMAC hmac = CryptoHelpers.CreateFromName(SignatureMethod); + HMAC? hmac = CryptoHelpers.CreateFromName(SignatureMethod!); if (hmac == null) { // We aren't signed with an HMAC algorithm, so we cannot have a truncated HMAC @@ -660,7 +660,7 @@ private bool DoesSignatureUseTruncatedHmac() // of approved algorithm URIs. private bool DoesSignatureUseSafeCanonicalizationMethod() { - foreach (string safeAlgorithm in SafeCanonicalizationMethods) + foreach (string safeAlgorithm in SafeCanonicalizationMethods!) { if (string.Equals(safeAlgorithm, SignedInfo.CanonicalizationMethod, StringComparison.OrdinalIgnoreCase)) { @@ -693,7 +693,7 @@ private bool ReferenceUsesSafeTransformMethods(Reference reference) private bool IsSafeTransform(string transformAlgorithm) { // All canonicalization algorithms are valid transform algorithms. - foreach (string safeAlgorithm in SafeCanonicalizationMethods) + foreach (string safeAlgorithm in SafeCanonicalizationMethods!) { if (string.Equals(safeAlgorithm, transformAlgorithm, StringComparison.OrdinalIgnoreCase)) { @@ -775,14 +775,14 @@ private static IList DefaultSafeTransformMethods bool isKeyedHashAlgorithm = hash is KeyedHashAlgorithm; if (isKeyedHashAlgorithm || !_bCacheValid || !SignedInfo.CacheValid) { - string baseUri = _containingDocument?.BaseURI; - XmlResolver resolver = (_bResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); - XmlDocument doc = Utils.PreProcessElementInput(SignedInfo.GetXml(), resolver, baseUri); + string? baseUri = _containingDocument?.BaseURI; + XmlResolver? resolver = (_bResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); + XmlDocument doc = Utils.PreProcessElementInput(SignedInfo.GetXml(), resolver!, baseUri!); // Add non default namespaces in scope - CanonicalXmlNodeList namespaces = (_context == null ? null : Utils.GetPropagatedAttributes(_context)); + CanonicalXmlNodeList? namespaces = (_context == null ? null : Utils.GetPropagatedAttributes(_context)); SignedXmlDebugLog.LogNamespacePropagation(this, namespaces); - Utils.AddNamespaces(doc.DocumentElement, namespaces); + Utils.AddNamespaces(doc.DocumentElement!, namespaces); Transform c14nMethodTransform = SignedInfo.CanonicalizationMethodObject; c14nMethodTransform.Resolver = resolver; @@ -800,12 +800,12 @@ private static IList DefaultSafeTransformMethods private int GetReferenceLevel(int index, ArrayList references) { - if (_refProcessed[index]) return _refLevelCache[index]; + if (_refProcessed![index]) return _refLevelCache![index]; _refProcessed[index] = true; - Reference reference = (Reference)references[index]; + Reference reference = (Reference)references[index]!; if (reference.Uri == null || reference.Uri.Length == 0 || (reference.Uri.Length > 0 && reference.Uri[0] != '#')) { - _refLevelCache[index] = 0; + _refLevelCache![index] = 0; return 0; } if (reference.Uri.Length > 0 && reference.Uri[0] == '#') @@ -813,20 +813,20 @@ private int GetReferenceLevel(int index, ArrayList references) string idref = Utils.ExtractIdFromLocalUri(reference.Uri); if (idref == "xpointer(/)") { - _refLevelCache[index] = 0; + _refLevelCache![index] = 0; return 0; } // If this is pointing to another reference for (int j = 0; j < references.Count; ++j) { - if (((Reference)references[j]).Id == idref) + if ((((Reference)references[j]!)!).Id == idref) { - _refLevelCache[index] = GetReferenceLevel(j, references) + 1; + _refLevelCache![index] = GetReferenceLevel(j, references) + 1; return (_refLevelCache[index]); } } // Then the reference points to an object tag - _refLevelCache[index] = 0; + _refLevelCache![index] = 0; return 0; } // Malformed reference @@ -860,8 +860,8 @@ public int Compare(object? a, object? b) i++; } - int iLevelA = referenceA.SignedXml.GetReferenceLevel(iIndexA, References); - int iLevelB = referenceB.SignedXml.GetReferenceLevel(iIndexB, References); + int iLevelA = referenceA!.SignedXml!.GetReferenceLevel(iIndexA, References); + int iLevelB = referenceB!.SignedXml!.GetReferenceLevel(iIndexB, References); return iLevelA.CompareTo(iLevelB); } } @@ -885,7 +885,7 @@ private void BuildDigestedReferences() sortedReferences.Sort(sortOrder); CanonicalXmlNodeList nodeList = new CanonicalXmlNodeList(); - foreach (DataObject obj in m_signature.ObjectList) + foreach (DataObject obj in m_signature!.ObjectList) { nodeList.Add(obj.GetXml()); } @@ -896,7 +896,7 @@ private void BuildDigestedReferences() SignedXmlDebugLog.LogSigningReference(this, reference); - reference.UpdateHashValue(_containingDocument, nodeList); + reference.UpdateHashValue(_containingDocument!, nodeList); // If this reference has an Id attribute, add it if (reference.Id != null) nodeList.Add(reference.GetXml()); @@ -905,10 +905,10 @@ private void BuildDigestedReferences() private bool CheckDigestedReferences() { - ArrayList references = m_signature.SignedInfo.References; + ArrayList references = m_signature!.SignedInfo.References; for (int i = 0; i < references.Count; ++i) { - Reference digestedReference = (Reference)references[i]; + Reference digestedReference = (Reference)references[i]!; if (!ReferenceUsesSafeTransformMethods(digestedReference)) { @@ -919,7 +919,7 @@ private bool CheckDigestedReferences() byte[]? calculatedHash; try { - calculatedHash = digestedReference.CalculateHashValue(_containingDocument, m_signature.ReferencedItems); + calculatedHash = digestedReference.CalculateHashValue(_containingDocument!, m_signature.ReferencedItems); } catch (CryptoSignedXmlRecursionException) { @@ -998,18 +998,18 @@ private bool CheckSignedInfo(AsymmetricAlgorithm key) throw new ArgumentNullException(nameof(key)); } - SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature.SignedInfo); + SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature!.SignedInfo); - SignatureDescription signatureDescription = CryptoHelpers.CreateFromName(SignatureMethod); + SignatureDescription? signatureDescription = CryptoHelpers.CreateFromName(SignatureMethod); if (signatureDescription == null) throw new CryptographicException(SR.Cryptography_Xml_SignatureDescriptionNotCreated); // Let's see if the key corresponds with the SignatureMethod - Type ta = Type.GetType(signatureDescription.KeyAlgorithm); + Type ta = Type.GetType(signatureDescription.KeyAlgorithm!)!; if (!IsKeyTheCorrectAlgorithm(key, ta)) return false; - HashAlgorithm hashAlgorithm = signatureDescription.CreateDigest(); + HashAlgorithm? hashAlgorithm = signatureDescription.CreateDigest(); if (hashAlgorithm == null) throw new CryptographicException(SR.Cryptography_Xml_CreateHashAlgorithmFailed); byte[]? hashval = GetC14NDigest(hashAlgorithm); @@ -1022,7 +1022,7 @@ private bool CheckSignedInfo(AsymmetricAlgorithm key) asymmetricSignatureDeformatter, hashval, m_signature.SignatureValue); - return asymmetricSignatureDeformatter.VerifySignature(hashval, m_signature.SignatureValue); + return asymmetricSignatureDeformatter.VerifySignature(hashval!, m_signature.SignatureValue!); } private bool CheckSignedInfo(KeyedHashAlgorithm macAlg) @@ -1032,7 +1032,7 @@ private bool CheckSignedInfo(KeyedHashAlgorithm macAlg) throw new ArgumentNullException(nameof(macAlg)); } - SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature.SignedInfo); + SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature!.SignedInfo); int signatureLength; if (m_signature.SignedInfo.SignatureLength == null) @@ -1109,7 +1109,7 @@ private static bool IsKeyTheCorrectAlgorithm(AsymmetricAlgorithm key, Type expec // while (expectedType != null && expectedType.BaseType != typeof(AsymmetricAlgorithm)) { - expectedType = expectedType.BaseType; + expectedType = expectedType.BaseType!; } if (expectedType == null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs index cfedf51821efb..47d89bba7c1ca 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs @@ -206,9 +206,9 @@ private static string GetKeyName(object key) { Debug.Assert(key != null, "key != null"); - ICspAsymmetricAlgorithm cspKey = key as ICspAsymmetricAlgorithm; - X509Certificate certificate = key as X509Certificate; - X509Certificate2 certificate2 = key as X509Certificate2; + ICspAsymmetricAlgorithm? cspKey = key as ICspAsymmetricAlgorithm; + X509Certificate? certificate = key as X509Certificate; + X509Certificate2? certificate2 = key as X509Certificate2; // // Use the following sources for key names, if available: @@ -255,11 +255,11 @@ private static string GetObjectId(object o) /// /// Map an OID to the friendliest name possible /// - private static string GetOidName(Oid oid) + private static string? GetOidName(Oid oid) { Debug.Assert(oid != null, "oid != null"); - string friendlyName = oid.FriendlyName; + string? friendlyName = oid.FriendlyName; if (string.IsNullOrEmpty(friendlyName)) friendlyName = oid.Value; @@ -292,7 +292,7 @@ internal static void LogBeginCanonicalization(SignedXml signedXml, Transform can { string canonicalizationSettings = SR.Format(CultureInfo.InvariantCulture, SR.Log_CanonicalizationSettings, - canonicalizationTransform.Resolver.GetType(), + canonicalizationTransform.Resolver!.GetType(), canonicalizationTransform.BaseURI); WriteLine(signedXml, TraceEventType.Verbose, @@ -378,7 +378,7 @@ internal static void LogBeginSignatureComputation(SignedXml signedXml, XmlElemen /// /// SignedXml object doing the verification /// Context of the verification - internal static void LogBeginSignatureVerification(SignedXml signedXml, XmlElement context) + internal static void LogBeginSignatureVerification(SignedXml signedXml, XmlElement? context) { Debug.Assert(signedXml != null, "signedXml != null"); @@ -415,7 +415,7 @@ internal static void LogCanonicalizedOutput(SignedXml signedXml, Transform canon if (VerboseLoggingEnabled) { - using (StreamReader reader = new StreamReader(canonicalizationTransform.GetOutput(typeof(Stream)) as Stream)) + using (StreamReader reader = new StreamReader((canonicalizationTransform.GetOutput(typeof(Stream)) as Stream)!)) { string logMessage = SR.Format(CultureInfo.InvariantCulture, SR.Log_CanonicalizedOutput, @@ -452,7 +452,7 @@ internal static void LogFormatValidationResult(SignedXml signedXml, bool result) /// SignedXml object doing the signature verification /// Canonicalization algorithm /// List of valid canonicalization algorithms - internal static void LogUnsafeCanonicalizationMethod(SignedXml signedXml, string algorithm, IEnumerable validAlgorithms) + internal static void LogUnsafeCanonicalizationMethod(SignedXml signedXml, string algorithm, IEnumerable? validAlgorithms) { Debug.Assert(signedXml != null, "signedXml != null"); Debug.Assert(validAlgorithms != null, "validAlgorithms != null"); @@ -490,7 +490,7 @@ internal static void LogUnsafeCanonicalizationMethod(SignedXml signedXml, string internal static void LogUnsafeTransformMethod( SignedXml signedXml, string algorithm, - IEnumerable validC14nAlgorithms, + IEnumerable? validC14nAlgorithms, IEnumerable validTransformAlgorithms) { Debug.Assert(signedXml != null, "signedXml != null"); @@ -534,7 +534,7 @@ internal static void LogUnsafeTransformMethod( /// /// SignedXml doing the signing or verification /// namespaces being propagated - internal static void LogNamespacePropagation(SignedXml signedXml, XmlNodeList namespaces) + internal static void LogNamespacePropagation(SignedXml signedXml, XmlNodeList? namespaces) { Debug.Assert(signedXml != null, "signedXml != null"); @@ -589,7 +589,7 @@ internal static void LogNamespacePropagation(SignedXml signedXml, XmlNodeList na int readBytes; do { - readBytes = data.Read(buffer, 0, buffer.Length); + readBytes = data!.Read(buffer, 0, buffer.Length); ms.Write(buffer, 0, readBytes); } while (readBytes == buffer.Length); @@ -682,7 +682,7 @@ internal static void LogSigningReference(SignedXml signedXml, Reference referenc if (VerboseLoggingEnabled) { - HashAlgorithm hashAlgorithm = CryptoHelpers.CreateFromName(reference.DigestMethod); + HashAlgorithm? hashAlgorithm = CryptoHelpers.CreateFromName(reference.DigestMethod); string hashAlgorithmName = hashAlgorithm == null ? "null" : hashAlgorithm.GetType().Name; string logMessage = SR.Format(CultureInfo.InvariantCulture, SR.Log_SigningReference, @@ -762,7 +762,7 @@ internal static void LogVerifyKeyUsage(SignedXml signedXml, X509Certificate cert string logMessage = SR.Format(CultureInfo.InvariantCulture, SR.Log_KeyUsages, keyUsages.KeyUsages, - GetOidName(keyUsages.Oid), + GetOidName(keyUsages.Oid!), GetKeyName(certificate)); WriteLine(signedXml, @@ -817,7 +817,7 @@ internal static void LogVerifyReferenceHash(SignedXml signedXml, if (VerboseLoggingEnabled) { - HashAlgorithm hashAlgorithm = CryptoHelpers.CreateFromName(reference.DigestMethod); + HashAlgorithm? hashAlgorithm = CryptoHelpers.CreateFromName(reference.DigestMethod); string hashAlgorithmName = hashAlgorithm == null ? "null" : hashAlgorithm.GetType().Name; string logMessage = SR.Format(CultureInfo.InvariantCulture, SR.Log_ReferenceHash, @@ -1031,7 +1031,7 @@ internal static void LogSignedXmlRecursionLimit(SignedXml signedXml, if (InformationLoggingEnabled) { - HashAlgorithm hashAlgorithm = CryptoHelpers.CreateFromName(reference.DigestMethod); + HashAlgorithm? hashAlgorithm = CryptoHelpers.CreateFromName(reference.DigestMethod); string hashAlgorithmName = hashAlgorithm == null ? "null" : hashAlgorithm.GetType().Name; string logMessage = SR.Format(CultureInfo.InvariantCulture, SR.Log_SignedXmlRecursionLimit, From 71db6e3cb04364fd70d8185b290131d6dff1b784 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 22:03:37 +0100 Subject: [PATCH 13/67] More annotations --- .../src/System/Security/Cryptography/Xml/EncryptedXml.cs | 4 ++-- .../src/System/Security/Cryptography/Xml/SignedXml.cs | 2 +- .../src/System/Security/Cryptography/Xml/Utils.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 3e105548b42d2..efb8f846a1930 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -344,7 +344,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme kiRetrievalMethod = keyInfoEnum.Current as KeyInfoRetrievalMethod; if (kiRetrievalMethod != null) { - string idref = Utils.ExtractIdFromLocalUri(kiRetrievalMethod.Uri); + string idref = Utils.ExtractIdFromLocalUri(kiRetrievalMethod.Uri!); ek = new EncryptedKey(); ek.LoadXml(GetIdElement(_document, idref)!); break; @@ -450,7 +450,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme kiRetrievalMethod = keyInfoEnum.Current as KeyInfoRetrievalMethod; if (kiRetrievalMethod != null) { - string idref = Utils.ExtractIdFromLocalUri(kiRetrievalMethod.Uri); + string idref = Utils.ExtractIdFromLocalUri(kiRetrievalMethod.Uri!); ek = new EncryptedKey(); ek.LoadXml(GetIdElement(_document, idref)!); try diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index d709f0ff301ce..162f2223059c0 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -810,7 +810,7 @@ private int GetReferenceLevel(int index, ArrayList references) } if (reference.Uri.Length > 0 && reference.Uri[0] == '#') { - string idref = Utils.ExtractIdFromLocalUri(reference.Uri); + string idref = Utils.ExtractIdFromLocalUri(reference.Uri!); if (idref == "xpointer(/)") { _refLevelCache![index] = 0; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 5a643502af86b..4ca63412e33d3 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -324,7 +324,7 @@ internal static bool NodeInList(XmlNode? node, XmlNodeList nodeList) return false; } - internal static string GetIdFromLocalUri(string? uri, out bool discardComments) + internal static string GetIdFromLocalUri(string uri, out bool discardComments) { string idref = uri.Substring(1); // initialize the return value From 408d355345ff318a6d0ef87863bee19f3a41de9e Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 22:13:04 +0100 Subject: [PATCH 14/67] More annotations --- .../Security/Cryptography/Xml/Reference.cs | 18 ++++---- .../Xml/XmlDecryptionTransform.cs | 42 +++++++++---------- .../Cryptography/Xml/XmlDsigC14NTransform.cs | 12 +++--- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index 9f1666dc76b64..5b844d5010f57 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -359,8 +359,8 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList { case ReferenceTargetType.Stream: // This is the easiest case. We already have a stream, so just pump it through the TransformChain - resolver = (SignedXml.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); - hashInputStream = TransformChain.TransformToOctetStream((Stream)_refTarget, resolver, baseUri); + resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); + hashInputStream = TransformChain.TransformToOctetStream((Stream?)_refTarget, resolver, baseUri); break; case ReferenceTargetType.UriReference: // Second-easiest case -- dereference the URI & pump through the TransformChain @@ -372,7 +372,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); // In the case of a Uri-less reference, we will simply pass null to the transform chain. // The first transform in the chain is expected to know how to retrieve the data to hash. - hashInputStream = TransformChain.TransformToOctetStream((Stream?)null, resolver, baseUri); + hashInputStream = TransformChain.TransformToOctetStream((Stream?)null, resolver!, baseUri); } else if (_uri.Length == 0) { @@ -383,8 +383,8 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList // Normalize the containing document resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); - XmlDocument docWithNoComments = Utils.DiscardComments(Utils.PreProcessDocumentInput(document, resolver, baseUri)); - hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver, baseUri); + XmlDocument docWithNoComments = Utils.DiscardComments(Utils.PreProcessDocumentInput(document, resolver!, baseUri)); + hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver!, baseUri); } else if (_uri[0] == '#') { @@ -399,7 +399,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList // We should not discard comments here!!! resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); - hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessDocumentInput(document, resolver, baseUri), resolver, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessDocumentInput(document, resolver!, baseUri), resolver!, baseUri); break; } @@ -439,12 +439,12 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList { // We should discard comments before going into the transform chain XmlDocument docWithNoComments = Utils.DiscardComments(normDocument); - hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver!, baseUri); } else { // This is an XPointer reference, do not discard comments!!! - hashInputStream = TransformChain.TransformToOctetStream(normDocument, resolver, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(normDocument, resolver!, baseUri); } } else @@ -455,7 +455,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList case ReferenceTargetType.XmlElement: // We need to create a DocumentNavigator out of the XmlElement resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); - hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessElementInput((XmlElement)_refTarget, resolver, baseUri), resolver, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessElementInput((XmlElement)_refTarget!, resolver!, baseUri), resolver!, baseUri); break; default: throw new CryptographicException(SR.Cryptography_Xml_UriNotResolved, _uri); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs index 54886e938853d..5f1cc9e26f169 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs @@ -14,11 +14,11 @@ public class XmlDecryptionTransform : Transform { private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlDocument) }; private readonly Type[] _outputTypes = { typeof(XmlDocument) }; - private XmlNodeList _encryptedDataList; - private ArrayList _arrayListUri; // this ArrayList object represents the Uri's to be excluded - private EncryptedXml _exml; // defines the XML encryption processing rules - private XmlDocument _containingDocument; - private XmlNamespaceManager _nsm; + private XmlNodeList? _encryptedDataList; + private ArrayList? _arrayListUri; // this ArrayList object represents the Uri's to be excluded + private EncryptedXml? _exml; // defines the XML encryption processing rules + private XmlDocument? _containingDocument; + private XmlNamespaceManager? _nsm; private const string XmlDecryptionTransformNamespaceUrl = "http://www.w3.org/2002/07/decrypt#"; public XmlDecryptionTransform() @@ -28,7 +28,7 @@ public XmlDecryptionTransform() private ArrayList ExceptUris => _arrayListUri ??= new ArrayList(); - protected virtual bool IsTargetElement(XmlElement inputElement, string idValue) + protected virtual bool IsTargetElement(XmlElement? inputElement, string idValue) { if (inputElement == null) return false; @@ -46,10 +46,10 @@ public EncryptedXml EncryptedXml if (_exml != null) return _exml; - Reference reference = Reference; - SignedXml signedXml = (reference == null ? SignedXml : reference.SignedXml); + Reference? reference = Reference; + SignedXml? signedXml = (reference == null ? SignedXml : reference.SignedXml); if (signedXml == null || signedXml.EncryptedXml == null) - _exml = new EncryptedXml(_containingDocument); // default processing rules + _exml = new EncryptedXml(_containingDocument!); // default processing rules else _exml = signedXml.EncryptedXml; @@ -85,7 +85,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) ExceptUris.Clear(); foreach (XmlNode node in nodeList) { - XmlElement elem = node as XmlElement; + XmlElement? elem = node as XmlElement; if (elem != null) { if (elem.LocalName == "Except" && elem.NamespaceURI == XmlDecryptionTransformNamespaceUrl) @@ -143,7 +143,7 @@ private void LoadStreamInput(Stream stream) XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; XmlResolver resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); - XmlReader xmlReader = Utils.PreProcessStreamInput(stream, resolver, BaseURI); + XmlReader xmlReader = Utils.PreProcessStreamInput(stream, resolver, BaseURI!); document.Load(xmlReader); _containingDocument = document; _nsm = new XmlNamespaceManager(_containingDocument.NameTable); @@ -169,7 +169,7 @@ private void LoadXmlDocumentInput(XmlDocument document) // Replace the encrypted XML element with the decrypted data for signature verification private void ReplaceEncryptedData(XmlElement encryptedDataElement, byte[] decrypted) { - XmlNode parent = encryptedDataElement.ParentNode; + XmlNode parent = encryptedDataElement.ParentNode!; if (parent.NodeType == XmlNodeType.Document) { // We're replacing the root element. In order to correctly reflect the semantics of the @@ -194,13 +194,13 @@ private bool ProcessEncryptedDataItem(XmlElement encryptedDataElement) { for (int index = 0; index < ExceptUris.Count; index++) { - if (IsTargetElement(encryptedDataElement, (string)ExceptUris[index])) + if (IsTargetElement(encryptedDataElement, ((string)ExceptUris[index]!)!)) return false; } } EncryptedData ed = new EncryptedData(); ed.LoadXml(encryptedDataElement); - SymmetricAlgorithm symAlg = EncryptedXml.GetDecryptionKey(ed, null); + SymmetricAlgorithm? symAlg = EncryptedXml.GetDecryptionKey(ed, null); if (symAlg == null) throw new CryptographicException(SR.Cryptography_Xml_MissingDecryptionKey); byte[] decrypted = EncryptedXml.DecryptData(ed, symAlg); @@ -218,24 +218,24 @@ private void ProcessElementRecursively(XmlNodeList encryptedDatas) { encryptedDatasQueue.Enqueue(value); } - XmlNode node = encryptedDatasQueue.Dequeue() as XmlNode; + XmlNode? node = encryptedDatasQueue.Dequeue() as XmlNode; while (node != null) { - XmlElement encryptedDataElement = node as XmlElement; + XmlElement? encryptedDataElement = node as XmlElement; if (encryptedDataElement != null && encryptedDataElement.LocalName == "EncryptedData" && encryptedDataElement.NamespaceURI == EncryptedXml.XmlEncNamespaceUrl) { - XmlNode sibling = encryptedDataElement.NextSibling; - XmlNode parent = encryptedDataElement.ParentNode; + XmlNode sibling = encryptedDataElement.NextSibling!; + XmlNode parent = encryptedDataElement.ParentNode!; if (ProcessEncryptedDataItem(encryptedDataElement)) { // find the new decrypted element. - XmlNode child = parent.FirstChild; + XmlNode? child = parent.FirstChild; while (child != null && child.NextSibling != sibling) child = child.NextSibling; if (child != null) { - XmlNodeList nodes = child.SelectNodes("//enc:EncryptedData", _nsm); + XmlNodeList nodes = child.SelectNodes("//enc:EncryptedData", _nsm!)!; if (nodes.Count > 0) { foreach (XmlNode value in nodes) @@ -258,7 +258,7 @@ public override object GetOutput() if (_encryptedDataList != null) ProcessElementRecursively(_encryptedDataList); // propagate namespaces - Utils.AddNamespaces(_containingDocument.DocumentElement, PropagatedNamespaces); + Utils.AddNamespaces(_containingDocument!.DocumentElement!, PropagatedNamespaces); return _containingDocument; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs index 9ccfd1e060a5f..d42d6ce38591c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs @@ -10,7 +10,7 @@ public class XmlDsigC14NTransform : Transform { private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlDocument), typeof(XmlNodeList) }; private readonly Type[] _outputTypes = { typeof(Stream) }; - private CanonicalXml _cXml; + private CanonicalXml? _cXml; private readonly bool _includeComments; public XmlDsigC14NTransform() @@ -47,10 +47,10 @@ public override void LoadInnerXml(XmlNodeList nodeList) public override void LoadInput(object obj) { - XmlResolver resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); + XmlResolver resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver())!; if (obj is Stream) { - _cXml = new CanonicalXml((Stream)obj, _includeComments, resolver, BaseURI); + _cXml = new CanonicalXml((Stream)obj, _includeComments, resolver, BaseURI!); return; } if (obj is XmlDocument) @@ -70,19 +70,19 @@ public override void LoadInput(object obj) public override object GetOutput() { - return new MemoryStream(_cXml.GetBytes()); + return new MemoryStream(_cXml!.GetBytes()); } public override object GetOutput(Type type) { if (type != typeof(Stream) && !type.IsSubclassOf(typeof(Stream))) throw new ArgumentException(SR.Cryptography_Xml_TransformIncorrectInputType, nameof(type)); - return new MemoryStream(_cXml.GetBytes()); + return new MemoryStream(_cXml!.GetBytes()); } public override byte[]? GetDigestedOutput(HashAlgorithm hash) { - return _cXml.GetDigestedBytes(hash); + return _cXml!.GetDigestedBytes(hash); } } } From d32935dbd78ef5bf1f2f36e82ce661416a9618e6 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 22:40:32 +0100 Subject: [PATCH 15/67] More annotations --- .../C14NAncestralNamespaceContextManager.cs | 4 +- .../Cryptography/Xml/CanonicalXmlDocument.cs | 10 ++--- .../Cryptography/Xml/CanonicalXmlElement.cs | 12 +++--- .../Security/Cryptography/Xml/DSAKeyValue.cs | 2 +- .../Xml/EncryptionPropertyCollection.cs | 4 +- .../Security/Cryptography/Xml/KeyInfo.cs | 8 ++-- .../Security/Cryptography/Xml/KeyInfoNode.cs | 6 +-- .../Security/Cryptography/Xml/Signature.cs | 38 ++++++++--------- .../Security/Cryptography/Xml/SignedInfo.cs | 32 +++++++------- .../Security/Cryptography/Xml/SignedXml.cs | 42 +++++++++---------- .../Xml/XmlDsigEnvelopedSignatureTransform.cs | 14 +++---- 11 files changed, 86 insertions(+), 86 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs index c96d3cc79727f..9f384afbc83a7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs @@ -86,12 +86,12 @@ internal override void GetNamespacesToRender(XmlElement element, SortedList attr internal override void TrackNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) { - nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr), attr); + nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr!), attr); } internal override void TrackXmlNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) { - nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr), attr); + nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr!), attr); } } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index b519bb4a2a273..326469558650f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -63,7 +63,7 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace public override XmlElement CreateElement(string? prefix, string localName, string? namespaceURI) { - return new CanonicalXmlElement(prefix, localName, namespaceURI, this, _defaultNodeSetInclusionState); + return new CanonicalXmlElement(prefix!, localName, namespaceURI!, this, _defaultNodeSetInclusionState); } public override XmlAttribute CreateAttribute(string? prefix, string localName, string? namespaceURI) @@ -83,12 +83,12 @@ public override XmlText CreateTextNode(string? text) public override XmlWhitespace CreateWhitespace(string? prefix) { - return new CanonicalXmlWhitespace(prefix, this, _defaultNodeSetInclusionState); + return new CanonicalXmlWhitespace(prefix!, this, _defaultNodeSetInclusionState); } public override XmlSignificantWhitespace CreateSignificantWhitespace(string? text) { - return new CanonicalXmlSignificantWhitespace(text, this, _defaultNodeSetInclusionState); + return new CanonicalXmlSignificantWhitespace(text!, this, _defaultNodeSetInclusionState); } public override XmlProcessingInstruction CreateProcessingInstruction(string target, string data) @@ -98,7 +98,7 @@ public override XmlProcessingInstruction CreateProcessingInstruction(string targ public override XmlComment CreateComment(string? data) { - return new CanonicalXmlComment(data, this, _defaultNodeSetInclusionState, _includeComments); + return new CanonicalXmlComment(data!, this, _defaultNodeSetInclusionState, _includeComments); } public override XmlEntityReference CreateEntityReference(string name) @@ -108,7 +108,7 @@ public override XmlEntityReference CreateEntityReference(string name) public override XmlCDataSection CreateCDataSection(string? data) { - return new CanonicalXmlCDataSection(data, this, _defaultNodeSetInclusionState); + return new CanonicalXmlCDataSection(data!, this, _defaultNodeSetInclusionState); } } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index 3c0ffbcd4ea0c..8cb21d2f2f2d4 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -30,12 +30,12 @@ public void Write(StringBuilder strBuilder, DocPosition docPos, AncestralNamespa SortedList nsListToRender = new SortedList(new NamespaceSortOrder()); SortedList attrListToRender = new SortedList(new AttributeSortOrder()); - XmlAttributeCollection attrList = Attributes; + XmlAttributeCollection? attrList = Attributes; if (attrList != null) { - foreach (XmlAttribute? attr in attrList) + foreach (XmlAttribute attr in attrList) { - if (((CanonicalXmlAttribute)attr).IsInNodeSet || Utils.IsNamespaceNode(attr) || Utils.IsXmlNamespaceNode(attr)) + if ((((CanonicalXmlAttribute)attr)).IsInNodeSet || Utils.IsNamespaceNode(attr) || Utils.IsXmlNamespaceNode(attr)) { if (Utils.IsNamespaceNode(attr)) { @@ -56,7 +56,7 @@ public void Write(StringBuilder strBuilder, DocPosition docPos, AncestralNamespa if (!Utils.IsCommittedNamespace(this, Prefix, NamespaceURI)) { string name = ((Prefix.Length > 0) ? "xmlns" + ":" + Prefix : "xmlns"); - XmlAttribute? nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name); + XmlAttribute nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name)!; nsattrib.Value = NamespaceURI; anc.TrackNamespaceNode(nsattrib, nsListToRender, nsLocallyDeclared); } @@ -106,7 +106,7 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace XmlAttributeCollection attrList = Attributes; if (attrList != null) { - foreach (XmlAttribute? attr in attrList) + foreach (XmlAttribute attr in attrList) { if (((CanonicalXmlAttribute)attr).IsInNodeSet || Utils.IsNamespaceNode(attr) || Utils.IsXmlNamespaceNode(attr)) { @@ -129,7 +129,7 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace if (!Utils.IsCommittedNamespace(this, Prefix, NamespaceURI)) { string name = ((Prefix.Length > 0) ? "xmlns" + ":" + Prefix : "xmlns"); - XmlAttribute? nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name); + XmlAttribute nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name); nsattrib.Value = NamespaceURI; anc.TrackNamespaceNode(nsattrib, nsListToRender, nsLocallyDeclared); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs index c60440bafff35..b419031f68cad 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs @@ -182,7 +182,7 @@ public override void LoadXml(XmlElement value) try { - Key.ImportParameters(new DSAParameters + Key!.ImportParameters(new DSAParameters { P = (pNode != null) ? Convert.FromBase64String(pNode.InnerText) : null, Q = (qNode != null) ? Convert.FromBase64String(qNode.InnerText) : null, diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs index 94553fab29a06..6246b541f61b9 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs @@ -116,7 +116,7 @@ public bool IsReadOnly public EncryptionProperty Item(int index) { - return (EncryptionProperty)_props[index]; + return (EncryptionProperty)_props[index]!; } [System.Runtime.CompilerServices.IndexerName("ItemOf")] @@ -124,7 +124,7 @@ public EncryptionProperty this[int index] { get { - return (EncryptionProperty)((IList)this)[index]; + return (EncryptionProperty)((IList)this)[index]!; } set { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs index cee0ecc8f9b73..8b4db7b767466 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs @@ -81,14 +81,14 @@ public void LoadXml(XmlElement value) // Special-case handling for KeyValue -- we have to go one level deeper if (kicString == "http://www.w3.org/2000/09/xmldsig# KeyValue") { - if (!Utils.VerifyAttributes(elem, (string[])null)) + if (!Utils.VerifyAttributes(elem, (string[]?)null)) { throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "KeyInfo/KeyValue"); } XmlNodeList nodeList2 = elem.ChildNodes; foreach (XmlNode node2 in nodeList2) { - XmlElement elem2 = node2 as XmlElement; + XmlElement? elem2 = node2 as XmlElement; if (elem2 != null) { kicString += "/" + elem2.LocalName; @@ -97,7 +97,7 @@ public void LoadXml(XmlElement value) } } - KeyInfoClause keyInfoClause = CryptoHelpers.CreateFromName(kicString); + KeyInfoClause? keyInfoClause = CryptoHelpers.CreateFromName(kicString); // if we don't know what kind of KeyInfoClause we're looking at, use a generic KeyInfoNode: keyInfoClause ??= new KeyInfoNode(); @@ -138,7 +138,7 @@ public IEnumerator GetEnumerator(Type requestedObjectType) while (tempEnum.MoveNext()) { - tempObj = tempEnum.Current; + tempObj = tempEnum.Current!; if (requestedObjectType.Equals(tempObj.GetType())) requestedList.Add(tempObj); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoNode.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoNode.cs index 87e7c19c33533..c0c47bd4cc318 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoNode.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoNode.cs @@ -8,7 +8,7 @@ namespace System.Security.Cryptography.Xml // This is for generic, unknown nodes public class KeyInfoNode : KeyInfoClause { - private XmlElement _node; + private XmlElement? _node; // // public constructors @@ -25,7 +25,7 @@ public KeyInfoNode(XmlElement node) // public properties // - public XmlElement Value + public XmlElement? Value { get { return _node; } set { _node = value; } @@ -44,7 +44,7 @@ public override XmlElement GetXml() internal override XmlElement GetXml(XmlDocument xmlDocument) { - return xmlDocument.ImportNode(_node, true) as XmlElement; + return (xmlDocument.ImportNode(_node!, true) as XmlElement)!; } public override void LoadXml(XmlElement value) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Signature.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Signature.cs index 388f5f95a23de..d227ea020a1b3 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Signature.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Signature.cs @@ -8,16 +8,16 @@ namespace System.Security.Cryptography.Xml { public class Signature { - private string _id; - private SignedInfo _signedInfo; + private string? _id; + private SignedInfo? _signedInfo; private byte[]? _signatureValue; - private string _signatureValueId; - private KeyInfo _keyInfo; + private string? _signatureValueId; + private KeyInfo? _keyInfo; private IList _embeddedObjects; private readonly CanonicalXmlNodeList _referencedItems; - private SignedXml _signedXml; + private SignedXml? _signedXml; - internal SignedXml SignedXml + internal SignedXml? SignedXml { get { return _signedXml; } set { _signedXml = value; } @@ -37,13 +37,13 @@ public Signature() // public properties // - public string Id + public string? Id { get { return _id; } set { _id = value; } } - public SignedInfo SignedInfo + public SignedInfo? SignedInfo { get { return _signedInfo; } set @@ -118,7 +118,7 @@ internal XmlElement GetXml(XmlDocument document) // Add the Objects foreach (object obj in _embeddedObjects) { - DataObject dataObj = obj as DataObject; + DataObject? dataObj = obj as DataObject; if (dataObj != null) { signatureElement.AppendChild(dataObj.GetXml(document)); @@ -150,20 +150,20 @@ public void LoadXml(XmlElement value) int expectedChildNodes = 0; // SignedInfo - XmlNodeList signedInfoNodes = signatureElement.SelectNodes("ds:SignedInfo", nsm); + XmlNodeList? signedInfoNodes = signatureElement.SelectNodes("ds:SignedInfo", nsm); if (signedInfoNodes == null || signedInfoNodes.Count == 0 || signedInfoNodes.Count > 1) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo"); - XmlElement signedInfoElement = signedInfoNodes[0] as XmlElement; + XmlElement signedInfoElement = (signedInfoNodes[0] as XmlElement)!; expectedChildNodes += signedInfoNodes.Count; SignedInfo = new SignedInfo(); SignedInfo.LoadXml(signedInfoElement); // SignatureValue - XmlNodeList signatureValueNodes = signatureElement.SelectNodes("ds:SignatureValue", nsm); + XmlNodeList? signatureValueNodes = signatureElement.SelectNodes("ds:SignatureValue", nsm); if (signatureValueNodes == null || signatureValueNodes.Count == 0 || signatureValueNodes.Count > 1) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignatureValue"); - XmlElement signatureValueElement = signatureValueNodes[0] as XmlElement; + XmlElement signatureValueElement = (signatureValueNodes[0] as XmlElement)!; expectedChildNodes += signatureValueNodes.Count; _signatureValue = Convert.FromBase64String(Utils.DiscardWhiteSpaces(signatureValueElement.InnerText)); _signatureValueId = Utils.GetAttribute(signatureValueElement, "Id", SignedXml.XmlDsigNamespaceUrl); @@ -171,7 +171,7 @@ public void LoadXml(XmlElement value) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignatureValue"); // KeyInfo - optional single element - XmlNodeList keyInfoNodes = signatureElement.SelectNodes("ds:KeyInfo", nsm); + XmlNodeList? keyInfoNodes = signatureElement.SelectNodes("ds:KeyInfo", nsm); _keyInfo = new KeyInfo(); if (keyInfoNodes != null) { @@ -181,7 +181,7 @@ public void LoadXml(XmlElement value) } foreach (XmlNode node in keyInfoNodes) { - XmlElement keyInfoElement = node as XmlElement; + XmlElement? keyInfoElement = node as XmlElement; if (keyInfoElement != null) _keyInfo.LoadXml(keyInfoElement); } @@ -189,13 +189,13 @@ public void LoadXml(XmlElement value) } // Object - zero or more elements allowed - XmlNodeList objectNodes = signatureElement.SelectNodes("ds:Object", nsm); + XmlNodeList? objectNodes = signatureElement.SelectNodes("ds:Object", nsm); _embeddedObjects.Clear(); if (objectNodes != null) { foreach (XmlNode node in objectNodes) { - XmlElement objectElement = node as XmlElement; + XmlElement? objectElement = node as XmlElement; if (objectElement != null) { DataObject dataObj = new DataObject(); @@ -207,7 +207,7 @@ public void LoadXml(XmlElement value) } // Select all elements that have Id attributes - XmlNodeList nodeList = signatureElement.SelectNodes("//*[@Id]", nsm); + XmlNodeList? nodeList = signatureElement.SelectNodes("//*[@Id]", nsm); if (nodeList != null) { foreach (XmlNode node in nodeList) @@ -216,7 +216,7 @@ public void LoadXml(XmlElement value) } } // Verify that there aren't any extra nodes that aren't allowed - if (signatureElement.SelectNodes("*").Count != expectedChildNodes) + if (signatureElement.SelectNodes("*")!.Count != expectedChildNodes) { throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Signature"); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs index c4e03d749d72f..49ababd3b7353 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs @@ -8,10 +8,10 @@ namespace System.Security.Cryptography.Xml { public class SignedInfo : ICollection { - private string _id; - private string _canonicalizationMethod; - private string _signatureMethod; - private string _signatureLength; + private string? _id; + private string? _canonicalizationMethod; + private string? _signatureMethod; + private string? _signatureLength; private readonly ArrayList _references; private XmlElement? _cachedXml; private SignedXml? _signedXml; @@ -62,7 +62,7 @@ public object SyncRoot // public properties // - public string Id + public string? Id { get { return _id; } set @@ -104,7 +104,7 @@ public Transform CanonicalizationMethodObject } } - public string SignatureMethod + public string? SignatureMethod { get { return _signatureMethod; } set @@ -114,7 +114,7 @@ public string SignatureMethod } } - public string SignatureLength + public string? SignatureLength { get { return _signatureLength; } set @@ -190,7 +190,7 @@ internal XmlElement GetXml(XmlDocument document) for (int i = 0; i < _references.Count; ++i) { - Reference reference = (Reference)_references[i]; + Reference reference = (Reference)_references[i]!; signedInfoElement.AppendChild(reference.GetXml(document)); } @@ -219,10 +219,10 @@ public void LoadXml(XmlElement value) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo"); // CanonicalizationMethod -- must be present - XmlNodeList canonicalizationMethodNodes = signedInfoElement.SelectNodes("ds:CanonicalizationMethod", nsm); + XmlNodeList? canonicalizationMethodNodes = signedInfoElement.SelectNodes("ds:CanonicalizationMethod", nsm); if (canonicalizationMethodNodes == null || canonicalizationMethodNodes.Count == 0 || canonicalizationMethodNodes.Count > 1) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo/CanonicalizationMethod"); - XmlElement canonicalizationMethodElement = canonicalizationMethodNodes.Item(0) as XmlElement; + XmlElement canonicalizationMethodElement = (canonicalizationMethodNodes.Item(0) as XmlElement)!; expectedChildNodes += canonicalizationMethodNodes.Count; _canonicalizationMethod = Utils.GetAttribute(canonicalizationMethodElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); if (_canonicalizationMethod == null || !Utils.VerifyAttributes(canonicalizationMethodElement, "Algorithm")) @@ -232,17 +232,17 @@ public void LoadXml(XmlElement value) CanonicalizationMethodObject.LoadInnerXml(canonicalizationMethodElement.ChildNodes); // SignatureMethod -- must be present - XmlNodeList signatureMethodNodes = signedInfoElement.SelectNodes("ds:SignatureMethod", nsm); + XmlNodeList? signatureMethodNodes = signedInfoElement.SelectNodes("ds:SignatureMethod", nsm); if (signatureMethodNodes == null || signatureMethodNodes.Count == 0 || signatureMethodNodes.Count > 1) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo/SignatureMethod"); - XmlElement signatureMethodElement = signatureMethodNodes.Item(0) as XmlElement; + XmlElement signatureMethodElement = (signatureMethodNodes.Item(0) as XmlElement)!; expectedChildNodes += signatureMethodNodes.Count; _signatureMethod = Utils.GetAttribute(signatureMethodElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); if (_signatureMethod == null || !Utils.VerifyAttributes(signatureMethodElement, "Algorithm")) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo/SignatureMethod"); // Now get the output length if we are using a MAC algorithm - XmlElement signatureLengthElement = signatureMethodElement.SelectSingleNode("ds:HMACOutputLength", nsm) as XmlElement; + XmlElement? signatureLengthElement = signatureMethodElement.SelectSingleNode("ds:HMACOutputLength", nsm) as XmlElement; if (signatureLengthElement != null) _signatureLength = signatureLengthElement.InnerXml; @@ -250,7 +250,7 @@ public void LoadXml(XmlElement value) _references.Clear(); // Reference - 0 or more - XmlNodeList referenceNodes = signedInfoElement.SelectNodes("ds:Reference", nsm); + XmlNodeList? referenceNodes = signedInfoElement.SelectNodes("ds:Reference", nsm); if (referenceNodes != null) { if (referenceNodes.Count > Utils.MaxReferencesPerSignedInfo) @@ -259,14 +259,14 @@ public void LoadXml(XmlElement value) } foreach (XmlNode node in referenceNodes) { - XmlElement referenceElement = node as XmlElement; + XmlElement referenceElement = (node as XmlElement)!; Reference reference = new Reference(); AddReference(reference); reference.LoadXml(referenceElement); } expectedChildNodes += referenceNodes.Count; // Verify that there aren't any extra nodes that aren't allowed - if (signedInfoElement.SelectNodes("*").Count != expectedChildNodes) + if (signedInfoElement.SelectNodes("*")!.Count != expectedChildNodes) { throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo"); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index 162f2223059c0..68c7039c7871b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -178,19 +178,19 @@ public Signature? Signature get { return m_signature; } } - public SignedInfo SignedInfo + public SignedInfo? SignedInfo { get { return m_signature!.SignedInfo; } } - public string SignatureMethod + public string? SignatureMethod { - get { return m_signature!.SignedInfo.SignatureMethod; } + get { return m_signature!.SignedInfo!.SignatureMethod; } } - public string SignatureLength + public string? SignatureLength { - get { return m_signature!.SignedInfo.SignatureLength; } + get { return m_signature!.SignedInfo!.SignatureLength; } } public byte[]? SignatureValue @@ -233,7 +233,7 @@ public void LoadXml(XmlElement value) public void AddReference(Reference reference) { - m_signature!.SignedInfo.AddReference(reference); + m_signature!.SignedInfo!.AddReference(reference); } public void AddObject(DataObject dataObject) @@ -383,7 +383,7 @@ public void ComputeSignature() throw new CryptographicException(SR.Cryptography_Xml_LoadKeyFailed); // Check the signature algorithm associated with the key so that we can accordingly set the signature method - if (SignedInfo.SignatureMethod == null) + if (SignedInfo!.SignatureMethod == null) { if (key is DSA) { @@ -430,7 +430,7 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) throw new CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch); int signatureLength; - if (m_signature!.SignedInfo.SignatureLength == null) + if (m_signature!.SignedInfo!.SignatureLength == null) signatureLength = hash.HashSize; else signatureLength = Convert.ToInt32(m_signature.SignedInfo.SignatureLength, null); @@ -441,7 +441,7 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) throw new CryptographicException(SR.Cryptography_Xml_InvalidSignatureLength2); BuildDigestedReferences(); - SignedInfo.SignatureMethod = hash.HashName switch + SignedInfo!.SignatureMethod = hash.HashName switch { "SHA1" => SignedXml.XmlDsigHMACSHA1Url, "SHA256" => SignedXml.XmlDsigMoreHMACSHA256Url, @@ -629,7 +629,7 @@ private static bool DefaultSignatureFormatValidator(SignedXml signedXml) private bool DoesSignatureUseTruncatedHmac() { // If we're not using the SignatureLength property, then we're not truncating the signature length - if (SignedInfo.SignatureLength == null) + if (SignedInfo!.SignatureLength == null) { return false; } @@ -662,13 +662,13 @@ private bool DoesSignatureUseSafeCanonicalizationMethod() { foreach (string safeAlgorithm in SafeCanonicalizationMethods!) { - if (string.Equals(safeAlgorithm, SignedInfo.CanonicalizationMethod, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(safeAlgorithm, SignedInfo!.CanonicalizationMethod, StringComparison.OrdinalIgnoreCase)) { return true; } } - SignedXmlDebugLog.LogUnsafeCanonicalizationMethod(this, SignedInfo.CanonicalizationMethod, SafeCanonicalizationMethods); + SignedXmlDebugLog.LogUnsafeCanonicalizationMethod(this, SignedInfo!.CanonicalizationMethod, SafeCanonicalizationMethods); return false; } @@ -773,11 +773,11 @@ private static IList DefaultSafeTransformMethods private byte[]? GetC14NDigest(HashAlgorithm hash) { bool isKeyedHashAlgorithm = hash is KeyedHashAlgorithm; - if (isKeyedHashAlgorithm || !_bCacheValid || !SignedInfo.CacheValid) + if (isKeyedHashAlgorithm || !_bCacheValid || !SignedInfo!.CacheValid) { string? baseUri = _containingDocument?.BaseURI; XmlResolver? resolver = (_bResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); - XmlDocument doc = Utils.PreProcessElementInput(SignedInfo.GetXml(), resolver!, baseUri!); + XmlDocument doc = Utils.PreProcessElementInput(SignedInfo!.GetXml(), resolver!, baseUri!); // Add non default namespaces in scope CanonicalXmlNodeList? namespaces = (_context == null ? null : Utils.GetPropagatedAttributes(_context)); @@ -835,12 +835,12 @@ private int GetReferenceLevel(int index, ArrayList references) private sealed class ReferenceLevelSortOrder : IComparer { - private ArrayList _references; + private ArrayList? _references; public ReferenceLevelSortOrder() { } public ArrayList References { - get { return _references; } + get { return _references!; } set { _references = value; } } @@ -869,7 +869,7 @@ public int Compare(object? a, object? b) private void BuildDigestedReferences() { // Default the DigestMethod and Canonicalization - ArrayList references = SignedInfo.References; + ArrayList references = SignedInfo!.References; // Reset the cache _refProcessed = new bool[references.Count]; _refLevelCache = new int[references.Count]; @@ -905,7 +905,7 @@ private void BuildDigestedReferences() private bool CheckDigestedReferences() { - ArrayList references = m_signature!.SignedInfo.References; + ArrayList references = m_signature!.SignedInfo!.References; for (int i = 0; i < references.Count; ++i) { Reference digestedReference = (Reference)references[i]!; @@ -998,7 +998,7 @@ private bool CheckSignedInfo(AsymmetricAlgorithm key) throw new ArgumentNullException(nameof(key)); } - SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature!.SignedInfo); + SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature!.SignedInfo!); SignatureDescription? signatureDescription = CryptoHelpers.CreateFromName(SignatureMethod); if (signatureDescription == null) @@ -1032,10 +1032,10 @@ private bool CheckSignedInfo(KeyedHashAlgorithm macAlg) throw new ArgumentNullException(nameof(macAlg)); } - SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature!.SignedInfo); + SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature!.SignedInfo!); int signatureLength; - if (m_signature.SignedInfo.SignatureLength == null) + if (m_signature.SignedInfo!.SignatureLength == null) signatureLength = macAlg.HashSize; else signatureLength = Convert.ToInt32(m_signature.SignedInfo.SignatureLength, null); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs index b2d29245edb5f..ac75d5141cd1d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs @@ -79,8 +79,8 @@ private void LoadStreamInput(Stream stream) { XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; - XmlResolver resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); - XmlReader xmlReader = Utils.PreProcessStreamInput(stream, resolver, BaseURI); + XmlResolver resolver = ((ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver())! ); + XmlReader xmlReader = Utils.PreProcessStreamInput(stream, resolver, BaseURI!); doc.Load(xmlReader); _containingDocument = doc; if (_containingDocument == null) @@ -127,7 +127,7 @@ public override object GetOutput() { // If the position has not been set, then we don't want to remove any signature tags if (_signaturePosition == 0) return _inputNodeList; - XmlNodeList signatureList = _containingDocument.SelectNodes("//dsig:Signature", _nsm); + XmlNodeList? signatureList = _containingDocument.SelectNodes("//dsig:Signature", _nsm!); if (signatureList == null) return _inputNodeList; CanonicalXmlNodeList resultNodeList = new CanonicalXmlNodeList(); @@ -145,7 +145,7 @@ public override object GetOutput() try { // Find the nearest signature ancestor tag - XmlNode result = node.SelectSingleNode("ancestor-or-self::dsig:Signature[1]", _nsm); + XmlNode result = node.SelectSingleNode("ancestor-or-self::dsig:Signature[1]", _nsm!)!; int position = 0; foreach (XmlNode node1 in signatureList) { @@ -165,12 +165,12 @@ public override object GetOutput() // Else we have received either a stream or a document as input else { - XmlNodeList signatureList = _containingDocument.SelectNodes("//dsig:Signature", _nsm); + XmlNodeList? signatureList = _containingDocument.SelectNodes("//dsig:Signature", _nsm!); if (signatureList == null) return _containingDocument; if (signatureList.Count < _signaturePosition || _signaturePosition <= 0) return _containingDocument; // Remove the signature node with all its children nodes - signatureList[_signaturePosition - 1].ParentNode.RemoveChild(signatureList[_signaturePosition - 1]); + signatureList[_signaturePosition - 1]!.ParentNode!.RemoveChild(signatureList[_signaturePosition - 1]!); return _containingDocument; } } @@ -179,7 +179,7 @@ public override object GetOutput(Type type) { if (type == typeof(XmlNodeList) || type.IsSubclassOf(typeof(XmlNodeList))) { - _inputNodeList ??= Utils.AllDescendantNodes(_containingDocument, true); + _inputNodeList ??= Utils.AllDescendantNodes(_containingDocument!, true); return (XmlNodeList)GetOutput(); } else if (type == typeof(XmlDocument) || type.IsSubclassOf(typeof(XmlDocument))) From 5b355834c792f3abf99e16a1d152fc3841f9edb3 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 22:45:41 +0100 Subject: [PATCH 16/67] Remove red flag comment --- .../src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs index ce385265050b3..87bb9ac53ef7f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoX509Data.cs @@ -270,8 +270,6 @@ public override void LoadXml(XmlElement element) XmlNamespaceManager nsm = new XmlNamespaceManager(element.OwnerDocument.NameTable); nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); - //red flag - no existing null checks before usage, so should we assume that it's not null, - //or should we add checks ourselves? (or is the dammit operator fine here?) XmlNodeList x509IssuerSerialNodes = element.SelectNodes("ds:X509IssuerSerial", nsm)!; XmlNodeList x509SKINodes = element.SelectNodes("ds:X509SKI", nsm)!; XmlNodeList x509SubjectNameNodes = element.SelectNodes("ds:X509SubjectName", nsm)!; From e4d3350ad8406743e22991dca424b923b4a43e50 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 22:50:16 +0100 Subject: [PATCH 17/67] PR feedback --- .../Cryptography/Xml/C14NAncestralNamespaceContextManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs index 9f384afbc83a7..47adb7cfe2e7c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs @@ -26,7 +26,7 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender, } int rDepth; - XmlAttribute? local = (XmlAttribute)nsLocallyDeclared[nsPrefix]!; + XmlAttribute local = (XmlAttribute)nsLocallyDeclared[nsPrefix]!; XmlAttribute? rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth); if (local != null) { From 1f75d3c54331483c666602b3e63807997dd2f411 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Tue, 29 Mar 2022 23:38:12 +0100 Subject: [PATCH 18/67] Remove 'red flag' comments --- .../Analyzers/ConvertToLibraryImportFixer.cs | 2 +- .../Cryptography/Pkcs/SubjectIdentifier.cs | 2 +- .../Xml/AncestralNamespaceContextManager.cs | 12 ++---- .../Security/Cryptography/Xml/CanonicalXml.cs | 2 +- .../Security/Cryptography/Xml/DataObject.cs | 1 - .../Cryptography/Xml/EncryptedReference.cs | 1 - .../Security/Cryptography/Xml/EncryptedXml.cs | 6 +-- .../Cryptography/Xml/EncryptionProperty.cs | 1 - .../ExcAncestralNamespaceContextManager.cs | 10 ++--- .../Cryptography/Xml/ExcCanonicalXml.cs | 4 +- .../Xml/KeyInfoRetrievalMethod.cs | 2 +- .../Cryptography/Xml/NamespaceFrame.cs | 4 +- .../Security/Cryptography/Xml/RSAKeyValue.cs | 10 ++--- .../Xml/RSAPKCS1SignatureDescription.cs | 3 +- .../Cryptography/Xml/ReferenceList.cs | 11 +++-- .../Security/Cryptography/Xml/SignedXml.cs | 2 +- .../Security/Cryptography/Xml/Transform.cs | 12 +++--- .../Cryptography/Xml/TransformChain.cs | 4 +- .../System/Security/Cryptography/Xml/Utils.cs | 4 +- .../Xml/XmlDsigExcC14NTransform.cs | 16 ++++---- .../Cryptography/Xml/XmlDsigXPathTransform.cs | 24 +++++------ .../Cryptography/Xml/XmlDsigXsltTransform.cs | 16 ++++---- .../Cryptography/Xml/XmlLicenseTransform.cs | 41 +++++++++---------- .../tests/KeyInfoRetrievalMethodTest.cs | 6 +-- ...tem.Security.Cryptography.Xml.Tests.csproj | 1 + 25 files changed, 94 insertions(+), 103 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs index 9ebbb56a339f2..4fdedd095bed8 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs @@ -74,7 +74,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) context.Diagnostics); if (!bool.Parse(diagnostic.Properties[ConvertToLibraryImportAnalyzer.ExactSpelling])) { - CharSet charSet = (CharSet)Enum.Parse(typeof(CharSet), diagnostic.Properties[ConvertToLibraryImportAnalyzer.CharSet]); + CharSet charSet = (CharSet)Enum.Parse(typeof(CharSet), diagnostic.Properties[ConvertToLibraryImportAnalyzer.CharSet]!); // CharSet.Auto traditionally maps to either an A or W suffix // depending on the default CharSet of the platform. // We will offer both suffix options when CharSet.Auto is provided diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs index d6747d06c3ace..f38af284185f1 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs @@ -79,7 +79,7 @@ internal SubjectIdentifier( Type = SubjectIdentifierType.IssuerAndSerialNumber; var name = new X500DistinguishedName(issuerNameSpan.ToArray()); - Value = new X509IssuerSerial(name.Name, serial.ToBigEndianHex()); + Value = new X509IssuerSerial(name.Name!, serial.ToBigEndianHex()); } } else if (subjectKeyIdentifier != null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs index 04971d778e2f7..05cb49c7a388b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs @@ -26,7 +26,6 @@ internal NamespaceFrame GetCurrentScope() depth = -1; for (int i = _ancestorStack.Count - 1; i >= 0; i--) { - // red flag if ((attr = GetScopeAt(i).GetRendered(nsPrefix)) != null) { depth = i; @@ -42,7 +41,6 @@ internal NamespaceFrame GetCurrentScope() depth = -1; for (int i = _ancestorStack.Count - 1; i >= 0; i--) { - //red flag if ((attr = GetScopeAt(i).GetUnrendered(nsPrefix)) != null) { depth = i; @@ -62,8 +60,8 @@ internal void ExitElementContext() _ancestorStack.RemoveAt(_ancestorStack.Count - 1); } - internal abstract void TrackNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, Hashtable nsLocallyDeclared); - internal abstract void TrackXmlNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared); + internal abstract void TrackNamespaceNode(XmlAttribute attr, SortedList nsListToRender, Hashtable nsLocallyDeclared); + internal abstract void TrackXmlNamespaceNode(XmlAttribute attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared); internal abstract void GetNamespacesToRender(XmlElement element, SortedList attrListToRender, SortedList nsListToRender, Hashtable nsLocallyDeclared); internal void LoadUnrenderedNamespaces(Hashtable nsLocallyDeclared) @@ -84,15 +82,13 @@ internal void LoadRenderedNamespaces(SortedList nsRenderedList) } } - internal void AddRendered(XmlAttribute? attr) + internal void AddRendered(XmlAttribute attr) { - //red flag GetCurrentScope()!.AddRendered(attr); } - internal void AddUnrendered(XmlAttribute? attr) + internal void AddUnrendered(XmlAttribute attr) { - //red flag GetCurrentScope()!.AddUnrendered(attr); } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs index cd9fb2c5b44f9..79ebad121c8e2 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs @@ -121,7 +121,7 @@ internal byte[] GetBytes() { _c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr); hash.TransformFinalBlock(Array.Empty(), 0, 0); - byte[]? res = (byte[])hash.Hash.Clone(); + byte[]? res = (byte[])hash.Hash!.Clone(); // reinitialize the hash so it is still usable after the call hash.Initialize(); return res; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs index 813a6230dc54f..1b21c55c344a2 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs @@ -122,7 +122,6 @@ internal XmlElement GetXml(XmlDocument document) if (!string.IsNullOrEmpty(_encoding)) objectElement.SetAttribute("Encoding", _encoding); - // red flag - not changed, but always false if (_elData != null) { foreach (XmlNode node in _elData) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index c1ce9c443e447..c980fc4f39689 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -74,7 +74,6 @@ protected internal bool CacheValid public virtual XmlElement GetXml() { - // red flag if (CacheValid) return _cachedXml!; XmlDocument document = new XmlDocument(); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index efb8f846a1930..51eab26bb7d62 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -318,8 +318,8 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme if (kiName != null) { // Get the decryption key from the key mapping - string? keyName = kiName.Value; - if ((SymmetricAlgorithm)_keyNameMapping[keyName] != null) + string keyName = kiName.Value!; + if ((SymmetricAlgorithm?)_keyNameMapping[keyName] != null) return (SymmetricAlgorithm)_keyNameMapping[keyName]!; // try to get it from a CarriedKeyName XmlNamespaceManager nsm = new XmlNamespaceManager(_document.NameTable); @@ -408,7 +408,7 @@ public virtual byte[] GetDecryptionIV(EncryptedData encryptedData, string? symme if (kiName != null) { // Get the decryption key from the key mapping - string? keyName = kiName.Value; + string keyName = kiName.Value!; object? kek = _keyNameMapping[keyName]; if (kek != null) { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs index 502f3930c1f37..69957e2f5ab77 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs @@ -65,7 +65,6 @@ private bool CacheValid public XmlElement GetXml() { - // red flag if (CacheValid) return _cachedXml!; XmlDocument document = new XmlDocument(); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs index 6826b4395afec..92b47b42f5c4e 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs @@ -17,7 +17,7 @@ internal ExcAncestralNamespaceContextManager(string inclusiveNamespacesPrefixLis _inclusivePrefixSet = Utils.TokenizePrefixListString(inclusiveNamespacesPrefixList); } - private bool HasNonRedundantInclusivePrefix(XmlAttribute? attr) + private bool HasNonRedundantInclusivePrefix(XmlAttribute attr) { string nsPrefix = Utils.GetNamespacePrefix(attr); return _inclusivePrefixSet.ContainsKey(nsPrefix) && @@ -33,8 +33,8 @@ private void GatherNamespaceToRender(string nsPrefix, SortedList nsListToRender, } int rDepth; - XmlAttribute? local = (XmlAttribute)nsLocallyDeclared[nsPrefix]; - XmlAttribute rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth); + XmlAttribute? local = (XmlAttribute?)nsLocallyDeclared[nsPrefix]; + XmlAttribute? rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth); if (local != null) { @@ -66,7 +66,7 @@ internal override void GetNamespacesToRender(XmlElement element, SortedList attr } } - internal override void TrackNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) + internal override void TrackNamespaceNode(XmlAttribute attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) { if (!Utils.IsXmlPrefixDefinitionNode(attr)) { @@ -77,7 +77,7 @@ internal override void TrackNamespaceNode(XmlAttribute? attr, SortedList nsListT } } - internal override void TrackXmlNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) + internal override void TrackXmlNamespaceNode(XmlAttribute attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) { // exclusive canonicalization treats Xml namespaces as simple attributes. They are not propagated. attrListToRender.Add(attr, null); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs index cefe182f483bc..993c5ed0df425 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs @@ -45,7 +45,7 @@ internal ExcCanonicalXml(XmlNodeList nodeList, bool includeComments, string incl throw new ArgumentNullException(nameof(nodeList)); } - XmlDocument doc = Utils.GetOwnerDocument(nodeList); + XmlDocument? doc = Utils.GetOwnerDocument(nodeList); if (doc == null) throw new ArgumentException(nameof(nodeList)); @@ -69,7 +69,7 @@ internal byte[] GetBytes() { _c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr); hash.TransformFinalBlock(Array.Empty(), 0, 0); - byte[]? res = (byte[])hash.Hash.Clone(); + byte[]? res = (byte[]?)hash.Hash!.Clone(); // reinitialize the hash so it is still usable after the call hash.Initialize(); return res; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs index 9b00310d97cba..9f26e6f18668c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs @@ -37,7 +37,7 @@ public string? Uri set { _uri = value; } } - public string Type + public string? Type { get { return _type; } set { _type = value; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs index edb07610ca439..639f25e15796b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs @@ -16,7 +16,7 @@ internal sealed class NamespaceFrame internal NamespaceFrame() { } - internal void AddRendered(XmlAttribute? attr) + internal void AddRendered(XmlAttribute attr) { _rendered.Add(Utils.GetNamespacePrefix(attr), attr); } @@ -26,7 +26,7 @@ internal void AddRendered(XmlAttribute? attr) return (XmlAttribute?)_rendered[nsPrefix]; } - internal void AddUnrendered(XmlAttribute? attr) + internal void AddUnrendered(XmlAttribute attr) { _unrendered.Add(Utils.GetNamespacePrefix(attr), attr); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAKeyValue.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAKeyValue.cs index 44b524db5f995..60969d497d56d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAKeyValue.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAKeyValue.cs @@ -66,11 +66,11 @@ internal override XmlElement GetXml(XmlDocument xmlDocument) XmlElement rsaKeyValueElement = xmlDocument.CreateElement(RSAKeyValueElementName, SignedXml.XmlDsigNamespaceUrl); XmlElement modulusElement = xmlDocument.CreateElement(ModulusElementName, SignedXml.XmlDsigNamespaceUrl); - modulusElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(rsaParams.Modulus))); + modulusElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(rsaParams.Modulus!))); rsaKeyValueElement.AppendChild(modulusElement); XmlElement exponentElement = xmlDocument.CreateElement(ExponentElementName, SignedXml.XmlDsigNamespaceUrl); - exponentElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(rsaParams.Exponent))); + exponentElement.AppendChild(xmlDocument.CreateTextNode(Convert.ToBase64String(rsaParams.Exponent!))); rsaKeyValueElement.AppendChild(exponentElement); keyValueElement.AppendChild(rsaKeyValueElement); @@ -110,7 +110,7 @@ public override void LoadXml(XmlElement value) XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(value.OwnerDocument.NameTable); xmlNamespaceManager.AddNamespace(xmlDsigNamespacePrefix, SignedXml.XmlDsigNamespaceUrl); - XmlNode rsaKeyValueElement = value.SelectSingleNode($"{xmlDsigNamespacePrefix}:{RSAKeyValueElementName}", xmlNamespaceManager); + XmlNode? rsaKeyValueElement = value.SelectSingleNode($"{xmlDsigNamespacePrefix}:{RSAKeyValueElementName}", xmlNamespaceManager); if (rsaKeyValueElement == null) { throw new CryptographicException(SR.Format(SR.MustContainChildElement, KeyValueElementName, RSAKeyValueElementName)); @@ -120,8 +120,8 @@ public override void LoadXml(XmlElement value) { Key.ImportParameters(new RSAParameters { - Modulus = Convert.FromBase64String(rsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{ModulusElementName}", xmlNamespaceManager).InnerText), - Exponent = Convert.FromBase64String(rsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{ExponentElementName}", xmlNamespaceManager).InnerText) + Modulus = Convert.FromBase64String(rsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{ModulusElementName}", xmlNamespaceManager)!.InnerText), + Exponent = Convert.FromBase64String(rsaKeyValueElement.SelectSingleNode($"{xmlDsigNamespacePrefix}:{ExponentElementName}", xmlNamespaceManager)!.InnerText) }); } catch (Exception ex) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs index 7ede048d08ebc..9c48c3172d45f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SignatureDescription.cs @@ -16,8 +16,7 @@ public RSAPKCS1SignatureDescription(string hashAlgorithmName) public sealed override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key) { var item = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm!)!; - //red flag - item!.SetKey(key); + item.SetKey(key); item.SetHashAlgorithm(DigestAlgorithm!); return item; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs index 01f9e202a2520..a1c76e80507da 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs @@ -24,7 +24,8 @@ public int Count get { return _references.Count; } } - public int Add(object value) +#pragma warning disable CS8995 // Nullable type 'object?' is null-checked and will throw if null. + public int Add(object? value) { if (value is null) { @@ -36,6 +37,7 @@ public int Add(object value) return _references.Add(value); } +#pragma warning restore public void Clear() { @@ -52,7 +54,7 @@ public int IndexOf(object? value) return _references.IndexOf(value); } - public void Insert(int index, object value) + public void Insert(int index, object? value) { if (value is null) { @@ -64,6 +66,7 @@ public void Insert(int index, object value) _references.Insert(index, value); } +#pragma warning restore public void Remove(object? value) { @@ -77,7 +80,7 @@ public void RemoveAt(int index) public EncryptedReference? Item(int index) { - return (EncryptedReference)_references[index]; + return (EncryptedReference?)_references[index]; } [System.Runtime.CompilerServices.IndexerName("ItemOf")] @@ -85,7 +88,7 @@ public EncryptedReference this[int index] { get { - return Item(index); + return Item(index)!; } set { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index 68c7039c7871b..4b47db13117d7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -681,7 +681,7 @@ private bool ReferenceUsesSafeTransformMethods(Reference reference) { Transform transform = transformChain[i]; - if (!IsSafeTransform(transform.Algorithm)) + if (!IsSafeTransform(transform.Algorithm!)) { return false; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs index 7087d16e229a7..698af0c4899cc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs @@ -58,7 +58,7 @@ protected Transform() { } // public properties // - public string Algorithm + public string? Algorithm { get { return _algorithm; } set { _algorithm = value; } @@ -128,7 +128,7 @@ internal XmlElement GetXml(XmlDocument document, string name) XmlElement transformElement = document.CreateElement(name, SignedXml.XmlDsigNamespaceUrl); if (!string.IsNullOrEmpty(Algorithm)) transformElement.SetAttribute("Algorithm", Algorithm); - XmlNodeList children = GetInnerXml(); + XmlNodeList? children = GetInnerXml(); if (children != null) { foreach (XmlNode node in children) @@ -154,15 +154,15 @@ internal XmlElement GetXml(XmlDocument document, string name) return hash.ComputeHash((Stream)GetOutput(typeof(Stream))); } - public XmlElement Context + public XmlElement? Context { get { if (_context != null) return _context; - Reference reference = Reference; - SignedXml signedXml = (reference == null ? SignedXml : reference.SignedXml); + Reference? reference = Reference; + SignedXml? signedXml = (reference == null ? SignedXml : reference.SignedXml); if (signedXml == null) return null; @@ -182,7 +182,7 @@ public Hashtable PropagatedNamespaces return _propagatedNamespaces; Reference? reference = Reference; - SignedXml signedXml = (reference == null ? SignedXml : reference.SignedXml); + SignedXml? signedXml = (reference == null ? SignedXml : reference.SignedXml); // If the reference is not a Uri reference with a DataObject target, return an empty hashtable. if (reference != null && diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index c4178eb521a27..26d9d6ddc670b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -52,7 +52,7 @@ public Transform this[int index] { if (index >= _transforms.Count) throw new ArgumentException(SR.ArgumentOutOfRange_IndexMustBeLess, nameof(index)); - return (Transform)_transforms[index]; + return (Transform)_transforms[index]!; } } @@ -187,14 +187,12 @@ internal void LoadXml(XmlElement value) nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); XmlNodeList? transformNodes = value.SelectNodes("ds:Transform", nsm); - //red flag if (transformNodes!.Count == 0) throw new CryptographicException(SR.Cryptography_Xml_InvalidElement, "Transforms"); _transforms.Clear(); for (int i = 0; i < transformNodes.Count; ++i) { - //red flag XmlElement transformElement = (XmlElement)transformNodes.Item(i)!; string? algorithm = Utils.GetAttribute(transformElement, "Algorithm", SignedXml.XmlDsigNamespaceUrl); Transform? transform = CryptoHelpers.CreateFromName(algorithm); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 4ca63412e33d3..710840d0106fa 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -347,7 +347,7 @@ internal static string GetIdFromLocalUri(string uri, out bool discardComments) internal static string ExtractIdFromLocalUri(string? uri) { - string idref = uri.Substring(1); + string idref = uri!.Substring(1); // Deal with XPointer of type #xpointer(id("ID")). Other XPointer support isn't handled here and is anyway optional if (idref.StartsWith("xpointer(id(", StringComparison.Ordinal)) @@ -396,7 +396,7 @@ internal static long Pump(Stream? input, Stream output) int numBytes; long totalBytes = 0; - while ((numBytes = input.Read(bytes, 0, count)) > 0) + while ((numBytes = input!.Read(bytes, 0, count)) > 0) { output.Write(bytes, 0, numBytes); totalBytes += numBytes; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs index 98071ad5f3dfa..1f78959715252 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs @@ -12,7 +12,7 @@ public class XmlDsigExcC14NTransform : Transform private readonly Type[] _outputTypes = { typeof(Stream) }; private readonly bool _includeComments; private string? _inclusiveNamespacesPrefixList; - private ExcCanonicalXml _excCanonicalXml; + private ExcCanonicalXml? _excCanonicalXml; public XmlDsigExcC14NTransform() : this(false, null) { } @@ -49,7 +49,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) { foreach (XmlNode n in nodeList) { - XmlElement e = n as XmlElement; + XmlElement? e = n as XmlElement; if (e != null) { if (e.LocalName.Equals("InclusiveNamespaces") @@ -77,15 +77,15 @@ public override void LoadInput(object obj) XmlResolver resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); if (obj is Stream) { - _excCanonicalXml = new ExcCanonicalXml((Stream)obj, _includeComments, _inclusiveNamespacesPrefixList, resolver, BaseURI); + _excCanonicalXml = new ExcCanonicalXml((Stream)obj, _includeComments, _inclusiveNamespacesPrefixList!, resolver, BaseURI!); } else if (obj is XmlDocument) { - _excCanonicalXml = new ExcCanonicalXml((XmlDocument)obj, _includeComments, _inclusiveNamespacesPrefixList, resolver); + _excCanonicalXml = new ExcCanonicalXml((XmlDocument)obj, _includeComments, _inclusiveNamespacesPrefixList!, resolver); } else if (obj is XmlNodeList) { - _excCanonicalXml = new ExcCanonicalXml((XmlNodeList)obj, _includeComments, _inclusiveNamespacesPrefixList, resolver); + _excCanonicalXml = new ExcCanonicalXml((XmlNodeList)obj, _includeComments, _inclusiveNamespacesPrefixList!, resolver); } else throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(obj)); @@ -107,19 +107,19 @@ public override void LoadInput(object obj) public override object GetOutput() { - return new MemoryStream(_excCanonicalXml.GetBytes()); + return new MemoryStream(_excCanonicalXml!.GetBytes()); } public override object GetOutput(Type type) { if (type != typeof(Stream) && !type.IsSubclassOf(typeof(Stream))) throw new ArgumentException(SR.Cryptography_Xml_TransformIncorrectInputType, nameof(type)); - return new MemoryStream(_excCanonicalXml.GetBytes()); + return new MemoryStream(_excCanonicalXml!.GetBytes()); } public override byte[]? GetDigestedOutput(HashAlgorithm hash) { - return _excCanonicalXml.GetDigestedBytes(hash); + return _excCanonicalXml!.GetDigestedBytes(hash); } } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs index 2bb0211dbadcd..889f37d1a8a8c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs @@ -13,8 +13,8 @@ public class XmlDsigXPathTransform : Transform { private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlNodeList), typeof(XmlDocument) }; private readonly Type[] _outputTypes = { typeof(XmlNodeList) }; - private string _xpathexpr; - private XmlDocument _document; + private string? _xpathexpr; + private XmlDocument? _document; private XmlNamespaceManager? _nsm; public XmlDsigXPathTransform() @@ -40,18 +40,18 @@ public override void LoadInnerXml(XmlNodeList nodeList) foreach (XmlNode node in nodeList) { - string prefix = null; - string namespaceURI = null; - XmlElement elem = node as XmlElement; + string? prefix = null; + string? namespaceURI = null; + XmlElement? elem = node as XmlElement; if (elem != null) { if (elem.LocalName == "XPath") { _xpathexpr = elem.InnerXml.Trim(null); XmlNodeReader nr = new XmlNodeReader(elem); - XmlNameTable nt = nr.NameTable; + XmlNameTable nt = nr.NameTable!; _nsm = new XmlNamespaceManager(nt); - if (!Utils.VerifyAttributes(elem, (string)null)) + if (!Utils.VerifyAttributes(elem, (string?)null)) { throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform); } @@ -110,7 +110,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) } } // Add the XPath as the inner xml of the element - element.InnerXml = _xpathexpr; + element.InnerXml = _xpathexpr!; document.AppendChild(element); return document.ChildNodes; } @@ -161,15 +161,15 @@ public override object GetOutput() CanonicalXmlNodeList resultNodeList = new CanonicalXmlNodeList(); if (!string.IsNullOrEmpty(_xpathexpr)) { - XPathNavigator navigator = _document.CreateNavigator(); + XPathNavigator navigator = _document!.CreateNavigator()!; XPathNodeIterator it = navigator.Select("//. | //@*"); XPathExpression xpathExpr = navigator.Compile("boolean(" + _xpathexpr + ")"); - xpathExpr.SetContext(_nsm); + xpathExpr.SetContext(_nsm!); while (it.MoveNext()) { - XmlNode node = ((IHasXmlNode)it.Current).GetNode(); + XmlNode node = (((IHasXmlNode)it.Current!)!).GetNode(); bool include = (bool)it.Current.Evaluate(xpathExpr); if (include) @@ -180,7 +180,7 @@ public override object GetOutput() it = navigator.Select("//namespace::*"); while (it.MoveNext()) { - XmlNode node = ((IHasXmlNode)it.Current).GetNode(); + XmlNode node = (((IHasXmlNode)it.Current!)!).GetNode(); resultNodeList.Add(node); } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs index b702110fd1960..c58e07d7a0eae 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs @@ -12,9 +12,9 @@ public class XmlDsigXsltTransform : Transform { private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlDocument), typeof(XmlNodeList) }; private readonly Type[] _outputTypes = { typeof(Stream) }; - private XmlNodeList _xslNodes; - private string _xslFragment; - private Stream _inputStream; + private XmlNodeList? _xslNodes; + private string? _xslFragment; + private Stream? _inputStream; private readonly bool _includeComments; public XmlDsigXsltTransform() @@ -49,7 +49,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) if (nodeList == null) throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform); // check that the XSLT element is well formed - XmlElement firstDataElement = null; + XmlElement? firstDataElement = null; int count = 0; foreach (XmlNode node in nodeList) { @@ -87,7 +87,7 @@ public override void LoadInput(object obj) } else if (obj is XmlNodeList) { - CanonicalXml xmlDoc = new CanonicalXml((XmlNodeList)obj, null, _includeComments); + CanonicalXml xmlDoc = new CanonicalXml((XmlNodeList)obj, null!, _includeComments); byte[] buffer = xmlDoc.GetBytes(); if (buffer == null) return; _inputStream.Write(buffer, 0, buffer.Length); @@ -118,13 +118,13 @@ public override object GetOutput() settings.XmlResolver = null; settings.MaxCharactersFromEntities = Utils.MaxCharactersFromEntities; settings.MaxCharactersInDocument = Utils.MaxCharactersInDocument; - using (StringReader sr = new StringReader(_xslFragment)) + using (StringReader sr = new StringReader(_xslFragment!)) { - XmlReader readerXsl = XmlReader.Create(sr, settings, (string)null); + XmlReader readerXsl = XmlReader.Create(sr, settings, (string)null!); xslt.Load(readerXsl, XsltSettings.Default, null); // Now load the input stream, XmlDocument can be used but is less efficient - XmlReader reader = XmlReader.Create(_inputStream, settings, BaseURI); + XmlReader reader = XmlReader.Create(_inputStream!, settings, BaseURI); XPathDocument inputData = new XPathDocument(reader, XmlSpace.Preserve); // Create an XmlTextWriter diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs index daaeab2e98dcc..b19de95bb0141 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs @@ -10,9 +10,9 @@ public class XmlLicenseTransform : Transform { private readonly Type[] _inputTypes = { typeof(XmlDocument) }; private readonly Type[] _outputTypes = { typeof(XmlDocument) }; - private XmlNamespaceManager _namespaceManager; - private XmlDocument _license; - private IRelDecryptor _relDecryptor; + private XmlNamespaceManager? _namespaceManager; + private XmlDocument? _license; + private IRelDecryptor? _relDecryptor; private const string ElementIssuer = "issuer"; private const string NamespaceUriCore = "urn:mpeg:mpeg21:2003:01-REL-R-NS"; @@ -31,7 +31,7 @@ public override Type[] OutputTypes get { return _outputTypes; } } - public IRelDecryptor Decryptor + public IRelDecryptor? Decryptor { get { return _relDecryptor; } set { _relDecryptor = value; } @@ -48,10 +48,9 @@ private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecrypto for (int i = 0, count = encryptedGrantList.Count; i < count; i++) { - //red flag - encryptionMethod = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/enc:EncryptionMethod", _namespaceManager) as XmlElement; - keyInfo = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/dsig:KeyInfo", _namespaceManager) as XmlElement; - cipherData = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/enc:CipherData", _namespaceManager) as XmlElement; + encryptionMethod = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/enc:EncryptionMethod", _namespaceManager!) as XmlElement; + keyInfo = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/dsig:KeyInfo", _namespaceManager!) as XmlElement; + cipherData = encryptedGrantList[i]!.SelectSingleNode("//r:encryptedGrant/enc:CipherData", _namespaceManager!) as XmlElement; if ((encryptionMethod != null) && (keyInfo != null) && (cipherData != null)) @@ -70,8 +69,8 @@ private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecrypto try { - toDecrypt = new MemoryStream(cipherDataObj.CipherValue); - decryptedContent = _relDecryptor.Decrypt(encryptionMethodObj, + toDecrypt = new MemoryStream(cipherDataObj.CipherValue!); + decryptedContent = _relDecryptor!.Decrypt(encryptionMethodObj, keyInfoObj, toDecrypt); if ((decryptedContent == null) || (decryptedContent.Length == 0)) @@ -81,7 +80,7 @@ private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecrypto string clearContent = streamReader.ReadToEnd(); // red flag - encryptedGrantList[i]!.ParentNode.InnerXml = clearContent; + encryptedGrantList[i]!.ParentNode!.InnerXml = clearContent; } finally { @@ -101,7 +100,7 @@ private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecrypto public override object GetOutput() { - return _license; + return _license!; } public override object GetOutput(Type type) @@ -132,9 +131,9 @@ public override void LoadInput(object obj) _namespaceManager.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl); _namespaceManager.AddNamespace("r", NamespaceUriCore); - XmlElement currentIssuerContext; - XmlElement currentLicenseContext; - XmlNode signatureNode; + XmlElement? currentIssuerContext; + XmlElement? currentLicenseContext; + XmlNode? signatureNode; // Get the nearest issuer node currentIssuerContext = Context.SelectSingleNode("ancestor-or-self::r:issuer[1]", _namespaceManager) as XmlElement; @@ -142,29 +141,27 @@ public override void LoadInput(object obj) throw new CryptographicException(SR.Cryptography_Xml_XrmlMissingIssuer); signatureNode = currentIssuerContext.SelectSingleNode("descendant-or-self::dsig:Signature[1]", _namespaceManager) as XmlElement; - signatureNode?.ParentNode.RemoveChild(signatureNode); + signatureNode?.ParentNode!.RemoveChild(signatureNode); // Get the nearest license node currentLicenseContext = currentIssuerContext.SelectSingleNode("ancestor-or-self::r:license[1]", _namespaceManager) as XmlElement; if (currentLicenseContext == null) throw new CryptographicException(SR.Cryptography_Xml_XrmlMissingLicence); - //red flag XmlNodeList issuerList = currentLicenseContext.SelectNodes("descendant-or-self::r:license[1]/r:issuer", _namespaceManager)!; // Remove all issuer nodes except current for (int i = 0, count = issuerList.Count; i < count; i++) { - //red flag if (issuerList[i]! == currentIssuerContext) continue; - if ((issuerList[i].LocalName == ElementIssuer) && - (issuerList[i].NamespaceURI == NamespaceUriCore)) - issuerList[i].ParentNode.RemoveChild(issuerList[i]); + if ((issuerList[i]!.LocalName == ElementIssuer) && + (issuerList[i]!.NamespaceURI == NamespaceUriCore)) + issuerList[i]!.ParentNode!.RemoveChild(issuerList[i]!); } - XmlNodeList encryptedGrantList = currentLicenseContext.SelectNodes("/r:license/r:grant/r:encryptedGrant", _namespaceManager); + XmlNodeList encryptedGrantList = currentLicenseContext.SelectNodes("/r:license/r:grant/r:encryptedGrant", _namespaceManager)!; if (encryptedGrantList.Count > 0) { diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs index 39d5e65b63a44..e388317817ccc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs @@ -48,7 +48,7 @@ public void TestImportKeyNode() doc.LoadXml(value); KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod(); - uri1.LoadXml(doc.DocumentElement); + uri1.LoadXml(doc.DocumentElement!); // verify that proper XML is generated (equals to original) string s = (uri1.GetXml().OuterXml); @@ -62,7 +62,7 @@ public void TestImportKeyNode() public void InvalidKeyNode1() { KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod(); - Assert.Throws(() => uri1.LoadXml(null)); + Assert.Throws(() => uri1.LoadXml(null!)); } [Fact] @@ -74,7 +74,7 @@ public void InvalidKeyNode2() KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod(); // no exception is thrown - uri1.LoadXml(doc.DocumentElement); + uri1.LoadXml(doc.DocumentElement!); AssertCrypto.AssertXmlEquals("invalid", "", (uri1.GetXml().OuterXml)); } } diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj b/src/libraries/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj index 1d31bc399da7e..57c47b0787b65 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj +++ b/src/libraries/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent);$(NetFrameworkMinimum) + annotations Date: Wed, 30 Mar 2022 06:48:50 +0100 Subject: [PATCH 19/67] Revert unintended changes to tests --- .../tests/KeyInfoRetrievalMethodTest.cs | 8 ++++---- .../tests/ReferenceTest.cs | 4 ++-- .../tests/SignedXmlTest.cs | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs index e388317817ccc..a16525ecb4077 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/KeyInfoRetrievalMethodTest.cs @@ -28,7 +28,7 @@ public void TestNewEmptyKeyNode() [Fact] public void TestNewKeyNode() { - string? uri = "http://www.go-mono.com/"; + string uri = "http://www.go-mono.com/"; KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod(); uri1.Uri = uri; XmlElement xel = uri1.GetXml(); @@ -48,7 +48,7 @@ public void TestImportKeyNode() doc.LoadXml(value); KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod(); - uri1.LoadXml(doc.DocumentElement!); + uri1.LoadXml(doc.DocumentElement); // verify that proper XML is generated (equals to original) string s = (uri1.GetXml().OuterXml); @@ -62,7 +62,7 @@ public void TestImportKeyNode() public void InvalidKeyNode1() { KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod(); - Assert.Throws(() => uri1.LoadXml(null!)); + Assert.Throws(() => uri1.LoadXml(null)); } [Fact] @@ -74,7 +74,7 @@ public void InvalidKeyNode2() KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod(); // no exception is thrown - uri1.LoadXml(doc.DocumentElement!); + uri1.LoadXml(doc.DocumentElement); AssertCrypto.AssertXmlEquals("invalid", "", (uri1.GetXml().OuterXml)); } } diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/ReferenceTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/ReferenceTest.cs index 06d08b1d13fe5..d84b2fb9c7aec 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/ReferenceTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/ReferenceTest.cs @@ -39,7 +39,7 @@ public void Ctor() [InlineData(" ")] [InlineData("uri")] [InlineData("http://mysite.com/")] - public void Ctor_Uri(string? uri) + public void Ctor_Uri(string uri) { Reference reference = new Reference(uri); @@ -189,7 +189,7 @@ public void AddAllTransforms() { Reference reference = new Reference(); // adding an empty hash value - byte[]? hash = new byte[20]; + byte[] hash = new byte[20]; reference.DigestMethod = SignedXml.XmlDsigSHA1Url; reference.DigestValue = hash; XmlElement xel = reference.GetXml(); diff --git a/src/libraries/System.Security.Cryptography.Xml/tests/SignedXmlTest.cs b/src/libraries/System.Security.Cryptography.Xml/tests/SignedXmlTest.cs index aafbc7310837d..8841cf2f821f0 100644 --- a/src/libraries/System.Security.Cryptography.Xml/tests/SignedXmlTest.cs +++ b/src/libraries/System.Security.Cryptography.Xml/tests/SignedXmlTest.cs @@ -657,7 +657,7 @@ public void DigestValue_CRLF() s.Position = 0; - byte[]? digest; + byte[] digest; using (HashAlgorithm hash = SHA1.Create()) { digest = hash.ComputeHash(s); @@ -717,7 +717,7 @@ public void DigestValue_LF() s.Position = 0; - byte[]? digest; + byte[] digest; using (HashAlgorithm hash = SHA1.Create()) { digest = hash.ComputeHash(s); From fd784fbfa218920c6732aa1e9360d8c00cde17a0 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Wed, 30 Mar 2022 06:52:50 +0100 Subject: [PATCH 20/67] Revert unintended changes --- .../src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs index f38af284185f1..d6747d06c3ace 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SubjectIdentifier.cs @@ -79,7 +79,7 @@ internal SubjectIdentifier( Type = SubjectIdentifierType.IssuerAndSerialNumber; var name = new X500DistinguishedName(issuerNameSpan.ToArray()); - Value = new X509IssuerSerial(name.Name!, serial.ToBigEndianHex()); + Value = new X509IssuerSerial(name.Name, serial.ToBigEndianHex()); } } else if (subjectKeyIdentifier != null) From 3419961383db1b01c62bb8ca4a40ff44e78ead5f Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Thu, 31 Mar 2022 20:36:49 +0100 Subject: [PATCH 21/67] PR feedback: remove redundant !'s from things that can never return null --- .../Cryptography/Xml/AncestralNamespaceContextManager.cs | 4 ++-- .../System/Security/Cryptography/Xml/CanonicalXmlElement.cs | 2 +- .../src/System/Security/Cryptography/Xml/SignedXml.cs | 2 +- .../Security/Cryptography/Xml/XmlDecryptionTransform.cs | 2 +- .../Security/Cryptography/Xml/XmlDsigXPathTransform.cs | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs index 05cb49c7a388b..54341e375e07e 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs @@ -84,12 +84,12 @@ internal void LoadRenderedNamespaces(SortedList nsRenderedList) internal void AddRendered(XmlAttribute attr) { - GetCurrentScope()!.AddRendered(attr); + GetCurrentScope().AddRendered(attr); } internal void AddUnrendered(XmlAttribute attr) { - GetCurrentScope()!.AddUnrendered(attr); + GetCurrentScope().AddUnrendered(attr); } } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index 8cb21d2f2f2d4..a80e0a12948f7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -56,7 +56,7 @@ public void Write(StringBuilder strBuilder, DocPosition docPos, AncestralNamespa if (!Utils.IsCommittedNamespace(this, Prefix, NamespaceURI)) { string name = ((Prefix.Length > 0) ? "xmlns" + ":" + Prefix : "xmlns"); - XmlAttribute nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name)!; + XmlAttribute nsattrib = (XmlAttribute)OwnerDocument.CreateAttribute(name); nsattrib.Value = NamespaceURI; anc.TrackNamespaceNode(nsattrib, nsListToRender, nsLocallyDeclared); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index 4b47db13117d7..c2d46aa71adf8 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -819,7 +819,7 @@ private int GetReferenceLevel(int index, ArrayList references) // If this is pointing to another reference for (int j = 0; j < references.Count; ++j) { - if ((((Reference)references[j]!)!).Id == idref) + if (((Reference)references[j]!).Id == idref) { _refLevelCache![index] = GetReferenceLevel(j, references) + 1; return (_refLevelCache[index]); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs index 5f1cc9e26f169..e12455290d362 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDecryptionTransform.cs @@ -194,7 +194,7 @@ private bool ProcessEncryptedDataItem(XmlElement encryptedDataElement) { for (int index = 0; index < ExceptUris.Count; index++) { - if (IsTargetElement(encryptedDataElement, ((string)ExceptUris[index]!)!)) + if (IsTargetElement(encryptedDataElement, (string)ExceptUris[index]!)) return false; } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs index 889f37d1a8a8c..05c0f4b8146c3 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs @@ -49,7 +49,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) { _xpathexpr = elem.InnerXml.Trim(null); XmlNodeReader nr = new XmlNodeReader(elem); - XmlNameTable nt = nr.NameTable!; + XmlNameTable nt = nr.NameTable; _nsm = new XmlNamespaceManager(nt); if (!Utils.VerifyAttributes(elem, (string?)null)) { @@ -169,7 +169,7 @@ public override object GetOutput() while (it.MoveNext()) { - XmlNode node = (((IHasXmlNode)it.Current!)!).GetNode(); + XmlNode node = ((IHasXmlNode)it.Current!).GetNode(); bool include = (bool)it.Current.Evaluate(xpathExpr); if (include) @@ -180,7 +180,7 @@ public override object GetOutput() it = navigator.Select("//namespace::*"); while (it.MoveNext()) { - XmlNode node = (((IHasXmlNode)it.Current!)!).GetNode(); + XmlNode node = ((IHasXmlNode)it.Current!).GetNode(); resultNodeList.Add(node); } } From d3180bb85dfd2fe0006d9a96dd44e78ce28213f1 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Thu, 31 Mar 2022 21:03:54 +0100 Subject: [PATCH 22/67] Added `[MemberNotNullWhen(true, nameof(_cachedXml))]` on implementation. --- .../Xml/C14NAncestralNamespaceContextManager.cs | 4 ++-- .../Security/Cryptography/Xml/CanonicalXmlNodeList.cs | 2 +- .../src/System/Security/Cryptography/Xml/CipherData.cs | 4 +++- .../src/System/Security/Cryptography/Xml/DataObject.cs | 6 ++++-- .../src/System/Security/Cryptography/Xml/EncryptedData.cs | 2 +- .../src/System/Security/Cryptography/Xml/EncryptedKey.cs | 2 +- .../System/Security/Cryptography/Xml/EncryptedReference.cs | 4 +++- .../src/System/Security/Cryptography/Xml/EncryptedType.cs | 2 ++ .../System/Security/Cryptography/Xml/EncryptionMethod.cs | 4 +++- .../System/Security/Cryptography/Xml/EncryptionProperty.cs | 3 ++- .../src/System/Security/Cryptography/Xml/Reference.cs | 4 +++- .../src/System/Security/Cryptography/Xml/SignedInfo.cs | 4 +++- 12 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs index 47adb7cfe2e7c..7ff5663dcf77b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs @@ -84,9 +84,9 @@ internal override void GetNamespacesToRender(XmlElement element, SortedList attr } } - internal override void TrackNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) + internal override void TrackNamespaceNode(XmlAttribute attr, SortedList nsListToRender, Hashtable nsLocallyDeclared) { - nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr!), attr); + nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr), attr); } internal override void TrackXmlNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs index 1acf90c24282e..03db7132bf317 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlNodeList.cs @@ -15,7 +15,7 @@ internal CanonicalXmlNodeList() _nodeArray = new ArrayList(); } - public override XmlNode? Item(int index) + public override XmlNode Item(int index) { return (XmlNode)_nodeArray[index]!; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs index 4d4a1715704f7..00705eb664f08 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml @@ -23,6 +24,7 @@ public CipherData(CipherReference cipherReference) CipherReference = cipherReference; } + [MemberNotNullWhen(true, nameof(_cachedXml))] private bool CacheValid { get @@ -63,7 +65,7 @@ public byte[]? CipherValue public XmlElement GetXml() { - if (CacheValid) return _cachedXml!; + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs index 1b21c55c344a2..3242122610acd 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml @@ -90,6 +91,7 @@ public XmlNodeList Data } } + [MemberNotNullWhen(true, nameof(_cachedXml))] private bool CacheValid { get @@ -102,9 +104,9 @@ private bool CacheValid // public methods // - public XmlElement? GetXml() + public XmlElement GetXml() { - if (CacheValid) return (_cachedXml); + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs index 594671992f9f4..016018883c434 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs @@ -67,7 +67,7 @@ public override void LoadXml(XmlElement value) public override XmlElement GetXml() { - if (CacheValid) return (_cachedXml!); + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs index 1e9e3ecb83593..e0cac61aa0ec2 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs @@ -141,7 +141,7 @@ public override void LoadXml(XmlElement value) public override XmlElement GetXml() { - if (CacheValid) return _cachedXml!; + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index c980fc4f39689..a1515e64960a5 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml @@ -64,6 +65,7 @@ protected string? ReferenceType } } + [MemberNotNullWhen(true, nameof(_cachedXml))] protected internal bool CacheValid { get @@ -74,7 +76,7 @@ protected internal bool CacheValid public virtual XmlElement GetXml() { - if (CacheValid) return _cachedXml!; + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs index 899f1324f0752..bd393897519c2 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml @@ -17,6 +18,7 @@ public abstract class EncryptedType private KeyInfo? _keyInfo; internal XmlElement? _cachedXml; + [MemberNotNullWhen(true, nameof(_cachedXml))] internal bool CacheValid { get diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs index adb6ab4ae894f..d50aa84b1dc2a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml @@ -22,6 +23,7 @@ public EncryptionMethod(string? algorithm) _cachedXml = null; } + [MemberNotNullWhen(true, nameof(_cachedXml))] private bool CacheValid { get @@ -54,7 +56,7 @@ public string? KeyAlgorithm public XmlElement GetXml() { - if (CacheValid) return (_cachedXml!); + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs index 69957e2f5ab77..a1c67daac4e5c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs @@ -55,6 +55,7 @@ public XmlElement? PropertyElement } } + [MemberNotNullWhen(true, nameof(_cachedXml))] private bool CacheValid { get @@ -65,7 +66,7 @@ private bool CacheValid public XmlElement GetXml() { - if (CacheValid) return _cachedXml!; + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index 5b844d5010f57..fd8e82799563f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -4,6 +4,7 @@ using System.IO; using System.Net; using System.Xml; +using System.Diagnostics.CodeAnalysis; namespace System.Security.Cryptography.Xml { @@ -125,6 +126,7 @@ public TransformChain TransformChain } } + [MemberNotNullWhen(true, nameof(_cachedXml))] internal bool CacheValid { get @@ -153,7 +155,7 @@ internal ReferenceTargetType ReferenceTargetType public XmlElement GetXml() { - if (CacheValid) return (_cachedXml!); + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs index 49ababd3b7353..450740711496d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections; +using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml @@ -129,6 +130,7 @@ public ArrayList References get { return _references; } } + [MemberNotNullWhen(true, nameof(_cachedXml))] internal bool CacheValid { get @@ -149,7 +151,7 @@ internal bool CacheValid public XmlElement GetXml() { - if (CacheValid) return _cachedXml!; + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; From 29f13a68fa25b8c7b0e2c256ebfafb7e07d86408 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Thu, 31 Mar 2022 21:08:59 +0100 Subject: [PATCH 23/67] Updated ref t ypes (with `dotnet msbuild /t:GenerateReferenceAssemblySource`) --- .../ref/System.Security.Cryptography.Xml.cs | 210 +++++++++--------- 1 file changed, 109 insertions(+), 101 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 2ee83409bebac..0f887cfcb018f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -11,43 +11,50 @@ public sealed partial class CipherData public CipherData() { } public CipherData(byte[] cipherValue) { } public CipherData(System.Security.Cryptography.Xml.CipherReference cipherReference) { } - public System.Security.Cryptography.Xml.CipherReference CipherReference { get { throw null; } set { } } - public byte[] CipherValue { get { throw null; } set { } } + public System.Security.Cryptography.Xml.CipherReference? CipherReference { get { throw null; } set { } } + public byte[]? CipherValue { get { throw null; } set { } } public System.Xml.XmlElement GetXml() { throw null; } public void LoadXml(System.Xml.XmlElement value) { } } public sealed partial class CipherReference : System.Security.Cryptography.Xml.EncryptedReference { public CipherReference() { } - public CipherReference(string uri) { } - public CipherReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + public CipherReference(string? uri) { } + public CipherReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } + public partial class CryptoSignedXmlRecursionException : System.Xml.XmlException + { + public CryptoSignedXmlRecursionException() { } + protected CryptoSignedXmlRecursionException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } + public CryptoSignedXmlRecursionException(string message) { } + public CryptoSignedXmlRecursionException(string message, System.Exception inner) { } + } public partial class DataObject { public DataObject() { } public DataObject(string id, string mimeType, string encoding, System.Xml.XmlElement data) { } public System.Xml.XmlNodeList Data { get { throw null; } set { } } - public string Encoding { get { throw null; } set { } } - public string Id { get { throw null; } set { } } - public string MimeType { get { throw null; } set { } } + public string? Encoding { get { throw null; } set { } } + public string? Id { get { throw null; } set { } } + public string? MimeType { get { throw null; } set { } } public System.Xml.XmlElement GetXml() { throw null; } public void LoadXml(System.Xml.XmlElement value) { } } public sealed partial class DataReference : System.Security.Cryptography.Xml.EncryptedReference { public DataReference() { } - public DataReference(string uri) { } - public DataReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + public DataReference(string? uri) { } + public DataReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } } public partial class DSAKeyValue : System.Security.Cryptography.Xml.KeyInfoClause { [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] public DSAKeyValue() { } - public DSAKeyValue(System.Security.Cryptography.DSA key) { } - public System.Security.Cryptography.DSA Key { get { throw null; } set { } } + public DSAKeyValue(System.Security.Cryptography.DSA? key) { } + public System.Security.Cryptography.DSA? Key { get { throw null; } set { } } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } @@ -60,8 +67,8 @@ public override void LoadXml(System.Xml.XmlElement value) { } public sealed partial class EncryptedKey : System.Security.Cryptography.Xml.EncryptedType { public EncryptedKey() { } - public string CarriedKeyName { get { throw null; } set { } } - public string Recipient { get { throw null; } set { } } + public string? CarriedKeyName { get { throw null; } set { } } + public string? Recipient { get { throw null; } set { } } public System.Security.Cryptography.Xml.ReferenceList ReferenceList { get { throw null; } } public void AddReference(System.Security.Cryptography.Xml.DataReference dataReference) { } public void AddReference(System.Security.Cryptography.Xml.KeyReference keyReference) { } @@ -71,12 +78,13 @@ public override void LoadXml(System.Xml.XmlElement value) { } public abstract partial class EncryptedReference { protected EncryptedReference() { } - protected EncryptedReference(string uri) { } - protected EncryptedReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } - protected internal bool CacheValid { get { throw null; } } - protected string ReferenceType { get { throw null; } set { } } + protected EncryptedReference(string? uri) { } + protected EncryptedReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + [System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute(true, "_cachedXml")] + protected internal bool CacheValid { [System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute(true, "_cachedXml")] get { throw null; } } + protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } - public string Uri { get { throw null; } set { } } + public string? Uri { get { throw null; } set { } } public void AddTransform(System.Security.Cryptography.Xml.Transform transform) { } public virtual System.Xml.XmlElement GetXml() { throw null; } public virtual void LoadXml(System.Xml.XmlElement value) { } @@ -85,13 +93,13 @@ public abstract partial class EncryptedType { protected EncryptedType() { } public virtual System.Security.Cryptography.Xml.CipherData CipherData { get { throw null; } set { } } - public virtual string Encoding { get { throw null; } set { } } - public virtual System.Security.Cryptography.Xml.EncryptionMethod EncryptionMethod { get { throw null; } set { } } + public virtual string? Encoding { get { throw null; } set { } } + public virtual System.Security.Cryptography.Xml.EncryptionMethod? EncryptionMethod { get { throw null; } set { } } public virtual System.Security.Cryptography.Xml.EncryptionPropertyCollection EncryptionProperties { get { throw null; } } - public virtual string Id { get { throw null; } set { } } + public virtual string? Id { get { throw null; } set { } } public System.Security.Cryptography.Xml.KeyInfo KeyInfo { get { throw null; } set { } } - public virtual string MimeType { get { throw null; } set { } } - public virtual string Type { get { throw null; } set { } } + public virtual string? MimeType { get { throw null; } set { } } + public virtual string? Type { get { throw null; } set { } } public void AddProperty(System.Security.Cryptography.Xml.EncryptionProperty ep) { } public abstract System.Xml.XmlElement GetXml(); public abstract void LoadXml(System.Xml.XmlElement value); @@ -117,19 +125,19 @@ public partial class EncryptedXml public const string XmlEncTripleDESUrl = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc"; public EncryptedXml() { } public EncryptedXml(System.Xml.XmlDocument document) { } - public EncryptedXml(System.Xml.XmlDocument document, System.Security.Policy.Evidence evidence) { } - public System.Security.Policy.Evidence DocumentEvidence { get { throw null; } set { } } + public EncryptedXml(System.Xml.XmlDocument document, System.Security.Policy.Evidence? evidence) { } + public System.Security.Policy.Evidence? DocumentEvidence { get { throw null; } set { } } public System.Text.Encoding Encoding { get { throw null; } set { } } public System.Security.Cryptography.CipherMode Mode { get { throw null; } set { } } public System.Security.Cryptography.PaddingMode Padding { get { throw null; } set { } } public string Recipient { get { throw null; } set { } } - public System.Xml.XmlResolver Resolver { get { throw null; } set { } } + public System.Xml.XmlResolver? Resolver { get { throw null; } set { } } public int XmlDSigSearchDepth { get { throw null; } set { } } public void AddKeyNameMapping(string keyName, object keyObject) { } public void ClearKeyNameMappings() { } public byte[] DecryptData(System.Security.Cryptography.Xml.EncryptedData encryptedData, System.Security.Cryptography.SymmetricAlgorithm symmetricAlgorithm) { throw null; } public void DecryptDocument() { } - public virtual byte[] DecryptEncryptedKey(System.Security.Cryptography.Xml.EncryptedKey encryptedKey) { throw null; } + public virtual byte[]? DecryptEncryptedKey(System.Security.Cryptography.Xml.EncryptedKey encryptedKey) { throw null; } public static byte[] DecryptKey(byte[] keyData, System.Security.Cryptography.RSA rsa, bool useOAEP) { throw null; } public static byte[] DecryptKey(byte[] keyData, System.Security.Cryptography.SymmetricAlgorithm symmetricAlgorithm) { throw null; } public System.Security.Cryptography.Xml.EncryptedData Encrypt(System.Xml.XmlElement inputElement, System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) { throw null; } @@ -138,17 +146,17 @@ public void DecryptDocument() { } public byte[] EncryptData(System.Xml.XmlElement inputElement, System.Security.Cryptography.SymmetricAlgorithm symmetricAlgorithm, bool content) { throw null; } public static byte[] EncryptKey(byte[] keyData, System.Security.Cryptography.RSA rsa, bool useOAEP) { throw null; } public static byte[] EncryptKey(byte[] keyData, System.Security.Cryptography.SymmetricAlgorithm symmetricAlgorithm) { throw null; } - public virtual byte[] GetDecryptionIV(System.Security.Cryptography.Xml.EncryptedData encryptedData, string symmetricAlgorithmUri) { throw null; } - public virtual System.Security.Cryptography.SymmetricAlgorithm GetDecryptionKey(System.Security.Cryptography.Xml.EncryptedData encryptedData, string symmetricAlgorithmUri) { throw null; } - public virtual System.Xml.XmlElement GetIdElement(System.Xml.XmlDocument document, string idValue) { throw null; } + public virtual byte[] GetDecryptionIV(System.Security.Cryptography.Xml.EncryptedData encryptedData, string? symmetricAlgorithmUri) { throw null; } + public virtual System.Security.Cryptography.SymmetricAlgorithm? GetDecryptionKey(System.Security.Cryptography.Xml.EncryptedData encryptedData, string? symmetricAlgorithmUri) { throw null; } + public virtual System.Xml.XmlElement? GetIdElement(System.Xml.XmlDocument document, string idValue) { throw null; } public void ReplaceData(System.Xml.XmlElement inputElement, byte[] decryptedData) { } public static void ReplaceElement(System.Xml.XmlElement inputElement, System.Security.Cryptography.Xml.EncryptedData encryptedData, bool content) { } } public partial class EncryptionMethod { public EncryptionMethod() { } - public EncryptionMethod(string algorithm) { } - public string KeyAlgorithm { get { throw null; } set { } } + public EncryptionMethod(string? algorithm) { } + public string? KeyAlgorithm { get { throw null; } set { } } public int KeySize { get { throw null; } set { } } public System.Xml.XmlElement GetXml() { throw null; } public void LoadXml(System.Xml.XmlElement value) { } @@ -157,9 +165,9 @@ public sealed partial class EncryptionProperty { public EncryptionProperty() { } public EncryptionProperty(System.Xml.XmlElement elementProperty) { } - public string Id { get { throw null; } } - public System.Xml.XmlElement PropertyElement { get { throw null; } set { } } - public string Target { get { throw null; } } + public string? Id { get { throw null; } } + public System.Xml.XmlElement? PropertyElement { get { throw null; } set { } } + public string? Target { get { throw null; } } public System.Xml.XmlElement GetXml() { throw null; } public void LoadXml(System.Xml.XmlElement value) { } } @@ -173,7 +181,7 @@ public EncryptionPropertyCollection() { } [System.Runtime.CompilerServices.IndexerName("ItemOf")] public System.Security.Cryptography.Xml.EncryptionProperty this[int index] { get { throw null; } set { } } public object SyncRoot { get { throw null; } } - object System.Collections.IList.this[int index] { get { throw null; } set { } } + object? System.Collections.IList.this[int index] { get { throw null; } set { } } public int Add(System.Security.Cryptography.Xml.EncryptionProperty value) { throw null; } public void Clear() { } public bool Contains(System.Security.Cryptography.Xml.EncryptionProperty value) { throw null; } @@ -199,7 +207,7 @@ public partial class KeyInfo : System.Collections.IEnumerable { public KeyInfo() { } public int Count { get { throw null; } } - public string Id { get { throw null; } set { } } + public string? Id { get { throw null; } set { } } public void AddClause(System.Security.Cryptography.Xml.KeyInfoClause clause) { } public System.Collections.IEnumerator GetEnumerator() { throw null; } public System.Collections.IEnumerator GetEnumerator(System.Type requestedObjectType) { throw null; } @@ -216,15 +224,15 @@ public partial class KeyInfoEncryptedKey : System.Security.Cryptography.Xml.KeyI { public KeyInfoEncryptedKey() { } public KeyInfoEncryptedKey(System.Security.Cryptography.Xml.EncryptedKey encryptedKey) { } - public System.Security.Cryptography.Xml.EncryptedKey EncryptedKey { get { throw null; } set { } } + public System.Security.Cryptography.Xml.EncryptedKey? EncryptedKey { get { throw null; } set { } } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } public partial class KeyInfoName : System.Security.Cryptography.Xml.KeyInfoClause { public KeyInfoName() { } - public KeyInfoName(string keyName) { } - public string Value { get { throw null; } set { } } + public KeyInfoName(string? keyName) { } + public string? Value { get { throw null; } set { } } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } @@ -232,17 +240,17 @@ public partial class KeyInfoNode : System.Security.Cryptography.Xml.KeyInfoClaus { public KeyInfoNode() { } public KeyInfoNode(System.Xml.XmlElement node) { } - public System.Xml.XmlElement Value { get { throw null; } set { } } + public System.Xml.XmlElement? Value { get { throw null; } set { } } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } public partial class KeyInfoRetrievalMethod : System.Security.Cryptography.Xml.KeyInfoClause { public KeyInfoRetrievalMethod() { } - public KeyInfoRetrievalMethod(string strUri) { } + public KeyInfoRetrievalMethod(string? strUri) { } public KeyInfoRetrievalMethod(string strUri, string typeName) { } - public string Type { get { throw null; } set { } } - public string Uri { get { throw null; } set { } } + public string? Type { get { throw null; } set { } } + public string? Uri { get { throw null; } set { } } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } @@ -252,11 +260,11 @@ public KeyInfoX509Data() { } public KeyInfoX509Data(byte[] rgbCert) { } public KeyInfoX509Data(System.Security.Cryptography.X509Certificates.X509Certificate cert) { } public KeyInfoX509Data(System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509IncludeOption includeOption) { } - public System.Collections.ArrayList Certificates { get { throw null; } } - public byte[] CRL { get { throw null; } set { } } - public System.Collections.ArrayList IssuerSerials { get { throw null; } } - public System.Collections.ArrayList SubjectKeyIds { get { throw null; } } - public System.Collections.ArrayList SubjectNames { get { throw null; } } + public System.Collections.ArrayList? Certificates { get { throw null; } } + public byte[]? CRL { get { throw null; } set { } } + public System.Collections.ArrayList? IssuerSerials { get { throw null; } } + public System.Collections.ArrayList? SubjectKeyIds { get { throw null; } } + public System.Collections.ArrayList? SubjectNames { get { throw null; } } public void AddCertificate(System.Security.Cryptography.X509Certificates.X509Certificate certificate) { } public void AddIssuerSerial(string issuerName, string serialNumber) { } public void AddSubjectKeyId(byte[] subjectKeyId) { } @@ -268,20 +276,20 @@ public override void LoadXml(System.Xml.XmlElement element) { } public sealed partial class KeyReference : System.Security.Cryptography.Xml.EncryptedReference { public KeyReference() { } - public KeyReference(string uri) { } - public KeyReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + public KeyReference(string? uri) { } + public KeyReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } } public partial class Reference { public Reference() { } public Reference(System.IO.Stream stream) { } - public Reference(string uri) { } + public Reference(string? uri) { } public string DigestMethod { get { throw null; } set { } } - public byte[] DigestValue { get { throw null; } set { } } - public string Id { get { throw null; } set { } } + public byte[]? DigestValue { get { throw null; } set { } } + public string? Id { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } - public string Type { get { throw null; } set { } } - public string Uri { get { throw null; } set { } } + public string? Type { get { throw null; } set { } } + public string? Uri { get { throw null; } set { } } public void AddTransform(System.Security.Cryptography.Xml.Transform transform) { } public System.Xml.XmlElement GetXml() { throw null; } public void LoadXml(System.Xml.XmlElement value) { } @@ -296,16 +304,16 @@ public ReferenceList() { } public object SyncRoot { get { throw null; } } bool System.Collections.IList.IsFixedSize { get { throw null; } } bool System.Collections.IList.IsReadOnly { get { throw null; } } - object System.Collections.IList.this[int index] { get { throw null; } set { } } - public int Add(object value) { throw null; } + object? System.Collections.IList.this[int index] { get { throw null; } set { } } + public int Add(object? value) { throw null; } public void Clear() { } - public bool Contains(object value) { throw null; } + public bool Contains(object? value) { throw null; } public void CopyTo(System.Array array, int index) { } public System.Collections.IEnumerator GetEnumerator() { throw null; } - public int IndexOf(object value) { throw null; } - public void Insert(int index, object value) { } - public System.Security.Cryptography.Xml.EncryptedReference Item(int index) { throw null; } - public void Remove(object value) { } + public int IndexOf(object? value) { throw null; } + public void Insert(int index, object? value) { } + public System.Security.Cryptography.Xml.EncryptedReference? Item(int index) { throw null; } + public void Remove(object? value) { } public void RemoveAt(int index) { } } public partial class RSAKeyValue : System.Security.Cryptography.Xml.KeyInfoClause @@ -319,11 +327,11 @@ public override void LoadXml(System.Xml.XmlElement value) { } public partial class Signature { public Signature() { } - public string Id { get { throw null; } set { } } + public string? Id { get { throw null; } set { } } public System.Security.Cryptography.Xml.KeyInfo KeyInfo { get { throw null; } set { } } public System.Collections.IList ObjectList { get { throw null; } set { } } - public byte[] SignatureValue { get { throw null; } set { } } - public System.Security.Cryptography.Xml.SignedInfo SignedInfo { get { throw null; } set { } } + public byte[]? SignatureValue { get { throw null; } set { } } + public System.Security.Cryptography.Xml.SignedInfo? SignedInfo { get { throw null; } set { } } public void AddObject(System.Security.Cryptography.Xml.DataObject dataObject) { } public System.Xml.XmlElement GetXml() { throw null; } public void LoadXml(System.Xml.XmlElement value) { } @@ -334,12 +342,12 @@ public SignedInfo() { } public string CanonicalizationMethod { get { throw null; } set { } } public System.Security.Cryptography.Xml.Transform CanonicalizationMethodObject { get { throw null; } } public int Count { get { throw null; } } - public string Id { get { throw null; } set { } } + public string? Id { get { throw null; } set { } } public bool IsReadOnly { get { throw null; } } public bool IsSynchronized { get { throw null; } } public System.Collections.ArrayList References { get { throw null; } } - public string SignatureLength { get { throw null; } set { } } - public string SignatureMethod { get { throw null; } set { } } + public string? SignatureLength { get { throw null; } set { } } + public string? SignatureMethod { get { throw null; } set { } } public object SyncRoot { get { throw null; } } public void AddReference(System.Security.Cryptography.Xml.Reference reference) { } public void CopyTo(System.Array array, int index) { } @@ -349,8 +357,8 @@ public void LoadXml(System.Xml.XmlElement value) { } } public partial class SignedXml { - protected System.Security.Cryptography.Xml.Signature m_signature; - protected string m_strSigningKeyName; + protected System.Security.Cryptography.Xml.Signature? m_signature; + protected string? m_strSigningKeyName; public const string XmlDecryptionTransformUrl = "http://www.w3.org/2002/07/decrypt#XML"; public const string XmlDsigBase64TransformUrl = "http://www.w3.org/2000/09/xmldsig#base64"; public const string XmlDsigC14NTransformUrl = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; @@ -381,40 +389,40 @@ public SignedXml(System.Xml.XmlElement elem) { } public System.Security.Cryptography.Xml.EncryptedXml EncryptedXml { get { throw null; } set { } } public System.Security.Cryptography.Xml.KeyInfo KeyInfo { get { throw null; } set { } } public System.Xml.XmlResolver Resolver { set { } } - public System.Collections.ObjectModel.Collection SafeCanonicalizationMethods { get { throw null; } } - public System.Security.Cryptography.Xml.Signature Signature { get { throw null; } } + public System.Collections.ObjectModel.Collection? SafeCanonicalizationMethods { get { throw null; } } + public System.Security.Cryptography.Xml.Signature? Signature { get { throw null; } } public System.Func SignatureFormatValidator { get { throw null; } set { } } - public string SignatureLength { get { throw null; } } - public string SignatureMethod { get { throw null; } } - public byte[] SignatureValue { get { throw null; } } - public System.Security.Cryptography.Xml.SignedInfo SignedInfo { get { throw null; } } - public System.Security.Cryptography.AsymmetricAlgorithm SigningKey { get { throw null; } set { } } - public string SigningKeyName { get { throw null; } set { } } + public string? SignatureLength { get { throw null; } } + public string? SignatureMethod { get { throw null; } } + public byte[]? SignatureValue { get { throw null; } } + public System.Security.Cryptography.Xml.SignedInfo? SignedInfo { get { throw null; } } + public System.Security.Cryptography.AsymmetricAlgorithm? SigningKey { get { throw null; } set { } } + public string? SigningKeyName { get { throw null; } set { } } public void AddObject(System.Security.Cryptography.Xml.DataObject dataObject) { } public void AddReference(System.Security.Cryptography.Xml.Reference reference) { } public bool CheckSignature() { throw null; } public bool CheckSignature(System.Security.Cryptography.AsymmetricAlgorithm key) { throw null; } public bool CheckSignature(System.Security.Cryptography.KeyedHashAlgorithm macAlg) { throw null; } public bool CheckSignature(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate, bool verifySignatureOnly) { throw null; } - public bool CheckSignatureReturningKey(out System.Security.Cryptography.AsymmetricAlgorithm signingKey) { throw null; } + public bool CheckSignatureReturningKey(out System.Security.Cryptography.AsymmetricAlgorithm? signingKey) { throw null; } public void ComputeSignature() { } public void ComputeSignature(System.Security.Cryptography.KeyedHashAlgorithm macAlg) { } - public virtual System.Xml.XmlElement GetIdElement(System.Xml.XmlDocument document, string idValue) { throw null; } - protected virtual System.Security.Cryptography.AsymmetricAlgorithm GetPublicKey() { throw null; } + public virtual System.Xml.XmlElement? GetIdElement(System.Xml.XmlDocument? document, string idValue) { throw null; } + protected virtual System.Security.Cryptography.AsymmetricAlgorithm? GetPublicKey() { throw null; } public System.Xml.XmlElement GetXml() { throw null; } public void LoadXml(System.Xml.XmlElement value) { } } public abstract partial class Transform { protected Transform() { } - public string Algorithm { get { throw null; } set { } } - public System.Xml.XmlElement Context { get { throw null; } set { } } + public string? Algorithm { get { throw null; } set { } } + public System.Xml.XmlElement? Context { get { throw null; } set { } } public abstract System.Type[] InputTypes { get; } public abstract System.Type[] OutputTypes { get; } public System.Collections.Hashtable PropagatedNamespaces { get { throw null; } } - public System.Xml.XmlResolver Resolver { set { } } - public virtual byte[] GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } - protected abstract System.Xml.XmlNodeList GetInnerXml(); + public System.Xml.XmlResolver? Resolver { set { } } + public virtual byte[]? GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } + protected abstract System.Xml.XmlNodeList? GetInnerXml(); public abstract object GetOutput(); public abstract object GetOutput(System.Type type); public System.Xml.XmlElement GetXml() { throw null; } @@ -436,10 +444,10 @@ public XmlDecryptionTransform() { } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } public void AddExceptUri(string uri) { } - protected override System.Xml.XmlNodeList GetInnerXml() { throw null; } + protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } - protected virtual bool IsTargetElement(System.Xml.XmlElement inputElement, string idValue) { throw null; } + protected virtual bool IsTargetElement(System.Xml.XmlElement? inputElement, string idValue) { throw null; } public override void LoadInnerXml(System.Xml.XmlNodeList nodeList) { } public override void LoadInput(object obj) { } } @@ -448,7 +456,7 @@ public partial class XmlDsigBase64Transform : System.Security.Cryptography.Xml.T public XmlDsigBase64Transform() { } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - protected override System.Xml.XmlNodeList GetInnerXml() { throw null; } + protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } public override void LoadInnerXml(System.Xml.XmlNodeList nodeList) { } @@ -460,8 +468,8 @@ public XmlDsigC14NTransform() { } public XmlDsigC14NTransform(bool includeComments) { } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - public override byte[] GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } - protected override System.Xml.XmlNodeList GetInnerXml() { throw null; } + public override byte[]? GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } + protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } public override void LoadInnerXml(System.Xml.XmlNodeList nodeList) { } @@ -477,7 +485,7 @@ public XmlDsigEnvelopedSignatureTransform() { } public XmlDsigEnvelopedSignatureTransform(bool includeComments) { } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - protected override System.Xml.XmlNodeList GetInnerXml() { throw null; } + protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } public override void LoadInnerXml(System.Xml.XmlNodeList nodeList) { } @@ -487,13 +495,13 @@ public partial class XmlDsigExcC14NTransform : System.Security.Cryptography.Xml. { public XmlDsigExcC14NTransform() { } public XmlDsigExcC14NTransform(bool includeComments) { } - public XmlDsigExcC14NTransform(bool includeComments, string inclusiveNamespacesPrefixList) { } + public XmlDsigExcC14NTransform(bool includeComments, string? inclusiveNamespacesPrefixList) { } public XmlDsigExcC14NTransform(string inclusiveNamespacesPrefixList) { } - public string InclusiveNamespacesPrefixList { get { throw null; } set { } } + public string? InclusiveNamespacesPrefixList { get { throw null; } set { } } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - public override byte[] GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } - protected override System.Xml.XmlNodeList GetInnerXml() { throw null; } + public override byte[]? GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } + protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } public override void LoadInnerXml(System.Xml.XmlNodeList nodeList) { } @@ -509,7 +517,7 @@ public partial class XmlDsigXPathTransform : System.Security.Cryptography.Xml.Tr public XmlDsigXPathTransform() { } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - protected override System.Xml.XmlNodeList GetInnerXml() { throw null; } + protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } public override void LoadInnerXml(System.Xml.XmlNodeList nodeList) { } @@ -521,7 +529,7 @@ public XmlDsigXsltTransform() { } public XmlDsigXsltTransform(bool includeComments) { } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - protected override System.Xml.XmlNodeList GetInnerXml() { throw null; } + protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } public override void LoadInnerXml(System.Xml.XmlNodeList nodeList) { } @@ -530,10 +538,10 @@ public override void LoadInput(object obj) { } public partial class XmlLicenseTransform : System.Security.Cryptography.Xml.Transform { public XmlLicenseTransform() { } - public System.Security.Cryptography.Xml.IRelDecryptor Decryptor { get { throw null; } set { } } + public System.Security.Cryptography.Xml.IRelDecryptor? Decryptor { get { throw null; } set { } } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - protected override System.Xml.XmlNodeList GetInnerXml() { throw null; } + protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } public override void LoadInnerXml(System.Xml.XmlNodeList nodeList) { } From a3d906aa2b30a5a505c0d6c4a660d853610ed9e3 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Thu, 31 Mar 2022 21:25:50 +0100 Subject: [PATCH 24/67] Updated ref assembly --- .../ref/System.Security.Cryptography.Xml.cs | 13 ++++++------- .../ref/System.Security.Cryptography.Xml.csproj | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 0f887cfcb018f..d4fe345ada0bc 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -80,8 +80,7 @@ public abstract partial class EncryptedReference protected EncryptedReference() { } protected EncryptedReference(string? uri) { } protected EncryptedReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } - [System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute(true, "_cachedXml")] - protected internal bool CacheValid { [System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute(true, "_cachedXml")] get { throw null; } } + protected internal bool CacheValid { get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } public string? Uri { get { throw null; } set { } } @@ -193,11 +192,11 @@ public void Insert(int index, System.Security.Cryptography.Xml.EncryptionPropert public System.Security.Cryptography.Xml.EncryptionProperty Item(int index) { throw null; } public void Remove(System.Security.Cryptography.Xml.EncryptionProperty value) { } public void RemoveAt(int index) { } - int System.Collections.IList.Add(object value) { throw null; } - bool System.Collections.IList.Contains(object value) { throw null; } - int System.Collections.IList.IndexOf(object value) { throw null; } - void System.Collections.IList.Insert(int index, object value) { } - void System.Collections.IList.Remove(object value) { } + int System.Collections.IList.Add(object? value) { throw null; } + bool System.Collections.IList.Contains(object? value) { throw null; } + int System.Collections.IList.IndexOf(object? value) { throw null; } + void System.Collections.IList.Insert(int index, object? value) { } + void System.Collections.IList.Remove(object? value) { } } public partial interface IRelDecryptor { diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.csproj b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.csproj index 9f5143a0efab8..8e8a114241224 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.csproj +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.csproj @@ -1,7 +1,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) - disable + enable From fdb26d4b947e4838cd037ea0305fe772f07190c9 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Thu, 31 Mar 2022 21:45:08 +0100 Subject: [PATCH 25/67] PR feedback: add `MemberNotNull` attribute on `Initialize`) --- .../Security/Cryptography/Xml/SignedXml.cs | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index c2d46aa71adf8..e9b3a9b83866a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -13,7 +13,7 @@ namespace System.Security.Cryptography.Xml { public class SignedXml { - protected Signature? m_signature; + protected Signature m_signature; protected string? m_strSigningKeyName; private AsymmetricAlgorithm? _signingKey; @@ -30,7 +30,7 @@ public class SignedXml private bool _bResolverSet; private Func _signatureFormatValidator = DefaultSignatureFormatValidator; - private Collection? _safeCanonicalizationMethods; + private Collection _safeCanonicalizationMethods; // Built in canonicalization algorithm URIs private static IList? s_knownCanonicalizationMethods; @@ -111,6 +111,8 @@ public SignedXml(XmlElement elem) Initialize(elem); } + [MemberNotNull(nameof(m_signature))] + [MemberNotNull(nameof(_safeCanonicalizationMethods))] private void Initialize(XmlElement? element) { _containingDocument = element?.OwnerDocument; @@ -156,7 +158,7 @@ public Func SignatureFormatValidator set { _signatureFormatValidator = value; } } - public Collection? SafeCanonicalizationMethods + public Collection SafeCanonicalizationMethods { get { return _safeCanonicalizationMethods; } } @@ -173,44 +175,44 @@ public EncryptedXml EncryptedXml set => _exml = value; } - public Signature? Signature + public Signature Signature { get { return m_signature; } } public SignedInfo? SignedInfo { - get { return m_signature!.SignedInfo; } + get { return m_signature.SignedInfo; } } public string? SignatureMethod { - get { return m_signature!.SignedInfo!.SignatureMethod; } + get { return m_signature.SignedInfo!.SignatureMethod; } } public string? SignatureLength { - get { return m_signature!.SignedInfo!.SignatureLength; } + get { return m_signature.SignedInfo!.SignatureLength; } } public byte[]? SignatureValue { - get { return m_signature!.SignatureValue; } + get { return m_signature.SignatureValue; } } public KeyInfo KeyInfo { - get { return m_signature!.KeyInfo; } - set { m_signature!.KeyInfo = value; } + get { return m_signature.KeyInfo; } + set { m_signature.KeyInfo = value; } } public XmlElement GetXml() { // If we have a document context, then return a signature element in this context if (_containingDocument != null) - return m_signature!.GetXml(_containingDocument); + return m_signature.GetXml(_containingDocument); else - return m_signature!.GetXml(); + return m_signature.GetXml(); } public void LoadXml(XmlElement value) @@ -220,7 +222,7 @@ public void LoadXml(XmlElement value) throw new ArgumentNullException(nameof(value)); } - m_signature!.LoadXml(value); + m_signature.LoadXml(value); _context ??= value; @@ -233,12 +235,12 @@ public void LoadXml(XmlElement value) public void AddReference(Reference reference) { - m_signature!.SignedInfo!.AddReference(reference); + m_signature.SignedInfo!.AddReference(reference); } public void AddObject(DataObject dataObject) { - m_signature!.AddObject(dataObject); + m_signature.AddObject(dataObject); } public bool CheckSignature() @@ -415,7 +417,7 @@ public void ComputeSignature() AsymmetricSignatureFormatter asymmetricSignatureFormatter = signatureDescription.CreateFormatter(key); SignedXmlDebugLog.LogSigning(this, key, signatureDescription, hashAlg, asymmetricSignatureFormatter); - m_signature!.SignatureValue = asymmetricSignatureFormatter.CreateSignature(hashAlg); + m_signature.SignatureValue = asymmetricSignatureFormatter.CreateSignature(hashAlg); } public void ComputeSignature(KeyedHashAlgorithm macAlg) @@ -430,7 +432,7 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) throw new CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch); int signatureLength; - if (m_signature!.SignedInfo!.SignatureLength == null) + if (m_signature.SignedInfo!.SignatureLength == null) signatureLength = hash.HashSize; else signatureLength = Convert.ToInt32(m_signature.SignedInfo.SignatureLength, null); @@ -885,7 +887,7 @@ private void BuildDigestedReferences() sortedReferences.Sort(sortOrder); CanonicalXmlNodeList nodeList = new CanonicalXmlNodeList(); - foreach (DataObject obj in m_signature!.ObjectList) + foreach (DataObject obj in m_signature.ObjectList) { nodeList.Add(obj.GetXml()); } @@ -905,7 +907,7 @@ private void BuildDigestedReferences() private bool CheckDigestedReferences() { - ArrayList references = m_signature!.SignedInfo!.References; + ArrayList references = m_signature.SignedInfo!.References; for (int i = 0; i < references.Count; ++i) { Reference digestedReference = (Reference)references[i]!; @@ -998,7 +1000,7 @@ private bool CheckSignedInfo(AsymmetricAlgorithm key) throw new ArgumentNullException(nameof(key)); } - SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature!.SignedInfo!); + SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature.SignedInfo!); SignatureDescription? signatureDescription = CryptoHelpers.CreateFromName(SignatureMethod); if (signatureDescription == null) @@ -1032,7 +1034,7 @@ private bool CheckSignedInfo(KeyedHashAlgorithm macAlg) throw new ArgumentNullException(nameof(macAlg)); } - SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature!.SignedInfo!); + SignedXmlDebugLog.LogBeginCheckSignedInfo(this, m_signature.SignedInfo!); int signatureLength; if (m_signature.SignedInfo!.SignatureLength == null) From fe764fa5b6fbfefb263331c4541c4e98cfbe0980 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Thu, 31 Mar 2022 21:53:09 +0100 Subject: [PATCH 26/67] Updated ref again --- .../ref/System.Security.Cryptography.Xml.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index d4fe345ada0bc..fe5603adc7018 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -80,6 +80,7 @@ public abstract partial class EncryptedReference protected EncryptedReference() { } protected EncryptedReference(string? uri) { } protected EncryptedReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + [System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute(true, (string)null!)] protected internal bool CacheValid { get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } @@ -356,7 +357,7 @@ public void LoadXml(System.Xml.XmlElement value) { } } public partial class SignedXml { - protected System.Security.Cryptography.Xml.Signature? m_signature; + protected System.Security.Cryptography.Xml.Signature m_signature; protected string? m_strSigningKeyName; public const string XmlDecryptionTransformUrl = "http://www.w3.org/2002/07/decrypt#XML"; public const string XmlDsigBase64TransformUrl = "http://www.w3.org/2000/09/xmldsig#base64"; @@ -388,8 +389,8 @@ public SignedXml(System.Xml.XmlElement elem) { } public System.Security.Cryptography.Xml.EncryptedXml EncryptedXml { get { throw null; } set { } } public System.Security.Cryptography.Xml.KeyInfo KeyInfo { get { throw null; } set { } } public System.Xml.XmlResolver Resolver { set { } } - public System.Collections.ObjectModel.Collection? SafeCanonicalizationMethods { get { throw null; } } - public System.Security.Cryptography.Xml.Signature? Signature { get { throw null; } } + public System.Collections.ObjectModel.Collection SafeCanonicalizationMethods { get { throw null; } } + public System.Security.Cryptography.Xml.Signature Signature { get { throw null; } } public System.Func SignatureFormatValidator { get { throw null; } set { } } public string? SignatureLength { get { throw null; } } public string? SignatureMethod { get { throw null; } } From 86e1784103459c7625626b3c2d9d49b9e5f5f366 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Fri, 1 Apr 2022 06:18:01 +0100 Subject: [PATCH 27/67] Put `MemberNotNull` attribute on the getter rather than the property in the reference file. --- .../ref/System.Security.Cryptography.Xml.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index fe5603adc7018..3ee533f62f52f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -80,8 +80,7 @@ public abstract partial class EncryptedReference protected EncryptedReference() { } protected EncryptedReference(string? uri) { } protected EncryptedReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } - [System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute(true, (string)null!)] - protected internal bool CacheValid { get { throw null; } } + protected internal bool CacheValid { [System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute(true, (string)null!)] get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } public string? Uri { get { throw null; } set { } } From d76993c5ec26c55348c7fc9e5bf8d693dd30bf01 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Sat, 2 Apr 2022 06:20:57 +0100 Subject: [PATCH 28/67] PR feedback: remove MemberNotNullWhenAttribute from ref --- .../ref/System.Security.Cryptography.Xml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 3ee533f62f52f..e7e66f7e3ff32 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -80,7 +80,7 @@ public abstract partial class EncryptedReference protected EncryptedReference() { } protected EncryptedReference(string? uri) { } protected EncryptedReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } - protected internal bool CacheValid { [System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute(true, (string)null!)] get { throw null; } } + protected internal bool CacheValid { get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } public string? Uri { get { throw null; } set { } } From f4acf19e23fb7b2a3b35f544a733d970c3e883e0 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Sun, 3 Apr 2022 13:42:16 +0100 Subject: [PATCH 29/67] PR feedback: non nullable param to match method in base class --- .../Cryptography/Xml/C14NAncestralNamespaceContextManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs index 7ff5663dcf77b..5020d69364e08 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs @@ -89,9 +89,9 @@ internal override void TrackNamespaceNode(XmlAttribute attr, SortedList nsListTo nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr), attr); } - internal override void TrackXmlNamespaceNode(XmlAttribute? attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) + internal override void TrackXmlNamespaceNode(XmlAttribute attr, SortedList nsListToRender, SortedList attrListToRender, Hashtable nsLocallyDeclared) { - nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr!), attr); + nsLocallyDeclared.Add(Utils.GetNamespacePrefix(attr), attr); } } } From 295698d0852d7a225d5c3c6b1d3da0ffaa80d5d6 Mon Sep 17 00:00:00 2001 From: stevedunnhq Date: Sat, 23 Apr 2022 16:53:13 +0100 Subject: [PATCH 30/67] Fix post-merge build errors --- .../System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs index c58e07d7a0eae..d536c2894f459 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs @@ -96,7 +96,7 @@ public override void LoadInput(object obj) } else if (obj is XmlDocument) { - CanonicalXml xmlDoc = new CanonicalXml((XmlDocument)obj, null, _includeComments); + CanonicalXml xmlDoc = new CanonicalXml((XmlDocument)obj, null!, _includeComments); byte[] buffer = xmlDoc.GetBytes(); if (buffer == null) return; _inputStream.Write(buffer, 0, buffer.Length); From c394bc00134835cf6ef1c80af4b0b4a6ca5feee5 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Wed, 27 Apr 2022 06:06:57 +0100 Subject: [PATCH 31/67] PR feedback --- .../ref/System.Security.Cryptography.Xml.cs | 9 +-------- .../Xml/C14NAncestralNamespaceContextManager.cs | 2 +- .../src/System/Security/Cryptography/Xml/CanonicalXml.cs | 4 ++-- .../System/Security/Cryptography/Xml/ExcCanonicalXml.cs | 4 ++-- .../src/System/Security/Cryptography/Xml/Transform.cs | 2 +- .../Security/Cryptography/Xml/XmlDsigC14NTransform.cs | 2 +- .../Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs | 2 +- 7 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index e7e66f7e3ff32..fd8bedc6ff69d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -24,13 +24,6 @@ public CipherReference(string? uri, System.Security.Cryptography.Xml.TransformCh public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } - public partial class CryptoSignedXmlRecursionException : System.Xml.XmlException - { - public CryptoSignedXmlRecursionException() { } - protected CryptoSignedXmlRecursionException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } - public CryptoSignedXmlRecursionException(string message) { } - public CryptoSignedXmlRecursionException(string message, System.Exception inner) { } - } public partial class DataObject { public DataObject() { } @@ -467,7 +460,7 @@ public XmlDsigC14NTransform() { } public XmlDsigC14NTransform(bool includeComments) { } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - public override byte[]? GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } + public override byte[] GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs index 5020d69364e08..5365bae76f58a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs @@ -26,7 +26,7 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender, } int rDepth; - XmlAttribute local = (XmlAttribute)nsLocallyDeclared[nsPrefix]!; + XmlAttribute? local = (XmlAttribute?)nsLocallyDeclared[nsPrefix]; XmlAttribute? rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth); if (local != null) { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs index 79ebad121c8e2..8f6eb33e47a35 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs @@ -117,11 +117,11 @@ internal byte[] GetBytes() return utf8.GetBytes(sb.ToString()); } - internal byte[]? GetDigestedBytes(HashAlgorithm hash) + internal byte[] GetDigestedBytes(HashAlgorithm hash) { _c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr); hash.TransformFinalBlock(Array.Empty(), 0, 0); - byte[]? res = (byte[])hash.Hash!.Clone(); + byte[] res = (byte[])hash.Hash!.Clone(); // reinitialize the hash so it is still usable after the call hash.Initialize(); return res; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs index 993c5ed0df425..41d3a466d6507 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs @@ -65,11 +65,11 @@ internal byte[] GetBytes() return utf8.GetBytes(sb.ToString()); } - internal byte[]? GetDigestedBytes(HashAlgorithm hash) + internal byte[] GetDigestedBytes(HashAlgorithm hash) { _c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr); hash.TransformFinalBlock(Array.Empty(), 0, 0); - byte[]? res = (byte[]?)hash.Hash!.Clone(); + byte[] res = (byte[])hash.Hash!.Clone(); // reinitialize the hash so it is still usable after the call hash.Initialize(); return res; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs index 698af0c4899cc..181d0dabea8dd 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs @@ -149,7 +149,7 @@ internal XmlElement GetXml(XmlDocument document, string name) public abstract object GetOutput(Type type); - public virtual byte[]? GetDigestedOutput(HashAlgorithm hash) + public virtual byte[] GetDigestedOutput(HashAlgorithm hash) { return hash.ComputeHash((Stream)GetOutput(typeof(Stream))); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs index d42d6ce38591c..e50a538cd4513 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs @@ -80,7 +80,7 @@ public override object GetOutput(Type type) return new MemoryStream(_cXml!.GetBytes()); } - public override byte[]? GetDigestedOutput(HashAlgorithm hash) + public override byte[] GetDigestedOutput(HashAlgorithm hash) { return _cXml!.GetDigestedBytes(hash); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs index 1f78959715252..844601a0adcac 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NTransform.cs @@ -117,7 +117,7 @@ public override object GetOutput(Type type) return new MemoryStream(_excCanonicalXml!.GetBytes()); } - public override byte[]? GetDigestedOutput(HashAlgorithm hash) + public override byte[] GetDigestedOutput(HashAlgorithm hash) { return _excCanonicalXml!.GetDigestedBytes(hash); } From 917af636c599fc547650a37210c5a1307be46e88 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Wed, 27 Apr 2022 06:21:33 +0100 Subject: [PATCH 32/67] PR feedback from @bartonjs - add [DisallowNull] and cascade --- .../ref/System.Security.Cryptography.Xml.cs | 20 +++++++++---------- .../Cryptography/Xml/CipherReference.cs | 4 ++-- .../Cryptography/Xml/DataReference.cs | 4 ++-- .../Cryptography/Xml/EncryptedReference.cs | 5 +++-- .../Security/Cryptography/Xml/KeyReference.cs | 4 ++-- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index fd8bedc6ff69d..ff5d69d8bb83e 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -19,8 +19,8 @@ public void LoadXml(System.Xml.XmlElement value) { } public sealed partial class CipherReference : System.Security.Cryptography.Xml.EncryptedReference { public CipherReference() { } - public CipherReference(string? uri) { } - public CipherReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + public CipherReference(string uri) { } + public CipherReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } @@ -38,8 +38,8 @@ public void LoadXml(System.Xml.XmlElement value) { } public sealed partial class DataReference : System.Security.Cryptography.Xml.EncryptedReference { public DataReference() { } - public DataReference(string? uri) { } - public DataReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + public DataReference(string uri) { } + public DataReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } } public partial class DSAKeyValue : System.Security.Cryptography.Xml.KeyInfoClause { @@ -71,8 +71,8 @@ public override void LoadXml(System.Xml.XmlElement value) { } public abstract partial class EncryptedReference { protected EncryptedReference() { } - protected EncryptedReference(string? uri) { } - protected EncryptedReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + protected EncryptedReference(string uri) { } + protected EncryptedReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } protected internal bool CacheValid { get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } @@ -268,8 +268,8 @@ public override void LoadXml(System.Xml.XmlElement element) { } public sealed partial class KeyReference : System.Security.Cryptography.Xml.EncryptedReference { public KeyReference() { } - public KeyReference(string? uri) { } - public KeyReference(string? uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + public KeyReference(string uri) { } + public KeyReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } } public partial class Reference { @@ -413,7 +413,7 @@ protected Transform() { } public abstract System.Type[] OutputTypes { get; } public System.Collections.Hashtable PropagatedNamespaces { get { throw null; } } public System.Xml.XmlResolver? Resolver { set { } } - public virtual byte[]? GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } + public virtual byte[] GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } protected abstract System.Xml.XmlNodeList? GetInnerXml(); public abstract object GetOutput(); public abstract object GetOutput(System.Type type); @@ -492,7 +492,7 @@ public XmlDsigExcC14NTransform(string inclusiveNamespacesPrefixList) { } public string? InclusiveNamespacesPrefixList { get { throw null; } set { } } public override System.Type[] InputTypes { get { throw null; } } public override System.Type[] OutputTypes { get { throw null; } } - public override byte[]? GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } + public override byte[] GetDigestedOutput(System.Security.Cryptography.HashAlgorithm hash) { throw null; } protected override System.Xml.XmlNodeList? GetInnerXml() { throw null; } public override object GetOutput() { throw null; } public override object GetOutput(System.Type type) { throw null; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs index c34da1163806e..5fdd29471d63d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs @@ -14,12 +14,12 @@ public CipherReference() : base() ReferenceType = "CipherReference"; } - public CipherReference(string? uri) : base(uri) + public CipherReference(string uri) : base(uri) { ReferenceType = "CipherReference"; } - public CipherReference(string? uri, TransformChain transformChain) : base(uri, transformChain) + public CipherReference(string uri, TransformChain transformChain) : base(uri, transformChain) { ReferenceType = "CipherReference"; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs index 4059826eb8029..18ca820c66139 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs @@ -10,12 +10,12 @@ public DataReference() : base() ReferenceType = "DataReference"; } - public DataReference(string? uri) : base(uri) + public DataReference(string uri) : base(uri) { ReferenceType = "DataReference"; } - public DataReference(string? uri, TransformChain transformChain) : base(uri, transformChain) + public DataReference(string uri, TransformChain transformChain) : base(uri, transformChain) { ReferenceType = "DataReference"; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index a1515e64960a5..6b33ee701e8a7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -17,17 +17,18 @@ public abstract class EncryptedReference { } - protected EncryptedReference(string? uri) : this(uri, new TransformChain()) + protected EncryptedReference(string uri) : this(uri, new TransformChain()) { } - protected EncryptedReference(string? uri, TransformChain transformChain) + protected EncryptedReference(string uri, TransformChain transformChain) { TransformChain = transformChain; Uri = uri; _cachedXml = null; } + [DisallowNull] public string? Uri { get { return _uri; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs index 40471720e7061..fd8d8452608da 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs @@ -10,12 +10,12 @@ public KeyReference() : base() ReferenceType = "KeyReference"; } - public KeyReference(string? uri) : base(uri) + public KeyReference(string uri) : base(uri) { ReferenceType = "KeyReference"; } - public KeyReference(string? uri, TransformChain transformChain) : base(uri, transformChain) + public KeyReference(string uri, TransformChain transformChain) : base(uri, transformChain) { ReferenceType = "KeyReference"; } From 3ee85981e8de50a1320283b03a4d57dc409a21a4 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Wed, 27 Apr 2022 07:54:57 +0100 Subject: [PATCH 33/67] PR feedback --- .../Security/Cryptography/Xml/Reference.cs | 2 +- .../Security/Cryptography/Xml/SignedXml.cs | 16 ++++++++++------ .../Cryptography/Xml/SignedXmlDebugLog.cs | 4 +++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index fd8e82799563f..7860c984e1c89 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -353,7 +353,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList WebResponse? response = null; Stream? inputStream = null; XmlResolver? resolver = null; - byte[]? hashval = null; + byte[] hashval; try { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index e9b3a9b83866a..6754bb5f7d123 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; @@ -802,20 +803,23 @@ private static IList DefaultSafeTransformMethods private int GetReferenceLevel(int index, ArrayList references) { - if (_refProcessed![index]) return _refLevelCache![index]; + Debug.Assert(_refProcessed != null); + Debug.Assert(_refLevelCache != null); + + if (_refProcessed[index]) return _refLevelCache[index]; _refProcessed[index] = true; Reference reference = (Reference)references[index]!; if (reference.Uri == null || reference.Uri.Length == 0 || (reference.Uri.Length > 0 && reference.Uri[0] != '#')) { - _refLevelCache![index] = 0; + _refLevelCache[index] = 0; return 0; } if (reference.Uri.Length > 0 && reference.Uri[0] == '#') { - string idref = Utils.ExtractIdFromLocalUri(reference.Uri!); + string idref = Utils.ExtractIdFromLocalUri(reference.Uri); if (idref == "xpointer(/)") { - _refLevelCache![index] = 0; + _refLevelCache[index] = 0; return 0; } // If this is pointing to another reference @@ -823,12 +827,12 @@ private int GetReferenceLevel(int index, ArrayList references) { if (((Reference)references[j]!).Id == idref) { - _refLevelCache![index] = GetReferenceLevel(j, references) + 1; + _refLevelCache[index] = GetReferenceLevel(j, references) + 1; return (_refLevelCache[index]); } } // Then the reference points to an object tag - _refLevelCache![index] = 0; + _refLevelCache[index] = 0; return 0; } // Malformed reference diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs index 47d89bba7c1ca..7c9f7e74be89f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Reflection; @@ -571,6 +572,7 @@ internal static void LogNamespacePropagation(SignedXml signedXml, XmlNodeList? n /// The reference being processed /// Stream containing the output of the reference /// Stream containing the output of the reference + [return: NotNullIfNotNull("data")] internal static Stream? LogReferenceData(Reference reference, Stream? data) { if (VerboseLoggingEnabled) @@ -582,7 +584,7 @@ internal static void LogNamespacePropagation(SignedXml signedXml, XmlNodeList? n // value. // - MemoryStream? ms = new MemoryStream(); + MemoryStream ms = new MemoryStream(); // First read the input stream into our temporary stream byte[] buffer = new byte[4096]; From c332f84ce60e7c4ec61aa8f41e011edcc364e1a2 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 28 Apr 2022 20:13:05 +0100 Subject: [PATCH 34/67] PR feedback --- .../Security/Cryptography/Xml/CanonicalXmlDocument.cs | 4 +++- .../Security/Cryptography/Xml/CipherReference.cs | 2 +- .../System/Security/Cryptography/Xml/EncryptedXml.cs | 2 +- .../src/System/Security/Cryptography/Xml/SignedXml.cs | 10 +++++----- .../src/System/Security/Cryptography/Xml/Transform.cs | 2 ++ .../src/System/Security/Cryptography/Xml/Utils.cs | 6 +++--- .../Security/Cryptography/Xml/XmlDsigXPathTransform.cs | 2 +- 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index 326469558650f..8b085e9037e5a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -61,7 +61,9 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace } } - public override XmlElement CreateElement(string? prefix, string localName, string? namespaceURI) +#pragma warning disable CS8765 // Nullability doesn't match overriden member + public override XmlElement CreateElement(string prefix, string localName, string namespaceURI) +#pragma warning restore CS8765 { return new CanonicalXmlElement(prefix!, localName, namespaceURI!, this, _defaultNodeSetInclusionState); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs index 5fdd29471d63d..635d8996e2af2 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs @@ -41,7 +41,7 @@ internal byte[]? CipherValue public override XmlElement GetXml() { - if (CacheValid) return _cachedXml!; + if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 51eab26bb7d62..73e771925dc4f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -232,7 +232,7 @@ private byte[] GetCipherValue(CipherData cipherData) byte[]? cipherValue = null; using (MemoryStream ms = new MemoryStream()) { - Utils.Pump(decInputStream, ms); + Utils.Pump(decInputStream!, ms); cipherValue = ms.ToArray(); // Close the stream and return inputStream?.Close(); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index 6754bb5f7d123..8a28b17473e78 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -454,7 +454,7 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) "RIPEMD160" => SignedXml.XmlDsigMoreHMACRIPEMD160Url, _ => throw new CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch), }; - byte[]? hashValue = GetC14NDigest(hash); + byte[] hashValue = GetC14NDigest(hash); SignedXmlDebugLog.LogSigning(this, hash); m_signature.SignatureValue = new byte[signatureLength / 8]; @@ -773,7 +773,7 @@ private static IList DefaultSafeTransformMethods } } - private byte[]? GetC14NDigest(HashAlgorithm hash) + private byte[] GetC14NDigest(HashAlgorithm hash) { bool isKeyedHashAlgorithm = hash is KeyedHashAlgorithm; if (isKeyedHashAlgorithm || !_bCacheValid || !SignedInfo!.CacheValid) @@ -798,7 +798,7 @@ private static IList DefaultSafeTransformMethods _bCacheValid = !isKeyedHashAlgorithm; } - return _digestedSignedInfo; + return _digestedSignedInfo!; } private int GetReferenceLevel(int index, ArrayList references) @@ -1018,7 +1018,7 @@ private bool CheckSignedInfo(AsymmetricAlgorithm key) HashAlgorithm? hashAlgorithm = signatureDescription.CreateDigest(); if (hashAlgorithm == null) throw new CryptographicException(SR.Cryptography_Xml_CreateHashAlgorithmFailed); - byte[]? hashval = GetC14NDigest(hashAlgorithm); + byte[] hashval = GetC14NDigest(hashAlgorithm); AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = signatureDescription.CreateDeformatter(key); SignedXmlDebugLog.LogVerifySignedInfo(this, @@ -1057,7 +1057,7 @@ private bool CheckSignedInfo(KeyedHashAlgorithm macAlg) throw new CryptographicException(SR.Cryptography_Xml_InvalidSignatureLength); // Calculate the hash - byte[]? hashValue = GetC14NDigest(macAlg); + byte[] hashValue = GetC14NDigest(macAlg); SignedXmlDebugLog.LogVerifySignedInfo(this, macAlg, hashValue, m_signature.SignatureValue); return m_signature.SignatureValue.AsSpan().SequenceEqual(hashValue.AsSpan(0, m_signature.SignatureValue.Length)); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs index 181d0dabea8dd..b4d9f66b69c05 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs @@ -14,6 +14,7 @@ // stream. (We only bother implementing that much now since every use of transform chains in XmlDsig ultimately yields something to hash). using System.Collections; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Xml; @@ -79,6 +80,7 @@ internal get } } + [MemberNotNullWhen(true, nameof(_xmlResolver))] internal bool ResolverSet { get { return _bResolverSet; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 710840d0106fa..f79a5c054aebb 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -381,7 +381,7 @@ internal static void RemoveAllChildren(XmlElement inputElement) // an output stream, connecting them up and reading until // hitting the end of the input stream. // returns the number of bytes copied - internal static long Pump(Stream? input, Stream output) + internal static long Pump(Stream input, Stream output) { // Use MemoryStream's WriteTo(Stream) method if possible MemoryStream? inputMS = input as MemoryStream; @@ -396,7 +396,7 @@ internal static long Pump(Stream? input, Stream output) int numBytes; long totalBytes = 0; - while ((numBytes = input!.Read(bytes, 0, count)) > 0) + while ((numBytes = input.Read(bytes, 0, count)) > 0) { output.Write(bytes, 0, numBytes); totalBytes += numBytes; @@ -758,7 +758,7 @@ internal static bool IsSelfSigned(X509Chain chain) internal static AsymmetricAlgorithm? GetAnyPublicKey(X509Certificate2 certificate) { - AsymmetricAlgorithm? algorithm = (AsymmetricAlgorithm)certificate.GetRSAPublicKey()! ?? certificate.GetECDsaPublicKey(); + AsymmetricAlgorithm? algorithm = (AsymmetricAlgorithm?)certificate.GetRSAPublicKey() ?? certificate.GetECDsaPublicKey(); #if NETCOREAPP if (algorithm is null && !OperatingSystem.IsTvOS() && !OperatingSystem.IsIOS()) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs index 05c0f4b8146c3..136cb3285f9a5 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs @@ -134,7 +134,7 @@ public override void LoadInput(object obj) private void LoadStreamInput(Stream stream) { XmlResolver resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); - XmlReader valReader = Utils.PreProcessStreamInput(stream, resolver, BaseURI); + XmlReader valReader = Utils.PreProcessStreamInput(stream, resolver, BaseURI!); _document = new XmlDocument(); _document.PreserveWhitespace = true; _document.Load(valReader); From e29fc434aea86e9b93d0e068d06ef2277d1b902a Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 28 Apr 2022 21:11:55 +0100 Subject: [PATCH 35/67] PR feedback --- .../src/System/Security/Cryptography/Xml/CanonicalXml.cs | 6 +++--- .../src/System/Security/Cryptography/Xml/EncryptedXml.cs | 8 +++++--- .../System/Security/Cryptography/Xml/TransformChain.cs | 6 +++--- .../src/System/Security/Cryptography/Xml/Utils.cs | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs index 8f6eb33e47a35..2de2731895c4a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs @@ -17,7 +17,7 @@ internal sealed class CanonicalXml // private static string defaultXPathWithComments = "(//. | //@* | //namespace::*)"; // private static string defaultXPathWithComments = "(//. | //@* | //namespace::*)"; - internal CanonicalXml(Stream inputStream, bool includeComments, XmlResolver resolver, string strBaseUri) + internal CanonicalXml(Stream inputStream, bool includeComments, XmlResolver? resolver, string strBaseUri) { if (inputStream is null) { @@ -30,7 +30,7 @@ internal CanonicalXml(Stream inputStream, bool includeComments, XmlResolver reso _ancMgr = new C14NAncestralNamespaceContextManager(); } - internal CanonicalXml(XmlDocument document, XmlResolver resolver) : this(document, resolver, false) { } + internal CanonicalXml(XmlDocument document, XmlResolver? resolver) : this(document, resolver, false) { } internal CanonicalXml(XmlDocument document, XmlResolver? resolver, bool includeComments) { if (document is null) @@ -44,7 +44,7 @@ internal CanonicalXml(XmlDocument document, XmlResolver? resolver, bool includeC _ancMgr = new C14NAncestralNamespaceContextManager(); } - internal CanonicalXml(XmlNodeList nodeList, XmlResolver resolver, bool includeComments) + internal CanonicalXml(XmlNodeList nodeList, XmlResolver? resolver, bool includeComments) { if (nodeList is null) { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 73e771925dc4f..394842f334692 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -198,13 +198,15 @@ private byte[] GetCipherValue(CipherData cipherData) if (cipherData.CipherReference.Uri.Length == 0) { // self referenced Uri - string? baseUri = _document?.BaseURI; + string baseUri = _document?.BaseURI!; TransformChain tc = cipherData.CipherReference.TransformChain; if (tc == null) { throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported); } - decInputStream = tc.TransformToOctetStream(_document!, _xmlResolver!, baseUri!); +#pragma warning disable CS8604 // the check for _document == null above is never true, but because it's there, the analyzer thinks it can be null here too. + decInputStream = tc.TransformToOctetStream(_document, _xmlResolver, baseUri); +#pragma warning restore CS8604 } else if (cipherData.CipherReference.Uri[0] == '#') { @@ -222,7 +224,7 @@ private byte[] GetCipherValue(CipherData cipherData) { throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported); } - decInputStream = tc.TransformToOctetStream(inputStream, _xmlResolver!, baseUri!); + decInputStream = tc.TransformToOctetStream(inputStream, _xmlResolver, baseUri); } else { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index 26d9d6ddc670b..09e4443ad0648 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -58,7 +58,7 @@ public Transform this[int index] // The goal behind this method is to pump the input stream through the transforms and get back something that // can be hashed - internal Stream? TransformToOctetStream(object? inputObject, Type inputType, XmlResolver resolver, string baseUri) + internal Stream? TransformToOctetStream(object? inputObject, Type inputType, XmlResolver? resolver, string baseUri) { object? currentInput = inputObject; foreach (Transform transform in _transforms) @@ -150,12 +150,12 @@ public Transform this[int index] throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType); } - internal Stream? TransformToOctetStream(Stream? input, XmlResolver resolver, string baseUri) + internal Stream? TransformToOctetStream(Stream? input, XmlResolver? resolver, string baseUri) { return TransformToOctetStream(input, typeof(Stream), resolver, baseUri); } - internal Stream? TransformToOctetStream(XmlDocument document, XmlResolver resolver, string baseUri) + internal Stream? TransformToOctetStream(XmlDocument document, XmlResolver? resolver, string baseUri) { return TransformToOctetStream(document, typeof(XmlDocument), resolver, baseUri); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index f79a5c054aebb..8ad2f72b613d5 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -183,7 +183,7 @@ internal static void SBReplaceCharWithString(StringBuilder sb, char oldChar, str } } - internal static XmlReader PreProcessStreamInput(Stream inputStream, XmlResolver xmlResolver, string baseUri) + internal static XmlReader PreProcessStreamInput(Stream inputStream, XmlResolver? xmlResolver, string baseUri) { XmlReaderSettings settings = GetSecureXmlReaderSettings(xmlResolver); XmlReader reader = XmlReader.Create(inputStream, settings, baseUri); From 67bd994d28413b4d2f824def3b5f7b8caf306ab5 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Fri, 29 Apr 2022 06:13:33 +0100 Subject: [PATCH 36/67] Fix build again, by removing the `protected internal` property `CacheValid` --- .../ref/System.Security.Cryptography.Xml.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index ff5d69d8bb83e..72adc5e799ff4 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -73,7 +73,6 @@ public abstract partial class EncryptedReference protected EncryptedReference() { } protected EncryptedReference(string uri) { } protected EncryptedReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } - protected internal bool CacheValid { get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } public string? Uri { get { throw null; } set { } } From ccb6a3ff352b78b8756db6fccbc541a27593d278 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Tue, 3 May 2022 05:44:28 +0100 Subject: [PATCH 37/67] PR feedback --- .../System/Security/Cryptography/Xml/EncryptedXml.cs | 2 -- .../src/System/Security/Cryptography/Xml/Reference.cs | 10 +++++----- .../System/Security/Cryptography/Xml/TransformChain.cs | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 394842f334692..48df95d655571 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -204,9 +204,7 @@ private byte[] GetCipherValue(CipherData cipherData) { throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported); } -#pragma warning disable CS8604 // the check for _document == null above is never true, but because it's there, the analyzer thinks it can be null here too. decInputStream = tc.TransformToOctetStream(_document, _xmlResolver, baseUri); -#pragma warning restore CS8604 } else if (cipherData.CipherReference.Uri[0] == '#') { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index 7860c984e1c89..6593caa8e75b0 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -386,7 +386,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList // Normalize the containing document resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); XmlDocument docWithNoComments = Utils.DiscardComments(Utils.PreProcessDocumentInput(document, resolver!, baseUri)); - hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver!, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver, baseUri); } else if (_uri[0] == '#') { @@ -401,7 +401,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList // We should not discard comments here!!! resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); - hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessDocumentInput(document, resolver!, baseUri), resolver!, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessDocumentInput(document, resolver!, baseUri), resolver , baseUri); break; } @@ -441,12 +441,12 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList { // We should discard comments before going into the transform chain XmlDocument docWithNoComments = Utils.DiscardComments(normDocument); - hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver!, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver, baseUri); } else { // This is an XPointer reference, do not discard comments!!! - hashInputStream = TransformChain.TransformToOctetStream(normDocument, resolver!, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(normDocument, resolver, baseUri); } } else @@ -457,7 +457,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList case ReferenceTargetType.XmlElement: // We need to create a DocumentNavigator out of the XmlElement resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); - hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessElementInput((XmlElement)_refTarget!, resolver!, baseUri), resolver!, baseUri); + hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessElementInput((XmlElement)_refTarget!, resolver!, baseUri), resolver , baseUri); break; default: throw new CryptographicException(SR.Cryptography_Xml_UriNotResolved, _uri); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index 09e4443ad0648..8cbbbf47ec647 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -155,7 +155,7 @@ public Transform this[int index] return TransformToOctetStream(input, typeof(Stream), resolver, baseUri); } - internal Stream? TransformToOctetStream(XmlDocument document, XmlResolver? resolver, string baseUri) + internal Stream? TransformToOctetStream(XmlDocument? document, XmlResolver? resolver, string baseUri) { return TransformToOctetStream(document, typeof(XmlDocument), resolver, baseUri); } From e13afbf7e2214341e45175ee3acc9f12d8fd964a Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Tue, 3 May 2022 05:54:14 +0100 Subject: [PATCH 38/67] Added back `CacheValid` based on PR comment --- .../ref/System.Security.Cryptography.Xml.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 72adc5e799ff4..ff5d69d8bb83e 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -73,6 +73,7 @@ public abstract partial class EncryptedReference protected EncryptedReference() { } protected EncryptedReference(string uri) { } protected EncryptedReference(string uri, System.Security.Cryptography.Xml.TransformChain transformChain) { } + protected internal bool CacheValid { get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } public string? Uri { get { throw null; } set { } } From ee8a0aaa182333887c233ec1f6a4f276433b5ff4 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 2 Jun 2022 20:58:51 +0100 Subject: [PATCH 39/67] Fix issues from recent rebase --- .../src/System/Security/Cryptography/Xml/EncryptedXml.cs | 2 +- .../src/System/Security/Cryptography/Xml/Reference.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 48df95d655571..e17e44f10d167 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -803,7 +803,7 @@ public void ReplaceData(XmlElement inputElement, byte[] decryptedData) throw new ArgumentNullException(nameof(decryptedData)); } - XmlNode parent = inputElement.ParentNode; + XmlNode parent = inputElement.ParentNode!; if (parent.NodeType == XmlNodeType.Document) { // We're replacing the root element, but we can't just wholesale replace the owner diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index 6593caa8e75b0..70ea342f9ae8a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -270,10 +270,10 @@ public void LoadXml(XmlElement value) XmlNode? signatureTag = transformElement.SelectSingleNode("ancestor::ds:Signature[1]", nsm); // Resolve the reference to get starting point for position calculation. - XmlNode referenceTarget = + XmlNode? referenceTarget = _uri.Length == 0 ? transformElement.OwnerDocument - : SignedXml.GetIdElement(transformElement.OwnerDocument, Utils.GetIdFromLocalUri(_uri, out bool _)); + : SignedXml!.GetIdElement(transformElement.OwnerDocument, Utils.GetIdFromLocalUri(_uri, out bool _)); XmlNodeList? signatureList = referenceTarget?.SelectNodes(".//ds:Signature", nsm); if (signatureList != null) From 987940adfccec65cb7ae883518d4a2911d380133 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 2 Jun 2022 21:51:44 +0100 Subject: [PATCH 40/67] Add `DisallowNull` attribute to ref file and remove it from `DefaultGenApiDocIds.txt` --- .../ref/System.Security.Cryptography.Xml.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index ff5d69d8bb83e..9537d39bbc4a4 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -76,6 +76,7 @@ protected EncryptedReference(string uri, System.Security.Cryptography.Xml.Transf protected internal bool CacheValid { get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.DisallowNullAttribute] public string? Uri { get { throw null; } set { } } public void AddTransform(System.Security.Cryptography.Xml.Transform transform) { } public virtual System.Xml.XmlElement GetXml() { throw null; } From 11dfa6d966e87bf59e3ff8c68cc1df3caf6ca91d Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 23 Jun 2022 19:56:07 +0100 Subject: [PATCH 41/67] PR feedback --- .../Security/Cryptography/Xml/CanonicalXmlCDataSection.cs | 2 +- .../System/Security/Cryptography/Xml/CanonicalXmlDocument.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlCDataSection.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlCDataSection.cs index ff5d9d4529150..a80a5d7f54650 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlCDataSection.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlCDataSection.cs @@ -10,7 +10,7 @@ namespace System.Security.Cryptography.Xml internal sealed class CanonicalXmlCDataSection : XmlCDataSection, ICanonicalizableNode { private bool _isInNodeSet; - public CanonicalXmlCDataSection(string data, XmlDocument doc, bool defaultNodeSetInclusionState) : base(data, doc) + public CanonicalXmlCDataSection(string? data, XmlDocument doc, bool defaultNodeSetInclusionState) : base(data, doc) { _isInNodeSet = defaultNodeSetInclusionState; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index 8b085e9037e5a..800dbd64207ed 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -110,7 +110,7 @@ public override XmlEntityReference CreateEntityReference(string name) public override XmlCDataSection CreateCDataSection(string? data) { - return new CanonicalXmlCDataSection(data!, this, _defaultNodeSetInclusionState); + return new CanonicalXmlCDataSection(data, this, _defaultNodeSetInclusionState); } } } From 9e650512480c550fd4ff6525a1431375edd86953 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 23 Jun 2022 21:05:19 +0100 Subject: [PATCH 42/67] Fix issues from recent merge --- .../src/System/Security/Cryptography/Xml/EncryptedXml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index e17e44f10d167..48ea28d3db568 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -216,7 +216,7 @@ private byte[] GetCipherValue(CipherData cipherData) throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported); } inputStream = new MemoryStream(_encoding.GetBytes(idElem.OuterXml)); - string? baseUri = _document?.BaseURI; + string baseUri = _document.BaseURI; TransformChain tc = cipherData.CipherReference.TransformChain; if (tc == null) { From 7f0dcd09d845d42e7c46d84e4c8bfa3499184aef Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 23 Jun 2022 22:20:35 +0100 Subject: [PATCH 43/67] Remove some more extraneous damnit operators --- .../Security/Cryptography/Xml/CanonicalXmlDocument.cs | 2 +- .../src/System/Security/Cryptography/Xml/SignedXml.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index 800dbd64207ed..fe29c396c39f7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -65,7 +65,7 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace public override XmlElement CreateElement(string prefix, string localName, string namespaceURI) #pragma warning restore CS8765 { - return new CanonicalXmlElement(prefix!, localName, namespaceURI!, this, _defaultNodeSetInclusionState); + return new CanonicalXmlElement(prefix, localName, namespaceURI, this, _defaultNodeSetInclusionState); } public override XmlAttribute CreateAttribute(string? prefix, string localName, string? namespaceURI) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index 8a28b17473e78..88b8b10eacfbb 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -458,7 +458,7 @@ public void ComputeSignature(KeyedHashAlgorithm macAlg) SignedXmlDebugLog.LogSigning(this, hash); m_signature.SignatureValue = new byte[signatureLength / 8]; - Buffer.BlockCopy(hashValue!, 0, m_signature.SignatureValue, 0, signatureLength / 8); + Buffer.BlockCopy(hashValue, 0, m_signature.SignatureValue, 0, signatureLength / 8); } // @@ -663,7 +663,7 @@ private bool DoesSignatureUseTruncatedHmac() // of approved algorithm URIs. private bool DoesSignatureUseSafeCanonicalizationMethod() { - foreach (string safeAlgorithm in SafeCanonicalizationMethods!) + foreach (string safeAlgorithm in SafeCanonicalizationMethods) { if (string.Equals(safeAlgorithm, SignedInfo!.CanonicalizationMethod, StringComparison.OrdinalIgnoreCase)) { @@ -696,7 +696,7 @@ private bool ReferenceUsesSafeTransformMethods(Reference reference) private bool IsSafeTransform(string transformAlgorithm) { // All canonicalization algorithms are valid transform algorithms. - foreach (string safeAlgorithm in SafeCanonicalizationMethods!) + foreach (string safeAlgorithm in SafeCanonicalizationMethods) { if (string.Equals(safeAlgorithm, transformAlgorithm, StringComparison.OrdinalIgnoreCase)) { @@ -1028,7 +1028,7 @@ private bool CheckSignedInfo(AsymmetricAlgorithm key) asymmetricSignatureDeformatter, hashval, m_signature.SignatureValue); - return asymmetricSignatureDeformatter.VerifySignature(hashval!, m_signature.SignatureValue!); + return asymmetricSignatureDeformatter.VerifySignature(hashval, m_signature.SignatureValue!); } private bool CheckSignedInfo(KeyedHashAlgorithm macAlg) From 836f21c5a6d175a449fae6ed3d2c936fed34e0b6 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Fri, 8 Jul 2022 21:58:27 +0100 Subject: [PATCH 44/67] Fix NRT analysis errors after prior rebase --- .../src/System/Security/Cryptography/Xml/SignedXml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index 88b8b10eacfbb..d4c4e1df4e420 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -172,7 +172,7 @@ public AsymmetricAlgorithm? SigningKey public EncryptedXml EncryptedXml { - get => _exml ??= new EncryptedXml(_containingDocument); // default processing rules + get => _exml ??= new EncryptedXml(_containingDocument!); // default processing rules set => _exml = value; } From 943a5d65a354a8a942813670fa6cc6d39ea7b569 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Fri, 8 Jul 2022 22:18:58 +0100 Subject: [PATCH 45/67] PR feedback --- .../src/System.Security.Cryptography.Xml.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj b/src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj index a4ece4da5d8cc..8694a0575e8b7 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj +++ b/src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj @@ -2,10 +2,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) true - disable - $(NoWarn);nullable $(NoWarn);CA1850 - enable true Provides classes to support the creation and validation of XML digital signatures. The classes in this namespace implement the World Wide Web Consortium Recommendation, "XML-Signature Syntax and Processing", described at http://www.w3.org/TR/xmldsig-core/. From f3a4984c6ecd05442b8b8a982b68c904e1c685ed Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Fri, 8 Jul 2022 22:37:34 +0100 Subject: [PATCH 46/67] PR feedback --- .../Security/Cryptography/Xml/CanonicalXmlDocument.cs | 4 +--- .../System/Security/Cryptography/Xml/CanonicalXmlElement.cs | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index fe29c396c39f7..f469040316407 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -61,9 +61,7 @@ public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespace } } -#pragma warning disable CS8765 // Nullability doesn't match overriden member - public override XmlElement CreateElement(string prefix, string localName, string namespaceURI) -#pragma warning restore CS8765 + public override XmlElement CreateElement(string? prefix, string localName, string? namespaceURI) { return new CanonicalXmlElement(prefix, localName, namespaceURI, this, _defaultNodeSetInclusionState); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index a80e0a12948f7..8ff7c13ad1d72 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -12,8 +12,10 @@ internal sealed class CanonicalXmlElement : XmlElement, ICanonicalizableNode { private bool _isInNodeSet; - public CanonicalXmlElement(string prefix, string localName, string namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState) - : base(prefix, localName, namespaceURI, doc) + // The ! on prefix is because System.Xml has a bug where the parameter is not null when it should be. + // We can remove this when we fix the associated bug filed against System.Xml: https://github.com/dotnet/runtime/issues/71858 + public CanonicalXmlElement(string? prefix, string localName, string? namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState) + : base(prefix!, localName, namespaceURI, doc) { _isInNodeSet = defaultNodeSetInclusionState; } From 11fac03cdf58684b5d22bdb7990c07f82e70d4fe Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Fri, 8 Jul 2022 23:06:04 +0100 Subject: [PATCH 47/67] PR feedback --- .../src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs index 01b8c9014bcfc..af36db1a1b7e1 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlComment.cs @@ -12,7 +12,7 @@ internal sealed class CanonicalXmlComment : XmlComment, ICanonicalizableNode private bool _isInNodeSet; private readonly bool _includeComments; - public CanonicalXmlComment(string comment, XmlDocument doc, bool defaultNodeSetInclusionState, bool includeComments) + public CanonicalXmlComment(string? comment, XmlDocument doc, bool defaultNodeSetInclusionState, bool includeComments) : base(comment, doc) { _isInNodeSet = defaultNodeSetInclusionState; From 321e958392b00baa88003899d8f1f639331181e1 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Fri, 8 Jul 2022 23:08:30 +0100 Subject: [PATCH 48/67] PR feedback --- .../System/Security/Cryptography/Xml/CanonicalXmlDocument.cs | 2 +- .../src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index f469040316407..0001cc58999b2 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -98,7 +98,7 @@ public override XmlProcessingInstruction CreateProcessingInstruction(string targ public override XmlComment CreateComment(string? data) { - return new CanonicalXmlComment(data!, this, _defaultNodeSetInclusionState, _includeComments); + return new CanonicalXmlComment(data, this, _defaultNodeSetInclusionState, _includeComments); } public override XmlEntityReference CreateEntityReference(string name) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index 8ff7c13ad1d72..d65dfc6be9ae5 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -37,7 +37,7 @@ public void Write(StringBuilder strBuilder, DocPosition docPos, AncestralNamespa { foreach (XmlAttribute attr in attrList) { - if ((((CanonicalXmlAttribute)attr)).IsInNodeSet || Utils.IsNamespaceNode(attr) || Utils.IsXmlNamespaceNode(attr)) + if (((CanonicalXmlAttribute)attr).IsInNodeSet || Utils.IsNamespaceNode(attr) || Utils.IsXmlNamespaceNode(attr)) { if (Utils.IsNamespaceNode(attr)) { From 9418cf3d666c87344935ebad081bb3cc62dfc0e5 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Fri, 8 Jul 2022 23:23:27 +0100 Subject: [PATCH 49/67] PR feedback --- .../ref/System.Security.Cryptography.Xml.cs | 3 +++ .../src/System/Security/Cryptography/Xml/EncryptedKey.cs | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 9537d39bbc4a4..207d0fd5c17e2 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -4,6 +4,8 @@ // Changes to this file must follow the https://aka.ms/api-review process. // ------------------------------------------------------------------------------ +using System.Diagnostics.CodeAnalysis; + namespace System.Security.Cryptography.Xml { public sealed partial class CipherData @@ -61,6 +63,7 @@ public sealed partial class EncryptedKey : System.Security.Cryptography.Xml.Encr { public EncryptedKey() { } public string? CarriedKeyName { get { throw null; } set { } } + [AllowNull] public string? Recipient { get { throw null; } set { } } public System.Security.Cryptography.Xml.ReferenceList ReferenceList { get { throw null; } } public void AddReference(System.Security.Cryptography.Xml.DataReference dataReference) { } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs index e0cac61aa0ec2..2b3f9e02ac3f6 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml @@ -13,7 +14,8 @@ public sealed class EncryptedKey : EncryptedType public EncryptedKey() { } - public string? Recipient + [AllowNull] + public string Recipient { get => _recipient ??= string.Empty; // an unspecified value for an XmlAttribute is string.Empty set From 3324d39f99a4378975c4c1343c3fe992a2d07985 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 9 Jul 2022 06:20:29 +0100 Subject: [PATCH 50/67] CipherReference and CipherValue made to disallow null --- .../ref/System.Security.Cryptography.Xml.cs | 2 ++ .../src/System/Security/Cryptography/Xml/CipherData.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 207d0fd5c17e2..64ae781cb663a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -13,7 +13,9 @@ public sealed partial class CipherData public CipherData() { } public CipherData(byte[] cipherValue) { } public CipherData(System.Security.Cryptography.Xml.CipherReference cipherReference) { } + [System.Diagnostics.CodeAnalysis.DisallowNullAttribute] public System.Security.Cryptography.Xml.CipherReference? CipherReference { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.DisallowNullAttribute] public byte[]? CipherValue { get { throw null; } set { } } public System.Xml.XmlElement GetXml() { throw null; } public void LoadXml(System.Xml.XmlElement value) { } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs index 00705eb664f08..9b1dfb2d6aa01 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs @@ -33,6 +33,7 @@ private bool CacheValid } } + [DisallowNull] public CipherReference? CipherReference { get { return _cipherReference; } @@ -48,6 +49,7 @@ public CipherReference? CipherReference } } + [DisallowNull] public byte[]? CipherValue { get { return _cipherValue; } From e4af7519603f5e95405d095f5d00aa2ad3db8764 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 9 Jul 2022 06:43:45 +0100 Subject: [PATCH 51/67] Make Uri non-nullable on EnctrypedReference --- .../ref/System.Security.Cryptography.Xml.cs | 3 +-- .../System/Security/Cryptography/Xml/EncryptedReference.cs | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 64ae781cb663a..b724d309860d5 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -81,8 +81,7 @@ protected EncryptedReference(string uri, System.Security.Cryptography.Xml.Transf protected internal bool CacheValid { get { throw null; } } protected string? ReferenceType { get { throw null; } set { } } public System.Security.Cryptography.Xml.TransformChain TransformChain { get { throw null; } set { } } - [System.Diagnostics.CodeAnalysis.DisallowNullAttribute] - public string? Uri { get { throw null; } set { } } + public string Uri { get { throw null; } set { } } public void AddTransform(System.Security.Cryptography.Xml.Transform transform) { } public virtual System.Xml.XmlElement GetXml() { throw null; } public virtual void LoadXml(System.Xml.XmlElement value) { } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index 6b33ee701e8a7..86d15e2696e08 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -8,7 +8,7 @@ namespace System.Security.Cryptography.Xml { public abstract class EncryptedReference { - private string? _uri; + private string _uri = null!; private string? _referenceType; private TransformChain? _transformChain; internal XmlElement? _cachedXml; @@ -28,8 +28,7 @@ protected EncryptedReference(string uri, TransformChain transformChain) _cachedXml = null; } - [DisallowNull] - public string? Uri + public string Uri { get { return _uri; } set From 2642850a157b2659a5ff450d89defc0e5fb3ddd9 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 9 Jul 2022 06:47:52 +0100 Subject: [PATCH 52/67] Add AllowNull attribute on EncryptedType::KeyInfo --- .../ref/System.Security.Cryptography.Xml.cs | 1 + .../src/System/Security/Cryptography/Xml/EncryptedType.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index b724d309860d5..33a25007689b9 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -94,6 +94,7 @@ protected EncryptedType() { } public virtual System.Security.Cryptography.Xml.EncryptionMethod? EncryptionMethod { get { throw null; } set { } } public virtual System.Security.Cryptography.Xml.EncryptionPropertyCollection EncryptionProperties { get { throw null; } } public virtual string? Id { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public System.Security.Cryptography.Xml.KeyInfo KeyInfo { get { throw null; } set { } } public virtual string? MimeType { get { throw null; } set { } } public virtual string? Type { get { throw null; } set { } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs index bd393897519c2..e32f63ec067f5 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs @@ -67,6 +67,7 @@ public virtual string? Encoding } } + [AllowNull] public KeyInfo KeyInfo { get => _keyInfo ??= new KeyInfo(); From 98a657224cd5b5886da5b3ab473538c5aaf39976 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 9 Jul 2022 06:51:43 +0100 Subject: [PATCH 53/67] AllowNull on EncryptedXml::Recipient --- .../ref/System.Security.Cryptography.Xml.cs | 5 ++--- .../src/System/Security/Cryptography/Xml/EncryptedXml.cs | 2 ++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 33a25007689b9..f5891e577d10d 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -4,8 +4,6 @@ // Changes to this file must follow the https://aka.ms/api-review process. // ------------------------------------------------------------------------------ -using System.Diagnostics.CodeAnalysis; - namespace System.Security.Cryptography.Xml { public sealed partial class CipherData @@ -65,7 +63,7 @@ public sealed partial class EncryptedKey : System.Security.Cryptography.Xml.Encr { public EncryptedKey() { } public string? CarriedKeyName { get { throw null; } set { } } - [AllowNull] + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string? Recipient { get { throw null; } set { } } public System.Security.Cryptography.Xml.ReferenceList ReferenceList { get { throw null; } } public void AddReference(System.Security.Cryptography.Xml.DataReference dataReference) { } @@ -128,6 +126,7 @@ public EncryptedXml(System.Xml.XmlDocument document, System.Security.Policy.Evid public System.Text.Encoding Encoding { get { throw null; } set { } } public System.Security.Cryptography.CipherMode Mode { get { throw null; } set { } } public System.Security.Cryptography.PaddingMode Padding { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Recipient { get { throw null; } set { } } public System.Xml.XmlResolver? Resolver { get { throw null; } set { } } public int XmlDSigSearchDepth { get { throw null; } set { } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 48ea28d3db568..133bee35edf6e 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Security.Cryptography.X509Certificates; using System.Security.Policy; @@ -162,6 +163,7 @@ public Encoding Encoding // This is used to specify the EncryptedKey elements that should be considered // when an EncryptedData references an EncryptedKey using a CarriedKeyName and Recipient + [AllowNull] public string Recipient { get => _recipient ??= string.Empty; // an unspecified value for an XmlAttribute is string.Empty From 9c033c71bedcc7df9e6a31baf15e039f9ba6dc8c Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 9 Jul 2022 07:01:19 +0100 Subject: [PATCH 54/67] Made baseUri nullable --- .../src/System/Security/Cryptography/Xml/EncryptedXml.cs | 2 +- .../src/System/Security/Cryptography/Xml/TransformChain.cs | 4 ++-- .../src/System/Security/Cryptography/Xml/Utils.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 133bee35edf6e..7842e9a7a1bac 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -200,7 +200,7 @@ private byte[] GetCipherValue(CipherData cipherData) if (cipherData.CipherReference.Uri.Length == 0) { // self referenced Uri - string baseUri = _document?.BaseURI!; + string? baseUri = _document?.BaseURI; TransformChain tc = cipherData.CipherReference.TransformChain; if (tc == null) { diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index 8cbbbf47ec647..5db5f72de9c39 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -58,7 +58,7 @@ public Transform this[int index] // The goal behind this method is to pump the input stream through the transforms and get back something that // can be hashed - internal Stream? TransformToOctetStream(object? inputObject, Type inputType, XmlResolver? resolver, string baseUri) + internal Stream? TransformToOctetStream(object? inputObject, Type inputType, XmlResolver? resolver, string? baseUri) { object? currentInput = inputObject; foreach (Transform transform in _transforms) @@ -155,7 +155,7 @@ public Transform this[int index] return TransformToOctetStream(input, typeof(Stream), resolver, baseUri); } - internal Stream? TransformToOctetStream(XmlDocument? document, XmlResolver? resolver, string baseUri) + internal Stream? TransformToOctetStream(XmlDocument? document, XmlResolver? resolver, string? baseUri) { return TransformToOctetStream(document, typeof(XmlDocument), resolver, baseUri); } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 8ad2f72b613d5..2cc356798318f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -183,7 +183,7 @@ internal static void SBReplaceCharWithString(StringBuilder sb, char oldChar, str } } - internal static XmlReader PreProcessStreamInput(Stream inputStream, XmlResolver? xmlResolver, string baseUri) + internal static XmlReader PreProcessStreamInput(Stream inputStream, XmlResolver? xmlResolver, string? baseUri) { XmlReaderSettings settings = GetSecureXmlReaderSettings(xmlResolver); XmlReader reader = XmlReader.Create(inputStream, settings, baseUri); From 0ce398fca1066b719d4720ec10269ba4382d1746 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 9 Jul 2022 07:02:55 +0100 Subject: [PATCH 55/67] DisallowNull on PropertlyElement --- .../ref/System.Security.Cryptography.Xml.cs | 1 + .../src/System/Security/Cryptography/Xml/EncryptionProperty.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index f5891e577d10d..f54932536494f 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -163,6 +163,7 @@ public sealed partial class EncryptionProperty public EncryptionProperty() { } public EncryptionProperty(System.Xml.XmlElement elementProperty) { } public string? Id { get { throw null; } } + [System.Diagnostics.CodeAnalysis.DisallowNullAttribute] public System.Xml.XmlElement? PropertyElement { get { throw null; } set { } } public string? Target { get { throw null; } } public System.Xml.XmlElement GetXml() { throw null; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs index a1c67daac4e5c..73fc558402c51 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs @@ -40,6 +40,7 @@ public string? Target get { return _target; } } + [DisallowNull] public XmlElement? PropertyElement { get { return _elemProp; } From fe63a28a6761f3068cb7c8888aca27350abc236e Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 9 Jul 2022 07:09:12 +0100 Subject: [PATCH 56/67] AllowNull on SignedInfo::CanonicalizationMethod --- .../ref/System.Security.Cryptography.Xml.cs | 1 + .../src/System/Security/Cryptography/Xml/SignedInfo.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index f54932536494f..6173415bdcc4a 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -337,6 +337,7 @@ public void LoadXml(System.Xml.XmlElement value) { } public partial class SignedInfo : System.Collections.ICollection, System.Collections.IEnumerable { public SignedInfo() { } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string CanonicalizationMethod { get { throw null; } set { } } public System.Security.Cryptography.Xml.Transform CanonicalizationMethodObject { get { throw null; } } public int Count { get { throw null; } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs index 450740711496d..8941e4635b62b 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedInfo.cs @@ -73,6 +73,7 @@ public string? Id } } + [AllowNull] public string CanonicalizationMethod { get From 03f84527280f278ce7867239357c4483793a8e79 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 9 Jul 2022 07:11:30 +0100 Subject: [PATCH 57/67] AllowNull on SignedXml::EncryptedXml --- .../ref/System.Security.Cryptography.Xml.cs | 1 + .../src/System/Security/Cryptography/Xml/SignedXml.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 6173415bdcc4a..b9a2cf00fa055 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -385,6 +385,7 @@ public partial class SignedXml public SignedXml() { } public SignedXml(System.Xml.XmlDocument document) { } public SignedXml(System.Xml.XmlElement elem) { } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public System.Security.Cryptography.Xml.EncryptedXml EncryptedXml { get { throw null; } set { } } public System.Security.Cryptography.Xml.KeyInfo KeyInfo { get { throw null; } set { } } public System.Xml.XmlResolver Resolver { set { } } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index d4c4e1df4e420..4201299aca2ff 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -170,6 +170,7 @@ public AsymmetricAlgorithm? SigningKey set { _signingKey = value; } } + [AllowNull] public EncryptedXml EncryptedXml { get => _exml ??= new EncryptedXml(_containingDocument!); // default processing rules From 2866e92613098e8f0421b1710761d9a02c708042 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Mon, 11 Jul 2022 06:53:24 +0100 Subject: [PATCH 58/67] Fix build after recent rebase --- .../src/System/Security/Cryptography/Xml/EncryptedXml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 7842e9a7a1bac..6d58279c2fa40 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -238,7 +238,7 @@ private byte[] GetCipherValue(CipherData cipherData) cipherValue = ms.ToArray(); // Close the stream and return inputStream?.Close(); - decInputStream.Close(); + decInputStream!.Close(); } // cache the cipher value for Perf reasons in case we call this routine twice From 5a24eaec2f6c5deeebd3451c046373d7231643bb Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Mon, 11 Jul 2022 07:38:36 +0100 Subject: [PATCH 59/67] Remove redundant damnit operator now that https://github.com/dotnet/runtime/pull/71860 has been merged --- .../System/Security/Cryptography/Xml/CanonicalXmlElement.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index d65dfc6be9ae5..0ed3fe9b1f562 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -12,10 +12,8 @@ internal sealed class CanonicalXmlElement : XmlElement, ICanonicalizableNode { private bool _isInNodeSet; - // The ! on prefix is because System.Xml has a bug where the parameter is not null when it should be. - // We can remove this when we fix the associated bug filed against System.Xml: https://github.com/dotnet/runtime/issues/71858 public CanonicalXmlElement(string? prefix, string localName, string? namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState) - : base(prefix!, localName, namespaceURI, doc) + : base(prefix, localName, namespaceURI, doc) { _isInNodeSet = defaultNodeSetInclusionState; } From 8e00c0086746743735b935cb9ca6baea22dc415c Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Mon, 11 Jul 2022 21:01:09 +0100 Subject: [PATCH 60/67] PR feedback --- .../Security/Cryptography/Xml/EncryptedReference.cs | 4 +++- .../System/Security/Cryptography/Xml/TransformChain.cs | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs index 86d15e2696e08..04d6e3a0d1490 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs @@ -8,7 +8,7 @@ namespace System.Security.Cryptography.Xml { public abstract class EncryptedReference { - private string _uri = null!; + private string _uri; private string? _referenceType; private TransformChain? _transformChain; internal XmlElement? _cachedXml; @@ -31,6 +31,8 @@ protected EncryptedReference(string uri, TransformChain transformChain) public string Uri { get { return _uri; } + + [MemberNotNull(nameof(_uri))] set { if (value == null) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index 5db5f72de9c39..ecd99d7938de9 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -58,7 +58,7 @@ public Transform this[int index] // The goal behind this method is to pump the input stream through the transforms and get back something that // can be hashed - internal Stream? TransformToOctetStream(object? inputObject, Type inputType, XmlResolver? resolver, string? baseUri) + internal Stream TransformToOctetStream(object? inputObject, Type inputType, XmlResolver? resolver, string? baseUri) { object? currentInput = inputObject; foreach (Transform transform in _transforms) @@ -133,7 +133,7 @@ public Transform this[int index] // Final processing, either we already have a stream or have to canonicalize if (currentInput is Stream) { - return currentInput as Stream; + return (Stream)currentInput; } if (currentInput is XmlNodeList) { @@ -150,12 +150,12 @@ public Transform this[int index] throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType); } - internal Stream? TransformToOctetStream(Stream? input, XmlResolver? resolver, string baseUri) + internal Stream TransformToOctetStream(Stream? input, XmlResolver? resolver, string baseUri) { return TransformToOctetStream(input, typeof(Stream), resolver, baseUri); } - internal Stream? TransformToOctetStream(XmlDocument? document, XmlResolver? resolver, string? baseUri) + internal Stream TransformToOctetStream(XmlDocument? document, XmlResolver? resolver, string? baseUri) { return TransformToOctetStream(document, typeof(XmlDocument), resolver, baseUri); } From c3bbe9ef815b720f89e7042be2245be93878fb3a Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Mon, 11 Jul 2022 21:26:47 +0100 Subject: [PATCH 61/67] Fix build due to change of the CanonicalXmlElement constructor --- .../System/Security/Cryptography/Xml/CanonicalXmlElement.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index 0ed3fe9b1f562..b90ecc2e1dcc0 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -13,7 +13,11 @@ internal sealed class CanonicalXmlElement : XmlElement, ICanonicalizableNode private bool _isInNodeSet; public CanonicalXmlElement(string? prefix, string localName, string? namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState) +#if NET7_0_OR_GREATER : base(prefix, localName, namespaceURI, doc) +#else + : base(prefix!, localName, namespaceURI, doc) +#endif { _isInNodeSet = defaultNodeSetInclusionState; } From 7b1ca68a1f016b02d61370eebf59d6c69903f566 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Mon, 11 Jul 2022 21:30:26 +0100 Subject: [PATCH 62/67] PR feedback --- .../src/System/Security/Cryptography/Xml/TransformChain.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs index ecd99d7938de9..bec0ab08e3fad 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs @@ -131,9 +131,9 @@ internal Stream TransformToOctetStream(object? inputObject, Type inputType, XmlR } // Final processing, either we already have a stream or have to canonicalize - if (currentInput is Stream) + if (currentInput is Stream inputStream) { - return (Stream)currentInput; + return inputStream; } if (currentInput is XmlNodeList) { From f47ad418bdba3f974f635a81bcb0e465d8fea68a Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Tue, 12 Jul 2022 19:51:44 +0100 Subject: [PATCH 63/67] Remove redundant damnit operators --- .../src/System/Security/Cryptography/Xml/EncryptedXml.cs | 4 ++-- .../src/System/Security/Cryptography/Xml/SignedXml.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs index 6d58279c2fa40..1b769ebe3f687 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs @@ -234,11 +234,11 @@ private byte[] GetCipherValue(CipherData cipherData) byte[]? cipherValue = null; using (MemoryStream ms = new MemoryStream()) { - Utils.Pump(decInputStream!, ms); + Utils.Pump(decInputStream, ms); cipherValue = ms.ToArray(); // Close the stream and return inputStream?.Close(); - decInputStream!.Close(); + decInputStream.Close(); } // cache the cipher value for Perf reasons in case we call this routine twice diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index 4201299aca2ff..d3ff5dadfe9be 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -779,9 +779,9 @@ private byte[] GetC14NDigest(HashAlgorithm hash) bool isKeyedHashAlgorithm = hash is KeyedHashAlgorithm; if (isKeyedHashAlgorithm || !_bCacheValid || !SignedInfo!.CacheValid) { - string? baseUri = _containingDocument?.BaseURI; + string? baseUri = _containingDocument!.BaseURI; XmlResolver? resolver = (_bResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); - XmlDocument doc = Utils.PreProcessElementInput(SignedInfo!.GetXml(), resolver!, baseUri!); + XmlDocument doc = Utils.PreProcessElementInput(SignedInfo!.GetXml(), resolver!, baseUri ); // Add non default namespaces in scope CanonicalXmlNodeList? namespaces = (_context == null ? null : Utils.GetPropagatedAttributes(_context)); From e952d69dcf114ea013942187c750be3f8fd20feb Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Wed, 20 Jul 2022 07:58:06 +0100 Subject: [PATCH 64/67] Fix broken tests --- .../src/System/Security/Cryptography/Xml/SignedXml.cs | 2 +- .../src/System/Security/Cryptography/Xml/Utils.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs index d3ff5dadfe9be..0a3a3510cb8ca 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXml.cs @@ -779,7 +779,7 @@ private byte[] GetC14NDigest(HashAlgorithm hash) bool isKeyedHashAlgorithm = hash is KeyedHashAlgorithm; if (isKeyedHashAlgorithm || !_bCacheValid || !SignedInfo!.CacheValid) { - string? baseUri = _containingDocument!.BaseURI; + string? baseUri = _containingDocument?.BaseURI; XmlResolver? resolver = (_bResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver()); XmlDocument doc = Utils.PreProcessElementInput(SignedInfo!.GetXml(), resolver!, baseUri ); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 2cc356798318f..634e30e499087 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -224,7 +224,7 @@ internal static XmlDocument PreProcessDocumentInput(XmlDocument document, XmlRes return doc; } - internal static XmlDocument PreProcessElementInput(XmlElement elem, XmlResolver xmlResolver, string baseUri) + internal static XmlDocument PreProcessElementInput(XmlElement elem, XmlResolver xmlResolver, string? baseUri) { if (elem is null) { From 32bb40d7e937340b5419e25fbbbbe3889c1ffb40 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 3 Sep 2022 06:19:34 +0100 Subject: [PATCH 65/67] Fix issues after rebase --- .../System/Security/Cryptography/Xml/CanonicalXmlDocument.cs | 4 ++-- .../src/System/Security/Cryptography/Xml/Reference.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index 0001cc58999b2..82dae6e16454c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -91,9 +91,9 @@ public override XmlSignificantWhitespace CreateSignificantWhitespace(string? tex return new CanonicalXmlSignificantWhitespace(text!, this, _defaultNodeSetInclusionState); } - public override XmlProcessingInstruction CreateProcessingInstruction(string target, string data) + public override XmlProcessingInstruction CreateProcessingInstruction(string target, string? data) { - return new CanonicalXmlProcessingInstruction(target, data, this, _defaultNodeSetInclusionState); + return new CanonicalXmlProcessingInstruction(target, data!, this, _defaultNodeSetInclusionState); } public override XmlComment CreateComment(string? data) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs index 70ea342f9ae8a..801dae7f9e902 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs @@ -401,7 +401,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList // We should not discard comments here!!! resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); - hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessDocumentInput(document, resolver!, baseUri), resolver , baseUri); + hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessDocumentInput(document, resolver!, baseUri), resolver, baseUri); break; } @@ -457,7 +457,7 @@ internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList case ReferenceTargetType.XmlElement: // We need to create a DocumentNavigator out of the XmlElement resolver = (SignedXml!.ResolverSet ? SignedXml._xmlResolver : XmlResolverHelper.GetThrowingResolver()); - hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessElementInput((XmlElement)_refTarget!, resolver!, baseUri), resolver , baseUri); + hashInputStream = TransformChain.TransformToOctetStream(Utils.PreProcessElementInput((XmlElement)_refTarget!, resolver!, baseUri), resolver, baseUri); break; default: throw new CryptographicException(SR.Cryptography_Xml_UriNotResolved, _uri); From 9fda330c05735e9702105f8948d28e5845ef3eba Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 3 Sep 2022 07:27:15 +0100 Subject: [PATCH 66/67] PR feedback --- .../ref/System.Security.Cryptography.Xml.cs | 2 +- .../Security/Cryptography/Xml/CanonicalXmlDocument.cs | 4 ++-- .../Security/Cryptography/Xml/CanonicalXmlElement.cs | 4 ---- .../Cryptography/Xml/CanonicalXmlSignificantWhitespace.cs | 2 +- .../Security/Cryptography/Xml/CanonicalXmlWhitespace.cs | 2 +- .../src/System/Security/Cryptography/Xml/DSAKeyValue.cs | 8 ++++---- 6 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index b9a2cf00fa055..123cf370a3597 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -48,7 +48,7 @@ public partial class DSAKeyValue : System.Security.Cryptography.Xml.KeyInfoClaus [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] public DSAKeyValue() { } - public DSAKeyValue(System.Security.Cryptography.DSA? key) { } + public DSAKeyValue(System.Security.Cryptography.DSA key) { } public System.Security.Cryptography.DSA? Key { get { throw null; } set { } } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs index 82dae6e16454c..da8481cd7597c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlDocument.cs @@ -83,12 +83,12 @@ public override XmlText CreateTextNode(string? text) public override XmlWhitespace CreateWhitespace(string? prefix) { - return new CanonicalXmlWhitespace(prefix!, this, _defaultNodeSetInclusionState); + return new CanonicalXmlWhitespace(prefix, this, _defaultNodeSetInclusionState); } public override XmlSignificantWhitespace CreateSignificantWhitespace(string? text) { - return new CanonicalXmlSignificantWhitespace(text!, this, _defaultNodeSetInclusionState); + return new CanonicalXmlSignificantWhitespace(text, this, _defaultNodeSetInclusionState); } public override XmlProcessingInstruction CreateProcessingInstruction(string target, string? data) diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs index b90ecc2e1dcc0..71cb45868b0a9 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlElement.cs @@ -13,11 +13,7 @@ internal sealed class CanonicalXmlElement : XmlElement, ICanonicalizableNode private bool _isInNodeSet; public CanonicalXmlElement(string? prefix, string localName, string? namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState) -#if NET7_0_OR_GREATER - : base(prefix, localName, namespaceURI, doc) -#else : base(prefix!, localName, namespaceURI, doc) -#endif { _isInNodeSet = defaultNodeSetInclusionState; } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlSignificantWhitespace.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlSignificantWhitespace.cs index cd4ec4820e247..2d3e8066b18fb 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlSignificantWhitespace.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlSignificantWhitespace.cs @@ -11,7 +11,7 @@ internal sealed class CanonicalXmlSignificantWhitespace : XmlSignificantWhitespa { private bool _isInNodeSet; - public CanonicalXmlSignificantWhitespace(string strData, XmlDocument doc, bool defaultNodeSetInclusionState) + public CanonicalXmlSignificantWhitespace(string? strData, XmlDocument doc, bool defaultNodeSetInclusionState) : base(strData, doc) { _isInNodeSet = defaultNodeSetInclusionState; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlWhitespace.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlWhitespace.cs index 38fae307ed4dc..9871fac19b4ea 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlWhitespace.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlWhitespace.cs @@ -11,7 +11,7 @@ internal sealed class CanonicalXmlWhitespace : XmlWhitespace, ICanonicalizableNo { private bool _isInNodeSet; - public CanonicalXmlWhitespace(string strData, XmlDocument doc, bool defaultNodeSetInclusionState) + public CanonicalXmlWhitespace(string? strData, XmlDocument doc, bool defaultNodeSetInclusionState) : base(strData, doc) { _isInNodeSet = defaultNodeSetInclusionState; diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs index b419031f68cad..43e7328754edb 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs @@ -10,7 +10,7 @@ namespace System.Security.Cryptography.Xml { public class DSAKeyValue : KeyInfoClause { - private DSA? _key; + private DSA _key; // // public constructors @@ -23,7 +23,7 @@ public DSAKeyValue() _key = DSA.Create(); } - public DSAKeyValue(DSA? key) + public DSAKeyValue(DSA key) { _key = key; } @@ -32,7 +32,7 @@ public DSAKeyValue(DSA? key) // public properties // - public DSA? Key + public DSA Key { get { return _key; } set { _key = value; } @@ -78,7 +78,7 @@ public override XmlElement GetXml() internal override XmlElement GetXml(XmlDocument xmlDocument) { - DSAParameters dsaParams = _key!.ExportParameters(false); + DSAParameters dsaParams = _key.ExportParameters(false); XmlElement keyValueElement = xmlDocument.CreateElement(KeyValueElementName, SignedXml.XmlDsigNamespaceUrl); XmlElement dsaKeyValueElement = xmlDocument.CreateElement(DSAKeyValueElementName, SignedXml.XmlDsigNamespaceUrl); From e768859595faa864df5419b99af74310ad1f5268 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Tue, 13 Sep 2022 20:50:57 +0100 Subject: [PATCH 67/67] PR feedback --- .../ref/System.Security.Cryptography.Xml.cs | 4 ++-- .../src/System/Security/Cryptography/Xml/DSAKeyValue.cs | 2 +- .../System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs | 2 +- .../Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs index 123cf370a3597..d53d23b1c55c1 100644 --- a/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs +++ b/src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs @@ -49,7 +49,7 @@ public partial class DSAKeyValue : System.Security.Cryptography.Xml.KeyInfoClaus [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] public DSAKeyValue() { } public DSAKeyValue(System.Security.Cryptography.DSA key) { } - public System.Security.Cryptography.DSA? Key { get { throw null; } set { } } + public System.Security.Cryptography.DSA Key { get { throw null; } set { } } public override System.Xml.XmlElement GetXml() { throw null; } public override void LoadXml(System.Xml.XmlElement value) { } } @@ -64,7 +64,7 @@ public sealed partial class EncryptedKey : System.Security.Cryptography.Xml.Encr public EncryptedKey() { } public string? CarriedKeyName { get { throw null; } set { } } [System.Diagnostics.CodeAnalysis.AllowNullAttribute] - public string? Recipient { get { throw null; } set { } } + public string Recipient { get { throw null; } set { } } public System.Security.Cryptography.Xml.ReferenceList ReferenceList { get { throw null; } } public void AddReference(System.Security.Cryptography.Xml.DataReference dataReference) { } public void AddReference(System.Security.Cryptography.Xml.KeyReference keyReference) { } diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs index 43e7328754edb..19d2f73817c67 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs @@ -182,7 +182,7 @@ public override void LoadXml(XmlElement value) try { - Key!.ImportParameters(new DSAParameters + Key.ImportParameters(new DSAParameters { P = (pNode != null) ? Convert.FromBase64String(pNode.InnerText) : null, Q = (qNode != null) ? Convert.FromBase64String(qNode.InnerText) : null, diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs index e50a538cd4513..ea087bdb0f72c 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigC14NTransform.cs @@ -47,7 +47,7 @@ public override void LoadInnerXml(XmlNodeList nodeList) public override void LoadInput(object obj) { - XmlResolver resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver())!; + XmlResolver resolver = ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver(); if (obj is Stream) { _cXml = new CanonicalXml((Stream)obj, _includeComments, resolver, BaseURI!); diff --git a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs index ac75d5141cd1d..f9c7594c30dad 100644 --- a/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs +++ b/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigEnvelopedSignatureTransform.cs @@ -79,7 +79,7 @@ private void LoadStreamInput(Stream stream) { XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; - XmlResolver resolver = ((ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver())! ); + XmlResolver resolver = ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver(); XmlReader xmlReader = Utils.PreProcessStreamInput(stream, resolver, BaseURI!); doc.Load(xmlReader); _containingDocument = doc;