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;
30 public void testClientIntitiatedRenegotiation() 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(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);
49 throw new Error("Client re-negotiation succeded (possible DoS vulnerability");
50 } catch (EOFException e) {
51 // Cool, server closed connection
56 private void work(SSLEngine se, SocketChannel s) throws SSLException, IOException {
57 while (se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING
58 && se.getHandshakeStatus() != HandshakeStatus.FINISHED) {
59 switch (se.getHandshakeStatus()) {
67 se.getDelegatedTask().run();
70 System.out.println(se.getHandshakeStatus());
75 private SSLEngineResult unwrap(SSLEngine se, SocketChannel s) throws IOException, SSLException {
76 if (inC.remaining() == 0) {
81 SSLEngineResult result = se.unwrap(inC, in);
82 if (result.getStatus() == javax.net.ssl.SSLEngineResult.Status.BUFFER_UNDERFLOW) {
83 int pos = inC.position();
84 int limit = inC.limit();
85 inC.limit(inC.capacity());
87 int read = s.read(inC);
89 throw new EOFException();
91 inC.limit(inC.position());
97 private SSLEngineResult wrap(SSLEngine se, SocketChannel s) throws SSLException, IOException {
99 SSLEngineResult result = se.wrap(out, outC);