1 package org.cacert.gigi;
3 import java.io.EOFException;
4 import java.io.IOException;
5 import java.net.InetSocketAddress;
6 import java.nio.ByteBuffer;
7 import java.nio.channels.SocketChannel;
8 import java.security.NoSuchAlgorithmException;
10 import javax.net.ssl.SSLContext;
11 import javax.net.ssl.SSLEngine;
12 import javax.net.ssl.SSLEngineResult;
13 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
14 import javax.net.ssl.SSLException;
16 import org.cacert.gigi.testUtils.ManagedTest;
17 import org.junit.Test;
19 public class TestSSL extends ManagedTest {
20 private ByteBuffer in;
21 private ByteBuffer inC;
22 private ByteBuffer outC;
23 private ByteBuffer out;
28 public void testClientIntitiatedRenegotiation()
29 throws NoSuchAlgorithmException, IOException {
30 SSLContext sc = SSLContext.getDefault();
31 SSLEngine se = sc.createSSLEngine();
32 String[] serverParts = getServerName().split(":", 2);
33 SocketChannel s = SocketChannel.open(new InetSocketAddress(
34 serverParts[0], Integer.parseInt(serverParts[1])));
36 in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
37 inC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
39 out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
40 outC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
42 se.setUseClientMode(true);
50 "Client re-negotiation succeded (possible DoS vulnerability");
51 } catch (EOFException e) {
52 // Cool, server closed connection
56 private void work(SSLEngine se, SocketChannel s) throws SSLException,
58 while (se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING
59 && se.getHandshakeStatus() != HandshakeStatus.FINISHED) {
60 switch (se.getHandshakeStatus()) {
68 se.getDelegatedTask().run();
71 System.out.println(se.getHandshakeStatus());
75 private SSLEngineResult unwrap(SSLEngine se, SocketChannel s)
76 throws IOException, SSLException {
77 if (inC.remaining() == 0) {
82 SSLEngineResult result = se.unwrap(inC, in);
83 if (result.getStatus() == javax.net.ssl.SSLEngineResult.Status.BUFFER_UNDERFLOW) {
84 int pos = inC.position();
85 int limit = inC.limit();
86 inC.limit(inC.capacity());
88 int read = s.read(inC);
90 throw new EOFException();
92 inC.limit(inC.position());
97 private SSLEngineResult wrap(SSLEngine se, SocketChannel s)
98 throws SSLException, IOException {
100 SSLEngineResult result = se.wrap(out, outC);