static DTLSTransport createClientTransport(final DatagramTransport transport) throws IOException { final DefaultTlsClient defaultTlsClient = new DefaultTlsClient() { @Override public ProtocolVersion getClientVersion() { return ProtocolVersion.DTLSv10; } @Override public ProtocolVersion getMinimumVersion() { return ProtocolVersion.DTLSv10; } @Override public TlsAuthentication getAuthentication() throws IOException { return new ServerOnlyTlsAuthentication() { @Override public void notifyServerCertificate(final Certificate serverCertificate) throws IOException { //TODO Check if certificate is signed by a trusted party. } }; } }; return new DTLSClientProtocol(SECURE_RANDOM).connect(defaultTlsClient, transport); }
public void testClientServer() throws Exception { SecureRandom secureRandom = new SecureRandom(); DTLSClientProtocol clientProtocol = new DTLSClientProtocol(secureRandom); DTLSServerProtocol serverProtocol = new DTLSServerProtocol(secureRandom); MockDatagramAssociation network = new MockDatagramAssociation(1500); ServerThread serverThread = new ServerThread(serverProtocol, network.getServer()); serverThread.start(); DatagramTransport clientTransport = network.getClient(); clientTransport = new UnreliableDatagramTransport(clientTransport, secureRandom, 0, 0); clientTransport = new LoggingDatagramTransport(clientTransport, System.out); MockDTLSClient client = new MockDTLSClient(null); DTLSTransport dtlsClient = clientProtocol.connect(client, clientTransport); for (int i = 1; i <= 10; ++i) { byte[] data = new byte[i]; Arrays.fill(data, (byte)i); dtlsClient.send(data, 0, data.length); } byte[] buf = new byte[dtlsClient.getReceiveLimit()]; while (dtlsClient.receive(buf, 0, buf.length, 1000) >= 0) { ; } dtlsClient.close(); serverThread.shutdown(); }
private static DTLSTransport openDTLSConnection(InetAddress address, int port, TlsClient client) throws IOException { DatagramSocket socket = new DatagramSocket(); socket.connect(address, port); int mtu = 1500; DatagramTransport transport = new UDPTransport(socket, mtu); transport = new UnreliableDatagramTransport(transport, secureRandom, 0, 0); transport = new LoggingDatagramTransport(transport, System.out); DTLSClientProtocol protocol = new DTLSClientProtocol(secureRandom); return protocol.connect(client, transport); }