Ejemplo n.º 1
0
        /// <summary>
        /// <p><strong>Purpose:</strong></p>  <p>This method processes the references and creates the digital signature
        /// of the contents, and returns the whole XML Node of the signed contents. It produces an XmlNode
        /// of type signature.</p>
        /// <p>Note: Current implementation does not create a KeyInfoNode</p>
        /// </summary>
        /// <param name="references">references that we will be signing</param>
        /// <param name="canonicalizer">the specific canonicalizer to use</param>
        /// <param name="signer">the specific signer to use</param>
        /// <param name="signID">Signature id</param>
        /// <returns>Xml Node with digitally signed representation of the data</returns>
        /// <exception cref="ArgumentNullException">if any element is null</exception>
        /// <exception cref="SignatureManagerException">any internal exception thrown in this function
        /// is wrapped up as SignatureManagerException</exception>
        public XmlNode Sign(IList <IReference> references, InstantiationVO canonicalizer, InstantiationVO signer,
                            string signID)
        {
            //Validate not null
            ExceptionHelper.ValidateNotNull(references, "references");
            ExceptionHelper.ValidateNotNull(canonicalizer, "canonicalizer");
            ExceptionHelper.ValidateNotNull(signer, "signer");
            ExceptionHelper.ValidateNotNull(signID, "signID");

            XmlDocument doc = new XmlDocument();

            try
            {
                //Create Signature node
                XmlNode signatureNode = doc.CreateNode(XmlNodeType.Element, "Signature", null);

                //Create SignedInfo Node
                XmlNode signedInfoNode = CreateSignedInfoNode(doc, canonicalizer, signer, references);

                //Add an attribute representing default namespace to SignedInfo node
                //since the two have the exact literal format
                ((XmlElement)signedInfoNode).SetAttribute("xmlns", DEF_XMLDSIG_NS);

                //Get Canonicalizer and canonicalize
                ICanonicalizer canonInst = registry.GetCanonicalizerInstance(canonicalizer.Key,
                                                                             canonicalizer.Params);
                string canonicalized = canonInst.BringToCanonicalForm(signedInfoNode.OuterXml);

                //Add SignedInfo node to Signature node
                signatureNode.InnerXml += canonicalized;

                //Get Signer Instance, sign, assign to signatureValue node
                ISigner signerInst    = registry.GetSignerInstance(signer.Key, signer.Params);
                string  signed        = signerInst.Sign(Encoding.UTF8.GetBytes(canonicalized));
                XmlNode signValueNode = CreateSignatureValue(doc, signed);

                //Append signatureValue to Signature node
                signatureNode.InnerXml += signValueNode.OuterXml;

                //Add an attribute representing default namespace to Signature node
                XmlAttribute defNs = doc.CreateAttribute("xmlns");
                defNs.Value = DEF_XMLDSIG_NS;
                signatureNode.Attributes.Append(defNs);
                //Add Id attribute to Signature node
                XmlAttribute idAttr = doc.CreateAttribute("Id");
                idAttr.Value = signID;
                signatureNode.Attributes.Append(idAttr);

                //Let default namespace takes effect
                doc.LoadXml(signatureNode.OuterXml);
                return(doc.DocumentElement);
            }
            catch (Exception ex)
            {
                throw new SignatureManagerException(SIGN_MAN_EXCP_MSG, ex);
            }
        }
Ejemplo n.º 2
0
        public void TestGetCanonicalizerInstance()
        {
            ICanonicalizer canonc = pr.GetCanonicalizerInstance("http://www.w3.org/TR/2001/REC-xml-c14n-20010315",
                                                                emptyDic);

            Assert.IsNotNull(canonc, "Canonicalizer instance is null");
            Assert.IsTrue(canonc is Canonicalizers.StandardFormCanonicalizer,
                          "Canonicalizer has incorect type");
        }