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