Exemplo n.º 1
0
        public void TestGetDigesterInstance()
        {
            IDigester dig = pr.GetDigesterInstance("http://www.w3.org/2000/09/xmldsig#sha1", emptyDic);

            Assert.IsNotNull(dig, "Digester instance is null");
            Assert.IsTrue(dig is Digesters.SHA1Digester, "Digester has incorect type");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates SignedInfo node according to Xml spec at http://www.w3.org/TR/xmldsig-core/#sec-SignedInfo
        /// </summary>
        /// <param name="doc">XmlDocument used to create new attribute and nodes</param>
        /// <param name="canonicalizer">InstantiationVO containing the instance and
        /// property values of the canonicalizer to load</param>
        /// <param name="signer">InstantiationVO containing the instance and property
        /// values of the signer to load</param>
        /// <param name="references">List of references to sign</param>
        /// <returns>SignedInfo node according to Xml spec at http://www.w3.org/TR/xmldsig-core/#sec-SignedInfo
        /// </returns>
        private XmlNode CreateSignedInfoNode(XmlDocument doc, InstantiationVO canonicalizer, InstantiationVO signer,
                                             IList <IReference> references)
        {
            //Create SignedInfo node
            XmlNode signedInfoNode = doc.CreateNode(XmlNodeType.Element, "SignedInfo", null);

            //Create CanonicalizationMethod node and add it to SignedInfo node
            XmlNode canonMethodNode = CreateCanonicalizationMethodNode(doc, canonicalizer.Key);

            signedInfoNode.InnerXml += canonMethodNode.OuterXml;

            //Create SignatureMethod node and add it to SignedInfo node
            XmlNode signMethodNode = CreateSignatureMethod(doc, signer.Key);

            signedInfoNode.InnerXml += signMethodNode.OuterXml;

            //Process all the references
            foreach (IReference reference in references)
            {
                //Get instance of reference loader.
                IReferenceLoader referenceLoader = null;

                switch (reference.Protocol)
                {
                case "http":
                    referenceLoader = registry.GetReferenceLoaderInstance("http", emptyDic);
                    break;

                case "soap":
                    referenceLoader = reference.SoapRefLoader;
                    break;
                }

                //Load reference data using the reference loader.
                byte[] uriData = referenceLoader.LoadReferenceData(reference.ReferenceURI);

                XmlNode dummyNode = doc.CreateElement("dummy");
                //Apply Transforms if necessary
                if (reference.TransformerInstanceDefinitions.Count > 0)
                {
                    //Create Transforms node
                    XmlNode transformsNode = doc.CreateNode(XmlNodeType.Element, "Transforms", null);
                    foreach (InstantiationVO transfInstVO in reference.TransformerInstanceDefinitions)
                    {
                        //Get Transformer instance
                        ITransformer transfInst =
                            registry.GetTransformerInstance(transfInstVO.Key, transfInstVO.Params);

                        //Create Transform node and add Algorithm attribute
                        XmlNode      transformNode = doc.CreateNode(XmlNodeType.Element, "Transform", null);
                        XmlAttribute attr          = doc.CreateAttribute("Algorithm");
                        attr.Value = transfInstVO.Key;
                        transformNode.Attributes.Append(attr);

                        //Add Transform node to Transforms node
                        transformsNode.InnerXml += transformNode.OuterXml;

                        //Do the transform
                        uriData = transfInst.Transform(uriData);
                    }
                    //Add Transforms node to the dummy node
                    dummyNode.InnerXml += transformsNode.OuterXml;
                }

                //Get instance of digester
                IDigester digester = registry.GetDigesterInstance(reference.DigesterInstanceDefinition.Key,
                                                                  reference.DigesterInstanceDefinition.Params);

                //Produce a digest for the reference data bytes
                byte[] digestedData = digester.Digest(uriData);

                //Create Reference node with appropriate data set
                XmlNode refNode = CreateReferenceNode(doc, Convert.ToBase64String(digestedData),
                                                      reference.ReferenceURI, reference.DigesterInstanceDefinition.Key);

                //Add Transforms node to the dummy node
                refNode.InnerXml = dummyNode.InnerXml + refNode.InnerXml;

                //Add reference node to SignedInfo node
                signedInfoNode.InnerXml += refNode.OuterXml;
            }

            return(signedInfoNode);
        }