1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.filter.ssl;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.net.InetAddress;
25 import java.net.InetSocketAddress;
26 import java.net.Socket;
27 import java.security.GeneralSecurityException;
28 import java.security.KeyStore;
29 import java.security.Security;
30
31 import javax.net.ssl.KeyManagerFactory;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.SSLSocketFactory;
34 import javax.net.ssl.TrustManagerFactory;
35
36 import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
37 import org.apache.mina.core.service.IoHandlerAdapter;
38 import org.apache.mina.core.session.IoSession;
39 import org.apache.mina.filter.codec.ProtocolCodecFilter;
40 import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
41 import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
42 import org.apache.mina.util.AvailablePortFinder;
43 import org.junit.Test;
44
45
46
47
48
49
50
51 public class SslTest {
52
53 private static final int port = AvailablePortFinder.getNextAvailable(5555);
54
55 private static Exception clientError = null;
56
57 private static InetAddress address;
58
59 private static SSLSocketFactory factory;
60
61
62 private static final String KEY_MANAGER_FACTORY_ALGORITHM;
63
64 static {
65 String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
66 if (algorithm == null) {
67 algorithm = KeyManagerFactory.getDefaultAlgorithm();
68 }
69
70 KEY_MANAGER_FACTORY_ALGORITHM = algorithm;
71 }
72
73 private static class TestHandler extends IoHandlerAdapter {
74 public void messageReceived(IoSession session, Object message) throws Exception {
75 String line = (String) message;
76
77 if (line.startsWith("hello")) {
78 System.out.println("Server got: 'hello', waiting for 'send'");
79 Thread.sleep(1500);
80 } else if (line.startsWith("send")) {
81 System.out.println("Server got: 'send', sending 'data'");
82 session.write("data");
83 }
84 }
85 }
86
87
88
89
90
91 private static void startServer() throws Exception {
92 NioSocketAcceptor acceptor = new NioSocketAcceptor();
93
94 acceptor.setReuseAddress(true);
95 DefaultIoFilterChainBuilder filters = acceptor.getFilterChain();
96
97
98 SslFilter sslFilter = new SslFilter(createSSLContext());
99 filters.addLast("sslFilter", sslFilter);
100
101
102 filters.addLast("text", new ProtocolCodecFilter(new TextLineCodecFactory()));
103
104 acceptor.setHandler(new TestHandler());
105 acceptor.bind(new InetSocketAddress(port));
106 }
107
108
109
110
111 private static void startClient() throws Exception {
112 address = InetAddress.getByName("localhost");
113
114 SSLContext context = createSSLContext();
115 factory = context.getSocketFactory();
116
117 connectAndSend();
118
119
120 connectAndSend();
121 }
122
123 private static void connectAndSend() throws Exception {
124 Socket parent = new Socket(address, port);
125 Socket socket = factory.createSocket(parent, address.getCanonicalHostName(), port, false);
126
127 System.out.println("Client sending: hello");
128 socket.getOutputStream().write("hello \n".getBytes());
129 socket.getOutputStream().flush();
130 socket.setSoTimeout(10000);
131
132 System.out.println("Client sending: send");
133 socket.getOutputStream().write("send\n".getBytes());
134 socket.getOutputStream().flush();
135
136 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
137 String line = in.readLine();
138 System.out.println("Client got: " + line);
139 socket.close();
140
141 }
142
143 private static SSLContext createSSLContext() throws IOException, GeneralSecurityException {
144 char[] passphrase = "password".toCharArray();
145
146 SSLContext ctx = SSLContext.getInstance("TLS");
147 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEY_MANAGER_FACTORY_ALGORITHM);
148 TrustManagerFactory tmf = TrustManagerFactory.getInstance(KEY_MANAGER_FACTORY_ALGORITHM);
149
150 KeyStore ks = KeyStore.getInstance("JKS");
151 KeyStore ts = KeyStore.getInstance("JKS");
152
153 ks.load(SslTest.class.getResourceAsStream("keystore.sslTest"), passphrase);
154 ts.load(SslTest.class.getResourceAsStream("truststore.sslTest"), passphrase);
155
156 kmf.init(ks, passphrase);
157 tmf.init(ts);
158 ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
159
160 return ctx;
161 }
162
163 @Test
164 public void testSSL() throws Exception {
165 startServer();
166
167 Thread t = new Thread() {
168 public void run() {
169 try {
170 startClient();
171 } catch (Exception e) {
172 clientError = e;
173 }
174 }
175 };
176 t.start();
177 t.join();
178 if (clientError != null)
179 throw clientError;
180 }
181 }