9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.security;
27
28 import java.io.*;
29 import java.net.URI;
30 import java.security.cert.Certificate;
31 import java.security.cert.X509Certificate;
32 import java.security.cert.CertificateException;
33 import java.security.spec.AlgorithmParameterSpec;
34 import java.util.*;
35 import javax.crypto.SecretKey;
36
37 import javax.security.auth.DestroyFailedException;
38 import javax.security.auth.callback.*;
39
40 import sun.security.util.Debug;
41
42 /**
43 * This class represents a storage facility for cryptographic
44 * keys and certificates.
45 *
46 * <p> A {@code KeyStore} manages different types of entries.
47 * Each type of entry implements the {@code KeyStore.Entry} interface.
48 * Three basic {@code KeyStore.Entry} implementations are provided:
49 *
838 * KeyStoreSpi implementation from the first
839 * Provider that supports the specified type is returned.
840 *
841 * <p> Note that the list of registered providers may be retrieved via
842 * the {@link Security#getProviders() Security.getProviders()} method.
843 *
844 * @implNote
845 * The JDK Reference Implementation additionally uses the
846 * {@code jdk.security.provider.preferred}
847 * {@link Security#getProperty(String) Security} property to determine
848 * the preferred provider order for the specified algorithm. This
849 * may be different than the order of providers returned by
850 * {@link Security#getProviders() Security.getProviders()}.
851 *
852 * @param type the type of keystore.
853 * See the KeyStore section in the <a href=
854 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
855 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
856 * for information about standard keystore types.
857 *
858 * @return a keystore object of the specified type.
859 *
860 * @exception KeyStoreException if no Provider supports a
861 * KeyStoreSpi implementation for the
862 * specified type.
863 *
864 * @see Provider
865 */
866 public static KeyStore getInstance(String type)
867 throws KeyStoreException
868 {
869 try {
870 Object[] objs = Security.getImpl(type, "KeyStore", (String)null);
871 return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
872 } catch (NoSuchAlgorithmException nsae) {
873 throw new KeyStoreException(type + " not found", nsae);
874 } catch (NoSuchProviderException nspe) {
875 throw new KeyStoreException(type + " not found", nspe);
876 }
877 }
878
879 /**
880 * Returns a keystore object of the specified type.
881 *
882 * <p> A new KeyStore object encapsulating the
883 * KeyStoreSpi implementation from the specified provider
884 * is returned. The specified provider must be registered
885 * in the security provider list.
886 *
887 * <p> Note that the list of registered providers may be retrieved via
888 * the {@link Security#getProviders() Security.getProviders()} method.
889 *
890 * @param type the type of keystore.
891 * See the KeyStore section in the <a href=
892 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
893 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
894 * for information about standard keystore types.
895 *
896 * @param provider the name of the provider.
897 *
898 * @return a keystore object of the specified type.
899 *
900 * @exception KeyStoreException if a KeyStoreSpi
901 * implementation for the specified type is not
902 * available from the specified provider.
903 *
904 * @exception NoSuchProviderException if the specified provider is not
905 * registered in the security provider list.
906 *
907 * @exception IllegalArgumentException if the provider name is null
908 * or empty.
909 *
910 * @see Provider
911 */
912 public static KeyStore getInstance(String type, String provider)
913 throws KeyStoreException, NoSuchProviderException
914 {
915 if (provider == null || provider.length() == 0)
916 throw new IllegalArgumentException("missing provider");
917 try {
918 Object[] objs = Security.getImpl(type, "KeyStore", provider);
919 return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
920 } catch (NoSuchAlgorithmException nsae) {
921 throw new KeyStoreException(type + " not found", nsae);
922 }
923 }
924
925 /**
926 * Returns a keystore object of the specified type.
927 *
928 * <p> A new KeyStore object encapsulating the
929 * KeyStoreSpi implementation from the specified Provider
930 * object is returned. Note that the specified Provider object
931 * does not have to be registered in the provider list.
932 *
933 * @param type the type of keystore.
934 * See the KeyStore section in the <a href=
935 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
936 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
937 * for information about standard keystore types.
938 *
939 * @param provider the provider.
940 *
941 * @return a keystore object of the specified type.
942 *
943 * @exception KeyStoreException if KeyStoreSpi
944 * implementation for the specified type is not available
945 * from the specified Provider object.
946 *
947 * @exception IllegalArgumentException if the specified provider is null.
948 *
949 * @see Provider
950 *
951 * @since 1.4
952 */
953 public static KeyStore getInstance(String type, Provider provider)
954 throws KeyStoreException
955 {
956 if (provider == null)
957 throw new IllegalArgumentException("missing provider");
958 try {
959 Object[] objs = Security.getImpl(type, "KeyStore", provider);
960 return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
961 } catch (NoSuchAlgorithmException nsae) {
962 throw new KeyStoreException(type + " not found", nsae);
963 }
964 }
965
966 /**
967 * Returns the default keystore type as specified by the
968 * {@code keystore.type} security property, or the string
969 * {@literal "jks"} (acronym for {@literal "Java keystore"})
970 * if no such property exists.
971 *
972 * <p>The default keystore type can be used by applications that do not
973 * want to use a hard-coded keystore type when calling one of the
974 * {@code getInstance} methods, and want to provide a default keystore
975 * type in case a user does not specify its own.
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.security;
27
28 import java.io.*;
29 import java.security.cert.Certificate;
30 import java.security.cert.X509Certificate;
31 import java.security.cert.CertificateException;
32 import java.security.spec.AlgorithmParameterSpec;
33 import java.util.*;
34 import javax.crypto.SecretKey;
35
36 import javax.security.auth.DestroyFailedException;
37 import javax.security.auth.callback.*;
38
39 import sun.security.util.Debug;
40
41 /**
42 * This class represents a storage facility for cryptographic
43 * keys and certificates.
44 *
45 * <p> A {@code KeyStore} manages different types of entries.
46 * Each type of entry implements the {@code KeyStore.Entry} interface.
47 * Three basic {@code KeyStore.Entry} implementations are provided:
48 *
837 * KeyStoreSpi implementation from the first
838 * Provider that supports the specified type is returned.
839 *
840 * <p> Note that the list of registered providers may be retrieved via
841 * the {@link Security#getProviders() Security.getProviders()} method.
842 *
843 * @implNote
844 * The JDK Reference Implementation additionally uses the
845 * {@code jdk.security.provider.preferred}
846 * {@link Security#getProperty(String) Security} property to determine
847 * the preferred provider order for the specified algorithm. This
848 * may be different than the order of providers returned by
849 * {@link Security#getProviders() Security.getProviders()}.
850 *
851 * @param type the type of keystore.
852 * See the KeyStore section in the <a href=
853 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
854 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
855 * for information about standard keystore types.
856 *
857 * @return a keystore object of the specified type
858 *
859 * @throws KeyStoreException if no {@code Provider} supports a
860 * {@code KeyStoreSpi} implementation for the
861 * specified type
862 *
863 * @throws NullPointerException if {@code type} is {@code null}
864 *
865 * @see Provider
866 */
867 public static KeyStore getInstance(String type)
868 throws KeyStoreException
869 {
870 Objects.requireNonNull(type, "null type name");
871 try {
872 Object[] objs = Security.getImpl(type, "KeyStore", (String)null);
873 return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
874 } catch (NoSuchAlgorithmException nsae) {
875 throw new KeyStoreException(type + " not found", nsae);
876 } catch (NoSuchProviderException nspe) {
877 throw new KeyStoreException(type + " not found", nspe);
878 }
879 }
880
881 /**
882 * Returns a keystore object of the specified type.
883 *
884 * <p> A new KeyStore object encapsulating the
885 * KeyStoreSpi implementation from the specified provider
886 * is returned. The specified provider must be registered
887 * in the security provider list.
888 *
889 * <p> Note that the list of registered providers may be retrieved via
890 * the {@link Security#getProviders() Security.getProviders()} method.
891 *
892 * @param type the type of keystore.
893 * See the KeyStore section in the <a href=
894 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
895 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
896 * for information about standard keystore types.
897 *
898 * @param provider the name of the provider.
899 *
900 * @return a keystore object of the specified type
901 *
902 * @throws IllegalArgumentException if the provider name is {@code null}
903 * or empty
904 *
905 * @throws KeyStoreException if a {@code KeyStoreSpi}
906 * implementation for the specified type is not
907 * available from the specified provider
908 *
909 * @throws NoSuchProviderException if the specified provider is not
910 * registered in the security provider list
911 *
912 * @throws NullPointerException if {@code type} is {@code null}
913 *
914 * @see Provider
915 */
916 public static KeyStore getInstance(String type, String provider)
917 throws KeyStoreException, NoSuchProviderException
918 {
919 Objects.requireNonNull(type, "null type name");
920 if (provider == null || provider.length() == 0)
921 throw new IllegalArgumentException("missing provider");
922 try {
923 Object[] objs = Security.getImpl(type, "KeyStore", provider);
924 return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
925 } catch (NoSuchAlgorithmException nsae) {
926 throw new KeyStoreException(type + " not found", nsae);
927 }
928 }
929
930 /**
931 * Returns a keystore object of the specified type.
932 *
933 * <p> A new KeyStore object encapsulating the
934 * KeyStoreSpi implementation from the specified Provider
935 * object is returned. Note that the specified Provider object
936 * does not have to be registered in the provider list.
937 *
938 * @param type the type of keystore.
939 * See the KeyStore section in the <a href=
940 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
941 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
942 * for information about standard keystore types.
943 *
944 * @param provider the provider.
945 *
946 * @return a keystore object of the specified type
947 *
948 * @throws IllegalArgumentException if the specified provider is
949 * {@code null}
950 *
951 * @throws KeyStoreException if {@code KeyStoreSpi}
952 * implementation for the specified type is not available
953 * from the specified {@code Provider} object
954 *
955 * @throws NullPointerException if {@code type} is {@code null}
956 *
957 * @see Provider
958 *
959 * @since 1.4
960 */
961 public static KeyStore getInstance(String type, Provider provider)
962 throws KeyStoreException
963 {
964 Objects.requireNonNull(type, "null type name");
965 if (provider == null)
966 throw new IllegalArgumentException("missing provider");
967 try {
968 Object[] objs = Security.getImpl(type, "KeyStore", provider);
969 return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
970 } catch (NoSuchAlgorithmException nsae) {
971 throw new KeyStoreException(type + " not found", nsae);
972 }
973 }
974
975 /**
976 * Returns the default keystore type as specified by the
977 * {@code keystore.type} security property, or the string
978 * {@literal "jks"} (acronym for {@literal "Java keystore"})
979 * if no such property exists.
980 *
981 * <p>The default keystore type can be used by applications that do not
982 * want to use a hard-coded keystore type when calling one of the
983 * {@code getInstance} methods, and want to provide a default keystore
984 * type in case a user does not specify its own.
|