-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathClientSSLSocket.java
More file actions
144 lines (116 loc) · 4.53 KB
/
Copy pathClientSSLSocket.java
File metadata and controls
144 lines (116 loc) · 4.53 KB
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
/* ClientSSLSocket.java
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/**
* Example SSL/TLS client using SSLSocket class.
*
* This example is compiled when "ant" is run from the main wolfssljni
* root directory.
*
* To run, usage is:
* $ ./examples/provider/ClientSSLSocket.sh [host] [port] [keystore] \
* [truststore]
*
* Note, that this uses a wrapper script to set up the correct environment
* variables for use with the wolfJSSE provider included in the wolfssljni
* package.
*
* The wrapper script enables javax.net logging, by defining:
* -Djavax.net.debug=all
*
* Example usage for connecting to the wolfSSL example server is:
*
* $ ./examples/provider/ClientSSLSocket.sh 127.0.0.1 11111 \
* ./examples/provider/client.jks ./examples/provider/client.jks
*
* The password for client.jks is: "wolfSSL test"
*/
import java.io.*;
import java.security.*;
import javax.net.ssl.*;
import com.wolfssl.provider.jsse.WolfSSLProvider;
public class ClientSSLSocket {
static String host = null;
static int port;
static String keyStorePath = null;
static char[] keyStorePass = null;
static String trustStorePath = null;
static char[] trustStorePass = null;
public static void main(String[] args) {
KeyStore ks = null; /* key store with client cert and key */
KeyStore ts = null; /* trust store with trusted roots */
TrustManagerFactory tmf = null;
KeyManagerFactory kmf = null;
System.out.println("-----------------------------------");
System.out.println("wolfSSL JSSE Example SSL/TLS Client");
System.out.println("-----------------------------------\n");
/* read in args */
if (args.length != 4) {
showUsage();
}
parseArgsAndPasswords(args);
try {
/* load wolfJSSE as provider as top priority provider */
Security.insertProviderAt(new WolfSSLProvider(), 1);
/* set up key and trust stores */
ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keyStorePath), keyStorePass);
/* NOTE: Some versions of Java/JDK do not have support for EC
* certificate types. If run on one of those versions, this
* example may fail with an ASN no signer error / -188. If that
* is the case, try again using RSA certs and CA certs instead */
ts = KeyStore.getInstance("JKS");
ts.load(new FileInputStream(trustStorePath), trustStorePass);
tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ts);
kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keyStorePass);
SSLContext ctx = SSLContext.getInstance("TLSV1.2", "wolfJSSE");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLSocketFactory sf = ctx.getSocketFactory();
SSLSocket sock = (SSLSocket)sf.createSocket(host, port);
sock.startHandshake();
sock.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static void parseArgsAndPasswords(String[] args) {
host = args[0];
port = Integer.parseInt(args[1]);
keyStorePath = args[2];
trustStorePath = args[3];
getPasswords();
}
static void getPasswords() {
Console c = System.console();
if (c == null) {
System.out.println("ERROR: Unable to get console");
System.exit(-1);
}
keyStorePass = c.readPassword("Enter keystore password: ");
trustStorePass = c.readPassword("Enter truststore password: ");
}
static void showUsage() {
System.out.println("USAGE: java ClientSSLSocket " +
"host port keyStore trustStore");
System.exit(-1);
}
}