1

I've been generated a PKCS12 keystore through a API, but the return of the process is a KeyStore object. I need to send it, directly to the browser to be downloaded when the client send the requisition.

How can I do that?

I'm using java and jboss 5AS.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

2

You can use KeyStore#store() to write it out to an OutputStream.

keyStore.store(outputStream, password);

That's basically it. The OutputStream could be the one of the HTTP response. For a generic kickoff example of how to provide a file download in JSF wherein you need to integrate this line, head to this answer: How to provide a file download from a JSF backing bean? Use a content type of application/x-pkcs12.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • BalusC, thank you a lot, i followed you tip and the link, and all worked fine, I'll post the code to the peoples can use it too! – Marcos Fontana Mar 27 '13 at 13:48
1

Here is the code:

public void cadastrar () throws Exception
{       
    byte[] encodedKeyStore = controlador.cadastrar(certificadoModel);

    java.security.KeyStore keyStore = java.security.KeyStore.getInstance("PKCS12");
    keyStore.load(new ByteArrayInputStream(encodedKeyStore), certificadoModel.getPassword().toCharArray());

    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();

    ec.responseReset(); 
    ec.setResponseContentType("application/x-pkcs12"); 
    //ec.setResponseContentLength(contentLength); 
    ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + certificadoModel.getUsername() + ".p12" + "\""); 

    OutputStream output = ec.getResponseOutputStream();
    keyStore.store(output, certificadoModel.getPassword().toCharArray());

    fc.responseComplete(); 
}