|
| 1 | +/* MultiThreadedSSLClient.java |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2021 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * Multi threaded SSLSocket example that connects a specified number of |
| 24 | + * client threads to a server. Intended to test multi-threading with wolfJSSE. |
| 25 | + * |
| 26 | + * This example creates a specified number of client threads to a server |
| 27 | + * located at 127.0.0.1:11118. This example is set up to use the SSLSocket |
| 28 | + * class. It makes one connection (handshake), sends/receives data, and shuts |
| 29 | + * down. |
| 30 | + * |
| 31 | + * A random amount of time is injected into each client thread before: |
| 32 | + * 1) The SSL/TLS handshake |
| 33 | + * 2) Doing I/O operations after the handshake |
| 34 | + * |
| 35 | + * The maximum amount of sleep time for each of those is "maxSleep", or |
| 36 | + * 3 seconds by default. This is intended to add some randomness into the |
| 37 | + * the client thread operations. |
| 38 | + * |
| 39 | + * Example usage: |
| 40 | + * |
| 41 | + * $ ant examples |
| 42 | + * $ ./examples/provider/MultiThreadedSSLClient.java |
| 43 | + * |
| 44 | + * This example is designed to connect against the MultiThreadedSSLServer |
| 45 | + * example: |
| 46 | + * |
| 47 | + * $ ./examples/provider/MultiThreadedSSLServer.java |
| 48 | + * |
| 49 | + * This example also prints out average SSL/TLS handshake time, which is |
| 50 | + * measured in milliseconds on the "startHandshake()" API call. |
| 51 | + */ |
| 52 | + |
| 53 | +import java.util.*; |
| 54 | +import java.io.*; |
| 55 | +import java.net.*; |
| 56 | +import javax.net.ssl.*; |
| 57 | +import java.security.*; |
| 58 | +import java.util.concurrent.CountDownLatch; |
| 59 | +import java.util.concurrent.ExecutorService; |
| 60 | +import java.util.concurrent.Executor; |
| 61 | +import java.util.concurrent.Executors; |
| 62 | +import java.util.concurrent.ThreadLocalRandom; |
| 63 | +import com.wolfssl.provider.jsse.WolfSSLProvider; |
| 64 | + |
| 65 | +public class MultiThreadedSSLClient |
| 66 | +{ |
| 67 | + String tmfImpl = "SunX509"; /* TrustManagerFactory provider */ |
| 68 | + String kmfImpl = "SunX509"; /* KeyManagerFactory provider */ |
| 69 | + String ctxImpl = "wolfJSSE"; /* SSLContext provider */ |
| 70 | + |
| 71 | + String srvHost = "127.0.0.1"; /* server host */ |
| 72 | + int srvPort = 11118; /* server port */ |
| 73 | + |
| 74 | + int numClientConnections = 10; /* number of client connection threads */ |
| 75 | + int startedClientConnections = 0; /* active clients connected to server */ |
| 76 | + int successClientConnections = 0; /* successful client connections */ |
| 77 | + int failedClientConnections = 0; /* failed client connections */ |
| 78 | + |
| 79 | + long totalConnectionTimeMs = 0; /* total handshake time, across clients */ |
| 80 | + final Object timeLock = new Object(); |
| 81 | + |
| 82 | + class ClientThread implements Runnable |
| 83 | + { |
| 84 | + private KeyManagerFactory km = null; |
| 85 | + private TrustManagerFactory tm = null; |
| 86 | + private CountDownLatch latch; |
| 87 | + |
| 88 | + public ClientThread(KeyManagerFactory km, TrustManagerFactory tm, |
| 89 | + CountDownLatch latch) { |
| 90 | + this.km = km; |
| 91 | + this.tm = tm; |
| 92 | + this.latch = latch; |
| 93 | + } |
| 94 | + |
| 95 | + public void run() { |
| 96 | + |
| 97 | + byte[] back = new byte[80]; |
| 98 | + String msg = "Too legit to quit"; |
| 99 | + |
| 100 | + /* max sleep is 3 seconds */ |
| 101 | + int maxSleep = 3000; |
| 102 | + |
| 103 | + /* get random sleep value before calling connect() */ |
| 104 | + int randConnectSleep = |
| 105 | + ThreadLocalRandom.current().nextInt(0, maxSleep + 1); |
| 106 | + |
| 107 | + /* get random sleep value before doing I/O after handshake */ |
| 108 | + int randIOSleep = |
| 109 | + ThreadLocalRandom.current().nextInt(0, maxSleep +1); |
| 110 | + |
| 111 | + try { |
| 112 | + SSLContext ctx = SSLContext.getInstance("TLS", ctxImpl); |
| 113 | + ctx.init(km.getKeyManagers(), tm.getTrustManagers(), null); |
| 114 | + |
| 115 | + SSLSocket sock = (SSLSocket)ctx.getSocketFactory() |
| 116 | + .createSocket(); |
| 117 | + |
| 118 | + Thread.sleep(randConnectSleep); |
| 119 | + |
| 120 | + sock.connect(new InetSocketAddress(srvHost, srvPort)); |
| 121 | + |
| 122 | + final long startTime = System.currentTimeMillis(); |
| 123 | + sock.startHandshake(); |
| 124 | + final long endTime = System.currentTimeMillis(); |
| 125 | + |
| 126 | + synchronized (timeLock) { |
| 127 | + totalConnectionTimeMs += (endTime - startTime); |
| 128 | + } |
| 129 | + |
| 130 | + Thread.sleep(randIOSleep); |
| 131 | + |
| 132 | + sock.getOutputStream().write(msg.getBytes()); |
| 133 | + sock.getInputStream().read(back); |
| 134 | + System.out.println("Server message : " + new String(back)); |
| 135 | + |
| 136 | + sock.close(); |
| 137 | + successClientConnections++; |
| 138 | + |
| 139 | + } catch (Exception e) { |
| 140 | + e.printStackTrace(); |
| 141 | + failedClientConnections++; |
| 142 | + } |
| 143 | + |
| 144 | + this.latch.countDown(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + public MultiThreadedSSLClient(String[] args) { |
| 149 | + |
| 150 | + Security.addProvider(new WolfSSLProvider()); |
| 151 | + |
| 152 | + String clientKS = "./examples/provider/client.jks"; |
| 153 | + String clientTS = "./examples/provider/client.jks"; |
| 154 | + String jkspass = "wolfSSL test"; |
| 155 | + char[] passArr = jkspass.toCharArray(); |
| 156 | + |
| 157 | + if (args.length != 2) { |
| 158 | + printUsage(); |
| 159 | + } |
| 160 | + |
| 161 | + /* pull in command line options from user */ |
| 162 | + for (int i = 0; i < args.length; i++) |
| 163 | + { |
| 164 | + String arg = args[i]; |
| 165 | + |
| 166 | + if (arg.equals("-n")) { |
| 167 | + if (args.length < i+2) |
| 168 | + printUsage(); |
| 169 | + numClientConnections = Integer.parseInt(args[++i]); |
| 170 | + |
| 171 | + } else { |
| 172 | + printUsage(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + try { |
| 177 | + List<ClientThread> clientList = new ArrayList<ClientThread>(); |
| 178 | + CountDownLatch latch = new CountDownLatch(numClientConnections); |
| 179 | + |
| 180 | + /* set up client KeyStore */ |
| 181 | + KeyStore clientKeyStore = KeyStore.getInstance("JKS"); |
| 182 | + clientKeyStore.load(new FileInputStream(clientKS), passArr); |
| 183 | + |
| 184 | + KeyManagerFactory clientKMF = |
| 185 | + KeyManagerFactory.getInstance(kmfImpl); |
| 186 | + clientKMF.init(clientKeyStore, passArr); |
| 187 | + |
| 188 | + /* set up CA TrustManagerFactory */ |
| 189 | + KeyStore caKeyStore = KeyStore.getInstance("JKS"); |
| 190 | + caKeyStore.load(new FileInputStream(clientTS), passArr); |
| 191 | + |
| 192 | + TrustManagerFactory tm = TrustManagerFactory.getInstance(tmfImpl); |
| 193 | + tm.init(caKeyStore); |
| 194 | + |
| 195 | + for (int i = 0; i < numClientConnections; i++) { |
| 196 | + clientList.add(new ClientThread(clientKMF, tm, latch)); |
| 197 | + } |
| 198 | + |
| 199 | + ExecutorService executor = Executors.newFixedThreadPool( |
| 200 | + clientList.size()); |
| 201 | + |
| 202 | + for (final ClientThread c : clientList) { |
| 203 | + executor.execute(c); |
| 204 | + } |
| 205 | + |
| 206 | + latch.await(); |
| 207 | + executor.shutdown(); |
| 208 | + |
| 209 | + } catch (Exception e) { |
| 210 | + e.printStackTrace(); |
| 211 | + } |
| 212 | + |
| 213 | + Security.removeProvider("wolfJSSE"); |
| 214 | + |
| 215 | + System.out.println("================================================"); |
| 216 | + System.out.println("All Client Connections Finished"); |
| 217 | + System.out.println("Successful = " + successClientConnections); |
| 218 | + System.out.println("Failed = " + failedClientConnections); |
| 219 | + System.out.println("Avg handshake time = " + |
| 220 | + totalConnectionTimeMs / successClientConnections + " ms"); |
| 221 | + System.out.println("================================================"); |
| 222 | + } |
| 223 | + |
| 224 | + |
| 225 | + public static void main(String[] args) { |
| 226 | + new MultiThreadedSSLClient(args); |
| 227 | + } |
| 228 | + |
| 229 | + private void printUsage() { |
| 230 | + System.out.println("Java wolfJSSE example threaded client usage:"); |
| 231 | + System.out.println("-n <num>\tNumber of client connections"); |
| 232 | + System.exit(1); |
| 233 | + } |
| 234 | +} |
| 235 | + |
0 commit comments