jdk/src/share/classes/sun/net/httpserver/SSLStreams.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7537 b7d2d2428f31
child 10596 39b3a979e600
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7537
diff changeset
     2
 * Copyright (c) 2005, 2010, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * given a non-blocking SocketChannel, it produces
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (blocking) streams which encrypt/decrypt the SSL content
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and handle the SSL handshaking automatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class SSLStreams {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    SSLContext sslctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    TimeSource time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    SSLEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    EngineWrapper wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* held by thread doing the hand-shake on this connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    Lock handshaking = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    SSLStreams (ServerImpl server, SSLContext sslctx, SocketChannel chan) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.time= (TimeSource)server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.sslctx= sslctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.chan= chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        InetSocketAddress addr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                (InetSocketAddress)chan.socket().getRemoteSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        engine = sslctx.createSSLEngine (addr.getHostName(), addr.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        engine.setUseClientMode (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        HttpsConfigurator cfg = server.getHttpsConfigurator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        configureEngine (cfg, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        wrapper = new EngineWrapper (chan, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private void configureEngine(HttpsConfigurator cfg, InetSocketAddress addr){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (cfg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Parameters params = new Parameters (cfg, addr);
7537
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
    77
//BEGIN_TIGER_EXCLUDE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            cfg.configure (params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            SSLParameters sslParams = params.getSSLParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (sslParams != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                engine.setSSLParameters (sslParams);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    82
            } else
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    83
//END_TIGER_EXCLUDE
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    84
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                /* tiger compatibility */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (params.getCipherSuites() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        engine.setEnabledCipherSuites (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                            params.getCipherSuites()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    } catch (IllegalArgumentException e) { /* LOG */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                engine.setNeedClientAuth (params.getNeedClientAuth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                engine.setWantClientAuth (params.getWantClientAuth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (params.getProtocols() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        engine.setEnabledProtocols (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                            params.getProtocols()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    } catch (IllegalArgumentException e) { /* LOG */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    class Parameters extends HttpsParameters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        InetSocketAddress addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        HttpsConfigurator cfg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Parameters (HttpsConfigurator cfg, InetSocketAddress addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            this.addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            this.cfg = cfg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        public InetSocketAddress getClientAddress () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public HttpsConfigurator getHttpsConfigurator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return cfg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   120
//BEGIN_TIGER_EXCLUDE
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   121
        SSLParameters params;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public void setSSLParameters (SSLParameters p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            params = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        SSLParameters getSSLParameters () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   128
//END_TIGER_EXCLUDE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * cleanup resources allocated inside this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        wrapper.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * return the SSL InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    InputStream getInputStream () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            is = new InputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * return the SSL OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    OutputStream getOutputStream () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (os == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            os = new OutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    SSLEngine getSSLEngine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * request the engine to repeat the handshake on this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * the handshake must be driven by reads/writes on the streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Normally, not necessary to call this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    void beginHandshake() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        engine.beginHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    class WrapperResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        SSLEngineResult result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        /* if passed in buffer was not big enough then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * a reallocated buffer is returned here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    int app_buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    int packet_buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    enum BufType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        PACKET, APPLICATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private ByteBuffer allocate (BufType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return allocate (type, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private ByteBuffer allocate (BufType type, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        assert engine != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (type == BufType.PACKET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (packet_buf_size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    SSLSession sess = engine.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    packet_buf_size = sess.getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (len > packet_buf_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    packet_buf_size = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                size = packet_buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                if (app_buf_size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    SSLSession sess = engine.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    app_buf_size = sess.getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (len > app_buf_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    app_buf_size = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                size = app_buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return ByteBuffer.allocate (size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /* reallocates the buffer by :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * 1. creating a new buffer double the size of the old one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * 2. putting the contents of the old buffer into the new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * 3. set xx_buf_size to the new size if it was smaller than new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * flip is set to true if the old buffer needs to be flipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * before it is copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private ByteBuffer realloc (ByteBuffer b, boolean flip, BufType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            int nsize = 2 * b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            ByteBuffer n = allocate (type, nsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (flip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            n.put(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            b = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * This is a thin wrapper over SSLEngine and the SocketChannel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * which guarantees the ordering of wraps/unwraps with respect to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * channel read/writes. It handles the UNDER/OVERFLOW status codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * It does not handle the handshaking status codes, or the CLOSED status code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * though once the engine is closed, any attempt to read/write to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * will get an exception.  The overall result is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * It functions synchronously/blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    class EngineWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        SSLEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Object wrapLock, unwrapLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        ByteBuffer unwrap_src, wrap_dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int u_remaining; // the number of bytes left in unwrap_src after an unwrap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        EngineWrapper (SocketChannel chan, SSLEngine engine) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            this.chan = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            wrapLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            unwrapLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            unwrap_src = allocate(BufType.PACKET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            wrap_dst = allocate(BufType.PACKET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        /* try to wrap and send the data in src. Handles OVERFLOW.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * Might block if there is an outbound blockage or if another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * thread is calling wrap(). Also, might not send any data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * if an unwrap is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        WrapperResult wrapAndSend(ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return wrapAndSendX(src, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        WrapperResult wrapAndSendX(ByteBuffer src, boolean ignoreClose) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (closed && !ignoreClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw new IOException ("Engine is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            Status status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            WrapperResult r = new WrapperResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            synchronized (wrapLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                wrap_dst.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    r.result = engine.wrap (src, wrap_dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    status = r.result.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    if (status == Status.BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        wrap_dst = realloc (wrap_dst, true, BufType.PACKET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                } while (status == Status.BUFFER_OVERFLOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if (status == Status.CLOSED && !ignoreClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                if (r.result.bytesProduced() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    wrap_dst.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    int l = wrap_dst.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    assert l == r.result.bytesProduced();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    while (l>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        l -= chan.write (wrap_dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /* block until a complete message is available and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * in dst, together with the Result. dst may have been re-allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * so caller should check the returned value in Result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * If handshaking is in progress then, possibly no data is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        WrapperResult recvAndUnwrap(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            Status status = Status.OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            WrapperResult r = new WrapperResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            r.buf = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                throw new IOException ("Engine is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            boolean needData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (u_remaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                unwrap_src.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                unwrap_src.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                needData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                unwrap_src.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                needData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            synchronized (unwrapLock) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   330
                int x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    if (needData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        x = chan.read (unwrap_src);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   335
                        } while (x == 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        if (x == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            throw new IOException ("connection closed for reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        unwrap_src.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    r.result = engine.unwrap (unwrap_src, r.buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    status = r.result.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    if (status == Status.BUFFER_UNDERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        if (unwrap_src.limit() == unwrap_src.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                            /* buffer not big enough */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                            unwrap_src = realloc (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                unwrap_src, false, BufType.PACKET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            /* Buffer not full, just need to read more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                             * data off the channel. Reset pointers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                             * for reading off SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            unwrap_src.position (unwrap_src.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            unwrap_src.limit (unwrap_src.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        needData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    } else if (status == Status.BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        r.buf = realloc (r.buf, true, BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        needData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    } else if (status == Status.CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        r.buf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                } while (status != Status.OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            u_remaining = unwrap_src.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * send the data in the given ByteBuffer. If a handshake is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * then this is handled within this method. When this call returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * all of the given user data has been sent and any handshake has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * completed. Caller should check if engine has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public WrapperResult sendData (ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        WrapperResult r=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        while (src.remaining() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            r = wrapper.wrapAndSend(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            Status status = r.result.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (status == Status.CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                doClosure ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            HandshakeStatus hs_status = r.result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (hs_status != HandshakeStatus.FINISHED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                hs_status != HandshakeStatus.NOT_HANDSHAKING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                doHandshake(hs_status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * read data thru the engine into the given ByteBuffer. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * given buffer was not large enough, a new one is allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * and returned. This call handles handshaking automatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Caller should check if engine has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public WrapperResult recvData (ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        /* we wait until some user data arrives */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        WrapperResult r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        assert dst.position() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        while (dst.position() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            r = wrapper.recvAndUnwrap (dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            dst = (r.buf != dst) ? r.buf: dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            Status status = r.result.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (status == Status.CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                doClosure ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            HandshakeStatus hs_status = r.result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (hs_status != HandshakeStatus.FINISHED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                hs_status != HandshakeStatus.NOT_HANDSHAKING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                doHandshake (hs_status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        dst.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /* we've received a close notify. Need to call wrap to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    void doClosure () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            handshaking.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            ByteBuffer tmp = allocate(BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            WrapperResult r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                tmp.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                tmp.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                r = wrapper.wrapAndSendX (tmp, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } while (r.result.getStatus() != Status.CLOSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            handshaking.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /* do the (complete) handshake after acquiring the handshake lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * If two threads call this at the same time, then we depend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * on the wrapper methods being idempotent. eg. if wrapAndSend()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * is called with no data to send then there must be no problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    void doHandshake (HandshakeStatus hs_status) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            handshaking.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            ByteBuffer tmp = allocate(BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            while (hs_status != HandshakeStatus.FINISHED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                   hs_status != HandshakeStatus.NOT_HANDSHAKING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                WrapperResult r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                switch (hs_status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    case NEED_TASK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        Runnable task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        while ((task = engine.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            /* run in current thread, because we are already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                             * running an external Executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            task.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        /* fall thru - call wrap again */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    case NEED_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        tmp.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        tmp.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        r = wrapper.wrapAndSend(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    case NEED_UNWRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        tmp.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        r = wrapper.recvAndUnwrap (tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        if (r.buf != tmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                            tmp = r.buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        assert tmp.position() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                hs_status = r.result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            handshaking.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * represents an SSL input stream. Multiple https requests can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * be sent over one stream. closing this stream causes an SSL close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    class InputStream extends java.io.InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        ByteBuffer bbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        /* this stream eof */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        boolean eof = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        boolean needData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        InputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            bbuf = allocate (BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        public int read (byte[] buf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                throw new IOException ("SSL stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            int available=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            if (!needData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                available = bbuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                needData = (available==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (needData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                bbuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                WrapperResult r = recvData (bbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                bbuf = r.buf== bbuf? bbuf: r.buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if ((available=bbuf.remaining()) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    needData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            /* copy as much as possible from buf into users buf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (len > available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                len = available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            bbuf.get (buf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        public int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return bbuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        public boolean markSupported () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            return false; /* not possible with SSLEngine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        public void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            throw new IOException ("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        public long skip (long s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            int n = (int)s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                throw new IOException ("SSL stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            int ret = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            while (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                if (bbuf.remaining() >= n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    bbuf.position (bbuf.position()+n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    n -= bbuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    bbuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    WrapperResult r = recvData (bbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    bbuf = r.buf==bbuf? bbuf: r.buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return ret; /* not reached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * close the SSL connection. All data must have been consumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         * before this is called. Otherwise an exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * [Note. May need to revisit this. not quite the normal close() symantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            engine.closeInbound ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        public int read (byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return read (buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        byte single[] = new byte [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        public int read () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            int n = read (single, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                return single[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * represents an SSL output stream. plain text data written to this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * is encrypted by the stream. Multiple HTTPS responses can be sent on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * one stream. closing this stream initiates an SSL closure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    class OutputStream extends java.io.OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        byte single[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        OutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            buf = allocate(BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            single[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            write (single, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                throw new IOException ("output stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                int l = len > buf.capacity() ? buf.capacity() : len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                buf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                buf.put (b, off, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                len -= l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                off += l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                buf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                WrapperResult r = sendData (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                if (r.result.getStatus() == Status.CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        throw new IOException ("output stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            /* no-op */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            WrapperResult r=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            engine.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            HandshakeStatus stat = HandshakeStatus.NEED_WRAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            buf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            while (stat == HandshakeStatus.NEED_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                r = wrapper.wrapAndSend (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                stat = r.result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            assert r.result.getStatus() == Status.CLOSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
}