summaryrefslogtreecommitdiff
path: root/jessie-tests/testServerHello.java
blob: fb6f0cbe1394b0ae78d13f3f9d78d081c603ab9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

import gnu.javax.net.ssl.provider.CipherSuite;
import gnu.javax.net.ssl.provider.CompressionMethod;
import gnu.javax.net.ssl.provider.Extension;
import gnu.javax.net.ssl.provider.ExtensionList;
import gnu.javax.net.ssl.provider.Handshake;
import gnu.javax.net.ssl.provider.ProtocolVersion;
import gnu.javax.net.ssl.provider.Random;
import gnu.javax.net.ssl.provider.ServerHello;

import java.nio.ByteBuffer;
import java.util.Arrays;

class testServerHello
{
  public static void main (String[] argv)
  {
    try
      {
        check ();
      }
    catch (Exception x)
      {
        System.out.println ("FAIL: caught exception " + x);
        x.printStackTrace ();
      }
  }

  static void check () throws Exception
  {
    final int alloc_len = 4096;
    ByteBuffer buffer = ByteBuffer.allocate (alloc_len);
    Handshake handshake = new Handshake (buffer);

    handshake.setType (Handshake.Type.SERVER_HELLO);
    handshake.setLength (alloc_len - 4);

    ServerHello hello = (ServerHello) handshake.body ();

    hello.setVersion (ProtocolVersion.TLS_1);
    Random random = hello.random ();
    random.setGmtUnixTime (123456);
    byte[] nonce = new byte[28];
    for (int i = 0; i < nonce.length; i++)
      nonce[i] = (byte) i;
    random.setRandomBytes (nonce);
    byte[] sessionId = new byte[32];
    for (int i = 0; i < sessionId.length; i++)
      sessionId[i] = (byte) i;
    hello.setSessionId (sessionId);
    hello.setCipherSuite (CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA);
    hello.setCompressionMethod (CompressionMethod.ZLIB);
    hello.setExtensionsLength (12);
    ExtensionList exts = hello.extensions();
    // Max fragment length of 2^9-1
    exts.set (0, Extension.Type.MAX_FRAGMENT_LENGTH, 1); // 2 + 2 + 1
    exts.get (0).setValue (new byte[] { 1 });
    // Zero-length server name.
    exts.set (1, Extension.Type.SERVER_NAME, 3); // 2 + 2 + 3
    exts.get(1).setValue(new byte[3]);

    handshake.setLength (hello.length ());
    System.err.println (handshake);

    handshake = new Handshake (buffer);
    hello = (ServerHello) handshake.body ();
    if (Arrays.equals (sessionId, hello.sessionId ()))
      System.out.println ("PASS: sessionId");
    else
      System.out.println ("FAIL: sessionId");

    if (hello.cipherSuite () == CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA)
      System.out.println ("PASS: cipherSuite");
    else
      System.out.println ("FAIL: cipherSuite");

    if (hello.compressionMethod () == CompressionMethod.ZLIB)
      System.out.println ("PASS: compressionMethod");
    else
      System.out.println ("FAIL: compressionMethod");
    
    exts = hello.extensions();
    Extension e = exts.get(0);
    if (e.type() == Extension.Type.MAX_FRAGMENT_LENGTH)
      System.out.println ("PASS: extensions().get(0).type");
    else
      System.out.println ("FAIL: extensions().get(0).type");
    if (Arrays.equals(e.valueBytes(), new byte[] { 1 }))
      System.out.println ("PASS: extensions().get(0).value");
    else
      System.out.println ("FAIL: extensions().get(0).value");

    e = exts.get(1);
    if (e.type() == Extension.Type.SERVER_NAME)
      System.out.println ("PASS: extensions().get(1).type");
    else
      System.out.println ("FAIL: extensions().get(1).type");
    if (Arrays.equals(e.valueBytes(), new byte[3]))
      System.out.println ("PASS: extensions().get(1).value");
    else
      System.out.println ("FAIL: extensions().get(1).value");
 
    // Part 2: with no extensions.
    buffer = ByteBuffer.allocate (74);
    handshake = new Handshake (buffer);

    handshake.setType (Handshake.Type.SERVER_HELLO);
    handshake.setLength (70);

    hello = (ServerHello) handshake.body ();

    hello.setVersion (ProtocolVersion.TLS_1); // 2
    random = hello.random ();
    random.setGmtUnixTime (123456);
    nonce = new byte[28];
    for (int i = 0; i < nonce.length; i++)
      nonce[i] = (byte) i;
    random.setRandomBytes (nonce);            // + 32
    sessionId = new byte[32];
    for (int i = 0; i < sessionId.length; i++)
      sessionId[i] = (byte) i;
    hello.setSessionId (sessionId);           // + 33
    hello.setCipherSuite (CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA); // + 2
    hello.setCompressionMethod (CompressionMethod.ZLIB); // + 1
    
    handshake = new Handshake (buffer);
    hello = (ServerHello) handshake.body();
    if (hello.extensions() == null)
      System.out.println ("PASS: hello.extensions() == null");
    else
      System.out.println ("FAIL: hello.extensions() != null");
    
    System.err.println (handshake);
  }
}