PDA

View Full Version : java e ssl


Unrue
14-04-2005, 11:38
Ho creato un certificato server e client e relativi keystores, per fare la mutua autenticazione tra i due tramite tunnel ssl. Ho inoltre scritto il seguente programma client in java che dovrebbe inviare il certificato del client al server per verificare l'autenticazione e viceversa. Il metodo che richiamo è implementato in un web services sotto Tomcat 5. Nel file server.xml di Tomcat,dopo aver abilitato il connettore ssl, ho settato il clientauth= true, per abilitare la mutua autenticazione. Questo è il programma che ho scritto:

package mypackage1;
import java.net.URL;
import java.util.Properties;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.net.ssl.SSLSocketFactory;
import java.security.Security;



public class ProvaCertificatoClient
{
public ProvaCertificatoClient()
{
}
public static void main(String [] args) throws Exception {
System.setProperty("javax.net.ssl.trustStore","C:\\Documents and Settings\\gabriele\\keyclient.jks");
System.setProperty("javax.net.ssl.trustStorePassword","changeit");



String endpoint = "https://localhost:8443/axis/services/Ciao";
String method = "prova" ;
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(method); // Definisco il metodo da invocare
String ret = (String)call.invoke( new Object [] {"Buongiorno"});
System.out.println("Risultato: " +ret);
}
}

Il problema è che mi da' questo messaggio di errore: "Software caused connection abort: recv failed" . Chi mi puo' aiutare? Grazie.

kingv
15-04-2005, 20:15
il truststore è l'archivio dei certificati che vuoi considerare "trusted", che ritieni emessi da CA riconosciute (la tua, ad esempio :D ).
il keystore (settaabile con la property javax.net.ssl.keyStore) invece deve contenere la chiave privata e il certificato del tuo client (ovvero quello che ti rende autenticato lato server).

N.B. nulla vieta che siano lo stesso file ;)

Unrue
16-04-2005, 20:52
Ciao, ho modificato le due righe scrivendo:

System.setProperty("javax.net.ssl.keyStore","C:\\Documents and Settings\\gabriele\\keyclient.jks");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");

Adesso mi dice:

avax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found

Eppure il certificato nel percorso c'e', e al suo interno c'e' sia la firma del client che del server. HO provato a sbagliare appositamente la password per vedere se mi leggeva quel keystore, ed in effetti mi dice che è sbagliata, quindi il keystore lo va ad analizzare. Perchè mi dice che non c'e'?Dove sbaglio?

kingv
18-04-2005, 19:48
Ciao, ho modificato le due righe scrivendo:
Perchè mi dice che non c'e'?Dove sbaglio?


perchè per fare quello che vuoi hai bisogno sia del keystore che del truststore.

sul client
- nel keystore ci deve essere chiave e certificto del client
- nel truststore ci deve essere il certificato della CA che ha emesso quello del server (o di quest'ultimo nel caso di certificati self-signed)

sul server
- nel keystore ci deve essere chiave e certificto del server
- nel truststore ci deve essere il certificato della CA che ha emesso quello del client (o di quest'ultimo nel caso di certificati self-signed)


Ciao

Unrue
18-04-2005, 20:37
Il truststore mi serve per fare il confronto dei certificati? Io ho aggiunto il certificato del server e del client in un nuovo trusstore e quello del client e del server nel cacerts.jks nella java home che già c'era. Questi sono i passi che ho fatto:

Lato Server:

keytool -genkey -alias server-alias -keyalg RSA -keypass changeit -storepass changeit -keystore keystoreserver.jks

keytool -export -alias server-alias -storepass changeit -file server.cer -keystore keystoreserver.jks

keytool -import -v -trustcacerts -alias server-alias -file server.cer -keystore cacerts.jks -keypass changeit -storepass changeit

Lato Client:

keytool -genkey -alias client-alias -keyalg RSA -keypass changeit -storepass changeit -keystore keystoreclient.jks

keytool -export -alias client-alias -storepass changeit -file client.cer -keystore keystoreclient.jks

keytool -import -v -trustcacerts -alias client-alias -file client.cer -keystore D:\Programmi\Java\domains\domain1\config\cacerts.jks -keypass changeit -storepass changeit

keytool -import -alias client -file client.cer -keystore "D:\Programmi\Java\jdk1.5.0_01\bin\cacerts.jks" -storepass changeit

keytool -import -alias tomcat -file server.cer -keystore "D:\Programmi\Java\domains\domain1\config\cacerts.jks" -storepass changeit

In Tomcat 5 :
<!-- Define a SSL HTTP/1.1 Connector on port 8443 -->

<Connector port="8443"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="true" sslProtocol="TLS"
keystoreFile="D:\Programmi\Java\jdk1.5.0_01\bin\keystoreserver.jks" keystorePass="changeit" >
</Connector>

in Java:

System.setProperty("javax.net.ssl.keyStore","D:\\Programmi\\Java\\jdk1.5.0_01\\bin\\keystoreclient.jks");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");
System.setProperty("javax.net.ssl.trustStore","D:\\Programmi\\Java\\domains\\domain1\\config\\cacerts.jks");
System.setProperty("javax.net.ssl.trustStorePassword","changeit");


Adesso mi dice:


{http://xml.apache.org/axis/}stackTrace:java.net.SocketException: Software caused connection abort: recv failed

Scusa per la lunghezza del messaggio! Ciao e grazie.

kingv
19-04-2005, 08:08
Perchè nel codice non richiamo il truststore, ma solo il keystore.


devi settare entrambe le proprietà ma come ti ho detto puoi farle puntare allo stesso file.

Unrue
21-04-2005, 13:43
devi settare entrambe le proprietà ma come ti ho detto puoi farle puntare allo stesso file.


Ehi, sei sparito? :)

kingv
21-04-2005, 15:01
Ehi, sei sparito? :)


hai modificato dopo il post la mia risposta non si vede nella ricerca.


ma da dove hai preso il procedimento che hai utilizzato?
a parte che nella configurazione di tomcat è abilitata la client authentication ma non indichi il truststore (e quindi come fa ad autenticare gli utenti? ) ma i passaggi sono un po' a caso e manca la firma del certificato server.
Segui questo per la parte server:
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/ssl-howto.html