Skip to content

Instantly share code, notes, and snippets.

@rafaelwkerr
Created September 24, 2014 01:23
Show Gist options
  • Save rafaelwkerr/a585d324d3e534a2c16b to your computer and use it in GitHub Desktop.
Save rafaelwkerr/a585d324d3e534a2c16b to your computer and use it in GitHub Desktop.
An activity that will load a .BKS Keystore in Android and Sign an XML file using Apache Santuario (xmlSecurity)
package sign.bematech.com.br.signxmlandroid;
import android.app.Activity;
import android.os.Bundle;
import org.apache.xml.security.Init;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.signature.XMLSignature;
import org.apache.xml.security.transforms.Transforms;
import org.apache.xml.security.utils.Constants;
import org.apache.xml.security.utils.ElementProxy;
import org.w3c.dom.Document;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.xml.parsers.DocumentBuilderFactory;
public class SignActivity extends Activity {
private static final String PRIVATE_KEY_ALIAS = "bematechkey";
private static final String PRIVATE_KEY_PASS = "bema123";
private static final String KEY_STORE_PASS = "bema123";
private static final String KEY_STORE_TYPE = "BKS";
private static KeyStore keyStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sign();
}
private void sign(){
InputStream in = null;
try {
keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
in = getResources().openRawResource(R.raw.bematech);
keyStore.load(in, KEY_STORE_PASS.toCharArray());
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
try {
InputStream fileInputStream = getAssets().open("test.xml");
ByteArrayOutputStream signedOutputStream = signXml(fileInputStream, keyStore);
System.out.print(signedOutputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
//IOUtils.closeQuietly(fileInputStream);
}
}
public ByteArrayOutputStream signXml(InputStream xmlFile, KeyStore keyStore) throws Exception {
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
Init.init();
ElementProxy.setDefaultPrefix(Constants.SignatureSpecNS, "");
XMLSignature xmlSignature = createSignature(doc);
final Transforms transforms = new Transforms(doc);
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
transforms.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
xmlSignature.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
final Key privateKey = keyStore.getKey(PRIVATE_KEY_ALIAS, PRIVATE_KEY_PASS.toCharArray());
final X509Certificate cert = (X509Certificate) keyStore.getCertificate(PRIVATE_KEY_ALIAS);
xmlSignature.addKeyInfo(cert);
xmlSignature.addKeyInfo(cert.getPublicKey());
xmlSignature.sign(privateKey);
doc.getDocumentElement().appendChild(xmlSignature.getElement());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS).canonicalizeSubtree(doc));
return outputStream;
}
private static XMLSignature createSignature(Document doc) {
XMLSignature xmlSignature = null;
String uri = "#NFe13140782373077000171650290000030531000030538";
try {
xmlSignature = new XMLSignature(doc, uri, XMLSignature.ALGO_ID_SIGNATURE_RSA);
} catch (XMLSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xmlSignature;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment