]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/TestSSL.java
83bc7f69b51214e5ab63675a3668c8288ac2a68e
[gigi.git] / tests / org / cacert / gigi / TestSSL.java
1 package org.cacert.gigi;
2
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;
9
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;
15
16 import org.cacert.gigi.testUtils.InitTruststore;
17 import org.cacert.gigi.testUtils.ManagedTest;
18 import org.junit.Test;
19
20 public class TestSSL extends ManagedTest {
21
22     private ByteBuffer in;
23
24     private ByteBuffer inC;
25
26     private ByteBuffer outC;
27
28     private ByteBuffer out;
29     static {
30         InitTruststore.run();
31     }
32
33     @Test
34     public void testClientIntitiatedRenegotiation() throws NoSuchAlgorithmException, IOException {
35         SSLContext sc = SSLContext.getDefault();
36         SSLEngine se = sc.createSSLEngine();
37         String[] serverParts = getServerName().split(":", 2);
38         SocketChannel s = SocketChannel.open(new InetSocketAddress(serverParts[0], Integer.parseInt(serverParts[1])));
39
40         in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
41         inC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
42         inC.limit(0);
43         out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
44         outC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
45         outC.limit(0);
46         se.setUseClientMode(true);
47         se.beginHandshake();
48
49         work(se, s);
50         se.beginHandshake();
51         try {
52             work(se, s);
53             throw new Error("Client re-negotiation succeded (possible DoS vulnerability");
54         } catch (EOFException e) {
55             // Cool, server closed connection
56         }
57
58     }
59
60     private void work(SSLEngine se, SocketChannel s) throws SSLException, IOException {
61         while (se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING && se.getHandshakeStatus() != HandshakeStatus.FINISHED) {
62             switch (se.getHandshakeStatus()) {
63             case NEED_WRAP:
64                 wrap(se, s);
65                 break;
66             case NEED_UNWRAP:
67                 unwrap(se, s);
68                 break;
69             case NEED_TASK:
70                 se.getDelegatedTask().run();
71                 break;
72             default:
73                 System.out.println(se.getHandshakeStatus());
74             }
75         }
76     }
77
78     private SSLEngineResult unwrap(SSLEngine se, SocketChannel s) throws IOException, SSLException {
79         if (inC.remaining() == 0) {
80             inC.clear();
81             s.read(inC);
82             inC.flip();
83         }
84         SSLEngineResult result = se.unwrap(inC, in);
85         if (result.getStatus() == javax.net.ssl.SSLEngineResult.Status.BUFFER_UNDERFLOW) {
86             int pos = inC.position();
87             int limit = inC.limit();
88             inC.limit(inC.capacity());
89             inC.position(limit);
90             int read = s.read(inC);
91             if (read <= 0) {
92                 throw new EOFException();
93             }
94             inC.limit(inC.position());
95             inC.position(pos);
96         }
97         return result;
98     }
99
100     private SSLEngineResult wrap(SSLEngine se, SocketChannel s) throws SSLException, IOException {
101         outC.clear();
102         SSLEngineResult result = se.wrap(out, outC);
103         outC.flip();
104         s.write(outC);
105
106         return result;
107     }
108 }