jdk/src/share/classes/sun/security/ssl/CipherBox.java
author wetmore
Fri, 19 Dec 2008 10:35:56 +0800
changeset 1763 0a6b65d56746
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider Summary: This is the JSSE portion of the fix. Main part is in PKCS11. Reviewed-by: valeriep, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
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
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.IvParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import static sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This class handles bulk data enciphering/deciphering for each SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * message.  This provides data confidentiality.  Stream ciphers (such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * as RC4) don't need to do padding; block ciphers (e.g. DES) need it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Individual instances are obtained by calling the static method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * newCipherBox(), which should only be invoked by BulkCipher.newCipher().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * NOTE that any ciphering involved in key exchange (e.g. with RSA) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * handled separately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
final class CipherBox {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // A CipherBox that implements the identity operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    final static CipherBox NULL = new CipherBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // the protocol version this cipher conforms to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final ProtocolVersion protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // cipher object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Cipher blocksize, 0 for stream ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private int blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * NULL cipherbox. Identity operation, no encryption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private CipherBox() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.protocolVersion = ProtocolVersion.DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.cipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Construct a new CipherBox using the cipher transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @exception NoSuchAlgorithmException if no appropriate JCE Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * implementation could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private CipherBox(ProtocolVersion protocolVersion, BulkCipher bulkCipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            SecretKey key,  IvParameterSpec iv, boolean encrypt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            this.cipher = JsseJce.getCipher(bulkCipher.transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            int mode = encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            cipher.init(mode, key, iv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // do not call getBlockSize until after init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // otherwise we would disrupt JCE delayed provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            blockSize = cipher.getBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // some providers implement getBlockSize() incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (blockSize == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                blockSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    ("Could not create cipher " + bulkCipher, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (ExceptionInInitializerError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    ("Could not create cipher " + bulkCipher, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Factory method to obtain a new CipherBox object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static CipherBox newCipherBox(ProtocolVersion version, BulkCipher cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            SecretKey key, IvParameterSpec iv, boolean encrypt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (cipher.allowed == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new NoSuchAlgorithmException("Unsupported cipher " + cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (cipher == B_NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return new CipherBox(version, cipher, key, iv, encrypt);
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Encrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * resulting block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    int encrypt(byte[] buf, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                len = addPadding(buf, offset, len, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        "Padded plaintext before ENCRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    hd.encodeBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        new ByteArrayInputStream(buf, offset, len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            int newLen = cipher.update(buf, offset, len, buf, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new ArrayIndexOutOfBoundsException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Encrypts a ByteBuffer block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * resulting block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The byte buffers position and limit initially define the amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * to encrypt.  On return, the position and limit are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * set to last position padded/encrypted.  The limit may have changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * because of the added padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    int encrypt(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                // addPadding adjusts pos/limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                len = addPadding(bb, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        "Padded plaintext before ENCRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                 * reset back to beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
             * Encrypt "in-place".  This does not add its own padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            ByteBuffer dup = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            int newLen = cipher.update(dup, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (bb.position() != dup.position()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                throw new RuntimeException("bytebuffer padding error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            RuntimeException exc = new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Decrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * resulting block if padding was required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    int decrypt(byte[] buf, int offset, int len) throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            int newLen = cipher.update(buf, offset, len, buf, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        "Padded plaintext after DECRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        + newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    hd.encodeBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        new ByteArrayInputStream(buf, offset, newLen),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                newLen = removePadding(buf, offset, newLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                             blockSize, protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new ArrayIndexOutOfBoundsException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Decrypts a block of data, returning the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * resulting block if padding was required.  position and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * point to the end of the decrypted/depadded data.  The initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * limit and new limit may be different, given we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * have stripped off some padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    int decrypt(ByteBuffer bb) throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (cipher == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            bb.position(bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
             * Decrypt "in-place".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            ByteBuffer dup = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            int newLen = cipher.update(dup, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (newLen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                // catch BouncyCastle buffering error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                throw new RuntimeException("Cipher buffering error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    "in JCE provider " + cipher.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (debug != null && Debug.isOn("plaintext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        "Padded plaintext after DECRYPTION:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        + newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
             * Remove the block padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (blockSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                bb.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                newLen = removePadding(bb, blockSize, protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            RuntimeException exc = new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private static int addPadding(byte[] buf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int     newlen = len + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        byte    pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if ((newlen % blockSize) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            newlen += blockSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            newlen -= newlen % blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        pad = (byte) (newlen - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (buf.length < (newlen + offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IllegalArgumentException("no space to pad buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         * TLS version of the padding works for both SSLv3 and TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        for (i = 0, offset += len; i < pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            buf [offset++] = (byte) (pad - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Apply the padding to the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Limit is advanced to the new buffer length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Position is equal to limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private static int addPadding(ByteBuffer bb, int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        int     len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int     offset = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int     newlen = len + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        byte    pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if ((newlen % blockSize) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            newlen += blockSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            newlen -= newlen % blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        pad = (byte) (newlen - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         * Update the limit to what will be padded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        bb.limit(newlen + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * TLS version of the padding works for both SSLv3 and TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        for (i = 0, offset += len; i < pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            bb.put(offset++, (byte) (pad - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        bb.position(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        bb.limit(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Typical TLS padding format for a 64 bit block cipher is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *   xx xx xx xx xx xx xx 00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *   xx xx xx xx xx xx 01 01
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *   xx 06 06 06 06 06 06 06
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *   07 07 07 07 07 07 07 07
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * TLS also allows any amount of padding from 1 and 256 bytes as long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * as it makes the data a multiple of the block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    private static int removePadding(byte[] buf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            int blockSize, ProtocolVersion protocolVersion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // last byte is length byte (i.e. actual padding length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        int padOffset = offset + len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int pad = buf[padOffset] & 0x0ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        int newlen = len - (pad + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (newlen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            throw new BadPaddingException("Padding length invalid: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            for (int i = 1; i <= pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                int val = buf[padOffset - i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (val != pad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    throw new BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                        ("Invalid TLS padding: " + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        } else { // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // SSLv3 requires 0 <= length byte < block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // some implementations do 1 <= length byte <= block size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // so accept that as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // v3 does not require any particular value for the other bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (pad > blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                throw new BadPaddingException("Invalid SSLv3 padding: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Position/limit is equal the removed padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    private static int removePadding(ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            int blockSize, ProtocolVersion protocolVersion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        int len = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        int offset = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // last byte is length byte (i.e. actual padding length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        int padOffset = offset + len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        int pad = bb.get(padOffset) & 0x0ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        int newlen = len - (pad + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (newlen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            throw new BadPaddingException("Padding length invalid: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         * We could zero the padding area, but not much useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * information there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            bb.put(padOffset, (byte)0);         // zero the padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            for (int i = 1; i <= pad; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                int val = bb.get(padOffset - i) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (val != pad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    throw new BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                        ("Invalid TLS padding: " + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        } else { // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            // SSLv3 requires 0 <= length byte < block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            // some implementations do 1 <= length byte <= block size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            // so accept that as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            // v3 does not require any particular value for the other bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (pad > blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                throw new BadPaddingException("Invalid SSLv3 padding: " + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         * Reset buffer limit to remove padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        bb.position(offset + newlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        bb.limit(offset + newlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return newlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
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
   489
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
   490
    /*
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
   491
     * 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
   492
     * 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
   493
     * 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
   494
     */
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
   495
    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
   496
        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
   497
            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
   498
                // 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
   499
                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
   500
            }
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
   501
        } 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
   502
            // 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
   503
        }
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
   504
    }
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
   505
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
}