Java 类org.bouncycastle.crypto.tls.DTLSTransport 实例源码

项目:rtcdcjava    文件:DTLSTransportFactoryTest.java   
private boolean receiveACK(final DTLSTransport clientTransport) throws IOException {

        final byte[] receiveBuffer = new byte[1500];

        final int len;
        if ((len = clientTransport.receive(receiveBuffer,
                                           0,
                                           1500,
                                           1000)) != -1) {
            Assert.assertEquals("ACK",
                                new String(receiveBuffer,
                                           0,
                                           len));

            return true;
        }
        else {
            return false;
        }
    }
项目:irma_future_id    文件:DTLSProtocolTest.java   
public void run()
{
    try
    {
        MockDTLSServer server = new MockDTLSServer();
        DTLSTransport dtlsServer = serverProtocol.accept(server, serverTransport);
        byte[] buf = new byte[dtlsServer.getReceiveLimit()];
        while (!isShutdown)
        {
            int length = dtlsServer.receive(buf, 0, buf.length, 1000);
            if (length >= 0)
            {
                dtlsServer.send(buf, 0, length);
            }
        }
        dtlsServer.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
项目:bc-java    文件:DTLSProtocolTest.java   
public void run()
{
    try
    {
        MockDTLSServer server = new MockDTLSServer();
        DTLSTransport dtlsServer = serverProtocol.accept(server, serverTransport);
        byte[] buf = new byte[dtlsServer.getReceiveLimit()];
        while (!isShutdown)
        {
            int length = dtlsServer.receive(buf, 0, buf.length, 1000);
            if (length >= 0)
            {
                dtlsServer.send(buf, 0, length);
            }
        }
        dtlsServer.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
项目:rtcdcjava    文件:DTLSTransportFactory.java   
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);
}
项目:rtcdcjava    文件:DTLSTransportFactory.java   
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);
    }
项目:irma_future_id    文件:DTLSProtocolTest.java   
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();
}
项目:irma_future_id    文件:DTLSClientTest.java   
public static void main(String[] args)
    throws Exception
{
    InetAddress address = InetAddress.getLocalHost();
    int port = 5556;

    TlsSession session = createSession(address, port);

    MockDTLSClient client = new MockDTLSClient(session);

    DTLSTransport dtls = openDTLSConnection(address, port, client);

    System.out.println("Receive limit: " + dtls.getReceiveLimit());
    System.out.println("Send limit: " + dtls.getSendLimit());

    // Send and hopefully receive a packet back

    byte[] request = "Hello World!\n".getBytes("UTF-8");
    dtls.send(request, 0, request.length);

    byte[] response = new byte[dtls.getReceiveLimit()];
    int received = dtls.receive(response, 0, response.length, 30000);
    if (received >= 0)
    {
        System.out.println(new String(response, 0, received, "UTF-8"));
    }

    dtls.close();
}
项目:irma_future_id    文件:DTLSClientTest.java   
private static TlsSession createSession(InetAddress address, int port)
    throws IOException
{
    MockDTLSClient client = new MockDTLSClient(null);
    DTLSTransport dtls = openDTLSConnection(address, port, client);
    TlsSession session = client.getSessionToResume();
    dtls.close();
    return session;
}
项目:irma_future_id    文件:DTLSClientTest.java   
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);
}
项目:bc-java    文件:DTLSProtocolTest.java   
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();
}
项目:bc-java    文件:DTLSClientTest.java   
public static void main(String[] args)
    throws Exception
{
    InetAddress address = InetAddress.getLocalHost();
    int port = 5556;

    TlsSession session = createSession(address, port);

    MockDTLSClient client = new MockDTLSClient(session);

    DTLSTransport dtls = openDTLSConnection(address, port, client);

    System.out.println("Receive limit: " + dtls.getReceiveLimit());
    System.out.println("Send limit: " + dtls.getSendLimit());

    // Send and hopefully receive a packet back

    byte[] request = "Hello World!\n".getBytes("UTF-8");
    dtls.send(request, 0, request.length);

    byte[] response = new byte[dtls.getReceiveLimit()];
    int received = dtls.receive(response, 0, response.length, 30000);
    if (received >= 0)
    {
        System.out.println(new String(response, 0, received, "UTF-8"));
    }

    dtls.close();
}
项目:bc-java    文件:DTLSClientTest.java   
private static TlsSession createSession(InetAddress address, int port)
    throws IOException
{
    MockDTLSClient client = new MockDTLSClient(null);
    DTLSTransport dtls = openDTLSConnection(address, port, client);
    TlsSession session = client.getSessionToResume();
    dtls.close();
    return session;
}
项目:bc-java    文件:DTLSClientTest.java   
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);
}