jdk/src/share/classes/sun/security/ssl/CipherBox.java
author xuelei
Sat, 30 Oct 2010 18:39:17 +0800
changeset 7039 6464c8e62a18
parent 5506 202f599c92aa
child 10915 1e20964cebf3
child 10787 717ef54d7dd3
permissions -rw-r--r--
4873188: Support TLS 1.1 Reviewed-by: wetmore, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   116
     * Fixed masks of various block size, as the initial decryption IVs
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   117
     * for TLS 1.1 or later.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   118
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   119
     * For performance, we do not use random IVs. As the initial decryption
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   120
     * IVs will be discarded by TLS decryption processes, so the fixed masks
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   121
     * do not hurt cryptographic strength.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   122
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   123
    private static Hashtable<Integer, IvParameterSpec> masks;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   124
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   125
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * NULL cipherbox. Identity operation, no encryption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private CipherBox() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.protocolVersion = ProtocolVersion.DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.cipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Construct a new CipherBox using the cipher transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @exception NoSuchAlgorithmException if no appropriate JCE Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * implementation could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private CipherBox(ProtocolVersion protocolVersion, BulkCipher bulkCipher,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   140
            SecretKey key, IvParameterSpec iv, SecureRandom random,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   141
            boolean encrypt) throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            this.cipher = JsseJce.getCipher(bulkCipher.transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            int mode = encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   146
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   147
            if (random == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   148
                random = JsseJce.getSecureRandom();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   149
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   150
            this.random = random;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   151
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   152
            /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   153
             * RFC 4346 recommends two algorithms used to generated the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   154
             * per-record IV. The implementation uses the algorithm (2)(b),
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   155
             * as described at section 6.2.3.2 of RFC 4346.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   156
             *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   157
             * 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
   158
             * later, so if the "iv" parameter is null, we use the default
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   159
             * value generated by Cipher.init() for encryption, and a fixed
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   160
             * mask for decryption.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   161
             */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   162
            if (iv == null && bulkCipher.ivSize != 0 &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   163
                    mode == Cipher.DECRYPT_MODE &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   164
                    protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   165
                iv = getFixedMask(bulkCipher.ivSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   166
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   167
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   168
            cipher.init(mode, key, iv, random);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   169
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   170
            // Do not call getBlockSize until after init()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // otherwise we would disrupt JCE delayed provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            blockSize = cipher.getBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // some providers implement getBlockSize() incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (blockSize == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                blockSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    ("Could not create cipher " + bulkCipher, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (ExceptionInInitializerError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    ("Could not create cipher " + bulkCipher, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Factory method to obtain a new CipherBox object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static CipherBox newCipherBox(ProtocolVersion version, BulkCipher cipher,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   192
            SecretKey key, IvParameterSpec iv, SecureRandom random,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   193
            boolean encrypt) throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (cipher.allowed == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new NoSuchAlgorithmException("Unsupported cipher " + cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   197
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (cipher == B_NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   201
            return new CipherBox(version, cipher, key, iv, random, encrypt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   206
     * 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
   207
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   208
    private static IvParameterSpec getFixedMask(int ivSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   209
        if (masks == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   210
            masks = new Hashtable<Integer, IvParameterSpec>(5);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   211
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   212
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   213
        IvParameterSpec iv = masks.get(ivSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   214
        if (iv == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   215
            iv = new IvParameterSpec(new byte[ivSize]);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   216
            masks.put(ivSize, iv);
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
        return iv;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   220
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   221
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   222
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Encrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * resulting block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    int encrypt(byte[] buf, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   230
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (blockSize != 0) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   233
                // TLSv1.1 needs a IV block
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   234
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   235
                    // generate a random number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   236
                    byte[] prefix = new byte[blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   237
                    random.nextBytes(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   238
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   239
                    // move forward the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   240
                    System.arraycopy(buf, offset,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   241
                                     buf, offset + prefix.length, len);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   242
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   243
                    // prefix the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   244
                    System.arraycopy(prefix, 0,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   245
                                     buf, offset, prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   246
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   247
                    len += prefix.length;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   248
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   249
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                len = addPadding(buf, offset, len, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        "Padded plaintext before ENCRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    hd.encodeBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        new ByteArrayInputStream(buf, offset, len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            int newLen = cipher.update(buf, offset, len, buf, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            throw new ArrayIndexOutOfBoundsException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Encrypts a ByteBuffer block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * resulting block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * The byte buffers position and limit initially define the amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * to encrypt.  On return, the position and limit are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * set to last position padded/encrypted.  The limit may have changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * because of the added padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    int encrypt(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (blockSize != 0) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   298
                // TLSv1.1 needs a IV block
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   299
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   300
                    // generate a random number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   301
                    byte[] prefix = new byte[blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   302
                    random.nextBytes(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   303
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   304
                    // move forward the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   305
                    byte[] buf = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   306
                    int limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   307
                    if (bb.hasArray()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   308
                        buf = bb.array();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   309
                        System.arraycopy(buf, pos,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   310
                                buf, pos + prefix.length, limit - pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   311
                        bb.limit(limit + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   312
                    } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   313
                        buf = new byte[limit - pos];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   314
                        bb.get(buf, 0, limit - pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   315
                        bb.position(pos + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   316
                        bb.limit(limit + prefix.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   317
                        bb.put(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   318
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   319
                    bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   320
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   321
                    // prefix the plaintext
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   322
                    bb.put(prefix);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   323
                    bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   324
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   325
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // addPadding adjusts pos/limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                len = addPadding(bb, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        "Padded plaintext before ENCRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                 * reset back to beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
             * Encrypt "in-place".  This does not add its own padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            ByteBuffer dup = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            int newLen = cipher.update(dup, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (bb.position() != dup.position()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                throw new RuntimeException("bytebuffer padding error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            RuntimeException exc = new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Decrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * resulting block if padding was required.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   373
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   374
     * For SSLv3 and TLSv1.0, with block ciphers in CBC mode the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   375
     * Initialization Vector (IV) for the first record is generated by
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   376
     * the handshake protocol, the IV for subsequent records is the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   377
     * last ciphertext block from the previous record.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   378
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   379
     * From TLSv1.1, the implicit IV is replaced with an explicit IV to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   380
     * protect against CBC attacks.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   381
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   382
     * Differentiating between bad_record_mac and decryption_failed alerts
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   383
     * may permit certain attacks against CBC mode. It is preferable to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   384
     * uniformly use the bad_record_mac alert to hide the specific type of
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   385
     * the error.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    int decrypt(byte[] buf, int offset, int len) throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   391
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            int newLen = cipher.update(buf, offset, len, buf, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        "Padded plaintext after DECRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        + newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    hd.encodeBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        new ByteArrayInputStream(buf, offset, newLen),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                newLen = removePadding(buf, offset, newLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                             blockSize, protocolVersion);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   414
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   415
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   416
                    if (newLen < blockSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   417
                        throw new BadPaddingException("invalid explicit IV");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   418
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   419
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   420
                    // discards the first cipher block, the IV component.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   421
                    System.arraycopy(buf, offset + blockSize,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   422
                                     buf, offset, newLen - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   423
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   424
                    newLen -= blockSize;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   425
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            throw new ArrayIndexOutOfBoundsException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Decrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * resulting block if padding was required.  position and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * point to the end of the decrypted/depadded data.  The initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * limit and new limit may be different, given we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * have stripped off some padding bytes.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   440
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   441
     *  @see decrypt(byte[], int, int)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    int decrypt(ByteBuffer bb) throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
             * Decrypt "in-place".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            ByteBuffer dup = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            int newLen = cipher.update(dup, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        "Padded plaintext after DECRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        + newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
             * Remove the block padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                newLen = removePadding(bb, blockSize, protocolVersion);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   484
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   485
                if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   486
                    if (newLen < blockSize) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   487
                        throw new BadPaddingException("invalid explicit IV");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   488
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   489
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   490
                    // discards the first cipher block, the IV component.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   491
                    byte[] buf = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   492
                    int limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   493
                    if (bb.hasArray()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   494
                        buf = bb.array();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   495
                        System.arraycopy(buf, pos + blockSize,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   496
                                         buf, pos, limit - pos - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   497
                        bb.limit(limit - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   498
                    } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   499
                        buf = new byte[limit - pos - blockSize];
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   500
                        bb.position(pos + blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   501
                        bb.get(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   502
                        bb.position(pos);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   503
                        bb.put(buf);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   504
                        bb.limit(limit - blockSize);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   505
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   506
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   507
                    // reset the position to the end of the decrypted data
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   508
                    limit = bb.limit();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   509
                    bb.position(limit);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   510
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            RuntimeException exc = new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private static int addPadding(byte[] buf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int     newlen = len + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        byte    pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if ((newlen % blockSize) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            newlen += blockSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            newlen -= newlen % blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        pad = (byte) (newlen - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (buf.length < (newlen + offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            throw new IllegalArgumentException("no space to pad buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         * TLS version of the padding works for both SSLv3 and TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        for (i = 0, offset += len; i < pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            buf [offset++] = (byte) (pad - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Apply the padding to the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Limit is advanced to the new buffer length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Position is equal to limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private static int addPadding(ByteBuffer bb, int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        int     len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int     offset = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        int     newlen = len + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        byte    pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if ((newlen % blockSize) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            newlen += blockSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            newlen -= newlen % blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        pad = (byte) (newlen - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * Update the limit to what will be padded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        bb.limit(newlen + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         * TLS version of the padding works for both SSLv3 and TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        for (i = 0, offset += len; i < pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            bb.put(offset++, (byte) (pad - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        bb.position(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        bb.limit(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Typical TLS padding format for a 64 bit block cipher is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *   xx xx xx xx xx xx xx 00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *   xx xx xx xx xx xx 01 01
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *   xx 06 06 06 06 06 06 06
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *   07 07 07 07 07 07 07 07
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * TLS also allows any amount of padding from 1 and 256 bytes as long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * as it makes the data a multiple of the block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    private static int removePadding(byte[] buf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            int blockSize, ProtocolVersion protocolVersion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        // last byte is length byte (i.e. actual padding length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        int padOffset = offset + len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        int pad = buf[padOffset] & 0x0ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        int newlen = len - (pad + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (newlen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new BadPaddingException("Padding length invalid: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            for (int i = 1; i <= pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                int val = buf[padOffset - i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                if (val != pad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    throw new BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                        ("Invalid TLS padding: " + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        } else { // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            // SSLv3 requires 0 <= length byte < block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            // some implementations do 1 <= length byte <= block size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            // so accept that as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            // v3 does not require any particular value for the other bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (pad > blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                throw new BadPaddingException("Invalid SSLv3 padding: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Position/limit is equal the removed padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    private static int removePadding(ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            int blockSize, ProtocolVersion protocolVersion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        int offset = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        // last byte is length byte (i.e. actual padding length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        int padOffset = offset + len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        int pad = bb.get(padOffset) & 0x0ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        int newlen = len - (pad + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (newlen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            throw new BadPaddingException("Padding length invalid: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         * We could zero the padding area, but not much useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         * information there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            bb.put(padOffset, (byte)0);         // zero the padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            for (int i = 1; i <= pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                int val = bb.get(padOffset - i) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                if (val != pad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    throw new BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                        ("Invalid TLS padding: " + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        } else { // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            // SSLv3 requires 0 <= length byte < block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            // some implementations do 1 <= length byte <= block size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            // so accept that as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            // v3 does not require any particular value for the other bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            if (pad > blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                throw new BadPaddingException("Invalid SSLv3 padding: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         * Reset buffer limit to remove padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        bb.position(offset + newlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        bb.limit(offset + newlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
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
   677
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
   678
    /*
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
   679
     * 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
   680
     * 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
   681
     * 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
   682
     */
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
   683
    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
   684
        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
   685
            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
   686
                // 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
   687
                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
   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
        } 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
   690
            // 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
   691
        }
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
}