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.junit.Test;
18 public class TestSSL {
19 private ByteBuffer in;
20 private ByteBuffer inC;
21 private ByteBuffer outC;
22 private ByteBuffer out;
27 public void testClientIntitiatedRenegotiation()
28 throws NoSuchAlgorithmException, IOException {
29 SSLContext sc = SSLContext.getDefault();
30 SSLEngine se = sc.createSSLEngine();
31 SocketChannel s = SocketChannel.open(new InetSocketAddress("localhost",
34 in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
35 inC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
37 out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
38 outC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
40 se.setUseClientMode(true);
48 "Client re-negotiation succeded (possible DoS vulnerability");
49 } catch (EOFException e) {
50 // Cool, server closed connection
54 private void work(SSLEngine se, SocketChannel s) throws SSLException,
56 while (se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING
57 && se.getHandshakeStatus() != HandshakeStatus.FINISHED) {
58 switch (se.getHandshakeStatus()) {
66 se.getDelegatedTask().run();
69 System.out.println(se.getHandshakeStatus());
73 private SSLEngineResult unwrap(SSLEngine se, SocketChannel s)
74 throws IOException, SSLException {
75 if (inC.remaining() == 0) {
80 SSLEngineResult result = se.unwrap(inC, in);
81 if (result.getStatus() == javax.net.ssl.SSLEngineResult.Status.BUFFER_UNDERFLOW) {
82 int pos = inC.position();
83 int limit = inC.limit();
84 inC.limit(inC.capacity());
86 int read = s.read(inC);
88 throw new EOFException();
90 inC.limit(inC.position());
95 private SSLEngineResult wrap(SSLEngine se, SocketChannel s)
96 throws SSLException, IOException {
98 SSLEngineResult result = se.wrap(out, outC);