Android : SpongyCastle: how to generate signed data?

on Thursday, December 4, 2014


I have a message and I want to sign that data with a digital certificate, in ANDROID, with android-studio. This my code:





List certList = new ArrayList();
//CMSTypedData msg = new CMSProcessableByteArray("Hello world!".getBytes());
certList.add(pCertificate);
Store certs = new JcaCertStore(certList);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
ContentSigner sha1Signer = new JcaContentSignerBuilder(gen.DIGEST_SHA1).setProvider("SC").build((PrivateKey) keyStore.getKey(alias, "zara2008".toCharArray()));

gen.addSignerInfoGenerator(
new JcaSignerInfoGeneratorBuilder(
new JcaDigestCalculatorProviderBuilder().setProvider("SC").build())
.build(sha1Signer, pCertificate));

gen.addCertificates(certs );
// Add the data (XML) to the Message
String dstdn = "cn=wsaahomo,o=afip,c=ar,serialNumber=CUIT 33693450239";
String LoginTicketRequest_xml;
LoginTicketRequest_xml = LoginTicket.create_LoginTicketRequest(SignerDN, dstdn, "wsfe", (long) 3600);
CMSProcessable data = new CMSProcessableByteArray(LoginTicketRequest_xml.getBytes());
CMSTypedData datatyped = new CMSProcessableByteArray(LoginTicketRequest_xml.getBytes());
CMSSignedData sigData = gen.generate(datatyped, false);



This is the error: "Unknown signature type requested: 1.3.14.3.2.26"


I tryed distints solutions, like put "DIGEST_SHA1" but doesn't works.


I tryed with BouncyCastle in ANDROID (I run some script that I found elsewhere that makes that BC works in Android). It's gives me a lot of messages "deprecated".





// Create a new empty CMS Message
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

// Add a Signer to the Message
gen.addSigner(pKey, pCertificate, CMSSignedDataGenerator.DIGEST_SHA1);

// Add the Certificate to the Message
gen.addCertificatesAndCRLs(cstore);

// Add the data (XML) to the Message
CMSProcessable data = new CMSProcessableByteArray(LoginTicketRequest_xml.getBytes());

// Add a Sign of the Data to the Message
CMSSignedData signed = gen.generate(data, true, "BC");

//
asn1_cms = signed.getEncoded();



But this is not a solution, I dont want to work with deprecated code.

0 comments:

Post a Comment