Exemple #1
0
        /// <summary>
        /// Demonstrates a call to Mobile_XML_Signature Sign method
        /// </summary>
        /// <param name="message">The soapMessage to operate on</param>
        public void Call_Mobile_Signature(SoapMessage message)
        {
            //Create empty parameters dictionary
            Dictionary <string, object> emptyDic = new Dictionary <string, object>();

            //Create SoapMessage Reference Loader for reference
            IReferenceLoader smrl = new SoapMessageReferenceLoader(message);
            //Create Digester for Reference
            InstantiationVO digesterVO = new InstantiationVO("http://www.w3.org/2000/09/xmldsig#sha1", emptyDic);
            //Create Transformers
            IList <InstantiationVO> transfs = new List <InstantiationVO>();
            //Create Soap Reference and Add it to list
            Reference          soapReference = new Reference("#msgBody", transfs, digesterVO, smrl);
            IList <IReference> references    = new List <IReference>();

            references.Add(soapReference);

            //Create Canonicalizer
            InstantiationVO c14nIVO = new InstantiationVO("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", emptyDic);

            //Setup Signers
            DSACryptoServiceProvider dsa = new DSACryptoServiceProvider();
            //Usage of true here exports private key also
            DSAParameters dsaParams = dsa.ExportParameters(true);
            IDictionary <string, object> signerParams = new Dictionary <string, object>();

            signerParams.Add("DSAKeyInfo", dsaParams);
            InstantiationVO signerIVO = new InstantiationVO("xml:dig:signer:rsa-dsa",
                                                            signerParams);

            //Sign it
            SignatureManager sm     = new SignatureManager();
            XmlNode          signed = sm.Sign(references, c14nIVO, signerIVO, "myId");
        }
        public async Task <IActionResult> Sign(string uuid, string password, string description)
        {
            if (string.IsNullOrEmpty(uuid) || string.IsNullOrEmpty(password))
            {
                return(View("OperationNotAllowed"));
            }

            var document = await DBContext.Document
                           .SingleOrDefaultAsync(m => m.Uuid == uuid);

            if (document == null)
            {
                return(NotFound());
            }

            if (document.MimeType != "application/pdf")
            {
                return(View("InvalidDocument"));
            }

            var x509certificate = HttpContext.Connection.ClientCertificate;

            if (x509certificate == null)
            {
                return(View("OperationNotAllowed"));
            }

            var certificate = await DBContext.Certificate
                              .SingleOrDefaultAsync(r => r.SerialNumber == x509certificate.SerialNumber);

            if (certificate == null || certificate.ReviewerUuid != document.ReviewerUuid)
            {
                return(View("OperationNotAllowed"));
            }

            if (certificate.ExpireDate < DateTime.Now.Date)
            {
                certificate.Revoked    = true;
                certificate.RevokeDate = DateTime.UtcNow.Date;
                DBContext.Certificate.Update(certificate);
                await DBContext.SaveChangesAsync();
            }

            if (certificate.Revoked == true)
            {
                return(RedirectToAction("CertificateExpired", "Certificates"));
            }

            var pkcs12store = TrustManager.LoadPkcs12Store(certificate.Uuid, password, CertificateType.ReviewerCertificate);

            if (pkcs12store == null)
            {
                return(View("OperationNotAllowed"));
            }

            var reviewer = await DBContext.Reviewer
                           .SingleOrDefaultAsync(r => r.Uuid == certificate.ReviewerUuid);

            var metadata = new PDFMetadata()
            {
                Title    = "PDF Signed Document" + document.Name,
                Author   = certificate.Reviewer.Name,
                Creator  = certificate.Reviewer.Name,
                Producer = certificate.Reviewer.Name,
                Keywords = "UUID:" + document.Uuid,
                Subject  = "Signed Document"
            };

            var signature = new Signature()
            {
                Store      = pkcs12store,
                Reason     = "Document Aproved, Date:" + DateTime.UtcNow.Date,
                Page       = 1,
                Contact    = certificate.Reviewer.Email,
                CustomText = "Signed by " + reviewer.Name + " on " + DateTime.UtcNow.Date.ToString("dd-MM-yyyy") + " - " + description,
                Top        = 10,
                Left       = 10,
                Width      = 200,
                Height     = 50,

                Multi   = false,
                Visible = true
            };

            SignatureManager.Sign(
                signature,
                metadata,
                FileManager.DocumentRoot + "/" + document.Uuid,
                FileManager.DocumentRoot + "/" + document.Uuid + "-signed");

            document.SignatureDate = DateTime.UtcNow.Date;
            DBContext.Document.Update(document);
            await DBContext.SaveChangesAsync();

            var message = await RenderService
                          .RenderToStringAsync("Email/DocumentSigned", document);

            var attachments = new List <Attachment> ();

            attachments.Add(
                await EmailManager.LoadAttachment(
                    FileManager.DocumentRoot + "/" + document.Uuid + "-signed",
                    "Signed by " + reviewer.Name + "-" + document.Name,
                    document.MimeType));

            attachments.Add(
                await EmailManager.LoadAttachment(
                    TrustManager.CertificatePath(
                        certificate.Uuid,
                        CertificateType.ReviewerCertificate,
                        StoreFormat.CRT),
                    "public.crt",
                    "application/x-x509-ca-cert"));

            attachments.Add(
                await EmailManager.LoadAttachment(
                    TrustManager.CertificatePath(
                        "root",
                        CertificateType.AuthorityCertificate,
                        StoreFormat.CRT),
                    "authority.crt",
                    "application/x-x509-ca-cert"));

            var response = await EmailManager.SendEmailHTML(
                message,
                EmailManager.Sender,
                certificate.Reviewer.Email,
                "Your signed document is ready",
                attachments
                );

            if (!response.Successful)
            {
                return(View("ErrorSendingDocument", document));
            }

            return(View("DocumentSigned", document));
        }