src/java.base/share/classes/javax/crypto/SealedObject.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 52427 3c6aa484536c
child 57950 4612a3cfb927
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
package javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49783
diff changeset
    28
import jdk.internal.access.SharedSecrets;
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AlgorithmParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class enables a programmer to create an object and protect its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * confidentiality with a cryptographic algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> Given any Serializable object, one can create a SealedObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * that encapsulates the original object, in serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * format (i.e., a "deep copy"), and seals (encrypts) its serialized contents,
46053
6f7e93cb432a 8169080: Improve documentation examples for crypto applications
wetmore
parents: 36511
diff changeset
    45
 * using a cryptographic algorithm such as AES, to protect its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * confidentiality.  The encrypted content can later be decrypted (with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the corresponding algorithm using the correct decryption key) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * de-serialized, yielding the original object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p> Note that the Cipher object must be fully initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * correct algorithm, key, padding scheme, etc., before being applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * to a SealedObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p> The original object that was sealed can be recovered in two different
21955
abc02575919c 8029475: Fix more doclint issues in javax.security
darcy
parents: 10336
diff changeset
    55
 * ways:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <li>by using the {@link #getObject(javax.crypto.Cipher) getObject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * method that takes a <code>Cipher</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p> This method requires a fully initialized <code>Cipher</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * exact same algorithm, key, padding scheme, etc., that were used to seal the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p> This approach has the advantage that the party who unseals the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * sealed object does not require knowledge of the decryption key. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * after one party has initialized the cipher object with the required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * decryption key, it could hand over the cipher object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * another party who then unseals the sealed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <li>by using one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link #getObject(java.security.Key) getObject} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * that take a <code>Key</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p> In this approach, the <code>getObject</code> method creates a cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * object for the appropriate decryption algorithm and initializes it with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * given decryption key and the algorithm parameters (if any) that were stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * in the sealed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> This approach has the advantage that the party who
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * unseals the object does not need to keep track of the parameters (e.g., an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * IV) that were used to seal the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @see Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
public class SealedObject implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static final long serialVersionUID = 4482838265551344752L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The serialized object contents in encrypted format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private byte[] encryptedContent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * The algorithm that was used to seal this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private String sealAlg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The algorithm of the parameters used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private String paramsAlg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * The cryptographic parameters used by the sealing Cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * encoded in the default format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * That is, <code>cipher.getParameters().getEncoded()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected byte[] encodedParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Constructs a SealedObject from any Serializable object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p>The given object is serialized, and its serialized contents are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * encrypted using the given Cipher, which must be fully initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>Any algorithm parameters that may be used in the encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * operation are stored inside of the new <code>SealedObject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param object the object to be sealed; can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param c the cipher used to seal the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception NullPointerException if the given cipher is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @exception IOException if an error occurs during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception IllegalBlockSizeException if the given cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * cipher, no padding has been requested, and the total input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * (i.e., the length of the serialized object contents) is not a multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * of the cipher's block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public SealedObject(Serializable object, Cipher c) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        IllegalBlockSizeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * Serialize the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // creating a stream pipe-line, from a to b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        ByteArrayOutputStream b = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        ObjectOutput a = new ObjectOutputStream(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        byte[] content;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // write and flush the object content to byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            a.writeObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            a.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            content = b.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            a.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * Seal the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            this.encryptedContent = c.doFinal(content);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        catch (BadPaddingException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // if sealing is encryption only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // Should never happen??
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Save the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (c.getParameters() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            this.encodedParams = c.getParameters().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            this.paramsAlg = c.getParameters().getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // Save the encryption algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.sealAlg = c.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Constructs a SealedObject object from the passed-in SealedObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param so a SealedObject object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @exception NullPointerException if the given sealed object is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected SealedObject(SealedObject so) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   196
        this.encryptedContent = so.encryptedContent.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        this.sealAlg = so.sealAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this.paramsAlg = so.paramsAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (so.encodedParams != null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   200
            this.encodedParams = so.encodedParams.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            this.encodedParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Returns the algorithm that was used to seal this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the algorithm that was used to seal this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return this.sealAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Retrieves the original (encapsulated) object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p>This method creates a cipher for the algorithm that had been used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * the sealing operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * If the default provider package provides an implementation of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * algorithm, an instance of Cipher containing that implementation is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * If the algorithm is not available in the default package, other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * packages are searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * The Cipher object is initialized for decryption, using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <code>key</code> and the parameters (if any) that had been used in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * sealing operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <p>The encapsulated object is unsealed and de-serialized, before it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param key the key used to unseal the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return the original object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception IOException if an error occurs during de-serialiazation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @exception ClassNotFoundException if an error occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * de-serialiazation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @exception NoSuchAlgorithmException if the algorithm to unseal the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * object is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception InvalidKeyException if the given key cannot be used to unseal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * the object (e.g., it has the wrong algorithm).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @exception NullPointerException if <code>key</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public final Object getObject(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        throws IOException, ClassNotFoundException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new NullPointerException("key is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return unseal(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (NoSuchProviderException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // we've already caught NoSuchProviderException's and converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // them into NoSuchAlgorithmException's with details about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // the failing algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new NoSuchAlgorithmException("algorithm not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } catch (IllegalBlockSizeException ibse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new InvalidKeyException(ibse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (BadPaddingException bpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new InvalidKeyException(bpe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Retrieves the original (encapsulated) object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p>The encapsulated object is unsealed (using the given Cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * assuming that the Cipher is already properly initialized) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * de-serialized, before it is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param c the cipher used to unseal the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return the original object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception NullPointerException if the given cipher is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception IOException if an error occurs during de-serialiazation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @exception ClassNotFoundException if an error occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * de-serialiazation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @exception IllegalBlockSizeException if the given cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * cipher, no padding has been requested, and the total input length is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * not a multiple of the cipher's block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @exception BadPaddingException if the given cipher has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * initialized for decryption, and padding has been specified, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * the input data does not have proper expected padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public final Object getObject(Cipher c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        throws IOException, ClassNotFoundException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   292
        ObjectInput a = getExtObjectInputStream(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            Object obj = a.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            a.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Retrieves the original (encapsulated) object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <p>This method creates a cipher for the algorithm that had been used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * the sealing operation, using an implementation of that algorithm from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * the given <code>provider</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The Cipher object is initialized for decryption, using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <code>key</code> and the parameters (if any) that had been used in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * sealing operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>The encapsulated object is unsealed and de-serialized, before it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param key the key used to unseal the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param provider the name of the provider of the algorithm to unseal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return the original object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @exception IllegalArgumentException if the given provider is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @exception IOException if an error occurs during de-serialiazation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @exception ClassNotFoundException if an error occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * de-serialiazation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @exception NoSuchAlgorithmException if the algorithm to unseal the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * object is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @exception NoSuchProviderException if the given provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @exception InvalidKeyException if the given key cannot be used to unseal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * the object (e.g., it has the wrong algorithm).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @exception NullPointerException if <code>key</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public final Object getObject(Key key, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        throws IOException, ClassNotFoundException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            NoSuchProviderException, InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            throw new NullPointerException("key is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52427
diff changeset
   340
        if (provider == null || provider.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return unseal(key, provider);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   346
        } catch (IllegalBlockSizeException | BadPaddingException ex) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   347
            throw new InvalidKeyException(ex.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private Object unseal(Key key, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        throws IOException, ClassNotFoundException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            NoSuchProviderException, InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            IllegalBlockSizeException, BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * Create the parameter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        AlgorithmParameters params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (this.encodedParams != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                if (provider != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    params = AlgorithmParameters.getInstance(this.paramsAlg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                                             provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    params = AlgorithmParameters.getInstance(this.paramsAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            } catch (NoSuchProviderException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    throw new NoSuchAlgorithmException(this.paramsAlg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                                       + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    throw new NoSuchProviderException(nspe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            params.init(this.encodedParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * Create and initialize the cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        Cipher c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (provider != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                c = Cipher.getInstance(this.sealAlg, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                c = Cipher.getInstance(this.sealAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        } catch (NoSuchPaddingException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            throw new NoSuchAlgorithmException("Padding that was used in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                               + "sealing operation not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                               + "available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } catch (NoSuchProviderException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                throw new NoSuchAlgorithmException(this.sealAlg+" not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                throw new NoSuchProviderException(nspe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (params != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                c.init(Cipher.DECRYPT_MODE, key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                c.init(Cipher.DECRYPT_MODE, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // this should never happen, because we use the exact same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            // parameters that were used in the sealing operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new RuntimeException(iape.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   412
        ObjectInput a = getExtObjectInputStream(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            Object obj = a.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            a.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Restores the state of the SealedObject from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param s the object input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @exception NullPointerException if s is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        throws java.io.IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (encryptedContent != null)
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   431
            encryptedContent = encryptedContent.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (encodedParams != null)
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   433
            encodedParams = encodedParams.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   435
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   436
    // This method is also called inside SealedObjectForKeyProtector.java.
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   437
    private ObjectInputStream getExtObjectInputStream(Cipher c)
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   438
            throws BadPaddingException, IllegalBlockSizeException, IOException {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   439
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   440
        byte[] content = c.doFinal(this.encryptedContent);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   441
        ByteArrayInputStream b = new ByteArrayInputStream(content);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   442
        return new extObjectInputStream(b);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   443
    }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   444
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   445
    static {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   446
        SharedSecrets.setJavaxCryptoSealedObjectAccess((obj,c) -> obj.getExtObjectInputStream(c));
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   447
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
final class extObjectInputStream extends ObjectInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    extObjectInputStream(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        throws IOException, StreamCorruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   456
    protected Class<?> resolveClass(ObjectStreamClass v)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
             * Calling the super.resolveClass() first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
             * will let us pick up bug fixes in the super
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
             * class (e.g., 4171142).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return super.resolveClass(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
             * This is a workaround for bug 4224921.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (loader == null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   473
                loader = ClassLoader.getSystemClassLoader();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    throw new ClassNotFoundException(v.getName());
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
            return Class.forName(v.getName(), false, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
}