1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix.demo.ssl;
19
20 import java.io.File;
21 import java.io.FileDescriptor;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.charset.StandardCharsets;
26 import java.security.GeneralSecurityException;
27
28 import javax.net.ssl.SSLSocket;
29 import javax.net.ssl.SSLSocketFactory;
30 import javax.security.auth.DestroyFailedException;
31
32 import org.newsclub.net.unix.AFUNIXSocket;
33 import org.newsclub.net.unix.AFUNIXSocketAddress;
34 import org.newsclub.net.unix.ssl.SSLContextBuilder;
35
36
37
38
39
40
41
42
43 public class SSLDemoClient {
44 public static void main(String[] args) throws InterruptedException, IOException,
45 GeneralSecurityException, DestroyFailedException {
46
47
48
49
50 AFUNIXSocketAddress addr = AFUNIXSocketAddress.of(new File("/tmp/ssldemo"));
51
52 SSLSocketFactory clientSocketFactory = SSLContextBuilder.forClient()
53 .withKeyStore(new File("juxclient.p12"), () -> "clientpass".toCharArray())
54 .withTrustStore(new File("juxclient.truststore"), () -> "clienttrustpass".toCharArray())
55 .withDefaultSSLParameters((p) -> {
56
57
58
59
60
61
62 })
63 .buildAndDestroyBuilder().getSocketFactory();
64
65 System.out.println("Connecting to " + addr);
66
67 try (AFUNIXSocket plainSocket = AFUNIXSocket.connectTo(addr);
68 SSLSocket sslSocket = (SSLSocket) clientSocketFactory.createSocket(plainSocket,
69
70 "localhost.junixsocket",
71 plainSocket.getPort(), false)) {
72
73
74
75
76
77
78
79 try (InputStream in = sslSocket.getInputStream();
80 OutputStream out = sslSocket.getOutputStream()) {
81 plainSocket.setOutboundFileDescriptors(FileDescriptor.err);
82 System.out.println("Writing byte...");
83 out.write(0xAF);
84 out.flush();
85
86 byte[] by = new byte[11];
87 int r = in.read(by);
88 System.out.println("Received string: " + new String(by, 0, r, StandardCharsets.UTF_8));
89 }
90 }
91 }
92 }