jdk/src/share/classes/sun/security/ssl/CipherBox.java
author xuelei
Thu, 12 Jan 2012 03:39:37 -0800
changeset 11521 d7698e6c5f51
parent 10917 becbe249e46b
child 14664 e71aa0962e70
permissions -rw-r--r--
7106773: 512 bits RSA key cannot work with SHA384 and SHA512 Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
     2
 * Copyright (c) 1996, 2011, 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.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.spec.IvParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import static sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class handles bulk data enciphering/deciphering for each SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * message.  This provides data confidentiality.  Stream ciphers (such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * as RC4) don't need to do padding; block ciphers (e.g. DES) need it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Individual instances are obtained by calling the static method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * newCipherBox(), which should only be invoked by BulkCipher.newCipher().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    54
 * In RFC 2246, with bock ciphers in CBC mode, the Initialization
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    55
 * Vector (IV) for the first record is generated with the other keys
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    56
 * and secrets when the security parameters are set.  The IV for
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    57
 * subsequent records is the last ciphertext block from the previous
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    58
 * record.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    59
 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    60
 * In RFC 4346, the implicit Initialization Vector (IV) is replaced
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    61
 * with an explicit IV to protect against CBC attacks.  RFC 4346
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    62
 * recommends two algorithms used to generated the per-record IV.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    63
 * The implementation uses the algorithm (2)(b), as described at
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    64
 * section 6.2.3.2 of RFC 4346.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    65
 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    66
 * The usage of IV in CBC block cipher can be illustrated in
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    67
 * the following diagrams.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    68
 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    69
 *   (random)
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    70
 *        R         P1                    IV        C1
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    71
 *        |          |                     |         |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    72
 *  SIV---+    |-----+    |-...            |-----    |------
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    73
 *        |    |     |    |                |    |    |     |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    74
 *     +----+  |  +----+  |             +----+  |  +----+  |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    75
 *     | Ek |  |  + Ek +  |             | Dk |  |  | Dk |  |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    76
 *     +----+  |  +----+  |             +----+  |  +----+  |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    77
 *        |    |     |    |                |    |    |     |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    78
 *        |----|     |----|           SIV--+    |----|     |-...
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    79
 *        |          |                     |       |
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    80
 *       IV         C1                     R      P1
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    81
 *                                     (discard)
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    82
 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    83
 *       CBC Encryption                    CBC Decryption
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    84
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * NOTE that any ciphering involved in key exchange (e.g. with RSA) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * handled separately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
final class CipherBox {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // A CipherBox that implements the identity operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    final static CipherBox NULL = new CipherBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // the protocol version this cipher conforms to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private final ProtocolVersion protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // cipher object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private final Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Cipher blocksize, 0 for stream ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private int blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   111
     * secure random
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   112
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   113
    private SecureRandom random;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   114
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   115
    /**
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   116
     * Is the cipher of CBC mode?
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   117
     */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   118
    private final boolean isCBCMode;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   119
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   120
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   121
     * Fixed masks of various block size, as the initial decryption IVs
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   122
     * for TLS 1.1 or later.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   123
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   124
     * For performance, we do not use random IVs. As the initial decryption
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   125
     * IVs will be discarded by TLS decryption processes, so the fixed masks
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   126
     * do not hurt cryptographic strength.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   127
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   128
    private static Hashtable<Integer, IvParameterSpec> masks;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   129
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   130
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * NULL cipherbox. Identity operation, no encryption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private CipherBox() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.protocolVersion = ProtocolVersion.DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.cipher = null;
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   136
        this.isCBCMode = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Construct a new CipherBox using the cipher transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @exception NoSuchAlgorithmException if no appropriate JCE Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * implementation could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private CipherBox(ProtocolVersion protocolVersion, BulkCipher bulkCipher,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   146
            SecretKey key, IvParameterSpec iv, SecureRandom random,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   147
            boolean encrypt) throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            this.cipher = JsseJce.getCipher(bulkCipher.transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            int mode = encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   152
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   153
            if (random == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   154
                random = JsseJce.getSecureRandom();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   155
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   156
            this.random = random;
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   157
            this.isCBCMode = bulkCipher.isCBCMode;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   158
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   159
            /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   160
             * RFC 4346 recommends two algorithms used to generated the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   161
             * per-record IV. The implementation uses the algorithm (2)(b),
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   162
             * as described at section 6.2.3.2 of RFC 4346.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   163
             *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   164
             * 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
   165
             * later, so if the "iv" parameter is null, we use the default
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   166
             * value generated by Cipher.init() for encryption, and a fixed
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   167
             * mask for decryption.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   168
             */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   169
            if (iv == null && bulkCipher.ivSize != 0 &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   170
                    mode == Cipher.DECRYPT_MODE &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   171
                    protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   172
                iv = getFixedMask(bulkCipher.ivSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   173
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   174
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   175
            cipher.init(mode, key, iv, random);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   176
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   177
            // Do not call getBlockSize until after init()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            // otherwise we would disrupt JCE delayed provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            blockSize = cipher.getBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // some providers implement getBlockSize() incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (blockSize == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                blockSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    ("Could not create cipher " + bulkCipher, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } catch (ExceptionInInitializerError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    ("Could not create cipher " + bulkCipher, e);
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Factory method to obtain a new CipherBox object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static CipherBox newCipherBox(ProtocolVersion version, BulkCipher cipher,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   199
            SecretKey key, IvParameterSpec iv, SecureRandom random,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   200
            boolean encrypt) throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (cipher.allowed == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new NoSuchAlgorithmException("Unsupported cipher " + cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   204
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (cipher == B_NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   208
            return new CipherBox(version, cipher, key, iv, random, encrypt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   213
     * 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
   214
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   215
    private static IvParameterSpec getFixedMask(int ivSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   216
        if (masks == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   217
            masks = new Hashtable<Integer, IvParameterSpec>(5);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   218
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   219
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   220
        IvParameterSpec iv = masks.get(ivSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   221
        if (iv == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   222
            iv = new IvParameterSpec(new byte[ivSize]);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   223
            masks.put(ivSize, iv);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   224
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   225
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   226
        return iv;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   227
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   228
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   229
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Encrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * resulting block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    int encrypt(byte[] buf, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   237
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (blockSize != 0) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   240
                // TLSv1.1 needs a IV block
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   241
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   242
                    // generate a random number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   243
                    byte[] prefix = new byte[blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   244
                    random.nextBytes(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   245
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   246
                    // move forward the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   247
                    System.arraycopy(buf, offset,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   248
                                     buf, offset + prefix.length, len);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   249
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   250
                    // prefix the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   251
                    System.arraycopy(prefix, 0,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   252
                                     buf, offset, prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   253
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   254
                    len += prefix.length;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   255
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   256
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                len = addPadding(buf, offset, len, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        "Padded plaintext before ENCRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    hd.encodeBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        new ByteArrayInputStream(buf, offset, len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            int newLen = cipher.update(buf, offset, len, buf, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new ArrayIndexOutOfBoundsException(e.toString());
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Encrypts a ByteBuffer block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * resulting block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * The byte buffers position and limit initially define the amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * to encrypt.  On return, the position and limit are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * set to last position padded/encrypted.  The limit may have changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * because of the added padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    int encrypt(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (blockSize != 0) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   305
                // TLSv1.1 needs a IV block
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   306
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   307
                    // generate a random number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   308
                    byte[] prefix = new byte[blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   309
                    random.nextBytes(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   310
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   311
                    // move forward the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   312
                    byte[] buf = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   313
                    int limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   314
                    if (bb.hasArray()) {
10787
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   315
                        int arrayOffset = bb.arrayOffset();
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   316
                        buf = bb.array();
10787
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   317
                        System.arraycopy(buf, arrayOffset + pos,
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   318
                            buf, arrayOffset + pos + prefix.length,
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   319
                            limit - pos);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   320
                        bb.limit(limit + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   321
                    } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   322
                        buf = new byte[limit - pos];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   323
                        bb.get(buf, 0, limit - pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   324
                        bb.position(pos + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   325
                        bb.limit(limit + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   326
                        bb.put(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   327
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   328
                    bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   329
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   330
                    // prefix the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   331
                    bb.put(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   332
                    bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   333
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   334
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                // addPadding adjusts pos/limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                len = addPadding(bb, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        "Padded plaintext before ENCRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                 * reset back to beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
             * Encrypt "in-place".  This does not add its own padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            ByteBuffer dup = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            int newLen = cipher.update(dup, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (bb.position() != dup.position()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                throw new RuntimeException("bytebuffer padding error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            RuntimeException exc = new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw exc;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Decrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * resulting block if padding was required.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   382
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   383
     * For SSLv3 and TLSv1.0, with block ciphers in CBC mode the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   384
     * Initialization Vector (IV) for the first record is generated by
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   385
     * the handshake protocol, the IV for subsequent records is the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   386
     * last ciphertext block from the previous record.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   387
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   388
     * From TLSv1.1, the implicit IV is replaced with an explicit IV to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   389
     * protect against CBC attacks.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   390
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   391
     * Differentiating between bad_record_mac and decryption_failed alerts
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   392
     * may permit certain attacks against CBC mode. It is preferable to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   393
     * uniformly use the bad_record_mac alert to hide the specific type of
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   394
     * the error.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    int decrypt(byte[] buf, int offset, int len) throws BadPaddingException {
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                newLen = removePadding(buf, offset, newLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                             blockSize, protocolVersion);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   423
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   424
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   425
                    if (newLen < blockSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   426
                        throw new BadPaddingException("invalid explicit IV");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   427
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   428
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   429
                    // discards the first cipher block, the IV component.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   430
                    System.arraycopy(buf, offset + blockSize,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   431
                                     buf, offset, newLen - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   432
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   433
                    newLen -= blockSize;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   434
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            throw new ArrayIndexOutOfBoundsException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
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
     * Decrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * resulting block if padding was required.  position and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * point to the end of the decrypted/depadded data.  The initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * limit and new limit may be different, given we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * have stripped off some padding bytes.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   449
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   450
     *  @see decrypt(byte[], int, int)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    int decrypt(ByteBuffer bb) throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
             * Decrypt "in-place".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            ByteBuffer dup = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            int newLen = cipher.update(dup, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                bb.position(pos);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
             * Remove the block padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                newLen = removePadding(bb, blockSize, protocolVersion);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   493
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   494
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   495
                    if (newLen < blockSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   496
                        throw new BadPaddingException("invalid explicit IV");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   497
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   498
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   499
                    // discards the first cipher block, the IV component.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   500
                    byte[] buf = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   501
                    int limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   502
                    if (bb.hasArray()) {
10787
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   503
                        int arrayOffset = bb.arrayOffset();
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   504
                        buf = bb.array();
10787
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   505
                        System.arraycopy(buf, arrayOffset + pos + blockSize,
717ef54d7dd3 7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents: 7039
diff changeset
   506
                            buf, arrayOffset + pos, limit - pos - blockSize);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   507
                        bb.limit(limit - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   508
                    } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   509
                        buf = new byte[limit - pos - blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   510
                        bb.position(pos + blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   511
                        bb.get(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   512
                        bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   513
                        bb.put(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   514
                        bb.limit(limit - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   515
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   516
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   517
                    // reset the position to the end of the decrypted data
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   518
                    limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   519
                    bb.position(limit);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   520
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            RuntimeException exc = new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    private static int addPadding(byte[] buf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        int     newlen = len + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        byte    pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if ((newlen % blockSize) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            newlen += blockSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            newlen -= newlen % blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        pad = (byte) (newlen - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (buf.length < (newlen + offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            throw new IllegalArgumentException("no space to pad buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         * TLS version of the padding works for both SSLv3 and TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        for (i = 0, offset += len; i < pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            buf [offset++] = (byte) (pad - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Apply the padding to the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Limit is advanced to the new buffer length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Position is equal to limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    private static int addPadding(ByteBuffer bb, int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        int     len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int     offset = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        int     newlen = len + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        byte    pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if ((newlen % blockSize) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            newlen += blockSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            newlen -= newlen % blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        pad = (byte) (newlen - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * Update the limit to what will be padded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        bb.limit(newlen + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * TLS version of the padding works for both SSLv3 and TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        for (i = 0, offset += len; i < pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            bb.put(offset++, (byte) (pad - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        bb.position(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        bb.limit(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Typical TLS padding format for a 64 bit block cipher is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *   xx xx xx xx xx xx xx 00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *   xx xx xx xx xx xx 01 01
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *   xx 06 06 06 06 06 06 06
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *   07 07 07 07 07 07 07 07
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * TLS also allows any amount of padding from 1 and 256 bytes as long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * as it makes the data a multiple of the block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    private static int removePadding(byte[] buf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            int blockSize, ProtocolVersion protocolVersion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // last byte is length byte (i.e. actual padding length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        int padOffset = offset + len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int pad = buf[padOffset] & 0x0ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        int newlen = len - (pad + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (newlen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            throw new BadPaddingException("Padding length invalid: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            for (int i = 1; i <= pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                int val = buf[padOffset - i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                if (val != pad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    throw new BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                        ("Invalid TLS padding: " + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else { // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            // SSLv3 requires 0 <= length byte < block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // some implementations do 1 <= length byte <= block size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            // so accept that as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            // v3 does not require any particular value for the other bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (pad > blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                throw new BadPaddingException("Invalid SSLv3 padding: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Position/limit is equal the removed padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    private static int removePadding(ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            int blockSize, ProtocolVersion protocolVersion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        int offset = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // last byte is length byte (i.e. actual padding length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        int padOffset = offset + len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        int pad = bb.get(padOffset) & 0x0ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        int newlen = len - (pad + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (newlen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            throw new BadPaddingException("Padding length invalid: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * We could zero the padding area, but not much useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * information there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            bb.put(padOffset, (byte)0);         // zero the padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            for (int i = 1; i <= pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                int val = bb.get(padOffset - i) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                if (val != pad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    throw new BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                                        ("Invalid TLS padding: " + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        } else { // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            // SSLv3 requires 0 <= length byte < block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            // some implementations do 1 <= length byte <= block size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            // so accept that as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            // v3 does not require any particular value for the other bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (pad > blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                throw new BadPaddingException("Invalid SSLv3 padding: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         * Reset buffer limit to remove padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        bb.position(offset + newlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        bb.limit(offset + newlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
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
   687
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
   688
    /*
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
   689
     * 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
   690
     * 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
   691
     * 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
   692
     */
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
   693
    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
   694
        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
   695
            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
   696
                // 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
   697
                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
   698
            }
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
   699
        } 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
   700
            // 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
   701
        }
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
   702
    }
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
   703
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   704
    /*
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   705
     * Does the cipher use CBC mode?
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   706
     *
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   707
     * @return true if the cipher use CBC mode, false otherwise.
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   708
     */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   709
    boolean isCBCMode() {
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   710
        return isCBCMode;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 7039
diff changeset
   711
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
}