Example #1
0
        private Stream ApplyTransform(Transform t, XmlDocument input)
        {
            // These transformer modify input document, which should
            // not affect to the input itself.
            if (t is XmlDsigXPathTransform ||
                t is XmlDsigEnvelopedSignatureTransform
#if NET_2_0
                || t is XmlDecryptionTransform
#endif
                )
            {
                input = (XmlDocument)input.Clone();
            }

            t.LoadInput(input);

            if (t is XmlDsigEnvelopedSignatureTransform)
            {
                // It returns XmlDocument for XmlDocument input.
                return(CanonicalizeOutput(t.GetOutput()));
            }

            object obj = t.GetOutput();
            if (obj is Stream)
            {
                return((Stream)obj);
            }
            else if (obj is XmlDocument)
            {
                MemoryStream  ms  = new MemoryStream();
                XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
                ((XmlDocument)obj).WriteTo(xtw);

                xtw.Flush();

                // Rewind to the start of the stream
                ms.Position = 0;
                return(ms);
            }
            else if (obj == null)
            {
                throw new NotImplementedException("This should not occur. Transform is " + t + ".");
            }
            else
            {
                // e.g. XmlDsigXPathTransform returns XmlNodeList
                return(CanonicalizeOutput(obj));
            }
        }
Example #2
0
        private Stream CanonicalizeOutput(object obj)
        {
            Transform c14n = GetC14NMethod();

            c14n.LoadInput(obj);
            return((Stream)c14n.GetOutput());
        }
Example #3
0
        private Stream ApplyTransform(System.Security.Cryptography.Xml.Transform t, XmlDocument doc)
        {
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(doc.OuterXml);

            using (MemoryStream ms = new MemoryStream(buffer))
            {
                t.LoadInput(ms);
                return((MemoryStream)t.GetOutput(typeof(Stream)));
            }
        }
Example #4
0
 internal static void LogCanonicalizedOutput(SignedXml signedXml, Transform canonicalizationTransform)
 {
     if (VerboseLoggingEnabled)
     {
         using (StreamReader reader = new StreamReader(canonicalizationTransform.GetOutput(typeof(Stream)) as Stream))
         {
             string data = string.Format(CultureInfo.InvariantCulture, SecurityResources.GetResourceString("Log_CanonicalizedOutput"), new object[] { reader.ReadToEnd() });
             WriteLine(signedXml, TraceEventType.Verbose, SignedXmlDebugEvent.CanonicalizedData, data);
         }
     }
 }
Example #5
0
 public static byte[] ApplyTransform(XmlElement element, System.Security.Cryptography.Xml.Transform transform)
 {
     byte[] bytes = Encoding.UTF8.GetBytes(element.OuterXml);
     using (MemoryStream obj = new MemoryStream(bytes))
     {
         transform.LoadInput(obj);
         using (MemoryStream memoryStream = (MemoryStream)transform.GetOutput(typeof(Stream)))
         {
             return(memoryStream.ToArray());
         }
     }
 }
Example #6
0
 private Stream ApplyTransform(System.Security.Cryptography.Xml.Transform t, Stream s)
 {
     try
     {
         t.LoadInput(s);
         s = (Stream)t.GetOutput();
     }
     catch (Exception e)
     {
         string temp = e.ToString();
     }
     return(s);
 }
Example #7
0
        /// <summary>
        ///     Log the canonicalized data
        /// </summary>
        /// <param name="signedXml">SignedXml object doing the signing or verification</param>
        /// <param name="canonicalizationTransform">transform canonicalizing the input</param>
        internal static void LogCanonicalizedOutput(SignedXml signedXml, Transform canonicalizationTransform)
        {
            Debug.Assert(signedXml != null, "signedXml != null");
            Debug.Assert(canonicalizationTransform != null, "canonicalizationTransform != null");

            if (VerboseLoggingEnabled)
            {
                using (StreamReader reader = new StreamReader(canonicalizationTransform.GetOutput(typeof(Stream)) as Stream)) {
                    string logMessage = String.Format(CultureInfo.InvariantCulture,
                                                      SecurityResources.GetResourceString("Log_CanonicalizedOutput"),
                                                      reader.ReadToEnd());
                    WriteLine(signedXml,
                              TraceEventType.Verbose,
                              SignedXmlDebugEvent.CanonicalizedData,
                              logMessage);
                }
            }
        }
