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.InitTruststore;
17 import org.cacert.gigi.testUtils.ManagedTest;
18 import org.junit.Test;
20 public class TestSSL extends ManagedTest {
21 private ByteBuffer in;
22 private ByteBuffer inC;
23 private ByteBuffer outC;
24 private ByteBuffer out;
29 public void testClientIntitiatedRenegotiation()
30 throws NoSuchAlgorithmException, IOException {
31 SSLContext sc = SSLContext.getDefault();
32 SSLEngine se = sc.createSSLEngine();
33 String[] serverParts = getServerName().split(":", 2);
34 SocketChannel s = SocketChannel.open(new InetSocketAddress(
35 serverParts[0], Integer.parseInt(serverParts[1])));
37 in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
38 inC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
40 out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
41 outC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
43 se.setUseClientMode(true);
51 "Client re-negotiation succeded (possible DoS vulnerability");
52 } catch (EOFException e) {
53 // Cool, server closed connection
57 private void work(SSLEngine se, SocketChannel s) throws SSLException,
59 while (se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING
60 && se.getHandshakeStatus() != HandshakeStatus.FINISHED) {
61 switch (se.getHandshakeStatus()) {
69 se.getDelegatedTask().run();
72 System.out.println(se.getHandshakeStatus());
76 private SSLEngineResult unwrap(SSLEngine se, SocketChannel s)
77 throws IOException, SSLException {
78 if (inC.remaining() == 0) {
83 SSLEngineResult result = se.unwrap(inC, in);
84 if (result.getStatus() == javax.net.ssl.SSLEngineResult.Status.BUFFER_UNDERFLOW) {
85 int pos = inC.position();
86 int limit = inC.limit();
87 inC.limit(inC.capacity());
89 int read = s.read(inC);
91 throw new EOFException();
93 inC.limit(inC.position());
98 private SSLEngineResult wrap(SSLEngine se, SocketChannel s)
99 throws SSLException, IOException {
101 SSLEngineResult result = se.wrap(out, outC);