src/java.base/share/classes/java/security/KeyStore.java
author weijun
Thu, 13 Dec 2018 11:16:33 +0800
changeset 52996 2457d862a646
parent 52689 7084dae775f2
child 53018 8bf9268df0e2
permissions -rw-r--r--
8076190: Customizing the generation of a PKCS12 keystore Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48700
953eca1167b6 8194307: KeyStore#getInstance with custom LoadStoreParameter succeeds with invalid password
mullan
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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertificateException;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
    32
import java.security.spec.AlgorithmParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10114
diff changeset
    36
import javax.security.auth.DestroyFailedException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    39
import sun.security.util.Debug;
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class represents a storage facility for cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * keys and certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
    45
 * <p> A {@code KeyStore} manages different types of entries.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
    46
 * Each type of entry implements the {@code KeyStore.Entry} interface.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
    47
 * Three basic {@code KeyStore.Entry} implementations are provided:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li><b>KeyStore.PrivateKeyEntry</b>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
    51
 * <p> This type of entry holds a cryptographic {@code PrivateKey},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * which is optionally stored in a protected format to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * unauthorized access.  It is also accompanied by a certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * for the corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p> Private keys and certificate chains are used by a given entity for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * self-authentication. Applications for this authentication include software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * distribution organizations which sign JAR files as part of releasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * and/or licensing software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <li><b>KeyStore.SecretKeyEntry</b>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
    62
 * <p> This type of entry holds a cryptographic {@code SecretKey},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * which is optionally stored in a protected format to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * unauthorized access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <li><b>KeyStore.TrustedCertificateEntry</b>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
    67
 * <p> This type of entry contains a single public key {@code Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * belonging to another party. It is called a <i>trusted certificate</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * because the keystore owner trusts that the public key in the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * indeed belongs to the identity identified by the <i>subject</i> (owner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>This type of entry can be used to authenticate other parties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p> Each entry in a keystore is identified by an "alias" string. In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * case of private keys and their associated certificate chains, these strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * distinguish among the different ways in which the entity may authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * itself. For example, the entity may authenticate itself using different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * certificate authorities, or using different public key algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> Whether aliases are case sensitive is implementation dependent. In order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * to avoid problems, it is recommended not to use aliases in a KeyStore that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * only differ in case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p> Whether keystores are persistent, and the mechanisms used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * keystore if it is persistent, are not specified here. This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * use of a variety of techniques for protecting sensitive (e.g., private or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * secret) keys. Smart cards or other integrated cryptographic engines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * (SafeKeyper) are one option, and simpler mechanisms such as files may also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * be used (in a variety of formats).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p> Typical ways to request a KeyStore object include
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
    94
 * specifying an existing keystore file,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * relying on the default type and providing a specific keystore type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <ul>
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
    98
 * <li>To specify an existing keystore file:
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
    99
 * <pre>
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   100
 *    // get keystore password
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   101
 *    char[] password = getPassword();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   102
 *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   103
 *    // probe the keystore file and load the keystore entries
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   104
 *    KeyStore ks = KeyStore.getInstance(new File("keyStoreName"), password);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   105
 *</pre>
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   106
 * The system will probe the specified file to determine its keystore type
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   107
 * and return a keystore implementation with its entries already loaded.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   108
 * When this approach is used there is no need to call the keystore's
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   109
 * {@link #load(java.io.InputStream, char[]) load} method.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   110
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <li>To rely on the default type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * The system will return a keystore implementation for the default type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <li>To provide a specific keystore type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *      KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * The system will return the most preferred implementation of the
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28853
diff changeset
   122
 * specified keystore type available in the environment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <p> Before a keystore can be accessed, it must be
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   126
 * {@link #load(java.io.InputStream, char[]) loaded}
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   127
 * (unless it was already loaded during instantiation).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *    // get user password and file input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *    char[] password = getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
10114
d35f0b7bda65 7054969: Null-check-in-finally pattern in java/security documentation
mullan
parents: 9035
diff changeset
   134
 *    try (FileInputStream fis = new FileInputStream("keyStoreName")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *        ks.load(fis, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   139
 * To create an empty keystore using the above {@code load} method,
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   140
 * pass {@code null} as the {@code InputStream} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p> Once the keystore has been loaded, it is possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * to read existing entries from the keystore, or to write new entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * into the keystore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <pre>
6293
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   146
 *    KeyStore.ProtectionParameter protParam =
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   147
 *        new KeyStore.PasswordProtection(password);
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   148
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *    // get my private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *    KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
6293
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   151
 *        ks.getEntry("privateKeyAlias", protParam);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *    PrivateKey myPrivateKey = pkEntry.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *    // save my secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *    javax.crypto.SecretKey mySecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *    KeyStore.SecretKeyEntry skEntry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *        new KeyStore.SecretKeyEntry(mySecretKey);
6293
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   158
 *    ks.setEntry("secretKeyAlias", skEntry, protParam);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *    // store away the keystore
10114
d35f0b7bda65 7054969: Null-check-in-finally pattern in java/security documentation
mullan
parents: 9035
diff changeset
   161
 *    try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *        ks.store(fos, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * Note that although the same password may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * load the keystore, to protect the private key entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * to protect the secret key entry, and to store the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * (as is shown in the sample code above),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * different passwords or other protection parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * may also be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   173
 * <p> Every implementation of the Java platform is required to support
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   174
 * the following standard {@code KeyStore} type:
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   175
 * <ul>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   176
 * <li>{@code PKCS12}</li>
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   177
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   178
 * This type is described in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   179
 * "{@docRoot}/../specs/security/standard-names.html#keystore-types">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   180
 * KeyStore section</a> of the
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   181
 * Java Security Standard Algorithm Names Specification.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   182
 * Consult the release documentation for your implementation to see if any
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   183
 * other types are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   184
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * @see java.security.PrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * @see javax.crypto.SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * @see java.security.cert.Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
public class KeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
   196
    private static final Debug kdebug = Debug.getInstance("keystore");
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   197
    private static final Debug pdebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   198
                        Debug.getInstance("provider", "Provider");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   199
    private static final boolean skipDebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   200
        Debug.isOn("engine=") && !Debug.isOn("keystore");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   201
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Constant to lookup in the Security properties file to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the default keystore type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * In the Security properties file, the default keystore type is given as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * keystore.type=jks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static final String KEYSTORE_TYPE = "keystore.type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    // The keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // The provider implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private KeyStoreSpi keyStoreSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // Has this keystore been initialized (loaded)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   225
     * A marker interface for {@code KeyStore}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * {@link #load(KeyStore.LoadStoreParameter) load}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * {@link #store(KeyStore.LoadStoreParameter) store}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public static interface LoadStoreParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         * Gets the parameter used to protect keystore data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * @return the parameter used to protect keystore data, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public ProtectionParameter getProtectionParameter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * A marker interface for keystore protection parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   245
     * <p> The information stored in a {@code ProtectionParameter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * object protects the contents of a keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * For example, protection parameters may be used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * the integrity of keystore data, or to protect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * confidentiality of sensitive keystore data
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   250
     * (such as a {@code PrivateKey}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public static interface ProtectionParameter { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   257
     * A password-based implementation of {@code ProtectionParameter}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public static class PasswordProtection implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                ProtectionParameter, javax.security.auth.Destroyable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        private final char[] password;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   265
        private final String protectionAlgorithm;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   266
        private final AlgorithmParameterSpec protectionParameters;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        private volatile boolean destroyed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * Creates a password parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   272
         * <p> The specified {@code password} is cloned before it is stored
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   273
         * in the new {@code PasswordProtection} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   275
         * @param password the password, which may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        public PasswordProtection(char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            this.password = (password == null) ? null : password.clone();
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   279
            this.protectionAlgorithm = null;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   280
            this.protectionParameters = null;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   281
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   282
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   283
        /**
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   284
         * Creates a password parameter and specifies the protection algorithm
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   285
         * and associated parameters to use when encrypting a keystore entry.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   286
         * <p>
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   287
         * The specified {@code password} is cloned before it is stored in the
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   288
         * new {@code PasswordProtection} object.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   289
         *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   290
         * @param password the password, which may be {@code null}
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   291
         * @param protectionAlgorithm the encryption algorithm name, for
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   292
         *     example, {@code PBEWithHmacSHA256AndAES_256}.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   293
         *     See the Cipher section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   294
         * "{@docRoot}/../specs/security/standard-names.html#cipher-algorithm-names">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   295
         * Java Security Standard Algorithm Names Specification</a>
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   296
         *     for information about standard encryption algorithm names.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   297
         * @param protectionParameters the encryption algorithm parameter
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   298
         *     specification, which may be {@code null}
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   299
         * @exception NullPointerException if {@code protectionAlgorithm} is
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   300
         *     {@code null}
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   301
         *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   302
         * @since 1.8
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   303
         */
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   304
        public PasswordProtection(char[] password, String protectionAlgorithm,
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   305
            AlgorithmParameterSpec protectionParameters) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   306
            if (protectionAlgorithm == null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   307
                throw new NullPointerException("invalid null input");
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   308
            }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   309
            this.password = (password == null) ? null : password.clone();
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   310
            this.protectionAlgorithm = protectionAlgorithm;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   311
            this.protectionParameters = protectionParameters;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   312
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   313
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   314
        /**
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   315
         * Gets the name of the protection algorithm.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   316
         * If none was set then the keystore provider will use its default
52996
2457d862a646 8076190: Customizing the generation of a PKCS12 keystore
weijun
parents: 52689
diff changeset
   317
         * protection algorithm.
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   318
         *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   319
         * @return the algorithm name, or {@code null} if none was set
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   320
         *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   321
         * @since 1.8
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   322
         */
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   323
        public String getProtectionAlgorithm() {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   324
            return protectionAlgorithm;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   325
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   326
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   327
        /**
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   328
         * Gets the parameters supplied for the protection algorithm.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   329
         *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   330
         * @return the algorithm parameter specification, or {@code  null},
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   331
         *     if none was set
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   332
         *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   333
         * @since 1.8
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   334
         */
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   335
        public AlgorithmParameterSpec getProtectionParameters() {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14775
diff changeset
   336
            return protectionParameters;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * Gets the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * <p>Note that this method returns a reference to the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * If a clone of the array is created it is the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * responsibility to zero out the password information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * after it is no longer needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         * @see #destroy()
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   348
         * @return the password, which may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         * @exception IllegalStateException if the password has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         *              been cleared (destroyed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        public synchronized char[] getPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (destroyed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                throw new IllegalStateException("password has been cleared");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * Clears the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * @exception DestroyFailedException if this method was unable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         *      to clear the password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10114
diff changeset
   365
        public synchronized void destroy() throws DestroyFailedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            destroyed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                Arrays.fill(password, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * Determines if password has been cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * @return true if the password has been cleared, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        public synchronized boolean isDestroyed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return destroyed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * A ProtectionParameter encapsulating a CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public static class CallbackHandlerProtection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            implements ProtectionParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        private final CallbackHandler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * Constructs a new CallbackHandlerProtection from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         * CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * @param handler the CallbackHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * @exception NullPointerException if handler is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        public CallbackHandlerProtection(CallbackHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                throw new NullPointerException("handler must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            this.handler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * Returns the CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * @return the CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        public CallbackHandler getCallbackHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   418
     * A marker interface for {@code KeyStore} entry types.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   422
    public static interface Entry {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   423
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   424
        /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   425
         * Retrieves the attributes associated with an entry.
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 26736
diff changeset
   426
         *
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 26736
diff changeset
   427
         * @implSpec
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   428
         * The default implementation returns an empty {@code Set}.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   429
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   430
         * @return an unmodifiable {@code Set} of attributes, possibly empty
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   431
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   432
         * @since 1.8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   433
         */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   434
        public default Set<Attribute> getAttributes() {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   435
            return Collections.<Attribute>emptySet();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   436
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   437
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   438
        /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   439
         * An attribute associated with a keystore entry.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   440
         * It comprises a name and one or more values.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   441
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   442
         * @since 1.8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   443
         */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   444
        public interface Attribute {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   445
            /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   446
             * Returns the attribute's name.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   447
             *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   448
             * @return the attribute name
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   449
             */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   450
            public String getName();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   451
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   452
            /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   453
             * Returns the attribute's value.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   454
             * Multi-valued attributes encode their values as a single string.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   455
             *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   456
             * @return the attribute value
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   457
             */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   458
            public String getValue();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   459
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   460
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   463
     * A {@code KeyStore} entry that holds a {@code PrivateKey}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * and corresponding certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public static final class PrivateKeyEntry implements Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        private final PrivateKey privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        private final Certificate[] chain;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   472
        private final Set<Attribute> attributes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   475
         * Constructs a {@code PrivateKeyEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   476
         * {@code PrivateKey} and corresponding certificate chain.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   478
         * <p> The specified {@code chain} is cloned before it is stored
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   479
         * in the new {@code PrivateKeyEntry} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   481
         * @param privateKey the {@code PrivateKey}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   482
         * @param chain an array of {@code Certificate}s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         *      representing the certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         *      The chain must be ordered and contain a
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   485
         *      {@code Certificate} at index 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         *      corresponding to the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * @exception NullPointerException if
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   489
         *      {@code privateKey} or {@code chain}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   490
         *      is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * @exception IllegalArgumentException if the specified chain has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         *      length of 0, if the specified chain does not contain
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   493
         *      {@code Certificate}s of the same type,
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   494
         *      or if the {@code PrivateKey} algorithm
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   495
         *      does not match the algorithm of the {@code PublicKey}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   496
         *      in the end entity {@code Certificate} (at index 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        public PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   499
            this(privateKey, chain, Collections.<Attribute>emptySet());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   500
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   501
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   502
        /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   503
         * Constructs a {@code PrivateKeyEntry} with a {@code PrivateKey} and
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   504
         * corresponding certificate chain and associated entry attributes.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   505
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   506
         * <p> The specified {@code chain} and {@code attributes} are cloned
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   507
         * before they are stored in the new {@code PrivateKeyEntry} object.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   508
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   509
         * @param privateKey the {@code PrivateKey}
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   510
         * @param chain an array of {@code Certificate}s
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   511
         *      representing the certificate chain.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   512
         *      The chain must be ordered and contain a
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   513
         *      {@code Certificate} at index 0
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   514
         *      corresponding to the private key.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   515
         * @param attributes the attributes
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   516
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   517
         * @exception NullPointerException if {@code privateKey}, {@code chain}
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   518
         *      or {@code attributes} is {@code null}
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   519
         * @exception IllegalArgumentException if the specified chain has a
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   520
         *      length of 0, if the specified chain does not contain
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   521
         *      {@code Certificate}s of the same type,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   522
         *      or if the {@code PrivateKey} algorithm
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   523
         *      does not match the algorithm of the {@code PublicKey}
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   524
         *      in the end entity {@code Certificate} (at index 0)
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   525
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   526
         * @since 1.8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   527
         */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   528
        public PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   529
           Set<Attribute> attributes) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   530
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   531
            if (privateKey == null || chain == null || attributes == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (chain.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                ("invalid zero-length input chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            Certificate[] clonedChain = chain.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            String certType = clonedChain[0].getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            for (int i = 1; i < clonedChain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (!certType.equals(clonedChain[i].getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                ("chain does not contain certificates " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                "of the same type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (!privateKey.getAlgorithm().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        (clonedChain[0].getPublicKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                ("private key algorithm does not match " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                "algorithm of public key in end entity " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                "certificate (at index 0)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            this.privKey = privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (clonedChain[0] instanceof X509Certificate &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                !(clonedChain instanceof X509Certificate[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                this.chain = new X509Certificate[clonedChain.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                System.arraycopy(clonedChain, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                this.chain, 0, clonedChain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                this.chain = clonedChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   566
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   567
            this.attributes =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   568
                Collections.unmodifiableSet(new HashSet<>(attributes));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   572
         * Gets the {@code PrivateKey} from this entry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   574
         * @return the {@code PrivateKey} from this entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        public PrivateKey getPrivateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            return privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   581
         * Gets the {@code Certificate} chain from this entry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         * <p> The stored chain is cloned before being returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   585
         * @return an array of {@code Certificate}s corresponding
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         *      to the certificate chain for the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         *      If the certificates are of type X.509,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
         *      the runtime type of the returned array is
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   589
         *      {@code X509Certificate[]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        public Certificate[] getCertificateChain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            return chain.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   596
         * Gets the end entity {@code Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * from the certificate chain in this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   599
         * @return the end entity {@code Certificate} (at index 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         *      from the certificate chain in this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         *      If the certificate is of type X.509,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         *      the runtime type of the returned certificate is
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   603
         *      {@code X509Certificate}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        public Certificate getCertificate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            return chain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        /**
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   610
         * Retrieves the attributes associated with an entry.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   611
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   612
         * @return an unmodifiable {@code Set} of attributes, possibly empty
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   613
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   614
         * @since 1.8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   615
         */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   616
        @Override
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   617
        public Set<Attribute> getAttributes() {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   618
            return attributes;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   619
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   620
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   621
        /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
         * Returns a string representation of this PrivateKeyEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
         * @return a string representation of this PrivateKeyEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            sb.append("Private key entry and certificate chain with "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                + chain.length + " elements:\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            for (Certificate cert : chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                sb.append(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                sb.append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   639
     * A {@code KeyStore} entry that holds a {@code SecretKey}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public static final class SecretKeyEntry implements Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        private final SecretKey sKey;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   646
        private final Set<Attribute> attributes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   649
         * Constructs a {@code SecretKeyEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   650
         * {@code SecretKey}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   652
         * @param secretKey the {@code SecretKey}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   654
         * @exception NullPointerException if {@code secretKey}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   655
         *      is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        public SecretKeyEntry(SecretKey secretKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            if (secretKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            this.sKey = secretKey;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   662
            this.attributes = Collections.<Attribute>emptySet();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   663
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   664
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   665
        /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   666
         * Constructs a {@code SecretKeyEntry} with a {@code SecretKey} and
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   667
         * associated entry attributes.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   668
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   669
         * <p> The specified {@code attributes} is cloned before it is stored
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   670
         * in the new {@code SecretKeyEntry} object.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   671
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   672
         * @param secretKey the {@code SecretKey}
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   673
         * @param attributes the attributes
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   674
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   675
         * @exception NullPointerException if {@code secretKey} or
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   676
         *     {@code attributes} is {@code null}
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   677
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   678
         * @since 1.8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   679
         */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   680
        public SecretKeyEntry(SecretKey secretKey, Set<Attribute> attributes) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   681
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   682
            if (secretKey == null || attributes == null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   683
                throw new NullPointerException("invalid null input");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   684
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   685
            this.sKey = secretKey;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   686
            this.attributes =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   687
                Collections.unmodifiableSet(new HashSet<>(attributes));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   691
         * Gets the {@code SecretKey} from this entry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   693
         * @return the {@code SecretKey} from this entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        public SecretKey getSecretKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            return sKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        /**
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   700
         * Retrieves the attributes associated with an entry.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   701
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   702
         * @return an unmodifiable {@code Set} of attributes, possibly empty
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   703
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   704
         * @since 1.8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   705
         */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   706
        @Override
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   707
        public Set<Attribute> getAttributes() {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   708
            return attributes;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   709
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   710
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   711
        /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         * Returns a string representation of this SecretKeyEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         * @return a string representation of this SecretKeyEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            return "Secret key entry with algorithm " + sKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   721
     * A {@code KeyStore} entry that holds a trusted
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   722
     * {@code Certificate}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public static final class TrustedCertificateEntry implements Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        private final Certificate cert;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   729
        private final Set<Attribute> attributes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   732
         * Constructs a {@code TrustedCertificateEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   733
         * trusted {@code Certificate}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   735
         * @param trustedCert the trusted {@code Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
         * @exception NullPointerException if
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   738
         *      {@code trustedCert} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        public TrustedCertificateEntry(Certificate trustedCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            if (trustedCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            this.cert = trustedCert;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   745
            this.attributes = Collections.<Attribute>emptySet();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   746
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   747
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   748
        /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   749
         * Constructs a {@code TrustedCertificateEntry} with a
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   750
         * trusted {@code Certificate} and associated entry attributes.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   751
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   752
         * <p> The specified {@code attributes} is cloned before it is stored
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   753
         * in the new {@code TrustedCertificateEntry} object.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   754
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   755
         * @param trustedCert the trusted {@code Certificate}
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   756
         * @param attributes the attributes
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   757
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   758
         * @exception NullPointerException if {@code trustedCert} or
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   759
         *     {@code attributes} is {@code null}
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   760
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   761
         * @since 1.8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   762
         */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   763
        public TrustedCertificateEntry(Certificate trustedCert,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   764
           Set<Attribute> attributes) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   765
            if (trustedCert == null || attributes == null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   766
                throw new NullPointerException("invalid null input");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   767
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   768
            this.cert = trustedCert;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   769
            this.attributes =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   770
                Collections.unmodifiableSet(new HashSet<>(attributes));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   774
         * Gets the trusted {@code Certficate} from this entry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
   776
         * @return the trusted {@code Certificate} from this entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        public Certificate getTrustedCertificate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        /**
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   783
         * Retrieves the attributes associated with an entry.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   784
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   785
         * @return an unmodifiable {@code Set} of attributes, possibly empty
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   786
         *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   787
         * @since 1.8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   788
         */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   789
        @Override
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   790
        public Set<Attribute> getAttributes() {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   791
            return attributes;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   792
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   793
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   794
        /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * Returns a string representation of this TrustedCertificateEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * @return a string representation of this TrustedCertificateEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            return "Trusted certificate entry:\r\n" + cert.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Creates a KeyStore object of the given type, and encapsulates the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * provider implementation (SPI object) in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @param keyStoreSpi the provider implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @param type the keystore type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        this.keyStoreSpi = keyStoreSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        this.type = type;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   816
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   817
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   818
            pdebug.println("KeyStore." + type.toUpperCase() + " type from: " +
42780
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   819
                getProviderName());
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   820
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
42780
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   823
    private String getProviderName() {
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   824
        return (provider == null) ? "(no provider)" : provider.getName();
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   825
    }
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   826
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * Returns a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * A new KeyStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * KeyStoreSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Provider that supports the specified type is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   839
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   840
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 35302
diff changeset
   841
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 35302
diff changeset
   842
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   843
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   844
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   845
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   846
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @param type the type of keystore.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   848
     * See the KeyStore section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   849
     * "{@docRoot}/../specs/security/standard-names.html#keystore-types">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   850
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * for information about standard keystore types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   853
     * @return a keystore object of the specified type
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   855
     * @throws KeyStoreException if no {@code Provider} supports a
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   856
     *         {@code KeyStoreSpi} implementation for the
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   857
     *         specified type
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   858
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   859
     * @throws NullPointerException if {@code type} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    public static KeyStore getInstance(String type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   866
        Objects.requireNonNull(type, "null type name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            Object[] objs = Security.getImpl(type, "KeyStore", (String)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            throw new KeyStoreException(type + " not found", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        } catch (NoSuchProviderException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            throw new KeyStoreException(type + " not found", nspe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Returns a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * <p> A new KeyStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * KeyStoreSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param type the type of keystore.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   889
     * See the KeyStore section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   890
     * "{@docRoot}/../specs/security/standard-names.html#keystore-types">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   891
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * for information about standard keystore types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   896
     * @return a keystore object of the specified type
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   898
     * @throws IllegalArgumentException if the provider name is {@code null}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   899
     *         or empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   901
     * @throws KeyStoreException if a {@code KeyStoreSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   902
     *         implementation for the specified type is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   903
     *         available from the specified provider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   905
     * @throws NoSuchProviderException if the specified provider is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   906
     *         registered in the security provider list
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   907
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   908
     * @throws NullPointerException if {@code type} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    public static KeyStore getInstance(String type, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        throws KeyStoreException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   915
        Objects.requireNonNull(type, "null type name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (provider == null || provider.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            Object[] objs = Security.getImpl(type, "KeyStore", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            throw new KeyStoreException(type + " not found", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Returns a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <p> A new KeyStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * KeyStoreSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @param type the type of keystore.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   935
     * See the KeyStore section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   936
     * "{@docRoot}/../specs/security/standard-names.html#keystore-types">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   937
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * for information about standard keystore types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   942
     * @return a keystore object of the specified type
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   943
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   944
     * @throws IllegalArgumentException if the specified provider is
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   945
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   947
     * @throws KeyStoreException if {@code KeyStoreSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   948
     *         implementation for the specified type is not available
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   949
     *         from the specified {@code Provider} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   951
     * @throws NullPointerException if {@code type} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    public static KeyStore getInstance(String type, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   960
        Objects.requireNonNull(type, "null type name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        if (provider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            Object[] objs = Security.getImpl(type, "KeyStore", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            throw new KeyStoreException(type + " not found", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    /**
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 14342
diff changeset
   972
     * Returns the default keystore type as specified by the
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 14342
diff changeset
   973
     * {@code keystore.type} security property, or the string
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 14342
diff changeset
   974
     * {@literal "jks"} (acronym for {@literal "Java keystore"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * if no such property exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * <p>The default keystore type can be used by applications that do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * want to use a hard-coded keystore type when calling one of the
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 14342
diff changeset
   979
     * {@code getInstance} methods, and want to provide a default keystore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * type in case a user does not specify its own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <p>The default keystore type can be changed by setting the value of the
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 14342
diff changeset
   983
     * {@code keystore.type} security property to the desired keystore type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 14342
diff changeset
   985
     * @return the default keystore type as specified by the
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 14342
diff changeset
   986
     * {@code keystore.type} security property, or the string {@literal "jks"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * if no such property exists.
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 14342
diff changeset
   988
     * @see java.security.Security security properties
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32413
diff changeset
   990
    public static final String getDefaultType() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        String kstype;
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 29492
diff changeset
   992
        kstype = AccessController.doPrivileged(new PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                return Security.getProperty(KEYSTORE_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        if (kstype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            kstype = "jks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        return kstype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * Returns the provider of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @return the provider of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public final Provider getProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Returns the type of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @return the type of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    public final String getType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        return this.type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * Returns the key associated with the given alias, using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * password to recover it.  The key must have been associated with
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1026
     * the alias by a call to {@code setKeyEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1027
     * or by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1028
     * {@code PrivateKeyEntry} or {@code SecretKeyEntry}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @param password the password for recovering the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @return the requested key, or null if the given alias does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * or does not identify a key-related entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * key cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @exception UnrecoverableKeyException if the key cannot be recovered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * (e.g., the given password is wrong).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    public final Key getKey(String alias, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        throws KeyStoreException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return keyStoreSpi.engineGetKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * The certificate chain must have been associated with the alias
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1056
     * by a call to {@code setKeyEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1057
     * or by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1058
     * {@code PrivateKeyEntry}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @return the certificate chain (ordered with the user's certificate first
92
b0bdf21d1a53 6597349: KeyStore.getCertificateChain() may not return the full chain
weijun
parents: 2
diff changeset
  1063
     * followed by zero or more certificate authorities), or null if the given alias
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * does not exist or does not contain a certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    public final Certificate[] getCertificateChain(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        return keyStoreSpi.engineGetCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * Returns the certificate associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * <p> If the given alias name identifies an entry
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1082
     * created by a call to {@code setCertificateEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1083
     * or created by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1084
     * {@code TrustedCertificateEntry},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * then the trusted certificate contained in that entry is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * <p> If the given alias name identifies an entry
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1088
     * created by a call to {@code setKeyEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1089
     * or created by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1090
     * {@code PrivateKeyEntry},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * then the first element of the certificate chain in that entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @return the certificate, or null if the given alias does not exist or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * does not contain a certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    public final Certificate getCertificate(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        return keyStoreSpi.engineGetCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * Returns the creation date of the entry identified by the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @return the creation date of this entry, or null if the given alias does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    public final Date getCreationDate(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        return keyStoreSpi.engineGetCreationDate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Assigns the given key to the given alias, protecting it with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1135
     * <p>If the given key is of type {@code java.security.PrivateKey},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * it must be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @param key the key to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * @param password the password to protect the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * key (only required if the given key is of type
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1148
     * {@code java.security.PrivateKey}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * (loaded), the given key cannot be protected, or this operation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * for some other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    public final void setKeyEntry(String alias, Key key, char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if ((key instanceof PrivateKey) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            (chain == null || chain.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            throw new IllegalArgumentException("Private key must be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                                               + "accompanied by certificate "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                                               + "chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        keyStoreSpi.engineSetKeyEntry(alias, key, password, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * Assigns the given key (that has already been protected) to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * <p>If the protected key is of type
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1175
     * {@code java.security.PrivateKey}, it must be accompanied by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * certificate chain certifying the corresponding public key. If the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1177
     * underlying keystore implementation is of type {@code jks},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1178
     * {@code key} must be encoded as an
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1179
     * {@code EncryptedPrivateKeyInfo} as defined in the PKCS #8 standard.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @param key the key (in protected format) to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     *          key (only useful if the protected key is of type
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1189
     *          {@code java.security.PrivateKey}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * (loaded), or if this operation fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    public final void setKeyEntry(String alias, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        keyStoreSpi.engineSetKeyEntry(alias, key, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * Assigns the given trusted certificate to the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * <p> If the given alias identifies an existing entry
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1208
     * created by a call to {@code setCertificateEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1209
     * or created by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1210
     * {@code TrustedCertificateEntry},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * the trusted certificate in the existing entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * is overridden by the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * @param cert the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * @exception KeyStoreException if the keystore has not been initialized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * or the given alias already exists and does not identify an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * entry containing a trusted certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * or this operation fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    public final void setCertificateEntry(String alias, Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        keyStoreSpi.engineSetCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * Deletes the entry identified by the given alias from this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * @exception KeyStoreException if the keystore has not been initialized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * or if the entry cannot be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    public final void deleteEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        keyStoreSpi.engineDeleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * Lists all the alias names of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * @return enumeration of the alias names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    public final Enumeration<String> aliases()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        return keyStoreSpi.engineAliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * Checks if the given alias exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @return true if the alias exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    public final boolean containsAlias(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        return keyStoreSpi.engineContainsAlias(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * Retrieves the number of entries in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * @return the number of entries in this keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    public final int size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        return keyStoreSpi.engineSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * Returns true if the entry identified by the given alias
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1303
     * was created by a call to {@code setKeyEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1304
     * or created by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1305
     * {@code PrivateKeyEntry} or a {@code SecretKeyEntry}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * @param alias the alias for the keystore entry to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * key-related entry, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    public final boolean isKeyEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        return keyStoreSpi.engineIsKeyEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * Returns true if the entry identified by the given alias
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1326
     * was created by a call to {@code setCertificateEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1327
     * or created by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1328
     * {@code TrustedCertificateEntry}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * @param alias the alias for the keystore entry to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * @return true if the entry identified by the given alias contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * trusted certificate, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    public final boolean isCertificateEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        return keyStoreSpi.engineIsCertificateEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * Returns the (alias) name of the first keystore entry whose certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * matches the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * <p> This method attempts to match the given certificate with each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * keystore entry. If the entry being considered was
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1353
     * created by a call to {@code setCertificateEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1354
     * or created by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1355
     * {@code TrustedCertificateEntry},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * then the given certificate is compared to that entry's certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * <p> If the entry being considered was
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1359
     * created by a call to {@code setKeyEntry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1360
     * or created by a call to {@code setEntry} with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1361
     * {@code PrivateKeyEntry},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * then the given certificate is compared to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * element of that entry's certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * @param cert the certificate to match with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * @return the alias name of the first entry with a matching certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * or null if no such entry exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    public final String getCertificateAlias(Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        return keyStoreSpi.engineGetCertificateAlias(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * Stores this keystore to the given output stream, and protects its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * integrity with the given password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * @param stream the output stream to which this keystore is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * @param password the password to generate the keystore integrity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * @exception IOException if there was an I/O problem with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * @exception NoSuchAlgorithmException if the appropriate data integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * algorithm could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * @exception CertificateException if any of the certificates included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * the keystore data could not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    public final void store(OutputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        throws KeyStoreException, IOException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        keyStoreSpi.engineStore(stream, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1408
     * Stores this keystore using the given {@code LoadStoreParameter}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1410
     * @param param the {@code LoadStoreParameter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     *          that specifies how to store the keystore,
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1412
     *          which may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * @exception IllegalArgumentException if the given
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1415
     *          {@code LoadStoreParameter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     *          input is not recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *          (loaded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * @exception IOException if there was an I/O problem with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * @exception NoSuchAlgorithmException if the appropriate data integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     *          algorithm could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * @exception CertificateException if any of the certificates included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *          the keystore data could not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    public final void store(LoadStoreParameter param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                throws KeyStoreException, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                NoSuchAlgorithmException, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        keyStoreSpi.engineStore(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * Loads this KeyStore from the given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * <p>A password may be given to unlock the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * (e.g. the keystore resides on a hardware token device),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * or to check the integrity of the keystore data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * If a password is not given for integrity checking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * then integrity checking is not performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * <p>In order to create an empty keystore, or if the keystore cannot
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1446
     * be initialized from a stream, pass {@code null}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1447
     * as the {@code stream} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * <p> Note that if this keystore has already been loaded, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * reinitialized and loaded again from the given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * @param stream the input stream from which the keystore is loaded,
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1453
     * or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * @param password the password used to check the integrity of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * the keystore, the password used to unlock the keystore,
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1456
     * or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * keystore data, if a password is required but not given,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * or if the given password was incorrect. If the error is due to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * wrong password, the {@link Throwable#getCause cause} of the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1462
     * {@code IOException} should be an
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1463
     * {@code UnrecoverableKeyException}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    public final void load(InputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        keyStoreSpi.engineLoad(stream, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1477
     * Loads this keystore using the given {@code LoadStoreParameter}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * <p> Note that if this KeyStore has already been loaded, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * reinitialized and loaded again from the given parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1482
     * @param param the {@code LoadStoreParameter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     *          that specifies how to load the keystore,
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1484
     *          which may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * @exception IllegalArgumentException if the given
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1487
     *          {@code LoadStoreParameter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *          input is not recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     *          keystore data. If the error is due to an incorrect
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1491
     *         {@code ProtectionParameter} (e.g. wrong password)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *         the {@link Throwable#getCause cause} of the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1493
     *         {@code IOException} should be an
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1494
     *         {@code UnrecoverableKeyException}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     *          the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *          keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    public final void load(LoadStoreParameter param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                throws IOException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        keyStoreSpi.engineLoad(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1511
     * Gets a keystore {@code Entry} for the specified alias
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * with the specified protection parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1514
     * @param alias get the keystore {@code Entry} for this alias
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1515
     * @param protParam the {@code ProtectionParameter}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1516
     *          used to protect the {@code Entry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1517
     *          which may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1519
     * @return the keystore {@code Entry} for the specified alias,
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1520
     *          or {@code null} if there is no such entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * @exception NullPointerException if
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1523
     *          {@code alias} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     *          entry cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * @exception UnrecoverableEntryException if the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1527
     *          {@code protParam} were insufficient or invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * @exception UnrecoverableKeyException if the entry is a
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1529
     *          {@code PrivateKeyEntry} or {@code SecretKeyEntry}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1530
     *          and the specified {@code protParam} does not contain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     *          the information needed to recover the key (e.g. wrong password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     *          (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * @see #setEntry(String, KeyStore.Entry, KeyStore.ProtectionParameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    public final Entry getEntry(String alias, ProtectionParameter protParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                throws NoSuchAlgorithmException, UnrecoverableEntryException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        return keyStoreSpi.engineGetEntry(alias, protParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1552
     * Saves a keystore {@code Entry} under the specified alias.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * The protection parameter is used to protect the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1554
     * {@code Entry}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * <p> If an entry already exists for the specified alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * it is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1559
     * @param alias save the keystore {@code Entry} under this alias
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1560
     * @param entry the {@code Entry} to save
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1561
     * @param protParam the {@code ProtectionParameter}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1562
     *          used to protect the {@code Entry},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1563
     *          which may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @exception NullPointerException if
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1566
     *          {@code alias} or {@code entry}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1567
     *          is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     *          (loaded), or if this operation fails for some other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * @see #getEntry(String, KeyStore.ProtectionParameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    public final void setEntry(String alias, Entry entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                        ProtectionParameter protParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        if (alias == null || entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        keyStoreSpi.engineSetEntry(alias, entry, protParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1588
     * Determines if the keystore {@code Entry} for the specified
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1589
     * {@code alias} is an instance or subclass of the specified
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1590
     * {@code entryClass}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * @param entryClass the entry class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1595
     * @return true if the keystore {@code Entry} for the specified
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1596
     *          {@code alias} is an instance or subclass of the
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1597
     *          specified {@code entryClass}, false otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * @exception NullPointerException if
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1600
     *          {@code alias} or {@code entryClass}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1601
     *          is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * @exception KeyStoreException if the keystore has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     *          initialized (loaded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    public final boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        entryInstanceOf(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                        Class<? extends KeyStore.Entry> entryClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        if (alias == null || entryClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        return keyStoreSpi.engineEntryInstanceOf(alias, entryClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    /**
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1623
     * Returns a loaded keystore object of the appropriate keystore type.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1624
     * First the keystore type is determined by probing the specified file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1625
     * Then a keystore object is instantiated and loaded using the data from
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1626
     * that file.
32413
27f4ac5e88fb 8130800: KeyStore.getInstance(File,char[]) does not throw IOE for null password
vinnie
parents: 30033
diff changeset
  1627
     *
27f4ac5e88fb 8130800: KeyStore.getInstance(File,char[]) does not throw IOE for null password
vinnie
parents: 30033
diff changeset
  1628
     * <p>
27f4ac5e88fb 8130800: KeyStore.getInstance(File,char[]) does not throw IOE for null password
vinnie
parents: 30033
diff changeset
  1629
     * A password may be given to unlock the keystore
27f4ac5e88fb 8130800: KeyStore.getInstance(File,char[]) does not throw IOE for null password
vinnie
parents: 30033
diff changeset
  1630
     * (e.g. the keystore resides on a hardware token device),
27f4ac5e88fb 8130800: KeyStore.getInstance(File,char[]) does not throw IOE for null password
vinnie
parents: 30033
diff changeset
  1631
     * or to check the integrity of the keystore data.
27f4ac5e88fb 8130800: KeyStore.getInstance(File,char[]) does not throw IOE for null password
vinnie
parents: 30033
diff changeset
  1632
     * If a password is not given for integrity checking,
27f4ac5e88fb 8130800: KeyStore.getInstance(File,char[]) does not throw IOE for null password
vinnie
parents: 30033
diff changeset
  1633
     * then integrity checking is not performed.
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1634
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1635
     * <p>
28853
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1636
     * This method traverses the list of registered security
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1637
     * {@linkplain Provider providers}, starting with the most
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1638
     * preferred Provider.
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1639
     * For each {@link KeyStoreSpi} implementation supported by a
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1640
     * Provider, it invokes the {@link
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1641
     * KeyStoreSpi#engineProbe(InputStream) engineProbe} method to
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1642
     * determine if it supports the specified keystore.
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1643
     * A new KeyStore object is returned that encapsulates the KeyStoreSpi
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1644
     * implementation from the first Provider that supports the specified file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1645
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1646
     * <p> Note that the list of registered providers may be retrieved via
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1647
     * the {@link Security#getProviders() Security.getProviders()} method.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1648
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1649
     * @param  file the keystore file
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1650
     * @param  password the keystore password, which may be {@code null}
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1651
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1652
     * @return a keystore object loaded with keystore data
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1653
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1654
     * @throws KeyStoreException if no Provider supports a KeyStoreSpi
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1655
     *             implementation for the specified keystore file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1656
     * @throws IOException if there is an I/O or format problem with the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1657
     *             keystore data, if a password is required but not given,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1658
     *             or if the given password was incorrect. If the error is
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1659
     *             due to a wrong password, the {@link Throwable#getCause cause}
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1660
     *             of the {@code IOException} should be an
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1661
     *             {@code UnrecoverableKeyException}.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1662
     * @throws NoSuchAlgorithmException if the algorithm used to check the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1663
     *             integrity of the keystore cannot be found.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1664
     * @throws CertificateException if any of the certificates in the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1665
     *             keystore could not be loaded.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1666
     * @throws IllegalArgumentException if file does not exist or does not
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1667
     *             refer to a normal file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1668
     * @throws NullPointerException if file is {@code null}.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1669
     * @throws SecurityException if a security manager exists and its
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1670
     *             {@link java.lang.SecurityManager#checkRead} method denies
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1671
     *             read access to the specified file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1672
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1673
     * @see Provider
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1674
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33241
diff changeset
  1675
     * @since 9
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1676
     */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1677
    public static final KeyStore getInstance(File file, char[] password)
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1678
        throws KeyStoreException, IOException, NoSuchAlgorithmException,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1679
            CertificateException {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1680
        return getInstance(file, password, null, true);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1681
    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1682
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1683
    /**
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1684
     * Returns a loaded keystore object of the appropriate keystore type.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1685
     * First the keystore type is determined by probing the specified file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1686
     * Then a keystore object is instantiated and loaded using the data from
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1687
     * that file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1688
     * A {@code LoadStoreParameter} may be supplied which specifies how to
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1689
     * unlock the keystore data or perform an integrity check.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1690
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1691
     * <p>
28853
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1692
     * This method traverses the list of registered security {@linkplain
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1693
     * Provider providers}, starting with the most preferred Provider.
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1694
     * For each {@link KeyStoreSpi} implementation supported by a
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1695
     * Provider, it invokes the {@link
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1696
     * KeyStoreSpi#engineProbe(InputStream) engineProbe} method to
29df6c77d2b7 8069277: Doclint regression in java/security/KeyStore.java
darcy
parents: 28243
diff changeset
  1697
     * determine if it supports the specified keystore.
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1698
     * A new KeyStore object is returned that encapsulates the KeyStoreSpi
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1699
     * implementation from the first Provider that supports the specified file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1700
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1701
     * <p> Note that the list of registered providers may be retrieved via
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1702
     * the {@link Security#getProviders() Security.getProviders()} method.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1703
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1704
     * @param  file the keystore file
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1705
     * @param  param the {@code LoadStoreParameter} that specifies how to load
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1706
     *             the keystore, which may be {@code null}
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1707
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1708
     * @return a keystore object loaded with keystore data
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1709
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1710
     * @throws KeyStoreException if no Provider supports a KeyStoreSpi
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1711
     *             implementation for the specified keystore file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1712
     * @throws IOException if there is an I/O or format problem with the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1713
     *             keystore data. If the error is due to an incorrect
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1714
     *             {@code ProtectionParameter} (e.g. wrong password)
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1715
     *             the {@link Throwable#getCause cause} of the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1716
     *             {@code IOException} should be an
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1717
     *             {@code UnrecoverableKeyException}.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1718
     * @throws NoSuchAlgorithmException if the algorithm used to check the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1719
     *             integrity of the keystore cannot be found.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1720
     * @throws CertificateException if any of the certificates in the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1721
     *             keystore could not be loaded.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1722
     * @throws IllegalArgumentException if file does not exist or does not
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1723
     *             refer to a normal file, or if param is not recognized.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1724
     * @throws NullPointerException if file is {@code null}.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1725
     * @throws SecurityException if a security manager exists and its
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1726
     *             {@link java.lang.SecurityManager#checkRead} method denies
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1727
     *             read access to the specified file.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1728
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1729
     * @see Provider
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1730
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33241
diff changeset
  1731
     * @since 9
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1732
     */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1733
    public static final KeyStore getInstance(File file,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1734
        LoadStoreParameter param) throws KeyStoreException, IOException,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1735
            NoSuchAlgorithmException, CertificateException {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1736
        return getInstance(file, null, param, false);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1737
    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1738
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1739
    // Used by getInstance(File, char[]) & getInstance(File, LoadStoreParameter)
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1740
    private static final KeyStore getInstance(File file, char[] password,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1741
        LoadStoreParameter param, boolean hasPassword)
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1742
            throws KeyStoreException, IOException, NoSuchAlgorithmException,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1743
                CertificateException {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1744
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1745
        if (file == null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1746
            throw new NullPointerException();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1747
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1748
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1749
        if (file.isFile() == false) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1750
            throw new IllegalArgumentException(
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1751
                "File does not exist or it does not refer to a normal file: " +
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1752
                    file);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1753
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1754
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1755
        KeyStore keystore = null;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1756
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1757
        try (DataInputStream dataStream =
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1758
            new DataInputStream(
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1759
                new BufferedInputStream(
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1760
                    new FileInputStream(file)))) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1761
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1762
            dataStream.mark(Integer.MAX_VALUE);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1763
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1764
            // Detect the keystore type
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1765
            for (String type : Security.getAlgorithms("KeyStore")) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1766
                Object[] objs = null;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1767
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1768
                try {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1769
                    objs = Security.getImpl(type, "KeyStore", (String)null);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1770
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1771
                    KeyStoreSpi impl = (KeyStoreSpi)objs[0];
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1772
                    if (impl.engineProbe(dataStream)) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1773
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1774
                        if (kdebug != null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1775
                            kdebug.println(type + " keystore detected: " +
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1776
                                file);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1777
                        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1778
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1779
                        keystore = new KeyStore(impl, (Provider)objs[1], type);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1780
                        break;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1781
                    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1782
                } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1783
                    // ignore
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1784
                    if (kdebug != null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1785
                        kdebug.println(type + " not found - " + e);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1786
                    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1787
                } catch (IOException e) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1788
                    // ignore
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1789
                    if (kdebug != null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1790
                        kdebug.println("I/O error in " + file + " - " + e);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1791
                    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1792
                }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1793
                dataStream.reset(); // prepare the stream for the next probe
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1794
            }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1795
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1796
            // Load the keystore data
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1797
            if (keystore != null) {
48700
953eca1167b6 8194307: KeyStore#getInstance with custom LoadStoreParameter succeeds with invalid password
mullan
parents: 47216
diff changeset
  1798
                dataStream.reset(); // prepare the stream for loading
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1799
                if (hasPassword) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1800
                    keystore.load(dataStream, password);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1801
                } else {
48700
953eca1167b6 8194307: KeyStore#getInstance with custom LoadStoreParameter succeeds with invalid password
mullan
parents: 47216
diff changeset
  1802
                    keystore.keyStoreSpi.engineLoad(dataStream, param);
51131
9502e3b9d415 8207318: KeyStore#getInstance(File, LoadStoreParameter) does not load the keystore
weijun
parents: 48700
diff changeset
  1803
                    keystore.initialized = true;
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1804
                }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1805
                return keystore;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1806
            }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1807
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1808
52996
2457d862a646 8076190: Customizing the generation of a PKCS12 keystore
weijun
parents: 52689
diff changeset
  1809
        throw new KeyStoreException("Unrecognized keystore format. "
2457d862a646 8076190: Customizing the generation of a PKCS12 keystore
weijun
parents: 52689
diff changeset
  1810
                + "Please load it with a specified type");
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1811
    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1812
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1813
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * A description of a to-be-instantiated KeyStore object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * <p>An instance of this class encapsulates the information needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * instantiate and initialize a KeyStore object. That process is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * triggered when the {@linkplain #getKeyStore} method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * <p>This makes it possible to decouple configuration from KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * object creation and e.g. delay a password prompt until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * @see KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * @see javax.net.ssl.KeyStoreBuilderParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32413
diff changeset
  1828
    public abstract static class Builder {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        // maximum times to try the callbackhandler if the password is wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        static final int MAX_CALLBACK_TRIES = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
         * Construct a new Builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        protected Builder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
         * Returns the KeyStore described by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
         *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18592
diff changeset
  1843
         * @return the {@code KeyStore} described by this object
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20743
diff changeset
  1844
         * @exception KeyStoreException if an error occurred during the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
         *   operation, for example if the KeyStore could not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
         *   instantiated or loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        public abstract KeyStore getKeyStore() throws KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
         * Returns the ProtectionParameters that should be used to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
         * the {@link KeyStore.Entry Entry} with the given alias.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1853
         * The {@code getKeyStore} method must be invoked before this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
         * method may be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
         * @return the ProtectionParameters that should be used to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
         *   the {@link KeyStore.Entry Entry} with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
         * @param alias the alias of the KeyStore entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
         * @throws NullPointerException if alias is null
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20743
diff changeset
  1860
         * @throws KeyStoreException if an error occurred during the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
         *   operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
         * @throws IllegalStateException if the getKeyStore method has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
         *   not been invoked prior to calling this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        public abstract ProtectionParameter getProtectionParameter(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            throws KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
         * Returns a new Builder that encapsulates the given KeyStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
         * The {@linkplain #getKeyStore} method of the returned object
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1871
         * will return {@code keyStore}, the {@linkplain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
         * #getProtectionParameter getProtectionParameter()} method will
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1873
         * return {@code protectionParameters}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
         * <p> This is useful if an existing KeyStore object needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
         * used with Builder-based APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
         * @return a new Builder object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
         * @param keyStore the KeyStore to be encapsulated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
         * @param protectionParameter the ProtectionParameter used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         *   protect the KeyStore entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
         * @throws NullPointerException if keyStore or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
         *   protectionParameters is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
         * @throws IllegalArgumentException if the keyStore has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
         *   initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        public static Builder newInstance(final KeyStore keyStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                final ProtectionParameter protectionParameter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            if ((keyStore == null) || (protectionParameter == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
            if (keyStore.initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                throw new IllegalArgumentException("KeyStore not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            return new Builder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                private volatile boolean getCalled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                public KeyStore getKeyStore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                    getCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                    return keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                public ProtectionParameter getProtectionParameter(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                    if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                        throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                    if (getCalled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                        throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                            ("getKeyStore() must be called first");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                    return protectionParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
         * Returns a new Builder object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
         * <p>The first call to the {@link #getKeyStore} method on the returned
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1921
         * builder will create a KeyStore of type {@code type} and call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         * its {@link KeyStore#load load()} method.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1923
         * The {@code inputStream} argument is constructed from
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1924
         * {@code file}.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1925
         * If {@code protection} is a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1926
         * {@code PasswordProtection}, the password is obtained by
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1927
         * calling the {@code getPassword} method.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1928
         * Otherwise, if {@code protection} is a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1929
         * {@code CallbackHandlerProtection}, the password is obtained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
         * by invoking the CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
         * <p>Subsequent calls to {@link #getKeyStore} return the same object
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1933
         * as the initial call. If the initial call failed with a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         * KeyStoreException, subsequent calls also throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
         * KeyStoreException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1937
         * <p>The KeyStore is instantiated from {@code provider} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
         * non-null. Otherwise, all installed providers are searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
         * <p>Calls to {@link #getProtectionParameter getProtectionParameter()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
         * will return a {@link KeyStore.PasswordProtection PasswordProtection}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
         * object encapsulating the password that was used to invoke the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  1943
         * {@code load} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
         * <p><em>Note</em> that the {@link #getKeyStore} method is executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
         * within the {@link AccessControlContext} of the code invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
         * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
         * @return a new Builder object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
         * @param type the type of KeyStore to be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         * @param provider the provider from which the KeyStore is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         *   be instantiated (or null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         * @param file the File that contains the KeyStore data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         * @param protection the ProtectionParameter securing the KeyStore data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         * @throws NullPointerException if type, file or protection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         * @throws IllegalArgumentException if protection is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
         *   of either PasswordProtection or CallbackHandlerProtection; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
         *   if file does not exist or does not refer to a normal file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        public static Builder newInstance(String type, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                File file, ProtectionParameter protection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            if ((type == null) || (file == null) || (protection == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            if ((protection instanceof PasswordProtection == false) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                (protection instanceof CallbackHandlerProtection == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                ("Protection must be PasswordProtection or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                 "CallbackHandlerProtection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
            if (file.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                    ("File does not exist or it does not refer " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                     "to a normal file: " + file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            return new FileBuilder(type, provider, file, protection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                AccessController.getContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1980
        /**
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1981
         * Returns a new Builder object.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1982
         *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1983
         * <p>The first call to the {@link #getKeyStore} method on the returned
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1984
         * builder will create a KeyStore using {@code file} to detect the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1985
         * keystore type and then call its {@link KeyStore#load load()} method.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1986
         * It uses the same algorithm to determine the keystore type as
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1987
         * described in {@link KeyStore#getInstance(File, LoadStoreParameter)}.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1988
         * The {@code inputStream} argument is constructed from {@code file}.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1989
         * If {@code protection} is a {@code PasswordProtection}, the password
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1990
         * is obtained by calling the {@code getPassword} method.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1991
         * Otherwise, if {@code protection} is a
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1992
         * {@code CallbackHandlerProtection},
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1993
         * the password is obtained by invoking the CallbackHandler.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1994
         *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1995
         * <p>Subsequent calls to {@link #getKeyStore} return the same object
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1996
         * as the initial call. If the initial call failed with a
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1997
         * KeyStoreException, subsequent calls also throw a KeyStoreException.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1998
         *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  1999
         * <p>Calls to {@link #getProtectionParameter getProtectionParameter()}
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2000
         * will return a {@link KeyStore.PasswordProtection PasswordProtection}
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2001
         * object encapsulating the password that was used to invoke the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2002
         * {@code load} method.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2003
         *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2004
         * <p><em>Note</em> that the {@link #getKeyStore} method is executed
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2005
         * within the {@link AccessControlContext} of the code invoking this
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2006
         * method.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2007
         *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2008
         * @return a new Builder object
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2009
         * @param file the File that contains the KeyStore data
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2010
         * @param protection the ProtectionParameter securing the KeyStore data
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2011
         * @throws NullPointerException if file or protection is null
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2012
         * @throws IllegalArgumentException if protection is not an instance
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2013
         *   of either PasswordProtection or CallbackHandlerProtection; or
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2014
         *   if file does not exist or does not refer to a normal file
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2015
         *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33241
diff changeset
  2016
         * @since 9
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2017
         */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2018
        public static Builder newInstance(File file,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2019
            ProtectionParameter protection) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2020
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2021
            return newInstance("", null, file, protection);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2022
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2023
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        private static final class FileBuilder extends Builder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            private final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            private final Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            private final File file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            private ProtectionParameter protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            private ProtectionParameter keyProtection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            private final AccessControlContext context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            private KeyStore keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            private Throwable oldException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            FileBuilder(String type, Provider provider, File file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                    ProtectionParameter protection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                    AccessControlContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                this.file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                this.protection = protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            public synchronized KeyStore getKeyStore() throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                if (keyStore != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                    return keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                if (oldException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                    throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                        ("Previous KeyStore instantiation failed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                         oldException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                PrivilegedExceptionAction<KeyStore> action =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                        new PrivilegedExceptionAction<KeyStore>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                    public KeyStore run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                        if (protection instanceof CallbackHandlerProtection == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                            return run0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                        // when using a CallbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                        // reprompt if the password is wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                        int tries = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                            tries++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                                return run0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                                if ((tries < MAX_CALLBACK_TRIES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                                        && (e.getCause() instanceof UnrecoverableKeyException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                    public KeyStore run0() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                        KeyStore ks;
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2081
                        char[] password = null;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2082
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2083
                        // Acquire keystore password
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2084
                        if (protection instanceof PasswordProtection) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2085
                            password =
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2086
                                ((PasswordProtection)protection).getPassword();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2087
                            keyProtection = protection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                        } else {
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2089
                            CallbackHandler handler =
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2090
                                ((CallbackHandlerProtection)protection)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                                    .getCallbackHandler();
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2092
                            PasswordCallback callback = new PasswordCallback
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2093
                                ("Password for keystore " + file.getName(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                                    false);
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2095
                            handler.handle(new Callback[] {callback});
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2096
                            password = callback.getPassword();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2097
                            if (password == null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2098
                                throw new KeyStoreException("No password" +
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2099
                                                            " provided");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                            }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2101
                            callback.clearPassword();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2102
                            keyProtection = new PasswordProtection(password);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2103
                        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2104
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2105
                        if (type.isEmpty()) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2106
                            // Instantiate keystore and load keystore data
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2107
                            ks = KeyStore.getInstance(file, password);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2108
                        } else {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2109
                            // Instantiate keystore
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2110
                            if (provider == null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2111
                                ks = KeyStore.getInstance(type);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2112
                            } else {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2113
                                ks = KeyStore.getInstance(type, provider);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2114
                            }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2115
                            // Load keystore data
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2116
                            try (InputStream in = new FileInputStream(file)) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2117
                                ks.load(in, password);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                        }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27771
diff changeset
  2120
                        return ks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                    keyStore = AccessController.doPrivileged(action, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                    return keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                    oldException = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                    throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                        ("KeyStore instantiation failed", oldException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            public synchronized ProtectionParameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                        getProtectionParameter(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                    throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                if (keyStore == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                    throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                        ("getKeyStore() must be called first");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                return keyProtection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
         * Returns a new Builder object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
         * <p>Each call to the {@link #getKeyStore} method on the returned
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  2150
         * builder will return a new KeyStore object of type {@code type}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
         * Its {@link KeyStore#load(KeyStore.LoadStoreParameter) load()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
         * method is invoked using a
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  2153
         * {@code LoadStoreParameter} that encapsulates
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  2154
         * {@code protection}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
         *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  2156
         * <p>The KeyStore is instantiated from {@code provider} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
         * non-null. Otherwise, all installed providers are searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
         * <p>Calls to {@link #getProtectionParameter getProtectionParameter()}
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18274
diff changeset
  2160
         * will return {@code protection}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
         * <p><em>Note</em> that the {@link #getKeyStore} method is executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
         * within the {@link AccessControlContext} of the code invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
         * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
         * @return a new Builder object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
         * @param type the type of KeyStore to be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
         * @param provider the provider from which the KeyStore is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
         *   be instantiated (or null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
         * @param protection the ProtectionParameter securing the Keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
         * @throws NullPointerException if type or protection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        public static Builder newInstance(final String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                final Provider provider, final ProtectionParameter protection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
            if ((type == null) || (protection == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            final AccessControlContext context = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
            return new Builder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                private volatile boolean getCalled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                private IOException oldException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                private final PrivilegedExceptionAction<KeyStore> action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                        = new PrivilegedExceptionAction<KeyStore>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                    public KeyStore run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                            ks = KeyStore.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                            ks = KeyStore.getInstance(type, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                        LoadStoreParameter param = new SimpleLoadStoreParameter(protection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                        if (protection instanceof CallbackHandlerProtection == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                            ks.load(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                            // when using a CallbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                            // reprompt if the password is wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                            int tries = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                                tries++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                                    ks.load(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                                    if (e.getCause() instanceof UnrecoverableKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                                        if (tries < MAX_CALLBACK_TRIES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                                            oldException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                        getCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                public synchronized KeyStore getKeyStore()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                        throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                    if (oldException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                        throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                            ("Previous KeyStore instantiation failed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                             oldException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                    try {
18204
ccbc76c1d229 8009067: Improve storing keys in KeyStore
vinnie
parents: 15664
diff changeset
  2230
                        return AccessController.doPrivileged(action, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                    } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                        Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                        throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                            ("KeyStore instantiation failed", cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                public ProtectionParameter getProtectionParameter(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                    if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                        throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                    if (getCalled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                        throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                            ("getKeyStore() must be called first");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                    return protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    static class SimpleLoadStoreParameter implements LoadStoreParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        private final ProtectionParameter protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        SimpleLoadStoreParameter(ProtectionParameter protection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            this.protection = protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        public ProtectionParameter getProtectionParameter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            return protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
}