forked from marianobarrios/tls-channel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SniBlockingServer.java
81 lines (70 loc) · 3.15 KB
/
SniBlockingServer.java
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
package tlschannel.example;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Optional;
import javax.net.ssl.SNIHostName;
import javax.net.ssl.SNIServerName;
import javax.net.ssl.SSLContext;
import tlschannel.ServerTlsChannel;
import tlschannel.SniSslContextFactory;
import tlschannel.TlsChannel;
/**
* Server example. Accepts one connection and echos bytes sent by the client into standard output.
*
* <p>To test, use: <code>
* openssl s_client -connect localhost:10000 -servername domain.com -tls1
* </code>
*/
public class SniBlockingServer {
private static final Charset utf8 = StandardCharsets.UTF_8;
public static void main(String[] args) throws IOException, GeneralSecurityException {
// initialize the SSLContext, a configuration holder, reusable object
SSLContext sslContext = ContextFactory.authenticatedContext("TLSv1.2");
/*
* Set the SSLContext factory with a lambda expression. In this case we reject the connection in all cases
* except when the supplied domain matches exacting, in which case we just return our default context. A real
* implementation would have more than one context to return according to the supplied name.
*/
SniSslContextFactory exampleSslContextFactory = (Optional<SNIServerName> sniServerName) -> {
if (!sniServerName.isPresent()) {
return Optional.empty();
}
SNIServerName name = sniServerName.get();
if (!(name instanceof SNIHostName)) {
return Optional.empty();
}
SNIHostName hostName = (SNIHostName) name;
if (hostName.getAsciiName().equals("domain.com")) {
return Optional.of(sslContext);
} else {
return Optional.empty();
}
};
// connect server socket channel normally
try (ServerSocketChannel serverSocket = ServerSocketChannel.open()) {
serverSocket.socket().bind(new InetSocketAddress(10000));
// accept raw connections normally
System.out.println("Waiting for connection...");
try (SocketChannel rawChannel = serverSocket.accept()) {
// create TlsChannel builder, combining the raw channel and the defined SSLContext factory
ServerTlsChannel.Builder builder = ServerTlsChannel.newBuilder(rawChannel, exampleSslContextFactory);
// instantiate TlsChannel
try (TlsChannel tlsChannel = builder.build()) {
// write to stdout all data sent by the client
ByteBuffer res = ByteBuffer.allocate(10000);
while (tlsChannel.read(res) != -1) {
res.flip();
System.out.print(utf8.decode(res).toString());
res.compact();
}
}
}
}
}
}