static DTLSTransport createServerTransport(final RTCCertificate rtcCertificate, final DatagramTransport transport) throws IOException { final DefaultTlsServer defaultTlsServer = new DefaultTlsServer() { private final AsymmetricKeyParameter privateKeyAsymKeyParam = PrivateKeyFactory.createKey(rtcCertificate.getKeyPair() .getPrivate() .getEncoded()); private final Certificate cCert = new Certificate(new org.bouncycastle.asn1.x509.Certificate[]{rtcCertificate.getCertificate().toASN1Structure()}); @Override protected ProtocolVersion getMaximumVersion() { return ProtocolVersion.DTLSv10; } @Override protected ProtocolVersion getMinimumVersion() { return ProtocolVersion.DTLSv10; } @Override protected TlsSignerCredentials getRSASignerCredentials() throws IOException { return new DefaultTlsSignerCredentials(this.context, this.cCert, this.privateKeyAsymKeyParam); } }; return new DTLSServerProtocol(SECURE_RANDOM).accept(defaultTlsServer, 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(); }
ServerThread(DTLSServerProtocol serverProtocol, DatagramTransport serverTransport) { this.serverProtocol = serverProtocol; this.serverTransport = serverTransport; }