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