1/*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.io.InputStream;
25import java.io.OutputStream;
26import java.net.InetSocketAddress;
27
28import javax.net.ssl.SSLContext;
29import javax.net.ssl.SSLSocket;
30import javax.net.ssl.SSLSocketFactory;
31
32/*
33 * A SSL socket client.
34 */
35public class JSSEClient {
36
37    public static void main(String[] args) throws Exception {
38        System.out.println("Client: arguments=" + String.join("; ", args));
39
40        int port = Integer.valueOf(args[0]);
41        String[] trustNames = args[1].split(TLSRestrictions.DELIMITER);
42        String[] certNames = args[2].split(TLSRestrictions.DELIMITER);
43        String constraint = args[3];
44
45        TLSRestrictions.setConstraint("Client", constraint);
46
47        SSLContext context = TLSRestrictions.createSSLContext(
48                trustNames, certNames);
49        SSLSocketFactory socketFactory = context.getSocketFactory();
50        try (SSLSocket socket = (SSLSocket) socketFactory.createSocket()) {
51            socket.connect(new InetSocketAddress("localhost", port),
52                    TLSRestrictions.TIMEOUT);
53            socket.setSoTimeout(TLSRestrictions.TIMEOUT);
54            System.out.println("Client: connected");
55
56            InputStream sslIS = socket.getInputStream();
57            OutputStream sslOS = socket.getOutputStream();
58            sslOS.write('C');
59            sslOS.flush();
60            sslIS.read();
61            System.out.println("Client: finished");
62        } catch (Exception e) {
63            throw new RuntimeException("Client: failed.", e);
64        }
65    }
66}
67