src/java.base/share/classes/sun/security/ssl/DTLSOutputRecord.java
author jnimeh
Wed, 06 Jun 2018 23:53:47 -0700
branchJDK-8145252-TLS13-branch
changeset 56686 07dc566630ee
parent 56542 56aaa6cb3693
permissions -rw-r--r--
Address TLS 1.3 comments output stream classes, go through JsseJce class to get Mac instance. Summary: TLS 1.3 comments output stream classes, go through JsseJce class to get Mac instance.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
     4
 *
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    10
 *
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    15
 * accompanied this code).
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    16
 *
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    20
 *
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    23
 * questions.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    24
 */
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    25
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    26
package sun.security.ssl;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    27
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    28
import java.io.*;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    29
import java.nio.*;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    30
import java.util.*;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    31
import javax.net.ssl.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    32
import sun.security.ssl.SSLCipher.SSLWriteCipher;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    33
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    34
/**
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    35
 * DTLS {@code OutputRecord} implementation for {@code SSLEngine}.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    36
 */
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    37
final class DTLSOutputRecord extends OutputRecord implements DTLSRecord {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    38
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    39
    private DTLSFragmenter fragmenter = null;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    40
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    41
    int                 writeEpoch;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    42
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    43
    int                 prevWriteEpoch;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    44
    Authenticator       prevWriteAuthenticator;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    45
    SSLWriteCipher      prevWriteCipher;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    46
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    47
    private final LinkedList<RecordMemo> alertMemos = new LinkedList<>();
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    48
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    49
    DTLSOutputRecord(HandshakeHash handshakeHash) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    50
        super(handshakeHash, SSLWriteCipher.nullDTlsWriteCipher());
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    51
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    52
        this.writeEpoch = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    53
        this.prevWriteEpoch = 0;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    54
        this.prevWriteCipher = SSLWriteCipher.nullDTlsWriteCipher();
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    55
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    56
        this.packetSize = DTLSRecord.maxRecordSize;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    57
        this.protocolVersion = ProtocolVersion.NONE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    58
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    59
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    60
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    61
    void initHandshaker() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    62
        // clean up
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    63
        fragmenter = null;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    64
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    65
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    66
    @Override
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    67
    void finishHandshake() {
56686
07dc566630ee Address TLS 1.3 comments output stream classes, go through JsseJce class to get Mac instance.
jnimeh
parents: 56542
diff changeset
    68
        // Nothing to do here currently.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    69
    }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    70
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    71
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    72
    void changeWriteCiphers(SSLWriteCipher writeCipher,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    73
            boolean useChangeCipherSpec) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    74
        if (useChangeCipherSpec) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    75
            encodeChangeCipherSpec();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    76
        }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    77
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    78
        prevWriteCipher.dispose();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    79
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    80
        this.prevWriteCipher = this.writeCipher;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    81
        this.prevWriteEpoch = this.writeEpoch;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    82
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    83
        this.writeCipher = writeCipher;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    84
        this.writeEpoch++;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    85
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    86
        this.isFirstAppOutputRecord = true;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    87
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    88
        // set the epoch number
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    89
        this.writeCipher.authenticator.setEpochNumber(this.writeEpoch);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    90
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    91
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    92
    @Override
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    93
    void encodeAlert(byte level, byte description) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    94
        RecordMemo memo = new RecordMemo();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    95
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    96
        memo.contentType = ContentType.ALERT.id;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    97
        memo.majorVersion = protocolVersion.major;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    98
        memo.minorVersion = protocolVersion.minor;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
    99
        memo.encodeEpoch = writeEpoch;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   100
        memo.encodeCipher = writeCipher;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   101
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   102
        memo.fragment = new byte[2];
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   103
        memo.fragment[0] = level;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   104
        memo.fragment[1] = description;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   105
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   106
        alertMemos.add(memo);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   107
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   108
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   109
    @Override
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   110
    void encodeChangeCipherSpec() throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   111
        if (fragmenter == null) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   112
           fragmenter = new DTLSFragmenter();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   113
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   114
        fragmenter.queueUpChangeCipherSpec();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   115
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   116
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   117
    @Override
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   118
    void encodeHandshake(byte[] source,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   119
            int offset, int length) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   120
        if (firstMessage) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   121
            firstMessage = false;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   122
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   123
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   124
        if (fragmenter == null) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   125
           fragmenter = new DTLSFragmenter();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   126
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   127
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   128
        fragmenter.queueUpHandshake(source, offset, length);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   129
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   130
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   131
    @Override
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   132
    Ciphertext encode(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   133
        ByteBuffer[] srcs, int srcsOffset, int srcsLength,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   134
        ByteBuffer[] dsts, int dstsOffset, int dstsLength) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   135
        return encode(srcs, srcsOffset, srcsLength, dsts[0]);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   136
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   137
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   138
    private Ciphertext encode(ByteBuffer[] sources, int offset, int length,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   139
            ByteBuffer destination) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   140
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   141
        if (writeCipher.authenticator.seqNumOverflow()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   142
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   143
                SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   144
                    "sequence number extremely close to overflow " +
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   145
                    "(2^64-1 packets). Closing connection.");
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   146
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   147
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   148
            throw new SSLHandshakeException("sequence number overflow");
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   149
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   150
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   151
        // Don't process the incoming record until all of the buffered records
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   152
        // get handled.  May need retransmission if no sources specified.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   153
        if (!isEmpty() || sources == null || sources.length == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   154
            Ciphertext ct = acquireCiphertext(destination);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   155
            if (ct != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   156
                return ct;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   157
            }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   158
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   159
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   160
        if (sources == null || sources.length == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   161
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   162
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   163
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   164
        int srcsRemains = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   165
        for (int i = offset; i < offset + length; i++) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   166
            srcsRemains += sources[i].remaining();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   167
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   168
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   169
        if (srcsRemains == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   170
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   171
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   172
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   173
        // not apply to handshake message
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   174
        int fragLen;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   175
        if (packetSize > 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   176
            fragLen = Math.min(maxRecordSize, packetSize);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   177
            fragLen = writeCipher.calculateFragmentSize(
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   178
                    fragLen, headerSize);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   179
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   180
            fragLen = Math.min(fragLen, Record.maxDataSize);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   181
        } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   182
            fragLen = Record.maxDataSize;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   183
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   184
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   185
        if (fragmentSize > 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   186
            fragLen = Math.min(fragLen, fragmentSize);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   187
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   188
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   189
        int dstPos = destination.position();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   190
        int dstLim = destination.limit();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   191
        int dstContent = dstPos + headerSize +
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   192
                                writeCipher.getExplicitNonceSize();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   193
        destination.position(dstContent);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   194
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   195
        int remains = Math.min(fragLen, destination.remaining());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   196
        fragLen = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   197
        int srcsLen = offset + length;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   198
        for (int i = offset; (i < srcsLen) && (remains > 0); i++) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   199
            int amount = Math.min(sources[i].remaining(), remains);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   200
            int srcLimit = sources[i].limit();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   201
            sources[i].limit(sources[i].position() + amount);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   202
            destination.put(sources[i]);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   203
            sources[i].limit(srcLimit);         // restore the limit
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   204
            remains -= amount;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   205
            fragLen += amount;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   206
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   207
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   208
        destination.limit(destination.position());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   209
        destination.position(dstContent);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   210
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   211
        if (SSLLogger.isOn && SSLLogger.isOn("record")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   212
            SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   213
                    "WRITE: " + protocolVersion + " " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   214
                    ContentType.APPLICATION_DATA.name +
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   215
                    ", length = " + destination.remaining());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   216
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   217
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   218
        // Encrypt the fragment and wrap up a record.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   219
        long recordSN = encrypt(writeCipher,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   220
                ContentType.APPLICATION_DATA.id, destination,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   221
                dstPos, dstLim, headerSize,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   222
                protocolVersion);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   223
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   224
        if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   225
            ByteBuffer temporary = destination.duplicate();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   226
            temporary.limit(temporary.position());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   227
            temporary.position(dstPos);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   228
            SSLLogger.fine("Raw write", temporary);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   229
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   230
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   231
        // remain the limit unchanged
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   232
        destination.limit(dstLim);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   233
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   234
        return new Ciphertext(ContentType.APPLICATION_DATA.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   235
                SSLHandshake.NOT_APPLICABLE.id, recordSN);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   236
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   237
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   238
    private Ciphertext acquireCiphertext(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   239
            ByteBuffer destination) throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   240
        if (alertMemos != null && !alertMemos.isEmpty()) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   241
            RecordMemo memo = alertMemos.pop();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   242
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   243
            int dstPos = destination.position();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   244
            int dstLim = destination.limit();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   245
            int dstContent = dstPos + headerSize +
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   246
                                writeCipher.getExplicitNonceSize();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   247
            destination.position(dstContent);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   248
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   249
            destination.put(memo.fragment);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   250
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   251
            destination.limit(destination.position());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   252
            destination.position(dstContent);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   253
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   254
            if (SSLLogger.isOn && SSLLogger.isOn("record")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   255
                SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   256
                        "WRITE: " + protocolVersion + " " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   257
                        ContentType.ALERT.name +
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   258
                        ", length = " + destination.remaining());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   259
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   260
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   261
            // Encrypt the fragment and wrap up a record.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   262
            long recordSN = encrypt(memo.encodeCipher,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   263
                    ContentType.ALERT.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   264
                    destination, dstPos, dstLim, headerSize,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   265
                    ProtocolVersion.valueOf(memo.majorVersion,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   266
                            memo.minorVersion));
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   267
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   268
            if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   269
                ByteBuffer temporary = destination.duplicate();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   270
                temporary.limit(temporary.position());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   271
                temporary.position(dstPos);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   272
                SSLLogger.fine("Raw write", temporary);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   273
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   274
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   275
            // remain the limit unchanged
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   276
            destination.limit(dstLim);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   277
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   278
            return new Ciphertext(ContentType.ALERT.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   279
                    SSLHandshake.NOT_APPLICABLE.id, recordSN);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   280
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   281
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   282
        if (fragmenter != null) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   283
            return fragmenter.acquireCiphertext(destination);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   284
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   285
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   286
        return null;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   287
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   288
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   289
    @Override
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   290
    boolean isEmpty() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   291
        return ((fragmenter == null) || fragmenter.isEmpty()) &&
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   292
               ((alertMemos == null) || alertMemos.isEmpty());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   293
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   294
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   295
    @Override
41820
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   296
    void launchRetransmission() {
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   297
        // Note: Please don't retransmit if there are handshake messages
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   298
        // or alerts waiting in the queue.
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   299
        if (((alertMemos == null) || alertMemos.isEmpty()) &&
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   300
                (fragmenter != null) && fragmenter.isRetransmittable()) {
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   301
            fragmenter.setRetransmission();
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   302
        }
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   303
    }
3d8c88d00c9f 8167680: DTLS implementation bugs
xuelei
parents: 34687
diff changeset
   304
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   305
    // buffered record fragment
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   306
    private static class RecordMemo {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   307
        byte            contentType;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   308
        byte            majorVersion;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   309
        byte            minorVersion;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   310
        int             encodeEpoch;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   311
        SSLWriteCipher  encodeCipher;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   312
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   313
        byte[]          fragment;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   314
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   315
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   316
    private static class HandshakeMemo extends RecordMemo {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   317
        byte            handshakeType;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   318
        int             messageSequence;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   319
        int             acquireOffset;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   320
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   321
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   322
    private final class DTLSFragmenter {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   323
        private final LinkedList<RecordMemo> handshakeMemos =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   324
                new LinkedList<>();
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   325
        private int acquireIndex = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   326
        private int messageSequence = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   327
        private boolean flightIsReady = false;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   328
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   329
        // Per section 4.1.1, RFC 6347:
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   330
        //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   331
        // If repeated retransmissions do not result in a response, and the
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   332
        // PMTU is unknown, subsequent retransmissions SHOULD back off to a
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   333
        // smaller record size, fragmenting the handshake message as
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   334
        // appropriate.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   335
        //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   336
        // In this implementation, two times of retransmits would be attempted
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   337
        // before backing off.  The back off is supported only if the packet
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   338
        // size is bigger than 256 bytes.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   339
        private int retransmits = 2;            // attemps of retransmits
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   340
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   341
        void queueUpChangeCipherSpec() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   342
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   343
            // Cleanup if a new flight starts.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   344
            if (flightIsReady) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   345
                handshakeMemos.clear();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   346
                acquireIndex = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   347
                flightIsReady = false;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   348
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   349
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   350
            RecordMemo memo = new RecordMemo();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   351
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   352
            memo.contentType = ContentType.CHANGE_CIPHER_SPEC.id;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   353
            memo.majorVersion = protocolVersion.major;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   354
            memo.minorVersion = protocolVersion.minor;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   355
            memo.encodeEpoch = writeEpoch;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   356
            memo.encodeCipher = writeCipher;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   357
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   358
            memo.fragment = new byte[1];
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   359
            memo.fragment[0] = 1;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   360
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   361
            handshakeMemos.add(memo);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   362
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   363
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   364
        void queueUpHandshake(byte[] buf,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   365
                int offset, int length) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   366
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   367
            // Cleanup if a new flight starts.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   368
            if (flightIsReady) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   369
                handshakeMemos.clear();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   370
                acquireIndex = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   371
                flightIsReady = false;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   372
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   373
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   374
            HandshakeMemo memo = new HandshakeMemo();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   375
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   376
            memo.contentType = ContentType.HANDSHAKE.id;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   377
            memo.majorVersion = protocolVersion.major;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   378
            memo.minorVersion = protocolVersion.minor;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   379
            memo.encodeEpoch = writeEpoch;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   380
            memo.encodeCipher = writeCipher;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   381
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   382
            memo.handshakeType = buf[offset];
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   383
            memo.messageSequence = messageSequence++;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   384
            memo.acquireOffset = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   385
            memo.fragment = new byte[length - 4];       // 4: header size
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   386
                                                        //    1: HandshakeType
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   387
                                                        //    3: message length
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   388
            System.arraycopy(buf, offset + 4, memo.fragment, 0, length - 4);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   389
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   390
            handshakeHashing(memo, memo.fragment);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   391
            handshakeMemos.add(memo);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   392
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   393
            if ((memo.handshakeType == SSLHandshake.CLIENT_HELLO.id) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   394
                (memo.handshakeType == SSLHandshake.HELLO_REQUEST.id) ||
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   395
                (memo.handshakeType ==
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   396
                        SSLHandshake.HELLO_VERIFY_REQUEST.id) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   397
                (memo.handshakeType == SSLHandshake.SERVER_HELLO_DONE.id) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   398
                (memo.handshakeType == SSLHandshake.FINISHED.id)) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   399
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   400
                flightIsReady = true;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   401
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   402
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   403
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   404
        Ciphertext acquireCiphertext(ByteBuffer dstBuf) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   405
            if (isEmpty()) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   406
                if (isRetransmittable()) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   407
                    setRetransmission();    // configure for retransmission
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   408
                } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   409
                    return null;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   410
                }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   411
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   412
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   413
            RecordMemo memo = handshakeMemos.get(acquireIndex);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   414
            HandshakeMemo hsMemo = null;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   415
            if (memo.contentType == ContentType.HANDSHAKE.id) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   416
                hsMemo = (HandshakeMemo)memo;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   417
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   418
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   419
            // ChangeCipherSpec message is pretty small.  Don't worry about
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   420
            // the fragmentation of ChangeCipherSpec record.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   421
            int fragLen;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   422
            if (packetSize > 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   423
                fragLen = Math.min(maxRecordSize, packetSize);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   424
                fragLen = memo.encodeCipher.calculateFragmentSize(
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   425
                        fragLen, 25);   // 25: header size
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   426
                                                //   13: DTLS record
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   427
                                                //   12: DTLS handshake message
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   428
                fragLen = Math.min(fragLen, Record.maxDataSize);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   429
            } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   430
                fragLen = Record.maxDataSize;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   431
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   432
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   433
            if (fragmentSize > 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   434
                fragLen = Math.min(fragLen, fragmentSize);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   435
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   436
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   437
            int dstPos = dstBuf.position();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   438
            int dstLim = dstBuf.limit();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   439
            int dstContent = dstPos + headerSize +
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   440
                                    memo.encodeCipher.getExplicitNonceSize();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   441
            dstBuf.position(dstContent);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   442
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   443
            if (hsMemo != null) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   444
                fragLen = Math.min(fragLen,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   445
                        (hsMemo.fragment.length - hsMemo.acquireOffset));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   446
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   447
                dstBuf.put(hsMemo.handshakeType);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   448
                dstBuf.put((byte)((hsMemo.fragment.length >> 16) & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   449
                dstBuf.put((byte)((hsMemo.fragment.length >> 8) & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   450
                dstBuf.put((byte)(hsMemo.fragment.length & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   451
                dstBuf.put((byte)((hsMemo.messageSequence >> 8) & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   452
                dstBuf.put((byte)(hsMemo.messageSequence & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   453
                dstBuf.put((byte)((hsMemo.acquireOffset >> 16) & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   454
                dstBuf.put((byte)((hsMemo.acquireOffset >> 8) & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   455
                dstBuf.put((byte)(hsMemo.acquireOffset & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   456
                dstBuf.put((byte)((fragLen >> 16) & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   457
                dstBuf.put((byte)((fragLen >> 8) & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   458
                dstBuf.put((byte)(fragLen & 0xFF));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   459
                dstBuf.put(hsMemo.fragment, hsMemo.acquireOffset, fragLen);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   460
            } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   461
                fragLen = Math.min(fragLen, memo.fragment.length);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   462
                dstBuf.put(memo.fragment, 0, fragLen);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   463
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   464
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   465
            dstBuf.limit(dstBuf.position());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   466
            dstBuf.position(dstContent);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   467
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   468
            if (SSLLogger.isOn && SSLLogger.isOn("record")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   469
                SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   470
                        "WRITE: " + protocolVersion + " " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   471
                        ContentType.nameOf(memo.contentType) +
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   472
                        ", length = " + dstBuf.remaining());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   473
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   474
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   475
            // Encrypt the fragment and wrap up a record.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   476
            long recordSN = encrypt(memo.encodeCipher,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   477
                    memo.contentType, dstBuf,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   478
                    dstPos, dstLim, headerSize,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   479
                    ProtocolVersion.valueOf(memo.majorVersion,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   480
                            memo.minorVersion));
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   481
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   482
            if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   483
                ByteBuffer temporary = dstBuf.duplicate();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   484
                temporary.limit(temporary.position());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   485
                temporary.position(dstPos);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   486
                SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   487
                        "Raw write (" + temporary.remaining() + ")", temporary);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   488
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   489
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   490
            // remain the limit unchanged
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   491
            dstBuf.limit(dstLim);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   492
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   493
            // Reset the fragmentation offset.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   494
            if (hsMemo != null) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   495
                hsMemo.acquireOffset += fragLen;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   496
                if (hsMemo.acquireOffset == hsMemo.fragment.length) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   497
                    acquireIndex++;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   498
                }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   499
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   500
                return new Ciphertext(hsMemo.contentType,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   501
                        hsMemo.handshakeType, recordSN);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   502
            } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   503
                acquireIndex++;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   504
                return new Ciphertext(ContentType.CHANGE_CIPHER_SPEC.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   505
                        SSLHandshake.NOT_APPLICABLE.id, recordSN);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   506
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   507
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   508
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   509
        private void handshakeHashing(HandshakeMemo hsFrag, byte[] hsBody) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   510
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   511
            byte hsType = hsFrag.handshakeType;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   512
            if (!handshakeHash.isHashable(hsType)) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   513
                // omitted from handshake hash computation
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   514
                return;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   515
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   516
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   517
            // calculate the DTLS header
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   518
            byte[] temporary = new byte[12];    // 12: handshake header size
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   519
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   520
            // Handshake.msg_type
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   521
            temporary[0] = hsFrag.handshakeType;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   522
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   523
            // Handshake.length
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   524
            temporary[1] = (byte)((hsBody.length >> 16) & 0xFF);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   525
            temporary[2] = (byte)((hsBody.length >> 8) & 0xFF);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   526
            temporary[3] = (byte)(hsBody.length & 0xFF);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   527
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   528
            // Handshake.message_seq
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   529
            temporary[4] = (byte)((hsFrag.messageSequence >> 8) & 0xFF);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   530
            temporary[5] = (byte)(hsFrag.messageSequence & 0xFF);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   531
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   532
            // Handshake.fragment_offset
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   533
            temporary[6] = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   534
            temporary[7] = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   535
            temporary[8] = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   536
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   537
            // Handshake.fragment_length
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   538
            temporary[9] = temporary[1];
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   539
            temporary[10] = temporary[2];
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   540
            temporary[11] = temporary[3];
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   541
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   542
            handshakeHash.deliver(temporary, 0, 12);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   543
            handshakeHash.deliver(hsBody, 0, hsBody.length);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   544
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   545
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   546
        boolean isEmpty() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   547
            if (!flightIsReady || handshakeMemos.isEmpty() ||
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   548
                    acquireIndex >= handshakeMemos.size()) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   549
                return true;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   550
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   551
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   552
            return false;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   553
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   554
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   555
        boolean isRetransmittable() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   556
            return (flightIsReady && !handshakeMemos.isEmpty() &&
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   557
                                (acquireIndex >= handshakeMemos.size()));
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   558
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   559
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   560
        private void setRetransmission() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   561
            acquireIndex = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   562
            for (RecordMemo memo : handshakeMemos) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   563
                if (memo instanceof HandshakeMemo) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   564
                    HandshakeMemo hmemo = (HandshakeMemo)memo;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   565
                    hmemo.acquireOffset = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   566
                }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   567
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   568
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   569
            // Shrink packet size if:
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   570
            // 1. maximum fragment size is allowed, in which case the packet
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   571
            //    size is configured bigger than maxRecordSize;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   572
            // 2. maximum packet is bigger than 256 bytes;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   573
            // 3. two times of retransmits have been attempted.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   574
            if ((packetSize <= maxRecordSize) &&
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   575
                    (packetSize > 256) && ((retransmits--) <= 0)) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   576
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   577
                // shrink packet size
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   578
                shrinkPacketSize();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   579
                retransmits = 2;        // attemps of retransmits
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   580
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   581
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   582
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   583
        private void shrinkPacketSize() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   584
            packetSize = Math.max(256, packetSize / 2);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   585
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   586
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents:
diff changeset
   587
}