jdk/test/javax/net/ssl/SSLEngine/SSLEngineService.java
author stefank
Fri, 17 Apr 2015 17:10:38 +0000
changeset 30266 ef82cd1f2db3
parent 23052 241885315119
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 6388456
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Need adjustable TLS max record size for interoperability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      with non-compliant stacks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Helper class of SSL/TLS client/server communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Xuelei Fan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class SSLEngineService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    47
    private String pathToStores;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    48
    private String keyFilename;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    49
    private String trustFilename;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    50
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    51
    protected SSLEngineService() {
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14194
diff changeset
    52
        init("../etc");
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    53
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    54
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    55
    protected SSLEngineService(String pathToStores) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    56
        init(pathToStores);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    57
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    58
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    59
    private void init(String pathToStores) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    60
        this.pathToStores = pathToStores;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    61
        this.keyFilename =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                "/" + keyStoreFile;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    64
        this.trustFilename =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                "/" + trustStoreFile;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
    67
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // deliver local application data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected static void deliver(SSLEngine ssle, SocketChannel sc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // create buufer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int appBufferMax = ssle.getSession().getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        int netBufferMax = ssle.getSession().getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int length = appBufferMax * (Integer.SIZE / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // allocate more in order to check large packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ByteBuffer localAppData = ByteBuffer.allocate(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // allocate less in order to check BUFFER_OVERFLOW/BUFFER_UNDERFLOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ByteBuffer localNetData = ByteBuffer.allocate(netBufferMax/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // prepare local application data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        localAppData.putInt(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        for (int i = 1; i < appBufferMax; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            localAppData.putInt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        localAppData.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        while (localAppData.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // empty the local network packet buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            localNetData.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // generated local network packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            SSLEngineResult res = ssle.wrap(localAppData, localNetData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // checking status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            switch (res.getStatus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            case OK :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                localNetData.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                // send the network packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                while (localNetData.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    if (sc.write(localNetData) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        throw new IOException("Unable write to socket channel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (res.getHandshakeStatus() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        SSLEngineResult.HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    while ((runnable = ssle.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                // detect large buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if (res.bytesProduced() >= Short.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    System.out.println("Generate a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        res.bytesProduced() + " bytes large packet ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            case BUFFER_OVERFLOW :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                // maybe need to enlarge the local network packet buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                int size = ssle.getSession().getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if (size > localNetData.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    System.out.println("resize destination buffer upto " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                size + " bytes for BUFFER_OVERFLOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    localNetData = enlargeBuffer(localNetData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            default : // BUFFER_UNDERFLOW or CLOSED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                throw new IOException("Received invalid" + res.getStatus() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        "during transfer application data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // receive peer application data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    protected static void receive(SSLEngine ssle, SocketChannel sc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // create buufers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int appBufferMax = ssle.getSession().getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int netBufferMax = ssle.getSession().getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // allocate less in order to check BUFFER_OVERFLOW/BUFFER_UNDERFLOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        ByteBuffer peerAppData = ByteBuffer.allocate(appBufferMax/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        ByteBuffer peerNetData = ByteBuffer.allocate(netBufferMax/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int received = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   158
        boolean needToReadMore = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        while (received != 0) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   160
            if (needToReadMore) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   161
                if (ssle.isInboundDone() || sc.read(peerNetData) < 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   162
                    break;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   163
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            peerNetData.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            SSLEngineResult res = ssle.unwrap(peerNetData, peerAppData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            peerNetData.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // checking status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            switch (res.getStatus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            case OK :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (res.getHandshakeStatus() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        SSLEngineResult.HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    while ((runnable = ssle.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (received < 0 && res.bytesProduced() < 4 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if (received < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    received = peerAppData.getInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                System.out.println("received " + peerAppData.position() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        " bytes client application data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                System.out.println("\tcomsumed " + res.bytesConsumed() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        " byes network data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                peerAppData.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                received -= res.bytesProduced();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // detect large buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (res.bytesConsumed() >= Short.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    System.out.println("Consumes a " + res.bytesConsumed() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        " bytes large packet ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   204
                needToReadMore = (peerNetData.position() > 0) ? false : true;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   205
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            case BUFFER_OVERFLOW :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                // maybe need to enlarge the peer application data buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                int size = ssle.getSession().getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (size > peerAppData.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    System.out.println("resize destination buffer upto " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        size + " bytes for BUFFER_OVERFLOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    peerAppData = enlargeBuffer(peerAppData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            case BUFFER_UNDERFLOW :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                // maybe need to enlarge the peer network packet data buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                size = ssle.getSession().getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (size > peerNetData.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    System.out.println("resize source buffer upto " + size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        " bytes for BUFFER_UNDERFLOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    peerNetData = enlargeBuffer(peerNetData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   226
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   227
                needToReadMore = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            default : // CLOSED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                throw new IOException("Received invalid" + res.getStatus() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        "during transfer application data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   237
    protected static void handshaking(SSLEngine ssle, SocketChannel sc,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   238
            ByteBuffer additional) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int appBufferMax = ssle.getSession().getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int netBufferMax = ssle.getSession().getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // allocate less in order to check BUFFER_OVERFLOW/BUFFER_UNDERFLOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        ByteBuffer localAppData = ByteBuffer.allocate(appBufferMax/10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        ByteBuffer peerAppData = ByteBuffer.allocate(appBufferMax/10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        ByteBuffer localNetData = ByteBuffer.allocate(netBufferMax/10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        ByteBuffer peerNetData = ByteBuffer.allocate(netBufferMax/10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // begin handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        ssle.beginHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        SSLEngineResult.HandshakeStatus hs = ssle.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // start handshaking from unwrap
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   254
        byte[] buffer = new byte[0xFF];
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   255
        boolean underflow = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            switch (hs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            case NEED_UNWRAP :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (peerNetData.position() == 0) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   261
                    if (additional != null && additional.hasRemaining()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   262
                        do {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   263
                            int len = Math.min(buffer.length,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   264
                                                peerNetData.remaining());
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   265
                            len = Math.min(len, additional.remaining());
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   266
                            if (len != 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   267
                                additional.get(buffer, 0, len);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   268
                                peerNetData.put(buffer, 0, len);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   269
                            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   270
                        } while (peerNetData.remaining() > 0 &&
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   271
                                    additional.hasRemaining());
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   272
                    } else {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   273
                        if (sc.read(peerNetData) < 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   274
                            ssle.closeInbound();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   275
                            return;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   276
                        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   277
                    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   278
                }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   279
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   280
                if (underflow) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    if (sc.read(peerNetData) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        ssle.closeInbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    }
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   285
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   286
                    underflow = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                peerNetData.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                SSLEngineResult res = ssle.unwrap(peerNetData, peerAppData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                peerNetData.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                hs = res.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                switch (res.getStatus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                case OK :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                case BUFFER_UNDERFLOW :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    // maybe need to enlarge the peer network packet buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    int size = ssle.getSession().getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    if (size > peerNetData.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        System.out.println("resize source buffer upto " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                size + " bytes for BUFFER_UNDERFLOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        peerNetData = enlargeBuffer(peerNetData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    }
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   305
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   306
                    underflow = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                case BUFFER_OVERFLOW :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    // maybe need to enlarge the peer application data buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    size = ssle.getSession().getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    if (size > peerAppData.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        System.out.println("resize destination buffer upto " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                size + " bytes for BUFFER_OVERFLOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        peerAppData = enlargeBuffer(peerAppData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                default : //CLOSED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    throw new IOException("Received invalid" + res.getStatus() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        "during initial handshaking");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            case NEED_WRAP :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // empty the local network packet buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                localNetData.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                // generated local network packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                res = ssle.wrap(localAppData, localNetData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                hs = res.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // checking status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                switch (res.getStatus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                case OK :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    localNetData.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    // send the network packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    while (localNetData.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        if (sc.write(localNetData) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                "Unable write to socket channel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                case BUFFER_OVERFLOW :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    // maybe need to enlarge the local network packet buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    int size = ssle.getSession().getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    if (size > localNetData.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        System.out.println("resize destination buffer upto " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                size + " bytes for BUFFER_OVERFLOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        localNetData = enlargeBuffer(localNetData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                default : // BUFFER_UNDERFLOW or CLOSED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    throw new IOException("Received invalid" + res.getStatus() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        "during initial handshaking");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            case NEED_TASK :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                while ((runnable = ssle.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                hs = ssle.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            default : // FINISHED or NOT_HANDSHAKING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } while (hs != SSLEngineResult.HandshakeStatus.FINISHED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                hs != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private static ByteBuffer enlargeBuffer(ByteBuffer buffer, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        ByteBuffer bb = ByteBuffer.allocate(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        buffer.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        bb.put(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Create an initialized SSLContext to use for this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   387
    protected SSLEngine createSSLEngine(boolean mode) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        SSLEngine ssle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        KeyStore ts = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        ks.load(new FileInputStream(keyFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        ts.load(new FileInputStream(trustFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        tmf.init(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        SSLContext sslCtx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        ssle = sslCtx.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        ssle.setUseClientMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return ssle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
}