Ejemplo n.º 1
0
        /// <summary>
        /// Signs the CDA Package and creates the signature document.
        /// </summary>
        /// <param name="package">A CDAPackageBase instance containing the root document to sign.</param>
        /// <param name="signingCert">The certificate used to sign the root document.</param>
        /// <returns>Signature of the root document.</returns>
        private static byte[] CreateSignature(CDAPackage package, X509Certificate2 signingCert)
        {
            package.SigningTime = DateTime.Now.ToUniversalTime();

            byte[] rootDocumentContent = package.CDADocumentRoot.FileContent;

            byte[] hash = CalculateSHA1(rootDocumentContent);

            var manifest = new ManifestType();

            manifest.Reference = new ReferenceType[]
            {
                new ReferenceType()
                {
                    URI          = package.CDADocumentRoot.FileName,
                    DigestMethod = new DigestMethodType()
                    {
                        Algorithm = SignedXml.XmlDsigSHA1Url
                    },
                    DigestValue = hash
                }
            };

            var approver = new ApproverType();

            approver.personId              = package.Approver.PersonId.ToString();
            approver.personName            = new PersonNameType();
            approver.personName.familyName = package.Approver.PersonFamilyName;
            if (package.Approver.PersonTitles != null)
            {
                approver.personName.nameTitle = package.Approver.PersonTitles.ToArray();
            }
            if (package.Approver.PersonGivenNames != null)
            {
                approver.personName.givenName = package.Approver.PersonGivenNames.ToArray();
            }
            if (package.Approver.PersonNameSuffixes != null)
            {
                approver.personName.nameSuffix = package.Approver.PersonNameSuffixes.ToArray();
            }

            var eSignature = new eSignatureType();

            eSignature.Manifest    = manifest;
            eSignature.approver    = approver;
            eSignature.signingTime = package.SigningTime.Value;

            XmlDocument eSignatureXml = eSignature.SerializeToXml();

            ISignedContainerProfileService signedContainerService = XspFactory.Instance.GetSignedContainerProfileService(XspVersion.V_2010);

            XmlDocument signedDoc = signedContainerService.Create(eSignatureXml, signingCert);

            var ms = new MemoryStream();

            signedDoc.Save(ms);

            return(ms.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sign an XML document according to the XSPP.
        /// </summary>
        /// <param name="document">The XML document to be signed.</param>
        /// <param name="signingCert">The certificate to sign the XML document with.</param>
        /// <returns>The signed XML document.</returns>
        public static XmlDocument XspSign(this XmlDocument document, X509Certificate2 signingCert)
        {
            ISignedContainerProfileService signedContainerService = XspFactory.Instance.GetSignedContainerProfileService(XspVersion.V_2010);

            return(signedContainerService.Create(document, signingCert));
        }