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