jdk/src/share/classes/sun/security/ssl/CipherBox.java
author xuelei
Thu, 07 Feb 2013 16:05:55 -0800
changeset 16113 946ec9b22004
parent 14664 e71aa0962e70
child 16126 aad71cf676d7
permissions -rw-r--r--
8006777: Improve TLS handling of invalid messages Reviewed-by: wetmore, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1763
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1763
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1763
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1763
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1763
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
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.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    31
import java.util.Hashtable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.IvParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import static sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This class handles bulk data enciphering/deciphering for each SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * message.  This provides data confidentiality.  Stream ciphers (such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * as RC4) don't need to do padding; block ciphers (e.g. DES) need it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Individual instances are obtained by calling the static method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * newCipherBox(), which should only be invoked by BulkCipher.newCipher().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    53
 * In RFC 2246, with bock ciphers in CBC mode, the Initialization
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    54
 * Vector (IV) for the first record is generated with the other keys
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    55
 * and secrets when the security parameters are set.  The IV for
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    56
 * subsequent records is the last ciphertext block from the previous
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    57
 * record.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    58
 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    59
 * In RFC 4346, the implicit Initialization Vector (IV) is replaced
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    60
 * with an explicit IV to protect against CBC attacks.  RFC 4346
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    61
 * recommends two algorithms used to generated the per-record IV.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    62
 * The implementation uses the algorithm (2)(b), as described at
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    63
 * section 6.2.3.2 of RFC 4346.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    64
 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    65
 * The usage of IV in CBC block cipher can be illustrated in
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    66
 * the following diagrams.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    67
 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    68
 *   (random)
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    69
 *        R         P1                    IV        C1
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    70
 *        |          |                     |         |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    71
 *  SIV---+    |-----+    |-...            |-----    |------
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    72
 *        |    |     |    |                |    |    |     |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    73
 *     +----+  |  +----+  |             +----+  |  +----+  |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    74
 *     | Ek |  |  + Ek +  |             | Dk |  |  | Dk |  |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    75
 *     +----+  |  +----+  |             +----+  |  +----+  |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    76
 *        |    |     |    |                |    |    |     |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    77
 *        |----|     |----|           SIV--+    |----|     |-...
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    78
 *        |          |                     |       |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    79
 *       IV         C1                     R      P1
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    80
 *                                     (discard)
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    81
 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    82
 *       CBC Encryption                    CBC Decryption
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    83
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * NOTE that any ciphering involved in key exchange (e.g. with RSA) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * handled separately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
final class CipherBox {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // A CipherBox that implements the identity operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    final static CipherBox NULL = new CipherBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // the protocol version this cipher conforms to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private final ProtocolVersion protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // cipher object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Cipher blocksize, 0 for stream ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private int blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   110
     * secure random
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   111
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   112
    private SecureRandom random;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   113
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   114
    /**
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   115
     * Is the cipher of CBC mode?
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   116
     */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   117
    private final boolean isCBCMode;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   118
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   119
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   120
     * Fixed masks of various block size, as the initial decryption IVs
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   121
     * for TLS 1.1 or later.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   122
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   123
     * For performance, we do not use random IVs. As the initial decryption
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   124
     * IVs will be discarded by TLS decryption processes, so the fixed masks
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   125
     * do not hurt cryptographic strength.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   126
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   127
    private static Hashtable<Integer, IvParameterSpec> masks;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   128
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   129
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * NULL cipherbox. Identity operation, no encryption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private CipherBox() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.protocolVersion = ProtocolVersion.DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.cipher = null;
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   135
        this.isCBCMode = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Construct a new CipherBox using the cipher transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception NoSuchAlgorithmException if no appropriate JCE Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * implementation could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private CipherBox(ProtocolVersion protocolVersion, BulkCipher bulkCipher,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   145
            SecretKey key, IvParameterSpec iv, SecureRandom random,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   146
            boolean encrypt) throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            this.cipher = JsseJce.getCipher(bulkCipher.transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            int mode = encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   151
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   152
            if (random == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   153
                random = JsseJce.getSecureRandom();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   154
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   155
            this.random = random;
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   156
            this.isCBCMode = bulkCipher.isCBCMode;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   157
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   158
            /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   159
             * RFC 4346 recommends two algorithms used to generated the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   160
             * per-record IV. The implementation uses the algorithm (2)(b),
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   161
             * as described at section 6.2.3.2 of RFC 4346.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   162
             *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   163
             * As we don't care about the initial IV value for TLS 1.1 or
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   164
             * later, so if the "iv" parameter is null, we use the default
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   165
             * value generated by Cipher.init() for encryption, and a fixed
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   166
             * mask for decryption.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   167
             */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   168
            if (iv == null && bulkCipher.ivSize != 0 &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   169
                    mode == Cipher.DECRYPT_MODE &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   170
                    protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   171
                iv = getFixedMask(bulkCipher.ivSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   172
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   173
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   174
            cipher.init(mode, key, iv, random);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   175
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   176
            // Do not call getBlockSize until after init()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // otherwise we would disrupt JCE delayed provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            blockSize = cipher.getBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // some providers implement getBlockSize() incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (blockSize == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                blockSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    ("Could not create cipher " + bulkCipher, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } catch (ExceptionInInitializerError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    ("Could not create cipher " + bulkCipher, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Factory method to obtain a new CipherBox object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    static CipherBox newCipherBox(ProtocolVersion version, BulkCipher cipher,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   198
            SecretKey key, IvParameterSpec iv, SecureRandom random,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   199
            boolean encrypt) throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (cipher.allowed == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new NoSuchAlgorithmException("Unsupported cipher " + cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   203
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (cipher == B_NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } else {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   207
            return new CipherBox(version, cipher, key, iv, random, encrypt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   212
     * Get a fixed mask, as the initial decryption IVs for TLS 1.1 or later.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   213
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   214
    private static IvParameterSpec getFixedMask(int ivSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   215
        if (masks == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   216
            masks = new Hashtable<Integer, IvParameterSpec>(5);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   217
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   218
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   219
        IvParameterSpec iv = masks.get(ivSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   220
        if (iv == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   221
            iv = new IvParameterSpec(new byte[ivSize]);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   222
            masks.put(ivSize, iv);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   223
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   224
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   225
        return iv;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   226
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   227
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   228
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Encrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * resulting block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    int encrypt(byte[] buf, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   236
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (blockSize != 0) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   239
                // TLSv1.1 needs a IV block
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   240
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   241
                    // generate a random number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   242
                    byte[] prefix = new byte[blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   243
                    random.nextBytes(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   244
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   245
                    // move forward the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   246
                    System.arraycopy(buf, offset,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   247
                                     buf, offset + prefix.length, len);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   248
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   249
                    // prefix the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   250
                    System.arraycopy(prefix, 0,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   251
                                     buf, offset, prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   252
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   253
                    len += prefix.length;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   254
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   255
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                len = addPadding(buf, offset, len, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        "Padded plaintext before ENCRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    hd.encodeBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        new ByteArrayInputStream(buf, offset, len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            int newLen = cipher.update(buf, offset, len, buf, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new ArrayIndexOutOfBoundsException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Encrypts a ByteBuffer block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * resulting block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * The byte buffers position and limit initially define the amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * to encrypt.  On return, the position and limit are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * set to last position padded/encrypted.  The limit may have changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * because of the added padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    int encrypt(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (blockSize != 0) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   304
                // TLSv1.1 needs a IV block
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   305
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   306
                    // generate a random number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   307
                    byte[] prefix = new byte[blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   308
                    random.nextBytes(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   309
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   310
                    // move forward the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   311
                    byte[] buf = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   312
                    int limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   313
                    if (bb.hasArray()) {
10787
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   314
                        int arrayOffset = bb.arrayOffset();
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   315
                        buf = bb.array();
10787
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   316
                        System.arraycopy(buf, arrayOffset + pos,
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   317
                            buf, arrayOffset + pos + prefix.length,
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   318
                            limit - pos);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   319
                        bb.limit(limit + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   320
                    } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   321
                        buf = new byte[limit - pos];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   322
                        bb.get(buf, 0, limit - pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   323
                        bb.position(pos + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   324
                        bb.limit(limit + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   325
                        bb.put(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   326
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   327
                    bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   328
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   329
                    // prefix the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   330
                    bb.put(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   331
                    bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   332
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   333
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // addPadding adjusts pos/limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                len = addPadding(bb, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        "Padded plaintext before ENCRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                 * reset back to beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
             * Encrypt "in-place".  This does not add its own padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            ByteBuffer dup = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            int newLen = cipher.update(dup, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (bb.position() != dup.position()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                throw new RuntimeException("bytebuffer padding error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            RuntimeException exc = new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Decrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * resulting block if padding was required.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   381
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   382
     * For SSLv3 and TLSv1.0, with block ciphers in CBC mode the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   383
     * Initialization Vector (IV) for the first record is generated by
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   384
     * the handshake protocol, the IV for subsequent records is the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   385
     * last ciphertext block from the previous record.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   386
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   387
     * From TLSv1.1, the implicit IV is replaced with an explicit IV to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   388
     * protect against CBC attacks.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   389
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   390
     * Differentiating between bad_record_mac and decryption_failed alerts
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   391
     * may permit certain attacks against CBC mode. It is preferable to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   392
     * uniformly use the bad_record_mac alert to hide the specific type of
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   393
     * the error.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   395
    int decrypt(byte[] buf, int offset, int len,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   396
            int tagLen) throws BadPaddingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   400
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            int newLen = cipher.update(buf, offset, len, buf, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        "Padded plaintext after DECRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        + newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    hd.encodeBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        new ByteArrayInputStream(buf, offset, newLen),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   420
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (blockSize != 0) {
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   422
                newLen = removePadding(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   423
                    buf, offset, newLen, tagLen, blockSize, protocolVersion);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   424
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   425
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   426
                    if (newLen < blockSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   427
                        throw new BadPaddingException("invalid explicit IV");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   428
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   429
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   430
                    // discards the first cipher block, the IV component.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   431
                    System.arraycopy(buf, offset + blockSize,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   432
                                     buf, offset, newLen - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   433
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   434
                    newLen -= blockSize;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   435
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            throw new ArrayIndexOutOfBoundsException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Decrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * resulting block if padding was required.  position and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * point to the end of the decrypted/depadded data.  The initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * limit and new limit may be different, given we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * have stripped off some padding bytes.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   450
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   451
     *  @see decrypt(byte[], int, int)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   453
    int decrypt(ByteBuffer bb, int tagLen) throws BadPaddingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
             * Decrypt "in-place".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            ByteBuffer dup = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            int newLen = cipher.update(dup, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        "Padded plaintext after DECRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        + newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   483
                    hd.encodeBuffer(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   484
                        (ByteBuffer)bb.duplicate().position(pos), System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
             * Remove the block padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                bb.position(pos);
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   493
                newLen = removePadding(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   494
                    bb, tagLen, blockSize, protocolVersion);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   495
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   496
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   497
                    if (newLen < blockSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   498
                        throw new BadPaddingException("invalid explicit IV");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   499
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   500
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   501
                    // discards the first cipher block, the IV component.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   502
                    byte[] buf = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   503
                    int limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   504
                    if (bb.hasArray()) {
10787
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   505
                        int arrayOffset = bb.arrayOffset();
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   506
                        buf = bb.array();
10787
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   507
                        System.arraycopy(buf, arrayOffset + pos + blockSize,
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   508
                            buf, arrayOffset + pos, limit - pos - blockSize);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   509
                        bb.limit(limit - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   510
                    } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   511
                        buf = new byte[limit - pos - blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   512
                        bb.position(pos + blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   513
                        bb.get(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   514
                        bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   515
                        bb.put(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   516
                        bb.limit(limit - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   517
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   518
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   519
                    // reset the position to the end of the decrypted data
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   520
                    limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   521
                    bb.position(limit);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   522
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            RuntimeException exc = new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private static int addPadding(byte[] buf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int     newlen = len + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        byte    pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if ((newlen % blockSize) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            newlen += blockSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            newlen -= newlen % blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        pad = (byte) (newlen - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (buf.length < (newlen + offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            throw new IllegalArgumentException("no space to pad buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
         * TLS version of the padding works for both SSLv3 and TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        for (i = 0, offset += len; i < pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            buf [offset++] = (byte) (pad - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Apply the padding to the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Limit is advanced to the new buffer length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Position is equal to limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    private static int addPadding(ByteBuffer bb, int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        int     len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        int     offset = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        int     newlen = len + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        byte    pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if ((newlen % blockSize) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            newlen += blockSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            newlen -= newlen % blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        pad = (byte) (newlen - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * Update the limit to what will be padded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        bb.limit(newlen + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * TLS version of the padding works for both SSLv3 and TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        for (i = 0, offset += len; i < pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            bb.put(offset++, (byte) (pad - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        bb.position(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        bb.limit(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   596
    /*
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   597
     * A constant-time check of the padding.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   598
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   599
     * NOTE that we are checking both the padding and the padLen bytes here.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   600
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   601
     * The caller MUST ensure that the len parameter is a positive number.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   602
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   603
    private static int[] checkPadding(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   604
            byte[] buf, int offset, int len, byte pad) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   605
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   606
        if (len <= 0) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   607
            throw new RuntimeException("padding len must be positive");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   608
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   609
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   610
        // An array of hits is used to prevent Hotspot optimization for
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   611
        // the purpose of a constant-time check.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   612
        int[] results = {0, 0};    // {missed #, matched #}
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   613
        for (int i = 0; i <= 256;) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   614
            for (int j = 0; j < len && i <= 256; j++, i++) {     // j <= i
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   615
                if (buf[offset + j] != pad) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   616
                    results[0]++;       // mismatched padding data
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   617
                } else {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   618
                    results[1]++;       // matched padding data
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   619
                }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   620
            }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   621
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   622
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   623
        return results;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   624
    }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   625
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   626
    /*
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   627
     * A constant-time check of the padding.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   628
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   629
     * NOTE that we are checking both the padding and the padLen bytes here.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   630
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   631
     * The caller MUST ensure that the bb parameter has remaining.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   632
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   633
    private static int[] checkPadding(ByteBuffer bb, byte pad) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   634
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   635
        if (!bb.hasRemaining()) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   636
            throw new RuntimeException("hasRemaining() must be positive");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   637
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   638
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   639
        // An array of hits is used to prevent Hotspot optimization for
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   640
        // the purpose of a constant-time check.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   641
        int[] results = {0, 0};    // {missed #, matched #}
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   642
        bb.mark();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   643
        for (int i = 0; i <= 256; bb.reset()) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   644
            for (; bb.hasRemaining() && i <= 256; i++) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   645
                if (bb.get() != pad) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   646
                    results[0]++;       // mismatched padding data
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   647
                } else {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   648
                    results[1]++;       // matched padding data
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   649
                }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   650
            }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   651
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   652
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   653
        return results;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   654
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Typical TLS padding format for a 64 bit block cipher is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *   xx xx xx xx xx xx xx 00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *   xx xx xx xx xx xx 01 01
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *   xx 06 06 06 06 06 06 06
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *   07 07 07 07 07 07 07 07
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * TLS also allows any amount of padding from 1 and 256 bytes as long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * as it makes the data a multiple of the block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    private static int removePadding(byte[] buf, int offset, int len,
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   667
            int tagLen, int blockSize,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   668
            ProtocolVersion protocolVersion) throws BadPaddingException {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   669
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        // last byte is length byte (i.e. actual padding length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        int padOffset = offset + len - 1;
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   672
        int padLen = buf[padOffset] & 0xFF;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   674
        int newLen = len - (padLen + 1);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   675
        if ((newLen - tagLen) < 0) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   676
            // If the buffer is not long enough to contain the padding plus
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   677
            // a MAC tag, do a dummy constant-time padding check.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   678
            //
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   679
            // Note that it is a dummy check, so we won't care about what is
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   680
            // the actual padding data.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   681
            checkPadding(buf, offset, len, (byte)(padLen & 0xFF));
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   682
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   683
            throw new BadPaddingException("Invalid Padding length: " + padLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   686
        // The padding data should be filled with the padding length value.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   687
        int[] results = checkPadding(buf, offset + newLen,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   688
                        padLen + 1, (byte)(padLen & 0xFF));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   690
            if (results[0] != 0) {          // padding data has invalid bytes
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   691
                throw new BadPaddingException("Invalid TLS padding data");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        } else { // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            // SSLv3 requires 0 <= length byte < block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            // some implementations do 1 <= length byte <= block size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            // so accept that as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            // v3 does not require any particular value for the other bytes
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   698
            if (padLen > blockSize) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   699
                throw new BadPaddingException("Invalid SSLv3 padding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   702
        return newLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * Position/limit is equal the removed padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    private static int removePadding(ByteBuffer bb,
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   709
            int tagLen, int blockSize,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   710
            ProtocolVersion protocolVersion) throws BadPaddingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        int offset = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        // last byte is length byte (i.e. actual padding length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        int padOffset = offset + len - 1;
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   717
        int padLen = bb.get(padOffset) & 0xFF;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   719
        int newLen = len - (padLen + 1);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   720
        if ((newLen - tagLen) < 0) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   721
            // If the buffer is not long enough to contain the padding plus
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   722
            // a MAC tag, do a dummy constant-time padding check.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   723
            //
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   724
            // Note that it is a dummy check, so we won't care about what is
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   725
            // the actual padding data.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   726
            checkPadding(bb.duplicate(), (byte)(padLen & 0xFF));
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   727
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   728
            throw new BadPaddingException("Invalid Padding length: " + padLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   731
        // The padding data should be filled with the padding length value.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   732
        int[] results = checkPadding(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   733
                (ByteBuffer)bb.duplicate().position(offset + newLen),
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   734
                (byte)(padLen & 0xFF));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   736
            if (results[0] != 0) {          // padding data has invalid bytes
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   737
                throw new BadPaddingException("Invalid TLS padding data");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        } else { // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            // SSLv3 requires 0 <= length byte < block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            // some implementations do 1 <= length byte <= block size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            // so accept that as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            // v3 does not require any particular value for the other bytes
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   744
            if (padLen > blockSize) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   745
                throw new BadPaddingException("Invalid SSLv3 padding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * Reset buffer limit to remove padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         */
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   752
        bb.position(offset + newLen);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   753
        bb.limit(offset + newLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   755
        return newLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
1763
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   757
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   758
    /*
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   759
     * Dispose of any intermediate state in the underlying cipher.
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   760
     * For PKCS11 ciphers, this will release any attached sessions, and
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   761
     * thus make finalization faster.
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   762
     */
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   763
    void dispose() {
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   764
        try {
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   765
            if (cipher != null) {
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   766
                // ignore return value.
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   767
                cipher.doFinal();
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   768
            }
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   769
        } catch (GeneralSecurityException e) {
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   770
            // swallow for now.
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   771
        }
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   772
    }
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   773
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   774
    /*
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   775
     * Does the cipher use CBC mode?
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   776
     *
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   777
     * @return true if the cipher use CBC mode, false otherwise.
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   778
     */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   779
    boolean isCBCMode() {
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   780
        return isCBCMode;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   781
    }
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   782
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   783
    /**
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   784
     * Is the cipher null?
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   785
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   786
     * @return true if the cipher is null, false otherwise.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   787
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   788
    boolean isNullCipher() {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   789
        return cipher == null;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   790
    }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   791
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   792
    /**
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   793
     * Sanity check the length of a fragment before decryption.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   794
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   795
     * In CBC mode, check that the fragment length is one or multiple times
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   796
     * of the block size of the cipher suite, and is at least one (one is the
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   797
     * smallest size of padding in CBC mode) bigger than the tag size of the
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   798
     * MAC algorithm except the explicit IV size for TLS 1.1 or later.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   799
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   800
     * In non-CBC mode, check that the fragment length is not less than the
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   801
     * tag size of the MAC algorithm.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   802
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   803
     * @return true if the length of a fragment matches above requirements
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   804
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   805
    boolean sanityCheck(int tagLen, int fragmentLen) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   806
        if (!isCBCMode) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   807
            return fragmentLen >= tagLen;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   808
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   809
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   810
        if ((fragmentLen % blockSize) == 0) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   811
            int minimal = tagLen + 1;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   812
            minimal = (minimal >= blockSize) ? minimal : blockSize;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   813
            if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   814
                minimal += blockSize;   // plus the size of the explicit IV
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   815
            }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   816
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   817
            return (fragmentLen >= minimal);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   818
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   819
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   820
        return false;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   821
    }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   822
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
}