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