Quantcast
Channel: Visual COBOL - Forum - Recent Threads
Viewing all articles
Browse latest Browse all 4356

RE: Signature xml file

$
0
0

Something like the following:

      $set ilusing"System"
      $set ilusing"System.Windows.Forms"
      $set ilusing"System.Xml"
      $set ilusing"System.Security.Cryptography"
      $set ilusing"System.Security.Cryptography.Xml"
      $set ilusing"System.Security"
      $set ilusing"System.Security.Cryptography.X509Certificates"
       class-id testxmlsig.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       
       working-storage section.
       
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
           goback.
       end method.

       method-id button1_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
       
           declare XMLAssinado as string = "c:\OutXML.xml"
           declare #XML as string = "c:\InXML.xml"
           declare pCertificado as type X509Certificate2 = null

           try
               *> verificando existencia de certificado utilizado na assinatura
              declare subject as string = type String::Empty
              if (pCertificado not = null)
                 set subject to pCertificado::Subject::ToString
              end-if
              declare x509Certificate as type X509Certificate2 = new X509Certificate2
              declare store as type X509Store = new X509Store("MY", type StoreLocation::CurrentUser)
              invoke store::Open(type OpenFlags::ReadOnly b-or type OpenFlags::OpenExistingOnly)
              declare collection as type X509Certificate2Collection = store::Certificates 
                 as type X509Certificate2Collection
              declare collection1 as type X509Certificate2Collection 
                 = collection::Find(type X509FindType::FindByTimeValid, type DateTime::Now, false) 
                   as type X509Certificate2Collection
              declare collection2 as type X509Certificate2Collection 
                 = collection::Find(type X509FindType::FindByKeyUsage, 
                   type X509KeyUsageFlags::DigitalSignature, false) as type X509Certificate2Collection
              declare scollection as type X509Certificate2Collection 
                 = type X509Certificate2UI::SelectFromCollection(collection2, 
                   "Certificado(s) Digital(is) disponível(is)", 
                   "Selecione o certificado digital para uso no aplicativo", 
                   type X509SelectionFlag::SingleSelection)
              if (scollection::Count = 0)
                 invoke type MessageBox::Show("Nenhum certificado digital foi selecionado ou o "
                    & "certificado selecionado está com problemas.")
              else
                 set pCertificado to scollection[0]
                 declare documento as type XmlDocument = new XmlDocument
                 set documento::PreserveWhitespace to true
                 try
                    *> verificando elemento de referencia
                    invoke documento::Load(#XML)
                    try
                       *> selecionando certificado digital baseado no subject
                       set x509Certificate to collection1[0]
                       declare docXML as type SignedXml = new SignedXml(documento)
                       set docXML::SigningKey to pCertificado::PrivateKey
                       *> Create a reference to be signed.
                       declare #reference as type Reference = new Reference()
                       set #reference::Uri to ""
                       *> adicionando EnvelopedSignatureTransform a referencia
                       declare envelopedSigntature as type XmlDsigEnvelopedSignatureTransform
                          = new XmlDsigEnvelopedSignatureTransform
                       invoke #reference::AddTransform(envelopedSigntature)
                       declare c14Transform as type XmlDsigC14NTransform = new XmlDsigC14NTransform
                       invoke #reference::AddTransform(c14Transform)
                       invoke docXML::AddReference(#reference)
                       *> carrega o certificado em KeyInfoX509Data para adicionar a KeyInfo
                       declare keyInfo as type KeyInfo = new KeyInfo
                       invoke keyInfo::AddClause(new type KeyInfoX509Data(pCertificado))
                       set docXML::KeyInfo to keyInfo
                       invoke docXML::ComputeSignature
                       *> recuperando a representacao do XML assinado
                       declare xmlDigitalSignature as type XmlElement = docXML::GetXml
                       invoke documento::DocumentElement::AppendChild(documento::ImportNode(xmlDigitalSignature, 
                          true))
                       set XMLAssinado to documento::OuterXml
                       invoke documento::Save("c:\OutXML.xml")
                    catch ex as type Exception
                       invoke type MessageBox::Show("Erro: " & ex::Message)
                    end-try
                 catch caught as type Exception
                    invoke type MessageBox::Show("Erro ao assinar o documento - " & caught::Message)
                 end-try
              end-if
           catch caught as type Exception 
              invoke type MessageBox::Show("XML mal formado - " & caught::Message)
           end-try.
            
       end method.
      
       end class.

Viewing all articles
Browse latest Browse all 4356

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>