jueves, 26 de septiembre de 2013

Java y los certificados (HTTPS)

Please, see that:

1. http://alvinalexander.com/java/java-using-keytool-genkey-private-key-keystore
2. http://www.dotkam.com/2008/04/22/creating-public-and-private-certificateskeys/
3. http://alvinalexander.com/java/java-using-keytool-import-certificate-keystore

See also:

http://www.mkyong.com/tomcat/how-to-configure-tomcat-to-support-ssl-or-https/
http://www.thegeekstuff.com/2011/09/ssl-for-tomcat-server/
http://www.adictosaltrabajo.com/tutoriales/tutoriales.php?pagina=SSLenTomcat

Muy sencillo bueno y cortito...
http://aprenderdiaadia.wordpress.com/2011/07/23/keystore-truststore/


Ejemplo:

-----------------------
-----------------------
CONFIGURANDO EL SERVER
-----------------------
-----------------------
1)
keytool.exe -genkey -alias smkeystore -keyalg RSA
=> esto me lo tira en el directorio del usuario como .keystore

keytool -genkey -alias smkeystore -keyalg RSA -keystore c:\mkyongkeystore
=> pass: gemalto50

keytool.exe -list -keystore e:\tomcatkeystores

2)
server.xml:

   <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->  

  <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
  keystoreFile="e:/CERTIFICADOS_SERVER/smkeystore"
  keystorePass="xxx" />


Listo: probar https://localhost:8443/

De esta menera activamos el HTTPS al tomcat.

-----------------------
-----------------------
CONFIGURANDO EL CLIENTE
-----------------------
-----------------------
Para esto necesitamos generar un certificado o bien, bajarlo del servidor
Finally (and perhaps the most important), you need to export the self-signed certificate you've generated on the server side
(only the certificate, not the private key) and import it into the keystore you use as a trust store on the client side.

3)
Generamos el certificado

Opcion 1:

Con esto exportamos el certificado q esta dentro del keystore creado anteriormente:
keytool.exe -export -keystore e:\tomcatkeystores -alias smkeystore -file tomcatcer.crt

Opcion 2:

ingresamos a https://localhost:8443
file > properties > certificates > details > copy file > con esto exportamos el certificado de otra manera (sin el keytool de java)

4)
Importamos el certificado al keystore (lo importo en mi propio keystore (porque este va a estar en la PC cliente))

keytool.exe -import -file e:\tomcatcer.crt -keystore e:\tomcatkeystores -storepass xxxx12 (esta clave no tiene que se la misma que se genero cuando generamos el keystore del lado server)