Example #8
0
		private Stream ApplyTransform (Transform t, XmlDocument input) 
		{
			// These transformer modify input document, which should
			// not affect to the input itself.
			if (t is XmlDsigXPathTransform 
				|| t is XmlDsigEnvelopedSignatureTransform
				|| t is XmlDecryptionTransform
			)
				input = (XmlDocument) input.Clone ();

			t.LoadInput (input);

			if (t is XmlDsigEnvelopedSignatureTransform)
				// It returns XmlDocument for XmlDocument input.
				return CanonicalizeOutput (t.GetOutput ());

			object obj = t.GetOutput ();
			if (obj is Stream)
				return (Stream) obj;
			else if (obj is XmlDocument) {
				MemoryStream ms = new MemoryStream ();
				XmlTextWriter xtw = new XmlTextWriter (ms, Encoding.UTF8);
				((XmlDocument) obj).WriteTo (xtw);

				xtw.Flush ();

				// Rewind to the start of the stream
				ms.Position = 0;
				return ms;
			}
			else if (obj == null) {
				throw new NotImplementedException ("This should not occur. Transform is " + t + ".");
			}
			else {
				// e.g. XmlDsigXPathTransform returns XmlNodeList
				return CanonicalizeOutput (obj);
			}
		}
Example #9
0
        private Stream SignedInfoTransformed()
        {
            Transform t = GetC14NMethod();

            if (signatureElement == null)
            {
                // when creating signatures
                XmlDocument doc = new XmlDocument();
                doc.PreserveWhitespace = true;
                doc.LoadXml(m_signature.SignedInfo.GetXml().OuterXml);
                if (envdoc != null)
                {
                    foreach (XmlAttribute attr in envdoc.DocumentElement.SelectNodes("namespace::*"))
                    {
                        if (attr.LocalName == "xml")
                        {
                            continue;
                        }
                        if (attr.Prefix == doc.DocumentElement.Prefix)
                        {
                            continue;
                        }
                        doc.DocumentElement.SetAttributeNode(doc.ImportNode(attr, true) as XmlAttribute);
                    }
                }
                t.LoadInput(doc);
            }
            else
            {
                // when verifying signatures
                // TODO - check m_signature.SignedInfo.Id
                XmlElement    el  = signatureElement.GetElementsByTagName(XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI) [0] as XmlElement;
                StringWriter  sw  = new StringWriter();
                XmlTextWriter xtw = new XmlTextWriter(sw);
                xtw.WriteStartElement(el.Prefix, el.LocalName, el.NamespaceURI);

                // context namespace nodes (except for "xmlns:xml")
                XmlNodeList nl = el.SelectNodes("namespace::*");
                foreach (XmlAttribute attr in nl)
                {
                    if (attr.ParentNode == el)
                    {
                        continue;
                    }
                    if (attr.LocalName == "xml")
                    {
                        continue;
                    }
                    if (attr.Prefix == el.Prefix)
                    {
                        continue;
                    }
                    attr.WriteTo(xtw);
                }
                foreach (XmlNode attr in el.Attributes)
                {
                    attr.WriteTo(xtw);
                }
                foreach (XmlNode n in el.ChildNodes)
                {
                    n.WriteTo(xtw);
                }

                xtw.WriteEndElement();
                byte [] si = Encoding.UTF8.GetBytes(sw.ToString());

                MemoryStream ms = new MemoryStream();
                ms.Write(si, 0, si.Length);
                ms.Position = 0;

                t.LoadInput(ms);
            }
            // C14N and C14NWithComments always return a Stream in GetOutput
            return((Stream)t.GetOutput());
        }
        private static Stream TransformXml(Transform xForm, Object source)
        {
            (new PermissionSet(PermissionState.Unrestricted)).Assert();  // Blessed
            try
            {
                // transform
                xForm.LoadInput(source);
            }
            finally
            {
                PermissionSet.RevertAssert();
            }

            return (Stream)xForm.GetOutput();
        }
