EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Simple Signature in JAVA

Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.
#29951
Posted: 06/27/2014 17:25:12
by Mauricio Ahumada (Standard support level)
Joined: 06/16/2014
Posts: 7

Hello, and it's me again. This time i want to do the signature in Android/Java.

I tried doing the same function that worked for me on .NET however there are some peculiarities that prevent me from fullfilling my tasks:

First, the code:...

Code
public static void SigxXmlFile4(String FileName, String SignedFileName)
    {
        TElXAdESSigner XAdESSigner = new TElXAdESSigner();
        TElXMLSigner Signer = new TElXMLSigner();
        try
        {
            TElXMLDOMDocument XMLDocument = new TElXMLDOMDocument();
            XMLDocument.LoadFromFile(FileName);

            SBUtils.SetLicenseKey("5C4B...F930");
            TElXMLReferenceList TElXMLListInstance = new TElXMLReferenceList();
            TElXMLReference Ref = new TElXMLReference();
            Ref.SetDigestMethod((short) 1);
            Ref.SetURI("");
            Ref.SetURINode(XMLDocument.GetDocumentElement());
            //Ref.TransformChain.Add(new TElXMLEnvelopedSignatureTransform());
            Ref.TransformChain(new TElXMLEnvelopedSignatureTransform());
            TElXMLListInstance.Add(Ref);

            Signer.SetReferences(Ref);
            Signer.SetCanonicalizationMethod((short) 2); //xcmCanonComment
            Signer.SetSignatureMethod((short) 2); //xsmRSA_SHA1 = 2
            Signer.SetSignatureType((short)4); //xstEnveloped
            Signer.SetSignatureMethodType((short)0); //xmtSig = 0   xmtSig   The data is signed
            Signer.SetIncludeKey(true);
...
...


The line

Code
Ref.TransformChain(new TElXMLEnvelopedSignatureTransform());


Shows me that there is not a TRansformChain method, and neither does exists a setTransformChain method however appearing in the corresponding documentation
(https://www.eldos.com/documentation/sbb/documentation/ref_cl_xmlreference.html)

The line

Code
Signer.SetReferences(Ref);


Is expecting a TElXMLReferenceList instance, but in .NET the same method is happily receiving a TElXMLReference instance.

PD. I tried looking the examples included in the library, but really guys? 8 long .java files with a LOT of non essential stuff (i mean stuff not related to the real signature) is too much for me. I'm not expert in JAVA and i got lost in all the coding there. Really, you should simplify the samples if they are going to be of any use to someone who is not an expert.
#29952
Posted: 06/27/2014 17:50:14
by Dmytro Bogatskyy (EldoS Corp.)

Hello,
Quote
Ref.TransformChain(new TElXMLEnvelopedSignatureTransform());
Shows me that there is not a TRansformChain method, and neither does exists a setTransformChain method however appearing in the corresponding documentation

TransformChain is readonly property, please use a following code:
Code
               Ref.GetTransformChain().Add(new TElXMLEnvelopedSignatureTransform());

Quote
Signer.SetReferences(Ref);
Is expecting a TElXMLReferenceList instance, but in .NET the same method is happily receiving a TElXMLReference instance.

I think, you are mixing something. You don't need to create TElXMLReferenceList instance and can use Signer.GetReferences.Add(Ref) method directly. Setter for References property is left only for backward compatibility.
#29954
Posted: 06/30/2014 08:44:07
by Mauricio Ahumada (Standard support level)
Joined: 06/16/2014
Posts: 7

Thanks, i will try.
#29956
Posted: 06/30/2014 09:39:43
by Mauricio Ahumada (Standard support level)
Joined: 06/16/2014
Posts: 7

Oh, bollocks! After apparently getting rid of the error showed by eclipse i ran the first debung and I couldnt't get pass the Signer class instantiation! All i got is an java.lang.NoClassDefFoundError: SecureBlackbox.XMLSecurity.TElXAdESSigner when doing the
Code
XAdESSigner = new TElXAdESSigner();
line...

I'm starting the lose it here.

What in the name of god i'm doing wrong this time, please!

PD. I'm on an Android device.

Code

//The imports
import SecureBlackbox.Base.SBUtils;
import SecureBlackbox.Base.TElX509Certificate;
import SecureBlackbox.Base.TSBObject;
import SecureBlackbox.XML.TElXMLDOMDocument;
import SecureBlackbox.XML.TElXMLDOMNode;
import SecureBlackbox.XML.TElXMLNamespaceMap;
import SecureBlackbox.XMLSecurity.SBXMLAdES;
import SecureBlackbox.XMLSecurity.TElXAdESSigner;
import SecureBlackbox.XMLSecurity.TElXMLEnvelopedSignatureTransform;
import SecureBlackbox.XMLSecurity.TElXMLKeyInfoX509Data;
import SecureBlackbox.XMLSecurity.TElXMLQualifyingProperties;
import SecureBlackbox.XMLSecurity.TElXMLReference;
import SecureBlackbox.XMLSecurity.TElXMLReferenceList;
import SecureBlackbox.XMLSecurity.TElXMLSignature;
import SecureBlackbox.XMLSecurity.TElXMLSigner;

//The call (on the main method)
SigxXmlFile4("/storage/extSdCard/20101266819-07-BB11-00000025(2)SinFirma.xml", "/storage/extSdCard/20101266819-07-BB11-00000025Firmado.xml");

public static void SigxXmlFile4(String FileName, String SignedFileName)
    {
        TElXAdESSigner XAdESSigner = null;
        TElXMLSigner Signer;
        
        XAdESSigner = new TElXAdESSigner();
        Signer = new TElXMLSigner();
        try
        {
            TElXMLDOMDocument XMLDocument = new TElXMLDOMDocument();
            XMLDocument.LoadFromFile(FileName);

            SBUtils.SetLicenseKey("5C4B...F930");
            TElXMLReferenceList TElXMLListInstance = new TElXMLReferenceList();
            TElXMLReference Ref = new TElXMLReference();
            Ref.SetDigestMethod((short) 1);
            Ref.SetURI("");
            Ref.SetURINode(XMLDocument.GetDocumentElement());
            //Ref.TransformChain.Add(new TElXMLEnvelopedSignatureTransform());
            Ref.GetTransformChain().Add(new TElXMLEnvelopedSignatureTransform());
            TElXMLListInstance.Add(Ref);

            Signer.GetReferences().Add(Ref);
            Signer.SetCanonicalizationMethod((short) 2); //xcmCanonComment
            Signer.SetSignatureMethod((short) 2); //xsmRSA_SHA1 = 2
            Signer.SetSignatureType((short)4); //xstEnveloped
            Signer.SetSignatureMethodType((short)0); //xmtSig = 0   xmtSig   The data is signed
            Signer.SetIncludeKey(true);
            

            TElXMLKeyInfoX509Data X509Data = new TElXMLKeyInfoX509Data(true);
            TElX509Certificate Certificado = new TElX509Certificate();
            Certificado.LoadKeyFromFileAuto("/storage/extSdCard/TOTTUS.pfx", "1234");
            X509Data.SetCertificate(Certificado);

            Signer.SetKeyData(X509Data);
            Signer.UpdateReferencesDigest();

            XAdESSigner.SetXAdESVersion(SBXMLAdES.XAdES_v1_4_1);

            XAdESSigner.Generate(SBXMLAdES.XAdES_BES);
            TElXMLQualifyingProperties Prop = new TElXMLQualifyingProperties();
            Prop.SetTarget("#signature20101266819");
            XAdESSigner.SetQualifyingProperties(Prop);
            Signer.GenerateSignature();
            
            TElXMLSignature  sigId = new TElXMLSignature();
            sigId.SetID("signature20101266819");
            Signer.SetSignature(sigId);

            TElXMLDOMNode SigNode = XMLDocument.GetDocumentElement();
            
            TElXMLDOMDocument FXMLDocument = new TElXMLDOMDocument();
            SigNode = null;
            TSBObject obj = new TSBObject();
            Signer.Save(obj);
            SigNode = (TElXMLDOMNode)obj.Value;
            FXMLDocument = SigNode.GetOwnerDocument();

            TElXMLNamespaceMap XmlNSMap = new TElXMLNamespaceMap();
            XmlNSMap.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2");
            XmlNSMap.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
            XmlNSMap.AddNamespace("ccts", "urn:un:unece:uncefact:documentation:2");
            XmlNSMap.AddNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2");
            XmlNSMap.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
            XmlNSMap.AddNamespace("qdt", "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2");
            XmlNSMap.AddNamespace("sac", "urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1");
            XmlNSMap.AddNamespace("udt", "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2");
            XmlNSMap.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            TElXMLDOMNode asd = XMLDocument.FindNode("cac:AccountingSupplierParty", true).FindNode("cbc:CustomerAssignedAccountID");            
        }
        finally
        {
            Signer.Destroy();
            XAdESSigner.Destroy();
        }
    }
#29957
Posted: 06/30/2014 09:46:13
by Vsevolod Ievgiienko (EldoS Corp.)

Quote
java.lang.NoClassDefFoundError: SecureBlackbox.XMLSecurity.TElXAdESSigner


Most likely you didn't add SecureBlackbox.XMLSecurity.jar to the project.
#29958
Posted: 06/30/2014 09:49:02
by Mauricio Ahumada (Standard support level)
Joined: 06/16/2014
Posts: 7

Yes, it's the most obvious reason, however i did add the libraries.

(Attached SS)


#29961
Posted: 06/30/2014 09:52:30
by Vsevolod Ievgiienko (EldoS Corp.)

Please go to "Order and Export" section and check boxes in front of SecureBlackbox libraries.
#29962
Posted: 06/30/2014 10:14:03
by Mauricio Ahumada (Standard support level)
Joined: 06/16/2014
Posts: 7

Done, but there was no change in the errors.

Edit. Bah, I tried attaching another screenshot but it is not showing on the forum.

Edit2. It was too big.


#29963
Posted: 06/30/2014 10:27:47
by Vsevolod Ievgiienko (EldoS Corp.)

Could you post your project with JARs via Helpdesk. We'll check it.
#29964
Posted: 06/30/2014 10:46:15
by Mauricio Ahumada (Standard support level)
Joined: 06/16/2014
Posts: 7

Ok, will do.
Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.

Reply

Statistics

Topic viewed 2042 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!