bonjour à tous,

Je vais vous expliquer le problème que je rencontre.

Je fais une application client / serveur, avec des sockets SSL.

Voici le code du serveur :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
 
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.TrustManagerFactory;
 
public class EchoServer {
 
	/** Algorihtme de certification. */
	public static final String ALGORITHM = "sunx509";
 
	/** Type du conteneur keystore. */
	public static final String KEYSTORE_TYPE = "JKS";
 
	/** Protocole SSL à utiliser. */
	public static final String PROTOCOL = "TLS"; 
 
	static Thread t;
 
	public static void main(String[] arstring) {
 
		// Récupérer les infos.
		final int port = 9999;
		final String keystorePath = "mySrvKeystore";
		final String keystorePass = "123456";
 
		// Les objets que l'on va utiliser.
 
		SSLServerSocket sslserversocket = null;
		SSLServerSocketFactory sslserversocketfactory = null;
		KeyManagerFactory kmf = null;
		KeyStore ks = null;
		TrustManagerFactory tmf = null;
		SSLContext sslc = null;
 
		// Récupérer la keystore et charger le fichier.
		try {
 
			System.out.print("Récupération du keystore... ");
			ks = KeyStore.getInstance(KEYSTORE_TYPE);
 
			ks.load(ClassLoader.getSystemResourceAsStream(keystorePath),
					keystorePass.toCharArray());			
			System.out.print("OK\n");
 
		} catch (KeyStoreException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (CertificateException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
		// Récupérer une instance d'un KeyManagerFactory et l'initialiser.
		try {
			System.out.print("Récupération et configuration du KeyManager...");
			kmf = KeyManagerFactory.getInstance(ALGORITHM);
			kmf.init(ks, keystorePass.toCharArray());
			System.out.print(" OK\n");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnrecoverableKeyException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
 
 
		// Récupérer une instance d'un TrustManagerFactory et l'initialiser.
		try {
			System.out.print("Récupération et configuration du Trust... ");
			tmf = TrustManagerFactory.getInstance(ALGORITHM);
			tmf.init(ks);
			System.out.print("OK\n");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
 
 
		// Récupérer une instance d'un SSLContext et l'initialiser.
		try {
			System.out.print("Récupération et configuration de SSL ... ");
			sslc = SSLContext.getInstance(PROTOCOL);
			sslc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
			System.out.print("OK\n");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		}
 
		// Récupérer une instance d'une fabrique à socket SSL,
		// puis intancier une socket.
		sslserversocketfactory = sslc.getServerSocketFactory();
		try {
			System.out.print("Récupération et configuration de SSL fac ... ");
			sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(port);
			sslserversocket.setUseClientMode(false);
			sslserversocket.setNeedClientAuth(true);
			t = new Thread(new Accepter_connexion(sslserversocket));
			t.start();
			System.out.println("Lancement");
			System.out.print("OK\n");
 
 
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	}
 
}
Celui de mon client :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
 
package fr.astre.client;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
 
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
 
 
public final class Client {
 
	boolean error = true;
 
	/** Trame d'authentification. */
	private String login = null;
	private String mdp = null;
 
	/** Algorihtme de certification. */
	public static final String ALGORITHM = "sunx509";
 
	/** Type du conteneur keystore. */
	public static final String KEYSTORE_TYPE = "JKS";
 
	/** Protocole SSL à utiliser. */
	public static final String PROTOCOL = "TLS";
 
	/** Canaux de communication. */
	private PrintWriter out;
	private BufferedReader in;
 
	private BufferedWriter fileCfg = null;
	private SSLSocket socket = null;
 
	/**
         * @param args
         *            host, port, keystore, pass.
         * @throws KeyStoreException
         * @throws KeyStoreException
         * @throws IOException
         * @throws FileNotFoundException
         * @throws CertificateException
         * @throws NoSuchAlgorithmException
         * @throws IOException
         * @throws FileNotFoundException
         * @throws CertificateException
         * @throws NoSuchAlgorithmException
         * @throws UnrecoverableKeyException
         * @throws UnrecoverableKeyException
         * @throws KeyManagementException
         * @throws KeyManagementException
         */
	public Client(String login, String mdp) throws KeyStoreException,
			NoSuchAlgorithmException, CertificateException,
			FileNotFoundException, IOException, UnrecoverableKeyException,
			KeyManagementException {
 
		this.login = login;
		this.mdp = mdp;
 
		// Récupérer les infos.
		final String host = "127.0.0.1";
		final int port = 9999;
		final String keystorePath = "mySrvKeystore";
		final String keystorePass = "123456";
 
 
		// Les objets que l'on va utiliser.
		// SSLSocket socket = null;
		SSLSocketFactory sslsf = null;
		KeyManagerFactory kmf = null;
		KeyStore ks = null;
		TrustManagerFactory tmf = null;
		SSLContext sslc = null;
 
		// Récupérer la keystore et charger le fichier.
		ks = KeyStore.getInstance(KEYSTORE_TYPE);
		ks.load(ClassLoader.getSystemResourceAsStream(keystorePath), keystorePass.toCharArray());
 
		// Récupérer une instance d'un KeyManagerFactory et l'initialiser.
		kmf = KeyManagerFactory.getInstance(ALGORITHM);
		kmf.init(ks, keystorePass.toCharArray());
 
		// Récupérer une instance d'un TrustManagerFactory et l'initialiser.
		tmf = TrustManagerFactory.getInstance(ALGORITHM);
		tmf.init(ks);
 
		// Récupérer une instace d'un SSLContext et l'initialiser.
		sslc = SSLContext.getInstance(PROTOCOL);
		sslc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
 
		// Récupérer une instance d'une fabrique à socket SSL,
		// puis intancier une socket.
		sslsf = sslc.getSocketFactory();
 
		try {
			socket = (SSLSocket) sslsf.createSocket(host, port);
			out = new PrintWriter(socket.getOutputStream());
			in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			error = false;
 
		} catch (java.net.ConnectException e) {
			// TODO Auto-generated catch block
			System.err.println("Client : " + e.getMessage());
			error = true;
			/**
                         * Erreurs possibles : Firewall qui bloque l'ouverture d'une socket
                         * Serveur distant non joignable
                         * 
                         * 
                         */
 
		}
 
	}
 
	public boolean Connexion() {
		boolean succes = false;
		String temp;
		if (!this.error) {
			try {
 
				// Réponse du serveur
				temp = in.readLine();
				System.out.println("Serveur 1: '" + temp + "'");
				if (temp.equals(dial.getAuthLog())) {
					out.println(login);
					out.flush();
 
					temp = in.readLine();
					System.out.println("Serveur 2: '" + temp + "'");
 
					if (temp.equals(dial.getAuthMdp())) {
 
						out.println(mdp);
						out.flush();
 
						temp = in.readLine();
						System.out.println("Serveur 3: '" + temp + "'");
 
						if (temp.equals(dial.getAuthSucc())) {
							temp = in.readLine();
							System.out.println("Serveur 4: '" + temp + "'");
 
							if (temp.equals(dial.getSendCfg())) {
								System.out.println("Reception FIchier ??");
								// Si le fichier est bien réceptionné alors on
								// ferme la
								// connexion
								if (this.recpFile()) {
									this.close();
									succes = true;
								}
 
							}
						} else if (temp.equals(dial.getAuthFail())) {
							this.close();
						}
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
				this.close();
			}
 
		}
 
		return succes; // retourne true si ok
	}
 
	private void close() {
		/**
                 * Averti le serveur que le client ferme la connexion
                 */
		out.println(dial.getDecoCli());
		out.flush();
		try {
			socket.close();
			System.out.println("La connexion a été fermée.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}


Est ce que j'utilise la bonne méthode pour utiliser les certificats ?

Pour générer le keystore j'ai utiliser cette commande :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
keytool -genkey -keystore mySrvKeystore -keyalg RSA
le fichier généré ( mySrvKeystore ) a été ajouté au buildpath du client et du serveur.
Pour qu'il puisse lire les infos dedans.

Le problème c'est que si le client a le même fichier que le serveur au niveau sécurité ça ne sert à rien non ???

Dans le principe des clés privées/publiques il y a un fichier pour le serveur et un pour le client qui sont différents. Comment puis-je résoudre ce problème ?

Merci à tous !



J'ai utiliser en gros ce tuto :
http://stilius.net/java/java_ssl.php