Example #11
0
        /// <summary>
        ///     Log the canonicalized data
        /// </summary>
        /// <param name="signedXml">SignedXml object doing the signing or verification</param>
        /// <param name="canonicalizationTransform">transform canonicalizing the input</param>
        internal static void LogCanonicalizedOutput(SignedXml signedXml, Transform canonicalizationTransform) {
            Debug.Assert(signedXml != null, "signedXml != null");
            Debug.Assert(canonicalizationTransform != null, "canonicalizationTransform != null");

            if (VerboseLoggingEnabled) {
                using (StreamReader reader = new StreamReader(canonicalizationTransform.GetOutput(typeof(Stream)) as Stream)) {
                    string logMessage = String.Format(CultureInfo.InvariantCulture,
                                                      SecurityResources.GetResourceString("Log_CanonicalizedOutput"),
                                                      reader.ReadToEnd());
                    WriteLine(signedXml,
                              TraceEventType.Verbose,
                              SignedXmlDebugEvent.CanonicalizedData,
                              logMessage);
                }
            }
        }
Example #12
0
        // 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 strBaseUri)
        {
            Object currentInput = inputObject;

            foreach (Object obj in m_transforms)
            {
                Transform transform = obj as Transform;
                if (transform.AcceptsType(currentInput.GetType()))
                {
                    //in this case, no translation necessary, pump it through
                    transform.Resolver = resolver;
                    transform.BaseURI  = strBaseUri;
                    transform.LoadInput(currentInput);
                    currentInput = transform.GetOutput();
                }
                else
                {
                    // We need translation
                    // For now, we just know about Stream->{XmlNodeList,XmlDocument} and {XmlNodeList,XmlDocument}->Stream
                    if (currentInput is Stream)
                    {
                        if (transform.AcceptsType(typeof(XmlDocument)))
                        {
                            XmlDocument doc = new XmlDocument();
                            doc.PreserveWhitespace = true;
                            XmlValidatingReader valReader = SignedXml.PreProcessStreamInput((Stream)currentInput, resolver, strBaseUri);
                            doc.Load(valReader);
                            transform.LoadInput(doc);
                            currentInput = transform.GetOutput();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    }
                    if (currentInput is XmlNodeList)
                    {
                        if (transform.AcceptsType(typeof(Stream)))
                        {
                            CanonicalXml c14n = new CanonicalXml((XmlNodeList)currentInput, false);
                            MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                            transform.LoadInput((Stream)ms);
                            currentInput = transform.GetOutput();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    }
                    if (currentInput is XmlDocument)
                    {
                        if (transform.AcceptsType(typeof(Stream)))
                        {
                            CanonicalXml c14n = new CanonicalXml((XmlDocument)currentInput);
                            MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                            transform.LoadInput((Stream)ms);
                            currentInput = transform.GetOutput();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    }
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                }
            }

            // Final processing, either we already have a stream or have to canonicalize
            if (currentInput is Stream)
            {
                return(currentInput as Stream);
            }
            if (currentInput is XmlNodeList)
            {
                CanonicalXml c14n = new CanonicalXml((XmlNodeList)currentInput, false);
                MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                return(ms);
            }
            if (currentInput is XmlDocument)
            {
                CanonicalXml c14n = new CanonicalXml((XmlDocument)currentInput);
                MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                return(ms);
            }
            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
        }
 internal static void LogCanonicalizedOutput(SignedXml signedXml, Transform canonicalizationTransform)
 {
     if (VerboseLoggingEnabled)
     {
         using (StreamReader reader = new StreamReader(canonicalizationTransform.GetOutput(typeof(Stream)) as Stream))
         {
             string data = string.Format(CultureInfo.InvariantCulture, SecurityResources.GetResourceString("Log_CanonicalizedOutput"), new object[] { reader.ReadToEnd() });
             WriteLine(signedXml, TraceEventType.Verbose, SignedXmlDebugEvent.CanonicalizedData, data);
         }
     }
 }