jdk/src/share/classes/sun/security/ssl/HandshakeOutStream.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4236 02f52c723b79
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
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.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Output stream for handshake data.  This is used only internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * to the SSL classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * MT note:  one thread at a time is presumed be writing handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * messages, but (after initial connection setup) it's possible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * have other threads reading/writing application data.  It's the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * SSLSocketImpl class that synchronizes record writes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class HandshakeOutStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private SSLSocketImpl socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private SSLEngineImpl engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    OutputRecord r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    HandshakeOutStream(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            ProtocolVersion helloVersion, HandshakeHash handshakeHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            SSLSocketImpl socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.socket = socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        r = new OutputRecord(Record.ct_handshake);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        init(protocolVersion, helloVersion, handshakeHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    HandshakeOutStream(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            ProtocolVersion helloVersion, HandshakeHash handshakeHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            SSLEngineImpl engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        r = new EngineOutputRecord(Record.ct_handshake, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        init(protocolVersion, helloVersion, handshakeHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private void init(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            ProtocolVersion helloVersion, HandshakeHash handshakeHash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        r.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        r.setHelloVersion(helloVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        r.setHandshakeHash(handshakeHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Update the handshake data hashes ... mostly for use after a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * client cert has been sent, so the cert verify message can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * constructed correctly yet without forcing extra I/O.  In all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * other cases, automatic hash calculation suffices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    void doHashes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        r.doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Write some data out onto the stream ... buffers as much as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Hashes are updated automatically if something gets flushed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * network (e.g. a big cert message etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void write(byte buf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            int howmuch = Math.min(len, r.availableDataBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (howmuch == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                r.write(buf, off, howmuch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                off += howmuch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                len -= howmuch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
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
     * write-a-byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void write(int i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (r.availableDataBytes() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        r.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                socket.writeRecord(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                // Had problems writing; check if there was an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                // alert from peer. If alert received, waitForClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // will throw an exception for the alert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                socket.waitForClose(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // No alert was received, just rethrow exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } else {  // engine != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             * Even if record might be empty, flush anyway in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             * there is a finished handshake message that we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             * to queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            engine.writeRecord((EngineOutputRecord)r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Tell the OutputRecord that a finished message was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * contained either in this record or the one immeiately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * preceeding it.  We need to reliably pass back notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * that a finish message occured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void setFinishedMsg() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        assert(socket == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ((EngineOutputRecord)r).setFinishedMsg();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Put integers encoded in standard 8, 16, 24, and 32 bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * big endian formats. Note that OutputStream.write(int) only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * writes the least significant 8 bits and ignores the rest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    void putInt8(int i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        r.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    void putInt16(int i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (r.availableDataBytes() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        r.write(i >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        r.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    void putInt24(int i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (r.availableDataBytes() < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        r.write(i >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        r.write(i >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        r.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    void putInt32(int i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (r.availableDataBytes() < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        r.write(i >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        r.write(i >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        r.write(i >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        r.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Put byte arrays with length encoded as 8, 16, 24 bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * integers in big-endian format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    void putBytes8(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            putInt8(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        putInt8(b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        write(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    void putBytes16(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            putInt16(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        putInt16(b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        write(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    void putBytes24(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            putInt24(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        putInt24(b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        write(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
}