示例#1
0
        /// <summary>
        /// Método para realizar la ampliación de firmas
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="signatureFormat"></param>
        /// <param name="returnUpdatedSignatureType"></param>
        /// <returns></returns>
        public byte[] UpgradeSignature(byte[] signature, SignatureFormat signatureFormat, string returnUpdatedSignatureType)
        {
            object signatureObject = GetSignatureObject(signature, signatureFormat);

            VerifyResponse response = SendRequest(signatureObject, returnUpdatedSignatureType);

            if (response.Result.ResultMajor == "urn:oasis:names:tc:dss:1.0:resultmajor:Success")
            {
                XmlElement           updatedSignatureXmlElement = response.OptionalOutputs.Any.Single(e => e.LocalName == "UpdatedSignature");
                UpdatedSignatureType updatedSignatureType       = DeserializeXml <UpdatedSignatureType>(updatedSignatureXmlElement.OuterXml);

                if (updatedSignatureType.SignatureObject.Item.GetType() == typeof(SignaturePtr))
                {
                    SignaturePtr signaturePtr = updatedSignatureType.SignatureObject.Item as SignaturePtr;

                    DocumentWithSignature    docWithSignature = null;
                    IEnumerable <XmlElement> documentWithSignatureXmlElements = response.OptionalOutputs.Any.Where(e => e.LocalName == "DocumentWithSignature");
                    foreach (var item in documentWithSignatureXmlElements)
                    {
                        docWithSignature = DeserializeXml <DocumentWithSignature>(item.OuterXml);

                        if (docWithSignature.Document.ID == signaturePtr.WhichDocument)
                        {
                            break;
                        }
                    }

                    if (docWithSignature == null)
                    {
                        throw new Exception("No se ha encontrado el documento de firma");
                    }
                    else
                    {
                        return(docWithSignature.Document.Item as byte[]);
                    }
                }
                else if (updatedSignatureType.SignatureObject.Item.GetType() == typeof(Base64Signature))
                {
                    Base64Signature b64Signature = updatedSignatureType.SignatureObject.Item as Base64Signature;

                    return(b64Signature.Value);
                }
                else
                {
                    throw new Exception("Tipo de resultado no soportado");
                }
            }
            else
            {
                throw new Exception(response.Result.ResultMessage.Value);
            }
        }
示例#2
0
        public byte[] UpgradeSignature(byte[] signature, SignatureFormat signatureFormat, ReturnUpdatedSignatureType returnUpdateSignatureType,
                                       byte[] targetSignerCert = null, IEnumerable <DocumentBaseType> otherInputDocuments = null)
        {
            if (signature == null)
            {
                throw new ArgumentNullException("signature", "El valor no puede ser nulo.");
            }

            if (returnUpdateSignatureType == null)
            {
                throw new ArgumentNullException("returnUpdateSignatureType", "El valor no puede ser nulo.");
            }

            object document = GetDocument(signature, signatureFormat);

            ReturnUpdatedSignature returnUpdated = new ReturnUpdatedSignature();

            returnUpdated.Type = returnUpdateSignatureType.ResourceName;

            IgnoreGracePeriod igp = new IgnoreGracePeriod();

            DocumentType doc = new DocumentType();

            doc.ID   = "ID_DOCUMENTO";
            doc.Item = document;

            SignatureObject signatureObject = new SignatureObject();

            signatureObject.Item = new SignaturePtr()
            {
                WhichDocument = "ID_DOCUMENTO"
            };

            List <DocumentBaseType> documents = new List <DocumentBaseType>();

            documents.Add(doc);

            if (otherInputDocuments != null)
            {
                foreach (var inputDocument in otherInputDocuments)
                {
                    documents.Add(inputDocument);
                }
            }

            List <XmlElement> optionalInputs = new List <XmlElement>();

            optionalInputs.Add(GetXmlElement(igp));
            optionalInputs.Add(GetXmlElement(returnUpdated));

            if (targetSignerCert != null)
            {
                TargetSigner targetSigner = new TargetSigner();
                targetSigner.Value = targetSignerCert;

                optionalInputs.Add(GetXmlElement(targetSigner));
            }

            VerifyRequest request = BuildRequest(documents, signatureObject, optionalInputs);

            DSSAfirmaVerifyService ds = new DSSAfirmaVerifyService(_identity, _serverCert);

            string result = ds.verify(GetXmlElement(request).OuterXml);

            VerifyResponse response = DeserializeXml <VerifyResponse>(result);

            if (ResultType.Success.Equals(response.Result.ResultMajor))
            {
                XmlElement           updatedSignatureXmlElement = response.OptionalOutputs.Any.Single(e => e.LocalName == "UpdatedSignature");
                UpdatedSignatureType updatedSignatureType       = DeserializeXml <UpdatedSignatureType>(updatedSignatureXmlElement.OuterXml);

                if (updatedSignatureType.SignatureObject.Item.GetType() == typeof(SignaturePtr))
                {
                    SignaturePtr             signaturePtr     = updatedSignatureType.SignatureObject.Item as SignaturePtr;
                    DocumentWithSignature    docWithSignature = null;
                    IEnumerable <XmlElement> documentWithSignatureXmlElements = response.OptionalOutputs.Any.Where(e => e.LocalName == "DocumentWithSignature");
                    foreach (var item in documentWithSignatureXmlElements)
                    {
                        docWithSignature = DeserializeXml <DocumentWithSignature>(item.OuterXml);

                        if (docWithSignature.Document.ID == signaturePtr.WhichDocument)
                        {
                            return(docWithSignature.Document.Item as byte[]);
                        }
                    }

                    throw new Exception("No se ha encontrado el documento de firma");
                }
                else if (updatedSignatureType.SignatureObject.Item.GetType() == typeof(Base64Signature))
                {
                    Base64Signature b64Signature = updatedSignatureType.SignatureObject.Item as Base64Signature;

                    return(b64Signature.Value);
                }
                else
                {
                    throw new Exception("Tipo de resultado no soportado");
                }
            }
            else
            {
                throw new AfirmaResultException(response.Result.ResultMajor, response.Result.ResultMinor, response.Result.ResultMessage.Value);
            }
        }