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 {
22 private ByteBuffer in;
24 private ByteBuffer inC;
26 private ByteBuffer outC;
28 private ByteBuffer out;
34 public void testClientIntitiatedRenegotiation() throws NoSuchAlgorithmException, IOException {
35 SSLContext sc = SSLContext.getDefault();
36 SSLEngine se = sc.createSSLEngine();
37 String[] serverParts = getServerName().split(":", 2);
38 try (SocketChannel s = SocketChannel.open(new InetSocketAddress(serverParts[0], Integer.parseInt(serverParts[1])))) {
40 in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
41 inC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
43 out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
44 outC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
46 se.setUseClientMode(true);
53 throw new Error("Client re-negotiation succeded (possible DoS vulnerability");
54 } catch (EOFException e) {
55 // Cool, server closed connection
61 private void work(SSLEngine se, SocketChannel s) throws SSLException, IOException {
62 while (se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING && se.getHandshakeStatus() != HandshakeStatus.FINISHED) {
63 switch (se.getHandshakeStatus()) {
71 se.getDelegatedTask().run();
74 System.out.println(se.getHandshakeStatus());
79 private SSLEngineResult unwrap(SSLEngine se, SocketChannel s) throws IOException, SSLException {
80 if (inC.remaining() == 0) {
85 SSLEngineResult result = se.unwrap(inC, in);
86 if (result.getStatus() == javax.net.ssl.SSLEngineResult.Status.BUFFER_UNDERFLOW) {
87 int pos = inC.position();
88 int limit = inC.limit();
89 inC.limit(inC.capacity());
91 int read = s.read(inC);
93 throw new EOFException();
95 inC.limit(inC.position());
101 private SSLEngineResult wrap(SSLEngine se, SocketChannel s) throws SSLException, IOException {
103 SSLEngineResult result = se.wrap(out, outC);