Bonjour.
Je vous expose ce que j'aimerais faire.
Principe de fonctionnement
Une machine du réseau (serveur) connait, via un keystore, tous les certificats des machines "de confiance" sur ce réseau.
Une machine souhaite se connecter (via une SSLSocket) à ce serveur.
Le serveur connait le certificat de cette machine, et inversement.
La connexion est réussi, l'échange de flux fonctionne.
Une machine souhaite se connecter, toujours via SSLSocet à ce serveur.
Le serveur ne connait pas le certificat de cette machine.
Par "ne connait pas", j'entend que ce certificat n'est pas présent dans le keystore du serveur.
Aucun échange de flux ne doit être permis.
Codage
J'utilise un SSLContext (TLS), intialisé avec un KeyManagerFactory(.getKeyManagers()) ainsi qu'un TrustManagerFactory(.getTrustManagers())
Une fois la SSLContext initialisé, je récupère une instance d'une farique à SSLSocket ou à SSLServerSocket, selon les besoins.
Voici le code source de mes classes client et serveur.
Veuillez pardonner la qualité de ces classes.
Elles n'ont été écrite que pour tester.
Les try-catch à répétition dans Server sont horribles ...
Client.java
Server.java
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 /* * Client.java * 9 juil. 2008 */ package fr.pwn3d.test.sslsocket; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; 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; /** * Socket TLS cliente utilisant un keystore. * * @author yoyo * @version 0.1 */ public final class Client { /** 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"; /** * @param args host, port, keystore, pass. * @throws KeyStoreException * @throws IOException * @throws FileNotFoundException * @throws CertificateException * @throws NoSuchAlgorithmException * @throws UnrecoverableKeyException * @throws KeyManagementException */ public static void main(String[] args) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException, KeyManagementException { // Récupérer les infos. final String host = args[0]; final int port = new Integer(args[1]).intValue(); final String keystorePath = args[2]; final String keystorePass = args[3]; // 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(new FileInputStream(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(); socket = (SSLSocket) sslsf.createSocket(host, port); // Envoyer chaque saisie clavier au serveur. BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); String write = null; while(true) { write = keyboard.readLine(); out.println(write); out.flush(); } } }
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 /* * Server.java * 10 juil. 2008 */ package fr.pwn3d.test.sslsocket; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; 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.SSLSocket; import javax.net.ssl.TrustManagerFactory; /** * Socket TLS serveur utilisant un keystore. * * @author yoyo * @version 0.1 */ public final class Server { /** 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"; /** * @param args host, port, keystore, pass. * @throws KeyStoreException * @throws IOException * @throws FileNotFoundException * @throws CertificateException * @throws NoSuchAlgorithmException * @throws UnrecoverableKeyException * @throws KeyManagementException */ public static void main(String[] args) { // Récupérer les infos. final int port = new Integer(args[0]).intValue(); final String keystorePath = args[1]; final String keystorePass = args[2 ]; // Les objets que l'on va utiliser. SSLSocket client = null; SSLServerSocket listen = null; SSLServerSocketFactory sslssf = 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(new FileInputStream(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 instace 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. sslssf = sslc.getServerSocketFactory(); try { System.out.print("Récupération et configuration de SSL fac ... "); listen = (SSLServerSocket) sslssf.createServerSocket(port); listen.setUseClientMode(false); System.out.print("OK\n"); } catch (IOException e) { e.printStackTrace(); } // Attendre la connexion d'un client. try { System.out.print("Attendre une connexion ... "); client = (SSLSocket) listen.accept(); System.out.print("Client connecte\n"); } catch (IOException e) { e.printStackTrace(); } // Lire le message du client. BufferedReader in; try { in = new BufferedReader( new InputStreamReader(client.getInputStream())); String read = null; while(true) { read = in.readLine(); System.out.println("Client : " + read); } } catch (IOException e) { e.printStackTrace(); } } }
Le problème
Un client possède dans son keystore la clef publique du serveur.
Le serveur ne possède pas dans on keystore la clef publique du client.
Aucun transfert de flux ne doit être fait. Le serveur ne doit pas parler au client.
Le client parvient tout de même à échanger des flux avec le serveur...
Vous voyez le problème ? Vous pensez que ce que je demande est réalisable ?
Je remercie toute personne qui prendra de son temps pour essayer de trouver une solution à mon problème.
Partager