jdk/src/share/classes/java/security/KeyStore.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8152 94e5966bdf22
child 10114 d35f0b7bda65
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8152
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class represents a storage facility for cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * keys and certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p> A <code>KeyStore</code> manages different types of entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Each type of entry implements the <code>KeyStore.Entry</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Three basic <code>KeyStore.Entry</code> implementations are provided:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <li><b>KeyStore.PrivateKeyEntry</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> This type of entry holds a cryptographic <code>PrivateKey</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * which is optionally stored in a protected format to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * unauthorized access.  It is also accompanied by a certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * for the corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p> Private keys and certificate chains are used by a given entity for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * self-authentication. Applications for this authentication include software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * distribution organizations which sign JAR files as part of releasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * and/or licensing software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <li><b>KeyStore.SecretKeyEntry</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p> This type of entry holds a cryptographic <code>SecretKey</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * which is optionally stored in a protected format to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * unauthorized access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <li><b>KeyStore.TrustedCertificateEntry</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> This type of entry contains a single public key <code>Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * belonging to another party. It is called a <i>trusted certificate</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * because the keystore owner trusts that the public key in the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * indeed belongs to the identity identified by the <i>subject</i> (owner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>This type of entry can be used to authenticate other parties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p> Each entry in a keystore is identified by an "alias" string. In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * case of private keys and their associated certificate chains, these strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * distinguish among the different ways in which the entity may authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * itself. For example, the entity may authenticate itself using different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * certificate authorities, or using different public key algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p> Whether aliases are case sensitive is implementation dependent. In order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * to avoid problems, it is recommended not to use aliases in a KeyStore that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * only differ in case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> Whether keystores are persistent, and the mechanisms used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * keystore if it is persistent, are not specified here. This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * use of a variety of techniques for protecting sensitive (e.g., private or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * secret) keys. Smart cards or other integrated cryptographic engines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * (SafeKeyper) are one option, and simpler mechanisms such as files may also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * be used (in a variety of formats).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p> Typical ways to request a KeyStore object include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * relying on the default type and providing a specific keystore type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <li>To rely on the default type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * The system will return a keystore implementation for the default type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <li>To provide a specific keystore type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * The system will return the most preferred implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * specified keystore type available in the environment. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <p> Before a keystore can be accessed, it must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * {@link #load(java.io.InputStream, char[]) loaded}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *    // get user password and file input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *    char[] password = getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *    java.io.FileInputStream fis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *        fis = new java.io.FileInputStream("keyStoreName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *        ks.load(fis, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *        if (fis != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *            fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * To create an empty keystore using the above <code>load</code> method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * pass <code>null</code> as the <code>InputStream</code> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p> Once the keystore has been loaded, it is possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * to read existing entries from the keystore, or to write new entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * into the keystore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <pre>
6293
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   134
 *    KeyStore.ProtectionParameter protParam =
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   135
 *        new KeyStore.PasswordProtection(password);
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   136
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *    // get my private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *    KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
6293
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   139
 *        ks.getEntry("privateKeyAlias", protParam);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *    PrivateKey myPrivateKey = pkEntry.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *    // save my secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *    javax.crypto.SecretKey mySecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *    KeyStore.SecretKeyEntry skEntry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *        new KeyStore.SecretKeyEntry(mySecretKey);
6293
796f1f45fcb8 6653372: Error in java.security.KeyStore example code
mullan
parents: 5506
diff changeset
   146
 *    ks.setEntry("secretKeyAlias", skEntry, protParam);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *    // store away the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *    java.io.FileOutputStream fos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *        fos = new java.io.FileOutputStream("newKeyStoreName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *        ks.store(fos, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *        if (fos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *            fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * Note that although the same password may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * load the keystore, to protect the private key entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * to protect the secret key entry, and to store the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * (as is shown in the sample code above),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * different passwords or other protection parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * may also be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   167
 * <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
   168
 * the following standard <code>KeyStore</code> type:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   169
 * <ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   170
 * <li><tt>PKCS12</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   171
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   172
 * This type is described in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   173
 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   174
 * KeyStore section</a> of the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   175
 * Java Cryptography Architecture Standard Algorithm Name Documentation.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   176
 * 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
   177
 * other types are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   178
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * @see java.security.PrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * @see javax.crypto.SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * @see java.security.cert.Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
public class KeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Constant to lookup in the Security properties file to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * the default keystore type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * In the Security properties file, the default keystore type is given as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * keystore.type=jks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static final String KEYSTORE_TYPE = "keystore.type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    // The keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    // The provider implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private KeyStoreSpi keyStoreSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // Has this keystore been initialized (loaded)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * A marker interface for <code>KeyStore</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * {@link #load(KeyStore.LoadStoreParameter) load}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * {@link #store(KeyStore.LoadStoreParameter) store}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static interface LoadStoreParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * Gets the parameter used to protect keystore data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * @return the parameter used to protect keystore data, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        public ProtectionParameter getProtectionParameter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * A marker interface for keystore protection parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p> The information stored in a <code>ProtectionParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * object protects the contents of a keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * For example, protection parameters may be used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the integrity of keystore data, or to protect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * confidentiality of sensitive keystore data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * (such as a <code>PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public static interface ProtectionParameter { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * A password-based implementation of <code>ProtectionParameter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public static class PasswordProtection implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                ProtectionParameter, javax.security.auth.Destroyable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        private final char[] password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        private volatile boolean destroyed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         * Creates a password parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         * <p> The specified <code>password</code> is cloned before it is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * in the new <code>PasswordProtection</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * @param password the password, which may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        public PasswordProtection(char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            this.password = (password == null) ? null : password.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * Gets the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * <p>Note that this method returns a reference to the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * If a clone of the array is created it is the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * responsibility to zero out the password information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * after it is no longer needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * @see #destroy()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         * @return the password, which may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * @exception IllegalStateException if the password has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         *              been cleared (destroyed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        public synchronized char[] getPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (destroyed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                throw new IllegalStateException("password has been cleared");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * Clears the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * @exception DestroyFailedException if this method was unable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         *      to clear the password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        public synchronized void destroy()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                throws javax.security.auth.DestroyFailedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            destroyed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                Arrays.fill(password, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * Determines if password has been cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         * @return true if the password has been cleared, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        public synchronized boolean isDestroyed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return destroyed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * A ProtectionParameter encapsulating a CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public static class CallbackHandlerProtection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            implements ProtectionParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        private final CallbackHandler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * Constructs a new CallbackHandlerProtection from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * @param handler the CallbackHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * @exception NullPointerException if handler is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public CallbackHandlerProtection(CallbackHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                throw new NullPointerException("handler must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            this.handler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * Returns the CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * @return the CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        public CallbackHandler getCallbackHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * A marker interface for <code>KeyStore</code> entry types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public static interface Entry { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * A <code>KeyStore</code> entry that holds a <code>PrivateKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * and corresponding certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public static final class PrivateKeyEntry implements Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        private final PrivateKey privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        private final Certificate[] chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * Constructs a <code>PrivateKeyEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * <code>PrivateKey</code> and corresponding certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * <p> The specified <code>chain</code> is cloned before it is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         * in the new <code>PrivateKeyEntry</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         * @param privateKey the <code>PrivateKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         * @param chain an array of <code>Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         *      representing the certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         *      The chain must be ordered and contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         *      <code>Certificate</code> at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         *      corresponding to the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         * @exception NullPointerException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         *      <code>privateKey</code> or <code>chain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         *      is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * @exception IllegalArgumentException if the specified chain has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         *      length of 0, if the specified chain does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         *      <code>Certificate</code>s of the same type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         *      or if the <code>PrivateKey</code> algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         *      does not match the algorithm of the <code>PublicKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         *      in the end entity <code>Certificate</code> (at index 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        public PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (privateKey == null || chain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (chain.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                ("invalid zero-length input chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            Certificate[] clonedChain = chain.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            String certType = clonedChain[0].getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            for (int i = 1; i < clonedChain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (!certType.equals(clonedChain[i].getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                ("chain does not contain certificates " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                "of the same type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (!privateKey.getAlgorithm().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        (clonedChain[0].getPublicKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                ("private key algorithm does not match " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                "algorithm of public key in end entity " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                "certificate (at index 0)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            this.privKey = privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (clonedChain[0] instanceof X509Certificate &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                !(clonedChain instanceof X509Certificate[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                this.chain = new X509Certificate[clonedChain.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                System.arraycopy(clonedChain, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                this.chain, 0, clonedChain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                this.chain = clonedChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * Gets the <code>PrivateKey</code> from this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * @return the <code>PrivateKey</code> from this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        public PrivateKey getPrivateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * Gets the <code>Certificate</code> chain from this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * <p> The stored chain is cloned before being returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * @return an array of <code>Certificate</code>s corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         *      to the certificate chain for the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         *      If the certificates are of type X.509,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         *      the runtime type of the returned array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         *      <code>X509Certificate[]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        public Certificate[] getCertificateChain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return chain.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * Gets the end entity <code>Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * from the certificate chain in this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * @return the end entity <code>Certificate</code> (at index 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         *      from the certificate chain in this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         *      If the certificate is of type X.509,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         *      the runtime type of the returned certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         *      <code>X509Certificate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        public Certificate getCertificate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            return chain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * Returns a string representation of this PrivateKeyEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         * @return a string representation of this PrivateKeyEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            sb.append("Private key entry and certificate chain with "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                + chain.length + " elements:\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            for (Certificate cert : chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                sb.append(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                sb.append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * A <code>KeyStore</code> entry that holds a <code>SecretKey</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public static final class SecretKeyEntry implements Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        private final SecretKey sKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * Constructs a <code>SecretKeyEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * <code>SecretKey</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * @param secretKey the <code>SecretKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * @exception NullPointerException if <code>secretKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         *      is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        public SecretKeyEntry(SecretKey secretKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (secretKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            this.sKey = secretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * Gets the <code>SecretKey</code> from this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * @return the <code>SecretKey</code> from this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        public SecretKey getSecretKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return sKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * Returns a string representation of this SecretKeyEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * @return a string representation of this SecretKeyEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            return "Secret key entry with algorithm " + sKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * A <code>KeyStore</code> entry that holds a trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * <code>Certificate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public static final class TrustedCertificateEntry implements Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        private final Certificate cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         * Constructs a <code>TrustedCertificateEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         * trusted <code>Certificate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
         * @param trustedCert the trusted <code>Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         * @exception NullPointerException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         *      <code>trustedCert</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        public TrustedCertificateEntry(Certificate trustedCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (trustedCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            this.cert = trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         * Gets the trusted <code>Certficate</code> from this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
         * @return the trusted <code>Certificate</code> from this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        public Certificate getTrustedCertificate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            return cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * Returns a string representation of this TrustedCertificateEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * @return a string representation of this TrustedCertificateEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return "Trusted certificate entry:\r\n" + cert.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Creates a KeyStore object of the given type, and encapsulates the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * provider implementation (SPI object) in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param keyStoreSpi the provider implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param type the keystore type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        this.keyStoreSpi = keyStoreSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Returns a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * A new KeyStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * KeyStoreSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Provider that supports the specified type is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param type the type of keystore.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   596
     * See the KeyStore section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   597
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   598
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * for information about standard keystore types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @return a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @exception KeyStoreException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *          KeyStoreSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *          specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public static KeyStore getInstance(String type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            Object[] objs = Security.getImpl(type, "KeyStore", (String)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            throw new KeyStoreException(type + " not found", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        } catch (NoSuchProviderException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            throw new KeyStoreException(type + " not found", nspe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Returns a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <p> A new KeyStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * KeyStoreSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param type the type of keystore.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   634
     * See the KeyStore section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   635
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   636
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * for information about standard keystore types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @return a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @exception KeyStoreException if a KeyStoreSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *          implementation for the specified type is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @exception IllegalArgumentException if the provider name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *          or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public static KeyStore getInstance(String type, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        throws KeyStoreException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (provider == null || provider.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            Object[] objs = Security.getImpl(type, "KeyStore", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            throw new KeyStoreException(type + " not found", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * Returns a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <p> A new KeyStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * KeyStoreSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @param type the type of keystore.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   677
     * See the KeyStore section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   678
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 6293
diff changeset
   679
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * for information about standard keystore types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @return a keystore object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @exception KeyStoreException if KeyStoreSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *          implementation for the specified type is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @exception IllegalArgumentException if the specified provider is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public static KeyStore getInstance(String type, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (provider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            Object[] objs = Security.getImpl(type, "KeyStore", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            throw new KeyStoreException(type + " not found", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Returns the default keystore type as specified in the Java security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * properties file, or the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * &quot;jks&quot; (acronym for &quot;Java keystore&quot;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * if no such property exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * The Java security properties file is located in the file named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * &lt;JAVA_HOME&gt;/lib/security/java.security.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * &lt;JAVA_HOME&gt; refers to the value of the java.home system property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * and specifies the directory where the JRE is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <p>The default keystore type can be used by applications that do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * want to use a hard-coded keystore type when calling one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <code>getInstance</code> methods, and want to provide a default keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * type in case a user does not specify its own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * <p>The default keystore type can be changed by setting the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * "keystore.type" security property (in the Java security properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * file) to the desired keystore type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @return the default keystore type as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * Java security properties file, or the string &quot;jks&quot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * if no such property exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    public final static String getDefaultType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        String kstype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        kstype = AccessController.doPrivileged(new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                return Security.getProperty(KEYSTORE_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (kstype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            kstype = "jks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        return kstype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * Returns the provider of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @return the provider of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    public final Provider getProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Returns the type of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @return the type of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    public final String getType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        return this.type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * Returns the key associated with the given alias, using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * password to recover it.  The key must have been associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * the alias by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * or by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @param password the password for recovering the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @return the requested key, or null if the given alias does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * or does not identify a key-related entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * key cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @exception UnrecoverableKeyException if the key cannot be recovered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * (e.g., the given password is wrong).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    public final Key getKey(String alias, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        throws KeyStoreException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        return keyStoreSpi.engineGetKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * The certificate chain must have been associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * or by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * <code>PrivateKeyEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @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
   805
     * followed by zero or more certificate authorities), or null if the given alias
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * does not exist or does not contain a certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public final Certificate[] getCertificateChain(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        return keyStoreSpi.engineGetCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * Returns the certificate associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * <p> If the given alias name identifies an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * created by a call to <code>setCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * <code>TrustedCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * then the trusted certificate contained in that entry is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * <p> If the given alias name identifies an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * created by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * <code>PrivateKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * then the first element of the certificate chain in that entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @return the certificate, or null if the given alias does not exist or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * does not contain a certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public final Certificate getCertificate(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        return keyStoreSpi.engineGetCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * Returns the creation date of the entry identified by the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @return the creation date of this entry, or null if the given alias does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    public final Date getCreationDate(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        return keyStoreSpi.engineGetCreationDate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Assigns the given key to the given alias, protecting it with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * <p>If the given key is of type <code>java.security.PrivateKey</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * it must be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @param key the key to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param password the password to protect the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * key (only required if the given key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * (loaded), the given key cannot be protected, or this operation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * for some other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    public final void setKeyEntry(String alias, Key key, char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if ((key instanceof PrivateKey) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            (chain == null || chain.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            throw new IllegalArgumentException("Private key must be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                                               + "accompanied by certificate "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                                               + "chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        keyStoreSpi.engineSetKeyEntry(alias, key, password, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * Assigns the given key (that has already been protected) to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * <p>If the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * <code>java.security.PrivateKey</code>, it must be accompanied by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * certificate chain certifying the corresponding public key. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * underlying keystore implementation is of type <code>jks</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * <code>key</code> must be encoded as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @param key the key (in protected format) to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *          key (only useful if the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *          <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * (loaded), or if this operation fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    public final void setKeyEntry(String alias, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        keyStoreSpi.engineSetKeyEntry(alias, key, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * Assigns the given trusted certificate to the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * <p> If the given alias identifies an existing entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * created by a call to <code>setCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * <code>TrustedCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * the trusted certificate in the existing entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * is overridden by the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @param cert the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @exception KeyStoreException if the keystore has not been initialized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * or the given alias already exists and does not identify an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * entry containing a trusted certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * or this operation fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    public final void setCertificateEntry(String alias, Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        keyStoreSpi.engineSetCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * Deletes the entry identified by the given alias from this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @exception KeyStoreException if the keystore has not been initialized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * or if the entry cannot be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    public final void deleteEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        keyStoreSpi.engineDeleteEntry(alias);
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
     * Lists all the alias names of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @return enumeration of the alias names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public final Enumeration<String> aliases()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        return keyStoreSpi.engineAliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * Checks if the given alias exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * @return true if the alias exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    public final boolean containsAlias(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        return keyStoreSpi.engineContainsAlias(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Retrieves the number of entries in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @return the number of entries in this keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public final int size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        return keyStoreSpi.engineSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * Returns true if the entry identified by the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * was created by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * <code>PrivateKeyEntry</code> or a <code>SecretKeyEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @param alias the alias for the keystore entry to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * key-related entry, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    public final boolean isKeyEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        return keyStoreSpi.engineIsKeyEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Returns true if the entry identified by the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * was created by a call to <code>setCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * <code>TrustedCertificateEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * @param alias the alias for the keystore entry to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @return true if the entry identified by the given alias contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * trusted certificate, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    public final boolean isCertificateEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        return keyStoreSpi.engineIsCertificateEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * Returns the (alias) name of the first keystore entry whose certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * matches the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * <p> This method attempts to match the given certificate with each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * keystore entry. If the entry being considered was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * created by a call to <code>setCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * <code>TrustedCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * then the given certificate is compared to that entry's certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * <p> If the entry being considered was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * created by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * <code>PrivateKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * then the given certificate is compared to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * element of that entry's certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @param cert the certificate to match with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * @return the alias name of the first entry with a matching certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * or null if no such entry exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    public final String getCertificateAlias(Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        return keyStoreSpi.engineGetCertificateAlias(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Stores this keystore to the given output stream, and protects its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * integrity with the given password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * @param stream the output stream to which this keystore is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * @param password the password to generate the keystore integrity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * @exception IOException if there was an I/O problem with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @exception NoSuchAlgorithmException if the appropriate data integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * algorithm could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * @exception CertificateException if any of the certificates included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * the keystore data could not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    public final void store(OutputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        throws KeyStoreException, IOException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        keyStoreSpi.engineStore(stream, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Stores this keystore using the given <code>LoadStoreParameter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * @param param the <code>LoadStoreParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *          that specifies how to store the keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     *          which may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * @exception IllegalArgumentException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *          <code>LoadStoreParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     *          input is not recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *          (loaded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @exception IOException if there was an I/O problem with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @exception NoSuchAlgorithmException if the appropriate data integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *          algorithm could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @exception CertificateException if any of the certificates included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *          the keystore data could not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    public final void store(LoadStoreParameter param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                throws KeyStoreException, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                NoSuchAlgorithmException, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        keyStoreSpi.engineStore(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * Loads this KeyStore from the given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * <p>A password may be given to unlock the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * (e.g. the keystore resides on a hardware token device),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * or to check the integrity of the keystore data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * If a password is not given for integrity checking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * then integrity checking is not performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * <p>In order to create an empty keystore, or if the keystore cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * be initialized from a stream, pass <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * as the <code>stream</code> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * <p> Note that if this keystore has already been loaded, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * reinitialized and loaded again from the given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * @param stream the input stream from which the keystore is loaded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * @param password the password used to check the integrity of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * the keystore, the password used to unlock the keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * keystore data, if a password is required but not given,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * or if the given password was incorrect. If the error is due to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * wrong password, the {@link Throwable#getCause cause} of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * <code>IOException</code> should be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <code>UnrecoverableKeyException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    public final void load(InputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        keyStoreSpi.engineLoad(stream, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * Loads this keystore using the given <code>LoadStoreParameter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * <p> Note that if this KeyStore has already been loaded, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * reinitialized and loaded again from the given parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * @param param the <code>LoadStoreParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *          that specifies how to load the keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *          which may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * @exception IllegalArgumentException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *          <code>LoadStoreParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *          input is not recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *          keystore data. If the error is due to an incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *         <code>ProtectionParameter</code> (e.g. wrong password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *         the {@link Throwable#getCause cause} of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *         <code>IOException</code> should be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *         <code>UnrecoverableKeyException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *          the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *          keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    public final void load(LoadStoreParameter param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                throws IOException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        keyStoreSpi.engineLoad(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * Gets a keystore <code>Entry</code> for the specified alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * with the specified protection parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @param alias get the keystore <code>Entry</code> for this alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * @param protParam the <code>ProtectionParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *          used to protect the <code>Entry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     *          which may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * @return the keystore <code>Entry</code> for the specified alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *          or <code>null</code> if there is no such entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @exception NullPointerException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *          <code>alias</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     *          entry cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @exception UnrecoverableEntryException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *          <code>protParam</code> were insufficient or invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @exception UnrecoverableKeyException if the entry is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *          <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *          and the specified <code>protParam</code> does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *          the information needed to recover the key (e.g. wrong password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *          (loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @see #setEntry(String, KeyStore.Entry, KeyStore.ProtectionParameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    public final Entry getEntry(String alias, ProtectionParameter protParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                throws NoSuchAlgorithmException, UnrecoverableEntryException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        return keyStoreSpi.engineGetEntry(alias, protParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * Saves a keystore <code>Entry</code> under the specified alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * The protection parameter is used to protect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * <code>Entry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * <p> If an entry already exists for the specified alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * it is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @param alias save the keystore <code>Entry</code> under this alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * @param entry the <code>Entry</code> to save
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * @param protParam the <code>ProtectionParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     *          used to protect the <code>Entry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *          which may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * @exception NullPointerException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     *          <code>alias</code> or <code>entry</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     *          is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * @exception KeyStoreException if the keystore has not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *          (loaded), or if this operation fails for some other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * @see #getEntry(String, KeyStore.ProtectionParameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    public final void setEntry(String alias, Entry entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                        ProtectionParameter protParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        if (alias == null || entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        keyStoreSpi.engineSetEntry(alias, entry, protParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * Determines if the keystore <code>Entry</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * <code>alias</code> is an instance or subclass of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * <code>entryClass</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @param entryClass the entry class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @return true if the keystore <code>Entry</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     *          <code>alias</code> is an instance or subclass of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     *          specified <code>entryClass</code>, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * @exception NullPointerException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     *          <code>alias</code> or <code>entryClass</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     *          is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * @exception KeyStoreException if the keystore has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     *          initialized (loaded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    public final boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        entryInstanceOf(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                        Class<? extends KeyStore.Entry> entryClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        if (alias == null || entryClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            throw new NullPointerException("invalid null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            throw new KeyStoreException("Uninitialized keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        return keyStoreSpi.engineEntryInstanceOf(alias, entryClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * A description of a to-be-instantiated KeyStore object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * <p>An instance of this class encapsulates the information needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * instantiate and initialize a KeyStore object. That process is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * triggered when the {@linkplain #getKeyStore} method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * <p>This makes it possible to decouple configuration from KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * object creation and e.g. delay a password prompt until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @see KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @see javax.net.ssl.KeyStoreBuilderParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    public static abstract class Builder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        // maximum times to try the callbackhandler if the password is wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        static final int MAX_CALLBACK_TRIES = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
         * Construct a new Builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        protected Builder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
         * Returns the KeyStore described by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
         * @exception KeyStoreException if an error occured during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
         *   operation, for example if the KeyStore could not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
         *   instantiated or loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        public abstract KeyStore getKeyStore() throws KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         * Returns the ProtectionParameters that should be used to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
         * the {@link KeyStore.Entry Entry} with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
         * The <code>getKeyStore</code> method must be invoked before this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
         * method may be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
         * @return the ProtectionParameters that should be used to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
         *   the {@link KeyStore.Entry Entry} with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
         * @param alias the alias of the KeyStore entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
         * @throws NullPointerException if alias is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
         * @throws KeyStoreException if an error occured during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
         *   operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
         * @throws IllegalStateException if the getKeyStore method has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
         *   not been invoked prior to calling this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        public abstract ProtectionParameter getProtectionParameter(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            throws KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
         * Returns a new Builder that encapsulates the given KeyStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
         * The {@linkplain #getKeyStore} method of the returned object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
         * will return <code>keyStore</code>, the {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
         * #getProtectionParameter getProtectionParameter()} method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         * return <code>protectionParameters</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
         * <p> This is useful if an existing KeyStore object needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
         * used with Builder-based APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         * @return a new Builder object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
         * @param keyStore the KeyStore to be encapsulated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
         * @param protectionParameter the ProtectionParameter used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
         *   protect the KeyStore entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
         * @throws NullPointerException if keyStore or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
         *   protectionParameters is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
         * @throws IllegalArgumentException if the keyStore has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
         *   initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        public static Builder newInstance(final KeyStore keyStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                final ProtectionParameter protectionParameter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            if ((keyStore == null) || (protectionParameter == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            if (keyStore.initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                throw new IllegalArgumentException("KeyStore not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            return new Builder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                private volatile boolean getCalled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                public KeyStore getKeyStore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                    getCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                    return keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                public ProtectionParameter getProtectionParameter(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                    if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                        throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                    if (getCalled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                        throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                            ("getKeyStore() must be called first");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    return protectionParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
         * Returns a new Builder object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
         * <p>The first call to the {@link #getKeyStore} method on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
         * builder will create a KeyStore of type <code>type</code> and call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
         * its {@link KeyStore#load load()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
         * The <code>inputStream</code> argument is constructed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
         * <code>file</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
         * If <code>protection</code> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
         * <code>PasswordProtection</code>, the password is obtained by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
         * calling the <code>getPassword</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
         * Otherwise, if <code>protection</code> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
         * <code>CallbackHandlerProtection</code>, the password is obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
         * by invoking the CallbackHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
         * <p>Subsequent calls to {@link #getKeyStore} return the same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
         * as the initial call. If the initial call to failed with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
         * KeyStoreException, subsequent calls also throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
         * KeyStoreException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
         * <p>The KeyStore is instantiated from <code>provider</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
         * non-null. Otherwise, all installed providers are searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
         * <p>Calls to {@link #getProtectionParameter getProtectionParameter()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
         * will return a {@link KeyStore.PasswordProtection PasswordProtection}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
         * object encapsulating the password that was used to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         * <code>load</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
         * <p><em>Note</em> that the {@link #getKeyStore} method is executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
         * within the {@link AccessControlContext} of the code invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
         * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
         * @return a new Builder object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
         * @param type the type of KeyStore to be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
         * @param provider the provider from which the KeyStore is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
         *   be instantiated (or null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
         * @param file the File that contains the KeyStore data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
         * @param protection the ProtectionParameter securing the KeyStore data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
         * @throws NullPointerException if type, file or protection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
         * @throws IllegalArgumentException if protection is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
         *   of either PasswordProtection or CallbackHandlerProtection; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
         *   if file does not exist or does not refer to a normal file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        public static Builder newInstance(String type, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                File file, ProtectionParameter protection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            if ((type == null) || (file == null) || (protection == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            if ((protection instanceof PasswordProtection == false) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                (protection instanceof CallbackHandlerProtection == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                ("Protection must be PasswordProtection or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                 "CallbackHandlerProtection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            if (file.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                    ("File does not exist or it does not refer " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                     "to a normal file: " + file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            return new FileBuilder(type, provider, file, protection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                AccessController.getContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        private static final class FileBuilder extends Builder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            private final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            private final Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            private final File file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            private ProtectionParameter protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            private ProtectionParameter keyProtection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            private final AccessControlContext context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            private KeyStore keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            private Throwable oldException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            FileBuilder(String type, Provider provider, File file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    ProtectionParameter protection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    AccessControlContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                this.file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                this.protection = protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            public synchronized KeyStore getKeyStore() throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                if (keyStore != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    return keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                if (oldException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                    throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                        ("Previous KeyStore instantiation failed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                         oldException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                PrivilegedExceptionAction<KeyStore> action =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                        new PrivilegedExceptionAction<KeyStore>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                    public KeyStore run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                        if (protection instanceof CallbackHandlerProtection == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                            return run0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                        // when using a CallbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                        // reprompt if the password is wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                        int tries = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                            tries++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                                return run0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                                if ((tries < MAX_CALLBACK_TRIES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                                        && (e.getCause() instanceof UnrecoverableKeyException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                    public KeyStore run0() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                            ks = KeyStore.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                            ks = KeyStore.getInstance(type, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                        char[] password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                            in = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                            if (protection instanceof PasswordProtection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                                password =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                                ((PasswordProtection)protection).getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                                keyProtection = protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                                CallbackHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                                    ((CallbackHandlerProtection)protection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                                    .getCallbackHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                                PasswordCallback callback = new PasswordCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                                    ("Password for keystore " + file.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                                    false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                                handler.handle(new Callback[] {callback});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                                password = callback.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                                if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                                    throw new KeyStoreException("No password" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                                                                " provided");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                                callback.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                                keyProtection = new PasswordProtection(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                            ks.load(in, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                            return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                    keyStore = AccessController.doPrivileged(action, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                    return keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                    oldException = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                    throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                        ("KeyStore instantiation failed", oldException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            public synchronized ProtectionParameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                        getProtectionParameter(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                    throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                if (keyStore == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                    throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                        ("getKeyStore() must be called first");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                return keyProtection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
         * Returns a new Builder object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
         * <p>Each call to the {@link #getKeyStore} method on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
         * builder will return a new KeyStore object of type <code>type</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
         * Its {@link KeyStore#load(KeyStore.LoadStoreParameter) load()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
         * method is invoked using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
         * <code>LoadStoreParameter</code> that encapsulates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
         * <code>protection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
         * <p>The KeyStore is instantiated from <code>provider</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         * non-null. Otherwise, all installed providers are searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         * <p>Calls to {@link #getProtectionParameter getProtectionParameter()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         * will return <code>protection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
         * <p><em>Note</em> that the {@link #getKeyStore} method is executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
         * within the {@link AccessControlContext} of the code invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
         * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
         * @return a new Builder object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
         * @param type the type of KeyStore to be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
         * @param provider the provider from which the KeyStore is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
         *   be instantiated (or null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
         * @param protection the ProtectionParameter securing the Keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         * @throws NullPointerException if type or protection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        public static Builder newInstance(final String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                final Provider provider, final ProtectionParameter protection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            if ((type == null) || (protection == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            final AccessControlContext context = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            return new Builder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                private volatile boolean getCalled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                private IOException oldException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                private final PrivilegedExceptionAction<KeyStore> action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                        = new PrivilegedExceptionAction<KeyStore>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                    public KeyStore run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                            ks = KeyStore.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                            ks = KeyStore.getInstance(type, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                        LoadStoreParameter param = new SimpleLoadStoreParameter(protection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                        if (protection instanceof CallbackHandlerProtection == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                            ks.load(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                            // when using a CallbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                            // reprompt if the password is wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                            int tries = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                                tries++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                                    ks.load(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                                    if (e.getCause() instanceof UnrecoverableKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                                        if (tries < MAX_CALLBACK_TRIES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                                            oldException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                        getCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                public synchronized KeyStore getKeyStore()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                        throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                    if (oldException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                        throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                            ("Previous KeyStore instantiation failed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                             oldException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                        return AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                    } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                        Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                        throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                            ("KeyStore instantiation failed", cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                public ProtectionParameter getProtectionParameter(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                    if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                        throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                    if (getCalled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                        throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                            ("getKeyStore() must be called first");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                    return protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        }
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
    static class SimpleLoadStoreParameter implements LoadStoreParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        private final ProtectionParameter protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        SimpleLoadStoreParameter(ProtectionParameter protection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            this.protection = protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        public ProtectionParameter getProtectionParameter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            return protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
}