/// <summary> /// Reads XML conforming to https://www.w3.org/TR/2001/PR-xmldsig-core-20010820/#sec-Transforms /// </summary> /// <param name="reader">a <see cref="XmlReader"/>positioned on a <Transforms> element.</param> /// <param name="reference">a <see cref="Reference"/> to attach transforms.</param> /// <exception cref="ArgumentNullException">if <paramref name="reader"/> is null.</exception> /// <exception cref="ArgumentNullException">if <paramref name="reference"/> is null.</exception> /// <exception cref="XmlReadException">if there is a problem reading the XML.</exception> public virtual void ReadTransforms(XmlReader reader, Reference reference) { if (reader == null) { throw LogArgumentNullException(nameof(reader)); } if (reference == null) { throw LogArgumentNullException(nameof(reference)); } try { // <Transforms> - optional if (!reader.IsStartElement(XmlSignatureConstants.Elements.Transforms, XmlSignatureConstants.Namespace)) { return; } if (reader.IsEmptyElement) { reader.Read(); return; } reader.Read(); // <Transform> - unbounded while (reader.IsStartElement(XmlSignatureConstants.Elements.Transform, XmlSignatureConstants.Namespace)) { var isEmptyElement = reader.IsEmptyElement; var algorithm = reader.GetAttribute(XmlSignatureConstants.Attributes.Algorithm); if (string.IsNullOrEmpty(algorithm)) { throw XmlUtil.LogReadException(LogMessages.IDX30105); } if (TransformFactory.IsSupportedTransform(algorithm)) { reference.Transforms.Add(TransformFactory.GetTransform(algorithm)); reader.Read(); } else if (TransformFactory.IsSupportedCanonicalizingTransfrom(algorithm)) { reference.CanonicalizingTransfrom = TransformFactory.GetCanonicalizingTransform(algorithm); reader.Read(); // release 5.2.1 did not require 'ec' ns. So, we need to accept names with and without a prefix. if (reader.IsStartElement(XmlSignatureConstants.Elements.InclusiveNamespaces, XmlSignatureConstants.ExclusiveC14nNamespace) || reader.IsStartElement(XmlSignatureConstants.Elements.InclusiveNamespaces)) { bool isOnEmptyElement = reader.IsEmptyElement; reference.CanonicalizingTransfrom.InclusiveNamespacesPrefixList = reader.GetAttribute(XmlSignatureConstants.Attributes.PrefixList); reader.ReadStartElement(); if (!isOnEmptyElement) { reader.ReadEndElement(); } } } else { throw XmlUtil.LogReadException(LogMessages.IDX30210, algorithm); } reader.MoveToContent(); if (!isEmptyElement) { reader.ReadEndElement(); } } // </ Transforms> reader.MoveToContent(); reader.ReadEndElement(); } catch (Exception ex) { if (ex is XmlReadException) { throw; } throw XmlUtil.LogReadException(LogMessages.IDX30016, ex, XmlSignatureConstants.Elements.Transforms); } }
/// <summary> /// Static constructor that initializes the default <see cref="TransformFactory"/>. /// </summary> static TransformFactory() { Default = new TransformFactory(); }